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/module.h>
10 #include <linux/err.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/iio/consumer.h>
17 #include <linux/iio/types.h>
18
19 /**
20 * struct iio_hwmon_state - device instance state
21 * @channels: filled with array of channels from iio
22 * @num_channels: number of channels in channels (saves counting twice)
23 * @attr_group: the group of attributes
24 * @groups: null terminated array of attribute groups
25 * @attrs: null terminated array of attribute pointers.
26 */
27 struct iio_hwmon_state {
28 struct iio_channel *channels;
29 int num_channels;
30 struct attribute_group attr_group;
31 const struct attribute_group *groups[2];
32 struct attribute **attrs;
33 };
34
iio_hwmon_read_label(struct device * dev,struct device_attribute * attr,char * buf)35 static ssize_t iio_hwmon_read_label(struct device *dev,
36 struct device_attribute *attr,
37 char *buf)
38 {
39 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
40 struct iio_hwmon_state *state = dev_get_drvdata(dev);
41 struct iio_channel *chan = &state->channels[sattr->index];
42
43 return iio_read_channel_label(chan, buf);
44 }
45
46 /*
47 * Assumes that IIO and hwmon operate in the same base units.
48 * This is supposed to be true, but needs verification for
49 * new channel types.
50 */
iio_hwmon_read_val(struct device * dev,struct device_attribute * attr,char * buf)51 static ssize_t iio_hwmon_read_val(struct device *dev,
52 struct device_attribute *attr,
53 char *buf)
54 {
55 int result;
56 int ret;
57 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
58 struct iio_hwmon_state *state = dev_get_drvdata(dev);
59 struct iio_channel *chan = &state->channels[sattr->index];
60 enum iio_chan_type type;
61
62 ret = iio_get_channel_type(chan, &type);
63 if (ret < 0)
64 return ret;
65
66 if (type == IIO_POWER)
67 /* mili-Watts to micro-Watts conversion */
68 ret = iio_read_channel_processed_scale(chan, &result, 1000);
69 else
70 ret = iio_read_channel_processed(chan, &result);
71 if (ret < 0)
72 return ret;
73
74 return sprintf(buf, "%d\n", result);
75 }
76
iio_hwmon_probe(struct platform_device * pdev)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 MODULE_IMPORT_NS("IIO_CONSUMER");
225