blob: 50e67cdd8e5e3fd5030e3502ce81038e8e2b7140 [file] [log] [blame]
Thomas Gleixner03761482019-05-28 09:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +08002/*
3 * Copyright (C) ST-Ericsson 2010 - 2013
4 * Author: Martin Persson <martin.persson@stericsson.com>
5 * Hongbo Zhang <hongbo.zhang@linaro.org>
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +08006 *
7 * ABX500 does not provide auto ADC, so to monitor the required temperatures,
8 * a periodic work is used. It is more important to not wake up the CPU than
9 * to perform this job, hence the use of a deferred delay.
10 *
11 * A deferred delay for thermal monitor is considered safe because:
12 * If the chip gets too hot during a sleep state it's most likely due to
13 * external factors, such as the surrounding temperature. I.e. no SW decisions
14 * will make any difference.
15 */
16
17#include <linux/err.h>
18#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
20#include <linux/interrupt.h>
21#include <linux/jiffies.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/of.h>
25#include <linux/platform_device.h>
26#include <linux/pm.h>
27#include <linux/slab.h>
28#include <linux/sysfs.h>
29#include <linux/workqueue.h>
30#include "abx500.h"
31
32#define DEFAULT_MONITOR_DELAY HZ
33#define DEFAULT_MAX_TEMP 130
34
35static inline void schedule_monitor(struct abx500_temp *data)
36{
37 data->work_active = true;
38 schedule_delayed_work(&data->work, DEFAULT_MONITOR_DELAY);
39}
40
41static void threshold_updated(struct abx500_temp *data)
42{
43 int i;
44 for (i = 0; i < data->monitored_sensors; i++)
45 if (data->max[i] != 0 || data->min[i] != 0) {
46 schedule_monitor(data);
47 return;
48 }
49
50 dev_dbg(&data->pdev->dev, "No active thresholds.\n");
51 cancel_delayed_work_sync(&data->work);
52 data->work_active = false;
53}
54
55static void gpadc_monitor(struct work_struct *work)
56{
57 int temp, i, ret;
58 char alarm_node[30];
59 bool updated_min_alarm, updated_max_alarm;
60 struct abx500_temp *data;
61
62 data = container_of(work, struct abx500_temp, work.work);
63 mutex_lock(&data->lock);
64
65 for (i = 0; i < data->monitored_sensors; i++) {
66 /* Thresholds are considered inactive if set to 0 */
67 if (data->max[i] == 0 && data->min[i] == 0)
68 continue;
69
70 if (data->max[i] < data->min[i])
71 continue;
72
73 ret = data->ops.read_sensor(data, data->gpadc_addr[i], &temp);
74 if (ret < 0) {
75 dev_err(&data->pdev->dev, "GPADC read failed\n");
76 continue;
77 }
78
79 updated_min_alarm = false;
80 updated_max_alarm = false;
81
82 if (data->min[i] != 0) {
83 if (temp < data->min[i]) {
84 if (data->min_alarm[i] == false) {
85 data->min_alarm[i] = true;
86 updated_min_alarm = true;
87 }
88 } else {
89 if (data->min_alarm[i] == true) {
90 data->min_alarm[i] = false;
91 updated_min_alarm = true;
92 }
93 }
94 }
95 if (data->max[i] != 0) {
96 if (temp > data->max[i]) {
97 if (data->max_alarm[i] == false) {
98 data->max_alarm[i] = true;
99 updated_max_alarm = true;
100 }
101 } else if (temp < data->max[i] - data->max_hyst[i]) {
102 if (data->max_alarm[i] == true) {
103 data->max_alarm[i] = false;
104 updated_max_alarm = true;
105 }
106 }
107 }
108
109 if (updated_min_alarm) {
110 ret = sprintf(alarm_node, "temp%d_min_alarm", i + 1);
111 sysfs_notify(&data->pdev->dev.kobj, NULL, alarm_node);
112 }
113 if (updated_max_alarm) {
114 ret = sprintf(alarm_node, "temp%d_max_alarm", i + 1);
115 sysfs_notify(&data->pdev->dev.kobj, NULL, alarm_node);
116 }
117 }
118
119 schedule_monitor(data);
120 mutex_unlock(&data->lock);
121}
122
123/* HWMON sysfs interfaces */
Guenter Roeck4708e962018-12-10 14:01:58 -0800124static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800125 char *buf)
126{
127 struct abx500_temp *data = dev_get_drvdata(dev);
128 /* Show chip name */
129 return data->ops.show_name(dev, devattr, buf);
130}
131
Guenter Roeck4708e962018-12-10 14:01:58 -0800132static ssize_t label_show(struct device *dev,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800133 struct device_attribute *devattr, char *buf)
134{
135 struct abx500_temp *data = dev_get_drvdata(dev);
136 /* Show each sensor label */
137 return data->ops.show_label(dev, devattr, buf);
138}
139
Guenter Roeck4708e962018-12-10 14:01:58 -0800140static ssize_t input_show(struct device *dev,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800141 struct device_attribute *devattr, char *buf)
142{
143 int ret, temp;
144 struct abx500_temp *data = dev_get_drvdata(dev);
145 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
146 u8 gpadc_addr = data->gpadc_addr[attr->index];
147
148 ret = data->ops.read_sensor(data, gpadc_addr, &temp);
149 if (ret < 0)
150 return ret;
151
152 return sprintf(buf, "%d\n", temp);
153}
154
155/* Set functions (RW nodes) */
Guenter Roeck4708e962018-12-10 14:01:58 -0800156static ssize_t min_store(struct device *dev, struct device_attribute *devattr,
157 const char *buf, size_t count)
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800158{
159 unsigned long val;
160 struct abx500_temp *data = dev_get_drvdata(dev);
161 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
162 int res = kstrtol(buf, 10, &val);
163 if (res < 0)
164 return res;
165
166 val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
167
168 mutex_lock(&data->lock);
169 data->min[attr->index] = val;
170 threshold_updated(data);
171 mutex_unlock(&data->lock);
172
173 return count;
174}
175
Guenter Roeck4708e962018-12-10 14:01:58 -0800176static ssize_t max_store(struct device *dev, struct device_attribute *devattr,
177 const char *buf, size_t count)
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800178{
179 unsigned long val;
180 struct abx500_temp *data = dev_get_drvdata(dev);
181 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
182 int res = kstrtol(buf, 10, &val);
183 if (res < 0)
184 return res;
185
186 val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
187
188 mutex_lock(&data->lock);
189 data->max[attr->index] = val;
190 threshold_updated(data);
191 mutex_unlock(&data->lock);
192
193 return count;
194}
195
Guenter Roeck4708e962018-12-10 14:01:58 -0800196static ssize_t max_hyst_store(struct device *dev,
197 struct device_attribute *devattr,
198 const char *buf, size_t count)
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800199{
200 unsigned long val;
201 struct abx500_temp *data = dev_get_drvdata(dev);
202 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
203 int res = kstrtoul(buf, 10, &val);
204 if (res < 0)
205 return res;
206
207 val = clamp_val(val, 0, DEFAULT_MAX_TEMP);
208
209 mutex_lock(&data->lock);
210 data->max_hyst[attr->index] = val;
211 threshold_updated(data);
212 mutex_unlock(&data->lock);
213
214 return count;
215}
216
217/* Show functions (RO nodes) */
Guenter Roeck4708e962018-12-10 14:01:58 -0800218static ssize_t min_show(struct device *dev, struct device_attribute *devattr,
219 char *buf)
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800220{
221 struct abx500_temp *data = dev_get_drvdata(dev);
222 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
223
Asaf Vertza14c7072015-01-21 10:03:22 +0200224 return sprintf(buf, "%lu\n", data->min[attr->index]);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800225}
226
Guenter Roeck4708e962018-12-10 14:01:58 -0800227static ssize_t max_show(struct device *dev, struct device_attribute *devattr,
228 char *buf)
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800229{
230 struct abx500_temp *data = dev_get_drvdata(dev);
231 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
232
Asaf Vertza14c7072015-01-21 10:03:22 +0200233 return sprintf(buf, "%lu\n", data->max[attr->index]);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800234}
235
Guenter Roeck4708e962018-12-10 14:01:58 -0800236static ssize_t max_hyst_show(struct device *dev,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800237 struct device_attribute *devattr, char *buf)
238{
239 struct abx500_temp *data = dev_get_drvdata(dev);
240 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
241
Asaf Vertza14c7072015-01-21 10:03:22 +0200242 return sprintf(buf, "%lu\n", data->max_hyst[attr->index]);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800243}
244
Guenter Roeck4708e962018-12-10 14:01:58 -0800245static ssize_t min_alarm_show(struct device *dev,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800246 struct device_attribute *devattr, char *buf)
247{
248 struct abx500_temp *data = dev_get_drvdata(dev);
249 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
250
251 return sprintf(buf, "%d\n", data->min_alarm[attr->index]);
252}
253
Guenter Roeck4708e962018-12-10 14:01:58 -0800254static ssize_t max_alarm_show(struct device *dev,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800255 struct device_attribute *devattr, char *buf)
256{
257 struct abx500_temp *data = dev_get_drvdata(dev);
258 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
259
260 return sprintf(buf, "%d\n", data->max_alarm[attr->index]);
261}
262
Al Viro60b7bf42013-05-04 14:46:28 -0400263static umode_t abx500_attrs_visible(struct kobject *kobj,
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800264 struct attribute *attr, int n)
265{
266 struct device *dev = container_of(kobj, struct device, kobj);
267 struct abx500_temp *data = dev_get_drvdata(dev);
268
269 if (data->ops.is_visible)
270 return data->ops.is_visible(attr, n);
271
272 return attr->mode;
273}
274
275/* Chip name, required by hwmon */
Guenter Roeck4708e962018-12-10 14:01:58 -0800276static SENSOR_DEVICE_ATTR_RO(name, name, 0);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800277
278/* GPADC - SENSOR1 */
Guenter Roeck4708e962018-12-10 14:01:58 -0800279static SENSOR_DEVICE_ATTR_RO(temp1_label, label, 0);
280static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
281static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
282static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
283static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, max_hyst, 0);
284static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
285static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800286
287/* GPADC - SENSOR2 */
Guenter Roeck4708e962018-12-10 14:01:58 -0800288static SENSOR_DEVICE_ATTR_RO(temp2_label, label, 1);
289static SENSOR_DEVICE_ATTR_RO(temp2_input, input, 1);
290static SENSOR_DEVICE_ATTR_RW(temp2_min, min, 1);
291static SENSOR_DEVICE_ATTR_RW(temp2_max, max, 1);
292static SENSOR_DEVICE_ATTR_RW(temp2_max_hyst, max_hyst, 1);
293static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, min_alarm, 1);
294static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, max_alarm, 1);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800295
296/* GPADC - SENSOR3 */
Guenter Roeck4708e962018-12-10 14:01:58 -0800297static SENSOR_DEVICE_ATTR_RO(temp3_label, label, 2);
298static SENSOR_DEVICE_ATTR_RO(temp3_input, input, 2);
299static SENSOR_DEVICE_ATTR_RW(temp3_min, min, 2);
300static SENSOR_DEVICE_ATTR_RW(temp3_max, max, 2);
301static SENSOR_DEVICE_ATTR_RW(temp3_max_hyst, max_hyst, 2);
302static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, min_alarm, 2);
303static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, max_alarm, 2);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800304
305/* GPADC - SENSOR4 */
Guenter Roeck4708e962018-12-10 14:01:58 -0800306static SENSOR_DEVICE_ATTR_RO(temp4_label, label, 3);
307static SENSOR_DEVICE_ATTR_RO(temp4_input, input, 3);
308static SENSOR_DEVICE_ATTR_RW(temp4_min, min, 3);
309static SENSOR_DEVICE_ATTR_RW(temp4_max, max, 3);
310static SENSOR_DEVICE_ATTR_RW(temp4_max_hyst, max_hyst, 3);
311static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, min_alarm, 3);
312static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, max_alarm, 3);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800313
Sachin Kamat0b905d82013-07-11 10:21:00 +0530314static struct attribute *abx500_temp_attributes[] = {
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800315 &sensor_dev_attr_name.dev_attr.attr,
316
317 &sensor_dev_attr_temp1_label.dev_attr.attr,
318 &sensor_dev_attr_temp1_input.dev_attr.attr,
319 &sensor_dev_attr_temp1_min.dev_attr.attr,
320 &sensor_dev_attr_temp1_max.dev_attr.attr,
321 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
322 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
323 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
324
325 &sensor_dev_attr_temp2_label.dev_attr.attr,
326 &sensor_dev_attr_temp2_input.dev_attr.attr,
327 &sensor_dev_attr_temp2_min.dev_attr.attr,
328 &sensor_dev_attr_temp2_max.dev_attr.attr,
329 &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
330 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
331 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
332
333 &sensor_dev_attr_temp3_label.dev_attr.attr,
334 &sensor_dev_attr_temp3_input.dev_attr.attr,
335 &sensor_dev_attr_temp3_min.dev_attr.attr,
336 &sensor_dev_attr_temp3_max.dev_attr.attr,
337 &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
338 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
339 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
340
341 &sensor_dev_attr_temp4_label.dev_attr.attr,
342 &sensor_dev_attr_temp4_input.dev_attr.attr,
343 &sensor_dev_attr_temp4_min.dev_attr.attr,
344 &sensor_dev_attr_temp4_max.dev_attr.attr,
345 &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
346 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
347 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
348 NULL
349};
350
351static const struct attribute_group abx500_temp_group = {
352 .attrs = abx500_temp_attributes,
353 .is_visible = abx500_attrs_visible,
354};
355
356static irqreturn_t abx500_temp_irq_handler(int irq, void *irq_data)
357{
358 struct platform_device *pdev = irq_data;
359 struct abx500_temp *data = platform_get_drvdata(pdev);
360
361 data->ops.irq_handler(irq, data);
362 return IRQ_HANDLED;
363}
364
365static int setup_irqs(struct platform_device *pdev)
366{
367 int ret;
368 int irq = platform_get_irq_byname(pdev, "ABX500_TEMP_WARM");
369
370 if (irq < 0) {
371 dev_err(&pdev->dev, "Get irq by name failed\n");
372 return irq;
373 }
374
375 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
Sudeep Holla47e4b5e2015-09-21 16:47:04 +0100376 abx500_temp_irq_handler, 0, "abx500-temp", pdev);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800377 if (ret < 0)
378 dev_err(&pdev->dev, "Request threaded irq failed (%d)\n", ret);
379
380 return ret;
381}
382
383static int abx500_temp_probe(struct platform_device *pdev)
384{
385 struct abx500_temp *data;
386 int err;
387
388 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
389 if (!data)
390 return -ENOMEM;
391
392 data->pdev = pdev;
393 mutex_init(&data->lock);
394
395 /* Chip specific initialization */
396 err = abx500_hwmon_init(data);
397 if (err < 0 || !data->ops.read_sensor || !data->ops.show_name ||
398 !data->ops.show_label)
399 return err;
400
401 INIT_DEFERRABLE_WORK(&data->work, gpadc_monitor);
402
403 platform_set_drvdata(pdev, data);
404
405 err = sysfs_create_group(&pdev->dev.kobj, &abx500_temp_group);
406 if (err < 0) {
407 dev_err(&pdev->dev, "Create sysfs group failed (%d)\n", err);
408 return err;
409 }
410
411 data->hwmon_dev = hwmon_device_register(&pdev->dev);
412 if (IS_ERR(data->hwmon_dev)) {
413 err = PTR_ERR(data->hwmon_dev);
414 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
415 goto exit_sysfs_group;
416 }
417
418 if (data->ops.irq_handler) {
419 err = setup_irqs(pdev);
420 if (err < 0)
421 goto exit_hwmon_reg;
422 }
423 return 0;
424
425exit_hwmon_reg:
426 hwmon_device_unregister(data->hwmon_dev);
427exit_sysfs_group:
428 sysfs_remove_group(&pdev->dev.kobj, &abx500_temp_group);
429 return err;
430}
431
432static int abx500_temp_remove(struct platform_device *pdev)
433{
434 struct abx500_temp *data = platform_get_drvdata(pdev);
435
436 cancel_delayed_work_sync(&data->work);
437 hwmon_device_unregister(data->hwmon_dev);
438 sysfs_remove_group(&pdev->dev.kobj, &abx500_temp_group);
439
440 return 0;
441}
442
443static int abx500_temp_suspend(struct platform_device *pdev,
444 pm_message_t state)
445{
446 struct abx500_temp *data = platform_get_drvdata(pdev);
447
448 if (data->work_active)
449 cancel_delayed_work_sync(&data->work);
450
451 return 0;
452}
453
454static int abx500_temp_resume(struct platform_device *pdev)
455{
456 struct abx500_temp *data = platform_get_drvdata(pdev);
457
458 if (data->work_active)
459 schedule_monitor(data);
460
461 return 0;
462}
463
464#ifdef CONFIG_OF
465static const struct of_device_id abx500_temp_match[] = {
466 { .compatible = "stericsson,abx500-temp" },
467 {},
468};
Luis de Bethencourt3245acb2015-09-17 18:09:11 +0200469MODULE_DEVICE_TABLE(of, abx500_temp_match);
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800470#endif
471
472static struct platform_driver abx500_temp_driver = {
473 .driver = {
Hongbo Zhang0bbb06e2013-04-03 20:18:12 +0800474 .name = "abx500-temp",
475 .of_match_table = of_match_ptr(abx500_temp_match),
476 },
477 .suspend = abx500_temp_suspend,
478 .resume = abx500_temp_resume,
479 .probe = abx500_temp_probe,
480 .remove = abx500_temp_remove,
481};
482
483module_platform_driver(abx500_temp_driver);
484
485MODULE_AUTHOR("Martin Persson <martin.persson@stericsson.com>");
486MODULE_DESCRIPTION("ABX500 temperature driver");
487MODULE_LICENSE("GPL");