clk-s2mps11.c 7.1 KB

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