Kuninori Morimoto | d316522 | 2018-08-20 11:42:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * R-Car Gen3 THS thermal sensor driver |
| 4 | * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen. |
| 5 | * |
| 6 | * Copyright (C) 2016 Renesas Electronics Corporation. |
| 7 | * Copyright (C) 2016 Sang Engineering |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/module.h> |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 14 | #include <linux/of_device.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/pm_runtime.h> |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 18 | #include <linux/sys_soc.h> |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 19 | #include <linux/thermal.h> |
| 20 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 21 | #include "thermal_core.h" |
Marek Vasut | 6269e9f | 2019-02-11 20:56:20 +0100 | [diff] [blame] | 22 | #include "thermal_hwmon.h" |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 23 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 24 | /* Register offsets */ |
| 25 | #define REG_GEN3_IRQSTR 0x04 |
| 26 | #define REG_GEN3_IRQMSK 0x08 |
| 27 | #define REG_GEN3_IRQCTL 0x0C |
| 28 | #define REG_GEN3_IRQEN 0x10 |
| 29 | #define REG_GEN3_IRQTEMP1 0x14 |
| 30 | #define REG_GEN3_IRQTEMP2 0x18 |
| 31 | #define REG_GEN3_IRQTEMP3 0x1C |
| 32 | #define REG_GEN3_CTSR 0x20 |
| 33 | #define REG_GEN3_THCTR 0x20 |
| 34 | #define REG_GEN3_TEMP 0x28 |
| 35 | #define REG_GEN3_THCODE1 0x50 |
| 36 | #define REG_GEN3_THCODE2 0x54 |
| 37 | #define REG_GEN3_THCODE3 0x58 |
| 38 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 39 | /* IRQ{STR,MSK,EN} bits */ |
| 40 | #define IRQ_TEMP1 BIT(0) |
| 41 | #define IRQ_TEMP2 BIT(1) |
| 42 | #define IRQ_TEMP3 BIT(2) |
| 43 | #define IRQ_TEMPD1 BIT(3) |
| 44 | #define IRQ_TEMPD2 BIT(4) |
| 45 | #define IRQ_TEMPD3 BIT(5) |
| 46 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 47 | /* CTSR bits */ |
| 48 | #define CTSR_PONM BIT(8) |
| 49 | #define CTSR_AOUT BIT(7) |
| 50 | #define CTSR_THBGR BIT(5) |
| 51 | #define CTSR_VMEN BIT(4) |
| 52 | #define CTSR_VMST BIT(1) |
| 53 | #define CTSR_THSST BIT(0) |
| 54 | |
| 55 | /* THCTR bits */ |
| 56 | #define THCTR_PONM BIT(6) |
| 57 | #define THCTR_THSST BIT(0) |
| 58 | |
| 59 | #define CTEMP_MASK 0xFFF |
| 60 | |
| 61 | #define MCELSIUS(temp) ((temp) * 1000) |
| 62 | #define GEN3_FUSE_MASK 0xFFF |
| 63 | |
| 64 | #define TSC_MAX_NUM 3 |
| 65 | |
| 66 | /* Structure for thermal temperature calculation */ |
| 67 | struct equation_coefs { |
| 68 | int a1; |
| 69 | int b1; |
| 70 | int a2; |
| 71 | int b2; |
| 72 | }; |
| 73 | |
| 74 | struct rcar_gen3_thermal_tsc { |
| 75 | void __iomem *base; |
| 76 | struct thermal_zone_device *zone; |
| 77 | struct equation_coefs coef; |
Niklas Söderlund | 75f78d6 | 2017-03-29 20:43:56 +0200 | [diff] [blame] | 78 | int low; |
| 79 | int high; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct rcar_gen3_thermal_priv { |
| 83 | struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM]; |
Niklas Söderlund | 97dad1f | 2017-03-29 20:43:53 +0200 | [diff] [blame] | 84 | unsigned int num_tscs; |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 85 | spinlock_t lock; /* Protect interrupts on and off */ |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 86 | void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc); |
| 87 | }; |
| 88 | |
| 89 | static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc, |
| 90 | u32 reg) |
| 91 | { |
| 92 | return ioread32(tsc->base + reg); |
| 93 | } |
| 94 | |
| 95 | static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc, |
| 96 | u32 reg, u32 data) |
| 97 | { |
| 98 | iowrite32(data, tsc->base + reg); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Linear approximation for temperature |
| 103 | * |
| 104 | * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a |
| 105 | * |
| 106 | * The constants a and b are calculated using two triplets of int values PTAT |
| 107 | * and THCODE. PTAT and THCODE can either be read from hardware or use hard |
| 108 | * coded values from driver. The formula to calculate a and b are taken from |
| 109 | * BSP and sparsely documented and understood. |
| 110 | * |
| 111 | * Examining the linear formula and the formula used to calculate constants a |
| 112 | * and b while knowing that the span for PTAT and THCODE values are between |
| 113 | * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001. |
| 114 | * Integer also needs to be signed so that leaves 7 bits for binary |
| 115 | * fixed point scaling. |
| 116 | */ |
| 117 | |
| 118 | #define FIXPT_SHIFT 7 |
| 119 | #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT) |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 120 | #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT) |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 121 | #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b)) |
| 122 | #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT) |
| 123 | |
| 124 | #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */ |
| 125 | |
| 126 | /* no idea where these constants come from */ |
Hien Dang | fc66ddf | 2018-04-17 22:57:46 +0200 | [diff] [blame] | 127 | #define TJ_1 116 |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 128 | #define TJ_3 -41 |
| 129 | |
| 130 | static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef, |
| 131 | int *ptat, int *thcode) |
| 132 | { |
| 133 | int tj_2; |
| 134 | |
| 135 | /* TODO: Find documentation and document constant calculation formula */ |
| 136 | |
| 137 | /* |
| 138 | * Division is not scaled in BSP and if scaled it might overflow |
| 139 | * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled |
| 140 | */ |
Hien Dang | fc66ddf | 2018-04-17 22:57:46 +0200 | [diff] [blame] | 141 | tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 157) |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 142 | / (ptat[0] - ptat[2])) - FIXPT_INT(41); |
| 143 | |
| 144 | coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]), |
| 145 | tj_2 - FIXPT_INT(TJ_3)); |
| 146 | coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3; |
| 147 | |
| 148 | coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]), |
| 149 | tj_2 - FIXPT_INT(TJ_1)); |
| 150 | coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1; |
| 151 | } |
| 152 | |
| 153 | static int rcar_gen3_thermal_round(int temp) |
| 154 | { |
| 155 | int result, round_offs; |
| 156 | |
| 157 | round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 : |
| 158 | -RCAR3_THERMAL_GRAN / 2; |
| 159 | result = (temp + round_offs) / RCAR3_THERMAL_GRAN; |
| 160 | return result * RCAR3_THERMAL_GRAN; |
| 161 | } |
| 162 | |
| 163 | static int rcar_gen3_thermal_get_temp(void *devdata, int *temp) |
| 164 | { |
| 165 | struct rcar_gen3_thermal_tsc *tsc = devdata; |
| 166 | int mcelsius, val1, val2; |
| 167 | u32 reg; |
| 168 | |
| 169 | /* Read register and convert to mili Celsius */ |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 170 | reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK; |
| 171 | |
| 172 | val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1); |
| 173 | val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2); |
| 174 | mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2); |
| 175 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 176 | /* Make sure we are inside specifications */ |
| 177 | if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125))) |
| 178 | return -EIO; |
| 179 | |
| 180 | /* Round value to device granularity setting */ |
| 181 | *temp = rcar_gen3_thermal_round(mcelsius); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 186 | static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc, |
| 187 | int mcelsius) |
| 188 | { |
| 189 | int celsius, val1, val2; |
| 190 | |
| 191 | celsius = DIV_ROUND_CLOSEST(mcelsius, 1000); |
| 192 | val1 = celsius * tsc->coef.a1 + tsc->coef.b1; |
| 193 | val2 = celsius * tsc->coef.a2 + tsc->coef.b2; |
| 194 | |
| 195 | return INT_FIXPT((val1 + val2) / 2); |
| 196 | } |
| 197 | |
| 198 | static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high) |
| 199 | { |
| 200 | struct rcar_gen3_thermal_tsc *tsc = devdata; |
| 201 | |
Niklas Söderlund | 270ba43 | 2018-04-17 22:57:47 +0200 | [diff] [blame] | 202 | low = clamp_val(low, -40000, 120000); |
| 203 | high = clamp_val(high, -40000, 120000); |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 204 | |
| 205 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1, |
| 206 | rcar_gen3_thermal_mcelsius_to_temp(tsc, low)); |
| 207 | |
| 208 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2, |
| 209 | rcar_gen3_thermal_mcelsius_to_temp(tsc, high)); |
| 210 | |
Niklas Söderlund | 75f78d6 | 2017-03-29 20:43:56 +0200 | [diff] [blame] | 211 | tsc->low = low; |
| 212 | tsc->high = high; |
| 213 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Julia Lawall | a0def10 | 2017-08-08 17:08:57 +0200 | [diff] [blame] | 217 | static const struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = { |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 218 | .get_temp = rcar_gen3_thermal_get_temp, |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 219 | .set_trips = rcar_gen3_thermal_set_trips, |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 222 | static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on) |
| 223 | { |
| 224 | unsigned int i; |
| 225 | u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0; |
| 226 | |
| 227 | for (i = 0; i < priv->num_tscs; i++) |
| 228 | rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val); |
| 229 | } |
| 230 | |
| 231 | static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data) |
| 232 | { |
| 233 | struct rcar_gen3_thermal_priv *priv = data; |
| 234 | u32 status; |
| 235 | int i, ret = IRQ_HANDLED; |
| 236 | |
| 237 | spin_lock(&priv->lock); |
| 238 | for (i = 0; i < priv->num_tscs; i++) { |
| 239 | status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR); |
| 240 | rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0); |
| 241 | if (status) |
| 242 | ret = IRQ_WAKE_THREAD; |
| 243 | } |
| 244 | |
| 245 | if (ret == IRQ_WAKE_THREAD) |
| 246 | rcar_thermal_irq_set(priv, false); |
| 247 | |
| 248 | spin_unlock(&priv->lock); |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static irqreturn_t rcar_gen3_thermal_irq_thread(int irq, void *data) |
| 254 | { |
| 255 | struct rcar_gen3_thermal_priv *priv = data; |
| 256 | unsigned long flags; |
| 257 | int i; |
| 258 | |
| 259 | for (i = 0; i < priv->num_tscs; i++) |
| 260 | thermal_zone_device_update(priv->tscs[i]->zone, |
| 261 | THERMAL_EVENT_UNSPECIFIED); |
| 262 | |
| 263 | spin_lock_irqsave(&priv->lock, flags); |
| 264 | rcar_thermal_irq_set(priv, true); |
| 265 | spin_unlock_irqrestore(&priv->lock, flags); |
| 266 | |
| 267 | return IRQ_HANDLED; |
| 268 | } |
| 269 | |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 270 | static const struct soc_device_attribute r8a7795es1[] = { |
| 271 | { .soc_id = "r8a7795", .revision = "ES1.*" }, |
| 272 | { /* sentinel */ } |
| 273 | }; |
| 274 | |
| 275 | static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc) |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 276 | { |
| 277 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR); |
| 278 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0); |
| 279 | |
| 280 | usleep_range(1000, 2000); |
| 281 | |
| 282 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM); |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 283 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 284 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F); |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 285 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0); |
| 286 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2); |
| 287 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 288 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, |
| 289 | CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN); |
| 290 | |
| 291 | usleep_range(100, 200); |
| 292 | |
| 293 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, |
| 294 | CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN | |
| 295 | CTSR_VMST | CTSR_THSST); |
| 296 | |
| 297 | usleep_range(1000, 2000); |
| 298 | } |
| 299 | |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 300 | static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc) |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 301 | { |
| 302 | u32 reg_val; |
| 303 | |
| 304 | reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR); |
| 305 | reg_val &= ~THCTR_PONM; |
| 306 | rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val); |
| 307 | |
| 308 | usleep_range(1000, 2000); |
| 309 | |
Hoan Nguyen An | ed1b1ac | 2019-03-27 18:03:18 +0900 | [diff] [blame^] | 310 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0); |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 311 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0); |
| 312 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2); |
| 313 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 314 | reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR); |
| 315 | reg_val |= THCTR_THSST; |
| 316 | rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val); |
Niklas Söderlund | 78aefd2 | 2017-03-29 20:43:50 +0200 | [diff] [blame] | 317 | |
| 318 | usleep_range(1000, 2000); |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 321 | static const struct of_device_id rcar_gen3_thermal_dt_ids[] = { |
Fabrizio Castro | 1d9e6cf | 2018-08-17 15:53:03 +0100 | [diff] [blame] | 322 | { .compatible = "renesas,r8a774a1-thermal", }, |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 323 | { .compatible = "renesas,r8a7795-thermal", }, |
| 324 | { .compatible = "renesas,r8a7796-thermal", }, |
Niklas Söderlund | 2e7db3e | 2018-04-26 21:42:02 +0200 | [diff] [blame] | 325 | { .compatible = "renesas,r8a77965-thermal", }, |
Sergei Shtylyov | 853cbc1 | 2018-10-09 22:11:51 +0300 | [diff] [blame] | 326 | { .compatible = "renesas,r8a77980-thermal", }, |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 327 | {}, |
| 328 | }; |
| 329 | MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids); |
| 330 | |
| 331 | static int rcar_gen3_thermal_remove(struct platform_device *pdev) |
| 332 | { |
| 333 | struct device *dev = &pdev->dev; |
| 334 | |
| 335 | pm_runtime_put(dev); |
| 336 | pm_runtime_disable(dev); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Marek Vasut | 6269e9f | 2019-02-11 20:56:20 +0100 | [diff] [blame] | 341 | static void rcar_gen3_hwmon_action(void *data) |
| 342 | { |
| 343 | struct thermal_zone_device *zone = data; |
| 344 | |
| 345 | thermal_remove_hwmon_sysfs(zone); |
| 346 | } |
| 347 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 348 | static int rcar_gen3_thermal_probe(struct platform_device *pdev) |
| 349 | { |
| 350 | struct rcar_gen3_thermal_priv *priv; |
| 351 | struct device *dev = &pdev->dev; |
| 352 | struct resource *res; |
| 353 | struct thermal_zone_device *zone; |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 354 | int ret, irq, i; |
| 355 | char *irqname; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 356 | |
| 357 | /* default values if FUSEs are missing */ |
| 358 | /* TODO: Read values from hardware on supported platforms */ |
Hien Dang | fc66ddf | 2018-04-17 22:57:46 +0200 | [diff] [blame] | 359 | int ptat[3] = { 2631, 1509, 435 }; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 360 | int thcode[TSC_MAX_NUM][3] = { |
Hien Dang | fc66ddf | 2018-04-17 22:57:46 +0200 | [diff] [blame] | 361 | { 3397, 2800, 2221 }, |
| 362 | { 3393, 2795, 2216 }, |
| 363 | { 3389, 2805, 2237 }, |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 367 | if (!priv) |
| 368 | return -ENOMEM; |
| 369 | |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 370 | priv->thermal_init = rcar_gen3_thermal_init; |
| 371 | if (soc_device_match(r8a7795es1)) |
| 372 | priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1; |
Niklas Söderlund | cc4d072 | 2017-03-29 20:43:55 +0200 | [diff] [blame] | 373 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 374 | spin_lock_init(&priv->lock); |
| 375 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 376 | platform_set_drvdata(pdev, priv); |
| 377 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 378 | /* |
| 379 | * Request 2 (of the 3 possible) IRQs, the driver only needs to |
| 380 | * to trigger on the low and high trip points of the current |
| 381 | * temp window at this point. |
| 382 | */ |
| 383 | for (i = 0; i < 2; i++) { |
| 384 | irq = platform_get_irq(pdev, i); |
| 385 | if (irq < 0) |
| 386 | return irq; |
| 387 | |
| 388 | irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d", |
| 389 | dev_name(dev), i); |
| 390 | if (!irqname) |
| 391 | return -ENOMEM; |
| 392 | |
| 393 | ret = devm_request_threaded_irq(dev, irq, rcar_gen3_thermal_irq, |
| 394 | rcar_gen3_thermal_irq_thread, |
| 395 | IRQF_SHARED, irqname, priv); |
| 396 | if (ret) |
| 397 | return ret; |
| 398 | } |
| 399 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 400 | pm_runtime_enable(dev); |
| 401 | pm_runtime_get_sync(dev); |
| 402 | |
| 403 | for (i = 0; i < TSC_MAX_NUM; i++) { |
| 404 | struct rcar_gen3_thermal_tsc *tsc; |
| 405 | |
Niklas Söderlund | d51546c | 2017-03-29 20:43:52 +0200 | [diff] [blame] | 406 | res = platform_get_resource(pdev, IORESOURCE_MEM, i); |
| 407 | if (!res) |
| 408 | break; |
| 409 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 410 | tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL); |
| 411 | if (!tsc) { |
| 412 | ret = -ENOMEM; |
| 413 | goto error_unregister; |
| 414 | } |
| 415 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 416 | tsc->base = devm_ioremap_resource(dev, res); |
| 417 | if (IS_ERR(tsc->base)) { |
| 418 | ret = PTR_ERR(tsc->base); |
| 419 | goto error_unregister; |
| 420 | } |
| 421 | |
| 422 | priv->tscs[i] = tsc; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 423 | |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 424 | priv->thermal_init(tsc); |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 425 | rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]); |
| 426 | |
| 427 | zone = devm_thermal_zone_of_sensor_register(dev, i, tsc, |
| 428 | &rcar_gen3_tz_of_ops); |
| 429 | if (IS_ERR(zone)) { |
| 430 | dev_err(dev, "Can't register thermal zone\n"); |
| 431 | ret = PTR_ERR(zone); |
| 432 | goto error_unregister; |
| 433 | } |
| 434 | tsc->zone = zone; |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 435 | |
| 436 | ret = of_thermal_get_ntrips(tsc->zone); |
| 437 | if (ret < 0) |
| 438 | goto error_unregister; |
| 439 | |
Marek Vasut | 6269e9f | 2019-02-11 20:56:20 +0100 | [diff] [blame] | 440 | tsc->zone->tzp->no_hwmon = false; |
| 441 | ret = thermal_add_hwmon_sysfs(tsc->zone); |
| 442 | if (ret) |
| 443 | goto error_unregister; |
| 444 | |
| 445 | ret = devm_add_action(dev, rcar_gen3_hwmon_action, zone); |
| 446 | if (ret) { |
| 447 | rcar_gen3_hwmon_action(zone); |
| 448 | goto error_unregister; |
| 449 | } |
| 450 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 451 | dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret); |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Niklas Söderlund | 97dad1f | 2017-03-29 20:43:53 +0200 | [diff] [blame] | 454 | priv->num_tscs = i; |
| 455 | |
| 456 | if (!priv->num_tscs) { |
| 457 | ret = -ENODEV; |
| 458 | goto error_unregister; |
| 459 | } |
| 460 | |
Niklas Söderlund | 7d4b269 | 2017-03-29 20:43:54 +0200 | [diff] [blame] | 461 | rcar_thermal_irq_set(priv, true); |
| 462 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 463 | return 0; |
| 464 | |
| 465 | error_unregister: |
| 466 | rcar_gen3_thermal_remove(pdev); |
| 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
Niklas Söderlund | 75f78d6 | 2017-03-29 20:43:56 +0200 | [diff] [blame] | 471 | static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev) |
| 472 | { |
| 473 | struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev); |
| 474 | |
| 475 | rcar_thermal_irq_set(priv, false); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev) |
| 481 | { |
| 482 | struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev); |
| 483 | unsigned int i; |
| 484 | |
| 485 | for (i = 0; i < priv->num_tscs; i++) { |
| 486 | struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i]; |
| 487 | |
Niklas Söderlund | d668c80 | 2017-10-17 13:36:13 +0200 | [diff] [blame] | 488 | priv->thermal_init(tsc); |
Niklas Söderlund | 75f78d6 | 2017-03-29 20:43:56 +0200 | [diff] [blame] | 489 | rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high); |
| 490 | } |
| 491 | |
| 492 | rcar_thermal_irq_set(priv, true); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend, |
| 498 | rcar_gen3_thermal_resume); |
| 499 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 500 | static struct platform_driver rcar_gen3_thermal_driver = { |
| 501 | .driver = { |
| 502 | .name = "rcar_gen3_thermal", |
Niklas Söderlund | 75f78d6 | 2017-03-29 20:43:56 +0200 | [diff] [blame] | 503 | .pm = &rcar_gen3_thermal_pm_ops, |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 504 | .of_match_table = rcar_gen3_thermal_dt_ids, |
| 505 | }, |
| 506 | .probe = rcar_gen3_thermal_probe, |
| 507 | .remove = rcar_gen3_thermal_remove, |
| 508 | }; |
| 509 | module_platform_driver(rcar_gen3_thermal_driver); |
| 510 | |
| 511 | MODULE_LICENSE("GPL v2"); |
| 512 | MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver"); |
| 513 | MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>"); |