Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 17 | #include <linux/mutex.h> |
| 18 | #include <linux/msm_tsens.h> |
| 19 | #include <linux/workqueue.h> |
Eugene Seah | 7d6d273 | 2012-03-09 17:48:42 -0700 | [diff] [blame] | 20 | #include <linux/cpu.h> |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 21 | #include <linux/cpufreq.h> |
| 22 | #include <linux/msm_tsens.h> |
| 23 | #include <linux/msm_thermal.h> |
| 24 | #include <mach/cpufreq.h> |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 25 | |
| 26 | static int enabled; |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 27 | static struct msm_thermal_data msm_thermal_info; |
| 28 | static uint32_t limited_max_freq = MSM_CPUFREQ_NO_LIMIT; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 29 | static struct delayed_work check_temp_work; |
| 30 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 31 | static int update_cpu_max_freq(int cpu, uint32_t max_freq) |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 32 | { |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 33 | int ret = 0; |
| 34 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 35 | ret = msm_cpufreq_set_freq_limits(cpu, MSM_CPUFREQ_NO_LIMIT, max_freq); |
| 36 | if (ret) |
| 37 | return ret; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 38 | |
| 39 | ret = cpufreq_update_policy(cpu); |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 40 | if (ret) |
| 41 | return ret; |
| 42 | |
| 43 | limited_max_freq = max_freq; |
| 44 | if (max_freq != MSM_CPUFREQ_NO_LIMIT) |
| 45 | pr_info("msm_thermal: Limiting cpu%d max frequency to %d\n", |
| 46 | cpu, max_freq); |
| 47 | else |
| 48 | pr_info("msm_thermal: Max frequency reset for cpu%d\n", cpu); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | static void check_temp(struct work_struct *work) |
| 54 | { |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 55 | struct tsens_device tsens_dev; |
| 56 | unsigned long temp = 0; |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 57 | uint32_t max_freq = limited_max_freq; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 58 | int cpu = 0; |
| 59 | int ret = 0; |
| 60 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 61 | tsens_dev.sensor_num = msm_thermal_info.sensor_id; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 62 | ret = tsens_get_temp(&tsens_dev, &temp); |
| 63 | if (ret) { |
Jeff Ohlstein | f744c86 | 2012-02-01 17:52:44 -0800 | [diff] [blame] | 64 | pr_debug("msm_thermal: Unable to read TSENS sensor %d\n", |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 65 | tsens_dev.sensor_num); |
| 66 | goto reschedule; |
| 67 | } |
| 68 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 69 | if (temp >= msm_thermal_info.limit_temp) |
| 70 | max_freq = msm_thermal_info.limit_freq; |
| 71 | else if (temp < |
| 72 | msm_thermal_info.limit_temp - msm_thermal_info.temp_hysteresis) |
| 73 | max_freq = MSM_CPUFREQ_NO_LIMIT; |
| 74 | |
| 75 | if (max_freq == limited_max_freq) |
| 76 | goto reschedule; |
| 77 | |
| 78 | /* Update new limits */ |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 79 | for_each_possible_cpu(cpu) { |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 80 | ret = update_cpu_max_freq(cpu, max_freq); |
| 81 | if (ret) |
| 82 | pr_debug("Unable to limit cpu%d max freq to %d\n", |
| 83 | cpu, max_freq); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | reschedule: |
| 87 | if (enabled) |
| 88 | schedule_delayed_work(&check_temp_work, |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 89 | msecs_to_jiffies(msm_thermal_info.poll_ms)); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 92 | static void disable_msm_thermal(void) |
| 93 | { |
| 94 | int cpu = 0; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 95 | |
Eugene Seah | cbc0753 | 2012-04-11 19:32:27 -0600 | [diff] [blame] | 96 | /* make sure check_temp is no longer running */ |
| 97 | cancel_delayed_work(&check_temp_work); |
| 98 | flush_scheduled_work(); |
| 99 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 100 | if (limited_max_freq == MSM_CPUFREQ_NO_LIMIT) |
| 101 | return; |
| 102 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 103 | for_each_possible_cpu(cpu) { |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 104 | update_cpu_max_freq(cpu, MSM_CPUFREQ_NO_LIMIT); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 105 | } |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int set_enabled(const char *val, const struct kernel_param *kp) |
| 109 | { |
| 110 | int ret = 0; |
| 111 | |
| 112 | ret = param_set_bool(val, kp); |
| 113 | if (!enabled) |
| 114 | disable_msm_thermal(); |
| 115 | else |
| 116 | pr_info("msm_thermal: no action for enabled = %d\n", enabled); |
| 117 | |
| 118 | pr_info("msm_thermal: enabled = %d\n", enabled); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static struct kernel_param_ops module_ops = { |
| 124 | .set = set_enabled, |
| 125 | .get = param_get_bool, |
| 126 | }; |
| 127 | |
| 128 | module_param_cb(enabled, &module_ops, &enabled, 0644); |
| 129 | MODULE_PARM_DESC(enabled, "enforce thermal limit on cpu"); |
| 130 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 131 | int __init msm_thermal_init(struct msm_thermal_data *pdata) |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 132 | { |
| 133 | int ret = 0; |
| 134 | |
Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame^] | 135 | BUG_ON(!pdata); |
| 136 | BUG_ON(pdata->sensor_id >= TSENS_MAX_SENSORS); |
| 137 | memcpy(&msm_thermal_info, pdata, sizeof(struct msm_thermal_data)); |
| 138 | |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 139 | enabled = 1; |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 140 | INIT_DELAYED_WORK(&check_temp_work, check_temp); |
Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 141 | schedule_delayed_work(&check_temp_work, 0); |
| 142 | |
| 143 | return ret; |
| 144 | } |