Commit ac663db3 authored by Alexander Koch's avatar Alexander Koch Committed by Jonathan Cameron

iio: light: opt3001: enable operation w/o IRQ

Enable operation of the TI OPT3001 light sensor without having an
interrupt line available to connect the INT pin to.

In this operation mode, we issue a conversion request and simply wait
for the conversion time available as timeout value, determined from
integration time configuration and the worst-case time given in the data
sheet (sect. 6.5, table on p. 5):

  short integration time (100ms): 110ms + 3ms = 113ms
   long integration time (800ms): 880ms + 3ms = 883ms

This change is transparent as behaviour defaults to using the interrupt
method if an interrupt no. is configured via device tree. Interrupt-less
operation mode is performed when no valid interrupt no. is given.
Signed-off-by: default avatarAlexander Koch <mail@alexanderkoch.net>
Signed-off-by: default avatarMichael Hornung <mhornung.linux@gmail.com>
Tested-by: default avatarAndreas Dannenberg <dannenberg@ti.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 84e8d090
...@@ -70,9 +70,12 @@ ...@@ -70,9 +70,12 @@
/* /*
* Time to wait for conversion result to be ready. The device datasheet * Time to wait for conversion result to be ready. The device datasheet
* worst-case max value is 880ms. Add some slack to be on the safe side. * sect. 6.5 states results are ready after total integration time plus 3ms.
* This results in worst-case max values of 113ms or 883ms, respectively.
* Add some slack to be on the safe side.
*/ */
#define OPT3001_RESULT_READY_TIMEOUT msecs_to_jiffies(1000) #define OPT3001_RESULT_READY_SHORT 150
#define OPT3001_RESULT_READY_LONG 1000
struct opt3001 { struct opt3001 {
struct i2c_client *client; struct i2c_client *client;
...@@ -92,6 +95,8 @@ struct opt3001 { ...@@ -92,6 +95,8 @@ struct opt3001 {
u8 high_thresh_exp; u8 high_thresh_exp;
u8 low_thresh_exp; u8 low_thresh_exp;
bool use_irq;
}; };
struct opt3001_scale { struct opt3001_scale {
...@@ -230,13 +235,16 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ...@@ -230,13 +235,16 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
u16 reg; u16 reg;
u8 exponent; u8 exponent;
u16 value; u16 value;
long timeout;
if (opt->use_irq) {
/* /*
* Enable the end-of-conversion interrupt mechanism. Note that doing * Enable the end-of-conversion interrupt mechanism. Note that
* so will overwrite the low-level limit value however we will restore * doing so will overwrite the low-level limit value however we
* this value later on. * will restore this value later on.
*/ */
ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT, ret = i2c_smbus_write_word_swapped(opt->client,
OPT3001_LOW_LIMIT,
OPT3001_LOW_LIMIT_EOC_ENABLE); OPT3001_LOW_LIMIT_EOC_ENABLE);
if (ret < 0) { if (ret < 0) {
dev_err(opt->dev, "failed to write register %02x\n", dev_err(opt->dev, "failed to write register %02x\n",
...@@ -244,11 +252,12 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ...@@ -244,11 +252,12 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
return ret; return ret;
} }
/* Reset data-ready indicator flag (will be set in the IRQ routine) */
opt->result_ready = false;
/* Allow IRQ to access the device despite lock being set */ /* Allow IRQ to access the device despite lock being set */
opt->ok_to_ignore_lock = true; opt->ok_to_ignore_lock = true;
}
/* Reset data-ready indicator flag */
opt->result_ready = false;
/* Configure for single-conversion mode and start a new conversion */ /* Configure for single-conversion mode and start a new conversion */
ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
...@@ -269,11 +278,44 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ...@@ -269,11 +278,44 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
goto err; goto err;
} }
if (opt->use_irq) {
/* Wait for the IRQ to indicate the conversion is complete */ /* Wait for the IRQ to indicate the conversion is complete */
ret = wait_event_timeout(opt->result_ready_queue, opt->result_ready, ret = wait_event_timeout(opt->result_ready_queue,
OPT3001_RESULT_READY_TIMEOUT); opt->result_ready,
msecs_to_jiffies(OPT3001_RESULT_READY_LONG));
} else {
/* Sleep for result ready time */
timeout = (opt->int_time == OPT3001_INT_TIME_SHORT) ?
OPT3001_RESULT_READY_SHORT : OPT3001_RESULT_READY_LONG;
msleep(timeout);
/* Check result ready flag */
ret = i2c_smbus_read_word_swapped(opt->client,
OPT3001_CONFIGURATION);
if (ret < 0) {
dev_err(opt->dev, "failed to read register %02x\n",
OPT3001_CONFIGURATION);
goto err;
}
if (!(ret & OPT3001_CONFIGURATION_CRF)) {
ret = -ETIMEDOUT;
goto err;
}
/* Obtain value */
ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
if (ret < 0) {
dev_err(opt->dev, "failed to read register %02x\n",
OPT3001_RESULT);
goto err;
}
opt->result = ret;
opt->result_ready = true;
}
err: err:
if (opt->use_irq)
/* Disallow IRQ to access the device while lock is active */ /* Disallow IRQ to access the device while lock is active */
opt->ok_to_ignore_lock = false; opt->ok_to_ignore_lock = false;
...@@ -282,20 +324,24 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2) ...@@ -282,20 +324,24 @@ static int opt3001_get_lux(struct opt3001 *opt, int *val, int *val2)
else if (ret < 0) else if (ret < 0)
return ret; return ret;
if (opt->use_irq) {
/* /*
* Disable the end-of-conversion interrupt mechanism by restoring the * Disable the end-of-conversion interrupt mechanism by
* low-level limit value (clearing OPT3001_LOW_LIMIT_EOC_ENABLE). Note * restoring the low-level limit value (clearing
* that selectively clearing those enable bits would affect the actual * OPT3001_LOW_LIMIT_EOC_ENABLE). Note that selectively clearing
* limit value due to bit-overlap and therefore can't be done. * those enable bits would affect the actual limit value due to
* bit-overlap and therefore can't be done.
*/ */
value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa; value = (opt->low_thresh_exp << 12) | opt->low_thresh_mantissa;
ret = i2c_smbus_write_word_swapped(opt->client, OPT3001_LOW_LIMIT, ret = i2c_smbus_write_word_swapped(opt->client,
OPT3001_LOW_LIMIT,
value); value);
if (ret < 0) { if (ret < 0) {
dev_err(opt->dev, "failed to write register %02x\n", dev_err(opt->dev, "failed to write register %02x\n",
OPT3001_LOW_LIMIT); OPT3001_LOW_LIMIT);
return ret; return ret;
} }
}
exponent = OPT3001_REG_EXPONENT(opt->result); exponent = OPT3001_REG_EXPONENT(opt->result);
mantissa = OPT3001_REG_MANTISSA(opt->result); mantissa = OPT3001_REG_MANTISSA(opt->result);
...@@ -736,6 +782,8 @@ static int opt3001_probe(struct i2c_client *client, ...@@ -736,6 +782,8 @@ static int opt3001_probe(struct i2c_client *client,
return ret; return ret;
} }
/* Make use of INT pin only if valid IRQ no. is given */
if (irq > 0) {
ret = request_threaded_irq(irq, NULL, opt3001_irq, ret = request_threaded_irq(irq, NULL, opt3001_irq,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
"opt3001", iio); "opt3001", iio);
...@@ -743,6 +791,10 @@ static int opt3001_probe(struct i2c_client *client, ...@@ -743,6 +791,10 @@ static int opt3001_probe(struct i2c_client *client,
dev_err(dev, "failed to request IRQ #%d\n", irq); dev_err(dev, "failed to request IRQ #%d\n", irq);
return ret; return ret;
} }
opt->use_irq = true;
} else {
dev_dbg(opt->dev, "enabling interrupt-less operation\n");
}
return 0; return 0;
} }
...@@ -754,6 +806,7 @@ static int opt3001_remove(struct i2c_client *client) ...@@ -754,6 +806,7 @@ static int opt3001_remove(struct i2c_client *client)
int ret; int ret;
u16 reg; u16 reg;
if (opt->use_irq)
free_irq(client->irq, iio); free_irq(client->irq, iio);
ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION); ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_CONFIGURATION);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment