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