On Tue, 21 Apr 2020 09:55:30 +0200 Mathieu Othacehe <m.othacehe@xxxxxxxxx> wrote: > The VCNL4010 and VCNL4020 chips are able to raise interrupts on proximity > threshold events. Add support for threshold rising and falling events for > those two chips. > > Signed-off-by: Mathieu Othacehe <m.othacehe@xxxxxxxxx> > --- ... > + > +static const struct iio_chan_spec vcnl4010_channels[] = { > + { > + .type = IIO_LIGHT, > + .scan_index = -1, Why introduce scan index here? That's only used for buffers. > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | > + BIT(IIO_CHAN_INFO_SCALE), > + }, { > + .type = IIO_PROXIMITY, > + .scan_index = 0, > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), > + .event_spec = vcnl4000_event_spec, > + .num_event_specs = ARRAY_SIZE(vcnl4000_event_spec), > + .ext_info = vcnl4000_ext_info, > + }, > +}; > + > static const struct iio_info vcnl4000_info = { > .read_raw = vcnl4000_read_raw, > }; > > +static const struct iio_info vcnl4010_info = { > + .read_raw = vcnl4010_read_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, > +}; > + > +static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = { > + [VCNL4000] = { > + .prod = "VCNL4000", > + .init = vcnl4000_init, > + .measure_light = vcnl4000_measure_light, > + .measure_proximity = vcnl4000_measure_proximity, > + .set_power_state = vcnl4000_set_power_state, > + .channels = vcnl4000_channels, > + .num_channels = ARRAY_SIZE(vcnl4000_channels), > + .info = &vcnl4000_info, > + .irq_support = false, > + }, > + [VCNL4010] = { > + .prod = "VCNL4010/4020", > + .init = vcnl4000_init, > + .measure_light = vcnl4000_measure_light, > + .measure_proximity = vcnl4000_measure_proximity, > + .set_power_state = vcnl4000_set_power_state, > + .channels = vcnl4010_channels, > + .num_channels = ARRAY_SIZE(vcnl4010_channels), > + .info = &vcnl4010_info, > + .irq_support = true, > + }, > + [VCNL4040] = { > + .prod = "VCNL4040", > + .init = vcnl4200_init, > + .measure_light = vcnl4200_measure_light, > + .measure_proximity = vcnl4200_measure_proximity, > + .set_power_state = vcnl4200_set_power_state, > + .channels = vcnl4000_channels, > + .num_channels = ARRAY_SIZE(vcnl4000_channels), > + .info = &vcnl4000_info, > + .irq_support = false, > + }, > + [VCNL4200] = { > + .prod = "VCNL4200", > + .init = vcnl4200_init, > + .measure_light = vcnl4200_measure_light, > + .measure_proximity = vcnl4200_measure_proximity, > + .set_power_state = vcnl4200_set_power_state, > + .channels = vcnl4000_channels, > + .num_channels = ARRAY_SIZE(vcnl4000_channels), > + .info = &vcnl4000_info, > + .irq_support = false, > + }, > +}; > + > +static irqreturn_t vcnl4010_irq_thread(int irq, void *p) > +{ > + struct iio_dev *indio_dev = p; > + struct vcnl4000_data *data = iio_priv(indio_dev); > + unsigned long isr; > + int ret; > + > + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR); > + if (ret < 0) > + goto end; > + > + isr = ret; > + > + if (isr & VCNL4010_INT_THR) { > + if (test_bit(VCNL4010_INT_THR_LOW, &isr)) { > + iio_push_event(indio_dev, > + IIO_UNMOD_EVENT_CODE( > + IIO_PROXIMITY, > + 1, > + IIO_EV_TYPE_THRESH, > + IIO_EV_DIR_FALLING), > + iio_get_time_ns(indio_dev)); > + } > + > + if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) { > + iio_push_event(indio_dev, > + IIO_UNMOD_EVENT_CODE( > + IIO_PROXIMITY, > + 1, > + IIO_EV_TYPE_THRESH, > + IIO_EV_DIR_RISING), > + iio_get_time_ns(indio_dev)); > + } > + > + i2c_smbus_write_byte_data(data->client, VCNL4010_ISR, > + isr & VCNL4010_INT_THR); > + } > + > +end: > + return IRQ_HANDLED; > +} > + > static int vcnl4000_probe(struct i2c_client *client, > const struct i2c_device_id *id) > { > @@ -486,12 +805,25 @@ static int vcnl4000_probe(struct i2c_client *client, > data->near_level = 0; > > indio_dev->dev.parent = &client->dev; > - indio_dev->info = &vcnl4000_info; > - indio_dev->channels = vcnl4000_channels; > - indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels); > + indio_dev->info = data->chip_spec->info; > + indio_dev->channels = data->chip_spec->channels; > + indio_dev->num_channels = data->chip_spec->num_channels; > indio_dev->name = VCNL4000_DRV_NAME; > indio_dev->modes = INDIO_DIRECT_MODE; > > + if (client->irq && data->chip_spec->irq_support) { > + ret = devm_request_threaded_irq(&client->dev, client->irq, > + NULL, vcnl4010_irq_thread, > + IRQF_TRIGGER_FALLING | > + IRQF_ONESHOT, > + "vcnl4010_irq", > + indio_dev); > + if (ret < 0) { > + dev_err(&client->dev, "irq request failed\n"); > + return ret; > + } > + } > + > ret = pm_runtime_set_active(&client->dev); > if (ret < 0) > goto fail_poweroff;