blob: e19b267f76d6ec5389130015d41a04ecb3842a23 [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 Morimoto1e426ff2012-07-21 10:53:48 +100032#define THSCR 0x2c
33#define THSSR 0x30
34
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
41
42struct rcar_thermal_priv {
43 void __iomem *base;
44 struct device *dev;
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +000045 struct mutex lock;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100046};
47
Kuninori Morimotoc4997032012-11-26 02:32:06 +000048#define MCELSIUS(temp) ((temp) * 1000)
Kuninori Morimoto9dde8f82013-01-31 09:02:51 +000049#define rcar_zone_to_priv(zone) ((zone)->devdata)
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000050#define rcar_priv_to_dev(priv) ((priv)->dev)
Kuninori Morimotoc4997032012-11-26 02:32:06 +000051
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100052/*
53 * basic functions
54 */
55static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
56{
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +000057 return ioread32(priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100058}
59
60#if 0 /* no user at this point */
61static void rcar_thermal_write(struct rcar_thermal_priv *priv,
62 u32 reg, u32 data)
63{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100064 iowrite32(data, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100065}
66#endif
67
68static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
69 u32 mask, u32 data)
70{
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100071 u32 val;
72
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100073 val = ioread32(priv->base + reg);
74 val &= ~mask;
75 val |= (data & mask);
76 iowrite32(val, priv->base + reg);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100077}
78
79/*
80 * zone device functions
81 */
82static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
83 unsigned long *temp)
84{
Kuninori Morimotod12250e2012-11-26 02:32:20 +000085 struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone);
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000086 struct device *dev = rcar_priv_to_dev(priv);
87 int i;
88 int ctemp, old, new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100089
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +000090 mutex_lock(&priv->lock);
91
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000092 /*
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 Morimoto1e426ff2012-07-21 10:53:48 +100097
Kuninori Morimotof8f53e12013-01-31 09:03:11 +000098 ctemp = 0;
99 old = ~0;
100 for (i = 0; i < 128; i++) {
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000101 /*
102 * we need to wait 300us after changing comparator offset
103 * to get stable temperature.
104 * see "Usage Notes" on datasheet
105 */
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000106 udelay(300);
107
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000108 new = rcar_thermal_read(priv, THSSR) & CTEMP;
109 if (new == old) {
110 ctemp = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000111 break;
112 }
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000113 old = new;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000114 }
115
Kuninori Morimotof8f53e12013-01-31 09:03:11 +0000116 if (!ctemp) {
117 dev_err(dev, "thermal sensor was broken\n");
118 return -EINVAL;
119 }
120
121 *temp = MCELSIUS((ctemp * 5) - 65);
122
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000123 mutex_unlock(&priv->lock);
124
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000125 return 0;
126}
127
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800128static 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
146static 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
164static 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 Morimoto1e426ff2012-07-21 10:53:48 +1000183static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800184 .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 Morimoto1e426ff2012-07-21 10:53:48 +1000188};
189
190/*
191 * platform functions
192 */
193static 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 Morimoto1e426ff2012-07-21 10:53:48 +1000198
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 Morimoto1e426ff2012-07-21 10:53:48 +1000211 priv->dev = &pdev->dev;
Kuninori Morimotob2bbc6a2013-01-31 09:03:22 +0000212 mutex_init(&priv->lock);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000213 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 Morimoto4e8e2f62012-10-02 23:51:09 -0700217 return -ENOMEM;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000218 }
219
kuninori.morimoto.gx@renesas.comd2a73e22012-12-02 18:48:41 -0800220 zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv,
221 &rcar_thermal_zone_ops, NULL, 0,
222 IDLE_INTERVAL);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000223 if (IS_ERR(zone)) {
224 dev_err(&pdev->dev, "thermal zone device is NULL\n");
Kuninori Morimoto4e8e2f62012-10-02 23:51:09 -0700225 return PTR_ERR(zone);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000226 }
227
228 platform_set_drvdata(pdev, zone);
229
230 dev_info(&pdev->dev, "proved\n");
231
232 return 0;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000233}
234
235static int rcar_thermal_remove(struct platform_device *pdev)
236{
237 struct thermal_zone_device *zone = platform_get_drvdata(pdev);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000238
239 thermal_zone_device_unregister(zone);
240 platform_set_drvdata(pdev, NULL);
241
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000242 return 0;
243}
244
245static struct platform_driver rcar_thermal_driver = {
246 .driver = {
247 .name = "rcar_thermal",
248 },
249 .probe = rcar_thermal_probe,
250 .remove = rcar_thermal_remove,
251};
252module_platform_driver(rcar_thermal_driver);
253
254MODULE_LICENSE("GPL");
255MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
256MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");