Re: [PATCH v2] iio: potentiometer: add driver for Maxim Integrated DS1803
From: Slawomir Stepien
Date: Sat Apr 09 2016 - 07:12:39 EST
On Apr 08, 2016 23:27, Slawomir Stepien wrote:
> +static int ds1803_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct ds1803_data *data = iio_priv(indio_dev);
> + int pot = chan->channel;
> + int ret;
> + u16 result;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + ret = i2c_master_recv(data->client, (char *)&result,
> + indio_dev->num_channels);
> + if (ret < 0)
> + return ret;
> +
> + /* Get bits for given pot */
> + *val = (pot == 0 ? result & 0xff : result >> 8);
> + return IIO_VAL_INT;
> +
> + case IIO_CHAN_INFO_SCALE:
> + *val = 1000 * data->cfg->kohms;
> + *val2 = DS1803_MAX_POS;
> + return IIO_VAL_FRACTIONAL;
> + }
> +
> + return -EINVAL;
> +}
Or maybe this is more clean?
static int ds1803_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
struct ds1803_data *data = iio_priv(indio_dev);
int pot = chan->channel;
int ret;
unsigned char result[indio_dev->num_channels];
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = i2c_master_recv(data->client, result,
indio_dev->num_channels);
if (ret < 0)
return ret;
*val = result[pot];
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
*val = 1000 * data->cfg->kohms;
*val2 = DS1803_MAX_POS;
return IIO_VAL_FRACTIONAL;
}
return -EINVAL;
}
What do you think?
--
Slawomir Stepien