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_update_bits(data->regmap, reg, bitmask, bitmask); in sx9500_inc_users()
218 (*counter)--; in sx9500_dec_users()
223 return regmap_update_bits(data->regmap, reg, bitmask, 0); 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()
657 for_each_set_bit(bit, indio_dev->active_scan_mask, in sx9500_trigger_handler()
658 indio_dev->masklength) { in sx9500_trigger_handler()
659 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit], in sx9500_trigger_handler()
664 data->buffer[i++] = val; in sx9500_trigger_handler()
667 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, in sx9500_trigger_handler()
671 mutex_unlock(&data->mutex); in sx9500_trigger_handler()
673 iio_trigger_notify_done(indio_dev->trig); in sx9500_trigger_handler()
683 mutex_lock(&data->mutex); in sx9500_buffer_postenable()
686 if (test_bit(i, indio_dev->active_scan_mask)) { in sx9500_buffer_postenable()
693 for (i = i - 1; i >= 0; i--) in sx9500_buffer_postenable()
694 if (test_bit(i, indio_dev->active_scan_mask)) in sx9500_buffer_postenable()
697 mutex_unlock(&data->mutex); in sx9500_buffer_postenable()
707 mutex_lock(&data->mutex); in sx9500_buffer_predisable()
710 if (test_bit(i, indio_dev->active_scan_mask)) { in sx9500_buffer_predisable()
717 for (i = i - 1; i >= 0; i--) in sx9500_buffer_predisable()
718 if (test_bit(i, indio_dev->active_scan_mask)) in sx9500_buffer_predisable()
721 mutex_unlock(&data->mutex); in sx9500_buffer_predisable()
798 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_init_compensation()
803 for (i = 10; i >= 0; i--) { in sx9500_init_compensation()
805 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val); in sx9500_init_compensation()
813 dev_err(&data->client->dev, "initial compensation timed out"); in sx9500_init_compensation()
814 ret = -ETIMEDOUT; in sx9500_init_compensation()
818 regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_init_compensation()
829 if (data->gpiod_rst) { in sx9500_init_device()
830 gpiod_set_value_cansleep(data->gpiod_rst, 0); in sx9500_init_device()
832 gpiod_set_value_cansleep(data->gpiod_rst, 1); in sx9500_init_device()
836 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0); in sx9500_init_device()
840 ret = regmap_write(data->regmap, SX9500_REG_RESET, in sx9500_init_device()
845 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val); in sx9500_init_device()
850 ret = regmap_write(data->regmap, in sx9500_init_device()
864 { "reset-gpios", &reset_gpios, 1 },
869 { "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
883 dev = &client->dev; in sx9500_gpio_probe()
889 if (client->irq <= 0) { in sx9500_gpio_probe()
894 client->irq = gpiod_to_irq(gpiod_int); in sx9500_gpio_probe()
897 data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); in sx9500_gpio_probe()
898 if (IS_ERR(data->gpiod_rst)) { in sx9500_gpio_probe()
900 data->gpiod_rst = NULL; in sx9500_gpio_probe()
910 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in sx9500_probe()
912 return -ENOMEM; in sx9500_probe()
915 data->client = client; in sx9500_probe()
916 mutex_init(&data->mutex); in sx9500_probe()
917 init_completion(&data->completion); in sx9500_probe()
918 data->trigger_enabled = false; in sx9500_probe()
920 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config); in sx9500_probe()
921 if (IS_ERR(data->regmap)) in sx9500_probe()
922 return PTR_ERR(data->regmap); in sx9500_probe()
924 indio_dev->name = SX9500_DRIVER_NAME; in sx9500_probe()
925 indio_dev->channels = sx9500_channels; in sx9500_probe()
926 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels); in sx9500_probe()
927 indio_dev->info = &sx9500_info; in sx9500_probe()
928 indio_dev->modes = INDIO_DIRECT_MODE; in sx9500_probe()
937 if (client->irq <= 0) in sx9500_probe()
938 dev_warn(&client->dev, "no valid irq found\n"); in sx9500_probe()
940 ret = devm_request_threaded_irq(&client->dev, client->irq, in sx9500_probe()
947 data->trig = devm_iio_trigger_alloc(&client->dev, in sx9500_probe()
948 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); in sx9500_probe()
949 if (!data->trig) in sx9500_probe()
950 return -ENOMEM; in sx9500_probe()
952 data->trig->ops = &sx9500_trigger_ops; in sx9500_probe()
953 iio_trigger_set_drvdata(data->trig, indio_dev); in sx9500_probe()
955 ret = iio_trigger_register(data->trig); in sx9500_probe()
975 if (client->irq > 0) in sx9500_probe()
976 iio_trigger_unregister(data->trig); in sx9500_probe()
988 if (client->irq > 0) in sx9500_remove()
989 iio_trigger_unregister(data->trig); in sx9500_remove()
990 kfree(data->buffer); in sx9500_remove()
999 mutex_lock(&data->mutex); in sx9500_suspend()
1000 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_suspend()
1001 &data->suspend_ctrl0); in sx9500_suspend()
1009 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0); in sx9500_suspend()
1012 mutex_unlock(&data->mutex); in sx9500_suspend()
1022 mutex_lock(&data->mutex); in sx9500_resume()
1023 ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, in sx9500_resume()
1024 data->suspend_ctrl0); in sx9500_resume()
1025 mutex_unlock(&data->mutex); in sx9500_resume()