18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * MMP Audio Clock Controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Lubomir Rintel <lkundrak@v3.sk>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
98c2ecf20Sopenharmony_ci#include <linux/io.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/pm_clock.h>
138c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <dt-bindings/clock/marvell,mmp2-audio.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Audio Controller Registers */
188c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL				0x04
198c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0			0x08
208c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1			0x0c
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* SSPA Audio Control Register */
238c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SYSCLK_SHIFT		0
248c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SYSCLK_DIV_SHIFT		1
258c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA0_MUX_SHIFT		7
268c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA0_SHIFT		8
278c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA0_DIV_SHIFT		9
288c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA1_SHIFT		16
298c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA1_DIV_SHIFT		17
308c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_SSPA1_MUX_SHIFT		23
318c2ecf20Sopenharmony_ci#define SSPA_AUD_CTRL_DIV_MASK			0x7e
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* SSPA Audio PLL Control 0 Register */
348c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO_MASK (0x7 << 28)
358c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(x)	((x) << 28)
368c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_FRACT_MASK		(0xfffff << 8)
378c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_FRACT(x)		((x) << 8)
388c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_ENA_DITHER		(1 << 7)
398c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_ICP_2UA		(0 << 5)
408c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_ICP_5UA		(1 << 5)
418c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_ICP_7UA		(2 << 5)
428c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_ICP_10UA		(3 << 5)
438c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_FBCCLK_MASK	(0x3 << 3)
448c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(x)	((x) << 3)
458c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_MCLK_MASK	(0x1 << 2)
468c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_DIV_MCLK(x)		((x) << 2)
478c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_PD_OVPROT_DIS	(1 << 1)
488c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL0_PU			(1 << 0)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* SSPA Audio PLL Control 1 Register */
518c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_SEL_FAST_CLK		(1 << 24)
528c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_CLK_SEL_MASK		(1 << 11)
538c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL	(1 << 11)
548c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_CLK_SEL_VCXO		(0 << 11)
558c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN_MASK (0x7ff << 0)
568c2ecf20Sopenharmony_ci#define SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(x)	((x) << 0)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct mmp2_audio_clk {
598c2ecf20Sopenharmony_ci	void __iomem *mmio_base;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	struct clk_hw audio_pll_hw;
628c2ecf20Sopenharmony_ci	struct clk_mux sspa_mux;
638c2ecf20Sopenharmony_ci	struct clk_mux sspa1_mux;
648c2ecf20Sopenharmony_ci	struct clk_divider sysclk_div;
658c2ecf20Sopenharmony_ci	struct clk_divider sspa0_div;
668c2ecf20Sopenharmony_ci	struct clk_divider sspa1_div;
678c2ecf20Sopenharmony_ci	struct clk_gate sysclk_gate;
688c2ecf20Sopenharmony_ci	struct clk_gate sspa0_gate;
698c2ecf20Sopenharmony_ci	struct clk_gate sspa1_gate;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	u32 aud_ctrl;
728c2ecf20Sopenharmony_ci	u32 aud_pll_ctrl0;
738c2ecf20Sopenharmony_ci	u32 aud_pll_ctrl1;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	spinlock_t lock;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* Must be last */
788c2ecf20Sopenharmony_ci	struct clk_hw_onecell_data clk_data;
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic const struct {
828c2ecf20Sopenharmony_ci	unsigned long parent_rate;
838c2ecf20Sopenharmony_ci	unsigned long freq_vco;
848c2ecf20Sopenharmony_ci	unsigned char mclk;
858c2ecf20Sopenharmony_ci	unsigned char fbcclk;
868c2ecf20Sopenharmony_ci	unsigned short fract;
878c2ecf20Sopenharmony_ci} predivs[] = {
888c2ecf20Sopenharmony_ci	{ 26000000, 135475200, 0, 0, 0x8a18 },
898c2ecf20Sopenharmony_ci	{ 26000000, 147456000, 0, 1, 0x0da1 },
908c2ecf20Sopenharmony_ci	{ 38400000, 135475200, 1, 2, 0x8208 },
918c2ecf20Sopenharmony_ci	{ 38400000, 147456000, 1, 3, 0xaaaa },
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic const struct {
958c2ecf20Sopenharmony_ci	unsigned char divisor;
968c2ecf20Sopenharmony_ci	unsigned char modulo;
978c2ecf20Sopenharmony_ci	unsigned char pattern;
988c2ecf20Sopenharmony_ci} postdivs[] = {
998c2ecf20Sopenharmony_ci	{   1,	3,  0, },
1008c2ecf20Sopenharmony_ci	{   2,	5,  0, },
1018c2ecf20Sopenharmony_ci	{   4,	0,  0, },
1028c2ecf20Sopenharmony_ci	{   6,	1,  1, },
1038c2ecf20Sopenharmony_ci	{   8,	1,  0, },
1048c2ecf20Sopenharmony_ci	{   9,	1,  2, },
1058c2ecf20Sopenharmony_ci	{  12,	2,  1, },
1068c2ecf20Sopenharmony_ci	{  16,	2,  0, },
1078c2ecf20Sopenharmony_ci	{  18,	2,  2, },
1088c2ecf20Sopenharmony_ci	{  24,	4,  1, },
1098c2ecf20Sopenharmony_ci	{  36,	4,  2, },
1108c2ecf20Sopenharmony_ci	{  48,	6,  1, },
1118c2ecf20Sopenharmony_ci	{  72,	6,  2, },
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic unsigned long audio_pll_recalc_rate(struct clk_hw *hw,
1158c2ecf20Sopenharmony_ci					   unsigned long parent_rate)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct mmp2_audio_clk *priv = container_of(hw, struct mmp2_audio_clk, audio_pll_hw);
1188c2ecf20Sopenharmony_ci	unsigned int prediv;
1198c2ecf20Sopenharmony_ci	unsigned int postdiv;
1208c2ecf20Sopenharmony_ci	u32 aud_pll_ctrl0;
1218c2ecf20Sopenharmony_ci	u32 aud_pll_ctrl1;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	aud_pll_ctrl0 = readl(priv->mmio_base + SSPA_AUD_PLL_CTRL0);
1248c2ecf20Sopenharmony_ci	aud_pll_ctrl0 &= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO_MASK |
1258c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL0_FRACT_MASK |
1268c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL0_ENA_DITHER |
1278c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL0_DIV_FBCCLK_MASK |
1288c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL0_DIV_MCLK_MASK |
1298c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL0_PU;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	aud_pll_ctrl1 = readl(priv->mmio_base + SSPA_AUD_PLL_CTRL1);
1328c2ecf20Sopenharmony_ci	aud_pll_ctrl1 &= SSPA_AUD_PLL_CTRL1_CLK_SEL_MASK |
1338c2ecf20Sopenharmony_ci			 SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN_MASK;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for (prediv = 0; prediv < ARRAY_SIZE(predivs); prediv++) {
1368c2ecf20Sopenharmony_ci		if (predivs[prediv].parent_rate != parent_rate)
1378c2ecf20Sopenharmony_ci			continue;
1388c2ecf20Sopenharmony_ci		for (postdiv = 0; postdiv < ARRAY_SIZE(postdivs); postdiv++) {
1398c2ecf20Sopenharmony_ci			unsigned long freq;
1408c2ecf20Sopenharmony_ci			u32 val;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci			val = SSPA_AUD_PLL_CTRL0_ENA_DITHER;
1438c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_PU;
1448c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(postdivs[postdiv].modulo);
1458c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_FRACT(predivs[prediv].fract);
1468c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(predivs[prediv].fbcclk);
1478c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_MCLK(predivs[prediv].mclk);
1488c2ecf20Sopenharmony_ci			if (val != aud_pll_ctrl0)
1498c2ecf20Sopenharmony_ci				continue;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci			val = SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL;
1528c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(postdivs[postdiv].pattern);
1538c2ecf20Sopenharmony_ci			if (val != aud_pll_ctrl1)
1548c2ecf20Sopenharmony_ci				continue;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci			freq = predivs[prediv].freq_vco;
1578c2ecf20Sopenharmony_ci			freq /= postdivs[postdiv].divisor;
1588c2ecf20Sopenharmony_ci			return freq;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic long audio_pll_round_rate(struct clk_hw *hw, unsigned long rate,
1668c2ecf20Sopenharmony_ci				 unsigned long *parent_rate)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	unsigned int prediv;
1698c2ecf20Sopenharmony_ci	unsigned int postdiv;
1708c2ecf20Sopenharmony_ci	long rounded = 0;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	for (prediv = 0; prediv < ARRAY_SIZE(predivs); prediv++) {
1738c2ecf20Sopenharmony_ci		if (predivs[prediv].parent_rate != *parent_rate)
1748c2ecf20Sopenharmony_ci			continue;
1758c2ecf20Sopenharmony_ci		for (postdiv = 0; postdiv < ARRAY_SIZE(postdivs); postdiv++) {
1768c2ecf20Sopenharmony_ci			long freq = predivs[prediv].freq_vco;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci			freq /= postdivs[postdiv].divisor;
1798c2ecf20Sopenharmony_ci			if (freq == rate)
1808c2ecf20Sopenharmony_ci				return rate;
1818c2ecf20Sopenharmony_ci			if (freq < rate)
1828c2ecf20Sopenharmony_ci				continue;
1838c2ecf20Sopenharmony_ci			if (rounded && freq > rounded)
1848c2ecf20Sopenharmony_ci				continue;
1858c2ecf20Sopenharmony_ci			rounded = freq;
1868c2ecf20Sopenharmony_ci		}
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return rounded;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int audio_pll_set_rate(struct clk_hw *hw, unsigned long rate,
1938c2ecf20Sopenharmony_ci			      unsigned long parent_rate)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct mmp2_audio_clk *priv = container_of(hw, struct mmp2_audio_clk, audio_pll_hw);
1968c2ecf20Sopenharmony_ci	unsigned int prediv;
1978c2ecf20Sopenharmony_ci	unsigned int postdiv;
1988c2ecf20Sopenharmony_ci	unsigned long val;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	for (prediv = 0; prediv < ARRAY_SIZE(predivs); prediv++) {
2018c2ecf20Sopenharmony_ci		if (predivs[prediv].parent_rate != parent_rate)
2028c2ecf20Sopenharmony_ci			continue;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		for (postdiv = 0; postdiv < ARRAY_SIZE(postdivs); postdiv++) {
2058c2ecf20Sopenharmony_ci			if (rate * postdivs[postdiv].divisor != predivs[prediv].freq_vco)
2068c2ecf20Sopenharmony_ci				continue;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci			val = SSPA_AUD_PLL_CTRL0_ENA_DITHER;
2098c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_PU;
2108c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_OCLK_MODULO(postdivs[postdiv].modulo);
2118c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_FRACT(predivs[prediv].fract);
2128c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_FBCCLK(predivs[prediv].fbcclk);
2138c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL0_DIV_MCLK(predivs[prediv].mclk);
2148c2ecf20Sopenharmony_ci			writel(val, priv->mmio_base + SSPA_AUD_PLL_CTRL0);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci			val = SSPA_AUD_PLL_CTRL1_CLK_SEL_AUDIO_PLL;
2178c2ecf20Sopenharmony_ci			val |= SSPA_AUD_PLL_CTRL1_DIV_OCLK_PATTERN(postdivs[postdiv].pattern);
2188c2ecf20Sopenharmony_ci			writel(val, priv->mmio_base + SSPA_AUD_PLL_CTRL1);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci			return 0;
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return -ERANGE;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic const struct clk_ops audio_pll_ops = {
2288c2ecf20Sopenharmony_ci	.recalc_rate = audio_pll_recalc_rate,
2298c2ecf20Sopenharmony_ci	.round_rate = audio_pll_round_rate,
2308c2ecf20Sopenharmony_ci	.set_rate = audio_pll_set_rate,
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic int register_clocks(struct mmp2_audio_clk *priv, struct device *dev)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	const struct clk_parent_data sspa_mux_parents[] = {
2368c2ecf20Sopenharmony_ci		{ .hw = &priv->audio_pll_hw },
2378c2ecf20Sopenharmony_ci		{ .fw_name = "i2s0" },
2388c2ecf20Sopenharmony_ci	};
2398c2ecf20Sopenharmony_ci	const struct clk_parent_data sspa1_mux_parents[] = {
2408c2ecf20Sopenharmony_ci		{ .hw = &priv->audio_pll_hw },
2418c2ecf20Sopenharmony_ci		{ .fw_name = "i2s1" },
2428c2ecf20Sopenharmony_ci	};
2438c2ecf20Sopenharmony_ci	int ret;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	priv->audio_pll_hw.init = CLK_HW_INIT_FW_NAME("audio_pll",
2468c2ecf20Sopenharmony_ci				"vctcxo", &audio_pll_ops,
2478c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
2488c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->audio_pll_hw);
2498c2ecf20Sopenharmony_ci	if (ret)
2508c2ecf20Sopenharmony_ci		return ret;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	priv->sspa_mux.hw.init = CLK_HW_INIT_PARENTS_DATA("sspa_mux",
2538c2ecf20Sopenharmony_ci				sspa_mux_parents, &clk_mux_ops,
2548c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
2558c2ecf20Sopenharmony_ci	priv->sspa_mux.reg = priv->mmio_base + SSPA_AUD_CTRL;
2568c2ecf20Sopenharmony_ci	priv->sspa_mux.mask = 1;
2578c2ecf20Sopenharmony_ci	priv->sspa_mux.shift = SSPA_AUD_CTRL_SSPA0_MUX_SHIFT;
2588c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa_mux.hw);
2598c2ecf20Sopenharmony_ci	if (ret)
2608c2ecf20Sopenharmony_ci		return ret;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	priv->sysclk_div.hw.init = CLK_HW_INIT_HW("sys_div",
2638c2ecf20Sopenharmony_ci				&priv->sspa_mux.hw, &clk_divider_ops,
2648c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
2658c2ecf20Sopenharmony_ci	priv->sysclk_div.reg = priv->mmio_base + SSPA_AUD_CTRL;
2668c2ecf20Sopenharmony_ci	priv->sysclk_div.shift = SSPA_AUD_CTRL_SYSCLK_DIV_SHIFT;
2678c2ecf20Sopenharmony_ci	priv->sysclk_div.width = 6;
2688c2ecf20Sopenharmony_ci	priv->sysclk_div.flags = CLK_DIVIDER_ONE_BASED;
2698c2ecf20Sopenharmony_ci	priv->sysclk_div.flags |= CLK_DIVIDER_ROUND_CLOSEST;
2708c2ecf20Sopenharmony_ci	priv->sysclk_div.flags |= CLK_DIVIDER_ALLOW_ZERO;
2718c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sysclk_div.hw);
2728c2ecf20Sopenharmony_ci	if (ret)
2738c2ecf20Sopenharmony_ci		return ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	priv->sysclk_gate.hw.init = CLK_HW_INIT_HW("sys_clk",
2768c2ecf20Sopenharmony_ci				&priv->sysclk_div.hw, &clk_gate_ops,
2778c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
2788c2ecf20Sopenharmony_ci	priv->sysclk_gate.reg = priv->mmio_base + SSPA_AUD_CTRL;
2798c2ecf20Sopenharmony_ci	priv->sysclk_gate.bit_idx = SSPA_AUD_CTRL_SYSCLK_SHIFT;
2808c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sysclk_gate.hw);
2818c2ecf20Sopenharmony_ci	if (ret)
2828c2ecf20Sopenharmony_ci		return ret;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	priv->sspa0_div.hw.init = CLK_HW_INIT_HW("sspa0_div",
2858c2ecf20Sopenharmony_ci				&priv->sspa_mux.hw, &clk_divider_ops, 0);
2868c2ecf20Sopenharmony_ci	priv->sspa0_div.reg = priv->mmio_base + SSPA_AUD_CTRL;
2878c2ecf20Sopenharmony_ci	priv->sspa0_div.shift = SSPA_AUD_CTRL_SSPA0_DIV_SHIFT;
2888c2ecf20Sopenharmony_ci	priv->sspa0_div.width = 6;
2898c2ecf20Sopenharmony_ci	priv->sspa0_div.flags = CLK_DIVIDER_ONE_BASED;
2908c2ecf20Sopenharmony_ci	priv->sspa0_div.flags |= CLK_DIVIDER_ROUND_CLOSEST;
2918c2ecf20Sopenharmony_ci	priv->sspa0_div.flags |= CLK_DIVIDER_ALLOW_ZERO;
2928c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa0_div.hw);
2938c2ecf20Sopenharmony_ci	if (ret)
2948c2ecf20Sopenharmony_ci		return ret;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	priv->sspa0_gate.hw.init = CLK_HW_INIT_HW("sspa0_clk",
2978c2ecf20Sopenharmony_ci				&priv->sspa0_div.hw, &clk_gate_ops,
2988c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
2998c2ecf20Sopenharmony_ci	priv->sspa0_gate.reg = priv->mmio_base + SSPA_AUD_CTRL;
3008c2ecf20Sopenharmony_ci	priv->sspa0_gate.bit_idx = SSPA_AUD_CTRL_SSPA0_SHIFT;
3018c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa0_gate.hw);
3028c2ecf20Sopenharmony_ci	if (ret)
3038c2ecf20Sopenharmony_ci		return ret;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	priv->sspa1_mux.hw.init = CLK_HW_INIT_PARENTS_DATA("sspa1_mux",
3068c2ecf20Sopenharmony_ci				sspa1_mux_parents, &clk_mux_ops,
3078c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
3088c2ecf20Sopenharmony_ci	priv->sspa1_mux.reg = priv->mmio_base + SSPA_AUD_CTRL;
3098c2ecf20Sopenharmony_ci	priv->sspa1_mux.mask = 1;
3108c2ecf20Sopenharmony_ci	priv->sspa1_mux.shift = SSPA_AUD_CTRL_SSPA1_MUX_SHIFT;
3118c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa1_mux.hw);
3128c2ecf20Sopenharmony_ci	if (ret)
3138c2ecf20Sopenharmony_ci		return ret;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	priv->sspa1_div.hw.init = CLK_HW_INIT_HW("sspa1_div",
3168c2ecf20Sopenharmony_ci				&priv->sspa1_mux.hw, &clk_divider_ops, 0);
3178c2ecf20Sopenharmony_ci	priv->sspa1_div.reg = priv->mmio_base + SSPA_AUD_CTRL;
3188c2ecf20Sopenharmony_ci	priv->sspa1_div.shift = SSPA_AUD_CTRL_SSPA1_DIV_SHIFT;
3198c2ecf20Sopenharmony_ci	priv->sspa1_div.width = 6;
3208c2ecf20Sopenharmony_ci	priv->sspa1_div.flags = CLK_DIVIDER_ONE_BASED;
3218c2ecf20Sopenharmony_ci	priv->sspa1_div.flags |= CLK_DIVIDER_ROUND_CLOSEST;
3228c2ecf20Sopenharmony_ci	priv->sspa1_div.flags |= CLK_DIVIDER_ALLOW_ZERO;
3238c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa1_div.hw);
3248c2ecf20Sopenharmony_ci	if (ret)
3258c2ecf20Sopenharmony_ci		return ret;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	priv->sspa1_gate.hw.init = CLK_HW_INIT_HW("sspa1_clk",
3288c2ecf20Sopenharmony_ci				&priv->sspa1_div.hw, &clk_gate_ops,
3298c2ecf20Sopenharmony_ci				CLK_SET_RATE_PARENT);
3308c2ecf20Sopenharmony_ci	priv->sspa1_gate.reg = priv->mmio_base + SSPA_AUD_CTRL;
3318c2ecf20Sopenharmony_ci	priv->sspa1_gate.bit_idx = SSPA_AUD_CTRL_SSPA1_SHIFT;
3328c2ecf20Sopenharmony_ci	ret = devm_clk_hw_register(dev, &priv->sspa1_gate.hw);
3338c2ecf20Sopenharmony_ci	if (ret)
3348c2ecf20Sopenharmony_ci		return ret;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	priv->clk_data.hws[MMP2_CLK_AUDIO_SYSCLK] = &priv->sysclk_gate.hw;
3378c2ecf20Sopenharmony_ci	priv->clk_data.hws[MMP2_CLK_AUDIO_SSPA0] = &priv->sspa0_gate.hw;
3388c2ecf20Sopenharmony_ci	priv->clk_data.hws[MMP2_CLK_AUDIO_SSPA1] = &priv->sspa1_gate.hw;
3398c2ecf20Sopenharmony_ci	priv->clk_data.num = MMP2_CLK_AUDIO_NR_CLKS;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
3428c2ecf20Sopenharmony_ci				      &priv->clk_data);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int mmp2_audio_clk_probe(struct platform_device *pdev)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	struct mmp2_audio_clk *priv;
3488c2ecf20Sopenharmony_ci	int ret;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&pdev->dev,
3518c2ecf20Sopenharmony_ci			    struct_size(priv, clk_data.hws,
3528c2ecf20Sopenharmony_ci					MMP2_CLK_AUDIO_NR_CLKS),
3538c2ecf20Sopenharmony_ci			    GFP_KERNEL);
3548c2ecf20Sopenharmony_ci	if (!priv)
3558c2ecf20Sopenharmony_ci		return -ENOMEM;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	spin_lock_init(&priv->lock);
3588c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	priv->mmio_base = devm_platform_ioremap_resource(pdev, 0);
3618c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mmio_base))
3628c2ecf20Sopenharmony_ci		return PTR_ERR(priv->mmio_base);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
3658c2ecf20Sopenharmony_ci	ret = pm_clk_create(&pdev->dev);
3668c2ecf20Sopenharmony_ci	if (ret)
3678c2ecf20Sopenharmony_ci		goto disable_pm_runtime;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	ret = pm_clk_add(&pdev->dev, "audio");
3708c2ecf20Sopenharmony_ci	if (ret)
3718c2ecf20Sopenharmony_ci		goto destroy_pm_clk;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	ret = register_clocks(priv, &pdev->dev);
3748c2ecf20Sopenharmony_ci	if (ret)
3758c2ecf20Sopenharmony_ci		goto destroy_pm_clk;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return 0;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cidestroy_pm_clk:
3808c2ecf20Sopenharmony_ci	pm_clk_destroy(&pdev->dev);
3818c2ecf20Sopenharmony_cidisable_pm_runtime:
3828c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	return ret;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic int mmp2_audio_clk_remove(struct platform_device *pdev)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	pm_clk_destroy(&pdev->dev);
3908c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return 0;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
3968c2ecf20Sopenharmony_cistatic int mmp2_audio_clk_suspend(struct device *dev)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct mmp2_audio_clk *priv = dev_get_drvdata(dev);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	priv->aud_ctrl = readl(priv->mmio_base + SSPA_AUD_CTRL);
4018c2ecf20Sopenharmony_ci	priv->aud_pll_ctrl0 = readl(priv->mmio_base + SSPA_AUD_PLL_CTRL0);
4028c2ecf20Sopenharmony_ci	priv->aud_pll_ctrl1 = readl(priv->mmio_base + SSPA_AUD_PLL_CTRL1);
4038c2ecf20Sopenharmony_ci	pm_clk_suspend(dev);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	return 0;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic int mmp2_audio_clk_resume(struct device *dev)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct mmp2_audio_clk *priv = dev_get_drvdata(dev);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	pm_clk_resume(dev);
4138c2ecf20Sopenharmony_ci	writel(priv->aud_ctrl, priv->mmio_base + SSPA_AUD_CTRL);
4148c2ecf20Sopenharmony_ci	writel(priv->aud_pll_ctrl0, priv->mmio_base + SSPA_AUD_PLL_CTRL0);
4158c2ecf20Sopenharmony_ci	writel(priv->aud_pll_ctrl1, priv->mmio_base + SSPA_AUD_PLL_CTRL1);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	return 0;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci#endif
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mmp2_audio_clk_pm_ops = {
4228c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(mmp2_audio_clk_suspend, mmp2_audio_clk_resume, NULL)
4238c2ecf20Sopenharmony_ci};
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic const struct of_device_id mmp2_audio_clk_of_match[] = {
4268c2ecf20Sopenharmony_ci	{ .compatible = "marvell,mmp2-audio-clock" },
4278c2ecf20Sopenharmony_ci	{}
4288c2ecf20Sopenharmony_ci};
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mmp2_audio_clk_of_match);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic struct platform_driver mmp2_audio_clk_driver = {
4338c2ecf20Sopenharmony_ci	.driver = {
4348c2ecf20Sopenharmony_ci		.name = "mmp2-audio-clock",
4358c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mmp2_audio_clk_of_match),
4368c2ecf20Sopenharmony_ci		.pm = &mmp2_audio_clk_pm_ops,
4378c2ecf20Sopenharmony_ci	},
4388c2ecf20Sopenharmony_ci	.probe = mmp2_audio_clk_probe,
4398c2ecf20Sopenharmony_ci	.remove = mmp2_audio_clk_remove,
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_cimodule_platform_driver(mmp2_audio_clk_driver);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ciMODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
4448c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Clock driver for MMP2 Audio subsystem");
4458c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
446