of-dma.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Device tree helpers for DMA request / controller
  3. *
  4. * Based on of_gpio.c
  5. *
  6. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_dma.h>
  19. static LIST_HEAD(of_dma_list);
  20. static DEFINE_MUTEX(of_dma_lock);
  21. /**
  22. * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
  23. * @dma_spec: pointer to DMA specifier as found in the device tree
  24. *
  25. * Finds a DMA controller with matching device node and number for dma cells
  26. * in a list of registered DMA controllers. If a match is found a valid pointer
  27. * to the DMA data stored is retuned. A NULL pointer is returned if no match is
  28. * found.
  29. */
  30. static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
  31. {
  32. struct of_dma *ofdma;
  33. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  34. if (ofdma->of_node == dma_spec->np)
  35. return ofdma;
  36. pr_debug("%s: can't find DMA controller %s\n", __func__,
  37. dma_spec->np->full_name);
  38. return NULL;
  39. }
  40. /**
  41. * of_dma_controller_register - Register a DMA controller to DT DMA helpers
  42. * @np: device node of DMA controller
  43. * @of_dma_xlate: translation function which converts a phandle
  44. * arguments list into a dma_chan structure
  45. * @data pointer to controller specific data to be used by
  46. * translation function
  47. *
  48. * Returns 0 on success or appropriate errno value on error.
  49. *
  50. * Allocated memory should be freed with appropriate of_dma_controller_free()
  51. * call.
  52. */
  53. int of_dma_controller_register(struct device_node *np,
  54. struct dma_chan *(*of_dma_xlate)
  55. (struct of_phandle_args *, struct of_dma *),
  56. void *data)
  57. {
  58. struct of_dma *ofdma;
  59. if (!np || !of_dma_xlate) {
  60. pr_err("%s: not enough information provided\n", __func__);
  61. return -EINVAL;
  62. }
  63. ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
  64. if (!ofdma)
  65. return -ENOMEM;
  66. ofdma->of_node = np;
  67. ofdma->of_dma_xlate = of_dma_xlate;
  68. ofdma->of_dma_data = data;
  69. /* Now queue of_dma controller structure in list */
  70. mutex_lock(&of_dma_lock);
  71. list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
  72. mutex_unlock(&of_dma_lock);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL_GPL(of_dma_controller_register);
  76. /**
  77. * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
  78. * @np: device node of DMA controller
  79. *
  80. * Memory allocated by of_dma_controller_register() is freed here.
  81. */
  82. void of_dma_controller_free(struct device_node *np)
  83. {
  84. struct of_dma *ofdma;
  85. mutex_lock(&of_dma_lock);
  86. list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
  87. if (ofdma->of_node == np) {
  88. list_del(&ofdma->of_dma_controllers);
  89. kfree(ofdma);
  90. break;
  91. }
  92. mutex_unlock(&of_dma_lock);
  93. }
  94. EXPORT_SYMBOL_GPL(of_dma_controller_free);
  95. /**
  96. * of_dma_match_channel - Check if a DMA specifier matches name
  97. * @np: device node to look for DMA channels
  98. * @name: channel name to be matched
  99. * @index: index of DMA specifier in list of DMA specifiers
  100. * @dma_spec: pointer to DMA specifier as found in the device tree
  101. *
  102. * Check if the DMA specifier pointed to by the index in a list of DMA
  103. * specifiers, matches the name provided. Returns 0 if the name matches and
  104. * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
  105. */
  106. static int of_dma_match_channel(struct device_node *np, const char *name,
  107. int index, struct of_phandle_args *dma_spec)
  108. {
  109. const char *s;
  110. if (of_property_read_string_index(np, "dma-names", index, &s))
  111. return -ENODEV;
  112. if (strcmp(name, s))
  113. return -ENODEV;
  114. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  115. dma_spec))
  116. return -ENODEV;
  117. return 0;
  118. }
  119. /**
  120. * of_dma_request_slave_channel - Get the DMA slave channel
  121. * @np: device node to get DMA request from
  122. * @name: name of desired channel
  123. *
  124. * Returns pointer to appropriate dma channel on success or NULL on error.
  125. */
  126. struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
  127. const char *name)
  128. {
  129. struct of_phandle_args dma_spec;
  130. struct of_dma *ofdma;
  131. struct dma_chan *chan;
  132. int count, i;
  133. if (!np || !name) {
  134. pr_err("%s: not enough information provided\n", __func__);
  135. return NULL;
  136. }
  137. count = of_property_count_strings(np, "dma-names");
  138. if (count < 0) {
  139. pr_err("%s: dma-names property of node '%s' missing or empty\n",
  140. __func__, np->full_name);
  141. return NULL;
  142. }
  143. for (i = 0; i < count; i++) {
  144. if (of_dma_match_channel(np, name, i, &dma_spec))
  145. continue;
  146. mutex_lock(&of_dma_lock);
  147. ofdma = of_dma_find_controller(&dma_spec);
  148. if (ofdma)
  149. chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
  150. else
  151. chan = NULL;
  152. mutex_unlock(&of_dma_lock);
  153. of_node_put(dma_spec.np);
  154. if (chan)
  155. return chan;
  156. }
  157. return NULL;
  158. }
  159. /**
  160. * of_dma_simple_xlate - Simple DMA engine translation function
  161. * @dma_spec: pointer to DMA specifier as found in the device tree
  162. * @of_dma: pointer to DMA controller data
  163. *
  164. * A simple translation function for devices that use a 32-bit value for the
  165. * filter_param when calling the DMA engine dma_request_channel() function.
  166. * Note that this translation function requires that #dma-cells is equal to 1
  167. * and the argument of the dma specifier is the 32-bit filter_param. Returns
  168. * pointer to appropriate dma channel on success or NULL on error.
  169. */
  170. struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  171. struct of_dma *ofdma)
  172. {
  173. int count = dma_spec->args_count;
  174. struct of_dma_filter_info *info = ofdma->of_dma_data;
  175. if (!info || !info->filter_fn)
  176. return NULL;
  177. if (count != 1)
  178. return NULL;
  179. return dma_request_channel(info->dma_cap, info->filter_fn,
  180. &dma_spec->args[0]);
  181. }
  182. EXPORT_SYMBOL_GPL(of_dma_simple_xlate);