Commit 190420ab authored by Samu Onkalo's avatar Samu Onkalo Committed by Linus Torvalds

drivers/misc: driver for bh1770glc / sfh7770 ALS and proximity sensor

This is a driver for ROHM BH1770GLC and OSRAM SFH7770 combined ALS and
proximity sensor.

Interface is sysfs based.  The driver uses interrupts to provide new data.
 The driver supports pm_runtime and regulator frameworks.

See Documentation/misc-devices/bh1770glc.txt for details
Signed-off-by: default avatarSamu Onkalo <samu.p.onkalo@nokia.com>
Acked-by: default avatarJonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 4b068de9
......@@ -315,6 +315,16 @@ config SENSORS_BH1780
This driver can also be built as a module. If so, the module
will be called bh1780gli.
config SENSORS_BH1770
tristate "BH1770GLC / SFH7770 combined ALS - Proximity sensor"
depends on I2C
---help---
Say Y here if you want to build a driver for BH1770GLC (ROHM) or
SFH7770 (Osram) combined ambient light and proximity sensor chip.
To compile this driver as a module, choose M here: the
module will be called bh1770glc. If unsure, say N here.
config HMC6352
tristate "Honeywell HMC6352 compass"
depends on I2C
......
......@@ -16,6 +16,7 @@ obj-$(CONFIG_TIFM_CORE) += tifm_core.o
obj-$(CONFIG_TIFM_7XX1) += tifm_7xx1.o
obj-$(CONFIG_PHANTOM) += phantom.o
obj-$(CONFIG_SENSORS_BH1780) += bh1780gli.o
obj-$(CONFIG_SENSORS_BH1770) += bh1770glc.o
obj-$(CONFIG_SGI_IOC4) += ioc4.o
obj-$(CONFIG_ENCLOSURE_SERVICES) += enclosure.o
obj-$(CONFIG_KGDB_TESTS) += kgdbts.o
......
/*
* 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 */
#define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
#define BH1770_STARTUP_DELAY 50
#define BH1770_RESET_TIME 10
#define BH1770_TIMEOUT 2100 /* Timeout in 2.1 seconds */
#define BH1770_LUX_RANGE 65535
#define BH1770_PROX_RANGE 255
#define BH1770_COEF_SCALER 1024
#define BH1770_CALIB_SCALER 8192
#define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
#define BH1770_LUX_DEF_THRES 1000
#define BH1770_PROX_DEF_THRES 70
#define BH1770_PROX_DEF_ABS_THRES 100
#define BH1770_DEFAULT_PERSISTENCE 10
#define BH1770_PROX_MAX_PERSISTENCE 50
#define BH1770_LUX_GA_SCALE 16384
#define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
#define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
#define BH1770_LUX_CORR_SCALE 4096
#define PROX_ABOVE_THRESHOLD 1
#define PROX_BELOW_THRESHOLD 0
#define PROX_IGNORE_LUX_LIMIT 500
struct bh1770_chip {
struct bh1770_platform_data *pdata;
char chipname[10];
u8 revision;
struct i2c_client *client;
struct regulator_bulk_data regs[2];
struct mutex mutex; /* avoid parallel access */
wait_queue_head_t wait;
bool int_mode_prox;
bool int_mode_lux;
struct delayed_work prox_work;
u32 lux_cf; /* Chip specific factor */
u32 lux_ga;
u32 lux_calib;
int lux_rate_index;
u32 lux_corr;
u16 lux_data_raw;
u16 lux_threshold_hi;
u16 lux_threshold_lo;
u16 lux_thres_hi_onchip;
u16 lux_thres_lo_onchip;
bool lux_wait_result;
int prox_enable_count;
u16 prox_coef;
u16 prox_const;
int prox_rate;
int prox_rate_threshold;
u8 prox_persistence;
u8 prox_persistence_counter;
u8 prox_data;
u8 prox_threshold;
u8 prox_threshold_hw;
bool prox_force_update;
u8 prox_abs_thres;
u8 prox_led;
};
static const char reg_vcc[] = "Vcc";
static const char reg_vleds[] = "Vleds";
/*
* Supported stand alone rates in ms from chip data sheet
* {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
*/
static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
/* Supported IR-led currents in mA */
static const u8 prox_curr_ma[] = {5, 10, 20, 50, 100, 150, 200};
/*
* Supported stand alone rates in ms from chip data sheet
* {100, 200, 500, 1000, 2000};
*/
static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
/*
* interrupt control functions are called while keeping chip->mutex
* excluding module probe / remove
*/
static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
int lux)
{
chip->int_mode_lux = lux;
/* Set interrupt modes, interrupt active low, latched */
return i2c_smbus_write_byte_data(chip->client,
BH1770_INTERRUPT,
(lux << 1) | chip->int_mode_prox);
}
static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
int ps)
{
chip->int_mode_prox = ps;
return i2c_smbus_write_byte_data(chip->client,
BH1770_INTERRUPT,
(chip->int_mode_lux << 1) | (ps << 0));
}
/* chip->mutex is always kept here */
static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
{
/* sysfs may call this when the chip is powered off */
if (pm_runtime_suspended(&chip->client->dev))
return 0;
/* Proper proximity response needs fastest lux rate (100ms) */
if (chip->prox_enable_count)
rate_index = 0;
return i2c_smbus_write_byte_data(chip->client,
BH1770_ALS_MEAS_RATE,
rate_index);
}
static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
{
int rate;
rate = (mode == PROX_ABOVE_THRESHOLD) ?
chip->prox_rate_threshold : chip->prox_rate;
return i2c_smbus_write_byte_data(chip->client,
BH1770_PS_MEAS_RATE,
rate);
}
/* InfraredLED is controlled by the chip during proximity scanning */
static inline int bh1770_led_cfg(struct bh1770_chip *chip)
{
/* LED cfg, current for leds 1 and 2 */
return i2c_smbus_write_byte_data(chip->client,
BH1770_I_LED,
(BH1770_LED1 << 6) |
(BH1770_LED_5mA << 3) |
chip->prox_led);
}
/*
* Following two functions converts raw ps values from HW to normalized
* values. Purpose is to compensate differences between different sensor
* versions and variants so that result means about the same between
* versions.
*/
static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
{
u16 adjusted;
adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
BH1770_COEF_SCALER);
if (adjusted > BH1770_PROX_RANGE)
adjusted = BH1770_PROX_RANGE;
return adjusted;
}
static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
{
u16 raw;
raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
if (raw > chip->prox_const)
raw = raw - chip->prox_const;
else
raw = 0;
return raw;
}
/*
* Following two functions converts raw lux values from HW to normalized
* values. Purpose is to compensate differences between different sensor
* versions and variants so that result means about the same between
* versions. Chip->mutex is kept when this is called.
*/
static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
{
u8 tmp = 0;
/* sysfs may call this when the chip is powered off */
if (pm_runtime_suspended(&chip->client->dev))
return 0;
tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
chip->prox_threshold_hw = tmp;
return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
tmp);
}
static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
{
u32 lux;
lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
return min(lux, (u32)BH1770_LUX_RANGE);
}
static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
u16 adjusted)
{
return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
}
/* chip->mutex is kept when this is called */
static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
u16 threshold_hi, u16 threshold_lo)
{
u8 data[4];
int ret;
/* sysfs may call this when the chip is powered off */
if (pm_runtime_suspended(&chip->client->dev))
return 0;
/*
* Compensate threshold values with the correction factors if not
* set to minimum or maximum.
* Min & max values disables interrupts.
*/
if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
if (chip->lux_thres_hi_onchip == threshold_hi &&
chip->lux_thres_lo_onchip == threshold_lo)
return 0;
chip->lux_thres_hi_onchip = threshold_hi;
chip->lux_thres_lo_onchip = threshold_lo;
data[0] = threshold_hi;
data[1] = threshold_hi >> 8;
data[2] = threshold_lo;
data[3] = threshold_lo >> 8;
ret = i2c_smbus_write_i2c_block_data(chip->client,
BH1770_ALS_TH_UP_0,
ARRAY_SIZE(data),
data);
return ret;
}
static int bh1770_lux_get_result(struct bh1770_chip *chip)
{
u16 data;
int ret;
ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
if (ret < 0)
return ret;
data = ret & 0xff;
ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
if (ret < 0)
return ret;
chip->lux_data_raw = data | ((ret & 0xff) << 8);
return 0;
}
/* Calculate correction value which contains chip and device specific parts */
static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
{
u32 tmp;
/* Impact of glass attenuation correction */
tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
/* Impact of chip factor correction */
tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
/* Impact of Device specific calibration correction */
tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
return tmp;
}
static int bh1770_lux_read_result(struct bh1770_chip *chip)
{
bh1770_lux_get_result(chip);
return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
}
/*
* Chip on / off functions are called while keeping mutex except probe
* or remove phase
*/
static int bh1770_chip_on(struct bh1770_chip *chip)
{
int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
chip->regs);
if (ret < 0)
return ret;
usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
/* Reset the chip */
i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
BH1770_SWRESET);
usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
/*
* ALS is started always since proximity needs als results
* for realibility estimation.
* Let's assume dark until the first ALS measurement is ready.
*/
chip->lux_data_raw = 0;
chip->prox_data = 0;
ret = i2c_smbus_write_byte_data(chip->client,
BH1770_ALS_CONTROL, BH1770_STANDALONE);
/* Assume reset defaults */
chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
chip->lux_thres_lo_onchip = 0;
return ret;
}
static void bh1770_chip_off(struct bh1770_chip *chip)
{
i2c_smbus_write_byte_data(chip->client,
BH1770_INTERRUPT, BH1770_DISABLE);
i2c_smbus_write_byte_data(chip->client,
BH1770_ALS_CONTROL, BH1770_STANDBY);
i2c_smbus_write_byte_data(chip->client,
BH1770_PS_CONTROL, BH1770_STANDBY);
regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
}
/* chip->mutex is kept when this is called */
static int bh1770_prox_mode_control(struct bh1770_chip *chip)
{
if (chip->prox_enable_count) {
chip->prox_force_update = true; /* Force immediate update */
bh1770_lux_rate(chip, chip->lux_rate_index);
bh1770_prox_set_threshold(chip);
bh1770_led_cfg(chip);
bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
i2c_smbus_write_byte_data(chip->client,
BH1770_PS_CONTROL, BH1770_STANDALONE);
} else {
chip->prox_data = 0;
bh1770_lux_rate(chip, chip->lux_rate_index);
bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
i2c_smbus_write_byte_data(chip->client,
BH1770_PS_CONTROL, BH1770_STANDBY);
}
return 0;
}
/* chip->mutex is kept when this is called */
static int bh1770_prox_read_result(struct bh1770_chip *chip)
{
int ret;
bool above;
u8 mode;
ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
if (ret < 0)
goto out;
if (ret > chip->prox_threshold_hw)
above = true;
else
above = false;
/*
* when ALS levels goes above limit, proximity result may be
* false proximity. Thus ignore the result. With real proximity
* there is a shadow causing low als levels.
*/
if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
ret = 0;
chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
/* Strong proximity level or force mode requires immediate response */
if (chip->prox_data >= chip->prox_abs_thres ||
chip->prox_force_update)
chip->prox_persistence_counter = chip->prox_persistence;
chip->prox_force_update = false;
/* Persistence filttering to reduce false proximity events */
if (likely(above)) {
if (chip->prox_persistence_counter < chip->prox_persistence) {
chip->prox_persistence_counter++;
ret = -ENODATA;
} else {
mode = PROX_ABOVE_THRESHOLD;
ret = 0;
}
} else {
chip->prox_persistence_counter = 0;
mode = PROX_BELOW_THRESHOLD;
chip->prox_data = 0;
ret = 0;
}
/* Set proximity detection rate based on above or below value */
if (ret == 0) {
bh1770_prox_rate(chip, mode);
sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
}
out:
return ret;
}
static int bh1770_detect(struct bh1770_chip *chip)
{
struct i2c_client *client = chip->client;
s32 ret;
u8 manu, part;
ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
if (ret < 0)
goto error;
manu = (u8)ret;
ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
if (ret < 0)
goto error;
part = (u8)ret;
chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
chip->prox_coef = BH1770_COEF_SCALER;
chip->prox_const = 0;
chip->lux_cf = BH1770_NEUTRAL_CF;
if ((manu == BH1770_MANUFACT_ROHM) &&
((part & BH1770_PART_MASK) == BH1770_PART)) {
snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
return 0;
}
if ((manu == BH1770_MANUFACT_OSRAM) &&
((part & BH1770_PART_MASK) == BH1770_PART)) {
snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
/* Values selected by comparing different versions */
chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */
chip->prox_const = 40;
return 0;
}
ret = -ENODEV;
error:
dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
return ret;
}
/*
* This work is re-scheduled at every proximity interrupt.
* If this work is running, it means that there hasn't been any
* proximity interrupt in time. Situation is handled as no-proximity.
* It would be nice to have low-threshold interrupt or interrupt
* when measurement and hi-threshold are both 0. But neither of those exists.
* This is a workaroud for missing HW feature.
*/
static void bh1770_prox_work(struct work_struct *work)
{
struct bh1770_chip *chip =
container_of(work, struct bh1770_chip, prox_work.work);
mutex_lock(&chip->mutex);
bh1770_prox_read_result(chip);
mutex_unlock(&chip->mutex);
}
/* This is threaded irq handler */
static irqreturn_t bh1770_irq(int irq, void *data)
{
struct bh1770_chip *chip = data;
int status;
int rate = 0;
mutex_lock(&chip->mutex);
status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
/* Acknowledge interrupt by reading this register */
i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
/*
* Check if there is fresh data available for als.
* If this is the very first data, update thresholds after that.
*/
if (status & BH1770_INT_ALS_DATA) {
bh1770_lux_get_result(chip);
if (unlikely(chip->lux_wait_result)) {
chip->lux_wait_result = false;
wake_up(&chip->wait);
bh1770_lux_update_thresholds(chip,
chip->lux_threshold_hi,
chip->lux_threshold_lo);
}
}
/* Disable interrupt logic to guarantee acknowledgement */
i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
(0 << 1) | (0 << 0));
if ((status & BH1770_INT_ALS_INT))
sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
rate = prox_rates_ms[chip->prox_rate_threshold];
bh1770_prox_read_result(chip);
}
/* Re-enable interrupt logic */
i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
(chip->int_mode_lux << 1) |
(chip->int_mode_prox << 0));