clk-cs2000-cp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CS2000 -- CIRRUS LOGIC Fractional-N Clock Synthesizer & Clock Multiplier
  4. *
  5. * Copyright (C) 2015 Renesas Electronics Corporation
  6. * Kuninori Morimoto <[email protected]>
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/delay.h>
  10. #include <linux/clk.h>
  11. #include <linux/i2c.h>
  12. #include <linux/of_device.h>
  13. #include <linux/module.h>
  14. #include <linux/regmap.h>
  15. #define CH_MAX 4
  16. #define RATIO_REG_SIZE 4
  17. #define DEVICE_ID 0x1
  18. #define DEVICE_CTRL 0x2
  19. #define DEVICE_CFG1 0x3
  20. #define DEVICE_CFG2 0x4
  21. #define GLOBAL_CFG 0x5
  22. #define Ratio_Add(x, nth) (6 + (x * 4) + (nth))
  23. #define Ratio_Val(x, nth) ((x >> (24 - (8 * nth))) & 0xFF)
  24. #define Val_Ratio(x, nth) ((x & 0xFF) << (24 - (8 * nth)))
  25. #define FUNC_CFG1 0x16
  26. #define FUNC_CFG2 0x17
  27. /* DEVICE_ID */
  28. #define REVISION_MASK (0x7)
  29. #define REVISION_B2_B3 (0x4)
  30. #define REVISION_C1 (0x6)
  31. /* DEVICE_CTRL */
  32. #define PLL_UNLOCK (1 << 7)
  33. #define AUXOUTDIS (1 << 1)
  34. #define CLKOUTDIS (1 << 0)
  35. /* DEVICE_CFG1 */
  36. #define RSEL(x) (((x) & 0x3) << 3)
  37. #define RSEL_MASK RSEL(0x3)
  38. #define AUXOUTSRC(x) (((x) & 0x3) << 1)
  39. #define AUXOUTSRC_MASK AUXOUTSRC(0x3)
  40. #define ENDEV1 (0x1)
  41. /* DEVICE_CFG2 */
  42. #define AUTORMOD (1 << 3)
  43. #define LOCKCLK(x) (((x) & 0x3) << 1)
  44. #define LOCKCLK_MASK LOCKCLK(0x3)
  45. #define FRACNSRC_MASK (1 << 0)
  46. #define FRACNSRC_STATIC (0 << 0)
  47. #define FRACNSRC_DYNAMIC (1 << 0)
  48. /* GLOBAL_CFG */
  49. #define FREEZE (1 << 7)
  50. #define ENDEV2 (0x1)
  51. /* FUNC_CFG1 */
  52. #define CLKSKIPEN (1 << 7)
  53. #define REFCLKDIV(x) (((x) & 0x3) << 3)
  54. #define REFCLKDIV_MASK REFCLKDIV(0x3)
  55. /* FUNC_CFG2 */
  56. #define LFRATIO_MASK (1 << 3)
  57. #define LFRATIO_20_12 (0 << 3)
  58. #define LFRATIO_12_20 (1 << 3)
  59. #define CH_SIZE_ERR(ch) ((ch < 0) || (ch >= CH_MAX))
  60. #define hw_to_priv(_hw) container_of(_hw, struct cs2000_priv, hw)
  61. #define priv_to_client(priv) (priv->client)
  62. #define priv_to_dev(priv) (&(priv_to_client(priv)->dev))
  63. #define CLK_IN 0
  64. #define REF_CLK 1
  65. #define CLK_MAX 2
  66. static bool cs2000_readable_reg(struct device *dev, unsigned int reg)
  67. {
  68. return reg > 0;
  69. }
  70. static bool cs2000_writeable_reg(struct device *dev, unsigned int reg)
  71. {
  72. return reg != DEVICE_ID;
  73. }
  74. static bool cs2000_volatile_reg(struct device *dev, unsigned int reg)
  75. {
  76. return reg == DEVICE_CTRL;
  77. }
  78. static const struct regmap_config cs2000_regmap_config = {
  79. .reg_bits = 8,
  80. .val_bits = 8,
  81. .max_register = FUNC_CFG2,
  82. .readable_reg = cs2000_readable_reg,
  83. .writeable_reg = cs2000_writeable_reg,
  84. .volatile_reg = cs2000_volatile_reg,
  85. };
  86. struct cs2000_priv {
  87. struct clk_hw hw;
  88. struct i2c_client *client;
  89. struct clk *clk_in;
  90. struct clk *ref_clk;
  91. struct regmap *regmap;
  92. bool dynamic_mode;
  93. bool lf_ratio;
  94. bool clk_skip;
  95. /* suspend/resume */
  96. unsigned long saved_rate;
  97. unsigned long saved_parent_rate;
  98. };
  99. static const struct of_device_id cs2000_of_match[] = {
  100. { .compatible = "cirrus,cs2000-cp", },
  101. {},
  102. };
  103. MODULE_DEVICE_TABLE(of, cs2000_of_match);
  104. static const struct i2c_device_id cs2000_id[] = {
  105. { "cs2000-cp", },
  106. {}
  107. };
  108. MODULE_DEVICE_TABLE(i2c, cs2000_id);
  109. static int cs2000_enable_dev_config(struct cs2000_priv *priv, bool enable)
  110. {
  111. int ret;
  112. ret = regmap_update_bits(priv->regmap, DEVICE_CFG1, ENDEV1,
  113. enable ? ENDEV1 : 0);
  114. if (ret < 0)
  115. return ret;
  116. ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, ENDEV2,
  117. enable ? ENDEV2 : 0);
  118. if (ret < 0)
  119. return ret;
  120. ret = regmap_update_bits(priv->regmap, FUNC_CFG1, CLKSKIPEN,
  121. (enable && priv->clk_skip) ? CLKSKIPEN : 0);
  122. if (ret < 0)
  123. return ret;
  124. return 0;
  125. }
  126. static int cs2000_ref_clk_bound_rate(struct cs2000_priv *priv,
  127. u32 rate_in)
  128. {
  129. u32 val;
  130. if (rate_in >= 32000000 && rate_in < 56000000)
  131. val = 0x0;
  132. else if (rate_in >= 16000000 && rate_in < 28000000)
  133. val = 0x1;
  134. else if (rate_in >= 8000000 && rate_in < 14000000)
  135. val = 0x2;
  136. else
  137. return -EINVAL;
  138. return regmap_update_bits(priv->regmap, FUNC_CFG1,
  139. REFCLKDIV_MASK,
  140. REFCLKDIV(val));
  141. }
  142. static int cs2000_wait_pll_lock(struct cs2000_priv *priv)
  143. {
  144. struct device *dev = priv_to_dev(priv);
  145. unsigned int i, val;
  146. int ret;
  147. for (i = 0; i < 256; i++) {
  148. ret = regmap_read(priv->regmap, DEVICE_CTRL, &val);
  149. if (ret < 0)
  150. return ret;
  151. if (!(val & PLL_UNLOCK))
  152. return 0;
  153. udelay(1);
  154. }
  155. dev_err(dev, "pll lock failed\n");
  156. return -ETIMEDOUT;
  157. }
  158. static int cs2000_clk_out_enable(struct cs2000_priv *priv, bool enable)
  159. {
  160. /* enable both AUX_OUT, CLK_OUT */
  161. return regmap_update_bits(priv->regmap, DEVICE_CTRL,
  162. (AUXOUTDIS | CLKOUTDIS),
  163. enable ? 0 :
  164. (AUXOUTDIS | CLKOUTDIS));
  165. }
  166. static u32 cs2000_rate_to_ratio(u32 rate_in, u32 rate_out, bool lf_ratio)
  167. {
  168. u64 ratio;
  169. u32 multiplier = lf_ratio ? 12 : 20;
  170. /*
  171. * ratio = rate_out / rate_in * 2^multiplier
  172. *
  173. * To avoid over flow, rate_out is u64.
  174. * The result should be u32.
  175. */
  176. ratio = (u64)rate_out << multiplier;
  177. do_div(ratio, rate_in);
  178. return ratio;
  179. }
  180. static unsigned long cs2000_ratio_to_rate(u32 ratio, u32 rate_in, bool lf_ratio)
  181. {
  182. u64 rate_out;
  183. u32 multiplier = lf_ratio ? 12 : 20;
  184. /*
  185. * ratio = rate_out / rate_in * 2^multiplier
  186. *
  187. * To avoid over flow, rate_out is u64.
  188. * The result should be u32 or unsigned long.
  189. */
  190. rate_out = (u64)ratio * rate_in;
  191. return rate_out >> multiplier;
  192. }
  193. static int cs2000_ratio_set(struct cs2000_priv *priv,
  194. int ch, u32 rate_in, u32 rate_out)
  195. {
  196. u32 val;
  197. unsigned int i;
  198. int ret;
  199. if (CH_SIZE_ERR(ch))
  200. return -EINVAL;
  201. val = cs2000_rate_to_ratio(rate_in, rate_out, priv->lf_ratio);
  202. for (i = 0; i < RATIO_REG_SIZE; i++) {
  203. ret = regmap_write(priv->regmap,
  204. Ratio_Add(ch, i),
  205. Ratio_Val(val, i));
  206. if (ret < 0)
  207. return ret;
  208. }
  209. return 0;
  210. }
  211. static u32 cs2000_ratio_get(struct cs2000_priv *priv, int ch)
  212. {
  213. unsigned int tmp, i;
  214. u32 val;
  215. int ret;
  216. val = 0;
  217. for (i = 0; i < RATIO_REG_SIZE; i++) {
  218. ret = regmap_read(priv->regmap, Ratio_Add(ch, i), &tmp);
  219. if (ret < 0)
  220. return 0;
  221. val |= Val_Ratio(tmp, i);
  222. }
  223. return val;
  224. }
  225. static int cs2000_ratio_select(struct cs2000_priv *priv, int ch)
  226. {
  227. int ret;
  228. u8 fracnsrc;
  229. if (CH_SIZE_ERR(ch))
  230. return -EINVAL;
  231. ret = regmap_update_bits(priv->regmap, DEVICE_CFG1, RSEL_MASK, RSEL(ch));
  232. if (ret < 0)
  233. return ret;
  234. fracnsrc = priv->dynamic_mode ? FRACNSRC_DYNAMIC : FRACNSRC_STATIC;
  235. ret = regmap_update_bits(priv->regmap, DEVICE_CFG2,
  236. AUTORMOD | LOCKCLK_MASK | FRACNSRC_MASK,
  237. LOCKCLK(ch) | fracnsrc);
  238. if (ret < 0)
  239. return ret;
  240. return 0;
  241. }
  242. static unsigned long cs2000_recalc_rate(struct clk_hw *hw,
  243. unsigned long parent_rate)
  244. {
  245. struct cs2000_priv *priv = hw_to_priv(hw);
  246. int ch = 0; /* it uses ch0 only at this point */
  247. u32 ratio;
  248. ratio = cs2000_ratio_get(priv, ch);
  249. return cs2000_ratio_to_rate(ratio, parent_rate, priv->lf_ratio);
  250. }
  251. static long cs2000_round_rate(struct clk_hw *hw, unsigned long rate,
  252. unsigned long *parent_rate)
  253. {
  254. struct cs2000_priv *priv = hw_to_priv(hw);
  255. u32 ratio;
  256. ratio = cs2000_rate_to_ratio(*parent_rate, rate, priv->lf_ratio);
  257. return cs2000_ratio_to_rate(ratio, *parent_rate, priv->lf_ratio);
  258. }
  259. static int cs2000_select_ratio_mode(struct cs2000_priv *priv,
  260. unsigned long rate,
  261. unsigned long parent_rate)
  262. {
  263. /*
  264. * From the datasheet:
  265. *
  266. * | It is recommended that the 12.20 High-Resolution format be
  267. * | utilized whenever the desired ratio is less than 4096 since
  268. * | the output frequency accuracy of the PLL is directly proportional
  269. * | to the accuracy of the timing reference clock and the resolution
  270. * | of the R_UD.
  271. *
  272. * This mode is only available in dynamic mode.
  273. */
  274. priv->lf_ratio = priv->dynamic_mode && ((rate / parent_rate) > 4096);
  275. return regmap_update_bits(priv->regmap, FUNC_CFG2, LFRATIO_MASK,
  276. priv->lf_ratio ? LFRATIO_20_12 : LFRATIO_12_20);
  277. }
  278. static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
  279. unsigned long rate, unsigned long parent_rate)
  280. {
  281. int ret;
  282. ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, FREEZE);
  283. if (ret < 0)
  284. return ret;
  285. ret = cs2000_select_ratio_mode(priv, rate, parent_rate);
  286. if (ret < 0)
  287. return ret;
  288. ret = cs2000_ratio_set(priv, ch, parent_rate, rate);
  289. if (ret < 0)
  290. return ret;
  291. ret = cs2000_ratio_select(priv, ch);
  292. if (ret < 0)
  293. return ret;
  294. ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, 0);
  295. if (ret < 0)
  296. return ret;
  297. priv->saved_rate = rate;
  298. priv->saved_parent_rate = parent_rate;
  299. return 0;
  300. }
  301. static int cs2000_set_rate(struct clk_hw *hw,
  302. unsigned long rate, unsigned long parent_rate)
  303. {
  304. struct cs2000_priv *priv = hw_to_priv(hw);
  305. int ch = 0; /* it uses ch0 only at this point */
  306. return __cs2000_set_rate(priv, ch, rate, parent_rate);
  307. }
  308. static int cs2000_set_saved_rate(struct cs2000_priv *priv)
  309. {
  310. int ch = 0; /* it uses ch0 only at this point */
  311. return __cs2000_set_rate(priv, ch,
  312. priv->saved_rate,
  313. priv->saved_parent_rate);
  314. }
  315. static int cs2000_enable(struct clk_hw *hw)
  316. {
  317. struct cs2000_priv *priv = hw_to_priv(hw);
  318. int ret;
  319. ret = cs2000_enable_dev_config(priv, true);
  320. if (ret < 0)
  321. return ret;
  322. ret = cs2000_clk_out_enable(priv, true);
  323. if (ret < 0)
  324. return ret;
  325. ret = cs2000_wait_pll_lock(priv);
  326. if (ret < 0)
  327. return ret;
  328. return ret;
  329. }
  330. static void cs2000_disable(struct clk_hw *hw)
  331. {
  332. struct cs2000_priv *priv = hw_to_priv(hw);
  333. cs2000_enable_dev_config(priv, false);
  334. cs2000_clk_out_enable(priv, false);
  335. }
  336. static u8 cs2000_get_parent(struct clk_hw *hw)
  337. {
  338. struct cs2000_priv *priv = hw_to_priv(hw);
  339. /*
  340. * In dynamic mode, output rates are derived from CLK_IN.
  341. * In static mode, CLK_IN is ignored, so we return REF_CLK instead.
  342. */
  343. return priv->dynamic_mode ? CLK_IN : REF_CLK;
  344. }
  345. static const struct clk_ops cs2000_ops = {
  346. .get_parent = cs2000_get_parent,
  347. .recalc_rate = cs2000_recalc_rate,
  348. .round_rate = cs2000_round_rate,
  349. .set_rate = cs2000_set_rate,
  350. .prepare = cs2000_enable,
  351. .unprepare = cs2000_disable,
  352. };
  353. static int cs2000_clk_get(struct cs2000_priv *priv)
  354. {
  355. struct device *dev = priv_to_dev(priv);
  356. struct clk *clk_in, *ref_clk;
  357. clk_in = devm_clk_get(dev, "clk_in");
  358. /* not yet provided */
  359. if (IS_ERR(clk_in))
  360. return -EPROBE_DEFER;
  361. ref_clk = devm_clk_get(dev, "ref_clk");
  362. /* not yet provided */
  363. if (IS_ERR(ref_clk))
  364. return -EPROBE_DEFER;
  365. priv->clk_in = clk_in;
  366. priv->ref_clk = ref_clk;
  367. return 0;
  368. }
  369. static int cs2000_clk_register(struct cs2000_priv *priv)
  370. {
  371. struct device *dev = priv_to_dev(priv);
  372. struct device_node *np = dev->of_node;
  373. struct clk_init_data init;
  374. const char *name = np->name;
  375. static const char *parent_names[CLK_MAX];
  376. u32 aux_out = 0;
  377. int ref_clk_rate;
  378. int ch = 0; /* it uses ch0 only at this point */
  379. int ret;
  380. of_property_read_string(np, "clock-output-names", &name);
  381. priv->dynamic_mode = of_property_read_bool(np, "cirrus,dynamic-mode");
  382. dev_info(dev, "operating in %s mode\n",
  383. priv->dynamic_mode ? "dynamic" : "static");
  384. of_property_read_u32(np, "cirrus,aux-output-source", &aux_out);
  385. ret = regmap_update_bits(priv->regmap, DEVICE_CFG1,
  386. AUXOUTSRC_MASK, AUXOUTSRC(aux_out));
  387. if (ret < 0)
  388. return ret;
  389. priv->clk_skip = of_property_read_bool(np, "cirrus,clock-skip");
  390. ref_clk_rate = clk_get_rate(priv->ref_clk);
  391. ret = cs2000_ref_clk_bound_rate(priv, ref_clk_rate);
  392. if (ret < 0)
  393. return ret;
  394. if (priv->dynamic_mode) {
  395. /* Default to low-frequency mode to allow for large ratios */
  396. priv->lf_ratio = true;
  397. } else {
  398. /*
  399. * set default rate as 1/1.
  400. * otherwise .set_rate which setup ratio
  401. * is never called if user requests 1/1 rate
  402. */
  403. ret = __cs2000_set_rate(priv, ch, ref_clk_rate, ref_clk_rate);
  404. if (ret < 0)
  405. return ret;
  406. }
  407. parent_names[CLK_IN] = __clk_get_name(priv->clk_in);
  408. parent_names[REF_CLK] = __clk_get_name(priv->ref_clk);
  409. init.name = name;
  410. init.ops = &cs2000_ops;
  411. init.flags = CLK_SET_RATE_GATE;
  412. init.parent_names = parent_names;
  413. init.num_parents = ARRAY_SIZE(parent_names);
  414. priv->hw.init = &init;
  415. ret = clk_hw_register(dev, &priv->hw);
  416. if (ret)
  417. return ret;
  418. ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
  419. if (ret < 0) {
  420. clk_hw_unregister(&priv->hw);
  421. return ret;
  422. }
  423. return 0;
  424. }
  425. static int cs2000_version_print(struct cs2000_priv *priv)
  426. {
  427. struct device *dev = priv_to_dev(priv);
  428. const char *revision;
  429. unsigned int val;
  430. int ret;
  431. ret = regmap_read(priv->regmap, DEVICE_ID, &val);
  432. if (ret < 0)
  433. return ret;
  434. /* CS2000 should be 0x0 */
  435. if (val >> 3)
  436. return -EIO;
  437. switch (val & REVISION_MASK) {
  438. case REVISION_B2_B3:
  439. revision = "B2 / B3";
  440. break;
  441. case REVISION_C1:
  442. revision = "C1";
  443. break;
  444. default:
  445. return -EIO;
  446. }
  447. dev_info(dev, "revision - %s\n", revision);
  448. return 0;
  449. }
  450. static void cs2000_remove(struct i2c_client *client)
  451. {
  452. struct cs2000_priv *priv = i2c_get_clientdata(client);
  453. struct device *dev = priv_to_dev(priv);
  454. struct device_node *np = dev->of_node;
  455. of_clk_del_provider(np);
  456. clk_hw_unregister(&priv->hw);
  457. }
  458. static int cs2000_probe(struct i2c_client *client)
  459. {
  460. struct cs2000_priv *priv;
  461. struct device *dev = &client->dev;
  462. int ret;
  463. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  464. if (!priv)
  465. return -ENOMEM;
  466. priv->client = client;
  467. i2c_set_clientdata(client, priv);
  468. priv->regmap = devm_regmap_init_i2c(client, &cs2000_regmap_config);
  469. if (IS_ERR(priv->regmap))
  470. return PTR_ERR(priv->regmap);
  471. ret = cs2000_clk_get(priv);
  472. if (ret < 0)
  473. return ret;
  474. ret = cs2000_clk_register(priv);
  475. if (ret < 0)
  476. return ret;
  477. ret = cs2000_version_print(priv);
  478. if (ret < 0)
  479. goto probe_err;
  480. return 0;
  481. probe_err:
  482. cs2000_remove(client);
  483. return ret;
  484. }
  485. static int __maybe_unused cs2000_resume(struct device *dev)
  486. {
  487. struct cs2000_priv *priv = dev_get_drvdata(dev);
  488. return cs2000_set_saved_rate(priv);
  489. }
  490. static const struct dev_pm_ops cs2000_pm_ops = {
  491. SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, cs2000_resume)
  492. };
  493. static struct i2c_driver cs2000_driver = {
  494. .driver = {
  495. .name = "cs2000-cp",
  496. .pm = &cs2000_pm_ops,
  497. .of_match_table = cs2000_of_match,
  498. },
  499. .probe_new = cs2000_probe,
  500. .remove = cs2000_remove,
  501. .id_table = cs2000_id,
  502. };
  503. module_i2c_driver(cs2000_driver);
  504. MODULE_DESCRIPTION("CS2000-CP driver");
  505. MODULE_AUTHOR("Kuninori Morimoto <[email protected]>");
  506. MODULE_LICENSE("GPL v2");