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_update_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()
116 irq << data->chip_info->irq_msk_offset); in sx_common_enable_irq()
121 if (!data->client->irq) in sx_common_disable_irq()
123 return regmap_update_bits(data->regmap, data->chip_info->reg_irq_msk, in sx_common_disable_irq()
124 irq << data->chip_info->irq_msk_offset, 0); in sx_common_disable_irq()
134 if ((data->chan_read | data->chan_event) != channels) { in sx_common_update_chan_en()
135 ret = regmap_update_bits(data->regmap, in sx_common_update_chan_en()
136 data->chip_info->reg_enable_chan, in sx_common_update_chan_en()
137 data->chip_info->mask_enable_chan, in sx_common_update_chan_en()
142 data->chan_read = chan_read; in sx_common_update_chan_en()
143 data->chan_event = chan_event; in sx_common_update_chan_en()
149 return sx_common_update_chan_en(data, data->chan_read | BIT(channel), in sx_common_get_read_channel()
150 data->chan_event); in sx_common_get_read_channel()
155 return sx_common_update_chan_en(data, data->chan_read & ~BIT(channel), in sx_common_put_read_channel()
156 data->chan_event); in sx_common_put_read_channel()
161 return sx_common_update_chan_en(data, data->chan_read, in sx_common_get_event_channel()
162 data->chan_event | BIT(channel)); in sx_common_get_event_channel()
167 return sx_common_update_chan_en(data, data->chan_read, in sx_common_put_event_channel()
168 data->chan_event & ~BIT(channel)); in sx_common_put_event_channel()
172 * sx_common_read_proximity() - Read raw proximity value.
186 mutex_lock(&data->mutex); in sx_common_read_proximity()
188 ret = sx_common_get_read_channel(data, chan->channel); in sx_common_read_proximity()
196 mutex_unlock(&data->mutex); in sx_common_read_proximity()
198 if (data->client->irq) { in sx_common_read_proximity()
199 ret = wait_for_completion_interruptible(&data->completion); in sx_common_read_proximity()
200 reinit_completion(&data->completion); in sx_common_read_proximity()
202 ret = data->chip_info->ops.wait_for_sample(data); in sx_common_read_proximity()
205 mutex_lock(&data->mutex); in sx_common_read_proximity()
210 ret = data->chip_info->ops.read_prox_data(data, chan, &rawval); in sx_common_read_proximity()
214 *val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1); in sx_common_read_proximity()
220 ret = sx_common_put_read_channel(data, chan->channel); in sx_common_read_proximity()
224 mutex_unlock(&data->mutex); in sx_common_read_proximity()
231 sx_common_put_read_channel(data, chan->channel); in sx_common_read_proximity()
233 mutex_unlock(&data->mutex); in sx_common_read_proximity()
240 * sx_common_read_event_config() - Configure event setting.
255 return !!(data->chan_event & BIT(chan->channel)); in sx_common_read_event_config()
260 * sx_common_write_event_config() - Configure event setting.
279 if (!!(data->chan_event & BIT(chan->channel)) == state) in sx_common_write_event_config()
282 mutex_lock(&data->mutex); in sx_common_write_event_config()
284 ret = sx_common_get_event_channel(data, chan->channel); in sx_common_write_event_config()
287 if (!(data->chan_event & ~BIT(chan->channel))) { in sx_common_write_event_config()
290 sx_common_put_event_channel(data, chan->channel); in sx_common_write_event_config()
293 ret = sx_common_put_event_channel(data, chan->channel); in sx_common_write_event_config()
296 if (!data->chan_event) { in sx_common_write_event_config()
299 sx_common_get_event_channel(data, chan->channel); in sx_common_write_event_config()
304 mutex_unlock(&data->mutex); in sx_common_write_event_config()
315 mutex_lock(&data->mutex); in sx_common_set_trigger_state()
319 else if (!data->chan_read) in sx_common_set_trigger_state()
324 data->trigger_enabled = state; in sx_common_set_trigger_state()
327 mutex_unlock(&data->mutex); in sx_common_set_trigger_state()
343 mutex_lock(&data->mutex); in sx_common_irq_thread_handler()
345 ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val); in sx_common_irq_thread_handler()
347 dev_err(&data->client->dev, "i2c transfer error in irq\n"); in sx_common_irq_thread_handler()
351 if (val & ((SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ) << data->chip_info->irq_msk_offset)) in sx_common_irq_thread_handler()
354 if (val & (SX_COMMON_CONVDONE_IRQ << data->chip_info->irq_msk_offset)) in sx_common_irq_thread_handler()
355 complete(&data->completion); in sx_common_irq_thread_handler()
358 mutex_unlock(&data->mutex); in sx_common_irq_thread_handler()
366 struct iio_dev *indio_dev = pf->indio_dev; in sx_common_trigger_handler()
371 mutex_lock(&data->mutex); in sx_common_trigger_handler()
373 for_each_set_bit(bit, indio_dev->active_scan_mask, in sx_common_trigger_handler()
374 indio_dev->masklength) { in sx_common_trigger_handler()
375 ret = data->chip_info->ops.read_prox_data(data, in sx_common_trigger_handler()
376 &indio_dev->channels[bit], in sx_common_trigger_handler()
381 data->buffer.channels[i++] = val; in sx_common_trigger_handler()
384 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, in sx_common_trigger_handler()
385 pf->timestamp); in sx_common_trigger_handler()
388 mutex_unlock(&data->mutex); in sx_common_trigger_handler()
390 iio_trigger_notify_done(indio_dev->trig); in sx_common_trigger_handler()
401 mutex_lock(&data->mutex); in sx_common_buffer_preenable()
402 for_each_set_bit(bit, indio_dev->active_scan_mask, in sx_common_buffer_preenable()
403 indio_dev->masklength) in sx_common_buffer_preenable()
404 __set_bit(indio_dev->channels[bit].channel, &channels); in sx_common_buffer_preenable()
406 ret = sx_common_update_chan_en(data, channels, data->chan_event); in sx_common_buffer_preenable()
407 mutex_unlock(&data->mutex); in sx_common_buffer_preenable()
416 mutex_lock(&data->mutex); in sx_common_buffer_postdisable()
417 ret = sx_common_update_chan_en(data, 0, data->chan_event); in sx_common_buffer_postdisable()
418 mutex_unlock(&data->mutex); in sx_common_buffer_postdisable()
431 regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies); in sx_common_regulator_disable()
444 ret = regmap_write(data->regmap, data->chip_info->reg_reset, in sx_common_init_device()
449 usleep_range(1000, 2000); /* power-up time is ~1ms. */ in sx_common_init_device()
452 ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val); in sx_common_init_device()
457 for (i = 0; i < data->chip_info->num_default_regs; i++) { in sx_common_init_device()
458 initval = data->chip_info->ops.get_default_reg(dev, i, &tmp); in sx_common_init_device()
459 ret = regmap_write(data->regmap, initval->reg, initval->def); in sx_common_init_device()
464 return data->chip_info->ops.init_compensation(indio_dev); in sx_common_init_device()
468 * sx_common_probe() - Common setup for Semtech SAR sensor
477 struct device *dev = &client->dev; in sx_common_probe()
484 return -ENOMEM; in sx_common_probe()
488 data->chip_info = chip_info; in sx_common_probe()
489 data->client = client; in sx_common_probe()
490 data->supplies[0].supply = "vdd"; in sx_common_probe()
491 data->supplies[1].supply = "svdd"; in sx_common_probe()
492 mutex_init(&data->mutex); in sx_common_probe()
493 init_completion(&data->completion); in sx_common_probe()
495 data->regmap = devm_regmap_init_i2c(client, regmap_config); in sx_common_probe()
496 if (IS_ERR(data->regmap)) in sx_common_probe()
497 return dev_err_probe(dev, PTR_ERR(data->regmap), in sx_common_probe()
500 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies), in sx_common_probe()
501 data->supplies); in sx_common_probe()
505 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies); in sx_common_probe()
517 ret = data->chip_info->ops.check_whoami(dev, indio_dev); in sx_common_probe()
521 indio_dev->modes = INDIO_DIRECT_MODE; in sx_common_probe()
523 indio_dev->channels = data->chip_info->iio_channels; in sx_common_probe()
524 indio_dev->num_channels = data->chip_info->num_iio_channels; in sx_common_probe()
525 indio_dev->info = &data->chip_info->iio_info; in sx_common_probe()
533 if (client->irq) { in sx_common_probe()
534 ret = devm_request_threaded_irq(dev, client->irq, in sx_common_probe()
542 data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", in sx_common_probe()
543 indio_dev->name, in sx_common_probe()
545 if (!data->trig) in sx_common_probe()
546 return -ENOMEM; in sx_common_probe()
548 data->trig->ops = &sx_common_trigger_ops; in sx_common_probe()
549 iio_trigger_set_drvdata(data->trig, indio_dev); in sx_common_probe()
551 ret = devm_iio_trigger_register(dev, data->trig); in sx_common_probe()