xref: /openbmc/linux/drivers/dma/of-dma.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25fa422c9SVinod Koul /*
35fa422c9SVinod Koul  * Device tree helpers for DMA request / controller
45fa422c9SVinod Koul  *
55fa422c9SVinod Koul  * Based on of_gpio.c
65fa422c9SVinod Koul  *
75fa422c9SVinod Koul  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
85fa422c9SVinod Koul  */
95fa422c9SVinod Koul 
105fa422c9SVinod Koul #include <linux/device.h>
115fa422c9SVinod Koul #include <linux/err.h>
125fa422c9SVinod Koul #include <linux/module.h>
13de61608aSLars-Peter Clausen #include <linux/mutex.h>
145fa422c9SVinod Koul #include <linux/slab.h>
155fa422c9SVinod Koul #include <linux/of.h>
165fa422c9SVinod Koul #include <linux/of_dma.h>
175fa422c9SVinod Koul 
18c3c431deSGeert Uytterhoeven #include "dmaengine.h"
19c3c431deSGeert Uytterhoeven 
205fa422c9SVinod Koul static LIST_HEAD(of_dma_list);
21de61608aSLars-Peter Clausen static DEFINE_MUTEX(of_dma_lock);
225fa422c9SVinod Koul 
235fa422c9SVinod Koul /**
24de61608aSLars-Peter Clausen  * of_dma_find_controller - Get a DMA controller in DT DMA helpers list
255fa422c9SVinod Koul  * @dma_spec:	pointer to DMA specifier as found in the device tree
265fa422c9SVinod Koul  *
275fa422c9SVinod Koul  * Finds a DMA controller with matching device node and number for dma cells
28de61608aSLars-Peter Clausen  * in a list of registered DMA controllers. If a match is found a valid pointer
29de61608aSLars-Peter Clausen  * to the DMA data stored is retuned. A NULL pointer is returned if no match is
30de61608aSLars-Peter Clausen  * found.
315fa422c9SVinod Koul  */
of_dma_find_controller(struct of_phandle_args * dma_spec)32de61608aSLars-Peter Clausen static struct of_dma *of_dma_find_controller(struct of_phandle_args *dma_spec)
335fa422c9SVinod Koul {
345fa422c9SVinod Koul 	struct of_dma *ofdma;
355fa422c9SVinod Koul 
365fa422c9SVinod Koul 	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
378552bb4fSLars-Peter Clausen 		if (ofdma->of_node == dma_spec->np)
385fa422c9SVinod Koul 			return ofdma;
395fa422c9SVinod Koul 
40c6c93048SRob Herring 	pr_debug("%s: can't find DMA controller %pOF\n", __func__,
41c6c93048SRob Herring 		 dma_spec->np);
425fa422c9SVinod Koul 
435fa422c9SVinod Koul 	return NULL;
445fa422c9SVinod Koul }
455fa422c9SVinod Koul 
465fa422c9SVinod Koul /**
4756f13c0dSPeter Ujfalusi  * of_dma_router_xlate - translation function for router devices
4856f13c0dSPeter Ujfalusi  * @dma_spec:	pointer to DMA specifier as found in the device tree
497d8c9148SLee Jones  * @ofdma:	pointer to DMA controller data (router information)
5056f13c0dSPeter Ujfalusi  *
5156f13c0dSPeter Ujfalusi  * The function creates new dma_spec to be passed to the router driver's
5256f13c0dSPeter Ujfalusi  * of_dma_route_allocate() function to prepare a dma_spec which will be used
5356f13c0dSPeter Ujfalusi  * to request channel from the real DMA controller.
5456f13c0dSPeter Ujfalusi  */
of_dma_router_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)5556f13c0dSPeter Ujfalusi static struct dma_chan *of_dma_router_xlate(struct of_phandle_args *dma_spec,
5656f13c0dSPeter Ujfalusi 					    struct of_dma *ofdma)
5756f13c0dSPeter Ujfalusi {
5856f13c0dSPeter Ujfalusi 	struct dma_chan		*chan;
5956f13c0dSPeter Ujfalusi 	struct of_dma		*ofdma_target;
6056f13c0dSPeter Ujfalusi 	struct of_phandle_args	dma_spec_target;
6156f13c0dSPeter Ujfalusi 	void			*route_data;
6256f13c0dSPeter Ujfalusi 
6356f13c0dSPeter Ujfalusi 	/* translate the request for the real DMA controller */
6456f13c0dSPeter Ujfalusi 	memcpy(&dma_spec_target, dma_spec, sizeof(dma_spec_target));
6556f13c0dSPeter Ujfalusi 	route_data = ofdma->of_dma_route_allocate(&dma_spec_target, ofdma);
6656f13c0dSPeter Ujfalusi 	if (IS_ERR(route_data))
6756f13c0dSPeter Ujfalusi 		return NULL;
6856f13c0dSPeter Ujfalusi 
6956f13c0dSPeter Ujfalusi 	ofdma_target = of_dma_find_controller(&dma_spec_target);
70eda97cb0SPeter Ujfalusi 	if (!ofdma_target) {
71eda97cb0SPeter Ujfalusi 		ofdma->dma_router->route_free(ofdma->dma_router->dev,
72eda97cb0SPeter Ujfalusi 					      route_data);
73eda97cb0SPeter Ujfalusi 		chan = ERR_PTR(-EPROBE_DEFER);
74eda97cb0SPeter Ujfalusi 		goto err;
75eda97cb0SPeter Ujfalusi 	}
7656f13c0dSPeter Ujfalusi 
7756f13c0dSPeter Ujfalusi 	chan = ofdma_target->of_dma_xlate(&dma_spec_target, ofdma_target);
785b2aa9f9SPeter Ujfalusi 	if (IS_ERR_OR_NULL(chan)) {
7956f13c0dSPeter Ujfalusi 		ofdma->dma_router->route_free(ofdma->dma_router->dev,
8056f13c0dSPeter Ujfalusi 					      route_data);
815b2aa9f9SPeter Ujfalusi 	} else {
824f910c03SPeter Ujfalusi 		int ret = 0;
834f910c03SPeter Ujfalusi 
845b2aa9f9SPeter Ujfalusi 		chan->router = ofdma->dma_router;
855b2aa9f9SPeter Ujfalusi 		chan->route_data = route_data;
864f910c03SPeter Ujfalusi 
874f910c03SPeter Ujfalusi 		if (chan->device->device_router_config)
884f910c03SPeter Ujfalusi 			ret = chan->device->device_router_config(chan);
894f910c03SPeter Ujfalusi 
904f910c03SPeter Ujfalusi 		if (ret) {
914f910c03SPeter Ujfalusi 			dma_release_channel(chan);
924f910c03SPeter Ujfalusi 			chan = ERR_PTR(ret);
934f910c03SPeter Ujfalusi 		}
9456f13c0dSPeter Ujfalusi 	}
9556f13c0dSPeter Ujfalusi 
96eda97cb0SPeter Ujfalusi err:
9756f13c0dSPeter Ujfalusi 	/*
9856f13c0dSPeter Ujfalusi 	 * Need to put the node back since the ofdma->of_dma_route_allocate
9956f13c0dSPeter Ujfalusi 	 * has taken it for generating the new, translated dma_spec
10056f13c0dSPeter Ujfalusi 	 */
10156f13c0dSPeter Ujfalusi 	of_node_put(dma_spec_target.np);
10256f13c0dSPeter Ujfalusi 	return chan;
10356f13c0dSPeter Ujfalusi }
10456f13c0dSPeter Ujfalusi 
10556f13c0dSPeter Ujfalusi /**
1065fa422c9SVinod Koul  * of_dma_controller_register - Register a DMA controller to DT DMA helpers
1075fa422c9SVinod Koul  * @np:			device node of DMA controller
1085fa422c9SVinod Koul  * @of_dma_xlate:	translation function which converts a phandle
1095fa422c9SVinod Koul  *			arguments list into a dma_chan structure
1107d8c9148SLee Jones  * @data:		pointer to controller specific data to be used by
1115fa422c9SVinod Koul  *			translation function
1125fa422c9SVinod Koul  *
1135fa422c9SVinod Koul  * Returns 0 on success or appropriate errno value on error.
1145fa422c9SVinod Koul  *
1155fa422c9SVinod Koul  * Allocated memory should be freed with appropriate of_dma_controller_free()
1165fa422c9SVinod Koul  * call.
1175fa422c9SVinod Koul  */
of_dma_controller_register(struct device_node * np,struct dma_chan * (* of_dma_xlate)(struct of_phandle_args *,struct of_dma *),void * data)1185fa422c9SVinod Koul int of_dma_controller_register(struct device_node *np,
1195fa422c9SVinod Koul 				struct dma_chan *(*of_dma_xlate)
1205fa422c9SVinod Koul 				(struct of_phandle_args *, struct of_dma *),
1215fa422c9SVinod Koul 				void *data)
1225fa422c9SVinod Koul {
1235fa422c9SVinod Koul 	struct of_dma	*ofdma;
1245fa422c9SVinod Koul 
1255fa422c9SVinod Koul 	if (!np || !of_dma_xlate) {
1265fa422c9SVinod Koul 		pr_err("%s: not enough information provided\n", __func__);
1275fa422c9SVinod Koul 		return -EINVAL;
1285fa422c9SVinod Koul 	}
1295fa422c9SVinod Koul 
1305fa422c9SVinod Koul 	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
1315fa422c9SVinod Koul 	if (!ofdma)
1325fa422c9SVinod Koul 		return -ENOMEM;
1335fa422c9SVinod Koul 
1345fa422c9SVinod Koul 	ofdma->of_node = np;
1355fa422c9SVinod Koul 	ofdma->of_dma_xlate = of_dma_xlate;
1365fa422c9SVinod Koul 	ofdma->of_dma_data = data;
1375fa422c9SVinod Koul 
1385fa422c9SVinod Koul 	/* Now queue of_dma controller structure in list */
139de61608aSLars-Peter Clausen 	mutex_lock(&of_dma_lock);
1405fa422c9SVinod Koul 	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
141de61608aSLars-Peter Clausen 	mutex_unlock(&of_dma_lock);
1425fa422c9SVinod Koul 
1435fa422c9SVinod Koul 	return 0;
1445fa422c9SVinod Koul }
1455fa422c9SVinod Koul EXPORT_SYMBOL_GPL(of_dma_controller_register);
1465fa422c9SVinod Koul 
1475fa422c9SVinod Koul /**
1485fa422c9SVinod Koul  * of_dma_controller_free - Remove a DMA controller from DT DMA helpers list
1495fa422c9SVinod Koul  * @np:		device node of DMA controller
1505fa422c9SVinod Koul  *
1515fa422c9SVinod Koul  * Memory allocated by of_dma_controller_register() is freed here.
1525fa422c9SVinod Koul  */
of_dma_controller_free(struct device_node * np)153de61608aSLars-Peter Clausen void of_dma_controller_free(struct device_node *np)
1545fa422c9SVinod Koul {
1555fa422c9SVinod Koul 	struct of_dma *ofdma;
1565fa422c9SVinod Koul 
157de61608aSLars-Peter Clausen 	mutex_lock(&of_dma_lock);
1585fa422c9SVinod Koul 
1595fa422c9SVinod Koul 	list_for_each_entry(ofdma, &of_dma_list, of_dma_controllers)
1605fa422c9SVinod Koul 		if (ofdma->of_node == np) {
1615fa422c9SVinod Koul 			list_del(&ofdma->of_dma_controllers);
1625fa422c9SVinod Koul 			kfree(ofdma);
163de61608aSLars-Peter Clausen 			break;
1645fa422c9SVinod Koul 		}
1655fa422c9SVinod Koul 
166de61608aSLars-Peter Clausen 	mutex_unlock(&of_dma_lock);
1675fa422c9SVinod Koul }
1685fa422c9SVinod Koul EXPORT_SYMBOL_GPL(of_dma_controller_free);
1695fa422c9SVinod Koul 
1705fa422c9SVinod Koul /**
17156f13c0dSPeter Ujfalusi  * of_dma_router_register - Register a DMA router to DT DMA helpers as a
17256f13c0dSPeter Ujfalusi  *			    controller
17356f13c0dSPeter Ujfalusi  * @np:				device node of DMA router
17456f13c0dSPeter Ujfalusi  * @of_dma_route_allocate:	setup function for the router which need to
17556f13c0dSPeter Ujfalusi  *				modify the dma_spec for the DMA controller to
17656f13c0dSPeter Ujfalusi  *				use and to set up the requested route.
17756f13c0dSPeter Ujfalusi  * @dma_router:			pointer to dma_router structure to be used when
17856f13c0dSPeter Ujfalusi  *				the route need to be free up.
17956f13c0dSPeter Ujfalusi  *
18056f13c0dSPeter Ujfalusi  * Returns 0 on success or appropriate errno value on error.
18156f13c0dSPeter Ujfalusi  *
18256f13c0dSPeter Ujfalusi  * Allocated memory should be freed with appropriate of_dma_controller_free()
18356f13c0dSPeter Ujfalusi  * call.
18456f13c0dSPeter Ujfalusi  */
of_dma_router_register(struct device_node * np,void * (* of_dma_route_allocate)(struct of_phandle_args *,struct of_dma *),struct dma_router * dma_router)18556f13c0dSPeter Ujfalusi int of_dma_router_register(struct device_node *np,
18656f13c0dSPeter Ujfalusi 			   void *(*of_dma_route_allocate)
18756f13c0dSPeter Ujfalusi 			   (struct of_phandle_args *, struct of_dma *),
18856f13c0dSPeter Ujfalusi 			   struct dma_router *dma_router)
18956f13c0dSPeter Ujfalusi {
19056f13c0dSPeter Ujfalusi 	struct of_dma	*ofdma;
19156f13c0dSPeter Ujfalusi 
19256f13c0dSPeter Ujfalusi 	if (!np || !of_dma_route_allocate || !dma_router) {
19356f13c0dSPeter Ujfalusi 		pr_err("%s: not enough information provided\n", __func__);
19456f13c0dSPeter Ujfalusi 		return -EINVAL;
19556f13c0dSPeter Ujfalusi 	}
19656f13c0dSPeter Ujfalusi 
19756f13c0dSPeter Ujfalusi 	ofdma = kzalloc(sizeof(*ofdma), GFP_KERNEL);
19856f13c0dSPeter Ujfalusi 	if (!ofdma)
19956f13c0dSPeter Ujfalusi 		return -ENOMEM;
20056f13c0dSPeter Ujfalusi 
20156f13c0dSPeter Ujfalusi 	ofdma->of_node = np;
20256f13c0dSPeter Ujfalusi 	ofdma->of_dma_xlate = of_dma_router_xlate;
20356f13c0dSPeter Ujfalusi 	ofdma->of_dma_route_allocate = of_dma_route_allocate;
20456f13c0dSPeter Ujfalusi 	ofdma->dma_router = dma_router;
20556f13c0dSPeter Ujfalusi 
20656f13c0dSPeter Ujfalusi 	/* Now queue of_dma controller structure in list */
20756f13c0dSPeter Ujfalusi 	mutex_lock(&of_dma_lock);
20856f13c0dSPeter Ujfalusi 	list_add_tail(&ofdma->of_dma_controllers, &of_dma_list);
20956f13c0dSPeter Ujfalusi 	mutex_unlock(&of_dma_lock);
21056f13c0dSPeter Ujfalusi 
21156f13c0dSPeter Ujfalusi 	return 0;
21256f13c0dSPeter Ujfalusi }
21356f13c0dSPeter Ujfalusi EXPORT_SYMBOL_GPL(of_dma_router_register);
21456f13c0dSPeter Ujfalusi 
21556f13c0dSPeter Ujfalusi /**
2165fa422c9SVinod Koul  * of_dma_match_channel - Check if a DMA specifier matches name
2175fa422c9SVinod Koul  * @np:		device node to look for DMA channels
2185fa422c9SVinod Koul  * @name:	channel name to be matched
2195fa422c9SVinod Koul  * @index:	index of DMA specifier in list of DMA specifiers
2205fa422c9SVinod Koul  * @dma_spec:	pointer to DMA specifier as found in the device tree
2215fa422c9SVinod Koul  *
2225fa422c9SVinod Koul  * Check if the DMA specifier pointed to by the index in a list of DMA
2235fa422c9SVinod Koul  * specifiers, matches the name provided. Returns 0 if the name matches and
2245fa422c9SVinod Koul  * a valid pointer to the DMA specifier is found. Otherwise returns -ENODEV.
2255fa422c9SVinod Koul  */
of_dma_match_channel(struct device_node * np,const char * name,int index,struct of_phandle_args * dma_spec)226bef29ec5SMarkus Pargmann static int of_dma_match_channel(struct device_node *np, const char *name,
227bef29ec5SMarkus Pargmann 				int index, struct of_phandle_args *dma_spec)
2285fa422c9SVinod Koul {
2295fa422c9SVinod Koul 	const char *s;
2305fa422c9SVinod Koul 
2315fa422c9SVinod Koul 	if (of_property_read_string_index(np, "dma-names", index, &s))
2325fa422c9SVinod Koul 		return -ENODEV;
2335fa422c9SVinod Koul 
2345fa422c9SVinod Koul 	if (strcmp(name, s))
2355fa422c9SVinod Koul 		return -ENODEV;
2365fa422c9SVinod Koul 
2375fa422c9SVinod Koul 	if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
2385fa422c9SVinod Koul 				       dma_spec))
2395fa422c9SVinod Koul 		return -ENODEV;
2405fa422c9SVinod Koul 
2415fa422c9SVinod Koul 	return 0;
2425fa422c9SVinod Koul }
2435fa422c9SVinod Koul 
2445fa422c9SVinod Koul /**
2455fa422c9SVinod Koul  * of_dma_request_slave_channel - Get the DMA slave channel
2465fa422c9SVinod Koul  * @np:		device node to get DMA request from
2475fa422c9SVinod Koul  * @name:	name of desired channel
2485fa422c9SVinod Koul  *
2490ad7c000SStephen Warren  * Returns pointer to appropriate DMA channel on success or an error pointer.
2505fa422c9SVinod Koul  */
of_dma_request_slave_channel(struct device_node * np,const char * name)2515fa422c9SVinod Koul struct dma_chan *of_dma_request_slave_channel(struct device_node *np,
252bef29ec5SMarkus Pargmann 					      const char *name)
2535fa422c9SVinod Koul {
2545fa422c9SVinod Koul 	struct of_phandle_args	dma_spec;
2555fa422c9SVinod Koul 	struct of_dma		*ofdma;
2565fa422c9SVinod Koul 	struct dma_chan		*chan;
25720ea6be6SNiklas Söderlund 	int			count, i, start;
2580ad7c000SStephen Warren 	int			ret_no_channel = -ENODEV;
25920ea6be6SNiklas Söderlund 	static atomic_t		last_index;
2605fa422c9SVinod Koul 
2615fa422c9SVinod Koul 	if (!np || !name) {
2625fa422c9SVinod Koul 		pr_err("%s: not enough information provided\n", __func__);
2630ad7c000SStephen Warren 		return ERR_PTR(-ENODEV);
2645fa422c9SVinod Koul 	}
2655fa422c9SVinod Koul 
266c914570fSWolfram Sang 	/* Silently fail if there is not even the "dmas" property */
267*3765af04SRob Herring 	if (!of_property_present(np, "dmas"))
268c914570fSWolfram Sang 		return ERR_PTR(-ENODEV);
269c914570fSWolfram Sang 
2705fa422c9SVinod Koul 	count = of_property_count_strings(np, "dma-names");
2715fa422c9SVinod Koul 	if (count < 0) {
272c6c93048SRob Herring 		pr_err("%s: dma-names property of node '%pOF' missing or empty\n",
273c6c93048SRob Herring 			__func__, np);
2740ad7c000SStephen Warren 		return ERR_PTR(-ENODEV);
2755fa422c9SVinod Koul 	}
2765fa422c9SVinod Koul 
27720ea6be6SNiklas Söderlund 	/*
27820ea6be6SNiklas Söderlund 	 * approximate an average distribution across multiple
27920ea6be6SNiklas Söderlund 	 * entries with the same name
28020ea6be6SNiklas Söderlund 	 */
28120ea6be6SNiklas Söderlund 	start = atomic_inc_return(&last_index);
2825fa422c9SVinod Koul 	for (i = 0; i < count; i++) {
28320ea6be6SNiklas Söderlund 		if (of_dma_match_channel(np, name,
28420ea6be6SNiklas Söderlund 					 (i + start) % count,
28520ea6be6SNiklas Söderlund 					 &dma_spec))
2865fa422c9SVinod Koul 			continue;
2875fa422c9SVinod Koul 
288de61608aSLars-Peter Clausen 		mutex_lock(&of_dma_lock);
289de61608aSLars-Peter Clausen 		ofdma = of_dma_find_controller(&dma_spec);
2905fa422c9SVinod Koul 
2910ad7c000SStephen Warren 		if (ofdma) {
2925fa422c9SVinod Koul 			chan = ofdma->of_dma_xlate(&dma_spec, ofdma);
2930ad7c000SStephen Warren 		} else {
2940ad7c000SStephen Warren 			ret_no_channel = -EPROBE_DEFER;
295f22eb140SLars-Peter Clausen 			chan = NULL;
2960ad7c000SStephen Warren 		}
297de61608aSLars-Peter Clausen 
298de61608aSLars-Peter Clausen 		mutex_unlock(&of_dma_lock);
2995fa422c9SVinod Koul 
3005fa422c9SVinod Koul 		of_node_put(dma_spec.np);
3015fa422c9SVinod Koul 
3025fa422c9SVinod Koul 		if (chan)
3035fa422c9SVinod Koul 			return chan;
3045fa422c9SVinod Koul 	}
3055fa422c9SVinod Koul 
3060ad7c000SStephen Warren 	return ERR_PTR(ret_no_channel);
3075fa422c9SVinod Koul }
3080aed1124SKuninori Morimoto EXPORT_SYMBOL_GPL(of_dma_request_slave_channel);
3095fa422c9SVinod Koul 
3105fa422c9SVinod Koul /**
3115fa422c9SVinod Koul  * of_dma_simple_xlate - Simple DMA engine translation function
3125fa422c9SVinod Koul  * @dma_spec:	pointer to DMA specifier as found in the device tree
3137d8c9148SLee Jones  * @ofdma:	pointer to DMA controller data
3145fa422c9SVinod Koul  *
3155fa422c9SVinod Koul  * A simple translation function for devices that use a 32-bit value for the
3165fa422c9SVinod Koul  * filter_param when calling the DMA engine dma_request_channel() function.
3175fa422c9SVinod Koul  * Note that this translation function requires that #dma-cells is equal to 1
3185fa422c9SVinod Koul  * and the argument of the dma specifier is the 32-bit filter_param. Returns
3195fa422c9SVinod Koul  * pointer to appropriate dma channel on success or NULL on error.
3205fa422c9SVinod Koul  */
of_dma_simple_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)3215fa422c9SVinod Koul struct dma_chan *of_dma_simple_xlate(struct of_phandle_args *dma_spec,
3225fa422c9SVinod Koul 						struct of_dma *ofdma)
3235fa422c9SVinod Koul {
3245fa422c9SVinod Koul 	int count = dma_spec->args_count;
3255fa422c9SVinod Koul 	struct of_dma_filter_info *info = ofdma->of_dma_data;
3265fa422c9SVinod Koul 
3275fa422c9SVinod Koul 	if (!info || !info->filter_fn)
3285fa422c9SVinod Koul 		return NULL;
3295fa422c9SVinod Koul 
3305fa422c9SVinod Koul 	if (count != 1)
3315fa422c9SVinod Koul 		return NULL;
3325fa422c9SVinod Koul 
333f5151311SBaolin Wang 	return __dma_request_channel(&info->dma_cap, info->filter_fn,
334f5151311SBaolin Wang 				     &dma_spec->args[0], dma_spec->np);
3355fa422c9SVinod Koul }
3365fa422c9SVinod Koul EXPORT_SYMBOL_GPL(of_dma_simple_xlate);
33716369efbSAlexander Popov 
33816369efbSAlexander Popov /**
33916369efbSAlexander Popov  * of_dma_xlate_by_chan_id - Translate dt property to DMA channel by channel id
34016369efbSAlexander Popov  * @dma_spec:	pointer to DMA specifier as found in the device tree
3417d8c9148SLee Jones  * @ofdma:	pointer to DMA controller data
34216369efbSAlexander Popov  *
34316369efbSAlexander Popov  * This function can be used as the of xlate callback for DMA driver which wants
34416369efbSAlexander Popov  * to match the channel based on the channel id. When using this xlate function
34516369efbSAlexander Popov  * the #dma-cells propety of the DMA controller dt node needs to be set to 1.
34616369efbSAlexander Popov  * The data parameter of of_dma_controller_register must be a pointer to the
34716369efbSAlexander Popov  * dma_device struct the function should match upon.
34816369efbSAlexander Popov  *
34916369efbSAlexander Popov  * Returns pointer to appropriate dma channel on success or NULL on error.
35016369efbSAlexander Popov  */
of_dma_xlate_by_chan_id(struct of_phandle_args * dma_spec,struct of_dma * ofdma)35116369efbSAlexander Popov struct dma_chan *of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
35216369efbSAlexander Popov 					 struct of_dma *ofdma)
35316369efbSAlexander Popov {
35416369efbSAlexander Popov 	struct dma_device *dev = ofdma->of_dma_data;
35516369efbSAlexander Popov 	struct dma_chan *chan, *candidate = NULL;
35616369efbSAlexander Popov 
35716369efbSAlexander Popov 	if (!dev || dma_spec->args_count != 1)
35816369efbSAlexander Popov 		return NULL;
35916369efbSAlexander Popov 
36016369efbSAlexander Popov 	list_for_each_entry(chan, &dev->channels, device_node)
36116369efbSAlexander Popov 		if (chan->chan_id == dma_spec->args[0]) {
36216369efbSAlexander Popov 			candidate = chan;
36316369efbSAlexander Popov 			break;
36416369efbSAlexander Popov 		}
36516369efbSAlexander Popov 
36616369efbSAlexander Popov 	if (!candidate)
36716369efbSAlexander Popov 		return NULL;
36816369efbSAlexander Popov 
36916369efbSAlexander Popov 	return dma_get_slave_channel(candidate);
37016369efbSAlexander Popov }
37116369efbSAlexander Popov EXPORT_SYMBOL_GPL(of_dma_xlate_by_chan_id);
372