diff mbox series

[v3,4/9] RFC: iio: core: add char type for sysfs attributes

Message ID 20191120144756.28424-5-andrea.merello@gmail.com (mailing list archive)
State New, archived
Headers show
Series iio: max31856: provide more configuration options | expand

Commit Message

Andrea Merello Nov. 20, 2019, 2:47 p.m. UTC
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>
---
 drivers/iio/industrialio-core.c | 22 ++++++++++++++++++----
 include/linux/iio/types.h       |  1 +
 2 files changed, 19 insertions(+), 4 deletions(-)

Comments

Jonathan Cameron Nov. 23, 2019, 12:33 p.m. UTC | #1
On Wed, 20 Nov 2019 15:47:51 +0100
Andrea Merello <andrea.merello@gmail.com> wrote:

> 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>
applied.  I dropped the RFC part though given it's going in ;)

Thanks,

Jonathan

> ---
>  drivers/iio/industrialio-core.c | 22 ++++++++++++++++++----
>  include/linux/iio/types.h       |  1 +
>  2 files changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index f72c2dc5f703..958b5c48a86f 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -596,6 +596,8 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
>  		}
>  		return l;
>  	}
> +	case IIO_VAL_CHAR:
> +		return snprintf(buf, len, "%c", (char)vals[0]);
>  	default:
>  		return 0;
>  	}
> @@ -837,7 +839,8 @@ static ssize_t iio_write_channel_info(struct device *dev,
>  	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
>  	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
>  	int ret, fract_mult = 100000;
> -	int integer, fract;
> +	int integer, fract = 0;
> +	bool is_char = false;
>  
>  	/* Assumes decimal - precision based on number of digits */
>  	if (!indio_dev->info->write_raw)
> @@ -855,13 +858,24 @@ static ssize_t iio_write_channel_info(struct device *dev,
>  		case IIO_VAL_INT_PLUS_NANO:
>  			fract_mult = 100000000;
>  			break;
> +		case IIO_VAL_CHAR:
> +			is_char = true;
> +			break;
>  		default:
>  			return -EINVAL;
>  		}
>  
> -	ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
> -	if (ret)
> -		return ret;
> +	if (is_char) {
> +		char ch;
> +
> +		if (sscanf(buf, "%c", &ch) != 1)
> +			return -EINVAL;
> +		integer = ch;
> +	} else {
> +		ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
> +		if (ret)
> +			return ret;
> +	}
>  
>  	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
>  					 integer, fract, this_attr->address);
> diff --git a/include/linux/iio/types.h b/include/linux/iio/types.h
> index fa824e160f35..8e0026da38c9 100644
> --- a/include/linux/iio/types.h
> +++ b/include/linux/iio/types.h
> @@ -25,6 +25,7 @@ enum iio_event_info {
>  #define IIO_VAL_INT_MULTIPLE 5
>  #define IIO_VAL_FRACTIONAL 10
>  #define IIO_VAL_FRACTIONAL_LOG2 11
> +#define IIO_VAL_CHAR 12
>  
>  enum iio_available_type {
>  	IIO_AVAIL_LIST,
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index f72c2dc5f703..958b5c48a86f 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -596,6 +596,8 @@  static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
 		}
 		return l;
 	}
+	case IIO_VAL_CHAR:
+		return snprintf(buf, len, "%c", (char)vals[0]);
 	default:
 		return 0;
 	}
@@ -837,7 +839,8 @@  static ssize_t iio_write_channel_info(struct device *dev,
 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 	int ret, fract_mult = 100000;
-	int integer, fract;
+	int integer, fract = 0;
+	bool is_char = false;
 
 	/* Assumes decimal - precision based on number of digits */
 	if (!indio_dev->info->write_raw)
@@ -855,13 +858,24 @@  static ssize_t iio_write_channel_info(struct device *dev,
 		case IIO_VAL_INT_PLUS_NANO:
 			fract_mult = 100000000;
 			break;
+		case IIO_VAL_CHAR:
+			is_char = true;
+			break;
 		default:
 			return -EINVAL;
 		}
 
-	ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
-	if (ret)
-		return ret;
+	if (is_char) {
+		char ch;
+
+		if (sscanf(buf, "%c", &ch) != 1)
+			return -EINVAL;
+		integer = ch;
+	} else {
+		ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
+		if (ret)
+			return ret;
+	}
 
 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
 					 integer, fract, this_attr->address);
diff --git a/include/linux/iio/types.h b/include/linux/iio/types.h
index fa824e160f35..8e0026da38c9 100644
--- a/include/linux/iio/types.h
+++ b/include/linux/iio/types.h
@@ -25,6 +25,7 @@  enum iio_event_info {
 #define IIO_VAL_INT_MULTIPLE 5
 #define IIO_VAL_FRACTIONAL 10
 #define IIO_VAL_FRACTIONAL_LOG2 11
+#define IIO_VAL_CHAR 12
 
 enum iio_available_type {
 	IIO_AVAIL_LIST,