]>
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 KM |
30 | #define IDLE_INTERVAL 5000 |
31 | ||
1e426ffd KM |
32 | #define THSCR 0x2c |
33 | #define THSSR 0x30 | |
34 | ||
35 | /* THSCR */ | |
f8f53e18 | 36 | #define CPCTL (1 << 12) |
1e426ffd KM |
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; | |
1e426ffd KM |
46 | }; |
47 | ||
c499703e | 48 | #define MCELSIUS(temp) ((temp) * 1000) |
9dde8f86 | 49 | #define rcar_zone_to_priv(zone) ((zone)->devdata) |
f8f53e18 | 50 | #define rcar_priv_to_dev(priv) ((priv)->dev) |
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); |
f8f53e18 KM |
106 | struct device *dev = rcar_priv_to_dev(priv); |
107 | int i; | |
108 | int ctemp, old, new; | |
109 | ||
110 | /* | |
111 | * TSC decides a value of CPTAP automatically, | |
112 | * and this is the conditions which validate interrupt. | |
113 | */ | |
114 | rcar_thermal_bset(priv, THSCR, CPCTL, CPCTL); | |
115 | ||
116 | ctemp = 0; | |
117 | old = ~0; | |
118 | for (i = 0; i < 128; i++) { | |
1e426ffd KM |
119 | /* |
120 | * we need to wait 300us after changing comparator offset | |
121 | * to get stable temperature. | |
122 | * see "Usage Notes" on datasheet | |
123 | */ | |
1e426ffd KM |
124 | udelay(300); |
125 | ||
f8f53e18 KM |
126 | new = rcar_thermal_read(priv, THSSR) & CTEMP; |
127 | if (new == old) { | |
128 | ctemp = new; | |
1e426ffd KM |
129 | break; |
130 | } | |
f8f53e18 | 131 | old = new; |
1e426ffd KM |
132 | } |
133 | ||
f8f53e18 KM |
134 | if (!ctemp) { |
135 | dev_err(dev, "thermal sensor was broken\n"); | |
136 | return -EINVAL; | |
137 | } | |
138 | ||
139 | *temp = MCELSIUS((ctemp * 5) - 65); | |
140 | ||
1e426ffd KM |
141 | return 0; |
142 | } | |
143 | ||
d2a73e22 KM |
144 | static int rcar_thermal_get_trip_type(struct thermal_zone_device *zone, |
145 | int trip, enum thermal_trip_type *type) | |
146 | { | |
147 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); | |
148 | ||
149 | /* see rcar_thermal_get_temp() */ | |
150 | switch (trip) { | |
151 | case 0: /* +90 <= temp */ | |
152 | *type = THERMAL_TRIP_CRITICAL; | |
153 | break; | |
154 | default: | |
155 | dev_err(priv->dev, "rcar driver trip error\n"); | |
156 | return -EINVAL; | |
157 | } | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | static int rcar_thermal_get_trip_temp(struct thermal_zone_device *zone, | |
163 | int trip, unsigned long *temp) | |
164 | { | |
165 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); | |
166 | ||
167 | /* see rcar_thermal_get_temp() */ | |
168 | switch (trip) { | |
169 | case 0: /* +90 <= temp */ | |
170 | *temp = MCELSIUS(90); | |
171 | break; | |
172 | default: | |
173 | dev_err(priv->dev, "rcar driver trip error\n"); | |
174 | return -EINVAL; | |
175 | } | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static int rcar_thermal_notify(struct thermal_zone_device *zone, | |
181 | int trip, enum thermal_trip_type type) | |
182 | { | |
183 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); | |
184 | ||
185 | switch (type) { | |
186 | case THERMAL_TRIP_CRITICAL: | |
187 | /* FIXME */ | |
188 | dev_warn(priv->dev, | |
189 | "Thermal reached to critical temperature\n"); | |
190 | machine_power_off(); | |
191 | break; | |
192 | default: | |
193 | break; | |
194 | } | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
1e426ffd | 199 | static struct thermal_zone_device_ops rcar_thermal_zone_ops = { |
d2a73e22 KM |
200 | .get_temp = rcar_thermal_get_temp, |
201 | .get_trip_type = rcar_thermal_get_trip_type, | |
202 | .get_trip_temp = rcar_thermal_get_trip_temp, | |
203 | .notify = rcar_thermal_notify, | |
1e426ffd KM |
204 | }; |
205 | ||
206 | /* | |
207 | * platform functions | |
208 | */ | |
209 | static int rcar_thermal_probe(struct platform_device *pdev) | |
210 | { | |
211 | struct thermal_zone_device *zone; | |
212 | struct rcar_thermal_priv *priv; | |
213 | struct resource *res; | |
1e426ffd KM |
214 | |
215 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
216 | if (!res) { | |
217 | dev_err(&pdev->dev, "Could not get platform resource\n"); | |
218 | return -ENODEV; | |
219 | } | |
220 | ||
221 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); | |
222 | if (!priv) { | |
223 | dev_err(&pdev->dev, "Could not allocate priv\n"); | |
224 | return -ENOMEM; | |
225 | } | |
226 | ||
1e426ffd KM |
227 | priv->dev = &pdev->dev; |
228 | spin_lock_init(&priv->lock); | |
229 | priv->base = devm_ioremap_nocache(&pdev->dev, | |
230 | res->start, resource_size(res)); | |
231 | if (!priv->base) { | |
232 | dev_err(&pdev->dev, "Unable to ioremap thermal register\n"); | |
4e8e2f64 | 233 | return -ENOMEM; |
1e426ffd KM |
234 | } |
235 | ||
d2a73e22 KM |
236 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
237 | &rcar_thermal_zone_ops, NULL, 0, | |
238 | IDLE_INTERVAL); | |
1e426ffd KM |
239 | if (IS_ERR(zone)) { |
240 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); | |
4e8e2f64 | 241 | return PTR_ERR(zone); |
1e426ffd KM |
242 | } |
243 | ||
244 | platform_set_drvdata(pdev, zone); | |
245 | ||
246 | dev_info(&pdev->dev, "proved\n"); | |
247 | ||
248 | return 0; | |
1e426ffd KM |
249 | } |
250 | ||
251 | static int rcar_thermal_remove(struct platform_device *pdev) | |
252 | { | |
253 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); | |
1e426ffd KM |
254 | |
255 | thermal_zone_device_unregister(zone); | |
256 | platform_set_drvdata(pdev, NULL); | |
257 | ||
1e426ffd KM |
258 | return 0; |
259 | } | |
260 | ||
261 | static struct platform_driver rcar_thermal_driver = { | |
262 | .driver = { | |
263 | .name = "rcar_thermal", | |
264 | }, | |
265 | .probe = rcar_thermal_probe, | |
266 | .remove = rcar_thermal_remove, | |
267 | }; | |
268 | module_platform_driver(rcar_thermal_driver); | |
269 | ||
270 | MODULE_LICENSE("GPL"); | |
271 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); | |
272 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |