| Duy Truong | e833aca | 2013-02-12 13:35:08 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 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> | 
| Eugene Seah | b77b0c4 | 2012-07-02 19:28:50 -0600 | [diff] [blame] | 24 | #include <linux/platform_device.h> | 
 | 25 | #include <linux/of.h> | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 26 | #include <mach/cpufreq.h> | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 27 |  | 
 | 28 | static int enabled; | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 29 | static struct msm_thermal_data msm_thermal_info; | 
 | 30 | static uint32_t limited_max_freq = MSM_CPUFREQ_NO_LIMIT; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 31 | static struct delayed_work check_temp_work; | 
 | 32 |  | 
| Eugene Seah | 2ee4a5d | 2012-06-25 18:16:41 -0600 | [diff] [blame] | 33 | static int limit_idx; | 
 | 34 | static int limit_idx_low; | 
 | 35 | static int limit_idx_high; | 
 | 36 | static struct cpufreq_frequency_table *table; | 
 | 37 |  | 
 | 38 | static int msm_thermal_get_freq_table(void) | 
 | 39 | { | 
 | 40 | 	int ret = 0; | 
 | 41 | 	int i = 0; | 
 | 42 |  | 
 | 43 | 	table = cpufreq_frequency_get_table(0); | 
 | 44 | 	if (table == NULL) { | 
 | 45 | 		pr_debug("%s: error reading cpufreq table\n", __func__); | 
 | 46 | 		ret = -EINVAL; | 
 | 47 | 		goto fail; | 
 | 48 | 	} | 
 | 49 |  | 
 | 50 | 	while (table[i].frequency != CPUFREQ_TABLE_END) | 
 | 51 | 		i++; | 
 | 52 |  | 
 | 53 | 	limit_idx_low = 0; | 
 | 54 | 	limit_idx_high = limit_idx = i - 1; | 
 | 55 | 	BUG_ON(limit_idx_high <= 0 || limit_idx_high <= limit_idx_low); | 
 | 56 | fail: | 
 | 57 | 	return ret; | 
 | 58 | } | 
 | 59 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 60 | static int update_cpu_max_freq(int cpu, uint32_t max_freq) | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 61 | { | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 62 | 	int ret = 0; | 
 | 63 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 64 | 	ret = msm_cpufreq_set_freq_limits(cpu, MSM_CPUFREQ_NO_LIMIT, max_freq); | 
 | 65 | 	if (ret) | 
 | 66 | 		return ret; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 67 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 68 | 	limited_max_freq = max_freq; | 
 | 69 | 	if (max_freq != MSM_CPUFREQ_NO_LIMIT) | 
 | 70 | 		pr_info("msm_thermal: Limiting cpu%d max frequency to %d\n", | 
 | 71 | 				cpu, max_freq); | 
 | 72 | 	else | 
 | 73 | 		pr_info("msm_thermal: Max frequency reset for cpu%d\n", cpu); | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 74 |  | 
| Eugene Seah | 2ee4a5d | 2012-06-25 18:16:41 -0600 | [diff] [blame] | 75 | 	ret = cpufreq_update_policy(cpu); | 
 | 76 |  | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 77 | 	return ret; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | static void check_temp(struct work_struct *work) | 
 | 81 | { | 
| Eugene Seah | 2ee4a5d | 2012-06-25 18:16:41 -0600 | [diff] [blame] | 82 | 	static int limit_init; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 83 | 	struct tsens_device tsens_dev; | 
 | 84 | 	unsigned long temp = 0; | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 85 | 	uint32_t max_freq = limited_max_freq; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 86 | 	int cpu = 0; | 
 | 87 | 	int ret = 0; | 
 | 88 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 89 | 	tsens_dev.sensor_num = msm_thermal_info.sensor_id; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 90 | 	ret = tsens_get_temp(&tsens_dev, &temp); | 
 | 91 | 	if (ret) { | 
| Jeff Ohlstein | f744c86 | 2012-02-01 17:52:44 -0800 | [diff] [blame] | 92 | 		pr_debug("msm_thermal: Unable to read TSENS sensor %d\n", | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 93 | 				tsens_dev.sensor_num); | 
 | 94 | 		goto reschedule; | 
 | 95 | 	} | 
 | 96 |  | 
| Eugene Seah | 2ee4a5d | 2012-06-25 18:16:41 -0600 | [diff] [blame] | 97 | 	if (!limit_init) { | 
 | 98 | 		ret = msm_thermal_get_freq_table(); | 
 | 99 | 		if (ret) | 
 | 100 | 			goto reschedule; | 
 | 101 | 		else | 
 | 102 | 			limit_init = 1; | 
 | 103 | 	} | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 104 |  | 
| Eugene Seah | 2ee4a5d | 2012-06-25 18:16:41 -0600 | [diff] [blame] | 105 | 	if (temp >= msm_thermal_info.limit_temp_degC) { | 
 | 106 | 		if (limit_idx == limit_idx_low) | 
 | 107 | 			goto reschedule; | 
 | 108 |  | 
 | 109 | 		limit_idx -= msm_thermal_info.freq_step; | 
 | 110 | 		if (limit_idx < limit_idx_low) | 
 | 111 | 			limit_idx = limit_idx_low; | 
 | 112 | 		max_freq = table[limit_idx].frequency; | 
 | 113 | 	} else if (temp < msm_thermal_info.limit_temp_degC - | 
 | 114 | 		 msm_thermal_info.temp_hysteresis_degC) { | 
 | 115 | 		if (limit_idx == limit_idx_high) | 
 | 116 | 			goto reschedule; | 
 | 117 |  | 
 | 118 | 		limit_idx += msm_thermal_info.freq_step; | 
 | 119 | 		if (limit_idx >= limit_idx_high) { | 
 | 120 | 			limit_idx = limit_idx_high; | 
 | 121 | 			max_freq = MSM_CPUFREQ_NO_LIMIT; | 
 | 122 | 		} else | 
 | 123 | 			max_freq = table[limit_idx].frequency; | 
 | 124 | 	} | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 125 | 	if (max_freq == limited_max_freq) | 
 | 126 | 		goto reschedule; | 
 | 127 |  | 
 | 128 | 	/* Update new limits */ | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 129 | 	for_each_possible_cpu(cpu) { | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 130 | 		ret = update_cpu_max_freq(cpu, max_freq); | 
 | 131 | 		if (ret) | 
 | 132 | 			pr_debug("Unable to limit cpu%d max freq to %d\n", | 
 | 133 | 					cpu, max_freq); | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 134 | 	} | 
 | 135 |  | 
 | 136 | reschedule: | 
 | 137 | 	if (enabled) | 
 | 138 | 		schedule_delayed_work(&check_temp_work, | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 139 | 				msecs_to_jiffies(msm_thermal_info.poll_ms)); | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 140 | } | 
 | 141 |  | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 142 | static void disable_msm_thermal(void) | 
 | 143 | { | 
 | 144 | 	int cpu = 0; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 145 |  | 
| Eugene Seah | cbc0753 | 2012-04-11 19:32:27 -0600 | [diff] [blame] | 146 | 	/* make sure check_temp is no longer running */ | 
 | 147 | 	cancel_delayed_work(&check_temp_work); | 
 | 148 | 	flush_scheduled_work(); | 
 | 149 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 150 | 	if (limited_max_freq == MSM_CPUFREQ_NO_LIMIT) | 
 | 151 | 		return; | 
 | 152 |  | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 153 | 	for_each_possible_cpu(cpu) { | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 154 | 		update_cpu_max_freq(cpu, MSM_CPUFREQ_NO_LIMIT); | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 155 | 	} | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 156 | } | 
 | 157 |  | 
 | 158 | static int set_enabled(const char *val, const struct kernel_param *kp) | 
 | 159 | { | 
 | 160 | 	int ret = 0; | 
 | 161 |  | 
 | 162 | 	ret = param_set_bool(val, kp); | 
 | 163 | 	if (!enabled) | 
 | 164 | 		disable_msm_thermal(); | 
 | 165 | 	else | 
 | 166 | 		pr_info("msm_thermal: no action for enabled = %d\n", enabled); | 
 | 167 |  | 
 | 168 | 	pr_info("msm_thermal: enabled = %d\n", enabled); | 
 | 169 |  | 
 | 170 | 	return ret; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | static struct kernel_param_ops module_ops = { | 
 | 174 | 	.set = set_enabled, | 
 | 175 | 	.get = param_get_bool, | 
 | 176 | }; | 
 | 177 |  | 
 | 178 | module_param_cb(enabled, &module_ops, &enabled, 0644); | 
 | 179 | MODULE_PARM_DESC(enabled, "enforce thermal limit on cpu"); | 
 | 180 |  | 
| Eugene Seah | b77b0c4 | 2012-07-02 19:28:50 -0600 | [diff] [blame] | 181 | int __devinit msm_thermal_init(struct msm_thermal_data *pdata) | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 182 | { | 
 | 183 | 	int ret = 0; | 
 | 184 |  | 
| Praveen Chidambaram | 9181436 | 2012-05-25 17:36:07 -0600 | [diff] [blame] | 185 | 	BUG_ON(!pdata); | 
 | 186 | 	BUG_ON(pdata->sensor_id >= TSENS_MAX_SENSORS); | 
 | 187 | 	memcpy(&msm_thermal_info, pdata, sizeof(struct msm_thermal_data)); | 
 | 188 |  | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 189 | 	enabled = 1; | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 190 | 	INIT_DELAYED_WORK(&check_temp_work, check_temp); | 
| Praveen Chidambaram | f248bb7 | 2012-01-20 11:38:44 -0700 | [diff] [blame] | 191 | 	schedule_delayed_work(&check_temp_work, 0); | 
 | 192 |  | 
 | 193 | 	return ret; | 
 | 194 | } | 
| Eugene Seah | b77b0c4 | 2012-07-02 19:28:50 -0600 | [diff] [blame] | 195 |  | 
 | 196 | static int __devinit msm_thermal_dev_probe(struct platform_device *pdev) | 
 | 197 | { | 
 | 198 | 	int ret = 0; | 
 | 199 | 	char *key = NULL; | 
 | 200 | 	struct device_node *node = pdev->dev.of_node; | 
 | 201 | 	struct msm_thermal_data data; | 
 | 202 |  | 
 | 203 | 	memset(&data, 0, sizeof(struct msm_thermal_data)); | 
 | 204 | 	key = "qcom,sensor-id"; | 
 | 205 | 	ret = of_property_read_u32(node, key, &data.sensor_id); | 
 | 206 | 	if (ret) | 
 | 207 | 		goto fail; | 
 | 208 | 	WARN_ON(data.sensor_id >= TSENS_MAX_SENSORS); | 
 | 209 |  | 
 | 210 | 	key = "qcom,poll-ms"; | 
 | 211 | 	ret = of_property_read_u32(node, key, &data.poll_ms); | 
 | 212 | 	if (ret) | 
 | 213 | 		goto fail; | 
 | 214 |  | 
 | 215 | 	key = "qcom,limit-temp"; | 
 | 216 | 	ret = of_property_read_u32(node, key, &data.limit_temp_degC); | 
 | 217 | 	if (ret) | 
 | 218 | 		goto fail; | 
 | 219 |  | 
 | 220 | 	key = "qcom,temp-hysteresis"; | 
 | 221 | 	ret = of_property_read_u32(node, key, &data.temp_hysteresis_degC); | 
 | 222 | 	if (ret) | 
 | 223 | 		goto fail; | 
 | 224 |  | 
 | 225 | 	key = "qcom,freq-step"; | 
 | 226 | 	ret = of_property_read_u32(node, key, &data.freq_step); | 
 | 227 |  | 
 | 228 | fail: | 
 | 229 | 	if (ret) | 
 | 230 | 		pr_err("%s: Failed reading node=%s, key=%s\n", | 
 | 231 | 		       __func__, node->full_name, key); | 
 | 232 | 	else | 
 | 233 | 		ret = msm_thermal_init(&data); | 
 | 234 |  | 
 | 235 | 	return ret; | 
 | 236 | } | 
 | 237 |  | 
 | 238 | static struct of_device_id msm_thermal_match_table[] = { | 
 | 239 | 	{.compatible = "qcom,msm-thermal"}, | 
 | 240 | 	{}, | 
 | 241 | }; | 
 | 242 |  | 
 | 243 | static struct platform_driver msm_thermal_device_driver = { | 
 | 244 | 	.probe = msm_thermal_dev_probe, | 
 | 245 | 	.driver = { | 
 | 246 | 		.name = "msm-thermal", | 
 | 247 | 		.owner = THIS_MODULE, | 
 | 248 | 		.of_match_table = msm_thermal_match_table, | 
 | 249 | 	}, | 
 | 250 | }; | 
 | 251 |  | 
 | 252 | int __init msm_thermal_device_init(void) | 
 | 253 | { | 
 | 254 | 	return platform_driver_register(&msm_thermal_device_driver); | 
 | 255 | } |