clk-cdce925.c 19 KB

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