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 */ |
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 | |
| 41 | |
| 42 | struct rcar_thermal_priv { |
| 43 | void __iomem *base; |
| 44 | struct device *dev; |
| 45 | spinlock_t lock; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 46 | }; |
| 47 | |
Kuninori Morimoto | c499703 | 2012-11-26 02:32:06 +0000 | [diff] [blame] | 48 | #define MCELSIUS(temp) ((temp) * 1000) |
Kuninori Morimoto | 9dde8f8 | 2013-01-31 09:02:51 +0000 | [diff] [blame] | 49 | #define rcar_zone_to_priv(zone) ((zone)->devdata) |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 50 | #define rcar_priv_to_dev(priv) ((priv)->dev) |
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 | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 106 | struct device *dev = rcar_priv_to_dev(priv); |
| 107 | int i; |
| 108 | int ctemp, old, new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 109 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 110 | /* |
| 111 | * TSC decides a value of CPTAP automatically, |
| 112 | * and this is the conditions which validate interrupt. |
| 113 | */ |
| 114 | rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 115 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 116 | ctemp = 0; |
| 117 | old = ~0; |
| 118 | for (i = 0; i < 128; i++) { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 119 | /* |
| 120 | * we need to wait 300us after changing comparator offset |
| 121 | * to get stable temperature. |
| 122 | * see "Usage Notes" on datasheet |
| 123 | */ |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 124 | udelay(300); |
| 125 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 126 | new = rcar_thermal_read(priv, THSSR) & CTEMP; |
| 127 | if (new == old) { |
| 128 | ctemp = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 129 | break; |
| 130 | } |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 131 | old = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 132 | } |
| 133 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame^] | 134 | if (!ctemp) { |
| 135 | dev_err(dev, "thermal sensor was broken\n"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | *temp = MCELSIUS((ctemp * 5) - 65); |
| 140 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 144 | static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone, |
| 145 | int trip, enum thermal_trip_type *type) |
| 146 | { |
| 147 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 148 | |
| 149 | /* see rcar_thermal_get_temp() */ |
| 150 | switch (trip) { |
| 151 | case 0: /* +90 <= temp */ |
| 152 | *type = THERMAL_TRIP_CRITICAL; |
| 153 | break; |
| 154 | default: |
| 155 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone, |
| 163 | int trip, unsigned long *temp) |
| 164 | { |
| 165 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 166 | |
| 167 | /* see rcar_thermal_get_temp() */ |
| 168 | switch (trip) { |
| 169 | case 0: /* +90 <= temp */ |
| 170 | *temp = MCELSIUS(90); |
| 171 | break; |
| 172 | default: |
| 173 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int rcar_thermal_notify(struct thermal_zone_device *zone, |
| 181 | int trip, enum thermal_trip_type type) |
| 182 | { |
| 183 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 184 | |
| 185 | switch (type) { |
| 186 | case THERMAL_TRIP_CRITICAL: |
| 187 | /* FIXME */ |
| 188 | dev_warn(priv->dev, |
| 189 | "Thermal reached to critical temperature\n"); |
| 190 | machine_power_off(); |
| 191 | break; |
| 192 | default: |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 199 | 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] | 200 | .get_temp = rcar_thermal_get_temp, |
| 201 | .get_trip_type = rcar_thermal_get_trip_type, |
| 202 | .get_trip_temp = rcar_thermal_get_trip_temp, |
| 203 | .notify = rcar_thermal_notify, |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | /* |
| 207 | * platform functions |
| 208 | */ |
| 209 | static int rcar_thermal_probe(struct platform_device *pdev) |
| 210 | { |
| 211 | struct thermal_zone_device *zone; |
| 212 | struct rcar_thermal_priv *priv; |
| 213 | struct resource *res; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 214 | |
| 215 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 216 | if (!res) { |
| 217 | dev_err(&pdev->dev, "Could not get platform resource\n"); |
| 218 | return -ENODEV; |
| 219 | } |
| 220 | |
| 221 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 222 | if (!priv) { |
| 223 | dev_err(&pdev->dev, "Could not allocate priv\n"); |
| 224 | return -ENOMEM; |
| 225 | } |
| 226 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 227 | priv->dev = &pdev->dev; |
| 228 | spin_lock_init(&priv->lock); |
| 229 | priv->base = devm_ioremap_nocache(&pdev->dev, |
| 230 | res->start, resource_size(res)); |
| 231 | if (!priv->base) { |
| 232 | dev_err(&pdev->dev, "Unable to ioremap thermal register\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 233 | return -ENOMEM; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 234 | } |
| 235 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 236 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
| 237 | &rcar_thermal_zone_ops, NULL, 0, |
| 238 | IDLE_INTERVAL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 239 | if (IS_ERR(zone)) { |
| 240 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 241 | return PTR_ERR(zone); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | platform_set_drvdata(pdev, zone); |
| 245 | |
| 246 | dev_info(&pdev->dev, "proved\n"); |
| 247 | |
| 248 | return 0; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int rcar_thermal_remove(struct platform_device *pdev) |
| 252 | { |
| 253 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 254 | |
| 255 | thermal_zone_device_unregister(zone); |
| 256 | platform_set_drvdata(pdev, NULL); |
| 257 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static struct platform_driver rcar_thermal_driver = { |
| 262 | .driver = { |
| 263 | .name = "rcar_thermal", |
| 264 | }, |
| 265 | .probe = rcar_thermal_probe, |
| 266 | .remove = rcar_thermal_remove, |
| 267 | }; |
| 268 | module_platform_driver(rcar_thermal_driver); |
| 269 | |
| 270 | MODULE_LICENSE("GPL"); |
| 271 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); |
| 272 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |