ALSA: hda - Don't override global PCM hw info flag
[linux/fpc-iii.git] / drivers / clk / clk-cs2000-cp.c
blob021f3daf34e140edef6b678fd99d2de0f5eee3b6
1 /*
2 * CS2000 -- CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier
4 * Copyright (C) 2015 Renesas Electronics Corporation
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/clk-provider.h>
12 #include <linux/delay.h>
13 #include <linux/clk.h>
14 #include <linux/i2c.h>
15 #include <linux/of_device.h>
16 #include <linux/module.h>
18 #define CH_MAX 4
19 #define RATIO_REG_SIZE 4
21 #define DEVICE_ID 0x1
22 #define DEVICE_CTRL 0x2
23 #define DEVICE_CFG1 0x3
24 #define DEVICE_CFG2 0x4
25 #define GLOBAL_CFG 0x5
26 #define Ratio_Add(x, nth) (6 + (x * 4) + (nth))
27 #define Ratio_Val(x, nth) ((x >> (24 - (8 * nth))) & 0xFF)
28 #define Val_Ratio(x, nth) ((x & 0xFF) << (24 - (8 * nth)))
29 #define FUNC_CFG1 0x16
30 #define FUNC_CFG2 0x17
32 /* DEVICE_ID */
33 #define REVISION_MASK (0x7)
34 #define REVISION_B2_B3 (0x4)
35 #define REVISION_C1 (0x6)
37 /* DEVICE_CTRL */
38 #define PLL_UNLOCK (1 << 7)
40 /* DEVICE_CFG1 */
41 #define RSEL(x) (((x) & 0x3) << 3)
42 #define RSEL_MASK RSEL(0x3)
43 #define ENDEV1 (0x1)
45 /* GLOBAL_CFG */
46 #define ENDEV2 (0x1)
48 #define CH_SIZE_ERR(ch) ((ch < 0) || (ch >= CH_MAX))
49 #define hw_to_priv(_hw) container_of(_hw, struct cs2000_priv, hw)
50 #define priv_to_client(priv) (priv->client)
51 #define priv_to_dev(priv) (&(priv_to_client(priv)->dev))
53 #define CLK_IN 0
54 #define REF_CLK 1
55 #define CLK_MAX 2
57 struct cs2000_priv {
58 struct clk_hw hw;
59 struct i2c_client *client;
60 struct clk *clk_in;
61 struct clk *ref_clk;
64 static const struct of_device_id cs2000_of_match[] = {
65 { .compatible = "cirrus,cs2000-cp", },
66 {},
68 MODULE_DEVICE_TABLE(of, cs2000_of_match);
70 static const struct i2c_device_id cs2000_id[] = {
71 { "cs2000-cp", },
74 MODULE_DEVICE_TABLE(i2c, cs2000_id);
76 #define cs2000_read(priv, addr) \
77 i2c_smbus_read_byte_data(priv_to_client(priv), addr)
78 #define cs2000_write(priv, addr, val) \
79 i2c_smbus_write_byte_data(priv_to_client(priv), addr, val)
81 static int cs2000_bset(struct cs2000_priv *priv, u8 addr, u8 mask, u8 val)
83 s32 data;
85 data = cs2000_read(priv, addr);
86 if (data < 0)
87 return data;
89 data &= ~mask;
90 data |= (val & mask);
92 return cs2000_write(priv, addr, data);
95 static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable)
97 int ret;
99 ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1,
100 enable ? ENDEV1 : 0);
101 if (ret < 0)
102 return ret;
104 ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2,
105 enable ? ENDEV2 : 0);
106 if (ret < 0)
107 return ret;
109 return 0;
112 static int cs2000_clk_in_bound_rate(struct cs2000_priv *priv,
113 u32 rate_in)
115 u32 val;
117 if (rate_in >= 32000000 && rate_in < 56000000)
118 val = 0x0;
119 else if (rate_in >= 16000000 && rate_in < 28000000)
120 val = 0x1;
121 else if (rate_in >= 8000000 && rate_in < 14000000)
122 val = 0x2;
123 else
124 return -EINVAL;
126 return cs2000_bset(priv, FUNC_CFG1, 0x3 << 3, val << 3);
129 static int cs2000_wait_pll_lock(struct cs2000_priv *priv)
131 struct device *dev = priv_to_dev(priv);
132 s32 val;
133 unsigned int i;
135 for (i = 0; i < 256; i++) {
136 val = cs2000_read(priv, DEVICE_CTRL);
137 if (val < 0)
138 return val;
139 if (!(val & PLL_UNLOCK))
140 return 0;
141 udelay(1);
144 dev_err(dev, "pll lock failed\n");
146 return -ETIMEDOUT;
149 static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable)
151 /* enable both AUX_OUT, CLK_OUT */
152 return cs2000_write(priv, DEVICE_CTRL, enable ? 0 : 0x3);
155 static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out)
157 u64 ratio;
160 * ratio = rate_out / rate_in * 2^20
162 * To avoid over flow, rate_out is u64.
163 * The result should be u32.
165 ratio = (u64)rate_out << 20;
166 do_div(ratio, rate_in);
168 return ratio;
171 static unsigned long cs2000_ratio_to_rate(u32 ratio, u32 rate_in)
173 u64 rate_out;
176 * ratio = rate_out / rate_in * 2^20
178 * To avoid over flow, rate_out is u64.
179 * The result should be u32 or unsigned long.
182 rate_out = (u64)ratio * rate_in;
183 return rate_out >> 20;
186 static int cs2000_ratio_set(struct cs2000_priv *priv,
187 int ch, u32 rate_in, u32 rate_out)
189 u32 val;
190 unsigned int i;
191 int ret;
193 if (CH_SIZE_ERR(ch))
194 return -EINVAL;
196 val = cs2000_rate_to_ratio(rate_in, rate_out);
197 for (i = 0; i < RATIO_REG_SIZE; i++) {
198 ret = cs2000_write(priv,
199 Ratio_Add(ch, i),
200 Ratio_Val(val, i));
201 if (ret < 0)
202 return ret;
205 return 0;
208 static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch)
210 s32 tmp;
211 u32 val;
212 unsigned int i;
214 val = 0;
215 for (i = 0; i < RATIO_REG_SIZE; i++) {
216 tmp = cs2000_read(priv, Ratio_Add(ch, i));
217 if (tmp < 0)
218 return 0;
220 val |= Val_Ratio(tmp, i);
223 return val;
226 static int cs2000_ratio_select(struct cs2000_priv *priv, int ch)
228 int ret;
230 if (CH_SIZE_ERR(ch))
231 return -EINVAL;
234 * FIXME
236 * this driver supports static ratio mode only at this point.
238 ret = cs2000_bset(priv, DEVICE_CFG1, RSEL_MASK, RSEL(ch));
239 if (ret < 0)
240 return ret;
242 ret = cs2000_write(priv, DEVICE_CFG2, 0x0);
243 if (ret < 0)
244 return ret;
246 return 0;
249 static unsigned long cs2000_recalc_rate(struct clk_hw *hw,
250 unsigned long parent_rate)
252 struct cs2000_priv *priv = hw_to_priv(hw);
253 int ch = 0; /* it uses ch0 only at this point */
254 u32 ratio;
256 ratio = cs2000_ratio_get(priv, ch);
258 return cs2000_ratio_to_rate(ratio, parent_rate);
261 static long cs2000_round_rate(struct clk_hw *hw, unsigned long rate,
262 unsigned long *parent_rate)
264 u32 ratio;
266 ratio = cs2000_rate_to_ratio(*parent_rate, rate);
268 return cs2000_ratio_to_rate(ratio, *parent_rate);
271 static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
272 unsigned long rate, unsigned long parent_rate)
275 int ret;
277 ret = cs2000_clk_in_bound_rate(priv, parent_rate);
278 if (ret < 0)
279 return ret;
281 ret = cs2000_ratio_set(priv, ch, parent_rate, rate);
282 if (ret < 0)
283 return ret;
285 ret = cs2000_ratio_select(priv, ch);
286 if (ret < 0)
287 return ret;
289 return 0;
292 static int cs2000_set_rate(struct clk_hw *hw,
293 unsigned long rate, unsigned long parent_rate)
295 struct cs2000_priv *priv = hw_to_priv(hw);
296 int ch = 0; /* it uses ch0 only at this point */
298 return __cs2000_set_rate(priv, ch, rate, parent_rate);
301 static int cs2000_enable(struct clk_hw *hw)
303 struct cs2000_priv *priv = hw_to_priv(hw);
304 int ret;
306 ret = cs2000_enable_dev_config(priv, true);
307 if (ret < 0)
308 return ret;
310 ret = cs2000_clk_out_enable(priv, true);
311 if (ret < 0)
312 return ret;
314 ret = cs2000_wait_pll_lock(priv);
315 if (ret < 0)
316 return ret;
318 return ret;
321 static void cs2000_disable(struct clk_hw *hw)
323 struct cs2000_priv *priv = hw_to_priv(hw);
325 cs2000_enable_dev_config(priv, false);
327 cs2000_clk_out_enable(priv, false);
330 static u8 cs2000_get_parent(struct clk_hw *hw)
332 /* always return REF_CLK */
333 return REF_CLK;
336 static const struct clk_ops cs2000_ops = {
337 .get_parent = cs2000_get_parent,
338 .recalc_rate = cs2000_recalc_rate,
339 .round_rate = cs2000_round_rate,
340 .set_rate = cs2000_set_rate,
341 .prepare = cs2000_enable,
342 .unprepare = cs2000_disable,
345 static int cs2000_clk_get(struct cs2000_priv *priv)
347 struct i2c_client *client = priv_to_client(priv);
348 struct device *dev = &client->dev;
349 struct clk *clk_in, *ref_clk;
351 clk_in = devm_clk_get(dev, "clk_in");
352 /* not yet provided */
353 if (IS_ERR(clk_in))
354 return -EPROBE_DEFER;
356 ref_clk = devm_clk_get(dev, "ref_clk");
357 /* not yet provided */
358 if (IS_ERR(ref_clk))
359 return -EPROBE_DEFER;
361 priv->clk_in = clk_in;
362 priv->ref_clk = ref_clk;
364 return 0;
367 static int cs2000_clk_register(struct cs2000_priv *priv)
369 struct device *dev = priv_to_dev(priv);
370 struct device_node *np = dev->of_node;
371 struct clk_init_data init;
372 const char *name = np->name;
373 static const char *parent_names[CLK_MAX];
374 int ch = 0; /* it uses ch0 only at this point */
375 int rate;
376 int ret;
378 of_property_read_string(np, "clock-output-names", &name);
381 * set default rate as 1/1.
382 * otherwise .set_rate which setup ratio
383 * is never called if user requests 1/1 rate
385 rate = clk_get_rate(priv->ref_clk);
386 ret = __cs2000_set_rate(priv, ch, rate, rate);
387 if (ret < 0)
388 return ret;
390 parent_names[CLK_IN] = __clk_get_name(priv->clk_in);
391 parent_names[REF_CLK] = __clk_get_name(priv->ref_clk);
393 init.name = name;
394 init.ops = &cs2000_ops;
395 init.flags = CLK_SET_RATE_GATE;
396 init.parent_names = parent_names;
397 init.num_parents = ARRAY_SIZE(parent_names);
399 priv->hw.init = &init;
401 ret = clk_hw_register(dev, &priv->hw);
402 if (ret)
403 return ret;
405 ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
406 if (ret < 0) {
407 clk_hw_unregister(&priv->hw);
408 return ret;
411 return 0;
414 static int cs2000_version_print(struct cs2000_priv *priv)
416 struct i2c_client *client = priv_to_client(priv);
417 struct device *dev = &client->dev;
418 s32 val;
419 const char *revision;
421 val = cs2000_read(priv, DEVICE_ID);
422 if (val < 0)
423 return val;
425 /* CS2000 should be 0x0 */
426 if (val >> 3)
427 return -EIO;
429 switch (val & REVISION_MASK) {
430 case REVISION_B2_B3:
431 revision = "B2 / B3";
432 break;
433 case REVISION_C1:
434 revision = "C1";
435 break;
436 default:
437 return -EIO;
440 dev_info(dev, "revision - %s\n", revision);
442 return 0;
445 static int cs2000_remove(struct i2c_client *client)
447 struct cs2000_priv *priv = i2c_get_clientdata(client);
448 struct device *dev = &client->dev;
449 struct device_node *np = dev->of_node;
451 of_clk_del_provider(np);
453 clk_hw_unregister(&priv->hw);
455 return 0;
458 static int cs2000_probe(struct i2c_client *client,
459 const struct i2c_device_id *id)
461 struct cs2000_priv *priv;
462 struct device *dev = &client->dev;
463 int ret;
465 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
466 if (!priv)
467 return -ENOMEM;
469 priv->client = client;
470 i2c_set_clientdata(client, priv);
472 ret = cs2000_clk_get(priv);
473 if (ret < 0)
474 return ret;
476 ret = cs2000_clk_register(priv);
477 if (ret < 0)
478 return ret;
480 ret = cs2000_version_print(priv);
481 if (ret < 0)
482 goto probe_err;
484 return 0;
486 probe_err:
487 cs2000_remove(client);
489 return ret;
492 static struct i2c_driver cs2000_driver = {
493 .driver = {
494 .name = "cs2000-cp",
495 .of_match_table = cs2000_of_match,
497 .probe = cs2000_probe,
498 .remove = cs2000_remove,
499 .id_table = cs2000_id,
502 module_i2c_driver(cs2000_driver);
504 MODULE_DESCRIPTION("CS2000-CP driver");
505 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
506 MODULE_LICENSE("GPL v2");