Lines Matching +full:trig +full:- +full:gpios
1 // SPDX-License-Identifier: GPL-2.0-only
77 #define SX9500_CHAN_MASK GENMASK(SX9500_NUM_CHANNELS - 1, 0)
82 struct iio_trigger *trig; member
171 regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
172 regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
173 regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
212 return regmap_set_bits(data->regmap, reg, bitmask); in sx9500_inc_users()
218 (*counter)--; in sx9500_dec_users()
223 return regmap_clear_bits(data->regmap, reg, bitmask); in sx9500_dec_users()
228 return sx9500_inc_users(data, &data->channel_users[chan], in sx9500_inc_chan_users()
234 return sx9500_dec_users(data, &data->channel_users[chan], in sx9500_dec_chan_users()
240 return sx9500_inc_users(data, &data->data_rdy_users, in sx9500_inc_data_rdy_users()
246 return sx9500_dec_users(data, &data->data_rdy_users, in sx9500_dec_data_rdy_users()
252 return sx9500_inc_users(data, &data->close_far_users, in sx9500_inc_close_far_users()
259 return sx9500_dec_users(data, &data->close_far_users, in sx9500_dec_close_far_users()
271 ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel); in sx9500_read_prox_data()
275 ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, ®val, 2); in sx9500_read_prox_data()
293 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val); in sx9500_wait_for_sample()
310 mutex_lock(&data->mutex); in sx9500_read_proximity()
312 ret = sx9500_inc_chan_users(data, chan->channel); in sx9500_read_proximity()
320 mutex_unlock(&data->mutex); in sx9500_read_proximity()
322 if (data->client->irq > 0) in sx9500_read_proximity()
323 ret = wait_for_completion_interruptible(&data->completion); in sx9500_read_proximity()
327 mutex_lock(&data->mutex); in sx9500_read_proximity()
340 ret = sx9500_dec_chan_users(data, chan->channel); in sx9500_read_proximity()
351 sx9500_dec_chan_users(data, chan->channel); in sx9500_read_proximity()
353 mutex_unlock(&data->mutex); in sx9500_read_proximity()
354 reinit_completion(&data->completion); in sx9500_read_proximity()
365 mutex_lock(&data->mutex); in sx9500_read_samp_freq()
366 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, ®val); in sx9500_read_samp_freq()
367 mutex_unlock(&data->mutex); in sx9500_read_samp_freq()
386 switch (chan->type) { in sx9500_read_raw()
399 return -EINVAL; in sx9500_read_raw()
402 return -EINVAL; in sx9500_read_raw()
417 return -EINVAL; in sx9500_set_samp_freq()
419 mutex_lock(&data->mutex); in sx9500_set_samp_freq()
421 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_set_samp_freq()
425 mutex_unlock(&data->mutex); in sx9500_set_samp_freq()
436 switch (chan->type) { in sx9500_write_raw()
442 return -EINVAL; in sx9500_write_raw()
445 return -EINVAL; in sx9500_write_raw()
454 if (data->trigger_enabled) in sx9500_irq_handler()
455 iio_trigger_poll(data->trig); in sx9500_irq_handler()
472 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); in sx9500_push_events()
474 dev_err(&data->client->dev, "i2c transfer error in irq\n"); in sx9500_push_events()
484 if (!data->event_enabled[chan]) in sx9500_push_events()
486 if (new_prox == data->prox_stat[chan]) in sx9500_push_events()
494 data->prox_stat[chan] = new_prox; in sx9500_push_events()
505 mutex_lock(&data->mutex); in sx9500_irq_thread_handler()
507 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); in sx9500_irq_thread_handler()
509 dev_err(&data->client->dev, "i2c transfer error in irq\n"); in sx9500_irq_thread_handler()
517 complete(&data->completion); in sx9500_irq_thread_handler()
520 mutex_unlock(&data->mutex); in sx9500_irq_thread_handler()
532 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || in sx9500_read_event_config()
534 return -EINVAL; in sx9500_read_event_config()
536 return data->event_enabled[chan->channel]; in sx9500_read_event_config()
548 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH || in sx9500_write_event_config()
550 return -EINVAL; in sx9500_write_event_config()
552 mutex_lock(&data->mutex); in sx9500_write_event_config()
555 ret = sx9500_inc_chan_users(data, chan->channel); in sx9500_write_event_config()
562 ret = sx9500_dec_chan_users(data, chan->channel); in sx9500_write_event_config()
570 data->event_enabled[chan->channel] = state; in sx9500_write_event_config()
575 sx9500_dec_chan_users(data, chan->channel); in sx9500_write_event_config()
577 sx9500_inc_chan_users(data, chan->channel); in sx9500_write_event_config()
579 mutex_unlock(&data->mutex); in sx9500_write_event_config()
588 mutex_lock(&data->mutex); in sx9500_update_scan_mode()
589 kfree(data->buffer); in sx9500_update_scan_mode()
590 data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL); in sx9500_update_scan_mode()
591 mutex_unlock(&data->mutex); in sx9500_update_scan_mode()
593 if (data->buffer == NULL) in sx9500_update_scan_mode()
594 return -ENOMEM; in sx9500_update_scan_mode()
620 static int sx9500_set_trigger_state(struct iio_trigger *trig, in sx9500_set_trigger_state() argument
623 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); in sx9500_set_trigger_state()
627 mutex_lock(&data->mutex); in sx9500_set_trigger_state()
636 data->trigger_enabled = state; in sx9500_set_trigger_state()
639 mutex_unlock(&data->mutex); in sx9500_set_trigger_state()
651 struct iio_dev *indio_dev = pf->indio_dev; in sx9500_trigger_handler()
655 mutex_lock(&data->mutex); in sx9500_trigger_handler()
658 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit], in sx9500_trigger_handler()
663 data->buffer[i++] = val; in sx9500_trigger_handler()
666 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, in sx9500_trigger_handler()
670 mutex_unlock(&data->mutex); in sx9500_trigger_handler()
672 iio_trigger_notify_done(indio_dev->trig); in sx9500_trigger_handler()
682 mutex_lock(&data->mutex); in sx9500_buffer_postenable()
685 if (test_bit(i, indio_dev->active_scan_mask)) { in sx9500_buffer_postenable()
692 for (i = i - 1; i >= 0; i--) in sx9500_buffer_postenable()
693 if (test_bit(i, indio_dev->active_scan_mask)) in sx9500_buffer_postenable()
696 mutex_unlock(&data->mutex); in sx9500_buffer_postenable()
706 mutex_lock(&data->mutex); in sx9500_buffer_predisable()
709 if (test_bit(i, indio_dev->active_scan_mask)) { in sx9500_buffer_predisable()
716 for (i = i - 1; i >= 0; i--) in sx9500_buffer_predisable()
717 if (test_bit(i, indio_dev->active_scan_mask)) in sx9500_buffer_predisable()
720 mutex_unlock(&data->mutex); in sx9500_buffer_predisable()
797 ret = regmap_set_bits(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_init_compensation()
802 for (i = 10; i >= 0; i--) { in sx9500_init_compensation()
804 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); in sx9500_init_compensation()
812 dev_err(&data->client->dev, "initial compensation timed out"); in sx9500_init_compensation()
813 ret = -ETIMEDOUT; in sx9500_init_compensation()
817 regmap_clear_bits(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_init_compensation()
828 if (data->gpiod_rst) { in sx9500_init_device()
829 gpiod_set_value_cansleep(data->gpiod_rst, 0); in sx9500_init_device()
831 gpiod_set_value_cansleep(data->gpiod_rst, 1); in sx9500_init_device()
835 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0); in sx9500_init_device()
839 ret = regmap_write(data->regmap, SX9500_REG_RESET, in sx9500_init_device()
844 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); in sx9500_init_device()
849 ret = regmap_write(data->regmap, in sx9500_init_device()
863 { "reset-gpios", &reset_gpios, 1 },
868 { "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
882 dev = &client->dev; in sx9500_gpio_probe()
888 if (client->irq <= 0) { in sx9500_gpio_probe()
893 client->irq = gpiod_to_irq(gpiod_int); in sx9500_gpio_probe()
896 data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); in sx9500_gpio_probe()
897 if (IS_ERR(data->gpiod_rst)) { in sx9500_gpio_probe()
899 data->gpiod_rst = NULL; in sx9500_gpio_probe()
909 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in sx9500_probe()
911 return -ENOMEM; in sx9500_probe()
914 data->client = client; in sx9500_probe()
915 mutex_init(&data->mutex); in sx9500_probe()
916 init_completion(&data->completion); in sx9500_probe()
917 data->trigger_enabled = false; in sx9500_probe()
919 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config); in sx9500_probe()
920 if (IS_ERR(data->regmap)) in sx9500_probe()
921 return PTR_ERR(data->regmap); in sx9500_probe()
923 indio_dev->name = SX9500_DRIVER_NAME; in sx9500_probe()
924 indio_dev->channels = sx9500_channels; in sx9500_probe()
925 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels); in sx9500_probe()
926 indio_dev->info = &sx9500_info; in sx9500_probe()
927 indio_dev->modes = INDIO_DIRECT_MODE; in sx9500_probe()
936 if (client->irq <= 0) in sx9500_probe()
937 dev_warn(&client->dev, "no valid irq found\n"); in sx9500_probe()
939 ret = devm_request_threaded_irq(&client->dev, client->irq, in sx9500_probe()
946 data->trig = devm_iio_trigger_alloc(&client->dev, in sx9500_probe()
947 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); in sx9500_probe()
948 if (!data->trig) in sx9500_probe()
949 return -ENOMEM; in sx9500_probe()
951 data->trig->ops = &sx9500_trigger_ops; in sx9500_probe()
952 iio_trigger_set_drvdata(data->trig, indio_dev); in sx9500_probe()
954 ret = iio_trigger_register(data->trig); in sx9500_probe()
974 if (client->irq > 0) in sx9500_probe()
975 iio_trigger_unregister(data->trig); in sx9500_probe()
987 if (client->irq > 0) in sx9500_remove()
988 iio_trigger_unregister(data->trig); in sx9500_remove()
989 kfree(data->buffer); in sx9500_remove()
998 mutex_lock(&data->mutex); in sx9500_suspend()
999 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_suspend()
1000 &data->suspend_ctrl0); in sx9500_suspend()
1008 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0); in sx9500_suspend()
1011 mutex_unlock(&data->mutex); in sx9500_suspend()
1021 mutex_lock(&data->mutex); in sx9500_resume()
1022 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_resume()
1023 data->suspend_ctrl0); in sx9500_resume()
1024 mutex_unlock(&data->mutex); in sx9500_resume()