clk-pwm.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2014 Philipp Zabel, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * PWM (mis)used as clock output
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pwm.h>
  16. struct clk_pwm {
  17. struct clk_hw hw;
  18. struct pwm_device *pwm;
  19. u32 fixed_rate;
  20. };
  21. static inline struct clk_pwm *to_clk_pwm(struct clk_hw *hw)
  22. {
  23. return container_of(hw, struct clk_pwm, hw);
  24. }
  25. static int clk_pwm_prepare(struct clk_hw *hw)
  26. {
  27. struct clk_pwm *clk_pwm = to_clk_pwm(hw);
  28. return pwm_enable(clk_pwm->pwm);
  29. }
  30. static void clk_pwm_unprepare(struct clk_hw *hw)
  31. {
  32. struct clk_pwm *clk_pwm = to_clk_pwm(hw);
  33. pwm_disable(clk_pwm->pwm);
  34. }
  35. static unsigned long clk_pwm_recalc_rate(struct clk_hw *hw,
  36. unsigned long parent_rate)
  37. {
  38. struct clk_pwm *clk_pwm = to_clk_pwm(hw);
  39. return clk_pwm->fixed_rate;
  40. }
  41. static const struct clk_ops clk_pwm_ops = {
  42. .prepare = clk_pwm_prepare,
  43. .unprepare = clk_pwm_unprepare,
  44. .recalc_rate = clk_pwm_recalc_rate,
  45. };
  46. static int clk_pwm_probe(struct platform_device *pdev)
  47. {
  48. struct device_node *node = pdev->dev.of_node;
  49. struct clk_init_data init;
  50. struct clk_pwm *clk_pwm;
  51. struct pwm_device *pwm;
  52. const char *clk_name;
  53. struct clk *clk;
  54. int ret;
  55. clk_pwm = devm_kzalloc(&pdev->dev, sizeof(*clk_pwm), GFP_KERNEL);
  56. if (!clk_pwm)
  57. return -ENOMEM;
  58. pwm = devm_pwm_get(&pdev->dev, NULL);
  59. if (IS_ERR(pwm))
  60. return PTR_ERR(pwm);
  61. if (!pwm->period) {
  62. dev_err(&pdev->dev, "invalid PWM period\n");
  63. return -EINVAL;
  64. }
  65. if (of_property_read_u32(node, "clock-frequency", &clk_pwm->fixed_rate))
  66. clk_pwm->fixed_rate = NSEC_PER_SEC / pwm->period;
  67. if (pwm->period != NSEC_PER_SEC / clk_pwm->fixed_rate &&
  68. pwm->period != DIV_ROUND_UP(NSEC_PER_SEC, clk_pwm->fixed_rate)) {
  69. dev_err(&pdev->dev,
  70. "clock-frequency does not match PWM period\n");
  71. return -EINVAL;
  72. }
  73. ret = pwm_config(pwm, (pwm->period + 1) >> 1, pwm->period);
  74. if (ret < 0)
  75. return ret;
  76. clk_name = node->name;
  77. of_property_read_string(node, "clock-output-names", &clk_name);
  78. init.name = clk_name;
  79. init.ops = &clk_pwm_ops;
  80. init.flags = CLK_IS_BASIC | CLK_IS_ROOT;
  81. init.num_parents = 0;
  82. clk_pwm->pwm = pwm;
  83. clk_pwm->hw.init = &init;
  84. clk = devm_clk_register(&pdev->dev, &clk_pwm->hw);
  85. if (IS_ERR(clk))
  86. return PTR_ERR(clk);
  87. return of_clk_add_provider(node, of_clk_src_simple_get, clk);
  88. }
  89. static int clk_pwm_remove(struct platform_device *pdev)
  90. {
  91. of_clk_del_provider(pdev->dev.of_node);
  92. return 0;
  93. }
  94. static const struct of_device_id clk_pwm_dt_ids[] = {
  95. { .compatible = "pwm-clock" },
  96. { }
  97. };
  98. MODULE_DEVICE_TABLE(of, clk_pwm_dt_ids);
  99. static struct platform_driver clk_pwm_driver = {
  100. .probe = clk_pwm_probe,
  101. .remove = clk_pwm_remove,
  102. .driver = {
  103. .name = "pwm-clock",
  104. .of_match_table = of_match_ptr(clk_pwm_dt_ids),
  105. },
  106. };
  107. module_platform_driver(clk_pwm_driver);
  108. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  109. MODULE_DESCRIPTION("PWM clock driver");
  110. MODULE_LICENSE("GPL");