2 * uniphier_thermal.c - Socionext UniPhier thermal driver
4 * Copyright 2014 Panasonic Corporation
5 * Copyright 2016-2017 Socionext Inc.
9 * Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 of
13 * the License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/bitops.h>
22 #include <linux/interrupt.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/regmap.h>
29 #include <linux/thermal.h>
31 #include "thermal_core.h"
35 * addresses are the offset from .block_base
37 #define PVTCTLEN 0x0000
38 #define PVTCTLEN_EN BIT(0)
40 #define PVTCTLMODE 0x0004
41 #define PVTCTLMODE_MASK 0xf
42 #define PVTCTLMODE_TEMPMON 0x5
44 #define EMONREPEAT 0x0040
45 #define EMONREPEAT_ENDLESS BIT(24)
46 #define EMONREPEAT_PERIOD GENMASK(3, 0)
47 #define EMONREPEAT_PERIOD_1000000 0x9
51 * addresses are the offset from .map_base
53 #define PVTCTLSEL 0x0900
54 #define PVTCTLSEL_MASK GENMASK(2, 0)
55 #define PVTCTLSEL_MONITOR 0
57 #define SETALERT0 0x0910
58 #define SETALERT1 0x0914
59 #define SETALERT2 0x0918
60 #define SETALERT_TEMP_OVF (GENMASK(7, 0) << 16)
61 #define SETALERT_TEMP_OVF_VALUE(val) (((val) & GENMASK(7, 0)) << 16)
62 #define SETALERT_EN BIT(0)
64 #define PMALERTINTCTL 0x0920
65 #define PMALERTINTCTL_CLR(ch) BIT(4 * (ch) + 2)
66 #define PMALERTINTCTL_SET(ch) BIT(4 * (ch) + 1)
67 #define PMALERTINTCTL_EN(ch) BIT(4 * (ch) + 0)
68 #define PMALERTINTCTL_MASK (GENMASK(10, 8) | GENMASK(6, 4) | \
74 #define TMODCOEF 0x0e5c
76 #define TMODSETUP0_EN BIT(30)
77 #define TMODSETUP0_VAL(val) (((val) & GENMASK(13, 0)) << 16)
78 #define TMODSETUP1_EN BIT(15)
79 #define TMODSETUP1_VAL(val) ((val) & GENMASK(14, 0))
81 /* SoC critical temperature */
82 #define CRITICAL_TEMP_LIMIT (120 * 1000)
84 /* Max # of alert channels */
85 #define ALERT_CH_NUM 3
87 /* SoC specific thermal sensor data */
88 struct uniphier_tm_soc_data
{
94 struct uniphier_tm_dev
{
95 struct regmap
*regmap
;
97 bool alert_en
[ALERT_CH_NUM
];
98 struct thermal_zone_device
*tz_dev
;
99 const struct uniphier_tm_soc_data
*data
;
102 static int uniphier_tm_initialize_sensor(struct uniphier_tm_dev
*tdev
)
104 struct regmap
*map
= tdev
->regmap
;
110 regmap_write_bits(map
, tdev
->data
->block_base
+ PVTCTLEN
,
114 * Since SoC has a calibrated value that was set in advance,
115 * TMODCOEF shows non-zero and PVT refers the value internally.
117 * If TMODCOEF shows zero, the boards don't have the calibrated
118 * value, and the driver has to set default value from DT.
120 ret
= regmap_read(map
, tdev
->data
->map_base
+ TMODCOEF
, &val
);
124 /* look for the default values in DT */
125 ret
= of_property_read_u32_array(tdev
->dev
->of_node
,
126 "socionext,tmod-calibration",
128 ARRAY_SIZE(tmod_calib
));
132 regmap_write(map
, tdev
->data
->tmod_setup_addr
,
133 TMODSETUP0_EN
| TMODSETUP0_VAL(tmod_calib
[0]) |
134 TMODSETUP1_EN
| TMODSETUP1_VAL(tmod_calib
[1]));
137 /* select temperature mode */
138 regmap_write_bits(map
, tdev
->data
->block_base
+ PVTCTLMODE
,
139 PVTCTLMODE_MASK
, PVTCTLMODE_TEMPMON
);
141 /* set monitoring period */
142 regmap_write_bits(map
, tdev
->data
->block_base
+ EMONREPEAT
,
143 EMONREPEAT_ENDLESS
| EMONREPEAT_PERIOD
,
144 EMONREPEAT_ENDLESS
| EMONREPEAT_PERIOD_1000000
);
146 /* set monitor mode */
147 regmap_write_bits(map
, tdev
->data
->map_base
+ PVTCTLSEL
,
148 PVTCTLSEL_MASK
, PVTCTLSEL_MONITOR
);
153 static void uniphier_tm_set_alert(struct uniphier_tm_dev
*tdev
, u32 ch
,
156 struct regmap
*map
= tdev
->regmap
;
158 /* set alert temperature */
159 regmap_write_bits(map
, tdev
->data
->map_base
+ SETALERT0
+ (ch
<< 2),
160 SETALERT_EN
| SETALERT_TEMP_OVF
,
162 SETALERT_TEMP_OVF_VALUE(temp
/ 1000));
165 static void uniphier_tm_enable_sensor(struct uniphier_tm_dev
*tdev
)
167 struct regmap
*map
= tdev
->regmap
;
171 for (i
= 0; i
< ALERT_CH_NUM
; i
++)
172 if (tdev
->alert_en
[i
])
173 bits
|= PMALERTINTCTL_EN(i
);
175 /* enable alert interrupt */
176 regmap_write_bits(map
, tdev
->data
->map_base
+ PMALERTINTCTL
,
177 PMALERTINTCTL_MASK
, bits
);
180 regmap_write_bits(map
, tdev
->data
->block_base
+ PVTCTLEN
,
181 PVTCTLEN_EN
, PVTCTLEN_EN
);
183 usleep_range(700, 1500); /* The spec note says at least 700us */
186 static void uniphier_tm_disable_sensor(struct uniphier_tm_dev
*tdev
)
188 struct regmap
*map
= tdev
->regmap
;
190 /* disable alert interrupt */
191 regmap_write_bits(map
, tdev
->data
->map_base
+ PMALERTINTCTL
,
192 PMALERTINTCTL_MASK
, 0);
195 regmap_write_bits(map
, tdev
->data
->block_base
+ PVTCTLEN
,
198 usleep_range(1000, 2000); /* The spec note says at least 1ms */
201 static int uniphier_tm_get_temp(void *data
, int *out_temp
)
203 struct uniphier_tm_dev
*tdev
= data
;
204 struct regmap
*map
= tdev
->regmap
;
208 ret
= regmap_read(map
, tdev
->data
->map_base
+ TMOD
, &temp
);
212 /* MSB of the TMOD field is a sign bit */
213 *out_temp
= sign_extend32(temp
, TMOD_WIDTH
- 1) * 1000;
218 static const struct thermal_zone_of_device_ops uniphier_of_thermal_ops
= {
219 .get_temp
= uniphier_tm_get_temp
,
222 static void uniphier_tm_irq_clear(struct uniphier_tm_dev
*tdev
)
224 u32 mask
= 0, bits
= 0;
227 for (i
= 0; i
< ALERT_CH_NUM
; i
++) {
228 mask
|= (PMALERTINTCTL_CLR(i
) | PMALERTINTCTL_SET(i
));
229 bits
|= PMALERTINTCTL_CLR(i
);
232 /* clear alert interrupt */
233 regmap_write_bits(tdev
->regmap
,
234 tdev
->data
->map_base
+ PMALERTINTCTL
, mask
, bits
);
237 static irqreturn_t
uniphier_tm_alarm_irq(int irq
, void *_tdev
)
239 struct uniphier_tm_dev
*tdev
= _tdev
;
241 disable_irq_nosync(irq
);
242 uniphier_tm_irq_clear(tdev
);
244 return IRQ_WAKE_THREAD
;
247 static irqreturn_t
uniphier_tm_alarm_irq_thread(int irq
, void *_tdev
)
249 struct uniphier_tm_dev
*tdev
= _tdev
;
251 thermal_zone_device_update(tdev
->tz_dev
, THERMAL_EVENT_UNSPECIFIED
);
256 static int uniphier_tm_probe(struct platform_device
*pdev
)
258 struct device
*dev
= &pdev
->dev
;
259 struct regmap
*regmap
;
260 struct device_node
*parent
;
261 struct uniphier_tm_dev
*tdev
;
262 const struct thermal_trip
*trips
;
263 int i
, ret
, irq
, ntrips
, crit_temp
= INT_MAX
;
265 tdev
= devm_kzalloc(dev
, sizeof(*tdev
), GFP_KERNEL
);
270 tdev
->data
= of_device_get_match_data(dev
);
271 if (WARN_ON(!tdev
->data
))
274 irq
= platform_get_irq(pdev
, 0);
278 /* get regmap from syscon node */
279 parent
= of_get_parent(dev
->of_node
); /* parent should be syscon node */
280 regmap
= syscon_node_to_regmap(parent
);
282 if (IS_ERR(regmap
)) {
283 dev_err(dev
, "failed to get regmap (error %ld)\n",
285 return PTR_ERR(regmap
);
287 tdev
->regmap
= regmap
;
289 ret
= uniphier_tm_initialize_sensor(tdev
);
291 dev_err(dev
, "failed to initialize sensor\n");
295 ret
= devm_request_threaded_irq(dev
, irq
, uniphier_tm_alarm_irq
,
296 uniphier_tm_alarm_irq_thread
,
301 platform_set_drvdata(pdev
, tdev
);
303 tdev
->tz_dev
= devm_thermal_zone_of_sensor_register(dev
, 0, tdev
,
304 &uniphier_of_thermal_ops
);
305 if (IS_ERR(tdev
->tz_dev
)) {
306 dev_err(dev
, "failed to register sensor device\n");
307 return PTR_ERR(tdev
->tz_dev
);
310 /* get trip points */
311 trips
= of_thermal_get_trip_points(tdev
->tz_dev
);
312 ntrips
= of_thermal_get_ntrips(tdev
->tz_dev
);
313 if (ntrips
> ALERT_CH_NUM
) {
314 dev_err(dev
, "thermal zone has too many trips\n");
318 /* set alert temperatures */
319 for (i
= 0; i
< ntrips
; i
++) {
320 if (trips
[i
].type
== THERMAL_TRIP_CRITICAL
&&
321 trips
[i
].temperature
< crit_temp
)
322 crit_temp
= trips
[i
].temperature
;
323 uniphier_tm_set_alert(tdev
, i
, trips
[i
].temperature
);
324 tdev
->alert_en
[i
] = true;
326 if (crit_temp
> CRITICAL_TEMP_LIMIT
) {
327 dev_err(dev
, "critical trip is over limit(>%d), or not set\n",
328 CRITICAL_TEMP_LIMIT
);
332 uniphier_tm_enable_sensor(tdev
);
337 static int uniphier_tm_remove(struct platform_device
*pdev
)
339 struct uniphier_tm_dev
*tdev
= platform_get_drvdata(pdev
);
342 uniphier_tm_disable_sensor(tdev
);
347 static const struct uniphier_tm_soc_data uniphier_pxs2_tm_data
= {
349 .block_base
= 0xe000,
350 .tmod_setup_addr
= 0xe904,
353 static const struct uniphier_tm_soc_data uniphier_ld20_tm_data
= {
355 .block_base
= 0xe800,
356 .tmod_setup_addr
= 0xe938,
359 static const struct of_device_id uniphier_tm_dt_ids
[] = {
361 .compatible
= "socionext,uniphier-pxs2-thermal",
362 .data
= &uniphier_pxs2_tm_data
,
365 .compatible
= "socionext,uniphier-ld20-thermal",
366 .data
= &uniphier_ld20_tm_data
,
369 .compatible
= "socionext,uniphier-pxs3-thermal",
370 .data
= &uniphier_ld20_tm_data
,
374 MODULE_DEVICE_TABLE(of
, uniphier_tm_dt_ids
);
376 static struct platform_driver uniphier_tm_driver
= {
377 .probe
= uniphier_tm_probe
,
378 .remove
= uniphier_tm_remove
,
380 .name
= "uniphier-thermal",
381 .of_match_table
= uniphier_tm_dt_ids
,
384 module_platform_driver(uniphier_tm_driver
);
386 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
387 MODULE_DESCRIPTION("UniPhier thermal driver");
388 MODULE_LICENSE("GPL v2");