highbank-cpufreq.c 3.0 KB

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