Lines Matching +full:sar +full:- +full:sensors
1 // SPDX-License-Identifier: GPL-2.0
5 * Common part of most Semtech SAR sensor.
32 /* All Semtech SAR sensors have IRQ bit in the same order. */
63 if (data->trigger_enabled) in sx_common_irq_handler()
64 iio_trigger_poll(data->trig); in sx_common_irq_handler()
83 ret = regmap_read(data->regmap, data->chip_info->reg_stat, &val); in sx_common_push_events()
85 dev_err(&data->client->dev, "i2c transfer error in irq\n"); in sx_common_push_events()
89 val >>= data->chip_info->stat_offset; in sx_common_push_events()
95 prox_changed = (data->chan_prox_stat ^ val) & data->chan_event; in sx_common_push_events()
97 for_each_set_bit(chan, &prox_changed, data->chip_info->num_channels) { in sx_common_push_events()
107 data->chan_prox_stat = val; in sx_common_push_events()
112 if (!data->client->irq) in sx_common_enable_irq()
114 return regmap_set_bits(data->regmap, data->chip_info->reg_irq_msk, in sx_common_enable_irq()
115 irq << data->chip_info->irq_msk_offset); in sx_common_enable_irq()
120 if (!data->client->irq) in sx_common_disable_irq()
122 return regmap_clear_bits(data->regmap, data->chip_info->reg_irq_msk, in sx_common_disable_irq()
123 irq << data->chip_info->irq_msk_offset); in sx_common_disable_irq()
133 if ((data->chan_read | data->chan_event) != channels) { in sx_common_update_chan_en()
134 ret = regmap_update_bits(data->regmap, in sx_common_update_chan_en()
135 data->chip_info->reg_enable_chan, in sx_common_update_chan_en()
136 data->chip_info->mask_enable_chan, in sx_common_update_chan_en()
141 data->chan_read = chan_read; in sx_common_update_chan_en()
142 data->chan_event = chan_event; in sx_common_update_chan_en()
148 return sx_common_update_chan_en(data, data->chan_read | BIT(channel), in sx_common_get_read_channel()
149 data->chan_event); in sx_common_get_read_channel()
154 return sx_common_update_chan_en(data, data->chan_read & ~BIT(channel), in sx_common_put_read_channel()
155 data->chan_event); in sx_common_put_read_channel()
160 return sx_common_update_chan_en(data, data->chan_read, in sx_common_get_event_channel()
161 data->chan_event | BIT(channel)); in sx_common_get_event_channel()
166 return sx_common_update_chan_en(data, data->chan_read, in sx_common_put_event_channel()
167 data->chan_event & ~BIT(channel)); in sx_common_put_event_channel()
171 * sx_common_read_proximity() - Read raw proximity value.
185 mutex_lock(&data->mutex); in sx_common_read_proximity()
187 ret = sx_common_get_read_channel(data, chan->channel); in sx_common_read_proximity()
195 mutex_unlock(&data->mutex); in sx_common_read_proximity()
197 if (data->client->irq) { in sx_common_read_proximity()
198 ret = wait_for_completion_interruptible(&data->completion); in sx_common_read_proximity()
199 reinit_completion(&data->completion); in sx_common_read_proximity()
201 ret = data->chip_info->ops.wait_for_sample(data); in sx_common_read_proximity()
204 mutex_lock(&data->mutex); in sx_common_read_proximity()
209 ret = data->chip_info->ops.read_prox_data(data, chan, &rawval); in sx_common_read_proximity()
213 *val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1); in sx_common_read_proximity()
219 ret = sx_common_put_read_channel(data, chan->channel); in sx_common_read_proximity()
223 mutex_unlock(&data->mutex); in sx_common_read_proximity()
230 sx_common_put_read_channel(data, chan->channel); in sx_common_read_proximity()
232 mutex_unlock(&data->mutex); in sx_common_read_proximity()
239 * sx_common_read_event_config() - Configure event setting.
254 return !!(data->chan_event & BIT(chan->channel)); in sx_common_read_event_config()
259 * sx_common_write_event_config() - Configure event setting.
278 if (!!(data->chan_event & BIT(chan->channel)) == state) in sx_common_write_event_config()
281 mutex_lock(&data->mutex); in sx_common_write_event_config()
283 ret = sx_common_get_event_channel(data, chan->channel); in sx_common_write_event_config()
286 if (!(data->chan_event & ~BIT(chan->channel))) { in sx_common_write_event_config()
289 sx_common_put_event_channel(data, chan->channel); in sx_common_write_event_config()
292 ret = sx_common_put_event_channel(data, chan->channel); in sx_common_write_event_config()
295 if (!data->chan_event) { in sx_common_write_event_config()
298 sx_common_get_event_channel(data, chan->channel); in sx_common_write_event_config()
303 mutex_unlock(&data->mutex); in sx_common_write_event_config()
314 mutex_lock(&data->mutex); in sx_common_set_trigger_state()
318 else if (!data->chan_read) in sx_common_set_trigger_state()
323 data->trigger_enabled = state; in sx_common_set_trigger_state()
326 mutex_unlock(&data->mutex); in sx_common_set_trigger_state()
342 mutex_lock(&data->mutex); in sx_common_irq_thread_handler()
344 ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val); in sx_common_irq_thread_handler()
346 dev_err(&data->client->dev, "i2c transfer error in irq\n"); in sx_common_irq_thread_handler()
350 if (val & ((SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ) << data->chip_info->irq_msk_offset)) in sx_common_irq_thread_handler()
353 if (val & (SX_COMMON_CONVDONE_IRQ << data->chip_info->irq_msk_offset)) in sx_common_irq_thread_handler()
354 complete(&data->completion); in sx_common_irq_thread_handler()
357 mutex_unlock(&data->mutex); in sx_common_irq_thread_handler()
365 struct iio_dev *indio_dev = pf->indio_dev; in sx_common_trigger_handler()
370 mutex_lock(&data->mutex); in sx_common_trigger_handler()
373 ret = data->chip_info->ops.read_prox_data(data, in sx_common_trigger_handler()
374 &indio_dev->channels[bit], in sx_common_trigger_handler()
379 data->buffer.channels[i++] = val; in sx_common_trigger_handler()
382 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, in sx_common_trigger_handler()
383 pf->timestamp); in sx_common_trigger_handler()
386 mutex_unlock(&data->mutex); in sx_common_trigger_handler()
388 iio_trigger_notify_done(indio_dev->trig); in sx_common_trigger_handler()
399 mutex_lock(&data->mutex); in sx_common_buffer_preenable()
401 __set_bit(indio_dev->channels[bit].channel, &channels); in sx_common_buffer_preenable()
403 ret = sx_common_update_chan_en(data, channels, data->chan_event); in sx_common_buffer_preenable()
404 mutex_unlock(&data->mutex); in sx_common_buffer_preenable()
413 mutex_lock(&data->mutex); in sx_common_buffer_postdisable()
414 ret = sx_common_update_chan_en(data, 0, data->chan_event); in sx_common_buffer_postdisable()
415 mutex_unlock(&data->mutex); in sx_common_buffer_postdisable()
432 if (!reg_def->property || !adev) in sx_common_get_raw_register_config()
435 snprintf(prop, ARRAY_SIZE(prop), "%s,reg_%s", acpi_device_hid(adev), reg_def->property); in sx_common_get_raw_register_config()
440 reg_def->def = raw; in sx_common_get_raw_register_config()
455 ret = regmap_write(data->regmap, data->chip_info->reg_reset, in sx_common_init_device()
460 usleep_range(1000, 2000); /* power-up time is ~1ms. */ in sx_common_init_device()
463 ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val); in sx_common_init_device()
468 for (i = 0; i < data->chip_info->num_default_regs; i++) { in sx_common_init_device()
469 initval = data->chip_info->ops.get_default_reg(dev, i, &tmp); in sx_common_init_device()
470 ret = regmap_write(data->regmap, initval->reg, initval->def); in sx_common_init_device()
475 return data->chip_info->ops.init_compensation(indio_dev); in sx_common_init_device()
479 * sx_common_probe() - Common setup for Semtech SAR sensor
489 struct device *dev = &client->dev; in sx_common_probe()
496 return -ENOMEM; in sx_common_probe()
500 data->chip_info = chip_info; in sx_common_probe()
501 data->client = client; in sx_common_probe()
502 mutex_init(&data->mutex); in sx_common_probe()
503 init_completion(&data->completion); in sx_common_probe()
505 data->regmap = devm_regmap_init_i2c(client, regmap_config); in sx_common_probe()
506 if (IS_ERR(data->regmap)) in sx_common_probe()
507 return dev_err_probe(dev, PTR_ERR(data->regmap), in sx_common_probe()
518 ret = data->chip_info->ops.check_whoami(dev, indio_dev); in sx_common_probe()
522 indio_dev->modes = INDIO_DIRECT_MODE; in sx_common_probe()
524 indio_dev->channels = data->chip_info->iio_channels; in sx_common_probe()
525 indio_dev->num_channels = data->chip_info->num_iio_channels; in sx_common_probe()
526 indio_dev->info = &data->chip_info->iio_info; in sx_common_probe()
534 if (client->irq) { in sx_common_probe()
535 ret = devm_request_threaded_irq(dev, client->irq, in sx_common_probe()
543 data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", in sx_common_probe()
544 indio_dev->name, in sx_common_probe()
546 if (!data->trig) in sx_common_probe()
547 return -ENOMEM; in sx_common_probe()
549 data->trig->ops = &sx_common_trigger_ops; in sx_common_probe()
550 iio_trigger_set_drvdata(data->trig, indio_dev); in sx_common_probe()
552 ret = devm_iio_trigger_register(dev, data->trig); in sx_common_probe()