clk-rk808.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Clkout driver for Rockchip RK808
  3. *
  4. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * Author:Chris Zhong <zyw@rock-chips.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mfd/rk808.h>
  22. #include <linux/i2c.h>
  23. struct rk808_clkout {
  24. struct rk808 *rk808;
  25. struct clk_hw clkout1_hw;
  26. struct clk_hw clkout2_hw;
  27. };
  28. static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
  29. unsigned long parent_rate)
  30. {
  31. return 32768;
  32. }
  33. static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
  34. {
  35. struct rk808_clkout *rk808_clkout = container_of(hw,
  36. struct rk808_clkout,
  37. clkout2_hw);
  38. struct rk808 *rk808 = rk808_clkout->rk808;
  39. return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
  40. CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
  41. }
  42. static int rk808_clkout2_prepare(struct clk_hw *hw)
  43. {
  44. return rk808_clkout2_enable(hw, true);
  45. }
  46. static void rk808_clkout2_unprepare(struct clk_hw *hw)
  47. {
  48. rk808_clkout2_enable(hw, false);
  49. }
  50. static int rk808_clkout2_is_prepared(struct clk_hw *hw)
  51. {
  52. struct rk808_clkout *rk808_clkout = container_of(hw,
  53. struct rk808_clkout,
  54. clkout2_hw);
  55. struct rk808 *rk808 = rk808_clkout->rk808;
  56. uint32_t val;
  57. int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
  58. if (ret < 0)
  59. return ret;
  60. return (val & CLK32KOUT2_EN) ? 1 : 0;
  61. }
  62. static const struct clk_ops rk808_clkout1_ops = {
  63. .recalc_rate = rk808_clkout_recalc_rate,
  64. };
  65. static const struct clk_ops rk808_clkout2_ops = {
  66. .prepare = rk808_clkout2_prepare,
  67. .unprepare = rk808_clkout2_unprepare,
  68. .is_prepared = rk808_clkout2_is_prepared,
  69. .recalc_rate = rk808_clkout_recalc_rate,
  70. };
  71. static struct clk_hw *
  72. of_clk_rk808_get(struct of_phandle_args *clkspec, void *data)
  73. {
  74. struct rk808_clkout *rk808_clkout = data;
  75. unsigned int idx = clkspec->args[0];
  76. if (idx >= 2) {
  77. pr_err("%s: invalid index %u\n", __func__, idx);
  78. return ERR_PTR(-EINVAL);
  79. }
  80. return idx ? &rk808_clkout->clkout2_hw : &rk808_clkout->clkout1_hw;
  81. }
  82. static int rk808_clkout_probe(struct platform_device *pdev)
  83. {
  84. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  85. struct i2c_client *client = rk808->i2c;
  86. struct device_node *node = client->dev.of_node;
  87. struct clk_init_data init = {};
  88. struct rk808_clkout *rk808_clkout;
  89. int ret;
  90. rk808_clkout = devm_kzalloc(&client->dev,
  91. sizeof(*rk808_clkout), GFP_KERNEL);
  92. if (!rk808_clkout)
  93. return -ENOMEM;
  94. rk808_clkout->rk808 = rk808;
  95. init.parent_names = NULL;
  96. init.num_parents = 0;
  97. init.name = "rk808-clkout1";
  98. init.ops = &rk808_clkout1_ops;
  99. rk808_clkout->clkout1_hw.init = &init;
  100. /* optional override of the clockname */
  101. of_property_read_string_index(node, "clock-output-names",
  102. 0, &init.name);
  103. ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout1_hw);
  104. if (ret)
  105. return ret;
  106. init.name = "rk808-clkout2";
  107. init.ops = &rk808_clkout2_ops;
  108. rk808_clkout->clkout2_hw.init = &init;
  109. /* optional override of the clockname */
  110. of_property_read_string_index(node, "clock-output-names",
  111. 1, &init.name);
  112. ret = devm_clk_hw_register(&client->dev, &rk808_clkout->clkout2_hw);
  113. if (ret)
  114. return ret;
  115. return of_clk_add_hw_provider(node, of_clk_rk808_get, rk808_clkout);
  116. }
  117. static int rk808_clkout_remove(struct platform_device *pdev)
  118. {
  119. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  120. struct i2c_client *client = rk808->i2c;
  121. struct device_node *node = client->dev.of_node;
  122. of_clk_del_provider(node);
  123. return 0;
  124. }
  125. static struct platform_driver rk808_clkout_driver = {
  126. .probe = rk808_clkout_probe,
  127. .remove = rk808_clkout_remove,
  128. .driver = {
  129. .name = "rk808-clkout",
  130. },
  131. };
  132. module_platform_driver(rk808_clkout_driver);
  133. MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
  134. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  135. MODULE_LICENSE("GPL");
  136. MODULE_ALIAS("platform:rk808-clkout");