blob: c8761c724daf7359208254290fa68509e6f73b94 [file] [log] [blame]
/*
* Copyright (C) 2010 Samsung Electronics. All rights reserved.
*
* 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/interrupt.h>
#include <linux/irq.h>
#include <linux/i2c.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/gpio.h>
#include <linux/wakelock.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/regulator/consumer.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/input.h>
#include <linux/sensor/sensors_core.h>
#include "gp2ap070s.h"
#define I2C_M_WR 0 /* for i2c Write */
#define DEFAULT_HIGH_THD 150
#define DEFAULT_LOW_THD 100
#define CANCEL_HIGH_THD 80
#define CANCEL_LOW_THD 50
#define DEFAULT_OFFSET 0
#define CHIP_DEV_NAME "GP2AP070S"
#define CHIP_DEV_VENDOR "SHARP"
#define MODULE_NAME "proximity_sensor"
/* Intelligent Cancelation*/
#define PROXIMITY_CANCELATION
#ifdef PROXIMITY_CANCELATION
#define CANCELATION_FILE_PATH "/efs/FactoryApp/prox_cal"
enum {
CAL_FAIL = 0,
CAL_CANCELATION,
CAL_SKIP,
};
#endif
#define PROX_READ_NUM 40
#undef PROXIMITY_FOR_TEST /* for HW to tune up */
enum {
PS_COM1 = 0,
PS_COM2,
PS_COM3,
PS_COM4,
PS_PS1,
PS_PS2,
PS_PS3,
PS_REG_NUM,
};
enum {
REG_ADDR = 0,
CMD,
};
static u16 ps_reg_init_setting[PS_REG_NUM][2] = {
{REG_COM1, COM1_SD},
{REG_COM2, COM2_INT_ALL_CLEAR},
{REG_COM3, COM3_INT_PULSE},
{REG_COM4, COM4_INTVAL33},
{REG_PS1, PS1_RES10},
{REG_PS2, (PS2_IS89 | PS2_SUM32)},
{REG_PS3, (PS3_PRST3 | PS3_TGINTEN_PS1 | PS3_TGIRDRON0)},
};
struct gp2a_data {
struct input_dev *proximity_input_dev;
struct device *dev;
struct i2c_client *i2c_client;
struct wake_lock prx_wake_lock;
struct hrtimer prox_timer;
struct workqueue_struct *prox_wq;
struct work_struct work_prox;
struct regulator *vdd;
struct regulator *vled;
ktime_t prox_poll_delay;
atomic_t prox_enable;
int ps_gpio;
int irq;
u8 detect;
u8 nondetect;
u16 prox_thd_high;
u16 prox_thd_low;
u16 prox_offset;
unsigned int prox_cal_result;
int avg[3];
int p_out;
int default_low_thd;
int default_high_thd;
int cal_skip_adc;
int prox_cancel_l;
int prox_cancel_h;
int default_trim;
int vdd_always_on; /* 1: vdd is always on, 0: enable only when proximity is on */
int vled_ldo; /*0: vled(anode) source regulator, other: get power by LDO control */
int regulator_divided; /* 1: vdd & vled uses divided circuit, 0: vdd & vled uses seperate circuit */
};
enum {
OFF = 0,
ON,
};
static int proximity_vdd_onoff(struct device *dev, bool onoff)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int ret;
SENSOR_INFO("%s\n", (onoff) ? "on" : "off");
if (!data->vdd) {
SENSOR_INFO("VDD get regulator\n");
data->vdd = devm_regulator_get(dev, "gp2a,vdd");
if (IS_ERR(data->vdd)) {
SENSOR_ERR("cannot get vdd\n");
data->vdd = NULL;
return -ENOMEM;
}
if (!regulator_get_voltage(data->vdd))
regulator_set_voltage(data->vdd, 2850000, 2850000);
}
if (onoff) {
if (regulator_is_enabled(data->vdd)) {
SENSOR_INFO("Regulator already enabled\n");
return 0;
}
ret = regulator_enable(data->vdd);
if (ret)
SENSOR_ERR("Failed to enable vdd.\n");
usleep_range(10000, 11000);
} else {
ret = regulator_disable(data->vdd);
if (ret)
SENSOR_ERR("Failed to disable vdd.\n");
}
SENSOR_INFO("end\n");
return 0;
}
static int proximity_vled_onoff(struct device *dev, bool onoff)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int ret;
SENSOR_INFO("%s, ldo:%d\n",
(onoff) ? "on" : "off", data->vled_ldo);
/* ldo control */
if (data->vled_ldo) {
gpio_set_value(data->vled_ldo, onoff);
return 0;
}
/* regulator control */
if (!data->vled) {
SENSOR_INFO("VLED get regulator\n");
data->vled = devm_regulator_get(dev, "gp2a,vled");
if (IS_ERR(data->vled)) {
SENSOR_ERR("cannot get vled\n");
data->vled = NULL;
return -ENOMEM;
}
}
if (onoff) {
if (regulator_is_enabled(data->vled)) {
SENSOR_INFO("Regulator already enabled\n");
return 0;
}
ret = regulator_enable(data->vled);
if (ret)
SENSOR_ERR("Failed to enable vled.\n");
usleep_range(10000, 11000);
} else {
ret = regulator_disable(data->vled);
if (ret)
SENSOR_ERR("Failed to disable vled.\n");
}
return 0;
}
int gp2a_i2c_read(struct i2c_client *client, u8 reg, int len, u8 *val)
{
u8 retry = 5;
int ret;
struct i2c_msg msgs[2];
if ((client == NULL) || (!client->adapter))
return -ENODEV;
/* send slave address & command */
msgs[0].addr = client->addr;
msgs[0].flags = I2C_M_WR;
msgs[0].len = 1;
msgs[0].buf = &reg;
/* read word data */
msgs[1].addr = client->addr;
msgs[1].flags = I2C_M_RD;
msgs[1].len = len;
msgs[1].buf = val;
while (retry--) {
ret = i2c_transfer(client->adapter, msgs, 2);
if (ret >= 0)
return ret;
}
SENSOR_ERR("i2c transfer error ret = %d\n", ret);
return ret;
}
static int gp2a_i2c_read_byte(struct i2c_client *client, u8 reg)
{
u8 value;
int ret;
ret = gp2a_i2c_read(client, reg, 1, &value);
if (ret < 0)
return ret;
return value;
}
int gp2a_i2c_write(struct i2c_client *client, u8 reg, int len, u8 *val)
{
int ret, index;
int retry = 5;
struct i2c_msg msg;
unsigned char data[11];
if ((client == NULL) || (!client->adapter))
return -ENODEV;
else if (len >= 10) {
SENSOR_ERR("length %d exceeds 10\n", len);
return -EINVAL;
}
data[0] = reg;
for (index = 1; index <= len; index++)
data[index] = val[index - 1];
msg.addr = client->addr;
msg.flags = I2C_M_WR;
msg.len = len + 1;
msg.buf = data;
while (retry--) {
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret >= 0)
return ret;
}
SENSOR_ERR("i2c transfer error ret= %d\n", ret);
return ret;
}
static int gp2a_i2c_write_byte(struct i2c_client *client, u8 reg, u8 value)
{
int ret;
ret = gp2a_i2c_write(client, reg, 1, &value);
return ret;
}
static uint32_t gp2a_get_proximity_adc(struct gp2a_data *data)
{
u8 value[2];
int ret;
ret = gp2a_i2c_read(data->i2c_client, REG_D0_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
return (value[0] | (value[1] << 8));
}
static void gp2a_set_mode(struct gp2a_data *data, u8 onoff)
{
int i, ret = 0;
SENSOR_INFO("onoff = %d\n", onoff);
if (onoff) {
/* enable settings */
for (i = 0; i < PS_REG_NUM; i++)
ret += gp2a_i2c_write_byte(data->i2c_client,
ps_reg_init_setting[i][REG_ADDR],
ps_reg_init_setting[i][CMD]);
/* PS mode */
ret += gp2a_i2c_write_byte(data->i2c_client, REG_COM1,
COM1_WAKEUP | COM1_PS);
} else {
/* disable settings */
ret = gp2a_i2c_write_byte(data->i2c_client, REG_COM1, COM1_SD);
}
if (ret < 0)
SENSOR_ERR("failed to set mode (%d)\n", ret);
}
static int32_t gp2a_set_data_offset(struct gp2a_data *data, u16 thd)
{
u8 val[2];
int ret;
val[0] = thd & 0x00FF;
val[1] = (thd & 0xFF00) >> 8;
ret = gp2a_i2c_write(data->i2c_client, REG_OS_D0_LSB, 2, val);
if (ret < 0)
SENSOR_ERR("set low thd failed. %d\n", ret);
else
data->prox_offset = thd;
SENSOR_INFO("offset = %d\n", data->prox_offset);
return ret;
}
static int32_t gp2a_set_threshold_low(struct gp2a_data *data, u16 thd)
{
u8 val[2];
int ret;
val[0] = thd & 0x00FF;
val[1] = (thd & 0xFF00) >> 8;
ret = gp2a_i2c_write(data->i2c_client, REG_PS_LT_LSB, 2, val);
if (ret < 0)
SENSOR_ERR("set low thd failed. %d\n", ret);
else
data->prox_thd_low = thd;
SENSOR_INFO("thd = %d\n", data->prox_thd_low);
return ret;
}
static int32_t gp2a_set_threshold_high(struct gp2a_data *data, u16 thd)
{
u8 val[2];
int ret;
val[0] = thd & 0x00FF;
val[1] = (thd & 0xFF00) >> 8;
ret = gp2a_i2c_write(data->i2c_client, REG_PS_HT_LSB, 2, val);
if (ret < 0)
SENSOR_ERR("set low thd failed. %d\n", ret);
else
data->prox_thd_high = thd;
SENSOR_INFO("thd = %d\n", data->prox_thd_high);
return ret;
}
static ssize_t name_read(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_NAME);
}
static ssize_t vendor_read(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_DEV_VENDOR);
}
static ssize_t proximity_dhr_sensor_info_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int ret;
u8 value[2];
int low_thresh, hi_thresh;
int ps_resolution, led_ctrl, persist_time, default_offset;
ret = gp2a_i2c_read(data->i2c_client, REG_PS_HT_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
hi_thresh = value[0] | (value[1] << 8);
ret = gp2a_i2c_read(data->i2c_client, REG_PS_LT_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
low_thresh = value[0] | (value[1] << 8);
ps_resolution = gp2a_i2c_read_byte(data->i2c_client, REG_PS1);
led_ctrl = gp2a_i2c_read_byte(data->i2c_client, REG_PS2);
persist_time = gp2a_i2c_read_byte(data->i2c_client, REG_PS3);
ret = gp2a_i2c_read(data->i2c_client, REG_OS_D0_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
default_offset = value[0] | (value[1] << 8);
return snprintf(buf, PAGE_SIZE,
"\"THD\":\"%d %d\","\
"\"PS_RESOLUTION\":\"0x%x\","\
"\"LED_CTRL\":\"0x%x\","\
"\"PERSIST_TIME\":\"0x%x\","\
"\"DEFAULT_OFFSET\":\"%d\","\
"\"CANCEL_THD\":\"%d %d\"\n",
hi_thresh, low_thresh,
ps_resolution,
led_ctrl,
persist_time,
default_offset,
data->prox_cancel_h, data->prox_cancel_l);
}
#if defined(PROXIMITY_CANCELATION)
static int proximity_open_cancelation(struct gp2a_data *data)
{
struct file *cal_filp = NULL;
mm_segment_t old_fs;
uint16_t file_offset_data;
int ret;
old_fs = get_fs();
set_fs(KERNEL_DS);
cal_filp = filp_open(CANCELATION_FILE_PATH, O_RDONLY, 0);
if (IS_ERR(cal_filp)) {
ret = PTR_ERR(cal_filp);
if (ret != -ENOENT)
SENSOR_ERR("Can't open calibration file\n");
set_fs(old_fs);
return ret;
}
ret = vfs_read(cal_filp,
(char *)&file_offset_data,
sizeof(u16), &cal_filp->f_pos);
if (ret != sizeof(u16)) {
SENSOR_ERR("Can't read the cal data from file(%d)\n", ret);
ret = -EIO;
}
if (file_offset_data != data->default_trim) {
data->prox_offset = file_offset_data;
gp2a_set_threshold_high(data, data->prox_cancel_h);
gp2a_set_threshold_low(data, data->prox_cancel_l);
}
SENSOR_INFO("file_offset = %d, ps_offset = %d, default_trim = %d\n",
file_offset_data, data->prox_offset,
data->default_trim);
filp_close(cal_filp, current->files);
set_fs(old_fs);
return ret;
}
static int proximity_store_cancelation(struct device *dev, bool do_calib)
{
struct gp2a_data *data = dev_get_drvdata(dev);
struct file *cal_filp = NULL;
mm_segment_t old_fs;
u8 value[2];
u16 ps_data = 0;
int ret;
if (do_calib) {
SENSOR_INFO("start\n");
ret = gp2a_i2c_read(data->i2c_client, REG_D0_LSB, 2,
&value[0]);
if (ret < 0) {
SENSOR_ERR("read adc fail, ret=%d\n", ret);
return ret;
}
ps_data = (value[0] | (value[1] << 8));
SENSOR_INFO("raw data = %d\n", ps_data);
if (ps_data < data->cal_skip_adc) {
data->prox_offset = data->default_trim;
SENSOR_INFO("skip calibration = %d, crosstalk <\n",
ps_data);
data->prox_cal_result = CAL_SKIP;
} else if (ps_data <= data->default_high_thd) {
data->prox_offset = ps_data + data->default_trim;
SENSOR_INFO("do calibration, crosstalk_offset = %u", ps_data);
data->prox_cal_result = CAL_CANCELATION;
} else {
data->prox_offset = data->default_trim;
SENSOR_INFO("fail calibration = %d, crosstalk >\n",
ps_data);
data->prox_cal_result = CAL_FAIL;
}
if (data->prox_cal_result == CAL_CANCELATION) {
data->prox_thd_high = data->prox_cancel_h;
data->prox_thd_low = data->prox_cancel_l;
} else {
data->prox_thd_high = data->default_high_thd;
data->prox_thd_low = data->default_low_thd;
}
} else { /*reset*/
SENSOR_INFO("reset\n");
data->prox_offset = data->default_trim;
data->prox_thd_high = data->default_high_thd;
data->prox_thd_low = data->default_low_thd;
}
if ((data->prox_cal_result == CAL_CANCELATION) || !do_calib) {
ret = gp2a_set_data_offset(data, data->prox_offset);
if (ret < 0)
SENSOR_ERR("fail : set proximity offset(%d)\n", ret);
ret = gp2a_set_threshold_high(data, data->prox_thd_high);
if (ret < 0)
SENSOR_ERR("fail : set proximity high thd(%d)\n", ret);
ret = gp2a_set_threshold_low(data, data->prox_thd_low);
if (ret < 0)
SENSOR_ERR("fail : set proximity low thd(%d)\n", ret);
}
SENSOR_INFO("prox_offset = 0x%x, high_thd = 0x%x, low_thd = 0x%x\n",
data->prox_offset,
data->prox_thd_high,
data->prox_thd_low);
old_fs = get_fs();
set_fs(KERNEL_DS);
cal_filp = filp_open(CANCELATION_FILE_PATH,
O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0660);
if (IS_ERR(cal_filp)) {
SENSOR_ERR("Can't open calibration file\n");
set_fs(old_fs);
ret = PTR_ERR(cal_filp);
return ret;
}
ret = vfs_write(cal_filp,
(char *)&data->prox_offset,
sizeof(u16), &cal_filp->f_pos);
if (ret != sizeof(u16)) {
SENSOR_ERR("Can't write the cancel data to file\n");
ret = -EIO;
}
filp_close(cal_filp, current->files);
set_fs(old_fs);
if (!do_calib) /* delay for clearing */
msleep(150);
return ret;
}
static ssize_t proximity_cancel_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
bool do_calib;
int err;
if (sysfs_streq(buf, "1")) /* calibrate cancelation value */
do_calib = true;
else if (sysfs_streq(buf, "0")) /* reset cancelation value */
do_calib = false;
else {
SENSOR_ERR("invalid value %d\n", *buf);
return size;
}
err = proximity_store_cancelation(dev, do_calib);
if (err < 0) {
SENSOR_ERR("proximity_store_cancelation() failed(%d)\n", err);
return size;
}
return size;
}
static ssize_t proximity_cancel_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u,%u,%u\n",
data->prox_offset,
(data->prox_offset != data->default_trim) ? data->prox_cancel_h : data->prox_thd_high,
(data->prox_offset != data->default_trim) ? data->prox_cancel_l : data->prox_thd_low);
}
static ssize_t proximity_cancel_pass_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
SENSOR_INFO("%u\n", data->prox_cal_result);
return snprintf(buf, PAGE_SIZE, "%u\n", data->prox_cal_result);
}
#endif
#if defined(PROXIMITY_FOR_TEST)
static ssize_t proximity_register_write_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
unsigned int regist = 0, val = 0;
struct gp2a_data *data = dev_get_drvdata(dev);
if (sscanf(buf, "%2x,%2x", &regist, &val) != 2) {
SENSOR_ERR("The number of data are wrong\n");
return count;
}
gp2a_i2c_write_byte(data->i2c_client, regist, val);
SENSOR_INFO("Register(0x%2x) 8:data(0x%2x) 10:%d\n",
regist, val, val);
return count;
}
static ssize_t proximity_register_read_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
u8 val[PS_REG_NUM], i;
struct gp2a_data *data = dev_get_drvdata(dev);
for (i = 0; i < PS_REG_NUM; i++) {
val[i] = gp2a_i2c_read_byte(data->i2c_client,
ps_reg_init_setting[i][REG_ADDR]);
SENSOR_INFO("Register(0x%2x) data(0x%2x)\n",
ps_reg_init_setting[i][REG_ADDR], val[i]);
}
return snprintf(buf, PAGE_SIZE, "0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x\n",
val[0], val[1], val[2], val[3], val[4], val[5], val[6]);
}
#endif
static void proximity_get_avg_val(struct gp2a_data *data)
{
int min = 0, max = 0, avg = 0;
int i;
u16 ps_data;
for (i = 0; i < PROX_READ_NUM; i++) {
msleep(40);
ps_data = gp2a_get_proximity_adc(data);
avg += ps_data;
if (!i)
min = ps_data;
else if (ps_data < min)
min = ps_data;
if (ps_data > max)
max = ps_data;
}
avg /= PROX_READ_NUM;
data->avg[0] = min;
data->avg[1] = avg;
data->avg[2] = max;
}
static ssize_t proximity_avg_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", data->avg[0],
data->avg[1], data->avg[2]);
}
static ssize_t proximity_avg_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct gp2a_data *data = dev_get_drvdata(dev);
bool new_value = false;
if (sysfs_streq(buf, "1"))
new_value = true;
else if (sysfs_streq(buf, "0"))
new_value = false;
else {
SENSOR_ERR("invalid value %d\n", *buf);
return size;
}
SENSOR_INFO("average enable = %d\n", new_value);
if (new_value) {
if (atomic_read(&data->prox_enable) == OFF) {
if (!data->vdd_always_on)
proximity_vdd_onoff(dev, ON);
if (!data->regulator_divided)
proximity_vled_onoff(dev, ON);
gp2a_set_mode(data, ON);
}
hrtimer_start(&data->prox_timer, data->prox_poll_delay,
HRTIMER_MODE_REL);
} else if (!new_value) {
hrtimer_cancel(&data->prox_timer);
cancel_work_sync(&data->work_prox);
if (atomic_read(&data->prox_enable) == OFF) {
gp2a_set_mode(data, OFF);
if (!data->regulator_divided)
proximity_vled_onoff(dev, OFF);
if (!data->vdd_always_on)
proximity_vdd_onoff(dev, OFF);
}
}
return size;
}
static ssize_t proximity_state_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
uint32_t ps_data;
ps_data = gp2a_get_proximity_adc(data);
return snprintf(buf, PAGE_SIZE, "%d\n", ps_data);
}
static ssize_t proximity_thresh_high_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int ret;
u8 value[2];
ret = gp2a_i2c_read(data->i2c_client, REG_PS_HT_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
return snprintf(buf, PAGE_SIZE, "%d\n", (value[0] | (value[1] << 8)));
}
static ssize_t proximity_thresh_high_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct gp2a_data *data = dev_get_drvdata(dev);
u16 thresh_value;
int err;
err = kstrtou16(buf, 10, &thresh_value);
if (err < 0) {
SENSOR_ERR("kstrtoint failed(%d)\n", err);
return size;
}
if (thresh_value > 2) {
gp2a_set_threshold_high(data, thresh_value);
SENSOR_INFO("new high threshold = %d\n",
data->prox_thd_high);
msleep(150);
} else
SENSOR_ERR("wrong high threshold value(%d)\n", thresh_value);
return size;
}
static ssize_t proximity_thresh_low_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int ret;
u8 value[2];
ret = gp2a_i2c_read(data->i2c_client, REG_PS_LT_LSB, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
return snprintf(buf, PAGE_SIZE, "%d\n", (value[0] | (value[1] << 8)));
}
static ssize_t proximity_thresh_low_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct gp2a_data *data = dev_get_drvdata(dev);
u16 thresh_value;
int err;
err = kstrtou16(buf, 10, &thresh_value);
if (err < 0)
SENSOR_ERR("kstrtoint failed\n");
if (thresh_value > 2) {
gp2a_set_threshold_low(data, thresh_value);
SENSOR_INFO("new low threshold = %d\n",
data->prox_thd_low);
msleep(150);
} else
SENSOR_ERR("wrong low threshold value(%d)\n", thresh_value);
return size;
}
static DEVICE_ATTR(name, S_IRUGO, name_read, NULL);
static DEVICE_ATTR(vendor, S_IRUGO, vendor_read, NULL);
#if defined(PROXIMITY_CANCELATION)
static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_cancel_show, proximity_cancel_store);
static DEVICE_ATTR(prox_offset_pass, S_IRUGO, proximity_cancel_pass_show,
NULL);
#endif
#if defined(PROXIMITY_FOR_TEST)
static DEVICE_ATTR(prox_register, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_register_read_show, proximity_register_write_store);
#endif
static DEVICE_ATTR(prox_avg, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_avg_show, proximity_avg_store);
static DEVICE_ATTR(raw_data, S_IRUGO, proximity_state_show, NULL);
static DEVICE_ATTR(state, S_IRUGO, proximity_state_show, NULL);
static DEVICE_ATTR(thresh_high, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_thresh_high_show, proximity_thresh_high_store);
static DEVICE_ATTR(thresh_low, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_thresh_low_show, proximity_thresh_low_store);
static DEVICE_ATTR(dhr_sensor_info, S_IRUSR | S_IRGRP,
proximity_dhr_sensor_info_show, NULL);
static struct device_attribute *proximity_attrs[] = {
&dev_attr_name,
&dev_attr_vendor,
#if defined(PROXIMITY_CANCELATION)
&dev_attr_prox_cal,
&dev_attr_prox_offset_pass,
#endif
#if defined(PROXIMITY_FOR_TEST)
&dev_attr_prox_register,
#endif
&dev_attr_prox_avg,
&dev_attr_raw_data,
&dev_attr_state,
&dev_attr_thresh_high,
&dev_attr_thresh_low,
&dev_attr_dhr_sensor_info,
NULL,
};
static ssize_t proximity_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gp2a_data *gp2a = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n",
atomic_read(&gp2a->prox_enable));
}
static ssize_t proximity_enable_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct gp2a_data *data = dev_get_drvdata(dev);
bool new_value;
int pre_enable;
if (sysfs_streq(buf, "1"))
new_value = true;
else if (sysfs_streq(buf, "0"))
new_value = false;
else {
SENSOR_ERR("invalid value %d\n", *buf);
return size;
}
pre_enable = atomic_read(&data->prox_enable);
SENSOR_INFO("new_value = %d, pre_enable = %d\n",
new_value, pre_enable);
if (new_value && !pre_enable) {
#if defined(PROXIMITY_CANCELATION)
int ret;
#endif
if (!data->vdd_always_on)
proximity_vdd_onoff(dev, ON);
if (!data->regulator_divided)
proximity_vled_onoff(dev, ON);
#if defined(PROXIMITY_CANCELATION)
/* open cancelation data */
ret = proximity_open_cancelation(data);
if (ret < 0 && ret != -ENOENT)
SENSOR_INFO("proximity_open_cancelation() failed\n");
ret = gp2a_set_data_offset(data, data->prox_offset);
if (ret < 0)
SENSOR_ERR("fail : set proximity offset(%d)\n", ret);
#endif
gp2a_set_mode(data, ON);
atomic_set(&data->prox_enable, ON);
/* 0 is close, 1 is far */
input_report_abs(data->proximity_input_dev, ABS_DISTANCE, 1);
input_sync(data->proximity_input_dev);
enable_irq_wake(data->irq);
msleep(200);
enable_irq(data->irq);
} else if (!new_value && pre_enable) {
disable_irq(data->irq);
disable_irq_wake(data->irq);
gp2a_set_mode(data, OFF);
atomic_set(&data->prox_enable, OFF);
if (!data->regulator_divided)
proximity_vled_onoff(dev, OFF);
if (!data->vdd_always_on)
proximity_vdd_onoff(dev, OFF);
}
SENSOR_INFO("enabled = %d\n", atomic_read(&data->prox_enable));
return size;
}
static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_enable_show, proximity_enable_store);
static struct attribute *proximity_sysfs_attrs[] = {
&dev_attr_enable.attr,
NULL
};
static struct attribute_group proximity_attribute_group = {
.attrs = proximity_sysfs_attrs,
};
static void gp2a_work_func_prox(struct work_struct *work)
{
struct gp2a_data *data = container_of(work,
struct gp2a_data, work_prox);
proximity_get_avg_val(data);
}
static enum hrtimer_restart gp2a_prox_timer_func(struct hrtimer *timer)
{
struct gp2a_data *data = container_of(timer,
struct gp2a_data, prox_timer);
queue_work(data->prox_wq, &data->work_prox);
hrtimer_forward_now(&data->prox_timer, data->prox_poll_delay);
return HRTIMER_RESTART;
}
/* interrupt happened due to transition/change of near/far proximity state */
irqreturn_t proximity_irq_thread_fn(int irq, void *data)
{
struct gp2a_data *gp2a = data;
u8 val;
u16 ps_data;
int enabled;
enabled = atomic_read(&gp2a->prox_enable);
val = gpio_get_value(gp2a->p_out);
ps_data = gp2a_get_proximity_adc(gp2a);
if (enabled) {
#ifdef CONFIG_SEC_FACTORY
SENSOR_INFO("FACTORY: near/far=%d, ps data = %d\n",
val, ps_data);
#else
SENSOR_INFO("near/far=%d, ps data = %d\n",
val, ps_data);
if (((!val) && (ps_data >= gp2a->prox_thd_high)) ||
(val && (ps_data <= gp2a->prox_thd_low)))
#endif
{
/* 0 is close, 1 is far */
input_report_abs(gp2a->proximity_input_dev, ABS_DISTANCE,
val);
input_sync(gp2a->proximity_input_dev);
}
}
wake_lock_timeout(&gp2a->prx_wake_lock, 3 * HZ);
return IRQ_HANDLED;
}
static int setup_register_gp2a(struct gp2a_data *data)
{
int ret, i;
/* PS initialization */
for (i = 0; i < PS_REG_NUM; i++) {
ret = gp2a_i2c_write_byte(data->i2c_client,
ps_reg_init_setting[i][REG_ADDR],
ps_reg_init_setting[i][CMD]);
if (ret < 0) {
SENSOR_ERR("failed. %d\n", ret);
return ret;
}
}
/* SET threshold */
ret = gp2a_set_threshold_low(data, data->default_low_thd);
ret += gp2a_set_threshold_high(data, data->default_high_thd);
if (ret < 0)
SENSOR_ERR("set thd failed. %d\n", ret);
/* SET data OFFSET(0x8C) */
ret = gp2a_set_data_offset(data, data->default_trim);
if (ret < 0)
SENSOR_ERR("set data offset failed. %d\n", ret);
return ret;
}
static int gp2a_setup_irq(struct gp2a_data *gp2a)
{
int ret;
ret = gpio_request(gp2a->p_out, "gpio_proximity_out");
if (ret < 0) {
SENSOR_ERR("gpio %d request failed (%d)\n", gp2a->p_out, ret);
return ret;
}
ret = gpio_direction_input(gp2a->p_out);
if (ret < 0) {
SENSOR_ERR("failed gpio %d as input (%d)\n", gp2a->p_out, ret);
goto err_gpio_direction_input;
}
gp2a->irq = gpio_to_irq(gp2a->p_out);
ret = request_threaded_irq(gp2a->irq, NULL, proximity_irq_thread_fn,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
"proximity_int", gp2a);
if (ret < 0) {
SENSOR_ERR("request_irq(%d) failed for gpio %d (%d)\n",
gp2a->irq, gp2a->p_out, ret);
goto err_request_irq;
}
SENSOR_INFO("request_irq(%d) success for gpio %d (%d)\n",
gp2a->irq, gp2a->ps_gpio, gp2a->p_out);
disable_irq(gp2a->irq);
goto done;
err_request_irq:
err_gpio_direction_input:
gpio_free(gp2a->p_out);
done:
return ret;
}
static int gp2a_input_init(struct gp2a_data *gp2a)
{
int ret;
struct input_dev *dev;
/* Create the input device */
dev = input_allocate_device();
if (!dev)
return -ENOMEM;
dev->name = MODULE_NAME;
dev->id.bustype = BUS_I2C;
input_set_drvdata(dev, gp2a);
input_set_capability(dev, EV_ABS, ABS_DISTANCE);
input_set_abs_params(dev, ABS_DISTANCE, 0, 1, 0, 0);
ret = input_register_device(dev);
if (ret < 0) {
SENSOR_ERR("could not register input device\n");
input_free_device(dev);
return ret;
}
ret = sensors_create_symlink(&dev->dev.kobj, dev->name);
if (ret < 0) {
SENSOR_ERR("create sysfs symlink error\n");
input_unregister_device(dev);
return ret;
}
ret = sysfs_create_group(&dev->dev.kobj, &proximity_attribute_group);
if (ret < 0) {
SENSOR_ERR("create sysfs group error\n");
sensors_remove_symlink(&dev->dev.kobj, dev->name);
input_unregister_device(dev);
return ret;
}
/* save the input pointer and finish initialization */
gp2a->proximity_input_dev = dev;
return ret;
}
static int gp2a_parse_dt(struct device *dev, struct gp2a_data *gp2a)
{
struct device_node *np = dev->of_node;
enum of_gpio_flags flags;
int ret;
u32 temp;
if (np == NULL)
return -ENODEV;
gp2a->p_out = of_get_named_gpio_flags(np, "gp2a,irq-gpio", 0,
&flags);
if (gp2a->p_out < 0) {
SENSOR_ERR("get irq_gpio(%d) error\n", gp2a->p_out);
return -ENODEV;
}
ret = of_property_read_u32(np, "gp2a,default_high_thd",
&gp2a->default_high_thd);
if (ret < 0) {
SENSOR_ERR("Cannot set default_high_thd\n");
gp2a->default_high_thd = DEFAULT_HIGH_THD;
}
ret = of_property_read_u32(np, "gp2a,default_low_thd",
&gp2a->default_low_thd);
if (ret < 0) {
SENSOR_ERR("Cannot set default_low_thd\n");
gp2a->default_low_thd = DEFAULT_LOW_THD;
}
ret = of_property_read_u32(np, "gp2a,cal_skip_adc",
&gp2a->cal_skip_adc);
if (ret < 0) {
SENSOR_ERR("Cannot set cal_skip_adc\n");
gp2a->cal_skip_adc = (gp2a->default_low_thd * 6) / 10;
}
ret = of_property_read_u32(np, "gp2a,cancel_high_thd",
&gp2a->prox_cancel_h);
if (ret < 0) {
SENSOR_ERR("Cannot set cancel_high_thd\n");
gp2a->prox_cancel_h = CANCEL_HIGH_THD;
}
ret = of_property_read_u32(np, "gp2a,cancel_low_thd",
&gp2a->prox_cancel_l);
if (ret < 0) {
SENSOR_ERR("Cannot set cancel_low_thd\n");
gp2a->prox_cancel_l = CANCEL_LOW_THD;
}
ret = of_property_read_u32(np, "gp2a,default_offset",
&gp2a->default_trim);
if (ret < 0) {
SENSOR_ERR("Cannot set default_trim\n");
gp2a->default_trim = DEFAULT_OFFSET;
}
ret = of_property_read_u32(np, "gp2a,reg_intval", &temp);
if (ret < 0) {
SENSOR_ERR("Cannot set reg_intval(0x83)\n");
ps_reg_init_setting[PS_COM4][CMD] = COM4_INTVAL33;
} else
ps_reg_init_setting[PS_COM4][CMD] = temp;
ret = of_property_read_u32(np, "gp2a,reg_res_p", &temp);
if (ret < 0) {
SENSOR_ERR("Cannot set reg_res_p(0x85)\n");
ps_reg_init_setting[PS_PS1][CMD] = PS1_RES14;
} else
ps_reg_init_setting[PS_PS1][CMD] = temp;
ret = of_property_read_u32(np, "gp2a,reg_ledctrl", &temp);
if (ret < 0) {
SENSOR_ERR("Cannot set reg_ledctrl(0x86)\n");
ps_reg_init_setting[PS_PS2][CMD] = (PS2_IS130 | PS2_SUM32);
} else
ps_reg_init_setting[PS_PS2][CMD] = temp;
ret = of_property_read_u32(np, "gp2a,reg_prst", &temp);
if (ret < 0) {
SENSOR_ERR("Cannot set reg_prst(0x87)\n");
ps_reg_init_setting[PS_PS3][CMD] = (PS3_PRST3 | PS3_TGINTEN_PS1 | PS3_TGIRDRON0);
} else
ps_reg_init_setting[PS_PS3][CMD] = temp;
gp2a->vled_ldo = of_get_named_gpio_flags(np, "gp2a,vled_ldo",
0, &flags);
if (gp2a->vled_ldo < 0) {
SENSOR_ERR("fail to get vled_ldo: means to use regulator as vLED\n");
gp2a->vled_ldo = 0;
} else {
ret = gpio_request(gp2a->vled_ldo, "prox_vled_en");
if (ret < 0) {
SENSOR_ERR("gpio %d request failed (%d)\n",
gp2a->vled_ldo, ret);
return ret;
}
gpio_direction_output(gp2a->vled_ldo, 0);
}
ret = of_property_read_u32(np, "gp2a,regulator_divided",
&gp2a->regulator_divided);
ret = of_property_read_u32(np, "gp2a,vdd_always_on",
&gp2a->vdd_always_on);
SENSOR_INFO("vdd_alwayson_on: %d, regulator_divided: %d, vled_ldo: %d\n",
gp2a->vdd_always_on, gp2a->regulator_divided,
gp2a->vled_ldo);
SENSOR_INFO("initial register 0x83 = 0x%x, 0x85 = 0x%x, 0x86 = 0x%x, 0x87 = 0x%x",
ps_reg_init_setting[PS_COM4][CMD],
ps_reg_init_setting[PS_PS1][CMD],
ps_reg_init_setting[PS_PS2][CMD],
ps_reg_init_setting[PS_PS3][CMD]);
return 0;
}
static int gp2a_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct gp2a_data *gp2a;
int ret;
SENSOR_INFO("start\n");
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
SENSOR_ERR("i2c functionality failed\n");
return -ENOMEM;
}
/* Allocate memory for driver data */
gp2a = kzalloc(sizeof(struct gp2a_data), GFP_KERNEL);
if (!gp2a) {
SENSOR_ERR("failed memory alloc\n");
ret = -ENOMEM;
goto err_mem_alloc;
}
ret = gp2a_parse_dt(&client->dev, gp2a);
if (ret) {
SENSOR_ERR("error in device tree");
goto err_device_tree;
}
gp2a->i2c_client = client;
i2c_set_clientdata(client, gp2a);
proximity_vdd_onoff(&client->dev, ON);
if (!gp2a->regulator_divided)
proximity_vled_onoff(&client->dev, ON);
usleep_range(1000, 1100);
/* Check if the device is there or not. (Shutdown operation) */
ret = gp2a_i2c_write_byte(client, REG_COM1, COM1_SD);
if (ret < 0) {
SENSOR_ERR("gp2a is not connected.(%d)\n", ret);
goto err_check_device;
}
/* setup initial registers */
ret = setup_register_gp2a(gp2a);
if (ret < 0) {
SENSOR_ERR("could not setup regs\n");
goto err_setup_register;
}
ret = gp2a_input_init(gp2a);
if (ret < 0) {
SENSOR_ERR("failed to get input dev\n");
goto err_input_init;
}
ret = sensors_register(&gp2a->dev, gp2a, proximity_attrs, MODULE_NAME);
if (ret < 0) {
SENSOR_INFO("could not sensors_register\n");
goto err_sensors_register;
}
wake_lock_init(&gp2a->prx_wake_lock, WAKE_LOCK_SUSPEND,
"prx_wake_lock");
/* For factory test mode, we use timer to get average proximity data. */
/* prox_timer settings. we poll for light values using a timer. */
hrtimer_init(&gp2a->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
gp2a->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
gp2a->prox_timer.function = gp2a_prox_timer_func;
/* the timer just fires off a work queue request. we need a thread
to read the i2c (can be slow and blocking). */
gp2a->prox_wq = create_singlethread_workqueue("gp2a_prox_wq");
if (!gp2a->prox_wq) {
ret = -ENOMEM;
SENSOR_ERR("could not create prox workqueue\n");
goto err_create_prox_workqueue;
}
/* this is the thread function we run on the work queue */
INIT_WORK(&gp2a->work_prox, gp2a_work_func_prox);
ret = gp2a_setup_irq(gp2a);
if (ret) {
SENSOR_ERR("could not setup irq\n");
goto err_setup_irq;
}
if (!gp2a->regulator_divided)
proximity_vled_onoff(&client->dev, OFF);
if (!gp2a->vdd_always_on)
proximity_vdd_onoff(&client->dev, OFF);
SENSOR_INFO("success\n");
return ret;
err_setup_irq:
destroy_workqueue(gp2a->prox_wq);
err_create_prox_workqueue:
wake_lock_destroy(&gp2a->prx_wake_lock);
sensors_unregister(gp2a->dev, proximity_attrs);
err_sensors_register:
sensors_remove_symlink(&gp2a->proximity_input_dev->dev.kobj,
gp2a->proximity_input_dev->name);
sysfs_remove_group(&gp2a->proximity_input_dev->dev.kobj,
&proximity_attribute_group);
input_unregister_device(gp2a->proximity_input_dev);
err_input_init:
err_setup_register:
err_check_device:
if (gp2a->vled_ldo)
gpio_free(gp2a->vled_ldo);
if (!gp2a->regulator_divided)
proximity_vled_onoff(&client->dev, OFF);
proximity_vdd_onoff(&client->dev, OFF);
err_device_tree:
kfree(gp2a);
err_mem_alloc:
SENSOR_ERR("failed\n");
return ret;
}
static int gp2a_suspend(struct device *dev)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int enable;
SENSOR_INFO("is called.\n");
enable = atomic_read(&data->prox_enable);
if (enable)
disable_irq(data->irq);
return 0;
}
static int gp2a_resume(struct device *dev)
{
struct gp2a_data *data = dev_get_drvdata(dev);
int enable;
SENSOR_INFO("is called.\n");
enable = atomic_read(&data->prox_enable);
if (enable)
enable_irq(data->irq);
return 0;
}
static const struct dev_pm_ops gp2a_pm_ops = {
.suspend = gp2a_suspend,
.resume = gp2a_resume
};
static const struct i2c_device_id gp2a_device_id[] = {
{"gp2a", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, gp2a_device_id);
static struct of_device_id gp2a_i2c_match_table[] = {
{ .compatible = "gp2a-i2c",},
{},
};
MODULE_DEVICE_TABLE(of, gp2a_i2c_match_table);
static struct i2c_driver gp2a_i2c_driver = {
.driver = {
.name = "gp2a",
.owner = THIS_MODULE,
.of_match_table = gp2a_i2c_match_table,
.pm = &gp2a_pm_ops
},
.probe = gp2a_i2c_probe,
.id_table = gp2a_device_id,
};
module_i2c_driver(gp2a_i2c_driver);
MODULE_AUTHOR("Samsung Electronics");
MODULE_DESCRIPTION("Proximity Sensor driver for gp2ap070s");
MODULE_LICENSE("GPL");