clk-cdce706.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * TI CDCE706 programmable 3-PLL clock synthesizer driver
  3. *
  4. * Copyright (c) 2014 Cadence Design Systems Inc.
  5. *
  6. * Reference: http://www.ti.com/lit/ds/symlink/cdce706.pdf
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/rational.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #define CDCE706_CLKIN_CLOCK 10
  23. #define CDCE706_CLKIN_SOURCE 11
  24. #define CDCE706_PLL_M_LOW(pll) (1 + 3 * (pll))
  25. #define CDCE706_PLL_N_LOW(pll) (2 + 3 * (pll))
  26. #define CDCE706_PLL_HI(pll) (3 + 3 * (pll))
  27. #define CDCE706_PLL_MUX 3
  28. #define CDCE706_PLL_FVCO 6
  29. #define CDCE706_DIVIDER(div) (13 + (div))
  30. #define CDCE706_CLKOUT(out) (19 + (out))
  31. #define CDCE706_CLKIN_CLOCK_MASK 0x10
  32. #define CDCE706_CLKIN_SOURCE_SHIFT 6
  33. #define CDCE706_CLKIN_SOURCE_MASK 0xc0
  34. #define CDCE706_CLKIN_SOURCE_LVCMOS 0x40
  35. #define CDCE706_PLL_MUX_MASK(pll) (0x80 >> (pll))
  36. #define CDCE706_PLL_LOW_M_MASK 0xff
  37. #define CDCE706_PLL_LOW_N_MASK 0xff
  38. #define CDCE706_PLL_HI_M_MASK 0x1
  39. #define CDCE706_PLL_HI_N_MASK 0x1e
  40. #define CDCE706_PLL_HI_N_SHIFT 1
  41. #define CDCE706_PLL_M_MAX 0x1ff
  42. #define CDCE706_PLL_N_MAX 0xfff
  43. #define CDCE706_PLL_FVCO_MASK(pll) (0x80 >> (pll))
  44. #define CDCE706_PLL_FREQ_MIN 80000000
  45. #define CDCE706_PLL_FREQ_MAX 300000000
  46. #define CDCE706_PLL_FREQ_HI 180000000
  47. #define CDCE706_DIVIDER_PLL(div) (9 + (div) - ((div) > 2) - ((div) > 4))
  48. #define CDCE706_DIVIDER_PLL_SHIFT(div) ((div) < 2 ? 5 : 3 * ((div) & 1))
  49. #define CDCE706_DIVIDER_PLL_MASK(div) (0x7 << CDCE706_DIVIDER_PLL_SHIFT(div))
  50. #define CDCE706_DIVIDER_DIVIDER_MASK 0x7f
  51. #define CDCE706_DIVIDER_DIVIDER_MAX 0x7f
  52. #define CDCE706_CLKOUT_DIVIDER_MASK 0x7
  53. #define CDCE706_CLKOUT_ENABLE_MASK 0x8
  54. static const struct regmap_config cdce706_regmap_config = {
  55. .reg_bits = 8,
  56. .val_bits = 8,
  57. .val_format_endian = REGMAP_ENDIAN_NATIVE,
  58. };
  59. #define to_hw_data(phw) (container_of((phw), struct cdce706_hw_data, hw))
  60. struct cdce706_hw_data {
  61. struct cdce706_dev_data *dev_data;
  62. unsigned idx;
  63. unsigned parent;
  64. struct clk *clk;
  65. struct clk_hw hw;
  66. unsigned div;
  67. unsigned mul;
  68. unsigned mux;
  69. };
  70. struct cdce706_dev_data {
  71. struct i2c_client *client;
  72. struct regmap *regmap;
  73. struct clk_onecell_data onecell;
  74. struct clk *clks[6];
  75. struct clk *clkin_clk[2];
  76. const char *clkin_name[2];
  77. struct cdce706_hw_data clkin[1];
  78. struct cdce706_hw_data pll[3];
  79. struct cdce706_hw_data divider[6];
  80. struct cdce706_hw_data clkout[6];
  81. };
  82. static const char * const cdce706_source_name[] = {
  83. "clk_in0", "clk_in1",
  84. };
  85. static const char * const cdce706_clkin_name[] = {
  86. "clk_in",
  87. };
  88. static const char * const cdce706_pll_name[] = {
  89. "pll1", "pll2", "pll3",
  90. };
  91. static const char * const cdce706_divider_parent_name[] = {
  92. "clk_in", "pll1", "pll2", "pll2", "pll3",
  93. };
  94. static const char *cdce706_divider_name[] = {
  95. "p0", "p1", "p2", "p3", "p4", "p5",
  96. };
  97. static const char * const cdce706_clkout_name[] = {
  98. "clk_out0", "clk_out1", "clk_out2", "clk_out3", "clk_out4", "clk_out5",
  99. };
  100. static int cdce706_reg_read(struct cdce706_dev_data *dev_data, unsigned reg,
  101. unsigned *val)
  102. {
  103. int rc = regmap_read(dev_data->regmap, reg | 0x80, val);
  104. if (rc < 0)
  105. dev_err(&dev_data->client->dev, "error reading reg %u", reg);
  106. return rc;
  107. }
  108. static int cdce706_reg_write(struct cdce706_dev_data *dev_data, unsigned reg,
  109. unsigned val)
  110. {
  111. int rc = regmap_write(dev_data->regmap, reg | 0x80, val);
  112. if (rc < 0)
  113. dev_err(&dev_data->client->dev, "error writing reg %u", reg);
  114. return rc;
  115. }
  116. static int cdce706_reg_update(struct cdce706_dev_data *dev_data, unsigned reg,
  117. unsigned mask, unsigned val)
  118. {
  119. int rc = regmap_update_bits(dev_data->regmap, reg | 0x80, mask, val);
  120. if (rc < 0)
  121. dev_err(&dev_data->client->dev, "error updating reg %u", reg);
  122. return rc;
  123. }
  124. static int cdce706_clkin_set_parent(struct clk_hw *hw, u8 index)
  125. {
  126. struct cdce706_hw_data *hwd = to_hw_data(hw);
  127. hwd->parent = index;
  128. return 0;
  129. }
  130. static u8 cdce706_clkin_get_parent(struct clk_hw *hw)
  131. {
  132. struct cdce706_hw_data *hwd = to_hw_data(hw);
  133. return hwd->parent;
  134. }
  135. static const struct clk_ops cdce706_clkin_ops = {
  136. .set_parent = cdce706_clkin_set_parent,
  137. .get_parent = cdce706_clkin_get_parent,
  138. };
  139. static unsigned long cdce706_pll_recalc_rate(struct clk_hw *hw,
  140. unsigned long parent_rate)
  141. {
  142. struct cdce706_hw_data *hwd = to_hw_data(hw);
  143. dev_dbg(&hwd->dev_data->client->dev,
  144. "%s, pll: %d, mux: %d, mul: %u, div: %u\n",
  145. __func__, hwd->idx, hwd->mux, hwd->mul, hwd->div);
  146. if (!hwd->mux) {
  147. if (hwd->div && hwd->mul) {
  148. u64 res = (u64)parent_rate * hwd->mul;
  149. do_div(res, hwd->div);
  150. return res;
  151. }
  152. } else {
  153. if (hwd->div)
  154. return parent_rate / hwd->div;
  155. }
  156. return 0;
  157. }
  158. static long cdce706_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  159. unsigned long *parent_rate)
  160. {
  161. struct cdce706_hw_data *hwd = to_hw_data(hw);
  162. unsigned long mul, div;
  163. u64 res;
  164. dev_dbg(&hwd->dev_data->client->dev,
  165. "%s, rate: %lu, parent_rate: %lu\n",
  166. __func__, rate, *parent_rate);
  167. rational_best_approximation(rate, *parent_rate,
  168. CDCE706_PLL_N_MAX, CDCE706_PLL_M_MAX,
  169. &mul, &div);
  170. hwd->mul = mul;
  171. hwd->div = div;
  172. dev_dbg(&hwd->dev_data->client->dev,
  173. "%s, pll: %d, mul: %lu, div: %lu\n",
  174. __func__, hwd->idx, mul, div);
  175. res = (u64)*parent_rate * hwd->mul;
  176. do_div(res, hwd->div);
  177. return res;
  178. }
  179. static int cdce706_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  180. unsigned long parent_rate)
  181. {
  182. struct cdce706_hw_data *hwd = to_hw_data(hw);
  183. unsigned long mul = hwd->mul, div = hwd->div;
  184. int err;
  185. dev_dbg(&hwd->dev_data->client->dev,
  186. "%s, pll: %d, mul: %lu, div: %lu\n",
  187. __func__, hwd->idx, mul, div);
  188. err = cdce706_reg_update(hwd->dev_data,
  189. CDCE706_PLL_HI(hwd->idx),
  190. CDCE706_PLL_HI_M_MASK | CDCE706_PLL_HI_N_MASK,
  191. ((div >> 8) & CDCE706_PLL_HI_M_MASK) |
  192. ((mul >> (8 - CDCE706_PLL_HI_N_SHIFT)) &
  193. CDCE706_PLL_HI_N_MASK));
  194. if (err < 0)
  195. return err;
  196. err = cdce706_reg_write(hwd->dev_data,
  197. CDCE706_PLL_M_LOW(hwd->idx),
  198. div & CDCE706_PLL_LOW_M_MASK);
  199. if (err < 0)
  200. return err;
  201. err = cdce706_reg_write(hwd->dev_data,
  202. CDCE706_PLL_N_LOW(hwd->idx),
  203. mul & CDCE706_PLL_LOW_N_MASK);
  204. if (err < 0)
  205. return err;
  206. err = cdce706_reg_update(hwd->dev_data,
  207. CDCE706_PLL_FVCO,
  208. CDCE706_PLL_FVCO_MASK(hwd->idx),
  209. rate > CDCE706_PLL_FREQ_HI ?
  210. CDCE706_PLL_FVCO_MASK(hwd->idx) : 0);
  211. return err;
  212. }
  213. static const struct clk_ops cdce706_pll_ops = {
  214. .recalc_rate = cdce706_pll_recalc_rate,
  215. .round_rate = cdce706_pll_round_rate,
  216. .set_rate = cdce706_pll_set_rate,
  217. };
  218. static int cdce706_divider_set_parent(struct clk_hw *hw, u8 index)
  219. {
  220. struct cdce706_hw_data *hwd = to_hw_data(hw);
  221. if (hwd->parent == index)
  222. return 0;
  223. hwd->parent = index;
  224. return cdce706_reg_update(hwd->dev_data,
  225. CDCE706_DIVIDER_PLL(hwd->idx),
  226. CDCE706_DIVIDER_PLL_MASK(hwd->idx),
  227. index << CDCE706_DIVIDER_PLL_SHIFT(hwd->idx));
  228. }
  229. static u8 cdce706_divider_get_parent(struct clk_hw *hw)
  230. {
  231. struct cdce706_hw_data *hwd = to_hw_data(hw);
  232. return hwd->parent;
  233. }
  234. static unsigned long cdce706_divider_recalc_rate(struct clk_hw *hw,
  235. unsigned long parent_rate)
  236. {
  237. struct cdce706_hw_data *hwd = to_hw_data(hw);
  238. dev_dbg(&hwd->dev_data->client->dev,
  239. "%s, divider: %d, div: %u\n",
  240. __func__, hwd->idx, hwd->div);
  241. if (hwd->div)
  242. return parent_rate / hwd->div;
  243. return 0;
  244. }
  245. static long cdce706_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  246. unsigned long *parent_rate)
  247. {
  248. struct cdce706_hw_data *hwd = to_hw_data(hw);
  249. struct cdce706_dev_data *cdce = hwd->dev_data;
  250. unsigned long mul, div;
  251. dev_dbg(&hwd->dev_data->client->dev,
  252. "%s, rate: %lu, parent_rate: %lu\n",
  253. __func__, rate, *parent_rate);
  254. rational_best_approximation(rate, *parent_rate,
  255. 1, CDCE706_DIVIDER_DIVIDER_MAX,
  256. &mul, &div);
  257. if (!mul)
  258. div = CDCE706_DIVIDER_DIVIDER_MAX;
  259. if (__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT) {
  260. unsigned long best_diff = rate;
  261. unsigned long best_div = 0;
  262. struct clk *gp_clk = cdce->clkin_clk[cdce->clkin[0].parent];
  263. unsigned long gp_rate = gp_clk ? clk_get_rate(gp_clk) : 0;
  264. for (div = CDCE706_PLL_FREQ_MIN / rate; best_diff &&
  265. div <= CDCE706_PLL_FREQ_MAX / rate; ++div) {
  266. unsigned long n, m;
  267. unsigned long diff;
  268. unsigned long div_rate;
  269. u64 div_rate64;
  270. if (rate * div < CDCE706_PLL_FREQ_MIN)
  271. continue;
  272. rational_best_approximation(rate * div, gp_rate,
  273. CDCE706_PLL_N_MAX,
  274. CDCE706_PLL_M_MAX,
  275. &n, &m);
  276. div_rate64 = (u64)gp_rate * n;
  277. do_div(div_rate64, m);
  278. do_div(div_rate64, div);
  279. div_rate = div_rate64;
  280. diff = max(div_rate, rate) - min(div_rate, rate);
  281. if (diff < best_diff) {
  282. best_diff = diff;
  283. best_div = div;
  284. dev_dbg(&hwd->dev_data->client->dev,
  285. "%s, %lu * %lu / %lu / %lu = %lu\n",
  286. __func__, gp_rate, n, m, div, div_rate);
  287. }
  288. }
  289. div = best_div;
  290. dev_dbg(&hwd->dev_data->client->dev,
  291. "%s, altering parent rate: %lu -> %lu\n",
  292. __func__, *parent_rate, rate * div);
  293. *parent_rate = rate * div;
  294. }
  295. hwd->div = div;
  296. dev_dbg(&hwd->dev_data->client->dev,
  297. "%s, divider: %d, div: %lu\n",
  298. __func__, hwd->idx, div);
  299. return *parent_rate / div;
  300. }
  301. static int cdce706_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  302. unsigned long parent_rate)
  303. {
  304. struct cdce706_hw_data *hwd = to_hw_data(hw);
  305. dev_dbg(&hwd->dev_data->client->dev,
  306. "%s, divider: %d, div: %u\n",
  307. __func__, hwd->idx, hwd->div);
  308. return cdce706_reg_update(hwd->dev_data,
  309. CDCE706_DIVIDER(hwd->idx),
  310. CDCE706_DIVIDER_DIVIDER_MASK,
  311. hwd->div);
  312. }
  313. static const struct clk_ops cdce706_divider_ops = {
  314. .set_parent = cdce706_divider_set_parent,
  315. .get_parent = cdce706_divider_get_parent,
  316. .recalc_rate = cdce706_divider_recalc_rate,
  317. .round_rate = cdce706_divider_round_rate,
  318. .set_rate = cdce706_divider_set_rate,
  319. };
  320. static int cdce706_clkout_prepare(struct clk_hw *hw)
  321. {
  322. struct cdce706_hw_data *hwd = to_hw_data(hw);
  323. return cdce706_reg_update(hwd->dev_data, CDCE706_CLKOUT(hwd->idx),
  324. CDCE706_CLKOUT_ENABLE_MASK,
  325. CDCE706_CLKOUT_ENABLE_MASK);
  326. }
  327. static void cdce706_clkout_unprepare(struct clk_hw *hw)
  328. {
  329. struct cdce706_hw_data *hwd = to_hw_data(hw);
  330. cdce706_reg_update(hwd->dev_data, CDCE706_CLKOUT(hwd->idx),
  331. CDCE706_CLKOUT_ENABLE_MASK, 0);
  332. }
  333. static int cdce706_clkout_set_parent(struct clk_hw *hw, u8 index)
  334. {
  335. struct cdce706_hw_data *hwd = to_hw_data(hw);
  336. if (hwd->parent == index)
  337. return 0;
  338. hwd->parent = index;
  339. return cdce706_reg_update(hwd->dev_data,
  340. CDCE706_CLKOUT(hwd->idx),
  341. CDCE706_CLKOUT_ENABLE_MASK, index);
  342. }
  343. static u8 cdce706_clkout_get_parent(struct clk_hw *hw)
  344. {
  345. struct cdce706_hw_data *hwd = to_hw_data(hw);
  346. return hwd->parent;
  347. }
  348. static unsigned long cdce706_clkout_recalc_rate(struct clk_hw *hw,
  349. unsigned long parent_rate)
  350. {
  351. return parent_rate;
  352. }
  353. static long cdce706_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  354. unsigned long *parent_rate)
  355. {
  356. *parent_rate = rate;
  357. return rate;
  358. }
  359. static int cdce706_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  360. unsigned long parent_rate)
  361. {
  362. return 0;
  363. }
  364. static const struct clk_ops cdce706_clkout_ops = {
  365. .prepare = cdce706_clkout_prepare,
  366. .unprepare = cdce706_clkout_unprepare,
  367. .set_parent = cdce706_clkout_set_parent,
  368. .get_parent = cdce706_clkout_get_parent,
  369. .recalc_rate = cdce706_clkout_recalc_rate,
  370. .round_rate = cdce706_clkout_round_rate,
  371. .set_rate = cdce706_clkout_set_rate,
  372. };
  373. static int cdce706_register_hw(struct cdce706_dev_data *cdce,
  374. struct cdce706_hw_data *hw, unsigned num_hw,
  375. const char * const *clk_names,
  376. struct clk_init_data *init)
  377. {
  378. unsigned i;
  379. for (i = 0; i < num_hw; ++i, ++hw) {
  380. init->name = clk_names[i];
  381. hw->dev_data = cdce;
  382. hw->idx = i;
  383. hw->hw.init = init;
  384. hw->clk = devm_clk_register(&cdce->client->dev,
  385. &hw->hw);
  386. if (IS_ERR(hw->clk)) {
  387. dev_err(&cdce->client->dev, "Failed to register %s\n",
  388. clk_names[i]);
  389. return PTR_ERR(hw->clk);
  390. }
  391. }
  392. return 0;
  393. }
  394. static int cdce706_register_clkin(struct cdce706_dev_data *cdce)
  395. {
  396. struct clk_init_data init = {
  397. .ops = &cdce706_clkin_ops,
  398. .parent_names = cdce->clkin_name,
  399. .num_parents = ARRAY_SIZE(cdce->clkin_name),
  400. };
  401. unsigned i;
  402. int ret;
  403. unsigned clock, source;
  404. for (i = 0; i < ARRAY_SIZE(cdce->clkin_name); ++i) {
  405. struct clk *parent = devm_clk_get(&cdce->client->dev,
  406. cdce706_source_name[i]);
  407. if (IS_ERR(parent)) {
  408. cdce->clkin_name[i] = cdce706_source_name[i];
  409. } else {
  410. cdce->clkin_name[i] = __clk_get_name(parent);
  411. cdce->clkin_clk[i] = parent;
  412. }
  413. }
  414. ret = cdce706_reg_read(cdce, CDCE706_CLKIN_SOURCE, &source);
  415. if (ret < 0)
  416. return ret;
  417. if ((source & CDCE706_CLKIN_SOURCE_MASK) ==
  418. CDCE706_CLKIN_SOURCE_LVCMOS) {
  419. ret = cdce706_reg_read(cdce, CDCE706_CLKIN_CLOCK, &clock);
  420. if (ret < 0)
  421. return ret;
  422. cdce->clkin[0].parent = !!(clock & CDCE706_CLKIN_CLOCK_MASK);
  423. }
  424. ret = cdce706_register_hw(cdce, cdce->clkin,
  425. ARRAY_SIZE(cdce->clkin),
  426. cdce706_clkin_name, &init);
  427. return ret;
  428. }
  429. static int cdce706_register_plls(struct cdce706_dev_data *cdce)
  430. {
  431. struct clk_init_data init = {
  432. .ops = &cdce706_pll_ops,
  433. .parent_names = cdce706_clkin_name,
  434. .num_parents = ARRAY_SIZE(cdce706_clkin_name),
  435. };
  436. unsigned i;
  437. int ret;
  438. unsigned mux;
  439. ret = cdce706_reg_read(cdce, CDCE706_PLL_MUX, &mux);
  440. if (ret < 0)
  441. return ret;
  442. for (i = 0; i < ARRAY_SIZE(cdce->pll); ++i) {
  443. unsigned m, n, v;
  444. ret = cdce706_reg_read(cdce, CDCE706_PLL_M_LOW(i), &m);
  445. if (ret < 0)
  446. return ret;
  447. ret = cdce706_reg_read(cdce, CDCE706_PLL_N_LOW(i), &n);
  448. if (ret < 0)
  449. return ret;
  450. ret = cdce706_reg_read(cdce, CDCE706_PLL_HI(i), &v);
  451. if (ret < 0)
  452. return ret;
  453. cdce->pll[i].div = m | ((v & CDCE706_PLL_HI_M_MASK) << 8);
  454. cdce->pll[i].mul = n | ((v & CDCE706_PLL_HI_N_MASK) <<
  455. (8 - CDCE706_PLL_HI_N_SHIFT));
  456. cdce->pll[i].mux = mux & CDCE706_PLL_MUX_MASK(i);
  457. dev_dbg(&cdce->client->dev,
  458. "%s: i: %u, div: %u, mul: %u, mux: %d\n", __func__, i,
  459. cdce->pll[i].div, cdce->pll[i].mul, cdce->pll[i].mux);
  460. }
  461. ret = cdce706_register_hw(cdce, cdce->pll,
  462. ARRAY_SIZE(cdce->pll),
  463. cdce706_pll_name, &init);
  464. return ret;
  465. }
  466. static int cdce706_register_dividers(struct cdce706_dev_data *cdce)
  467. {
  468. struct clk_init_data init = {
  469. .ops = &cdce706_divider_ops,
  470. .parent_names = cdce706_divider_parent_name,
  471. .num_parents = ARRAY_SIZE(cdce706_divider_parent_name),
  472. .flags = CLK_SET_RATE_PARENT,
  473. };
  474. unsigned i;
  475. int ret;
  476. for (i = 0; i < ARRAY_SIZE(cdce->divider); ++i) {
  477. unsigned val;
  478. ret = cdce706_reg_read(cdce, CDCE706_DIVIDER_PLL(i), &val);
  479. if (ret < 0)
  480. return ret;
  481. cdce->divider[i].parent =
  482. (val & CDCE706_DIVIDER_PLL_MASK(i)) >>
  483. CDCE706_DIVIDER_PLL_SHIFT(i);
  484. ret = cdce706_reg_read(cdce, CDCE706_DIVIDER(i), &val);
  485. if (ret < 0)
  486. return ret;
  487. cdce->divider[i].div = val & CDCE706_DIVIDER_DIVIDER_MASK;
  488. dev_dbg(&cdce->client->dev,
  489. "%s: i: %u, parent: %u, div: %u\n", __func__, i,
  490. cdce->divider[i].parent, cdce->divider[i].div);
  491. }
  492. ret = cdce706_register_hw(cdce, cdce->divider,
  493. ARRAY_SIZE(cdce->divider),
  494. cdce706_divider_name, &init);
  495. return ret;
  496. }
  497. static int cdce706_register_clkouts(struct cdce706_dev_data *cdce)
  498. {
  499. struct clk_init_data init = {
  500. .ops = &cdce706_clkout_ops,
  501. .parent_names = cdce706_divider_name,
  502. .num_parents = ARRAY_SIZE(cdce706_divider_name),
  503. .flags = CLK_SET_RATE_PARENT,
  504. };
  505. unsigned i;
  506. int ret;
  507. for (i = 0; i < ARRAY_SIZE(cdce->clkout); ++i) {
  508. unsigned val;
  509. ret = cdce706_reg_read(cdce, CDCE706_CLKOUT(i), &val);
  510. if (ret < 0)
  511. return ret;
  512. cdce->clkout[i].parent = val & CDCE706_CLKOUT_DIVIDER_MASK;
  513. dev_dbg(&cdce->client->dev,
  514. "%s: i: %u, parent: %u\n", __func__, i,
  515. cdce->clkout[i].parent);
  516. }
  517. ret = cdce706_register_hw(cdce, cdce->clkout,
  518. ARRAY_SIZE(cdce->clkout),
  519. cdce706_clkout_name, &init);
  520. for (i = 0; i < ARRAY_SIZE(cdce->clkout); ++i)
  521. cdce->clks[i] = cdce->clkout[i].clk;
  522. return ret;
  523. }
  524. static int cdce706_probe(struct i2c_client *client,
  525. const struct i2c_device_id *id)
  526. {
  527. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  528. struct cdce706_dev_data *cdce;
  529. int ret;
  530. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  531. return -EIO;
  532. cdce = devm_kzalloc(&client->dev, sizeof(*cdce), GFP_KERNEL);
  533. if (!cdce)
  534. return -ENOMEM;
  535. cdce->client = client;
  536. cdce->regmap = devm_regmap_init_i2c(client, &cdce706_regmap_config);
  537. if (IS_ERR(cdce->regmap)) {
  538. dev_err(&client->dev, "Failed to initialize regmap\n");
  539. return -EINVAL;
  540. }
  541. i2c_set_clientdata(client, cdce);
  542. ret = cdce706_register_clkin(cdce);
  543. if (ret < 0)
  544. return ret;
  545. ret = cdce706_register_plls(cdce);
  546. if (ret < 0)
  547. return ret;
  548. ret = cdce706_register_dividers(cdce);
  549. if (ret < 0)
  550. return ret;
  551. ret = cdce706_register_clkouts(cdce);
  552. if (ret < 0)
  553. return ret;
  554. cdce->onecell.clks = cdce->clks;
  555. cdce->onecell.clk_num = ARRAY_SIZE(cdce->clks);
  556. ret = of_clk_add_provider(client->dev.of_node, of_clk_src_onecell_get,
  557. &cdce->onecell);
  558. return ret;
  559. }
  560. static int cdce706_remove(struct i2c_client *client)
  561. {
  562. of_clk_del_provider(client->dev.of_node);
  563. return 0;
  564. }
  565. #ifdef CONFIG_OF
  566. static const struct of_device_id cdce706_dt_match[] = {
  567. { .compatible = "ti,cdce706" },
  568. { },
  569. };
  570. MODULE_DEVICE_TABLE(of, cdce706_dt_match);
  571. #endif
  572. static const struct i2c_device_id cdce706_id[] = {
  573. { "cdce706", 0 },
  574. { }
  575. };
  576. MODULE_DEVICE_TABLE(i2c, cdce706_id);
  577. static struct i2c_driver cdce706_i2c_driver = {
  578. .driver = {
  579. .name = "cdce706",
  580. .of_match_table = of_match_ptr(cdce706_dt_match),
  581. },
  582. .probe = cdce706_probe,
  583. .remove = cdce706_remove,
  584. .id_table = cdce706_id,
  585. };
  586. module_i2c_driver(cdce706_i2c_driver);
  587. MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
  588. MODULE_DESCRIPTION("TI CDCE 706 clock synthesizer driver");
  589. MODULE_LICENSE("GPL");