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 | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 73 | #define FACTOR0 10000000 |
| 74 | #define FACTOR1 15976 |
| 75 | #define FACTOR2 4297157 |
| 76 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 77 | #define TEMPMON_IMX6Q 1 |
| 78 | #define TEMPMON_IMX6SX 2 |
| 79 | |
| 80 | struct thermal_soc_data { |
| 81 | u32 version; |
| 82 | }; |
| 83 | |
| 84 | static struct thermal_soc_data thermal_imx6q_data = { |
| 85 | .version = TEMPMON_IMX6Q, |
| 86 | }; |
| 87 | |
| 88 | static struct thermal_soc_data thermal_imx6sx_data = { |
| 89 | .version = TEMPMON_IMX6SX, |
| 90 | }; |
| 91 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 92 | struct imx_thermal_data { |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 93 | struct cpufreq_policy *policy; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 94 | struct thermal_zone_device *tz; |
| 95 | struct thermal_cooling_device *cdev; |
| 96 | enum thermal_device_mode mode; |
| 97 | struct regmap *tempmon; |
Leonard Crestez | ae62155 | 2017-07-14 17:11:08 +0300 | [diff] [blame] | 98 | u32 c1, c2; /* See formula in imx_init_calib() */ |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 99 | int temp_passive; |
| 100 | int temp_critical; |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 101 | int temp_max; |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 102 | int alarm_temp; |
| 103 | int last_temp; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 104 | bool irq_enabled; |
| 105 | int irq; |
Anson Huang | 329fe7b | 2013-12-23 15:49:22 -0500 | [diff] [blame] | 106 | struct clk *thermal_clk; |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 107 | const struct thermal_soc_data *socdata; |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 108 | const char *temp_grade; |
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 | |
Leonard Crestez | ae62155 | 2017-07-14 17:11:08 +0300 | [diff] [blame] | 181 | /* See imx_init_calib() 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; |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 252 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 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 | |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 288 | /* do not allow changing critical threshold */ |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 289 | if (trip == IMX_TRIP_CRITICAL) |
| 290 | return -EPERM; |
| 291 | |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 292 | /* do not allow passive to be set higher than critical */ |
| 293 | if (temp < 0 || temp > data->temp_critical) |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | |
| 296 | data->temp_passive = temp; |
| 297 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 298 | imx_set_alarm_temp(data, temp); |
| 299 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int imx_bind(struct thermal_zone_device *tz, |
| 304 | struct thermal_cooling_device *cdev) |
| 305 | { |
| 306 | int ret; |
| 307 | |
| 308 | ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev, |
| 309 | THERMAL_NO_LIMIT, |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 310 | THERMAL_NO_LIMIT, |
| 311 | THERMAL_WEIGHT_DEFAULT); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 312 | if (ret) { |
| 313 | dev_err(&tz->device, |
| 314 | "binding zone %s with cdev %s failed:%d\n", |
| 315 | tz->type, cdev->type, ret); |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int imx_unbind(struct thermal_zone_device *tz, |
| 323 | struct thermal_cooling_device *cdev) |
| 324 | { |
| 325 | int ret; |
| 326 | |
| 327 | ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev); |
| 328 | if (ret) { |
| 329 | dev_err(&tz->device, |
| 330 | "unbinding zone %s with cdev %s failed:%d\n", |
| 331 | tz->type, cdev->type, ret); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Eduardo Valentin | cbb07bb | 2014-01-06 08:54:34 -0400 | [diff] [blame] | 338 | static struct thermal_zone_device_ops imx_tz_ops = { |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 339 | .bind = imx_bind, |
| 340 | .unbind = imx_unbind, |
| 341 | .get_temp = imx_get_temp, |
| 342 | .get_mode = imx_get_mode, |
| 343 | .set_mode = imx_set_mode, |
| 344 | .get_trip_type = imx_get_trip_type, |
| 345 | .get_trip_temp = imx_get_trip_temp, |
| 346 | .get_crit_temp = imx_get_crit_temp, |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 347 | .set_trip_temp = imx_set_trip_temp, |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 348 | }; |
| 349 | |
Uwe Kleine-König | e4bb224 | 2017-11-30 10:17:35 +0100 | [diff] [blame^] | 350 | static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1) |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 351 | { |
| 352 | struct imx_thermal_data *data = platform_get_drvdata(pdev); |
Anson Huang | d0f9d64 | 2014-06-20 15:03:06 +0800 | [diff] [blame] | 353 | int t1, n1; |
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 | |
Uwe Kleine-König | e4bb224 | 2017-11-30 10:17:35 +0100 | [diff] [blame^] | 356 | if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) { |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 357 | dev_err(&pdev->dev, "invalid sensor calibration data\n"); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Sensor data layout: |
| 363 | * [31:20] - sensor value @ 25C |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 364 | * Use universal formula now and only need sensor value @ 25C |
| 365 | * slope = 0.4297157 - (0.0015976 * 25C fuse) |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 366 | */ |
Uwe Kleine-König | e4bb224 | 2017-11-30 10:17:35 +0100 | [diff] [blame^] | 367 | n1 = ocotp_ana1 >> 20; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 368 | t1 = 25; /* t1 always 25C */ |
| 369 | |
| 370 | /* |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 371 | * Derived from linear interpolation: |
| 372 | * slope = 0.4297157 - (0.0015976 * 25C fuse) |
| 373 | * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0 |
| 374 | * (Nmeas - n1) / (Tmeas - t1) = slope |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 375 | * We want to reduce this down to the minimum computation necessary |
| 376 | * for each temperature read. Also, we want Tmeas in millicelsius |
| 377 | * and we don't want to lose precision from integer division. So... |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 378 | * Tmeas = (Nmeas - n1) / slope + t1 |
| 379 | * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1 |
| 380 | * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1 |
| 381 | * Let constant c1 = (-1000 / slope) |
| 382 | * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1 |
| 383 | * Let constant c2 = n1 *c1 + 1000 * t1 |
| 384 | * milli_Tmeas = c2 - Nmeas * c1 |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 385 | */ |
Anson Huang | 749e8be | 2014-02-12 18:06:35 +0800 | [diff] [blame] | 386 | temp64 = FACTOR0; |
| 387 | temp64 *= 1000; |
| 388 | do_div(temp64, FACTOR1 * n1 - FACTOR2); |
| 389 | data->c1 = temp64; |
| 390 | data->c2 = n1 * data->c1 + 1000 * t1; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 391 | |
Leonard Crestez | ae62155 | 2017-07-14 17:11:08 +0300 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
Uwe Kleine-König | e4bb224 | 2017-11-30 10:17:35 +0100 | [diff] [blame^] | 395 | 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] | 396 | { |
| 397 | struct imx_thermal_data *data = platform_get_drvdata(pdev); |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 398 | |
| 399 | /* The maximum die temp is specified by the Temperature Grade */ |
Uwe Kleine-König | e4bb224 | 2017-11-30 10:17:35 +0100 | [diff] [blame^] | 400 | switch ((ocotp_mem0 >> 6) & 0x3) { |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 401 | case 0: /* Commercial (0 to 95C) */ |
| 402 | data->temp_grade = "Commercial"; |
| 403 | data->temp_max = 95000; |
| 404 | break; |
| 405 | case 1: /* Extended Commercial (-20 to 105C) */ |
| 406 | data->temp_grade = "Extended Commercial"; |
| 407 | data->temp_max = 105000; |
| 408 | break; |
| 409 | case 2: /* Industrial (-40 to 105C) */ |
| 410 | data->temp_grade = "Industrial"; |
| 411 | data->temp_max = 105000; |
| 412 | break; |
| 413 | case 3: /* Automotive (-40 to 125C) */ |
| 414 | data->temp_grade = "Automotive"; |
| 415 | data->temp_max = 125000; |
| 416 | break; |
| 417 | } |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 418 | |
| 419 | /* |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 420 | * Set the critical trip point at 5C under max |
| 421 | * 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] | 422 | */ |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 423 | data->temp_critical = data->temp_max - (1000 * 5); |
| 424 | data->temp_passive = data->temp_max - (1000 * 10); |
Leonard Crestez | ae62155 | 2017-07-14 17:11:08 +0300 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static int imx_init_from_tempmon_data(struct platform_device *pdev) |
| 428 | { |
| 429 | struct regmap *map; |
| 430 | int ret; |
| 431 | u32 val; |
| 432 | |
| 433 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
| 434 | "fsl,tempmon-data"); |
| 435 | if (IS_ERR(map)) { |
| 436 | ret = PTR_ERR(map); |
| 437 | dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | ret = regmap_read(map, OCOTP_ANA1, &val); |
| 442 | if (ret) { |
| 443 | dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); |
| 444 | return ret; |
| 445 | } |
| 446 | ret = imx_init_calib(pdev, val); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | |
| 450 | ret = regmap_read(map, OCOTP_MEM0, &val); |
| 451 | if (ret) { |
| 452 | dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret); |
| 453 | return ret; |
| 454 | } |
| 455 | imx_init_temp_grade(pdev, val); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int imx_init_from_nvmem_cells(struct platform_device *pdev) |
| 461 | { |
| 462 | int ret; |
| 463 | u32 val; |
| 464 | |
| 465 | ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | imx_init_calib(pdev, val); |
| 469 | |
| 470 | ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | imx_init_temp_grade(pdev, val); |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 474 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 478 | static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev) |
| 479 | { |
| 480 | struct imx_thermal_data *data = dev; |
| 481 | |
| 482 | disable_irq_nosync(irq); |
| 483 | data->irq_enabled = false; |
| 484 | |
| 485 | return IRQ_WAKE_THREAD; |
| 486 | } |
| 487 | |
| 488 | static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev) |
| 489 | { |
| 490 | struct imx_thermal_data *data = dev; |
| 491 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 492 | dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n", |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 493 | data->alarm_temp / 1000); |
| 494 | |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 495 | thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED); |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 496 | |
| 497 | return IRQ_HANDLED; |
| 498 | } |
| 499 | |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 500 | static const struct of_device_id of_imx_thermal_match[] = { |
| 501 | { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, }, |
| 502 | { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, }, |
| 503 | { /* end */ } |
| 504 | }; |
| 505 | MODULE_DEVICE_TABLE(of, of_imx_thermal_match); |
| 506 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 507 | static int imx_thermal_probe(struct platform_device *pdev) |
| 508 | { |
| 509 | struct imx_thermal_data *data; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 510 | struct regmap *map; |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 511 | int measure_freq; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 512 | int ret; |
| 513 | |
| 514 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 515 | if (!data) |
| 516 | return -ENOMEM; |
| 517 | |
| 518 | map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon"); |
| 519 | if (IS_ERR(map)) { |
| 520 | ret = PTR_ERR(map); |
| 521 | dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret); |
| 522 | return ret; |
| 523 | } |
| 524 | data->tempmon = map; |
| 525 | |
Corentin LABBE | 829bc78a | 2016-08-16 10:51:38 +0200 | [diff] [blame] | 526 | data->socdata = of_device_get_match_data(&pdev->dev); |
Shailendra Verma | 8b051ec | 2017-01-30 10:34:58 +0530 | [diff] [blame] | 527 | if (!data->socdata) { |
| 528 | dev_err(&pdev->dev, "no device match found\n"); |
| 529 | return -ENODEV; |
| 530 | } |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 531 | |
| 532 | /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */ |
| 533 | if (data->socdata->version == TEMPMON_IMX6SX) { |
| 534 | regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH | |
| 535 | MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC); |
| 536 | /* |
| 537 | * reset value of LOW ALARM is incorrect, set it to lowest |
| 538 | * value to avoid false trigger of low alarm. |
| 539 | */ |
| 540 | regmap_write(map, TEMPSENSE2 + REG_SET, |
| 541 | TEMPSENSE2_LOW_VALUE_MASK); |
| 542 | } |
| 543 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 544 | data->irq = platform_get_irq(pdev, 0); |
| 545 | if (data->irq < 0) |
| 546 | return data->irq; |
| 547 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 548 | platform_set_drvdata(pdev, data); |
| 549 | |
Leonard Crestez | ae62155 | 2017-07-14 17:11:08 +0300 | [diff] [blame] | 550 | if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) { |
| 551 | ret = imx_init_from_nvmem_cells(pdev); |
| 552 | if (ret == -EPROBE_DEFER) |
| 553 | return ret; |
| 554 | if (ret) { |
| 555 | dev_err(&pdev->dev, "failed to init from nvmem: %d\n", |
| 556 | ret); |
| 557 | return ret; |
| 558 | } |
| 559 | } else { |
| 560 | ret = imx_init_from_tempmon_data(pdev); |
| 561 | if (ret) { |
| 562 | dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n"); |
| 563 | return ret; |
| 564 | } |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* Make sure sensor is in known good state for measurements */ |
| 568 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 569 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 570 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); |
| 571 | regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF); |
| 572 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 573 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 574 | data->policy = cpufreq_cpu_get(0); |
| 575 | if (!data->policy) { |
| 576 | pr_debug("%s: CPUFreq policy not found\n", __func__); |
| 577 | return -EPROBE_DEFER; |
| 578 | } |
| 579 | |
| 580 | data->cdev = cpufreq_cooling_register(data->policy); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 581 | if (IS_ERR(data->cdev)) { |
| 582 | ret = PTR_ERR(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 583 | dev_err(&pdev->dev, |
| 584 | "failed to register cpufreq cooling device: %d\n", ret); |
| 585 | cpufreq_cpu_put(data->policy); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 586 | return ret; |
| 587 | } |
| 588 | |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 589 | data->thermal_clk = devm_clk_get(&pdev->dev, NULL); |
| 590 | if (IS_ERR(data->thermal_clk)) { |
| 591 | ret = PTR_ERR(data->thermal_clk); |
| 592 | if (ret != -EPROBE_DEFER) |
| 593 | dev_err(&pdev->dev, |
| 594 | "failed to get thermal clk: %d\n", ret); |
| 595 | cpufreq_cooling_unregister(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 596 | cpufreq_cpu_put(data->policy); |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Thermal sensor needs clk on to get correct value, normally |
| 602 | * we should enable its clk before taking measurement and disable |
| 603 | * clk after measurement is done, but if alarm function is enabled, |
| 604 | * hardware will auto measure the temperature periodically, so we |
| 605 | * need to keep the clk always on for alarm function. |
| 606 | */ |
| 607 | ret = clk_prepare_enable(data->thermal_clk); |
| 608 | if (ret) { |
| 609 | dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret); |
| 610 | cpufreq_cooling_unregister(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 611 | cpufreq_cpu_put(data->policy); |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 612 | return ret; |
| 613 | } |
| 614 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 615 | data->tz = thermal_zone_device_register("imx_thermal_zone", |
Philipp Zabel | 017e514 | 2013-08-01 18:33:11 +0200 | [diff] [blame] | 616 | IMX_TRIP_NUM, |
| 617 | BIT(IMX_TRIP_PASSIVE), data, |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 618 | &imx_tz_ops, NULL, |
| 619 | IMX_PASSIVE_DELAY, |
| 620 | IMX_POLLING_DELAY); |
| 621 | if (IS_ERR(data->tz)) { |
| 622 | ret = PTR_ERR(data->tz); |
| 623 | dev_err(&pdev->dev, |
| 624 | "failed to register thermal zone device %d\n", ret); |
Heiner Kallweit | 90a21ff | 2014-11-08 20:35:54 +0100 | [diff] [blame] | 625 | clk_disable_unprepare(data->thermal_clk); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 626 | cpufreq_cooling_unregister(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 627 | cpufreq_cpu_put(data->policy); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 628 | return ret; |
| 629 | } |
| 630 | |
Tim Harvey | a2291ba | 2015-11-19 06:49:40 -0800 | [diff] [blame] | 631 | dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC" |
| 632 | " critical:%dC passive:%dC\n", data->temp_grade, |
| 633 | data->temp_max / 1000, data->temp_critical / 1000, |
| 634 | data->temp_passive / 1000); |
| 635 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 636 | /* Enable measurements at ~ 10 Hz */ |
| 637 | regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); |
| 638 | measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */ |
| 639 | regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq); |
| 640 | imx_set_alarm_temp(data, data->temp_passive); |
Anson Huang | 3c94f17 | 2014-08-06 15:12:09 +0800 | [diff] [blame] | 641 | |
| 642 | if (data->socdata->version == TEMPMON_IMX6SX) |
| 643 | imx_set_panic_temp(data, data->temp_critical); |
| 644 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 645 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 646 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 647 | |
Bai Ping | 84866ee | 2015-09-14 19:09:51 +0800 | [diff] [blame] | 648 | ret = devm_request_threaded_irq(&pdev->dev, data->irq, |
| 649 | imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread, |
| 650 | 0, "imx_thermal", data); |
| 651 | if (ret < 0) { |
| 652 | dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret); |
| 653 | clk_disable_unprepare(data->thermal_clk); |
| 654 | thermal_zone_device_unregister(data->tz); |
| 655 | cpufreq_cooling_unregister(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 656 | cpufreq_cpu_put(data->policy); |
Bai Ping | 84866ee | 2015-09-14 19:09:51 +0800 | [diff] [blame] | 657 | return ret; |
| 658 | } |
| 659 | |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 660 | data->irq_enabled = true; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 661 | data->mode = THERMAL_DEVICE_ENABLED; |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int imx_thermal_remove(struct platform_device *pdev) |
| 667 | { |
| 668 | struct imx_thermal_data *data = platform_get_drvdata(pdev); |
Philipp Zabel | 37713a1 | 2013-08-01 18:33:12 +0200 | [diff] [blame] | 669 | struct regmap *map = data->tempmon; |
| 670 | |
| 671 | /* Disable measurements */ |
| 672 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
Anson Huang | 329fe7b | 2013-12-23 15:49:22 -0500 | [diff] [blame] | 673 | if (!IS_ERR(data->thermal_clk)) |
| 674 | clk_disable_unprepare(data->thermal_clk); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 675 | |
| 676 | thermal_zone_device_unregister(data->tz); |
| 677 | cpufreq_cooling_unregister(data->cdev); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 678 | cpufreq_cpu_put(data->policy); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | #ifdef CONFIG_PM_SLEEP |
| 684 | static int imx_thermal_suspend(struct device *dev) |
| 685 | { |
| 686 | struct imx_thermal_data *data = dev_get_drvdata(dev); |
| 687 | struct regmap *map = data->tempmon; |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 688 | |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 689 | /* |
| 690 | * Need to disable thermal sensor, otherwise, when thermal core |
| 691 | * try to get temperature before thermal sensor resume, a wrong |
| 692 | * temperature will be read as the thermal sensor is powered |
| 693 | * down. |
| 694 | */ |
| 695 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP); |
| 696 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN); |
| 697 | data->mode = THERMAL_DEVICE_DISABLED; |
Anson Huang | d26eef8 | 2015-01-06 18:50:22 +0800 | [diff] [blame] | 698 | clk_disable_unprepare(data->thermal_clk); |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static int imx_thermal_resume(struct device *dev) |
| 704 | { |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 705 | struct imx_thermal_data *data = dev_get_drvdata(dev); |
| 706 | struct regmap *map = data->tempmon; |
Arvind Yadav | e3bdc8d | 2017-06-06 15:12:37 +0530 | [diff] [blame] | 707 | int ret; |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 708 | |
Arvind Yadav | e3bdc8d | 2017-06-06 15:12:37 +0530 | [diff] [blame] | 709 | ret = clk_prepare_enable(data->thermal_clk); |
| 710 | if (ret) |
| 711 | return ret; |
Anson Huang | b46cce5 | 2013-12-24 09:43:24 -0500 | [diff] [blame] | 712 | /* Enabled thermal sensor after resume */ |
| 713 | regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); |
| 714 | regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); |
| 715 | data->mode = THERMAL_DEVICE_ENABLED; |
| 716 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops, |
| 722 | imx_thermal_suspend, imx_thermal_resume); |
| 723 | |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 724 | static struct platform_driver imx_thermal = { |
| 725 | .driver = { |
| 726 | .name = "imx_thermal", |
Shawn Guo | ca3de46 | 2013-06-24 14:30:44 +0800 | [diff] [blame] | 727 | .pm = &imx_thermal_pm_ops, |
| 728 | .of_match_table = of_imx_thermal_match, |
| 729 | }, |
| 730 | .probe = imx_thermal_probe, |
| 731 | .remove = imx_thermal_remove, |
| 732 | }; |
| 733 | module_platform_driver(imx_thermal); |
| 734 | |
| 735 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 736 | MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs"); |
| 737 | MODULE_LICENSE("GPL v2"); |
| 738 | MODULE_ALIAS("platform:imx-thermal"); |