2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-02-16 03:48:03 +08:00
|
|
|
/* Hwmon client for industrial I/O devices
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Jonathan Cameron
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/hwmon.h>
|
2013-03-20 23:52:00 +08:00
|
|
|
#include <linux/of.h>
|
2012-02-16 03:48:03 +08:00
|
|
|
#include <linux/hwmon-sysfs.h>
|
2012-04-25 22:54:58 +08:00
|
|
|
#include <linux/iio/consumer.h>
|
|
|
|
#include <linux/iio/types.h>
|
2012-02-16 03:48:03 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* struct iio_hwmon_state - device instance state
|
|
|
|
* @channels: filled with array of channels from iio
|
|
|
|
* @num_channels: number of channels in channels (saves counting twice)
|
2017-12-04 07:22:26 +08:00
|
|
|
* @attr_group: the group of attributes
|
|
|
|
* @groups: null terminated array of attribute groups
|
2012-02-16 03:48:03 +08:00
|
|
|
* @attrs: null terminated array of attribute pointers.
|
|
|
|
*/
|
|
|
|
struct iio_hwmon_state {
|
|
|
|
struct iio_channel *channels;
|
|
|
|
int num_channels;
|
|
|
|
struct attribute_group attr_group;
|
2013-12-02 11:33:16 +08:00
|
|
|
const struct attribute_group *groups[2];
|
2012-02-16 03:48:03 +08:00
|
|
|
struct attribute **attrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assumes that IIO and hwmon operate in the same base units.
|
|
|
|
* This is supposed to be true, but needs verification for
|
|
|
|
* new channel types.
|
|
|
|
*/
|
|
|
|
static ssize_t iio_hwmon_read_val(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2012-09-17 20:17:00 +08:00
|
|
|
int result;
|
|
|
|
int ret;
|
2012-02-16 03:48:03 +08:00
|
|
|
struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
|
|
|
|
struct iio_hwmon_state *state = dev_get_drvdata(dev);
|
2019-08-22 22:22:24 +08:00
|
|
|
struct iio_channel *chan = &state->channels[sattr->index];
|
|
|
|
enum iio_chan_type type;
|
|
|
|
|
|
|
|
ret = iio_read_channel_processed(chan, &result);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-02-16 03:48:03 +08:00
|
|
|
|
2019-08-22 22:22:24 +08:00
|
|
|
ret = iio_get_channel_type(chan, &type);
|
2012-02-16 03:48:03 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-08-22 22:22:24 +08:00
|
|
|
if (type == IIO_POWER)
|
|
|
|
result *= 1000; /* mili-Watts to micro-Watts conversion */
|
|
|
|
|
2012-09-17 20:17:00 +08:00
|
|
|
return sprintf(buf, "%d\n", result);
|
2012-02-16 03:48:03 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 02:21:57 +08:00
|
|
|
static int iio_hwmon_probe(struct platform_device *pdev)
|
2012-02-16 03:48:03 +08:00
|
|
|
{
|
2013-02-01 05:42:00 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
2012-02-16 03:48:03 +08:00
|
|
|
struct iio_hwmon_state *st;
|
|
|
|
struct sensor_device_attribute *a;
|
|
|
|
int ret, i;
|
2019-08-22 22:22:24 +08:00
|
|
|
int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
|
2012-02-16 03:48:03 +08:00
|
|
|
enum iio_chan_type type;
|
2013-02-01 05:43:00 +08:00
|
|
|
struct iio_channel *channels;
|
2018-07-23 11:33:29 +08:00
|
|
|
struct device *hwmon_dev;
|
2016-02-16 13:00:53 +08:00
|
|
|
char *sname;
|
2013-12-02 11:33:16 +08:00
|
|
|
|
2018-07-23 11:33:29 +08:00
|
|
|
channels = devm_iio_channel_get_all(dev);
|
2016-09-08 22:28:35 +08:00
|
|
|
if (IS_ERR(channels)) {
|
|
|
|
if (PTR_ERR(channels) == -ENODEV)
|
|
|
|
return -EPROBE_DEFER;
|
2013-02-01 05:43:00 +08:00
|
|
|
return PTR_ERR(channels);
|
2016-09-08 22:28:35 +08:00
|
|
|
}
|
2012-02-16 03:48:03 +08:00
|
|
|
|
2013-02-01 05:42:00 +08:00
|
|
|
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
|
2018-07-23 11:33:29 +08:00
|
|
|
if (st == NULL)
|
|
|
|
return -ENOMEM;
|
2012-02-16 03:48:03 +08:00
|
|
|
|
2013-02-01 05:43:00 +08:00
|
|
|
st->channels = channels;
|
2012-02-16 03:48:03 +08:00
|
|
|
|
|
|
|
/* count how many attributes we have */
|
|
|
|
while (st->channels[st->num_channels].indio_dev)
|
|
|
|
st->num_channels++;
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
st->attrs = devm_kcalloc(dev,
|
|
|
|
st->num_channels + 1, sizeof(*st->attrs),
|
2013-02-01 05:42:00 +08:00
|
|
|
GFP_KERNEL);
|
2018-07-23 11:33:29 +08:00
|
|
|
if (st->attrs == NULL)
|
|
|
|
return -ENOMEM;
|
2013-02-01 05:42:00 +08:00
|
|
|
|
2012-02-16 03:48:03 +08:00
|
|
|
for (i = 0; i < st->num_channels; i++) {
|
2019-04-03 12:28:11 +08:00
|
|
|
const char *prefix;
|
|
|
|
int n;
|
|
|
|
|
2013-02-01 05:42:00 +08:00
|
|
|
a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
|
2018-07-23 11:33:29 +08:00
|
|
|
if (a == NULL)
|
|
|
|
return -ENOMEM;
|
2012-02-16 03:48:03 +08:00
|
|
|
|
|
|
|
sysfs_attr_init(&a->dev_attr.attr);
|
2012-05-02 04:04:24 +08:00
|
|
|
ret = iio_get_channel_type(&st->channels[i], &type);
|
2013-02-01 05:42:00 +08:00
|
|
|
if (ret < 0)
|
2018-07-23 11:33:29 +08:00
|
|
|
return ret;
|
2013-02-01 05:42:00 +08:00
|
|
|
|
2012-02-16 03:48:03 +08:00
|
|
|
switch (type) {
|
|
|
|
case IIO_VOLTAGE:
|
2019-04-03 12:28:11 +08:00
|
|
|
n = in_i++;
|
|
|
|
prefix = "in";
|
2012-02-16 03:48:03 +08:00
|
|
|
break;
|
|
|
|
case IIO_TEMP:
|
2019-04-03 12:28:11 +08:00
|
|
|
n = temp_i++;
|
|
|
|
prefix = "temp";
|
2012-02-16 03:48:03 +08:00
|
|
|
break;
|
|
|
|
case IIO_CURRENT:
|
2019-04-03 12:28:11 +08:00
|
|
|
n = curr_i++;
|
|
|
|
prefix = "curr";
|
2012-02-16 03:48:03 +08:00
|
|
|
break;
|
2019-08-22 22:22:24 +08:00
|
|
|
case IIO_POWER:
|
|
|
|
n = power_i++;
|
|
|
|
prefix = "power";
|
|
|
|
break;
|
2014-09-27 23:31:12 +08:00
|
|
|
case IIO_HUMIDITYRELATIVE:
|
2019-04-03 12:28:11 +08:00
|
|
|
n = humidity_i++;
|
|
|
|
prefix = "humidity";
|
2014-09-27 23:31:12 +08:00
|
|
|
break;
|
2012-02-16 03:48:03 +08:00
|
|
|
default:
|
2018-07-23 11:33:29 +08:00
|
|
|
return -EINVAL;
|
2012-02-16 03:48:03 +08:00
|
|
|
}
|
2019-04-03 12:28:11 +08:00
|
|
|
|
|
|
|
a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
|
|
|
|
"%s%d_input",
|
|
|
|
prefix, n);
|
2018-07-23 11:33:29 +08:00
|
|
|
if (a->dev_attr.attr.name == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-02-16 03:48:03 +08:00
|
|
|
a->dev_attr.show = iio_hwmon_read_val;
|
2018-12-11 06:02:09 +08:00
|
|
|
a->dev_attr.attr.mode = 0444;
|
2012-02-16 03:48:03 +08:00
|
|
|
a->index = i;
|
|
|
|
st->attrs[i] = &a->dev_attr.attr;
|
|
|
|
}
|
|
|
|
|
2013-12-02 11:33:16 +08:00
|
|
|
st->attr_group.attrs = st->attrs;
|
|
|
|
st->groups[0] = &st->attr_group;
|
2016-02-16 13:00:53 +08:00
|
|
|
|
2018-08-29 02:41:41 +08:00
|
|
|
if (dev->of_node) {
|
2018-08-28 09:52:21 +08:00
|
|
|
sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
|
2018-08-29 02:41:41 +08:00
|
|
|
if (!sname)
|
|
|
|
return -ENOMEM;
|
|
|
|
strreplace(sname, '-', '_');
|
|
|
|
} else {
|
|
|
|
sname = "iio_hwmon";
|
|
|
|
}
|
2016-02-16 13:00:53 +08:00
|
|
|
|
2018-07-23 11:33:29 +08:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
|
|
|
|
st->groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2012-02-16 03:48:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 16:29:02 +08:00
|
|
|
static const struct of_device_id iio_hwmon_of_match[] = {
|
2013-02-01 05:43:00 +08:00
|
|
|
{ .compatible = "iio-hwmon", },
|
|
|
|
{ }
|
|
|
|
};
|
2013-06-10 23:47:45 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
|
2013-02-01 05:43:00 +08:00
|
|
|
|
2012-02-16 03:48:03 +08:00
|
|
|
static struct platform_driver __refdata iio_hwmon_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "iio_hwmon",
|
2013-02-01 05:43:00 +08:00
|
|
|
.of_match_table = iio_hwmon_of_match,
|
2012-02-16 03:48:03 +08:00
|
|
|
},
|
|
|
|
.probe = iio_hwmon_probe,
|
|
|
|
};
|
|
|
|
|
2012-07-21 16:54:00 +08:00
|
|
|
module_platform_driver(iio_hwmon_driver);
|
2012-02-16 03:48:03 +08:00
|
|
|
|
2012-09-03 04:34:59 +08:00
|
|
|
MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
|
2012-02-16 03:48:03 +08:00
|
|
|
MODULE_DESCRIPTION("IIO to hwmon driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|