clk-cs2000-cp.c 10 KB

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