1 // SPDX-License-Identifier: GPL-2.0-only
3 * HD-audio core bus driver
6 #include <linux/init.h>
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <sound/hdaudio.h>
15 static const struct hdac_bus_ops default_ops = {
16 .command = snd_hdac_bus_send_cmd,
17 .get_response = snd_hdac_bus_get_response,
21 * snd_hdac_bus_init - initialize a HD-audio bas bus
22 * @bus: the pointer to bus object
23 * @ops: bus verb operators
25 * Returns 0 if successful, or a negative error code.
27 int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
28 const struct hdac_bus_ops *ops)
30 memset(bus, 0, sizeof(*bus));
35 bus->ops = &default_ops;
36 bus->dma_type = SNDRV_DMA_TYPE_DEV;
37 INIT_LIST_HEAD(&bus->stream_list);
38 INIT_LIST_HEAD(&bus->codec_list);
39 INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
40 spin_lock_init(&bus->reg_lock);
41 mutex_init(&bus->cmd_mutex);
42 mutex_init(&bus->lock);
43 INIT_LIST_HEAD(&bus->hlink_list);
47 EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
50 * snd_hdac_bus_exit - clean up a HD-audio bas bus
51 * @bus: the pointer to bus object
53 void snd_hdac_bus_exit(struct hdac_bus *bus)
55 WARN_ON(!list_empty(&bus->stream_list));
56 WARN_ON(!list_empty(&bus->codec_list));
57 cancel_work_sync(&bus->unsol_work);
59 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
62 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
64 * @cmd: HD-audio encoded verb
65 * @res: pointer to store the response, NULL if performing asynchronously
67 * Returns 0 if successful, or a negative error code.
69 int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
70 unsigned int cmd, unsigned int *res)
74 mutex_lock(&bus->cmd_mutex);
75 err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
76 mutex_unlock(&bus->cmd_mutex);
79 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
82 * snd_hdac_bus_exec_verb_unlocked - unlocked version
84 * @cmd: HD-audio encoded verb
85 * @res: pointer to store the response, NULL if performing asynchronously
87 * Returns 0 if successful, or a negative error code.
89 int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
90 unsigned int cmd, unsigned int *res)
100 else if (bus->sync_write)
103 trace_hda_send_cmd(bus, cmd);
104 err = bus->ops->command(bus, cmd);
107 /* process pending verbs */
108 err = bus->ops->get_response(bus, addr, &tmp);
113 err = bus->ops->get_response(bus, addr, res);
114 trace_hda_get_response(bus, addr, *res);
118 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
121 * snd_hdac_bus_queue_event - add an unsolicited event to queue
123 * @res: unsolicited event (lower 32bit of RIRB entry)
124 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
126 * Adds the given event to the queue. The events are processed in
127 * the workqueue asynchronously. Call this function in the interrupt
128 * hanlder when RIRB receives an unsolicited event.
130 void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
137 trace_hda_unsol_event(bus, res, res_ex);
138 wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
142 bus->unsol_queue[wp] = res;
143 bus->unsol_queue[wp + 1] = res_ex;
145 schedule_work(&bus->unsol_work);
147 EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
150 * process queued unsolicited events
152 void snd_hdac_bus_process_unsol_events(struct work_struct *work)
154 struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
155 struct hdac_device *codec;
156 struct hdac_driver *drv;
157 unsigned int rp, caddr, res;
159 while (bus->unsol_rp != bus->unsol_wp) {
160 rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
163 res = bus->unsol_queue[rp];
164 caddr = bus->unsol_queue[rp + 1];
165 if (!(caddr & (1 << 4))) /* no unsolicited event? */
167 codec = bus->caddr_tbl[caddr & 0x0f];
168 if (!codec || !codec->dev.driver)
170 drv = drv_to_hdac_driver(codec->dev.driver);
171 if (drv->unsol_event)
172 drv->unsol_event(codec, res);
175 EXPORT_SYMBOL_GPL(snd_hdac_bus_process_unsol_events);
178 * snd_hdac_bus_add_device - Add a codec to bus
180 * @codec: HDA core device to add
182 * Adds the given codec to the list in the bus. The caddr_tbl array
183 * and codec_powered bits are updated, as well.
184 * Returns zero if success, or a negative error code.
186 int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
188 if (bus->caddr_tbl[codec->addr]) {
189 dev_err(bus->dev, "address 0x%x is already occupied\n",
194 list_add_tail(&codec->list, &bus->codec_list);
195 bus->caddr_tbl[codec->addr] = codec;
196 set_bit(codec->addr, &bus->codec_powered);
202 * snd_hdac_bus_remove_device - Remove a codec from bus
204 * @codec: HDA core device to remove
206 void snd_hdac_bus_remove_device(struct hdac_bus *bus,
207 struct hdac_device *codec)
209 WARN_ON(bus != codec->bus);
210 if (list_empty(&codec->list))
212 list_del_init(&codec->list);
213 bus->caddr_tbl[codec->addr] = NULL;
214 clear_bit(codec->addr, &bus->codec_powered);
216 flush_work(&bus->unsol_work);
219 #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
220 /* Helpers for aligned read/write of mmio space, for Tegra */
221 unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
223 void __iomem *aligned_addr =
224 (void __iomem *)((unsigned long)(addr) & ~0x3);
225 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
228 v = readl(aligned_addr);
229 return (v >> shift) & mask;
231 EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
233 void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
236 void __iomem *aligned_addr =
237 (void __iomem *)((unsigned long)(addr) & ~0x3);
238 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
241 v = readl(aligned_addr);
242 v &= ~(mask << shift);
244 writel(v, aligned_addr);
246 EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
247 #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */