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