hdac_ext_bus.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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_ext_bus *ebus, struct device *dev,
  79. const struct hdac_bus_ops *ops,
  80. const struct hdac_io_ops *io_ops)
  81. {
  82. int ret;
  83. static int idx;
  84. /* check if io ops are provided, if not load the defaults */
  85. if (io_ops == NULL)
  86. io_ops = &hdac_ext_default_io;
  87. ret = snd_hdac_bus_init(&ebus->bus, dev, ops, io_ops);
  88. if (ret < 0)
  89. return ret;
  90. INIT_LIST_HEAD(&ebus->hlink_list);
  91. ebus->idx = idx++;
  92. mutex_init(&ebus->lock);
  93. ebus->cmd_dma_state = true;
  94. return 0;
  95. }
  96. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
  97. /**
  98. * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
  99. * @ebus: the pointer to extended bus object
  100. */
  101. void snd_hdac_ext_bus_exit(struct hdac_ext_bus *ebus)
  102. {
  103. snd_hdac_bus_exit(&ebus->bus);
  104. WARN_ON(!list_empty(&ebus->hlink_list));
  105. }
  106. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
  107. static void default_release(struct device *dev)
  108. {
  109. snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
  110. }
  111. /**
  112. * snd_hdac_ext_bus_device_init - initialize the HDA extended codec base device
  113. * @ebus: hdac extended bus to attach to
  114. * @addr: codec address
  115. *
  116. * Returns zero for success or a negative error code.
  117. */
  118. int snd_hdac_ext_bus_device_init(struct hdac_ext_bus *ebus, int addr)
  119. {
  120. struct hdac_ext_device *edev;
  121. struct hdac_device *hdev = NULL;
  122. struct hdac_bus *bus = ebus_to_hbus(ebus);
  123. char name[15];
  124. int ret;
  125. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  126. if (!edev)
  127. return -ENOMEM;
  128. hdev = &edev->hdac;
  129. edev->ebus = ebus;
  130. snprintf(name, sizeof(name), "ehdaudio%dD%d", ebus->idx, addr);
  131. ret = snd_hdac_device_init(hdev, bus, name, addr);
  132. if (ret < 0) {
  133. dev_err(bus->dev, "device init failed for hdac device\n");
  134. return ret;
  135. }
  136. hdev->type = HDA_DEV_ASOC;
  137. hdev->dev.release = default_release;
  138. ret = snd_hdac_device_register(hdev);
  139. if (ret) {
  140. dev_err(bus->dev, "failed to register hdac device\n");
  141. snd_hdac_ext_bus_device_exit(hdev);
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
  147. /**
  148. * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
  149. * @hdev: hdac device to clean up
  150. */
  151. void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
  152. {
  153. struct hdac_ext_device *edev = to_ehdac_device(hdev);
  154. snd_hdac_device_exit(hdev);
  155. kfree(edev);
  156. }
  157. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);
  158. /**
  159. * snd_hdac_ext_bus_device_remove - remove HD-audio extended codec base devices
  160. *
  161. * @ebus: HD-audio extended bus
  162. */
  163. void snd_hdac_ext_bus_device_remove(struct hdac_ext_bus *ebus)
  164. {
  165. struct hdac_device *codec, *__codec;
  166. /*
  167. * we need to remove all the codec devices objects created in the
  168. * snd_hdac_ext_bus_device_init
  169. */
  170. list_for_each_entry_safe(codec, __codec, &ebus->bus.codec_list, list) {
  171. snd_hdac_device_unregister(codec);
  172. put_device(&codec->dev);
  173. }
  174. }
  175. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_remove);
  176. #define dev_to_hdac(dev) (container_of((dev), \
  177. struct hdac_device, dev))
  178. static inline struct hdac_ext_driver *get_edrv(struct device *dev)
  179. {
  180. struct hdac_driver *hdrv = drv_to_hdac_driver(dev->driver);
  181. struct hdac_ext_driver *edrv = to_ehdac_driver(hdrv);
  182. return edrv;
  183. }
  184. static inline struct hdac_ext_device *get_edev(struct device *dev)
  185. {
  186. struct hdac_device *hdev = dev_to_hdac_dev(dev);
  187. struct hdac_ext_device *edev = to_ehdac_device(hdev);
  188. return edev;
  189. }
  190. static int hda_ext_drv_probe(struct device *dev)
  191. {
  192. return (get_edrv(dev))->probe(get_edev(dev));
  193. }
  194. static int hdac_ext_drv_remove(struct device *dev)
  195. {
  196. return (get_edrv(dev))->remove(get_edev(dev));
  197. }
  198. static void hdac_ext_drv_shutdown(struct device *dev)
  199. {
  200. return (get_edrv(dev))->shutdown(get_edev(dev));
  201. }
  202. /**
  203. * snd_hda_ext_driver_register - register a driver for ext hda devices
  204. *
  205. * @drv: ext hda driver structure
  206. */
  207. int snd_hda_ext_driver_register(struct hdac_ext_driver *drv)
  208. {
  209. drv->hdac.type = HDA_DEV_ASOC;
  210. drv->hdac.driver.bus = &snd_hda_bus_type;
  211. /* we use default match */
  212. if (drv->probe)
  213. drv->hdac.driver.probe = hda_ext_drv_probe;
  214. if (drv->remove)
  215. drv->hdac.driver.remove = hdac_ext_drv_remove;
  216. if (drv->shutdown)
  217. drv->hdac.driver.shutdown = hdac_ext_drv_shutdown;
  218. return driver_register(&drv->hdac.driver);
  219. }
  220. EXPORT_SYMBOL_GPL(snd_hda_ext_driver_register);
  221. /**
  222. * snd_hda_ext_driver_unregister - unregister a driver for ext hda devices
  223. *
  224. * @drv: ext hda driver structure
  225. */
  226. void snd_hda_ext_driver_unregister(struct hdac_ext_driver *drv)
  227. {
  228. driver_unregister(&drv->hdac.driver);
  229. }
  230. EXPORT_SYMBOL_GPL(snd_hda_ext_driver_unregister);