1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2011-2 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/mfd/wm831x/core.h>
18 struct wm831x
*wm831x
;
19 struct clk_hw xtal_hw
;
21 struct clk_hw clkout_hw
;
25 static int wm831x_xtal_is_prepared(struct clk_hw
*hw
)
27 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
30 return clkdata
->xtal_ena
;
33 static unsigned long wm831x_xtal_recalc_rate(struct clk_hw
*hw
,
34 unsigned long parent_rate
)
36 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
39 if (clkdata
->xtal_ena
)
45 static const struct clk_ops wm831x_xtal_ops
= {
46 .is_prepared
= wm831x_xtal_is_prepared
,
47 .recalc_rate
= wm831x_xtal_recalc_rate
,
50 static const struct clk_init_data wm831x_xtal_init
= {
52 .ops
= &wm831x_xtal_ops
,
55 static const unsigned long wm831x_fll_auto_rates
[] = {
66 static int wm831x_fll_is_prepared(struct clk_hw
*hw
)
68 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
70 struct wm831x
*wm831x
= clkdata
->wm831x
;
73 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_1
);
75 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_1: %d\n",
80 return (ret
& WM831X_FLL_ENA
) != 0;
83 static int wm831x_fll_prepare(struct clk_hw
*hw
)
85 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
87 struct wm831x
*wm831x
= clkdata
->wm831x
;
90 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
,
91 WM831X_FLL_ENA
, WM831X_FLL_ENA
);
93 dev_crit(wm831x
->dev
, "Failed to enable FLL: %d\n", ret
);
95 /* wait 2-3 ms for new frequency taking effect */
96 usleep_range(2000, 3000);
101 static void wm831x_fll_unprepare(struct clk_hw
*hw
)
103 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
105 struct wm831x
*wm831x
= clkdata
->wm831x
;
108 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
, WM831X_FLL_ENA
, 0);
110 dev_crit(wm831x
->dev
, "Failed to disable FLL: %d\n", ret
);
113 static unsigned long wm831x_fll_recalc_rate(struct clk_hw
*hw
,
114 unsigned long parent_rate
)
116 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
118 struct wm831x
*wm831x
= clkdata
->wm831x
;
121 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
123 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
128 if (ret
& WM831X_FLL_AUTO
)
129 return wm831x_fll_auto_rates
[ret
& WM831X_FLL_AUTO_FREQ_MASK
];
131 dev_err(wm831x
->dev
, "FLL only supported in AUTO mode\n");
136 static long wm831x_fll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
137 unsigned long *unused
)
142 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
143 if (abs(wm831x_fll_auto_rates
[i
] - rate
) <
144 abs(wm831x_fll_auto_rates
[best
] - rate
))
147 return wm831x_fll_auto_rates
[best
];
150 static int wm831x_fll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
151 unsigned long parent_rate
)
153 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
155 struct wm831x
*wm831x
= clkdata
->wm831x
;
158 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
159 if (wm831x_fll_auto_rates
[i
] == rate
)
161 if (i
== ARRAY_SIZE(wm831x_fll_auto_rates
))
164 if (wm831x_fll_is_prepared(hw
))
167 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_2
,
168 WM831X_FLL_AUTO_FREQ_MASK
, i
);
171 static const char *wm831x_fll_parents
[] = {
176 static u8
wm831x_fll_get_parent(struct clk_hw
*hw
)
178 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
180 struct wm831x
*wm831x
= clkdata
->wm831x
;
183 /* AUTO mode is always clocked from the crystal */
184 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
186 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
191 if (ret
& WM831X_FLL_AUTO
)
194 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_5
);
196 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_5: %d\n",
201 switch (ret
& WM831X_FLL_CLK_SRC_MASK
) {
207 dev_err(wm831x
->dev
, "Unsupported FLL clock source %d\n",
208 ret
& WM831X_FLL_CLK_SRC_MASK
);
213 static const struct clk_ops wm831x_fll_ops
= {
214 .is_prepared
= wm831x_fll_is_prepared
,
215 .prepare
= wm831x_fll_prepare
,
216 .unprepare
= wm831x_fll_unprepare
,
217 .round_rate
= wm831x_fll_round_rate
,
218 .recalc_rate
= wm831x_fll_recalc_rate
,
219 .set_rate
= wm831x_fll_set_rate
,
220 .get_parent
= wm831x_fll_get_parent
,
223 static const struct clk_init_data wm831x_fll_init
= {
225 .ops
= &wm831x_fll_ops
,
226 .parent_names
= wm831x_fll_parents
,
227 .num_parents
= ARRAY_SIZE(wm831x_fll_parents
),
228 .flags
= CLK_SET_RATE_GATE
,
231 static int wm831x_clkout_is_prepared(struct clk_hw
*hw
)
233 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
235 struct wm831x
*wm831x
= clkdata
->wm831x
;
238 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
240 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
245 return (ret
& WM831X_CLKOUT_ENA
) != 0;
248 static int wm831x_clkout_prepare(struct clk_hw
*hw
)
250 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
252 struct wm831x
*wm831x
= clkdata
->wm831x
;
255 ret
= wm831x_reg_unlock(wm831x
);
257 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
261 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
262 WM831X_CLKOUT_ENA
, WM831X_CLKOUT_ENA
);
264 dev_crit(wm831x
->dev
, "Failed to enable CLKOUT: %d\n", ret
);
266 wm831x_reg_lock(wm831x
);
271 static void wm831x_clkout_unprepare(struct clk_hw
*hw
)
273 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
275 struct wm831x
*wm831x
= clkdata
->wm831x
;
278 ret
= wm831x_reg_unlock(wm831x
);
280 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
284 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
285 WM831X_CLKOUT_ENA
, 0);
287 dev_crit(wm831x
->dev
, "Failed to disable CLKOUT: %d\n", ret
);
289 wm831x_reg_lock(wm831x
);
292 static const char *wm831x_clkout_parents
[] = {
297 static u8
wm831x_clkout_get_parent(struct clk_hw
*hw
)
299 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
301 struct wm831x
*wm831x
= clkdata
->wm831x
;
304 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
306 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
311 if (ret
& WM831X_CLKOUT_SRC
)
317 static int wm831x_clkout_set_parent(struct clk_hw
*hw
, u8 parent
)
319 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
321 struct wm831x
*wm831x
= clkdata
->wm831x
;
323 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
325 parent
<< WM831X_CLKOUT_SRC_SHIFT
);
328 static const struct clk_ops wm831x_clkout_ops
= {
329 .is_prepared
= wm831x_clkout_is_prepared
,
330 .prepare
= wm831x_clkout_prepare
,
331 .unprepare
= wm831x_clkout_unprepare
,
332 .get_parent
= wm831x_clkout_get_parent
,
333 .set_parent
= wm831x_clkout_set_parent
,
336 static const struct clk_init_data wm831x_clkout_init
= {
338 .ops
= &wm831x_clkout_ops
,
339 .parent_names
= wm831x_clkout_parents
,
340 .num_parents
= ARRAY_SIZE(wm831x_clkout_parents
),
341 .flags
= CLK_SET_RATE_PARENT
,
344 static int wm831x_clk_probe(struct platform_device
*pdev
)
346 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
347 struct wm831x_clk
*clkdata
;
350 clkdata
= devm_kzalloc(&pdev
->dev
, sizeof(*clkdata
), GFP_KERNEL
);
354 clkdata
->wm831x
= wm831x
;
356 /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
357 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
359 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
363 clkdata
->xtal_ena
= ret
& WM831X_XTAL_ENA
;
365 clkdata
->xtal_hw
.init
= &wm831x_xtal_init
;
366 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->xtal_hw
);
370 clkdata
->fll_hw
.init
= &wm831x_fll_init
;
371 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->fll_hw
);
375 clkdata
->clkout_hw
.init
= &wm831x_clkout_init
;
376 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->clkout_hw
);
380 platform_set_drvdata(pdev
, clkdata
);
385 static struct platform_driver wm831x_clk_driver
= {
386 .probe
= wm831x_clk_probe
,
388 .name
= "wm831x-clk",
392 module_platform_driver(wm831x_clk_driver
);
394 /* Module information */
395 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
396 MODULE_DESCRIPTION("WM831x clock driver");
397 MODULE_LICENSE("GPL");
398 MODULE_ALIAS("platform:wm831x-clk");