clk-si521xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 int si521xx_diff_determine_rate(struct clk_hw *hw,
  145. struct clk_rate_request *req)
  146. {
  147. unsigned long best_parent;
  148. best_parent = (req->rate / SI521XX_DIFF_MULT) * SI521XX_DIFF_DIV;
  149. req->best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
  150. req->rate = (req->best_parent_rate / SI521XX_DIFF_DIV) * SI521XX_DIFF_MULT;
  151. return 0;
  152. }
  153. static int si521xx_diff_set_rate(struct clk_hw *hw, unsigned long rate,
  154. unsigned long parent_rate)
  155. {
  156. /*
  157. * We must report success but we can do so unconditionally because
  158. * si521xx_diff_round_rate returns values that ensure this call is a
  159. * nop.
  160. */
  161. return 0;
  162. }
  163. #define to_si521xx_clk(_hw) container_of(_hw, struct si_clk, hw)
  164. static int si521xx_diff_prepare(struct clk_hw *hw)
  165. {
  166. struct si_clk *si_clk = to_si521xx_clk(hw);
  167. struct si521xx *si = si_clk->si;
  168. regmap_set_bits(si->regmap, SI521XX_REG_OE(si_clk->reg), si_clk->bit);
  169. return 0;
  170. }
  171. static void si521xx_diff_unprepare(struct clk_hw *hw)
  172. {
  173. struct si_clk *si_clk = to_si521xx_clk(hw);
  174. struct si521xx *si = si_clk->si;
  175. regmap_clear_bits(si->regmap, SI521XX_REG_OE(si_clk->reg), si_clk->bit);
  176. }
  177. static const struct clk_ops si521xx_diff_clk_ops = {
  178. .determine_rate = si521xx_diff_determine_rate,
  179. .set_rate = si521xx_diff_set_rate,
  180. .recalc_rate = si521xx_diff_recalc_rate,
  181. .prepare = si521xx_diff_prepare,
  182. .unprepare = si521xx_diff_unprepare,
  183. };
  184. static int si521xx_get_common_config(struct si521xx *si)
  185. {
  186. struct i2c_client *client = si->client;
  187. struct device_node *np = client->dev.of_node;
  188. unsigned int amp;
  189. int ret;
  190. /* Set defaults */
  191. si->pll_amplitude = SI521XX_REG_DA_AMP(SI521XX_REG_DA_AMP_DEFAULT);
  192. /* Output clock amplitude */
  193. ret = of_property_read_u32(np, "skyworks,out-amplitude-microvolt",
  194. &amp);
  195. if (!ret) {
  196. if (amp < SI521XX_REG_DA_AMP_MIN || amp > SI521XX_REG_DA_AMP_MAX ||
  197. amp % SI521XX_REG_DA_AMP_STEP) {
  198. return dev_err_probe(&client->dev, -EINVAL,
  199. "Invalid skyworks,out-amplitude-microvolt value\n");
  200. }
  201. si->pll_amplitude = SI521XX_REG_DA_AMP(amp);
  202. }
  203. return 0;
  204. }
  205. static void si521xx_update_config(struct si521xx *si)
  206. {
  207. /* If amplitude is non-default, update it. */
  208. if (si->pll_amplitude == SI521XX_REG_DA_AMP(SI521XX_REG_DA_AMP_DEFAULT))
  209. return;
  210. regmap_update_bits(si->regmap, SI521XX_REG_DA,
  211. SI521XX_REG_DA_AMP_MASK, si->pll_amplitude);
  212. }
  213. static void si521xx_diff_idx_to_reg_bit(const u16 chip_info, const int idx,
  214. struct si_clk *clk)
  215. {
  216. unsigned long mask;
  217. int oe, b, ctr = 0;
  218. for (oe = 1; oe <= 2; oe++) {
  219. mask = bitrev8(SI521XX_OE_MAP_GET_OE(oe, chip_info));
  220. for_each_set_bit(b, &mask, 8) {
  221. if (ctr++ != idx)
  222. continue;
  223. clk->reg = SI521XX_REG_OE(oe);
  224. clk->bit = 7 - b;
  225. return;
  226. }
  227. }
  228. }
  229. static struct clk_hw *
  230. si521xx_of_clk_get(struct of_phandle_args *clkspec, void *data)
  231. {
  232. struct si521xx *si = data;
  233. unsigned int idx = clkspec->args[0];
  234. return &si->clk_dif[idx].hw;
  235. }
  236. static int si521xx_probe(struct i2c_client *client)
  237. {
  238. const u16 chip_info = (u16)(uintptr_t)i2c_get_match_data(client);
  239. const struct clk_parent_data clk_parent_data = { .index = 0 };
  240. const u8 data[3] = { SI521XX_REG_BC, 1, 1 };
  241. unsigned char name[16] = "DIFF0";
  242. struct clk_init_data init = {};
  243. struct si521xx *si;
  244. int i, ret;
  245. if (!chip_info)
  246. return -EINVAL;
  247. si = devm_kzalloc(&client->dev, sizeof(*si), GFP_KERNEL);
  248. if (!si)
  249. return -ENOMEM;
  250. i2c_set_clientdata(client, si);
  251. si->client = client;
  252. /* Fetch common configuration from DT (if specified) */
  253. ret = si521xx_get_common_config(si);
  254. if (ret)
  255. return ret;
  256. si->regmap = devm_regmap_init(&client->dev, NULL, client,
  257. &si521xx_regmap_config);
  258. if (IS_ERR(si->regmap))
  259. return dev_err_probe(&client->dev, PTR_ERR(si->regmap),
  260. "Failed to allocate register map\n");
  261. /* Always read back 1 Byte via I2C */
  262. ret = i2c_master_send(client, data, ARRAY_SIZE(data));
  263. if (ret < 0)
  264. return ret;
  265. /* Register clock */
  266. for (i = 0; i < hweight16(chip_info); i++) {
  267. memset(&init, 0, sizeof(init));
  268. snprintf(name, sizeof(name), "DIFF%d", i);
  269. init.name = name;
  270. init.ops = &si521xx_diff_clk_ops;
  271. init.parent_data = &clk_parent_data;
  272. init.num_parents = 1;
  273. init.flags = CLK_SET_RATE_PARENT;
  274. si->clk_dif[i].hw.init = &init;
  275. si->clk_dif[i].si = si;
  276. si521xx_diff_idx_to_reg_bit(chip_info, i, &si->clk_dif[i]);
  277. ret = devm_clk_hw_register(&client->dev, &si->clk_dif[i].hw);
  278. if (ret)
  279. return ret;
  280. }
  281. ret = devm_of_clk_add_hw_provider(&client->dev, si521xx_of_clk_get, si);
  282. if (!ret)
  283. si521xx_update_config(si);
  284. return ret;
  285. }
  286. static int __maybe_unused si521xx_suspend(struct device *dev)
  287. {
  288. struct si521xx *si = dev_get_drvdata(dev);
  289. regcache_cache_only(si->regmap, true);
  290. regcache_mark_dirty(si->regmap);
  291. return 0;
  292. }
  293. static int __maybe_unused si521xx_resume(struct device *dev)
  294. {
  295. struct si521xx *si = dev_get_drvdata(dev);
  296. int ret;
  297. regcache_cache_only(si->regmap, false);
  298. ret = regcache_sync(si->regmap);
  299. if (ret)
  300. dev_err(dev, "Failed to restore register map: %d\n", ret);
  301. return ret;
  302. }
  303. static const struct i2c_device_id si521xx_id[] = {
  304. { "si52144", .driver_data = SI521XX_OE_MAP(0x5, 0xc0) },
  305. { "si52146", .driver_data = SI521XX_OE_MAP(0x15, 0xe0) },
  306. { "si52147", .driver_data = SI521XX_OE_MAP(0x17, 0xf8) },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(i2c, si521xx_id);
  310. static const struct of_device_id clk_si521xx_of_match[] = {
  311. { .compatible = "skyworks,si52144", .data = (void *)SI521XX_OE_MAP(0x5, 0xc0) },
  312. { .compatible = "skyworks,si52146", .data = (void *)SI521XX_OE_MAP(0x15, 0xe0) },
  313. { .compatible = "skyworks,si52147", .data = (void *)SI521XX_OE_MAP(0x15, 0xf8) },
  314. { }
  315. };
  316. MODULE_DEVICE_TABLE(of, clk_si521xx_of_match);
  317. static SIMPLE_DEV_PM_OPS(si521xx_pm_ops, si521xx_suspend, si521xx_resume);
  318. static struct i2c_driver si521xx_driver = {
  319. .driver = {
  320. .name = "clk-si521xx",
  321. .pm = &si521xx_pm_ops,
  322. .of_match_table = clk_si521xx_of_match,
  323. },
  324. .probe = si521xx_probe,
  325. .id_table = si521xx_id,
  326. };
  327. module_i2c_driver(si521xx_driver);
  328. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  329. MODULE_DESCRIPTION("Skyworks Si521xx PCIe clock generator driver");
  330. MODULE_LICENSE("GPL");