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
;
33 static int wm831x_xtal_is_prepared(struct clk_hw
*hw
)
35 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
38 return clkdata
->xtal_ena
;
41 static unsigned long wm831x_xtal_recalc_rate(struct clk_hw
*hw
,
42 unsigned long parent_rate
)
44 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
47 if (clkdata
->xtal_ena
)
53 static const struct clk_ops wm831x_xtal_ops
= {
54 .is_prepared
= wm831x_xtal_is_prepared
,
55 .recalc_rate
= wm831x_xtal_recalc_rate
,
58 static struct clk_init_data wm831x_xtal_init
= {
60 .ops
= &wm831x_xtal_ops
,
64 static const unsigned long wm831x_fll_auto_rates
[] = {
75 static int wm831x_fll_is_prepared(struct clk_hw
*hw
)
77 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
79 struct wm831x
*wm831x
= clkdata
->wm831x
;
82 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_1
);
84 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_1: %d\n",
89 return (ret
& WM831X_FLL_ENA
) != 0;
92 static int wm831x_fll_prepare(struct clk_hw
*hw
)
94 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
96 struct wm831x
*wm831x
= clkdata
->wm831x
;
99 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
,
100 WM831X_FLL_ENA
, WM831X_FLL_ENA
);
102 dev_crit(wm831x
->dev
, "Failed to enable FLL: %d\n", ret
);
104 usleep_range(2000, 2000);
109 static void wm831x_fll_unprepare(struct clk_hw
*hw
)
111 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
113 struct wm831x
*wm831x
= clkdata
->wm831x
;
116 ret
= wm831x_set_bits(wm831x
, WM831X_FLL_CONTROL_1
, WM831X_FLL_ENA
, 0);
118 dev_crit(wm831x
->dev
, "Failed to disable FLL: %d\n", ret
);
121 static unsigned long wm831x_fll_recalc_rate(struct clk_hw
*hw
,
122 unsigned long parent_rate
)
124 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
126 struct wm831x
*wm831x
= clkdata
->wm831x
;
129 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
131 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
136 if (ret
& WM831X_FLL_AUTO
)
137 return wm831x_fll_auto_rates
[ret
& WM831X_FLL_AUTO_FREQ_MASK
];
139 dev_err(wm831x
->dev
, "FLL only supported in AUTO mode\n");
144 static long wm831x_fll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
145 unsigned long *unused
)
150 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
151 if (abs(wm831x_fll_auto_rates
[i
] - rate
) <
152 abs(wm831x_fll_auto_rates
[best
] - rate
))
155 return wm831x_fll_auto_rates
[best
];
158 static int wm831x_fll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
159 unsigned long parent_rate
)
161 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
163 struct wm831x
*wm831x
= clkdata
->wm831x
;
166 for (i
= 0; i
< ARRAY_SIZE(wm831x_fll_auto_rates
); i
++)
167 if (wm831x_fll_auto_rates
[i
] == rate
)
169 if (i
== ARRAY_SIZE(wm831x_fll_auto_rates
))
172 if (wm831x_fll_is_prepared(hw
))
175 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_2
,
176 WM831X_FLL_AUTO_FREQ_MASK
, i
);
179 static const char *wm831x_fll_parents
[] = {
184 static u8
wm831x_fll_get_parent(struct clk_hw
*hw
)
186 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
188 struct wm831x
*wm831x
= clkdata
->wm831x
;
191 /* AUTO mode is always clocked from the crystal */
192 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
194 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
199 if (ret
& WM831X_FLL_AUTO
)
202 ret
= wm831x_reg_read(wm831x
, WM831X_FLL_CONTROL_5
);
204 dev_err(wm831x
->dev
, "Unable to read FLL_CONTROL_5: %d\n",
209 switch (ret
& WM831X_FLL_CLK_SRC_MASK
) {
215 dev_err(wm831x
->dev
, "Unsupported FLL clock source %d\n",
216 ret
& WM831X_FLL_CLK_SRC_MASK
);
221 static const struct clk_ops wm831x_fll_ops
= {
222 .is_prepared
= wm831x_fll_is_prepared
,
223 .prepare
= wm831x_fll_prepare
,
224 .unprepare
= wm831x_fll_unprepare
,
225 .round_rate
= wm831x_fll_round_rate
,
226 .recalc_rate
= wm831x_fll_recalc_rate
,
227 .set_rate
= wm831x_fll_set_rate
,
228 .get_parent
= wm831x_fll_get_parent
,
231 static struct clk_init_data wm831x_fll_init
= {
233 .ops
= &wm831x_fll_ops
,
234 .parent_names
= wm831x_fll_parents
,
235 .num_parents
= ARRAY_SIZE(wm831x_fll_parents
),
236 .flags
= CLK_SET_RATE_GATE
,
239 static int wm831x_clkout_is_prepared(struct clk_hw
*hw
)
241 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
243 struct wm831x
*wm831x
= clkdata
->wm831x
;
246 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
248 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
253 return (ret
& WM831X_CLKOUT_ENA
) != 0;
256 static int wm831x_clkout_prepare(struct clk_hw
*hw
)
258 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
260 struct wm831x
*wm831x
= clkdata
->wm831x
;
263 ret
= wm831x_reg_unlock(wm831x
);
265 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
269 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
270 WM831X_CLKOUT_ENA
, WM831X_CLKOUT_ENA
);
272 dev_crit(wm831x
->dev
, "Failed to enable CLKOUT: %d\n", ret
);
274 wm831x_reg_lock(wm831x
);
279 static void wm831x_clkout_unprepare(struct clk_hw
*hw
)
281 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
283 struct wm831x
*wm831x
= clkdata
->wm831x
;
286 ret
= wm831x_reg_unlock(wm831x
);
288 dev_crit(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
292 ret
= wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
293 WM831X_CLKOUT_ENA
, 0);
295 dev_crit(wm831x
->dev
, "Failed to disable CLKOUT: %d\n", ret
);
297 wm831x_reg_lock(wm831x
);
300 static const char *wm831x_clkout_parents
[] = {
305 static u8
wm831x_clkout_get_parent(struct clk_hw
*hw
)
307 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
309 struct wm831x
*wm831x
= clkdata
->wm831x
;
312 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_1
);
314 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_1: %d\n",
319 if (ret
& WM831X_CLKOUT_SRC
)
325 static int wm831x_clkout_set_parent(struct clk_hw
*hw
, u8 parent
)
327 struct wm831x_clk
*clkdata
= container_of(hw
, struct wm831x_clk
,
329 struct wm831x
*wm831x
= clkdata
->wm831x
;
331 return wm831x_set_bits(wm831x
, WM831X_CLOCK_CONTROL_1
,
333 parent
<< WM831X_CLKOUT_SRC_SHIFT
);
336 static const struct clk_ops wm831x_clkout_ops
= {
337 .is_prepared
= wm831x_clkout_is_prepared
,
338 .prepare
= wm831x_clkout_prepare
,
339 .unprepare
= wm831x_clkout_unprepare
,
340 .get_parent
= wm831x_clkout_get_parent
,
341 .set_parent
= wm831x_clkout_set_parent
,
344 static struct clk_init_data wm831x_clkout_init
= {
346 .ops
= &wm831x_clkout_ops
,
347 .parent_names
= wm831x_clkout_parents
,
348 .num_parents
= ARRAY_SIZE(wm831x_clkout_parents
),
349 .flags
= CLK_SET_RATE_PARENT
,
352 static int wm831x_clk_probe(struct platform_device
*pdev
)
354 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
355 struct wm831x_clk
*clkdata
;
358 clkdata
= devm_kzalloc(&pdev
->dev
, sizeof(*clkdata
), GFP_KERNEL
);
362 clkdata
->wm831x
= wm831x
;
364 /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
365 ret
= wm831x_reg_read(wm831x
, WM831X_CLOCK_CONTROL_2
);
367 dev_err(wm831x
->dev
, "Unable to read CLOCK_CONTROL_2: %d\n",
371 clkdata
->xtal_ena
= ret
& WM831X_XTAL_ENA
;
373 clkdata
->xtal_hw
.init
= &wm831x_xtal_init
;
374 clkdata
->xtal
= devm_clk_register(&pdev
->dev
, &clkdata
->xtal_hw
);
375 if (IS_ERR(clkdata
->xtal
))
376 return PTR_ERR(clkdata
->xtal
);
378 clkdata
->fll_hw
.init
= &wm831x_fll_init
;
379 clkdata
->fll
= devm_clk_register(&pdev
->dev
, &clkdata
->fll_hw
);
380 if (IS_ERR(clkdata
->fll
))
381 return PTR_ERR(clkdata
->fll
);
383 clkdata
->clkout_hw
.init
= &wm831x_clkout_init
;
384 clkdata
->clkout
= devm_clk_register(&pdev
->dev
, &clkdata
->clkout_hw
);
385 if (IS_ERR(clkdata
->clkout
))
386 return PTR_ERR(clkdata
->clkout
);
388 platform_set_drvdata(pdev
, clkdata
);
393 static struct platform_driver wm831x_clk_driver
= {
394 .probe
= wm831x_clk_probe
,
396 .name
= "wm831x-clk",
400 module_platform_driver(wm831x_clk_driver
);
402 /* Module information */
403 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
404 MODULE_DESCRIPTION("WM831x clock driver");
405 MODULE_LICENSE("GPL");
406 MODULE_ALIAS("platform:wm831x-clk");