2 * CS2000 -- CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier
4 * Copyright (C) 2015 Renesas Electronics Corporation
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>
19 #define RATIO_REG_SIZE 4
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
33 #define REVISION_MASK (0x7)
34 #define REVISION_B2_B3 (0x4)
35 #define REVISION_C1 (0x6)
38 #define PLL_UNLOCK (1 << 7)
39 #define AUXOUTDIS (1 << 1)
40 #define CLKOUTDIS (1 << 0)
43 #define RSEL(x) (((x) & 0x3) << 3)
44 #define RSEL_MASK RSEL(0x3)
48 #define AUTORMOD (1 << 3)
49 #define LOCKCLK(x) (((x) & 0x3) << 1)
50 #define LOCKCLK_MASK LOCKCLK(0x3)
51 #define FRACNSRC_MASK (1 << 0)
52 #define FRACNSRC_STATIC (0 << 0)
53 #define FRACNSRC_DYNAMIC (1 << 1)
59 #define CLKSKIPEN (1 << 7)
60 #define REFCLKDIV(x) (((x) & 0x3) << 3)
61 #define REFCLKDIV_MASK REFCLKDIV(0x3)
64 #define LFRATIO_MASK (1 << 3)
65 #define LFRATIO_20_12 (0 << 3)
66 #define LFRATIO_12_20 (1 << 3)
68 #define CH_SIZE_ERR(ch) ((ch < 0) || (ch >= CH_MAX))
69 #define hw_to_priv(_hw) container_of(_hw, struct cs2000_priv, hw)
70 #define priv_to_client(priv) (priv->client)
71 #define priv_to_dev(priv) (&(priv_to_client(priv)->dev))
79 struct i2c_client *client;
84 unsigned long saved_rate;
85 unsigned long saved_parent_rate;
88 static const struct of_device_id cs2000_of_match[] = {
89 { .compatible = "cirrus,cs2000-cp", },
92 MODULE_DEVICE_TABLE(of, cs2000_of_match);
94 static const struct i2c_device_id cs2000_id[] = {
98 MODULE_DEVICE_TABLE(i2c, cs2000_id);
100 #define cs2000_read(priv, addr) \
101 i2c_smbus_read_byte_data(priv_to_client(priv), addr)
102 #define cs2000_write(priv, addr, val) \
103 i2c_smbus_write_byte_data(priv_to_client(priv), addr, val)
105 static int cs2000_bset(struct cs2000_priv *priv, u8 addr, u8 mask, u8 val)
109 data = cs2000_read(priv, addr);
114 data |= (val & mask);
116 return cs2000_write(priv, addr, data);
119 static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable)
123 ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1,
124 enable ? ENDEV1 : 0);
128 ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2,
129 enable ? ENDEV2 : 0);
133 ret = cs2000_bset(priv, FUNC_CFG1, CLKSKIPEN,
134 enable ? CLKSKIPEN : 0);
138 /* FIXME: for Static ratio mode */
139 ret = cs2000_bset(priv, FUNC_CFG2, LFRATIO_MASK,
147 static int cs2000_clk_in_bound_rate(struct cs2000_priv *priv,
152 if (rate_in >= 32000000 && rate_in < 56000000)
154 else if (rate_in >= 16000000 && rate_in < 28000000)
156 else if (rate_in >= 8000000 && rate_in < 14000000)
161 return cs2000_bset(priv, FUNC_CFG1,
166 static int cs2000_wait_pll_lock(struct cs2000_priv *priv)
168 struct device *dev = priv_to_dev(priv);
172 for (i = 0; i < 256; i++) {
173 val = cs2000_read(priv, DEVICE_CTRL);
176 if (!(val & PLL_UNLOCK))
181 dev_err(dev, "pll lock failed\n");
186 static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable)
188 /* enable both AUX_OUT, CLK_OUT */
189 return cs2000_bset(priv, DEVICE_CTRL,
190 (AUXOUTDIS | CLKOUTDIS),
192 (AUXOUTDIS | CLKOUTDIS));
195 static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out)
200 * ratio = rate_out / rate_in * 2^20
202 * To avoid over flow, rate_out is u64.
203 * The result should be u32.
205 ratio = (u64)rate_out << 20;
206 do_div(ratio, rate_in);
211 static unsigned long cs2000_ratio_to_rate(u32 ratio, u32 rate_in)
216 * ratio = rate_out / rate_in * 2^20
218 * To avoid over flow, rate_out is u64.
219 * The result should be u32 or unsigned long.
222 rate_out = (u64)ratio * rate_in;
223 return rate_out >> 20;
226 static int cs2000_ratio_set(struct cs2000_priv *priv,
227 int ch, u32 rate_in, u32 rate_out)
236 val = cs2000_rate_to_ratio(rate_in, rate_out);
237 for (i = 0; i < RATIO_REG_SIZE; i++) {
238 ret = cs2000_write(priv,
248 static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch)
255 for (i = 0; i < RATIO_REG_SIZE; i++) {
256 tmp = cs2000_read(priv, Ratio_Add(ch, i));
260 val |= Val_Ratio(tmp, i);
266 static int cs2000_ratio_select(struct cs2000_priv *priv, int ch)
276 * this driver supports static ratio mode only at this point.
278 ret = cs2000_bset(priv, DEVICE_CFG1, RSEL_MASK, RSEL(ch));
282 ret = cs2000_bset(priv, DEVICE_CFG2,
283 (AUTORMOD | LOCKCLK_MASK | FRACNSRC_MASK),
284 (LOCKCLK(ch) | FRACNSRC_STATIC));
291 static unsigned long cs2000_recalc_rate(struct clk_hw *hw,
292 unsigned long parent_rate)
294 struct cs2000_priv *priv = hw_to_priv(hw);
295 int ch = 0; /* it uses ch0 only at this point */
298 ratio = cs2000_ratio_get(priv, ch);
300 return cs2000_ratio_to_rate(ratio, parent_rate);
303 static long cs2000_round_rate(struct clk_hw *hw, unsigned long rate,
304 unsigned long *parent_rate)
308 ratio = cs2000_rate_to_ratio(*parent_rate, rate);
310 return cs2000_ratio_to_rate(ratio, *parent_rate);
313 static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
314 unsigned long rate, unsigned long parent_rate)
319 ret = cs2000_clk_in_bound_rate(priv, parent_rate);
323 ret = cs2000_ratio_set(priv, ch, parent_rate, rate);
327 ret = cs2000_ratio_select(priv, ch);
331 priv->saved_rate = rate;
332 priv->saved_parent_rate = parent_rate;
337 static int cs2000_set_rate(struct clk_hw *hw,
338 unsigned long rate, unsigned long parent_rate)
340 struct cs2000_priv *priv = hw_to_priv(hw);
341 int ch = 0; /* it uses ch0 only at this point */
343 return __cs2000_set_rate(priv, ch, rate, parent_rate);
346 static int cs2000_set_saved_rate(struct cs2000_priv *priv)
348 int ch = 0; /* it uses ch0 only at this point */
350 return __cs2000_set_rate(priv, ch,
352 priv->saved_parent_rate);
355 static int cs2000_enable(struct clk_hw *hw)
357 struct cs2000_priv *priv = hw_to_priv(hw);
360 ret = cs2000_enable_dev_config(priv, true);
364 ret = cs2000_clk_out_enable(priv, true);
368 ret = cs2000_wait_pll_lock(priv);
375 static void cs2000_disable(struct clk_hw *hw)
377 struct cs2000_priv *priv = hw_to_priv(hw);
379 cs2000_enable_dev_config(priv, false);
381 cs2000_clk_out_enable(priv, false);
384 static u8 cs2000_get_parent(struct clk_hw *hw)
386 /* always return REF_CLK */
390 static const struct clk_ops cs2000_ops = {
391 .get_parent = cs2000_get_parent,
392 .recalc_rate = cs2000_recalc_rate,
393 .round_rate = cs2000_round_rate,
394 .set_rate = cs2000_set_rate,
395 .prepare = cs2000_enable,
396 .unprepare = cs2000_disable,
399 static int cs2000_clk_get(struct cs2000_priv *priv)
401 struct device *dev = priv_to_dev(priv);
402 struct clk *clk_in, *ref_clk;
404 clk_in = devm_clk_get(dev, "clk_in");
405 /* not yet provided */
407 return -EPROBE_DEFER;
409 ref_clk = devm_clk_get(dev, "ref_clk");
410 /* not yet provided */
412 return -EPROBE_DEFER;
414 priv->clk_in = clk_in;
415 priv->ref_clk = ref_clk;
420 static int cs2000_clk_register(struct cs2000_priv *priv)
422 struct device *dev = priv_to_dev(priv);
423 struct device_node *np = dev->of_node;
424 struct clk_init_data init;
425 const char *name = np->name;
426 static const char *parent_names[CLK_MAX];
427 int ch = 0; /* it uses ch0 only at this point */
431 of_property_read_string(np, "clock-output-names", &name);
434 * set default rate as 1/1.
435 * otherwise .set_rate which setup ratio
436 * is never called if user requests 1/1 rate
438 rate = clk_get_rate(priv->ref_clk);
439 ret = __cs2000_set_rate(priv, ch, rate, rate);
443 parent_names[CLK_IN] = __clk_get_name(priv->clk_in);
444 parent_names[REF_CLK] = __clk_get_name(priv->ref_clk);
447 init.ops = &cs2000_ops;
448 init.flags = CLK_SET_RATE_GATE;
449 init.parent_names = parent_names;
450 init.num_parents = ARRAY_SIZE(parent_names);
452 priv->hw.init = &init;
454 ret = clk_hw_register(dev, &priv->hw);
458 ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
460 clk_hw_unregister(&priv->hw);
467 static int cs2000_version_print(struct cs2000_priv *priv)
469 struct device *dev = priv_to_dev(priv);
471 const char *revision;
473 val = cs2000_read(priv, DEVICE_ID);
477 /* CS2000 should be 0x0 */
481 switch (val & REVISION_MASK) {
483 revision = "B2 / B3";
492 dev_info(dev, "revision - %s\n", revision);
497 static int cs2000_remove(struct i2c_client *client)
499 struct cs2000_priv *priv = i2c_get_clientdata(client);
500 struct device *dev = priv_to_dev(priv);
501 struct device_node *np = dev->of_node;
503 of_clk_del_provider(np);
505 clk_hw_unregister(&priv->hw);
510 static int cs2000_probe(struct i2c_client *client,
511 const struct i2c_device_id *id)
513 struct cs2000_priv *priv;
514 struct device *dev = &client->dev;
517 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
521 priv->client = client;
522 i2c_set_clientdata(client, priv);
524 ret = cs2000_clk_get(priv);
528 ret = cs2000_clk_register(priv);
532 ret = cs2000_version_print(priv);
539 cs2000_remove(client);
544 static int cs2000_resume(struct device *dev)
546 struct cs2000_priv *priv = dev_get_drvdata(dev);
548 return cs2000_set_saved_rate(priv);
551 static const struct dev_pm_ops cs2000_pm_ops = {
552 .resume_early = cs2000_resume,
555 static struct i2c_driver cs2000_driver = {
558 .pm = &cs2000_pm_ops,
559 .of_match_table = cs2000_of_match,
561 .probe = cs2000_probe,
562 .remove = cs2000_remove,
563 .id_table = cs2000_id,
566 module_i2c_driver(cs2000_driver);
568 MODULE_DESCRIPTION("CS2000-CP driver");
570 MODULE_LICENSE("GPL v2");