Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/hda/core/bus.c
29266 views
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
.link_power = snd_hdac_bus_link_power,
21
};
22
23
/**
24
* snd_hdac_bus_init - initialize a HD-audio bas bus
25
* @bus: the pointer to bus object
26
* @dev: device pointer
27
* @ops: bus verb operators
28
*
29
* Returns 0 if successful, or a negative error code.
30
*/
31
int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
32
const struct hdac_bus_ops *ops)
33
{
34
memset(bus, 0, sizeof(*bus));
35
bus->dev = dev;
36
if (ops)
37
bus->ops = ops;
38
else
39
bus->ops = &default_ops;
40
bus->dma_type = SNDRV_DMA_TYPE_DEV;
41
INIT_LIST_HEAD(&bus->stream_list);
42
INIT_LIST_HEAD(&bus->codec_list);
43
INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
44
spin_lock_init(&bus->reg_lock);
45
mutex_init(&bus->cmd_mutex);
46
mutex_init(&bus->lock);
47
INIT_LIST_HEAD(&bus->hlink_list);
48
init_waitqueue_head(&bus->rirb_wq);
49
bus->irq = -1;
50
51
/*
52
* Default value of '8' is as per the HD audio specification (Rev 1.0a).
53
* Following relation is used to derive STRIPE control value.
54
* For sample rate <= 48K:
55
* { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
56
* For sample rate > 48K:
57
* { ((num_channels * bits_per_sample * rate/48000) /
58
* number of SDOs) >= 8 }
59
*/
60
bus->sdo_limit = 8;
61
62
return 0;
63
}
64
EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
65
66
/**
67
* snd_hdac_bus_exit - clean up a HD-audio bas bus
68
* @bus: the pointer to bus object
69
*/
70
void snd_hdac_bus_exit(struct hdac_bus *bus)
71
{
72
WARN_ON(!list_empty(&bus->stream_list));
73
WARN_ON(!list_empty(&bus->codec_list));
74
cancel_work_sync(&bus->unsol_work);
75
}
76
EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
77
78
/**
79
* snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
80
* @bus: bus object
81
* @addr: the HDAC device address
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
*/
87
int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
88
unsigned int cmd, unsigned int *res)
89
{
90
guard(mutex)(&bus->cmd_mutex);
91
return snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
92
}
93
94
/**
95
* snd_hdac_bus_exec_verb_unlocked - unlocked version
96
* @bus: bus object
97
* @addr: the HDAC device address
98
* @cmd: HD-audio encoded verb
99
* @res: pointer to store the response, NULL if performing asynchronously
100
*
101
* Returns 0 if successful, or a negative error code.
102
*/
103
int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
104
unsigned int cmd, unsigned int *res)
105
{
106
unsigned int tmp;
107
int err;
108
109
if (cmd == ~0)
110
return -EINVAL;
111
112
if (res)
113
*res = -1;
114
else if (bus->sync_write)
115
res = &tmp;
116
for (;;) {
117
trace_hda_send_cmd(bus, cmd);
118
err = bus->ops->command(bus, cmd);
119
if (err != -EAGAIN)
120
break;
121
/* process pending verbs */
122
err = bus->ops->get_response(bus, addr, &tmp);
123
if (err)
124
break;
125
}
126
if (!err && res) {
127
err = bus->ops->get_response(bus, addr, res);
128
trace_hda_get_response(bus, addr, *res);
129
}
130
return err;
131
}
132
EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
133
134
/**
135
* snd_hdac_bus_queue_event - add an unsolicited event to queue
136
* @bus: the BUS
137
* @res: unsolicited event (lower 32bit of RIRB entry)
138
* @res_ex: codec addr and flags (upper 32bit or RIRB entry)
139
*
140
* Adds the given event to the queue. The events are processed in
141
* the workqueue asynchronously. Call this function in the interrupt
142
* hanlder when RIRB receives an unsolicited event.
143
*/
144
void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
145
{
146
unsigned int wp;
147
148
if (!bus)
149
return;
150
151
trace_hda_unsol_event(bus, res, res_ex);
152
wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
153
bus->unsol_wp = wp;
154
155
wp <<= 1;
156
bus->unsol_queue[wp] = res;
157
bus->unsol_queue[wp + 1] = res_ex;
158
159
schedule_work(&bus->unsol_work);
160
}
161
162
/*
163
* process queued unsolicited events
164
*/
165
static void snd_hdac_bus_process_unsol_events(struct work_struct *work)
166
{
167
struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
168
struct hdac_device *codec;
169
struct hdac_driver *drv;
170
unsigned int rp, caddr, res;
171
172
spin_lock_irq(&bus->reg_lock);
173
while (bus->unsol_rp != bus->unsol_wp) {
174
rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
175
bus->unsol_rp = rp;
176
rp <<= 1;
177
res = bus->unsol_queue[rp];
178
caddr = bus->unsol_queue[rp + 1];
179
if (!(caddr & (1 << 4))) /* no unsolicited event? */
180
continue;
181
codec = bus->caddr_tbl[caddr & 0x0f];
182
if (!codec || !codec->registered)
183
continue;
184
spin_unlock_irq(&bus->reg_lock);
185
drv = drv_to_hdac_driver(codec->dev.driver);
186
if (drv->unsol_event)
187
drv->unsol_event(codec, res);
188
spin_lock_irq(&bus->reg_lock);
189
}
190
spin_unlock_irq(&bus->reg_lock);
191
}
192
193
/**
194
* snd_hdac_bus_add_device - Add a codec to bus
195
* @bus: HDA core bus
196
* @codec: HDA core device to add
197
*
198
* Adds the given codec to the list in the bus. The caddr_tbl array
199
* and codec_powered bits are updated, as well.
200
* Returns zero if success, or a negative error code.
201
*/
202
int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
203
{
204
if (bus->caddr_tbl[codec->addr]) {
205
dev_err(bus->dev, "address 0x%x is already occupied\n",
206
codec->addr);
207
return -EBUSY;
208
}
209
210
list_add_tail(&codec->list, &bus->codec_list);
211
bus->caddr_tbl[codec->addr] = codec;
212
set_bit(codec->addr, &bus->codec_powered);
213
bus->num_codecs++;
214
return 0;
215
}
216
217
/**
218
* snd_hdac_bus_remove_device - Remove a codec from bus
219
* @bus: HDA core bus
220
* @codec: HDA core device to remove
221
*/
222
void snd_hdac_bus_remove_device(struct hdac_bus *bus,
223
struct hdac_device *codec)
224
{
225
WARN_ON(bus != codec->bus);
226
if (list_empty(&codec->list))
227
return;
228
list_del_init(&codec->list);
229
bus->caddr_tbl[codec->addr] = NULL;
230
clear_bit(codec->addr, &bus->codec_powered);
231
bus->num_codecs--;
232
flush_work(&bus->unsol_work);
233
}
234
235
#ifdef CONFIG_SND_HDA_ALIGNED_MMIO
236
/* Helpers for aligned read/write of mmio space, for Tegra */
237
unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
238
{
239
void __iomem *aligned_addr =
240
(void __iomem *)((unsigned long)(addr) & ~0x3);
241
unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
242
unsigned int v;
243
244
v = readl(aligned_addr);
245
return (v >> shift) & mask;
246
}
247
EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
248
249
void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
250
unsigned int mask)
251
{
252
void __iomem *aligned_addr =
253
(void __iomem *)((unsigned long)(addr) & ~0x3);
254
unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
255
unsigned int v;
256
257
v = readl(aligned_addr);
258
v &= ~(mask << shift);
259
v |= val << shift;
260
writel(v, aligned_addr);
261
}
262
EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
263
#endif /* CONFIG_SND_HDA_ALIGNED_MMIO */
264
265
void snd_hdac_codec_link_up(struct hdac_device *codec)
266
{
267
struct hdac_bus *bus = codec->bus;
268
269
if (bus->ops->link_power)
270
bus->ops->link_power(codec, true);
271
else
272
snd_hdac_bus_link_power(codec, true);
273
}
274
EXPORT_SYMBOL_GPL(snd_hdac_codec_link_up);
275
276
void snd_hdac_codec_link_down(struct hdac_device *codec)
277
{
278
struct hdac_bus *bus = codec->bus;
279
280
if (bus->ops->link_power)
281
bus->ops->link_power(codec, false);
282
else
283
snd_hdac_bus_link_power(codec, false);
284
}
285
EXPORT_SYMBOL_GPL(snd_hdac_codec_link_down);
286
287