clk-rk808.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Clkout driver for Rockchip RK808
  4. *
  5. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  6. *
  7. * Author:Chris Zhong <zyw@rock-chips.com>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mfd/rk808.h>
  14. struct rk808_clkout {
  15. struct regmap *regmap;
  16. struct clk_hw clkout1_hw;
  17. struct clk_hw clkout2_hw;
  18. };
  19. static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
  20. unsigned long parent_rate)
  21. {
  22. return 32768;
  23. }
  24. static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
  25. {
  26. struct rk808_clkout *rk808_clkout = container_of(hw,
  27. struct rk808_clkout,
  28. clkout2_hw);
  29. return regmap_update_bits(rk808_clkout->regmap, RK808_CLK32OUT_REG,
  30. CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
  31. }
  32. static int rk808_clkout2_prepare(struct clk_hw *hw)
  33. {
  34. return rk808_clkout2_enable(hw, true);
  35. }
  36. static void rk808_clkout2_unprepare(struct clk_hw *hw)
  37. {
  38. rk808_clkout2_enable(hw, false);
  39. }
  40. static int rk808_clkout2_is_prepared(struct clk_hw *hw)
  41. {
  42. struct rk808_clkout *rk808_clkout = container_of(hw,
  43. struct rk808_clkout,
  44. clkout2_hw);
  45. uint32_t val;
  46. int ret = regmap_read(rk808_clkout->regmap, RK808_CLK32OUT_REG, &val);
  47. if (ret < 0)
  48. return ret;
  49. return (val & CLK32KOUT2_EN) ? 1 : 0;
  50. }
  51. static const struct clk_ops rk808_clkout1_ops = {
  52. .recalc_rate = rk808_clkout_recalc_rate,
  53. };
  54. static const struct clk_ops rk808_clkout2_ops = {
  55. .prepare = rk808_clkout2_prepare,
  56. .unprepare = rk808_clkout2_unprepare,
  57. .is_prepared = rk808_clkout2_is_prepared,
  58. .recalc_rate = rk808_clkout_recalc_rate,
  59. };
  60. static struct clk_hw *
  61. of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
  62. {
  63. struct rk808_clkout *rk808_clkout = data;
  64. unsigned int idx = clkspec->args[0];
  65. if (idx >= 2) {
  66. pr_err("%s: invalid index %u\n", __func__, idx);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
  70. }
  71. static int rk817_clkout2_enable(struct clk_hw *hw, bool enable)
  72. {
  73. struct rk808_clkout *rk808_clkout = container_of(hw,
  74. struct rk808_clkout,
  75. clkout2_hw);
  76. return regmap_update_bits(rk808_clkout->regmap, RK817_SYS_CFG(1),
  77. RK817_CLK32KOUT2_EN,
  78. enable ? RK817_CLK32KOUT2_EN : 0);
  79. }
  80. static int rk817_clkout2_prepare(struct clk_hw *hw)
  81. {
  82. return rk817_clkout2_enable(hw, true);
  83. }
  84. static void rk817_clkout2_unprepare(struct clk_hw *hw)
  85. {
  86. rk817_clkout2_enable(hw, false);
  87. }
  88. static int rk817_clkout2_is_prepared(struct clk_hw *hw)
  89. {
  90. struct rk808_clkout *rk808_clkout = container_of(hw,
  91. struct rk808_clkout,
  92. clkout2_hw);
  93. unsigned int val;
  94. int ret = regmap_read(rk808_clkout->regmap, RK817_SYS_CFG(1), &val);
  95. if (ret < 0)
  96. return 0;
  97. return (val & RK817_CLK32KOUT2_EN) ? 1 : 0;
  98. }
  99. static const struct clk_ops rk817_clkout2_ops = {
  100. .prepare = rk817_clkout2_prepare,
  101. .unprepare = rk817_clkout2_unprepare,
  102. .is_prepared = rk817_clkout2_is_prepared,
  103. .recalc_rate = rk808_clkout_recalc_rate,
  104. };
  105. static const struct clk_ops *rkpmic_get_ops(long variant)
  106. {
  107. switch (variant) {
  108. case RK809_ID:
  109. case RK817_ID:
  110. return &rk817_clkout2_ops;
  111. /*
  112. * For the default case, it match the following PMIC type.
  113. * RK805_ID
  114. * RK808_ID
  115. * RK818_ID
  116. */
  117. default:
  118. return &rk808_clkout2_ops;
  119. }
  120. }
  121. static int rk808_clkout_probe(struct platform_device *pdev)
  122. {
  123. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  124. struct device *dev = &pdev->dev;
  125. struct clk_init_data init = {};
  126. struct rk808_clkout *rk808_clkout;
  127. int ret;
  128. dev->of_node = pdev->dev.parent->of_node;
  129. rk808_clkout = devm_kzalloc(dev,
  130. sizeof(*rk808_clkout), GFP_KERNEL);
  131. if (!rk808_clkout)
  132. return -ENOMEM;
  133. rk808_clkout->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  134. if (!rk808_clkout->regmap)
  135. return -ENODEV;
  136. init.parent_names = NULL;
  137. init.num_parents = 0;
  138. init.name = "rk808-clkout1";
  139. init.ops = &rk808_clkout1_ops;
  140. rk808_clkout->clkout1_hw.init = &init;
  141. /* optional override of the clockname */
  142. of_property_read_string_index(dev->of_node, "clock-output-names",
  143. 0, &init.name);
  144. ret = devm_clk_hw_register(dev, &rk808_clkout->clkout1_hw);
  145. if (ret)
  146. return ret;
  147. init.name = "rk808-clkout2";
  148. init.ops = rkpmic_get_ops(rk808->variant);
  149. rk808_clkout->clkout2_hw.init = &init;
  150. /* optional override of the clockname */
  151. of_property_read_string_index(dev->of_node, "clock-output-names",
  152. 1, &init.name);
  153. ret = devm_clk_hw_register(dev, &rk808_clkout->clkout2_hw);
  154. if (ret)
  155. return ret;
  156. return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rk808_get,
  157. rk808_clkout);
  158. }
  159. static struct platform_driver rk808_clkout_driver = {
  160. .probe = rk808_clkout_probe,
  161. .driver = {
  162. .name = "rk808-clkout",
  163. },
  164. };
  165. module_platform_driver(rk808_clkout_driver);
  166. MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
  167. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  168. MODULE_LICENSE("GPL");
  169. MODULE_ALIAS("platform:rk808-clkout");