0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/slab.h>
0009 #include <linux/module.h>
0010 #include <linux/err.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/of.h>
0014 #include <linux/hwmon-sysfs.h>
0015 #include <linux/iio/consumer.h>
0016 #include <linux/iio/types.h>
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 struct iio_hwmon_state {
0027 struct iio_channel *channels;
0028 int num_channels;
0029 struct attribute_group attr_group;
0030 const struct attribute_group *groups[2];
0031 struct attribute **attrs;
0032 };
0033
0034
0035
0036
0037
0038
0039 static ssize_t iio_hwmon_read_val(struct device *dev,
0040 struct device_attribute *attr,
0041 char *buf)
0042 {
0043 int result;
0044 int ret;
0045 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
0046 struct iio_hwmon_state *state = dev_get_drvdata(dev);
0047 struct iio_channel *chan = &state->channels[sattr->index];
0048 enum iio_chan_type type;
0049
0050 ret = iio_read_channel_processed(chan, &result);
0051 if (ret < 0)
0052 return ret;
0053
0054 ret = iio_get_channel_type(chan, &type);
0055 if (ret < 0)
0056 return ret;
0057
0058 if (type == IIO_POWER)
0059 result *= 1000;
0060
0061 return sprintf(buf, "%d\n", result);
0062 }
0063
0064 static int iio_hwmon_probe(struct platform_device *pdev)
0065 {
0066 struct device *dev = &pdev->dev;
0067 struct iio_hwmon_state *st;
0068 struct sensor_device_attribute *a;
0069 int ret, i;
0070 int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
0071 enum iio_chan_type type;
0072 struct iio_channel *channels;
0073 struct device *hwmon_dev;
0074 char *sname;
0075
0076 channels = devm_iio_channel_get_all(dev);
0077 if (IS_ERR(channels)) {
0078 if (PTR_ERR(channels) == -ENODEV)
0079 return -EPROBE_DEFER;
0080 return PTR_ERR(channels);
0081 }
0082
0083 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
0084 if (st == NULL)
0085 return -ENOMEM;
0086
0087 st->channels = channels;
0088
0089
0090 while (st->channels[st->num_channels].indio_dev)
0091 st->num_channels++;
0092
0093 st->attrs = devm_kcalloc(dev,
0094 st->num_channels + 1, sizeof(*st->attrs),
0095 GFP_KERNEL);
0096 if (st->attrs == NULL)
0097 return -ENOMEM;
0098
0099 for (i = 0; i < st->num_channels; i++) {
0100 const char *prefix;
0101 int n;
0102
0103 a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
0104 if (a == NULL)
0105 return -ENOMEM;
0106
0107 sysfs_attr_init(&a->dev_attr.attr);
0108 ret = iio_get_channel_type(&st->channels[i], &type);
0109 if (ret < 0)
0110 return ret;
0111
0112 switch (type) {
0113 case IIO_VOLTAGE:
0114 n = in_i++;
0115 prefix = "in";
0116 break;
0117 case IIO_TEMP:
0118 n = temp_i++;
0119 prefix = "temp";
0120 break;
0121 case IIO_CURRENT:
0122 n = curr_i++;
0123 prefix = "curr";
0124 break;
0125 case IIO_POWER:
0126 n = power_i++;
0127 prefix = "power";
0128 break;
0129 case IIO_HUMIDITYRELATIVE:
0130 n = humidity_i++;
0131 prefix = "humidity";
0132 break;
0133 default:
0134 return -EINVAL;
0135 }
0136
0137 a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
0138 "%s%d_input",
0139 prefix, n);
0140 if (a->dev_attr.attr.name == NULL)
0141 return -ENOMEM;
0142
0143 a->dev_attr.show = iio_hwmon_read_val;
0144 a->dev_attr.attr.mode = 0444;
0145 a->index = i;
0146 st->attrs[i] = &a->dev_attr.attr;
0147 }
0148
0149 st->attr_group.attrs = st->attrs;
0150 st->groups[0] = &st->attr_group;
0151
0152 if (dev->of_node) {
0153 sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
0154 if (!sname)
0155 return -ENOMEM;
0156 strreplace(sname, '-', '_');
0157 } else {
0158 sname = "iio_hwmon";
0159 }
0160
0161 hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
0162 st->groups);
0163 return PTR_ERR_OR_ZERO(hwmon_dev);
0164 }
0165
0166 static const struct of_device_id iio_hwmon_of_match[] = {
0167 { .compatible = "iio-hwmon", },
0168 { }
0169 };
0170 MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
0171
0172 static struct platform_driver iio_hwmon_driver = {
0173 .driver = {
0174 .name = "iio_hwmon",
0175 .of_match_table = iio_hwmon_of_match,
0176 },
0177 .probe = iio_hwmon_probe,
0178 };
0179
0180 module_platform_driver(iio_hwmon_driver);
0181
0182 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
0183 MODULE_DESCRIPTION("IIO to hwmon driver");
0184 MODULE_LICENSE("GPL v2");