raspberrypi-cpufreq.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Raspberry Pi cpufreq driver
  4. *
  5. * Copyright (C) 2019, Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/cpu.h>
  9. #include <linux/cpufreq.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_opp.h>
  13. #define RASPBERRYPI_FREQ_INTERVAL 100000000
  14. static struct platform_device *cpufreq_dt;
  15. static int raspberrypi_cpufreq_probe(struct platform_device *pdev)
  16. {
  17. struct device *cpu_dev;
  18. unsigned long min, max;
  19. unsigned long rate;
  20. struct clk *clk;
  21. int ret;
  22. cpu_dev = get_cpu_device(0);
  23. if (!cpu_dev) {
  24. pr_err("Cannot get CPU for cpufreq driver\n");
  25. return -ENODEV;
  26. }
  27. clk = clk_get(cpu_dev, NULL);
  28. if (IS_ERR(clk)) {
  29. dev_err(cpu_dev, "Cannot get clock for CPU0\n");
  30. return PTR_ERR(clk);
  31. }
  32. /*
  33. * The max and min frequencies are configurable in the Raspberry Pi
  34. * firmware, so we query them at runtime.
  35. */
  36. min = roundup(clk_round_rate(clk, 0), RASPBERRYPI_FREQ_INTERVAL);
  37. max = roundup(clk_round_rate(clk, ULONG_MAX), RASPBERRYPI_FREQ_INTERVAL);
  38. clk_put(clk);
  39. for (rate = min; rate <= max; rate += RASPBERRYPI_FREQ_INTERVAL) {
  40. ret = dev_pm_opp_add(cpu_dev, rate, 0);
  41. if (ret)
  42. goto remove_opp;
  43. }
  44. cpufreq_dt = platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
  45. ret = PTR_ERR_OR_ZERO(cpufreq_dt);
  46. if (ret) {
  47. dev_err(cpu_dev, "Failed to create platform device, %d\n", ret);
  48. goto remove_opp;
  49. }
  50. return 0;
  51. remove_opp:
  52. dev_pm_opp_remove_all_dynamic(cpu_dev);
  53. return ret;
  54. }
  55. static int raspberrypi_cpufreq_remove(struct platform_device *pdev)
  56. {
  57. struct device *cpu_dev;
  58. cpu_dev = get_cpu_device(0);
  59. if (cpu_dev)
  60. dev_pm_opp_remove_all_dynamic(cpu_dev);
  61. platform_device_unregister(cpufreq_dt);
  62. return 0;
  63. }
  64. /*
  65. * Since the driver depends on clk-raspberrypi, which may return EPROBE_DEFER,
  66. * all the activity is performed in the probe, which may be defered as well.
  67. */
  68. static struct platform_driver raspberrypi_cpufreq_driver = {
  69. .driver = {
  70. .name = "raspberrypi-cpufreq",
  71. },
  72. .probe = raspberrypi_cpufreq_probe,
  73. .remove = raspberrypi_cpufreq_remove,
  74. };
  75. module_platform_driver(raspberrypi_cpufreq_driver);
  76. MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de");
  77. MODULE_DESCRIPTION("Raspberry Pi cpufreq driver");
  78. MODULE_LICENSE("GPL");
  79. MODULE_ALIAS("platform:raspberrypi-cpufreq");