Lines Matching full:opt
151 static int opt3001_find_scale(const struct opt3001 *opt, int val, in opt3001_find_scale() argument
174 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent, in opt3001_to_iio_ret() argument
184 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode) in opt3001_set_mode() argument
188 opt->mode = mode; in opt3001_set_mode()
228 static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) in opt3001_get_lux() argument
237 if (opt->use_irq) { in opt3001_get_lux()
243 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_lux()
247 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
253 opt->ok_to_ignore_lock = true; in opt3001_get_lux()
257 opt->result_ready = false; in opt3001_get_lux()
260 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_get_lux()
262 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
268 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE); in opt3001_get_lux()
270 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_get_lux()
273 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
278 if (opt->use_irq) { in opt3001_get_lux()
280 ret = wait_event_timeout(opt->result_ready_queue, in opt3001_get_lux()
281 opt->result_ready, in opt3001_get_lux()
287 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ? in opt3001_get_lux()
292 ret = i2c_smbus_read_word_swapped(opt->client, in opt3001_get_lux()
295 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
306 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_get_lux()
308 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
312 opt->result = ret; in opt3001_get_lux()
313 opt->result_ready = true; in opt3001_get_lux()
317 if (opt->use_irq) in opt3001_get_lux()
319 opt->ok_to_ignore_lock = false; in opt3001_get_lux()
324 if (opt->use_irq) { in opt3001_get_lux()
332 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa; in opt3001_get_lux()
333 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_lux()
337 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
343 exponent = OPT3001_REG_EXPONENT(opt->result); in opt3001_get_lux()
344 mantissa = OPT3001_REG_MANTISSA(opt->result); in opt3001_get_lux()
346 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2); in opt3001_get_lux()
351 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2) in opt3001_get_int_time() argument
354 *val2 = opt->int_time; in opt3001_get_int_time()
359 static int opt3001_set_int_time(struct opt3001 *opt, int time) in opt3001_set_int_time() argument
364 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_set_int_time()
366 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_set_int_time()
376 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_set_int_time()
380 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_set_int_time()
386 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_set_int_time()
394 struct opt3001 *opt = iio_priv(iio); in opt3001_read_raw() local
397 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_read_raw()
403 mutex_lock(&opt->lock); in opt3001_read_raw()
407 ret = opt3001_get_lux(opt, val, val2); in opt3001_read_raw()
410 ret = opt3001_get_int_time(opt, val, val2); in opt3001_read_raw()
416 mutex_unlock(&opt->lock); in opt3001_read_raw()
425 struct opt3001 *opt = iio_priv(iio); in opt3001_write_raw() local
428 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_raw()
440 mutex_lock(&opt->lock); in opt3001_write_raw()
441 ret = opt3001_set_int_time(opt, val2); in opt3001_write_raw()
442 mutex_unlock(&opt->lock); in opt3001_write_raw()
452 struct opt3001 *opt = iio_priv(iio); in opt3001_read_event_value() local
455 mutex_lock(&opt->lock); in opt3001_read_event_value()
459 opt3001_to_iio_ret(opt, opt->high_thresh_exp, in opt3001_read_event_value()
460 opt->high_thresh_mantissa, val, val2); in opt3001_read_event_value()
463 opt3001_to_iio_ret(opt, opt->low_thresh_exp, in opt3001_read_event_value()
464 opt->low_thresh_mantissa, val, val2); in opt3001_read_event_value()
470 mutex_unlock(&opt->lock); in opt3001_read_event_value()
480 struct opt3001 *opt = iio_priv(iio); in opt3001_write_event_value() local
492 mutex_lock(&opt->lock); in opt3001_write_event_value()
494 ret = opt3001_find_scale(opt, val, val2, &exponent); in opt3001_write_event_value()
496 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2); in opt3001_write_event_value()
506 opt->high_thresh_mantissa = mantissa; in opt3001_write_event_value()
507 opt->high_thresh_exp = exponent; in opt3001_write_event_value()
511 opt->low_thresh_mantissa = mantissa; in opt3001_write_event_value()
512 opt->low_thresh_exp = exponent; in opt3001_write_event_value()
519 ret = i2c_smbus_write_word_swapped(opt->client, reg, value); in opt3001_write_event_value()
521 dev_err(opt->dev, "failed to write register %02x\n", reg); in opt3001_write_event_value()
526 mutex_unlock(&opt->lock); in opt3001_write_event_value()
535 struct opt3001 *opt = iio_priv(iio); in opt3001_read_event_config() local
537 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS; in opt3001_read_event_config()
544 struct opt3001 *opt = iio_priv(iio); in opt3001_write_event_config() local
549 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_event_config()
552 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN) in opt3001_write_event_config()
555 mutex_lock(&opt->lock); in opt3001_write_event_config()
560 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_write_event_config()
562 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_write_event_config()
568 opt3001_set_mode(opt, ®, mode); in opt3001_write_event_config()
570 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_write_event_config()
573 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_write_event_config()
579 mutex_unlock(&opt->lock); in opt3001_write_event_config()
594 static int opt3001_read_id(struct opt3001 *opt) in opt3001_read_id() argument
600 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID); in opt3001_read_id()
602 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
610 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID); in opt3001_read_id()
612 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
619 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0], in opt3001_read_id()
625 static int opt3001_configure(struct opt3001 *opt) in opt3001_configure() argument
630 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_configure()
632 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
645 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_configure()
647 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_configure()
650 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN); in opt3001_configure()
658 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_configure()
661 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_configure()
666 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT); in opt3001_configure()
668 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
673 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
674 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
676 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT); in opt3001_configure()
678 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
683 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
684 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
692 struct opt3001 *opt = iio_priv(iio); in opt3001_irq() local
696 if (!opt->ok_to_ignore_lock) in opt3001_irq()
697 mutex_lock(&opt->lock); in opt3001_irq()
699 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_irq()
701 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
721 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_irq()
723 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
727 opt->result = ret; in opt3001_irq()
728 opt->result_ready = true; in opt3001_irq()
733 if (!opt->ok_to_ignore_lock) in opt3001_irq()
734 mutex_unlock(&opt->lock); in opt3001_irq()
737 wake_up(&opt->result_ready_queue); in opt3001_irq()
747 struct opt3001 *opt; in opt3001_probe() local
751 iio = devm_iio_device_alloc(dev, sizeof(*opt)); in opt3001_probe()
755 opt = iio_priv(iio); in opt3001_probe()
756 opt->client = client; in opt3001_probe()
757 opt->dev = dev; in opt3001_probe()
759 mutex_init(&opt->lock); in opt3001_probe()
760 init_waitqueue_head(&opt->result_ready_queue); in opt3001_probe()
763 ret = opt3001_read_id(opt); in opt3001_probe()
767 ret = opt3001_configure(opt); in opt3001_probe()
792 opt->use_irq = true; in opt3001_probe()
794 dev_dbg(opt->dev, "enabling interrupt-less operation\n"); in opt3001_probe()
803 struct opt3001 *opt = iio_priv(iio); in opt3001_remove() local
807 if (opt->use_irq) in opt3001_remove()
810 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_remove()
812 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_remove()
818 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN); in opt3001_remove()
820 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_remove()
823 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_remove()