clk-cs2000-cp.c 10 KB

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