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
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/slab.h>
18#include <linux/of.h>
19#include <linux/of_dma.h>
20
21static LIST_HEAD(of_dma_list);
22static DEFINE_MUTEX(of_dma_lock);
23
24/**
25 * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
26 * @dma_spec:	pointer to DMA specifier as found in the device tree
27 *
28 * Finds a DMA controller with matching device node and number for dma cells
29 * in a list of registered DMA controllers. If a match is found a valid pointer
30 * to the DMA data stored is retuned. A NULL pointer is returned if no match is
31 * found.
32 */
33static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
34{
35	struct of_dma *ofdma;
36
37	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
38		if (ofdma->of_node == dma_spec->np)
39			return ofdma;
40
41	pr_debug("%s: can't find DMA controller %s\n", __func__,
42		 dma_spec->np->full_name);
43
44	return NULL;
45}
46
47/**
48 * of_dma_router_xlate - translation function for router devices
49 * @dma_spec:	pointer to DMA specifier as found in the device tree
50 * @of_dma:	pointer to DMA controller data (router information)
51 *
52 * The function creates new dma_spec to be passed to the router driver's
53 * of_dma_route_allocate() function to prepare a dma_spec which will be used
54 * to request channel from the real DMA controller.
55 */
56static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
57					    struct of_dma *ofdma)
58{
59	struct dma_chan		*chan;
60	struct of_dma		*ofdma_target;
61	struct of_phandle_args	dma_spec_target;
62	void			*route_data;
63
64	/* translate the request for the real DMA controller */
65	memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
66	route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
67	if (IS_ERR(route_data))
68		return NULL;
69
70	ofdma_target = of_dma_find_controller(&dma_spec_target);
71	if (!ofdma_target)
72		return NULL;
73
74	chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
75	if (chan) {
76		chan->router = ofdma->dma_router;
77		chan->route_data = route_data;
78	} else {
79		ofdma->dma_router->route_free(ofdma->dma_router->dev,
80					      route_data);
81	}
82
83	/*
84	 * Need to put the node back since the ofdma->of_dma_route_allocate
85	 * has taken it for generating the new, translated dma_spec
86	 */
87	of_node_put(dma_spec_target.np);
88	return chan;
89}
90
91/**
92 * of_dma_controller_register - Register a DMA controller to DT DMA helpers
93 * @np:			device node of DMA controller
94 * @of_dma_xlate:	translation function which converts a phandle
95 *			arguments list into a dma_chan structure
96 * @data		pointer to controller specific data to be used by
97 *			translation function
98 *
99 * Returns 0 on success or appropriate errno value on error.
100 *
101 * Allocated memory should be freed with appropriate of_dma_controller_free()
102 * call.
103 */
104int of_dma_controller_register(struct device_node *np,
105				struct dma_chan *(*of_dma_xlate)
106				(struct of_phandle_args *, struct of_dma *),
107				void *data)
108{
109	struct of_dma	*ofdma;
110
111	if (!np || !of_dma_xlate) {
112		pr_err("%s: not enough information provided\n", __func__);
113		return -EINVAL;
114	}
115
116	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
117	if (!ofdma)
118		return -ENOMEM;
119
120	ofdma->of_node = np;
121	ofdma->of_dma_xlate = of_dma_xlate;
122	ofdma->of_dma_data = data;
123
124	/* Now queue of_dma controller structure in list */
125	mutex_lock(&of_dma_lock);
126	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
127	mutex_unlock(&of_dma_lock);
128
129	return 0;
130}
131EXPORT_SYMBOL_GPL(of_dma_controller_register);
132
133/**
134 * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
135 * @np:		device node of DMA controller
136 *
137 * Memory allocated by of_dma_controller_register() is freed here.
138 */
139void of_dma_controller_free(struct device_node *np)
140{
141	struct of_dma *ofdma;
142
143	mutex_lock(&of_dma_lock);
144
145	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
146		if (ofdma->of_node == np) {
147			list_del(&ofdma->of_dma_controllers);
148			kfree(ofdma);
149			break;
150		}
151
152	mutex_unlock(&of_dma_lock);
153}
154EXPORT_SYMBOL_GPL(of_dma_controller_free);
155
156/**
157 * of_dma_router_register - Register a DMA router to DT DMA helpers as a
158 *			    controller
159 * @np:				device node of DMA router
160 * @of_dma_route_allocate:	setup function for the router which need to
161 *				modify the dma_spec for the DMA controller to
162 *				use and to set up the requested route.
163 * @dma_router:			pointer to dma_router structure to be used when
164 *				the route need to be free up.
165 *
166 * Returns 0 on success or appropriate errno value on error.
167 *
168 * Allocated memory should be freed with appropriate of_dma_controller_free()
169 * call.
170 */
171int of_dma_router_register(struct device_node *np,
172			   void *(*of_dma_route_allocate)
173			   (struct of_phandle_args *, struct of_dma *),
174			   struct dma_router *dma_router)
175{
176	struct of_dma	*ofdma;
177
178	if (!np || !of_dma_route_allocate || !dma_router) {
179		pr_err("%s: not enough information provided\n", __func__);
180		return -EINVAL;
181	}
182
183	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
184	if (!ofdma)
185		return -ENOMEM;
186
187	ofdma->of_node = np;
188	ofdma->of_dma_xlate = of_dma_router_xlate;
189	ofdma->of_dma_route_allocate = of_dma_route_allocate;
190	ofdma->dma_router = dma_router;
191
192	/* Now queue of_dma controller structure in list */
193	mutex_lock(&of_dma_lock);
194	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
195	mutex_unlock(&of_dma_lock);
196
197	return 0;
198}
199EXPORT_SYMBOL_GPL(of_dma_router_register);
200
201/**
202 * of_dma_match_channel - Check if a DMA specifier matches name
203 * @np:		device node to look for DMA channels
204 * @name:	channel name to be matched
205 * @index:	index of DMA specifier in list of DMA specifiers
206 * @dma_spec:	pointer to DMA specifier as found in the device tree
207 *
208 * Check if the DMA specifier pointed to by the index in a list of DMA
209 * specifiers, matches the name provided. Returns 0 if the name matches and
210 * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
211 */
212static int of_dma_match_channel(struct device_node *np, const char *name,
213				int index, struct of_phandle_args *dma_spec)
214{
215	const char *s;
216
217	if (of_property_read_string_index(np, "dma-names", index, &s))
218		return -ENODEV;
219
220	if (strcmp(name, s))
221		return -ENODEV;
222
223	if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
224				       dma_spec))
225		return -ENODEV;
226
227	return 0;
228}
229
230/**
231 * of_dma_request_slave_channel - Get the DMA slave channel
232 * @np:		device node to get DMA request from
233 * @name:	name of desired channel
234 *
235 * Returns pointer to appropriate DMA channel on success or an error pointer.
236 */
237struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
238					      const char *name)
239{
240	struct of_phandle_args	dma_spec;
241	struct of_dma		*ofdma;
242	struct dma_chan		*chan;
243	int			count, i;
244	int			ret_no_channel = -ENODEV;
245
246	if (!np || !name) {
247		pr_err("%s: not enough information provided\n", __func__);
248		return ERR_PTR(-ENODEV);
249	}
250
251	/* Silently fail if there is not even the "dmas" property */
252	if (!of_find_property(np, "dmas", NULL))
253		return ERR_PTR(-ENODEV);
254
255	count = of_property_count_strings(np, "dma-names");
256	if (count < 0) {
257		pr_err("%s: dma-names property of node '%s' missing or empty\n",
258			__func__, np->full_name);
259		return ERR_PTR(-ENODEV);
260	}
261
262	for (i = 0; i < count; i++) {
263		if (of_dma_match_channel(np, name, i, &dma_spec))
264			continue;
265
266		mutex_lock(&of_dma_lock);
267		ofdma = of_dma_find_controller(&dma_spec);
268
269		if (ofdma) {
270			chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
271		} else {
272			ret_no_channel = -EPROBE_DEFER;
273			chan = NULL;
274		}
275
276		mutex_unlock(&of_dma_lock);
277
278		of_node_put(dma_spec.np);
279
280		if (chan)
281			return chan;
282	}
283
284	return ERR_PTR(ret_no_channel);
285}
286EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
287
288/**
289 * of_dma_simple_xlate - Simple DMA engine translation function
290 * @dma_spec:	pointer to DMA specifier as found in the device tree
291 * @of_dma:	pointer to DMA controller data
292 *
293 * A simple translation function for devices that use a 32-bit value for the
294 * filter_param when calling the DMA engine dma_request_channel() function.
295 * Note that this translation function requires that #dma-cells is equal to 1
296 * and the argument of the dma specifier is the 32-bit filter_param. Returns
297 * pointer to appropriate dma channel on success or NULL on error.
298 */
299struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
300						struct of_dma *ofdma)
301{
302	int count = dma_spec->args_count;
303	struct of_dma_filter_info *info = ofdma->of_dma_data;
304
305	if (!info || !info->filter_fn)
306		return NULL;
307
308	if (count != 1)
309		return NULL;
310
311	return dma_request_channel(info->dma_cap, info->filter_fn,
312			&dma_spec->args[0]);
313}
314EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
315
316/**
317 * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
318 * @dma_spec:	pointer to DMA specifier as found in the device tree
319 * @of_dma:	pointer to DMA controller data
320 *
321 * This function can be used as the of xlate callback for DMA driver which wants
322 * to match the channel based on the channel id. When using this xlate function
323 * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
324 * The data parameter of of_dma_controller_register must be a pointer to the
325 * dma_device struct the function should match upon.
326 *
327 * Returns pointer to appropriate dma channel on success or NULL on error.
328 */
329struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
330					 struct of_dma *ofdma)
331{
332	struct dma_device *dev = ofdma->of_dma_data;
333	struct dma_chan *chan, *candidate = NULL;
334
335	if (!dev || dma_spec->args_count != 1)
336		return NULL;
337
338	list_for_each_entry(chan, &dev->channels, device_node)
339		if (chan->chan_id == dma_spec->args[0]) {
340			candidate = chan;
341			break;
342		}
343
344	if (!candidate)
345		return NULL;
346
347	return dma_get_slave_channel(candidate);
348}
349EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);
350