1104871Sobrien// SPDX-License-Identifier: GPL-2.0-only
2104871Sobrien/* Hwmon client for industrial I/O devices
3104871Sobrien *
4104871Sobrien * Copyright (c) 2011 Jonathan Cameron
5104871Sobrien */
6104871Sobrien
7104871Sobrien#include <linux/kernel.h>
8104871Sobrien#include <linux/slab.h>
9104871Sobrien#include <linux/mod_devicetable.h>
10104871Sobrien#include <linux/module.h>
11104871Sobrien#include <linux/err.h>
12104871Sobrien#include <linux/platform_device.h>
13104871Sobrien#include <linux/property.h>
14104871Sobrien
15104871Sobrien#include <linux/hwmon.h>
16104871Sobrien#include <linux/hwmon-sysfs.h>
17104871Sobrien#include <linux/iio/consumer.h>
18104871Sobrien#include <linux/iio/types.h>
19104871Sobrien
20104871Sobrien/**
21104871Sobrien * struct iio_hwmon_state - device instance state
22104871Sobrien * @channels:		filled with array of channels from iio
23104871Sobrien * @num_channels:	number of channels in channels (saves counting twice)
24104871Sobrien * @attr_group:		the group of attributes
25104871Sobrien * @groups:		null terminated array of attribute groups
26104871Sobrien * @attrs:		null terminated array of attribute pointers.
27104871Sobrien */
28104871Sobrienstruct iio_hwmon_state {
29104871Sobrien	struct iio_channel *channels;
30104871Sobrien	int num_channels;
31104871Sobrien	struct attribute_group attr_group;
32104871Sobrien	const struct attribute_group *groups[2];
33104871Sobrien	struct attribute **attrs;
34104871Sobrien};
35104871Sobrien
36104871Sobrien/*
37104871Sobrien * Assumes that IIO and hwmon operate in the same base units.
38104871Sobrien * This is supposed to be true, but needs verification for
39104871Sobrien * new channel types.
40104871Sobrien */
41104871Sobrienstatic ssize_t iio_hwmon_read_val(struct device *dev,
42104871Sobrien				  struct device_attribute *attr,
43104871Sobrien				  char *buf)
44104871Sobrien{
45104871Sobrien	int result;
46104871Sobrien	int ret;
47104871Sobrien	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
48104871Sobrien	struct iio_hwmon_state *state = dev_get_drvdata(dev);
49104871Sobrien	struct iio_channel *chan = &state->channels[sattr->index];
50104871Sobrien	enum iio_chan_type type;
51104871Sobrien
52104871Sobrien	ret = iio_read_channel_processed(chan, &result);
53104871Sobrien	if (ret < 0)
54104871Sobrien		return ret;
55104871Sobrien
56104871Sobrien	ret = iio_get_channel_type(chan, &type);
57	if (ret < 0)
58		return ret;
59
60	if (type == IIO_POWER)
61		result *= 1000; /* mili-Watts to micro-Watts conversion */
62
63	return sprintf(buf, "%d\n", result);
64}
65
66static int iio_hwmon_probe(struct platform_device *pdev)
67{
68	struct device *dev = &pdev->dev;
69	struct iio_hwmon_state *st;
70	struct sensor_device_attribute *a;
71	int ret, i;
72	int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
73	enum iio_chan_type type;
74	struct iio_channel *channels;
75	struct device *hwmon_dev;
76	char *sname;
77
78	channels = devm_iio_channel_get_all(dev);
79	if (IS_ERR(channels)) {
80		ret = PTR_ERR(channels);
81		if (ret == -ENODEV)
82			ret = -EPROBE_DEFER;
83		return dev_err_probe(dev, ret,
84				     "Failed to get channels\n");
85	}
86
87	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
88	if (st == NULL)
89		return -ENOMEM;
90
91	st->channels = channels;
92
93	/* count how many attributes we have */
94	while (st->channels[st->num_channels].indio_dev)
95		st->num_channels++;
96
97	st->attrs = devm_kcalloc(dev,
98				 st->num_channels + 1, sizeof(*st->attrs),
99				 GFP_KERNEL);
100	if (st->attrs == NULL)
101		return -ENOMEM;
102
103	for (i = 0; i < st->num_channels; i++) {
104		const char *prefix;
105		int n;
106
107		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
108		if (a == NULL)
109			return -ENOMEM;
110
111		sysfs_attr_init(&a->dev_attr.attr);
112		ret = iio_get_channel_type(&st->channels[i], &type);
113		if (ret < 0)
114			return ret;
115
116		switch (type) {
117		case IIO_VOLTAGE:
118			n = in_i++;
119			prefix = "in";
120			break;
121		case IIO_TEMP:
122			n = temp_i++;
123			prefix = "temp";
124			break;
125		case IIO_CURRENT:
126			n = curr_i++;
127			prefix = "curr";
128			break;
129		case IIO_POWER:
130			n = power_i++;
131			prefix = "power";
132			break;
133		case IIO_HUMIDITYRELATIVE:
134			n = humidity_i++;
135			prefix = "humidity";
136			break;
137		default:
138			return -EINVAL;
139		}
140
141		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
142						       "%s%d_input",
143						       prefix, n);
144		if (a->dev_attr.attr.name == NULL)
145			return -ENOMEM;
146
147		a->dev_attr.show = iio_hwmon_read_val;
148		a->dev_attr.attr.mode = 0444;
149		a->index = i;
150		st->attrs[i] = &a->dev_attr.attr;
151	}
152
153	st->attr_group.attrs = st->attrs;
154	st->groups[0] = &st->attr_group;
155
156	if (dev_fwnode(dev)) {
157		sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev));
158		if (!sname)
159			return -ENOMEM;
160		strreplace(sname, '-', '_');
161	} else {
162		sname = "iio_hwmon";
163	}
164
165	hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
166							   st->groups);
167	return PTR_ERR_OR_ZERO(hwmon_dev);
168}
169
170static const struct of_device_id iio_hwmon_of_match[] = {
171	{ .compatible = "iio-hwmon", },
172	{ }
173};
174MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
175
176static struct platform_driver iio_hwmon_driver = {
177	.driver = {
178		.name = "iio_hwmon",
179		.of_match_table = iio_hwmon_of_match,
180	},
181	.probe = iio_hwmon_probe,
182};
183
184module_platform_driver(iio_hwmon_driver);
185
186MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
187MODULE_DESCRIPTION("IIO to hwmon driver");
188MODULE_LICENSE("GPL v2");
189