1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/spinlock.h>
27#include <linux/thermal.h>
28
29#include "thermal_core.h"
30
31
32#define REG_GEN3_IRQSTR 0x04
33#define REG_GEN3_IRQMSK 0x08
34#define REG_GEN3_IRQCTL 0x0C
35#define REG_GEN3_IRQEN 0x10
36#define REG_GEN3_IRQTEMP1 0x14
37#define REG_GEN3_IRQTEMP2 0x18
38#define REG_GEN3_IRQTEMP3 0x1C
39#define REG_GEN3_CTSR 0x20
40#define REG_GEN3_THCTR 0x20
41#define REG_GEN3_TEMP 0x28
42#define REG_GEN3_THCODE1 0x50
43#define REG_GEN3_THCODE2 0x54
44#define REG_GEN3_THCODE3 0x58
45
46
47#define IRQ_TEMP1 BIT(0)
48#define IRQ_TEMP2 BIT(1)
49#define IRQ_TEMP3 BIT(2)
50#define IRQ_TEMPD1 BIT(3)
51#define IRQ_TEMPD2 BIT(4)
52#define IRQ_TEMPD3 BIT(5)
53
54
55#define CTSR_PONM BIT(8)
56#define CTSR_AOUT BIT(7)
57#define CTSR_THBGR BIT(5)
58#define CTSR_VMEN BIT(4)
59#define CTSR_VMST BIT(1)
60#define CTSR_THSST BIT(0)
61
62
63#define THCTR_PONM BIT(6)
64#define THCTR_THSST BIT(0)
65
66#define CTEMP_MASK 0xFFF
67
68#define MCELSIUS(temp) ((temp) * 1000)
69#define GEN3_FUSE_MASK 0xFFF
70
71#define TSC_MAX_NUM 3
72
73
74struct equation_coefs {
75 int a1;
76 int b1;
77 int a2;
78 int b2;
79};
80
81struct rcar_gen3_thermal_tsc {
82 void __iomem *base;
83 struct thermal_zone_device *zone;
84 struct equation_coefs coef;
85 int low;
86 int high;
87};
88
89struct rcar_gen3_thermal_priv {
90 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
91 unsigned int num_tscs;
92 spinlock_t lock;
93 const struct rcar_gen3_thermal_data *data;
94};
95
96struct rcar_gen3_thermal_data {
97 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
98};
99
100static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
101 u32 reg)
102{
103 return ioread32(tsc->base + reg);
104}
105
106static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
107 u32 reg, u32 data)
108{
109 iowrite32(data, tsc->base + reg);
110}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129#define FIXPT_SHIFT 7
130#define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
131#define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
132#define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
133#define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
134
135#define RCAR3_THERMAL_GRAN 500
136
137
138#define TJ_1 96
139#define TJ_3 -41
140
141static void rcar_gen3_thermal_calc_coefs(struct equation_coefs *coef,
142 int *ptat, int *thcode)
143{
144 int tj_2;
145
146
147
148
149
150
151
152 tj_2 = (FIXPT_INT((ptat[1] - ptat[2]) * 137)
153 / (ptat[0] - ptat[2])) - FIXPT_INT(41);
154
155 coef->a1 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[2]),
156 tj_2 - FIXPT_INT(TJ_3));
157 coef->b1 = FIXPT_INT(thcode[2]) - coef->a1 * TJ_3;
158
159 coef->a2 = FIXPT_DIV(FIXPT_INT(thcode[1] - thcode[0]),
160 tj_2 - FIXPT_INT(TJ_1));
161 coef->b2 = FIXPT_INT(thcode[0]) - coef->a2 * TJ_1;
162}
163
164static int rcar_gen3_thermal_round(int temp)
165{
166 int result, round_offs;
167
168 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
169 -RCAR3_THERMAL_GRAN / 2;
170 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
171 return result * RCAR3_THERMAL_GRAN;
172}
173
174static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
175{
176 struct rcar_gen3_thermal_tsc *tsc = devdata;
177 int mcelsius, val1, val2;
178 u32 reg;
179
180
181 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
182
183 val1 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1, tsc->coef.a1);
184 val2 = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2, tsc->coef.a2);
185 mcelsius = FIXPT_TO_MCELSIUS((val1 + val2) / 2);
186
187
188 if ((mcelsius < MCELSIUS(-40)) || (mcelsius > MCELSIUS(125)))
189 return -EIO;
190
191
192 *temp = rcar_gen3_thermal_round(mcelsius);
193
194 return 0;
195}
196
197static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
198 int mcelsius)
199{
200 int celsius, val1, val2;
201
202 celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
203 val1 = celsius * tsc->coef.a1 + tsc->coef.b1;
204 val2 = celsius * tsc->coef.a2 + tsc->coef.b2;
205
206 return INT_FIXPT((val1 + val2) / 2);
207}
208
209static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
210{
211 struct rcar_gen3_thermal_tsc *tsc = devdata;
212
213 low = clamp_val(low, -40000, 125000);
214 high = clamp_val(high, -40000, 125000);
215
216 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
217 rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
218
219 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
220 rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
221
222 tsc->low = low;
223 tsc->high = high;
224
225 return 0;
226}
227
228static struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
229 .get_temp = rcar_gen3_thermal_get_temp,
230 .set_trips = rcar_gen3_thermal_set_trips,
231};
232
233static void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv *priv, bool on)
234{
235 unsigned int i;
236 u32 val = on ? IRQ_TEMPD1 | IRQ_TEMP2 : 0;
237
238 for (i = 0; i < priv->num_tscs; i++)
239 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQMSK, val);
240}
241
242static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
243{
244 struct rcar_gen3_thermal_priv *priv = data;
245 u32 status;
246 int i, ret = IRQ_HANDLED;
247
248 spin_lock(&priv->lock);
249 for (i = 0; i < priv->num_tscs; i++) {
250 status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
251 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
252 if (status)
253 ret = IRQ_WAKE_THREAD;
254 }
255
256 if (ret == IRQ_WAKE_THREAD)
257 rcar_thermal_irq_set(priv, false);
258
259 spin_unlock(&priv->lock);
260
261 return ret;
262}
263
264static irqreturn_t rcar_gen3_thermal_irq_thread(int irq, void *data)
265{
266 struct rcar_gen3_thermal_priv *priv = data;
267 unsigned long flags;
268 int i;
269
270 for (i = 0; i < priv->num_tscs; i++)
271 thermal_zone_device_update(priv->tscs[i]->zone,
272 THERMAL_EVENT_UNSPECIFIED);
273
274 spin_lock_irqsave(&priv->lock, flags);
275 rcar_thermal_irq_set(priv, true);
276 spin_unlock_irqrestore(&priv->lock, flags);
277
278 return IRQ_HANDLED;
279}
280
281static void r8a7795_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
282{
283 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
284 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
285
286 usleep_range(1000, 2000);
287
288 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
289
290 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
291 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
292 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
293
294 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
295 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
296
297 usleep_range(100, 200);
298
299 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
300 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
301 CTSR_VMST | CTSR_THSST);
302
303 usleep_range(1000, 2000);
304}
305
306static void r8a7796_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
307{
308 u32 reg_val;
309
310 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
311 reg_val &= ~THCTR_PONM;
312 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
313
314 usleep_range(1000, 2000);
315
316 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
317 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
318 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN, IRQ_TEMPD1 | IRQ_TEMP2);
319
320 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
321 reg_val |= THCTR_THSST;
322 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
323
324 usleep_range(1000, 2000);
325}
326
327static const struct rcar_gen3_thermal_data r8a7795_data = {
328 .thermal_init = r8a7795_thermal_init,
329};
330
331static const struct rcar_gen3_thermal_data r8a7796_data = {
332 .thermal_init = r8a7796_thermal_init,
333};
334
335static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
336 { .compatible = "renesas,r8a7795-thermal", .data = &r8a7795_data},
337 { .compatible = "renesas,r8a7796-thermal", .data = &r8a7796_data},
338 {},
339};
340MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
341
342static int rcar_gen3_thermal_remove(struct platform_device *pdev)
343{
344 struct device *dev = &pdev->dev;
345
346 pm_runtime_put(dev);
347 pm_runtime_disable(dev);
348
349 return 0;
350}
351
352static int rcar_gen3_thermal_probe(struct platform_device *pdev)
353{
354 struct rcar_gen3_thermal_priv *priv;
355 struct device *dev = &pdev->dev;
356 struct resource *res;
357 struct thermal_zone_device *zone;
358 int ret, irq, i;
359 char *irqname;
360
361
362
363 int ptat[3] = { 2351, 1509, 435 };
364 int thcode[TSC_MAX_NUM][3] = {
365 { 3248, 2800, 2221 },
366 { 3245, 2795, 2216 },
367 { 3250, 2805, 2237 },
368 };
369
370 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
371 if (!priv)
372 return -ENOMEM;
373
374 priv->data = of_device_get_match_data(dev);
375
376 spin_lock_init(&priv->lock);
377
378 platform_set_drvdata(pdev, priv);
379
380
381
382
383
384
385 for (i = 0; i < 2; i++) {
386 irq = platform_get_irq(pdev, i);
387 if (irq < 0)
388 return irq;
389
390 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
391 dev_name(dev), i);
392 if (!irqname)
393 return -ENOMEM;
394
395 ret = devm_request_threaded_irq(dev, irq, rcar_gen3_thermal_irq,
396 rcar_gen3_thermal_irq_thread,
397 IRQF_SHARED, irqname, priv);
398 if (ret)
399 return ret;
400 }
401
402 pm_runtime_enable(dev);
403 pm_runtime_get_sync(dev);
404
405 for (i = 0; i < TSC_MAX_NUM; i++) {
406 struct rcar_gen3_thermal_tsc *tsc;
407
408 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
409 if (!res)
410 break;
411
412 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
413 if (!tsc) {
414 ret = -ENOMEM;
415 goto error_unregister;
416 }
417
418 tsc->base = devm_ioremap_resource(dev, res);
419 if (IS_ERR(tsc->base)) {
420 ret = PTR_ERR(tsc->base);
421 goto error_unregister;
422 }
423
424 priv->tscs[i] = tsc;
425
426 priv->data->thermal_init(tsc);
427 rcar_gen3_thermal_calc_coefs(&tsc->coef, ptat, thcode[i]);
428
429 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
430 &rcar_gen3_tz_of_ops);
431 if (IS_ERR(zone)) {
432 dev_err(dev, "Can't register thermal zone\n");
433 ret = PTR_ERR(zone);
434 goto error_unregister;
435 }
436 tsc->zone = zone;
437
438 ret = of_thermal_get_ntrips(tsc->zone);
439 if (ret < 0)
440 goto error_unregister;
441
442 dev_info(dev, "TSC%d: Loaded %d trip points\n", i, ret);
443 }
444
445 priv->num_tscs = i;
446
447 if (!priv->num_tscs) {
448 ret = -ENODEV;
449 goto error_unregister;
450 }
451
452 rcar_thermal_irq_set(priv, true);
453
454 return 0;
455
456error_unregister:
457 rcar_gen3_thermal_remove(pdev);
458
459 return ret;
460}
461
462static int __maybe_unused rcar_gen3_thermal_suspend(struct device *dev)
463{
464 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
465
466 rcar_thermal_irq_set(priv, false);
467
468 return 0;
469}
470
471static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
472{
473 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
474 unsigned int i;
475
476 for (i = 0; i < priv->num_tscs; i++) {
477 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
478
479 priv->data->thermal_init(tsc);
480 rcar_gen3_thermal_set_trips(tsc, tsc->low, tsc->high);
481 }
482
483 rcar_thermal_irq_set(priv, true);
484
485 return 0;
486}
487
488static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, rcar_gen3_thermal_suspend,
489 rcar_gen3_thermal_resume);
490
491static struct platform_driver rcar_gen3_thermal_driver = {
492 .driver = {
493 .name = "rcar_gen3_thermal",
494 .pm = &rcar_gen3_thermal_pm_ops,
495 .of_match_table = rcar_gen3_thermal_dt_ids,
496 },
497 .probe = rcar_gen3_thermal_probe,
498 .remove = rcar_gen3_thermal_remove,
499};
500module_platform_driver(rcar_gen3_thermal_driver);
501
502MODULE_LICENSE("GPL v2");
503MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
504MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");
505