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

iio: core: add char type for sysfs attributes

This patch introduces IIO_VAL_CHAR type for standard IIO attributes to
allow for attributes that needs to be represented by character rather
than a number. This is preparatory for introducing a new attribute whose
purpose is to describe thermocouple type, that can be i.e. "J", "K", etc..

The char-type value is stored in the first "value" integer that is passed
to the .[read/write]_raw() callbacks.

Note that in order to make it possible for the IIO core to correctly parse
this type (actually, to avoid integer parsing), it became mandatory for
any driver that wish to use IIO_VAL_CHAR on a writable attribute to
implement .write_raw_get_fmt().

Cc: Hartmut Knaack <knaack.h@gmx.de>
Cc: Lars-Peter Clausen <lars@metafoo.de>
Cc: Peter Meerwald-Stadler <pmeerw@pmeerw.net>
Cc: Colin Ian King <colin.king@canonical.com>
Cc: Patrick Havelange <patrick.havelange@essensium.com>
Cc: Matt Weber <matthew.weber@rockwellcollins.com>
Cc: Matt Ranostay <matt.ranostay@konsulko.com>
Cc: Chuhong Yuan <hslester96@gmail.com>
Cc: Daniel Gomez <dagmcr@gmail.com>
Cc: linux-iio@vger.kernel.org
Signed-off-by: Andrea Merello <andrea.merello@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Andrea Merello and committed by
Jonathan Cameron
8cb34036 57a4274c

+19 -4
+18 -4
drivers/iio/industrialio-core.c
··· 596 596 } 597 597 return l; 598 598 } 599 + case IIO_VAL_CHAR: 600 + return snprintf(buf, len, "%c", (char)vals[0]); 599 601 default: 600 602 return 0; 601 603 } ··· 839 837 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 840 838 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 841 839 int ret, fract_mult = 100000; 842 - int integer, fract; 840 + int integer, fract = 0; 841 + bool is_char = false; 843 842 844 843 /* Assumes decimal - precision based on number of digits */ 845 844 if (!indio_dev->info->write_raw) ··· 858 855 case IIO_VAL_INT_PLUS_NANO: 859 856 fract_mult = 100000000; 860 857 break; 858 + case IIO_VAL_CHAR: 859 + is_char = true; 860 + break; 861 861 default: 862 862 return -EINVAL; 863 863 } 864 864 865 - ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 866 - if (ret) 867 - return ret; 865 + if (is_char) { 866 + char ch; 867 + 868 + if (sscanf(buf, "%c", &ch) != 1) 869 + return -EINVAL; 870 + integer = ch; 871 + } else { 872 + ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 873 + if (ret) 874 + return ret; 875 + } 868 876 869 877 ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 870 878 integer, fract, this_attr->address);
+1
include/linux/iio/types.h
··· 25 25 #define IIO_VAL_INT_MULTIPLE 5 26 26 #define IIO_VAL_FRACTIONAL 10 27 27 #define IIO_VAL_FRACTIONAL_LOG2 11 28 + #define IIO_VAL_CHAR 12 28 29 29 30 enum iio_available_type { 30 31 IIO_AVAIL_LIST,