Source
x
/*
* apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
*
* Copyright 2013 Oleksandr Kravchenko <o.v.kravchenko@globallogic.com>
*
* This file is subject to the terms and conditions of version 2 of
* the GNU General Public License. See the file COPYING in the main
* directory of this archive for more details.
*/
/* Command register bits */
/* Select command register. Must write as 1 */
/* I2C write/read: if 1 word, if 0 byte */
/* Interrupt clear. Clears pending interrupt */
/* Register set */
/* Control of basic functions */
/* Low byte of low interrupt threshold */
/* Low byte of high interrupt threshold */
/* Interrupt control */
/* Low byte of ADC channel 0 */
/* Low byte of ADC channel 1 */
/* Power on/off value for APDS9300_CONTROL register */
/* Interrupts */
/* Interrupt Persist Function: Any value outside of threshold range */
/* Max threshold value */
struct apds9300_data {
struct i2c_client *client;
struct mutex mutex;
int power_state;
int thresh_low;
int thresh_hi;
int intr_en;
};
/* Lux calculation */
/* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
static const u16 apds9300_lux_ratio[] = {
0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
98, 105, 112, 120, 128, 136, 144, 152, 160, 168, 177, 185, 194, 203,
212, 221, 230, 239, 249, 258, 268, 277, 287, 297, 307, 317, 327, 337,
347, 358, 368, 379, 390, 400,
};
static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1)
{
unsigned long lux, tmp;
/* avoid division by zero */
if (ch0 == 0)
return 0;
tmp = DIV_ROUND_UP(ch1 * 100, ch0);
if (tmp <= 52) {
lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0
* apds9300_lux_ratio[tmp] * 5930ull, 1000);
} else if (tmp <= 65) {
lux = 2290 * ch0 - 2910 * ch1;
} else if (tmp <= 80) {
lux = 1570 * ch0 - 1800 * ch1;
} else if (tmp <= 130) {
lux = 338 * ch0 - 260 * ch1;
} else {
lux = 0;
}
return lux / 100000;
}