clk-hi655x.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Clock driver for Hi655x
  4. *
  5. * Copyright (c) 2017, Linaro Ltd.
  6. *
  7. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/hi655x-pmic.h>
  16. #define HI655X_CLK_BASE HI655X_BUS_ADDR(0x1c)
  17. #define HI655X_CLK_SET BIT(6)
  18. struct hi655x_clk {
  19. struct hi655x_pmic *hi655x;
  20. struct clk_hw clk_hw;
  21. };
  22. static unsigned long hi655x_clk_recalc_rate(struct clk_hw *hw,
  23. unsigned long parent_rate)
  24. {
  25. return 32768;
  26. }
  27. static int hi655x_clk_enable(struct clk_hw *hw, bool enable)
  28. {
  29. struct hi655x_clk *hi655x_clk =
  30. container_of(hw, struct hi655x_clk, clk_hw);
  31. struct hi655x_pmic *hi655x = hi655x_clk->hi655x;
  32. return regmap_update_bits(hi655x->regmap, HI655X_CLK_BASE,
  33. HI655X_CLK_SET, enable ? HI655X_CLK_SET : 0);
  34. }
  35. static int hi655x_clk_prepare(struct clk_hw *hw)
  36. {
  37. return hi655x_clk_enable(hw, true);
  38. }
  39. static void hi655x_clk_unprepare(struct clk_hw *hw)
  40. {
  41. hi655x_clk_enable(hw, false);
  42. }
  43. static int hi655x_clk_is_prepared(struct clk_hw *hw)
  44. {
  45. struct hi655x_clk *hi655x_clk =
  46. container_of(hw, struct hi655x_clk, clk_hw);
  47. struct hi655x_pmic *hi655x = hi655x_clk->hi655x;
  48. int ret;
  49. uint32_t val;
  50. ret = regmap_read(hi655x->regmap, HI655X_CLK_BASE, &val);
  51. if (ret < 0)
  52. return ret;
  53. return val & HI655X_CLK_BASE;
  54. }
  55. static const struct clk_ops hi655x_clk_ops = {
  56. .prepare = hi655x_clk_prepare,
  57. .unprepare = hi655x_clk_unprepare,
  58. .is_prepared = hi655x_clk_is_prepared,
  59. .recalc_rate = hi655x_clk_recalc_rate,
  60. };
  61. static int hi655x_clk_probe(struct platform_device *pdev)
  62. {
  63. struct device *parent = pdev->dev.parent;
  64. struct hi655x_pmic *hi655x = dev_get_drvdata(parent);
  65. struct hi655x_clk *hi655x_clk;
  66. const char *clk_name = "hi655x-clk";
  67. struct clk_init_data init = {
  68. .name = clk_name,
  69. .ops = &hi655x_clk_ops
  70. };
  71. int ret;
  72. hi655x_clk = devm_kzalloc(&pdev->dev, sizeof(*hi655x_clk), GFP_KERNEL);
  73. if (!hi655x_clk)
  74. return -ENOMEM;
  75. of_property_read_string_index(parent->of_node, "clock-output-names",
  76. 0, &clk_name);
  77. hi655x_clk->clk_hw.init = &init;
  78. hi655x_clk->hi655x = hi655x;
  79. platform_set_drvdata(pdev, hi655x_clk);
  80. ret = devm_clk_hw_register(&pdev->dev, &hi655x_clk->clk_hw);
  81. if (ret)
  82. return ret;
  83. return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
  84. &hi655x_clk->clk_hw);
  85. }
  86. static struct platform_driver hi655x_clk_driver = {
  87. .probe = hi655x_clk_probe,
  88. .driver = {
  89. .name = "hi655x-clk",
  90. },
  91. };
  92. module_platform_driver(hi655x_clk_driver);
  93. MODULE_DESCRIPTION("Clk driver for the hi655x series PMICs");
  94. MODULE_AUTHOR("Daniel Lezcano <daniel.lezcano@linaro.org>");
  95. MODULE_LICENSE("GPL");
  96. MODULE_ALIAS("platform:hi655x-clk");