hdac_ext_bus.c 6.6 KB

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