clk-cdce925.c 21 KB

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