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