iio: Factor IIO value formating into its own function
authorLars-Peter Clausen <lars@metafoo.de>
Mon, 7 Oct 2013 14:11:00 +0000 (15:11 +0100)
committerJonathan Cameron <jic23@kernel.org>
Sat, 12 Oct 2013 11:23:33 +0000 (12:23 +0100)
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
drivers/iio/iio_core.h
drivers/iio/industrialio-core.c

index 9939917033ca721795cb4e02465f4c168481c8fa..f6db6af36ba6de20ceb2cf9dc159dff9f53a1987 100644 (file)
@@ -35,6 +35,8 @@ int __iio_add_chan_devattr(const char *postfix,
                           struct list_head *attr_list);
 void iio_free_chan_devattr_list(struct list_head *attr_list);
 
+ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2);
+
 /* Event interface flags */
 #define IIO_BUSY_BIT_POS 1
 
index 572982fe31556ffe46533e11fae67de513b3e236..f7211576abe5d5385740df8276f0424d9c270574 100644 (file)
@@ -363,22 +363,20 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev,
 }
 EXPORT_SYMBOL_GPL(iio_enum_write);
 
-static ssize_t iio_read_channel_info(struct device *dev,
-                                    struct device_attribute *attr,
-                                    char *buf)
+/**
+ * iio_format_value() - Formats a IIO value into its string representation
+ * @buf: The buffer to which the formated value gets written
+ * @type: One of the IIO_VAL_... constants. This decides how the val and val2
+ *        parameters are formatted.
+ * @val: First part of the value, exact meaning depends on the type parameter.
+ * @val2: Second part of the value, exact meaning depends on the type parameter.
+ */
+ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
 {
-       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
-       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
        unsigned long long tmp;
-       int val, val2;
        bool scale_db = false;
-       int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
-                                           &val, &val2, this_attr->address);
 
-       if (ret < 0)
-               return ret;
-
-       switch (ret) {
+       switch (type) {
        case IIO_VAL_INT:
                return sprintf(buf, "%d\n", val);
        case IIO_VAL_INT_PLUS_MICRO_DB:
@@ -410,6 +408,22 @@ static ssize_t iio_read_channel_info(struct device *dev,
        }
 }
 
+static ssize_t iio_read_channel_info(struct device *dev,
+                                    struct device_attribute *attr,
+                                    char *buf)
+{
+       struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+       struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
+       int val, val2;
+       int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
+                                           &val, &val2, this_attr->address);
+
+       if (ret < 0)
+               return ret;
+
+       return iio_format_value(buf, ret, val, val2);
+}
+
 /**
  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  * @str: The string to parse
This page took 0.026091 seconds and 5 git commands to generate.