162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// Freescale ALSA SoC Machine driver utility
462306a36Sopenharmony_ci//
562306a36Sopenharmony_ci// Author: Timur Tabi <timur@freescale.com>
662306a36Sopenharmony_ci//
762306a36Sopenharmony_ci// Copyright 2010 Freescale Semiconductor, Inc.
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/clk.h>
1062306a36Sopenharmony_ci#include <linux/clk-provider.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/of_address.h>
1362306a36Sopenharmony_ci#include <sound/soc.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "fsl_utils.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/**
1862306a36Sopenharmony_ci * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * @ssi_np: pointer to the SSI device tree node
2162306a36Sopenharmony_ci * @name: name of the phandle pointing to the dma channel
2262306a36Sopenharmony_ci * @dai: ASoC DAI link pointer to be filled with platform_name
2362306a36Sopenharmony_ci * @dma_channel_id: dma channel id to be returned
2462306a36Sopenharmony_ci * @dma_id: dma id to be returned
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * This function determines the dma and channel id for given SSI node.  It
2762306a36Sopenharmony_ci * also discovers the platform_name for the ASoC DAI link.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ciint fsl_asoc_get_dma_channel(struct device_node *ssi_np,
3062306a36Sopenharmony_ci			     const char *name,
3162306a36Sopenharmony_ci			     struct snd_soc_dai_link *dai,
3262306a36Sopenharmony_ci			     unsigned int *dma_channel_id,
3362306a36Sopenharmony_ci			     unsigned int *dma_id)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	struct resource res;
3662306a36Sopenharmony_ci	struct device_node *dma_channel_np, *dma_np;
3762306a36Sopenharmony_ci	const __be32 *iprop;
3862306a36Sopenharmony_ci	int ret;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	dma_channel_np = of_parse_phandle(ssi_np, name, 0);
4162306a36Sopenharmony_ci	if (!dma_channel_np)
4262306a36Sopenharmony_ci		return -EINVAL;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
4562306a36Sopenharmony_ci		of_node_put(dma_channel_np);
4662306a36Sopenharmony_ci		return -EINVAL;
4762306a36Sopenharmony_ci	}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	/* Determine the dev_name for the device_node.  This code mimics the
5062306a36Sopenharmony_ci	 * behavior of of_device_make_bus_id(). We need this because ASoC uses
5162306a36Sopenharmony_ci	 * the dev_name() of the device to match the platform (DMA) device with
5262306a36Sopenharmony_ci	 * the CPU (SSI) device.  It's all ugly and hackish, but it works (for
5362306a36Sopenharmony_ci	 * now).
5462306a36Sopenharmony_ci	 *
5562306a36Sopenharmony_ci	 * dai->platform name should already point to an allocated buffer.
5662306a36Sopenharmony_ci	 */
5762306a36Sopenharmony_ci	ret = of_address_to_resource(dma_channel_np, 0, &res);
5862306a36Sopenharmony_ci	if (ret) {
5962306a36Sopenharmony_ci		of_node_put(dma_channel_np);
6062306a36Sopenharmony_ci		return ret;
6162306a36Sopenharmony_ci	}
6262306a36Sopenharmony_ci	snprintf((char *)dai->platforms->name, DAI_NAME_SIZE, "%llx.%pOFn",
6362306a36Sopenharmony_ci		 (unsigned long long) res.start, dma_channel_np);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	iprop = of_get_property(dma_channel_np, "cell-index", NULL);
6662306a36Sopenharmony_ci	if (!iprop) {
6762306a36Sopenharmony_ci		of_node_put(dma_channel_np);
6862306a36Sopenharmony_ci		return -EINVAL;
6962306a36Sopenharmony_ci	}
7062306a36Sopenharmony_ci	*dma_channel_id = be32_to_cpup(iprop);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	dma_np = of_get_parent(dma_channel_np);
7362306a36Sopenharmony_ci	iprop = of_get_property(dma_np, "cell-index", NULL);
7462306a36Sopenharmony_ci	if (!iprop) {
7562306a36Sopenharmony_ci		of_node_put(dma_np);
7662306a36Sopenharmony_ci		of_node_put(dma_channel_np);
7762306a36Sopenharmony_ci		return -EINVAL;
7862306a36Sopenharmony_ci	}
7962306a36Sopenharmony_ci	*dma_id = be32_to_cpup(iprop);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	of_node_put(dma_np);
8262306a36Sopenharmony_ci	of_node_put(dma_channel_np);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	return 0;
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ciEXPORT_SYMBOL(fsl_asoc_get_dma_channel);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/**
8962306a36Sopenharmony_ci * fsl_asoc_get_pll_clocks - get two PLL clock source
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * @dev: device pointer
9262306a36Sopenharmony_ci * @pll8k_clk: PLL clock pointer for 8kHz
9362306a36Sopenharmony_ci * @pll11k_clk: PLL clock pointer for 11kHz
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * This function get two PLL clock source
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_civoid fsl_asoc_get_pll_clocks(struct device *dev, struct clk **pll8k_clk,
9862306a36Sopenharmony_ci			     struct clk **pll11k_clk)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	*pll8k_clk = devm_clk_get(dev, "pll8k");
10162306a36Sopenharmony_ci	if (IS_ERR(*pll8k_clk))
10262306a36Sopenharmony_ci		*pll8k_clk = NULL;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	*pll11k_clk = devm_clk_get(dev, "pll11k");
10562306a36Sopenharmony_ci	if (IS_ERR(*pll11k_clk))
10662306a36Sopenharmony_ci		*pll11k_clk = NULL;
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ciEXPORT_SYMBOL(fsl_asoc_get_pll_clocks);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/**
11162306a36Sopenharmony_ci * fsl_asoc_reparent_pll_clocks - set clock parent if necessary
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci * @dev: device pointer
11462306a36Sopenharmony_ci * @clk: root clock pointer
11562306a36Sopenharmony_ci * @pll8k_clk: PLL clock pointer for 8kHz
11662306a36Sopenharmony_ci * @pll11k_clk: PLL clock pointer for 11kHz
11762306a36Sopenharmony_ci * @ratio: target requency for root clock
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci * This function set root clock parent according to the target ratio
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_civoid fsl_asoc_reparent_pll_clocks(struct device *dev, struct clk *clk,
12262306a36Sopenharmony_ci				  struct clk *pll8k_clk,
12362306a36Sopenharmony_ci				  struct clk *pll11k_clk, u64 ratio)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	struct clk *p, *pll = NULL, *npll = NULL;
12662306a36Sopenharmony_ci	bool reparent = false;
12762306a36Sopenharmony_ci	int ret;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	if (!clk || !pll8k_clk || !pll11k_clk)
13062306a36Sopenharmony_ci		return;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	p = clk;
13362306a36Sopenharmony_ci	while (p && pll8k_clk && pll11k_clk) {
13462306a36Sopenharmony_ci		struct clk *pp = clk_get_parent(p);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci		if (clk_is_match(pp, pll8k_clk) ||
13762306a36Sopenharmony_ci		    clk_is_match(pp, pll11k_clk)) {
13862306a36Sopenharmony_ci			pll = pp;
13962306a36Sopenharmony_ci			break;
14062306a36Sopenharmony_ci		}
14162306a36Sopenharmony_ci		p = pp;
14262306a36Sopenharmony_ci	}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	npll = (do_div(ratio, 8000) ? pll11k_clk : pll8k_clk);
14562306a36Sopenharmony_ci	reparent = (pll && !clk_is_match(pll, npll));
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	if (reparent) {
14862306a36Sopenharmony_ci		ret = clk_set_parent(p, npll);
14962306a36Sopenharmony_ci		if (ret < 0)
15062306a36Sopenharmony_ci			dev_warn(dev, "failed to set parent:%d\n", ret);
15162306a36Sopenharmony_ci	}
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ciEXPORT_SYMBOL(fsl_asoc_reparent_pll_clocks);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciMODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
15662306a36Sopenharmony_ciMODULE_DESCRIPTION("Freescale ASoC utility code");
15762306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
158