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; |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame^] | 45 | struct mutex 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 | { |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame^] | 57 | return ioread32(priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | #if 0 /* no user at this point */ |
| 61 | static void rcar_thermal_write(struct rcar_thermal_priv *priv, |
| 62 | u32 reg, u32 data) |
| 63 | { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 64 | iowrite32(data, priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | |
| 68 | static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg, |
| 69 | u32 mask, u32 data) |
| 70 | { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 71 | u32 val; |
| 72 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 73 | val = ioread32(priv->base + reg); |
| 74 | val &= ~mask; |
| 75 | val |= (data & mask); |
| 76 | iowrite32(val, priv->base + reg); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * zone device functions |
| 81 | */ |
| 82 | static int rcar_thermal_get_temp(struct thermal_zone_device *zone, |
| 83 | unsigned long *temp) |
| 84 | { |
Kuninori Morimoto | d12250e | 2012-11-26 02:32:20 +0000 | [diff] [blame] | 85 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 86 | struct device *dev = rcar_priv_to_dev(priv); |
| 87 | int i; |
| 88 | int ctemp, old, new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 89 | |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame^] | 90 | mutex_lock(&priv->lock); |
| 91 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 92 | /* |
| 93 | * TSC decides a value of CPTAP automatically, |
| 94 | * and this is the conditions which validate interrupt. |
| 95 | */ |
| 96 | rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 97 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 98 | ctemp = 0; |
| 99 | old = ~0; |
| 100 | for (i = 0; i < 128; i++) { |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 101 | /* |
| 102 | * we need to wait 300us after changing comparator offset |
| 103 | * to get stable temperature. |
| 104 | * see "Usage Notes" on datasheet |
| 105 | */ |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 106 | udelay(300); |
| 107 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 108 | new = rcar_thermal_read(priv, THSSR) & CTEMP; |
| 109 | if (new == old) { |
| 110 | ctemp = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 111 | break; |
| 112 | } |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 113 | old = new; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 114 | } |
| 115 | |
Kuninori Morimoto | f8f53e1 | 2013-01-31 09:03:11 +0000 | [diff] [blame] | 116 | if (!ctemp) { |
| 117 | dev_err(dev, "thermal sensor was broken\n"); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | *temp = MCELSIUS((ctemp * 5) - 65); |
| 122 | |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame^] | 123 | mutex_unlock(&priv->lock); |
| 124 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 128 | static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone, |
| 129 | int trip, enum thermal_trip_type *type) |
| 130 | { |
| 131 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 132 | |
| 133 | /* see rcar_thermal_get_temp() */ |
| 134 | switch (trip) { |
| 135 | case 0: /* +90 <= temp */ |
| 136 | *type = THERMAL_TRIP_CRITICAL; |
| 137 | break; |
| 138 | default: |
| 139 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone, |
| 147 | int trip, unsigned long *temp) |
| 148 | { |
| 149 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 150 | |
| 151 | /* see rcar_thermal_get_temp() */ |
| 152 | switch (trip) { |
| 153 | case 0: /* +90 <= temp */ |
| 154 | *temp = MCELSIUS(90); |
| 155 | break; |
| 156 | default: |
| 157 | dev_err(priv->dev, "rcar driver trip error\n"); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int rcar_thermal_notify(struct thermal_zone_device *zone, |
| 165 | int trip, enum thermal_trip_type type) |
| 166 | { |
| 167 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
| 168 | |
| 169 | switch (type) { |
| 170 | case THERMAL_TRIP_CRITICAL: |
| 171 | /* FIXME */ |
| 172 | dev_warn(priv->dev, |
| 173 | "Thermal reached to critical temperature\n"); |
| 174 | machine_power_off(); |
| 175 | break; |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 183 | 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] | 184 | .get_temp = rcar_thermal_get_temp, |
| 185 | .get_trip_type = rcar_thermal_get_trip_type, |
| 186 | .get_trip_temp = rcar_thermal_get_trip_temp, |
| 187 | .notify = rcar_thermal_notify, |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | /* |
| 191 | * platform functions |
| 192 | */ |
| 193 | static int rcar_thermal_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | struct thermal_zone_device *zone; |
| 196 | struct rcar_thermal_priv *priv; |
| 197 | struct resource *res; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 198 | |
| 199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 200 | if (!res) { |
| 201 | dev_err(&pdev->dev, "Could not get platform resource\n"); |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 206 | if (!priv) { |
| 207 | dev_err(&pdev->dev, "Could not allocate priv\n"); |
| 208 | return -ENOMEM; |
| 209 | } |
| 210 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 211 | priv->dev = &pdev->dev; |
Kuninori Morimoto | b2bbc6a | 2013-01-31 09:03:22 +0000 | [diff] [blame^] | 212 | mutex_init(&priv->lock); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 213 | priv->base = devm_ioremap_nocache(&pdev->dev, |
| 214 | res->start, resource_size(res)); |
| 215 | if (!priv->base) { |
| 216 | dev_err(&pdev->dev, "Unable to ioremap thermal register\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 217 | return -ENOMEM; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 218 | } |
| 219 | |
kuninori.morimoto.gx@renesas.com | d2a73e2 | 2012-12-02 18:48:41 -0800 | [diff] [blame] | 220 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
| 221 | &rcar_thermal_zone_ops, NULL, 0, |
| 222 | IDLE_INTERVAL); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 223 | if (IS_ERR(zone)) { |
| 224 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); |
Kuninori Morimoto | 4e8e2f6 | 2012-10-02 23:51:09 -0700 | [diff] [blame] | 225 | return PTR_ERR(zone); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | platform_set_drvdata(pdev, zone); |
| 229 | |
| 230 | dev_info(&pdev->dev, "proved\n"); |
| 231 | |
| 232 | return 0; |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int rcar_thermal_remove(struct platform_device *pdev) |
| 236 | { |
| 237 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 238 | |
| 239 | thermal_zone_device_unregister(zone); |
| 240 | platform_set_drvdata(pdev, NULL); |
| 241 | |
Kuninori Morimoto | 1e426ff | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static struct platform_driver rcar_thermal_driver = { |
| 246 | .driver = { |
| 247 | .name = "rcar_thermal", |
| 248 | }, |
| 249 | .probe = rcar_thermal_probe, |
| 250 | .remove = rcar_thermal_remove, |
| 251 | }; |
| 252 | module_platform_driver(rcar_thermal_driver); |
| 253 | |
| 254 | MODULE_LICENSE("GPL"); |
| 255 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); |
| 256 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |