blob: 936cc59fa4ada0057cfa94c40c83471cb119010a [file] [log] [blame]
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/errno.h>
#include <linux/wakelock.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/regulator/consumer.h>
#include <linux/of_gpio.h>
#include <linux/sensor/sensors_core.h>
#include "stk3013.h"
#define DRIVER_VERSION "3.10.0_ps_only_20150508"
/* Driver Settings */
#define STK_INT_PS_MODE 1 /* 1, 2, or 3 */
#undef STK_CHK_REG
#define PROX_READ_NUM 40
/* Define Register Map */
#define STK_STATE_REG 0x00
#define STK_PSCTRL_REG 0x01
#define STK_LEDCTRL_REG 0x03
#define STK_INT_REG 0x04
#define STK_WAIT_REG 0x05
#define STK_THDH1_PS_REG 0x06
#define STK_THDH2_PS_REG 0x07
#define STK_THDL1_PS_REG 0x08
#define STK_THDL2_PS_REG 0x09
#define STK_FLAG_REG 0x10
#define STK_DATA1_PS_REG 0x11
#define STK_DATA2_PS_REG 0x12
#define STK_DATA1_OFFSET_REG 0x15
#define STK_DATA2_OFFSET_REG 0x16
#define STK_DATA1_IR_REG 0x17
#define STK_DATA2_IR_REG 0x18
#define STK_PDT_ID_REG 0x3E
#define STK_RSRVD_REG 0x3F
#define STK_SW_RESET_REG 0x80
#define STK_GSCTRL_REG 0x1A
#define STK_FLAG2_REG 0x1C
/* Define state reg */
#define STK_STATE_EN_IRS_SHIFT 7
#define STK_STATE_EN_AK_SHIFT 6
#define STK_STATE_EN_ASO_SHIFT 5
#define STK_STATE_EN_IRO_SHIFT 4
#define STK_STATE_EN_WAIT_SHIFT 2
#define STK_STATE_EN_PS_SHIFT 0
#define STK_STATE_EN_IRS_MASK 0x80
#define STK_STATE_EN_AK_MASK 0x40
#define STK_STATE_EN_ASO_MASK 0x20
#define STK_STATE_EN_IRO_MASK 0x10
#define STK_STATE_EN_WAIT_MASK 0x04
#define STK_STATE_EN_PS_MASK 0x01
/* Define PS ctrl reg */
#define STK_PS_PRS_SHIFT 6
#define STK_PS_GAIN_SHIFT 4
#define STK_PS_IT_SHIFT 0
#define STK_PS_PRS_MASK 0xC0
#define STK_PS_GAIN_MASK 0x30
#define STK_PS_IT_MASK 0x0F
/* Define LED ctrl reg */
#define STK_LED_IRDR_SHIFT 6
#define STK_LED_DT_SHIFT 0
#define STK_LED_IRDR_MASK 0xC0
#define STK_LED_DT_MASK 0x3F
/* Define interrupt reg */
#define STK_INT_CTRL_SHIFT 7
#define STK_INT_OUI_SHIFT 4
#define STK_INT_PS_SHIFT 0
#define STK_INT_CTRL_MASK 0x80
#define STK_INT_OUI_MASK 0x10
#define STK_INT_PS_MASK 0x07
/* Define flag reg */
#define STK_FLG_PSDR_SHIFT 6
#define STK_FLG_PSINT_SHIFT 4
#define STK_FLG_OUI_SHIFT 2
#define STK_FLG_IR_RDY_SHIFT 1
#define STK_FLG_NF_SHIFT 0
#define STK_FLG_PSDR_MASK 0x40
#define STK_FLG_PSINT_MASK 0x10
#define STK_FLG_OUI_MASK 0x04
#define STK_FLG_IR_RDY_MASK 0x02
#define STK_FLG_NF_MASK 0x01
#define VENDOR "SENSORTEK"
#define CHIP_ID "STK3013"
#define MODULE_NAME "proximity_sensor"
#define STK3310SA_PID 0x17
#define STK3311SA_PID 0x1E
#define STK3311WV_PID 0x1D
#define PROXIMITY_FOR_TEST /* for HW to tune up */
#define PROXIMITY_CALIBRATION
#ifdef PROXIMITY_CALIBRATION
#define CALIBRATION_FILE_PATH "/efs/FactoryApp/prox_cal"
#endif
enum {
OFF = 0,
ON,
};
struct stk3013_data {
struct i2c_client *client;
struct stk3013_platform_data *pdata;
int32_t irq;
struct work_struct stk_work;
struct workqueue_struct *stk_wq;
uint16_t ir_code;
uint8_t psctrl_reg;
uint8_t ledctrl_reg;
uint8_t state_reg;
int int_pin;
uint8_t wait_reg;
uint8_t int_reg;
uint16_t ps_thd_h;
uint16_t ps_thd_l;
uint16_t ps_default_thd_h;
uint16_t ps_default_thd_l;
uint16_t ps_cancel_thd_h;
uint16_t ps_cancel_thd_l;
uint16_t ps_cal_skip_adc;
uint16_t ps_cal_fail_adc;
uint16_t ps_default_offset;
uint16_t ps_offset;
unsigned int cal_result;
struct mutex io_lock;
struct input_dev *ps_input_dev;
bool ps_enabled;
bool re_enable_ps;
struct wake_lock ps_wakelock;
ktime_t ps_poll_delay;
bool first_boot;
atomic_t recv_reg;
uint8_t pid;
uint8_t p_wv_r_bd_with_co;
struct regulator *vdd;
struct regulator *vio;
struct device *ps_dev;
struct hrtimer prox_timer;
ktime_t prox_poll_delay;
struct workqueue_struct *prox_wq;
struct work_struct work_prox;
int avg[3];
};
static int32_t stk3013_enable_ps(struct device *dev, uint8_t enable,
uint8_t validate_reg);
static int32_t stk3013_set_ps_thd_l(struct stk3013_data *ps_data,
uint16_t thd_l);
static int32_t stk3013_set_ps_thd_h(struct stk3013_data *ps_data,
uint16_t thd_h);
static int32_t stk3013_set_ps_offset(struct stk3013_data *ps_data,
uint16_t ps_offset);
#ifdef PROXIMITY_CALIBRATION
static int check_calibration_offset(struct stk3013_data *ps_data);
#endif
#ifdef STK_CHK_REG
static int stk3013_validate_n_handle(struct i2c_client *client);
#endif
static int stk3013_regulator_onoff(struct device *dev, bool onoff);
static int32_t stk3013_init_all_setting(struct i2c_client *client,
struct stk3013_platform_data *plat_data);
static int stk3013_i2c_read_data(struct i2c_client *client,
unsigned char command, int length, unsigned char *values)
{
uint8_t retry;
int ret;
struct i2c_msg msgs[] = {
{
.addr = client->addr,
.flags = 0,
.len = 1,
.buf = &command,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = length,
.buf = values,
},
};
for (retry = 0; retry < 5; retry++) {
ret = i2c_transfer(client->adapter, msgs, 2);
if (ret == 2)
break;
}
if (retry >= 5) {
SENSOR_ERR("i2c read fail, err=%d\n", ret);
return -EIO;
}
return 0;
}
static int stk3013_i2c_write_data(struct i2c_client *client,
unsigned char command, int length, unsigned char *values)
{
int retry;
int ret;
unsigned char data[11];
struct i2c_msg msg;
int index;
if (!client)
return -EINVAL;
else if (length >= 10) {
SENSOR_ERR("length %d exceeds 10\n", length);
return -EINVAL;
}
data[0] = command;
for (index = 1; index <= length; index++)
data[index] = values[index-1];
msg.addr = client->addr;
msg.flags = 0;
msg.len = length+1;
msg.buf = data;
for (retry = 0; retry < 5; retry++) {
ret = i2c_transfer(client->adapter, &msg, 1);
if (ret == 1)
break;
}
if (retry >= 5) {
SENSOR_ERR("i2c write fail, err=%d\n", ret);
return -EIO;
}
return 0;
}
static int stk3013_i2c_smbus_read_byte_data(struct i2c_client *client,
unsigned char command)
{
unsigned char value;
int ret;
ret = stk3013_i2c_read_data(client, command, 1, &value);
if (ret < 0)
return ret;
return value;
}
static int stk3013_i2c_smbus_write_byte_data(struct i2c_client *client,
unsigned char command, unsigned char value)
{
int ret;
ret = stk3013_i2c_write_data(client, command, 1, &value);
return ret;
}
static void stk3013_proc_plat_data(struct stk3013_data *ps_data,
struct stk3013_platform_data *plat_data)
{
uint8_t w_reg;
ps_data->state_reg = plat_data->state_reg;
ps_data->psctrl_reg = plat_data->psctrl_reg;
ps_data->ledctrl_reg = plat_data->ledctrl_reg;
if (ps_data->pid == STK3310SA_PID || ps_data->pid == STK3311SA_PID)
ps_data->ledctrl_reg &= 0x3F;
ps_data->wait_reg = plat_data->wait_reg;
if (ps_data->wait_reg < 2) {
SENSOR_INFO("wait_reg should be larger than 2, force to write 2\n");
ps_data->wait_reg = 2;
} else if (ps_data->wait_reg > 0xFF) {
SENSOR_INFO("wait_reg should be less than 0xFF, force to write 0xFF\n");
ps_data->wait_reg = 0xFF;
}
if (ps_data->ps_thd_h == 0 && ps_data->ps_thd_l == 0) {
ps_data->ps_thd_h = plat_data->ps_thd_h;
ps_data->ps_thd_l = plat_data->ps_thd_l;
ps_data->ps_default_thd_h = plat_data->ps_thd_h;
ps_data->ps_default_thd_l = plat_data->ps_thd_l;
ps_data->ps_cancel_thd_h = plat_data->ps_cancel_thd_h;
ps_data->ps_cancel_thd_l = plat_data->ps_cancel_thd_l;
ps_data->ps_cal_skip_adc = plat_data->ps_cal_skip_adc;
ps_data->ps_cal_fail_adc = plat_data->ps_cal_fail_adc;
/*initialize the offset data*/
ps_data->ps_default_offset = plat_data->ps_default_offset;
ps_data->ps_offset = ps_data->ps_default_offset;
}
w_reg = 0;
w_reg |= STK_INT_PS_MODE;
ps_data->int_reg = w_reg;
}
static int32_t stk3013_init_all_reg(struct stk3013_data *ps_data)
{
int32_t ret;
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_STATE_REG, ps_data->state_reg);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_PSCTRL_REG, ps_data->psctrl_reg);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_LEDCTRL_REG, ps_data->ledctrl_reg);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_WAIT_REG, ps_data->wait_reg);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
stk3013_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
stk3013_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
stk3013_set_ps_offset(ps_data, ps_data->ps_default_offset);
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_INT_REG, ps_data->int_reg);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
return 0;
}
static int32_t stk3013_read_otp25(struct stk3013_data *ps_data)
{
int32_t ret, otp25;
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client, 0x0, 0x2);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client, 0x90, 0x25);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client, 0x92, 0x82);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
usleep_range(1000, 5000);
ret = stk3013_i2c_smbus_read_byte_data(ps_data->client, 0x91);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
otp25 = ret;
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client, 0x0, 0x0);
if (ret < 0) {
SENSOR_ERR("write i2c error\n");
return ret;
}
SENSOR_INFO("otp25=0x%x\n", otp25);
if (otp25 & 0x80)
return 1;
return 0;
}
static int32_t stk3013_check_pid(struct stk3013_data *ps_data)
{
unsigned char value[2], pid_msb;
int ret;
ps_data->p_wv_r_bd_with_co = 0;
ret = stk3013_i2c_read_data(ps_data->client,
STK_PDT_ID_REG, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
SENSOR_INFO("PID=0x%x, RID=0x%x\n", value[0], value[1]);
ps_data->pid = value[0];
if (value[0] == STK3311WV_PID)
ps_data->p_wv_r_bd_with_co |= 0xb100;
if (value[1] == 0xC3)
ps_data->p_wv_r_bd_with_co |= 0xb010;
if (stk3013_read_otp25(ps_data) == 1)
ps_data->p_wv_r_bd_with_co |= 0xb001;
SENSOR_INFO("p_wv_r_bd_with_co = 0x%x\n", ps_data->p_wv_r_bd_with_co);
if (value[0] == 0) {
SENSOR_ERR("PID=0x0, please make sure the chip is stk3013!\n");
return -ENXIO;
}
pid_msb = value[0] & 0xF0;
switch (pid_msb) {
case 0x10:
case 0x20:
case 0x30:
return 0;
default:
SENSOR_ERR("invalid PID(%#x)\n", value[0]);
return -EPERM;
}
return 0;
}
static int32_t stk3013_software_reset(struct stk3013_data *ps_data)
{
int32_t r;
uint8_t w_reg;
w_reg = 0x7F;
r = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_WAIT_REG, w_reg);
if (r < 0) {
SENSOR_ERR("software reset: write i2c error, ret=%d\n", r);
return r;
}
r = stk3013_i2c_smbus_read_byte_data(ps_data->client, STK_WAIT_REG);
if (w_reg != r) {
SENSOR_ERR("software reset: read-back value is not the same\n");
return -EPERM;
}
r = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_SW_RESET_REG, 0);
if (r < 0) {
SENSOR_ERR("software reset: read error after reset\n");
return r;
}
usleep_range(13000, 15000);
return 0;
}
static int32_t stk3013_set_ps_thd_l(struct stk3013_data *ps_data,
uint16_t thd_l)
{
unsigned char val[2];
int ret;
val[0] = (thd_l & 0xFF00) >> 8;
val[1] = thd_l & 0x00FF;
ret = stk3013_i2c_write_data(ps_data->client,
STK_THDL1_PS_REG, 2, val);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
else
ps_data->ps_thd_l = thd_l;
SENSOR_INFO("thd_l=%d\n", thd_l);
return ret;
}
static int32_t stk3013_set_ps_thd_h(struct stk3013_data *ps_data,
uint16_t thd_h)
{
unsigned char val[2];
int ret;
val[0] = (thd_h & 0xFF00) >> 8;
val[1] = thd_h & 0x00FF;
ret = stk3013_i2c_write_data(ps_data->client,
STK_THDH1_PS_REG, 2, val);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
else
ps_data->ps_thd_h = thd_h;
SENSOR_INFO("thd_h=%d\n", thd_h);
return ret;
}
static int32_t stk3013_set_ps_offset(struct stk3013_data *ps_data,
uint16_t ps_offset)
{
unsigned char val[2];
int ret;
val[0] = (ps_offset & 0xFF00) >> 8;
val[1] = ps_offset & 0x00FF;
ret = stk3013_i2c_write_data(ps_data->client,
STK_DATA1_OFFSET_REG, 2, val);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
static uint32_t stk3013_get_ps_reading(struct stk3013_data *ps_data)
{
unsigned char value[2];
int ret;
ret = stk3013_i2c_read_data(ps_data->client,
STK_DATA1_PS_REG, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("DATA1 fail, ret=%d\n", ret);
return ret;
}
return (value[0]<<8) | value[1];
}
static int32_t stk3013_set_flag(struct stk3013_data *ps_data,
uint8_t org_flag_reg, uint8_t clr)
{
uint8_t w_flag;
int ret;
w_flag = org_flag_reg | (STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK |
STK_FLG_IR_RDY_MASK);
w_flag &= (~clr);
/*SENSOR_INFO(" org_flag_reg=0x%x, w_flag = 0x%x\n",
org_flag_reg, w_flag);*/
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_FLAG_REG, w_flag);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
static int32_t stk3013_get_flag(struct stk3013_data *ps_data)
{
int ret;
ret = stk3013_i2c_smbus_read_byte_data(ps_data->client,
STK_FLAG_REG);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
static int32_t stk3013_set_state(struct stk3013_data *ps_data, uint8_t state)
{
int ret;
ret = stk3013_i2c_smbus_write_byte_data(ps_data->client,
STK_STATE_REG, state);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
static int32_t stk3013_get_state(struct stk3013_data *ps_data)
{
int ret;
ret = stk3013_i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
return ret;
}
static int32_t stk3013_enable_ps(struct device *dev,
uint8_t enable, uint8_t validate_reg)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
int32_t ret;
uint8_t w_state_reg;
uint8_t curr_ps_enable;
uint32_t read_value;
int32_t near_far_state;
#ifdef STK_CHK_REG
if (validate_reg) {
ret = stk3013_validate_n_handle(ps_data->client);
if (ret < 0)
SENSOR_ERR("stk3013_validate_n_handle fail: %d\n",
ret);
}
#endif /* #ifdef STK_CHK_REG */
curr_ps_enable = ps_data->ps_enabled ? 1 : 0;
if (curr_ps_enable == enable)
return 0;
if (enable) {
/*stk3013_regulator_onoff(dev, ON);*/
msleep(20);
ret = stk3013_init_all_setting(ps_data->client,
ps_data->pdata);
if (ret < 0) {
SENSOR_ERR("init setting fail, ret=%d\n", ret);
return ret;
}
}
if (ps_data->first_boot == true)
ps_data->first_boot = false;
ret = stk3013_get_state(ps_data);
if (ret < 0)
return ret;
w_state_reg = ret;
w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | STK_STATE_EN_AK_MASK);
if (enable)
w_state_reg |= (STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK);
ret = stk3013_set_state(ps_data, w_state_reg);
if (ret < 0)
return ret;
ps_data->state_reg = w_state_reg;
if (enable) {
#ifdef PROXIMITY_CALIBRATION
check_calibration_offset(ps_data);
stk3013_set_ps_offset(ps_data, ps_data->ps_offset);
#endif
enable_irq(ps_data->irq);
ps_data->ps_enabled = true;
#ifdef STK_CHK_REG
if (!validate_reg) {
input_report_abs(ps_data->ps_input_dev,
ABS_DISTANCE, 1);
input_sync(ps_data->ps_input_dev);
wake_lock_timeout(&ps_data->ps_wakelock, 3 * HZ);
read_value = stk3013_get_ps_reading(ps_data);
SENSOR_INFO("force report ps input event=1, ps code=%d\n",
read_value);
} else
#endif/* #ifdef STK_CHK_REG */
{
usleep_range(4000, 5000);
ret = stk3013_get_flag(ps_data);
if (ret < 0)
return ret;
near_far_state = ret & STK_FLG_NF_MASK;
input_report_abs(ps_data->ps_input_dev,
ABS_DISTANCE, near_far_state);
input_sync(ps_data->ps_input_dev);
wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
read_value = stk3013_get_ps_reading(ps_data);
SENSOR_INFO("ps input event=%d, ps code = %d\n",
near_far_state, read_value);
}
} else {
disable_irq(ps_data->irq);
/*stk3013_regulator_onoff(dev, OFF);*/
ps_data->ps_enabled = false;
}
return ret;
}
#ifdef STK_CHK_REG
static int stk3013_chk_reg_valid(struct stk3013_data *ps_data)
{
unsigned char value[9];
int ret;
/*
uint8_t cnt;
for(cnt=0;cnt<9;cnt++)
{
value[cnt] = stk3013_i2c_smbus_read_byte_data(ps_data->client,
(cnt+1));
if (value[cnt] < 0)
{
SENSOR_ERR("%s fail, ret=%d", value[cnt]);
return value[cnt];
}
}
*/
ret = stk3013_i2c_read_data(ps_data->client,
STK_PSCTRL_REG, 9, &value[0]);
if (ret < 0) {
SENSOR_ERR(" fail, ret=%d\n", ret);
return ret;
}
if (value[0] != ps_data->psctrl_reg) {
SENSOR_ERR(" invalid reg 0x01=0x%2x\n", value[0]);
return 0xFF;
}
if (value[2] != ps_data->ledctrl_reg) {
SENSOR_ERR(" invalid reg 0x03=0x%2x\n", value[2]);
return 0xFF;
}
if (value[3] != ps_data->int_reg) {
SENSOR_ERR(" invalid reg 0x04=0x%2x\n", value[3]);
return 0xFF;
}
if (value[4] != ps_data->wait_reg) {
SENSOR_ERR(" invalid reg 0x05=0x%2x\n", value[4]);
return 0xFF;
}
if (value[5] != ((ps_data->ps_thd_h & 0xFF00) >> 8)) {
SENSOR_ERR(" invalid reg 0x06=0x%2x\n", value[5]);
return 0xFF;
}
if (value[6] != (ps_data->ps_thd_h & 0x00FF)) {
SENSOR_ERR(" invalid reg 0x07=0x%2x\n", value[6]);
return 0xFF;
}
if (value[7] != ((ps_data->ps_thd_l & 0xFF00) >> 8)) {
SENSOR_ERR(" invalid reg 0x08=0x%2x\n", value[7]);
return 0xFF;
}
if (value[8] != (ps_data->ps_thd_l & 0x00FF)) {
SENSOR_ERR(" invalid reg 0x09=0x%2x\n", value[8]);
return 0xFF;
}
return 0;
}
static int stk3013_validate_n_handle(struct i2c_client *client)
{
struct stk3013_data *ps_data = i2c_get_clientdata(client);
int ret;
ret = stk3013_chk_reg_valid(ps_data);
if (ret < 0) {
SENSOR_ERR("stk3013_chk_reg_valid fail: %d\n", ret);
return ret;
}
if (ret == 0xFF) {
SENSOR_ERR("Re-init chip\n");
ret = stk3013_software_reset(ps_data);
if (ret < 0)
return ret;
ret = stk3013_init_all_reg(ps_data);
if (ret < 0)
return ret;
stk3013_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
stk3013_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
stk3013_set_ps_offset(ps_data, ps_data->ps_default_offset);
return 0xFF;
}
return 0;
}
#endif /* #ifdef STK_CHK_REG */
#ifdef PROXIMITY_CALIBRATION
static int proximity_store_calibration(struct device *dev, bool do_calib)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
struct file *cal_filp = NULL;
mm_segment_t old_fs;
unsigned char value[2];
int ret;
uint16_t temp[2];
uint16_t offset_data = 0;
SENSOR_INFO("start\n");
if (do_calib) {
ret = stk3013_i2c_read_data(ps_data->client,
STK_DATA1_PS_REG, 2, &value[0]);
if (ret < 0) {
SENSOR_ERR("DATA1 fail, ret=%d\n", ret);
return ret;
}
offset_data = ((value[0]<<8) | value[1]);
SENSOR_INFO("ps_offset = %d\n", offset_data);
if (offset_data < ps_data->ps_cal_skip_adc) {
SENSOR_INFO("skip calibration = %d\n", offset_data);
ps_data->ps_offset = ps_data->ps_default_offset;
ps_data->cal_result = 2;
} else if (offset_data <= ps_data->ps_cal_fail_adc/*DO_CAL*/) {
SENSOR_INFO("do calibration = %d\n", offset_data);
temp[0] = ps_data->ps_default_offset;
ps_data->ps_offset = offset_data + ps_data->ps_default_offset;
ret = stk3013_set_ps_offset(ps_data,
ps_data->ps_offset);
if (ret < 0) {
SENSOR_ERR("calibration fail\n");
ps_data->ps_default_offset = temp[0];
ps_data->cal_result = 0;
} else {
stk3013_set_ps_thd_h(ps_data,
ps_data->ps_cancel_thd_h);
stk3013_set_ps_thd_l(ps_data,
ps_data->ps_cancel_thd_l);
ps_data->cal_result = 1;
}
} else {
SENSOR_INFO("fail offset calibration = %d\n",
offset_data);
ps_data->ps_offset = ps_data->ps_default_offset;
}
} else {
/*reset*/
SENSOR_INFO("reset start\n");
temp[0] = ps_data->ps_offset;
temp[1] = ps_data->cal_result;
ps_data->ps_offset = ps_data->ps_default_offset;
ps_data->cal_result = 0;
ret = stk3013_set_ps_offset(ps_data, ps_data->ps_offset);
if (ret < 0) {
SENSOR_ERR("calibration reset fail\n");
ps_data->ps_default_offset = temp[0];
ps_data->cal_result = temp[1];
}
SENSOR_INFO("ps_thd_h=%d, ps_thd_l=%d, ps_offset=%d\n",
ps_data->ps_thd_h, ps_data->ps_thd_l,
ps_data->ps_offset);
stk3013_set_ps_thd_h(ps_data, ps_data->ps_default_thd_h);
stk3013_set_ps_thd_l(ps_data, ps_data->ps_default_thd_l);
}
old_fs = get_fs();
set_fs(KERNEL_DS);
cal_filp = filp_open(CALIBRATION_FILE_PATH,
O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
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 *)&ps_data->ps_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);
SENSOR_INFO("end\n");
return ret;
}
static ssize_t proximity_calibration_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 -EINVAL;
}
SENSOR_INFO("%d\n", do_calib);
err = proximity_store_calibration(dev, do_calib);
if (err < 0) {
SENSOR_ERR("proximity_store_cancelation() failed\n");
return err;
}
return size;
}
static ssize_t proximity_calibration_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u,%u,%u\n",
ps_data->ps_offset,
(ps_data->ps_offset != ps_data->ps_default_offset) ? ps_data->ps_cancel_thd_h : ps_data->ps_thd_h,
(ps_data->ps_offset != ps_data->ps_default_offset) ? ps_data->ps_cancel_thd_l : ps_data->ps_thd_l);
}
static ssize_t proximity_calibration_pass_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
SENSOR_INFO("result = %d\n", ps_data->cal_result);
return snprintf(buf, PAGE_SIZE, "%u\n",
ps_data->cal_result);
}
#endif
static ssize_t proximity_avg_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", ps_data->avg[0],
ps_data->avg[1], ps_data->avg[2]);
}
static void proximity_get_avg_val(struct stk3013_data *ps_data)
{
int min = 0, max = 0, avg = 0;
int i;
uint32_t read_value;
for (i = 0; i < PROX_READ_NUM; i++) {
msleep(40);
read_value = stk3013_get_ps_reading(ps_data);
avg += read_value;
if (!i)
min = read_value;
else if (read_value < min)
min = read_value;
if (read_value > max)
max = read_value;
}
avg /= PROX_READ_NUM;
ps_data->avg[0] = min;
ps_data->avg[1] = avg;
ps_data->avg[2] = max;
}
static ssize_t proximity_avg_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct stk3013_data *ps_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 -EINVAL;
}
SENSOR_INFO("average enable = %d\n", new_value);
if (new_value) {
if ((ps_data->ps_enabled ? 1 : 0) == OFF) {
mutex_lock(&ps_data->io_lock);
stk3013_enable_ps(dev, new_value, 1);
mutex_unlock(&ps_data->io_lock);
}
hrtimer_start(&ps_data->prox_timer, ps_data->prox_poll_delay,
HRTIMER_MODE_REL);
} else if (!new_value) {
hrtimer_cancel(&ps_data->prox_timer);
cancel_work_sync(&ps_data->work_prox);
if ((ps_data->ps_enabled ? 1 : 0) == OFF) {
mutex_lock(&ps_data->io_lock);
stk3013_enable_ps(dev, new_value, 0);
mutex_unlock(&ps_data->io_lock);
}
}
return size;
}
static ssize_t proximity_trim_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
SENSOR_INFO("trim: %d\n", ps_data->ps_default_offset);
return snprintf(buf, PAGE_SIZE, "%u\n",
ps_data->ps_default_offset);
}
static void stk3013_work_func_prox(struct work_struct *work)
{
struct stk3013_data *ps_data = container_of(work,
struct stk3013_data, work_prox);
proximity_get_avg_val(ps_data);
}
static enum hrtimer_restart stk3013_prox_timer_func(struct hrtimer *timer)
{
struct stk3013_data *ps_data = container_of(timer,
struct stk3013_data, prox_timer);
queue_work(ps_data->prox_wq, &ps_data->work_prox);
hrtimer_forward_now(&ps_data->prox_timer, ps_data->prox_poll_delay);
return HRTIMER_RESTART;
}
static ssize_t proximity_thresh_high_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int32_t ps_thd_h1_reg, ps_thd_h2_reg;
struct stk3013_data *ps_data = dev_get_drvdata(dev);
ps_thd_h1_reg = stk3013_i2c_smbus_read_byte_data(ps_data->client,
STK_THDH1_PS_REG);
if (ps_thd_h1_reg < 0) {
SENSOR_ERR("fail, err=0x%x", ps_thd_h1_reg);
return -EINVAL;
}
ps_thd_h2_reg = stk3013_i2c_smbus_read_byte_data(ps_data->client,
STK_THDH2_PS_REG);
if (ps_thd_h2_reg < 0) {
SENSOR_ERR("fail, err=0x%x", ps_thd_h2_reg);
return -EINVAL;
}
ps_thd_h1_reg = ps_thd_h1_reg<<8 | ps_thd_h2_reg;
SENSOR_INFO("thresh:0x%x", ps_thd_h1_reg);
return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg);
}
static ssize_t proximity_thresh_high_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
u16 value = 0;
int ret;
ret = kstrtou16(buf, 10, &value);
if (ret < 0) {
SENSOR_ERR("kstrtoul failed, ret=0x%x\n", ret);
return ret;
}
SENSOR_INFO("thresh: %d\n", value);
stk3013_set_ps_thd_h(ps_data, value);
return size;
}
static ssize_t proximity_thresh_low_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int32_t ps_thd_l1_reg, ps_thd_l2_reg;
struct stk3013_data *ps_data = dev_get_drvdata(dev);
ps_thd_l1_reg = stk3013_i2c_smbus_read_byte_data(ps_data->client,
STK_THDL1_PS_REG);
if (ps_thd_l1_reg < 0) {
SENSOR_ERR("fail, err=0x%x", ps_thd_l1_reg);
return -EINVAL;
}
ps_thd_l2_reg = stk3013_i2c_smbus_read_byte_data(ps_data->client,
STK_THDL2_PS_REG);
if (ps_thd_l2_reg < 0) {
SENSOR_ERR("fail, err=0x%x", ps_thd_l2_reg);
return -EINVAL;
}
ps_thd_l1_reg = ps_thd_l1_reg<<8 | ps_thd_l2_reg;
return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg);
}
static ssize_t proximity_thresh_low_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
u16 value = 0;
int ret;
ret = kstrtou16(buf, 10, &value);
if (ret < 0) {
SENSOR_ERR("kstrtoul failed, ret=0x%x\n", ret);
return ret;
}
SENSOR_INFO("thresh: %d\n", value);
stk3013_set_ps_thd_l(ps_data, value);
return size;
}
static ssize_t proximity_state_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
uint32_t read_value;
read_value = stk3013_get_ps_reading(ps_data);
return scnprintf(buf, PAGE_SIZE, "%d\n", read_value);
}
static ssize_t stk3013_vendor_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
}
static ssize_t stk3013_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_ID);
}
#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;
int ret;
struct stk3013_data *data = dev_get_drvdata(dev);
if (sscanf(buf, "%2x,%2x", &regist, &val) != 2) {
SENSOR_ERR("The number of data are wrong\n");
return -EINVAL;
}
ret = stk3013_i2c_write_data(data->client, regist, 1, (unsigned char *)&val);
if (ret < 0)
SENSOR_ERR("fail, ret=%d\n", ret);
else
SENSOR_INFO("Register(0x%2x) data(0x%2x)\n", regist, val);
return count;
}
static ssize_t proximity_register_read_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
u8 reg;
unsigned char val = 0;
int offset = 0;
struct stk3013_data *data = dev_get_drvdata(dev);
for (reg = STK_STATE_REG; reg <= STK_DATA2_OFFSET_REG; reg++) {
stk3013_i2c_read_data(data->client, reg, 1, &val);
SENSOR_INFO("Register(0x%2x) data(0x%2x)\n", reg, val);
offset += snprintf(buf + offset, PAGE_SIZE - offset,
"Reg: 0x%2x, Val: 0x%2x\n", reg, val);
}
return offset;
}
#endif
#ifdef PROXIMITY_CALIBRATION
static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
proximity_calibration_show, proximity_calibration_store);
static DEVICE_ATTR(prox_offset_pass, S_IRUGO, proximity_calibration_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(prox_trim, S_IRUGO,
proximity_trim_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(state, S_IRUGO, proximity_state_show, NULL);
static DEVICE_ATTR(raw_data, S_IRUGO, proximity_state_show, NULL);
static DEVICE_ATTR(vendor, S_IRUGO, stk3013_vendor_show, NULL);
static DEVICE_ATTR(name, S_IRUGO, stk3013_name_show, NULL);
static struct device_attribute *prox_sensor_attrs[] = {
#ifdef PROXIMITY_CALIBRATION
&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_prox_trim,
&dev_attr_thresh_high,
&dev_attr_thresh_low,
&dev_attr_state,
&dev_attr_raw_data,
&dev_attr_vendor,
&dev_attr_name,
NULL,
};
static ssize_t proximity_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int32_t ret;
struct stk3013_data *ps_data = dev_get_drvdata(dev);
ret = stk3013_get_state(ps_data);
if (ret < 0)
return ret;
ret = (ret & STK_STATE_EN_PS_MASK) ? 1 : 0;
return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
}
static ssize_t proximity_enable_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
uint8_t en;
if (sysfs_streq(buf, "1"))
en = 1;
else if (sysfs_streq(buf, "0"))
en = 0;
else {
SENSOR_ERR("invalid value %d\n", *buf);
return size;
}
SENSOR_INFO("Enable PS : %d\n", en);
mutex_lock(&ps_data->io_lock);
stk3013_enable_ps(dev, en, 1);
mutex_unlock(&ps_data->io_lock);
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 stk_work_func(struct work_struct *work)
{
uint32_t read_value;
#if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02))
int32_t ret;
uint8_t disable_flag = 0;
int32_t org_flag_reg;
#endif/* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */
struct stk3013_data *ps_data = container_of(work,
struct stk3013_data, stk_work);
int32_t near_far_state;
#if (STK_INT_PS_MODE == 0x03)
near_far_state = gpio_get_value(ps_data->int_pin);
#elif (STK_INT_PS_MODE == 0x02)
near_far_state = !(gpio_get_value(ps_data->int_pin));
#endif
#if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
input_sync(ps_data->ps_input_dev);
wake_lock_timeout(&ps_data->ps_wakelock, 3 * HZ);
read_value = stk3013_get_ps_reading(ps_data);
SENSOR_INFO("ps input event %d cm, ps code = %d\n",
near_far_state, read_value);
#else
/* mode 0x01 or 0x04 */
org_flag_reg = stk3013_get_flag(ps_data);
if (org_flag_reg < 0)
goto err_i2c_rw;
if (org_flag_reg & STK_FLG_PSINT_MASK) {
disable_flag |= STK_FLG_PSINT_MASK;
near_far_state = (org_flag_reg & STK_FLG_NF_MASK) ? 1 : 0;
read_value = stk3013_get_ps_reading(ps_data);
#ifdef CONFIG_SEC_FACTORY
SENSOR_INFO("FACTORY: near/far=%d, ps code = %d\n",
near_far_state, read_value);
#else
SENSOR_INFO("near/far=%d, ps code = %d\n",
near_far_state, read_value);
if ((near_far_state == 0 && read_value >= ps_data->ps_thd_h)
|| (near_far_state == 1 && read_value <= ps_data->ps_thd_l))
#endif
{
input_report_abs(ps_data->ps_input_dev,
ABS_DISTANCE, near_far_state);
input_sync(ps_data->ps_input_dev);
wake_lock_timeout(&ps_data->ps_wakelock, 3 * HZ);
}
}
if (disable_flag) {
ret = stk3013_set_flag(ps_data, org_flag_reg, disable_flag);
if (ret < 0)
goto err_i2c_rw;
}
#endif
usleep_range(1000, 2000);
goto exit;
err_i2c_rw:
msleep(30);
exit:
enable_irq(ps_data->irq);
}
static irqreturn_t stk_oss_irq_handler(int irq, void *data)
{
struct stk3013_data *pData = data;
disable_irq_nosync(irq);
queue_work(pData->stk_wq, &pData->stk_work);
return IRQ_HANDLED;
}
static int32_t stk3013_init_all_setting(struct i2c_client *client,
struct stk3013_platform_data *plat_data)
{
int32_t ret;
struct stk3013_data *ps_data = i2c_get_clientdata(client);
ret = stk3013_software_reset(ps_data);
if (ret < 0)
return ret;
ret = stk3013_check_pid(ps_data);
if (ret < 0)
return ret;
stk3013_proc_plat_data(ps_data, plat_data);
ret = stk3013_init_all_reg(ps_data);
if (ret < 0)
return ret;
ps_data->ps_enabled = false;
ps_data->re_enable_ps = false;
ps_data->ir_code = 0;
ps_data->first_boot = true;
atomic_set(&ps_data->recv_reg, 0);
return 0;
}
static int stk3013_setup_irq(struct i2c_client *client)
{
int irq, ret = -EIO;
struct stk3013_data *ps_data = i2c_get_clientdata(client);
irq = gpio_to_irq(ps_data->int_pin);
SENSOR_INFO("int pin #=%d, irq=%d\n", ps_data->int_pin, irq);
if (irq <= 0) {
SENSOR_ERR("irq number is not specified, irq=%d, int pin=%d\n",
irq, ps_data->int_pin);
return irq;
}
ps_data->irq = irq;
ret = gpio_request(ps_data->int_pin, "stk-int");
if (ret < 0) {
SENSOR_ERR("gpio_request, err=%d", ret);
return ret;
}
ret = gpio_direction_input(ps_data->int_pin);
if (ret < 0) {
SENSOR_ERR("gpio_direction_input, err=%d", ret);
return ret;
}
#if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
ret = request_any_context_irq(irq, stk_oss_irq_handler,
IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"proximity_int", ps_data);
#else
ret = request_any_context_irq(irq, stk_oss_irq_handler,
IRQF_TRIGGER_LOW, "proximity_int", ps_data);
#endif
if (ret < 0) {
SENSOR_WARN("request_any_context_irq(%d) failed for (%d)\n",
irq, ret);
goto err_request_any_context_irq;
}
disable_irq(irq);
return 0;
err_request_any_context_irq:
gpio_free(ps_data->int_pin);
return ret;
}
static int stk3013_suspend(struct device *dev)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
int ret;
struct i2c_client *client = to_i2c_client(dev);
SENSOR_INFO("\n");
mutex_lock(&ps_data->io_lock);
#ifdef STK_CHK_REG
ret = stk3013_validate_n_handle(ps_data->client);
if (ret < 0) {
SENSOR_ERR("stk3013_validate_n_handle fail: %d\n", ret);
} else if (ret == 0xFF) {
if (ps_data->ps_enabled)
stk3013_enable_ps(ps_data, 1, 0);
}
#endif /* #ifdef STK_CHK_REG */
if (ps_data->ps_enabled) {
if (device_may_wakeup(&client->dev)) {
ret = enable_irq_wake(ps_data->irq);
if (ret)
SENSOR_WARN("set_irq_wake(%d) failed(%d)\n",
ps_data->irq, ret);
} else {
SENSOR_ERR("not support wakeup source");
}
}
mutex_unlock(&ps_data->io_lock);
return 0;
}
static int stk3013_resume(struct device *dev)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
int ret;
struct i2c_client *client = to_i2c_client(dev);
SENSOR_INFO("\n");
mutex_lock(&ps_data->io_lock);
#ifdef STK_CHK_REG
ret = stk3013_validate_n_handle(ps_data->client);
if (ret < 0) {
SENSOR_ERR("stk3013_validate_n_handle fail: %d\n", ret);
} else if (ret == 0xFF) {
if (ps_data->ps_enabled)
stk3013_enable_ps(ps_data, 1, 0);
}
#endif/* #ifdef STK_CHK_REG */
if (ps_data->ps_enabled) {
if (device_may_wakeup(&client->dev)) {
ret = disable_irq_wake(ps_data->irq);
if (ret)
SENSOR_WARN("disable_irq_wake(%d) fail(%d)\n",
ps_data->irq, ret);
}
}
mutex_unlock(&ps_data->io_lock);
return 0;
}
static const struct dev_pm_ops stk3013_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(stk3013_suspend, stk3013_resume)
};
static int stk3013_regulator_onoff(struct device *dev, bool onoff)
{
struct stk3013_data *ps_data = dev_get_drvdata(dev);
int ret;
SENSOR_INFO("%s\n", (onoff) ? "on" : "off");
if (!ps_data->vdd || IS_ERR(ps_data->vdd)) {
SENSOR_INFO("VDD get regulator\n");
ps_data->vdd = devm_regulator_get(dev, "stk,vdd");
if (IS_ERR(ps_data->vdd)) {
SENSOR_ERR("cannot get vdd\n");
return -ENOMEM;
}
regulator_set_voltage(ps_data->vdd, 2800000,2800000);
}
if (!ps_data->vio || IS_ERR(ps_data->vio)) {
SENSOR_INFO("VIO get regulator\n");
ps_data->vio = devm_regulator_get(dev, "stk,vio");
if (IS_ERR(ps_data->vio)) {
SENSOR_ERR("cannot get vio\n");
devm_regulator_put(ps_data->vdd);
return -ENOMEM;
}
regulator_set_voltage(ps_data->vio, 1800000, 1800000);
}
if (onoff) {
ret = regulator_enable(ps_data->vdd);
if (ret)
SENSOR_ERR("Failed to enable vdd.\n");
msleep(20);
ret = regulator_enable(ps_data->vio);
if (ret)
SENSOR_ERR("Failed to enable vio.\n");
msleep(20);
} else {
ret = regulator_disable(ps_data->vdd);
if (ret)
SENSOR_ERR("Failed to disable vdd.\n");
msleep(20);
ret = regulator_disable(ps_data->vio);
if (ret)
SENSOR_ERR("Failed to disable vio.\n");
msleep(20);
}
return 0;
}
static int stk3013_parse_dt(struct device *dev,
struct stk3013_platform_data *pdata)
{
int rc;
struct device_node *np = dev->of_node;
u32 temp_val;
if (!pdata)
return -ENOMEM;
pdata->int_pin = of_get_named_gpio_flags(np, "stk,irq-gpio", 0,
&pdata->int_flags);
if (pdata->int_pin < 0) {
dev_err(dev, "Unable to read irq-gpio\n");
return pdata->int_pin;
}
rc = of_property_read_u32(np, "stk,transmittance", &temp_val);
if (!rc)
pdata->transmittance = temp_val;
else {
dev_err(dev, "Unable to read transmittance\n");
return rc;
}
rc = of_property_read_u32(np, "stk,state-reg", &temp_val);
if (!rc)
pdata->state_reg = temp_val;
else {
dev_err(dev, "Unable to read state-reg\n");
return rc;
}
rc = of_property_read_u32(np, "stk,psctrl-reg", &temp_val);
if (!rc)
pdata->psctrl_reg = (u8)temp_val;
else {
dev_err(dev, "Unable to read psctrl-reg\n");
return rc;
}
/*
rc = of_property_read_u32(np, "stk,alsctrl-reg", &temp_val);
if (!rc)
pdata->alsctrl_reg = (u8)temp_val;
else {
dev_err(dev, "Unable to read alsctrl-reg\n");
return rc;
}
*/
rc = of_property_read_u32(np, "stk,ledctrl-reg", &temp_val);
if (!rc)
pdata->ledctrl_reg = (u8)temp_val;
else {
dev_err(dev, "Unable to read ledctrl-reg\n");
return rc;
}
rc = of_property_read_u32(np, "stk,wait-reg", &temp_val);
if (!rc)
pdata->wait_reg = (u8)temp_val;
else {
dev_err(dev, "Unable to read wait-reg\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-thd-h", &temp_val);
if (!rc)
pdata->ps_thd_h = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-thd-h\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-thd-l", &temp_val);
if (!rc)
pdata->ps_thd_l = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-thd-l\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-cancel-thd-h", &temp_val);
if (!rc)
pdata->ps_cancel_thd_h = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-cancel-thd-h\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-cancel-thd-l", &temp_val);
if (!rc)
pdata->ps_cancel_thd_l = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-cancel-thd-l\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-cal-skip-adc", &temp_val);
if (!rc)
pdata->ps_cal_skip_adc = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-cal-skip-adc\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-cal-fail-adc", &temp_val);
if (!rc)
pdata->ps_cal_fail_adc = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-cal-fail-adc\n");
return rc;
}
rc = of_property_read_u32(np, "stk,ps-default-offset", &temp_val);
if (!rc)
pdata->ps_default_offset = (u16)temp_val;
else {
dev_err(dev, "Unable to read ps-default-offset\n");
return rc;
}
return 0;
}
#ifdef PROXIMITY_CALIBRATION
static int check_calibration_offset(struct stk3013_data *ps_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(CALIBRATION_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);
ps_data->ps_offset = ps_data->ps_default_offset;
SENSOR_ERR("Can't open calibration file 2(%d) ps_offset =%d\n",
ret, ps_data->ps_offset);
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\n");
ret = -EIO;
}
if(file_offset_data < ps_data->ps_cal_skip_adc)
goto exit;
if (file_offset_data != ps_data->ps_offset)
ps_data->ps_offset = file_offset_data;
if (ps_data->ps_offset != ps_data->ps_default_offset) {
stk3013_set_ps_thd_h(ps_data, ps_data->ps_cancel_thd_h);
stk3013_set_ps_thd_l(ps_data, ps_data->ps_cancel_thd_l);
}
exit:
SENSOR_INFO("file_offset = %d, ps_offset = %d, default_offset = %d\n",
file_offset_data, ps_data->ps_offset,
ps_data->ps_default_offset);
filp_close(cal_filp, current->files);
set_fs(old_fs);
return ret;
}
#endif
static int stk3013_set_wq(struct stk3013_data *ps_data)
{
ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
INIT_WORK(&ps_data->stk_work, stk_work_func);
return 0;
}
static int stk3013_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret = -ENODEV;
struct stk3013_data *ps_data;
struct stk3013_platform_data *plat_data;
SENSOR_INFO("driver version = %s\n", DRIVER_VERSION);
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
SENSOR_ERR("No Support for I2C_FUNC_I2C\n");
return ret;
}
ps_data = kzalloc(sizeof(struct stk3013_data), GFP_KERNEL);
if (!ps_data) {
SENSOR_ERR("failed to allocate stk3013_data\n");
return -ENOMEM;
}
ps_data->client = client;
i2c_set_clientdata(client, ps_data);
mutex_init(&ps_data->io_lock);
wake_lock_init(&ps_data->ps_wakelock, WAKE_LOCK_SUSPEND,
"stk_input_wakelock");
if (client->dev.of_node) {
SENSOR_INFO("with device tree\n");
plat_data = devm_kzalloc(&client->dev,
sizeof(struct stk3013_platform_data), GFP_KERNEL);
if (!plat_data) {
dev_err(&client->dev, "Failed to allocate memory\n");
ret = -ENOMEM;
goto err_als_input_allocate;
}
ret = stk3013_parse_dt(&client->dev, plat_data);
if (ret) {
SENSOR_ERR("stk3013_parse_dt ret=%d\n", ret);
goto err_als_input_allocate;
}
} else {
SENSOR_INFO("with platform data\n");
plat_data = client->dev.platform_data;
}
if (!plat_data) {
SENSOR_ERR("no stk3013 platform data!\n");
ret = -ENOMEM;
goto err_als_input_allocate;
}
stk3013_regulator_onoff(&client->dev, ON);
ps_data->int_pin = plat_data->int_pin;
ps_data->pdata = plat_data;
stk3013_set_wq(ps_data);
ret = stk3013_init_all_setting(client, plat_data);
if (ret < 0)
goto err_init_all_setting;
ps_data->ps_input_dev = input_allocate_device();
if (ps_data->ps_input_dev == NULL) {
SENSOR_ERR("could not allocate ps device\n");
ret = -ENOMEM;
goto err_input_alloc_device;
}
ps_data->ps_input_dev->name = MODULE_NAME;
set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
input_set_capability(ps_data->ps_input_dev, EV_ABS, ABS_DISTANCE);
input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
ret = input_register_device(ps_data->ps_input_dev);
if (ret < 0) {
SENSOR_ERR("can not register ps input device\n");
goto err_input_register_device;
}
ret = sensors_create_symlink(&ps_data->ps_input_dev->dev.kobj,
ps_data->ps_input_dev->name);
if (ret < 0) {
SENSOR_ERR("create_symlink error\n");
goto err_sensors_create_symlink_prox;
}
ret = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj,
&proximity_attribute_group);
if (ret < 0) {
SENSOR_ERR("could not create sysfs group for ps\n");
goto err_sysfs_create_group_proximity;
}
input_set_drvdata(ps_data->ps_input_dev, ps_data);
ret = stk3013_setup_irq(client);
if (ret < 0)
goto err_stk3013_setup_irq;
device_init_wakeup(&client->dev, true);
hrtimer_init(&ps_data->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ps_data->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
ps_data->prox_timer.function = stk3013_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). */
ps_data->prox_wq = create_singlethread_workqueue("stk3013_prox_wq");
if (!ps_data->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(&ps_data->work_prox, stk3013_work_func_prox);
ret = sensors_register(&ps_data->ps_dev, ps_data,
prox_sensor_attrs, MODULE_NAME);
if (ret) {
SENSOR_ERR("cound not register proximity sensor device(%d)\n",
ret);
goto prox_sensor_register_failed;
}
/*stk3013_regulator_onoff(&client->dev, OFF);*/
SENSOR_INFO("success\n");
return 0;
/*device_init_wakeup(&client->dev, false);*/
prox_sensor_register_failed:
destroy_workqueue(ps_data->prox_wq);
err_create_prox_workqueue:
err_stk3013_setup_irq:
free_irq(ps_data->irq, ps_data);
gpio_free(ps_data->int_pin);
err_sysfs_create_group_proximity:
sensors_remove_symlink(&ps_data->ps_input_dev->dev.kobj,
ps_data->ps_input_dev->name);
err_sensors_create_symlink_prox:
input_unregister_device(ps_data->ps_input_dev);
err_input_register_device:
err_input_alloc_device:
err_init_all_setting:
destroy_workqueue(ps_data->stk_wq);
/*stk3013_regulator_onoff(&client->dev, OFF);*/
err_als_input_allocate:
wake_lock_destroy(&ps_data->ps_wakelock);
mutex_destroy(&ps_data->io_lock);
kfree(ps_data);
return ret;
}
static int stk3013_remove(struct i2c_client *client)
{
SENSOR_INFO("\n");
return 0;
}
static const struct i2c_device_id stk_ps_id[] = {
{ "stk_ps", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, stk_ps_id);
static struct of_device_id stk_match_table[] = {
{ .compatible = "stk,stk3013", },
{ },
};
static struct i2c_driver stk_ps_driver = {
.driver = {
.name = CHIP_ID,
.owner = THIS_MODULE,
.of_match_table = stk_match_table,
.pm = &stk3013_pm_ops,
},
.probe = stk3013_probe,
.remove = stk3013_remove,
.id_table = stk_ps_id,
};
static int __init stk3013_init(void)
{
int ret;
ret = i2c_add_driver(&stk_ps_driver);
if (ret)
i2c_del_driver(&stk_ps_driver);
return ret;
}
static void __exit stk3013_exit(void)
{
i2c_del_driver(&stk_ps_driver);
}
module_init(stk3013_init);
module_exit(stk3013_exit);
MODULE_AUTHOR("Samsung Electronics");
MODULE_DESCRIPTION("Sensortek stk3013 Proximity Sensor driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);