hdac_bus.c 6.9 KB

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