1 // SPDX-License-Identifier: GPL-2.0
3 // Freescale ALSA SoC Machine driver utility
5 // Author: Timur Tabi <timur@freescale.com>
7 // Copyright 2010 Freescale Semiconductor, Inc.
10 #include <linux/clk-provider.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <sound/soc.h>
15 #include "fsl_utils.h"
18 * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
20 * @ssi_np: pointer to the SSI device tree node
21 * @name: name of the phandle pointing to the dma channel
22 * @dai: ASoC DAI link pointer to be filled with platform_name
23 * @dma_channel_id: dma channel id to be returned
24 * @dma_id: dma id to be returned
26 * This function determines the dma and channel id for given SSI node. It
27 * also discovers the platform_name for the ASoC DAI link.
29 int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
31 struct snd_soc_dai_link *dai,
32 unsigned int *dma_channel_id,
36 struct device_node *dma_channel_np, *dma_np;
40 dma_channel_np = of_parse_phandle(ssi_np, name, 0);
44 if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
45 of_node_put(dma_channel_np);
49 /* Determine the dev_name for the device_node. This code mimics the
50 * behavior of of_device_make_bus_id(). We need this because ASoC uses
51 * the dev_name() of the device to match the platform (DMA) device with
52 * the CPU (SSI) device. It's all ugly and hackish, but it works (for
55 * dai->platform name should already point to an allocated buffer.
57 ret = of_address_to_resource(dma_channel_np, 0, &res);
59 of_node_put(dma_channel_np);
62 snprintf((char *)dai->platforms->name, DAI_NAME_SIZE, "%llx.%pOFn",
63 (unsigned long long) res.start, dma_channel_np);
65 iprop = of_get_property(dma_channel_np, "cell-index", NULL);
67 of_node_put(dma_channel_np);
70 *dma_channel_id = be32_to_cpup(iprop);
72 dma_np = of_get_parent(dma_channel_np);
73 iprop = of_get_property(dma_np, "cell-index", NULL);
76 of_node_put(dma_channel_np);
79 *dma_id = be32_to_cpup(iprop);
82 of_node_put(dma_channel_np);
86 EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
89 * fsl_asoc_get_pll_clocks - get two PLL clock source
91 * @dev: device pointer
92 * @pll8k_clk: PLL clock pointer for 8kHz
93 * @pll11k_clk: PLL clock pointer for 11kHz
95 * This function get two PLL clock source
97 void fsl_asoc_get_pll_clocks(struct device *dev, struct clk **pll8k_clk,
98 struct clk **pll11k_clk)
100 *pll8k_clk = devm_clk_get(dev, "pll8k");
101 if (IS_ERR(*pll8k_clk))
104 *pll11k_clk = devm_clk_get(dev, "pll11k");
105 if (IS_ERR(*pll11k_clk))
108 EXPORT_SYMBOL(fsl_asoc_get_pll_clocks);
111 * fsl_asoc_reparent_pll_clocks - set clock parent if necessary
113 * @dev: device pointer
114 * @clk: root clock pointer
115 * @pll8k_clk: PLL clock pointer for 8kHz
116 * @pll11k_clk: PLL clock pointer for 11kHz
117 * @ratio: target requency for root clock
119 * This function set root clock parent according to the target ratio
121 void fsl_asoc_reparent_pll_clocks(struct device *dev, struct clk *clk,
122 struct clk *pll8k_clk,
123 struct clk *pll11k_clk, u64 ratio)
125 struct clk *p, *pll = 0, *npll = 0;
126 bool reparent = false;
129 if (!clk || !pll8k_clk || !pll11k_clk)
133 while (p && pll8k_clk && pll11k_clk) {
134 struct clk *pp = clk_get_parent(p);
136 if (clk_is_match(pp, pll8k_clk) ||
137 clk_is_match(pp, pll11k_clk)) {
144 npll = (do_div(ratio, 8000) ? pll11k_clk : pll8k_clk);
145 reparent = (pll && !clk_is_match(pll, npll));
148 ret = clk_set_parent(p, npll);
150 dev_warn(dev, "failed to set parent %s: %d\n", __clk_get_name(npll), ret);
153 EXPORT_SYMBOL(fsl_asoc_reparent_pll_clocks);
155 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
156 MODULE_DESCRIPTION("Freescale ASoC utility code");
157 MODULE_LICENSE("GPL v2");