Lines Matching +full:end +full:- +full:of +full:- +full:conversion
1 // SPDX-License-Identifier: GPL-2.0-only
3 * opt3001.c - Texas Instruments OPT3001 Light Sensor
5 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com
55 /* The end-of-conversion enable is located in the low-limit register */
65 * Time to wait for conversion result to be ready. The device datasheet
67 * This results in worst-case max values of 113ms or 883ms, respectively.
85 * Factor as specified by conversion equation in datasheet.
94 * Factor used to align decimal part of proccessed value to six decimal
231 for (i = 0; i < ARRAY_SIZE(*opt->chip_info->scales); i++) { in opt3001_find_scale()
232 const struct opt3001_scale *scale = &(*opt->chip_info->scales)[i]; in opt3001_find_scale()
236 if (val < scale->val || in opt3001_find_scale()
237 (val == scale->val && val2 <= scale->val2)) { in opt3001_find_scale()
243 return -EINVAL; in opt3001_find_scale()
250 int whole = opt->chip_info->factor_whole; in opt3001_to_iio_ret()
251 int integer = opt->chip_info->factor_integer; in opt3001_to_iio_ret()
252 int decimal = opt->chip_info->factor_decimal; in opt3001_to_iio_ret()
256 *val2 = (ret - (*val * integer)) * decimal; in opt3001_to_iio_ret()
263 opt->mode = mode; in opt3001_set_mode()
323 if (opt->use_irq) { in opt3001_get_processed()
325 * Enable the end-of-conversion interrupt mechanism. Note that in opt3001_get_processed()
326 * doing so will overwrite the low-level limit value however we in opt3001_get_processed()
329 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_processed()
333 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_processed()
339 opt->ok_to_ignore_lock = true; in opt3001_get_processed()
342 /* Reset data-ready indicator flag */ in opt3001_get_processed()
343 opt->result_ready = false; in opt3001_get_processed()
345 /* Configure for single-conversion mode and start a new conversion */ in opt3001_get_processed()
346 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_get_processed()
348 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_processed()
356 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_get_processed()
359 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_processed()
364 if (opt->use_irq) { in opt3001_get_processed()
365 /* Wait for the IRQ to indicate the conversion is complete */ in opt3001_get_processed()
366 ret = wait_event_timeout(opt->result_ready_queue, in opt3001_get_processed()
367 opt->result_ready, in opt3001_get_processed()
370 return -ETIMEDOUT; in opt3001_get_processed()
373 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ? in opt3001_get_processed()
378 ret = i2c_smbus_read_word_swapped(opt->client, in opt3001_get_processed()
381 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_processed()
387 ret = -ETIMEDOUT; in opt3001_get_processed()
392 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_get_processed()
394 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_processed()
398 opt->result = ret; in opt3001_get_processed()
399 opt->result_ready = true; in opt3001_get_processed()
403 if (opt->use_irq) in opt3001_get_processed()
405 opt->ok_to_ignore_lock = false; in opt3001_get_processed()
410 if (opt->use_irq) { in opt3001_get_processed()
412 * Disable the end-of-conversion interrupt mechanism by in opt3001_get_processed()
413 * restoring the low-level limit value (clearing in opt3001_get_processed()
416 * bit-overlap and therefore can't be done. in opt3001_get_processed()
418 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa; in opt3001_get_processed()
419 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_processed()
423 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_processed()
429 exponent = OPT3001_REG_EXPONENT(opt->result); in opt3001_get_processed()
430 mantissa = OPT3001_REG_MANTISSA(opt->result); in opt3001_get_processed()
440 *val2 = opt->int_time; in opt3001_get_int_time()
450 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_set_int_time()
452 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_set_int_time()
462 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_set_int_time()
466 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_set_int_time()
469 return -EINVAL; in opt3001_set_int_time()
472 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_set_int_time()
483 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_read_raw()
484 return -EBUSY; in opt3001_read_raw()
486 if (chan->type != opt->chip_info->chan_type) in opt3001_read_raw()
487 return -EINVAL; in opt3001_read_raw()
489 mutex_lock(&opt->lock); in opt3001_read_raw()
500 ret = -EINVAL; in opt3001_read_raw()
503 mutex_unlock(&opt->lock); in opt3001_read_raw()
515 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_raw()
516 return -EBUSY; in opt3001_write_raw()
518 if (chan->type != opt->chip_info->chan_type) in opt3001_write_raw()
519 return -EINVAL; in opt3001_write_raw()
522 return -EINVAL; in opt3001_write_raw()
525 return -EINVAL; in opt3001_write_raw()
527 mutex_lock(&opt->lock); in opt3001_write_raw()
529 mutex_unlock(&opt->lock); in opt3001_write_raw()
542 mutex_lock(&opt->lock); in opt3001_read_event_value()
546 opt3001_to_iio_ret(opt, opt->high_thresh_exp, in opt3001_read_event_value()
547 opt->high_thresh_mantissa, val, val2); in opt3001_read_event_value()
550 opt3001_to_iio_ret(opt, opt->low_thresh_exp, in opt3001_read_event_value()
551 opt->low_thresh_mantissa, val, val2); in opt3001_read_event_value()
554 ret = -EINVAL; in opt3001_read_event_value()
557 mutex_unlock(&opt->lock); in opt3001_read_event_value()
580 return -EINVAL; in opt3001_write_event_value()
582 mutex_lock(&opt->lock); in opt3001_write_event_value()
586 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2); in opt3001_write_event_value()
590 whole = opt->chip_info->factor_whole; in opt3001_write_event_value()
591 integer = opt->chip_info->factor_integer; in opt3001_write_event_value()
592 decimal = opt->chip_info->factor_decimal; in opt3001_write_event_value()
601 opt->high_thresh_mantissa = mantissa; in opt3001_write_event_value()
602 opt->high_thresh_exp = exponent; in opt3001_write_event_value()
606 opt->low_thresh_mantissa = mantissa; in opt3001_write_event_value()
607 opt->low_thresh_exp = exponent; in opt3001_write_event_value()
610 ret = -EINVAL; in opt3001_write_event_value()
614 ret = i2c_smbus_write_word_swapped(opt->client, reg, value); in opt3001_write_event_value()
616 dev_err(opt->dev, "failed to write register %02x\n", reg); in opt3001_write_event_value()
621 mutex_unlock(&opt->lock); in opt3001_write_event_value()
632 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS; in opt3001_read_event_config()
644 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_event_config()
647 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN) in opt3001_write_event_config()
650 mutex_lock(&opt->lock); in opt3001_write_event_config()
655 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_write_event_config()
657 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_write_event_config()
665 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_write_event_config()
668 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_write_event_config()
674 mutex_unlock(&opt->lock); in opt3001_write_event_config()
695 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID); in opt3001_read_id()
697 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
705 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID); in opt3001_read_id()
707 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
714 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0], in opt3001_read_id()
725 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_configure()
727 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
734 /* Enable automatic full-scale setting mode */ in opt3001_configure()
738 /* Reflect status of the device's integration time setting */ in opt3001_configure()
740 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_configure()
742 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_configure()
747 /* Configure for latched window-style comparison operation */ in opt3001_configure()
753 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_configure()
756 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_configure()
761 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT); in opt3001_configure()
763 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
768 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
769 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
771 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT); in opt3001_configure()
773 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
778 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
779 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
790 enum iio_chan_type chan_type = opt->chip_info->chan_type; in opt3001_irq()
791 bool ok_to_ignore_lock = opt->ok_to_ignore_lock; in opt3001_irq()
794 mutex_lock(&opt->lock); in opt3001_irq()
796 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_irq()
798 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
818 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_irq()
820 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
824 opt->result = ret; in opt3001_irq()
825 opt->result_ready = true; in opt3001_irq()
831 mutex_unlock(&opt->lock); in opt3001_irq()
834 wake_up(&opt->result_ready_queue); in opt3001_irq()
841 struct device *dev = &client->dev; in opt3001_probe()
845 int irq = client->irq; in opt3001_probe()
850 return -ENOMEM; in opt3001_probe()
853 opt->client = client; in opt3001_probe()
854 opt->dev = dev; in opt3001_probe()
855 opt->chip_info = i2c_get_match_data(client); in opt3001_probe()
857 mutex_init(&opt->lock); in opt3001_probe()
858 init_waitqueue_head(&opt->result_ready_queue); in opt3001_probe()
861 if (opt->chip_info->has_id) { in opt3001_probe()
871 iio->name = client->name; in opt3001_probe()
872 iio->channels = *opt->chip_info->channels; in opt3001_probe()
873 iio->num_channels = opt->chip_info->num_channels; in opt3001_probe()
874 iio->modes = INDIO_DIRECT_MODE; in opt3001_probe()
875 iio->info = &opt3001_info; in opt3001_probe()
883 /* Make use of INT pin only if valid IRQ no. is given */ in opt3001_probe()
892 opt->use_irq = true; in opt3001_probe()
894 dev_dbg(opt->dev, "enabling interrupt-less operation\n"); in opt3001_probe()
907 if (opt->use_irq) in opt3001_remove()
908 free_irq(client->irq, iio); in opt3001_remove()
910 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_remove()
912 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_remove()
920 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_remove()
923 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_remove()
962 MODULE_DEVICE_TABLE(of, opt3001_of_match);