clk-rk808.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/rk808.h>
  23. #include <linux/i2c.h>
  24. #define RK808_NR_OUTPUT 2
  25. struct rk808_clkout {
  26. struct rk808 *rk808;
  27. struct clk_onecell_data clk_data;
  28. struct clk_hw clkout1_hw;
  29. struct clk_hw clkout2_hw;
  30. };
  31. static unsigned long rk808_clkout_recalc_rate(struct clk_hw *hw,
  32. unsigned long parent_rate)
  33. {
  34. return 32768;
  35. }
  36. static int rk808_clkout2_enable(struct clk_hw *hw, bool enable)
  37. {
  38. struct rk808_clkout *rk808_clkout = container_of(hw,
  39. struct rk808_clkout,
  40. clkout2_hw);
  41. struct rk808 *rk808 = rk808_clkout->rk808;
  42. return regmap_update_bits(rk808->regmap, RK808_CLK32OUT_REG,
  43. CLK32KOUT2_EN, enable ? CLK32KOUT2_EN : 0);
  44. }
  45. static int rk808_clkout2_prepare(struct clk_hw *hw)
  46. {
  47. return rk808_clkout2_enable(hw, true);
  48. }
  49. static void rk808_clkout2_unprepare(struct clk_hw *hw)
  50. {
  51. rk808_clkout2_enable(hw, false);
  52. }
  53. static int rk808_clkout2_is_prepared(struct clk_hw *hw)
  54. {
  55. struct rk808_clkout *rk808_clkout = container_of(hw,
  56. struct rk808_clkout,
  57. clkout2_hw);
  58. struct rk808 *rk808 = rk808_clkout->rk808;
  59. uint32_t val;
  60. int ret = regmap_read(rk808->regmap, RK808_CLK32OUT_REG, &val);
  61. if (ret < 0)
  62. return ret;
  63. return (val & CLK32KOUT2_EN) ? 1 : 0;
  64. }
  65. static const struct clk_ops rk808_clkout1_ops = {
  66. .recalc_rate = rk808_clkout_recalc_rate,
  67. };
  68. static const struct clk_ops rk808_clkout2_ops = {
  69. .prepare = rk808_clkout2_prepare,
  70. .unprepare = rk808_clkout2_unprepare,
  71. .is_prepared = rk808_clkout2_is_prepared,
  72. .recalc_rate = rk808_clkout_recalc_rate,
  73. };
  74. static int rk808_clkout_probe(struct platform_device *pdev)
  75. {
  76. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  77. struct i2c_client *client = rk808->i2c;
  78. struct device_node *node = client->dev.of_node;
  79. struct clk_init_data init = {};
  80. struct clk **clk_table;
  81. struct rk808_clkout *rk808_clkout;
  82. rk808_clkout = devm_kzalloc(&client->dev,
  83. sizeof(*rk808_clkout), GFP_KERNEL);
  84. if (!rk808_clkout)
  85. return -ENOMEM;
  86. rk808_clkout->rk808 = rk808;
  87. clk_table = devm_kcalloc(&client->dev, RK808_NR_OUTPUT,
  88. sizeof(struct clk *), GFP_KERNEL);
  89. if (!clk_table)
  90. return -ENOMEM;
  91. init.flags = CLK_IS_ROOT;
  92. init.parent_names = NULL;
  93. init.num_parents = 0;
  94. init.name = "rk808-clkout1";
  95. init.ops = &rk808_clkout1_ops;
  96. rk808_clkout->clkout1_hw.init = &init;
  97. /* optional override of the clockname */
  98. of_property_read_string_index(node, "clock-output-names",
  99. 0, &init.name);
  100. clk_table[0] = devm_clk_register(&client->dev,
  101. &rk808_clkout->clkout1_hw);
  102. if (IS_ERR(clk_table[0]))
  103. return PTR_ERR(clk_table[0]);
  104. init.name = "rk808-clkout2";
  105. init.ops = &rk808_clkout2_ops;
  106. rk808_clkout->clkout2_hw.init = &init;
  107. /* optional override of the clockname */
  108. of_property_read_string_index(node, "clock-output-names",
  109. 1, &init.name);
  110. clk_table[1] = devm_clk_register(&client->dev,
  111. &rk808_clkout->clkout2_hw);
  112. if (IS_ERR(clk_table[1]))
  113. return PTR_ERR(clk_table[1]);
  114. rk808_clkout->clk_data.clks = clk_table;
  115. rk808_clkout->clk_data.clk_num = RK808_NR_OUTPUT;
  116. return of_clk_add_provider(node, of_clk_src_onecell_get,
  117. &rk808_clkout->clk_data);
  118. }
  119. static int rk808_clkout_remove(struct platform_device *pdev)
  120. {
  121. struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
  122. struct i2c_client *client = rk808->i2c;
  123. struct device_node *node = client->dev.of_node;
  124. of_clk_del_provider(node);
  125. return 0;
  126. }
  127. static struct platform_driver rk808_clkout_driver = {
  128. .probe = rk808_clkout_probe,
  129. .remove = rk808_clkout_remove,
  130. .driver = {
  131. .name = "rk808-clkout",
  132. },
  133. };
  134. module_platform_driver(rk808_clkout_driver);
  135. MODULE_DESCRIPTION("Clkout driver for the rk808 series PMICs");
  136. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  137. MODULE_LICENSE("GPL");
  138. MODULE_ALIAS("platform:rk808-clkout");