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