2 * Synopsys AXS10X SDP I2S PLL clock driver
4 * Copyright (C) 2016 Synopsys
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <linux/clk-provider.h>
14 #include <linux/err.h>
15 #include <linux/device.h>
16 #include <linux/of_address.h>
17 #include <linux/slab.h>
20 /* PLL registers addresses */
21 #define PLL_IDIV_REG 0x0
22 #define PLL_FBDIV_REG 0x4
23 #define PLL_ODIV0_REG 0x8
24 #define PLL_ODIV1_REG 0xC
34 static const struct i2s_pll_cfg i2s_pll_cfg_27m
[] = {
36 { 1024000, 0x104, 0x451, 0x10E38, 0x2000 },
37 { 1411200, 0x104, 0x596, 0x10D35, 0x2000 },
38 { 1536000, 0x208, 0xA28, 0x10B2C, 0x2000 },
39 { 2048000, 0x82, 0x451, 0x10E38, 0x2000 },
40 { 2822400, 0x82, 0x596, 0x10D35, 0x2000 },
41 { 3072000, 0x104, 0xA28, 0x10B2C, 0x2000 },
42 { 2116800, 0x82, 0x3CF, 0x10C30, 0x2000 },
43 { 2304000, 0x104, 0x79E, 0x10B2C, 0x2000 },
47 static const struct i2s_pll_cfg i2s_pll_cfg_28m
[] = {
49 { 1024000, 0x82, 0x105, 0x107DF, 0x2000 },
50 { 1411200, 0x28A, 0x1, 0x10001, 0x2000 },
51 { 1536000, 0xA28, 0x187, 0x10042, 0x2000 },
52 { 2048000, 0x41, 0x105, 0x107DF, 0x2000 },
53 { 2822400, 0x145, 0x1, 0x10001, 0x2000 },
54 { 3072000, 0x514, 0x187, 0x10042, 0x2000 },
55 { 2116800, 0x514, 0x42, 0x10001, 0x2000 },
56 { 2304000, 0x619, 0x82, 0x10001, 0x2000 },
66 static inline void i2s_pll_write(struct i2s_pll_clk
*clk
, unsigned int reg
,
69 writel_relaxed(val
, clk
->base
+ reg
);
72 static inline unsigned int i2s_pll_read(struct i2s_pll_clk
*clk
,
75 return readl_relaxed(clk
->base
+ reg
);
78 static inline struct i2s_pll_clk
*to_i2s_pll_clk(struct clk_hw
*hw
)
80 return container_of(hw
, struct i2s_pll_clk
, hw
);
83 static inline unsigned int i2s_pll_get_value(unsigned int val
)
85 return (val
& 0x3F) + ((val
>> 6) & 0x3F);
88 static const struct i2s_pll_cfg
*i2s_pll_get_cfg(unsigned long prate
)
92 return i2s_pll_cfg_27m
;
94 return i2s_pll_cfg_28m
;
100 static unsigned long i2s_pll_recalc_rate(struct clk_hw
*hw
,
101 unsigned long parent_rate
)
103 struct i2s_pll_clk
*clk
= to_i2s_pll_clk(hw
);
104 unsigned int idiv
, fbdiv
, odiv
;
106 idiv
= i2s_pll_get_value(i2s_pll_read(clk
, PLL_IDIV_REG
));
107 fbdiv
= i2s_pll_get_value(i2s_pll_read(clk
, PLL_FBDIV_REG
));
108 odiv
= i2s_pll_get_value(i2s_pll_read(clk
, PLL_ODIV0_REG
));
110 return ((parent_rate
/ idiv
) * fbdiv
) / odiv
;
113 static long i2s_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
114 unsigned long *prate
)
116 struct i2s_pll_clk
*clk
= to_i2s_pll_clk(hw
);
117 const struct i2s_pll_cfg
*pll_cfg
= i2s_pll_get_cfg(*prate
);
121 dev_err(clk
->dev
, "invalid parent rate=%ld\n", *prate
);
125 for (i
= 0; pll_cfg
[i
].rate
!= 0; i
++)
126 if (pll_cfg
[i
].rate
== rate
)
132 static int i2s_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
133 unsigned long parent_rate
)
135 struct i2s_pll_clk
*clk
= to_i2s_pll_clk(hw
);
136 const struct i2s_pll_cfg
*pll_cfg
= i2s_pll_get_cfg(parent_rate
);
140 dev_err(clk
->dev
, "invalid parent rate=%ld\n", parent_rate
);
144 for (i
= 0; pll_cfg
[i
].rate
!= 0; i
++) {
145 if (pll_cfg
[i
].rate
== rate
) {
146 i2s_pll_write(clk
, PLL_IDIV_REG
, pll_cfg
[i
].idiv
);
147 i2s_pll_write(clk
, PLL_FBDIV_REG
, pll_cfg
[i
].fbdiv
);
148 i2s_pll_write(clk
, PLL_ODIV0_REG
, pll_cfg
[i
].odiv0
);
149 i2s_pll_write(clk
, PLL_ODIV1_REG
, pll_cfg
[i
].odiv1
);
154 dev_err(clk
->dev
, "invalid rate=%ld, parent_rate=%ld\n", rate
,
159 static const struct clk_ops i2s_pll_ops
= {
160 .recalc_rate
= i2s_pll_recalc_rate
,
161 .round_rate
= i2s_pll_round_rate
,
162 .set_rate
= i2s_pll_set_rate
,
165 static int i2s_pll_clk_probe(struct platform_device
*pdev
)
167 struct device
*dev
= &pdev
->dev
;
168 struct device_node
*node
= dev
->of_node
;
169 const char *clk_name
;
170 const char *parent_name
;
172 struct i2s_pll_clk
*pll_clk
;
173 struct clk_init_data init
;
174 struct resource
*mem
;
176 pll_clk
= devm_kzalloc(dev
, sizeof(*pll_clk
), GFP_KERNEL
);
180 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
181 pll_clk
->base
= devm_ioremap_resource(dev
, mem
);
182 if (IS_ERR(pll_clk
->base
))
183 return PTR_ERR(pll_clk
->base
);
185 memset(&init
, 0, sizeof(init
));
186 clk_name
= node
->name
;
187 init
.name
= clk_name
;
188 init
.ops
= &i2s_pll_ops
;
189 parent_name
= of_clk_get_parent_name(node
, 0);
190 init
.parent_names
= &parent_name
;
191 init
.num_parents
= 1;
192 pll_clk
->hw
.init
= &init
;
195 clk
= devm_clk_register(dev
, &pll_clk
->hw
);
197 dev_err(dev
, "failed to register %s clock (%ld)\n",
198 clk_name
, PTR_ERR(clk
));
202 return of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
205 static int i2s_pll_clk_remove(struct platform_device
*pdev
)
207 of_clk_del_provider(pdev
->dev
.of_node
);
211 static const struct of_device_id i2s_pll_clk_id
[] = {
212 { .compatible
= "snps,axs10x-i2s-pll-clock", },
215 MODULE_DEVICE_TABLE(of
, i2s_pll_clk_id
);
217 static struct platform_driver i2s_pll_clk_driver
= {
219 .name
= "axs10x-i2s-pll-clock",
220 .of_match_table
= i2s_pll_clk_id
,
222 .probe
= i2s_pll_clk_probe
,
223 .remove
= i2s_pll_clk_remove
,
225 module_platform_driver(i2s_pll_clk_driver
);
227 MODULE_AUTHOR("Jose Abreu <joabreu@synopsys.com>");
228 MODULE_DESCRIPTION("Synopsys AXS10X SDP I2S PLL Clock Driver");
229 MODULE_LICENSE("GPL v2");