clk-cdce925.c 20 KB

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