clk-si514.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Driver for Silicon Labs Si514 Programmable Oscillator
  3. *
  4. * Copyright (C) 2015 Topic Embedded Products
  5. *
  6. * Author: Mike Looijmans <mike.looijmans@topic.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/clk-provider.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. /* I2C registers */
  25. #define SI514_REG_LP 0
  26. #define SI514_REG_M_FRAC1 5
  27. #define SI514_REG_M_FRAC2 6
  28. #define SI514_REG_M_FRAC3 7
  29. #define SI514_REG_M_INT_FRAC 8
  30. #define SI514_REG_M_INT 9
  31. #define SI514_REG_HS_DIV 10
  32. #define SI514_REG_LS_HS_DIV 11
  33. #define SI514_REG_OE_STATE 14
  34. #define SI514_REG_RESET 128
  35. #define SI514_REG_CONTROL 132
  36. /* Register values */
  37. #define SI514_RESET_RST BIT(7)
  38. #define SI514_CONTROL_FCAL BIT(0)
  39. #define SI514_CONTROL_OE BIT(2)
  40. #define SI514_MIN_FREQ 100000U
  41. #define SI514_MAX_FREQ 250000000U
  42. #define FXO 31980000U
  43. #define FVCO_MIN 2080000000U
  44. #define FVCO_MAX 2500000000U
  45. #define HS_DIV_MAX 1022
  46. struct clk_si514 {
  47. struct clk_hw hw;
  48. struct regmap *regmap;
  49. struct i2c_client *i2c_client;
  50. };
  51. #define to_clk_si514(_hw) container_of(_hw, struct clk_si514, hw)
  52. /* Multiplier/divider settings */
  53. struct clk_si514_muldiv {
  54. u32 m_frac; /* 29-bit Fractional part of multiplier M */
  55. u8 m_int; /* Integer part of multiplier M, 65..78 */
  56. u8 ls_div_bits; /* 2nd divider, as 2^x */
  57. u16 hs_div; /* 1st divider, must be even and 10<=x<=1022 */
  58. };
  59. /* Enables or disables the output driver */
  60. static int si514_enable_output(struct clk_si514 *data, bool enable)
  61. {
  62. return regmap_update_bits(data->regmap, SI514_REG_CONTROL,
  63. SI514_CONTROL_OE, enable ? SI514_CONTROL_OE : 0);
  64. }
  65. /* Retrieve clock multiplier and dividers from hardware */
  66. static int si514_get_muldiv(struct clk_si514 *data,
  67. struct clk_si514_muldiv *settings)
  68. {
  69. int err;
  70. u8 reg[7];
  71. err = regmap_bulk_read(data->regmap, SI514_REG_M_FRAC1,
  72. reg, ARRAY_SIZE(reg));
  73. if (err)
  74. return err;
  75. settings->m_frac = reg[0] | reg[1] << 8 | reg[2] << 16 |
  76. (reg[3] & 0x1F) << 24;
  77. settings->m_int = (reg[4] & 0x3f) << 3 | reg[3] >> 5;
  78. settings->ls_div_bits = (reg[6] >> 4) & 0x07;
  79. settings->hs_div = (reg[6] & 0x03) << 8 | reg[5];
  80. return 0;
  81. }
  82. static int si514_set_muldiv(struct clk_si514 *data,
  83. struct clk_si514_muldiv *settings)
  84. {
  85. u8 lp;
  86. u8 reg[7];
  87. int err;
  88. /* Calculate LP1/LP2 according to table 13 in the datasheet */
  89. /* 65.259980246 */
  90. if (settings->m_int < 65 ||
  91. (settings->m_int == 65 && settings->m_frac <= 139575831))
  92. lp = 0x22;
  93. /* 67.859763463 */
  94. else if (settings->m_int < 67 ||
  95. (settings->m_int == 67 && settings->m_frac <= 461581994))
  96. lp = 0x23;
  97. /* 72.937624981 */
  98. else if (settings->m_int < 72 ||
  99. (settings->m_int == 72 && settings->m_frac <= 503383578))
  100. lp = 0x33;
  101. /* 75.843265046 */
  102. else if (settings->m_int < 75 ||
  103. (settings->m_int == 75 && settings->m_frac <= 452724474))
  104. lp = 0x34;
  105. else
  106. lp = 0x44;
  107. err = regmap_write(data->regmap, SI514_REG_LP, lp);
  108. if (err < 0)
  109. return err;
  110. reg[0] = settings->m_frac;
  111. reg[1] = settings->m_frac >> 8;
  112. reg[2] = settings->m_frac >> 16;
  113. reg[3] = settings->m_frac >> 24 | settings->m_int << 5;
  114. reg[4] = settings->m_int >> 3;
  115. reg[5] = settings->hs_div;
  116. reg[6] = (settings->hs_div >> 8) | (settings->ls_div_bits << 4);
  117. err = regmap_bulk_write(data->regmap, SI514_REG_HS_DIV, reg + 5, 2);
  118. if (err < 0)
  119. return err;
  120. /*
  121. * Writing to SI514_REG_M_INT_FRAC triggers the clock change, so that
  122. * must be written last
  123. */
  124. return regmap_bulk_write(data->regmap, SI514_REG_M_FRAC1, reg, 5);
  125. }
  126. /* Calculate divider settings for a given frequency */
  127. static int si514_calc_muldiv(struct clk_si514_muldiv *settings,
  128. unsigned long frequency)
  129. {
  130. u64 m;
  131. u32 ls_freq;
  132. u32 tmp;
  133. u8 res;
  134. if ((frequency < SI514_MIN_FREQ) || (frequency > SI514_MAX_FREQ))
  135. return -EINVAL;
  136. /* Determine the minimum value of LS_DIV and resulting target freq. */
  137. ls_freq = frequency;
  138. if (frequency >= (FVCO_MIN / HS_DIV_MAX))
  139. settings->ls_div_bits = 0;
  140. else {
  141. res = 1;
  142. tmp = 2 * HS_DIV_MAX;
  143. while (tmp <= (HS_DIV_MAX * 32)) {
  144. if ((frequency * tmp) >= FVCO_MIN)
  145. break;
  146. ++res;
  147. tmp <<= 1;
  148. }
  149. settings->ls_div_bits = res;
  150. ls_freq = frequency << res;
  151. }
  152. /* Determine minimum HS_DIV, round up to even number */
  153. settings->hs_div = DIV_ROUND_UP(FVCO_MIN >> 1, ls_freq) << 1;
  154. /* M = LS_DIV x HS_DIV x frequency / F_XO (in fixed-point) */
  155. m = ((u64)(ls_freq * settings->hs_div) << 29) + (FXO / 2);
  156. do_div(m, FXO);
  157. settings->m_frac = (u32)m & (BIT(29) - 1);
  158. settings->m_int = (u32)(m >> 29);
  159. return 0;
  160. }
  161. /* Calculate resulting frequency given the register settings */
  162. static unsigned long si514_calc_rate(struct clk_si514_muldiv *settings)
  163. {
  164. u64 m = settings->m_frac | ((u64)settings->m_int << 29);
  165. u32 d = settings->hs_div * BIT(settings->ls_div_bits);
  166. return ((u32)(((m * FXO) + (FXO / 2)) >> 29)) / d;
  167. }
  168. static unsigned long si514_recalc_rate(struct clk_hw *hw,
  169. unsigned long parent_rate)
  170. {
  171. struct clk_si514 *data = to_clk_si514(hw);
  172. struct clk_si514_muldiv settings;
  173. int err;
  174. err = si514_get_muldiv(data, &settings);
  175. if (err) {
  176. dev_err(&data->i2c_client->dev, "unable to retrieve settings\n");
  177. return 0;
  178. }
  179. return si514_calc_rate(&settings);
  180. }
  181. static long si514_round_rate(struct clk_hw *hw, unsigned long rate,
  182. unsigned long *parent_rate)
  183. {
  184. struct clk_si514_muldiv settings;
  185. int err;
  186. if (!rate)
  187. return 0;
  188. err = si514_calc_muldiv(&settings, rate);
  189. if (err)
  190. return err;
  191. return si514_calc_rate(&settings);
  192. }
  193. /*
  194. * Update output frequency for big frequency changes (> 1000 ppm).
  195. * The chip supports <1000ppm changes "on the fly", we haven't implemented
  196. * that here.
  197. */
  198. static int si514_set_rate(struct clk_hw *hw, unsigned long rate,
  199. unsigned long parent_rate)
  200. {
  201. struct clk_si514 *data = to_clk_si514(hw);
  202. struct clk_si514_muldiv settings;
  203. int err;
  204. err = si514_calc_muldiv(&settings, rate);
  205. if (err)
  206. return err;
  207. si514_enable_output(data, false);
  208. err = si514_set_muldiv(data, &settings);
  209. if (err < 0)
  210. return err; /* Undefined state now, best to leave disabled */
  211. /* Trigger calibration */
  212. err = regmap_write(data->regmap, SI514_REG_CONTROL, SI514_CONTROL_FCAL);
  213. if (err < 0)
  214. return err;
  215. /* Applying a new frequency can take up to 10ms */
  216. usleep_range(10000, 12000);
  217. si514_enable_output(data, true);
  218. return err;
  219. }
  220. static const struct clk_ops si514_clk_ops = {
  221. .recalc_rate = si514_recalc_rate,
  222. .round_rate = si514_round_rate,
  223. .set_rate = si514_set_rate,
  224. };
  225. static bool si514_regmap_is_volatile(struct device *dev, unsigned int reg)
  226. {
  227. switch (reg) {
  228. case SI514_REG_CONTROL:
  229. case SI514_REG_RESET:
  230. return true;
  231. default:
  232. return false;
  233. }
  234. }
  235. static bool si514_regmap_is_writeable(struct device *dev, unsigned int reg)
  236. {
  237. switch (reg) {
  238. case SI514_REG_LP:
  239. case SI514_REG_M_FRAC1 ... SI514_REG_LS_HS_DIV:
  240. case SI514_REG_OE_STATE:
  241. case SI514_REG_RESET:
  242. case SI514_REG_CONTROL:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. static const struct regmap_config si514_regmap_config = {
  249. .reg_bits = 8,
  250. .val_bits = 8,
  251. .cache_type = REGCACHE_RBTREE,
  252. .max_register = SI514_REG_CONTROL,
  253. .writeable_reg = si514_regmap_is_writeable,
  254. .volatile_reg = si514_regmap_is_volatile,
  255. };
  256. static int si514_probe(struct i2c_client *client,
  257. const struct i2c_device_id *id)
  258. {
  259. struct clk_si514 *data;
  260. struct clk_init_data init;
  261. int err;
  262. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  263. if (!data)
  264. return -ENOMEM;
  265. init.ops = &si514_clk_ops;
  266. init.flags = 0;
  267. init.num_parents = 0;
  268. data->hw.init = &init;
  269. data->i2c_client = client;
  270. if (of_property_read_string(client->dev.of_node, "clock-output-names",
  271. &init.name))
  272. init.name = client->dev.of_node->name;
  273. data->regmap = devm_regmap_init_i2c(client, &si514_regmap_config);
  274. if (IS_ERR(data->regmap)) {
  275. dev_err(&client->dev, "failed to allocate register map\n");
  276. return PTR_ERR(data->regmap);
  277. }
  278. i2c_set_clientdata(client, data);
  279. err = devm_clk_hw_register(&client->dev, &data->hw);
  280. if (err) {
  281. dev_err(&client->dev, "clock registration failed\n");
  282. return err;
  283. }
  284. err = of_clk_add_hw_provider(client->dev.of_node, of_clk_hw_simple_get,
  285. &data->hw);
  286. if (err) {
  287. dev_err(&client->dev, "unable to add clk provider\n");
  288. return err;
  289. }
  290. return 0;
  291. }
  292. static int si514_remove(struct i2c_client *client)
  293. {
  294. of_clk_del_provider(client->dev.of_node);
  295. return 0;
  296. }
  297. static const struct i2c_device_id si514_id[] = {
  298. { "si514", 0 },
  299. { }
  300. };
  301. MODULE_DEVICE_TABLE(i2c, si514_id);
  302. static const struct of_device_id clk_si514_of_match[] = {
  303. { .compatible = "silabs,si514" },
  304. { },
  305. };
  306. MODULE_DEVICE_TABLE(of, clk_si514_of_match);
  307. static struct i2c_driver si514_driver = {
  308. .driver = {
  309. .name = "si514",
  310. .of_match_table = clk_si514_of_match,
  311. },
  312. .probe = si514_probe,
  313. .remove = si514_remove,
  314. .id_table = si514_id,
  315. };
  316. module_i2c_driver(si514_driver);
  317. MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
  318. MODULE_DESCRIPTION("Si514 driver");
  319. MODULE_LICENSE("GPL");