// SPDX-License-Identifier: GPL-2.0-only
/*
* RPR-0521 ROHM Ambient Light and Proximity Sensor
*
* Copyright (c) 2015, Intel Corporation.
*
* IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
*
* TODO: illuminance channel
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <linux/delay.h>
#include <linux/acpi.h>
#include <linux/iio/iio.h>
#include <linux/iio/buffer.h>
#include <linux/iio/trigger.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/iio/sysfs.h>
#include <linux/pm_runtime.h>
#define RPR0521_REG_SYSTEM_CTRL 0x40
#define RPR0521_REG_MODE_CTRL 0x41
#define RPR0521_REG_ALS_CTRL 0x42
#define RPR0521_REG_PXS_CTRL 0x43
#define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
#define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
#define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
#define RPR0521_REG_INTERRUPT 0x4A
#define RPR0521_REG_PS_OFFSET_LSB 0x53
#define RPR0521_REG_ID 0x92
#define RPR0521_MODE_ALS_MASK BIT(7)
#define RPR0521_MODE_PXS_MASK BIT(6)
#define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
#define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
#define RPR0521_ALS_DATA0_GAIN_SHIFT 4
#define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
#define RPR0521_ALS_DATA1_GAIN_SHIFT 2
#define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
#define RPR0521_PXS_GAIN_SHIFT 4
#define RPR0521_PXS_PERSISTENCE_MASK GENMASK(3, 0)
#define RPR0521_INTERRUPT_INT_TRIG_PS_MASK BIT(0)
#define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK BIT(1)
#define RPR0521_INTERRUPT_INT_REASSERT_MASK BIT(3)
#define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK BIT(6)
#define RPR0521_INTERRUPT_PS_INT_STATUS_MASK BIT(7)
#define RPR0521_MODE_ALS_ENABLE BIT(7)
#define RPR0521_MODE_ALS_DISABLE 0x00
#define RPR0521_MODE_PXS_ENABLE BIT(6)
#define RPR0521_MODE_PXS_DISABLE 0x00
#define RPR0521_PXS_PERSISTENCE_DRDY 0x00
#define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE BIT(0)
#define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE 0x00
#define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE BIT(1)
#define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE 0x00
#define RPR0521_INTERRUPT_INT_REASSERT_ENABLE BIT(3)
#define RPR0521_INTERRUPT_INT_REASSERT_DISABLE 0x00
#define RPR0521_MANUFACT_ID 0xE0
#define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
#define RPR0521_DRV_NAME "RPR0521"
#define RPR0521_IRQ_NAME "rpr0521_event"
#define RPR0521_REGMAP_NAME "rpr0521_regmap"
#define RPR0521_SLEEP_DELAY_MS 2000
#define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
#define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
struct rpr0521_gain {
int scale;
int uscale;
};
static const struct rpr0521_gain rpr0521_als_gain[4] = {
{1, 0}, /* x1 */
{0, 500000}, /* x2 */
{0, 15625}, /* x64 */
{0, 7812}, /* x128 */
};
static const struct rpr0521_gain rpr0521_pxs_gain[3] = {
{1, 0}, /* x1 */
{0, 500000}, /* x2 */
{0, 125000}, /* x4 */
};
enum rpr0521_channel {
RPR0521_CHAN_PXS,
RPR0521_CHAN_ALS_DATA0,
RPR0521_CHAN_ALS_DATA1,
};
struct rpr0521_reg_desc {
u8 address;
u8 device_mask;
};
static const struct rpr0521_reg_desc rpr0521_data_reg[] = {
[RPR0521_CHAN_PXS] = {
.address = RPR0521_REG_PXS_DATA,
.device_mask = RPR0521_MODE_PXS_MASK,
},
[RPR0521_CHAN_ALS_DATA0] = {
.address = RPR0521_REG_ALS_DATA0,
.device_mask = RPR0521_MODE_ALS_MASK,
},
[RPR0521_CHAN_ALS_DATA1] = {
.address = RPR0521_REG_ALS_DATA1,
.device_mask = RPR0521_MODE_ALS_MASK,
},
};
static const struct rpr0521_gain_info {
u8 reg;
u8 mask;
u8 shift;
const struct rpr0521_gain *gain;
int size;
} rpr0521_gain[] = {
[RPR0521_CHAN_PXS] = {
.reg = RPR0521_REG_PXS_CTRL,
.mask = RPR0521_PXS_GAIN_MASK,
.shift = RPR0521_PXS_GAIN_SHIFT,
.gain = rpr0521_pxs_gain,
.size = ARRAY_SIZE(rpr0521_pxs_gain),
},
[RPR0521_CHAN_ALS_DATA0] = {
.reg = RPR0521_REG_ALS_CTRL,
.mask = RPR0521_ALS_DATA0_GAIN_MASK,
.shift = RPR0521_ALS_DATA0_GAIN_SHIFT,
.gain = rpr0521_als_gain,
.size = ARRAY_SIZE(rpr0521_als_gain),
},
[RPR0521_CHAN_ALS_DATA1] = {
.reg = RPR0521_REG_ALS_CTRL,
.mask = RPR0521_ALS_DATA1_GAIN_MASK,
.shift = RPR0521_ALS_DATA1_GAIN_SHIFT,
.gain = rpr0521_als_gain,
.size = ARRAY_SIZE(rpr0521_als_gain),
},
};
struct rpr0521_samp_freq {
int als_hz;
int als_uhz;
int pxs_hz;
int pxs_uhz;
};
static const struct rpr0521_samp_freq rpr0521_samp_freq_i[13] = {
/* {ALS, PXS}, W==currently writable option */
{0, 0, 0, 0}, /* W0000, 0=standby */
{0, 0, 100, 0}, /* 0001 */
{0, 0, 25, 0}, /* 0010 */
{0, 0, 10, 0}, /* 0011 */
{0, 0, 2, 500000}, /* 0100 */
{10, 0, 20, 0}, /* 0101 */
{10, 0, 10, 0}, /* W0110 */
{10, 0, 2, 500000}, /* 0111 */
{2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
{2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
{2, 500000, 0, 0}, /* 1010, high sensitivity mode */
{2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
{20, 0,