Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Freescale Semiconductor, 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 | */ |
| 9 | |
Anson Huang | 329fe7b | 2013-12-23 15:49:22 -0500 | [diff] [blame] | 10 | #include <linux/clk.h> |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 11 | #include <linux/cpu_cooling.h> |
| 12 | #include <linux/cpufreq.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/init.h> |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mfd/syscon.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 22 | #include <linux/of_device.h> |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/regmap.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/thermal.h> |
| 27 | #include <linux/types.h> |
| 28 | |
| 29 | #define REG_SET 0x4 |
| 30 | #define REG_CLR 0x8 |
| 31 | #define REG_TOG 0xc |
| 32 | |
| 33 | #define MISC0 0x0150 |
| 34 | #define MISC0_REFTOP_SELBIASOFF (1 << 3) |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 35 | #define MISC1 0x0160 |
| 36 | #define MISC1_IRQ_TEMPHIGH (1 << 29) |
| 37 | /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */ |
| 38 | #define MISC1_IRQ_TEMPLOW (1 << 28) |
| 39 | #define MISC1_IRQ_TEMPPANIC (1 << 27) |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 40 | |
| 41 | #define TEMPSENSE0 0x0180 |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 42 | #define TEMPSENSE0_ALARM_VALUE_SHIFT 20 |
| 43 | #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT) |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 44 | #define TEMPSENSE0_TEMP_CNT_SHIFT 8 |
| 45 | #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT) |
| 46 | #define TEMPSENSE0_FINISHED (1 << 2) |
| 47 | #define TEMPSENSE0_MEASURE_TEMP (1 << 1) |
| 48 | #define TEMPSENSE0_POWER_DOWN (1 << 0) |
| 49 | |
| 50 | #define TEMPSENSE1 0x0190 |
| 51 | #define TEMPSENSE1_MEASURE_FREQ 0xffff |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 52 | /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */ |
| 53 | #define TEMPSENSE2 0x0290 |
| 54 | #define TEMPSENSE2_LOW_VALUE_SHIFT 0 |
| 55 | #define TEMPSENSE2_LOW_VALUE_MASK 0xfff |
| 56 | #define TEMPSENSE2_PANIC_VALUE_SHIFT 16 |
| 57 | #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000 |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 58 | |
| 59 | #define OCOTP_ANA1 0x04e0 |
| 60 | |
| 61 | /* The driver supports 1 passive trip point and 1 critical trip point */ |
| 62 | enum imx_thermal_trip { |
| 63 | IMX_TRIP_PASSIVE, |
| 64 | IMX_TRIP_CRITICAL, |
| 65 | IMX_TRIP_NUM, |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * It defines the temperature in millicelsius for passive trip point |
| 70 | * that will trigger cooling action when crossed. |
| 71 | */ |
| 72 | #define IMX_TEMP_PASSIVE 85000 |
| 73 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 74 | #define IMX_POLLING_DELAY 2000 /* millisecond */ |
| 75 | #define IMX_PASSIVE_DELAY 1000 |
| 76 | |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 77 | #define FACTOR0 10000000 |
| 78 | #define FACTOR1 15976 |
| 79 | #define FACTOR2 4297157 |
| 80 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 81 | #define TEMPMON_IMX6Q 1 |
| 82 | #define TEMPMON_IMX6SX 2 |
| 83 | |
| 84 | struct thermal_soc_data { |
| 85 | u32 version; |
| 86 | }; |
| 87 | |
| 88 | static struct thermal_soc_data thermal_imx6q_data = { |
| 89 | .version = TEMPMON_IMX6Q, |
| 90 | }; |
| 91 | |
| 92 | static struct thermal_soc_data thermal_imx6sx_data = { |
| 93 | .version = TEMPMON_IMX6SX, |
| 94 | }; |
| 95 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 96 | struct imx_thermal_data { |
| 97 | struct thermal_zone_device *tz; |
| 98 | struct thermal_cooling_device *cdev; |
| 99 | enum thermal_device_mode mode; |
| 100 | struct regmap *tempmon; |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 101 | u32 c1, c2; /* See formula in imx_get_sensor_data() */ |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 102 | unsigned long temp_passive; |
| 103 | unsigned long temp_critical; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 104 | unsigned long alarm_temp; |
| 105 | unsigned long last_temp; |
| 106 | bool irq_enabled; |
| 107 | int irq; |
Anson Huang | 329fe7b | 2013-12-23 15:49:22 -0500 | [diff] [blame] | 108 | struct clk *thermal_clk; |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 109 | const struct thermal_soc_data *socdata; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 112 | static void imx_set_panic_temp(struct imx_thermal_data *data, |
| 113 | signed long panic_temp) |
| 114 | { |
| 115 | struct regmap *map = data->tempmon; |
| 116 | int critical_value; |
| 117 | |
| 118 | critical_value = (data->c2 - panic_temp) / data->c1; |
| 119 | regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK); |
| 120 | regmap_write(map, TEMPSENSE2 + REG_SET, critical_value << |
| 121 | TEMPSENSE2_PANIC_VALUE_SHIFT); |
| 122 | } |
| 123 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 124 | static void imx_set_alarm_temp(struct imx_thermal_data *data, |
| 125 | signed long alarm_temp) |
| 126 | { |
| 127 | struct regmap *map = data->tempmon; |
| 128 | int alarm_value; |
| 129 | |
| 130 | data->alarm_temp = alarm_temp; |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 131 | alarm_value = (data->c2 - alarm_temp) / data->c1; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 132 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK); |
| 133 | regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value << |
| 134 | TEMPSENSE0_ALARM_VALUE_SHIFT); |
| 135 | } |
| 136 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 137 | static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp) |
| 138 | { |
| 139 | struct imx_thermal_data *data = tz->devdata; |
| 140 | struct regmap *map = data->tempmon; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 141 | unsigned int n_meas; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 142 | bool wait; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 143 | u32 val; |
| 144 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 145 | if (data->mode == THERMAL_DEVICE_ENABLED) { |
| 146 | /* Check if a measurement is currently in progress */ |
| 147 | regmap_read(map, TEMPSENSE0, &val); |
| 148 | wait = !(val & TEMPSENSE0_FINISHED); |
| 149 | } else { |
| 150 | /* |
| 151 | * Every time we measure the temperature, we will power on the |
| 152 | * temperature sensor, enable measurements, take a reading, |
| 153 | * disable measurements, power off the temperature sensor. |
| 154 | */ |
| 155 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 156 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 157 | |
| 158 | wait = true; |
| 159 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * According to the temp sensor designers, it may require up to ~17us |
| 163 | * to complete a measurement. |
| 164 | */ |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 165 | if (wait) |
| 166 | usleep_range(20, 50); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 167 | |
| 168 | regmap_read(map, TEMPSENSE0, &val); |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 169 | |
| 170 | if (data->mode != THERMAL_DEVICE_ENABLED) { |
| 171 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 172 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 173 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 174 | |
| 175 | if ((val & TEMPSENSE0_FINISHED) == 0) { |
| 176 | dev_dbg(&tz->device, "temp measurement never finished\n"); |
| 177 | return -EAGAIN; |
| 178 | } |
| 179 | |
| 180 | n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT; |
| 181 | |
| 182 | /* See imx_get_sensor_data() for formula derivation */ |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 183 | *temp = data->c2 - n_meas * data->c1; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 184 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 185 | /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */ |
| 186 | if (data->socdata->version == TEMPMON_IMX6Q) { |
| 187 | if (data->alarm_temp == data->temp_passive && |
| 188 | *temp >= data->temp_passive) |
| 189 | imx_set_alarm_temp(data, data->temp_critical); |
| 190 | if (data->alarm_temp == data->temp_critical && |
| 191 | *temp < data->temp_passive) { |
| 192 | imx_set_alarm_temp(data, data->temp_passive); |
| 193 | dev_dbg(&tz->device, "thermal alarm off: T < %lu\n", |
| 194 | data->alarm_temp / 1000); |
| 195 | } |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (*temp != data->last_temp) { |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 199 | dev_dbg(&tz->device, "millicelsius: %ld\n", *temp); |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 200 | data->last_temp = *temp; |
| 201 | } |
| 202 | |
| 203 | /* Reenable alarm IRQ if temperature below alarm temperature */ |
| 204 | if (!data->irq_enabled && *temp < data->alarm_temp) { |
| 205 | data->irq_enabled = true; |
| 206 | enable_irq(data->irq); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int imx_get_mode(struct thermal_zone_device *tz, |
| 213 | enum thermal_device_mode *mode) |
| 214 | { |
| 215 | struct imx_thermal_data *data = tz->devdata; |
| 216 | |
| 217 | *mode = data->mode; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int imx_set_mode(struct thermal_zone_device *tz, |
| 223 | enum thermal_device_mode mode) |
| 224 | { |
| 225 | struct imx_thermal_data *data = tz->devdata; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 226 | struct regmap *map = data->tempmon; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 227 | |
| 228 | if (mode == THERMAL_DEVICE_ENABLED) { |
| 229 | tz->polling_delay = IMX_POLLING_DELAY; |
| 230 | tz->passive_delay = IMX_PASSIVE_DELAY; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 231 | |
| 232 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 233 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 234 | |
| 235 | if (!data->irq_enabled) { |
| 236 | data->irq_enabled = true; |
| 237 | enable_irq(data->irq); |
| 238 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 239 | } else { |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 240 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 241 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 242 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 243 | tz->polling_delay = 0; |
| 244 | tz->passive_delay = 0; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 245 | |
| 246 | if (data->irq_enabled) { |
| 247 | disable_irq(data->irq); |
| 248 | data->irq_enabled = false; |
| 249 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | data->mode = mode; |
| 253 | thermal_zone_device_update(tz); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int imx_get_trip_type(struct thermal_zone_device *tz, int trip, |
| 259 | enum thermal_trip_type *type) |
| 260 | { |
| 261 | *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE : |
| 262 | THERMAL_TRIP_CRITICAL; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int imx_get_crit_temp(struct thermal_zone_device *tz, |
| 267 | unsigned long *temp) |
| 268 | { |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 269 | struct imx_thermal_data *data = tz->devdata; |
| 270 | |
| 271 | *temp = data->temp_critical; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip, |
| 276 | unsigned long *temp) |
| 277 | { |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 278 | struct imx_thermal_data *data = tz->devdata; |
| 279 | |
| 280 | *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive : |
| 281 | data->temp_critical; |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip, |
| 286 | unsigned long temp) |
| 287 | { |
| 288 | struct imx_thermal_data *data = tz->devdata; |
| 289 | |
| 290 | if (trip == IMX_TRIP_CRITICAL) |
| 291 | return -EPERM; |
| 292 | |
| 293 | if (temp > IMX_TEMP_PASSIVE) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | data->temp_passive = temp; |
| 297 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 298 | imx_set_alarm_temp(data, temp); |
| 299 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int imx_bind(struct thermal_zone_device *tz, |
| 304 | struct thermal_cooling_device *cdev) |
| 305 | { |
| 306 | int ret; |
| 307 | |
| 308 | ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev, |
| 309 | THERMAL_NO_LIMIT, |
| 310 | THERMAL_NO_LIMIT); |
| 311 | if (ret) { |
| 312 | dev_err(&tz->device, |
| 313 | "binding zone %s with cdev %s failed:%d\n", |
| 314 | tz->type, cdev->type, ret); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int imx_unbind(struct thermal_zone_device *tz, |
| 322 | struct thermal_cooling_device *cdev) |
| 323 | { |
| 324 | int ret; |
| 325 | |
| 326 | ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev); |
| 327 | if (ret) { |
| 328 | dev_err(&tz->device, |
| 329 | "unbinding zone %s with cdev %s failed:%d\n", |
| 330 | tz->type, cdev->type, ret); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
Eduardo Valentin | cbb07bb | 2014-01-06 08:54:34 -0400 | [diff] [blame] | 337 | static struct thermal_zone_device_ops imx_tz_ops = { |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 338 | .bind = imx_bind, |
| 339 | .unbind = imx_unbind, |
| 340 | .get_temp = imx_get_temp, |
| 341 | .get_mode = imx_get_mode, |
| 342 | .set_mode = imx_set_mode, |
| 343 | .get_trip_type = imx_get_trip_type, |
| 344 | .get_trip_temp = imx_get_trip_temp, |
| 345 | .get_crit_temp = imx_get_crit_temp, |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 346 | .set_trip_temp = imx_set_trip_temp, |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | static int imx_get_sensor_data(struct platform_device *pdev) |
| 350 | { |
| 351 | struct imx_thermal_data *data = platform_get_drvdata(pdev); |
| 352 | struct regmap *map; |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 353 | int t1, n1; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 354 | int ret; |
| 355 | u32 val; |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 356 | u64 temp64; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 357 | |
| 358 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
| 359 | "fsl,tempmon-data"); |
| 360 | if (IS_ERR(map)) { |
| 361 | ret = PTR_ERR(map); |
| 362 | dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret); |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | ret = regmap_read(map, OCOTP_ANA1, &val); |
| 367 | if (ret) { |
| 368 | dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | if (val == 0 || val == ~0) { |
| 373 | dev_err(&pdev->dev, "invalid sensor calibration data\n"); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Sensor data layout: |
| 379 | * [31:20] - sensor value @ 25C |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 380 | * Use universal formula now and only need sensor value @ 25C |
| 381 | * slope = 0.4297157 - (0.0015976 * 25C fuse) |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 382 | */ |
| 383 | n1 = val >> 20; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 384 | t1 = 25; /* t1 always 25C */ |
| 385 | |
| 386 | /* |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 387 | * Derived from linear interpolation: |
| 388 | * slope = 0.4297157 - (0.0015976 * 25C fuse) |
| 389 | * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0 |
| 390 | * (Nmeas - n1) / (Tmeas - t1) = slope |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 391 | * We want to reduce this down to the minimum computation necessary |
| 392 | * for each temperature read. Also, we want Tmeas in millicelsius |
| 393 | * and we don't want to lose precision from integer division. So... |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 394 | * Tmeas = (Nmeas - n1) / slope + t1 |
| 395 | * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1 |
| 396 | * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1 |
| 397 | * Let constant c1 = (-1000 / slope) |
| 398 | * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1 |
| 399 | * Let constant c2 = n1 *c1 + 1000 * t1 |
| 400 | * milli_Tmeas = c2 - Nmeas * c1 |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 401 | */ |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 402 | temp64 = FACTOR0; |
| 403 | temp64 *= 1000; |
| 404 | do_div(temp64, FACTOR1 * n1 - FACTOR2); |
| 405 | data->c1 = temp64; |
| 406 | data->c2 = n1 * data->c1 + 1000 * t1; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 407 | |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 408 | /* |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 409 | * Set the default passive cooling trip point, |
| 410 | * can be changed from userspace. |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 411 | */ |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 412 | data->temp_passive = IMX_TEMP_PASSIVE; |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 415 | * The maximum die temperature set to 20 C higher than |
| 416 | * IMX_TEMP_PASSIVE. |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 417 | */ |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 418 | data->temp_critical = 1000 * 20 + data->temp_passive; |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 419 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 423 | static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev) |
| 424 | { |
| 425 | struct imx_thermal_data *data = dev; |
| 426 | |
| 427 | disable_irq_nosync(irq); |
| 428 | data->irq_enabled = false; |
| 429 | |
| 430 | return IRQ_WAKE_THREAD; |
| 431 | } |
| 432 | |
| 433 | static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev) |
| 434 | { |
| 435 | struct imx_thermal_data *data = dev; |
| 436 | |
| 437 | dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n", |
| 438 | data->alarm_temp / 1000); |
| 439 | |
| 440 | thermal_zone_device_update(data->tz); |
| 441 | |
| 442 | return IRQ_HANDLED; |
| 443 | } |
| 444 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 445 | static const struct of_device_id of_imx_thermal_match[] = { |
| 446 | { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, }, |
| 447 | { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, }, |
| 448 | { /* end */ } |
| 449 | }; |
| 450 | MODULE_DEVICE_TABLE(of, of_imx_thermal_match); |
| 451 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 452 | static int imx_thermal_probe(struct platform_device *pdev) |
| 453 | { |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 454 | const struct of_device_id *of_id = |
| 455 | of_match_device(of_imx_thermal_match, &pdev->dev); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 456 | struct imx_thermal_data *data; |
| 457 | struct cpumask clip_cpus; |
| 458 | struct regmap *map; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 459 | int measure_freq; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 460 | int ret; |
| 461 | |
Bai Ping | 491b079 | 2014-10-14 13:12:07 +0800 | [diff] [blame] | 462 | if (!cpufreq_get_current_driver()) { |
| 463 | dev_dbg(&pdev->dev, "no cpufreq driver!"); |
| 464 | return -EPROBE_DEFER; |
| 465 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 466 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 467 | if (!data) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon"); |
| 471 | if (IS_ERR(map)) { |
| 472 | ret = PTR_ERR(map); |
| 473 | dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret); |
| 474 | return ret; |
| 475 | } |
| 476 | data->tempmon = map; |
| 477 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 478 | data->socdata = of_id->data; |
| 479 | |
| 480 | /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */ |
| 481 | if (data->socdata->version == TEMPMON_IMX6SX) { |
| 482 | regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH | |
| 483 | MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC); |
| 484 | /* |
| 485 | * reset value of LOW ALARM is incorrect, set it to lowest |
| 486 | * value to avoid false trigger of low alarm. |
| 487 | */ |
| 488 | regmap_write(map, TEMPSENSE2 + REG_SET, |
| 489 | TEMPSENSE2_LOW_VALUE_MASK); |
| 490 | } |
| 491 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 492 | data->irq = platform_get_irq(pdev, 0); |
| 493 | if (data->irq < 0) |
| 494 | return data->irq; |
| 495 | |
| 496 | ret = devm_request_threaded_irq(&pdev->dev, data->irq, |
| 497 | imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread, |
| 498 | 0, "imx_thermal", data); |
| 499 | if (ret < 0) { |
| 500 | dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret); |
| 501 | return ret; |
| 502 | } |
| 503 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 504 | platform_set_drvdata(pdev, data); |
| 505 | |
| 506 | ret = imx_get_sensor_data(pdev); |
| 507 | if (ret) { |
| 508 | dev_err(&pdev->dev, "failed to get sensor data\n"); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /* Make sure sensor is in known good state for measurements */ |
| 513 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 514 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 515 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); |
| 516 | regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF); |
| 517 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 518 | |
| 519 | cpumask_set_cpu(0, &clip_cpus); |
| 520 | data->cdev = cpufreq_cooling_register(&clip_cpus); |
| 521 | if (IS_ERR(data->cdev)) { |
| 522 | ret = PTR_ERR(data->cdev); |
| 523 | dev_err(&pdev->dev, |
| 524 | "failed to register cpufreq cooling device: %d\n", ret); |
| 525 | return ret; |
| 526 | } |
| 527 | |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 528 | data->thermal_clk = devm_clk_get(&pdev->dev, NULL); |
| 529 | if (IS_ERR(data->thermal_clk)) { |
| 530 | ret = PTR_ERR(data->thermal_clk); |
| 531 | if (ret != -EPROBE_DEFER) |
| 532 | dev_err(&pdev->dev, |
| 533 | "failed to get thermal clk: %d\n", ret); |
| 534 | cpufreq_cooling_unregister(data->cdev); |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Thermal sensor needs clk on to get correct value, normally |
| 540 | * we should enable its clk before taking measurement and disable |
| 541 | * clk after measurement is done, but if alarm function is enabled, |
| 542 | * hardware will auto measure the temperature periodically, so we |
| 543 | * need to keep the clk always on for alarm function. |
| 544 | */ |
| 545 | ret = clk_prepare_enable(data->thermal_clk); |
| 546 | if (ret) { |
| 547 | dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret); |
| 548 | cpufreq_cooling_unregister(data->cdev); |
| 549 | return ret; |
| 550 | } |
| 551 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 552 | data->tz = thermal_zone_device_register("imx_thermal_zone", |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 553 | IMX_TRIP_NUM, |
| 554 | BIT(IMX_TRIP_PASSIVE), data, |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 555 | &imx_tz_ops, NULL, |
| 556 | IMX_PASSIVE_DELAY, |
| 557 | IMX_POLLING_DELAY); |
| 558 | if (IS_ERR(data->tz)) { |
| 559 | ret = PTR_ERR(data->tz); |
| 560 | dev_err(&pdev->dev, |
| 561 | "failed to register thermal zone device %d\n", ret); |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 562 | clk_disable_unprepare(data->thermal_clk); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 563 | cpufreq_cooling_unregister(data->cdev); |
| 564 | return ret; |
| 565 | } |
| 566 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 567 | /* Enable measurements at ~ 10 Hz */ |
| 568 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); |
| 569 | measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */ |
| 570 | regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq); |
| 571 | imx_set_alarm_temp(data, data->temp_passive); |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 572 | |
| 573 | if (data->socdata->version == TEMPMON_IMX6SX) |
| 574 | imx_set_panic_temp(data, data->temp_critical); |
| 575 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 576 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 577 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 578 | |
| 579 | data->irq_enabled = true; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 580 | data->mode = THERMAL_DEVICE_ENABLED; |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int imx_thermal_remove(struct platform_device *pdev) |
| 586 | { |
| 587 | struct imx_thermal_data *data = platform_get_drvdata(pdev); |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 588 | struct regmap *map = data->tempmon; |
| 589 | |
| 590 | /* Disable measurements */ |
| 591 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
Anson Huang | 329fe7b | 2013-12-23 15:49:22 -0500 | [diff] [blame] | 592 | if (!IS_ERR(data->thermal_clk)) |
| 593 | clk_disable_unprepare(data->thermal_clk); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 594 | |
| 595 | thermal_zone_device_unregister(data->tz); |
| 596 | cpufreq_cooling_unregister(data->cdev); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | #ifdef CONFIG_PM_SLEEP |
| 602 | static int imx_thermal_suspend(struct device *dev) |
| 603 | { |
| 604 | struct imx_thermal_data *data = dev_get_drvdata(dev); |
| 605 | struct regmap *map = data->tempmon; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 606 | |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 607 | /* |
| 608 | * Need to disable thermal sensor, otherwise, when thermal core |
| 609 | * try to get temperature before thermal sensor resume, a wrong |
| 610 | * temperature will be read as the thermal sensor is powered |
| 611 | * down. |
| 612 | */ |
| 613 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 614 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 615 | data->mode = THERMAL_DEVICE_DISABLED; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int imx_thermal_resume(struct device *dev) |
| 621 | { |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 622 | struct imx_thermal_data *data = dev_get_drvdata(dev); |
| 623 | struct regmap *map = data->tempmon; |
| 624 | |
| 625 | /* Enabled thermal sensor after resume */ |
| 626 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 627 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 628 | data->mode = THERMAL_DEVICE_ENABLED; |
| 629 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops, |
| 635 | imx_thermal_suspend, imx_thermal_resume); |
| 636 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 637 | static struct platform_driver imx_thermal = { |
| 638 | .driver = { |
| 639 | .name = "imx_thermal", |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 640 | .pm = &imx_thermal_pm_ops, |
| 641 | .of_match_table = of_imx_thermal_match, |
| 642 | }, |
| 643 | .probe = imx_thermal_probe, |
| 644 | .remove = imx_thermal_remove, |
| 645 | }; |
| 646 | module_platform_driver(imx_thermal); |
| 647 | |
| 648 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 649 | MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs"); |
| 650 | MODULE_LICENSE("GPL v2"); |
| 651 | MODULE_ALIAS("platform:imx-thermal"); |