1 /* 2 * CS2000 -- CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier 3 * 4 * Copyright (C) 2015 Renesas Electronics Corporation 5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 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. 10 */ 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> 17 18 #define CH_MAX 4 19 #define RATIO_REG_SIZE 4 20 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 31 32 /* DEVICE_ID */ 33 #define REVISION_MASK (0x7) 34 #define REVISION_B2_B3 (0x4) 35 #define REVISION_C1 (0x6) 36 37 /* DEVICE_CTRL */ 38 #define PLL_UNLOCK (1 << 7) 39 40 /* DEVICE_CFG1 */ 41 #define RSEL(x) (((x) & 0x3) << 3) 42 #define RSEL_MASK RSEL(0x3) 43 #define ENDEV1 (0x1) 44 45 /* GLOBAL_CFG */ 46 #define ENDEV2 (0x1) 47 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)) 52 53 #define CLK_IN 0 54 #define REF_CLK 1 55 #define CLK_MAX 2 56 57 struct cs2000_priv { 58 struct clk_hw hw; 59 struct i2c_client *client; 60 struct clk *clk_in; 61 struct clk *ref_clk; 62 63 /* suspend/resume */ 64 unsigned long saved_rate; 65 unsigned long saved_parent_rate; 66 }; 67 68 static const struct of_device_id cs2000_of_match[] = { 69 { .compatible = "cirrus,cs2000-cp", }, 70 {}, 71 }; 72 MODULE_DEVICE_TABLE(of, cs2000_of_match); 73 74 static const struct i2c_device_id cs2000_id[] = { 75 { "cs2000-cp", }, 76 {} 77 }; 78 MODULE_DEVICE_TABLE(i2c, cs2000_id); 79 80 #define cs2000_read(priv, addr) \ 81 i2c_smbus_read_byte_data(priv_to_client(priv), addr) 82 #define cs2000_write(priv, addr, val) \ 83 i2c_smbus_write_byte_data(priv_to_client(priv), addr, val) 84 85 static int cs2000_bset(struct cs2000_priv *priv, u8 addr, u8 mask, u8 val) 86 { 87 s32 data; 88 89 data = cs2000_read(priv, addr); 90 if (data < 0) 91 return data; 92 93 data &= ~mask; 94 data |= (val & mask); 95 96 return cs2000_write(priv, addr, data); 97 } 98 99 static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable) 100 { 101 int ret; 102 103 ret = cs2000_bset(priv, DEVICE_CFG1, ENDEV1, 104 enable ? ENDEV1 : 0); 105 if (ret < 0) 106 return ret; 107 108 ret = cs2000_bset(priv, GLOBAL_CFG, ENDEV2, 109 enable ? ENDEV2 : 0); 110 if (ret < 0) 111 return ret; 112 113 return 0; 114 } 115 116 static int cs2000_clk_in_bound_rate(struct cs2000_priv *priv, 117 u32 rate_in) 118 { 119 u32 val; 120 121 if (rate_in >= 32000000 && rate_in < 56000000) 122 val = 0x0; 123 else if (rate_in >= 16000000 && rate_in < 28000000) 124 val = 0x1; 125 else if (rate_in >= 8000000 && rate_in < 14000000) 126 val = 0x2; 127 else 128 return -EINVAL; 129 130 return cs2000_bset(priv, FUNC_CFG1, 0x3 << 3, val << 3); 131 } 132 133 static int cs2000_wait_pll_lock(struct cs2000_priv *priv) 134 { 135 struct device *dev = priv_to_dev(priv); 136 s32 val; 137 unsigned int i; 138 139 for (i = 0; i < 256; i++) { 140 val = cs2000_read(priv, DEVICE_CTRL); 141 if (val < 0) 142 return val; 143 if (!(val & PLL_UNLOCK)) 144 return 0; 145 udelay(1); 146 } 147 148 dev_err(dev, "pll lock failed\n"); 149 150 return -ETIMEDOUT; 151 } 152 153 static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable) 154 { 155 /* enable both AUX_OUT, CLK_OUT */ 156 return cs2000_write(priv, DEVICE_CTRL, enable ? 0 : 0x3); 157 } 158 159 static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out) 160 { 161 u64 ratio; 162 163 /* 164 * ratio = rate_out / rate_in * 2^20 165 * 166 * To avoid over flow, rate_out is u64. 167 * The result should be u32. 168 */ 169 ratio = (u64)rate_out << 20; 170 do_div(ratio, rate_in); 171 172 return ratio; 173 } 174 175 static unsigned long cs2000_ratio_to_rate(u32 ratio, u32 rate_in) 176 { 177 u64 rate_out; 178 179 /* 180 * ratio = rate_out / rate_in * 2^20 181 * 182 * To avoid over flow, rate_out is u64. 183 * The result should be u32 or unsigned long. 184 */ 185 186 rate_out = (u64)ratio * rate_in; 187 return rate_out >> 20; 188 } 189 190 static int cs2000_ratio_set(struct cs2000_priv *priv, 191 int ch, u32 rate_in, u32 rate_out) 192 { 193 u32 val; 194 unsigned int i; 195 int ret; 196 197 if (CH_SIZE_ERR(ch)) 198 return -EINVAL; 199 200 val = cs2000_rate_to_ratio(rate_in, rate_out); 201 for (i = 0; i < RATIO_REG_SIZE; i++) { 202 ret = cs2000_write(priv, 203 Ratio_Add(ch, i), 204 Ratio_Val(val, i)); 205 if (ret < 0) 206 return ret; 207 } 208 209 return 0; 210 } 211 212 static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch) 213 { 214 s32 tmp; 215 u32 val; 216 unsigned int i; 217 218 val = 0; 219 for (i = 0; i < RATIO_REG_SIZE; i++) { 220 tmp = cs2000_read(priv, Ratio_Add(ch, i)); 221 if (tmp < 0) 222 return 0; 223 224 val |= Val_Ratio(tmp, i); 225 } 226 227 return val; 228 } 229 230 static int cs2000_ratio_select(struct cs2000_priv *priv, int ch) 231 { 232 int ret; 233 234 if (CH_SIZE_ERR(ch)) 235 return -EINVAL; 236 237 /* 238 * FIXME 239 * 240 * this driver supports static ratio mode only at this point. 241 */ 242 ret = cs2000_bset(priv, DEVICE_CFG1, RSEL_MASK, RSEL(ch)); 243 if (ret < 0) 244 return ret; 245 246 ret = cs2000_write(priv, DEVICE_CFG2, 0x0); 247 if (ret < 0) 248 return ret; 249 250 return 0; 251 } 252 253 static unsigned long cs2000_recalc_rate(struct clk_hw *hw, 254 unsigned long parent_rate) 255 { 256 struct cs2000_priv *priv = hw_to_priv(hw); 257 int ch = 0; /* it uses ch0 only at this point */ 258 u32 ratio; 259 260 ratio = cs2000_ratio_get(priv, ch); 261 262 return cs2000_ratio_to_rate(ratio, parent_rate); 263 } 264 265 static long cs2000_round_rate(struct clk_hw *hw, unsigned long rate, 266 unsigned long *parent_rate) 267 { 268 u32 ratio; 269 270 ratio = cs2000_rate_to_ratio(*parent_rate, rate); 271 272 return cs2000_ratio_to_rate(ratio, *parent_rate); 273 } 274 275 static int __cs2000_set_rate(struct cs2000_priv *priv, int ch, 276 unsigned long rate, unsigned long parent_rate) 277 278 { 279 int ret; 280 281 ret = cs2000_clk_in_bound_rate(priv, parent_rate); 282 if (ret < 0) 283 return ret; 284 285 ret = cs2000_ratio_set(priv, ch, parent_rate, rate); 286 if (ret < 0) 287 return ret; 288 289 ret = cs2000_ratio_select(priv, ch); 290 if (ret < 0) 291 return ret; 292 293 priv->saved_rate = rate; 294 priv->saved_parent_rate = parent_rate; 295 296 return 0; 297 } 298 299 static int cs2000_set_rate(struct clk_hw *hw, 300 unsigned long rate, unsigned long parent_rate) 301 { 302 struct cs2000_priv *priv = hw_to_priv(hw); 303 int ch = 0; /* it uses ch0 only at this point */ 304 305 return __cs2000_set_rate(priv, ch, rate, parent_rate); 306 } 307 308 static int cs2000_enable(struct clk_hw *hw) 309 { 310 struct cs2000_priv *priv = hw_to_priv(hw); 311 int ret; 312 313 ret = cs2000_enable_dev_config(priv, true); 314 if (ret < 0) 315 return ret; 316 317 ret = cs2000_clk_out_enable(priv, true); 318 if (ret < 0) 319 return ret; 320 321 ret = cs2000_wait_pll_lock(priv); 322 if (ret < 0) 323 return ret; 324 325 return ret; 326 } 327 328 static void cs2000_disable(struct clk_hw *hw) 329 { 330 struct cs2000_priv *priv = hw_to_priv(hw); 331 332 cs2000_enable_dev_config(priv, false); 333 334 cs2000_clk_out_enable(priv, false); 335 } 336 337 static u8 cs2000_get_parent(struct clk_hw *hw) 338 { 339 /* always return REF_CLK */ 340 return REF_CLK; 341 } 342 343 static const struct clk_ops cs2000_ops = { 344 .get_parent = cs2000_get_parent, 345 .recalc_rate = cs2000_recalc_rate, 346 .round_rate = cs2000_round_rate, 347 .set_rate = cs2000_set_rate, 348 .prepare = cs2000_enable, 349 .unprepare = cs2000_disable, 350 }; 351 352 static int cs2000_clk_get(struct cs2000_priv *priv) 353 { 354 struct i2c_client *client = priv_to_client(priv); 355 struct device *dev = &client->dev; 356 struct clk *clk_in, *ref_clk; 357 358 clk_in = devm_clk_get(dev, "clk_in"); 359 /* not yet provided */ 360 if (IS_ERR(clk_in)) 361 return -EPROBE_DEFER; 362 363 ref_clk = devm_clk_get(dev, "ref_clk"); 364 /* not yet provided */ 365 if (IS_ERR(ref_clk)) 366 return -EPROBE_DEFER; 367 368 priv->clk_in = clk_in; 369 priv->ref_clk = ref_clk; 370 371 return 0; 372 } 373 374 static int cs2000_clk_register(struct cs2000_priv *priv) 375 { 376 struct device *dev = priv_to_dev(priv); 377 struct device_node *np = dev->of_node; 378 struct clk_init_data init; 379 const char *name = np->name; 380 static const char *parent_names[CLK_MAX]; 381 int ch = 0; /* it uses ch0 only at this point */ 382 int rate; 383 int ret; 384 385 of_property_read_string(np, "clock-output-names", &name); 386 387 /* 388 * set default rate as 1/1. 389 * otherwise .set_rate which setup ratio 390 * is never called if user requests 1/1 rate 391 */ 392 rate = clk_get_rate(priv->ref_clk); 393 ret = __cs2000_set_rate(priv, ch, rate, rate); 394 if (ret < 0) 395 return ret; 396 397 parent_names[CLK_IN] = __clk_get_name(priv->clk_in); 398 parent_names[REF_CLK] = __clk_get_name(priv->ref_clk); 399 400 init.name = name; 401 init.ops = &cs2000_ops; 402 init.flags = CLK_SET_RATE_GATE; 403 init.parent_names = parent_names; 404 init.num_parents = ARRAY_SIZE(parent_names); 405 406 priv->hw.init = &init; 407 408 ret = clk_hw_register(dev, &priv->hw); 409 if (ret) 410 return ret; 411 412 ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw); 413 if (ret < 0) { 414 clk_hw_unregister(&priv->hw); 415 return ret; 416 } 417 418 return 0; 419 } 420 421 static int cs2000_version_print(struct cs2000_priv *priv) 422 { 423 struct i2c_client *client = priv_to_client(priv); 424 struct device *dev = &client->dev; 425 s32 val; 426 const char *revision; 427 428 val = cs2000_read(priv, DEVICE_ID); 429 if (val < 0) 430 return val; 431 432 /* CS2000 should be 0x0 */ 433 if (val >> 3) 434 return -EIO; 435 436 switch (val & REVISION_MASK) { 437 case REVISION_B2_B3: 438 revision = "B2 / B3"; 439 break; 440 case REVISION_C1: 441 revision = "C1"; 442 break; 443 default: 444 return -EIO; 445 } 446 447 dev_info(dev, "revision - %s\n", revision); 448 449 return 0; 450 } 451 452 static int cs2000_remove(struct i2c_client *client) 453 { 454 struct cs2000_priv *priv = i2c_get_clientdata(client); 455 struct device *dev = &client->dev; 456 struct device_node *np = dev->of_node; 457 458 of_clk_del_provider(np); 459 460 clk_hw_unregister(&priv->hw); 461 462 return 0; 463 } 464 465 static int cs2000_probe(struct i2c_client *client, 466 const struct i2c_device_id *id) 467 { 468 struct cs2000_priv *priv; 469 struct device *dev = &client->dev; 470 int ret; 471 472 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 473 if (!priv) 474 return -ENOMEM; 475 476 priv->client = client; 477 i2c_set_clientdata(client, priv); 478 479 ret = cs2000_clk_get(priv); 480 if (ret < 0) 481 return ret; 482 483 ret = cs2000_clk_register(priv); 484 if (ret < 0) 485 return ret; 486 487 ret = cs2000_version_print(priv); 488 if (ret < 0) 489 goto probe_err; 490 491 return 0; 492 493 probe_err: 494 cs2000_remove(client); 495 496 return ret; 497 } 498 499 static int cs2000_resume(struct device *dev) 500 { 501 struct cs2000_priv *priv = dev_get_drvdata(dev); 502 int ch = 0; /* it uses ch0 only at this point */ 503 504 return __cs2000_set_rate(priv, ch, 505 priv->saved_rate, 506 priv->saved_parent_rate); 507 } 508 509 static const struct dev_pm_ops cs2000_pm_ops = { 510 .resume_early = cs2000_resume, 511 }; 512 513 static struct i2c_driver cs2000_driver = { 514 .driver = { 515 .name = "cs2000-cp", 516 .pm = &cs2000_pm_ops, 517 .of_match_table = cs2000_of_match, 518 }, 519 .probe = cs2000_probe, 520 .remove = cs2000_remove, 521 .id_table = cs2000_id, 522 }; 523 524 module_i2c_driver(cs2000_driver); 525 526 MODULE_DESCRIPTION("CS2000-CP driver"); 527 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); 528 MODULE_LICENSE("GPL v2"); 529