4 * Copyright 2011-2 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/clk-provider.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/wm831x/core.h>
23 struct wm831x
*wm831x
;
24 struct clk_hw xtal_hw
;
26 struct clk_hw clkout_hw
;
30 static int wm831x_xtal_is_prepared(struct clk_hw
*hw
)
32 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
35 return clkdata
->xtal_ena
;
38 static unsigned long wm831x_xtal_recalc_rate(struct clk_hw
*hw
,
39 unsigned long parent_rate
)
41 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
44 if (clkdata
->xtal_ena
)
50 static const struct clk_ops wm831x_xtal_ops
= {
51 .is_prepared
= wm831x_xtal_is_prepared
,
52 .recalc_rate
= wm831x_xtal_recalc_rate
,
55 static struct clk_init_data wm831x_xtal_init
= {
57 .ops
= &wm831x_xtal_ops
,
60 static const unsigned long wm831x_fll_auto_rates
[] = {
71 static int wm831x_fll_is_prepared(struct clk_hw
*hw
)
73 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
75 struct wm831x
*wm831x
= clkdata
->wm831x
;
78 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_1
);
80 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_1: %d\n",
85 return (ret
& WM831X_FLL_ENA
) != 0;
88 static int wm831x_fll_prepare(struct clk_hw
*hw
)
90 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
92 struct wm831x
*wm831x
= clkdata
->wm831x
;
95 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
,
96 WM831X_FLL_ENA
, WM831X_FLL_ENA
);
98 dev_crit(wm831x
->dev
, "Failed to enable FLL: %d\n", ret
);
100 usleep_range(2000, 2000);
105 static void wm831x_fll_unprepare(struct clk_hw
*hw
)
107 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
109 struct wm831x
*wm831x
= clkdata
->wm831x
;
112 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
, WM831X_FLL_ENA
, 0);
114 dev_crit(wm831x
->dev
, "Failed to disable FLL: %d\n", ret
);
117 static unsigned long wm831x_fll_recalc_rate(struct clk_hw
*hw
,
118 unsigned long parent_rate
)
120 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
122 struct wm831x
*wm831x
= clkdata
->wm831x
;
125 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
127 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
132 if (ret
& WM831X_FLL_AUTO
)
133 return wm831x_fll_auto_rates
[ret
& WM831X_FLL_AUTO_FREQ_MASK
];
135 dev_err(wm831x
->dev
, "FLL only supported in AUTO mode\n");
140 static long wm831x_fll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
141 unsigned long *unused
)
146 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
147 if (abs(wm831x_fll_auto_rates
[i
] - rate
) <
148 abs(wm831x_fll_auto_rates
[best
] - rate
))
151 return wm831x_fll_auto_rates
[best
];
154 static int wm831x_fll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
155 unsigned long parent_rate
)
157 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
159 struct wm831x
*wm831x
= clkdata
->wm831x
;
162 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
163 if (wm831x_fll_auto_rates
[i
] == rate
)
165 if (i
== ARRAY_SIZE(wm831x_fll_auto_rates
))
168 if (wm831x_fll_is_prepared(hw
))
171 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_2
,
172 WM831X_FLL_AUTO_FREQ_MASK
, i
);
175 static const char *wm831x_fll_parents
[] = {
180 static u8
wm831x_fll_get_parent(struct clk_hw
*hw
)
182 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
184 struct wm831x
*wm831x
= clkdata
->wm831x
;
187 /* AUTO mode is always clocked from the crystal */
188 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
190 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
195 if (ret
& WM831X_FLL_AUTO
)
198 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_5
);
200 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_5: %d\n",
205 switch (ret
& WM831X_FLL_CLK_SRC_MASK
) {
211 dev_err(wm831x
->dev
, "Unsupported FLL clock source %d\n",
212 ret
& WM831X_FLL_CLK_SRC_MASK
);
217 static const struct clk_ops wm831x_fll_ops
= {
218 .is_prepared
= wm831x_fll_is_prepared
,
219 .prepare
= wm831x_fll_prepare
,
220 .unprepare
= wm831x_fll_unprepare
,
221 .round_rate
= wm831x_fll_round_rate
,
222 .recalc_rate
= wm831x_fll_recalc_rate
,
223 .set_rate
= wm831x_fll_set_rate
,
224 .get_parent
= wm831x_fll_get_parent
,
227 static struct clk_init_data wm831x_fll_init
= {
229 .ops
= &wm831x_fll_ops
,
230 .parent_names
= wm831x_fll_parents
,
231 .num_parents
= ARRAY_SIZE(wm831x_fll_parents
),
232 .flags
= CLK_SET_RATE_GATE
,
235 static int wm831x_clkout_is_prepared(struct clk_hw
*hw
)
237 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
239 struct wm831x
*wm831x
= clkdata
->wm831x
;
242 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
244 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
249 return (ret
& WM831X_CLKOUT_ENA
) != 0;
252 static int wm831x_clkout_prepare(struct clk_hw
*hw
)
254 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
256 struct wm831x
*wm831x
= clkdata
->wm831x
;
259 ret
= wm831x_reg_unlock(wm831x
);
261 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
265 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
266 WM831X_CLKOUT_ENA
, WM831X_CLKOUT_ENA
);
268 dev_crit(wm831x
->dev
, "Failed to enable CLKOUT: %d\n", ret
);
270 wm831x_reg_lock(wm831x
);
275 static void wm831x_clkout_unprepare(struct clk_hw
*hw
)
277 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
279 struct wm831x
*wm831x
= clkdata
->wm831x
;
282 ret
= wm831x_reg_unlock(wm831x
);
284 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
288 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
289 WM831X_CLKOUT_ENA
, 0);
291 dev_crit(wm831x
->dev
, "Failed to disable CLKOUT: %d\n", ret
);
293 wm831x_reg_lock(wm831x
);
296 static const char *wm831x_clkout_parents
[] = {
301 static u8
wm831x_clkout_get_parent(struct clk_hw
*hw
)
303 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
305 struct wm831x
*wm831x
= clkdata
->wm831x
;
308 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
310 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
315 if (ret
& WM831X_CLKOUT_SRC
)
321 static int wm831x_clkout_set_parent(struct clk_hw
*hw
, u8 parent
)
323 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
325 struct wm831x
*wm831x
= clkdata
->wm831x
;
327 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
329 parent
<< WM831X_CLKOUT_SRC_SHIFT
);
332 static const struct clk_ops wm831x_clkout_ops
= {
333 .is_prepared
= wm831x_clkout_is_prepared
,
334 .prepare
= wm831x_clkout_prepare
,
335 .unprepare
= wm831x_clkout_unprepare
,
336 .get_parent
= wm831x_clkout_get_parent
,
337 .set_parent
= wm831x_clkout_set_parent
,
340 static struct clk_init_data wm831x_clkout_init
= {
342 .ops
= &wm831x_clkout_ops
,
343 .parent_names
= wm831x_clkout_parents
,
344 .num_parents
= ARRAY_SIZE(wm831x_clkout_parents
),
345 .flags
= CLK_SET_RATE_PARENT
,
348 static int wm831x_clk_probe(struct platform_device
*pdev
)
350 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
351 struct wm831x_clk
*clkdata
;
354 clkdata
= devm_kzalloc(&pdev
->dev
, sizeof(*clkdata
), GFP_KERNEL
);
358 clkdata
->wm831x
= wm831x
;
360 /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
361 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
363 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
367 clkdata
->xtal_ena
= ret
& WM831X_XTAL_ENA
;
369 clkdata
->xtal_hw
.init
= &wm831x_xtal_init
;
370 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->xtal_hw
);
374 clkdata
->fll_hw
.init
= &wm831x_fll_init
;
375 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->fll_hw
);
379 clkdata
->clkout_hw
.init
= &wm831x_clkout_init
;
380 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->clkout_hw
);
384 platform_set_drvdata(pdev
, clkdata
);
389 static struct platform_driver wm831x_clk_driver
= {
390 .probe
= wm831x_clk_probe
,
392 .name
= "wm831x-clk",
396 module_platform_driver(wm831x_clk_driver
);
398 /* Module information */
399 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
400 MODULE_DESCRIPTION("WM831x clock driver");
401 MODULE_LICENSE("GPL");
402 MODULE_ALIAS("platform:wm831x-clk");