/*
* This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
* Chip is combined proximity and ambient light sensor.
*
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/i2c/bh1770glc.h>
#include <linux/regulator/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/slab.h>
#define BH1770_ALS_CONTROL 0x80 /* ALS operation mode control */
#define BH1770_PS_CONTROL 0x81 /* PS operation mode control */
#define BH1770_I_LED 0x82 /* active LED and LED1, LED2 current */
#define BH1770_I_LED3 0x83 /* LED3 current setting */
#define BH1770_ALS_PS_MEAS 0x84 /* Forced mode trigger */
#define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
#define BH1770_ALS_MEAS_RATE 0x86 /* ALS meas. rate at stand alone mode */
#define BH1770_PART_ID 0x8a /* Part number and revision ID */
#define BH1770_MANUFACT_ID 0x8b /* Manufacturerer ID */
#define BH1770_ALS_DATA_0 0x8c /* ALS DATA low byte */
#define BH1770_ALS_DATA_1 0x8d /* ALS DATA high byte */
#define BH1770_ALS_PS_STATUS 0x8e /* Measurement data and int status */
#define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
#define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
#define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
#define BH1770_INTERRUPT 0x92 /* Interrupt setting */
#define BH1770_PS_TH_LED1 0x93 /* PS interrupt threshold for LED1 */
#define BH1770_PS_TH_LED2 0x94 /* PS interrupt threshold for LED2 */
#define BH1770_PS_TH_LED3 0x95 /* PS interrupt threshold for LED3 */
#define BH1770_ALS_TH_UP_0 0x96 /* ALS upper threshold low byte */
#define BH1770_ALS_TH_UP_1 0x97 /* ALS upper threshold high byte */
#define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
#define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
/* MANUFACT_ID */
#define BH1770_MANUFACT_ROHM 0x01
#define BH1770_MANUFACT_OSRAM 0x03
/* PART_ID */
#define BH1770_PART 0x90
#define BH1770_PART_MASK 0xf0
#define BH1770_REV_MASK 0x0f
#define BH1770_REV_SHIFT 0
#define BH1770_REV_0 0x00
#define BH1770_REV_1 0x01
/* Operating modes for both */
#define BH1770_STANDBY 0x00
#define BH1770_FORCED 0x02
#define BH1770_STANDALONE 0x03
#define BH1770_SWRESET (0x01 << 2)
#define BH1770_PS_TRIG_MEAS (1 << 0)
#define BH1770_ALS_TRIG_MEAS (1 << 1)
/* Interrupt control */
#define BH1770_INT_OUTPUT_MODE (1 << 3) /* 0 = latched */
#define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
#define BH1770_INT_ALS_ENA (1 << 1)
#define BH1770_INT_PS_ENA (1 << 0)
/* Interrupt status */
#define BH1770_INT_LED1_DATA (1 << 0)
#define BH1770_INT_LED1_INT (1 << 1)
#define BH1770_INT_LED2_DATA (1 << 2)
#define BH1770_INT_LED2_INT (1 << 3)
#define BH1770_INT_LED3_DATA (1 << 4)
#define BH1770_INT_LED3_INT (1 << 5)
#define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
#define BH1770_INT_ALS_DATA (1 << 6)
#define BH1770_INT_ALS_INT (1 << 7)
/* Led channels */
#define BH1770_LED1 0x00
#define BH1770_DISABLE 0
#define BH1770_ENABLE 1
#define BH1770_PROX_CHANNELS 1
#define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
#define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */