Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 2 | /* |
| 3 | * HD-audio core bus driver |
| 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
Stephen Rothwell | fe40106 | 2019-08-09 12:54:58 +1000 | [diff] [blame] | 7 | #include <linux/io.h> |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 8 | #include <linux/device.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <sound/hdaudio.h> |
Takashi Iwai | 53eff75 | 2019-08-14 18:27:08 +0200 | [diff] [blame] | 12 | #include "local.h" |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 13 | #include "trace.h" |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 14 | |
Takashi Iwai | ddf7cb8 | 2019-08-14 19:59:44 +0200 | [diff] [blame] | 15 | static void snd_hdac_bus_process_unsol_events(struct work_struct *work); |
| 16 | |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 17 | static const struct hdac_bus_ops default_ops = { |
| 18 | .command = snd_hdac_bus_send_cmd, |
| 19 | .get_response = snd_hdac_bus_get_response, |
Kai Vehmanen | f9e5fd1 | 2021-02-05 20:46:28 +0200 | [diff] [blame] | 20 | .link_power = snd_hdac_bus_link_power, |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 23 | /** |
| 24 | * snd_hdac_bus_init - initialize a HD-audio bas bus |
| 25 | * @bus: the pointer to bus object |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 26 | * @dev: device pointer |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 27 | * @ops: bus verb operators |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 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, |
Takashi Iwai | 19abfef | 2019-08-07 20:32:08 +0200 | [diff] [blame] | 32 | const struct hdac_bus_ops *ops) |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 33 | { |
| 34 | memset(bus, 0, sizeof(*bus)); |
| 35 | bus->dev = dev; |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 36 | if (ops) |
| 37 | bus->ops = ops; |
| 38 | else |
| 39 | bus->ops = &default_ops; |
Takashi Iwai | 619a1f1 | 2019-08-07 20:02:31 +0200 | [diff] [blame] | 40 | bus->dma_type = SNDRV_DMA_TYPE_DEV; |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 41 | INIT_LIST_HEAD(&bus->stream_list); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 42 | INIT_LIST_HEAD(&bus->codec_list); |
Keyon Jie | 18d43c9 | 2018-12-11 15:30:27 -0600 | [diff] [blame] | 43 | INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events); |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 44 | spin_lock_init(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 45 | mutex_init(&bus->cmd_mutex); |
Takashi Iwai | d7a181d | 2019-04-10 12:49:55 +0200 | [diff] [blame] | 46 | mutex_init(&bus->lock); |
Takashi Iwai | e61ab9f | 2019-04-10 16:00:54 +0200 | [diff] [blame] | 47 | INIT_LIST_HEAD(&bus->hlink_list); |
Takashi Iwai | 88452da | 2019-12-10 15:57:27 +0100 | [diff] [blame] | 48 | init_waitqueue_head(&bus->rirb_wq); |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 49 | bus->irq = -1; |
Sameer Pujar | b90b925 | 2020-08-19 21:02:10 +0530 | [diff] [blame] | 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 | |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 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 | { |
Takashi Iwai | 1475241 | 2015-04-14 12:15:47 +0200 | [diff] [blame] | 72 | WARN_ON(!list_empty(&bus->stream_list)); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 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 |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 81 | * @addr: the HDAC device address |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 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 | int err; |
| 91 | |
| 92 | mutex_lock(&bus->cmd_mutex); |
| 93 | err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res); |
| 94 | mutex_unlock(&bus->cmd_mutex); |
| 95 | return err; |
| 96 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * snd_hdac_bus_exec_verb_unlocked - unlocked version |
| 100 | * @bus: bus object |
Keyon Jie | 6e57188f | 2020-01-13 14:56:38 -0600 | [diff] [blame] | 101 | * @addr: the HDAC device address |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 102 | * @cmd: HD-audio encoded verb |
| 103 | * @res: pointer to store the response, NULL if performing asynchronously |
| 104 | * |
| 105 | * Returns 0 if successful, or a negative error code. |
| 106 | */ |
| 107 | int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr, |
| 108 | unsigned int cmd, unsigned int *res) |
| 109 | { |
| 110 | unsigned int tmp; |
| 111 | int err; |
| 112 | |
| 113 | if (cmd == ~0) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | if (res) |
| 117 | *res = -1; |
| 118 | else if (bus->sync_write) |
| 119 | res = &tmp; |
| 120 | for (;;) { |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 121 | trace_hda_send_cmd(bus, cmd); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 122 | err = bus->ops->command(bus, cmd); |
| 123 | if (err != -EAGAIN) |
| 124 | break; |
| 125 | /* process pending verbs */ |
| 126 | err = bus->ops->get_response(bus, addr, &tmp); |
| 127 | if (err) |
| 128 | break; |
| 129 | } |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 130 | if (!err && res) { |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 131 | err = bus->ops->get_response(bus, addr, res); |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 132 | trace_hda_get_response(bus, addr, *res); |
| 133 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 134 | return err; |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(snd_hdac_bus_exec_verb_unlocked); |
| 137 | |
| 138 | /** |
| 139 | * snd_hdac_bus_queue_event - add an unsolicited event to queue |
| 140 | * @bus: the BUS |
| 141 | * @res: unsolicited event (lower 32bit of RIRB entry) |
| 142 | * @res_ex: codec addr and flags (upper 32bit or RIRB entry) |
| 143 | * |
| 144 | * Adds the given event to the queue. The events are processed in |
| 145 | * the workqueue asynchronously. Call this function in the interrupt |
| 146 | * hanlder when RIRB receives an unsolicited event. |
| 147 | */ |
| 148 | void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex) |
| 149 | { |
| 150 | unsigned int wp; |
| 151 | |
| 152 | if (!bus) |
| 153 | return; |
| 154 | |
Takashi Iwai | e311782 | 2015-03-10 15:16:28 +0100 | [diff] [blame] | 155 | trace_hda_unsol_event(bus, res, res_ex); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 156 | wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 157 | bus->unsol_wp = wp; |
| 158 | |
| 159 | wp <<= 1; |
| 160 | bus->unsol_queue[wp] = res; |
| 161 | bus->unsol_queue[wp + 1] = res_ex; |
| 162 | |
| 163 | schedule_work(&bus->unsol_work); |
| 164 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * process queued unsolicited events |
| 168 | */ |
Takashi Iwai | ddf7cb8 | 2019-08-14 19:59:44 +0200 | [diff] [blame] | 169 | static void snd_hdac_bus_process_unsol_events(struct work_struct *work) |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 170 | { |
| 171 | struct hdac_bus *bus = container_of(work, struct hdac_bus, unsol_work); |
| 172 | struct hdac_device *codec; |
| 173 | struct hdac_driver *drv; |
| 174 | unsigned int rp, caddr, res; |
| 175 | |
Takashi Iwai | c637fa1 | 2020-05-16 08:25:56 +0200 | [diff] [blame] | 176 | spin_lock_irq(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 177 | while (bus->unsol_rp != bus->unsol_wp) { |
| 178 | rp = (bus->unsol_rp + 1) % HDA_UNSOL_QUEUE_SIZE; |
| 179 | bus->unsol_rp = rp; |
| 180 | rp <<= 1; |
| 181 | res = bus->unsol_queue[rp]; |
| 182 | caddr = bus->unsol_queue[rp + 1]; |
| 183 | if (!(caddr & (1 << 4))) /* no unsolicited event? */ |
| 184 | continue; |
| 185 | codec = bus->caddr_tbl[caddr & 0x0f]; |
| 186 | if (!codec || !codec->dev.driver) |
| 187 | continue; |
Takashi Iwai | c637fa1 | 2020-05-16 08:25:56 +0200 | [diff] [blame] | 188 | spin_unlock_irq(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 189 | drv = drv_to_hdac_driver(codec->dev.driver); |
| 190 | if (drv->unsol_event) |
| 191 | drv->unsol_event(codec, res); |
Takashi Iwai | c637fa1 | 2020-05-16 08:25:56 +0200 | [diff] [blame] | 192 | spin_lock_irq(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 193 | } |
Takashi Iwai | c637fa1 | 2020-05-16 08:25:56 +0200 | [diff] [blame] | 194 | spin_unlock_irq(&bus->reg_lock); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 197 | /** |
| 198 | * snd_hdac_bus_add_device - Add a codec to bus |
| 199 | * @bus: HDA core bus |
| 200 | * @codec: HDA core device to add |
| 201 | * |
| 202 | * Adds the given codec to the list in the bus. The caddr_tbl array |
| 203 | * and codec_powered bits are updated, as well. |
| 204 | * Returns zero if success, or a negative error code. |
| 205 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 206 | int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec) |
| 207 | { |
| 208 | if (bus->caddr_tbl[codec->addr]) { |
| 209 | dev_err(bus->dev, "address 0x%x is already occupied\n", |
| 210 | codec->addr); |
| 211 | return -EBUSY; |
| 212 | } |
| 213 | |
| 214 | list_add_tail(&codec->list, &bus->codec_list); |
| 215 | bus->caddr_tbl[codec->addr] = codec; |
| 216 | set_bit(codec->addr, &bus->codec_powered); |
| 217 | bus->num_codecs++; |
| 218 | return 0; |
| 219 | } |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 220 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 221 | /** |
| 222 | * snd_hdac_bus_remove_device - Remove a codec from bus |
| 223 | * @bus: HDA core bus |
| 224 | * @codec: HDA core device to remove |
| 225 | */ |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 226 | void snd_hdac_bus_remove_device(struct hdac_bus *bus, |
| 227 | struct hdac_device *codec) |
| 228 | { |
| 229 | WARN_ON(bus != codec->bus); |
| 230 | if (list_empty(&codec->list)) |
| 231 | return; |
| 232 | list_del_init(&codec->list); |
| 233 | bus->caddr_tbl[codec->addr] = NULL; |
| 234 | clear_bit(codec->addr, &bus->codec_powered); |
| 235 | bus->num_codecs--; |
Takashi Iwai | eb8d0ea | 2017-06-19 17:49:48 +0200 | [diff] [blame] | 236 | flush_work(&bus->unsol_work); |
Takashi Iwai | d068ebc | 2015-03-02 23:22:59 +0100 | [diff] [blame] | 237 | } |
Takashi Iwai | 19abfef | 2019-08-07 20:32:08 +0200 | [diff] [blame] | 238 | |
| 239 | #ifdef CONFIG_SND_HDA_ALIGNED_MMIO |
| 240 | /* Helpers for aligned read/write of mmio space, for Tegra */ |
| 241 | unsigned int snd_hdac_aligned_read(void __iomem *addr, 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 | return (v >> shift) & mask; |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(snd_hdac_aligned_read); |
| 252 | |
| 253 | void snd_hdac_aligned_write(unsigned int val, void __iomem *addr, |
| 254 | unsigned int mask) |
| 255 | { |
| 256 | void __iomem *aligned_addr = |
| 257 | (void __iomem *)((unsigned long)(addr) & ~0x3); |
| 258 | unsigned int shift = ((unsigned long)(addr) & 0x3) << 3; |
| 259 | unsigned int v; |
| 260 | |
| 261 | v = readl(aligned_addr); |
| 262 | v &= ~(mask << shift); |
| 263 | v |= val << shift; |
| 264 | writel(v, aligned_addr); |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(snd_hdac_aligned_write); |
| 267 | #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */ |
Kai Vehmanen | f9e5fd1 | 2021-02-05 20:46:28 +0200 | [diff] [blame] | 268 | |
| 269 | void snd_hdac_codec_link_up(struct hdac_device *codec) |
| 270 | { |
| 271 | struct hdac_bus *bus = codec->bus; |
| 272 | |
| 273 | if (bus->ops->link_power) |
| 274 | bus->ops->link_power(codec, true); |
| 275 | else |
| 276 | snd_hdac_bus_link_power(codec, true); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(snd_hdac_codec_link_up); |
| 279 | |
| 280 | void snd_hdac_codec_link_down(struct hdac_device *codec) |
| 281 | { |
| 282 | struct hdac_bus *bus = codec->bus; |
| 283 | |
| 284 | if (bus->ops->link_power) |
| 285 | bus->ops->link_power(codec, false); |
| 286 | else |
| 287 | snd_hdac_bus_link_power(codec, false); |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(snd_hdac_codec_link_down); |