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