2012-02-16 02:48:03 +07:00
|
|
|
/* Hwmon client for industrial I/O devices
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Jonathan Cameron
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
* the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 22:52:00 +07:00
|
|
|
#include <linux/of.h>
|
2012-02-16 02:48:03 +07:00
|
|
|
#include <linux/hwmon-sysfs.h>
|
2012-04-25 21:54:58 +07:00
|
|
|
#include <linux/iio/consumer.h>
|
|
|
|
#include <linux/iio/types.h>
|
2012-02-16 02:48:03 +07: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 06:22:26 +07:00
|
|
|
* @attr_group: the group of attributes
|
|
|
|
* @groups: null terminated array of attribute groups
|
2012-02-16 02:48:03 +07: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 10:33:16 +07:00
|
|
|
const struct attribute_group *groups[2];
|
2012-02-16 02:48:03 +07: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 19:17:00 +07:00
|
|
|
int result;
|
|
|
|
int ret;
|
2012-02-16 02:48:03 +07:00
|
|
|
struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
|
|
|
|
struct iio_hwmon_state *state = dev_get_drvdata(dev);
|
|
|
|
|
2012-09-17 19:17:00 +07:00
|
|
|
ret = iio_read_channel_processed(&state->channels[sattr->index],
|
|
|
|
&result);
|
2012-02-16 02:48:03 +07:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-09-17 19:17:00 +07:00
|
|
|
return sprintf(buf, "%d\n", result);
|
2012-02-16 02:48:03 +07:00
|
|
|
}
|
|
|
|
|
2012-11-20 01:21:57 +07:00
|
|
|
static int iio_hwmon_probe(struct platform_device *pdev)
|
2012-02-16 02:48:03 +07:00
|
|
|
{
|
2013-02-01 04:42:00 +07:00
|
|
|
struct device *dev = &pdev->dev;
|
2012-02-16 02:48:03 +07:00
|
|
|
struct iio_hwmon_state *st;
|
|
|
|
struct sensor_device_attribute *a;
|
|
|
|
int ret, i;
|
2014-09-27 22:31:12 +07:00
|
|
|
int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1;
|
2012-02-16 02:48:03 +07:00
|
|
|
enum iio_chan_type type;
|
2013-02-01 04:43:00 +07:00
|
|
|
struct iio_channel *channels;
|
2018-07-23 10:33:29 +07:00
|
|
|
struct device *hwmon_dev;
|
2016-02-16 12:00:53 +07:00
|
|
|
char *sname;
|
2013-12-02 10:33:16 +07:00
|
|
|
|
2018-07-23 10:33:29 +07:00
|
|
|
channels = devm_iio_channel_get_all(dev);
|
2016-09-08 21:28:35 +07:00
|
|
|
if (IS_ERR(channels)) {
|
|
|
|
if (PTR_ERR(channels) == -ENODEV)
|
|
|
|
return -EPROBE_DEFER;
|
2013-02-01 04:43:00 +07:00
|
|
|
return PTR_ERR(channels);
|
2016-09-08 21:28:35 +07:00
|
|
|
}
|
2012-02-16 02:48:03 +07:00
|
|
|
|
2013-02-01 04:42:00 +07:00
|
|
|
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
|
2018-07-23 10:33:29 +07:00
|
|
|
if (st == NULL)
|
|
|
|
return -ENOMEM;
|
2012-02-16 02:48:03 +07:00
|
|
|
|
2013-02-01 04:43:00 +07:00
|
|
|
st->channels = channels;
|
2012-02-16 02:48:03 +07: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 04:07:58 +07:00
|
|
|
st->attrs = devm_kcalloc(dev,
|
|
|
|
st->num_channels + 1, sizeof(*st->attrs),
|
2013-02-01 04:42:00 +07:00
|
|
|
GFP_KERNEL);
|
2018-07-23 10:33:29 +07:00
|
|
|
if (st->attrs == NULL)
|
|
|
|
return -ENOMEM;
|
2013-02-01 04:42:00 +07:00
|
|
|
|
2012-02-16 02:48:03 +07:00
|
|
|
for (i = 0; i < st->num_channels; i++) {
|
2013-02-01 04:42:00 +07:00
|
|
|
a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
|
2018-07-23 10:33:29 +07:00
|
|
|
if (a == NULL)
|
|
|
|
return -ENOMEM;
|
2012-02-16 02:48:03 +07:00
|
|
|
|
|
|
|
sysfs_attr_init(&a->dev_attr.attr);
|
2012-05-02 03:04:24 +07:00
|
|
|
ret = iio_get_channel_type(&st->channels[i], &type);
|
2013-02-01 04:42:00 +07:00
|
|
|
if (ret < 0)
|
2018-07-23 10:33:29 +07:00
|
|
|
return ret;
|
2013-02-01 04:42:00 +07:00
|
|
|
|
2012-02-16 02:48:03 +07:00
|
|
|
switch (type) {
|
|
|
|
case IIO_VOLTAGE:
|
2016-07-26 14:47:09 +07:00
|
|
|
a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
|
|
|
|
"in%d_input",
|
|
|
|
in_i++);
|
2012-02-16 02:48:03 +07:00
|
|
|
break;
|
|
|
|
case IIO_TEMP:
|
2016-07-26 14:47:09 +07:00
|
|
|
a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
|
|
|
|
"temp%d_input",
|
|
|
|
temp_i++);
|
2012-02-16 02:48:03 +07:00
|
|
|
break;
|
|
|
|
case IIO_CURRENT:
|
2016-07-26 14:47:09 +07:00
|
|
|
a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
|
|
|
|
"curr%d_input",
|
|
|
|
curr_i++);
|
2012-02-16 02:48:03 +07:00
|
|
|
break;
|
2014-09-27 22:31:12 +07:00
|
|
|
case IIO_HUMIDITYRELATIVE:
|
2016-07-26 14:47:09 +07:00
|
|
|
a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
|
|
|
|
"humidity%d_input",
|
|
|
|
humidity_i++);
|
2014-09-27 22:31:12 +07:00
|
|
|
break;
|
2012-02-16 02:48:03 +07:00
|
|
|
default:
|
2018-07-23 10:33:29 +07:00
|
|
|
return -EINVAL;
|
2012-02-16 02:48:03 +07:00
|
|
|
}
|
2018-07-23 10:33:29 +07:00
|
|
|
if (a->dev_attr.attr.name == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-02-16 02:48:03 +07:00
|
|
|
a->dev_attr.show = iio_hwmon_read_val;
|
|
|
|
a->dev_attr.attr.mode = S_IRUGO;
|
|
|
|
a->index = i;
|
|
|
|
st->attrs[i] = &a->dev_attr.attr;
|
|
|
|
}
|
|
|
|
|
2013-12-02 10:33:16 +07:00
|
|
|
st->attr_group.attrs = st->attrs;
|
|
|
|
st->groups[0] = &st->attr_group;
|
2016-02-16 12:00:53 +07:00
|
|
|
|
2018-08-29 01:41:41 +07:00
|
|
|
if (dev->of_node) {
|
2018-08-28 08:52:21 +07:00
|
|
|
sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
|
2018-08-29 01:41:41 +07:00
|
|
|
if (!sname)
|
|
|
|
return -ENOMEM;
|
|
|
|
strreplace(sname, '-', '_');
|
|
|
|
} else {
|
|
|
|
sname = "iio_hwmon";
|
|
|
|
}
|
2016-02-16 12:00:53 +07:00
|
|
|
|
2018-07-23 10:33:29 +07:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
|
|
|
|
st->groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2012-02-16 02:48:03 +07:00
|
|
|
}
|
|
|
|
|
2014-05-07 15:29:02 +07:00
|
|
|
static const struct of_device_id iio_hwmon_of_match[] = {
|
2013-02-01 04:43:00 +07:00
|
|
|
{ .compatible = "iio-hwmon", },
|
|
|
|
{ }
|
|
|
|
};
|
2013-06-10 22:47:45 +07:00
|
|
|
MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
|
2013-02-01 04:43:00 +07:00
|
|
|
|
2012-02-16 02:48:03 +07:00
|
|
|
static struct platform_driver __refdata iio_hwmon_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "iio_hwmon",
|
2013-02-01 04:43:00 +07:00
|
|
|
.of_match_table = iio_hwmon_of_match,
|
2012-02-16 02:48:03 +07:00
|
|
|
},
|
|
|
|
.probe = iio_hwmon_probe,
|
|
|
|
};
|
|
|
|
|
2012-07-21 15:54:00 +07:00
|
|
|
module_platform_driver(iio_hwmon_driver);
|
2012-02-16 02:48:03 +07:00
|
|
|
|
2012-09-03 03:34:59 +07:00
|
|
|
MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
|
2012-02-16 02:48:03 +07:00
|
|
|
MODULE_DESCRIPTION("IIO to hwmon driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|