xref: /openbmc/linux/sound/hda/hdac_bus.c (revision 015d239a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HD-audio core bus driver
4  */
5 
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"
14 
15 static void snd_hdac_bus_process_unsol_events(struct work_struct *work);
16 
17 static const struct hdac_bus_ops default_ops = {
18 	.command = snd_hdac_bus_send_cmd,
19 	.get_response = snd_hdac_bus_get_response,
20 };
21 
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
27  *
28  * Returns 0 if successful, or a negative error code.
29  */
30 int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
31 		      const struct hdac_bus_ops *ops)
32 {
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;
49 	return 0;
50 }
51 EXPORT_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  */
57 void snd_hdac_bus_exit(struct hdac_bus *bus)
58 {
59 	WARN_ON(!list_empty(&bus->stream_list));
60 	WARN_ON(!list_empty(&bus->codec_list));
61 	cancel_work_sync(&bus->unsol_work);
62 }
63 EXPORT_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
68  * @addr: the HDAC device address
69  * @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  */
74 int 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 }
84 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
85 
86 /**
87  * snd_hdac_bus_exec_verb_unlocked - unlocked version
88  * @bus: bus object
89  * @addr: the HDAC device address
90  * @cmd: HD-audio encoded verb
91  * @res: pointer to store the response, NULL if performing asynchronously
92  *
93  * Returns 0 if successful, or a negative error code.
94  */
95 int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
96 				    unsigned int cmd, unsigned int *res)
97 {
98 	unsigned int tmp;
99 	int err;
100 
101 	if (cmd == ~0)
102 		return -EINVAL;
103 
104 	if (res)
105 		*res = -1;
106 	else if (bus->sync_write)
107 		res = &tmp;
108 	for (;;) {
109 		trace_hda_send_cmd(bus, cmd);
110 		err = bus->ops->command(bus, cmd);
111 		if (err != -EAGAIN)
112 			break;
113 		/* process pending verbs */
114 		err = bus->ops->get_response(bus, addr, &tmp);
115 		if (err)
116 			break;
117 	}
118 	if (!err && res) {
119 		err = bus->ops->get_response(bus, addr, res);
120 		trace_hda_get_response(bus, addr, *res);
121 	}
122 	return err;
123 }
124 EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
125 
126 /**
127  * snd_hdac_bus_queue_event - add an unsolicited event to queue
128  * @bus: the BUS
129  * @res: unsolicited event (lower 32bit of RIRB entry)
130  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
131  *
132  * Adds the given event to the queue.  The events are processed in
133  * the workqueue asynchronously.  Call this function in the interrupt
134  * hanlder when RIRB receives an unsolicited event.
135  */
136 void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
137 {
138 	unsigned int wp;
139 
140 	if (!bus)
141 		return;
142 
143 	trace_hda_unsol_event(bus, res, res_ex);
144 	wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
145 	bus->unsol_wp = wp;
146 
147 	wp <<= 1;
148 	bus->unsol_queue[wp] = res;
149 	bus->unsol_queue[wp + 1] = res_ex;
150 
151 	schedule_work(&bus->unsol_work);
152 }
153 EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
154 
155 /*
156  * process queued unsolicited events
157  */
158 static void snd_hdac_bus_process_unsol_events(struct work_struct *work)
159 {
160 	struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
161 	struct hdac_device *codec;
162 	struct hdac_driver *drv;
163 	unsigned int rp, caddr, res;
164 
165 	while (bus->unsol_rp != bus->unsol_wp) {
166 		rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
167 		bus->unsol_rp = rp;
168 		rp <<= 1;
169 		res = bus->unsol_queue[rp];
170 		caddr = bus->unsol_queue[rp + 1];
171 		if (!(caddr & (1 << 4))) /* no unsolicited event? */
172 			continue;
173 		codec = bus->caddr_tbl[caddr & 0x0f];
174 		if (!codec || !codec->dev.driver)
175 			continue;
176 		drv = drv_to_hdac_driver(codec->dev.driver);
177 		if (drv->unsol_event)
178 			drv->unsol_event(codec, res);
179 	}
180 }
181 
182 /**
183  * snd_hdac_bus_add_device - Add a codec to bus
184  * @bus: HDA core bus
185  * @codec: HDA core device to add
186  *
187  * Adds the given codec to the list in the bus.  The caddr_tbl array
188  * and codec_powered bits are updated, as well.
189  * Returns zero if success, or a negative error code.
190  */
191 int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
192 {
193 	if (bus->caddr_tbl[codec->addr]) {
194 		dev_err(bus->dev, "address 0x%x is already occupied\n",
195 			codec->addr);
196 		return -EBUSY;
197 	}
198 
199 	list_add_tail(&codec->list, &bus->codec_list);
200 	bus->caddr_tbl[codec->addr] = codec;
201 	set_bit(codec->addr, &bus->codec_powered);
202 	bus->num_codecs++;
203 	return 0;
204 }
205 
206 /**
207  * snd_hdac_bus_remove_device - Remove a codec from bus
208  * @bus: HDA core bus
209  * @codec: HDA core device to remove
210  */
211 void snd_hdac_bus_remove_device(struct hdac_bus *bus,
212 				struct hdac_device *codec)
213 {
214 	WARN_ON(bus != codec->bus);
215 	if (list_empty(&codec->list))
216 		return;
217 	list_del_init(&codec->list);
218 	bus->caddr_tbl[codec->addr] = NULL;
219 	clear_bit(codec->addr, &bus->codec_powered);
220 	bus->num_codecs--;
221 	flush_work(&bus->unsol_work);
222 }
223 
224 #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
225 /* Helpers for aligned read/write of mmio space, for Tegra */
226 unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
227 {
228 	void __iomem *aligned_addr =
229 		(void __iomem *)((unsigned long)(addr) & ~0x3);
230 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
231 	unsigned int v;
232 
233 	v = readl(aligned_addr);
234 	return (v >> shift) & mask;
235 }
236 EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
237 
238 void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
239 			    unsigned int mask)
240 {
241 	void __iomem *aligned_addr =
242 		(void __iomem *)((unsigned long)(addr) & ~0x3);
243 	unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
244 	unsigned int v;
245 
246 	v = readl(aligned_addr);
247 	v &= ~(mask << shift);
248 	v |= val << shift;
249 	writel(v, aligned_addr);
250 }
251 EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
252 #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */
253