clk-vexpress-osc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2012 ARM Limited
  12. */
  13. #include <linux/clkdev.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/vexpress.h>
  20. struct vexpress_osc {
  21. struct regmap *reg;
  22. struct clk_hw hw;
  23. unsigned long rate_min;
  24. unsigned long rate_max;
  25. };
  26. #define to_vexpress_osc(osc) container_of(osc, struct vexpress_osc, hw)
  27. static unsigned long vexpress_osc_recalc_rate(struct clk_hw *hw,
  28. unsigned long parent_rate)
  29. {
  30. struct vexpress_osc *osc = to_vexpress_osc(hw);
  31. u32 rate;
  32. regmap_read(osc->reg, 0, &rate);
  33. return rate;
  34. }
  35. static long vexpress_osc_round_rate(struct clk_hw *hw, unsigned long rate,
  36. unsigned long *parent_rate)
  37. {
  38. struct vexpress_osc *osc = to_vexpress_osc(hw);
  39. if (WARN_ON(osc->rate_min && rate < osc->rate_min))
  40. rate = osc->rate_min;
  41. if (WARN_ON(osc->rate_max && rate > osc->rate_max))
  42. rate = osc->rate_max;
  43. return rate;
  44. }
  45. static int vexpress_osc_set_rate(struct clk_hw *hw, unsigned long rate,
  46. unsigned long parent_rate)
  47. {
  48. struct vexpress_osc *osc = to_vexpress_osc(hw);
  49. return regmap_write(osc->reg, 0, rate);
  50. }
  51. static const struct clk_ops vexpress_osc_ops = {
  52. .recalc_rate = vexpress_osc_recalc_rate,
  53. .round_rate = vexpress_osc_round_rate,
  54. .set_rate = vexpress_osc_set_rate,
  55. };
  56. static int vexpress_osc_probe(struct platform_device *pdev)
  57. {
  58. struct clk_init_data init;
  59. struct vexpress_osc *osc;
  60. struct clk *clk;
  61. u32 range[2];
  62. osc = devm_kzalloc(&pdev->dev, sizeof(*osc), GFP_KERNEL);
  63. if (!osc)
  64. return -ENOMEM;
  65. osc->reg = devm_regmap_init_vexpress_config(&pdev->dev);
  66. if (IS_ERR(osc->reg))
  67. return PTR_ERR(osc->reg);
  68. if (of_property_read_u32_array(pdev->dev.of_node, "freq-range", range,
  69. ARRAY_SIZE(range)) == 0) {
  70. osc->rate_min = range[0];
  71. osc->rate_max = range[1];
  72. }
  73. if (of_property_read_string(pdev->dev.of_node, "clock-output-names",
  74. &init.name) != 0)
  75. init.name = dev_name(&pdev->dev);
  76. init.ops = &vexpress_osc_ops;
  77. init.flags = 0;
  78. init.num_parents = 0;
  79. osc->hw.init = &init;
  80. clk = clk_register(NULL, &osc->hw);
  81. if (IS_ERR(clk))
  82. return PTR_ERR(clk);
  83. of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get, clk);
  84. dev_dbg(&pdev->dev, "Registered clock '%s'\n", init.name);
  85. return 0;
  86. }
  87. static const struct of_device_id vexpress_osc_of_match[] = {
  88. { .compatible = "arm,vexpress-osc", },
  89. {}
  90. };
  91. static struct platform_driver vexpress_osc_driver = {
  92. .driver = {
  93. .name = "vexpress-osc",
  94. .of_match_table = vexpress_osc_of_match,
  95. },
  96. .probe = vexpress_osc_probe,
  97. };
  98. static int __init vexpress_osc_init(void)
  99. {
  100. return platform_driver_register(&vexpress_osc_driver);
  101. }
  102. core_initcall(vexpress_osc_init);