Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 1 | /* |
| 2 | * R-Car THS/TSC thermal sensor driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 19 | */ |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/platform_device.h> |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame^] | 25 | #include <linux/reboot.h> |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/thermal.h> |
| 29 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame^] | 30 | #define IDLE_INTERVAL 5000 |
| 31 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 32 | #define THSCR 0x2c |
| 33 | #define THSSR 0x30 |
| 34 | |
| 35 | /* THSCR */ |
| 36 | #define CPTAP 0xf |
| 37 | |
| 38 | /* THSSR */ |
| 39 | #define CTEMP 0x3f |
| 40 | |
| 41 | |
| 42 | struct rcar_thermal_priv { |
| 43 | void __iomem *base; |
| 44 | struct device *dev; |
| 45 | spinlock_t lock; |
| 46 | u32 comp; |
| 47 | }; |
| 48 | |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 49 | #define MCELSIUS(temp) ((temp) * 1000) |
Kuninori Morimoto | d12250e | 2012-11-26 02:32:20 +0000 | [diff] [blame] | 50 | #define rcar_zone_to_priv(zone) (zone->devdata) |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 51 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 52 | /* |
| 53 | * basic functions |
| 54 | */ |
| 55 | static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg) |
| 56 | { |
| 57 | unsigned long flags; |
| 58 | u32 ret; |
| 59 | |
| 60 | spin_lock_irqsave(&priv->lock, flags); |
| 61 | |
| 62 | ret = ioread32(priv->base + reg); |
| 63 | |
| 64 | spin_unlock_irqrestore(&priv->lock, flags); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | #if 0 /* no user at this point */ |
| 70 | static void rcar_thermal_write(struct rcar_thermal_priv *priv, |
| 71 | u32 reg, u32 data) |
| 72 | { |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&priv->lock, flags); |
| 76 | |
| 77 | iowrite32(data, priv->base + reg); |
| 78 | |
| 79 | spin_unlock_irqrestore(&priv->lock, flags); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg, |
| 84 | u32 mask, u32 data) |
| 85 | { |
| 86 | unsigned long flags; |
| 87 | u32 val; |
| 88 | |
| 89 | spin_lock_irqsave(&priv->lock, flags); |
| 90 | |
| 91 | val = ioread32(priv->base + reg); |
| 92 | val &= ~mask; |
| 93 | val |= (data & mask); |
| 94 | iowrite32(val, priv->base + reg); |
| 95 | |
| 96 | spin_unlock_irqrestore(&priv->lock, flags); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * zone device functions |
| 101 | */ |
| 102 | static int rcar_thermal_get_temp(struct thermal_zone_device *zone, |
| 103 | unsigned long *temp) |
| 104 | { |
Kuninori Morimoto | d12250e | 2012-11-26 02:32:20 +0000 | [diff] [blame] | 105 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 106 | int val, min, max, tmp; |
| 107 | |
| 108 | tmp = -200; /* default */ |
| 109 | while (1) { |
| 110 | if (priv->comp < 1 || priv->comp > 12) { |
| 111 | dev_err(priv->dev, |
| 112 | "THSSR invalid data (%d)\n", priv->comp); |
| 113 | priv->comp = 4; /* for next thermal */ |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * THS comparator offset and the reference temperature |
| 119 | * |
| 120 | * Comparator | reference | Temperature field |
| 121 | * offset | temperature | measurement |
| 122 | * | (degrees C) | (degrees C) |
| 123 | * -------------+---------------+------------------- |
| 124 | * 1 | -45 | -45 to -30 |
| 125 | * 2 | -30 | -30 to -15 |
| 126 | * 3 | -15 | -15 to 0 |
| 127 | * 4 | 0 | 0 to +15 |
| 128 | * 5 | +15 | +15 to +30 |
| 129 | * 6 | +30 | +30 to +45 |
| 130 | * 7 | +45 | +45 to +60 |
| 131 | * 8 | +60 | +60 to +75 |
| 132 | * 9 | +75 | +75 to +90 |
| 133 | * 10 | +90 | +90 to +105 |
| 134 | * 11 | +105 | +105 to +120 |
| 135 | * 12 | +120 | +120 to +135 |
| 136 | */ |
| 137 | |
| 138 | /* calculate thermal limitation */ |
| 139 | min = (priv->comp * 15) - 60; |
| 140 | max = min + 15; |
| 141 | |
| 142 | /* |
| 143 | * we need to wait 300us after changing comparator offset |
| 144 | * to get stable temperature. |
| 145 | * see "Usage Notes" on datasheet |
| 146 | */ |
| 147 | rcar_thermal_bset(priv, THSCR, CPTAP, priv->comp); |
| 148 | udelay(300); |
| 149 | |
| 150 | /* calculate current temperature */ |
| 151 | val = rcar_thermal_read(priv, THSSR) & CTEMP; |
| 152 | val = (val * 5) - 65; |
| 153 | |
| 154 | dev_dbg(priv->dev, "comp/min/max/val = %d/%d/%d/%d\n", |
| 155 | priv->comp, min, max, val); |
| 156 | |
| 157 | /* |
| 158 | * If val is same as min/max, then, |
| 159 | * it should try again on next comparator. |
| 160 | * But the val might be correct temperature. |
| 161 | * Keep it on "tmp" and compare with next val. |
| 162 | */ |
| 163 | if (tmp == val) |
| 164 | break; |
| 165 | |
| 166 | if (val <= min) { |
| 167 | tmp = min; |
| 168 | priv->comp--; /* try again */ |
| 169 | } else if (val >= max) { |
| 170 | tmp = max; |
| 171 | priv->comp++; /* try again */ |
| 172 | } else { |
| 173 | tmp = val; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 178 | *temp = MCELSIUS(tmp); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame^] | 182 | static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone, |
| 183 | int trip, enum thermal_trip_type *type) |
| 184 | { |
| 185 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 186 | |
| 187 | /* see rcar_thermal_get_temp() */ |
| 188 | switch (trip) { |
| 189 | case 0: /* +90 <= temp */ |
| 190 | *type = THERMAL_TRIP_CRITICAL; |
| 191 | break; |
| 192 | default: |
| 193 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone, |
| 201 | int trip, unsigned long *temp) |
| 202 | { |
| 203 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 204 | |
| 205 | /* see rcar_thermal_get_temp() */ |
| 206 | switch (trip) { |
| 207 | case 0: /* +90 <= temp */ |
| 208 | *temp = MCELSIUS(90); |
| 209 | break; |
| 210 | default: |
| 211 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int rcar_thermal_notify(struct thermal_zone_device *zone, |
| 219 | int trip, enum thermal_trip_type type) |
| 220 | { |
| 221 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 222 | |
| 223 | switch (type) { |
| 224 | case THERMAL_TRIP_CRITICAL: |
| 225 | /* FIXME */ |
| 226 | dev_warn(priv->dev, |
| 227 | "Thermal reached to critical temperature\n"); |
| 228 | machine_power_off(); |
| 229 | break; |
| 230 | default: |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 237 | static struct thermal_zone_device_ops rcar_thermal_zone_ops = { |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame^] | 238 | .get_temp = rcar_thermal_get_temp, |
| 239 | .get_trip_type = rcar_thermal_get_trip_type, |
| 240 | .get_trip_temp = rcar_thermal_get_trip_temp, |
| 241 | .notify = rcar_thermal_notify, |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | /* |
| 245 | * platform functions |
| 246 | */ |
| 247 | static int rcar_thermal_probe(struct platform_device *pdev) |
| 248 | { |
| 249 | struct thermal_zone_device *zone; |
| 250 | struct rcar_thermal_priv *priv; |
| 251 | struct resource *res; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 252 | |
| 253 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 254 | if (!res) { |
| 255 | dev_err(&pdev->dev, "Could not get platform resource\n"); |
| 256 | return -ENODEV; |
| 257 | } |
| 258 | |
| 259 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 260 | if (!priv) { |
| 261 | dev_err(&pdev->dev, "Could not allocate priv\n"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
| 265 | priv->comp = 4; /* basic setup */ |
| 266 | priv->dev = &pdev->dev; |
| 267 | spin_lock_init(&priv->lock); |
| 268 | priv->base = devm_ioremap_nocache(&pdev->dev, |
| 269 | res->start, resource_size(res)); |
| 270 | if (!priv->base) { |
| 271 | dev_err(&pdev->dev, "Unable to ioremap thermal register\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 272 | return -ENOMEM; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 273 | } |
| 274 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame^] | 275 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
| 276 | &rcar_thermal_zone_ops, NULL, 0, |
| 277 | IDLE_INTERVAL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 278 | if (IS_ERR(zone)) { |
| 279 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 280 | return PTR_ERR(zone); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | platform_set_drvdata(pdev, zone); |
| 284 | |
| 285 | dev_info(&pdev->dev, "proved\n"); |
| 286 | |
| 287 | return 0; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int rcar_thermal_remove(struct platform_device *pdev) |
| 291 | { |
| 292 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 293 | |
| 294 | thermal_zone_device_unregister(zone); |
| 295 | platform_set_drvdata(pdev, NULL); |
| 296 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static struct platform_driver rcar_thermal_driver = { |
| 301 | .driver = { |
| 302 | .name = "rcar_thermal", |
| 303 | }, |
| 304 | .probe = rcar_thermal_probe, |
| 305 | .remove = rcar_thermal_remove, |
| 306 | }; |
| 307 | module_platform_driver(rcar_thermal_driver); |
| 308 | |
| 309 | MODULE_LICENSE("GPL"); |
| 310 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); |
| 311 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |