clk-tps68470.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Clock driver for TPS68470 PMIC
  4. *
  5. * Copyright (c) 2021 Red Hat Inc.
  6. * Copyright (C) 2018 Intel Corporation
  7. *
  8. * Authors:
  9. * Hans de Goede <[email protected]>
  10. * Zaikuo Wang <[email protected]>
  11. * Tianshu Qiu <[email protected]>
  12. * Jian Xu Zheng <[email protected]>
  13. * Yuning Pu <[email protected]>
  14. * Antti Laakso <[email protected]>
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mfd/tps68470.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/platform_data/tps68470.h>
  23. #include <linux/regmap.h>
  24. #define TPS68470_CLK_NAME "tps68470-clk"
  25. #define to_tps68470_clkdata(clkd) \
  26. container_of(clkd, struct tps68470_clkdata, clkout_hw)
  27. static struct tps68470_clkout_freqs {
  28. unsigned long freq;
  29. unsigned int xtaldiv;
  30. unsigned int plldiv;
  31. unsigned int postdiv;
  32. unsigned int buckdiv;
  33. unsigned int boostdiv;
  34. } clk_freqs[] = {
  35. /*
  36. * The PLL is used to multiply the crystal oscillator
  37. * frequency range of 3 MHz to 27 MHz by a programmable
  38. * factor of F = (M/N)*(1/P) such that the output
  39. * available at the HCLK_A or HCLK_B pins are in the range
  40. * of 4 MHz to 64 MHz in increments of 0.1 MHz.
  41. *
  42. * hclk_# = osc_in * (((plldiv*2)+320) / (xtaldiv+30)) * (1 / 2^postdiv)
  43. *
  44. * PLL_REF_CLK should be as close as possible to 100kHz
  45. * PLL_REF_CLK = input clk / XTALDIV[7:0] + 30)
  46. *
  47. * PLL_VCO_CLK = (PLL_REF_CLK * (plldiv*2 + 320))
  48. *
  49. * BOOST should be as close as possible to 2Mhz
  50. * BOOST = PLL_VCO_CLK / (BOOSTDIV[4:0] + 16) *
  51. *
  52. * BUCK should be as close as possible to 5.2Mhz
  53. * BUCK = PLL_VCO_CLK / (BUCKDIV[3:0] + 5)
  54. *
  55. * osc_in xtaldiv plldiv postdiv hclk_#
  56. * 20Mhz 170 32 1 19.2Mhz
  57. * 20Mhz 170 40 1 20Mhz
  58. * 20Mhz 170 80 1 24Mhz
  59. */
  60. { 19200000, 170, 32, 1, 2, 3 },
  61. { 20000000, 170, 40, 1, 3, 4 },
  62. { 24000000, 170, 80, 1, 4, 8 },
  63. };
  64. struct tps68470_clkdata {
  65. struct clk_hw clkout_hw;
  66. struct regmap *regmap;
  67. unsigned long rate;
  68. };
  69. static int tps68470_clk_is_prepared(struct clk_hw *hw)
  70. {
  71. struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
  72. int val;
  73. if (regmap_read(clkdata->regmap, TPS68470_REG_PLLCTL, &val))
  74. return 0;
  75. return val & TPS68470_PLL_EN_MASK;
  76. }
  77. static int tps68470_clk_prepare(struct clk_hw *hw)
  78. {
  79. struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
  80. regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG1,
  81. (TPS68470_PLL_OUTPUT_ENABLE << TPS68470_OUTPUT_A_SHIFT) |
  82. (TPS68470_PLL_OUTPUT_ENABLE << TPS68470_OUTPUT_B_SHIFT));
  83. regmap_update_bits(clkdata->regmap, TPS68470_REG_PLLCTL,
  84. TPS68470_PLL_EN_MASK, TPS68470_PLL_EN_MASK);
  85. /*
  86. * The PLLCTL reg lock bit is set by the PMIC after approx. 4ms and
  87. * does not indicate a true lock, so just wait 4 ms.
  88. */
  89. usleep_range(4000, 5000);
  90. return 0;
  91. }
  92. static void tps68470_clk_unprepare(struct clk_hw *hw)
  93. {
  94. struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
  95. /* Disable clock first ... */
  96. regmap_update_bits(clkdata->regmap, TPS68470_REG_PLLCTL, TPS68470_PLL_EN_MASK, 0);
  97. /* ... and then tri-state the clock outputs. */
  98. regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG1, 0);
  99. }
  100. static unsigned long tps68470_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  101. {
  102. struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
  103. return clkdata->rate;
  104. }
  105. /*
  106. * This returns the index of the clk_freqs[] cfg with the closest rate for
  107. * use in tps68470_clk_round_rate(). tps68470_clk_set_rate() checks that
  108. * the rate of the returned cfg is an exact match.
  109. */
  110. static unsigned int tps68470_clk_cfg_lookup(unsigned long rate)
  111. {
  112. long diff, best_diff = LONG_MAX;
  113. unsigned int i, best_idx = 0;
  114. for (i = 0; i < ARRAY_SIZE(clk_freqs); i++) {
  115. diff = clk_freqs[i].freq - rate;
  116. if (diff == 0)
  117. return i;
  118. diff = abs(diff);
  119. if (diff < best_diff) {
  120. best_diff = diff;
  121. best_idx = i;
  122. }
  123. }
  124. return best_idx;
  125. }
  126. static long tps68470_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  127. unsigned long *parent_rate)
  128. {
  129. unsigned int idx = tps68470_clk_cfg_lookup(rate);
  130. return clk_freqs[idx].freq;
  131. }
  132. static int tps68470_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  133. unsigned long parent_rate)
  134. {
  135. struct tps68470_clkdata *clkdata = to_tps68470_clkdata(hw);
  136. unsigned int idx = tps68470_clk_cfg_lookup(rate);
  137. if (rate != clk_freqs[idx].freq)
  138. return -EINVAL;
  139. regmap_write(clkdata->regmap, TPS68470_REG_BOOSTDIV, clk_freqs[idx].boostdiv);
  140. regmap_write(clkdata->regmap, TPS68470_REG_BUCKDIV, clk_freqs[idx].buckdiv);
  141. regmap_write(clkdata->regmap, TPS68470_REG_PLLSWR, TPS68470_PLLSWR_DEFAULT);
  142. regmap_write(clkdata->regmap, TPS68470_REG_XTALDIV, clk_freqs[idx].xtaldiv);
  143. regmap_write(clkdata->regmap, TPS68470_REG_PLLDIV, clk_freqs[idx].plldiv);
  144. regmap_write(clkdata->regmap, TPS68470_REG_POSTDIV, clk_freqs[idx].postdiv);
  145. regmap_write(clkdata->regmap, TPS68470_REG_POSTDIV2, clk_freqs[idx].postdiv);
  146. regmap_write(clkdata->regmap, TPS68470_REG_CLKCFG2, TPS68470_CLKCFG2_DRV_STR_2MA);
  147. regmap_write(clkdata->regmap, TPS68470_REG_PLLCTL,
  148. TPS68470_OSC_EXT_CAP_DEFAULT << TPS68470_OSC_EXT_CAP_SHIFT |
  149. TPS68470_CLK_SRC_XTAL << TPS68470_CLK_SRC_SHIFT);
  150. clkdata->rate = rate;
  151. return 0;
  152. }
  153. static const struct clk_ops tps68470_clk_ops = {
  154. .is_prepared = tps68470_clk_is_prepared,
  155. .prepare = tps68470_clk_prepare,
  156. .unprepare = tps68470_clk_unprepare,
  157. .recalc_rate = tps68470_clk_recalc_rate,
  158. .round_rate = tps68470_clk_round_rate,
  159. .set_rate = tps68470_clk_set_rate,
  160. };
  161. static int tps68470_clk_probe(struct platform_device *pdev)
  162. {
  163. struct tps68470_clk_platform_data *pdata = pdev->dev.platform_data;
  164. struct clk_init_data tps68470_clk_initdata = {
  165. .name = TPS68470_CLK_NAME,
  166. .ops = &tps68470_clk_ops,
  167. /* Changing the dividers when the PLL is on is not allowed */
  168. .flags = CLK_SET_RATE_GATE,
  169. };
  170. struct tps68470_clkdata *tps68470_clkdata;
  171. struct tps68470_clk_consumer *consumer;
  172. int ret;
  173. int i;
  174. tps68470_clkdata = devm_kzalloc(&pdev->dev, sizeof(*tps68470_clkdata),
  175. GFP_KERNEL);
  176. if (!tps68470_clkdata)
  177. return -ENOMEM;
  178. tps68470_clkdata->regmap = dev_get_drvdata(pdev->dev.parent);
  179. tps68470_clkdata->clkout_hw.init = &tps68470_clk_initdata;
  180. /* Set initial rate */
  181. tps68470_clk_set_rate(&tps68470_clkdata->clkout_hw, clk_freqs[0].freq, 0);
  182. ret = devm_clk_hw_register(&pdev->dev, &tps68470_clkdata->clkout_hw);
  183. if (ret)
  184. return ret;
  185. ret = devm_clk_hw_register_clkdev(&pdev->dev, &tps68470_clkdata->clkout_hw,
  186. TPS68470_CLK_NAME, NULL);
  187. if (ret)
  188. return ret;
  189. if (pdata) {
  190. for (i = 0; i < pdata->n_consumers; i++) {
  191. consumer = &pdata->consumers[i];
  192. ret = devm_clk_hw_register_clkdev(&pdev->dev,
  193. &tps68470_clkdata->clkout_hw,
  194. consumer->consumer_con_id,
  195. consumer->consumer_dev_name);
  196. }
  197. }
  198. return ret;
  199. }
  200. static struct platform_driver tps68470_clk_driver = {
  201. .driver = {
  202. .name = TPS68470_CLK_NAME,
  203. },
  204. .probe = tps68470_clk_probe,
  205. };
  206. /*
  207. * The ACPI tps68470 probe-ordering depends on the clk/gpio/regulator drivers
  208. * registering before the drivers for the camera-sensors which use them bind.
  209. * subsys_initcall() ensures this when the drivers are builtin.
  210. */
  211. static int __init tps68470_clk_init(void)
  212. {
  213. return platform_driver_register(&tps68470_clk_driver);
  214. }
  215. subsys_initcall(tps68470_clk_init);
  216. static void __exit tps68470_clk_exit(void)
  217. {
  218. platform_driver_unregister(&tps68470_clk_driver);
  219. }
  220. module_exit(tps68470_clk_exit);
  221. MODULE_ALIAS("platform:tps68470-clk");
  222. MODULE_DESCRIPTION("clock driver for TPS68470 pmic");
  223. MODULE_LICENSE("GPL");