Skip to content

Commit 8cb3403

Browse files
andreamerelloJonathan Cameron
authored andcommitted
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>
1 parent 57a4274 commit 8cb3403

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎drivers/iio/industrialio-core.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
596596
}
597597
return l;
598598
}
599+
case IIO_VAL_CHAR:
600+
return snprintf(buf, len, "%c", (char)vals[0]);
599601
default:
600602
return 0;
601603
}
@@ -837,7 +839,8 @@ static ssize_t iio_write_channel_info(struct device *dev,
837839
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
838840
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
839841
int ret, fract_mult = 100000;
840-
int integer, fract;
842+
int integer, fract = 0;
843+
bool is_char = false;
841844

842845
/* Assumes decimal - precision based on number of digits */
843846
if (!indio_dev->info->write_raw)
@@ -855,13 +858,24 @@ static ssize_t iio_write_channel_info(struct device *dev,
855858
case IIO_VAL_INT_PLUS_NANO:
856859
fract_mult = 100000000;
857860
break;
861+
case IIO_VAL_CHAR:
862+
is_char = true;
863+
break;
858864
default:
859865
return -EINVAL;
860866
}
861867

862-
ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
863-
if (ret)
864-
return ret;
868+
if (is_char) {
869+
char ch;
870+
871+
if (sscanf(buf, "%c", &ch) != 1)
872+
return -EINVAL;
873+
integer = ch;
874+
} else {
875+
ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
876+
if (ret)
877+
return ret;
878+
}
865879

866880
ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
867881
integer, fract, this_attr->address);

‎include/linux/iio/types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum iio_event_info {
2525
#define IIO_VAL_INT_MULTIPLE 5
2626
#define IIO_VAL_FRACTIONAL 10
2727
#define IIO_VAL_FRACTIONAL_LOG2 11
28+
#define IIO_VAL_CHAR 12
2829

2930
enum iio_available_type {
3031
IIO_AVAIL_LIST,

0 commit comments

Comments
 (0)