blob: ae6dd38ec0537c7234718d5bfb22eea40855d738 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brownf05259a2012-05-17 10:04:57 +01002/*
3 * WM831x clock control
4 *
5 * Copyright 2011-2 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brownf05259a2012-05-17 10:04:57 +01008 */
9
Mark Brownf05259a2012-05-17 10:04:57 +010010#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>
16
17struct wm831x_clk {
18 struct wm831x *wm831x;
19 struct clk_hw xtal_hw;
20 struct clk_hw fll_hw;
21 struct clk_hw clkout_hw;
Mark Brownf05259a2012-05-17 10:04:57 +010022 bool xtal_ena;
23};
24
Mark Browna5828a62013-08-29 15:13:28 +010025static int wm831x_xtal_is_prepared(struct clk_hw *hw)
Mark Brownf05259a2012-05-17 10:04:57 +010026{
27 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
28 xtal_hw);
29
30 return clkdata->xtal_ena;
31}
32
33static unsigned long wm831x_xtal_recalc_rate(struct clk_hw *hw,
34 unsigned long parent_rate)
35{
36 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
37 xtal_hw);
38
39 if (clkdata->xtal_ena)
40 return 32768;
41 else
42 return 0;
43}
44
45static const struct clk_ops wm831x_xtal_ops = {
Mark Browna5828a62013-08-29 15:13:28 +010046 .is_prepared = wm831x_xtal_is_prepared,
Mark Brownf05259a2012-05-17 10:04:57 +010047 .recalc_rate = wm831x_xtal_recalc_rate,
48};
49
Bhumika Goyal07770662017-08-18 15:38:17 +053050static const struct clk_init_data wm831x_xtal_init = {
Mark Brownf05259a2012-05-17 10:04:57 +010051 .name = "xtal",
52 .ops = &wm831x_xtal_ops,
Mark Brownf05259a2012-05-17 10:04:57 +010053};
54
55static const unsigned long wm831x_fll_auto_rates[] = {
56 2048000,
57 11289600,
58 12000000,
59 12288000,
60 19200000,
61 22579600,
62 24000000,
63 24576000,
64};
65
Mark Browna5828a62013-08-29 15:13:28 +010066static int wm831x_fll_is_prepared(struct clk_hw *hw)
Mark Brownf05259a2012-05-17 10:04:57 +010067{
68 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
69 fll_hw);
70 struct wm831x *wm831x = clkdata->wm831x;
71 int ret;
72
73 ret = wm831x_reg_read(wm831x, WM831X_FLL_CONTROL_1);
74 if (ret < 0) {
75 dev_err(wm831x->dev, "Unable to read FLL_CONTROL_1: %d\n",
76 ret);
77 return true;
78 }
79
80 return (ret & WM831X_FLL_ENA) != 0;
81}
82
83static int wm831x_fll_prepare(struct clk_hw *hw)
84{
85 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
86 fll_hw);
87 struct wm831x *wm831x = clkdata->wm831x;
88 int ret;
89
Axel Lin6f8b3142013-05-01 23:31:01 +080090 ret = wm831x_set_bits(wm831x, WM831X_FLL_CONTROL_1,
Mark Brownf05259a2012-05-17 10:04:57 +010091 WM831X_FLL_ENA, WM831X_FLL_ENA);
92 if (ret != 0)
93 dev_crit(wm831x->dev, "Failed to enable FLL: %d\n", ret);
94
Nicholas Mc Guireed784c52016-12-12 08:40:09 +010095 /* wait 2-3 ms for new frequency taking effect */
96 usleep_range(2000, 3000);
Mark Brownf05259a2012-05-17 10:04:57 +010097
98 return ret;
99}
100
101static void wm831x_fll_unprepare(struct clk_hw *hw)
102{
103 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
104 fll_hw);
105 struct wm831x *wm831x = clkdata->wm831x;
106 int ret;
107
Axel Lin6f8b3142013-05-01 23:31:01 +0800108 ret = wm831x_set_bits(wm831x, WM831X_FLL_CONTROL_1, WM831X_FLL_ENA, 0);
Mark Brownf05259a2012-05-17 10:04:57 +0100109 if (ret != 0)
Axel Lin6f8b3142013-05-01 23:31:01 +0800110 dev_crit(wm831x->dev, "Failed to disable FLL: %d\n", ret);
Mark Brownf05259a2012-05-17 10:04:57 +0100111}
112
113static unsigned long wm831x_fll_recalc_rate(struct clk_hw *hw,
114 unsigned long parent_rate)
115{
116 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
117 fll_hw);
118 struct wm831x *wm831x = clkdata->wm831x;
119 int ret;
120
121 ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
122 if (ret < 0) {
123 dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
124 ret);
125 return 0;
126 }
127
128 if (ret & WM831X_FLL_AUTO)
129 return wm831x_fll_auto_rates[ret & WM831X_FLL_AUTO_FREQ_MASK];
130
131 dev_err(wm831x->dev, "FLL only supported in AUTO mode\n");
132
133 return 0;
134}
135
136static long wm831x_fll_round_rate(struct clk_hw *hw, unsigned long rate,
137 unsigned long *unused)
138{
139 int best = 0;
140 int i;
141
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))
145 best = i;
146
147 return wm831x_fll_auto_rates[best];
148}
149
150static int wm831x_fll_set_rate(struct clk_hw *hw, unsigned long rate,
151 unsigned long parent_rate)
152{
153 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
154 fll_hw);
155 struct wm831x *wm831x = clkdata->wm831x;
156 int i;
157
158 for (i = 0; i < ARRAY_SIZE(wm831x_fll_auto_rates); i++)
159 if (wm831x_fll_auto_rates[i] == rate)
160 break;
161 if (i == ARRAY_SIZE(wm831x_fll_auto_rates))
162 return -EINVAL;
163
Mark Browna5828a62013-08-29 15:13:28 +0100164 if (wm831x_fll_is_prepared(hw))
Mark Brownf05259a2012-05-17 10:04:57 +0100165 return -EPERM;
166
167 return wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_2,
168 WM831X_FLL_AUTO_FREQ_MASK, i);
169}
170
171static const char *wm831x_fll_parents[] = {
172 "xtal",
173 "clkin",
174};
175
176static u8 wm831x_fll_get_parent(struct clk_hw *hw)
177{
178 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
179 fll_hw);
180 struct wm831x *wm831x = clkdata->wm831x;
181 int ret;
182
183 /* AUTO mode is always clocked from the crystal */
184 ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
185 if (ret < 0) {
186 dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
187 ret);
188 return 0;
189 }
190
191 if (ret & WM831X_FLL_AUTO)
192 return 0;
193
194 ret = wm831x_reg_read(wm831x, WM831X_FLL_CONTROL_5);
195 if (ret < 0) {
196 dev_err(wm831x->dev, "Unable to read FLL_CONTROL_5: %d\n",
197 ret);
198 return 0;
199 }
200
201 switch (ret & WM831X_FLL_CLK_SRC_MASK) {
202 case 0:
203 return 0;
204 case 1:
205 return 1;
206 default:
207 dev_err(wm831x->dev, "Unsupported FLL clock source %d\n",
208 ret & WM831X_FLL_CLK_SRC_MASK);
209 return 0;
210 }
211}
212
213static const struct clk_ops wm831x_fll_ops = {
Mark Browna5828a62013-08-29 15:13:28 +0100214 .is_prepared = wm831x_fll_is_prepared,
Mark Brownf05259a2012-05-17 10:04:57 +0100215 .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,
221};
222
Bhumika Goyal07770662017-08-18 15:38:17 +0530223static const struct clk_init_data wm831x_fll_init = {
Mark Brownf05259a2012-05-17 10:04:57 +0100224 .name = "fll",
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,
229};
230
Mark Browna5828a62013-08-29 15:13:28 +0100231static int wm831x_clkout_is_prepared(struct clk_hw *hw)
Mark Brownf05259a2012-05-17 10:04:57 +0100232{
233 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
234 clkout_hw);
235 struct wm831x *wm831x = clkdata->wm831x;
236 int ret;
237
238 ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_1);
239 if (ret < 0) {
240 dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_1: %d\n",
241 ret);
Pan Bian20979202016-12-01 14:25:44 +0800242 return false;
Mark Brownf05259a2012-05-17 10:04:57 +0100243 }
244
245 return (ret & WM831X_CLKOUT_ENA) != 0;
246}
247
248static int wm831x_clkout_prepare(struct clk_hw *hw)
249{
250 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
251 clkout_hw);
252 struct wm831x *wm831x = clkdata->wm831x;
253 int ret;
254
255 ret = wm831x_reg_unlock(wm831x);
256 if (ret != 0) {
257 dev_crit(wm831x->dev, "Failed to lock registers: %d\n", ret);
258 return ret;
259 }
260
261 ret = wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
262 WM831X_CLKOUT_ENA, WM831X_CLKOUT_ENA);
263 if (ret != 0)
264 dev_crit(wm831x->dev, "Failed to enable CLKOUT: %d\n", ret);
265
266 wm831x_reg_lock(wm831x);
267
268 return ret;
269}
270
271static void wm831x_clkout_unprepare(struct clk_hw *hw)
272{
273 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
274 clkout_hw);
275 struct wm831x *wm831x = clkdata->wm831x;
276 int ret;
277
278 ret = wm831x_reg_unlock(wm831x);
279 if (ret != 0) {
280 dev_crit(wm831x->dev, "Failed to lock registers: %d\n", ret);
281 return;
282 }
283
284 ret = wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
285 WM831X_CLKOUT_ENA, 0);
286 if (ret != 0)
287 dev_crit(wm831x->dev, "Failed to disable CLKOUT: %d\n", ret);
288
289 wm831x_reg_lock(wm831x);
290}
291
292static const char *wm831x_clkout_parents[] = {
Mark Brownf05259a2012-05-17 10:04:57 +0100293 "fll",
Axel Linbcc7fd202013-05-01 23:32:05 +0800294 "xtal",
Mark Brownf05259a2012-05-17 10:04:57 +0100295};
296
297static u8 wm831x_clkout_get_parent(struct clk_hw *hw)
298{
299 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
300 clkout_hw);
301 struct wm831x *wm831x = clkdata->wm831x;
302 int ret;
303
304 ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_1);
305 if (ret < 0) {
306 dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_1: %d\n",
307 ret);
308 return 0;
309 }
310
311 if (ret & WM831X_CLKOUT_SRC)
Mark Brownf05259a2012-05-17 10:04:57 +0100312 return 1;
Axel Linbcc7fd202013-05-01 23:32:05 +0800313 else
314 return 0;
Mark Brownf05259a2012-05-17 10:04:57 +0100315}
316
317static int wm831x_clkout_set_parent(struct clk_hw *hw, u8 parent)
318{
319 struct wm831x_clk *clkdata = container_of(hw, struct wm831x_clk,
320 clkout_hw);
321 struct wm831x *wm831x = clkdata->wm831x;
322
323 return wm831x_set_bits(wm831x, WM831X_CLOCK_CONTROL_1,
324 WM831X_CLKOUT_SRC,
325 parent << WM831X_CLKOUT_SRC_SHIFT);
326}
327
328static const struct clk_ops wm831x_clkout_ops = {
Mark Browna5828a62013-08-29 15:13:28 +0100329 .is_prepared = wm831x_clkout_is_prepared,
Mark Brownf05259a2012-05-17 10:04:57 +0100330 .prepare = wm831x_clkout_prepare,
331 .unprepare = wm831x_clkout_unprepare,
332 .get_parent = wm831x_clkout_get_parent,
333 .set_parent = wm831x_clkout_set_parent,
334};
335
Bhumika Goyal07770662017-08-18 15:38:17 +0530336static const struct clk_init_data wm831x_clkout_init = {
Mark Brownf05259a2012-05-17 10:04:57 +0100337 .name = "clkout",
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,
342};
343
Bill Pemberton018ae932012-11-19 13:22:52 -0500344static int wm831x_clk_probe(struct platform_device *pdev)
Mark Brownf05259a2012-05-17 10:04:57 +0100345{
346 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
347 struct wm831x_clk *clkdata;
348 int ret;
349
350 clkdata = devm_kzalloc(&pdev->dev, sizeof(*clkdata), GFP_KERNEL);
351 if (!clkdata)
352 return -ENOMEM;
353
Mark Brown08442ce2013-08-29 12:21:01 +0100354 clkdata->wm831x = wm831x;
355
Mark Brownf05259a2012-05-17 10:04:57 +0100356 /* XTAL_ENA can only be set via OTP/InstantConfig so just read once */
357 ret = wm831x_reg_read(wm831x, WM831X_CLOCK_CONTROL_2);
358 if (ret < 0) {
359 dev_err(wm831x->dev, "Unable to read CLOCK_CONTROL_2: %d\n",
360 ret);
361 return ret;
362 }
363 clkdata->xtal_ena = ret & WM831X_XTAL_ENA;
364
365 clkdata->xtal_hw.init = &wm831x_xtal_init;
Stephen Boydcc671d12016-06-01 16:15:33 -0700366 ret = devm_clk_hw_register(&pdev->dev, &clkdata->xtal_hw);
367 if (ret)
368 return ret;
Mark Brownf05259a2012-05-17 10:04:57 +0100369
370 clkdata->fll_hw.init = &wm831x_fll_init;
Stephen Boydcc671d12016-06-01 16:15:33 -0700371 ret = devm_clk_hw_register(&pdev->dev, &clkdata->fll_hw);
372 if (ret)
373 return ret;
Mark Brownf05259a2012-05-17 10:04:57 +0100374
375 clkdata->clkout_hw.init = &wm831x_clkout_init;
Stephen Boydcc671d12016-06-01 16:15:33 -0700376 ret = devm_clk_hw_register(&pdev->dev, &clkdata->clkout_hw);
377 if (ret)
378 return ret;
Mark Brownf05259a2012-05-17 10:04:57 +0100379
Jingoo Hanc0431032013-05-24 10:11:46 +0900380 platform_set_drvdata(pdev, clkdata);
Mark Brownf05259a2012-05-17 10:04:57 +0100381
382 return 0;
Mark Brownf05259a2012-05-17 10:04:57 +0100383}
384
Mark Brownf05259a2012-05-17 10:04:57 +0100385static struct platform_driver wm831x_clk_driver = {
386 .probe = wm831x_clk_probe,
Mark Brownf05259a2012-05-17 10:04:57 +0100387 .driver = {
388 .name = "wm831x-clk",
Mark Brownf05259a2012-05-17 10:04:57 +0100389 },
390};
391
392module_platform_driver(wm831x_clk_driver);
393
394/* Module information */
395MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
396MODULE_DESCRIPTION("WM831x clock driver");
397MODULE_LICENSE("GPL");
398MODULE_ALIAS("platform:wm831x-clk");