xref: /OK3568_Linux_fs/kernel/sound/soc/fsl/fsl_utils.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Freescale ALSA SoC Machine driver utility
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Author: Timur Tabi <timur@freescale.com>
6*4882a593Smuzhiyun //
7*4882a593Smuzhiyun // Copyright 2010 Freescale Semiconductor, Inc.
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/of_address.h>
11*4882a593Smuzhiyun #include <sound/soc.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "fsl_utils.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun  * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * @ssi_np: pointer to the SSI device tree node
19*4882a593Smuzhiyun  * @name: name of the phandle pointing to the dma channel
20*4882a593Smuzhiyun  * @dai: ASoC DAI link pointer to be filled with platform_name
21*4882a593Smuzhiyun  * @dma_channel_id: dma channel id to be returned
22*4882a593Smuzhiyun  * @dma_id: dma id to be returned
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This function determines the dma and channel id for given SSI node.  It
25*4882a593Smuzhiyun  * also discovers the platform_name for the ASoC DAI link.
26*4882a593Smuzhiyun  */
fsl_asoc_get_dma_channel(struct device_node * ssi_np,const char * name,struct snd_soc_dai_link * dai,unsigned int * dma_channel_id,unsigned int * dma_id)27*4882a593Smuzhiyun int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
28*4882a593Smuzhiyun 			     const char *name,
29*4882a593Smuzhiyun 			     struct snd_soc_dai_link *dai,
30*4882a593Smuzhiyun 			     unsigned int *dma_channel_id,
31*4882a593Smuzhiyun 			     unsigned int *dma_id)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct resource res;
34*4882a593Smuzhiyun 	struct device_node *dma_channel_np, *dma_np;
35*4882a593Smuzhiyun 	const __be32 *iprop;
36*4882a593Smuzhiyun 	int ret;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	dma_channel_np = of_parse_phandle(ssi_np, name, 0);
39*4882a593Smuzhiyun 	if (!dma_channel_np)
40*4882a593Smuzhiyun 		return -EINVAL;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
43*4882a593Smuzhiyun 		of_node_put(dma_channel_np);
44*4882a593Smuzhiyun 		return -EINVAL;
45*4882a593Smuzhiyun 	}
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/* Determine the dev_name for the device_node.  This code mimics the
48*4882a593Smuzhiyun 	 * behavior of of_device_make_bus_id(). We need this because ASoC uses
49*4882a593Smuzhiyun 	 * the dev_name() of the device to match the platform (DMA) device with
50*4882a593Smuzhiyun 	 * the CPU (SSI) device.  It's all ugly and hackish, but it works (for
51*4882a593Smuzhiyun 	 * now).
52*4882a593Smuzhiyun 	 *
53*4882a593Smuzhiyun 	 * dai->platform name should already point to an allocated buffer.
54*4882a593Smuzhiyun 	 */
55*4882a593Smuzhiyun 	ret = of_address_to_resource(dma_channel_np, 0, &res);
56*4882a593Smuzhiyun 	if (ret) {
57*4882a593Smuzhiyun 		of_node_put(dma_channel_np);
58*4882a593Smuzhiyun 		return ret;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 	snprintf((char *)dai->platforms->name, DAI_NAME_SIZE, "%llx.%pOFn",
61*4882a593Smuzhiyun 		 (unsigned long long) res.start, dma_channel_np);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	iprop = of_get_property(dma_channel_np, "cell-index", NULL);
64*4882a593Smuzhiyun 	if (!iprop) {
65*4882a593Smuzhiyun 		of_node_put(dma_channel_np);
66*4882a593Smuzhiyun 		return -EINVAL;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	*dma_channel_id = be32_to_cpup(iprop);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	dma_np = of_get_parent(dma_channel_np);
71*4882a593Smuzhiyun 	iprop = of_get_property(dma_np, "cell-index", NULL);
72*4882a593Smuzhiyun 	if (!iprop) {
73*4882a593Smuzhiyun 		of_node_put(dma_np);
74*4882a593Smuzhiyun 		of_node_put(dma_channel_np);
75*4882a593Smuzhiyun 		return -EINVAL;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	*dma_id = be32_to_cpup(iprop);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	of_node_put(dma_np);
80*4882a593Smuzhiyun 	of_node_put(dma_channel_np);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
87*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale ASoC utility code");
88*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
89