xref: /openbmc/linux/drivers/clk/axs10x/i2s_pll_clock.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
13bb16560SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2923587aaSJose Abreu /*
3923587aaSJose Abreu  * Synopsys AXS10X SDP I2S PLL clock driver
4923587aaSJose Abreu  *
5923587aaSJose Abreu  * Copyright (C) 2016 Synopsys
6923587aaSJose Abreu  */
7923587aaSJose Abreu 
8923587aaSJose Abreu #include <linux/platform_device.h>
9923587aaSJose Abreu #include <linux/module.h>
10923587aaSJose Abreu #include <linux/clk-provider.h>
11923587aaSJose Abreu #include <linux/err.h>
12923587aaSJose Abreu #include <linux/device.h>
1362e59c4eSStephen Boyd #include <linux/io.h>
14923587aaSJose Abreu #include <linux/of_address.h>
15923587aaSJose Abreu #include <linux/slab.h>
16923587aaSJose Abreu #include <linux/of.h>
17923587aaSJose Abreu 
18923587aaSJose Abreu /* PLL registers addresses */
19923587aaSJose Abreu #define PLL_IDIV_REG	0x0
20923587aaSJose Abreu #define PLL_FBDIV_REG	0x4
21923587aaSJose Abreu #define PLL_ODIV0_REG	0x8
22923587aaSJose Abreu #define PLL_ODIV1_REG	0xC
23923587aaSJose Abreu 
24923587aaSJose Abreu struct i2s_pll_cfg {
25923587aaSJose Abreu 	unsigned int rate;
26923587aaSJose Abreu 	unsigned int idiv;
27923587aaSJose Abreu 	unsigned int fbdiv;
28923587aaSJose Abreu 	unsigned int odiv0;
29923587aaSJose Abreu 	unsigned int odiv1;
30923587aaSJose Abreu };
31923587aaSJose Abreu 
32923587aaSJose Abreu static const struct i2s_pll_cfg i2s_pll_cfg_27m[] = {
33923587aaSJose Abreu 	/* 27 Mhz */
34923587aaSJose Abreu 	{ 1024000, 0x104, 0x451, 0x10E38, 0x2000 },
35923587aaSJose Abreu 	{ 1411200, 0x104, 0x596, 0x10D35, 0x2000 },
36923587aaSJose Abreu 	{ 1536000, 0x208, 0xA28, 0x10B2C, 0x2000 },
37923587aaSJose Abreu 	{ 2048000, 0x82, 0x451, 0x10E38, 0x2000 },
38923587aaSJose Abreu 	{ 2822400, 0x82, 0x596, 0x10D35, 0x2000 },
39923587aaSJose Abreu 	{ 3072000, 0x104, 0xA28, 0x10B2C, 0x2000 },
40923587aaSJose Abreu 	{ 2116800, 0x82, 0x3CF, 0x10C30, 0x2000 },
41923587aaSJose Abreu 	{ 2304000, 0x104, 0x79E, 0x10B2C, 0x2000 },
42923587aaSJose Abreu 	{ 0, 0, 0, 0, 0 },
43923587aaSJose Abreu };
44923587aaSJose Abreu 
45923587aaSJose Abreu static const struct i2s_pll_cfg i2s_pll_cfg_28m[] = {
46923587aaSJose Abreu 	/* 28.224 Mhz */
47923587aaSJose Abreu 	{ 1024000, 0x82, 0x105, 0x107DF, 0x2000 },
48923587aaSJose Abreu 	{ 1411200, 0x28A, 0x1, 0x10001, 0x2000 },
49923587aaSJose Abreu 	{ 1536000, 0xA28, 0x187, 0x10042, 0x2000 },
50923587aaSJose Abreu 	{ 2048000, 0x41, 0x105, 0x107DF, 0x2000 },
51923587aaSJose Abreu 	{ 2822400, 0x145, 0x1, 0x10001, 0x2000 },
52923587aaSJose Abreu 	{ 3072000, 0x514, 0x187, 0x10042, 0x2000 },
53923587aaSJose Abreu 	{ 2116800, 0x514, 0x42, 0x10001, 0x2000 },
54923587aaSJose Abreu 	{ 2304000, 0x619, 0x82, 0x10001, 0x2000 },
55923587aaSJose Abreu 	{ 0, 0, 0, 0, 0 },
56923587aaSJose Abreu };
57923587aaSJose Abreu 
58923587aaSJose Abreu struct i2s_pll_clk {
59923587aaSJose Abreu 	void __iomem *base;
60923587aaSJose Abreu 	struct clk_hw hw;
61923587aaSJose Abreu 	struct device *dev;
62923587aaSJose Abreu };
63923587aaSJose Abreu 
i2s_pll_write(struct i2s_pll_clk * clk,unsigned int reg,unsigned int val)64923587aaSJose Abreu static inline void i2s_pll_write(struct i2s_pll_clk *clk, unsigned int reg,
65923587aaSJose Abreu 		unsigned int val)
66923587aaSJose Abreu {
67923587aaSJose Abreu 	writel_relaxed(val, clk->base + reg);
68923587aaSJose Abreu }
69923587aaSJose Abreu 
i2s_pll_read(struct i2s_pll_clk * clk,unsigned int reg)70923587aaSJose Abreu static inline unsigned int i2s_pll_read(struct i2s_pll_clk *clk,
71923587aaSJose Abreu 		unsigned int reg)
72923587aaSJose Abreu {
73923587aaSJose Abreu 	return readl_relaxed(clk->base + reg);
74923587aaSJose Abreu }
75923587aaSJose Abreu 
to_i2s_pll_clk(struct clk_hw * hw)76923587aaSJose Abreu static inline struct i2s_pll_clk *to_i2s_pll_clk(struct clk_hw *hw)
77923587aaSJose Abreu {
78923587aaSJose Abreu 	return container_of(hw, struct i2s_pll_clk, hw);
79923587aaSJose Abreu }
80923587aaSJose Abreu 
i2s_pll_get_value(unsigned int val)81923587aaSJose Abreu static inline unsigned int i2s_pll_get_value(unsigned int val)
82923587aaSJose Abreu {
83923587aaSJose Abreu 	return (val & 0x3F) + ((val >> 6) & 0x3F);
84923587aaSJose Abreu }
85923587aaSJose Abreu 
i2s_pll_get_cfg(unsigned long prate)86923587aaSJose Abreu static const struct i2s_pll_cfg *i2s_pll_get_cfg(unsigned long prate)
87923587aaSJose Abreu {
88923587aaSJose Abreu 	switch (prate) {
89923587aaSJose Abreu 	case 27000000:
90923587aaSJose Abreu 		return i2s_pll_cfg_27m;
91923587aaSJose Abreu 	case 28224000:
92923587aaSJose Abreu 		return i2s_pll_cfg_28m;
93923587aaSJose Abreu 	default:
94923587aaSJose Abreu 		return NULL;
95923587aaSJose Abreu 	}
96923587aaSJose Abreu }
97923587aaSJose Abreu 
i2s_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)98923587aaSJose Abreu static unsigned long i2s_pll_recalc_rate(struct clk_hw *hw,
99923587aaSJose Abreu 			unsigned long parent_rate)
100923587aaSJose Abreu {
101923587aaSJose Abreu 	struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
102923587aaSJose Abreu 	unsigned int idiv, fbdiv, odiv;
103923587aaSJose Abreu 
104923587aaSJose Abreu 	idiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_IDIV_REG));
105923587aaSJose Abreu 	fbdiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_FBDIV_REG));
106923587aaSJose Abreu 	odiv = i2s_pll_get_value(i2s_pll_read(clk, PLL_ODIV0_REG));
107923587aaSJose Abreu 
108923587aaSJose Abreu 	return ((parent_rate / idiv) * fbdiv) / odiv;
109923587aaSJose Abreu }
110923587aaSJose Abreu 
i2s_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)111923587aaSJose Abreu static long i2s_pll_round_rate(struct clk_hw *hw, unsigned long rate,
112923587aaSJose Abreu 			unsigned long *prate)
113923587aaSJose Abreu {
114923587aaSJose Abreu 	struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
115923587aaSJose Abreu 	const struct i2s_pll_cfg *pll_cfg = i2s_pll_get_cfg(*prate);
116923587aaSJose Abreu 	int i;
117923587aaSJose Abreu 
118923587aaSJose Abreu 	if (!pll_cfg) {
119923587aaSJose Abreu 		dev_err(clk->dev, "invalid parent rate=%ld\n", *prate);
120923587aaSJose Abreu 		return -EINVAL;
121923587aaSJose Abreu 	}
122923587aaSJose Abreu 
123923587aaSJose Abreu 	for (i = 0; pll_cfg[i].rate != 0; i++)
124923587aaSJose Abreu 		if (pll_cfg[i].rate == rate)
125923587aaSJose Abreu 			return rate;
126923587aaSJose Abreu 
127923587aaSJose Abreu 	return -EINVAL;
128923587aaSJose Abreu }
129923587aaSJose Abreu 
i2s_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)130923587aaSJose Abreu static int i2s_pll_set_rate(struct clk_hw *hw, unsigned long rate,
131923587aaSJose Abreu 			unsigned long parent_rate)
132923587aaSJose Abreu {
133923587aaSJose Abreu 	struct i2s_pll_clk *clk = to_i2s_pll_clk(hw);
134923587aaSJose Abreu 	const struct i2s_pll_cfg *pll_cfg = i2s_pll_get_cfg(parent_rate);
135923587aaSJose Abreu 	int i;
136923587aaSJose Abreu 
137923587aaSJose Abreu 	if (!pll_cfg) {
138923587aaSJose Abreu 		dev_err(clk->dev, "invalid parent rate=%ld\n", parent_rate);
139923587aaSJose Abreu 		return -EINVAL;
140923587aaSJose Abreu 	}
141923587aaSJose Abreu 
142923587aaSJose Abreu 	for (i = 0; pll_cfg[i].rate != 0; i++) {
143923587aaSJose Abreu 		if (pll_cfg[i].rate == rate) {
144923587aaSJose Abreu 			i2s_pll_write(clk, PLL_IDIV_REG, pll_cfg[i].idiv);
145923587aaSJose Abreu 			i2s_pll_write(clk, PLL_FBDIV_REG, pll_cfg[i].fbdiv);
146923587aaSJose Abreu 			i2s_pll_write(clk, PLL_ODIV0_REG, pll_cfg[i].odiv0);
147923587aaSJose Abreu 			i2s_pll_write(clk, PLL_ODIV1_REG, pll_cfg[i].odiv1);
148923587aaSJose Abreu 			return 0;
149923587aaSJose Abreu 		}
150923587aaSJose Abreu 	}
151923587aaSJose Abreu 
152923587aaSJose Abreu 	dev_err(clk->dev, "invalid rate=%ld, parent_rate=%ld\n", rate,
153923587aaSJose Abreu 			parent_rate);
154923587aaSJose Abreu 	return -EINVAL;
155923587aaSJose Abreu }
156923587aaSJose Abreu 
157923587aaSJose Abreu static const struct clk_ops i2s_pll_ops = {
158923587aaSJose Abreu 	.recalc_rate = i2s_pll_recalc_rate,
159923587aaSJose Abreu 	.round_rate = i2s_pll_round_rate,
160923587aaSJose Abreu 	.set_rate = i2s_pll_set_rate,
161923587aaSJose Abreu };
162923587aaSJose Abreu 
i2s_pll_clk_probe(struct platform_device * pdev)163923587aaSJose Abreu static int i2s_pll_clk_probe(struct platform_device *pdev)
164923587aaSJose Abreu {
165923587aaSJose Abreu 	struct device *dev = &pdev->dev;
166923587aaSJose Abreu 	struct device_node *node = dev->of_node;
167923587aaSJose Abreu 	const char *clk_name;
168923587aaSJose Abreu 	const char *parent_name;
169923587aaSJose Abreu 	struct clk *clk;
170923587aaSJose Abreu 	struct i2s_pll_clk *pll_clk;
171923587aaSJose Abreu 	struct clk_init_data init;
172923587aaSJose Abreu 
173923587aaSJose Abreu 	pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
174923587aaSJose Abreu 	if (!pll_clk)
175923587aaSJose Abreu 		return -ENOMEM;
176923587aaSJose Abreu 
17721ec8679SYueHaibing 	pll_clk->base = devm_platform_ioremap_resource(pdev, 0);
178923587aaSJose Abreu 	if (IS_ERR(pll_clk->base))
179923587aaSJose Abreu 		return PTR_ERR(pll_clk->base);
180923587aaSJose Abreu 
1816205406cSJose Abreu 	memset(&init, 0, sizeof(init));
182923587aaSJose Abreu 	clk_name = node->name;
183923587aaSJose Abreu 	init.name = clk_name;
184923587aaSJose Abreu 	init.ops = &i2s_pll_ops;
185923587aaSJose Abreu 	parent_name = of_clk_get_parent_name(node, 0);
186923587aaSJose Abreu 	init.parent_names = &parent_name;
187923587aaSJose Abreu 	init.num_parents = 1;
188923587aaSJose Abreu 	pll_clk->hw.init = &init;
189923587aaSJose Abreu 	pll_clk->dev = dev;
190923587aaSJose Abreu 
191923587aaSJose Abreu 	clk = devm_clk_register(dev, &pll_clk->hw);
192923587aaSJose Abreu 	if (IS_ERR(clk)) {
193923587aaSJose Abreu 		dev_err(dev, "failed to register %s clock (%ld)\n",
194923587aaSJose Abreu 				clk_name, PTR_ERR(clk));
195923587aaSJose Abreu 		return PTR_ERR(clk);
196923587aaSJose Abreu 	}
197923587aaSJose Abreu 
198923587aaSJose Abreu 	return of_clk_add_provider(node, of_clk_src_simple_get, clk);
199923587aaSJose Abreu }
200923587aaSJose Abreu 
i2s_pll_clk_remove(struct platform_device * pdev)201*b3438f55SUwe Kleine-König static void i2s_pll_clk_remove(struct platform_device *pdev)
202923587aaSJose Abreu {
203923587aaSJose Abreu 	of_clk_del_provider(pdev->dev.of_node);
204923587aaSJose Abreu }
205923587aaSJose Abreu 
206923587aaSJose Abreu static const struct of_device_id i2s_pll_clk_id[] = {
207923587aaSJose Abreu 	{ .compatible = "snps,axs10x-i2s-pll-clock", },
208923587aaSJose Abreu 	{ },
209923587aaSJose Abreu };
210923587aaSJose Abreu MODULE_DEVICE_TABLE(of, i2s_pll_clk_id);
211923587aaSJose Abreu 
212923587aaSJose Abreu static struct platform_driver i2s_pll_clk_driver = {
213923587aaSJose Abreu 	.driver = {
214923587aaSJose Abreu 		.name = "axs10x-i2s-pll-clock",
215923587aaSJose Abreu 		.of_match_table = i2s_pll_clk_id,
216923587aaSJose Abreu 	},
217923587aaSJose Abreu 	.probe = i2s_pll_clk_probe,
218*b3438f55SUwe Kleine-König 	.remove_new = i2s_pll_clk_remove,
219923587aaSJose Abreu };
220923587aaSJose Abreu module_platform_driver(i2s_pll_clk_driver);
221923587aaSJose Abreu 
222923587aaSJose Abreu MODULE_AUTHOR("Jose Abreu <joabreu@synopsys.com>");
223923587aaSJose Abreu MODULE_DESCRIPTION("Synopsys AXS10X SDP I2S PLL Clock Driver");
224923587aaSJose Abreu MODULE_LICENSE("GPL v2");
225