clk-spmi-pmic-div.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/log2.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #define REG_DIV_CTL1 0x43
  25. #define DIV_CTL1_DIV_FACTOR_MASK GENMASK(2, 0)
  26. #define REG_EN_CTL 0x46
  27. #define REG_EN_MASK BIT(7)
  28. struct clkdiv {
  29. struct regmap *regmap;
  30. u16 base;
  31. spinlock_t lock;
  32. struct clk_hw hw;
  33. unsigned int cxo_period_ns;
  34. };
  35. static inline struct clkdiv *to_clkdiv(struct clk_hw *hw)
  36. {
  37. return container_of(hw, struct clkdiv, hw);
  38. }
  39. static inline unsigned int div_factor_to_div(unsigned int div_factor)
  40. {
  41. if (!div_factor)
  42. div_factor = 1;
  43. return 1 << (div_factor - 1);
  44. }
  45. static inline unsigned int div_to_div_factor(unsigned int div)
  46. {
  47. return min(ilog2(div) + 1, 7);
  48. }
  49. static bool is_spmi_pmic_clkdiv_enabled(struct clkdiv *clkdiv)
  50. {
  51. unsigned int val = 0;
  52. regmap_read(clkdiv->regmap, clkdiv->base + REG_EN_CTL, &val);
  53. return val & REG_EN_MASK;
  54. }
  55. static int
  56. __spmi_pmic_clkdiv_set_enable_state(struct clkdiv *clkdiv, bool enable,
  57. unsigned int div_factor)
  58. {
  59. int ret;
  60. unsigned int ns = clkdiv->cxo_period_ns;
  61. unsigned int div = div_factor_to_div(div_factor);
  62. ret = regmap_update_bits(clkdiv->regmap, clkdiv->base + REG_EN_CTL,
  63. REG_EN_MASK, enable ? REG_EN_MASK : 0);
  64. if (ret)
  65. return ret;
  66. if (enable)
  67. ndelay((2 + 3 * div) * ns);
  68. else
  69. ndelay(3 * div * ns);
  70. return 0;
  71. }
  72. static int spmi_pmic_clkdiv_set_enable_state(struct clkdiv *clkdiv, bool enable)
  73. {
  74. unsigned int div_factor;
  75. regmap_read(clkdiv->regmap, clkdiv->base + REG_DIV_CTL1, &div_factor);
  76. div_factor &= DIV_CTL1_DIV_FACTOR_MASK;
  77. return __spmi_pmic_clkdiv_set_enable_state(clkdiv, enable, div_factor);
  78. }
  79. static int clk_spmi_pmic_div_enable(struct clk_hw *hw)
  80. {
  81. struct clkdiv *clkdiv = to_clkdiv(hw);
  82. unsigned long flags;
  83. int ret;
  84. spin_lock_irqsave(&clkdiv->lock, flags);
  85. ret = spmi_pmic_clkdiv_set_enable_state(clkdiv, true);
  86. spin_unlock_irqrestore(&clkdiv->lock, flags);
  87. return ret;
  88. }
  89. static void clk_spmi_pmic_div_disable(struct clk_hw *hw)
  90. {
  91. struct clkdiv *clkdiv = to_clkdiv(hw);
  92. unsigned long flags;
  93. spin_lock_irqsave(&clkdiv->lock, flags);
  94. spmi_pmic_clkdiv_set_enable_state(clkdiv, false);
  95. spin_unlock_irqrestore(&clkdiv->lock, flags);
  96. }
  97. static long clk_spmi_pmic_div_round_rate(struct clk_hw *hw, unsigned long rate,
  98. unsigned long *parent_rate)
  99. {
  100. unsigned int div, div_factor;
  101. div = DIV_ROUND_UP(*parent_rate, rate);
  102. div_factor = div_to_div_factor(div);
  103. div = div_factor_to_div(div_factor);
  104. return *parent_rate / div;
  105. }
  106. static unsigned long
  107. clk_spmi_pmic_div_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  108. {
  109. struct clkdiv *clkdiv = to_clkdiv(hw);
  110. unsigned int div_factor;
  111. regmap_read(clkdiv->regmap, clkdiv->base + REG_DIV_CTL1, &div_factor);
  112. div_factor &= DIV_CTL1_DIV_FACTOR_MASK;
  113. return parent_rate / div_factor_to_div(div_factor);
  114. }
  115. static int clk_spmi_pmic_div_set_rate(struct clk_hw *hw, unsigned long rate,
  116. unsigned long parent_rate)
  117. {
  118. struct clkdiv *clkdiv = to_clkdiv(hw);
  119. unsigned int div_factor = div_to_div_factor(parent_rate / rate);
  120. unsigned long flags;
  121. bool enabled;
  122. int ret;
  123. spin_lock_irqsave(&clkdiv->lock, flags);
  124. enabled = is_spmi_pmic_clkdiv_enabled(clkdiv);
  125. if (enabled) {
  126. ret = spmi_pmic_clkdiv_set_enable_state(clkdiv, false);
  127. if (ret)
  128. goto unlock;
  129. }
  130. ret = regmap_update_bits(clkdiv->regmap, clkdiv->base + REG_DIV_CTL1,
  131. DIV_CTL1_DIV_FACTOR_MASK, div_factor);
  132. if (ret)
  133. goto unlock;
  134. if (enabled)
  135. ret = __spmi_pmic_clkdiv_set_enable_state(clkdiv, true,
  136. div_factor);
  137. unlock:
  138. spin_unlock_irqrestore(&clkdiv->lock, flags);
  139. return ret;
  140. }
  141. static const struct clk_ops clk_spmi_pmic_div_ops = {
  142. .enable = clk_spmi_pmic_div_enable,
  143. .disable = clk_spmi_pmic_div_disable,
  144. .set_rate = clk_spmi_pmic_div_set_rate,
  145. .recalc_rate = clk_spmi_pmic_div_recalc_rate,
  146. .round_rate = clk_spmi_pmic_div_round_rate,
  147. };
  148. struct spmi_pmic_div_clk_cc {
  149. int nclks;
  150. struct clkdiv clks[];
  151. };
  152. static struct clk_hw *
  153. spmi_pmic_div_clk_hw_get(struct of_phandle_args *clkspec, void *data)
  154. {
  155. struct spmi_pmic_div_clk_cc *cc = data;
  156. int idx = clkspec->args[0] - 1; /* Start at 1 instead of 0 */
  157. if (idx < 0 || idx >= cc->nclks) {
  158. pr_err("%s: index value %u is invalid; allowed range [1, %d]\n",
  159. __func__, clkspec->args[0], cc->nclks);
  160. return ERR_PTR(-EINVAL);
  161. }
  162. return &cc->clks[idx].hw;
  163. }
  164. static int spmi_pmic_clkdiv_probe(struct platform_device *pdev)
  165. {
  166. struct spmi_pmic_div_clk_cc *cc;
  167. struct clk_init_data init = {};
  168. struct clkdiv *clkdiv;
  169. struct clk *cxo;
  170. struct regmap *regmap;
  171. struct device *dev = &pdev->dev;
  172. struct device_node *of_node = dev->of_node;
  173. const char *parent_name;
  174. int nclks, i, ret, cxo_hz;
  175. char name[20];
  176. u32 start;
  177. ret = of_property_read_u32(of_node, "reg", &start);
  178. if (ret < 0) {
  179. dev_err(dev, "reg property reading failed\n");
  180. return ret;
  181. }
  182. regmap = dev_get_regmap(dev->parent, NULL);
  183. if (!regmap) {
  184. dev_err(dev, "Couldn't get parent's regmap\n");
  185. return -EINVAL;
  186. }
  187. ret = of_property_read_u32(of_node, "qcom,num-clkdivs", &nclks);
  188. if (ret < 0) {
  189. dev_err(dev, "qcom,num-clkdivs property reading failed, ret=%d\n",
  190. ret);
  191. return ret;
  192. }
  193. if (!nclks)
  194. return -EINVAL;
  195. cc = devm_kzalloc(dev, struct_size(cc, clks, nclks), GFP_KERNEL);
  196. if (!cc)
  197. return -ENOMEM;
  198. cc->nclks = nclks;
  199. cxo = clk_get(dev, "xo");
  200. if (IS_ERR(cxo)) {
  201. ret = PTR_ERR(cxo);
  202. if (ret != -EPROBE_DEFER)
  203. dev_err(dev, "failed to get xo clock\n");
  204. return ret;
  205. }
  206. cxo_hz = clk_get_rate(cxo);
  207. clk_put(cxo);
  208. parent_name = of_clk_get_parent_name(of_node, 0);
  209. if (!parent_name) {
  210. dev_err(dev, "missing parent clock\n");
  211. return -ENODEV;
  212. }
  213. init.name = name;
  214. init.parent_names = &parent_name;
  215. init.num_parents = 1;
  216. init.ops = &clk_spmi_pmic_div_ops;
  217. for (i = 0, clkdiv = cc->clks; i < nclks; i++) {
  218. snprintf(name, sizeof(name), "div_clk%d", i + 1);
  219. spin_lock_init(&clkdiv[i].lock);
  220. clkdiv[i].base = start + i * 0x100;
  221. clkdiv[i].regmap = regmap;
  222. clkdiv[i].cxo_period_ns = NSEC_PER_SEC / cxo_hz;
  223. clkdiv[i].hw.init = &init;
  224. ret = devm_clk_hw_register(dev, &clkdiv[i].hw);
  225. if (ret)
  226. return ret;
  227. }
  228. return devm_of_clk_add_hw_provider(dev, spmi_pmic_div_clk_hw_get, cc);
  229. }
  230. static const struct of_device_id spmi_pmic_clkdiv_match_table[] = {
  231. { .compatible = "qcom,spmi-clkdiv" },
  232. { /* sentinel */ }
  233. };
  234. MODULE_DEVICE_TABLE(of, spmi_pmic_clkdiv_match_table);
  235. static struct platform_driver spmi_pmic_clkdiv_driver = {
  236. .driver = {
  237. .name = "qcom,spmi-pmic-clkdiv",
  238. .of_match_table = spmi_pmic_clkdiv_match_table,
  239. },
  240. .probe = spmi_pmic_clkdiv_probe,
  241. };
  242. module_platform_driver(spmi_pmic_clkdiv_driver);
  243. MODULE_DESCRIPTION("QCOM SPMI PMIC clkdiv driver");
  244. MODULE_LICENSE("GPL v2");