xref: /linux/drivers/hwmon/iio_hwmon.c (revision a1944676767e855869b6af8e1c7e185372feaf31)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Hwmon client for industrial I/O devices
3  *
4  * Copyright (c) 2011 Jonathan Cameron
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/iio/consumer.h>
18 #include <linux/iio/types.h>
19 
20 /**
21  * struct iio_hwmon_state - device instance state
22  * @channels:		filled with array of channels from iio
23  * @num_channels:	number of channels in channels (saves counting twice)
24  * @attr_group:		the group of attributes
25  * @groups:		null terminated array of attribute groups
26  * @attrs:		null terminated array of attribute pointers.
27  */
28 struct iio_hwmon_state {
29 	struct iio_channel *channels;
30 	int num_channels;
31 	struct attribute_group attr_group;
32 	const struct attribute_group *groups[2];
33 	struct attribute **attrs;
34 };
35 
36 static ssize_t iio_hwmon_read_label(struct device *dev,
37 				  struct device_attribute *attr,
38 				  char *buf)
39 {
40 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
41 	struct iio_hwmon_state *state = dev_get_drvdata(dev);
42 	struct iio_channel *chan = &state->channels[sattr->index];
43 
44 	return iio_read_channel_label(chan, buf);
45 }
46 
47 /*
48  * Assumes that IIO and hwmon operate in the same base units.
49  * This is supposed to be true, but needs verification for
50  * new channel types.
51  */
52 static ssize_t iio_hwmon_read_val(struct device *dev,
53 				  struct device_attribute *attr,
54 				  char *buf)
55 {
56 	int result;
57 	int ret;
58 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
59 	struct iio_hwmon_state *state = dev_get_drvdata(dev);
60 	struct iio_channel *chan = &state->channels[sattr->index];
61 	enum iio_chan_type type;
62 
63 	ret = iio_read_channel_processed(chan, &result);
64 	if (ret < 0)
65 		return ret;
66 
67 	ret = iio_get_channel_type(chan, &type);
68 	if (ret < 0)
69 		return ret;
70 
71 	if (type == IIO_POWER)
72 		result *= 1000; /* mili-Watts to micro-Watts conversion */
73 
74 	return sprintf(buf, "%d\n", result);
75 }
76 
77 static int iio_hwmon_probe(struct platform_device *pdev)
78 {
79 	struct device *dev = &pdev->dev;
80 	struct iio_hwmon_state *st;
81 	struct sensor_device_attribute *a;
82 	int ret, i, attr = 0;
83 	int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
84 	enum iio_chan_type type;
85 	struct iio_channel *channels;
86 	struct device *hwmon_dev;
87 	char *sname;
88 	void *buf;
89 
90 	channels = devm_iio_channel_get_all(dev);
91 	if (IS_ERR(channels)) {
92 		ret = PTR_ERR(channels);
93 		if (ret == -ENODEV)
94 			ret = -EPROBE_DEFER;
95 		return dev_err_probe(dev, ret,
96 				     "Failed to get channels\n");
97 	}
98 
99 	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
100 	buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
101 	if (!st || !buf)
102 		return -ENOMEM;
103 
104 	st->channels = channels;
105 
106 	/* count how many channels we have */
107 	while (st->channels[st->num_channels].indio_dev)
108 		st->num_channels++;
109 
110 	st->attrs = devm_kcalloc(dev,
111 				 2 * st->num_channels + 1, sizeof(*st->attrs),
112 				 GFP_KERNEL);
113 	if (st->attrs == NULL)
114 		return -ENOMEM;
115 
116 	for (i = 0; i < st->num_channels; i++) {
117 		const char *prefix;
118 		int n;
119 
120 		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
121 		if (a == NULL)
122 			return -ENOMEM;
123 
124 		sysfs_attr_init(&a->dev_attr.attr);
125 		ret = iio_get_channel_type(&st->channels[i], &type);
126 		if (ret < 0)
127 			return ret;
128 
129 		switch (type) {
130 		case IIO_VOLTAGE:
131 			n = in_i++;
132 			prefix = "in";
133 			break;
134 		case IIO_TEMP:
135 			n = temp_i++;
136 			prefix = "temp";
137 			break;
138 		case IIO_CURRENT:
139 			n = curr_i++;
140 			prefix = "curr";
141 			break;
142 		case IIO_POWER:
143 			n = power_i++;
144 			prefix = "power";
145 			break;
146 		case IIO_HUMIDITYRELATIVE:
147 			n = humidity_i++;
148 			prefix = "humidity";
149 			break;
150 		default:
151 			return -EINVAL;
152 		}
153 
154 		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
155 						       "%s%d_input",
156 						       prefix, n);
157 		if (a->dev_attr.attr.name == NULL)
158 			return -ENOMEM;
159 
160 		a->dev_attr.show = iio_hwmon_read_val;
161 		a->dev_attr.attr.mode = 0444;
162 		a->index = i;
163 		st->attrs[attr++] = &a->dev_attr.attr;
164 
165 		/* Let's see if we have a label... */
166 		if (iio_read_channel_label(&st->channels[i], buf) < 0)
167 			continue;
168 
169 		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
170 		if (a == NULL)
171 			return -ENOMEM;
172 
173 		sysfs_attr_init(&a->dev_attr.attr);
174 		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
175 						       "%s%d_label",
176 						       prefix, n);
177 		if (!a->dev_attr.attr.name)
178 			return -ENOMEM;
179 
180 		a->dev_attr.show = iio_hwmon_read_label;
181 		a->dev_attr.attr.mode = 0444;
182 		a->index = i;
183 		st->attrs[attr++] = &a->dev_attr.attr;
184 	}
185 
186 	devm_free_pages(dev, (unsigned long)buf);
187 
188 	st->attr_group.attrs = st->attrs;
189 	st->groups[0] = &st->attr_group;
190 
191 	if (dev_fwnode(dev)) {
192 		sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev));
193 		if (!sname)
194 			return -ENOMEM;
195 		strreplace(sname, '-', '_');
196 	} else {
197 		sname = "iio_hwmon";
198 	}
199 
200 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
201 							   st->groups);
202 	return PTR_ERR_OR_ZERO(hwmon_dev);
203 }
204 
205 static const struct of_device_id iio_hwmon_of_match[] = {
206 	{ .compatible = "iio-hwmon", },
207 	{ }
208 };
209 MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
210 
211 static struct platform_driver iio_hwmon_driver = {
212 	.driver = {
213 		.name = "iio_hwmon",
214 		.of_match_table = iio_hwmon_of_match,
215 	},
216 	.probe = iio_hwmon_probe,
217 };
218 
219 module_platform_driver(iio_hwmon_driver);
220 
221 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
222 MODULE_DESCRIPTION("IIO to hwmon driver");
223 MODULE_LICENSE("GPL v2");
224