2 * Thermal device driver for DA9062 and DA9061
3 * Copyright (C) 2017 Dialog Semiconductor
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 /* When over-temperature is reached, an interrupt from the device will be
17 * triggered. Following this event the interrupt will be disabled and
18 * periodic transmission of uevents (HOT trip point) should define the
19 * first level of temperature supervision. It is expected that any final
20 * implementation of the thermal driver will include a .notify() function
21 * to implement these uevents to userspace.
23 * These uevents are intended to indicate non-invasive temperature control
24 * of the system, where the necessary measures for cooling are the
25 * responsibility of the host software. Once the temperature falls again,
26 * the IRQ is re-enabled so the start of a new over-temperature event can
27 * be detected without constant software monitoring.
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/regmap.h>
36 #include <linux/thermal.h>
37 #include <linux/workqueue.h>
39 #include <linux/mfd/da9062/core.h>
40 #include <linux/mfd/da9062/registers.h>
42 /* Minimum, maximum and default polling millisecond periods are provided
43 * here as an example. It is expected that any final implementation to also
44 * include a modification of these settings to match the required
47 #define DA9062_DEFAULT_POLLING_MS_PERIOD 3000
48 #define DA9062_MAX_POLLING_MS_PERIOD 10000
49 #define DA9062_MIN_POLLING_MS_PERIOD 1000
51 #define DA9062_MILLI_CELSIUS(t) ((t) * 1000)
53 struct da9062_thermal_config
{
57 struct da9062_thermal
{
59 struct delayed_work work
;
60 struct thermal_zone_device
*zone
;
61 enum thermal_device_mode mode
;
62 struct mutex lock
; /* protection for da9062_thermal temperature */
65 const struct da9062_thermal_config
*config
;
69 static void da9062_thermal_poll_on(struct work_struct
*work
)
71 struct da9062_thermal
*thermal
= container_of(work
,
72 struct da9062_thermal
,
79 ret
= regmap_write(thermal
->hw
->regmap
,
81 DA9062AA_E_TEMP_MASK
);
84 "Cannot clear the TJUNC temperature status\n");
88 /* Now read E_TEMP again: it is acting like a status bit.
89 * If over-temperature, then this status will be true.
90 * If not over-temperature, this status will be false.
92 ret
= regmap_read(thermal
->hw
->regmap
,
97 "Cannot check the TJUNC temperature status\n");
101 if (val
& DA9062AA_E_TEMP_MASK
) {
102 mutex_lock(&thermal
->lock
);
103 thermal
->temperature
= DA9062_MILLI_CELSIUS(125);
104 mutex_unlock(&thermal
->lock
);
105 thermal_zone_device_update(thermal
->zone
,
106 THERMAL_EVENT_UNSPECIFIED
);
108 delay
= msecs_to_jiffies(thermal
->zone
->passive_delay
);
109 queue_delayed_work(system_freezable_wq
, &thermal
->work
, delay
);
113 mutex_lock(&thermal
->lock
);
114 thermal
->temperature
= DA9062_MILLI_CELSIUS(0);
115 mutex_unlock(&thermal
->lock
);
116 thermal_zone_device_update(thermal
->zone
,
117 THERMAL_EVENT_UNSPECIFIED
);
120 enable_irq(thermal
->irq
);
123 static irqreturn_t
da9062_thermal_irq_handler(int irq
, void *data
)
125 struct da9062_thermal
*thermal
= data
;
127 disable_irq_nosync(thermal
->irq
);
128 queue_delayed_work(system_freezable_wq
, &thermal
->work
, 0);
133 static int da9062_thermal_get_mode(struct thermal_zone_device
*z
,
134 enum thermal_device_mode
*mode
)
136 struct da9062_thermal
*thermal
= z
->devdata
;
137 *mode
= thermal
->mode
;
141 static int da9062_thermal_get_trip_type(struct thermal_zone_device
*z
,
143 enum thermal_trip_type
*type
)
145 struct da9062_thermal
*thermal
= z
->devdata
;
149 *type
= THERMAL_TRIP_HOT
;
152 dev_err(thermal
->dev
,
153 "Driver does not support more than 1 trip-wire\n");
160 static int da9062_thermal_get_trip_temp(struct thermal_zone_device
*z
,
164 struct da9062_thermal
*thermal
= z
->devdata
;
168 *temp
= DA9062_MILLI_CELSIUS(125);
171 dev_err(thermal
->dev
,
172 "Driver does not support more than 1 trip-wire\n");
179 static int da9062_thermal_get_temp(struct thermal_zone_device
*z
,
182 struct da9062_thermal
*thermal
= z
->devdata
;
184 mutex_lock(&thermal
->lock
);
185 *temp
= thermal
->temperature
;
186 mutex_unlock(&thermal
->lock
);
191 static struct thermal_zone_device_ops da9062_thermal_ops
= {
192 .get_temp
= da9062_thermal_get_temp
,
193 .get_mode
= da9062_thermal_get_mode
,
194 .get_trip_type
= da9062_thermal_get_trip_type
,
195 .get_trip_temp
= da9062_thermal_get_trip_temp
,
198 static const struct da9062_thermal_config da9062_config
= {
199 .name
= "da9062-thermal",
202 static const struct of_device_id da9062_compatible_reg_id_table
[] = {
203 { .compatible
= "dlg,da9062-thermal", .data
= &da9062_config
},
207 MODULE_DEVICE_TABLE(of
, da9062_compatible_reg_id_table
);
209 static int da9062_thermal_probe(struct platform_device
*pdev
)
211 struct da9062
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
212 struct da9062_thermal
*thermal
;
213 unsigned int pp_tmp
= DA9062_DEFAULT_POLLING_MS_PERIOD
;
214 const struct of_device_id
*match
;
217 match
= of_match_node(da9062_compatible_reg_id_table
,
222 if (pdev
->dev
.of_node
) {
223 if (!of_property_read_u32(pdev
->dev
.of_node
,
224 "polling-delay-passive",
226 if (pp_tmp
< DA9062_MIN_POLLING_MS_PERIOD
||
227 pp_tmp
> DA9062_MAX_POLLING_MS_PERIOD
) {
229 "Out-of-range polling period %d ms\n",
231 pp_tmp
= DA9062_DEFAULT_POLLING_MS_PERIOD
;
236 thermal
= devm_kzalloc(&pdev
->dev
, sizeof(struct da9062_thermal
),
243 thermal
->config
= match
->data
;
245 thermal
->mode
= THERMAL_DEVICE_ENABLED
;
246 thermal
->dev
= &pdev
->dev
;
248 INIT_DELAYED_WORK(&thermal
->work
, da9062_thermal_poll_on
);
249 mutex_init(&thermal
->lock
);
251 thermal
->zone
= thermal_zone_device_register(thermal
->config
->name
,
253 &da9062_thermal_ops
, NULL
, pp_tmp
,
255 if (IS_ERR(thermal
->zone
)) {
256 dev_err(&pdev
->dev
, "Cannot register thermal zone device\n");
257 ret
= PTR_ERR(thermal
->zone
);
262 "TJUNC temperature polling period set at %d ms\n",
263 thermal
->zone
->passive_delay
);
265 ret
= platform_get_irq_byname(pdev
, "THERMAL");
267 dev_err(&pdev
->dev
, "Failed to get platform IRQ.\n");
272 ret
= request_threaded_irq(thermal
->irq
, NULL
,
273 da9062_thermal_irq_handler
,
274 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
278 "Failed to request thermal device IRQ.\n");
282 platform_set_drvdata(pdev
, thermal
);
286 thermal_zone_device_unregister(thermal
->zone
);
291 static int da9062_thermal_remove(struct platform_device
*pdev
)
293 struct da9062_thermal
*thermal
= platform_get_drvdata(pdev
);
295 free_irq(thermal
->irq
, thermal
);
296 cancel_delayed_work_sync(&thermal
->work
);
297 thermal_zone_device_unregister(thermal
->zone
);
301 static struct platform_driver da9062_thermal_driver
= {
302 .probe
= da9062_thermal_probe
,
303 .remove
= da9062_thermal_remove
,
305 .name
= "da9062-thermal",
306 .of_match_table
= da9062_compatible_reg_id_table
,
310 module_platform_driver(da9062_thermal_driver
);
312 MODULE_AUTHOR("Steve Twiss");
313 MODULE_DESCRIPTION("Thermal TJUNC device driver for Dialog DA9062 and DA9061");
314 MODULE_LICENSE("GPL");
315 MODULE_ALIAS("platform:da9062-thermal");