1 From 4e87400732c77765afae2ea89ed43837457aa604 Mon Sep 17 00:00:00 2001
2 From: Rajith Cherian <rajith@codeaurora.org>
3 Date: Wed, 1 Feb 2017 19:00:26 +0530
4 Subject: [PATCH] ipq8064: tsens: Support for configurable interrupts
6 Provide support for adding configurable high and
7 configurable low trip temperatures. An interrupts is
8 also triggerred when these trip points are hit. The
9 interrupts can be activated or deactivated from sysfs.
10 This functionality is made available only if
11 CONFIG_THERMAL_WRITABLE_TRIPS is defined.
13 Change-Id: Ib73f3f9459de4fffce7bb985a0312a88291f4934
14 Signed-off-by: Rajith Cherian <rajith@codeaurora.org>
16 .../devicetree/bindings/thermal/qcom-tsens.txt | 4 ++
17 drivers/thermal/of-thermal.c | 63 ++++++++++++++++++----
18 drivers/thermal/qcom/tsens.c | 43 ++++++++++++---
19 drivers/thermal/qcom/tsens.h | 11 ++++
20 drivers/thermal/thermal_core.c | 44 ++++++++++++++-
21 include/linux/thermal.h | 14 +++++
22 6 files changed, 162 insertions(+), 17 deletions(-)
24 --- a/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
25 +++ b/Documentation/devicetree/bindings/thermal/qcom-tsens.txt
26 @@ -12,11 +12,15 @@ Required properties:
27 - Refer to Documentation/devicetree/bindings/nvmem/nvmem.txt to know how to specify
31 +- interrupts: Interrupt which gets triggered when threshold is hit
34 tsens: thermal-sensor@900000 {
35 compatible = "qcom,msm8916-tsens";
36 reg = <0x4a8000 0x2000>;
37 nvmem-cells = <&tsens_caldata>, <&tsens_calsel>;
38 nvmem-cell-names = "caldata", "calsel";
39 + interrupts = <0 178 0>;
40 #thermal-sensor-cells = <1>;
42 --- a/drivers/thermal/of-thermal.c
43 +++ b/drivers/thermal/of-thermal.c
44 @@ -95,7 +95,7 @@ static int of_thermal_get_temp(struct th
46 struct __thermal_zone *data = tz->devdata;
48 - if (!data->ops->get_temp)
49 + if (!data->ops->get_temp || (data->mode == THERMAL_DEVICE_DISABLED))
52 return data->ops->get_temp(data->sensor_data, temp);
53 @@ -106,7 +106,8 @@ static int of_thermal_set_trips(struct t
55 struct __thermal_zone *data = tz->devdata;
57 - if (!data->ops || !data->ops->set_trips)
58 + if (!data->ops || !data->ops->set_trips
59 + || (data->mode == THERMAL_DEVICE_DISABLED))
62 return data->ops->set_trips(data->sensor_data, low, high);
63 @@ -192,6 +193,9 @@ static int of_thermal_set_emul_temp(stru
65 struct __thermal_zone *data = tz->devdata;
67 + if (data->mode == THERMAL_DEVICE_DISABLED)
70 return data->ops->set_emul_temp(data->sensor_data, temp);
73 @@ -200,7 +204,7 @@ static int of_thermal_get_trend(struct t
75 struct __thermal_zone *data = tz->devdata;
77 - if (!data->ops->get_trend)
78 + if (!data->ops->get_trend || (data->mode == THERMAL_DEVICE_DISABLED))
81 return data->ops->get_trend(data->sensor_data, trip, trend);
82 @@ -286,7 +290,9 @@ static int of_thermal_set_mode(struct th
83 mutex_unlock(&tz->lock);
86 - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
88 + if (mode == THERMAL_DEVICE_ENABLED)
89 + thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
93 @@ -296,7 +302,8 @@ static int of_thermal_get_trip_type(stru
95 struct __thermal_zone *data = tz->devdata;
97 - if (trip >= data->ntrips || trip < 0)
98 + if (trip >= data->ntrips || trip < 0
99 + || (data->mode == THERMAL_DEVICE_DISABLED))
102 *type = data->trips[trip].type;
103 @@ -304,12 +311,39 @@ static int of_thermal_get_trip_type(stru
107 +static int of_thermal_activate_trip_type(struct thermal_zone_device *tz,
108 + int trip, enum thermal_trip_activation_mode mode)
110 + struct __thermal_zone *data = tz->devdata;
112 + if (trip >= data->ntrips || trip < 0
113 + || (data->mode == THERMAL_DEVICE_DISABLED))
117 + * The configurable_hi and configurable_lo trip points can be
118 + * activated and deactivated.
121 + if (data->ops->set_trip_activate) {
124 + ret = data->ops->set_trip_activate(data->sensor_data,
133 static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
136 struct __thermal_zone *data = tz->devdata;
138 - if (trip >= data->ntrips || trip < 0)
139 + if (trip >= data->ntrips || trip < 0
140 + || (data->mode == THERMAL_DEVICE_DISABLED))
143 *temp = data->trips[trip].temperature;
144 @@ -322,7 +356,8 @@ static int of_thermal_set_trip_temp(stru
146 struct __thermal_zone *data = tz->devdata;
148 - if (trip >= data->ntrips || trip < 0)
149 + if (trip >= data->ntrips || trip < 0
150 + || (data->mode == THERMAL_DEVICE_DISABLED))
153 if (data->ops->set_trip_temp) {
154 @@ -344,7 +379,8 @@ static int of_thermal_get_trip_hyst(stru
156 struct __thermal_zone *data = tz->devdata;
158 - if (trip >= data->ntrips || trip < 0)
159 + if (trip >= data->ntrips || trip < 0
160 + || (data->mode == THERMAL_DEVICE_DISABLED))
163 *hyst = data->trips[trip].hysteresis;
164 @@ -357,7 +393,8 @@ static int of_thermal_set_trip_hyst(stru
166 struct __thermal_zone *data = tz->devdata;
168 - if (trip >= data->ntrips || trip < 0)
169 + if (trip >= data->ntrips || trip < 0
170 + || (data->mode == THERMAL_DEVICE_DISABLED))
173 /* thermal framework should take care of data->mask & (1 << trip) */
174 @@ -432,6 +469,9 @@ thermal_zone_of_add_sensor(struct device
175 if (ops->set_emul_temp)
176 tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
178 + if (ops->set_trip_activate)
179 + tzd->ops->set_trip_activate = of_thermal_activate_trip_type;
181 mutex_unlock(&tzd->lock);
184 @@ -726,7 +766,10 @@ static const char * const trip_types[] =
185 [THERMAL_TRIP_ACTIVE] = "active",
186 [THERMAL_TRIP_PASSIVE] = "passive",
187 [THERMAL_TRIP_HOT] = "hot",
188 - [THERMAL_TRIP_CRITICAL] = "critical",
189 + [THERMAL_TRIP_CRITICAL] = "critical_high",
190 + [THERMAL_TRIP_CONFIGURABLE_HI] = "configurable_hi",
191 + [THERMAL_TRIP_CONFIGURABLE_LOW] = "configurable_lo",
192 + [THERMAL_TRIP_CRITICAL_LOW] = "critical_low",
196 --- a/drivers/thermal/qcom/tsens.c
197 +++ b/drivers/thermal/qcom/tsens.c
198 @@ -31,7 +31,7 @@ static int tsens_get_temp(void *data, in
200 static int tsens_get_trend(void *p, int trip, enum thermal_trend *trend)
202 - const struct tsens_sensor *s = p;
203 + struct tsens_sensor *s = p;
204 struct tsens_device *tmdev = s->tmdev;
206 if (tmdev->ops->get_trend)
207 @@ -40,9 +40,10 @@ static int tsens_get_trend(void *p, int
211 -static int __maybe_unused tsens_suspend(struct device *dev)
212 +static int __maybe_unused tsens_suspend(void *data)
214 - struct tsens_device *tmdev = dev_get_drvdata(dev);
215 + struct tsens_sensor *s = data;
216 + struct tsens_device *tmdev = s->tmdev;
218 if (tmdev->ops && tmdev->ops->suspend)
219 return tmdev->ops->suspend(tmdev);
220 @@ -50,9 +51,10 @@ static int __maybe_unused tsens_suspend
224 -static int __maybe_unused tsens_resume(struct device *dev)
225 +static int __maybe_unused tsens_resume(void *data)
227 - struct tsens_device *tmdev = dev_get_drvdata(dev);
228 + struct tsens_sensor *s = data;
229 + struct tsens_device *tmdev = s->tmdev;
231 if (tmdev->ops && tmdev->ops->resume)
232 return tmdev->ops->resume(tmdev);
233 @@ -60,6 +62,30 @@ static int __maybe_unused tsens_resume(s
237 +static int __maybe_unused tsens_set_trip_temp(void *data, int trip, int temp)
239 + struct tsens_sensor *s = data;
240 + struct tsens_device *tmdev = s->tmdev;
242 + if (tmdev->ops && tmdev->ops->set_trip_temp)
243 + return tmdev->ops->set_trip_temp(s, trip, temp);
248 +static int __maybe_unused tsens_activate_trip_type(void *data, int trip,
249 + enum thermal_trip_activation_mode mode)
251 + struct tsens_sensor *s = data;
252 + struct tsens_device *tmdev = s->tmdev;
254 + if (tmdev->ops && tmdev->ops->set_trip_activate)
255 + return tmdev->ops->set_trip_activate(s, trip, mode);
261 static SIMPLE_DEV_PM_OPS(tsens_pm_ops, tsens_suspend, tsens_resume);
263 static const struct of_device_id tsens_table[] = {
264 @@ -83,6 +109,8 @@ MODULE_DEVICE_TABLE(of, tsens_table);
265 static const struct thermal_zone_of_device_ops tsens_of_ops = {
266 .get_temp = tsens_get_temp,
267 .get_trend = tsens_get_trend,
268 + .set_trip_temp = tsens_set_trip_temp,
269 + .set_trip_activate = tsens_activate_trip_type,
272 static int tsens_register(struct tsens_device *tmdev)
273 @@ -131,7 +159,7 @@ static int tsens_probe(struct platform_d
280 if (data->num_sensors <= 0) {
281 dev_err(dev, "invalid number of sensors\n");
282 @@ -146,6 +174,9 @@ static int tsens_probe(struct platform_d
284 tmdev->num_sensors = data->num_sensors;
285 tmdev->ops = data->ops;
287 + tmdev->tsens_irq = platform_get_irq(pdev, 0);
289 for (i = 0; i < tmdev->num_sensors; i++) {
291 tmdev->sensor[i].hw_id = data->hw_ids[i];
292 --- a/drivers/thermal/qcom/tsens.h
293 +++ b/drivers/thermal/qcom/tsens.h
294 @@ -24,9 +24,12 @@ struct tsens_device;
295 struct tsens_sensor {
296 struct tsens_device *tmdev;
297 struct thermal_zone_device *tzd;
298 + struct work_struct notify_work;
303 + int calib_data_backup;
307 @@ -41,6 +44,9 @@ struct tsens_sensor {
308 * @suspend: Function to suspend the tsens device
309 * @resume: Function to resume the tsens device
310 * @get_trend: Function to get the thermal/temp trend
311 + * @set_trip_temp: Function to set trip temp
312 + * @get_trip_temp: Function to get trip temp
313 + * @set_trip_activate: Function to activate trip points
316 /* mandatory callbacks */
317 @@ -53,6 +59,9 @@ struct tsens_ops {
318 int (*suspend)(struct tsens_device *);
319 int (*resume)(struct tsens_device *);
320 int (*get_trend)(struct tsens_device *, int, enum thermal_trend *);
321 + int (*set_trip_temp)(void *, int, int);
322 + int (*set_trip_activate)(void *, int,
323 + enum thermal_trip_activation_mode);
327 @@ -76,11 +85,13 @@ struct tsens_context {
328 struct tsens_device {
333 struct regmap_field *status_field;
334 struct tsens_context ctx;
336 const struct tsens_ops *ops;
337 + struct work_struct tsens_work;
338 struct tsens_sensor sensor[0];
341 --- a/drivers/thermal/thermal_sysfs.c
342 +++ b/drivers/thermal/thermal_sysfs.c
343 @@ -115,12 +115,48 @@
344 return sprintf(buf, "passive\n");
345 case THERMAL_TRIP_ACTIVE:
346 return sprintf(buf, "active\n");
347 + case THERMAL_TRIP_CONFIGURABLE_HI:
348 + return sprintf(buf, "configurable_hi\n");
349 + case THERMAL_TRIP_CONFIGURABLE_LOW:
350 + return sprintf(buf, "configurable_low\n");
351 + case THERMAL_TRIP_CRITICAL_LOW:
352 + return sprintf(buf, "critical_low\n");
354 return sprintf(buf, "unknown\n");
359 +trip_point_type_activate(struct device *dev, struct device_attribute *attr,
360 + const char *buf, size_t count)
362 + struct thermal_zone_device *tz = to_thermal_zone(dev);
364 + char *enabled = "enabled";
365 + char *disabled = "disabled";
367 + if (!tz->ops->set_trip_activate)
370 + if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
373 + if (!strncmp(buf, enabled, strlen(enabled)))
374 + ret = tz->ops->set_trip_activate(tz, trip,
375 + THERMAL_TRIP_ACTIVATION_ENABLED);
376 + else if (!strncmp(buf, disabled, strlen(disabled)))
377 + ret = tz->ops->set_trip_activate(tz, trip,
378 + THERMAL_TRIP_ACTIVATION_DISABLED);
389 trip_point_temp_store(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
393 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
394 attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
396 + if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS)) {
397 + tz->trip_type_attrs[indx].attr.store
398 + = trip_point_type_activate;
399 + tz->trip_type_attrs[indx].attr.attr.mode |= S_IWUSR;
402 /* create trip temp attribute */
403 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
404 "trip_point_%d_temp", indx);
405 --- a/include/linux/thermal.h
406 +++ b/include/linux/thermal.h
407 @@ -78,11 +78,19 @@ enum thermal_device_mode {
408 THERMAL_DEVICE_ENABLED,
411 +enum thermal_trip_activation_mode {
412 + THERMAL_TRIP_ACTIVATION_DISABLED = 0,
413 + THERMAL_TRIP_ACTIVATION_ENABLED,
416 enum thermal_trip_type {
417 THERMAL_TRIP_ACTIVE = 0,
418 THERMAL_TRIP_PASSIVE,
420 THERMAL_TRIP_CRITICAL,
421 + THERMAL_TRIP_CONFIGURABLE_HI,
422 + THERMAL_TRIP_CONFIGURABLE_LOW,
423 + THERMAL_TRIP_CRITICAL_LOW,
427 @@ -120,6 +128,8 @@ struct thermal_zone_device_ops {
428 enum thermal_trip_type *);
429 int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
430 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
431 + int (*set_trip_activate) (struct thermal_zone_device *, int,
432 + enum thermal_trip_activation_mode);
433 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
434 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
435 int (*get_crit_temp) (struct thermal_zone_device *, int *);
436 @@ -363,6 +373,8 @@ struct thermal_genl_event {
438 * @set_trip_temp: a pointer to a function that sets the trip temperature on
440 + * @activate_trip_type: a pointer to a function to enable/disable trip
441 + * temperature interrupts
443 struct thermal_zone_of_device_ops {
444 int (*get_temp)(void *, int *);
445 @@ -370,6 +382,8 @@ struct thermal_zone_of_device_ops {
446 int (*set_trips)(void *, int, int);
447 int (*set_emul_temp)(void *, int);
448 int (*set_trip_temp)(void *, int, int);
449 + int (*set_trip_activate)(void *, int,
450 + enum thermal_trip_activation_mode);