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; | |
b2bbc6a2 | 45 | struct mutex 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 | { | |
b2bbc6a2 | 57 | return ioread32(priv->base + reg); |
1e426ffd KM |
58 | } |
59 | ||
60 | #if 0 /* no user at this point */ | |
61 | static void rcar_thermal_write(struct rcar_thermal_priv *priv, | |
62 | u32 reg, u32 data) | |
63 | { | |
1e426ffd | 64 | iowrite32(data, priv->base + reg); |
1e426ffd KM |
65 | } |
66 | #endif | |
67 | ||
68 | static void rcar_thermal_bset(struct rcar_thermal_priv *priv, u32 reg, | |
69 | u32 mask, u32 data) | |
70 | { | |
1e426ffd KM |
71 | u32 val; |
72 | ||
1e426ffd KM |
73 | val = ioread32(priv->base + reg); |
74 | val &= ~mask; | |
75 | val |= (data & mask); | |
76 | iowrite32(val, priv->base + reg); | |
1e426ffd KM |
77 | } |
78 | ||
79 | /* | |
80 | * zone device functions | |
81 | */ | |
82 | static int rcar_thermal_get_temp(struct thermal_zone_device *zone, | |
83 | unsigned long *temp) | |
84 | { | |
d12250ef | 85 | struct rcar_thermal_priv *priv = rcar_zone_to_priv(zone); |
f8f53e18 KM |
86 | struct device *dev = rcar_priv_to_dev(priv); |
87 | int i; | |
88 | int ctemp, old, new; | |
89 | ||
b2bbc6a2 KM |
90 | mutex_lock(&priv->lock); |
91 | ||
f8f53e18 KM |
92 | /* |
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); | |
97 | ||
98 | ctemp = 0; | |
99 | old = ~0; | |
100 | for (i = 0; i < 128; i++) { | |
1e426ffd KM |
101 | /* |
102 | * we need to wait 300us after changing comparator offset | |
103 | * to get stable temperature. | |
104 | * see "Usage Notes" on datasheet | |
105 | */ | |
1e426ffd KM |
106 | udelay(300); |
107 | ||
f8f53e18 KM |
108 | new = rcar_thermal_read(priv, THSSR) & CTEMP; |
109 | if (new == old) { | |
110 | ctemp = new; | |
1e426ffd KM |
111 | break; |
112 | } | |
f8f53e18 | 113 | old = new; |
1e426ffd KM |
114 | } |
115 | ||
f8f53e18 KM |
116 | if (!ctemp) { |
117 | dev_err(dev, "thermal sensor was broken\n"); | |
118 | return -EINVAL; | |
119 | } | |
120 | ||
121 | *temp = MCELSIUS((ctemp * 5) - 65); | |
122 | ||
b2bbc6a2 KM |
123 | mutex_unlock(&priv->lock); |
124 | ||
1e426ffd KM |
125 | return 0; |
126 | } | |
127 | ||
d2a73e22 KM |
128 | static 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 | ||
146 | static 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 | ||
164 | static 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 | ||
1e426ffd | 183 | static struct thermal_zone_device_ops rcar_thermal_zone_ops = { |
d2a73e22 KM |
184 | .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, | |
1e426ffd KM |
188 | }; |
189 | ||
190 | /* | |
191 | * platform functions | |
192 | */ | |
193 | static 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; | |
1e426ffd KM |
198 | |
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 | ||
1e426ffd | 211 | priv->dev = &pdev->dev; |
b2bbc6a2 | 212 | mutex_init(&priv->lock); |
1e426ffd KM |
213 | 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"); | |
4e8e2f64 | 217 | return -ENOMEM; |
1e426ffd KM |
218 | } |
219 | ||
d2a73e22 KM |
220 | zone = thermal_zone_device_register("rcar_thermal", 1, 0, priv, |
221 | &rcar_thermal_zone_ops, NULL, 0, | |
222 | IDLE_INTERVAL); | |
1e426ffd KM |
223 | if (IS_ERR(zone)) { |
224 | dev_err(&pdev->dev, "thermal zone device is NULL\n"); | |
4e8e2f64 | 225 | return PTR_ERR(zone); |
1e426ffd KM |
226 | } |
227 | ||
228 | platform_set_drvdata(pdev, zone); | |
229 | ||
230 | dev_info(&pdev->dev, "proved\n"); | |
231 | ||
232 | return 0; | |
1e426ffd KM |
233 | } |
234 | ||
235 | static int rcar_thermal_remove(struct platform_device *pdev) | |
236 | { | |
237 | struct thermal_zone_device *zone = platform_get_drvdata(pdev); | |
1e426ffd KM |
238 | |
239 | thermal_zone_device_unregister(zone); | |
240 | platform_set_drvdata(pdev, NULL); | |
241 | ||
1e426ffd KM |
242 | return 0; |
243 | } | |
244 | ||
245 | static struct platform_driver rcar_thermal_driver = { | |
246 | .driver = { | |
247 | .name = "rcar_thermal", | |
248 | }, | |
249 | .probe = rcar_thermal_probe, | |
250 | .remove = rcar_thermal_remove, | |
251 | }; | |
252 | module_platform_driver(rcar_thermal_driver); | |
253 | ||
254 | MODULE_LICENSE("GPL"); | |
255 | MODULE_DESCRIPTION("R-Car THS/TSC thermal sensor driver"); | |
256 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |