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> |
Andy Shevchenko | b7b568c | 2022-08-26 20:37:00 +0300 | [diff] [blame] | 9 | #include <linux/mod_devicetable.h> |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/platform_device.h> |
Andy Shevchenko | b7b568c | 2022-08-26 20:37:00 +0300 | [diff] [blame] | 13 | #include <linux/property.h> |
| 14 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 15 | #include <linux/hwmon.h> |
| 16 | #include <linux/hwmon-sysfs.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 17 | #include <linux/iio/consumer.h> |
| 18 | #include <linux/iio/types.h> |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * struct iio_hwmon_state - device instance state |
| 22 | * @channels: filled with array of channels from iio |
| 23 | * @num_channels: number of channels in channels (saves counting twice) |
Guenter Roeck | 7f6d70c | 2017-12-03 15:22:26 -0800 | [diff] [blame] | 24 | * @attr_group: the group of attributes |
| 25 | * @groups: null terminated array of attribute groups |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 26 | * @attrs: null terminated array of attribute pointers. |
| 27 | */ |
| 28 | struct iio_hwmon_state { |
| 29 | struct iio_channel *channels; |
| 30 | int num_channels; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 31 | struct attribute_group attr_group; |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 32 | const struct attribute_group *groups[2]; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 33 | struct attribute **attrs; |
| 34 | }; |
| 35 | |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 36 | static ssize_t iio_hwmon_read_label(struct device *dev, |
| 37 | struct device_attribute *attr, |
| 38 | char *buf) |
| 39 | { |
| 40 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 41 | struct iio_hwmon_state *state = dev_get_drvdata(dev); |
| 42 | struct iio_channel *chan = &state->channels[sattr->index]; |
| 43 | |
| 44 | return iio_read_channel_label(chan, buf); |
| 45 | } |
| 46 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Assumes that IIO and hwmon operate in the same base units. |
| 49 | * This is supposed to be true, but needs verification for |
| 50 | * new channel types. |
| 51 | */ |
| 52 | static ssize_t iio_hwmon_read_val(struct device *dev, |
| 53 | struct device_attribute *attr, |
| 54 | char *buf) |
| 55 | { |
Lars-Peter Clausen | a0e545e | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 56 | int result; |
| 57 | int ret; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 58 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); |
| 59 | struct iio_hwmon_state *state = dev_get_drvdata(dev); |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 60 | struct iio_channel *chan = &state->channels[sattr->index]; |
| 61 | enum iio_chan_type type; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 62 | |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 63 | ret = iio_get_channel_type(chan, &type); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
| 67 | if (type == IIO_POWER) |
Sean Anderson | 52c1e81 | 2024-06-20 17:20:05 -0400 | [diff] [blame] | 68 | /* mili-Watts to micro-Watts conversion */ |
| 69 | ret = iio_read_channel_processed_scale(chan, &result, 1000); |
| 70 | else |
| 71 | ret = iio_read_channel_processed(chan, &result); |
| 72 | if (ret < 0) |
| 73 | return ret; |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 74 | |
Lars-Peter Clausen | a0e545e | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 75 | return sprintf(buf, "%d\n", result); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 78 | static int iio_hwmon_probe(struct platform_device *pdev) |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 79 | { |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 80 | struct device *dev = &pdev->dev; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 81 | struct iio_hwmon_state *st; |
| 82 | struct sensor_device_attribute *a; |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 83 | int ret, i, attr = 0; |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 84 | 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] | 85 | enum iio_chan_type type; |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 86 | struct iio_channel *channels; |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 87 | struct device *hwmon_dev; |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 88 | char *sname; |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 89 | void *buf; |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 90 | |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 91 | channels = devm_iio_channel_get_all(dev); |
Quentin Schulz | 9417fef | 2016-09-08 16:28:35 +0200 | [diff] [blame] | 92 | if (IS_ERR(channels)) { |
Alexander Stein | 1c999af | 2023-01-31 11:33:59 +0100 | [diff] [blame] | 93 | ret = PTR_ERR(channels); |
| 94 | if (ret == -ENODEV) |
| 95 | ret = -EPROBE_DEFER; |
| 96 | return dev_err_probe(dev, ret, |
| 97 | "Failed to get channels\n"); |
Quentin Schulz | 9417fef | 2016-09-08 16:28:35 +0200 | [diff] [blame] | 98 | } |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 99 | |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 100 | st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 101 | buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); |
| 102 | if (!st || !buf) |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 103 | return -ENOMEM; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 104 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 105 | st->channels = channels; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 106 | |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 107 | /* count how many channels we have */ |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 108 | while (st->channels[st->num_channels].indio_dev) |
| 109 | st->num_channels++; |
| 110 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 111 | st->attrs = devm_kcalloc(dev, |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 112 | 2 * st->num_channels + 1, sizeof(*st->attrs), |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 113 | GFP_KERNEL); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 114 | if (st->attrs == NULL) |
| 115 | return -ENOMEM; |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 116 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 117 | for (i = 0; i < st->num_channels; i++) { |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 118 | const char *prefix; |
| 119 | int n; |
| 120 | |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 121 | a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 122 | if (a == NULL) |
| 123 | return -ENOMEM; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 124 | |
| 125 | sysfs_attr_init(&a->dev_attr.attr); |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 126 | ret = iio_get_channel_type(&st->channels[i], &type); |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 127 | if (ret < 0) |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 128 | return ret; |
Guenter Roeck | c4ac7b9 | 2013-01-31 21:42:00 +0000 | [diff] [blame] | 129 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 130 | switch (type) { |
| 131 | case IIO_VOLTAGE: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 132 | n = in_i++; |
| 133 | prefix = "in"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 134 | break; |
| 135 | case IIO_TEMP: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 136 | n = temp_i++; |
| 137 | prefix = "temp"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 138 | break; |
| 139 | case IIO_CURRENT: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 140 | n = curr_i++; |
| 141 | prefix = "curr"; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 142 | break; |
Michal Simek | bc34301 | 2019-08-22 16:22:24 +0200 | [diff] [blame] | 143 | case IIO_POWER: |
| 144 | n = power_i++; |
| 145 | prefix = "power"; |
| 146 | break; |
Guenter Roeck | 61bb53b | 2014-09-27 08:31:12 -0700 | [diff] [blame] | 147 | case IIO_HUMIDITYRELATIVE: |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 148 | n = humidity_i++; |
| 149 | prefix = "humidity"; |
Guenter Roeck | 61bb53b | 2014-09-27 08:31:12 -0700 | [diff] [blame] | 150 | break; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 151 | default: |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 152 | return -EINVAL; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 153 | } |
Andrey Smirnov | cb202bb | 2019-04-02 21:28:11 -0700 | [diff] [blame] | 154 | |
| 155 | a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, |
| 156 | "%s%d_input", |
| 157 | prefix, n); |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 158 | if (a->dev_attr.attr.name == NULL) |
| 159 | return -ENOMEM; |
| 160 | |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 161 | a->dev_attr.show = iio_hwmon_read_val; |
Guenter Roeck | 389bc38 | 2018-12-10 14:02:09 -0800 | [diff] [blame] | 162 | a->dev_attr.attr.mode = 0444; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 163 | a->index = i; |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 164 | st->attrs[attr++] = &a->dev_attr.attr; |
| 165 | |
| 166 | /* Let's see if we have a label... */ |
| 167 | if (iio_read_channel_label(&st->channels[i], buf) < 0) |
| 168 | continue; |
| 169 | |
| 170 | a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL); |
| 171 | if (a == NULL) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | sysfs_attr_init(&a->dev_attr.attr); |
| 175 | a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, |
| 176 | "%s%d_label", |
| 177 | prefix, n); |
| 178 | if (!a->dev_attr.attr.name) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | a->dev_attr.show = iio_hwmon_read_label; |
| 182 | a->dev_attr.attr.mode = 0444; |
| 183 | a->index = i; |
| 184 | st->attrs[attr++] = &a->dev_attr.attr; |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 185 | } |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 186 | |
Sean Anderson | 440db40 | 2024-06-24 13:46:01 -0400 | [diff] [blame] | 187 | devm_free_pages(dev, (unsigned long)buf); |
| 188 | |
Guenter Roeck | 4b49cca | 2013-12-01 19:33:16 -0800 | [diff] [blame] | 189 | st->attr_group.attrs = st->attrs; |
| 190 | st->groups[0] = &st->attr_group; |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 191 | |
Andy Shevchenko | b7b568c | 2022-08-26 20:37:00 +0300 | [diff] [blame] | 192 | if (dev_fwnode(dev)) { |
| 193 | sname = devm_kasprintf(dev, GFP_KERNEL, "%pfwP", dev_fwnode(dev)); |
Guenter Roeck | 86103cf | 2018-08-28 11:41:41 -0700 | [diff] [blame] | 194 | if (!sname) |
| 195 | return -ENOMEM; |
| 196 | strreplace(sname, '-', '_'); |
| 197 | } else { |
| 198 | sname = "iio_hwmon"; |
| 199 | } |
Sanchayan Maity | b92fe9e | 2016-02-16 10:30:53 +0530 | [diff] [blame] | 200 | |
Maxime Roussin-Bélanger | 12005ec | 2018-07-22 23:33:29 -0400 | [diff] [blame] | 201 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st, |
| 202 | st->groups); |
| 203 | return PTR_ERR_OR_ZERO(hwmon_dev); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Jingoo Han | cfe03d6 | 2014-05-07 17:29:02 +0900 | [diff] [blame] | 206 | static const struct of_device_id iio_hwmon_of_match[] = { |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 207 | { .compatible = "iio-hwmon", }, |
| 208 | { } |
| 209 | }; |
Sebastian Andrzej Siewior | 2ec2819 | 2013-06-10 17:47:45 +0200 | [diff] [blame] | 210 | MODULE_DEVICE_TABLE(of, iio_hwmon_of_match); |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 211 | |
Geert Uytterhoeven | 561e312 | 2020-12-11 14:35:12 +0100 | [diff] [blame] | 212 | static struct platform_driver iio_hwmon_driver = { |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 213 | .driver = { |
| 214 | .name = "iio_hwmon", |
Guenter Roeck | a11e619 | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 215 | .of_match_table = iio_hwmon_of_match, |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 216 | }, |
| 217 | .probe = iio_hwmon_probe, |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Devendra Naga | d16f6db | 2012-07-21 09:54:00 +0100 | [diff] [blame] | 220 | module_platform_driver(iio_hwmon_driver); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 221 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 222 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | e0f8a24 | 2012-02-15 19:48:03 +0000 | [diff] [blame] | 223 | MODULE_DESCRIPTION("IIO to hwmon driver"); |
| 224 | MODULE_LICENSE("GPL v2"); |