1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Thermal device driver for DA9062 and DA9061
* Copyright (C) 2017 Dialog Semiconductor
*/
/* When over-temperature is reached, an interrupt from the device will be
* triggered. Following this event the interrupt will be disabled and
* periodic transmission of uevents (HOT trip point) should define the
* first level of temperature supervision. It is expected that any final
* implementation of the thermal driver will include a .notify() function
* to implement these uevents to userspace.
*
* These uevents are intended to indicate non-invasive temperature control
* of the system, where the necessary measures for cooling are the
* responsibility of the host software. Once the temperature falls again,
* the IRQ is re-enabled so the start of a new over-temperature event can
* be detected without constant software monitoring.
*/
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/thermal.h>
#include <linux/workqueue.h>
#include <linux/mfd/da9062/core.h>
#include <linux/mfd/da9062/registers.h>
/* Minimum, maximum and default polling millisecond periods are provided
* here as an example. It is expected that any final implementation to also
* include a modification of these settings to match the required
* application.
*/
#define DA9062_DEFAULT_POLLING_MS_PERIOD 3000
#define DA9062_MAX_POLLING_MS_PERIOD 10000
#define DA9062_MIN_POLLING_MS_PERIOD 1000
#define DA9062_MILLI_CELSIUS(t) ((t) * 1000)
struct da9062_thermal_config {
const char *name;
};
struct da9062_thermal {
struct da9062 *hw;
struct delayed_work work;
struct thermal_zone_device *zone;
struct mutex lock; /* protection for da9062_thermal temperature */
int temperature;
int irq;
const struct da9062_thermal_config *config;
struct device *dev;
};
static void da9062_thermal_poll_on(struct work_struct *work)
{
struct da9062_thermal *thermal = container_of(work,
struct da9062_thermal,
work.work);
unsigned long delay;
unsigned int val;
int ret;
/* clear E_TEMP */
ret = regmap_write(thermal->hw->regmap,
DA9062AA_EVENT_B,
DA9062AA_E_TEMP_MASK);
if (ret < 0) {
dev_err(thermal->dev,
"Cannot clear the TJUNC temperature status\n");
goto err_enable_irq;
}
/* Now read E_TEMP again: it is acting like a status bit.
* If over-temperature, then this status will be true.
* If not over-temperature, this status will be false.
*/
ret = regmap_read(thermal->hw->regmap,
DA9062AA_EVENT_B,
&val);
if (ret < 0) {
dev_err(thermal->dev,
"Cannot check the TJUNC temperature status\n");
goto err_enable_irq;
}
if (val & DA9062AA_E_TEMP_MASK) {
mutex_lock(&thermal->lock);
thermal->temperature = DA9062_MILLI_CELSIUS(125);
mutex_unlock(&thermal->lock);
thermal_zone_device_update(thermal->zone,
THERMAL_EVENT_UNSPECIFIED);
delay = msecs_to_jiffies(thermal->zone->passive_delay);
queue_delayed_work(system_freezable_wq, &thermal->work, delay);
return;
}
mutex_lock(&thermal->lock);
thermal->temperature = DA9062_MILLI_CELSIUS(0);
mutex_unlock(&thermal->lock);
thermal_zone_device_update(thermal->zone,
THERMAL_EVENT_UNSPECIFIED);
err_enable_irq:
enable_irq(thermal->irq);
}
static irqreturn_t da9062_thermal_irq_handler(int irq, void *data)
{
struct da9062_thermal *thermal = data;
disable_irq_nosync(thermal->irq);
queue_delayed_work(system_freezable_wq, &thermal->work, 0);
return IRQ_HANDLED;
}
static int da9062_thermal_get_mode(struct thermal_zone_device *z,
enum thermal_device_mode *mode)
{
*mode = z->mode;
return 0;
}
static int da9062_thermal_get_trip_type(struct thermal_zone_device *z,
int trip,
enum thermal_trip_type *type)
{
struct da9062_thermal *thermal = z->devdata;
switch (trip) {
case 0:
*type = THERMAL_TRIP_HOT;
break;
default:
dev_err(thermal->dev,
"Driver does not support more than 1 trip-wire\n");
return -EINVAL;
}
return 0;
}
static int da9062_thermal_get_trip_temp(struct thermal_zone_device *z,
int trip,
int *temp)
{
struct da9062_thermal *thermal = z->devdata;
switch (trip) {
case 0:
*temp = DA9062_MILLI_CELSIUS(125);
break;
default:
dev_err(thermal->dev,
"Driver does not support more than 1 trip-wire\n");
return -EINVAL;
}
return 0;
}
static int da9062_thermal_get_temp(struct thermal_zone_device *z,
int *temp)
{
struct da9062_thermal *thermal = z->devdata;
mutex_lock(&thermal->lock);
*temp = thermal->temperature;
mutex_unlock(&thermal->lock);
return 0;
}
static struct thermal_zone_device_ops da9062_thermal_ops = {
.get_temp = da9062_thermal_get_temp,
.get_mode = da9062_thermal_get_mode,
.get_trip_type = da9062_thermal_get_trip_type,
.get_trip_temp = da9062_thermal_get_trip_temp,
};
static const struct da9062_thermal_config da9062_config = {
.name = "da9062-thermal",
};
static const struct of_device_id da9062_compatible_reg_id_table[] = {
{ .compatible = "dlg,da9062-thermal", .data = &da9062_config },
{ },
};
MODULE_DEVICE_TABLE(of, da9062_compatible_reg_id_table);
static int da9062_thermal_probe(struct platform_device *pdev)
{
struct da9062 *chip = dev_get_drvdata(pdev->dev.parent);
struct da9062_thermal *thermal;
unsigned int pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
const struct of_device_id *match;
int ret = 0;
match = of_match_node(da9062_compatible_reg_id_table,
pdev->dev.of_node);
if (!match)
return -ENXIO;
if (pdev->dev.of_node) {
if (!of_property_read_u32(pdev->dev.of_node,
"polling-delay-passive",
&pp_tmp)) {
if (pp_tmp < DA9062_MIN_POLLING_MS_PERIOD ||
pp_tmp > DA9062_MAX_POLLING_MS_PERIOD) {
dev_warn(&pdev->dev,
"Out-of-range polling period %d ms\n",
pp_tmp);
pp_tmp = DA9062_DEFAULT_POLLING_MS_PERIOD;
}
}
}
thermal = devm_kzalloc(&pdev->dev, sizeof(struct da9062_thermal),
GFP_KERNEL);
if (!thermal) {
ret = -ENOMEM;
goto err;
}
thermal->config = match->data;
thermal->hw = chip;
thermal->dev = &pdev->dev;
INIT_DELAYED_WORK(&thermal->work, da9062_thermal_poll_on);
mutex_init(&thermal->lock);
thermal->zone = thermal_zone_device_register(thermal->config->name,
1, 0, thermal,
&da9062_thermal_ops, NULL, pp_tmp,
0);
if (IS_ERR(thermal->zone)) {
dev_err(&pdev->dev, "Cannot register thermal zone device\n");
ret = PTR_ERR(thermal->zone);
goto err;
}
thermal->zone->mode = THERMAL_DEVICE_ENABLED;
dev_dbg(&pdev->dev,
"TJUNC temperature polling period set at %d ms\n",
thermal->zone->passive_delay);
ret = platform_get_irq_byname(pdev, "THERMAL");
if (ret < 0) {
dev_err(&pdev->dev, "Failed to get platform IRQ.\n");
goto err_zone;
}
thermal->irq = ret;
ret = request_threaded_irq(thermal->irq, NULL,
da9062_thermal_irq_handler,
IRQF_TRIGGER_LOW | IRQF_ONESHOT,
"THERMAL", thermal);
if (ret) {
dev_err(&pdev->dev,
"Failed to request thermal device IRQ.\n");
goto err_zone;
}
platform_set_drvdata(pdev, thermal);
return 0;
err_zone:
thermal_zone_device_unregister(thermal->zone);
err:
return ret;
}
static int da9062_thermal_remove(struct platform_device *pdev)
{
struct da9062_thermal *thermal = platform_get_drvdata(pdev);
free_irq(thermal->irq, thermal);
cancel_delayed_work_sync(&thermal->work);
thermal_zone_device_unregister(thermal->zone);
return 0;
}
static struct platform_driver da9062_thermal_driver = {
.probe = da9062_thermal_probe,
.remove = da9062_thermal_remove,
.driver = {
.name = "da9062-thermal",
.of_match_table = da9062_compatible_reg_id_table,
},
};
module_platform_driver(da9062_thermal_driver);
MODULE_AUTHOR("Steve Twiss");
MODULE_DESCRIPTION("Thermal TJUNC device driver for Dialog DA9062 and DA9061");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:da9062-thermal");
|