clk-rk808.c 5.0 KB

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