highbank-cpufreq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Calxeda, Inc.
  4. *
  5. * This driver provides the clk notifier callbacks that are used when
  6. * the cpufreq-dt driver changes to frequency to alert the highbank
  7. * EnergyCore Management Engine (ECME) about the need to change
  8. * voltage. The ECME interfaces with the actual voltage regulators.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/pl320-ipc.h>
  18. #include <linux/platform_device.h>
  19. #define HB_CPUFREQ_CHANGE_NOTE 0x80000001
  20. #define HB_CPUFREQ_IPC_LEN 7
  21. #define HB_CPUFREQ_VOLT_RETRIES 15
  22. static int hb_voltage_change(unsigned int freq)
  23. {
  24. u32 msg[HB_CPUFREQ_IPC_LEN] = {HB_CPUFREQ_CHANGE_NOTE, freq / 1000000};
  25. return pl320_ipc_transmit(msg);
  26. }
  27. static int hb_cpufreq_clk_notify(struct notifier_block *nb,
  28. unsigned long action, void *hclk)
  29. {
  30. struct clk_notifier_data *clk_data = hclk;
  31. int i = 0;
  32. if (action == PRE_RATE_CHANGE) {
  33. if (clk_data->new_rate > clk_data->old_rate)
  34. while (hb_voltage_change(clk_data->new_rate))
  35. if (i++ > HB_CPUFREQ_VOLT_RETRIES)
  36. return NOTIFY_BAD;
  37. } else if (action == POST_RATE_CHANGE) {
  38. if (clk_data->new_rate < clk_data->old_rate)
  39. while (hb_voltage_change(clk_data->new_rate))
  40. if (i++ > HB_CPUFREQ_VOLT_RETRIES)
  41. return NOTIFY_BAD;
  42. }
  43. return NOTIFY_DONE;
  44. }
  45. static struct notifier_block hb_cpufreq_clk_nb = {
  46. .notifier_call = hb_cpufreq_clk_notify,
  47. };
  48. static int hb_cpufreq_driver_init(void)
  49. {
  50. struct platform_device_info devinfo = { .name = "cpufreq-dt", };
  51. struct device *cpu_dev;
  52. struct clk *cpu_clk;
  53. struct device_node *np;
  54. int ret;
  55. if ((!of_machine_is_compatible("calxeda,highbank")) &&
  56. (!of_machine_is_compatible("calxeda,ecx-2000")))
  57. return -ENODEV;
  58. cpu_dev = get_cpu_device(0);
  59. if (!cpu_dev) {
  60. pr_err("failed to get highbank cpufreq device\n");
  61. return -ENODEV;
  62. }
  63. np = of_node_get(cpu_dev->of_node);
  64. if (!np) {
  65. pr_err("failed to find highbank cpufreq node\n");
  66. return -ENOENT;
  67. }
  68. cpu_clk = clk_get(cpu_dev, NULL);
  69. if (IS_ERR(cpu_clk)) {
  70. ret = PTR_ERR(cpu_clk);
  71. pr_err("failed to get cpu0 clock: %d\n", ret);
  72. goto out_put_node;
  73. }
  74. ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb);
  75. if (ret) {
  76. pr_err("failed to register clk notifier: %d\n", ret);
  77. goto out_put_node;
  78. }
  79. /* Instantiate cpufreq-dt */
  80. platform_device_register_full(&devinfo);
  81. out_put_node:
  82. of_node_put(np);
  83. return ret;
  84. }
  85. module_init(hb_cpufreq_driver_init);
  86. static const struct of_device_id __maybe_unused hb_cpufreq_of_match[] = {
  87. { .compatible = "calxeda,highbank" },
  88. { .compatible = "calxeda,ecx-2000" },
  89. { },
  90. };
  91. MODULE_DEVICE_TABLE(of, hb_cpufreq_of_match);
  92. MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@calxeda.com>");
  93. MODULE_DESCRIPTION("Calxeda Highbank cpufreq driver");
  94. MODULE_LICENSE("GPL");