blob: dc2523ef7d98b5938fc6a6a7385c5234e884bc55 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Takashi Iwaid068ebc2015-03-02 23:22:59 +01002/*
3 * HD-audio core bus driver
4 */
5
6#include <linux/init.h>
7#include <linux/device.h>
8#include <linux/module.h>
9#include <linux/export.h>
10#include <sound/hdaudio.h>
Takashi Iwaie3117822015-03-10 15:16:28 +010011#include "trace.h"
Takashi Iwaid068ebc2015-03-02 23:22:59 +010012
Takashi Iwai14752412015-04-14 12:15:47 +020013static const struct hdac_bus_ops default_ops = {
14 .command = snd_hdac_bus_send_cmd,
15 .get_response = snd_hdac_bus_get_response,
16};
17
Takashi Iwaid068ebc2015-03-02 23:22:59 +010018/**
19 * snd_hdac_bus_init - initialize a HD-audio bas bus
20 * @bus: the pointer to bus object
Takashi Iwai14752412015-04-14 12:15:47 +020021 * @ops: bus verb operators
Takashi Iwaid068ebc2015-03-02 23:22:59 +010022 *
23 * Returns 0 if successful, or a negative error code.
24 */
25int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
Takashi Iwai19abfef2019-08-07 20:32:08 +020026 const struct hdac_bus_ops *ops)
Takashi Iwaid068ebc2015-03-02 23:22:59 +010027{
28 memset(bus, 0, sizeof(*bus));
29 bus->dev = dev;
Takashi Iwai14752412015-04-14 12:15:47 +020030 if (ops)
31 bus->ops = ops;
32 else
33 bus->ops = &default_ops;
Takashi Iwai619a1f12019-08-07 20:02:31 +020034 bus->dma_type = SNDRV_DMA_TYPE_DEV;
Takashi Iwai14752412015-04-14 12:15:47 +020035 INIT_LIST_HEAD(&bus->stream_list);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010036 INIT_LIST_HEAD(&bus->codec_list);
Keyon Jie18d43c92018-12-11 15:30:27 -060037 INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
Takashi Iwai14752412015-04-14 12:15:47 +020038 spin_lock_init(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010039 mutex_init(&bus->cmd_mutex);
Takashi Iwaid7a181d2019-04-10 12:49:55 +020040 mutex_init(&bus->lock);
Takashi Iwaie61ab9f2019-04-10 16:00:54 +020041 INIT_LIST_HEAD(&bus->hlink_list);
Takashi Iwai14752412015-04-14 12:15:47 +020042 bus->irq = -1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010043 return 0;
44}
45EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
46
47/**
48 * snd_hdac_bus_exit - clean up a HD-audio bas bus
49 * @bus: the pointer to bus object
50 */
51void snd_hdac_bus_exit(struct hdac_bus *bus)
52{
Takashi Iwai14752412015-04-14 12:15:47 +020053 WARN_ON(!list_empty(&bus->stream_list));
Takashi Iwaid068ebc2015-03-02 23:22:59 +010054 WARN_ON(!list_empty(&bus->codec_list));
55 cancel_work_sync(&bus->unsol_work);
56}
57EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
58
59/**
60 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
61 * @bus: bus object
62 * @cmd: HD-audio encoded verb
63 * @res: pointer to store the response, NULL if performing asynchronously
64 *
65 * Returns 0 if successful, or a negative error code.
66 */
67int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
68 unsigned int cmd, unsigned int *res)
69{
70 int err;
71
72 mutex_lock(&bus->cmd_mutex);
73 err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
74 mutex_unlock(&bus->cmd_mutex);
75 return err;
76}
77EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
78
79/**
80 * snd_hdac_bus_exec_verb_unlocked - unlocked version
81 * @bus: bus object
82 * @cmd: HD-audio encoded verb
83 * @res: pointer to store the response, NULL if performing asynchronously
84 *
85 * Returns 0 if successful, or a negative error code.
86 */
87int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
88 unsigned int cmd, unsigned int *res)
89{
90 unsigned int tmp;
91 int err;
92
93 if (cmd == ~0)
94 return -EINVAL;
95
96 if (res)
97 *res = -1;
98 else if (bus->sync_write)
99 res = &tmp;
100 for (;;) {
Takashi Iwaie3117822015-03-10 15:16:28 +0100101 trace_hda_send_cmd(bus, cmd);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100102 err = bus->ops->command(bus, cmd);
103 if (err != -EAGAIN)
104 break;
105 /* process pending verbs */
106 err = bus->ops->get_response(bus, addr, &tmp);
107 if (err)
108 break;
109 }
Takashi Iwaie3117822015-03-10 15:16:28 +0100110 if (!err && res) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100111 err = bus->ops->get_response(bus, addr, res);
Takashi Iwaie3117822015-03-10 15:16:28 +0100112 trace_hda_get_response(bus, addr, *res);
113 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100114 return err;
115}
116EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
117
118/**
119 * snd_hdac_bus_queue_event - add an unsolicited event to queue
120 * @bus: the BUS
121 * @res: unsolicited event (lower 32bit of RIRB entry)
122 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
123 *
124 * Adds the given event to the queue. The events are processed in
125 * the workqueue asynchronously. Call this function in the interrupt
126 * hanlder when RIRB receives an unsolicited event.
127 */
128void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
129{
130 unsigned int wp;
131
132 if (!bus)
133 return;
134
Takashi Iwaie3117822015-03-10 15:16:28 +0100135 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100136 wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
137 bus->unsol_wp = wp;
138
139 wp <<= 1;
140 bus->unsol_queue[wp] = res;
141 bus->unsol_queue[wp + 1] = res_ex;
142
143 schedule_work(&bus->unsol_work);
144}
145EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
146
147/*
148 * process queued unsolicited events
149 */
Keyon Jie18d43c92018-12-11 15:30:27 -0600150void snd_hdac_bus_process_unsol_events(struct work_struct *work)
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100151{
152 struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
153 struct hdac_device *codec;
154 struct hdac_driver *drv;
155 unsigned int rp, caddr, res;
156
157 while (bus->unsol_rp != bus->unsol_wp) {
158 rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
159 bus->unsol_rp = rp;
160 rp <<= 1;
161 res = bus->unsol_queue[rp];
162 caddr = bus->unsol_queue[rp + 1];
163 if (!(caddr & (1 << 4))) /* no unsolicited event? */
164 continue;
165 codec = bus->caddr_tbl[caddr & 0x0f];
166 if (!codec || !codec->dev.driver)
167 continue;
168 drv = drv_to_hdac_driver(codec->dev.driver);
169 if (drv->unsol_event)
170 drv->unsol_event(codec, res);
171 }
172}
Keyon Jie18d43c92018-12-11 15:30:27 -0600173EXPORT_SYMBOL_GPL(snd_hdac_bus_process_unsol_events);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100174
Takashi Iwai78dd5e22015-10-28 12:26:48 +0100175/**
176 * snd_hdac_bus_add_device - Add a codec to bus
177 * @bus: HDA core bus
178 * @codec: HDA core device to add
179 *
180 * Adds the given codec to the list in the bus. The caddr_tbl array
181 * and codec_powered bits are updated, as well.
182 * Returns zero if success, or a negative error code.
183 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100184int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
185{
186 if (bus->caddr_tbl[codec->addr]) {
187 dev_err(bus->dev, "address 0x%x is already occupied\n",
188 codec->addr);
189 return -EBUSY;
190 }
191
192 list_add_tail(&codec->list, &bus->codec_list);
193 bus->caddr_tbl[codec->addr] = codec;
194 set_bit(codec->addr, &bus->codec_powered);
195 bus->num_codecs++;
196 return 0;
197}
198EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device);
199
Takashi Iwai78dd5e22015-10-28 12:26:48 +0100200/**
201 * snd_hdac_bus_remove_device - Remove a codec from bus
202 * @bus: HDA core bus
203 * @codec: HDA core device to remove
204 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100205void snd_hdac_bus_remove_device(struct hdac_bus *bus,
206 struct hdac_device *codec)
207{
208 WARN_ON(bus != codec->bus);
209 if (list_empty(&codec->list))
210 return;
211 list_del_init(&codec->list);
212 bus->caddr_tbl[codec->addr] = NULL;
213 clear_bit(codec->addr, &bus->codec_powered);
214 bus->num_codecs--;
Takashi Iwaieb8d0ea2017-06-19 17:49:48 +0200215 flush_work(&bus->unsol_work);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100216}
217EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device);
Takashi Iwai19abfef2019-08-07 20:32:08 +0200218
219#ifdef CONFIG_SND_HDA_ALIGNED_MMIO
220/* Helpers for aligned read/write of mmio space, for Tegra */
221unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
222{
223 void __iomem *aligned_addr =
224 (void __iomem *)((unsigned long)(addr) & ~0x3);
225 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
226 unsigned int v;
227
228 v = readl(aligned_addr);
229 return (v >> shift) & mask;
230}
231EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
232
233void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
234 unsigned int mask)
235{
236 void __iomem *aligned_addr =
237 (void __iomem *)((unsigned long)(addr) & ~0x3);
238 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
239 unsigned int v;
240
241 v = readl(aligned_addr);
242 v &= ~(mask << shift);
243 v |= val << shift;
244 writel(v, aligned_addr);
245}
246EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
247#endif /* CONFIG_SND_HDA_ALIGNED_MMIO */