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.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/i2c.h>
  26. #include <linux/regmap.h>
  27. #include <linux/slab.h>
  28. /* Si570 registers */
  29. #define SI570_REG_HS_N1 7
  30. #define SI570_REG_N1_RFREQ0 8
  31. #define SI570_REG_RFREQ1 9
  32. #define SI570_REG_RFREQ2 10
  33. #define SI570_REG_RFREQ3 11
  34. #define SI570_REG_RFREQ4 12
  35. #define SI570_REG_CONTROL 135
  36. #define SI570_REG_FREEZE_DCO 137
  37. #define SI570_DIV_OFFSET_7PPM 6
  38. #define HS_DIV_SHIFT 5
  39. #define HS_DIV_MASK 0xe0
  40. #define HS_DIV_OFFSET 4
  41. #define N1_6_2_MASK 0x1f
  42. #define N1_1_0_MASK 0xc0
  43. #define RFREQ_37_32_MASK 0x3f
  44. #define SI570_MIN_FREQ 10000000L
  45. #define SI570_MAX_FREQ 1417500000L
  46. #define SI598_MAX_FREQ 525000000L
  47. #define FDCO_MIN 4850000000LL
  48. #define FDCO_MAX 5670000000LL
  49. #define SI570_CNTRL_RECALL (1 << 0)
  50. #define SI570_CNTRL_FREEZE_M (1 << 5)
  51. #define SI570_CNTRL_NEWFREQ (1 << 6)
  52. #define SI570_FREEZE_DCO (1 << 4)
  53. /**
  54. * struct clk_si570:
  55. * @hw: Clock hw struct
  56. * @regmap: Device's regmap
  57. * @div_offset: Rgister offset for dividers
  58. * @max_freq: Maximum frequency for this device
  59. * @fxtal: Factory xtal frequency
  60. * @n1: Clock divider N1
  61. * @hs_div: Clock divider HSDIV
  62. * @rfreq: Clock multiplier RFREQ
  63. * @frequency: Current output frequency
  64. * @i2c_client: I2C client pointer
  65. */
  66. struct clk_si570 {
  67. struct clk_hw hw;
  68. struct regmap *regmap;
  69. unsigned int div_offset;
  70. u64 max_freq;
  71. u64 fxtal;
  72. unsigned int n1;
  73. unsigned int hs_div;
  74. u64 rfreq;
  75. u64 frequency;
  76. struct i2c_client *i2c_client;
  77. };
  78. #define to_clk_si570(_hw) container_of(_hw, struct clk_si570, hw)
  79. enum clk_si570_variant {
  80. si57x,
  81. si59x
  82. };
  83. /**
  84. * si570_get_divs() - Read clock dividers from HW
  85. * @data: Pointer to struct clk_si570
  86. * @rfreq: Fractional multiplier (output)
  87. * @n1: Divider N1 (output)
  88. * @hs_div: Divider HSDIV (output)
  89. * Returns 0 on success, negative errno otherwise.
  90. *
  91. * Retrieve clock dividers and multipliers from the HW.
  92. */
  93. static int si570_get_divs(struct clk_si570 *data, u64 *rfreq,
  94. unsigned int *n1, unsigned int *hs_div)
  95. {
  96. int err;
  97. u8 reg[6];
  98. u64 tmp;
  99. err = regmap_bulk_read(data->regmap, SI570_REG_HS_N1 + data->div_offset,
  100. reg, ARRAY_SIZE(reg));
  101. if (err)
  102. return err;
  103. *hs_div = ((reg[0] & HS_DIV_MASK) >> HS_DIV_SHIFT) + HS_DIV_OFFSET;
  104. *n1 = ((reg[0] & N1_6_2_MASK) << 2) + ((reg[1] & N1_1_0_MASK) >> 6) + 1;
  105. /* Handle invalid cases */
  106. if (*n1 > 1)
  107. *n1 &= ~1;
  108. tmp = reg[1] & RFREQ_37_32_MASK;
  109. tmp = (tmp << 8) + reg[2];
  110. tmp = (tmp << 8) + reg[3];
  111. tmp = (tmp << 8) + reg[4];
  112. tmp = (tmp << 8) + reg[5];
  113. *rfreq = tmp;
  114. return 0;
  115. }
  116. /**
  117. * si570_get_defaults() - Get default values
  118. * @data: Driver data structure
  119. * @fout: Factory frequency output
  120. * Returns 0 on success, negative errno otherwise.
  121. */
  122. static int si570_get_defaults(struct clk_si570 *data, u64 fout)
  123. {
  124. int err;
  125. u64 fdco;
  126. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_RECALL);
  127. err = si570_get_divs(data, &data->rfreq, &data->n1, &data->hs_div);
  128. if (err)
  129. return err;
  130. /*
  131. * Accept optional precision loss to avoid arithmetic overflows.
  132. * Acceptable per Silicon Labs Application Note AN334.
  133. */
  134. fdco = fout * data->n1 * data->hs_div;
  135. if (fdco >= (1LL << 36))
  136. data->fxtal = div64_u64(fdco << 24, data->rfreq >> 4);
  137. else
  138. data->fxtal = div64_u64(fdco << 28, data->rfreq);
  139. data->frequency = fout;
  140. return 0;
  141. }
  142. /**
  143. * si570_update_rfreq() - Update clock multiplier
  144. * @data: Driver data structure
  145. * Passes on regmap_bulk_write() return value.
  146. */
  147. static int si570_update_rfreq(struct clk_si570 *data)
  148. {
  149. u8 reg[5];
  150. reg[0] = ((data->n1 - 1) << 6) |
  151. ((data->rfreq >> 32) & RFREQ_37_32_MASK);
  152. reg[1] = (data->rfreq >> 24) & 0xff;
  153. reg[2] = (data->rfreq >> 16) & 0xff;
  154. reg[3] = (data->rfreq >> 8) & 0xff;
  155. reg[4] = data->rfreq & 0xff;
  156. return regmap_bulk_write(data->regmap, SI570_REG_N1_RFREQ0 +
  157. data->div_offset, reg, ARRAY_SIZE(reg));
  158. }
  159. /**
  160. * si570_calc_divs() - Caluclate clock dividers
  161. * @frequency: Target frequency
  162. * @data: Driver data structure
  163. * @out_rfreq: RFREG fractional multiplier (output)
  164. * @out_n1: Clock divider N1 (output)
  165. * @out_hs_div: Clock divider HSDIV (output)
  166. * Returns 0 on success, negative errno otherwise.
  167. *
  168. * Calculate the clock dividers (@out_hs_div, @out_n1) and clock multiplier
  169. * (@out_rfreq) for a given target @frequency.
  170. */
  171. static int si570_calc_divs(unsigned long frequency, struct clk_si570 *data,
  172. u64 *out_rfreq, unsigned int *out_n1, unsigned int *out_hs_div)
  173. {
  174. int i;
  175. unsigned int n1, hs_div;
  176. u64 fdco, best_fdco = ULLONG_MAX;
  177. static const uint8_t si570_hs_div_values[] = { 11, 9, 7, 6, 5, 4 };
  178. for (i = 0; i < ARRAY_SIZE(si570_hs_div_values); i++) {
  179. hs_div = si570_hs_div_values[i];
  180. /* Calculate lowest possible value for n1 */
  181. n1 = div_u64(div_u64(FDCO_MIN, hs_div), frequency);
  182. if (!n1 || (n1 & 1))
  183. n1++;
  184. while (n1 <= 128) {
  185. fdco = (u64)frequency * (u64)hs_div * (u64)n1;
  186. if (fdco > FDCO_MAX)
  187. break;
  188. if (fdco >= FDCO_MIN && fdco < best_fdco) {
  189. *out_n1 = n1;
  190. *out_hs_div = hs_div;
  191. *out_rfreq = div64_u64(fdco << 28, data->fxtal);
  192. best_fdco = fdco;
  193. }
  194. n1 += (n1 == 1 ? 1 : 2);
  195. }
  196. }
  197. if (best_fdco == ULLONG_MAX)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. static unsigned long si570_recalc_rate(struct clk_hw *hw,
  202. unsigned long parent_rate)
  203. {
  204. int err;
  205. u64 rfreq, rate;
  206. unsigned int n1, hs_div;
  207. struct clk_si570 *data = to_clk_si570(hw);
  208. err = si570_get_divs(data, &rfreq, &n1, &hs_div);
  209. if (err) {
  210. dev_err(&data->i2c_client->dev, "unable to recalc rate\n");
  211. return data->frequency;
  212. }
  213. rfreq = div_u64(rfreq, hs_div * n1);
  214. rate = (data->fxtal * rfreq) >> 28;
  215. return rate;
  216. }
  217. static long si570_round_rate(struct clk_hw *hw, unsigned long rate,
  218. unsigned long *parent_rate)
  219. {
  220. int err;
  221. u64 rfreq;
  222. unsigned int n1, hs_div;
  223. struct clk_si570 *data = to_clk_si570(hw);
  224. if (!rate)
  225. return 0;
  226. if (div64_u64(abs(rate - data->frequency) * 10000LL,
  227. data->frequency) < 35) {
  228. rfreq = div64_u64((data->rfreq * rate) +
  229. div64_u64(data->frequency, 2), data->frequency);
  230. n1 = data->n1;
  231. hs_div = data->hs_div;
  232. } else {
  233. err = si570_calc_divs(rate, data, &rfreq, &n1, &hs_div);
  234. if (err) {
  235. dev_err(&data->i2c_client->dev,
  236. "unable to round rate\n");
  237. return 0;
  238. }
  239. }
  240. return rate;
  241. }
  242. /**
  243. * si570_set_frequency() - Adjust output frequency
  244. * @data: Driver data structure
  245. * @frequency: Target frequency
  246. * Returns 0 on success.
  247. *
  248. * Update output frequency for big frequency changes (> 3,500 ppm).
  249. */
  250. static int si570_set_frequency(struct clk_si570 *data, unsigned long frequency)
  251. {
  252. int err;
  253. err = si570_calc_divs(frequency, data, &data->rfreq, &data->n1,
  254. &data->hs_div);
  255. if (err)
  256. return err;
  257. /*
  258. * The DCO reg should be accessed with a read-modify-write operation
  259. * per AN334
  260. */
  261. regmap_write(data->regmap, SI570_REG_FREEZE_DCO, SI570_FREEZE_DCO);
  262. regmap_write(data->regmap, SI570_REG_HS_N1 + data->div_offset,
  263. ((data->hs_div - HS_DIV_OFFSET) << HS_DIV_SHIFT) |
  264. (((data->n1 - 1) >> 2) & N1_6_2_MASK));
  265. si570_update_rfreq(data);
  266. regmap_write(data->regmap, SI570_REG_FREEZE_DCO, 0);
  267. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_NEWFREQ);
  268. /* Applying a new frequency can take up to 10ms */
  269. usleep_range(10000, 12000);
  270. return 0;
  271. }
  272. /**
  273. * si570_set_frequency_small() - Adjust output frequency
  274. * @data: Driver data structure
  275. * @frequency: Target frequency
  276. * Returns 0 on success.
  277. *
  278. * Update output frequency for small frequency changes (< 3,500 ppm).
  279. */
  280. static int si570_set_frequency_small(struct clk_si570 *data,
  281. unsigned long frequency)
  282. {
  283. /*
  284. * This is a re-implementation of DIV_ROUND_CLOSEST
  285. * using the div64_u64 function lieu of letting the compiler
  286. * insert EABI calls
  287. */
  288. data->rfreq = div64_u64((data->rfreq * frequency) +
  289. div_u64(data->frequency, 2), data->frequency);
  290. regmap_write(data->regmap, SI570_REG_CONTROL, SI570_CNTRL_FREEZE_M);
  291. si570_update_rfreq(data);
  292. regmap_write(data->regmap, SI570_REG_CONTROL, 0);
  293. /* Applying a new frequency (small change) can take up to 100us */
  294. usleep_range(100, 200);
  295. return 0;
  296. }
  297. static int si570_set_rate(struct clk_hw *hw, unsigned long rate,
  298. unsigned long parent_rate)
  299. {
  300. struct clk_si570 *data = to_clk_si570(hw);
  301. struct i2c_client *client = data->i2c_client;
  302. int err;
  303. if (rate < SI570_MIN_FREQ || rate > data->max_freq) {
  304. dev_err(&client->dev,
  305. "requested frequency %lu Hz is out of range\n", rate);
  306. return -EINVAL;
  307. }
  308. if (div64_u64(abs(rate - data->frequency) * 10000LL,
  309. data->frequency) < 35)
  310. err = si570_set_frequency_small(data, rate);
  311. else
  312. err = si570_set_frequency(data, rate);
  313. if (err)
  314. return err;
  315. data->frequency = rate;
  316. return 0;
  317. }
  318. static const struct clk_ops si570_clk_ops = {
  319. .recalc_rate = si570_recalc_rate,
  320. .round_rate = si570_round_rate,
  321. .set_rate = si570_set_rate,
  322. };
  323. static bool si570_regmap_is_volatile(struct device *dev, unsigned int reg)
  324. {
  325. switch (reg) {
  326. case SI570_REG_CONTROL:
  327. return true;
  328. default:
  329. return false;
  330. }
  331. }
  332. static bool si570_regmap_is_writeable(struct device *dev, unsigned int reg)
  333. {
  334. switch (reg) {
  335. case SI570_REG_HS_N1 ... (SI570_REG_RFREQ4 + SI570_DIV_OFFSET_7PPM):
  336. case SI570_REG_CONTROL:
  337. case SI570_REG_FREEZE_DCO:
  338. return true;
  339. default:
  340. return false;
  341. }
  342. }
  343. static const struct regmap_config si570_regmap_config = {
  344. .reg_bits = 8,
  345. .val_bits = 8,
  346. .cache_type = REGCACHE_RBTREE,
  347. .max_register = 137,
  348. .writeable_reg = si570_regmap_is_writeable,
  349. .volatile_reg = si570_regmap_is_volatile,
  350. };
  351. static int si570_probe(struct i2c_client *client,
  352. const struct i2c_device_id *id)
  353. {
  354. struct clk_si570 *data;
  355. struct clk_init_data init;
  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 = 0;
  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. err = devm_clk_hw_register(&client->dev, &data->hw);
  401. if (err) {
  402. dev_err(&client->dev, "clock registration failed\n");
  403. return err;
  404. }
  405. err = of_clk_add_hw_provider(client->dev.of_node, of_clk_hw_simple_get,
  406. &data->hw);
  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(data->hw.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");