blob: f2678ff0ed2bb575bf7ad2a9f82597cf0e853b28 [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>
25#include <linux/slab.h>
26#include <linux/spinlock.h>
27#include <linux/thermal.h>
28
29#define THSCR 0x2c
30#define THSSR 0x30
31
32/* THSCR */
33#define CPTAP 0xf
34
35/* THSSR */
36#define CTEMP 0x3f
37
38
39struct rcar_thermal_priv {
40 void __iomem *base;
41 struct device *dev;
42 spinlock_t lock;
43 u32 comp;
44};
45
Kuninori Morimotoc4997032012-11-26 02:32:06 +000046#define MCELSIUS(temp) ((temp) * 1000)
47
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +100048/*
49 * basic functions
50 */
51static u32 rcar_thermal_read(struct rcar_thermal_priv *priv, u32 reg)
52{
53 unsigned long flags;
54 u32 ret;
55
56 spin_lock_irqsave(&priv->lock, flags);
57
58 ret = ioread32(priv->base + reg);
59
60 spin_unlock_irqrestore(&priv->lock, flags);
61
62 return ret;
63}
64
65#if 0 /* no user at this point */
66static void rcar_thermal_write(struct rcar_thermal_priv *priv,
67 u32 reg, u32 data)
68{
69 unsigned long flags;
70
71 spin_lock_irqsave(&priv->lock, flags);
72
73 iowrite32(data, priv->base + reg);
74
75 spin_unlock_irqrestore(&priv->lock, flags);
76}
77#endif
78
79static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg,
80 u32 mask, u32 data)
81{
82 unsigned long flags;
83 u32 val;
84
85 spin_lock_irqsave(&priv->lock, flags);
86
87 val = ioread32(priv->base + reg);
88 val &= ~mask;
89 val |= (data & mask);
90 iowrite32(val, priv->base + reg);
91
92 spin_unlock_irqrestore(&priv->lock, flags);
93}
94
95/*
96 * zone device functions
97 */
98static int rcar_thermal_get_temp(struct thermal_zone_device *zone,
99 unsigned long *temp)
100{
101 struct rcar_thermal_priv *priv = zone->devdata;
102 int val, min, max, tmp;
103
104 tmp = -200; /* default */
105 while (1) {
106 if (priv->comp < 1 || priv->comp > 12) {
107 dev_err(priv->dev,
108 "THSSR invalid data (%d)\n", priv->comp);
109 priv->comp = 4; /* for next thermal */
110 return -EINVAL;
111 }
112
113 /*
114 * THS comparator offset and the reference temperature
115 *
116 * Comparator | reference | Temperature field
117 * offset | temperature | measurement
118 * | (degrees C) | (degrees C)
119 * -------------+---------------+-------------------
120 * 1 | -45 | -45 to -30
121 * 2 | -30 | -30 to -15
122 * 3 | -15 | -15 to 0
123 * 4 | 0 | 0 to +15
124 * 5 | +15 | +15 to +30
125 * 6 | +30 | +30 to +45
126 * 7 | +45 | +45 to +60
127 * 8 | +60 | +60 to +75
128 * 9 | +75 | +75 to +90
129 * 10 | +90 | +90 to +105
130 * 11 | +105 | +105 to +120
131 * 12 | +120 | +120 to +135
132 */
133
134 /* calculate thermal limitation */
135 min = (priv->comp * 15) - 60;
136 max = min + 15;
137
138 /*
139 * we need to wait 300us after changing comparator offset
140 * to get stable temperature.
141 * see "Usage Notes" on datasheet
142 */
143 rcar_thermal_bset(priv, THSCR, CPTAP, priv->comp);
144 udelay(300);
145
146 /* calculate current temperature */
147 val = rcar_thermal_read(priv, THSSR) & CTEMP;
148 val = (val * 5) - 65;
149
150 dev_dbg(priv->dev, "comp/min/max/val = %d/%d/%d/%d\n",
151 priv->comp, min, max, val);
152
153 /*
154 * If val is same as min/max, then,
155 * it should try again on next comparator.
156 * But the val might be correct temperature.
157 * Keep it on "tmp" and compare with next val.
158 */
159 if (tmp == val)
160 break;
161
162 if (val <= min) {
163 tmp = min;
164 priv->comp--; /* try again */
165 } else if (val >= max) {
166 tmp = max;
167 priv->comp++; /* try again */
168 } else {
169 tmp = val;
170 break;
171 }
172 }
173
Kuninori Morimotoc4997032012-11-26 02:32:06 +0000174 *temp = MCELSIUS(tmp);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000175 return 0;
176}
177
178static struct thermal_zone_device_ops rcar_thermal_zone_ops = {
179 .get_temp = rcar_thermal_get_temp,
180};
181
182/*
183 * platform functions
184 */
185static int rcar_thermal_probe(struct platform_device *pdev)
186{
187 struct thermal_zone_device *zone;
188 struct rcar_thermal_priv *priv;
189 struct resource *res;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000190
191 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192 if (!res) {
193 dev_err(&pdev->dev, "Could not get platform resource\n");
194 return -ENODEV;
195 }
196
197 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
198 if (!priv) {
199 dev_err(&pdev->dev, "Could not allocate priv\n");
200 return -ENOMEM;
201 }
202
203 priv->comp = 4; /* basic setup */
204 priv->dev = &pdev->dev;
205 spin_lock_init(&priv->lock);
206 priv->base = devm_ioremap_nocache(&pdev->dev,
207 res->start, resource_size(res));
208 if (!priv->base) {
209 dev_err(&pdev->dev, "Unable to ioremap thermal register\n");
Kuninori Morimoto4e8e2f62012-10-02 23:51:09 -0700210 return -ENOMEM;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000211 }
212
Devendra Naga608f62b2012-10-31 17:46:10 +0900213 zone = thermal_zone_device_register("rcar_thermal", 0, 0, priv,
Durgadoss R50125a92012-09-18 11:04:56 +0530214 &rcar_thermal_zone_ops, NULL, 0, 0);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000215 if (IS_ERR(zone)) {
216 dev_err(&pdev->dev, "thermal zone device is NULL\n");
Kuninori Morimoto4e8e2f62012-10-02 23:51:09 -0700217 return PTR_ERR(zone);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000218 }
219
220 platform_set_drvdata(pdev, zone);
221
222 dev_info(&pdev->dev, "proved\n");
223
224 return 0;
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000225}
226
227static int rcar_thermal_remove(struct platform_device *pdev)
228{
229 struct thermal_zone_device *zone = platform_get_drvdata(pdev);
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000230
231 thermal_zone_device_unregister(zone);
232 platform_set_drvdata(pdev, NULL);
233
Kuninori Morimoto1e426ff2012-07-21 10:53:48 +1000234 return 0;
235}
236
237static struct platform_driver rcar_thermal_driver = {
238 .driver = {
239 .name = "rcar_thermal",
240 },
241 .probe = rcar_thermal_probe,
242 .remove = rcar_thermal_remove,
243};
244module_platform_driver(rcar_thermal_driver);
245
246MODULE_LICENSE("GPL");
247MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver");
248MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");