hdac_ext_bus.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * hdac-ext-bus.c - HD-audio extended core bus functions.
  3. *
  4. * Copyright (C) 2014-2015 Intel Corp
  5. * Author: Jeeja KP <jeeja.kp@intel.com>
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <sound/hdaudio_ext.h>
  23. MODULE_DESCRIPTION("HDA extended core");
  24. MODULE_LICENSE("GPL v2");
  25. static void hdac_ext_writel(u32 value, u32 __iomem *addr)
  26. {
  27. writel(value, addr);
  28. }
  29. static u32 hdac_ext_readl(u32 __iomem *addr)
  30. {
  31. return readl(addr);
  32. }
  33. static void hdac_ext_writew(u16 value, u16 __iomem *addr)
  34. {
  35. writew(value, addr);
  36. }
  37. static u16 hdac_ext_readw(u16 __iomem *addr)
  38. {
  39. return readw(addr);
  40. }
  41. static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
  42. {
  43. writeb(value, addr);
  44. }
  45. static u8 hdac_ext_readb(u8 __iomem *addr)
  46. {
  47. return readb(addr);
  48. }
  49. static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
  50. size_t size, struct snd_dma_buffer *buf)
  51. {
  52. return snd_dma_alloc_pages(type, bus->dev, size, buf);
  53. }
  54. static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
  55. {
  56. snd_dma_free_pages(buf);
  57. }
  58. static const struct hdac_io_ops hdac_ext_default_io = {
  59. .reg_writel = hdac_ext_writel,
  60. .reg_readl = hdac_ext_readl,
  61. .reg_writew = hdac_ext_writew,
  62. .reg_readw = hdac_ext_readw,
  63. .reg_writeb = hdac_ext_writeb,
  64. .reg_readb = hdac_ext_readb,
  65. .dma_alloc_pages = hdac_ext_dma_alloc_pages,
  66. .dma_free_pages = hdac_ext_dma_free_pages,
  67. };
  68. /**
  69. * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
  70. * @ebus: the pointer to extended bus object
  71. * @dev: device pointer
  72. * @ops: bus verb operators
  73. * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
  74. * default ops
  75. *
  76. * Returns 0 if successful, or a negative error code.
  77. */
  78. int snd_hdac_ext_bus_init(struct hdac_bus *bus, struct device *dev,
  79. const struct hdac_bus_ops *ops,
  80. const struct hdac_io_ops *io_ops,
  81. const struct hdac_ext_bus_ops *ext_ops)
  82. {
  83. int ret;
  84. static int idx;
  85. /* check if io ops are provided, if not load the defaults */
  86. if (io_ops == NULL)
  87. io_ops = &hdac_ext_default_io;
  88. ret = snd_hdac_bus_init(bus, dev, ops, io_ops);
  89. if (ret < 0)
  90. return ret;
  91. bus->ext_ops = ext_ops;
  92. INIT_LIST_HEAD(&bus->hlink_list);
  93. bus->idx = idx++;
  94. mutex_init(&bus->lock);
  95. bus->cmd_dma_state = true;
  96. return 0;
  97. }
  98. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
  99. /**
  100. * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
  101. * @ebus: the pointer to extended bus object
  102. */
  103. void snd_hdac_ext_bus_exit(struct hdac_bus *bus)
  104. {
  105. snd_hdac_bus_exit(bus);
  106. WARN_ON(!list_empty(&bus->hlink_list));
  107. }
  108. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
  109. static void default_release(struct device *dev)
  110. {
  111. snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
  112. }
  113. /**
  114. * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
  115. * @ebus: hdac extended bus to attach to
  116. * @addr: codec address
  117. *
  118. * Returns zero for success or a negative error code.
  119. */
  120. int snd_hdac_ext_bus_device_init(struct hdac_bus *bus, int addr,
  121. struct hdac_device *hdev)
  122. {
  123. char name[15];
  124. int ret;
  125. hdev->bus = bus;
  126. snprintf(name, sizeof(name), "ehdaudio%dD%d", bus->idx, addr);
  127. ret = snd_hdac_device_init(hdev, bus, name, addr);
  128. if (ret < 0) {
  129. dev_err(bus->dev, "device init failed for hdac device\n");
  130. return ret;
  131. }
  132. hdev->type = HDA_DEV_ASOC;
  133. hdev->dev.release = default_release;
  134. ret = snd_hdac_device_register(hdev);
  135. if (ret) {
  136. dev_err(bus->dev, "failed to register hdac device\n");
  137. snd_hdac_ext_bus_device_exit(hdev);
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
  143. /**
  144. * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
  145. * @hdev: hdac device to clean up
  146. */
  147. void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
  148. {
  149. snd_hdac_device_exit(hdev);
  150. }
  151. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
  152. /**
  153. * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
  154. *
  155. * @ebus: HD-audio extended bus
  156. */
  157. void snd_hdac_ext_bus_device_remove(struct hdac_bus *bus)
  158. {
  159. struct hdac_device *codec, *__codec;
  160. /*
  161. * we need to remove all the codec devices objects created in the
  162. * snd_hdac_ext_bus_device_init
  163. */
  164. list_for_each_entry_safe(codec, __codec, &bus->codec_list, list) {
  165. snd_hdac_device_unregister(codec);
  166. put_device(&codec->dev);
  167. }
  168. }
  169. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
  170. #define dev_to_hdac(dev) (container_of((dev), \
  171. struct hdac_device, dev))
  172. static inline struct hdac_driver *get_hdrv(struct device *dev)
  173. {
  174. struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
  175. return hdrv;
  176. }
  177. static inline struct hdac_device *get_hdev(struct device *dev)
  178. {
  179. struct hdac_device *hdev = dev_to_hdac_dev(dev);
  180. return hdev;
  181. }
  182. static int hda_ext_drv_probe(struct device *dev)
  183. {
  184. return (get_hdrv(dev))->probe(get_hdev(dev));
  185. }
  186. static int hdac_ext_drv_remove(struct device *dev)
  187. {
  188. return (get_hdrv(dev))->remove(get_hdev(dev));
  189. }
  190. static void hdac_ext_drv_shutdown(struct device *dev)
  191. {
  192. return (get_hdrv(dev))->shutdown(get_hdev(dev));
  193. }
  194. /**
  195. * snd_hda_ext_driver_register - register a driver for ext hda devices
  196. *
  197. * @drv: ext hda driver structure
  198. */
  199. int snd_hda_ext_driver_register(struct hdac_driver *drv)
  200. {
  201. drv->type = HDA_DEV_ASOC;
  202. drv->driver.bus = &snd_hda_bus_type;
  203. /* we use default match */
  204. if (drv->probe)
  205. drv->driver.probe = hda_ext_drv_probe;
  206. if (drv->remove)
  207. drv->driver.remove = hdac_ext_drv_remove;
  208. if (drv->shutdown)
  209. drv->driver.shutdown = hdac_ext_drv_shutdown;
  210. return driver_register(&drv->driver);
  211. }
  212. EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
  213. /**
  214. * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
  215. *
  216. * @drv: ext hda driver structure
  217. */
  218. void snd_hda_ext_driver_unregister(struct hdac_driver *drv)
  219. {
  220. driver_unregister(&drv->driver);
  221. }
  222. EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);