Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * R-Car Gen3 THS thermal sensor driver |
| 3 | * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen. |
| 4 | * |
| 5 | * Copyright (C) 2016 Renesas Electronics Corporation. |
| 6 | * Copyright (C) 2016 Sang Engineering |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/module.h> |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | #include <linux/thermal.h> |
| 27 | |
| 28 | /* Register offsets */ |
| 29 | #define REG_GEN3_IRQSTR 0x04 |
| 30 | #define REG_GEN3_IRQMSK 0x08 |
| 31 | #define REG_GEN3_IRQCTL 0x0C |
| 32 | #define REG_GEN3_IRQEN 0x10 |
| 33 | #define REG_GEN3_IRQTEMP1 0x14 |
| 34 | #define REG_GEN3_IRQTEMP2 0x18 |
| 35 | #define REG_GEN3_IRQTEMP3 0x1C |
| 36 | #define REG_GEN3_CTSR 0x20 |
| 37 | #define REG_GEN3_THCTR 0x20 |
| 38 | #define REG_GEN3_TEMP 0x28 |
| 39 | #define REG_GEN3_THCODE1 0x50 |
| 40 | #define REG_GEN3_THCODE2 0x54 |
| 41 | #define REG_GEN3_THCODE3 0x58 |
| 42 | |
| 43 | /* CTSR bits */ |
| 44 | #define CTSR_PONM BIT(8) |
| 45 | #define CTSR_AOUT BIT(7) |
| 46 | #define CTSR_THBGR BIT(5) |
| 47 | #define CTSR_VMEN BIT(4) |
| 48 | #define CTSR_VMST BIT(1) |
| 49 | #define CTSR_THSST BIT(0) |
| 50 | |
| 51 | /* THCTR bits */ |
| 52 | #define THCTR_PONM BIT(6) |
| 53 | #define THCTR_THSST BIT(0) |
| 54 | |
| 55 | #define CTEMP_MASK 0xFFF |
| 56 | |
| 57 | #define MCELSIUS(temp) ((temp) * 1000) |
| 58 | #define GEN3_FUSE_MASK 0xFFF |
| 59 | |
| 60 | #define TSC_MAX_NUM 3 |
| 61 | |
| 62 | /* Structure for thermal temperature calculation */ |
| 63 | struct equation_coefs { |
| 64 | int a1; |
| 65 | int b1; |
| 66 | int a2; |
| 67 | int b2; |
| 68 | }; |
| 69 | |
| 70 | struct rcar_gen3_thermal_tsc { |
| 71 | void __iomem *base; |
| 72 | struct thermal_zone_device *zone; |
| 73 | struct equation_coefs coef; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | struct rcar_gen3_thermal_priv { |
| 77 | struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM]; |
Niklas Söderlund | 97dad1f | 2017-03-29 20:43:53 +0200 | [diff] [blame^] | 78 | unsigned int num_tscs; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct rcar_gen3_thermal_data { |
| 82 | void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc); |
| 83 | }; |
| 84 | |
| 85 | static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc, |
| 86 | u32 reg) |
| 87 | { |
| 88 | return ioread32(tsc->base + reg); |
| 89 | } |
| 90 | |
| 91 | static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc, |
| 92 | u32 reg, u32 data) |
| 93 | { |
| 94 | iowrite32(data, tsc->base + reg); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Linear approximation for temperature |
| 99 | * |
| 100 | * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a |
| 101 | * |
| 102 | * The constants a and b are calculated using two triplets of int values PTAT |
| 103 | * and THCODE. PTAT and THCODE can either be read from hardware or use hard |
| 104 | * coded values from driver. The formula to calculate a and b are taken from |
| 105 | * BSP and sparsely documented and understood. |
| 106 | * |
| 107 | * Examining the linear formula and the formula used to calculate constants a |
| 108 | * and b while knowing that the span for PTAT and THCODE values are between |
| 109 | * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001. |
| 110 | * Integer also needs to be signed so that leaves 7 bits for binary |
| 111 | * fixed point scaling. |
| 112 | */ |
| 113 | |
| 114 | #define FIXPT_SHIFT 7 |
| 115 | #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT) |
| 116 | #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b)) |
| 117 | #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT) |
| 118 | |
| 119 | #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */ |
| 120 | |
| 121 | /* no idea where these constants come from */ |
| 122 | #define TJ_1 96 |
| 123 | #define TJ_3 -41 |
| 124 | |
| 125 | static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef, |
| 126 | int *ptat, int *thcode) |
| 127 | { |
| 128 | int tj_2; |
| 129 | |
| 130 | /* TODO: Find documentation and document constant calculation formula */ |
| 131 | |
| 132 | /* |
| 133 | * Division is not scaled in BSP and if scaled it might overflow |
| 134 | * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled |
| 135 | */ |
| 136 | tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 137) |
| 137 | / (ptat[0] - ptat[2])) - FIXPT_INT(41); |
| 138 | |
| 139 | coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]), |
| 140 | tj_2 - FIXPT_INT(TJ_3)); |
| 141 | coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3; |
| 142 | |
| 143 | coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]), |
| 144 | tj_2 - FIXPT_INT(TJ_1)); |
| 145 | coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1; |
| 146 | } |
| 147 | |
| 148 | static int rcar_gen3_thermal_round(int temp) |
| 149 | { |
| 150 | int result, round_offs; |
| 151 | |
| 152 | round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 : |
| 153 | -RCAR3_THERMAL_GRAN / 2; |
| 154 | result = (temp + round_offs) / RCAR3_THERMAL_GRAN; |
| 155 | return result * RCAR3_THERMAL_GRAN; |
| 156 | } |
| 157 | |
| 158 | static int rcar_gen3_thermal_get_temp(void *devdata, int *temp) |
| 159 | { |
| 160 | struct rcar_gen3_thermal_tsc *tsc = devdata; |
| 161 | int mcelsius, val1, val2; |
| 162 | u32 reg; |
| 163 | |
| 164 | /* Read register and convert to mili Celsius */ |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 165 | reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK; |
| 166 | |
| 167 | val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1); |
| 168 | val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2); |
| 169 | mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2); |
| 170 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 171 | /* Make sure we are inside specifications */ |
| 172 | if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125))) |
| 173 | return -EIO; |
| 174 | |
| 175 | /* Round value to device granularity setting */ |
| 176 | *temp = rcar_gen3_thermal_round(mcelsius); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = { |
| 182 | .get_temp = rcar_gen3_thermal_get_temp, |
| 183 | }; |
| 184 | |
| 185 | static void r8a7795_thermal_init(struct rcar_gen3_thermal_tsc *tsc) |
| 186 | { |
| 187 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR); |
| 188 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0); |
| 189 | |
| 190 | usleep_range(1000, 2000); |
| 191 | |
| 192 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM); |
| 193 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F); |
| 194 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, |
| 195 | CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN); |
| 196 | |
| 197 | usleep_range(100, 200); |
| 198 | |
| 199 | rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, |
| 200 | CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN | |
| 201 | CTSR_VMST | CTSR_THSST); |
| 202 | |
| 203 | usleep_range(1000, 2000); |
| 204 | } |
| 205 | |
| 206 | static void r8a7796_thermal_init(struct rcar_gen3_thermal_tsc *tsc) |
| 207 | { |
| 208 | u32 reg_val; |
| 209 | |
| 210 | reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR); |
| 211 | reg_val &= ~THCTR_PONM; |
| 212 | rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val); |
| 213 | |
| 214 | usleep_range(1000, 2000); |
| 215 | |
| 216 | rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F); |
| 217 | reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR); |
| 218 | reg_val |= THCTR_THSST; |
| 219 | rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val); |
Niklas Söderlund | 78aefd2 | 2017-03-29 20:43:50 +0200 | [diff] [blame] | 220 | |
| 221 | usleep_range(1000, 2000); |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static const struct rcar_gen3_thermal_data r8a7795_data = { |
| 225 | .thermal_init = r8a7795_thermal_init, |
| 226 | }; |
| 227 | |
| 228 | static const struct rcar_gen3_thermal_data r8a7796_data = { |
| 229 | .thermal_init = r8a7796_thermal_init, |
| 230 | }; |
| 231 | |
| 232 | static const struct of_device_id rcar_gen3_thermal_dt_ids[] = { |
| 233 | { .compatible = "renesas,r8a7795-thermal", .data = &r8a7795_data}, |
| 234 | { .compatible = "renesas,r8a7796-thermal", .data = &r8a7796_data}, |
| 235 | {}, |
| 236 | }; |
| 237 | MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids); |
| 238 | |
| 239 | static int rcar_gen3_thermal_remove(struct platform_device *pdev) |
| 240 | { |
| 241 | struct device *dev = &pdev->dev; |
| 242 | |
| 243 | pm_runtime_put(dev); |
| 244 | pm_runtime_disable(dev); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int rcar_gen3_thermal_probe(struct platform_device *pdev) |
| 250 | { |
| 251 | struct rcar_gen3_thermal_priv *priv; |
| 252 | struct device *dev = &pdev->dev; |
| 253 | struct resource *res; |
| 254 | struct thermal_zone_device *zone; |
| 255 | int ret, i; |
| 256 | const struct rcar_gen3_thermal_data *match_data = |
| 257 | of_device_get_match_data(dev); |
| 258 | |
| 259 | /* default values if FUSEs are missing */ |
| 260 | /* TODO: Read values from hardware on supported platforms */ |
| 261 | int ptat[3] = { 2351, 1509, 435 }; |
| 262 | int thcode[TSC_MAX_NUM][3] = { |
| 263 | { 3248, 2800, 2221 }, |
| 264 | { 3245, 2795, 2216 }, |
| 265 | { 3250, 2805, 2237 }, |
| 266 | }; |
| 267 | |
| 268 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 269 | if (!priv) |
| 270 | return -ENOMEM; |
| 271 | |
| 272 | platform_set_drvdata(pdev, priv); |
| 273 | |
| 274 | pm_runtime_enable(dev); |
| 275 | pm_runtime_get_sync(dev); |
| 276 | |
| 277 | for (i = 0; i < TSC_MAX_NUM; i++) { |
| 278 | struct rcar_gen3_thermal_tsc *tsc; |
| 279 | |
Niklas Söderlund | d51546c | 2017-03-29 20:43:52 +0200 | [diff] [blame] | 280 | res = platform_get_resource(pdev, IORESOURCE_MEM, i); |
| 281 | if (!res) |
| 282 | break; |
| 283 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 284 | tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL); |
| 285 | if (!tsc) { |
| 286 | ret = -ENOMEM; |
| 287 | goto error_unregister; |
| 288 | } |
| 289 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 290 | tsc->base = devm_ioremap_resource(dev, res); |
| 291 | if (IS_ERR(tsc->base)) { |
| 292 | ret = PTR_ERR(tsc->base); |
| 293 | goto error_unregister; |
| 294 | } |
| 295 | |
| 296 | priv->tscs[i] = tsc; |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 297 | |
| 298 | match_data->thermal_init(tsc); |
| 299 | rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]); |
| 300 | |
| 301 | zone = devm_thermal_zone_of_sensor_register(dev, i, tsc, |
| 302 | &rcar_gen3_tz_of_ops); |
| 303 | if (IS_ERR(zone)) { |
| 304 | dev_err(dev, "Can't register thermal zone\n"); |
| 305 | ret = PTR_ERR(zone); |
| 306 | goto error_unregister; |
| 307 | } |
| 308 | tsc->zone = zone; |
| 309 | } |
| 310 | |
Niklas Söderlund | 97dad1f | 2017-03-29 20:43:53 +0200 | [diff] [blame^] | 311 | priv->num_tscs = i; |
| 312 | |
| 313 | if (!priv->num_tscs) { |
| 314 | ret = -ENODEV; |
| 315 | goto error_unregister; |
| 316 | } |
| 317 | |
Wolfram Sang | 564e73d | 2016-12-22 11:38:21 +0100 | [diff] [blame] | 318 | return 0; |
| 319 | |
| 320 | error_unregister: |
| 321 | rcar_gen3_thermal_remove(pdev); |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static struct platform_driver rcar_gen3_thermal_driver = { |
| 327 | .driver = { |
| 328 | .name = "rcar_gen3_thermal", |
| 329 | .of_match_table = rcar_gen3_thermal_dt_ids, |
| 330 | }, |
| 331 | .probe = rcar_gen3_thermal_probe, |
| 332 | .remove = rcar_gen3_thermal_remove, |
| 333 | }; |
| 334 | module_platform_driver(rcar_gen3_thermal_driver); |
| 335 | |
| 336 | MODULE_LICENSE("GPL v2"); |
| 337 | MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver"); |
| 338 | MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>"); |