of-dma.c 6.1 KB

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