Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Calxeda, Inc. |
| 4 | * |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 5 | * This driver provides the clk notifier callbacks that are used when |
Viresh Kumar | bbcf071 | 2014-09-09 19:58:03 +0530 | [diff] [blame] | 6 | * the cpufreq-dt driver changes to frequency to alert the highbank |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 7 | * EnergyCore Management Engine (ECME) about the need to change |
| 8 | * voltage. The ECME interfaces with the actual voltage regulators. |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/of.h> |
Suman Anna | f2fc42b | 2014-06-12 22:30:34 +0530 | [diff] [blame] | 19 | #include <linux/pl320-ipc.h> |
Shawn Guo | 5553f9e | 2013-01-30 14:27:49 +0000 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 21 | |
| 22 | #define HB_CPUFREQ_CHANGE_NOTE 0x80000001 |
| 23 | #define HB_CPUFREQ_IPC_LEN 7 |
| 24 | #define HB_CPUFREQ_VOLT_RETRIES 15 |
| 25 | |
| 26 | static int hb_voltage_change(unsigned int freq) |
| 27 | { |
Emilio López | f5f43dc | 2013-02-25 11:07:45 +0000 | [diff] [blame] | 28 | u32 msg[HB_CPUFREQ_IPC_LEN] = {HB_CPUFREQ_CHANGE_NOTE, freq / 1000000}; |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 29 | |
| 30 | return pl320_ipc_transmit(msg); |
| 31 | } |
| 32 | |
| 33 | static int hb_cpufreq_clk_notify(struct notifier_block *nb, |
| 34 | unsigned long action, void *hclk) |
| 35 | { |
| 36 | struct clk_notifier_data *clk_data = hclk; |
| 37 | int i = 0; |
| 38 | |
| 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 | |
| 51 | return NOTIFY_DONE; |
| 52 | } |
| 53 | |
| 54 | static struct notifier_block hb_cpufreq_clk_nb = { |
| 55 | .notifier_call = hb_cpufreq_clk_notify, |
| 56 | }; |
| 57 | |
| 58 | static int hb_cpufreq_driver_init(void) |
| 59 | { |
Viresh Kumar | bbcf071 | 2014-09-09 19:58:03 +0530 | [diff] [blame] | 60 | struct platform_device_info devinfo = { .name = "cpufreq-dt", }; |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 61 | struct device *cpu_dev; |
| 62 | struct clk *cpu_clk; |
| 63 | struct device_node *np; |
| 64 | int ret; |
| 65 | |
Mark Langsdorf | fbbc5bf | 2013-10-01 10:30:24 -0500 | [diff] [blame] | 66 | if ((!of_machine_is_compatible("calxeda,highbank")) && |
| 67 | (!of_machine_is_compatible("calxeda,ecx-2000"))) |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 68 | return -ENODEV; |
| 69 | |
Sudeep KarkadaNagesha | 5de6e94 | 2013-06-17 15:07:28 +0100 | [diff] [blame] | 70 | cpu_dev = get_cpu_device(0); |
| 71 | if (!cpu_dev) { |
| 72 | pr_err("failed to get highbank cpufreq device\n"); |
| 73 | return -ENODEV; |
| 74 | } |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 75 | |
Sudeep KarkadaNagesha | 5de6e94 | 2013-06-17 15:07:28 +0100 | [diff] [blame] | 76 | np = of_node_get(cpu_dev->of_node); |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 77 | if (!np) { |
| 78 | pr_err("failed to find highbank cpufreq node\n"); |
| 79 | return -ENOENT; |
| 80 | } |
| 81 | |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 82 | cpu_clk = clk_get(cpu_dev, NULL); |
| 83 | if (IS_ERR(cpu_clk)) { |
| 84 | ret = PTR_ERR(cpu_clk); |
| 85 | pr_err("failed to get cpu0 clock: %d\n", ret); |
| 86 | goto out_put_node; |
| 87 | } |
| 88 | |
| 89 | ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb); |
| 90 | if (ret) { |
| 91 | pr_err("failed to register clk notifier: %d\n", ret); |
| 92 | goto out_put_node; |
| 93 | } |
| 94 | |
Viresh Kumar | bbcf071 | 2014-09-09 19:58:03 +0530 | [diff] [blame] | 95 | /* Instantiate cpufreq-dt */ |
Shawn Guo | 5553f9e | 2013-01-30 14:27:49 +0000 | [diff] [blame] | 96 | platform_device_register_full(&devinfo); |
| 97 | |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 98 | out_put_node: |
| 99 | of_node_put(np); |
| 100 | return ret; |
| 101 | } |
| 102 | module_init(hb_cpufreq_driver_init); |
| 103 | |
Pali Rohár | 9433777 | 2020-11-03 16:11:32 +0100 | [diff] [blame] | 104 | static const struct of_device_id __maybe_unused hb_cpufreq_of_match[] = { |
| 105 | { .compatible = "calxeda,highbank" }, |
| 106 | { .compatible = "calxeda,ecx-2000" }, |
| 107 | { }, |
| 108 | }; |
| 109 | MODULE_DEVICE_TABLE(of, hb_cpufreq_of_match); |
| 110 | |
Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 111 | MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@calxeda.com>"); |
| 112 | MODULE_DESCRIPTION("Calxeda Highbank cpufreq driver"); |
| 113 | MODULE_LICENSE("GPL"); |