]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hwmon/iio_hwmon.c
UBUNTU: Ubuntu-5.11.0-22.23
[mirror_ubuntu-hirsute-kernel.git] / drivers / hwmon / iio_hwmon.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e0f8a24e
JC
2/* Hwmon client for industrial I/O devices
3 *
4 * Copyright (c) 2011 Jonathan Cameron
e0f8a24e
JC
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/hwmon.h>
3465a224 13#include <linux/of.h>
e0f8a24e 14#include <linux/hwmon-sysfs.h>
06458e27
JC
15#include <linux/iio/consumer.h>
16#include <linux/iio/types.h>
e0f8a24e
JC
17
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)
7f6d70cd
GR
22 * @attr_group: the group of attributes
23 * @groups: null terminated array of attribute groups
e0f8a24e
JC
24 * @attrs: null terminated array of attribute pointers.
25 */
26struct iio_hwmon_state {
27 struct iio_channel *channels;
28 int num_channels;
e0f8a24e 29 struct attribute_group attr_group;
4b49cca3 30 const struct attribute_group *groups[2];
e0f8a24e
JC
31 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{
a0e545e0
LPC
43 int result;
44 int ret;
e0f8a24e
JC
45 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
46 struct iio_hwmon_state *state = dev_get_drvdata(dev);
bc34301b
MS
47 struct iio_channel *chan = &state->channels[sattr->index];
48 enum iio_chan_type type;
49
50 ret = iio_read_channel_processed(chan, &result);
51 if (ret < 0)
52 return ret;
e0f8a24e 53
bc34301b 54 ret = iio_get_channel_type(chan, &type);
e0f8a24e
JC
55 if (ret < 0)
56 return ret;
57
bc34301b
MS
58 if (type == IIO_POWER)
59 result *= 1000; /* mili-Watts to micro-Watts conversion */
60
a0e545e0 61 return sprintf(buf, "%d\n", result);
e0f8a24e
JC
62}
63
4ae1c61f 64static int iio_hwmon_probe(struct platform_device *pdev)
e0f8a24e 65{
c4ac7b98 66 struct device *dev = &pdev->dev;
e0f8a24e
JC
67 struct iio_hwmon_state *st;
68 struct sensor_device_attribute *a;
69 int ret, i;
bc34301b 70 int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
e0f8a24e 71 enum iio_chan_type type;
ca7d98db 72 struct iio_channel *channels;
12005ec3 73 struct device *hwmon_dev;
b92fe9e3 74 char *sname;
4b49cca3 75
12005ec3 76 channels = devm_iio_channel_get_all(dev);
9417fefe
QS
77 if (IS_ERR(channels)) {
78 if (PTR_ERR(channels) == -ENODEV)
79 return -EPROBE_DEFER;
ca7d98db 80 return PTR_ERR(channels);
9417fefe 81 }
e0f8a24e 82
c4ac7b98 83 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
12005ec3
MRB
84 if (st == NULL)
85 return -ENOMEM;
e0f8a24e 86
ca7d98db 87 st->channels = channels;
e0f8a24e
JC
88
89 /* count how many attributes we have */
90 while (st->channels[st->num_channels].indio_dev)
91 st->num_channels++;
92
a86854d0
KC
93 st->attrs = devm_kcalloc(dev,
94 st->num_channels + 1, sizeof(*st->attrs),
c4ac7b98 95 GFP_KERNEL);
12005ec3
MRB
96 if (st->attrs == NULL)
97 return -ENOMEM;
c4ac7b98 98
e0f8a24e 99 for (i = 0; i < st->num_channels; i++) {
cb202bb8
AS
100 const char *prefix;
101 int n;
102
c4ac7b98 103 a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
12005ec3
MRB
104 if (a == NULL)
105 return -ENOMEM;
e0f8a24e
JC
106
107 sysfs_attr_init(&a->dev_attr.attr);
314be14b 108 ret = iio_get_channel_type(&st->channels[i], &type);
c4ac7b98 109 if (ret < 0)
12005ec3 110 return ret;
c4ac7b98 111
e0f8a24e
JC
112 switch (type) {
113 case IIO_VOLTAGE:
cb202bb8
AS
114 n = in_i++;
115 prefix = "in";
e0f8a24e
JC
116 break;
117 case IIO_TEMP:
cb202bb8
AS
118 n = temp_i++;
119 prefix = "temp";
e0f8a24e
JC
120 break;
121 case IIO_CURRENT:
cb202bb8
AS
122 n = curr_i++;
123 prefix = "curr";
e0f8a24e 124 break;
bc34301b
MS
125 case IIO_POWER:
126 n = power_i++;
127 prefix = "power";
128 break;
61bb53bc 129 case IIO_HUMIDITYRELATIVE:
cb202bb8
AS
130 n = humidity_i++;
131 prefix = "humidity";
61bb53bc 132 break;
e0f8a24e 133 default:
12005ec3 134 return -EINVAL;
e0f8a24e 135 }
cb202bb8
AS
136
137 a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
138 "%s%d_input",
139 prefix, n);
12005ec3
MRB
140 if (a->dev_attr.attr.name == NULL)
141 return -ENOMEM;
142
e0f8a24e 143 a->dev_attr.show = iio_hwmon_read_val;
389bc38e 144 a->dev_attr.attr.mode = 0444;
e0f8a24e
JC
145 a->index = i;
146 st->attrs[i] = &a->dev_attr.attr;
147 }
e0f8a24e 148
4b49cca3
GR
149 st->attr_group.attrs = st->attrs;
150 st->groups[0] = &st->attr_group;
b92fe9e3 151
86103cff 152 if (dev->of_node) {
0debe4d0 153 sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
86103cff
GR
154 if (!sname)
155 return -ENOMEM;
156 strreplace(sname, '-', '_');
157 } else {
158 sname = "iio_hwmon";
159 }
b92fe9e3 160
12005ec3
MRB
161 hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
162 st->groups);
163 return PTR_ERR_OR_ZERO(hwmon_dev);
e0f8a24e
JC
164}
165
cfe03d64 166static const struct of_device_id iio_hwmon_of_match[] = {
a11e619b
GR
167 { .compatible = "iio-hwmon", },
168 { }
169};
2ec28196 170MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
a11e619b 171
561e3121 172static struct platform_driver iio_hwmon_driver = {
e0f8a24e
JC
173 .driver = {
174 .name = "iio_hwmon",
a11e619b 175 .of_match_table = iio_hwmon_of_match,
e0f8a24e
JC
176 },
177 .probe = iio_hwmon_probe,
e0f8a24e
JC
178};
179
d16f6dbd 180module_platform_driver(iio_hwmon_driver);
e0f8a24e 181
0f8c9620 182MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
e0f8a24e
JC
183MODULE_DESCRIPTION("IIO to hwmon driver");
184MODULE_LICENSE("GPL v2");