blob: 0e81ea89a5965a49a80e52c6405d9a456cc9decf [file] [log] [blame]
Takashi Iwaid068ebc2015-03-02 23:22:59 +01001/*
2 * HD-audio core bus driver
3 */
4
5#include <linux/init.h>
6#include <linux/device.h>
7#include <linux/module.h>
8#include <linux/export.h>
9#include <sound/hdaudio.h>
Takashi Iwaie3117822015-03-10 15:16:28 +010010#include "trace.h"
Takashi Iwaid068ebc2015-03-02 23:22:59 +010011
12static void process_unsol_events(struct work_struct *work);
13
Takashi Iwai14752412015-04-14 12:15:47 +020014static const struct hdac_bus_ops default_ops = {
15 .command = snd_hdac_bus_send_cmd,
16 .get_response = snd_hdac_bus_get_response,
17};
18
Takashi Iwaid068ebc2015-03-02 23:22:59 +010019/**
20 * snd_hdac_bus_init - initialize a HD-audio bas bus
21 * @bus: the pointer to bus object
Takashi Iwai14752412015-04-14 12:15:47 +020022 * @ops: bus verb operators
23 * @io_ops: lowlevel I/O operators
Takashi Iwaid068ebc2015-03-02 23:22:59 +010024 *
25 * Returns 0 if successful, or a negative error code.
26 */
27int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
Takashi Iwai14752412015-04-14 12:15:47 +020028 const struct hdac_bus_ops *ops,
29 const struct hdac_io_ops *io_ops)
Takashi Iwaid068ebc2015-03-02 23:22:59 +010030{
31 memset(bus, 0, sizeof(*bus));
32 bus->dev = dev;
Takashi Iwai14752412015-04-14 12:15:47 +020033 if (ops)
34 bus->ops = ops;
35 else
36 bus->ops = &default_ops;
37 bus->io_ops = io_ops;
38 INIT_LIST_HEAD(&bus->stream_list);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010039 INIT_LIST_HEAD(&bus->codec_list);
40 INIT_WORK(&bus->unsol_work, process_unsol_events);
Takashi Iwai14752412015-04-14 12:15:47 +020041 spin_lock_init(&bus->reg_lock);
Takashi Iwaid068ebc2015-03-02 23:22:59 +010042 mutex_init(&bus->cmd_mutex);
Takashi Iwai14752412015-04-14 12:15:47 +020043 bus->irq = -1;
Takashi Iwaid068ebc2015-03-02 23:22:59 +010044 return 0;
45}
46EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
47
48/**
49 * snd_hdac_bus_exit - clean up a HD-audio bas bus
50 * @bus: the pointer to bus object
51 */
52void snd_hdac_bus_exit(struct hdac_bus *bus)
53{
Takashi Iwai14752412015-04-14 12:15:47 +020054 WARN_ON(!list_empty(&bus->stream_list));
Takashi Iwaid068ebc2015-03-02 23:22:59 +010055 WARN_ON(!list_empty(&bus->codec_list));
56 cancel_work_sync(&bus->unsol_work);
57}
58EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
59
60/**
61 * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
62 * @bus: bus object
63 * @cmd: HD-audio encoded verb
64 * @res: pointer to store the response, NULL if performing asynchronously
65 *
66 * Returns 0 if successful, or a negative error code.
67 */
68int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
69 unsigned int cmd, unsigned int *res)
70{
71 int err;
72
73 mutex_lock(&bus->cmd_mutex);
74 err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
75 mutex_unlock(&bus->cmd_mutex);
76 return err;
77}
78EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb);
79
80/**
81 * snd_hdac_bus_exec_verb_unlocked - unlocked version
82 * @bus: bus object
83 * @cmd: HD-audio encoded verb
84 * @res: pointer to store the response, NULL if performing asynchronously
85 *
86 * Returns 0 if successful, or a negative error code.
87 */
88int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
89 unsigned int cmd, unsigned int *res)
90{
91 unsigned int tmp;
92 int err;
93
94 if (cmd == ~0)
95 return -EINVAL;
96
97 if (res)
98 *res = -1;
99 else if (bus->sync_write)
100 res = &tmp;
101 for (;;) {
Takashi Iwaie3117822015-03-10 15:16:28 +0100102 trace_hda_send_cmd(bus, cmd);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100103 err = bus->ops->command(bus, cmd);
104 if (err != -EAGAIN)
105 break;
106 /* process pending verbs */
107 err = bus->ops->get_response(bus, addr, &tmp);
108 if (err)
109 break;
110 }
Takashi Iwaie3117822015-03-10 15:16:28 +0100111 if (!err && res) {
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100112 err = bus->ops->get_response(bus, addr, res);
Takashi Iwaie3117822015-03-10 15:16:28 +0100113 trace_hda_get_response(bus, addr, *res);
114 }
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100115 return err;
116}
117EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked);
118
119/**
120 * snd_hdac_bus_queue_event - add an unsolicited event to queue
121 * @bus: the BUS
122 * @res: unsolicited event (lower 32bit of RIRB entry)
123 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
124 *
125 * Adds the given event to the queue. The events are processed in
126 * the workqueue asynchronously. Call this function in the interrupt
127 * hanlder when RIRB receives an unsolicited event.
128 */
129void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
130{
131 unsigned int wp;
132
133 if (!bus)
134 return;
135
Takashi Iwaie3117822015-03-10 15:16:28 +0100136 trace_hda_unsol_event(bus, res, res_ex);
Takashi Iwaid068ebc2015-03-02 23:22:59 +0100137 wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
138 bus->unsol_wp = wp;
139
140 wp <<= 1;
141 bus->unsol_queue[wp] = res;
142 bus->unsol_queue[wp + 1] = res_ex;
143
144 schedule_work(&bus->unsol_work);
145}
146EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event);
147
148/*
149 * process queued unsolicited events
150 */
151static void process_unsol_events(struct work_struct *work)
152{
153 struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work);
154 struct hdac_device *codec;
155 struct hdac_driver *drv;
156 unsigned int rp, caddr, res;
157
158 while (bus->unsol_rp != bus->unsol_wp) {
159 rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE;
160 bus->unsol_rp = rp;
161 rp <<= 1;
162 res = bus->unsol_queue[rp];
163 caddr = bus->unsol_queue[rp + 1];
164 if (!(caddr & (1 << 4))) /* no unsolicited event? */
165 continue;
166 codec = bus->caddr_tbl[caddr & 0x0f];
167 if (!codec || !codec->dev.driver)
168 continue;
169 drv = drv_to_hdac_driver(codec->dev.driver);
170 if (drv->unsol_event)
171 drv->unsol_event(codec, res);
172 }
173}
174
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--;
215}
216EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device);