Commit f50f9831 authored by Hans de Goede's avatar Hans de Goede Committed by Jonathan Cameron

iio: light: cm32181: Use units of 1/100000th for calibscale and lux_per_bit

Use units of 1/100000th for calibscale and lux_per_bit. The similar
cm3232 driver already uses 1/100000th as unit for calibscale.

This allows for higher-accuracy and makes it easier to add support
for getting device-specific calibscale and lux_per_bit values from
a device's ACPI tables, as the values in the ACPI tables also use
1/100000th units.

This units change means that our intermediate values in cm32181_get_lux()
may get quite big, change the type of the lux variable to a u64 to
deal with this.
Reviewed-by: default avatarAndy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: default avatarHans de Goede <hdegoede@redhat.com>
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 7574cb1d
...@@ -46,11 +46,11 @@ ...@@ -46,11 +46,11 @@
#define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
#define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
#define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ #define CM32181_LUX_PER_BIT 500 /* ALS_SM=01 IT=800ms */
#define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ #define CM32181_LUX_PER_BIT_RESOLUTION 100000
#define CM32181_CALIBSCALE_DEFAULT 1000 #define CM32181_LUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
#define CM32181_CALIBSCALE_RESOLUTION 1000 #define CM32181_CALIBSCALE_DEFAULT 100000
#define MLUX_PER_LUX 1000 #define CM32181_CALIBSCALE_RESOLUTION 100000
#define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c
...@@ -205,15 +205,15 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181) ...@@ -205,15 +205,15 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181)
struct i2c_client *client = cm32181->client; struct i2c_client *client = cm32181->client;
int ret; int ret;
int als_it; int als_it;
unsigned long lux; u64 lux;
ret = cm32181_read_als_it(cm32181, &als_it); ret = cm32181_read_als_it(cm32181, &als_it);
if (ret < 0) if (ret < 0)
return -EINVAL; return -EINVAL;
lux = CM32181_MLUX_PER_BIT; lux = CM32181_LUX_PER_BIT;
lux *= CM32181_MLUX_PER_BIT_BASE_IT; lux *= CM32181_LUX_PER_BIT_BASE_IT;
lux /= als_it; lux = div_u64(lux, als_it);
ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
if (ret < 0) if (ret < 0)
...@@ -221,8 +221,8 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181) ...@@ -221,8 +221,8 @@ static int cm32181_get_lux(struct cm32181_chip *cm32181)
lux *= ret; lux *= ret;
lux *= cm32181->calibscale; lux *= cm32181->calibscale;
lux /= CM32181_CALIBSCALE_RESOLUTION; lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION);
lux /= MLUX_PER_LUX; lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION);
if (lux > 0xFFFF) if (lux > 0xFFFF)
lux = 0xFFFF; lux = 0xFFFF;
......
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