clk-cs2000-cp.c 11 KB

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