highbank-cpufreq.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-cpu0 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/mailbox.h>
  21. #define HB_CPUFREQ_CHANGE_NOTE 0x80000001
  22. #define HB_CPUFREQ_IPC_LEN 7
  23. #define HB_CPUFREQ_VOLT_RETRIES 15
  24. static int hb_voltage_change(unsigned int freq)
  25. {
  26. int i;
  27. u32 msg[HB_CPUFREQ_IPC_LEN];
  28. msg[0] = HB_CPUFREQ_CHANGE_NOTE;
  29. msg[1] = freq / 1000000;
  30. for (i = 2; i < HB_CPUFREQ_IPC_LEN; i++)
  31. msg[i] = 0;
  32. return pl320_ipc_transmit(msg);
  33. }
  34. static int hb_cpufreq_clk_notify(struct notifier_block *nb,
  35. unsigned long action, void *hclk)
  36. {
  37. struct clk_notifier_data *clk_data = hclk;
  38. int i = 0;
  39. if (action == PRE_RATE_CHANGE) {
  40. if (clk_data->new_rate > clk_data->old_rate)
  41. while (hb_voltage_change(clk_data->new_rate))
  42. if (i++ > HB_CPUFREQ_VOLT_RETRIES)
  43. return NOTIFY_BAD;
  44. } else if (action == POST_RATE_CHANGE) {
  45. if (clk_data->new_rate < clk_data->old_rate)
  46. while (hb_voltage_change(clk_data->new_rate))
  47. if (i++ > HB_CPUFREQ_VOLT_RETRIES)
  48. return NOTIFY_BAD;
  49. }
  50. return NOTIFY_DONE;
  51. }
  52. static struct notifier_block hb_cpufreq_clk_nb = {
  53. .notifier_call = hb_cpufreq_clk_notify,
  54. };
  55. static int hb_cpufreq_driver_init(void)
  56. {
  57. struct device *cpu_dev;
  58. struct clk *cpu_clk;
  59. struct device_node *np;
  60. int ret;
  61. if (!of_machine_is_compatible("calxeda,highbank"))
  62. return -ENODEV;
  63. for_each_child_of_node(of_find_node_by_path("/cpus"), np)
  64. if (of_get_property(np, "operating-points", NULL))
  65. break;
  66. if (!np) {
  67. pr_err("failed to find highbank cpufreq node\n");
  68. return -ENOENT;
  69. }
  70. cpu_dev = get_cpu_device(0);
  71. if (!cpu_dev) {
  72. pr_err("failed to get highbank cpufreq device\n");
  73. ret = -ENODEV;
  74. goto out_put_node;
  75. }
  76. cpu_dev->of_node = np;
  77. cpu_clk = clk_get(cpu_dev, NULL);
  78. if (IS_ERR(cpu_clk)) {
  79. ret = PTR_ERR(cpu_clk);
  80. pr_err("failed to get cpu0 clock: %d\n", ret);
  81. goto out_put_node;
  82. }
  83. ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb);
  84. if (ret) {
  85. pr_err("failed to register clk notifier: %d\n", ret);
  86. goto out_put_node;
  87. }
  88. out_put_node:
  89. of_node_put(np);
  90. return ret;
  91. }
  92. module_init(hb_cpufreq_driver_init);
  93. MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@calxeda.com>");
  94. MODULE_DESCRIPTION("Calxeda Highbank cpufreq driver");
  95. MODULE_LICENSE("GPL");