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()
435 if (!reg_def->property || !adev) in sx_common_get_raw_register_config()
438 snprintf(prop, ARRAY_SIZE(prop), "%s,reg_%s", acpi_device_hid(adev), reg_def->property); in sx_common_get_raw_register_config()
443 reg_def->def = raw; in sx_common_get_raw_register_config()
458 ret = regmap_write(data->regmap, data->chip_info->reg_reset, in sx_common_init_device()
463 usleep_range(1000, 2000); /* power-up time is ~1ms. */ in sx_common_init_device()
466 ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val); in sx_common_init_device()
471 for (i = 0; i < data->chip_info->num_default_regs; i++) { in sx_common_init_device()
472 initval = data->chip_info->ops.get_default_reg(dev, i, &tmp); in sx_common_init_device()
473 ret = regmap_write(data->regmap, initval->reg, initval->def); in sx_common_init_device()
478 return data->chip_info->ops.init_compensation(indio_dev); in sx_common_init_device()
482 * sx_common_probe() - Common setup for Semtech SAR sensor
492 struct device *dev = &client->dev; in sx_common_probe()
499 return -ENOMEM; in sx_common_probe()
503 data->chip_info = chip_info; in sx_common_probe()
504 data->client = client; in sx_common_probe()
505 mutex_init(&data->mutex); in sx_common_probe()
506 init_completion(&data->completion); in sx_common_probe()
508 data->regmap = devm_regmap_init_i2c(client, regmap_config); in sx_common_probe()
509 if (IS_ERR(data->regmap)) in sx_common_probe()
510 return dev_err_probe(dev, PTR_ERR(data->regmap), in sx_common_probe()
521 ret = data->chip_info->ops.check_whoami(dev, indio_dev); in sx_common_probe()
525 indio_dev->modes = INDIO_DIRECT_MODE; in sx_common_probe()
527 indio_dev->channels = data->chip_info->iio_channels; in sx_common_probe()
528 indio_dev->num_channels = data->chip_info->num_iio_channels; in sx_common_probe()
529 indio_dev->info = &data->chip_info->iio_info; in sx_common_probe()
537 if (client->irq) { in sx_common_probe()
538 ret = devm_request_threaded_irq(dev, client->irq, in sx_common_probe()
546 data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", in sx_common_probe()
547 indio_dev->name, in sx_common_probe()
549 if (!data->trig) in sx_common_probe()
550 return -ENOMEM; in sx_common_probe()
552 data->trig->ops = &sx_common_trigger_ops; in sx_common_probe()
553 iio_trigger_set_drvdata(data->trig, indio_dev); in sx_common_probe()
555 ret = devm_iio_trigger_register(dev, data->trig); in sx_common_probe()