Re: [PATCH v5 3/4] iio: vcnl4000: Add sampling frequency support for VCNL4010/20.
From: Jonathan Cameron
Date: Sun Apr 26 2020 - 05:18:46 EST
On Wed, 22 Apr 2020 15:08:55 +0200
Mathieu Othacehe <m.othacehe@xxxxxxxxx> wrote:
> Add sampling frequency support for proximity data on VCNL4010 and VCNL4020
> chips.
>
> Signed-off-by: Mathieu Othacehe <m.othacehe@xxxxxxxxx>
One possible suggestion inline seeing as you'll be doing a v6.
I'm not that fussed if you would rather leave it for now though.
Jonathan
> ---
> drivers/iio/light/vcnl4000.c | 112 ++++++++++++++++++++++++++++++++++-
> 1 file changed, 111 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
> index 4041608910d0..aefb549953ad 100644
> --- a/drivers/iio/light/vcnl4000.c
> +++ b/drivers/iio/light/vcnl4000.c
> @@ -86,6 +86,19 @@
> #define VCNL4010_INT_DRDY \
> (BIT(VCNL4010_INT_PROXIMITY) | BIT(VCNL4010_INT_ALS))
>
> +#define VCNL4010_PROXIMITY_SAMPLING_FREQUENCY_AVAILABLE \
> + "1.95 3.90 7.81 16.62 31.25 62.5 125 250"
> +
> +static const int vcnl4010_prox_sampling_frequency[][2] = {
> + {1, 950000},
> + {3, 906250},
> + {7, 812500},
> + {16, 625000},
> + {31, 250000},
> + {62, 500000},
> + {125, 0},
> + {250, 0},
> +};
I mentioned in my (late) review for v4 that this lends itself
to using the read_avail callback. However that can always come
as a follow up patch as there is nothing 'wrong' with what you
do here as such.
>
> #define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend */
>
> @@ -366,6 +379,24 @@ static int vcnl4200_measure_proximity(struct vcnl4000_data *data, int *val)
> return vcnl4200_measure(data, &data->vcnl4200_ps, val);
> }
>
> +static int vcnl4010_read_proxy_samp_freq(struct vcnl4000_data *data, int *val,
> + int *val2)
> +{
> + int ret;
> +
> + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_PROX_RATE);
> + if (ret < 0)
> + return ret;
> +
> + if (ret >= ARRAY_SIZE(vcnl4010_prox_sampling_frequency))
> + return -EINVAL;
> +
> + *val = vcnl4010_prox_sampling_frequency[ret][0];
> + *val2 = vcnl4010_prox_sampling_frequency[ret][1];
> +
> + return 0;
> +}
> +
> static bool vcnl4010_is_in_periodic_mode(struct vcnl4000_data *data)
> {
> int ret;
> @@ -459,11 +490,75 @@ static int vcnl4010_read_raw(struct iio_dev *indio_dev,
>
> iio_device_release_direct_mode(indio_dev);
> return ret;
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + ret = vcnl4010_read_proxy_samp_freq(data, val, val2);
> + if (ret < 0)
> + return ret;
> + return IIO_VAL_INT_PLUS_MICRO;
> + default:
> + return -EINVAL;
> + }
> default:
> return -EINVAL;
> }
> }
>
> +static int vcnl4010_write_proxy_samp_freq(struct vcnl4000_data *data, int val,
> + int val2)
> +{
> + const unsigned int len = ARRAY_SIZE(vcnl4010_prox_sampling_frequency);
> + unsigned int i = len;
> +
> + do {
> + if (val > vcnl4010_prox_sampling_frequency[i][0])
> + break;
> + } while (--i);
> +
> + if (i == len)
> + return -EINVAL;
> +
> + return i2c_smbus_write_byte_data(data->client, VCNL4010_PROX_RATE, i);
> +}
> +
> +static int vcnl4010_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + int ret;
> + struct vcnl4000_data *data = iio_priv(indio_dev);
> +
> + ret = iio_device_claim_direct_mode(indio_dev);
> + if (ret)
> + return ret;
> +
> + /* Protect against event capture. */
> + if (vcnl4010_is_in_periodic_mode(data)) {
> + ret = -EBUSY;
> + goto end;
> + }
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + ret = vcnl4010_write_proxy_samp_freq(data, val, val2);
> + goto end;
> + default:
> + ret = -EINVAL;
> + goto end;
> + }
> + default:
> + ret = -EINVAL;
> + goto end;
> + }
> +
> +end:
> + iio_device_release_direct_mode(indio_dev);
> + return ret;
> +}
> +
> static int vcnl4010_read_event(struct iio_dev *indio_dev,
> const struct iio_chan_spec *chan,
> enum iio_event_type type,
> @@ -668,23 +763,38 @@ static const struct iio_chan_spec vcnl4010_channels[] = {
> }, {
> .type = IIO_PROXIMITY,
> .scan_index = 0,
> - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SAMP_FREQ),
> .event_spec = vcnl4000_event_spec,
> .num_event_specs = ARRAY_SIZE(vcnl4000_event_spec),
> .ext_info = vcnl4000_ext_info,
> },
> };
>
> +static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
> + VCNL4010_PROXIMITY_SAMPLING_FREQUENCY_AVAILABLE);
> +
> +static struct attribute *vcnl4010_attributes[] = {
> + &iio_const_attr_in_proximity_sampling_frequency_available.dev_attr.attr,
> + NULL
> +};
> +
> +static const struct attribute_group vcnl4010_attribute_group = {
> + .attrs = vcnl4010_attributes,
> +};
> +
> static const struct iio_info vcnl4000_info = {
> .read_raw = vcnl4000_read_raw,
> };
>
> static const struct iio_info vcnl4010_info = {
> .read_raw = vcnl4010_read_raw,
> + .write_raw = vcnl4010_write_raw,
> .read_event_value = vcnl4010_read_event,
> .write_event_value = vcnl4010_write_event,
> .read_event_config = vcnl4010_read_event_config,
> .write_event_config = vcnl4010_write_event_config,
> + .attrs = &vcnl4010_attribute_group,
> };
>
> static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = {