clk-cdce925.c 21 KB

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