xref: /openbmc/linux/drivers/hwmon/wm831x-hwmon.c (revision 08bad5a821371548942aa13565831f18fe1875f3)
1*08bad5a8SMark Brown /*
2*08bad5a8SMark Brown  * drivers/hwmon/wm831x-hwmon.c - Wolfson Microelectronics WM831x PMIC
3*08bad5a8SMark Brown  *                                hardware monitoring features.
4*08bad5a8SMark Brown  *
5*08bad5a8SMark Brown  * Copyright (C) 2009 Wolfson Microelectronics plc
6*08bad5a8SMark Brown  *
7*08bad5a8SMark Brown  * This program is free software; you can redistribute it and/or modify it
8*08bad5a8SMark Brown  * under the terms of the GNU General Public License v2 as published by the
9*08bad5a8SMark Brown  * Free Software Foundation.
10*08bad5a8SMark Brown  *
11*08bad5a8SMark Brown  * This program is distributed in the hope that it will be useful, but WITHOUT
12*08bad5a8SMark Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*08bad5a8SMark Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14*08bad5a8SMark Brown  * more details.
15*08bad5a8SMark Brown  *
16*08bad5a8SMark Brown  * You should have received a copy of the GNU General Public License along with
17*08bad5a8SMark Brown  * this program; if not, write to the Free Software Foundation, Inc.,
18*08bad5a8SMark Brown  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19*08bad5a8SMark Brown  */
20*08bad5a8SMark Brown 
21*08bad5a8SMark Brown #include <linux/kernel.h>
22*08bad5a8SMark Brown #include <linux/module.h>
23*08bad5a8SMark Brown #include <linux/platform_device.h>
24*08bad5a8SMark Brown #include <linux/err.h>
25*08bad5a8SMark Brown #include <linux/hwmon.h>
26*08bad5a8SMark Brown #include <linux/hwmon-sysfs.h>
27*08bad5a8SMark Brown 
28*08bad5a8SMark Brown #include <linux/mfd/wm831x/core.h>
29*08bad5a8SMark Brown #include <linux/mfd/wm831x/auxadc.h>
30*08bad5a8SMark Brown 
31*08bad5a8SMark Brown struct wm831x_hwmon {
32*08bad5a8SMark Brown 	struct wm831x *wm831x;
33*08bad5a8SMark Brown 	struct device *classdev;
34*08bad5a8SMark Brown };
35*08bad5a8SMark Brown 
36*08bad5a8SMark Brown static ssize_t show_name(struct device *dev,
37*08bad5a8SMark Brown 			 struct device_attribute *attr, char *buf)
38*08bad5a8SMark Brown {
39*08bad5a8SMark Brown 	return sprintf(buf, "wm831x\n");
40*08bad5a8SMark Brown }
41*08bad5a8SMark Brown 
42*08bad5a8SMark Brown static const char *input_names[] = {
43*08bad5a8SMark Brown 	[WM831X_AUX_SYSVDD]    = "SYSVDD",
44*08bad5a8SMark Brown 	[WM831X_AUX_USB]       = "USB",
45*08bad5a8SMark Brown 	[WM831X_AUX_BKUP_BATT] = "Backup battery",
46*08bad5a8SMark Brown 	[WM831X_AUX_BATT]      = "Battery",
47*08bad5a8SMark Brown 	[WM831X_AUX_WALL]      = "WALL",
48*08bad5a8SMark Brown 	[WM831X_AUX_CHIP_TEMP] = "PMIC",
49*08bad5a8SMark Brown 	[WM831X_AUX_BATT_TEMP] = "Battery",
50*08bad5a8SMark Brown };
51*08bad5a8SMark Brown 
52*08bad5a8SMark Brown 
53*08bad5a8SMark Brown static ssize_t show_voltage(struct device *dev,
54*08bad5a8SMark Brown 			    struct device_attribute *attr, char *buf)
55*08bad5a8SMark Brown {
56*08bad5a8SMark Brown 	struct wm831x_hwmon *hwmon = dev_get_drvdata(dev);
57*08bad5a8SMark Brown 	int channel = to_sensor_dev_attr(attr)->index;
58*08bad5a8SMark Brown 	int ret;
59*08bad5a8SMark Brown 
60*08bad5a8SMark Brown 	ret = wm831x_auxadc_read_uv(hwmon->wm831x, channel);
61*08bad5a8SMark Brown 	if (ret < 0)
62*08bad5a8SMark Brown 		return ret;
63*08bad5a8SMark Brown 
64*08bad5a8SMark Brown 	return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret, 1000));
65*08bad5a8SMark Brown }
66*08bad5a8SMark Brown 
67*08bad5a8SMark Brown static ssize_t show_chip_temp(struct device *dev,
68*08bad5a8SMark Brown 			      struct device_attribute *attr, char *buf)
69*08bad5a8SMark Brown {
70*08bad5a8SMark Brown 	struct wm831x_hwmon *hwmon = dev_get_drvdata(dev);
71*08bad5a8SMark Brown 	int channel = to_sensor_dev_attr(attr)->index;
72*08bad5a8SMark Brown 	int ret;
73*08bad5a8SMark Brown 
74*08bad5a8SMark Brown 	ret = wm831x_auxadc_read(hwmon->wm831x, channel);
75*08bad5a8SMark Brown 	if (ret < 0)
76*08bad5a8SMark Brown 		return ret;
77*08bad5a8SMark Brown 
78*08bad5a8SMark Brown 	/* Degrees celsius = (512.18-ret) / 1.0983 */
79*08bad5a8SMark Brown 	ret = 512180 - (ret * 1000);
80*08bad5a8SMark Brown 	ret = DIV_ROUND_CLOSEST(ret * 10000, 10983);
81*08bad5a8SMark Brown 
82*08bad5a8SMark Brown 	return sprintf(buf, "%d\n", ret);
83*08bad5a8SMark Brown }
84*08bad5a8SMark Brown 
85*08bad5a8SMark Brown static ssize_t show_label(struct device *dev,
86*08bad5a8SMark Brown 			  struct device_attribute *attr, char *buf)
87*08bad5a8SMark Brown {
88*08bad5a8SMark Brown 	int channel = to_sensor_dev_attr(attr)->index;
89*08bad5a8SMark Brown 
90*08bad5a8SMark Brown 	return sprintf(buf, "%s\n", input_names[channel]);
91*08bad5a8SMark Brown }
92*08bad5a8SMark Brown 
93*08bad5a8SMark Brown #define WM831X_VOLTAGE(id, name) \
94*08bad5a8SMark Brown 	static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage, \
95*08bad5a8SMark Brown 				  NULL, name)
96*08bad5a8SMark Brown 
97*08bad5a8SMark Brown #define WM831X_NAMED_VOLTAGE(id, name) \
98*08bad5a8SMark Brown 	WM831X_VOLTAGE(id, name); \
99*08bad5a8SMark Brown 	static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label,	\
100*08bad5a8SMark Brown 				  NULL, name)
101*08bad5a8SMark Brown 
102*08bad5a8SMark Brown static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
103*08bad5a8SMark Brown 
104*08bad5a8SMark Brown WM831X_VOLTAGE(0, WM831X_AUX_AUX1);
105*08bad5a8SMark Brown WM831X_VOLTAGE(1, WM831X_AUX_AUX2);
106*08bad5a8SMark Brown WM831X_VOLTAGE(2, WM831X_AUX_AUX3);
107*08bad5a8SMark Brown WM831X_VOLTAGE(3, WM831X_AUX_AUX4);
108*08bad5a8SMark Brown 
109*08bad5a8SMark Brown WM831X_NAMED_VOLTAGE(4, WM831X_AUX_SYSVDD);
110*08bad5a8SMark Brown WM831X_NAMED_VOLTAGE(5, WM831X_AUX_USB);
111*08bad5a8SMark Brown WM831X_NAMED_VOLTAGE(6, WM831X_AUX_BATT);
112*08bad5a8SMark Brown WM831X_NAMED_VOLTAGE(7, WM831X_AUX_WALL);
113*08bad5a8SMark Brown WM831X_NAMED_VOLTAGE(8, WM831X_AUX_BKUP_BATT);
114*08bad5a8SMark Brown 
115*08bad5a8SMark Brown static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_chip_temp, NULL,
116*08bad5a8SMark Brown 			  WM831X_AUX_CHIP_TEMP);
117*08bad5a8SMark Brown static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL,
118*08bad5a8SMark Brown 			  WM831X_AUX_CHIP_TEMP);
119*08bad5a8SMark Brown /* Report as a voltage since conversion depends on external components
120*08bad5a8SMark Brown  * and that's what the ABI wants. */
121*08bad5a8SMark Brown static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_voltage, NULL,
122*08bad5a8SMark Brown 			  WM831X_AUX_BATT_TEMP);
123*08bad5a8SMark Brown static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
124*08bad5a8SMark Brown 			  WM831X_AUX_BATT_TEMP);
125*08bad5a8SMark Brown 
126*08bad5a8SMark Brown static struct attribute *wm831x_attributes[] = {
127*08bad5a8SMark Brown 	&dev_attr_name.attr,
128*08bad5a8SMark Brown 
129*08bad5a8SMark Brown 	&sensor_dev_attr_in0_input.dev_attr.attr,
130*08bad5a8SMark Brown 	&sensor_dev_attr_in1_input.dev_attr.attr,
131*08bad5a8SMark Brown 	&sensor_dev_attr_in2_input.dev_attr.attr,
132*08bad5a8SMark Brown 	&sensor_dev_attr_in3_input.dev_attr.attr,
133*08bad5a8SMark Brown 
134*08bad5a8SMark Brown 	&sensor_dev_attr_in4_input.dev_attr.attr,
135*08bad5a8SMark Brown 	&sensor_dev_attr_in4_label.dev_attr.attr,
136*08bad5a8SMark Brown 	&sensor_dev_attr_in5_input.dev_attr.attr,
137*08bad5a8SMark Brown 	&sensor_dev_attr_in5_label.dev_attr.attr,
138*08bad5a8SMark Brown 	&sensor_dev_attr_in6_input.dev_attr.attr,
139*08bad5a8SMark Brown 	&sensor_dev_attr_in6_label.dev_attr.attr,
140*08bad5a8SMark Brown 	&sensor_dev_attr_in7_input.dev_attr.attr,
141*08bad5a8SMark Brown 	&sensor_dev_attr_in7_label.dev_attr.attr,
142*08bad5a8SMark Brown 	&sensor_dev_attr_in8_input.dev_attr.attr,
143*08bad5a8SMark Brown 	&sensor_dev_attr_in8_label.dev_attr.attr,
144*08bad5a8SMark Brown 
145*08bad5a8SMark Brown 	&sensor_dev_attr_temp1_input.dev_attr.attr,
146*08bad5a8SMark Brown 	&sensor_dev_attr_temp1_label.dev_attr.attr,
147*08bad5a8SMark Brown 	&sensor_dev_attr_temp2_input.dev_attr.attr,
148*08bad5a8SMark Brown 	&sensor_dev_attr_temp2_label.dev_attr.attr,
149*08bad5a8SMark Brown 
150*08bad5a8SMark Brown 	NULL
151*08bad5a8SMark Brown };
152*08bad5a8SMark Brown 
153*08bad5a8SMark Brown static const struct attribute_group wm831x_attr_group = {
154*08bad5a8SMark Brown 	.attrs	= wm831x_attributes,
155*08bad5a8SMark Brown };
156*08bad5a8SMark Brown 
157*08bad5a8SMark Brown static int __devinit wm831x_hwmon_probe(struct platform_device *pdev)
158*08bad5a8SMark Brown {
159*08bad5a8SMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
160*08bad5a8SMark Brown 	struct wm831x_hwmon *hwmon;
161*08bad5a8SMark Brown 	int ret;
162*08bad5a8SMark Brown 
163*08bad5a8SMark Brown 	hwmon = kzalloc(sizeof(struct wm831x_hwmon), GFP_KERNEL);
164*08bad5a8SMark Brown 	if (!hwmon)
165*08bad5a8SMark Brown 		return -ENOMEM;
166*08bad5a8SMark Brown 
167*08bad5a8SMark Brown 	hwmon->wm831x = wm831x;
168*08bad5a8SMark Brown 
169*08bad5a8SMark Brown 	ret = sysfs_create_group(&pdev->dev.kobj, &wm831x_attr_group);
170*08bad5a8SMark Brown 	if (ret)
171*08bad5a8SMark Brown 		goto err;
172*08bad5a8SMark Brown 
173*08bad5a8SMark Brown 	hwmon->classdev = hwmon_device_register(&pdev->dev);
174*08bad5a8SMark Brown 	if (IS_ERR(hwmon->classdev)) {
175*08bad5a8SMark Brown 		ret = PTR_ERR(hwmon->classdev);
176*08bad5a8SMark Brown 		goto err_sysfs;
177*08bad5a8SMark Brown 	}
178*08bad5a8SMark Brown 
179*08bad5a8SMark Brown 	platform_set_drvdata(pdev, hwmon);
180*08bad5a8SMark Brown 
181*08bad5a8SMark Brown 	return 0;
182*08bad5a8SMark Brown 
183*08bad5a8SMark Brown err_sysfs:
184*08bad5a8SMark Brown 	sysfs_remove_group(&pdev->dev.kobj, &wm831x_attr_group);
185*08bad5a8SMark Brown err:
186*08bad5a8SMark Brown 	kfree(hwmon);
187*08bad5a8SMark Brown 	return ret;
188*08bad5a8SMark Brown }
189*08bad5a8SMark Brown 
190*08bad5a8SMark Brown static int __devexit wm831x_hwmon_remove(struct platform_device *pdev)
191*08bad5a8SMark Brown {
192*08bad5a8SMark Brown 	struct wm831x_hwmon *hwmon = platform_get_drvdata(pdev);
193*08bad5a8SMark Brown 
194*08bad5a8SMark Brown 	hwmon_device_unregister(hwmon->classdev);
195*08bad5a8SMark Brown 	sysfs_remove_group(&pdev->dev.kobj, &wm831x_attr_group);
196*08bad5a8SMark Brown 	platform_set_drvdata(pdev, NULL);
197*08bad5a8SMark Brown 	kfree(hwmon);
198*08bad5a8SMark Brown 
199*08bad5a8SMark Brown 	return 0;
200*08bad5a8SMark Brown }
201*08bad5a8SMark Brown 
202*08bad5a8SMark Brown static struct platform_driver wm831x_hwmon_driver = {
203*08bad5a8SMark Brown 	.probe = wm831x_hwmon_probe,
204*08bad5a8SMark Brown 	.remove = __devexit_p(wm831x_hwmon_remove),
205*08bad5a8SMark Brown 	.driver = {
206*08bad5a8SMark Brown 		.name = "wm831x-hwmon",
207*08bad5a8SMark Brown 		.owner = THIS_MODULE,
208*08bad5a8SMark Brown 	},
209*08bad5a8SMark Brown };
210*08bad5a8SMark Brown 
211*08bad5a8SMark Brown static int __init wm831x_hwmon_init(void)
212*08bad5a8SMark Brown {
213*08bad5a8SMark Brown 	return platform_driver_register(&wm831x_hwmon_driver);
214*08bad5a8SMark Brown }
215*08bad5a8SMark Brown module_init(wm831x_hwmon_init);
216*08bad5a8SMark Brown 
217*08bad5a8SMark Brown static void __exit wm831x_hwmon_exit(void)
218*08bad5a8SMark Brown {
219*08bad5a8SMark Brown 	platform_driver_unregister(&wm831x_hwmon_driver);
220*08bad5a8SMark Brown }
221*08bad5a8SMark Brown module_exit(wm831x_hwmon_exit);
222*08bad5a8SMark Brown 
223*08bad5a8SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
224*08bad5a8SMark Brown MODULE_DESCRIPTION("WM831x Hardware Monitoring");
225*08bad5a8SMark Brown MODULE_LICENSE("GPL");
226*08bad5a8SMark Brown MODULE_ALIAS("platform:wm831x-hwmon");
227