clk-cs2000-cp.c 12 KB

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