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 /* wait 2-3 ms for new frequency taking effect */
101 usleep_range(2000, 3000);
106 static void wm831x_fll_unprepare(struct clk_hw
*hw
)
108 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
110 struct wm831x
*wm831x
= clkdata
->wm831x
;
113 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
, WM831X_FLL_ENA
, 0);
115 dev_crit(wm831x
->dev
, "Failed to disable FLL: %d\n", ret
);
118 static unsigned long wm831x_fll_recalc_rate(struct clk_hw
*hw
,
119 unsigned long parent_rate
)
121 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
123 struct wm831x
*wm831x
= clkdata
->wm831x
;
126 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
128 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
133 if (ret
& WM831X_FLL_AUTO
)
134 return wm831x_fll_auto_rates
[ret
& WM831X_FLL_AUTO_FREQ_MASK
];
136 dev_err(wm831x
->dev
, "FLL only supported in AUTO mode\n");
141 static long wm831x_fll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
142 unsigned long *unused
)
147 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
148 if (abs(wm831x_fll_auto_rates
[i
] - rate
) <
149 abs(wm831x_fll_auto_rates
[best
] - rate
))
152 return wm831x_fll_auto_rates
[best
];
155 static int wm831x_fll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
156 unsigned long parent_rate
)
158 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
160 struct wm831x
*wm831x
= clkdata
->wm831x
;
163 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
164 if (wm831x_fll_auto_rates
[i
] == rate
)
166 if (i
== ARRAY_SIZE(wm831x_fll_auto_rates
))
169 if (wm831x_fll_is_prepared(hw
))
172 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_2
,
173 WM831X_FLL_AUTO_FREQ_MASK
, i
);
176 static const char *wm831x_fll_parents
[] = {
181 static u8
wm831x_fll_get_parent(struct clk_hw
*hw
)
183 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
185 struct wm831x
*wm831x
= clkdata
->wm831x
;
188 /* AUTO mode is always clocked from the crystal */
189 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
191 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
196 if (ret
& WM831X_FLL_AUTO
)
199 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_5
);
201 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_5: %d\n",
206 switch (ret
& WM831X_FLL_CLK_SRC_MASK
) {
212 dev_err(wm831x
->dev
, "Unsupported FLL clock source %d\n",
213 ret
& WM831X_FLL_CLK_SRC_MASK
);
218 static const struct clk_ops wm831x_fll_ops
= {
219 .is_prepared
= wm831x_fll_is_prepared
,
220 .prepare
= wm831x_fll_prepare
,
221 .unprepare
= wm831x_fll_unprepare
,
222 .round_rate
= wm831x_fll_round_rate
,
223 .recalc_rate
= wm831x_fll_recalc_rate
,
224 .set_rate
= wm831x_fll_set_rate
,
225 .get_parent
= wm831x_fll_get_parent
,
228 static struct clk_init_data wm831x_fll_init
= {
230 .ops
= &wm831x_fll_ops
,
231 .parent_names
= wm831x_fll_parents
,
232 .num_parents
= ARRAY_SIZE(wm831x_fll_parents
),
233 .flags
= CLK_SET_RATE_GATE
,
236 static int wm831x_clkout_is_prepared(struct clk_hw
*hw
)
238 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
240 struct wm831x
*wm831x
= clkdata
->wm831x
;
243 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
245 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
250 return (ret
& WM831X_CLKOUT_ENA
) != 0;
253 static int wm831x_clkout_prepare(struct clk_hw
*hw
)
255 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
257 struct wm831x
*wm831x
= clkdata
->wm831x
;
260 ret
= wm831x_reg_unlock(wm831x
);
262 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
266 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
267 WM831X_CLKOUT_ENA
, WM831X_CLKOUT_ENA
);
269 dev_crit(wm831x
->dev
, "Failed to enable CLKOUT: %d\n", ret
);
271 wm831x_reg_lock(wm831x
);
276 static void wm831x_clkout_unprepare(struct clk_hw
*hw
)
278 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
280 struct wm831x
*wm831x
= clkdata
->wm831x
;
283 ret
= wm831x_reg_unlock(wm831x
);
285 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
289 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
290 WM831X_CLKOUT_ENA
, 0);
292 dev_crit(wm831x
->dev
, "Failed to disable CLKOUT: %d\n", ret
);
294 wm831x_reg_lock(wm831x
);
297 static const char *wm831x_clkout_parents
[] = {
302 static u8
wm831x_clkout_get_parent(struct clk_hw
*hw
)
304 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
306 struct wm831x
*wm831x
= clkdata
->wm831x
;
309 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
311 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
316 if (ret
& WM831X_CLKOUT_SRC
)
322 static int wm831x_clkout_set_parent(struct clk_hw
*hw
, u8 parent
)
324 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
326 struct wm831x
*wm831x
= clkdata
->wm831x
;
328 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
330 parent
<< WM831X_CLKOUT_SRC_SHIFT
);
333 static const struct clk_ops wm831x_clkout_ops
= {
334 .is_prepared
= wm831x_clkout_is_prepared
,
335 .prepare
= wm831x_clkout_prepare
,
336 .unprepare
= wm831x_clkout_unprepare
,
337 .get_parent
= wm831x_clkout_get_parent
,
338 .set_parent
= wm831x_clkout_set_parent
,
341 static struct clk_init_data wm831x_clkout_init
= {
343 .ops
= &wm831x_clkout_ops
,
344 .parent_names
= wm831x_clkout_parents
,
345 .num_parents
= ARRAY_SIZE(wm831x_clkout_parents
),
346 .flags
= CLK_SET_RATE_PARENT
,
349 static int wm831x_clk_probe(struct platform_device
*pdev
)
351 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
352 struct wm831x_clk
*clkdata
;
355 clkdata
= devm_kzalloc(&pdev
->dev
, sizeof(*clkdata
), GFP_KERNEL
);
359 clkdata
->wm831x
= wm831x
;
361 /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
362 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
364 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
368 clkdata
->xtal_ena
= ret
& WM831X_XTAL_ENA
;
370 clkdata
->xtal_hw
.init
= &wm831x_xtal_init
;
371 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->xtal_hw
);
375 clkdata
->fll_hw
.init
= &wm831x_fll_init
;
376 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->fll_hw
);
380 clkdata
->clkout_hw
.init
= &wm831x_clkout_init
;
381 ret
= devm_clk_hw_register(&pdev
->dev
, &clkdata
->clkout_hw
);
385 platform_set_drvdata(pdev
, clkdata
);
390 static struct platform_driver wm831x_clk_driver
= {
391 .probe
= wm831x_clk_probe
,
393 .name
= "wm831x-clk",
397 module_platform_driver(wm831x_clk_driver
);
399 /* Module information */
400 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
401 MODULE_DESCRIPTION("WM831x clock driver");
402 MODULE_LICENSE("GPL");
403 MODULE_ALIAS("platform:wm831x-clk");