mm: make wait_on_page_writeback() wait for multiple pending writebacks
[linux/fpc-iii.git] / sound / hda / hdac_bus.c
blob9766f6af87430db6068b812c7308f2767d8ad30b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HD-audio core bus driver
4 */
6 #include <linux/init.h>
7 #include <linux/io.h>
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <sound/hdaudio.h>
12 #include "local.h"
13 #include "trace.h"
15 static void snd_hdac_bus_process_unsol_events(struct work_struct *work);
17 static const struct hdac_bus_ops default_ops = {
18 .command = snd_hdac_bus_send_cmd,
19 .get_response = snd_hdac_bus_get_response,
22 /**
23 * snd_hdac_bus_init - initialize a HD-audio bas bus
24 * @bus: the pointer to bus object
25 * @dev: device pointer
26 * @ops: bus verb operators
28 * Returns 0 if successful, or a negative error code.
30 int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
31 const struct hdac_bus_ops *ops)
33 memset(bus, 0, sizeof(*bus));
34 bus->dev = dev;
35 if (ops)
36 bus->ops = ops;
37 else
38 bus->ops = &default_ops;
39 bus->dma_type = SNDRV_DMA_TYPE_DEV;
40 INIT_LIST_HEAD(&bus->stream_list);
41 INIT_LIST_HEAD(&bus->codec_list);
42 INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
43 spin_lock_init(&bus->reg_lock);
44 mutex_init(&bus->cmd_mutex);
45 mutex_init(&bus->lock);
46 INIT_LIST_HEAD(&bus->hlink_list);
47 init_waitqueue_head(&bus->rirb_wq);
48 bus->irq = -1;
51 * Default value of '8' is as per the HD audio specification (Rev 1.0a).
52 * Following relation is used to derive STRIPE control value.
53 * For sample rate <= 48K:
54 * { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
55 * For sample rate > 48K:
56 * { ((num_channels * bits_per_sample * rate/48000) /
57 * number of SDOs) >= 8 }
59 bus->sdo_limit = 8;
61 return 0;
63 EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
65 /**
66 * snd_hdac_bus_exit - clean up a HD-audio bas bus
67 * @bus: the pointer to bus object
69 void snd_hdac_bus_exit(struct hdac_bus *bus)
71 WARN_ON(!list_empty(&bus->stream_list));
72 WARN_ON(!list_empty(&bus->codec_list));
73 cancel_work_sync(&bus->unsol_work);
75 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
77 /**
78 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
79 * @bus: bus object
80 * @addr: the HDAC device address
81 * @cmd: HD-audio encoded verb
82 * @res: pointer to store the response, NULL if performing asynchronously
84 * Returns 0 if successful, or a negative error code.
86 int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
87 unsigned int cmd, unsigned int *res)
89 int err;
91 mutex_lock(&bus->cmd_mutex);
92 err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
93 mutex_unlock(&bus->cmd_mutex);
94 return err;
97 /**
98 * snd_hdac_bus_exec_verb_unlocked - unlocked version
99 * @bus: bus object
100 * @addr: the HDAC device address
101 * @cmd: HD-audio encoded verb
102 * @res: pointer to store the response, NULL if performing asynchronously
104 * Returns 0 if successful, or a negative error code.
106 int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
107 unsigned int cmd, unsigned int *res)
109 unsigned int tmp;
110 int err;
112 if (cmd == ~0)
113 return -EINVAL;
115 if (res)
116 *res = -1;
117 else if (bus->sync_write)
118 res = &tmp;
119 for (;;) {
120 trace_hda_send_cmd(bus, cmd);
121 err = bus->ops->command(bus, cmd);
122 if (err != -EAGAIN)
123 break;
124 /* process pending verbs */
125 err = bus->ops->get_response(bus, addr, &tmp);
126 if (err)
127 break;
129 if (!err && res) {
130 err = bus->ops->get_response(bus, addr, res);
131 trace_hda_get_response(bus, addr, *res);
133 return err;
135 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
138 * snd_hdac_bus_queue_event - add an unsolicited event to queue
139 * @bus: the BUS
140 * @res: unsolicited event (lower 32bit of RIRB entry)
141 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
143 * Adds the given event to the queue. The events are processed in
144 * the workqueue asynchronously. Call this function in the interrupt
145 * hanlder when RIRB receives an unsolicited event.
147 void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
149 unsigned int wp;
151 if (!bus)
152 return;
154 trace_hda_unsol_event(bus, res, res_ex);
155 wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
156 bus->unsol_wp = wp;
158 wp <<= 1;
159 bus->unsol_queue[wp] = res;
160 bus->unsol_queue[wp + 1] = res_ex;
162 schedule_work(&bus->unsol_work);
166 * process queued unsolicited events
168 static void snd_hdac_bus_process_unsol_events(struct work_struct *work)
170 struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
171 struct hdac_device *codec;
172 struct hdac_driver *drv;
173 unsigned int rp, caddr, res;
175 spin_lock_irq(&bus->reg_lock);
176 while (bus->unsol_rp != bus->unsol_wp) {
177 rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
178 bus->unsol_rp = rp;
179 rp <<= 1;
180 res = bus->unsol_queue[rp];
181 caddr = bus->unsol_queue[rp + 1];
182 if (!(caddr & (1 << 4))) /* no unsolicited event? */
183 continue;
184 codec = bus->caddr_tbl[caddr & 0x0f];
185 if (!codec || !codec->dev.driver)
186 continue;
187 spin_unlock_irq(&bus->reg_lock);
188 drv = drv_to_hdac_driver(codec->dev.driver);
189 if (drv->unsol_event)
190 drv->unsol_event(codec, res);
191 spin_lock_irq(&bus->reg_lock);
193 spin_unlock_irq(&bus->reg_lock);
197 * snd_hdac_bus_add_device - Add a codec to bus
198 * @bus: HDA core bus
199 * @codec: HDA core device to add
201 * Adds the given codec to the list in the bus. The caddr_tbl array
202 * and codec_powered bits are updated, as well.
203 * Returns zero if success, or a negative error code.
205 int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
207 if (bus->caddr_tbl[codec->addr]) {
208 dev_err(bus->dev, "address 0x%x is already occupied\n",
209 codec->addr);
210 return -EBUSY;
213 list_add_tail(&codec->list, &bus->codec_list);
214 bus->caddr_tbl[codec->addr] = codec;
215 set_bit(codec->addr, &bus->codec_powered);
216 bus->num_codecs++;
217 return 0;
221 * snd_hdac_bus_remove_device - Remove a codec from bus
222 * @bus: HDA core bus
223 * @codec: HDA core device to remove
225 void snd_hdac_bus_remove_device(struct hdac_bus *bus,
226 struct hdac_device *codec)
228 WARN_ON(bus != codec->bus);
229 if (list_empty(&codec->list))
230 return;
231 list_del_init(&codec->list);
232 bus->caddr_tbl[codec->addr] = NULL;
233 clear_bit(codec->addr, &bus->codec_powered);
234 bus->num_codecs--;
235 flush_work(&bus->unsol_work);
238 #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
239 /* Helpers for aligned read/write of mmio space, for Tegra */
240 unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
242 void __iomem *aligned_addr =
243 (void __iomem *)((unsigned long)(addr) & ~0x3);
244 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
245 unsigned int v;
247 v = readl(aligned_addr);
248 return (v >> shift) & mask;
250 EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
252 void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
253 unsigned int mask)
255 void __iomem *aligned_addr =
256 (void __iomem *)((unsigned long)(addr) & ~0x3);
257 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
258 unsigned int v;
260 v = readl(aligned_addr);
261 v &= ~(mask << shift);
262 v |= val << shift;
263 writel(v, aligned_addr);
265 EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
266 #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */