Lines Matching refs:opt
147 static int opt3001_find_scale(const struct opt3001 *opt, int val, in opt3001_find_scale() argument
170 static void opt3001_to_iio_ret(struct opt3001 *opt, u8 exponent, in opt3001_to_iio_ret() argument
180 static void opt3001_set_mode(struct opt3001 *opt, u16 *reg, u16 mode) in opt3001_set_mode() argument
184 opt->mode = mode; in opt3001_set_mode()
224 static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) in opt3001_get_lux() argument
233 if (opt->use_irq) { in opt3001_get_lux()
239 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_lux()
243 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
249 opt->ok_to_ignore_lock = true; in opt3001_get_lux()
253 opt->result_ready = false; in opt3001_get_lux()
256 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_get_lux()
258 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
264 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SINGLE); in opt3001_get_lux()
266 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_get_lux()
269 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
274 if (opt->use_irq) { in opt3001_get_lux()
276 ret = wait_event_timeout(opt->result_ready_queue, in opt3001_get_lux()
277 opt->result_ready, in opt3001_get_lux()
283 timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ? in opt3001_get_lux()
288 ret = i2c_smbus_read_word_swapped(opt->client, in opt3001_get_lux()
291 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
302 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_get_lux()
304 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_get_lux()
308 opt->result = ret; in opt3001_get_lux()
309 opt->result_ready = true; in opt3001_get_lux()
313 if (opt->use_irq) in opt3001_get_lux()
315 opt->ok_to_ignore_lock = false; in opt3001_get_lux()
320 if (opt->use_irq) { in opt3001_get_lux()
328 value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa; in opt3001_get_lux()
329 ret = i2c_smbus_write_word_swapped(opt->client, in opt3001_get_lux()
333 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_get_lux()
339 exponent = OPT3001_REG_EXPONENT(opt->result); in opt3001_get_lux()
340 mantissa = OPT3001_REG_MANTISSA(opt->result); in opt3001_get_lux()
342 opt3001_to_iio_ret(opt, exponent, mantissa, val, val2); in opt3001_get_lux()
347 static int opt3001_get_int_time(struct opt3001 *opt, int *val, int *val2) in opt3001_get_int_time() argument
350 *val2 = opt->int_time; in opt3001_get_int_time()
355 static int opt3001_set_int_time(struct opt3001 *opt, int time) in opt3001_set_int_time() argument
360 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_set_int_time()
362 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_set_int_time()
372 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_set_int_time()
376 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_set_int_time()
382 return i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_set_int_time()
390 struct opt3001 *opt = iio_priv(iio); in opt3001_read_raw() local
393 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_read_raw()
399 mutex_lock(&opt->lock); in opt3001_read_raw()
403 ret = opt3001_get_lux(opt, val, val2); in opt3001_read_raw()
406 ret = opt3001_get_int_time(opt, val, val2); in opt3001_read_raw()
412 mutex_unlock(&opt->lock); in opt3001_read_raw()
421 struct opt3001 *opt = iio_priv(iio); in opt3001_write_raw() local
424 if (opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_raw()
436 mutex_lock(&opt->lock); in opt3001_write_raw()
437 ret = opt3001_set_int_time(opt, val2); in opt3001_write_raw()
438 mutex_unlock(&opt->lock); in opt3001_write_raw()
448 struct opt3001 *opt = iio_priv(iio); in opt3001_read_event_value() local
451 mutex_lock(&opt->lock); in opt3001_read_event_value()
455 opt3001_to_iio_ret(opt, opt->high_thresh_exp, in opt3001_read_event_value()
456 opt->high_thresh_mantissa, val, val2); in opt3001_read_event_value()
459 opt3001_to_iio_ret(opt, opt->low_thresh_exp, in opt3001_read_event_value()
460 opt->low_thresh_mantissa, val, val2); in opt3001_read_event_value()
466 mutex_unlock(&opt->lock); in opt3001_read_event_value()
476 struct opt3001 *opt = iio_priv(iio); in opt3001_write_event_value() local
488 mutex_lock(&opt->lock); in opt3001_write_event_value()
490 ret = opt3001_find_scale(opt, val, val2, &exponent); in opt3001_write_event_value()
492 dev_err(opt->dev, "can't find scale for %d.%06u\n", val, val2); in opt3001_write_event_value()
502 opt->high_thresh_mantissa = mantissa; in opt3001_write_event_value()
503 opt->high_thresh_exp = exponent; in opt3001_write_event_value()
507 opt->low_thresh_mantissa = mantissa; in opt3001_write_event_value()
508 opt->low_thresh_exp = exponent; in opt3001_write_event_value()
515 ret = i2c_smbus_write_word_swapped(opt->client, reg, value); in opt3001_write_event_value()
517 dev_err(opt->dev, "failed to write register %02x\n", reg); in opt3001_write_event_value()
522 mutex_unlock(&opt->lock); in opt3001_write_event_value()
531 struct opt3001 *opt = iio_priv(iio); in opt3001_read_event_config() local
533 return opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS; in opt3001_read_event_config()
540 struct opt3001 *opt = iio_priv(iio); in opt3001_write_event_config() local
545 if (state && opt->mode == OPT3001_CONFIGURATION_M_CONTINUOUS) in opt3001_write_event_config()
548 if (!state && opt->mode == OPT3001_CONFIGURATION_M_SHUTDOWN) in opt3001_write_event_config()
551 mutex_lock(&opt->lock); in opt3001_write_event_config()
556 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_write_event_config()
558 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_write_event_config()
564 opt3001_set_mode(opt, ®, mode); in opt3001_write_event_config()
566 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_write_event_config()
569 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_write_event_config()
575 mutex_unlock(&opt->lock); in opt3001_write_event_config()
590 static int opt3001_read_id(struct opt3001 *opt) in opt3001_read_id() argument
596 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_MANUFACTURER_ID); in opt3001_read_id()
598 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
606 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_DEVICE_ID); in opt3001_read_id()
608 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_read_id()
615 dev_info(opt->dev, "Found %c%c OPT%04x\n", manufacturer[0], in opt3001_read_id()
621 static int opt3001_configure(struct opt3001 *opt) in opt3001_configure() argument
626 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_configure()
628 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
641 opt->int_time = OPT3001_INT_TIME_LONG; in opt3001_configure()
643 opt->int_time = OPT3001_INT_TIME_SHORT; in opt3001_configure()
646 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN); in opt3001_configure()
654 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_configure()
657 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_configure()
662 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_LOW_LIMIT); in opt3001_configure()
664 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
669 opt->low_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
670 opt->low_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
672 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_HIGH_LIMIT); in opt3001_configure()
674 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_configure()
679 opt->high_thresh_mantissa = OPT3001_REG_MANTISSA(ret); in opt3001_configure()
680 opt->high_thresh_exp = OPT3001_REG_EXPONENT(ret); in opt3001_configure()
688 struct opt3001 *opt = iio_priv(iio); in opt3001_irq() local
692 if (!opt->ok_to_ignore_lock) in opt3001_irq()
693 mutex_lock(&opt->lock); in opt3001_irq()
695 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_irq()
697 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
717 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); in opt3001_irq()
719 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_irq()
723 opt->result = ret; in opt3001_irq()
724 opt->result_ready = true; in opt3001_irq()
729 if (!opt->ok_to_ignore_lock) in opt3001_irq()
730 mutex_unlock(&opt->lock); in opt3001_irq()
733 wake_up(&opt->result_ready_queue); in opt3001_irq()
743 struct opt3001 *opt; in opt3001_probe() local
747 iio = devm_iio_device_alloc(dev, sizeof(*opt)); in opt3001_probe()
751 opt = iio_priv(iio); in opt3001_probe()
752 opt->client = client; in opt3001_probe()
753 opt->dev = dev; in opt3001_probe()
755 mutex_init(&opt->lock); in opt3001_probe()
756 init_waitqueue_head(&opt->result_ready_queue); in opt3001_probe()
759 ret = opt3001_read_id(opt); in opt3001_probe()
763 ret = opt3001_configure(opt); in opt3001_probe()
788 opt->use_irq = true; in opt3001_probe()
790 dev_dbg(opt->dev, "enabling interrupt-less operation\n"); in opt3001_probe()
799 struct opt3001 *opt = iio_priv(iio); in opt3001_remove() local
803 if (opt->use_irq) in opt3001_remove()
806 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); in opt3001_remove()
808 dev_err(opt->dev, "failed to read register %02x\n", in opt3001_remove()
814 opt3001_set_mode(opt, ®, OPT3001_CONFIGURATION_M_SHUTDOWN); in opt3001_remove()
816 ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_CONFIGURATION, in opt3001_remove()
819 dev_err(opt->dev, "failed to write register %02x\n", in opt3001_remove()