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 | e9137a5 | 2013-01-31 09:03:46 +0000 | [diff] [blame^] | 32 | #define REG_THSCR 0x2c |
| 33 | #define REG_THSSR 0x30 |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 34 | |
| 35 | /* THSCR */ |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 36 | #define CPCTL (1 << 12) |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 37 | |
| 38 | /* THSSR */ |
| 39 | #define CTEMP 0x3f |
| 40 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 41 | struct rcar_thermal_common { |
| 42 | void __iomem *base; |
| 43 | struct device *dev; |
| 44 | struct list_head head; |
| 45 | }; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 46 | |
| 47 | struct rcar_thermal_priv { |
| 48 | void __iomem *base; |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 49 | struct rcar_thermal_common *common; |
| 50 | struct thermal_zone_device *zone; |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame] | 51 | struct mutex lock; |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 52 | struct list_head list; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 53 | }; |
| 54 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 55 | #define rcar_thermal_for_each_priv(pos, common) \ |
| 56 | list_for_each_entry(pos, &common->head, list) |
| 57 | |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 58 | #define MCELSIUS(temp) ((temp) * 1000) |
Kuninori Morimoto | 9dde8f8 | 2013-01-31 09:02:51 +0000 | [diff] [blame] | 59 | #define rcar_zone_to_priv(zone) ((zone)->devdata) |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 60 | #define rcar_priv_to_dev(priv) ((priv)->common->dev) |
| 61 | #define rcar_has_irq_support(priv) ((priv)->common->base) |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 62 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 63 | /* |
| 64 | * basic functions |
| 65 | */ |
Kuninori Morimoto | e9137a5 | 2013-01-31 09:03:46 +0000 | [diff] [blame^] | 66 | #if 0 |
| 67 | #define rcar_thermal_common_read(c, r) \ |
| 68 | _rcar_thermal_common_read(c, COMMON_ ##r) |
| 69 | static u32 _rcar_thermal_common_read(struct rcar_thermal_common *common, |
| 70 | u32 reg) |
| 71 | { |
| 72 | return ioread32(common->base + reg); |
| 73 | } |
| 74 | |
| 75 | #define rcar_thermal_common_write(c, r, d) \ |
| 76 | _rcar_thermal_common_write(c, COMMON_ ##r, d) |
| 77 | static void _rcar_thermal_common_write(struct rcar_thermal_common *common, |
| 78 | u32 reg, u32 data) |
| 79 | { |
| 80 | iowrite32(data, common->base + reg); |
| 81 | } |
| 82 | |
| 83 | #define rcar_thermal_common_bset(c, r, m, d) \ |
| 84 | _rcar_thermal_common_bset(c, COMMON_ ##r, m, d) |
| 85 | static void _rcar_thermal_common_bset(struct rcar_thermal_common *common, |
| 86 | u32 reg, u32 mask, u32 data) |
| 87 | { |
| 88 | u32 val; |
| 89 | |
| 90 | val = ioread32(common->base + reg); |
| 91 | val &= ~mask; |
| 92 | val |= (data & mask); |
| 93 | iowrite32(val, common->base + reg); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #define rcar_thermal_read(p, r) _rcar_thermal_read(p, REG_ ##r) |
| 98 | static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg) |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 99 | { |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame] | 100 | return ioread32(priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #if 0 /* no user at this point */ |
Kuninori Morimoto | e9137a5 | 2013-01-31 09:03:46 +0000 | [diff] [blame^] | 104 | #define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d) |
| 105 | static void _rcar_thermal_write(struct rcar_thermal_priv *priv, |
| 106 | u32 reg, u32 data) |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 107 | { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 108 | iowrite32(data, priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 109 | } |
| 110 | #endif |
| 111 | |
Kuninori Morimoto | e9137a5 | 2013-01-31 09:03:46 +0000 | [diff] [blame^] | 112 | #define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d) |
| 113 | static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg, |
| 114 | u32 mask, u32 data) |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 115 | { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 116 | u32 val; |
| 117 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 118 | val = ioread32(priv->base + reg); |
| 119 | val &= ~mask; |
| 120 | val |= (data & mask); |
| 121 | iowrite32(val, priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * zone device functions |
| 126 | */ |
| 127 | static int rcar_thermal_get_temp(struct thermal_zone_device *zone, |
| 128 | unsigned long *temp) |
| 129 | { |
Kuninori Morimoto | d12250e | 2012-11-26 02:32:20 +0000 | [diff] [blame] | 130 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 131 | struct device *dev = rcar_priv_to_dev(priv); |
| 132 | int i; |
| 133 | int ctemp, old, new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 134 | |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame] | 135 | mutex_lock(&priv->lock); |
| 136 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 137 | /* |
| 138 | * TSC decides a value of CPTAP automatically, |
| 139 | * and this is the conditions which validate interrupt. |
| 140 | */ |
| 141 | rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 142 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 143 | ctemp = 0; |
| 144 | old = ~0; |
| 145 | for (i = 0; i < 128; i++) { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 146 | /* |
| 147 | * we need to wait 300us after changing comparator offset |
| 148 | * to get stable temperature. |
| 149 | * see "Usage Notes" on datasheet |
| 150 | */ |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 151 | udelay(300); |
| 152 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 153 | new = rcar_thermal_read(priv, THSSR) & CTEMP; |
| 154 | if (new == old) { |
| 155 | ctemp = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 156 | break; |
| 157 | } |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 158 | old = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 159 | } |
| 160 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 161 | if (!ctemp) { |
| 162 | dev_err(dev, "thermal sensor was broken\n"); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | *temp = MCELSIUS((ctemp * 5) - 65); |
| 167 | |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame] | 168 | mutex_unlock(&priv->lock); |
| 169 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 173 | static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone, |
| 174 | int trip, enum thermal_trip_type *type) |
| 175 | { |
| 176 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 177 | struct device *dev = rcar_priv_to_dev(priv); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 178 | |
| 179 | /* see rcar_thermal_get_temp() */ |
| 180 | switch (trip) { |
| 181 | case 0: /* +90 <= temp */ |
| 182 | *type = THERMAL_TRIP_CRITICAL; |
| 183 | break; |
| 184 | default: |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 185 | dev_err(dev, "rcar driver trip error\n"); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone, |
| 193 | int trip, unsigned long *temp) |
| 194 | { |
| 195 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 196 | struct device *dev = rcar_priv_to_dev(priv); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 197 | |
| 198 | /* see rcar_thermal_get_temp() */ |
| 199 | switch (trip) { |
| 200 | case 0: /* +90 <= temp */ |
| 201 | *temp = MCELSIUS(90); |
| 202 | break; |
| 203 | default: |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 204 | dev_err(dev, "rcar driver trip error\n"); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int rcar_thermal_notify(struct thermal_zone_device *zone, |
| 212 | int trip, enum thermal_trip_type type) |
| 213 | { |
| 214 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 215 | struct device *dev = rcar_priv_to_dev(priv); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 216 | |
| 217 | switch (type) { |
| 218 | case THERMAL_TRIP_CRITICAL: |
| 219 | /* FIXME */ |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 220 | dev_warn(dev, "Thermal reached to critical temperature\n"); |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 221 | machine_power_off(); |
| 222 | break; |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 230 | 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] | 231 | .get_temp = rcar_thermal_get_temp, |
| 232 | .get_trip_type = rcar_thermal_get_trip_type, |
| 233 | .get_trip_temp = rcar_thermal_get_trip_temp, |
| 234 | .notify = rcar_thermal_notify, |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * platform functions |
| 239 | */ |
| 240 | static int rcar_thermal_probe(struct platform_device *pdev) |
| 241 | { |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 242 | struct rcar_thermal_common *common; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 243 | struct rcar_thermal_priv *priv; |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 244 | struct device *dev = &pdev->dev; |
| 245 | struct resource *res, *irq; |
| 246 | int mres = 0; |
| 247 | int i; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 248 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 249 | common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL); |
| 250 | if (!common) { |
| 251 | dev_err(dev, "Could not allocate common\n"); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 252 | return -ENOMEM; |
| 253 | } |
| 254 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 255 | INIT_LIST_HEAD(&common->head); |
| 256 | common->dev = dev; |
| 257 | |
| 258 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 259 | if (irq) { |
| 260 | /* |
| 261 | * platform has IRQ support. |
| 262 | * Then, drier use common register |
| 263 | */ |
| 264 | res = platform_get_resource(pdev, IORESOURCE_MEM, mres++); |
| 265 | if (!res) { |
| 266 | dev_err(dev, "Could not get platform resource\n"); |
| 267 | return -ENODEV; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * rcar_has_irq_support() will be enabled |
| 272 | */ |
| 273 | common->base = devm_request_and_ioremap(dev, res); |
| 274 | if (!common->base) { |
| 275 | dev_err(dev, "Unable to ioremap thermal register\n"); |
| 276 | return -ENOMEM; |
| 277 | } |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 278 | } |
| 279 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 280 | for (i = 0;; i++) { |
| 281 | res = platform_get_resource(pdev, IORESOURCE_MEM, mres++); |
| 282 | if (!res) |
| 283 | break; |
| 284 | |
| 285 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 286 | if (!priv) { |
| 287 | dev_err(dev, "Could not allocate priv\n"); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | priv->base = devm_request_and_ioremap(dev, res); |
| 292 | if (!priv->base) { |
| 293 | dev_err(dev, "Unable to ioremap priv register\n"); |
| 294 | return -ENOMEM; |
| 295 | } |
| 296 | |
| 297 | priv->common = common; |
| 298 | mutex_init(&priv->lock); |
| 299 | INIT_LIST_HEAD(&priv->list); |
| 300 | |
| 301 | priv->zone = thermal_zone_device_register("rcar_thermal", |
| 302 | 1, 0, priv, |
| 303 | &rcar_thermal_zone_ops, NULL, 0, |
| 304 | IDLE_INTERVAL); |
| 305 | if (IS_ERR(priv->zone)) { |
| 306 | dev_err(dev, "can't register thermal zone\n"); |
| 307 | goto error_unregister; |
| 308 | } |
| 309 | |
| 310 | list_move_tail(&priv->list, &common->head); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 311 | } |
| 312 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 313 | platform_set_drvdata(pdev, common); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 314 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 315 | dev_info(dev, "%d sensor proved\n", i); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 316 | |
| 317 | return 0; |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 318 | |
| 319 | error_unregister: |
| 320 | rcar_thermal_for_each_priv(priv, common) |
| 321 | thermal_zone_device_unregister(priv->zone); |
| 322 | |
| 323 | return -ENODEV; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int rcar_thermal_remove(struct platform_device *pdev) |
| 327 | { |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 328 | struct rcar_thermal_common *common = platform_get_drvdata(pdev); |
| 329 | struct rcar_thermal_priv *priv; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 330 | |
Kuninori Morimoto | 3676d1d | 2013-01-31 09:03:33 +0000 | [diff] [blame] | 331 | rcar_thermal_for_each_priv(priv, common) |
| 332 | thermal_zone_device_unregister(priv->zone); |
| 333 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 334 | platform_set_drvdata(pdev, NULL); |
| 335 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static struct platform_driver rcar_thermal_driver = { |
| 340 | .driver = { |
| 341 | .name = "rcar_thermal", |
| 342 | }, |
| 343 | .probe = rcar_thermal_probe, |
| 344 | .remove = rcar_thermal_remove, |
| 345 | }; |
| 346 | module_platform_driver(rcar_thermal_driver); |
| 347 | |
| 348 | MODULE_LICENSE("GPL"); |
| 349 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); |
| 350 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |