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