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