clk-si570.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Driver for Silicon Labs Si570/Si571 Programmable XO/VCXO
  3. *
  4. * Copyright (C) 2010, 2011 Ericsson AB.
  5. * Copyright (C) 2011 Guenter Roeck.
  6. * Copyright (C) 2011 - 2013 Xilinx Inc.
  7. *
  8. * Author: Guenter Roeck <guenter.roeck@ericsson.com>
  9. * Sören Brinkmann <soren.brinkmann@xilinx.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. */
  21. #include <linux/clk-provider.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. /* Si570 registers */
  28. #define SI570_REG_HS_N1 7
  29. #define SI570_REG_N1_RFREQ0 8
  30. #define SI570_REG_RFREQ1 9
  31. #define SI570_REG_RFREQ2 10
  32. #define SI570_REG_RFREQ3 11
  33. #define SI570_REG_RFREQ4 12
  34. #define SI570_REG_CONTROL 135
  35. #define SI570_REG_FREEZE_DCO 137
  36. #define SI570_DIV_OFFSET_7PPM 6
  37. #define HS_DIV_SHIFT 5
  38. #define HS_DIV_MASK 0xe0
  39. #define HS_DIV_OFFSET 4
  40. #define N1_6_2_MASK 0x1f
  41. #define N1_1_0_MASK 0xc0
  42. #define RFREQ_37_32_MASK 0x3f
  43. #define SI570_MIN_FREQ 10000000L
  44. #define SI570_MAX_FREQ 1417500000L
  45. #define SI598_MAX_FREQ 525000000L
  46. #define FDCO_MIN 4850000000LL
  47. #define FDCO_MAX 5670000000LL
  48. #define SI570_CNTRL_RECALL (1 << 0)
  49. #define SI570_CNTRL_FREEZE_M (1 << 5)
  50. #define SI570_CNTRL_NEWFREQ (1 << 6)
  51. #define SI570_FREEZE_DCO (1 << 4)
  52. /**
  53. * struct clk_si570:
  54. * @hw: Clock hw struct
  55. * @regmap: Device's regmap
  56. * @div_offset: Rgister offset for dividers
  57. * @max_freq: Maximum frequency for this device
  58. * @fxtal: Factory xtal frequency
  59. * @n1: Clock divider N1
  60. * @hs_div: Clock divider HSDIV
  61. * @rfreq: Clock multiplier RFREQ
  62. * @frequency: Current output frequency
  63. * @i2c_client: I2C client pointer
  64. */
  65. struct clk_si570 {
  66. struct clk_hw hw;
  67. struct regmap *regmap;
  68. unsigned int div_offset;
  69. u64 max_freq;
  70. u64 fxtal;
  71. unsigned int n1;
  72. unsigned int hs_div;
  73. u64 rfreq;
  74. u64 frequency;
  75. struct i2c_client *i2c_client;
  76. };
  77. #define to_clk_si570(_hw) container_of(_hw, struct clk_si570, hw)
  78. enum clk_si570_variant {
  79. si57x,
  80. si59x
  81. };
  82. /**
  83. * si570_get_divs() - Read clock dividers from HW
  84. * @data: Pointer to struct clk_si570
  85. * @rfreq: Fractional multiplier (output)
  86. * @n1: Divider N1 (output)
  87. * @hs_div: Divider HSDIV (output)
  88. * Returns 0 on success, negative errno otherwise.
  89. *
  90. * Retrieve clock dividers and multipliers from the HW.
  91. */
  92. static int si570_get_divs(struct clk_si570 *data, u64 *rfreq,
  93. unsigned int *n1, unsigned int *hs_div)
  94. {
  95. int err;
  96. u8 reg[6];
  97. u64 tmp;
  98. err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset,
  99. reg, ARRAY_SIZE(reg));
  100. if (err)
  101. return err;
  102. *hs_div = ((reg[0] & HS_DIV_MASK) >> HS_DIV_SHIFT) + HS_DIV_OFFSET;
  103. *n1 = ((reg[0] & N1_6_2_MASK) << 2) + ((reg[1] & N1_1_0_MASK) >> 6) + 1;
  104. /* Handle invalid cases */
  105. if (*n1 > 1)
  106. *n1 &= ~1;
  107. tmp = reg[1] & RFREQ_37_32_MASK;
  108. tmp = (tmp << 8) + reg[2];
  109. tmp = (tmp << 8) + reg[3];
  110. tmp = (tmp << 8) + reg[4];
  111. tmp = (tmp << 8) + reg[5];
  112. *rfreq = tmp;
  113. return 0;
  114. }
  115. /**
  116. * si570_get_defaults() - Get default values
  117. * @data: Driver data structure
  118. * @fout: Factory frequency output
  119. * Returns 0 on success, negative errno otherwise.
  120. */
  121. static int si570_get_defaults(struct clk_si570 *data, u64 fout)
  122. {
  123. int err;
  124. u64 fdco;
  125. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_RECALL);
  126. err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div);
  127. if (err)
  128. return err;
  129. /*
  130. * Accept optional precision loss to avoid arithmetic overflows.
  131. * Acceptable per Silicon Labs Application Note AN334.
  132. */
  133. fdco = fout * data->n1 * data->hs_div;
  134. if (fdco >= (1LL << 36))
  135. data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4);
  136. else
  137. data->fxtal = div64_u64(fdco << 28, data->rfreq);
  138. data->frequency = fout;
  139. return 0;
  140. }
  141. /**
  142. * si570_update_rfreq() - Update clock multiplier
  143. * @data: Driver data structure
  144. * Passes on regmap_bulk_write() return value.
  145. */
  146. static int si570_update_rfreq(struct clk_si570 *data)
  147. {
  148. u8 reg[5];
  149. reg[0] = ((data->n1 - 1) << 6) |
  150. ((data->rfreq >> 32) & RFREQ_37_32_MASK);
  151. reg[1] = (data->rfreq >> 24) & 0xff;
  152. reg[2] = (data->rfreq >> 16) & 0xff;
  153. reg[3] = (data->rfreq >> 8) & 0xff;
  154. reg[4] = data->rfreq & 0xff;
  155. return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 +
  156. data->div_offset, reg, ARRAY_SIZE(reg));
  157. }
  158. /**
  159. * si570_calc_divs() - Caluclate clock dividers
  160. * @frequency: Target frequency
  161. * @data: Driver data structure
  162. * @out_rfreq: RFREG fractional multiplier (output)
  163. * @out_n1: Clock divider N1 (output)
  164. * @out_hs_div: Clock divider HSDIV (output)
  165. * Returns 0 on success, negative errno otherwise.
  166. *
  167. * Calculate the clock dividers (@out_hs_div, @out_n1) and clock multiplier
  168. * (@out_rfreq) for a given target @frequency.
  169. */
  170. static int si570_calc_divs(unsigned long frequency, struct clk_si570 *data,
  171. u64 *out_rfreq, unsigned int *out_n1, unsigned int *out_hs_div)
  172. {
  173. int i;
  174. unsigned int n1, hs_div;
  175. u64 fdco, best_fdco = ULLONG_MAX;
  176. static const uint8_t si570_hs_div_values[] = { 11, 9, 7, 6, 5, 4 };
  177. for (i = 0; i < ARRAY_SIZE(si570_hs_div_values); i++) {
  178. hs_div = si570_hs_div_values[i];
  179. /* Calculate lowest possible value for n1 */
  180. n1 = div_u64(div_u64(FDCO_MIN, hs_div), frequency);
  181. if (!n1 || (n1 & 1))
  182. n1++;
  183. while (n1 <= 128) {
  184. fdco = (u64)frequency * (u64)hs_div * (u64)n1;
  185. if (fdco > FDCO_MAX)
  186. break;
  187. if (fdco >= FDCO_MIN && fdco < best_fdco) {
  188. *out_n1 = n1;
  189. *out_hs_div = hs_div;
  190. *out_rfreq = div64_u64(fdco << 28, data->fxtal);
  191. best_fdco = fdco;
  192. }
  193. n1 += (n1 == 1 ? 1 : 2);
  194. }
  195. }
  196. if (best_fdco == ULLONG_MAX)
  197. return -EINVAL;
  198. return 0;
  199. }
  200. static unsigned long si570_recalc_rate(struct clk_hw *hw,
  201. unsigned long parent_rate)
  202. {
  203. int err;
  204. u64 rfreq, rate;
  205. unsigned int n1, hs_div;
  206. struct clk_si570 *data = to_clk_si570(hw);
  207. err = si570_get_divs(data, &rfreq, &n1, &hs_div);
  208. if (err) {
  209. dev_err(&data->i2c_client->dev, "unable to recalc rate\n");
  210. return data->frequency;
  211. }
  212. rfreq = div_u64(rfreq, hs_div * n1);
  213. rate = (data->fxtal * rfreq) >> 28;
  214. return rate;
  215. }
  216. static long si570_round_rate(struct clk_hw *hw, unsigned long rate,
  217. unsigned long *parent_rate)
  218. {
  219. int err;
  220. u64 rfreq;
  221. unsigned int n1, hs_div;
  222. struct clk_si570 *data = to_clk_si570(hw);
  223. if (!rate)
  224. return 0;
  225. if (div64_u64(abs(rate - data->frequency) * 10000LL,
  226. data->frequency) < 35) {
  227. rfreq = div64_u64((data->rfreq * rate) +
  228. div64_u64(data->frequency, 2), data->frequency);
  229. n1 = data->n1;
  230. hs_div = data->hs_div;
  231. } else {
  232. err = si570_calc_divs(rate, data, &rfreq, &n1, &hs_div);
  233. if (err) {
  234. dev_err(&data->i2c_client->dev,
  235. "unable to round rate\n");
  236. return 0;
  237. }
  238. }
  239. return rate;
  240. }
  241. /**
  242. * si570_set_frequency() - Adjust output frequency
  243. * @data: Driver data structure
  244. * @frequency: Target frequency
  245. * Returns 0 on success.
  246. *
  247. * Update output frequency for big frequency changes (> 3,500 ppm).
  248. */
  249. static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency)
  250. {
  251. int err;
  252. err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1,
  253. &data->hs_div);
  254. if (err)
  255. return err;
  256. /*
  257. * The DCO reg should be accessed with a read-modify-write operation
  258. * per AN334
  259. */
  260. regmap_write(data->regmap, SI570_REG_FREEZE_DCO, SI570_FREEZE_DCO);
  261. regmap_write(data->regmap, SI570_REG_HS_N1 + data->div_offset,
  262. ((data->hs_div - HS_DIV_OFFSET) << HS_DIV_SHIFT) |
  263. (((data->n1 - 1) >> 2) & N1_6_2_MASK));
  264. si570_update_rfreq(data);
  265. regmap_write(data->regmap, SI570_REG_FREEZE_DCO, 0);
  266. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_NEWFREQ);
  267. /* Applying a new frequency can take up to 10ms */
  268. usleep_range(10000, 12000);
  269. return 0;
  270. }
  271. /**
  272. * si570_set_frequency_small() - Adjust output frequency
  273. * @data: Driver data structure
  274. * @frequency: Target frequency
  275. * Returns 0 on success.
  276. *
  277. * Update output frequency for small frequency changes (< 3,500 ppm).
  278. */
  279. static int si570_set_frequency_small(struct clk_si570 *data,
  280. unsigned long frequency)
  281. {
  282. /*
  283. * This is a re-implementation of DIV_ROUND_CLOSEST
  284. * using the div64_u64 function lieu of letting the compiler
  285. * insert EABI calls
  286. */
  287. data->rfreq = div64_u64((data->rfreq * frequency) +
  288. div_u64(data->frequency, 2), data->frequency);
  289. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_FREEZE_M);
  290. si570_update_rfreq(data);
  291. regmap_write(data->regmap, SI570_REG_CONTROL, 0);
  292. /* Applying a new frequency (small change) can take up to 100us */
  293. usleep_range(100, 200);
  294. return 0;
  295. }
  296. static int si570_set_rate(struct clk_hw *hw, unsigned long rate,
  297. unsigned long parent_rate)
  298. {
  299. struct clk_si570 *data = to_clk_si570(hw);
  300. struct i2c_client *client = data->i2c_client;
  301. int err;
  302. if (rate < SI570_MIN_FREQ || rate > data->max_freq) {
  303. dev_err(&client->dev,
  304. "requested frequency %lu Hz is out of range\n", rate);
  305. return -EINVAL;
  306. }
  307. if (div64_u64(abs(rate - data->frequency) * 10000LL,
  308. data->frequency) < 35)
  309. err = si570_set_frequency_small(data, rate);
  310. else
  311. err = si570_set_frequency(data, rate);
  312. if (err)
  313. return err;
  314. data->frequency = rate;
  315. return 0;
  316. }
  317. static const struct clk_ops si570_clk_ops = {
  318. .recalc_rate = si570_recalc_rate,
  319. .round_rate = si570_round_rate,
  320. .set_rate = si570_set_rate,
  321. };
  322. static bool si570_regmap_is_volatile(struct device *dev, unsigned int reg)
  323. {
  324. switch (reg) {
  325. case SI570_REG_CONTROL:
  326. return true;
  327. default:
  328. return false;
  329. }
  330. }
  331. static bool si570_regmap_is_writeable(struct device *dev, unsigned int reg)
  332. {
  333. switch (reg) {
  334. case SI570_REG_HS_N1 ... (SI570_REG_RFREQ4 + SI570_DIV_OFFSET_7PPM):
  335. case SI570_REG_CONTROL:
  336. case SI570_REG_FREEZE_DCO:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. static const struct regmap_config si570_regmap_config = {
  343. .reg_bits = 8,
  344. .val_bits = 8,
  345. .cache_type = REGCACHE_RBTREE,
  346. .max_register = 137,
  347. .writeable_reg = si570_regmap_is_writeable,
  348. .volatile_reg = si570_regmap_is_volatile,
  349. };
  350. static int si570_probe(struct i2c_client *client,
  351. const struct i2c_device_id *id)
  352. {
  353. struct clk_si570 *data;
  354. struct clk_init_data init;
  355. struct clk *clk;
  356. u32 initial_fout, factory_fout, stability;
  357. int err;
  358. enum clk_si570_variant variant = id->driver_data;
  359. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  360. if (!data)
  361. return -ENOMEM;
  362. init.ops = &si570_clk_ops;
  363. init.flags = CLK_IS_ROOT;
  364. init.num_parents = 0;
  365. data->hw.init = &init;
  366. data->i2c_client = client;
  367. if (variant == si57x) {
  368. err = of_property_read_u32(client->dev.of_node,
  369. "temperature-stability", &stability);
  370. if (err) {
  371. dev_err(&client->dev,
  372. "'temperature-stability' property missing\n");
  373. return err;
  374. }
  375. /* adjust register offsets for 7ppm devices */
  376. if (stability == 7)
  377. data->div_offset = SI570_DIV_OFFSET_7PPM;
  378. data->max_freq = SI570_MAX_FREQ;
  379. } else {
  380. data->max_freq = SI598_MAX_FREQ;
  381. }
  382. if (of_property_read_string(client->dev.of_node, "clock-output-names",
  383. &init.name))
  384. init.name = client->dev.of_node->name;
  385. err = of_property_read_u32(client->dev.of_node, "factory-fout",
  386. &factory_fout);
  387. if (err) {
  388. dev_err(&client->dev, "'factory-fout' property missing\n");
  389. return err;
  390. }
  391. data->regmap = devm_regmap_init_i2c(client, &si570_regmap_config);
  392. if (IS_ERR(data->regmap)) {
  393. dev_err(&client->dev, "failed to allocate register map\n");
  394. return PTR_ERR(data->regmap);
  395. }
  396. i2c_set_clientdata(client, data);
  397. err = si570_get_defaults(data, factory_fout);
  398. if (err)
  399. return err;
  400. clk = devm_clk_register(&client->dev, &data->hw);
  401. if (IS_ERR(clk)) {
  402. dev_err(&client->dev, "clock registration failed\n");
  403. return PTR_ERR(clk);
  404. }
  405. err = of_clk_add_provider(client->dev.of_node, of_clk_src_simple_get,
  406. clk);
  407. if (err) {
  408. dev_err(&client->dev, "unable to add clk provider\n");
  409. return err;
  410. }
  411. /* Read the requested initial output frequency from device tree */
  412. if (!of_property_read_u32(client->dev.of_node, "clock-frequency",
  413. &initial_fout)) {
  414. err = clk_set_rate(clk, initial_fout);
  415. if (err) {
  416. of_clk_del_provider(client->dev.of_node);
  417. return err;
  418. }
  419. }
  420. /* Display a message indicating that we've successfully registered */
  421. dev_info(&client->dev, "registered, current frequency %llu Hz\n",
  422. data->frequency);
  423. return 0;
  424. }
  425. static int si570_remove(struct i2c_client *client)
  426. {
  427. of_clk_del_provider(client->dev.of_node);
  428. return 0;
  429. }
  430. static const struct i2c_device_id si570_id[] = {
  431. { "si570", si57x },
  432. { "si571", si57x },
  433. { "si598", si59x },
  434. { "si599", si59x },
  435. { }
  436. };
  437. MODULE_DEVICE_TABLE(i2c, si570_id);
  438. static const struct of_device_id clk_si570_of_match[] = {
  439. { .compatible = "silabs,si570" },
  440. { .compatible = "silabs,si571" },
  441. { .compatible = "silabs,si598" },
  442. { .compatible = "silabs,si599" },
  443. { },
  444. };
  445. MODULE_DEVICE_TABLE(of, clk_si570_of_match);
  446. static struct i2c_driver si570_driver = {
  447. .driver = {
  448. .name = "si570",
  449. .of_match_table = clk_si570_of_match,
  450. },
  451. .probe = si570_probe,
  452. .remove = si570_remove,
  453. .id_table = si570_id,
  454. };
  455. module_i2c_driver(si570_driver);
  456. MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
  457. MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
  458. MODULE_DESCRIPTION("Si570 driver");
  459. MODULE_LICENSE("GPL");