blob: b85a125dd86f46a26ac817bd42a718c4ad97310c [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>
9#include <linux/module.h>
10#include <linux/err.h>
11#include <linux/platform_device.h>
12#include <linux/hwmon.h>
Guenter Roeck3465a222013-03-20 15:52:00 +000013#include <linux/of.h>
Jonathan Camerone0f8a242012-02-15 19:48:03 +000014#include <linux/hwmon-sysfs.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/consumer.h>
16#include <linux/iio/types.h>
Jonathan Camerone0f8a242012-02-15 19:48:03 +000017
18/**
19 * struct iio_hwmon_state - device instance state
20 * @channels: filled with array of channels from iio
21 * @num_channels: number of channels in channels (saves counting twice)
Guenter Roeck7f6d70c2017-12-03 15:22:26 -080022 * @attr_group: the group of attributes
23 * @groups: null terminated array of attribute groups
Jonathan Camerone0f8a242012-02-15 19:48:03 +000024 * @attrs: null terminated array of attribute pointers.
25 */
26struct iio_hwmon_state {
27 struct iio_channel *channels;
28 int num_channels;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000029 struct attribute_group attr_group;
Guenter Roeck4b49cca2013-12-01 19:33:16 -080030 const struct attribute_group *groups[2];
Jonathan Camerone0f8a242012-02-15 19:48:03 +000031 struct attribute **attrs;
32};
33
34/*
35 * Assumes that IIO and hwmon operate in the same base units.
36 * This is supposed to be true, but needs verification for
37 * new channel types.
38 */
39static ssize_t iio_hwmon_read_val(struct device *dev,
40 struct device_attribute *attr,
41 char *buf)
42{
Lars-Peter Clausena0e545e2012-09-17 13:17:00 +010043 int result;
44 int ret;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000045 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
46 struct iio_hwmon_state *state = dev_get_drvdata(dev);
Michal Simekbc343012019-08-22 16:22:24 +020047 struct iio_channel *chan = &state->channels[sattr->index];
48 enum iio_chan_type type;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000049
Michal Simekbc343012019-08-22 16:22:24 +020050 ret = iio_read_channel_processed(chan, &result);
Jonathan Camerone0f8a242012-02-15 19:48:03 +000051 if (ret < 0)
52 return ret;
53
Michal Simekbc343012019-08-22 16:22:24 +020054 ret = iio_get_channel_type(chan, &type);
55 if (ret < 0)
56 return ret;
57
58 if (type == IIO_POWER)
59 result *= 1000; /* mili-Watts to micro-Watts conversion */
60
Lars-Peter Clausena0e545e2012-09-17 13:17:00 +010061 return sprintf(buf, "%d\n", result);
Jonathan Camerone0f8a242012-02-15 19:48:03 +000062}
63
Bill Pemberton4ae1c612012-11-19 13:21:57 -050064static int iio_hwmon_probe(struct platform_device *pdev)
Jonathan Camerone0f8a242012-02-15 19:48:03 +000065{
Guenter Roeckc4ac7b92013-01-31 21:42:00 +000066 struct device *dev = &pdev->dev;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000067 struct iio_hwmon_state *st;
68 struct sensor_device_attribute *a;
69 int ret, i;
Michal Simekbc343012019-08-22 16:22:24 +020070 int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000071 enum iio_chan_type type;
Guenter Roeckca7d98d2013-01-31 21:43:00 +000072 struct iio_channel *channels;
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040073 struct device *hwmon_dev;
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +053074 char *sname;
Guenter Roeck4b49cca2013-12-01 19:33:16 -080075
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040076 channels = devm_iio_channel_get_all(dev);
Quentin Schulz9417fef2016-09-08 16:28:35 +020077 if (IS_ERR(channels)) {
78 if (PTR_ERR(channels) == -ENODEV)
79 return -EPROBE_DEFER;
Guenter Roeckca7d98d2013-01-31 21:43:00 +000080 return PTR_ERR(channels);
Quentin Schulz9417fef2016-09-08 16:28:35 +020081 }
Jonathan Camerone0f8a242012-02-15 19:48:03 +000082
Guenter Roeckc4ac7b92013-01-31 21:42:00 +000083 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040084 if (st == NULL)
85 return -ENOMEM;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000086
Guenter Roeckca7d98d2013-01-31 21:43:00 +000087 st->channels = channels;
Jonathan Camerone0f8a242012-02-15 19:48:03 +000088
89 /* count how many attributes we have */
90 while (st->channels[st->num_channels].indio_dev)
91 st->num_channels++;
92
Kees Cooka86854d2018-06-12 14:07:58 -070093 st->attrs = devm_kcalloc(dev,
94 st->num_channels + 1, sizeof(*st->attrs),
Guenter Roeckc4ac7b92013-01-31 21:42:00 +000095 GFP_KERNEL);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -040096 if (st->attrs == NULL)
97 return -ENOMEM;
Guenter Roeckc4ac7b92013-01-31 21:42:00 +000098
Jonathan Camerone0f8a242012-02-15 19:48:03 +000099 for (i = 0; i < st->num_channels; i++) {
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700100 const char *prefix;
101 int n;
102
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000103 a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400104 if (a == NULL)
105 return -ENOMEM;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000106
107 sysfs_attr_init(&a->dev_attr.attr);
Jonathan Cameron314be142012-05-01 21:04:24 +0100108 ret = iio_get_channel_type(&st->channels[i], &type);
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000109 if (ret < 0)
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400110 return ret;
Guenter Roeckc4ac7b92013-01-31 21:42:00 +0000111
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000112 switch (type) {
113 case IIO_VOLTAGE:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700114 n = in_i++;
115 prefix = "in";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000116 break;
117 case IIO_TEMP:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700118 n = temp_i++;
119 prefix = "temp";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000120 break;
121 case IIO_CURRENT:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700122 n = curr_i++;
123 prefix = "curr";
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000124 break;
Michal Simekbc343012019-08-22 16:22:24 +0200125 case IIO_POWER:
126 n = power_i++;
127 prefix = "power";
128 break;
Guenter Roeck61bb53b2014-09-27 08:31:12 -0700129 case IIO_HUMIDITYRELATIVE:
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700130 n = humidity_i++;
131 prefix = "humidity";
Guenter Roeck61bb53b2014-09-27 08:31:12 -0700132 break;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000133 default:
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400134 return -EINVAL;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000135 }
Andrey Smirnovcb202bb2019-04-02 21:28:11 -0700136
137 a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
138 "%s%d_input",
139 prefix, n);
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400140 if (a->dev_attr.attr.name == NULL)
141 return -ENOMEM;
142
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000143 a->dev_attr.show = iio_hwmon_read_val;
Guenter Roeck389bc382018-12-10 14:02:09 -0800144 a->dev_attr.attr.mode = 0444;
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000145 a->index = i;
146 st->attrs[i] = &a->dev_attr.attr;
147 }
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000148
Guenter Roeck4b49cca2013-12-01 19:33:16 -0800149 st->attr_group.attrs = st->attrs;
150 st->groups[0] = &st->attr_group;
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +0530151
Guenter Roeck86103cf2018-08-28 11:41:41 -0700152 if (dev->of_node) {
Rob Herring0debe4d2018-08-27 20:52:21 -0500153 sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
Guenter Roeck86103cf2018-08-28 11:41:41 -0700154 if (!sname)
155 return -ENOMEM;
156 strreplace(sname, '-', '_');
157 } else {
158 sname = "iio_hwmon";
159 }
Sanchayan Maityb92fe9e2016-02-16 10:30:53 +0530160
Maxime Roussin-Bélanger12005ec2018-07-22 23:33:29 -0400161 hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
162 st->groups);
163 return PTR_ERR_OR_ZERO(hwmon_dev);
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000164}
165
Jingoo Hancfe03d62014-05-07 17:29:02 +0900166static const struct of_device_id iio_hwmon_of_match[] = {
Guenter Roecka11e6192013-01-31 21:43:00 +0000167 { .compatible = "iio-hwmon", },
168 { }
169};
Sebastian Andrzej Siewior2ec28192013-06-10 17:47:45 +0200170MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
Guenter Roecka11e6192013-01-31 21:43:00 +0000171
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000172static struct platform_driver __refdata iio_hwmon_driver = {
173 .driver = {
174 .name = "iio_hwmon",
Guenter Roecka11e6192013-01-31 21:43:00 +0000175 .of_match_table = iio_hwmon_of_match,
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000176 },
177 .probe = iio_hwmon_probe,
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000178};
179
Devendra Nagad16f6db2012-07-21 09:54:00 +0100180module_platform_driver(iio_hwmon_driver);
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000181
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100182MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerone0f8a242012-02-15 19:48:03 +0000183MODULE_DESCRIPTION("IIO to hwmon driver");
184MODULE_LICENSE("GPL v2");