blob: 38446e9d1def0147b7dfe737127c6bafc3a7d573 [file] [log] [blame]
Thomas Gleixner21eb0be2019-05-29 07:12:45 -07001// SPDX-License-Identifier: GPL-2.0-only
Mark Brown08bad5a2009-07-28 15:52:22 +01002/*
3 * drivers/hwmon/wm831x-hwmon.c - Wolfson Microelectronics WM831x PMIC
4 * hardware monitoring features.
5 *
6 * Copyright (C) 2009 Wolfson Microelectronics plc
Mark Brown08bad5a2009-07-28 15:52:22 +01007 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/err.h>
13#include <linux/hwmon.h>
14#include <linux/hwmon-sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mark Brown08bad5a2009-07-28 15:52:22 +010016
17#include <linux/mfd/wm831x/core.h>
18#include <linux/mfd/wm831x/auxadc.h>
19
Frans Meulenbroeksa6100f62012-01-08 19:34:19 +010020static const char * const input_names[] = {
Mark Brown08bad5a2009-07-28 15:52:22 +010021 [WM831X_AUX_SYSVDD] = "SYSVDD",
22 [WM831X_AUX_USB] = "USB",
23 [WM831X_AUX_BKUP_BATT] = "Backup battery",
24 [WM831X_AUX_BATT] = "Battery",
25 [WM831X_AUX_WALL] = "WALL",
26 [WM831X_AUX_CHIP_TEMP] = "PMIC",
27 [WM831X_AUX_BATT_TEMP] = "Battery",
28};
29
Mark Brown08bad5a2009-07-28 15:52:22 +010030static ssize_t show_voltage(struct device *dev,
31 struct device_attribute *attr, char *buf)
32{
Axel Lin9a70c972014-06-17 23:10:55 +080033 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown08bad5a2009-07-28 15:52:22 +010034 int channel = to_sensor_dev_attr(attr)->index;
35 int ret;
36
Axel Lin9a70c972014-06-17 23:10:55 +080037 ret = wm831x_auxadc_read_uv(wm831x, channel);
Mark Brown08bad5a2009-07-28 15:52:22 +010038 if (ret < 0)
39 return ret;
40
41 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret, 1000));
42}
43
44static ssize_t show_chip_temp(struct device *dev,
45 struct device_attribute *attr, char *buf)
46{
Axel Lin9a70c972014-06-17 23:10:55 +080047 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown08bad5a2009-07-28 15:52:22 +010048 int channel = to_sensor_dev_attr(attr)->index;
49 int ret;
50
Axel Lin9a70c972014-06-17 23:10:55 +080051 ret = wm831x_auxadc_read(wm831x, channel);
Mark Brown08bad5a2009-07-28 15:52:22 +010052 if (ret < 0)
53 return ret;
54
55 /* Degrees celsius = (512.18-ret) / 1.0983 */
56 ret = 512180 - (ret * 1000);
57 ret = DIV_ROUND_CLOSEST(ret * 10000, 10983);
58
59 return sprintf(buf, "%d\n", ret);
60}
61
62static ssize_t show_label(struct device *dev,
63 struct device_attribute *attr, char *buf)
64{
65 int channel = to_sensor_dev_attr(attr)->index;
66
67 return sprintf(buf, "%s\n", input_names[channel]);
68}
69
70#define WM831X_VOLTAGE(id, name) \
71 static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage, \
72 NULL, name)
73
74#define WM831X_NAMED_VOLTAGE(id, name) \
75 WM831X_VOLTAGE(id, name); \
76 static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label, \
77 NULL, name)
78
Mark Brown08bad5a2009-07-28 15:52:22 +010079WM831X_VOLTAGE(0, WM831X_AUX_AUX1);
80WM831X_VOLTAGE(1, WM831X_AUX_AUX2);
81WM831X_VOLTAGE(2, WM831X_AUX_AUX3);
82WM831X_VOLTAGE(3, WM831X_AUX_AUX4);
83
84WM831X_NAMED_VOLTAGE(4, WM831X_AUX_SYSVDD);
85WM831X_NAMED_VOLTAGE(5, WM831X_AUX_USB);
86WM831X_NAMED_VOLTAGE(6, WM831X_AUX_BATT);
87WM831X_NAMED_VOLTAGE(7, WM831X_AUX_WALL);
88WM831X_NAMED_VOLTAGE(8, WM831X_AUX_BKUP_BATT);
89
90static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_chip_temp, NULL,
91 WM831X_AUX_CHIP_TEMP);
92static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL,
93 WM831X_AUX_CHIP_TEMP);
Guenter Roeck07de3df2012-01-19 11:02:28 -080094/*
95 * Report as a voltage since conversion depends on external components
96 * and that's what the ABI wants.
97 */
Mark Brown08bad5a2009-07-28 15:52:22 +010098static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_voltage, NULL,
99 WM831X_AUX_BATT_TEMP);
100static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
101 WM831X_AUX_BATT_TEMP);
102
Axel Lin9a70c972014-06-17 23:10:55 +0800103static struct attribute *wm831x_attrs[] = {
Mark Brown08bad5a2009-07-28 15:52:22 +0100104 &sensor_dev_attr_in0_input.dev_attr.attr,
105 &sensor_dev_attr_in1_input.dev_attr.attr,
106 &sensor_dev_attr_in2_input.dev_attr.attr,
107 &sensor_dev_attr_in3_input.dev_attr.attr,
108
109 &sensor_dev_attr_in4_input.dev_attr.attr,
110 &sensor_dev_attr_in4_label.dev_attr.attr,
111 &sensor_dev_attr_in5_input.dev_attr.attr,
112 &sensor_dev_attr_in5_label.dev_attr.attr,
113 &sensor_dev_attr_in6_input.dev_attr.attr,
114 &sensor_dev_attr_in6_label.dev_attr.attr,
115 &sensor_dev_attr_in7_input.dev_attr.attr,
116 &sensor_dev_attr_in7_label.dev_attr.attr,
117 &sensor_dev_attr_in8_input.dev_attr.attr,
118 &sensor_dev_attr_in8_label.dev_attr.attr,
119
120 &sensor_dev_attr_temp1_input.dev_attr.attr,
121 &sensor_dev_attr_temp1_label.dev_attr.attr,
122 &sensor_dev_attr_temp2_input.dev_attr.attr,
123 &sensor_dev_attr_temp2_label.dev_attr.attr,
124
125 NULL
126};
127
Axel Lin9a70c972014-06-17 23:10:55 +0800128ATTRIBUTE_GROUPS(wm831x);
Mark Brown08bad5a2009-07-28 15:52:22 +0100129
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500130static int wm831x_hwmon_probe(struct platform_device *pdev)
Mark Brown08bad5a2009-07-28 15:52:22 +0100131{
132 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Axel Lin9a70c972014-06-17 23:10:55 +0800133 struct device *hwmon_dev;
Mark Brown08bad5a2009-07-28 15:52:22 +0100134
Axel Lin9a70c972014-06-17 23:10:55 +0800135 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, "wm831x",
136 wm831x,
137 wm831x_groups);
138 return PTR_ERR_OR_ZERO(hwmon_dev);
Mark Brown08bad5a2009-07-28 15:52:22 +0100139}
140
141static struct platform_driver wm831x_hwmon_driver = {
142 .probe = wm831x_hwmon_probe,
Mark Brown08bad5a2009-07-28 15:52:22 +0100143 .driver = {
144 .name = "wm831x-hwmon",
Mark Brown08bad5a2009-07-28 15:52:22 +0100145 },
146};
147
Axel Lin25a236a2011-11-25 02:31:00 -0500148module_platform_driver(wm831x_hwmon_driver);
Mark Brown08bad5a2009-07-28 15:52:22 +0100149
150MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
151MODULE_DESCRIPTION("WM831x Hardware Monitoring");
152MODULE_LICENSE("GPL");
153MODULE_ALIAS("platform:wm831x-hwmon");