| 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 | 
|  | 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 |  | 
|  | 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> | 
|  | 22 | #include <linux/mailbox.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 | { | 
|  | 31 | int i; | 
|  | 32 | u32 msg[HB_CPUFREQ_IPC_LEN]; | 
|  | 33 |  | 
|  | 34 | msg[0] = HB_CPUFREQ_CHANGE_NOTE; | 
|  | 35 | msg[1] = freq / 1000000; | 
|  | 36 | for (i = 2; i < HB_CPUFREQ_IPC_LEN; i++) | 
|  | 37 | msg[i] = 0; | 
|  | 38 |  | 
|  | 39 | return pl320_ipc_transmit(msg); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | static int hb_cpufreq_clk_notify(struct notifier_block *nb, | 
|  | 43 | unsigned long action, void *hclk) | 
|  | 44 | { | 
|  | 45 | struct clk_notifier_data *clk_data = hclk; | 
|  | 46 | int i = 0; | 
|  | 47 |  | 
|  | 48 | if (action == PRE_RATE_CHANGE) { | 
|  | 49 | if (clk_data->new_rate > clk_data->old_rate) | 
|  | 50 | while (hb_voltage_change(clk_data->new_rate)) | 
|  | 51 | if (i++ > HB_CPUFREQ_VOLT_RETRIES) | 
|  | 52 | return NOTIFY_BAD; | 
|  | 53 | } else if (action == POST_RATE_CHANGE) { | 
|  | 54 | if (clk_data->new_rate < clk_data->old_rate) | 
|  | 55 | while (hb_voltage_change(clk_data->new_rate)) | 
|  | 56 | if (i++ > HB_CPUFREQ_VOLT_RETRIES) | 
|  | 57 | return NOTIFY_BAD; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | return NOTIFY_DONE; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | static struct notifier_block hb_cpufreq_clk_nb = { | 
|  | 64 | .notifier_call = hb_cpufreq_clk_notify, | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | static int hb_cpufreq_driver_init(void) | 
|  | 68 | { | 
| Shawn Guo | 5553f9e | 2013-01-30 14:27:49 +0000 | [diff] [blame] | 69 | struct platform_device_info devinfo = { .name = "cpufreq-cpu0", }; | 
| Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 70 | struct device *cpu_dev; | 
|  | 71 | struct clk *cpu_clk; | 
|  | 72 | struct device_node *np; | 
|  | 73 | int ret; | 
|  | 74 |  | 
|  | 75 | if (!of_machine_is_compatible("calxeda,highbank")) | 
|  | 76 | return -ENODEV; | 
|  | 77 |  | 
|  | 78 | for_each_child_of_node(of_find_node_by_path("/cpus"), np) | 
|  | 79 | if (of_get_property(np, "operating-points", NULL)) | 
|  | 80 | break; | 
|  | 81 |  | 
|  | 82 | if (!np) { | 
|  | 83 | pr_err("failed to find highbank cpufreq node\n"); | 
|  | 84 | return -ENOENT; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | cpu_dev = get_cpu_device(0); | 
|  | 88 | if (!cpu_dev) { | 
|  | 89 | pr_err("failed to get highbank cpufreq device\n"); | 
|  | 90 | ret = -ENODEV; | 
|  | 91 | goto out_put_node; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | cpu_dev->of_node = np; | 
|  | 95 |  | 
|  | 96 | cpu_clk = clk_get(cpu_dev, NULL); | 
|  | 97 | if (IS_ERR(cpu_clk)) { | 
|  | 98 | ret = PTR_ERR(cpu_clk); | 
|  | 99 | pr_err("failed to get cpu0 clock: %d\n", ret); | 
|  | 100 | goto out_put_node; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | ret = clk_notifier_register(cpu_clk, &hb_cpufreq_clk_nb); | 
|  | 104 | if (ret) { | 
|  | 105 | pr_err("failed to register clk notifier: %d\n", ret); | 
|  | 106 | goto out_put_node; | 
|  | 107 | } | 
|  | 108 |  | 
| Shawn Guo | 5553f9e | 2013-01-30 14:27:49 +0000 | [diff] [blame] | 109 | /* Instantiate cpufreq-cpu0 */ | 
|  | 110 | platform_device_register_full(&devinfo); | 
|  | 111 |  | 
| Mark Langsdorf | 6754f55 | 2013-01-28 16:13:15 +0000 | [diff] [blame] | 112 | out_put_node: | 
|  | 113 | of_node_put(np); | 
|  | 114 | return ret; | 
|  | 115 | } | 
|  | 116 | module_init(hb_cpufreq_driver_init); | 
|  | 117 |  | 
|  | 118 | MODULE_AUTHOR("Mark Langsdorf <mark.langsdorf@calxeda.com>"); | 
|  | 119 | MODULE_DESCRIPTION("Calxeda Highbank cpufreq driver"); | 
|  | 120 | MODULE_LICENSE("GPL"); |