hdac_bus.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HD-audio core bus driver
  4. */
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/device.h>
  8. #include <linux/module.h>
  9. #include <linux/export.h>
  10. #include <sound/hdaudio.h>
  11. #include "local.h"
  12. #include "trace.h"
  13. static void snd_hdac_bus_process_unsol_events(struct work_struct *work);
  14. static const struct hdac_bus_ops default_ops = {
  15. .command = snd_hdac_bus_send_cmd,
  16. .get_response = snd_hdac_bus_get_response,
  17. .link_power = snd_hdac_bus_link_power,
  18. };
  19. /**
  20. * snd_hdac_bus_init - initialize a HD-audio bas bus
  21. * @bus: the pointer to bus object
  22. * @dev: device pointer
  23. * @ops: bus verb operators
  24. *
  25. * Returns 0 if successful, or a negative error code.
  26. */
  27. int snd_hdac_bus_init(struct hdac_bus *bus, struct device *dev,
  28. const struct hdac_bus_ops *ops)
  29. {
  30. memset(bus, 0, sizeof(*bus));
  31. bus->dev = dev;
  32. if (ops)
  33. bus->ops = ops;
  34. else
  35. bus->ops = &default_ops;
  36. bus->dma_type = SNDRV_DMA_TYPE_DEV;
  37. INIT_LIST_HEAD(&bus->stream_list);
  38. INIT_LIST_HEAD(&bus->codec_list);
  39. INIT_WORK(&bus->unsol_work, snd_hdac_bus_process_unsol_events);
  40. spin_lock_init(&bus->reg_lock);
  41. mutex_init(&bus->cmd_mutex);
  42. mutex_init(&bus->lock);
  43. INIT_LIST_HEAD(&bus->hlink_list);
  44. init_waitqueue_head(&bus->rirb_wq);
  45. bus->irq = -1;
  46. /*
  47. * Default value of '8' is as per the HD audio specification (Rev 1.0a).
  48. * Following relation is used to derive STRIPE control value.
  49. * For sample rate <= 48K:
  50. * { ((num_channels * bits_per_sample) / number of SDOs) >= 8 }
  51. * For sample rate > 48K:
  52. * { ((num_channels * bits_per_sample * rate/48000) /
  53. * number of SDOs) >= 8 }
  54. */
  55. bus->sdo_limit = 8;
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(snd_hdac_bus_init);
  59. /**
  60. * snd_hdac_bus_exit - clean up a HD-audio bas bus
  61. * @bus: the pointer to bus object
  62. */
  63. void snd_hdac_bus_exit(struct hdac_bus *bus)
  64. {
  65. WARN_ON(!list_empty(&bus->stream_list));
  66. WARN_ON(!list_empty(&bus->codec_list));
  67. cancel_work_sync(&bus->unsol_work);
  68. }
  69. EXPORT_SYMBOL_GPL(snd_hdac_bus_exit);
  70. /**
  71. * snd_hdac_bus_exec_verb - execute a HD-audio verb on the given bus
  72. * @bus: bus object
  73. * @addr: the HDAC device address
  74. * @cmd: HD-audio encoded verb
  75. * @res: pointer to store the response, NULL if performing asynchronously
  76. *
  77. * Returns 0 if successful, or a negative error code.
  78. */
  79. int snd_hdac_bus_exec_verb(struct hdac_bus *bus, unsigned int addr,
  80. unsigned int cmd, unsigned int *res)
  81. {
  82. int err;
  83. mutex_lock(&bus->cmd_mutex);
  84. err = snd_hdac_bus_exec_verb_unlocked(bus, addr, cmd, res);
  85. mutex_unlock(&bus->cmd_mutex);
  86. return err;
  87. }
  88. /**
  89. * snd_hdac_bus_exec_verb_unlocked - unlocked version
  90. * @bus: bus object
  91. * @addr: the HDAC device address
  92. * @cmd: HD-audio encoded verb
  93. * @res: pointer to store the response, NULL if performing asynchronously
  94. *
  95. * Returns 0 if successful, or a negative error code.
  96. */
  97. int snd_hdac_bus_exec_verb_unlocked(struct hdac_bus *bus, unsigned int addr,
  98. unsigned int cmd, unsigned int *res)
  99. {
  100. unsigned int tmp;
  101. int err;
  102. if (cmd == ~0)
  103. return -EINVAL;
  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. * snd_hdac_bus_queue_event - add an unsolicited event to queue
  127. * @bus: the BUS
  128. * @res: unsolicited event (lower 32bit of RIRB entry)
  129. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  130. *
  131. * Adds the given event to the queue. The events are processed in
  132. * the workqueue asynchronously. Call this function in the interrupt
  133. * hanlder when RIRB receives an unsolicited event.
  134. */
  135. void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex)
  136. {
  137. unsigned int wp;
  138. if (!bus)
  139. return;
  140. trace_hda_unsol_event(bus, res, res_ex);
  141. wp = (bus->unsol_wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  142. bus->unsol_wp = wp;
  143. wp <<= 1;
  144. bus->unsol_queue[wp] = res;
  145. bus->unsol_queue[wp + 1] = res_ex;
  146. schedule_work(&bus->unsol_work);
  147. }
  148. /*
  149. * process queued unsolicited events
  150. */
  151. static void snd_hdac_bus_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. spin_lock_irq(&bus->reg_lock);
  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->registered)
  168. continue;
  169. spin_unlock_irq(&bus->reg_lock);
  170. drv = drv_to_hdac_driver(codec->dev.driver);
  171. if (drv->unsol_event)
  172. drv->unsol_event(codec, res);
  173. spin_lock_irq(&bus->reg_lock);
  174. }
  175. spin_unlock_irq(&bus->reg_lock);
  176. }
  177. /**
  178. * snd_hdac_bus_add_device - Add a codec to bus
  179. * @bus: HDA core bus
  180. * @codec: HDA core device to add
  181. *
  182. * Adds the given codec to the list in the bus. The caddr_tbl array
  183. * and codec_powered bits are updated, as well.
  184. * Returns zero if success, or a negative error code.
  185. */
  186. int snd_hdac_bus_add_device(struct hdac_bus *bus, struct hdac_device *codec)
  187. {
  188. if (bus->caddr_tbl[codec->addr]) {
  189. dev_err(bus->dev, "address 0x%x is already occupied\n",
  190. codec->addr);
  191. return -EBUSY;
  192. }
  193. list_add_tail(&codec->list, &bus->codec_list);
  194. bus->caddr_tbl[codec->addr] = codec;
  195. set_bit(codec->addr, &bus->codec_powered);
  196. bus->num_codecs++;
  197. return 0;
  198. }
  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. */
  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--;
  214. flush_work(&bus->unsol_work);
  215. }
  216. #ifdef CONFIG_SND_HDA_ALIGNED_MMIO
  217. /* Helpers for aligned read/write of mmio space, for Tegra */
  218. unsigned int snd_hdac_aligned_read(void __iomem *addr, unsigned int mask)
  219. {
  220. void __iomem *aligned_addr =
  221. (void __iomem *)((unsigned long)(addr) & ~0x3);
  222. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  223. unsigned int v;
  224. v = readl(aligned_addr);
  225. return (v >> shift) & mask;
  226. }
  227. EXPORT_SYMBOL_GPL(snd_hdac_aligned_read);
  228. void snd_hdac_aligned_write(unsigned int val, void __iomem *addr,
  229. unsigned int mask)
  230. {
  231. void __iomem *aligned_addr =
  232. (void __iomem *)((unsigned long)(addr) & ~0x3);
  233. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  234. unsigned int v;
  235. v = readl(aligned_addr);
  236. v &= ~(mask << shift);
  237. v |= val << shift;
  238. writel(v, aligned_addr);
  239. }
  240. EXPORT_SYMBOL_GPL(snd_hdac_aligned_write);
  241. #endif /* CONFIG_SND_HDA_ALIGNED_MMIO */
  242. void snd_hdac_codec_link_up(struct hdac_device *codec)
  243. {
  244. struct hdac_bus *bus = codec->bus;
  245. if (bus->ops->link_power)
  246. bus->ops->link_power(codec, true);
  247. else
  248. snd_hdac_bus_link_power(codec, true);
  249. }
  250. EXPORT_SYMBOL_GPL(snd_hdac_codec_link_up);
  251. void snd_hdac_codec_link_down(struct hdac_device *codec)
  252. {
  253. struct hdac_bus *bus = codec->bus;
  254. if (bus->ops->link_power)
  255. bus->ops->link_power(codec, false);
  256. else
  257. snd_hdac_bus_link_power(codec, false);
  258. }
  259. EXPORT_SYMBOL_GPL(snd_hdac_codec_link_down);