clk-cdce925.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Driver for TI Dual PLL CDCE925 clock synthesizer
  3. *
  4. * This driver always connects the Y1 to the input clock, Y2/Y3 to PLL1
  5. * and Y4/Y5 to PLL2. PLL frequency is set on a first-come-first-serve
  6. * basis. Clients can directly request any frequency that the chip can
  7. * deliver using the standard clk framework. In addition, the device can
  8. * be configured and activated via the devicetree.
  9. *
  10. * Copyright (C) 2014, Topic Embedded Products
  11. * Licenced under GPL
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/gcd.h>
  21. /* The chip has 2 PLLs which can be routed through dividers to 5 outputs.
  22. * Model this as 2 PLL clocks which are parents to the outputs.
  23. */
  24. #define NUMBER_OF_PLLS 2
  25. #define NUMBER_OF_OUTPUTS 5
  26. #define CDCE925_REG_GLOBAL1 0x01
  27. #define CDCE925_REG_Y1SPIPDIVH 0x02
  28. #define CDCE925_REG_PDIVL 0x03
  29. #define CDCE925_REG_XCSEL 0x05
  30. /* PLL parameters start at 0x10, steps of 0x10 */
  31. #define CDCE925_OFFSET_PLL 0x10
  32. /* Add CDCE925_OFFSET_PLL * (pll) to these registers before sending */
  33. #define CDCE925_PLL_MUX_OUTPUTS 0x14
  34. #define CDCE925_PLL_MULDIV 0x18
  35. #define CDCE925_PLL_FREQUENCY_MIN 80000000ul
  36. #define CDCE925_PLL_FREQUENCY_MAX 230000000ul
  37. struct clk_cdce925_chip;
  38. struct clk_cdce925_output {
  39. struct clk_hw hw;
  40. struct clk_cdce925_chip *chip;
  41. u8 index;
  42. u16 pdiv; /* 1..127 for Y2-Y5; 1..1023 for Y1 */
  43. };
  44. #define to_clk_cdce925_output(_hw) \
  45. container_of(_hw, struct clk_cdce925_output, hw)
  46. struct clk_cdce925_pll {
  47. struct clk_hw hw;
  48. struct clk_cdce925_chip *chip;
  49. u8 index;
  50. u16 m; /* 1..511 */
  51. u16 n; /* 1..4095 */
  52. };
  53. #define to_clk_cdce925_pll(_hw) container_of(_hw, struct clk_cdce925_pll, hw)
  54. struct clk_cdce925_chip {
  55. struct regmap *regmap;
  56. struct i2c_client *i2c_client;
  57. struct clk_cdce925_pll pll[NUMBER_OF_PLLS];
  58. struct clk_cdce925_output clk[NUMBER_OF_OUTPUTS];
  59. };
  60. /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
  61. static unsigned long cdce925_pll_calculate_rate(unsigned long parent_rate,
  62. u16 n, u16 m)
  63. {
  64. if ((!m || !n) || (m == n))
  65. return parent_rate; /* In bypass mode runs at same frequency */
  66. return mult_frac(parent_rate, (unsigned long)n, (unsigned long)m);
  67. }
  68. static unsigned long cdce925_pll_recalc_rate(struct clk_hw *hw,
  69. unsigned long parent_rate)
  70. {
  71. /* Output frequency of PLL is Fout = (Fin/Pdiv)*(N/M) */
  72. struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
  73. return cdce925_pll_calculate_rate(parent_rate, data->n, data->m);
  74. }
  75. static void cdce925_pll_find_rate(unsigned long rate,
  76. unsigned long parent_rate, u16 *n, u16 *m)
  77. {
  78. unsigned long un;
  79. unsigned long um;
  80. unsigned long g;
  81. if (rate <= parent_rate) {
  82. /* Can always deliver parent_rate in bypass mode */
  83. rate = parent_rate;
  84. *n = 0;
  85. *m = 0;
  86. } else {
  87. /* In PLL mode, need to apply min/max range */
  88. if (rate < CDCE925_PLL_FREQUENCY_MIN)
  89. rate = CDCE925_PLL_FREQUENCY_MIN;
  90. else if (rate > CDCE925_PLL_FREQUENCY_MAX)
  91. rate = CDCE925_PLL_FREQUENCY_MAX;
  92. g = gcd(rate, parent_rate);
  93. um = parent_rate / g;
  94. un = rate / g;
  95. /* When outside hw range, reduce to fit (rounding errors) */
  96. while ((un > 4095) || (um > 511)) {
  97. un >>= 1;
  98. um >>= 1;
  99. }
  100. if (un == 0)
  101. un = 1;
  102. if (um == 0)
  103. um = 1;
  104. *n = un;
  105. *m = um;
  106. }
  107. }
  108. static long cdce925_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  109. unsigned long *parent_rate)
  110. {
  111. u16 n, m;
  112. cdce925_pll_find_rate(rate, *parent_rate, &n, &m);
  113. return (long)cdce925_pll_calculate_rate(*parent_rate, n, m);
  114. }
  115. static int cdce925_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  116. unsigned long parent_rate)
  117. {
  118. struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
  119. if (!rate || (rate == parent_rate)) {
  120. data->m = 0; /* Bypass mode */
  121. data->n = 0;
  122. return 0;
  123. }
  124. if ((rate < CDCE925_PLL_FREQUENCY_MIN) ||
  125. (rate > CDCE925_PLL_FREQUENCY_MAX)) {
  126. pr_debug("%s: rate %lu outside PLL range.\n", __func__, rate);
  127. return -EINVAL;
  128. }
  129. if (rate < parent_rate) {
  130. pr_debug("%s: rate %lu less than parent rate %lu.\n", __func__,
  131. rate, parent_rate);
  132. return -EINVAL;
  133. }
  134. cdce925_pll_find_rate(rate, parent_rate, &data->n, &data->m);
  135. return 0;
  136. }
  137. /* calculate p = max(0, 4 - int(log2 (n/m))) */
  138. static u8 cdce925_pll_calc_p(u16 n, u16 m)
  139. {
  140. u8 p;
  141. u16 r = n / m;
  142. if (r >= 16)
  143. return 0;
  144. p = 4;
  145. while (r > 1) {
  146. r >>= 1;
  147. --p;
  148. }
  149. return p;
  150. }
  151. /* Returns VCO range bits for VCO1_0_RANGE */
  152. static u8 cdce925_pll_calc_range_bits(struct clk_hw *hw, u16 n, u16 m)
  153. {
  154. struct clk *parent = clk_get_parent(hw->clk);
  155. unsigned long rate = clk_get_rate(parent);
  156. rate = mult_frac(rate, (unsigned long)n, (unsigned long)m);
  157. if (rate >= 175000000)
  158. return 0x3;
  159. if (rate >= 150000000)
  160. return 0x02;
  161. if (rate >= 125000000)
  162. return 0x01;
  163. return 0x00;
  164. }
  165. /* I2C clock, hence everything must happen in (un)prepare because this
  166. * may sleep */
  167. static int cdce925_pll_prepare(struct clk_hw *hw)
  168. {
  169. struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
  170. u16 n = data->n;
  171. u16 m = data->m;
  172. u16 r;
  173. u8 q;
  174. u8 p;
  175. u16 nn;
  176. u8 pll[4]; /* Bits are spread out over 4 byte registers */
  177. u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
  178. unsigned i;
  179. if ((!m || !n) || (m == n)) {
  180. /* Set PLL mux to bypass mode, leave the rest as is */
  181. regmap_update_bits(data->chip->regmap,
  182. reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
  183. } else {
  184. /* According to data sheet: */
  185. /* p = max(0, 4 - int(log2 (n/m))) */
  186. p = cdce925_pll_calc_p(n, m);
  187. /* nn = n * 2^p */
  188. nn = n * BIT(p);
  189. /* q = int(nn/m) */
  190. q = nn / m;
  191. if ((q < 16) || (1 > 64)) {
  192. pr_debug("%s invalid q=%d\n", __func__, q);
  193. return -EINVAL;
  194. }
  195. r = nn - (m*q);
  196. if (r > 511) {
  197. pr_debug("%s invalid r=%d\n", __func__, r);
  198. return -EINVAL;
  199. }
  200. pr_debug("%s n=%d m=%d p=%d q=%d r=%d\n", __func__,
  201. n, m, p, q, r);
  202. /* encode into register bits */
  203. pll[0] = n >> 4;
  204. pll[1] = ((n & 0x0F) << 4) | ((r >> 5) & 0x0F);
  205. pll[2] = ((r & 0x1F) << 3) | ((q >> 3) & 0x07);
  206. pll[3] = ((q & 0x07) << 5) | (p << 2) |
  207. cdce925_pll_calc_range_bits(hw, n, m);
  208. /* Write to registers */
  209. for (i = 0; i < ARRAY_SIZE(pll); ++i)
  210. regmap_write(data->chip->regmap,
  211. reg_ofs + CDCE925_PLL_MULDIV + i, pll[i]);
  212. /* Enable PLL */
  213. regmap_update_bits(data->chip->regmap,
  214. reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x00);
  215. }
  216. return 0;
  217. }
  218. static void cdce925_pll_unprepare(struct clk_hw *hw)
  219. {
  220. struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
  221. u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
  222. regmap_update_bits(data->chip->regmap,
  223. reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
  224. }
  225. static const struct clk_ops cdce925_pll_ops = {
  226. .prepare = cdce925_pll_prepare,
  227. .unprepare = cdce925_pll_unprepare,
  228. .recalc_rate = cdce925_pll_recalc_rate,
  229. .round_rate = cdce925_pll_round_rate,
  230. .set_rate = cdce925_pll_set_rate,
  231. };
  232. static void cdce925_clk_set_pdiv(struct clk_cdce925_output *data, u16 pdiv)
  233. {
  234. switch (data->index) {
  235. case 0:
  236. regmap_update_bits(data->chip->regmap,
  237. CDCE925_REG_Y1SPIPDIVH,
  238. 0x03, (pdiv >> 8) & 0x03);
  239. regmap_write(data->chip->regmap, 0x03, pdiv & 0xFF);
  240. break;
  241. case 1:
  242. regmap_update_bits(data->chip->regmap, 0x16, 0x7F, pdiv);
  243. break;
  244. case 2:
  245. regmap_update_bits(data->chip->regmap, 0x17, 0x7F, pdiv);
  246. break;
  247. case 3:
  248. regmap_update_bits(data->chip->regmap, 0x26, 0x7F, pdiv);
  249. break;
  250. case 4:
  251. regmap_update_bits(data->chip->regmap, 0x27, 0x7F, pdiv);
  252. break;
  253. }
  254. }
  255. static void cdce925_clk_activate(struct clk_cdce925_output *data)
  256. {
  257. switch (data->index) {
  258. case 0:
  259. regmap_update_bits(data->chip->regmap,
  260. CDCE925_REG_Y1SPIPDIVH, 0x0c, 0x0c);
  261. break;
  262. case 1:
  263. case 2:
  264. regmap_update_bits(data->chip->regmap, 0x14, 0x03, 0x03);
  265. break;
  266. case 3:
  267. case 4:
  268. regmap_update_bits(data->chip->regmap, 0x24, 0x03, 0x03);
  269. break;
  270. }
  271. }
  272. static int cdce925_clk_prepare(struct clk_hw *hw)
  273. {
  274. struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
  275. cdce925_clk_set_pdiv(data, data->pdiv);
  276. cdce925_clk_activate(data);
  277. return 0;
  278. }
  279. static void cdce925_clk_unprepare(struct clk_hw *hw)
  280. {
  281. struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
  282. /* Disable clock by setting divider to "0" */
  283. cdce925_clk_set_pdiv(data, 0);
  284. }
  285. static unsigned long cdce925_clk_recalc_rate(struct clk_hw *hw,
  286. unsigned long parent_rate)
  287. {
  288. struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
  289. if (data->pdiv)
  290. return parent_rate / data->pdiv;
  291. return 0;
  292. }
  293. static u16 cdce925_calc_divider(unsigned long rate,
  294. unsigned long parent_rate)
  295. {
  296. unsigned long divider;
  297. if (!rate)
  298. return 0;
  299. if (rate >= parent_rate)
  300. return 1;
  301. divider = DIV_ROUND_CLOSEST(parent_rate, rate);
  302. if (divider > 0x7F)
  303. divider = 0x7F;
  304. return (u16)divider;
  305. }
  306. static unsigned long cdce925_clk_best_parent_rate(
  307. struct clk_hw *hw, unsigned long rate)
  308. {
  309. struct clk *pll = clk_get_parent(hw->clk);
  310. struct clk *root = clk_get_parent(pll);
  311. unsigned long root_rate = clk_get_rate(root);
  312. unsigned long best_rate_error = rate;
  313. u16 pdiv_min;
  314. u16 pdiv_max;
  315. u16 pdiv_best;
  316. u16 pdiv_now;
  317. if (root_rate % rate == 0)
  318. return root_rate; /* Don't need the PLL, use bypass */
  319. pdiv_min = (u16)max(1ul, DIV_ROUND_UP(CDCE925_PLL_FREQUENCY_MIN, rate));
  320. pdiv_max = (u16)min(127ul, CDCE925_PLL_FREQUENCY_MAX / rate);
  321. if (pdiv_min > pdiv_max)
  322. return 0; /* No can do? */
  323. pdiv_best = pdiv_min;
  324. for (pdiv_now = pdiv_min; pdiv_now < pdiv_max; ++pdiv_now) {
  325. unsigned long target_rate = rate * pdiv_now;
  326. long pll_rate = clk_round_rate(pll, target_rate);
  327. unsigned long actual_rate;
  328. unsigned long rate_error;
  329. if (pll_rate <= 0)
  330. continue;
  331. actual_rate = pll_rate / pdiv_now;
  332. rate_error = abs((long)actual_rate - (long)rate);
  333. if (rate_error < best_rate_error) {
  334. pdiv_best = pdiv_now;
  335. best_rate_error = rate_error;
  336. }
  337. /* TODO: Consider PLL frequency based on smaller n/m values
  338. * and pick the better one if the error is equal */
  339. }
  340. return rate * pdiv_best;
  341. }
  342. static long cdce925_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  343. unsigned long *parent_rate)
  344. {
  345. unsigned long l_parent_rate = *parent_rate;
  346. u16 divider = cdce925_calc_divider(rate, l_parent_rate);
  347. if (l_parent_rate / divider != rate) {
  348. l_parent_rate = cdce925_clk_best_parent_rate(hw, rate);
  349. divider = cdce925_calc_divider(rate, l_parent_rate);
  350. *parent_rate = l_parent_rate;
  351. }
  352. if (divider)
  353. return (long)(l_parent_rate / divider);
  354. return 0;
  355. }
  356. static int cdce925_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  357. unsigned long parent_rate)
  358. {
  359. struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
  360. data->pdiv = cdce925_calc_divider(rate, parent_rate);
  361. return 0;
  362. }
  363. static const struct clk_ops cdce925_clk_ops = {
  364. .prepare = cdce925_clk_prepare,
  365. .unprepare = cdce925_clk_unprepare,
  366. .recalc_rate = cdce925_clk_recalc_rate,
  367. .round_rate = cdce925_clk_round_rate,
  368. .set_rate = cdce925_clk_set_rate,
  369. };
  370. static u16 cdce925_y1_calc_divider(unsigned long rate,
  371. unsigned long parent_rate)
  372. {
  373. unsigned long divider;
  374. if (!rate)
  375. return 0;
  376. if (rate >= parent_rate)
  377. return 1;
  378. divider = DIV_ROUND_CLOSEST(parent_rate, rate);
  379. if (divider > 0x3FF) /* Y1 has 10-bit divider */
  380. divider = 0x3FF;
  381. return (u16)divider;
  382. }
  383. static long cdce925_clk_y1_round_rate(struct clk_hw *hw, unsigned long rate,
  384. unsigned long *parent_rate)
  385. {
  386. unsigned long l_parent_rate = *parent_rate;
  387. u16 divider = cdce925_y1_calc_divider(rate, l_parent_rate);
  388. if (divider)
  389. return (long)(l_parent_rate / divider);
  390. return 0;
  391. }
  392. static int cdce925_clk_y1_set_rate(struct clk_hw *hw, unsigned long rate,
  393. unsigned long parent_rate)
  394. {
  395. struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
  396. data->pdiv = cdce925_y1_calc_divider(rate, parent_rate);
  397. return 0;
  398. }
  399. static const struct clk_ops cdce925_clk_y1_ops = {
  400. .prepare = cdce925_clk_prepare,
  401. .unprepare = cdce925_clk_unprepare,
  402. .recalc_rate = cdce925_clk_recalc_rate,
  403. .round_rate = cdce925_clk_y1_round_rate,
  404. .set_rate = cdce925_clk_y1_set_rate,
  405. };
  406. static struct regmap_config cdce925_regmap_config = {
  407. .name = "configuration0",
  408. .reg_bits = 8,
  409. .val_bits = 8,
  410. .cache_type = REGCACHE_RBTREE,
  411. .max_register = 0x2F,
  412. };
  413. #define CDCE925_I2C_COMMAND_BLOCK_TRANSFER 0x00
  414. #define CDCE925_I2C_COMMAND_BYTE_TRANSFER 0x80
  415. static int cdce925_regmap_i2c_write(
  416. void *context, const void *data, size_t count)
  417. {
  418. struct device *dev = context;
  419. struct i2c_client *i2c = to_i2c_client(dev);
  420. int ret;
  421. u8 reg_data[2];
  422. if (count != 2)
  423. return -ENOTSUPP;
  424. /* First byte is command code */
  425. reg_data[0] = CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)data)[0];
  426. reg_data[1] = ((u8 *)data)[1];
  427. dev_dbg(&i2c->dev, "%s(%zu) %#x %#x\n", __func__, count,
  428. reg_data[0], reg_data[1]);
  429. ret = i2c_master_send(i2c, reg_data, count);
  430. if (likely(ret == count))
  431. return 0;
  432. else if (ret < 0)
  433. return ret;
  434. else
  435. return -EIO;
  436. }
  437. static int cdce925_regmap_i2c_read(void *context,
  438. const void *reg, size_t reg_size, void *val, size_t val_size)
  439. {
  440. struct device *dev = context;
  441. struct i2c_client *i2c = to_i2c_client(dev);
  442. struct i2c_msg xfer[2];
  443. int ret;
  444. u8 reg_data[2];
  445. if (reg_size != 1)
  446. return -ENOTSUPP;
  447. xfer[0].addr = i2c->addr;
  448. xfer[0].flags = 0;
  449. xfer[0].buf = reg_data;
  450. if (val_size == 1) {
  451. reg_data[0] =
  452. CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)reg)[0];
  453. xfer[0].len = 1;
  454. } else {
  455. reg_data[0] =
  456. CDCE925_I2C_COMMAND_BLOCK_TRANSFER | ((u8 *)reg)[0];
  457. reg_data[1] = val_size;
  458. xfer[0].len = 2;
  459. }
  460. xfer[1].addr = i2c->addr;
  461. xfer[1].flags = I2C_M_RD;
  462. xfer[1].len = val_size;
  463. xfer[1].buf = val;
  464. ret = i2c_transfer(i2c->adapter, xfer, 2);
  465. if (likely(ret == 2)) {
  466. dev_dbg(&i2c->dev, "%s(%zu, %zu) %#x %#x\n", __func__,
  467. reg_size, val_size, reg_data[0], *((u8 *)val));
  468. return 0;
  469. } else if (ret < 0)
  470. return ret;
  471. else
  472. return -EIO;
  473. }
  474. static struct clk_hw *
  475. of_clk_cdce925_get(struct of_phandle_args *clkspec, void *_data)
  476. {
  477. struct clk_cdce925_chip *data = _data;
  478. unsigned int idx = clkspec->args[0];
  479. if (idx >= ARRAY_SIZE(data->clk)) {
  480. pr_err("%s: invalid index %u\n", __func__, idx);
  481. return ERR_PTR(-EINVAL);
  482. }
  483. return &data->clk[idx].hw;
  484. }
  485. /* The CDCE925 uses a funky way to read/write registers. Bulk mode is
  486. * just weird, so just use the single byte mode exclusively. */
  487. static struct regmap_bus regmap_cdce925_bus = {
  488. .write = cdce925_regmap_i2c_write,
  489. .read = cdce925_regmap_i2c_read,
  490. };
  491. static int cdce925_probe(struct i2c_client *client,
  492. const struct i2c_device_id *id)
  493. {
  494. struct clk_cdce925_chip *data;
  495. struct device_node *node = client->dev.of_node;
  496. const char *parent_name;
  497. const char *pll_clk_name[NUMBER_OF_PLLS] = {NULL,};
  498. struct clk_init_data init;
  499. u32 value;
  500. int i;
  501. int err;
  502. struct device_node *np_output;
  503. char child_name[6];
  504. dev_dbg(&client->dev, "%s\n", __func__);
  505. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  506. if (!data)
  507. return -ENOMEM;
  508. data->i2c_client = client;
  509. data->regmap = devm_regmap_init(&client->dev, &regmap_cdce925_bus,
  510. &client->dev, &cdce925_regmap_config);
  511. if (IS_ERR(data->regmap)) {
  512. dev_err(&client->dev, "failed to allocate register map\n");
  513. return PTR_ERR(data->regmap);
  514. }
  515. i2c_set_clientdata(client, data);
  516. parent_name = of_clk_get_parent_name(node, 0);
  517. if (!parent_name) {
  518. dev_err(&client->dev, "missing parent clock\n");
  519. return -ENODEV;
  520. }
  521. dev_dbg(&client->dev, "parent is: %s\n", parent_name);
  522. if (of_property_read_u32(node, "xtal-load-pf", &value) == 0)
  523. regmap_write(data->regmap,
  524. CDCE925_REG_XCSEL, (value << 3) & 0xF8);
  525. /* PWDN bit */
  526. regmap_update_bits(data->regmap, CDCE925_REG_GLOBAL1, BIT(4), 0);
  527. /* Set input source for Y1 to be the XTAL */
  528. regmap_update_bits(data->regmap, 0x02, BIT(7), 0);
  529. init.ops = &cdce925_pll_ops;
  530. init.flags = 0;
  531. init.parent_names = &parent_name;
  532. init.num_parents = parent_name ? 1 : 0;
  533. /* Register PLL clocks */
  534. for (i = 0; i < NUMBER_OF_PLLS; ++i) {
  535. pll_clk_name[i] = kasprintf(GFP_KERNEL, "%s.pll%d",
  536. client->dev.of_node->name, i);
  537. init.name = pll_clk_name[i];
  538. data->pll[i].chip = data;
  539. data->pll[i].hw.init = &init;
  540. data->pll[i].index = i;
  541. err = devm_clk_hw_register(&client->dev, &data->pll[i].hw);
  542. if (err) {
  543. dev_err(&client->dev, "Failed register PLL %d\n", i);
  544. goto error;
  545. }
  546. sprintf(child_name, "PLL%d", i+1);
  547. np_output = of_get_child_by_name(node, child_name);
  548. if (!np_output)
  549. continue;
  550. if (!of_property_read_u32(np_output,
  551. "clock-frequency", &value)) {
  552. err = clk_set_rate(data->pll[i].hw.clk, value);
  553. if (err)
  554. dev_err(&client->dev,
  555. "unable to set PLL frequency %ud\n",
  556. value);
  557. }
  558. if (!of_property_read_u32(np_output,
  559. "spread-spectrum", &value)) {
  560. u8 flag = of_property_read_bool(np_output,
  561. "spread-spectrum-center") ? 0x80 : 0x00;
  562. regmap_update_bits(data->regmap,
  563. 0x16 + (i*CDCE925_OFFSET_PLL),
  564. 0x80, flag);
  565. regmap_update_bits(data->regmap,
  566. 0x12 + (i*CDCE925_OFFSET_PLL),
  567. 0x07, value & 0x07);
  568. }
  569. }
  570. /* Register output clock Y1 */
  571. init.ops = &cdce925_clk_y1_ops;
  572. init.flags = 0;
  573. init.num_parents = 1;
  574. init.parent_names = &parent_name; /* Mux Y1 to input */
  575. init.name = kasprintf(GFP_KERNEL, "%s.Y1", client->dev.of_node->name);
  576. data->clk[0].chip = data;
  577. data->clk[0].hw.init = &init;
  578. data->clk[0].index = 0;
  579. data->clk[0].pdiv = 1;
  580. err = devm_clk_hw_register(&client->dev, &data->clk[0].hw);
  581. kfree(init.name); /* clock framework made a copy of the name */
  582. if (err) {
  583. dev_err(&client->dev, "clock registration Y1 failed\n");
  584. goto error;
  585. }
  586. /* Register output clocks Y2 .. Y5*/
  587. init.ops = &cdce925_clk_ops;
  588. init.flags = CLK_SET_RATE_PARENT;
  589. init.num_parents = 1;
  590. for (i = 1; i < NUMBER_OF_OUTPUTS; ++i) {
  591. init.name = kasprintf(GFP_KERNEL, "%s.Y%d",
  592. client->dev.of_node->name, i+1);
  593. data->clk[i].chip = data;
  594. data->clk[i].hw.init = &init;
  595. data->clk[i].index = i;
  596. data->clk[i].pdiv = 1;
  597. switch (i) {
  598. case 1:
  599. case 2:
  600. /* Mux Y2/3 to PLL1 */
  601. init.parent_names = &pll_clk_name[0];
  602. break;
  603. case 3:
  604. case 4:
  605. /* Mux Y4/5 to PLL2 */
  606. init.parent_names = &pll_clk_name[1];
  607. break;
  608. }
  609. err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
  610. kfree(init.name); /* clock framework made a copy of the name */
  611. if (err) {
  612. dev_err(&client->dev, "clock registration failed\n");
  613. goto error;
  614. }
  615. }
  616. /* Register the output clocks */
  617. err = of_clk_add_hw_provider(client->dev.of_node, of_clk_cdce925_get,
  618. data);
  619. if (err)
  620. dev_err(&client->dev, "unable to add OF clock provider\n");
  621. err = 0;
  622. error:
  623. for (i = 0; i < NUMBER_OF_PLLS; ++i)
  624. /* clock framework made a copy of the name */
  625. kfree(pll_clk_name[i]);
  626. return err;
  627. }
  628. static const struct i2c_device_id cdce925_id[] = {
  629. { "cdce925", 0 },
  630. { }
  631. };
  632. MODULE_DEVICE_TABLE(i2c, cdce925_id);
  633. static const struct of_device_id clk_cdce925_of_match[] = {
  634. { .compatible = "ti,cdce925" },
  635. { },
  636. };
  637. MODULE_DEVICE_TABLE(of, clk_cdce925_of_match);
  638. static struct i2c_driver cdce925_driver = {
  639. .driver = {
  640. .name = "cdce925",
  641. .of_match_table = of_match_ptr(clk_cdce925_of_match),
  642. },
  643. .probe = cdce925_probe,
  644. .id_table = cdce925_id,
  645. };
  646. module_i2c_driver(cdce925_driver);
  647. MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
  648. MODULE_DESCRIPTION("cdce925 driver");
  649. MODULE_LICENSE("GPL");