Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 2 | /* Hwmon client for industrial I/O devices |
| 3 | * |
| 4 | * Copyright (c) 2011 Jonathan Cameron |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 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> |
Guenter Roeck | 3465a22 | 2013-03-20 15:52:00 +0000 | [diff] [blame] | 13 | #include <linux/of.h> |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 14 | #include <linux/hwmon-sysfs.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/consumer.h> |
| 16 | #include <linux/iio/types.h> |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 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) |
Guenter Roeck | 7f6d70c | 2017-12-03 15:22:26 -0800 | [diff] [blame] | 22 | * @attr_group: the group of attributes |
| 23 | * @groups: null terminated array of attribute groups |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 24 | * @attrs: null terminated array of attribute pointers. |
| 25 | */ |
| 26 | struct iio_hwmon_state { |
| 27 | struct iio_channel *channels; |
| 28 | int num_channels; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 29 | struct attribute_group attr_group; |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 30 | const struct attribute_group *groups[2]; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 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 | */ |
| 39 | static ssize_t iio_hwmon_read_val(struct device *dev, |
| 40 | struct device_attribute *attr, |
| 41 | char *buf) |
| 42 | { |
Lars-Peter Clausen | a0e545e | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 43 | int result; |
| 44 | int ret; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 45 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 46 | struct iio_hwmon_state *state = dev_get_drvdata(dev); |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 47 | struct iio_channel *chan = &state->channels[sattr->index]; |
| 48 | enum iio_chan_type type; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 49 | |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 50 | ret = iio_read_channel_processed(chan, &result); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 51 | if (ret < 0) |
| 52 | return ret; |
| 53 | |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 54 | ret = iio_get_channel_type(chan, &type); |
| 55 | if (ret < 0) |
| 56 | return ret; |
| 57 | |
| 58 | if (type == IIO_POWER) |
| 59 | result *= 1000; /* mili-Watts to micro-Watts conversion */ |
| 60 | |
Lars-Peter Clausen | a0e545e | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 61 | return sprintf(buf, "%d\n", result); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 64 | static int iio_hwmon_probe(struct platform_device *pdev) |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 65 | { |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 66 | struct device *dev = &pdev->dev; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 67 | struct iio_hwmon_state *st; |
| 68 | struct sensor_device_attribute *a; |
| 69 | int ret, i; |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 70 | int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 71 | enum iio_chan_type type; |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 72 | struct iio_channel *channels; |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 73 | struct device *hwmon_dev; |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 74 | char *sname; |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 75 | |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 76 | channels = devm_iio_channel_get_all(dev); |
Quentin Schulz | 9417fef | 2016-09-08 16:28:35 +0200 | [diff] [blame] | 77 | if (IS_ERR(channels)) { |
| 78 | if (PTR_ERR(channels) == -ENODEV) |
| 79 | return -EPROBE_DEFER; |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 80 | return PTR_ERR(channels); |
Quentin Schulz | 9417fef | 2016-09-08 16:28:35 +0200 | [diff] [blame] | 81 | } |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 82 | |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 83 | st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 84 | if (st == NULL) |
| 85 | return -ENOMEM; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 86 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 87 | st->channels = channels; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 88 | |
| 89 | /* count how many attributes we have */ |
| 90 | while (st->channels[st->num_channels].indio_dev) |
| 91 | st->num_channels++; |
| 92 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 93 | st->attrs = devm_kcalloc(dev, |
| 94 | st->num_channels + 1, sizeof(*st->attrs), |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 95 | GFP_KERNEL); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 96 | if (st->attrs == NULL) |
| 97 | return -ENOMEM; |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 98 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 99 | for (i = 0; i < st->num_channels; i++) { |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 100 | const char *prefix; |
| 101 | int n; |
| 102 | |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 103 | a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 104 | if (a == NULL) |
| 105 | return -ENOMEM; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 106 | |
| 107 | sysfs_attr_init(&a->dev_attr.attr); |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 108 | ret = iio_get_channel_type(&st->channels[i], &type); |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 109 | if (ret < 0) |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 110 | return ret; |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 111 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 112 | switch (type) { |
| 113 | case IIO_VOLTAGE: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 114 | n = in_i++; |
| 115 | prefix = "in"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 116 | break; |
| 117 | case IIO_TEMP: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 118 | n = temp_i++; |
| 119 | prefix = "temp"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 120 | break; |
| 121 | case IIO_CURRENT: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 122 | n = curr_i++; |
| 123 | prefix = "curr"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 124 | break; |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 125 | case IIO_POWER: |
| 126 | n = power_i++; |
| 127 | prefix = "power"; |
| 128 | break; |
Guenter Roeck | 61bb53b | 2014-09-27 08:31:12 -0700 | [diff] [blame] | 129 | case IIO_HUMIDITYRELATIVE: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 130 | n = humidity_i++; |
| 131 | prefix = "humidity"; |
Guenter Roeck | 61bb53b | 2014-09-27 08:31:12 -0700 | [diff] [blame] | 132 | break; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 133 | default: |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 134 | return -EINVAL; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 135 | } |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 136 | |
| 137 | a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, |
| 138 | "%s%d_input", |
| 139 | prefix, n); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 140 | if (a->dev_attr.attr.name == NULL) |
| 141 | return -ENOMEM; |
| 142 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 143 | a->dev_attr.show = iio_hwmon_read_val; |
Guenter Roeck | 389bc38 | 2018-12-10 14:02:09 -0800 | [diff] [blame] | 144 | a->dev_attr.attr.mode = 0444; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 145 | a->index = i; |
| 146 | st->attrs[i] = &a->dev_attr.attr; |
| 147 | } |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 148 | |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 149 | st->attr_group.attrs = st->attrs; |
| 150 | st->groups[0] = &st->attr_group; |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 151 | |
Guenter Roeck | 86103cf | 2018-08-28 11:41:41 -0700 | [diff] [blame] | 152 | if (dev->of_node) { |
Rob Herring | 0debe4d | 2018-08-27 20:52:21 -0500 | [diff] [blame] | 153 | sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node); |
Guenter Roeck | 86103cf | 2018-08-28 11:41:41 -0700 | [diff] [blame] | 154 | if (!sname) |
| 155 | return -ENOMEM; |
| 156 | strreplace(sname, '-', '_'); |
| 157 | } else { |
| 158 | sname = "iio_hwmon"; |
| 159 | } |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 160 | |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 161 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st, |
| 162 | st->groups); |
| 163 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jingoo Han | cfe03d6 | 2014-05-07 17:29:02 +0900 | [diff] [blame] | 166 | static const struct of_device_id iio_hwmon_of_match[] = { |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 167 | { .compatible = "iio-hwmon", }, |
| 168 | { } |
| 169 | }; |
Sebastian Andrzej Siewior | 2ec2819 | 2013-06-10 17:47:45 +0200 | [diff] [blame] | 170 | MODULE_DEVICE_TABLE(of, iio_hwmon_of_match); |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 171 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 172 | static struct platform_driver __refdata iio_hwmon_driver = { |
| 173 | .driver = { |
| 174 | .name = "iio_hwmon", |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 175 | .of_match_table = iio_hwmon_of_match, |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 176 | }, |
| 177 | .probe = iio_hwmon_probe, |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Devendra Naga | d16f6db | 2012-07-21 09:54:00 +0100 | [diff] [blame] | 180 | module_platform_driver(iio_hwmon_driver); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 181 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 182 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 183 | MODULE_DESCRIPTION("IIO to hwmon driver"); |
| 184 | MODULE_LICENSE("GPL v2"); |