blob: 09ddab5f5caebebe39b4a5a8363fe7fde30a696d [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>
Stephen Rothwellfe401062019-08-09 12:54:58 +10007#include <linux/io.h>
Takashi Iwaid068ebc2015-03-02 23:22:59 +01008#include <linux/device.h>
9#include <linux/module.h>
10#include <linux/export.h>
11#include <sound/hdaudio.h>
Takashi Iwai53eff752019-08-14 18:27:08 +020012#include "local.h"
Takashi Iwaie3117822015-03-10 15:16:28 +010013#include "trace.h"
Takashi Iwaid068ebc2015-03-02 23:22:59 +010014
Takashi Iwaiddf7cb82019-08-14 19:59:44 +020015static void snd_hdac_bus_process_unsol_events(struct work_struct *work);
16
Takashi Iwai14752412015-04-14 12:15:47 +020017static const struct hdac_bus_ops default_ops = {
18 .command = snd_hdac_bus_send_cmd,
19 .get_response = snd_hdac_bus_get_response,
20};
21
Takashi Iwaid068ebc2015-03-02 23:22:59 +010022/**
23 * snd_hdac_bus_init - initialize a HD-audio bas bus
24 * @bus: the pointer to bus object
Keyon Jie6e57188f2020-01-13 14:56:38 -060025 * @dev: device pointer
Takashi Iwai14752412015-04-14 12:15:47 +020026 * @ops: bus verb operators
Takashi Iwaid068ebc2015-03-02 23:22:59 +010027 *
28 * Returns 0 if successful, or a negative error code.
29 */
30int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
Takashi Iwai19abfef2019-08-07 20:32:08 +020031 const struct hdac_bus_ops *ops)
Takashi Iwaid068ebc2015-03-02 23:22:59 +010032{
33 memset(bus, 0, sizeof(*bus));
34 bus->dev = dev;
Takashi Iwai14752412015-04-14 12:15:47 +020035 if (ops)
36 bus->ops = ops;
37 else
38 bus->ops = &default_ops;
Takashi Iwai619a1f12019-08-07 20:02:31 +020039 bus->dma_type = SNDRV_DMA_TYPE_DEV;
Takashi Iwai14752412015-04-14 12:15:47 +020040 INIT_LIST_HEAD(&bus->stream_list);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010041 INIT_LIST_HEAD(&bus->codec_list);
Keyon Jie18d43c92018-12-11 15:30:27 -060042 INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
Takashi Iwai14752412015-04-14 12:15:47 +020043 spin_lock_init(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010044 mutex_init(&bus->cmd_mutex);
Takashi Iwaid7a181d2019-04-10 12:49:55 +020045 mutex_init(&bus->lock);
Takashi Iwaie61ab9f2019-04-10 16:00:54 +020046 INIT_LIST_HEAD(&bus->hlink_list);
Takashi Iwai88452da2019-12-10 15:57:27 +010047 init_waitqueue_head(&bus->rirb_wq);
Takashi Iwai14752412015-04-14 12:15:47 +020048 bus->irq = -1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010049 return 0;
50}
51EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
52
53/**
54 * snd_hdac_bus_exit - clean up a HD-audio bas bus
55 * @bus: the pointer to bus object
56 */
57void snd_hdac_bus_exit(struct hdac_bus *bus)
58{
Takashi Iwai14752412015-04-14 12:15:47 +020059 WARN_ON(!list_empty(&bus->stream_list));
Takashi Iwaid068ebc2015-03-02 23:22:59 +010060 WARN_ON(!list_empty(&bus->codec_list));
61 cancel_work_sync(&bus->unsol_work);
62}
63EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
64
65/**
66 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
67 * @bus: bus object
Keyon Jie6e57188f2020-01-13 14:56:38 -060068 * @addr: the HDAC device address
Takashi Iwaid068ebc2015-03-02 23:22:59 +010069 * @cmd: HD-audio encoded verb
70 * @res: pointer to store the response, NULL if performing asynchronously
71 *
72 * Returns 0 if successful, or a negative error code.
73 */
74int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
75 unsigned int cmd, unsigned int *res)
76{
77 int err;
78
79 mutex_lock(&bus->cmd_mutex);
80 err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
81 mutex_unlock(&bus->cmd_mutex);
82 return err;
83}
Takashi Iwaid068ebc2015-03-02 23:22:59 +010084
85/**
86 * snd_hdac_bus_exec_verb_unlocked - unlocked version
87 * @bus: bus object
Keyon Jie6e57188f2020-01-13 14:56:38 -060088 * @addr: the HDAC device address
Takashi Iwaid068ebc2015-03-02 23:22:59 +010089 * @cmd: HD-audio encoded verb
90 * @res: pointer to store the response, NULL if performing asynchronously
91 *
92 * Returns 0 if successful, or a negative error code.
93 */
94int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
95 unsigned int cmd, unsigned int *res)
96{
97 unsigned int tmp;
98 int err;
99
100 if (cmd == ~0)
101 return -EINVAL;
102
103 if (res)
104 *res = -1;
105 else if (bus->sync_write)
106 res = &tmp;
107 for (;;) {
Takashi Iwaie3117822015-03-10 15:16:28 +0100108 trace_hda_send_cmd(bus, cmd);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100109 err = bus->ops->command(bus, cmd);
110 if (err != -EAGAIN)
111 break;
112 /* process pending verbs */
113 err = bus->ops->get_response(bus, addr, &tmp);
114 if (err)
115 break;
116 }
Takashi Iwaie3117822015-03-10 15:16:28 +0100117 if (!err && res) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100118 err = bus->ops->get_response(bus, addr, res);
Takashi Iwaie3117822015-03-10 15:16:28 +0100119 trace_hda_get_response(bus, addr, *res);
120 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100121 return err;
122}
123EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
124
125/**
126 * snd_hdac_bus_queue_event - add an unsolicited event to queue
127 * @bus: the BUS
128 * @res: unsolicited event (lower 32bit of RIRB entry)
129 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
130 *
131 * Adds the given event to the queue. The events are processed in
132 * the workqueue asynchronously. Call this function in the interrupt
133 * hanlder when RIRB receives an unsolicited event.
134 */
135void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
136{
137 unsigned int wp;
138
139 if (!bus)
140 return;
141
Takashi Iwaie3117822015-03-10 15:16:28 +0100142 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100143 wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
144 bus->unsol_wp = wp;
145
146 wp <<= 1;
147 bus->unsol_queue[wp] = res;
148 bus->unsol_queue[wp + 1] = res_ex;
149
150 schedule_work(&bus->unsol_work);
151}
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100152
153/*
154 * process queued unsolicited events
155 */
Takashi Iwaiddf7cb82019-08-14 19:59:44 +0200156static void snd_hdac_bus_process_unsol_events(struct work_struct *work)
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100157{
158 struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
159 struct hdac_device *codec;
160 struct hdac_driver *drv;
161 unsigned int rp, caddr, res;
162
Takashi Iwaic637fa12020-05-16 08:25:56 +0200163 spin_lock_irq(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100164 while (bus->unsol_rp != bus->unsol_wp) {
165 rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
166 bus->unsol_rp = rp;
167 rp <<= 1;
168 res = bus->unsol_queue[rp];
169 caddr = bus->unsol_queue[rp + 1];
170 if (!(caddr & (1 << 4))) /* no unsolicited event? */
171 continue;
172 codec = bus->caddr_tbl[caddr & 0x0f];
173 if (!codec || !codec->dev.driver)
174 continue;
Takashi Iwaic637fa12020-05-16 08:25:56 +0200175 spin_unlock_irq(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100176 drv = drv_to_hdac_driver(codec->dev.driver);
177 if (drv->unsol_event)
178 drv->unsol_event(codec, res);
Takashi Iwaic637fa12020-05-16 08:25:56 +0200179 spin_lock_irq(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100180 }
Takashi Iwaic637fa12020-05-16 08:25:56 +0200181 spin_unlock_irq(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100182}
183
Takashi Iwai78dd5e22015-10-28 12:26:48 +0100184/**
185 * snd_hdac_bus_add_device - Add a codec to bus
186 * @bus: HDA core bus
187 * @codec: HDA core device to add
188 *
189 * Adds the given codec to the list in the bus. The caddr_tbl array
190 * and codec_powered bits are updated, as well.
191 * Returns zero if success, or a negative error code.
192 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100193int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
194{
195 if (bus->caddr_tbl[codec->addr]) {
196 dev_err(bus->dev, "address 0x%x is already occupied\n",
197 codec->addr);
198 return -EBUSY;
199 }
200
201 list_add_tail(&codec->list, &bus->codec_list);
202 bus->caddr_tbl[codec->addr] = codec;
203 set_bit(codec->addr, &bus->codec_powered);
204 bus->num_codecs++;
205 return 0;
206}
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100207
Takashi Iwai78dd5e22015-10-28 12:26:48 +0100208/**
209 * snd_hdac_bus_remove_device - Remove a codec from bus
210 * @bus: HDA core bus
211 * @codec: HDA core device to remove
212 */
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100213void snd_hdac_bus_remove_device(struct hdac_bus *bus,
214 struct hdac_device *codec)
215{
216 WARN_ON(bus != codec->bus);
217 if (list_empty(&codec->list))
218 return;
219 list_del_init(&codec->list);
220 bus->caddr_tbl[codec->addr] = NULL;
221 clear_bit(codec->addr, &bus->codec_powered);
222 bus->num_codecs--;
Takashi Iwaieb8d0ea2017-06-19 17:49:48 +0200223 flush_work(&bus->unsol_work);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100224}
Takashi Iwai19abfef2019-08-07 20:32:08 +0200225
226#ifdef CONFIG_SND_HDA_ALIGNED_MMIO
227/* Helpers for aligned read/write of mmio space, for Tegra */
228unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
229{
230 void __iomem *aligned_addr =
231 (void __iomem *)((unsigned long)(addr) & ~0x3);
232 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
233 unsigned int v;
234
235 v = readl(aligned_addr);
236 return (v >> shift) & mask;
237}
238EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
239
240void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
241 unsigned int mask)
242{
243 void __iomem *aligned_addr =
244 (void __iomem *)((unsigned long)(addr) & ~0x3);
245 unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
246 unsigned int v;
247
248 v = readl(aligned_addr);
249 v &= ~(mask << shift);
250 v |= val << shift;
251 writel(v, aligned_addr);
252}
253EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
254#endif /* CONFIG_SND_HDA_ALIGNED_MMIO */