Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (iio_hwmon) Enable power exporting from IIO

There is no reason why power channel shouldn't be exported as is done for
voltage, current, temperature and humidity.

Power channel is available on iio ina226 driver.

Sysfs IIO documentation for power attribute added by commit 7c6d5c7ee883
("iio: Documentation: Add missing documentation for power attribute")
is declaring that value is in mili-Watts but hwmon interface is expecting
value in micro-Watts that's why there is a need for mili-Watts to
micro-Watts conversion.

Tested on Xilinx ZCU102 board.

Signed-off-by: Michal Simek <michal.simek@xilinx.com>
Link: https://lore.kernel.org/r/db71f5ae87e4521a2856a1be5544de0b6cede575.1566483741.git.michal.simek@xilinx.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Michal Simek and committed by
Guenter Roeck
bc34301b be7ec919

+15 -3
+15 -3
drivers/hwmon/iio_hwmon.c
··· 44 44 int ret; 45 45 struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr); 46 46 struct iio_hwmon_state *state = dev_get_drvdata(dev); 47 + struct iio_channel *chan = &state->channels[sattr->index]; 48 + enum iio_chan_type type; 47 49 48 - ret = iio_read_channel_processed(&state->channels[sattr->index], 49 - &result); 50 + ret = iio_read_channel_processed(chan, &result); 50 51 if (ret < 0) 51 52 return ret; 53 + 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 */ 52 60 53 61 return sprintf(buf, "%d\n", result); 54 62 } ··· 67 59 struct iio_hwmon_state *st; 68 60 struct sensor_device_attribute *a; 69 61 int ret, i; 70 - int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1; 62 + int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1; 71 63 enum iio_chan_type type; 72 64 struct iio_channel *channels; 73 65 struct device *hwmon_dev; ··· 121 113 case IIO_CURRENT: 122 114 n = curr_i++; 123 115 prefix = "curr"; 116 + break; 117 + case IIO_POWER: 118 + n = power_i++; 119 + prefix = "power"; 124 120 break; 125 121 case IIO_HUMIDITYRELATIVE: 126 122 n = humidity_i++;