Lines Matching refs:opt

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