clk-s2mps11.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // clk-s2mps11.c - Clock driver for S2MPS11.
  4. //
  5. // Copyright (C) 2013,2014 Samsung Electornics
  6. #include <linux/module.h>
  7. #include <linux/err.h>
  8. #include <linux/of.h>
  9. #include <linux/clkdev.h>
  10. #include <linux/regmap.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mfd/samsung/s2mps11.h>
  14. #include <linux/mfd/samsung/s2mps13.h>
  15. #include <linux/mfd/samsung/s2mps14.h>
  16. #include <linux/mfd/samsung/s5m8767.h>
  17. #include <linux/mfd/samsung/core.h>
  18. #include <dt-bindings/clock/samsung,s2mps11.h>
  19. struct s2mps11_clk {
  20. struct sec_pmic_dev *iodev;
  21. struct device_node *clk_np;
  22. struct clk_hw hw;
  23. struct clk *clk;
  24. struct clk_lookup *lookup;
  25. u32 mask;
  26. unsigned int reg;
  27. };
  28. static struct s2mps11_clk *to_s2mps11_clk(struct clk_hw *hw)
  29. {
  30. return container_of(hw, struct s2mps11_clk, hw);
  31. }
  32. static int s2mps11_clk_prepare(struct clk_hw *hw)
  33. {
  34. struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
  35. return regmap_update_bits(s2mps11->iodev->regmap_pmic,
  36. s2mps11->reg,
  37. s2mps11->mask, s2mps11->mask);
  38. }
  39. static void s2mps11_clk_unprepare(struct clk_hw *hw)
  40. {
  41. struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
  42. regmap_update_bits(s2mps11->iodev->regmap_pmic, s2mps11->reg,
  43. s2mps11->mask, ~s2mps11->mask);
  44. }
  45. static int s2mps11_clk_is_prepared(struct clk_hw *hw)
  46. {
  47. int ret;
  48. u32 val;
  49. struct s2mps11_clk *s2mps11 = to_s2mps11_clk(hw);
  50. ret = regmap_read(s2mps11->iodev->regmap_pmic,
  51. s2mps11->reg, &val);
  52. if (ret < 0)
  53. return -EINVAL;
  54. return val & s2mps11->mask;
  55. }
  56. static unsigned long s2mps11_clk_recalc_rate(struct clk_hw *hw,
  57. unsigned long parent_rate)
  58. {
  59. return 32768;
  60. }
  61. static const struct clk_ops s2mps11_clk_ops = {
  62. .prepare = s2mps11_clk_prepare,
  63. .unprepare = s2mps11_clk_unprepare,
  64. .is_prepared = s2mps11_clk_is_prepared,
  65. .recalc_rate = s2mps11_clk_recalc_rate,
  66. };
  67. /* This s2mps11_clks_init tructure is common to s2mps11, s2mps13 and s2mps14 */
  68. static struct clk_init_data s2mps11_clks_init[S2MPS11_CLKS_NUM] = {
  69. [S2MPS11_CLK_AP] = {
  70. .name = "s2mps11_ap",
  71. .ops = &s2mps11_clk_ops,
  72. },
  73. [S2MPS11_CLK_CP] = {
  74. .name = "s2mps11_cp",
  75. .ops = &s2mps11_clk_ops,
  76. },
  77. [S2MPS11_CLK_BT] = {
  78. .name = "s2mps11_bt",
  79. .ops = &s2mps11_clk_ops,
  80. },
  81. };
  82. static struct device_node *s2mps11_clk_parse_dt(struct platform_device *pdev,
  83. struct clk_init_data *clks_init)
  84. {
  85. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  86. struct device_node *clk_np;
  87. int i;
  88. if (!iodev->dev->of_node)
  89. return ERR_PTR(-EINVAL);
  90. clk_np = of_get_child_by_name(iodev->dev->of_node, "clocks");
  91. if (!clk_np) {
  92. dev_err(&pdev->dev, "could not find clock sub-node\n");
  93. return ERR_PTR(-EINVAL);
  94. }
  95. for (i = 0; i < S2MPS11_CLKS_NUM; i++)
  96. of_property_read_string_index(clk_np, "clock-output-names", i,
  97. &clks_init[i].name);
  98. return clk_np;
  99. }
  100. static int s2mps11_clk_probe(struct platform_device *pdev)
  101. {
  102. struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  103. struct s2mps11_clk *s2mps11_clks;
  104. struct clk_hw_onecell_data *clk_data;
  105. unsigned int s2mps11_reg;
  106. int i, ret = 0;
  107. enum sec_device_type hwid = platform_get_device_id(pdev)->driver_data;
  108. s2mps11_clks = devm_kcalloc(&pdev->dev, S2MPS11_CLKS_NUM,
  109. sizeof(*s2mps11_clks), GFP_KERNEL);
  110. if (!s2mps11_clks)
  111. return -ENOMEM;
  112. clk_data = devm_kzalloc(&pdev->dev,
  113. struct_size(clk_data, hws, S2MPS11_CLKS_NUM),
  114. GFP_KERNEL);
  115. if (!clk_data)
  116. return -ENOMEM;
  117. switch (hwid) {
  118. case S2MPS11X:
  119. s2mps11_reg = S2MPS11_REG_RTC_CTRL;
  120. break;
  121. case S2MPS13X:
  122. s2mps11_reg = S2MPS13_REG_RTCCTRL;
  123. break;
  124. case S2MPS14X:
  125. s2mps11_reg = S2MPS14_REG_RTCCTRL;
  126. break;
  127. case S5M8767X:
  128. s2mps11_reg = S5M8767_REG_CTRL1;
  129. break;
  130. default:
  131. dev_err(&pdev->dev, "Invalid device type\n");
  132. return -EINVAL;
  133. }
  134. /* Store clocks of_node in first element of s2mps11_clks array */
  135. s2mps11_clks->clk_np = s2mps11_clk_parse_dt(pdev, s2mps11_clks_init);
  136. if (IS_ERR(s2mps11_clks->clk_np))
  137. return PTR_ERR(s2mps11_clks->clk_np);
  138. for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
  139. if (i == S2MPS11_CLK_CP && hwid == S2MPS14X)
  140. continue; /* Skip clocks not present in some devices */
  141. s2mps11_clks[i].iodev = iodev;
  142. s2mps11_clks[i].hw.init = &s2mps11_clks_init[i];
  143. s2mps11_clks[i].mask = 1 << i;
  144. s2mps11_clks[i].reg = s2mps11_reg;
  145. s2mps11_clks[i].clk = devm_clk_register(&pdev->dev,
  146. &s2mps11_clks[i].hw);
  147. if (IS_ERR(s2mps11_clks[i].clk)) {
  148. dev_err(&pdev->dev, "Fail to register : %s\n",
  149. s2mps11_clks_init[i].name);
  150. ret = PTR_ERR(s2mps11_clks[i].clk);
  151. goto err_reg;
  152. }
  153. s2mps11_clks[i].lookup = clkdev_hw_create(&s2mps11_clks[i].hw,
  154. s2mps11_clks_init[i].name, NULL);
  155. if (!s2mps11_clks[i].lookup) {
  156. ret = -ENOMEM;
  157. goto err_reg;
  158. }
  159. clk_data->hws[i] = &s2mps11_clks[i].hw;
  160. }
  161. clk_data->num = S2MPS11_CLKS_NUM;
  162. of_clk_add_hw_provider(s2mps11_clks->clk_np, of_clk_hw_onecell_get,
  163. clk_data);
  164. platform_set_drvdata(pdev, s2mps11_clks);
  165. return ret;
  166. err_reg:
  167. of_node_put(s2mps11_clks[0].clk_np);
  168. while (--i >= 0)
  169. clkdev_drop(s2mps11_clks[i].lookup);
  170. return ret;
  171. }
  172. static int s2mps11_clk_remove(struct platform_device *pdev)
  173. {
  174. struct s2mps11_clk *s2mps11_clks = platform_get_drvdata(pdev);
  175. int i;
  176. of_clk_del_provider(s2mps11_clks[0].clk_np);
  177. /* Drop the reference obtained in s2mps11_clk_parse_dt */
  178. of_node_put(s2mps11_clks[0].clk_np);
  179. for (i = 0; i < S2MPS11_CLKS_NUM; i++) {
  180. /* Skip clocks not present on S2MPS14 */
  181. if (!s2mps11_clks[i].lookup)
  182. continue;
  183. clkdev_drop(s2mps11_clks[i].lookup);
  184. }
  185. return 0;
  186. }
  187. static const struct platform_device_id s2mps11_clk_id[] = {
  188. { "s2mps11-clk", S2MPS11X},
  189. { "s2mps13-clk", S2MPS13X},
  190. { "s2mps14-clk", S2MPS14X},
  191. { "s5m8767-clk", S5M8767X},
  192. { },
  193. };
  194. MODULE_DEVICE_TABLE(platform, s2mps11_clk_id);
  195. #ifdef CONFIG_OF
  196. /*
  197. * Device is instantiated through parent MFD device and device matching is done
  198. * through platform_device_id.
  199. *
  200. * However if device's DT node contains proper clock compatible and driver is
  201. * built as a module, then the *module* matching will be done trough DT aliases.
  202. * This requires of_device_id table. In the same time this will not change the
  203. * actual *device* matching so do not add .of_match_table.
  204. */
  205. static const struct of_device_id s2mps11_dt_match[] __used = {
  206. {
  207. .compatible = "samsung,s2mps11-clk",
  208. .data = (void *)S2MPS11X,
  209. }, {
  210. .compatible = "samsung,s2mps13-clk",
  211. .data = (void *)S2MPS13X,
  212. }, {
  213. .compatible = "samsung,s2mps14-clk",
  214. .data = (void *)S2MPS14X,
  215. }, {
  216. .compatible = "samsung,s5m8767-clk",
  217. .data = (void *)S5M8767X,
  218. }, {
  219. /* Sentinel */
  220. },
  221. };
  222. MODULE_DEVICE_TABLE(of, s2mps11_dt_match);
  223. #endif
  224. static struct platform_driver s2mps11_clk_driver = {
  225. .driver = {
  226. .name = "s2mps11-clk",
  227. },
  228. .probe = s2mps11_clk_probe,
  229. .remove = s2mps11_clk_remove,
  230. .id_table = s2mps11_clk_id,
  231. };
  232. module_platform_driver(s2mps11_clk_driver);
  233. MODULE_DESCRIPTION("S2MPS11 Clock Driver");
  234. MODULE_AUTHOR("Yadwinder Singh Brar <[email protected]>");
  235. MODULE_LICENSE("GPL");