xref: /openbmc/linux/sound/hda/hdac_bus.c (revision e7255c00)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d068ebc2STakashi Iwai /*
3d068ebc2STakashi Iwai  * HD-audio core bus driver
4d068ebc2STakashi Iwai  */
5d068ebc2STakashi Iwai 
6d068ebc2STakashi Iwai #include <linux/init.h>
7fe401066SStephen Rothwell #include <linux/io.h>
8d068ebc2STakashi Iwai #include <linux/device.h>
9d068ebc2STakashi Iwai #include <linux/module.h>
10d068ebc2STakashi Iwai #include <linux/export.h>
11d068ebc2STakashi Iwai #include <sound/hdaudio.h>
1253eff75eSTakashi Iwai #include "local.h"
13e311782aSTakashi Iwai #include "trace.h"
14d068ebc2STakashi Iwai 
15ddf7cb83STakashi Iwai static void snd_hdac_bus_process_unsol_events(struct work_struct *work);
16ddf7cb83STakashi Iwai 
1714752412STakashi Iwai static const struct hdac_bus_ops default_ops = {
1814752412STakashi Iwai 	.command = snd_hdac_bus_send_cmd,
1914752412STakashi Iwai 	.get_response = snd_hdac_bus_get_response,
20f9e5fd1bSKai Vehmanen 	.link_power = snd_hdac_bus_link_power,
2114752412STakashi Iwai };
2214752412STakashi Iwai 
23d068ebc2STakashi Iwai /**
24d068ebc2STakashi Iwai  * snd_hdac_bus_init - initialize a HD-audio bas bus
25d068ebc2STakashi Iwai  * @bus: the pointer to bus object
266e57188fSKeyon Jie  * @dev: device pointer
2714752412STakashi Iwai  * @ops: bus verb operators
28d068ebc2STakashi Iwai  *
29d068ebc2STakashi Iwai  * Returns 0 if successful, or a negative error code.
30d068ebc2STakashi Iwai  */
snd_hdac_bus_init(struct hdac_bus * bus,struct device * dev,const struct hdac_bus_ops * ops)31d068ebc2STakashi Iwai int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
3219abfefdSTakashi Iwai 		      const struct hdac_bus_ops *ops)
33d068ebc2STakashi Iwai {
34d068ebc2STakashi Iwai 	memset(bus, 0, sizeof(*bus));
35d068ebc2STakashi Iwai 	bus->dev = dev;
3614752412STakashi Iwai 	if (ops)
37d068ebc2STakashi Iwai 		bus->ops = ops;
3814752412STakashi Iwai 	else
3914752412STakashi Iwai 		bus->ops = &default_ops;
40619a1f19STakashi Iwai 	bus->dma_type = SNDRV_DMA_TYPE_DEV;
4114752412STakashi Iwai 	INIT_LIST_HEAD(&bus->stream_list);
42d068ebc2STakashi Iwai 	INIT_LIST_HEAD(&bus->codec_list);
4318d43c9bSKeyon Jie 	INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
4414752412STakashi Iwai 	spin_lock_init(&bus->reg_lock);
45d068ebc2STakashi Iwai 	mutex_init(&bus->cmd_mutex);
46d7a181daSTakashi Iwai 	mutex_init(&bus->lock);
47e61ab9f0STakashi Iwai 	INIT_LIST_HEAD(&bus->hlink_list);
4888452da9STakashi Iwai 	init_waitqueue_head(&bus->rirb_wq);
4914752412STakashi Iwai 	bus->irq = -1;
50b90b925fSSameer Pujar 
51b90b925fSSameer Pujar 	/*
52b90b925fSSameer Pujar 	 * Default value of '8' is as per the HD audio specification (Rev 1.0a).
53b90b925fSSameer Pujar 	 * Following relation is used to derive STRIPE control value.
54b90b925fSSameer Pujar 	 *  For sample rate <= 48K:
55b90b925fSSameer Pujar 	 *   { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
56b90b925fSSameer Pujar 	 *  For sample rate > 48K:
57b90b925fSSameer Pujar 	 *   { ((num_channels * bits_per_sample * rate/48000) /
58b90b925fSSameer Pujar 	 *	number of SDOs) >= 8 }
59b90b925fSSameer Pujar 	 */
60b90b925fSSameer Pujar 	bus->sdo_limit = 8;
61b90b925fSSameer Pujar 
62d068ebc2STakashi Iwai 	return 0;
63d068ebc2STakashi Iwai }
64d068ebc2STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
65d068ebc2STakashi Iwai 
66d068ebc2STakashi Iwai /**
67d068ebc2STakashi Iwai  * snd_hdac_bus_exit - clean up a HD-audio bas bus
68d068ebc2STakashi Iwai  * @bus: the pointer to bus object
69d068ebc2STakashi Iwai  */
snd_hdac_bus_exit(struct hdac_bus * bus)70d068ebc2STakashi Iwai void snd_hdac_bus_exit(struct hdac_bus *bus)
71d068ebc2STakashi Iwai {
7214752412STakashi Iwai 	WARN_ON(!list_empty(&bus->stream_list));
73d068ebc2STakashi Iwai 	WARN_ON(!list_empty(&bus->codec_list));
74d068ebc2STakashi Iwai 	cancel_work_sync(&bus->unsol_work);
75d068ebc2STakashi Iwai }
76d068ebc2STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
77d068ebc2STakashi Iwai 
78d068ebc2STakashi Iwai /**
79d068ebc2STakashi Iwai  * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
80d068ebc2STakashi Iwai  * @bus: bus object
816e57188fSKeyon Jie  * @addr: the HDAC device address
82d068ebc2STakashi Iwai  * @cmd: HD-audio encoded verb
83d068ebc2STakashi Iwai  * @res: pointer to store the response, NULL if performing asynchronously
84d068ebc2STakashi Iwai  *
85d068ebc2STakashi Iwai  * Returns 0 if successful, or a negative error code.
86d068ebc2STakashi Iwai  */
snd_hdac_bus_exec_verb(struct hdac_bus * bus,unsigned int addr,unsigned int cmd,unsigned int * res)87d068ebc2STakashi Iwai int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
88d068ebc2STakashi Iwai 			   unsigned int cmd, unsigned int *res)
89d068ebc2STakashi Iwai {
90d068ebc2STakashi Iwai 	int err;
91d068ebc2STakashi Iwai 
92d068ebc2STakashi Iwai 	mutex_lock(&bus->cmd_mutex);
93d068ebc2STakashi Iwai 	err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
94d068ebc2STakashi Iwai 	mutex_unlock(&bus->cmd_mutex);
95d068ebc2STakashi Iwai 	return err;
96d068ebc2STakashi Iwai }
97d068ebc2STakashi Iwai 
98d068ebc2STakashi Iwai /**
99d068ebc2STakashi Iwai  * snd_hdac_bus_exec_verb_unlocked - unlocked version
100d068ebc2STakashi Iwai  * @bus: bus object
1016e57188fSKeyon Jie  * @addr: the HDAC device address
102d068ebc2STakashi Iwai  * @cmd: HD-audio encoded verb
103d068ebc2STakashi Iwai  * @res: pointer to store the response, NULL if performing asynchronously
104d068ebc2STakashi Iwai  *
105d068ebc2STakashi Iwai  * Returns 0 if successful, or a negative error code.
106d068ebc2STakashi Iwai  */
snd_hdac_bus_exec_verb_unlocked(struct hdac_bus * bus,unsigned int addr,unsigned int cmd,unsigned int * res)107d068ebc2STakashi Iwai int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
108d068ebc2STakashi Iwai 				    unsigned int cmd, unsigned int *res)
109d068ebc2STakashi Iwai {
110d068ebc2STakashi Iwai 	unsigned int tmp;
111d068ebc2STakashi Iwai 	int err;
112d068ebc2STakashi Iwai 
113d068ebc2STakashi Iwai 	if (cmd == ~0)
114d068ebc2STakashi Iwai 		return -EINVAL;
115d068ebc2STakashi Iwai 
116d068ebc2STakashi Iwai 	if (res)
117d068ebc2STakashi Iwai 		*res = -1;
118d068ebc2STakashi Iwai 	else if (bus->sync_write)
119d068ebc2STakashi Iwai 		res = &tmp;
120d068ebc2STakashi Iwai 	for (;;) {
121e311782aSTakashi Iwai 		trace_hda_send_cmd(bus, cmd);
122d068ebc2STakashi Iwai 		err = bus->ops->command(bus, cmd);
123d068ebc2STakashi Iwai 		if (err != -EAGAIN)
124d068ebc2STakashi Iwai 			break;
125d068ebc2STakashi Iwai 		/* process pending verbs */
126d068ebc2STakashi Iwai 		err = bus->ops->get_response(bus, addr, &tmp);
127d068ebc2STakashi Iwai 		if (err)
128d068ebc2STakashi Iwai 			break;
129d068ebc2STakashi Iwai 	}
130e311782aSTakashi Iwai 	if (!err && res) {
131d068ebc2STakashi Iwai 		err = bus->ops->get_response(bus, addr, res);
132e311782aSTakashi Iwai 		trace_hda_get_response(bus, addr, *res);
133e311782aSTakashi Iwai 	}
134d068ebc2STakashi Iwai 	return err;
135d068ebc2STakashi Iwai }
136d068ebc2STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
137d068ebc2STakashi Iwai 
138d068ebc2STakashi Iwai /**
139d068ebc2STakashi Iwai  * snd_hdac_bus_queue_event - add an unsolicited event to queue
140d068ebc2STakashi Iwai  * @bus: the BUS
141d068ebc2STakashi Iwai  * @res: unsolicited event (lower 32bit of RIRB entry)
142d068ebc2STakashi Iwai  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
143d068ebc2STakashi Iwai  *
144d068ebc2STakashi Iwai  * Adds the given event to the queue.  The events are processed in
145d068ebc2STakashi Iwai  * the workqueue asynchronously.  Call this function in the interrupt
146d068ebc2STakashi Iwai  * hanlder when RIRB receives an unsolicited event.
147d068ebc2STakashi Iwai  */
snd_hdac_bus_queue_event(struct hdac_bus * bus,u32 res,u32 res_ex)148d068ebc2STakashi Iwai void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
149d068ebc2STakashi Iwai {
150d068ebc2STakashi Iwai 	unsigned int wp;
151d068ebc2STakashi Iwai 
152d068ebc2STakashi Iwai 	if (!bus)
153d068ebc2STakashi Iwai 		return;
154d068ebc2STakashi Iwai 
155e311782aSTakashi Iwai 	trace_hda_unsol_event(bus, res, res_ex);
156d068ebc2STakashi Iwai 	wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
157d068ebc2STakashi Iwai 	bus->unsol_wp = wp;
158d068ebc2STakashi Iwai 
159d068ebc2STakashi Iwai 	wp <<= 1;
160d068ebc2STakashi Iwai 	bus->unsol_queue[wp] = res;
161d068ebc2STakashi Iwai 	bus->unsol_queue[wp + 1] = res_ex;
162d068ebc2STakashi Iwai 
163d068ebc2STakashi Iwai 	schedule_work(&bus->unsol_work);
164d068ebc2STakashi Iwai }
165d068ebc2STakashi Iwai 
166d068ebc2STakashi Iwai /*
167d068ebc2STakashi Iwai  * process queued unsolicited events
168d068ebc2STakashi Iwai  */
snd_hdac_bus_process_unsol_events(struct work_struct * work)169ddf7cb83STakashi Iwai static void snd_hdac_bus_process_unsol_events(struct work_struct *work)
170d068ebc2STakashi Iwai {
171d068ebc2STakashi Iwai 	struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
172d068ebc2STakashi Iwai 	struct hdac_device *codec;
173d068ebc2STakashi Iwai 	struct hdac_driver *drv;
174d068ebc2STakashi Iwai 	unsigned int rp, caddr, res;
175d068ebc2STakashi Iwai 
176c637fa15STakashi Iwai 	spin_lock_irq(&bus->reg_lock);
177d068ebc2STakashi Iwai 	while (bus->unsol_rp != bus->unsol_wp) {
178d068ebc2STakashi Iwai 		rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
179d068ebc2STakashi Iwai 		bus->unsol_rp = rp;
180d068ebc2STakashi Iwai 		rp <<= 1;
181d068ebc2STakashi Iwai 		res = bus->unsol_queue[rp];
182d068ebc2STakashi Iwai 		caddr = bus->unsol_queue[rp + 1];
183d068ebc2STakashi Iwai 		if (!(caddr & (1 << 4))) /* no unsolicited event? */
184d068ebc2STakashi Iwai 			continue;
185d068ebc2STakashi Iwai 		codec = bus->caddr_tbl[caddr & 0x0f];
186*e7255c00SCezary Rojewski 		if (!codec || !codec->registered)
187d068ebc2STakashi Iwai 			continue;
188c637fa15STakashi Iwai 		spin_unlock_irq(&bus->reg_lock);
189d068ebc2STakashi Iwai 		drv = drv_to_hdac_driver(codec->dev.driver);
190d068ebc2STakashi Iwai 		if (drv->unsol_event)
191d068ebc2STakashi Iwai 			drv->unsol_event(codec, res);
192c637fa15STakashi Iwai 		spin_lock_irq(&bus->reg_lock);
193d068ebc2STakashi Iwai 	}
194c637fa15STakashi Iwai 	spin_unlock_irq(&bus->reg_lock);
195d068ebc2STakashi Iwai }
196d068ebc2STakashi Iwai 
19778dd5e21STakashi Iwai /**
19878dd5e21STakashi Iwai  * snd_hdac_bus_add_device - Add a codec to bus
19978dd5e21STakashi Iwai  * @bus: HDA core bus
20078dd5e21STakashi Iwai  * @codec: HDA core device to add
20178dd5e21STakashi Iwai  *
20278dd5e21STakashi Iwai  * Adds the given codec to the list in the bus.  The caddr_tbl array
20378dd5e21STakashi Iwai  * and codec_powered bits are updated, as well.
20478dd5e21STakashi Iwai  * Returns zero if success, or a negative error code.
20578dd5e21STakashi Iwai  */
snd_hdac_bus_add_device(struct hdac_bus * bus,struct hdac_device * codec)206d068ebc2STakashi Iwai int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
207d068ebc2STakashi Iwai {
208d068ebc2STakashi Iwai 	if (bus->caddr_tbl[codec->addr]) {
209d068ebc2STakashi Iwai 		dev_err(bus->dev, "address 0x%x is already occupied\n",
210d068ebc2STakashi Iwai 			codec->addr);
211d068ebc2STakashi Iwai 		return -EBUSY;
212d068ebc2STakashi Iwai 	}
213d068ebc2STakashi Iwai 
214d068ebc2STakashi Iwai 	list_add_tail(&codec->list, &bus->codec_list);
215d068ebc2STakashi Iwai 	bus->caddr_tbl[codec->addr] = codec;
216d068ebc2STakashi Iwai 	set_bit(codec->addr, &bus->codec_powered);
217d068ebc2STakashi Iwai 	bus->num_codecs++;
218d068ebc2STakashi Iwai 	return 0;
219d068ebc2STakashi Iwai }
220d068ebc2STakashi Iwai 
22178dd5e21STakashi Iwai /**
22278dd5e21STakashi Iwai  * snd_hdac_bus_remove_device - Remove a codec from bus
22378dd5e21STakashi Iwai  * @bus: HDA core bus
22478dd5e21STakashi Iwai  * @codec: HDA core device to remove
22578dd5e21STakashi Iwai  */
snd_hdac_bus_remove_device(struct hdac_bus * bus,struct hdac_device * codec)226d068ebc2STakashi Iwai void snd_hdac_bus_remove_device(struct hdac_bus *bus,
227d068ebc2STakashi Iwai 				struct hdac_device *codec)
228d068ebc2STakashi Iwai {
229d068ebc2STakashi Iwai 	WARN_ON(bus != codec->bus);
230d068ebc2STakashi Iwai 	if (list_empty(&codec->list))
231d068ebc2STakashi Iwai 		return;
232d068ebc2STakashi Iwai 	list_del_init(&codec->list);
233d068ebc2STakashi Iwai 	bus->caddr_tbl[codec->addr] = NULL;
234d068ebc2STakashi Iwai 	clear_bit(codec->addr, &bus->codec_powered);
235d068ebc2STakashi Iwai 	bus->num_codecs--;
236eb8d0eaaSTakashi Iwai 	flush_work(&bus->unsol_work);
237d068ebc2STakashi Iwai }
23819abfefdSTakashi Iwai 
23919abfefdSTakashi Iwai #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
24019abfefdSTakashi Iwai /* Helpers for aligned read/write of mmio space, for Tegra */
snd_hdac_aligned_read(void __iomem * addr,unsigned int mask)24119abfefdSTakashi Iwai unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
24219abfefdSTakashi Iwai {
24319abfefdSTakashi Iwai 	void __iomem *aligned_addr =
24419abfefdSTakashi Iwai 		(void __iomem *)((unsigned long)(addr) & ~0x3);
24519abfefdSTakashi Iwai 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
24619abfefdSTakashi Iwai 	unsigned int v;
24719abfefdSTakashi Iwai 
24819abfefdSTakashi Iwai 	v = readl(aligned_addr);
24919abfefdSTakashi Iwai 	return (v >> shift) & mask;
25019abfefdSTakashi Iwai }
25119abfefdSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
25219abfefdSTakashi Iwai 
snd_hdac_aligned_write(unsigned int val,void __iomem * addr,unsigned int mask)25319abfefdSTakashi Iwai void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
25419abfefdSTakashi Iwai 			    unsigned int mask)
25519abfefdSTakashi Iwai {
25619abfefdSTakashi Iwai 	void __iomem *aligned_addr =
25719abfefdSTakashi Iwai 		(void __iomem *)((unsigned long)(addr) & ~0x3);
25819abfefdSTakashi Iwai 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
25919abfefdSTakashi Iwai 	unsigned int v;
26019abfefdSTakashi Iwai 
26119abfefdSTakashi Iwai 	v = readl(aligned_addr);
26219abfefdSTakashi Iwai 	v &= ~(mask << shift);
26319abfefdSTakashi Iwai 	v |= val << shift;
26419abfefdSTakashi Iwai 	writel(v, aligned_addr);
26519abfefdSTakashi Iwai }
26619abfefdSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
26719abfefdSTakashi Iwai #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */
268f9e5fd1bSKai Vehmanen 
snd_hdac_codec_link_up(struct hdac_device * codec)269f9e5fd1bSKai Vehmanen void snd_hdac_codec_link_up(struct hdac_device *codec)
270f9e5fd1bSKai Vehmanen {
271f9e5fd1bSKai Vehmanen 	struct hdac_bus *bus = codec->bus;
272f9e5fd1bSKai Vehmanen 
273f9e5fd1bSKai Vehmanen 	if (bus->ops->link_power)
274f9e5fd1bSKai Vehmanen 		bus->ops->link_power(codec, true);
275f9e5fd1bSKai Vehmanen 	else
276f9e5fd1bSKai Vehmanen 		snd_hdac_bus_link_power(codec, true);
277f9e5fd1bSKai Vehmanen }
278f9e5fd1bSKai Vehmanen EXPORT_SYMBOL_GPL(snd_hdac_codec_link_up);
279f9e5fd1bSKai Vehmanen 
snd_hdac_codec_link_down(struct hdac_device * codec)280f9e5fd1bSKai Vehmanen void snd_hdac_codec_link_down(struct hdac_device *codec)
281f9e5fd1bSKai Vehmanen {
282f9e5fd1bSKai Vehmanen 	struct hdac_bus *bus = codec->bus;
283f9e5fd1bSKai Vehmanen 
284f9e5fd1bSKai Vehmanen 	if (bus->ops->link_power)
285f9e5fd1bSKai Vehmanen 		bus->ops->link_power(codec, false);
286f9e5fd1bSKai Vehmanen 	else
287f9e5fd1bSKai Vehmanen 		snd_hdac_bus_link_power(codec, false);
288f9e5fd1bSKai Vehmanen }
289f9e5fd1bSKai Vehmanen EXPORT_SYMBOL_GPL(snd_hdac_codec_link_down);
290