Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Hwmon client for industrial I/O devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2011 Jonathan Cameron
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/iio/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * struct iio_hwmon_state - device instance state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @channels:		filled with array of channels from iio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @num_channels:	number of channels in channels (saves counting twice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @attr_group:		the group of attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @groups:		null terminated array of attribute groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @attrs:		null terminated array of attribute pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct iio_hwmon_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct iio_channel *channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct attribute_group attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	const struct attribute_group *groups[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct attribute **attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * Assumes that IIO and hwmon operate in the same base units.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * This is supposed to be true, but needs verification for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * new channel types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static ssize_t iio_hwmon_read_val(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				  char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct iio_hwmon_state *state = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct iio_channel *chan = &state->channels[sattr->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	enum iio_chan_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	ret = iio_read_channel_processed(chan, &result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ret = iio_get_channel_type(chan, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (type == IIO_POWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		result *= 1000; /* mili-Watts to micro-Watts conversion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return sprintf(buf, "%d\n", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int iio_hwmon_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct iio_hwmon_state *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct sensor_device_attribute *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int in_i = 1, temp_i = 1, curr_i = 1, humidity_i = 1, power_i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	enum iio_chan_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct iio_channel *channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	char *sname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	channels = devm_iio_channel_get_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (IS_ERR(channels)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		if (PTR_ERR(channels) == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return PTR_ERR(channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (st == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	st->channels = channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* count how many attributes we have */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	while (st->channels[st->num_channels].indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		st->num_channels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	st->attrs = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				 st->num_channels + 1, sizeof(*st->attrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (st->attrs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	for (i = 0; i < st->num_channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		const char *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (a == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		sysfs_attr_init(&a->dev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		ret = iio_get_channel_type(&st->channels[i], &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		case IIO_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			n = in_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			prefix = "in";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			n = temp_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			prefix = "temp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		case IIO_CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			n = curr_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			prefix = "curr";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		case IIO_POWER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			n = power_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			prefix = "power";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		case IIO_HUMIDITYRELATIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			n = humidity_i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			prefix = "humidity";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		a->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 						       "%s%d_input",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 						       prefix, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (a->dev_attr.attr.name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		a->dev_attr.show = iio_hwmon_read_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		a->dev_attr.attr.mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		a->index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		st->attrs[i] = &a->dev_attr.attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	st->attr_group.attrs = st->attrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	st->groups[0] = &st->attr_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (dev->of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		sname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (!sname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		strreplace(sname, '-', '_');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		sname = "iio_hwmon";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, sname, st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 							   st->groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static const struct of_device_id iio_hwmon_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	{ .compatible = "iio-hwmon", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_DEVICE_TABLE(of, iio_hwmon_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static struct platform_driver __refdata iio_hwmon_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		.name = "iio_hwmon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		.of_match_table = iio_hwmon_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.probe = iio_hwmon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) module_platform_driver(iio_hwmon_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) MODULE_DESCRIPTION("IIO to hwmon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) MODULE_LICENSE("GPL v2");