of-dma.c 6.1 KB

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