Commit | Line | Data |
---|---|---|
1e426ffd KM |
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> | |
d2a73e22 | 25 | #include <linux/reboot.h> |
1e426ffd KM |
26 | #include <linux/slab.h> |
27 | #include <linux/spinlock.h> | |
28 | #include <linux/thermal.h> | |
29 | ||
d2a73e22 | 30 | #define IDLE_INTERVAL 5000 |
31 | ||
1e426ffd KM |
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 | ||
c499703e | 49 | #define MCELSIUS(temp) ((temp) * 1000) |
9dde8f86 | 50 | #define rcar_zone_to_priv(zone) ((zone)->devdata) |
c499703e | 51 | |
1e426ffd KM |
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 | { | |
d12250ef | 105 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
1e426ffd KM |
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 | ||
c499703e | 178 | *temp = MCELSIUS(tmp); |
1e426ffd KM |
179 | return 0; |
180 | } | |
181 | ||
d2a73e22 | 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 | ||
1e426ffd | 237 | static struct thermal_zone_device_ops rcar_thermal_zone_ops = { |
d2a73e22 | 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, | |
1e426ffd KM |
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; | |
1e426ffd KM |
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"); | |
4e8e2f64 | 272 | return -ENOMEM; |
1e426ffd KM |
273 | } |
274 | ||
d2a73e22 | 275 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
276 | &rcar_thermal_zone_ops, NULL, 0, | |
277 | IDLE_INTERVAL); | |
1e426ffd KM |
278 | if (IS_ERR(zone)) { |
279 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); | |
4e8e2f64 | 280 | return PTR_ERR(zone); |
1e426ffd KM |
281 | } |
282 | ||
283 | platform_set_drvdata(pdev, zone); | |
284 | ||
285 | dev_info(&pdev->dev, "proved\n"); | |
286 | ||
287 | return 0; | |
1e426ffd KM |
288 | } |
289 | ||
290 | static int rcar_thermal_remove(struct platform_device *pdev) | |
291 | { | |
292 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); | |
1e426ffd KM |
293 | |
294 | thermal_zone_device_unregister(zone); | |
295 | platform_set_drvdata(pdev, NULL); | |
296 | ||
1e426ffd KM |
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>"); |