clk-cs2000-cp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 <kuninori.morimoto.gx@renesas.com>
  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.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 int cs2000_determine_rate(struct clk_hw *hw,
  252. struct clk_rate_request *req)
  253. {
  254. struct cs2000_priv *priv = hw_to_priv(hw);
  255. u32 ratio;
  256. ratio = cs2000_rate_to_ratio(req->best_parent_rate, req->rate,
  257. priv->lf_ratio);
  258. req->rate = cs2000_ratio_to_rate(ratio, req->best_parent_rate,
  259. priv->lf_ratio);
  260. return 0;
  261. }
  262. static int cs2000_select_ratio_mode(struct cs2000_priv *priv,
  263. unsigned long rate,
  264. unsigned long parent_rate)
  265. {
  266. /*
  267. * From the datasheet:
  268. *
  269. * | It is recommended that the 12.20 High-Resolution format be
  270. * | utilized whenever the desired ratio is less than 4096 since
  271. * | the output frequency accuracy of the PLL is directly proportional
  272. * | to the accuracy of the timing reference clock and the resolution
  273. * | of the R_UD.
  274. *
  275. * This mode is only available in dynamic mode.
  276. */
  277. priv->lf_ratio = priv->dynamic_mode && ((rate / parent_rate) > 4096);
  278. return regmap_update_bits(priv->regmap, FUNC_CFG2, LFRATIO_MASK,
  279. priv->lf_ratio ? LFRATIO_20_12 : LFRATIO_12_20);
  280. }
  281. static int __cs2000_set_rate(struct cs2000_priv *priv, int ch,
  282. unsigned long rate, unsigned long parent_rate)
  283. {
  284. int ret;
  285. ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, FREEZE);
  286. if (ret < 0)
  287. return ret;
  288. ret = cs2000_select_ratio_mode(priv, rate, parent_rate);
  289. if (ret < 0)
  290. return ret;
  291. ret = cs2000_ratio_set(priv, ch, parent_rate, rate);
  292. if (ret < 0)
  293. return ret;
  294. ret = cs2000_ratio_select(priv, ch);
  295. if (ret < 0)
  296. return ret;
  297. ret = regmap_update_bits(priv->regmap, GLOBAL_CFG, FREEZE, 0);
  298. if (ret < 0)
  299. return ret;
  300. priv->saved_rate = rate;
  301. priv->saved_parent_rate = parent_rate;
  302. return 0;
  303. }
  304. static int cs2000_set_rate(struct clk_hw *hw,
  305. unsigned long rate, unsigned long parent_rate)
  306. {
  307. struct cs2000_priv *priv = hw_to_priv(hw);
  308. int ch = 0; /* it uses ch0 only at this point */
  309. return __cs2000_set_rate(priv, ch, rate, parent_rate);
  310. }
  311. static int cs2000_set_saved_rate(struct cs2000_priv *priv)
  312. {
  313. int ch = 0; /* it uses ch0 only at this point */
  314. return __cs2000_set_rate(priv, ch,
  315. priv->saved_rate,
  316. priv->saved_parent_rate);
  317. }
  318. static int cs2000_enable(struct clk_hw *hw)
  319. {
  320. struct cs2000_priv *priv = hw_to_priv(hw);
  321. int ret;
  322. ret = cs2000_enable_dev_config(priv, true);
  323. if (ret < 0)
  324. return ret;
  325. ret = cs2000_clk_out_enable(priv, true);
  326. if (ret < 0)
  327. return ret;
  328. ret = cs2000_wait_pll_lock(priv);
  329. if (ret < 0)
  330. return ret;
  331. return ret;
  332. }
  333. static void cs2000_disable(struct clk_hw *hw)
  334. {
  335. struct cs2000_priv *priv = hw_to_priv(hw);
  336. cs2000_enable_dev_config(priv, false);
  337. cs2000_clk_out_enable(priv, false);
  338. }
  339. static u8 cs2000_get_parent(struct clk_hw *hw)
  340. {
  341. struct cs2000_priv *priv = hw_to_priv(hw);
  342. /*
  343. * In dynamic mode, output rates are derived from CLK_IN.
  344. * In static mode, CLK_IN is ignored, so we return REF_CLK instead.
  345. */
  346. return priv->dynamic_mode ? CLK_IN : REF_CLK;
  347. }
  348. static const struct clk_ops cs2000_ops = {
  349. .get_parent = cs2000_get_parent,
  350. .recalc_rate = cs2000_recalc_rate,
  351. .determine_rate = cs2000_determine_rate,
  352. .set_rate = cs2000_set_rate,
  353. .prepare = cs2000_enable,
  354. .unprepare = cs2000_disable,
  355. };
  356. static int cs2000_clk_get(struct cs2000_priv *priv)
  357. {
  358. struct device *dev = priv_to_dev(priv);
  359. struct clk *clk_in, *ref_clk;
  360. clk_in = devm_clk_get(dev, "clk_in");
  361. /* not yet provided */
  362. if (IS_ERR(clk_in))
  363. return -EPROBE_DEFER;
  364. ref_clk = devm_clk_get(dev, "ref_clk");
  365. /* not yet provided */
  366. if (IS_ERR(ref_clk))
  367. return -EPROBE_DEFER;
  368. priv->clk_in = clk_in;
  369. priv->ref_clk = ref_clk;
  370. return 0;
  371. }
  372. static int cs2000_clk_register(struct cs2000_priv *priv)
  373. {
  374. struct device *dev = priv_to_dev(priv);
  375. struct device_node *np = dev->of_node;
  376. struct clk_init_data init;
  377. const char *name = np->name;
  378. static const char *parent_names[CLK_MAX];
  379. u32 aux_out = 0;
  380. int ref_clk_rate;
  381. int ch = 0; /* it uses ch0 only at this point */
  382. int ret;
  383. of_property_read_string(np, "clock-output-names", &name);
  384. priv->dynamic_mode = of_property_read_bool(np, "cirrus,dynamic-mode");
  385. dev_info(dev, "operating in %s mode\n",
  386. priv->dynamic_mode ? "dynamic" : "static");
  387. of_property_read_u32(np, "cirrus,aux-output-source", &aux_out);
  388. ret = regmap_update_bits(priv->regmap, DEVICE_CFG1,
  389. AUXOUTSRC_MASK, AUXOUTSRC(aux_out));
  390. if (ret < 0)
  391. return ret;
  392. priv->clk_skip = of_property_read_bool(np, "cirrus,clock-skip");
  393. ref_clk_rate = clk_get_rate(priv->ref_clk);
  394. ret = cs2000_ref_clk_bound_rate(priv, ref_clk_rate);
  395. if (ret < 0)
  396. return ret;
  397. if (priv->dynamic_mode) {
  398. /* Default to low-frequency mode to allow for large ratios */
  399. priv->lf_ratio = true;
  400. } else {
  401. /*
  402. * set default rate as 1/1.
  403. * otherwise .set_rate which setup ratio
  404. * is never called if user requests 1/1 rate
  405. */
  406. ret = __cs2000_set_rate(priv, ch, ref_clk_rate, ref_clk_rate);
  407. if (ret < 0)
  408. return ret;
  409. }
  410. parent_names[CLK_IN] = __clk_get_name(priv->clk_in);
  411. parent_names[REF_CLK] = __clk_get_name(priv->ref_clk);
  412. init.name = name;
  413. init.ops = &cs2000_ops;
  414. init.flags = CLK_SET_RATE_GATE;
  415. init.parent_names = parent_names;
  416. init.num_parents = ARRAY_SIZE(parent_names);
  417. priv->hw.init = &init;
  418. ret = clk_hw_register(dev, &priv->hw);
  419. if (ret)
  420. return ret;
  421. ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw);
  422. if (ret < 0) {
  423. clk_hw_unregister(&priv->hw);
  424. return ret;
  425. }
  426. return 0;
  427. }
  428. static int cs2000_version_print(struct cs2000_priv *priv)
  429. {
  430. struct device *dev = priv_to_dev(priv);
  431. const char *revision;
  432. unsigned int val;
  433. int ret;
  434. ret = regmap_read(priv->regmap, DEVICE_ID, &val);
  435. if (ret < 0)
  436. return ret;
  437. /* CS2000 should be 0x0 */
  438. if (val >> 3)
  439. return -EIO;
  440. switch (val & REVISION_MASK) {
  441. case REVISION_B2_B3:
  442. revision = "B2 / B3";
  443. break;
  444. case REVISION_C1:
  445. revision = "C1";
  446. break;
  447. default:
  448. return -EIO;
  449. }
  450. dev_info(dev, "revision - %s\n", revision);
  451. return 0;
  452. }
  453. static void cs2000_remove(struct i2c_client *client)
  454. {
  455. struct cs2000_priv *priv = i2c_get_clientdata(client);
  456. struct device *dev = priv_to_dev(priv);
  457. struct device_node *np = dev->of_node;
  458. of_clk_del_provider(np);
  459. clk_hw_unregister(&priv->hw);
  460. }
  461. static int cs2000_probe(struct i2c_client *client)
  462. {
  463. struct cs2000_priv *priv;
  464. struct device *dev = &client->dev;
  465. int ret;
  466. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  467. if (!priv)
  468. return -ENOMEM;
  469. priv->client = client;
  470. i2c_set_clientdata(client, priv);
  471. priv->regmap = devm_regmap_init_i2c(client, &cs2000_regmap_config);
  472. if (IS_ERR(priv->regmap))
  473. return PTR_ERR(priv->regmap);
  474. ret = cs2000_clk_get(priv);
  475. if (ret < 0)
  476. return ret;
  477. ret = cs2000_clk_register(priv);
  478. if (ret < 0)
  479. return ret;
  480. ret = cs2000_version_print(priv);
  481. if (ret < 0)
  482. goto probe_err;
  483. return 0;
  484. probe_err:
  485. cs2000_remove(client);
  486. return ret;
  487. }
  488. static int __maybe_unused cs2000_resume(struct device *dev)
  489. {
  490. struct cs2000_priv *priv = dev_get_drvdata(dev);
  491. return cs2000_set_saved_rate(priv);
  492. }
  493. static const struct dev_pm_ops cs2000_pm_ops = {
  494. SET_LATE_SYSTEM_SLEEP_PM_OPS(NULL, cs2000_resume)
  495. };
  496. static struct i2c_driver cs2000_driver = {
  497. .driver = {
  498. .name = "cs2000-cp",
  499. .pm = &cs2000_pm_ops,
  500. .of_match_table = cs2000_of_match,
  501. },
  502. .probe = cs2000_probe,
  503. .remove = cs2000_remove,
  504. .id_table = cs2000_id,
  505. };
  506. module_i2c_driver(cs2000_driver);
  507. MODULE_DESCRIPTION("CS2000-CP driver");
  508. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  509. MODULE_LICENSE("GPL v2");