clk-cdce706.c 18 KB

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