blob: e376d4cde5ad59d1bd50c84a17cfc3a7551a76ef [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Camerone0f8a242012-02-15 19:48:03 +00002/* Hwmon client for industrial I/O devices
3 *
4 * Copyright (c) 2011 Jonathan Cameron
Jonathan Camerone0f8a242012-02-15 19:48:03 +00005 */
6
7#include <linux/kernel.h>
8#include <linux/slab.h>
Andy Shevchenkob7b568c2022-08-26 20:37:00 +03009#include <linux/mod_devicetable.h>
Jonathan Camerone0f8a242012-02-15 19:48:03 +000010#include <linux/module.h>
11#include <linux/err.h>
12#include <linux/platform_device.h>
Andy Shevchenkob7b568c2022-08-26 20:37:00 +030013#include <linux/property.h>
14
Jonathan Camerone0f8a242012-02-15 19:48:03 +000015#include <linux/hwmon.h>
16#include <linux/hwmon-sysfs.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/consumer.h>
18#include <linux/iio/types.h>
Jonathan Camerone0f8a242012-02-15 19:48:03 +000019
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)
Guenter Roeck7f6d70c2017-12-03 15:22:26 -080024 * @attr_group: the group of attributes
25 * @groups: null terminated array of attribute groups
Jonathan Camerone0f8a242012-02-15 19:48:03 +000026 * @attrs: null terminated array of attribute pointers.
27 */
28struct iio_hwmon_state {
29 struct iio_channel *channels;
30 int num_channels;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000031 struct attribute_group attr_group;
Guenter Roeck4b49cca2013-12-01 19:33:16 -080032 const struct attribute_group *groups[2];
Jonathan Camerone0f8a242012-02-15 19:48:03 +000033 struct attribute **attrs;
34};
35
Sean Anderson440db402024-06-24 13:46:01 -040036static 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
Jonathan Camerone0f8a242012-02-15 19:48:03 +000047/*
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 */
52static ssize_t iio_hwmon_read_val(struct device *dev,
53 struct device_attribute *attr,
54 char *buf)
55{
Lars-Peter Clausena0e545e2012-09-17 13:17:00 +010056 int result;
57 int ret;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000058 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
59 struct iio_hwmon_state *state = dev_get_drvdata(dev);
Michal Simekbc343012019-08-22 16:22:24 +020060 struct iio_channel *chan = &state->channels[sattr->index];
61 enum iio_chan_type type;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000062
Michal Simekbc343012019-08-22 16:22:24 +020063 ret = iio_get_channel_type(chan, &type);
64 if (ret < 0)
65 return ret;
66
67 if (type == IIO_POWER)
Sean Anderson52c1e812024-06-20 17:20:05 -040068 /* mili-Watts to micro-Watts conversion */
69 ret = iio_read_channel_processed_scale(chan, &result, 1000);
70 else
71 ret = iio_read_channel_processed(chan, &result);
72 if (ret < 0)
73 return ret;
Michal Simekbc343012019-08-22 16:22:24 +020074
Lars-Peter Clausena0e545e2012-09-17 13:17:00 +010075 return sprintf(buf, "%d\n", result);
Jonathan Camerone0f8a242012-02-15 19:48:03 +000076}
77
Bill Pemberton4ae1c612012-11-19 13:21:57 -050078static int iio_hwmon_probe(struct platform_device *pdev)
Jonathan Camerone0f8a242012-02-15 19:48:03 +000079{
Guenter Roeckc4ac7b92013-01-31 21:42:00 +000080 struct device *dev = &pdev->dev;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000081 struct iio_hwmon_state *st;
82 struct sensor_device_attribute *a;
Sean Anderson440db402024-06-24 13:46:01 -040083 int ret, i, attr = 0;
Michal Simekbc343012019-08-22 16:22:24 +020084 int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000085 enum iio_chan_type type;
Guenter Roeckca7d98d2013-01-31 21:43:00 +000086 struct iio_channel *channels;
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040087 struct device *hwmon_dev;
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +053088 char *sname;
Sean Anderson440db402024-06-24 13:46:01 -040089 void *buf;
Guenter Roeck4b49cca2013-12-01 19:33:16 -080090
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040091 channels = devm_iio_channel_get_all(dev);
Quentin Schulz9417fef2016-09-08 16:28:35 +020092 if (IS_ERR(channels)) {
Alexander Stein1c999af2023-01-31 11:33:59 +010093 ret = PTR_ERR(channels);
94 if (ret == -ENODEV)
95 ret = -EPROBE_DEFER;
96 return dev_err_probe(dev, ret,
97 "Failed to get channels\n");
Quentin Schulz9417fef2016-09-08 16:28:35 +020098 }
Jonathan Camerone0f8a242012-02-15 19:48:03 +000099
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000100 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
Sean Anderson440db402024-06-24 13:46:01 -0400101 buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
102 if (!st || !buf)
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400103 return -ENOMEM;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000104
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000105 st->channels = channels;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000106
Sean Anderson440db402024-06-24 13:46:01 -0400107 /* count how many channels we have */
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000108 while (st->channels[st->num_channels].indio_dev)
109 st->num_channels++;
110
Kees Cooka86854d2018-06-12 14:07:58 -0700111 st->attrs = devm_kcalloc(dev,
Sean Anderson440db402024-06-24 13:46:01 -0400112 2 * st->num_channels + 1, sizeof(*st->attrs),
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000113 GFP_KERNEL);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400114 if (st->attrs == NULL)
115 return -ENOMEM;
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000116
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000117 for (i = 0; i < st->num_channels; i++) {
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700118 const char *prefix;
119 int n;
120
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000121 a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400122 if (a == NULL)
123 return -ENOMEM;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000124
125 sysfs_attr_init(&a->dev_attr.attr);
Jonathan Cameron314be142012-05-01 21:04:24 +0100126 ret = iio_get_channel_type(&st->channels[i], &type);
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000127 if (ret < 0)
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400128 return ret;
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000129
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000130 switch (type) {
131 case IIO_VOLTAGE:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700132 n = in_i++;
133 prefix = "in";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000134 break;
135 case IIO_TEMP:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700136 n = temp_i++;
137 prefix = "temp";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000138 break;
139 case IIO_CURRENT:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700140 n = curr_i++;
141 prefix = "curr";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000142 break;
Michal Simekbc343012019-08-22 16:22:24 +0200143 case IIO_POWER:
144 n = power_i++;
145 prefix = "power";
146 break;
Guenter Roeck61bb53b2014-09-27 08:31:12 -0700147 case IIO_HUMIDITYRELATIVE:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700148 n = humidity_i++;
149 prefix = "humidity";
Guenter Roeck61bb53b2014-09-27 08:31:12 -0700150 break;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000151 default:
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400152 return -EINVAL;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000153 }
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700154
155 a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
156 "%s%d_input",
157 prefix, n);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400158 if (a->dev_attr.attr.name == NULL)
159 return -ENOMEM;
160
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000161 a->dev_attr.show = iio_hwmon_read_val;
Guenter Roeck389bc382018-12-10 14:02:09 -0800162 a->dev_attr.attr.mode = 0444;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000163 a->index = i;
Sean Anderson440db402024-06-24 13:46:01 -0400164 st->attrs[attr++] = &a->dev_attr.attr;
165
166 /* Let's see if we have a label... */
167 if (iio_read_channel_label(&st->channels[i], buf) < 0)
168 continue;
169
170 a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
171 if (a == NULL)
172 return -ENOMEM;
173
174 sysfs_attr_init(&a->dev_attr.attr);
175 a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
176 "%s%d_label",
177 prefix, n);
178 if (!a->dev_attr.attr.name)
179 return -ENOMEM;
180
181 a->dev_attr.show = iio_hwmon_read_label;
182 a->dev_attr.attr.mode = 0444;
183 a->index = i;
184 st->attrs[attr++] = &a->dev_attr.attr;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000185 }
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000186
Sean Anderson440db402024-06-24 13:46:01 -0400187 devm_free_pages(dev, (unsigned long)buf);
188
Guenter Roeck4b49cca2013-12-01 19:33:16 -0800189 st->attr_group.attrs = st->attrs;
190 st->groups[0] = &st->attr_group;
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +0530191
Andy Shevchenkob7b568c2022-08-26 20:37:00 +0300192 if (dev_fwnode(dev)) {
193 sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev));
Guenter Roeck86103cf2018-08-28 11:41:41 -0700194 if (!sname)
195 return -ENOMEM;
196 strreplace(sname, '-', '_');
197 } else {
198 sname = "iio_hwmon";
199 }
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +0530200
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400201 hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
202 st->groups);
203 return PTR_ERR_OR_ZERO(hwmon_dev);
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000204}
205
Jingoo Hancfe03d62014-05-07 17:29:02 +0900206static const struct of_device_id iio_hwmon_of_match[] = {
Guenter Roecka11e6192013-01-31 21:43:00 +0000207 { .compatible = "iio-hwmon", },
208 { }
209};
Sebastian Andrzej Siewior2ec28192013-06-10 17:47:45 +0200210MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
Guenter Roecka11e6192013-01-31 21:43:00 +0000211
Geert Uytterhoeven561e3122020-12-11 14:35:12 +0100212static struct platform_driver iio_hwmon_driver = {
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000213 .driver = {
214 .name = "iio_hwmon",
Guenter Roecka11e6192013-01-31 21:43:00 +0000215 .of_match_table = iio_hwmon_of_match,
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000216 },
217 .probe = iio_hwmon_probe,
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000218};
219
Devendra Nagad16f6db2012-07-21 09:54:00 +0100220module_platform_driver(iio_hwmon_driver);
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000221
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100222MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000223MODULE_DESCRIPTION("IIO to hwmon driver");
224MODULE_LICENSE("GPL v2");