clk-si521xx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Skyworks Si521xx PCIe clock generator driver
  4. *
  5. * The following series can be supported:
  6. * - Si52144 - 4x DIFF
  7. * - Si52146 - 6x DIFF
  8. * - Si52147 - 9x DIFF
  9. * Currently tested:
  10. * - Si52144
  11. *
  12. * Copyright (C) 2022 Marek Vasut <marex@denx.de>
  13. */
  14. #include <linux/bitfield.h>
  15. #include <linux/bitrev.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/i2c.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/regmap.h>
  22. /* OE1 and OE2 register */
  23. #define SI521XX_REG_OE(n) (((n) & 0x1) + 1)
  24. #define SI521XX_REG_ID 0x3
  25. #define SI521XX_REG_ID_PROG GENMASK(7, 4)
  26. #define SI521XX_REG_ID_VENDOR GENMASK(3, 0)
  27. #define SI521XX_REG_BC 0x4
  28. #define SI521XX_REG_DA 0x5
  29. #define SI521XX_REG_DA_AMP_SEL BIT(7)
  30. #define SI521XX_REG_DA_AMP_MASK GENMASK(6, 4)
  31. #define SI521XX_REG_DA_AMP_MIN 300000
  32. #define SI521XX_REG_DA_AMP_DEFAULT 800000
  33. #define SI521XX_REG_DA_AMP_MAX 1000000
  34. #define SI521XX_REG_DA_AMP_STEP 100000
  35. #define SI521XX_REG_DA_AMP(UV) \
  36. FIELD_PREP(SI521XX_REG_DA_AMP_MASK, \
  37. ((UV) - SI521XX_REG_DA_AMP_MIN) / SI521XX_REG_DA_AMP_STEP)
  38. #define SI521XX_REG_DA_UNKNOWN BIT(3) /* Always set */
  39. /* Count of populated OE bits in control register ref, 1 and 2 */
  40. #define SI521XX_OE_MAP(cr1, cr2) (((cr2) << 8) | (cr1))
  41. #define SI521XX_OE_MAP_GET_OE(oe, map) (((map) >> (((oe) - 1) * 8)) & 0xff)
  42. #define SI521XX_DIFF_MULT 4
  43. #define SI521XX_DIFF_DIV 1
  44. /* Supported Skyworks Si521xx models. */
  45. enum si521xx_model {
  46. SI52144 = 0x44,
  47. SI52146 = 0x46,
  48. SI52147 = 0x47,
  49. };
  50. struct si521xx;
  51. struct si_clk {
  52. struct clk_hw hw;
  53. struct si521xx *si;
  54. u8 reg;
  55. u8 bit;
  56. };
  57. struct si521xx {
  58. struct i2c_client *client;
  59. struct regmap *regmap;
  60. struct si_clk clk_dif[9];
  61. u16 chip_info;
  62. u8 pll_amplitude;
  63. };
  64. /*
  65. * Si521xx i2c regmap
  66. */
  67. static const struct regmap_range si521xx_readable_ranges[] = {
  68. regmap_reg_range(SI521XX_REG_OE(0), SI521XX_REG_DA),
  69. };
  70. static const struct regmap_access_table si521xx_readable_table = {
  71. .yes_ranges = si521xx_readable_ranges,
  72. .n_yes_ranges = ARRAY_SIZE(si521xx_readable_ranges),
  73. };
  74. static const struct regmap_range si521xx_writeable_ranges[] = {
  75. regmap_reg_range(SI521XX_REG_OE(0), SI521XX_REG_OE(1)),
  76. regmap_reg_range(SI521XX_REG_BC, SI521XX_REG_DA),
  77. };
  78. static const struct regmap_access_table si521xx_writeable_table = {
  79. .yes_ranges = si521xx_writeable_ranges,
  80. .n_yes_ranges = ARRAY_SIZE(si521xx_writeable_ranges),
  81. };
  82. static int si521xx_regmap_i2c_write(void *context, unsigned int reg,
  83. unsigned int val)
  84. {
  85. struct i2c_client *i2c = context;
  86. const u8 data[2] = { reg, val };
  87. const int count = ARRAY_SIZE(data);
  88. int ret;
  89. ret = i2c_master_send(i2c, data, count);
  90. if (ret == count)
  91. return 0;
  92. else if (ret < 0)
  93. return ret;
  94. else
  95. return -EIO;
  96. }
  97. static int si521xx_regmap_i2c_read(void *context, unsigned int reg,
  98. unsigned int *val)
  99. {
  100. struct i2c_client *i2c = context;
  101. struct i2c_msg xfer[2];
  102. u8 txdata = reg;
  103. u8 rxdata[2];
  104. int ret;
  105. xfer[0].addr = i2c->addr;
  106. xfer[0].flags = 0;
  107. xfer[0].len = 1;
  108. xfer[0].buf = (void *)&txdata;
  109. xfer[1].addr = i2c->addr;
  110. xfer[1].flags = I2C_M_RD;
  111. xfer[1].len = 2;
  112. xfer[1].buf = (void *)rxdata;
  113. ret = i2c_transfer(i2c->adapter, xfer, 2);
  114. if (ret < 0)
  115. return ret;
  116. if (ret != 2)
  117. return -EIO;
  118. /*
  119. * Byte 0 is transfer length, which is always 1 due
  120. * to BCP register programming to 1 in si521xx_probe(),
  121. * ignore it and use data from Byte 1.
  122. */
  123. *val = rxdata[1];
  124. return 0;
  125. }
  126. static const struct regmap_config si521xx_regmap_config = {
  127. .reg_bits = 8,
  128. .val_bits = 8,
  129. .cache_type = REGCACHE_FLAT,
  130. .max_register = SI521XX_REG_DA,
  131. .rd_table = &si521xx_readable_table,
  132. .wr_table = &si521xx_writeable_table,
  133. .reg_write = si521xx_regmap_i2c_write,
  134. .reg_read = si521xx_regmap_i2c_read,
  135. };
  136. static unsigned long si521xx_diff_recalc_rate(struct clk_hw *hw,
  137. unsigned long parent_rate)
  138. {
  139. unsigned long long rate;
  140. rate = (unsigned long long)parent_rate * SI521XX_DIFF_MULT;
  141. do_div(rate, SI521XX_DIFF_DIV);
  142. return (unsigned long)rate;
  143. }
  144. static long si521xx_diff_round_rate(struct clk_hw *hw, unsigned long rate,
  145. unsigned long *prate)
  146. {
  147. unsigned long best_parent;
  148. best_parent = (rate / SI521XX_DIFF_MULT) * SI521XX_DIFF_DIV;
  149. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  150. return (*prate / SI521XX_DIFF_DIV) * SI521XX_DIFF_MULT;
  151. }
  152. static int si521xx_diff_set_rate(struct clk_hw *hw, unsigned long rate,
  153. unsigned long parent_rate)
  154. {
  155. /*
  156. * We must report success but we can do so unconditionally because
  157. * si521xx_diff_round_rate returns values that ensure this call is a
  158. * nop.
  159. */
  160. return 0;
  161. }
  162. #define to_si521xx_clk(_hw) container_of(_hw, struct si_clk, hw)
  163. static int si521xx_diff_prepare(struct clk_hw *hw)
  164. {
  165. struct si_clk *si_clk = to_si521xx_clk(hw);
  166. struct si521xx *si = si_clk->si;
  167. regmap_set_bits(si->regmap, SI521XX_REG_OE(si_clk->reg), si_clk->bit);
  168. return 0;
  169. }
  170. static void si521xx_diff_unprepare(struct clk_hw *hw)
  171. {
  172. struct si_clk *si_clk = to_si521xx_clk(hw);
  173. struct si521xx *si = si_clk->si;
  174. regmap_clear_bits(si->regmap, SI521XX_REG_OE(si_clk->reg), si_clk->bit);
  175. }
  176. static const struct clk_ops si521xx_diff_clk_ops = {
  177. .round_rate = si521xx_diff_round_rate,
  178. .set_rate = si521xx_diff_set_rate,
  179. .recalc_rate = si521xx_diff_recalc_rate,
  180. .prepare = si521xx_diff_prepare,
  181. .unprepare = si521xx_diff_unprepare,
  182. };
  183. static int si521xx_get_common_config(struct si521xx *si)
  184. {
  185. struct i2c_client *client = si->client;
  186. struct device_node *np = client->dev.of_node;
  187. unsigned int amp;
  188. int ret;
  189. /* Set defaults */
  190. si->pll_amplitude = SI521XX_REG_DA_AMP(SI521XX_REG_DA_AMP_DEFAULT);
  191. /* Output clock amplitude */
  192. ret = of_property_read_u32(np, "skyworks,out-amplitude-microvolt",
  193. &amp);
  194. if (!ret) {
  195. if (amp < SI521XX_REG_DA_AMP_MIN || amp > SI521XX_REG_DA_AMP_MAX ||
  196. amp % SI521XX_REG_DA_AMP_STEP) {
  197. return dev_err_probe(&client->dev, -EINVAL,
  198. "Invalid skyworks,out-amplitude-microvolt value\n");
  199. }
  200. si->pll_amplitude = SI521XX_REG_DA_AMP(amp);
  201. }
  202. return 0;
  203. }
  204. static void si521xx_update_config(struct si521xx *si)
  205. {
  206. /* If amplitude is non-default, update it. */
  207. if (si->pll_amplitude == SI521XX_REG_DA_AMP(SI521XX_REG_DA_AMP_DEFAULT))
  208. return;
  209. regmap_update_bits(si->regmap, SI521XX_REG_DA,
  210. SI521XX_REG_DA_AMP_MASK, si->pll_amplitude);
  211. }
  212. static void si521xx_diff_idx_to_reg_bit(const u16 chip_info, const int idx,
  213. struct si_clk *clk)
  214. {
  215. unsigned long mask;
  216. int oe, b, ctr = 0;
  217. for (oe = 1; oe <= 2; oe++) {
  218. mask = bitrev8(SI521XX_OE_MAP_GET_OE(oe, chip_info));
  219. for_each_set_bit(b, &mask, 8) {
  220. if (ctr++ != idx)
  221. continue;
  222. clk->reg = SI521XX_REG_OE(oe);
  223. clk->bit = 7 - b;
  224. return;
  225. }
  226. }
  227. }
  228. static struct clk_hw *
  229. si521xx_of_clk_get(struct of_phandle_args *clkspec, void *data)
  230. {
  231. struct si521xx *si = data;
  232. unsigned int idx = clkspec->args[0];
  233. return &si->clk_dif[idx].hw;
  234. }
  235. static int si521xx_probe(struct i2c_client *client)
  236. {
  237. const u16 chip_info = (u16)(uintptr_t)i2c_get_match_data(client);
  238. const struct clk_parent_data clk_parent_data = { .index = 0 };
  239. const u8 data[3] = { SI521XX_REG_BC, 1, 1 };
  240. unsigned char name[16] = "DIFF0";
  241. struct clk_init_data init = {};
  242. struct si521xx *si;
  243. int i, ret;
  244. if (!chip_info)
  245. return -EINVAL;
  246. si = devm_kzalloc(&client->dev, sizeof(*si), GFP_KERNEL);
  247. if (!si)
  248. return -ENOMEM;
  249. i2c_set_clientdata(client, si);
  250. si->client = client;
  251. /* Fetch common configuration from DT (if specified) */
  252. ret = si521xx_get_common_config(si);
  253. if (ret)
  254. return ret;
  255. si->regmap = devm_regmap_init(&client->dev, NULL, client,
  256. &si521xx_regmap_config);
  257. if (IS_ERR(si->regmap))
  258. return dev_err_probe(&client->dev, PTR_ERR(si->regmap),
  259. "Failed to allocate register map\n");
  260. /* Always read back 1 Byte via I2C */
  261. ret = i2c_master_send(client, data, ARRAY_SIZE(data));
  262. if (ret < 0)
  263. return ret;
  264. /* Register clock */
  265. for (i = 0; i < hweight16(chip_info); i++) {
  266. memset(&init, 0, sizeof(init));
  267. snprintf(name, sizeof(name), "DIFF%d", i);
  268. init.name = name;
  269. init.ops = &si521xx_diff_clk_ops;
  270. init.parent_data = &clk_parent_data;
  271. init.num_parents = 1;
  272. init.flags = CLK_SET_RATE_PARENT;
  273. si->clk_dif[i].hw.init = &init;
  274. si->clk_dif[i].si = si;
  275. si521xx_diff_idx_to_reg_bit(chip_info, i, &si->clk_dif[i]);
  276. ret = devm_clk_hw_register(&client->dev, &si->clk_dif[i].hw);
  277. if (ret)
  278. return ret;
  279. }
  280. ret = devm_of_clk_add_hw_provider(&client->dev, si521xx_of_clk_get, si);
  281. if (!ret)
  282. si521xx_update_config(si);
  283. return ret;
  284. }
  285. static int __maybe_unused si521xx_suspend(struct device *dev)
  286. {
  287. struct si521xx *si = dev_get_drvdata(dev);
  288. regcache_cache_only(si->regmap, true);
  289. regcache_mark_dirty(si->regmap);
  290. return 0;
  291. }
  292. static int __maybe_unused si521xx_resume(struct device *dev)
  293. {
  294. struct si521xx *si = dev_get_drvdata(dev);
  295. int ret;
  296. regcache_cache_only(si->regmap, false);
  297. ret = regcache_sync(si->regmap);
  298. if (ret)
  299. dev_err(dev, "Failed to restore register map: %d\n", ret);
  300. return ret;
  301. }
  302. static const struct i2c_device_id si521xx_id[] = {
  303. { "si52144", .driver_data = SI521XX_OE_MAP(0x5, 0xc0) },
  304. { "si52146", .driver_data = SI521XX_OE_MAP(0x15, 0xe0) },
  305. { "si52147", .driver_data = SI521XX_OE_MAP(0x17, 0xf8) },
  306. { }
  307. };
  308. MODULE_DEVICE_TABLE(i2c, si521xx_id);
  309. static const struct of_device_id clk_si521xx_of_match[] = {
  310. { .compatible = "skyworks,si52144", .data = (void *)SI521XX_OE_MAP(0x5, 0xc0) },
  311. { .compatible = "skyworks,si52146", .data = (void *)SI521XX_OE_MAP(0x15, 0xe0) },
  312. { .compatible = "skyworks,si52147", .data = (void *)SI521XX_OE_MAP(0x15, 0xf8) },
  313. { }
  314. };
  315. MODULE_DEVICE_TABLE(of, clk_si521xx_of_match);
  316. static SIMPLE_DEV_PM_OPS(si521xx_pm_ops, si521xx_suspend, si521xx_resume);
  317. static struct i2c_driver si521xx_driver = {
  318. .driver = {
  319. .name = "clk-si521xx",
  320. .pm = &si521xx_pm_ops,
  321. .of_match_table = clk_si521xx_of_match,
  322. },
  323. .probe = si521xx_probe,
  324. .id_table = si521xx_id,
  325. };
  326. module_i2c_driver(si521xx_driver);
  327. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  328. MODULE_DESCRIPTION("Skyworks Si521xx PCIe clock generator driver");
  329. MODULE_LICENSE("GPL");