clk-vexpress-osc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. */
  6. #include <linux/clkdev.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/vexpress.h>
  14. struct vexpress_osc {
  15. struct regmap *reg;
  16. struct clk_hw hw;
  17. unsigned long rate_min;
  18. unsigned long rate_max;
  19. };
  20. #define to_vexpress_osc(osc) container_of(osc, struct vexpress_osc, hw)
  21. static unsigned long vexpress_osc_recalc_rate(struct clk_hw *hw,
  22. unsigned long parent_rate)
  23. {
  24. struct vexpress_osc *osc = to_vexpress_osc(hw);
  25. u32 rate;
  26. regmap_read(osc->reg, 0, &rate);
  27. return rate;
  28. }
  29. static long vexpress_osc_round_rate(struct clk_hw *hw, unsigned long rate,
  30. unsigned long *parent_rate)
  31. {
  32. struct vexpress_osc *osc = to_vexpress_osc(hw);
  33. if (osc->rate_min && rate < osc->rate_min)
  34. rate = osc->rate_min;
  35. if (osc->rate_max && rate > osc->rate_max)
  36. rate = osc->rate_max;
  37. return rate;
  38. }
  39. static int vexpress_osc_set_rate(struct clk_hw *hw, unsigned long rate,
  40. unsigned long parent_rate)
  41. {
  42. struct vexpress_osc *osc = to_vexpress_osc(hw);
  43. return regmap_write(osc->reg, 0, rate);
  44. }
  45. static const struct clk_ops vexpress_osc_ops = {
  46. .recalc_rate = vexpress_osc_recalc_rate,
  47. .round_rate = vexpress_osc_round_rate,
  48. .set_rate = vexpress_osc_set_rate,
  49. };
  50. static int vexpress_osc_probe(struct platform_device *pdev)
  51. {
  52. struct clk_init_data init;
  53. struct vexpress_osc *osc;
  54. u32 range[2];
  55. int ret;
  56. osc = devm_kzalloc(&pdev->dev, sizeof(*osc), GFP_KERNEL);
  57. if (!osc)
  58. return -ENOMEM;
  59. osc->reg = devm_regmap_init_vexpress_config(&pdev->dev);
  60. if (IS_ERR(osc->reg))
  61. return PTR_ERR(osc->reg);
  62. if (of_property_read_u32_array(pdev->dev.of_node, "freq-range", range,
  63. ARRAY_SIZE(range)) == 0) {
  64. osc->rate_min = range[0];
  65. osc->rate_max = range[1];
  66. }
  67. if (of_property_read_string(pdev->dev.of_node, "clock-output-names",
  68. &init.name) != 0)
  69. init.name = dev_name(&pdev->dev);
  70. init.ops = &vexpress_osc_ops;
  71. init.flags = 0;
  72. init.num_parents = 0;
  73. osc->hw.init = &init;
  74. ret = devm_clk_hw_register(&pdev->dev, &osc->hw);
  75. if (ret < 0)
  76. return ret;
  77. devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get, &osc->hw);
  78. clk_hw_set_rate_range(&osc->hw, osc->rate_min, osc->rate_max);
  79. dev_dbg(&pdev->dev, "Registered clock '%s'\n", init.name);
  80. return 0;
  81. }
  82. static const struct of_device_id vexpress_osc_of_match[] = {
  83. { .compatible = "arm,vexpress-osc", },
  84. {}
  85. };
  86. MODULE_DEVICE_TABLE(of, vexpress_osc_of_match);
  87. static struct platform_driver vexpress_osc_driver = {
  88. .driver = {
  89. .name = "vexpress-osc",
  90. .of_match_table = vexpress_osc_of_match,
  91. },
  92. .probe = vexpress_osc_probe,
  93. };
  94. module_platform_driver(vexpress_osc_driver);
  95. MODULE_LICENSE("GPL v2");