hdac_ext_bus.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <sound/hdaudio_ext.h>
  22. MODULE_DESCRIPTION("HDA extended core");
  23. MODULE_LICENSE("GPL v2");
  24. static void hdac_ext_writel(u32 value, u32 __iomem *addr)
  25. {
  26. writel(value, addr);
  27. }
  28. static u32 hdac_ext_readl(u32 __iomem *addr)
  29. {
  30. return readl(addr);
  31. }
  32. static void hdac_ext_writew(u16 value, u16 __iomem *addr)
  33. {
  34. writew(value, addr);
  35. }
  36. static u16 hdac_ext_readw(u16 __iomem *addr)
  37. {
  38. return readw(addr);
  39. }
  40. static void hdac_ext_writeb(u8 value, u8 __iomem *addr)
  41. {
  42. writeb(value, addr);
  43. }
  44. static u8 hdac_ext_readb(u8 __iomem *addr)
  45. {
  46. return readb(addr);
  47. }
  48. static int hdac_ext_dma_alloc_pages(struct hdac_bus *bus, int type,
  49. size_t size, struct snd_dma_buffer *buf)
  50. {
  51. return snd_dma_alloc_pages(type, bus->dev, size, buf);
  52. }
  53. static void hdac_ext_dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
  54. {
  55. snd_dma_free_pages(buf);
  56. }
  57. static const struct hdac_io_ops hdac_ext_default_io = {
  58. .reg_writel = hdac_ext_writel,
  59. .reg_readl = hdac_ext_readl,
  60. .reg_writew = hdac_ext_writew,
  61. .reg_readw = hdac_ext_readw,
  62. .reg_writeb = hdac_ext_writeb,
  63. .reg_readb = hdac_ext_readb,
  64. .dma_alloc_pages = hdac_ext_dma_alloc_pages,
  65. .dma_free_pages = hdac_ext_dma_free_pages,
  66. };
  67. /**
  68. * snd_hdac_ext_bus_init - initialize a HD-audio extended bus
  69. * @ebus: the pointer to extended bus object
  70. * @dev: device pointer
  71. * @ops: bus verb operators
  72. * @io_ops: lowlevel I/O operators, can be NULL. If NULL core will use
  73. * default ops
  74. *
  75. * Returns 0 if successful, or a negative error code.
  76. */
  77. int snd_hdac_ext_bus_init(struct hdac_ext_bus *ebus, struct device *dev,
  78. const struct hdac_bus_ops *ops,
  79. const struct hdac_io_ops *io_ops)
  80. {
  81. int ret;
  82. static int idx;
  83. /* check if io ops are provided, if not load the defaults */
  84. if (io_ops == NULL)
  85. io_ops = &hdac_ext_default_io;
  86. ret = snd_hdac_bus_init(&ebus->bus, dev, ops, io_ops);
  87. if (ret < 0)
  88. return ret;
  89. INIT_LIST_HEAD(&ebus->hlink_list);
  90. ebus->idx = idx++;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_init);
  94. /**
  95. * snd_hdac_ext_bus_exit - clean up a HD-audio extended bus
  96. * @ebus: the pointer to extended bus object
  97. */
  98. void snd_hdac_ext_bus_exit(struct hdac_ext_bus *ebus)
  99. {
  100. snd_hdac_bus_exit(&ebus->bus);
  101. WARN_ON(!list_empty(&ebus->hlink_list));
  102. }
  103. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_exit);
  104. static void default_release(struct device *dev)
  105. {
  106. snd_hdac_ext_bus_device_exit(container_of(dev, struct hdac_device, dev));
  107. }
  108. /**
  109. * snd_hdac_ext_device_init - initialize the HDA extended codec base device
  110. * @ebus: hdac extended bus to attach to
  111. * @addr: codec address
  112. *
  113. * Returns zero for success or a negative error code.
  114. */
  115. int snd_hdac_ext_bus_device_init(struct hdac_ext_bus *ebus, int addr)
  116. {
  117. struct hdac_device *hdev = NULL;
  118. struct hdac_bus *bus = ebus_to_hbus(ebus);
  119. char name[15];
  120. int ret;
  121. hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
  122. if (!hdev)
  123. return -ENOMEM;
  124. snprintf(name, sizeof(name), "ehdaudio%dD%d", ebus->idx, addr);
  125. ret = snd_hdac_device_init(hdev, bus, name, addr);
  126. if (ret < 0) {
  127. dev_err(bus->dev, "device init failed for hdac device\n");
  128. return ret;
  129. }
  130. hdev->type = HDA_DEV_ASOC;
  131. hdev->dev.release = default_release;
  132. ret = snd_hdac_device_register(hdev);
  133. if (ret) {
  134. dev_err(bus->dev, "failed to register hdac device\n");
  135. snd_hdac_ext_bus_device_exit(hdev);
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_init);
  141. /**
  142. * snd_hdac_ext_bus_device_exit - clean up a HD-audio extended codec base device
  143. * @hdev: hdac device to clean up
  144. */
  145. void snd_hdac_ext_bus_device_exit(struct hdac_device *hdev)
  146. {
  147. snd_hdac_device_exit(hdev);
  148. kfree(hdev);
  149. }
  150. EXPORT_SYMBOL_GPL(snd_hdac_ext_bus_device_exit);