Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 1 | /* |
| 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 Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 10 | #include "trace.h" |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 11 | |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 12 | static const struct hdac_bus_ops default_ops = { |
| 13 | .command = snd_hdac_bus_send_cmd, |
| 14 | .get_response = snd_hdac_bus_get_response, |
| 15 | }; |
| 16 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 17 | /** |
| 18 | * snd_hdac_bus_init - initialize a HD-audio bas bus |
| 19 | * @bus: the pointer to bus object |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 20 | * @ops: bus verb operators |
| 21 | * @io_ops: lowlevel I/O operators |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 22 | * |
| 23 | * Returns 0 if successful, or a negative error code. |
| 24 | */ |
| 25 | int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev, |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 26 | const struct hdac_bus_ops *ops, |
| 27 | const struct hdac_io_ops *io_ops) |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 28 | { |
| 29 | memset(bus, 0, sizeof(*bus)); |
| 30 | bus->dev = dev; |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 31 | if (ops) |
| 32 | bus->ops = ops; |
| 33 | else |
| 34 | bus->ops = &default_ops; |
| 35 | bus->io_ops = io_ops; |
| 36 | INIT_LIST_HEAD(&bus->stream_list); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 37 | INIT_LIST_HEAD(&bus->codec_list); |
Keyon Jie | 18d43c9 | 2018-12-11 15:30:27 -0600 | [diff] [blame] | 38 | INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events); |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 39 | spin_lock_init(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 40 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 41 | bus->irq = -1; |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(snd_hdac_bus_init); |
| 45 | |
| 46 | /** |
| 47 | * snd_hdac_bus_exit - clean up a HD-audio bas bus |
| 48 | * @bus: the pointer to bus object |
| 49 | */ |
| 50 | void snd_hdac_bus_exit(struct hdac_bus *bus) |
| 51 | { |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 52 | WARN_ON(!list_empty(&bus->stream_list)); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 53 | WARN_ON(!list_empty(&bus->codec_list)); |
| 54 | cancel_work_sync(&bus->unsol_work); |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(snd_hdac_bus_exit); |
| 57 | |
| 58 | /** |
| 59 | * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus |
| 60 | * @bus: bus object |
| 61 | * @cmd: HD-audio encoded verb |
| 62 | * @res: pointer to store the response, NULL if performing asynchronously |
| 63 | * |
| 64 | * Returns 0 if successful, or a negative error code. |
| 65 | */ |
| 66 | int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr, |
| 67 | unsigned int cmd, unsigned int *res) |
| 68 | { |
| 69 | int err; |
| 70 | |
| 71 | mutex_lock(&bus->cmd_mutex); |
| 72 | err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res); |
| 73 | mutex_unlock(&bus->cmd_mutex); |
| 74 | return err; |
| 75 | } |
| 76 | EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb); |
| 77 | |
| 78 | /** |
| 79 | * snd_hdac_bus_exec_verb_unlocked - unlocked version |
| 80 | * @bus: bus object |
| 81 | * @cmd: HD-audio encoded verb |
| 82 | * @res: pointer to store the response, NULL if performing asynchronously |
| 83 | * |
| 84 | * Returns 0 if successful, or a negative error code. |
| 85 | */ |
| 86 | int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr, |
| 87 | unsigned int cmd, unsigned int *res) |
| 88 | { |
| 89 | unsigned int tmp; |
| 90 | int err; |
| 91 | |
| 92 | if (cmd == ~0) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | if (res) |
| 96 | *res = -1; |
| 97 | else if (bus->sync_write) |
| 98 | res = &tmp; |
| 99 | for (;;) { |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 100 | trace_hda_send_cmd(bus, cmd); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 101 | err = bus->ops->command(bus, cmd); |
| 102 | if (err != -EAGAIN) |
| 103 | break; |
| 104 | /* process pending verbs */ |
| 105 | err = bus->ops->get_response(bus, addr, &tmp); |
| 106 | if (err) |
| 107 | break; |
| 108 | } |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 109 | if (!err && res) { |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 110 | err = bus->ops->get_response(bus, addr, res); |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 111 | trace_hda_get_response(bus, addr, *res); |
| 112 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 113 | return err; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked); |
| 116 | |
| 117 | /** |
| 118 | * snd_hdac_bus_queue_event - add an unsolicited event to queue |
| 119 | * @bus: the BUS |
| 120 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 121 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 122 | * |
| 123 | * Adds the given event to the queue. The events are processed in |
| 124 | * the workqueue asynchronously. Call this function in the interrupt |
| 125 | * hanlder when RIRB receives an unsolicited event. |
| 126 | */ |
| 127 | void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex) |
| 128 | { |
| 129 | unsigned int wp; |
| 130 | |
| 131 | if (!bus) |
| 132 | return; |
| 133 | |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 134 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 135 | wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 136 | bus->unsol_wp = wp; |
| 137 | |
| 138 | wp <<= 1; |
| 139 | bus->unsol_queue[wp] = res; |
| 140 | bus->unsol_queue[wp + 1] = res_ex; |
| 141 | |
| 142 | schedule_work(&bus->unsol_work); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(snd_hdac_bus_queue_event); |
| 145 | |
| 146 | /* |
| 147 | * process queued unsolicited events |
| 148 | */ |
Keyon Jie | 18d43c9 | 2018-12-11 15:30:27 -0600 | [diff] [blame] | 149 | void snd_hdac_bus_process_unsol_events(struct work_struct *work) |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 150 | { |
| 151 | struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work); |
| 152 | struct hdac_device *codec; |
| 153 | struct hdac_driver *drv; |
| 154 | unsigned int rp, caddr, res; |
| 155 | |
| 156 | while (bus->unsol_rp != bus->unsol_wp) { |
| 157 | rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 158 | bus->unsol_rp = rp; |
| 159 | rp <<= 1; |
| 160 | res = bus->unsol_queue[rp]; |
| 161 | caddr = bus->unsol_queue[rp + 1]; |
| 162 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
| 163 | continue; |
| 164 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 165 | if (!codec || !codec->dev.driver) |
| 166 | continue; |
| 167 | drv = drv_to_hdac_driver(codec->dev.driver); |
| 168 | if (drv->unsol_event) |
| 169 | drv->unsol_event(codec, res); |
| 170 | } |
| 171 | } |
Keyon Jie | 18d43c9 | 2018-12-11 15:30:27 -0600 | [diff] [blame] | 172 | EXPORT_SYMBOL_GPL(snd_hdac_bus_process_unsol_events); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 173 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 174 | /** |
| 175 | * snd_hdac_bus_add_device - Add a codec to bus |
| 176 | * @bus: HDA core bus |
| 177 | * @codec: HDA core device to add |
| 178 | * |
| 179 | * Adds the given codec to the list in the bus. The caddr_tbl array |
| 180 | * and codec_powered bits are updated, as well. |
| 181 | * Returns zero if success, or a negative error code. |
| 182 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 183 | int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec) |
| 184 | { |
| 185 | if (bus->caddr_tbl[codec->addr]) { |
| 186 | dev_err(bus->dev, "address 0x%x is already occupied\n", |
| 187 | codec->addr); |
| 188 | return -EBUSY; |
| 189 | } |
| 190 | |
| 191 | list_add_tail(&codec->list, &bus->codec_list); |
| 192 | bus->caddr_tbl[codec->addr] = codec; |
| 193 | set_bit(codec->addr, &bus->codec_powered); |
| 194 | bus->num_codecs++; |
| 195 | return 0; |
| 196 | } |
| 197 | EXPORT_SYMBOL_GPL(snd_hdac_bus_add_device); |
| 198 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 199 | /** |
| 200 | * snd_hdac_bus_remove_device - Remove a codec from bus |
| 201 | * @bus: HDA core bus |
| 202 | * @codec: HDA core device to remove |
| 203 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 204 | void snd_hdac_bus_remove_device(struct hdac_bus *bus, |
| 205 | struct hdac_device *codec) |
| 206 | { |
| 207 | WARN_ON(bus != codec->bus); |
| 208 | if (list_empty(&codec->list)) |
| 209 | return; |
| 210 | list_del_init(&codec->list); |
| 211 | bus->caddr_tbl[codec->addr] = NULL; |
| 212 | clear_bit(codec->addr, &bus->codec_powered); |
| 213 | bus->num_codecs--; |
Takashi Iwai | eb8d0ea | 2017-06-19 17:49:48 +0200 | [diff] [blame] | 214 | flush_work(&bus->unsol_work); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 215 | } |
| 216 | EXPORT_SYMBOL_GPL(snd_hdac_bus_remove_device); |