blob: cf6aa98956b97026c1217718ff630644f84181c3 [file] [log] [blame]
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +10001/*
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.comd2a73e22012-12-02 18:48:41 -080025#include <linux/reboot.h>
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100026#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/thermal.h>
29
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -080030#define IDLE_INTERVAL 5000
31
Kuninori Morimotoe9137a52013-01-31 09:03:46 +000032#define REG_THSCR 0x2c
33#define REG_THSSR 0x30
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100034
35/* THSCR */
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000036#define CPCTL (1 << 12)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100037
38/* THSSR */
39#define CTEMP 0x3f
40
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000041struct rcar_thermal_common {
42 void __iomem *base;
43 struct device *dev;
44 struct list_head head;
45};
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100046
47struct rcar_thermal_priv {
48 void __iomem *base;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000049 struct rcar_thermal_common *common;
50 struct thermal_zone_device *zone;
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +000051 struct mutex lock;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000052 struct list_head list;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100053};
54
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000055#define rcar_thermal_for_each_priv(pos, common) \
56 list_for_each_entry(pos, &common->head, list)
57
Kuninori Morimotoc4997032012-11-26 02:32:06 +000058#define MCELSIUS(temp) ((temp) * 1000)
Kuninori Morimoto9dde8f82013-01-31 09:02:51 +000059#define rcar_zone_to_priv(zone) ((zone)->devdata)
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +000060#define rcar_priv_to_dev(priv) ((priv)->common->dev)
61#define rcar_has_irq_support(priv) ((priv)->common->base)
Kuninori Morimotoc4997032012-11-26 02:32:06 +000062
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100063/*
64 * basic functions
65 */
Kuninori Morimotoe9137a52013-01-31 09:03:46 +000066#if 0
67#define rcar_thermal_common_read(c, r) \
68 _rcar_thermal_common_read(c, COMMON_ ##r)
69static 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)
77static 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)
85static 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)
98static u32 _rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100099{
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000100 return ioread32(priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000101}
102
103#if 0 /* no user at this point */
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000104#define rcar_thermal_write(p, r, d) _rcar_thermal_write(p, REG_ ##r, d)
105static void _rcar_thermal_write(struct rcar_thermal_priv *priv,
106 u32 reg, u32 data)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000107{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000108 iowrite32(data, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000109}
110#endif
111
Kuninori Morimotoe9137a52013-01-31 09:03:46 +0000112#define rcar_thermal_bset(p, r, m, d) _rcar_thermal_bset(p, REG_ ##r, m, d)
113static void _rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
114 u32 mask, u32 data)
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000115{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000116 u32 val;
117
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000118 val = ioread32(priv->base + reg);
119 val &= ~mask;
120 val |= (data & mask);
121 iowrite32(val, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000122}
123
124/*
125 * zone device functions
126 */
127static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
128 unsigned long *temp)
129{
Kuninori Morimotod12250e2012-11-26 02:32:20 +0000130 struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000131 struct device *dev = rcar_priv_to_dev(priv);
132 int i;
133 int ctemp, old, new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000134
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000135 mutex_lock(&priv->lock);
136
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000137 /*
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 Morimoto1e426ff2012-07-21 10:53:48 +1000142
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000143 ctemp = 0;
144 old = ~0;
145 for (i = 0; i < 128; i++) {
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000146 /*
147 * we need to wait 300us after changing comparator offset
148 * to get stable temperature.
149 * see "Usage Notes" on datasheet
150 */
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000151 udelay(300);
152
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000153 new = rcar_thermal_read(priv, THSSR) & CTEMP;
154 if (new == old) {
155 ctemp = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000156 break;
157 }
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000158 old = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000159 }
160
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000161 if (!ctemp) {
162 dev_err(dev, "thermal sensor was broken\n");
163 return -EINVAL;
164 }
165
166 *temp = MCELSIUS((ctemp * 5) - 65);
167
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000168 mutex_unlock(&priv->lock);
169
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000170 return 0;
171}
172
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800173static 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 Morimoto3676d1d2013-01-31 09:03:33 +0000177 struct device *dev = rcar_priv_to_dev(priv);
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800178
179 /* see rcar_thermal_get_temp() */
180 switch (trip) {
181 case 0: /* +90 <= temp */
182 *type = THERMAL_TRIP_CRITICAL;
183 break;
184 default:
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000185 dev_err(dev, "rcar driver trip error\n");
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800186 return -EINVAL;
187 }
188
189 return 0;
190}
191
192static 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 Morimoto3676d1d2013-01-31 09:03:33 +0000196 struct device *dev = rcar_priv_to_dev(priv);
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800197
198 /* see rcar_thermal_get_temp() */
199 switch (trip) {
200 case 0: /* +90 <= temp */
201 *temp = MCELSIUS(90);
202 break;
203 default:
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000204 dev_err(dev, "rcar driver trip error\n");
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800205 return -EINVAL;
206 }
207
208 return 0;
209}
210
211static 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 Morimoto3676d1d2013-01-31 09:03:33 +0000215 struct device *dev = rcar_priv_to_dev(priv);
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800216
217 switch (type) {
218 case THERMAL_TRIP_CRITICAL:
219 /* FIXME */
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000220 dev_warn(dev, "Thermal reached to critical temperature\n");
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800221 machine_power_off();
222 break;
223 default:
224 break;
225 }
226
227 return 0;
228}
229
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000230static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800231 .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 Morimoto1e426ff2012-07-21 10:53:48 +1000235};
236
237/*
238 * platform functions
239 */
240static int rcar_thermal_probe(struct platform_device *pdev)
241{
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000242 struct rcar_thermal_common *common;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000243 struct rcar_thermal_priv *priv;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000244 struct device *dev = &pdev->dev;
245 struct resource *res, *irq;
246 int mres = 0;
247 int i;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000248
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000249 common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL);
250 if (!common) {
251 dev_err(dev, "Could not allocate common\n");
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000252 return -ENOMEM;
253 }
254
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000255 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 Morimoto1e426ff2012-07-21 10:53:48 +1000278 }
279
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000280 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 Morimoto1e426ff2012-07-21 10:53:48 +1000311 }
312
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000313 platform_set_drvdata(pdev, common);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000314
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000315 dev_info(dev, "%d sensor proved\n", i);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000316
317 return 0;
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000318
319error_unregister:
320 rcar_thermal_for_each_priv(priv, common)
321 thermal_zone_device_unregister(priv->zone);
322
323 return -ENODEV;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000324}
325
326static int rcar_thermal_remove(struct platform_device *pdev)
327{
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000328 struct rcar_thermal_common *common = platform_get_drvdata(pdev);
329 struct rcar_thermal_priv *priv;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000330
Kuninori Morimoto3676d1d2013-01-31 09:03:33 +0000331 rcar_thermal_for_each_priv(priv, common)
332 thermal_zone_device_unregister(priv->zone);
333
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000334 platform_set_drvdata(pdev, NULL);
335
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000336 return 0;
337}
338
339static struct platform_driver rcar_thermal_driver = {
340 .driver = {
341 .name = "rcar_thermal",
342 },
343 .probe = rcar_thermal_probe,
344 .remove = rcar_thermal_remove,
345};
346module_platform_driver(rcar_thermal_driver);
347
348MODULE_LICENSE("GPL");
349MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
350MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");