lineage_kernel_xcoverpro/drivers/sensorhub/tc3xxk.c

2317 lines
53 KiB
C
Executable File

/* tc3xxk.c -- Linux driver for coreriver chip as grip
*
* Copyright (C) 2013 Samsung Electronics Co.Ltd
* Author: Junkyeong Kim <jk0430.kim@samsung.com>
*
* 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, 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.
*
*/
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/regulator/consumer.h>
#include <linux/sec_class.h>
#include <linux/pinctrl/consumer.h>
#ifdef CONFIG_BATTERY_SAMSUNG
#include <linux/sec_batt.h>
#endif
#include "tc3xxk.h"
#if defined (CONFIG_VBUS_NOTIFIER)
#include <linux/muic/muic.h>
#include <linux/muic/muic_notifier.h>
#include <linux/vbus_notifier.h>
#endif
#define IDLE 0
#define ACTIVE 1
/* registers */
#define TC300K_FWVER 0x01
#define TC300K_MDVER 0x02
#define TC300K_MODE 0x03
#define TC300K_CHECKS_H 0x04
#define TC300K_CHECKS_L 0x05
/* registers for grip sensor */
#define TC305K_GRIPCODE 0x0F
#define TC305K_GRIP_THD_PRESS 0x00
#define TC305K_GRIP_THD_RELEASE 0x02
#define TC305K_GRIP_THD_NOISE 0x04
#define TC305K_GRIP_CH_PERCENT 0x06
#define TC305K_GRIP_DIFF_DATA 0x08
#define TC305K_GRIP_RAW_DATA 0x0A
#define TC305K_GRIP_BASELINE 0x0C
#define TC305K_GRIP_TOTAL_CAP 0x0E
#define TC305K_GRIP_REF_CAP 0x70
#define TC305K_1GRIP 0x30
#define TC305K_2GRIP 0x40
#define TC305K_3GRIP 0x50
#define TC305K_4GRIP 0x60
#define TC350K_DATA_SIZE 0x02
#define TC350K_DATA_H_OFFSET 0x00
#define TC350K_DATA_L_OFFSET 0x01
/* command */
#define TC300K_CMD_ADDR 0x00
#define TC300K_CMD_TA_ON 0x50
#define TC300K_CMD_TA_OFF 0x60
#define TC300K_CMD_CAL_CHECKSUM 0x70
#define TC300K_CMD_STOP_MODE 0x90
#define TC300K_CMD_NORMAL_MODE 0x91
#define TC300K_CMD_SAR_DISABLE 0xA0
#define TC300K_CMD_SAR_ENABLE 0xA1
#define TC300K_CMD_GRIP_BASELINE_CAL 0xC0
#define TC300K_CMD_WAKE_UP 0xF0
#define TC300K_CMD_DELAY 50
/* mode status bit */
#define TC300K_MODE_RUN (1 << 1)
#define TC300K_MODE_SAR (1 << 2)
/* firmware */
#define TC300K_FW_PATH_SDCARD "/sdcard/tc3xxk.bin"
#define HALL_PATH "/sys/class/sec/sec_key/hall_detect"
#define HALL_CLOSE_STATE 1
#define TK_UPDATE_PASS 0
#define TK_UPDATE_DOWN 1
#define TK_UPDATE_FAIL 2
/* ISP command */
#define TC300K_CSYNC1 0xA3
#define TC300K_CSYNC2 0xAC
#define TC300K_CSYNC3 0xA5
#define TC300K_CCFG 0x92
#define TC300K_PRDATA 0x81
#define TC300K_PEDATA 0x82
#define TC300K_PWDATA 0x83
#define TC300K_PECHIP 0x8A
#define TC300K_PEDISC 0xB0
#define TC300K_LDDATA 0xB2
#define TC300K_LDMODE 0xB8
#define TC300K_RDDATA 0xB9
#define TC300K_PCRST 0xB4
#define TC300K_PCRED 0xB5
#define TC300K_PCINC 0xB6
#define TC300K_RDPCH 0xBD
/* ISP delay */
#define TC300K_TSYNC1 300 /* us */
#define TC300K_TSYNC2 50 /* 1ms~50ms */
#define TC300K_TSYNC3 100 /* us */
#define TC300K_TDLY1 1 /* us */
#define TC300K_TDLY2 2 /* us */
#define TC300K_TFERASE 10 /* ms */
#define TC300K_TPROG 20 /* us */
#define TC300K_CHECKSUM_DELAY 500
enum {
FW_INKERNEL,
FW_SDCARD,
};
struct fw_image {
u8 hdr_ver;
u8 hdr_len;
u16 first_fw_ver;
u16 second_fw_ver;
u16 third_ver;
u32 fw_len;
u16 checksum;
u16 alignment_dummy;
u8 data[0];
} __attribute__ ((packed));
#define GRIP_RELEASE 0x00
#define GRIP_PRESS 0x01
struct grip_event_val {
u16 grip_bitmap;
u8 grip_status;
char* grip_name;
};
struct grip_event_val grip_ev[4] =
{
{0x01 << 0, GRIP_PRESS, "grip1"},
{0x01 << 1, GRIP_PRESS, "grip2"},
{0x01 << 4, GRIP_RELEASE, "grip1"},
{0x01 << 5, GRIP_RELEASE, "grip2"},
};
struct tc3xxk_data {
struct device *dev;
struct i2c_client *client;
struct input_dev *input_dev;
struct tc3xxk_platform_data *pdata;
struct mutex lock_fac;
struct fw_image *fw_img;
const struct firmware *fw;
char phys[32];
int irq;
bool irq_check;
u16 checksum;
u16 threhold;
int mode;
int (*power) (bool on);
u8 fw_ver;
u8 fw_ver_bin;
u8 md_ver;
u8 md_ver_bin;
u8 fw_update_status;
bool enabled;
bool fw_downloading;
struct pinctrl *pinctrl_i2c;
struct pinctrl *pinctrl_irq;
struct pinctrl_state *pin_state[4];
struct wake_lock grip_wake_lock;
u16 grip_p_thd;
u16 grip_r_thd;
u16 grip_n_thd;
u16 grip_s1;
u16 grip_s2;
u16 grip_baseline;
u16 grip_raw1;
u16 grip_raw2;
u16 grip_event;
bool sar_enable;
bool sar_enable_off;
int grip_num;
struct grip_event_val *grip_ev_val;
struct delayed_work debug_work;
#ifdef CONFIG_SEC_FACTORY
int irq_count;
int abnormal_mode;
s32 diff;
s32 max_diff;
#endif
#if defined (CONFIG_VBUS_NOTIFIER)
struct notifier_block vbus_nb;
#endif
};
extern struct class *sec_class;
char *str_states[] = {"on_irq", "off_irq", "on_i2c", "off_i2c"};
enum {
I_STATE_ON_IRQ = 0,
I_STATE_OFF_IRQ,
I_STATE_ON_I2C,
I_STATE_OFF_I2C,
};
static bool tc3xxk_power_enabled;
const char *regulator_ic;
static int tc3xxk_pinctrl_init(struct tc3xxk_data *data);
static void tc3xxk_config_gpio_i2c(struct tc3xxk_data *data, int onoff);
static int tc3xxk_pinctrl(struct tc3xxk_data *data, int status);
static int read_tc3xxk_register_data(struct tc3xxk_data *data, int read_key_num, int read_offset);
static int tc3xxk_mode_enable(struct i2c_client *client, u8 cmd);
static void tc3xxk_grip_cal_reset(struct tc3xxk_data *data);
static int tc3xxk_get_hallic_state(struct tc3xxk_data *data)
{
char hall_buf[6];
int ret = -ENODEV;
int hall_state = -1;
mm_segment_t old_fs;
struct file *filep;
memset(hall_buf, 0, sizeof(hall_buf));
old_fs = get_fs();
set_fs(KERNEL_DS);
filep = filp_open(HALL_PATH, O_RDONLY, 0666);
if (IS_ERR(filep)) {
set_fs(old_fs);
return hall_state;
}
ret = filep->f_op->read(filep, hall_buf,
sizeof(hall_buf) - 1, &filep->f_pos);
if (ret != sizeof(hall_buf) - 1)
goto exit;
if (strcmp(hall_buf, "CLOSE") == 0)
hall_state = HALL_CLOSE_STATE;
exit:
filp_close(filep, current->files);
set_fs(old_fs);
return hall_state;
}
static void tc3xxk_debug_work_func(struct work_struct *work)
{
struct tc3xxk_data *data = container_of((struct delayed_work *)work,
struct tc3xxk_data, debug_work);
static int hall_prev_state;
int hall_state;
hall_state = tc3xxk_get_hallic_state(data);
if (hall_state == HALL_CLOSE_STATE && hall_prev_state != hall_state) {
SENSOR_INFO("%s - hall is closed\n", __func__);
tc3xxk_grip_cal_reset(data);
}
hall_prev_state = hall_state;
#ifdef CONFIG_SEC_FACTORY
if (data->abnormal_mode) {
data->diff = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_DIFF_DATA);
if (data->max_diff < data->diff)
data->max_diff = data->diff;
}
#endif
schedule_delayed_work(&data->debug_work, msecs_to_jiffies(2000));
}
static int tc3xxk_mode_check(struct i2c_client *client)
{
int mode = i2c_smbus_read_byte_data(client, TC300K_MODE);
if (mode < 0)
SENSOR_ERR("failed to read mode (%d)\n", mode);
return mode;
}
static int tc3xxk_wake_up(struct i2c_client *client, u8 cmd)
{
//If stop mode enabled, touch key IC need wake_up CMD
//After wake_up CMD, IC need 10ms delay
int ret;
SENSOR_INFO("Send WAKE UP cmd: 0x%02x \n", cmd);
ret = i2c_smbus_write_byte_data(client, TC300K_CMD_ADDR, TC300K_CMD_WAKE_UP);
msleep(10);
return ret;
}
static void grip_sar_sensing(struct tc3xxk_data *data, bool on)
{
/* enable/disable sar sensing
* need to disable when earjack is connected (FM radio can't work normally)
*/
}
static void tc3xxk_grip_cal_reset(struct tc3xxk_data *data)
{
/* calibrate grip sensor chn */
struct i2c_client *client = data->client;
SENSOR_INFO("\n");
i2c_smbus_write_byte_data(client, TC300K_CMD_ADDR, TC300K_CMD_GRIP_BASELINE_CAL);
msleep(TC300K_CMD_DELAY);
}
static void tc3xxk_reset(struct tc3xxk_data *data)
{
SENSOR_INFO("\n");
if (data->irq_check) {
data->irq_check = false;
disable_irq_wake(data->client->irq);
disable_irq_nosync(data->client->irq);
}
data->pdata->power(data, false);
msleep(50);
data->pdata->power(data, true);
msleep(200);
if (data->sar_enable)
tc3xxk_mode_enable(data->client, TC300K_CMD_SAR_ENABLE);
if (!data->irq_check) {
data->irq_check = true;
enable_irq(data->client->irq);
enable_irq_wake(data->client->irq);
}
}
static void tc3xxk_reset_probe(struct tc3xxk_data *data)
{
data->pdata->power(data, false);
msleep(50);
data->pdata->power(data, true);
msleep(200);
}
int tc3xxk_get_fw_version(struct tc3xxk_data *data, bool probe)
{
struct i2c_client *client = data->client;
int retry = 3;
int buf;
if ((!data->enabled) || data->fw_downloading) {
SENSOR_ERR("can't excute\n");
return -1;
}
buf = i2c_smbus_read_byte_data(client, TC300K_FWVER);
if (buf < 0) {
while (retry--) {
SENSOR_ERR("read fail(%d)\n", retry);
if (probe)
tc3xxk_reset_probe(data);
else
tc3xxk_reset(data);
buf = i2c_smbus_read_byte_data(client, TC300K_FWVER);
if (buf > 0)
break;
}
if (retry <= 0) {
SENSOR_ERR("read fail\n");
data->fw_ver = 0;
return -1;
}
}
data->fw_ver = (u8)buf;
SENSOR_INFO( "fw_ver : 0x%x\n", data->fw_ver);
return 0;
}
int tc3xxk_get_md_version(struct tc3xxk_data *data, bool probe)
{
struct i2c_client *client = data->client;
int retry = 3;
int buf;
if ((!data->enabled) || data->fw_downloading) {
SENSOR_ERR("can't excute\n");
return -1;
}
buf = i2c_smbus_read_byte_data(client, TC300K_MDVER);
if (buf < 0) {
while (retry--) {
SENSOR_ERR("read fail(%d)\n", retry);
if (probe)
tc3xxk_reset_probe(data);
else
tc3xxk_reset(data);
buf = i2c_smbus_read_byte_data(client, TC300K_MDVER);
if (buf > 0)
break;
}
if (retry <= 0) {
SENSOR_ERR("read fail\n");
data->md_ver = 0;
return -1;
}
}
data->md_ver = (u8)buf;
SENSOR_INFO( "md_ver : 0x%x\n", data->md_ver);
return 0;
}
static void tc3xxk_gpio_request(struct tc3xxk_data *data)
{
int ret = 0;
SENSOR_INFO("\n");
if (!data->pdata->i2c_gpio) {
ret = gpio_request(data->pdata->gpio_scl, "grip_scl");
if (ret) {
SENSOR_ERR("unable to request grip_scl [%d]\n",
data->pdata->gpio_scl);
}
ret = gpio_request(data->pdata->gpio_sda, "grip_sda");
if (ret) {
SENSOR_ERR("unable to request grip_sda [%d]\n",
data->pdata->gpio_sda);
}
}
ret = gpio_request(data->pdata->gpio_int, "grip_irq");
if (ret) {
SENSOR_ERR("unable to request grip_irq [%d]\n",
data->pdata->gpio_int);
}
}
static int tc3xxk_parse_dt(struct device *dev,
struct tc3xxk_platform_data *pdata)
{
struct device_node *np = dev->of_node;
int ret;
enum of_gpio_flags flags;
of_property_read_u32(np, "coreriver,use_bitmap", &pdata->use_bitmap);
SENSOR_INFO("%s protocol.\n",
pdata->use_bitmap ? "Use Bit-map" : "Use OLD");
pdata->gpio_scl = of_get_named_gpio_flags(np, "coreriver,scl-gpio", 0, &pdata->scl_gpio_flags);
pdata->gpio_sda = of_get_named_gpio_flags(np, "coreriver,sda-gpio", 0, &pdata->sda_gpio_flags);
pdata->gpio_int = of_get_named_gpio_flags(np, "coreriver,irq-gpio", 0, &pdata->irq_gpio_flags);
pdata->ldo_en = of_get_named_gpio_flags(np, "coreriver,ldo_en", 0, &flags);
if (pdata->ldo_en < 0) {
SENSOR_ERR("fail to get ldo_en\n");
pdata->ldo_en = 0;
if (of_property_read_string(np, "coreriver,regulator_ic", &pdata->regulator_ic)) {
SENSOR_ERR("Failed to get regulator_ic name property\n");
return -EINVAL;
}
regulator_ic = pdata->regulator_ic;
} else {
ret = gpio_request(pdata->ldo_en, "grip_ldo_en");
if (ret < 0) {
SENSOR_ERR("gpio %d request failed %d\n", pdata->ldo_en, ret);
return ret;
}
gpio_direction_output(pdata->ldo_en, 0);
}
pdata->boot_on_ldo = of_property_read_bool(np, "coreriver,boot-on-ldo");
pdata->i2c_gpio = of_property_read_bool(np, "coreriver,i2c-gpio");
if (of_property_read_string(np, "coreriver,fw_name", &pdata->fw_name)) {
SENSOR_ERR("Failed to get fw_name property\n");
return -EINVAL;
} else {
SENSOR_INFO("fw_name %s\n", pdata->fw_name);
}
pdata->bringup = of_property_read_bool(np, "coreriver,bringup");
if (pdata->bringup < 0)
pdata->bringup = 0;
SENSOR_INFO("grip_int:%d, ldo_en:%d\n", pdata->gpio_int, pdata->ldo_en);
return 0;
}
int tc3xxk_grip_power(void *info, bool on)
{
struct tc3xxk_data *data = (struct tc3xxk_data *)info;
struct regulator *regulator;
int ret = 0;
if (tc3xxk_power_enabled == on)
return 0;
SENSOR_INFO("%s\n", on ? "on" : "off");
/* ldo control*/
if (data->pdata->ldo_en) {
gpio_set_value(data->pdata->ldo_en, on);
SENSOR_INFO("ldo_en power %d\n", on);
tc3xxk_power_enabled = on;
return 0;
}
/*regulator control*/
regulator = regulator_get(NULL, regulator_ic);
if (IS_ERR(regulator)){
SENSOR_ERR("regulator_ic get failed\n");
return -EIO;
}
if (on) {
ret = regulator_enable(regulator);
if (ret) {
SENSOR_ERR("regulator_ic enable failed\n");
return ret;
}
} else {
if (regulator_is_enabled(regulator)){
regulator_disable(regulator);
if (ret) {
SENSOR_ERR("regulator_ic disable failed\n");
return ret;
}
}
else
regulator_force_disable(regulator);
}
regulator_put(regulator);
tc3xxk_power_enabled = on;
return 0;
}
static irqreturn_t tc3xxk_interrupt(int irq, void *dev_id)
{
struct tc3xxk_data *data = dev_id;
struct i2c_client *client = data->client;
int ret, retry;
int i = 0;
u8 grip_val;
bool grip_handle_flag;
wake_lock(&data->grip_wake_lock);
SENSOR_INFO("\n");
if ((!data->enabled) || data->fw_downloading) {
SENSOR_ERR("can't excute\n");
wake_unlock(&data->grip_wake_lock);
return IRQ_HANDLED;
}
ret = tc3xxk_wake_up(client, TC300K_CMD_WAKE_UP);
ret = i2c_smbus_read_byte_data(client, TC305K_GRIPCODE);
if (ret < 0) {
retry = 3;
while (retry--) {
SENSOR_ERR("read fail ret=%d(retry:%d)\n", ret, retry);
msleep(10);
ret = i2c_smbus_read_byte_data(client, TC305K_GRIPCODE);
if (ret > 0)
break;
}
if (retry <= 0) {
tc3xxk_reset(data);
wake_unlock(&data->grip_wake_lock);
return IRQ_HANDLED;
}
}
grip_val = (u8)ret;
for (i = 0 ; i < data->grip_num * 2 ; i++){ //use 2 grip chanel
if (data->pdata->use_bitmap)
grip_handle_flag = (grip_val & data->grip_ev_val[i].grip_bitmap);
else
grip_handle_flag = (grip_val == data->grip_ev_val[i].grip_bitmap);
if (grip_handle_flag){
if(data->grip_ev_val[i].grip_status == ACTIVE){
data->grip_event = ACTIVE;
input_report_rel(data->input_dev, REL_MISC, 1);
}
else{
data->grip_event = IDLE;
input_report_rel(data->input_dev, REL_MISC, 2);
}
SENSOR_INFO(
"grip %s : %s(0x%02X) ver0x%02x\n",
data->grip_ev_val[i].grip_status? "P" : "R",
data->grip_ev_val[i].grip_name, grip_val,
data->fw_ver);
#ifdef CONFIG_SEC_FACTORY
data->diff = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_DIFF_DATA);
if (data->abnormal_mode) {
if (data->grip_event) {
if (data->max_diff < data->diff)
data->max_diff = data->diff;
data->irq_count++;
}
}
#endif
}
}
input_sync(data->input_dev);
wake_unlock(&data->grip_wake_lock);
return IRQ_HANDLED;
}
static int load_fw_in_kernel(struct tc3xxk_data *data)
{
struct i2c_client *client = data->client;
int ret;
ret = request_firmware(&data->fw, data->pdata->fw_name, &client->dev);
if (ret) {
SENSOR_ERR("fail(%d)\n", ret);
return -1;
}
data->fw_img = (struct fw_image *)data->fw->data;
SENSOR_INFO( "0x%x 0x%x firm (size=%d)\n",
data->fw_img->first_fw_ver, data->fw_img->second_fw_ver, data->fw_img->fw_len);
SENSOR_INFO("done\n");
return 0;
}
static int load_fw_sdcard(struct tc3xxk_data *data)
{
struct file *fp;
mm_segment_t old_fs;
long fsize, nread;
int ret = 0;
old_fs = get_fs();
set_fs(get_ds());
fp = filp_open(TC300K_FW_PATH_SDCARD, O_RDONLY, S_IRUSR);
if (IS_ERR(fp)) {
SENSOR_ERR("%s open error\n", TC300K_FW_PATH_SDCARD);
ret = -ENOENT;
goto fail_sdcard_open;
}
fsize = fp->f_path.dentry->d_inode->i_size;
data->fw_img = kzalloc((size_t)fsize, GFP_KERNEL);
if (!data->fw_img) {
SENSOR_ERR("fail to kzalloc for fw\n");
filp_close(fp, current->files);
ret = -ENOMEM;
goto fail_sdcard_kzalloc;
}
nread = vfs_read(fp, (char __user *)data->fw_img, fsize, &fp->f_pos);
if (nread != fsize) {
SENSOR_ERR("fail to vfs_read file\n");
ret = -EINVAL;
goto fail_sdcard_size;
}
filp_close(fp, current->files);
set_fs(old_fs);
SENSOR_INFO("fw_size : %lu\n", nread);
SENSOR_INFO("done\n");
return ret;
fail_sdcard_size:
kfree(&data->fw_img);
fail_sdcard_kzalloc:
filp_close(fp, current->files);
fail_sdcard_open:
set_fs(old_fs);
return ret;
}
static inline void setsda(struct tc3xxk_data *data, int state)
{
if (state)
gpio_direction_output(data->pdata->gpio_sda, 1);
else
gpio_direction_output(data->pdata->gpio_sda, 0);
}
static inline void setscl(struct tc3xxk_data *data, int state)
{
if (state)
gpio_direction_output(data->pdata->gpio_scl, 1);
else
gpio_direction_output(data->pdata->gpio_scl, 0);
}
static inline int getsda(struct tc3xxk_data *data)
{
return gpio_get_value(data->pdata->gpio_sda);
}
static inline int getscl(struct tc3xxk_data *data)
{
return gpio_get_value(data->pdata->gpio_scl);
}
static void send_9bit(struct tc3xxk_data *data, u8 buff)
{
int i;
setscl(data, 1);
ndelay(20);
setsda(data, 0);
ndelay(20);
setscl(data, 0);
ndelay(20);
for (i = 0; i < 8; i++) {
setscl(data, 1);
ndelay(20);
setsda(data, (buff >> i) & 0x01);
ndelay(20);
setscl(data, 0);
ndelay(20);
}
setsda(data, 0);
}
static u8 wait_9bit(struct tc3xxk_data *data)
{
int i;
int buf;
u8 send_buf = 0;
gpio_direction_input(data->pdata->gpio_sda);
getsda(data);
ndelay(10);
setscl(data, 1);
ndelay(40);
setscl(data, 0);
ndelay(20);
for (i = 0; i < 8; i++) {
setscl(data, 1);
ndelay(20);
buf = getsda(data);
ndelay(20);
setscl(data, 0);
ndelay(20);
send_buf |= (buf & 0x01) << i;
}
setsda(data, 0);
return send_buf;
}
static void tc3xxk_reset_for_isp(struct tc3xxk_data *data, bool start)
{
if (start) {
setscl(data, 0);
setsda(data, 0);
data->pdata->power(data, false);
msleep(100);
data->pdata->power(data, true);
usleep_range(5000, 6000);
} else {
data->pdata->power(data, false);
msleep(100);
data->pdata->power(data, true);
msleep(120);
gpio_direction_input(data->pdata->gpio_sda);
gpio_direction_input(data->pdata->gpio_scl);
}
}
static void load(struct tc3xxk_data *data, u8 buff)
{
send_9bit(data, TC300K_LDDATA);
udelay(1);
send_9bit(data, buff);
udelay(1);
}
static void step(struct tc3xxk_data *data, u8 buff)
{
send_9bit(data, TC300K_CCFG);
udelay(1);
send_9bit(data, buff);
udelay(2);
}
static void setpc(struct tc3xxk_data *data, u16 addr)
{
u8 buf[4];
int i;
buf[0] = 0x02;
buf[1] = addr >> 8;
buf[2] = addr & 0xff;
buf[3] = 0x00;
for (i = 0; i < 4; i++)
step(data, buf[i]);
}
static void configure_isp(struct tc3xxk_data *data)
{
u8 buf[7];
int i;
buf[0] = 0x75; buf[1] = 0xFC; buf[2] = 0xAC;
buf[3] = 0x75; buf[4] = 0xFC; buf[5] = 0x35;
buf[6] = 0x00;
/* Step(cmd) */
for (i = 0; i < 7; i++)
step(data, buf[i]);
}
static int tc3xxk_erase_fw(struct tc3xxk_data *data)
{
int i;
u8 state = 0;
tc3xxk_reset_for_isp(data, true);
/* isp_enable_condition */
send_9bit(data, TC300K_CSYNC1);
udelay(9);
send_9bit(data, TC300K_CSYNC2);
udelay(9);
send_9bit(data, TC300K_CSYNC3);
usleep_range(150, 160);
state = wait_9bit(data);
if (state != 0x01) {
SENSOR_ERR("isp enable error %d\n", state);
return -1;
}
configure_isp(data);
/* Full Chip Erase */
send_9bit(data, TC300K_PCRST);
udelay(1);
send_9bit(data, TC300K_PECHIP);
usleep_range(15000, 15500);
state = 0;
for (i = 0; i < 100; i++) {
udelay(2);
send_9bit(data, TC300K_CSYNC3);
udelay(1);
state = wait_9bit(data);
if ((state & 0x04) == 0x00)
break;
}
if (i == 100) {
SENSOR_ERR("fail\n");
return -1;
}
SENSOR_INFO("success\n");
return 0;
}
static int tc3xxk_write_fw(struct tc3xxk_data *data)
{
u16 addr = 0;
u8 code_data;
setpc(data, addr);
load(data, TC300K_PWDATA);
send_9bit(data, TC300K_LDMODE);
udelay(1);
while (addr < data->fw_img->fw_len) {
code_data = data->fw_img->data[addr++];
load(data, code_data);
usleep_range(20, 21);
}
send_9bit(data, TC300K_PEDISC);
udelay(1);
return 0;
}
static int tc3xxk_verify_fw(struct tc3xxk_data *data)
{
u16 addr = 0;
u8 code_data;
setpc(data, addr);
SENSOR_INFO( "fw code size = %#x (%u)",
data->fw_img->fw_len, data->fw_img->fw_len);
while (addr < data->fw_img->fw_len) {
if ((addr % 0x40) == 0)
SENSOR_INFO("fw verify addr = %#x\n", addr);
send_9bit(data, TC300K_PRDATA);
udelay(2);
code_data = wait_9bit(data);
udelay(1);
if (code_data != data->fw_img->data[addr++]) {
SENSOR_ERR("addr : %#x data error (0x%2x)\n",
addr - 1, code_data );
return -1;
}
}
SENSOR_INFO("success\n");
return 0;
}
static void t300k_release_fw(struct tc3xxk_data *data, u8 fw_path)
{
if (fw_path == FW_INKERNEL)
release_firmware(data->fw);
else if (fw_path == FW_SDCARD)
kfree(data->fw_img);
}
static int tc3xxk_flash_fw(struct tc3xxk_data *data, u8 fw_path)
{
int retry = 5;
int ret;
tc3xxk_config_gpio_i2c(data, 0);
do {
ret = tc3xxk_erase_fw(data);
if (ret)
SENSOR_ERR("erase fail(retry=%d)\n", retry);
else
break;
} while (retry-- > 0);
if (retry < 0)
goto err_tc3xxk_flash_fw;
retry = 5;
do {
tc3xxk_write_fw(data);
ret = tc3xxk_verify_fw(data);
if (ret)
SENSOR_ERR("verify fail(retry=%d)\n", retry);
else
break;
} while (retry-- > 0);
tc3xxk_reset_for_isp(data, false);
tc3xxk_config_gpio_i2c(data, 1);
if (retry < 0)
goto err_tc3xxk_flash_fw;
return 0;
err_tc3xxk_flash_fw:
return -1;
}
static int tc3xxk_crc_check(struct tc3xxk_data *data)
{
struct i2c_client *client = data->client;
int ret;
u8 cmd;
u8 checksum_h, checksum_l;
if ((!data->enabled) || data->fw_downloading) {
SENSOR_ERR("can't excute\n");
return -1;
}
cmd = TC300K_CMD_CAL_CHECKSUM;
ret = i2c_smbus_write_byte_data(client, TC300K_CMD_ADDR, cmd);
if (ret) {
SENSOR_ERR("command fail (%d)\n", ret);
return ret;
}
msleep(TC300K_CHECKSUM_DELAY);
ret = i2c_smbus_read_byte_data(client, TC300K_CHECKS_H);
if (ret < 0) {
SENSOR_ERR("failed to read checksum_h (%d)\n", ret);
return ret;
}
checksum_h = ret;
ret = i2c_smbus_read_byte_data(client, TC300K_CHECKS_L);
if (ret < 0) {
SENSOR_ERR("failed to read checksum_l (%d)\n", ret);
return ret;
}
checksum_l = ret;
data->checksum = (checksum_h << 8) | checksum_l;
if (data->fw_img->checksum != data->checksum) {
SENSOR_ERR("checksum fail - firm checksum(%d), compute checksum(%d)\n",
data->fw_img->checksum, data->checksum);
return -1;
}
SENSOR_INFO("success (%d)\n", data->checksum);
return 0;
}
static int tc3xxk_fw_update(struct tc3xxk_data *data, u8 fw_path, bool force)
{
int retry = 4;
int ret;
if (fw_path == FW_INKERNEL) {
ret = load_fw_in_kernel(data);
if (ret)
return -1;
data->fw_ver_bin = data->fw_img->first_fw_ver;
data->md_ver_bin = data->fw_img->second_fw_ver;
/* read model ver */
ret = tc3xxk_get_md_version(data, false);
if (ret) {
SENSOR_ERR("get md version fail\n");
force = 1;
}
if (data->md_ver != data->md_ver_bin) {
SENSOR_INFO( "fw model number = %x ic model number = %x \n", data->md_ver_bin, data->md_ver);
force = 1;
}
if (!force && (data->fw_ver >= data->fw_ver_bin)) {
SENSOR_INFO( "do not need firm update (IC:0x%x, BIN:0x%x)(MD IC:0x%x, BIN:0x%x)\n",
data->fw_ver, data->fw_ver_bin, data->md_ver, data->md_ver_bin);
t300k_release_fw(data, fw_path);
return 0;
}
} else if (fw_path == FW_SDCARD) {
ret = load_fw_sdcard(data);
if (ret)
return -1;
}
while (retry--) {
data->fw_downloading = true;
ret = tc3xxk_flash_fw(data, fw_path);
data->fw_downloading = false;
if (ret) {
SENSOR_ERR("tc3xxk_flash_fw fail (%d)\n", retry);
continue;
}
ret = tc3xxk_get_fw_version(data, false);
if (ret) {
SENSOR_ERR("tc3xxk_get_fw_version fail (%d)\n", retry);
continue;
}
if (data->fw_ver != data->fw_img->first_fw_ver) {
SENSOR_ERR("fw version fail (0x%x, 0x%x)(%d)\n",
data->fw_ver, data->fw_img->first_fw_ver, retry);
continue;
}
ret = tc3xxk_get_md_version(data, false);
if (ret) {
SENSOR_ERR("tc3xxk_get_md_version fail (%d)\n", retry);
continue;
}
if (data->md_ver != data->fw_img->second_fw_ver) {
SENSOR_ERR("md version fail (0x%x, 0x%x)(%d)\n",
data->md_ver, data->fw_img->second_fw_ver, retry);
continue;
}
ret = tc3xxk_crc_check(data);
if (ret) {
SENSOR_ERR("crc check fail (%d)\n", retry);
continue;
}
break;
}
if (retry > 0)
SENSOR_INFO("success\n");
t300k_release_fw(data, fw_path);
return ret;
}
/*
* Fw update by parameters:
* s | S = TSK FW from kernel binary and compare fw version.
* i | I = TSK FW from SD Card and Not compare fw version.
* f | F = TSK FW from kernel binary and Not compare fw version.
*/
static ssize_t tc3xxk_update_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;
u8 fw_path;
bool fw_update_force = false;
switch(*buf) {
case 's':
case 'S':
fw_path = FW_INKERNEL;
fw_update_force = true;
break;
case 'i':
case 'I':
fw_path = FW_SDCARD;
break;
case 'f':
case 'F':
fw_path = FW_INKERNEL;
fw_update_force = true;
break;
default:
SENSOR_ERR("wrong command fail\n");
data->fw_update_status = TK_UPDATE_FAIL;
return count;
}
data->fw_update_status = TK_UPDATE_DOWN;
if (data->irq_check) {
data->irq_check = false;
disable_irq_wake(client->irq);
disable_irq(client->irq);
}
ret = tc3xxk_fw_update(data, fw_path, fw_update_force);
if (!data->irq_check) {
data->irq_check = true;
enable_irq(client->irq);
enable_irq_wake(client->irq);
}
if (ret < 0) {
SENSOR_ERR("fail\n");
data->fw_update_status = TK_UPDATE_FAIL;
} else
data->fw_update_status = TK_UPDATE_PASS;
return count;
}
static ssize_t tc3xxk_firm_status_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
if (data->fw_update_status == TK_UPDATE_PASS)
ret = sprintf(buf, "PASS\n");
else if (data->fw_update_status == TK_UPDATE_DOWN)
ret = sprintf(buf, "DOWNLOADING\n");
else if (data->fw_update_status == TK_UPDATE_FAIL)
ret = sprintf(buf, "FAIL\n");
else
ret = sprintf(buf, "NG\n");
return ret;
}
static ssize_t tc3xxk_firm_version_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
return sprintf(buf, "0x%02x%02x\n", data->md_ver_bin, data->fw_ver_bin);
}
static ssize_t tc3xxk_md_version_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
return sprintf(buf, "0x%02x\n", data->md_ver_bin);
}
static ssize_t tc3xxk_firm_version_read_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = tc3xxk_get_fw_version(data, false);
if (ret < 0)
SENSOR_ERR("failed to read firmware version (%d)\n", ret);
ret = tc3xxk_get_md_version(data, false);
if (ret < 0)
SENSOR_ERR("failed to read md version (%d)\n", ret);
return sprintf(buf, "0x%02x%02x\n", data->md_ver, data->fw_ver);
}
static ssize_t tc3xxk_md_version_read_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = tc3xxk_get_md_version(data, false);
if (ret < 0)
SENSOR_ERR("failed to read md version (%d)\n", ret);
return sprintf(buf, "0x%02x\n", data->md_ver);
}
static int read_tc3xxk_register_data(struct tc3xxk_data *data, int read_key_num, int read_offset)
{
struct i2c_client *client = data->client;
int ret;
u8 buff[2];
int value;
mutex_lock(&data->lock_fac);
ret = i2c_smbus_read_i2c_block_data(client, read_key_num + read_offset, TC350K_DATA_SIZE, buff);
if (ret != TC350K_DATA_SIZE) {
SENSOR_ERR("read fail(%d)\n", ret);
value = 0;
goto exit;
}
value = (buff[TC350K_DATA_H_OFFSET] << 8) | buff[TC350K_DATA_L_OFFSET];
mutex_unlock(&data->lock_fac);
SENSOR_INFO("read key num/offset = [0x%X/0x%X], value : [%d]\n",
read_key_num, read_offset, value);
exit:
return value;
}
static int tc3xxk_mode_enable(struct i2c_client *client, u8 cmd)
{
int ret;
ret = i2c_smbus_write_byte_data(client, TC300K_CMD_ADDR, cmd);
msleep(15);
return ret;
}
static ssize_t tc3xxk_sar_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%u\n", !data->sar_enable_off);
}
static ssize_t tc3xxk_sar_enable_store(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int buff;
int ret;
bool on;
int cmd;
ret = sscanf(buf, "%d", &buff);
if (ret != 1) {
SENSOR_ERR("cmd read err\n");
return count;
}
SENSOR_INFO(" (%d) \n", buff);
if (!(buff >= 0 && buff <= 3)) {
SENSOR_ERR("wrong command(%d)\n", buff);
return count;
}
/* sar enable param
* 0 off
* 1 on
* 2 force off
* 3 force off -> on
*/
if (data->sar_enable && buff == 1) {
SENSOR_INFO("Grip sensor already ON\n");
return count;
} else if (!data->sar_enable && buff == 0) {
SENSOR_INFO("Grip sensor already OFF\n");
return count;
}
if (buff == 3) {
data->sar_enable_off = 0;
SENSOR_INFO("Power back off _ force off -> on (%d)\n",
data->sar_enable);
if (!data->sar_enable)
buff = 1;
else
return count;
}
if (data->sar_enable_off) {
if (buff == 1)
data->sar_enable = true;
else
data->sar_enable = false;
SENSOR_INFO("skip, Power back off _ force off mode (%d)\n",
data->sar_enable);
return count;
}
if (buff == 1) {
on = true;
if (!data->irq_check) {
data->irq_check = true;
enable_irq(client->irq);
enable_irq_wake(client->irq);
}
cmd = TC300K_CMD_SAR_ENABLE;
} else if (buff == 2) {
on = false;
data->sar_enable_off = 1;
if (data->irq_check) {
data->irq_check = false;
disable_irq_wake(client->irq);
disable_irq(client->irq);
}
cmd = TC300K_CMD_SAR_DISABLE;
} else {
on = false;
if (data->irq_check) {
data->irq_check = false;
disable_irq_wake(client->irq);
disable_irq(client->irq);
}
cmd = TC300K_CMD_SAR_DISABLE;
}
ret = tc3xxk_wake_up(data->client, TC300K_CMD_WAKE_UP);
ret = tc3xxk_mode_enable(data->client, cmd);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
return count;
}
if (buff == 1) {
data->sar_enable = true;
} else {
input_report_rel(data->input_dev, REL_MISC, 2);
input_sync(data->input_dev);
data->grip_event = 0;
data->sar_enable = false;
}
SENSOR_INFO("data:%d on:%d\n", buff, on);
return count;
}
static ssize_t tc3xxk_grip1_threshold_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_THD_PRESS);
if (ret < 0) {
SENSOR_ERR("fail to read press thd(%d)\n", ret);
data->grip_p_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_p_thd = ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_THD_RELEASE);
if (ret < 0) {
SENSOR_ERR("fail to read release thd(%d)\n", ret);
data->grip_r_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_r_thd = ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_THD_NOISE);
if (ret < 0) {
SENSOR_ERR("fail to read noise thd(%d)\n", ret);
data->grip_n_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_n_thd = ret;
return sprintf(buf, "%d,%d,%d\n",
data->grip_p_thd, data->grip_r_thd, data->grip_n_thd );
}
static ssize_t tc3xxk_grip2_threshold_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_THD_PRESS);
if (ret < 0) {
SENSOR_ERR("fail to read press thd(%d)\n", ret);
data->grip_p_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_p_thd = ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_THD_RELEASE);
if (ret < 0) {
SENSOR_ERR("fail to read release thd(%d)\n", ret);
data->grip_r_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_r_thd = ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_THD_NOISE);
if (ret < 0) {
SENSOR_ERR("fail to read noise thd(%d)\n", ret);
data->grip_n_thd = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_n_thd = ret;
return sprintf(buf, "%d,%d,%d\n",
data->grip_p_thd, data->grip_r_thd, data->grip_n_thd );
}
static ssize_t tc3xxk_total_cap1_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;
ret = i2c_smbus_read_byte_data(client, TC305K_1GRIP + TC305K_GRIP_TOTAL_CAP);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
return sprintf(buf, "%d\n", 0);
}
return sprintf(buf, "%d\n", ret);
}
static ssize_t tc3xxk_total_cap2_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;
ret = i2c_smbus_read_byte_data(client, TC305K_2GRIP + TC305K_GRIP_TOTAL_CAP);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
return sprintf(buf, "%d\n", 0);
}
return sprintf(buf, "%d\n", ret);
}
static ssize_t grip_ref_cap_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
struct i2c_client *client = data->client;
int ret;
ret = i2c_smbus_read_byte_data(client, TC305K_GRIP_REF_CAP);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
return sprintf(buf, "%d\n", 0);
}
return sprintf(buf, "%d\n", ret);
}
static ssize_t tc3xxk_grip1_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_DIFF_DATA);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_s1 = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_s1 = ret;
return sprintf(buf, "%d\n", data->grip_s1);
}
static ssize_t tc3xxk_grip2_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_DIFF_DATA);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_s1 = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_s1 = ret;
return sprintf(buf, "%d\n", data->grip_s1);
}
static ssize_t tc3xxk_grip1_baseline_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_BASELINE);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_baseline = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_baseline = ret;
return sprintf(buf, "%d\n", data->grip_baseline);
}
static ssize_t tc3xxk_grip2_baseline_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_BASELINE);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_baseline = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_baseline = ret;
return sprintf(buf, "%d\n", data->grip_baseline);
}
static ssize_t tc3xxk_grip1_raw_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_1GRIP, TC305K_GRIP_RAW_DATA);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_raw1 = 0;
data->grip_raw2 = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_raw1 = ret;
data->grip_raw2 = 0;
return sprintf(buf, "%d,%d\n", data->grip_raw1, data->grip_raw2);
}
static ssize_t tc3xxk_grip2_raw_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = read_tc3xxk_register_data(data, TC305K_2GRIP, TC305K_GRIP_RAW_DATA);
if (ret < 0) {
SENSOR_ERR("fail(%d)\n", ret);
data->grip_raw1 = 0;
data->grip_raw2 = 0;
return sprintf(buf, "%d\n", 0);
}
data->grip_raw1 = ret;
data->grip_raw2 = 0;
return sprintf(buf, "%d,%d\n", data->grip_raw1, data->grip_raw2);
}
static ssize_t tc3xxk_grip_gain_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%d,%d,%d,%d\n", 0, 0, 0, 0);
}
static ssize_t tc3xxk_grip_check_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
SENSOR_ERR("event:%d\n", data->grip_event);
return sprintf(buf, "%d\n", data->grip_event);
}
static ssize_t tc3xxk_grip_sw_reset(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int buff;
int ret;
ret = sscanf(buf, "%d", &buff);
if (ret != 1) {
SENSOR_ERR("cmd read err\n");
return count;
}
if (!(buff == 1)) {
SENSOR_ERR("wrong command(%d)\n", buff);
return count;
}
data->grip_event = 0;
SENSOR_INFO("data(%d)\n", buff);
tc3xxk_grip_cal_reset(data);
return count;
}
static ssize_t grip_sensing_change(struct device *dev,
struct device_attribute *attr, const char *buf,
size_t count)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret, buff;
ret = sscanf(buf, "%d", &buff);
if (ret != 1) {
SENSOR_ERR("cmd read err\n");
return count;
}
if (!(buff == 0 || buff == 1)) {
SENSOR_ERR("wrong command(%d)\n", buff);
return count;
}
grip_sar_sensing(data, buff);
SENSOR_INFO("earjack (%d)\n", buff);
return count;
}
#ifdef CONFIG_SEC_FACTORY
static ssize_t tc3xxk_grip_irq_count_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int result = 0;
if (data->irq_count)
result = -1;
SENSOR_INFO("\n");
return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n",
result, data->irq_count, data->max_diff);
}
static ssize_t tc3xxk_grip_irq_count_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
u8 onoff;
int ret;
ret = kstrtou8(buf, 10, &onoff);
if (ret < 0) {
SENSOR_ERR("kstrtou8 failed.(%d)\n", ret);
return count;
}
mutex_lock(&data->lock_fac);
if (onoff == 0) {
data->abnormal_mode = 0;
} else if (onoff == 1) {
data->abnormal_mode = 1;
data->irq_count = 0;
data->max_diff = 0;
} else {
SENSOR_ERR("unknown value %d\n", onoff);
}
mutex_unlock(&data->lock_fac);
SENSOR_INFO("%d\n", onoff);
return count;
}
#endif //#ifdef CONFIG_SEC_FACTORY
static ssize_t grip_chip_name(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", MODEL_NAME);
}
static ssize_t grip_vendor_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", VENDOR_NAME);
}
static ssize_t grip_crc_check_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
int ret;
ret = tc3xxk_crc_check(data);
return sprintf(buf, (ret == 0) ? "OK,%x\n" : "NG,%x\n", data->checksum);
}
static ssize_t tc3xxk_enable_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct tc3xxk_data *data = dev_get_drvdata(dev);
return snprintf(buf, PAGE_SIZE, "%d\n", data->sar_enable);
}
static DEVICE_ATTR(grip_firm_update, 0220, NULL, tc3xxk_update_store);
static DEVICE_ATTR(grip_firm_update_status, 0444, tc3xxk_firm_status_show, NULL);
static DEVICE_ATTR(grip_firm_version_phone, 0444, tc3xxk_firm_version_show, NULL);
static DEVICE_ATTR(grip_firm_version_panel, 0444, tc3xxk_firm_version_read_show, NULL);
static DEVICE_ATTR(grip_md_version_phone, 0444, tc3xxk_md_version_show, NULL);
static DEVICE_ATTR(grip_md_version_panel, 0444, tc3xxk_md_version_read_show, NULL);
static DEVICE_ATTR(grip_threshold, 0444, tc3xxk_grip1_threshold_show, NULL);
static DEVICE_ATTR(grip2ch_threshold, 0444, tc3xxk_grip2_threshold_show, NULL);
static DEVICE_ATTR(grip_total_cap, 0444, tc3xxk_total_cap1_show, NULL);
static DEVICE_ATTR(grip_total_cap2ch, 0444, tc3xxk_total_cap2_show, NULL);
static DEVICE_ATTR(grip_sar_enable, 0664, tc3xxk_sar_enable_show, tc3xxk_sar_enable_store);
static DEVICE_ATTR(grip_sw_reset, 0220, NULL, tc3xxk_grip_sw_reset);
static DEVICE_ATTR(grip_earjack, 0220, NULL, grip_sensing_change);
static DEVICE_ATTR(grip, 0444, tc3xxk_grip1_show, NULL);
static DEVICE_ATTR(grip2ch, 0444, tc3xxk_grip2_show, NULL);
static DEVICE_ATTR(grip_baseline, 0444, tc3xxk_grip1_baseline_show, NULL);
static DEVICE_ATTR(grip2ch_baseline, 0444, tc3xxk_grip2_baseline_show, NULL);
static DEVICE_ATTR(grip_raw, 0444, tc3xxk_grip1_raw_show, NULL);
static DEVICE_ATTR(grip2ch_raw, 0444, tc3xxk_grip2_raw_show, NULL);
static DEVICE_ATTR(grip_gain, 0444, tc3xxk_grip_gain_show, NULL);
static DEVICE_ATTR(grip_check, 0444, tc3xxk_grip_check_show, NULL);
#ifdef CONFIG_SEC_FACTORY
static DEVICE_ATTR(grip_irq_count, 0664, tc3xxk_grip_irq_count_show, tc3xxk_grip_irq_count_store);
#endif
static DEVICE_ATTR(grip_ref_cap, 0444, grip_ref_cap_show, NULL);
static DEVICE_ATTR(name, 0444, grip_chip_name, NULL);
static DEVICE_ATTR(vendor, 0444, grip_vendor_show, NULL);
static DEVICE_ATTR(grip_crc_check, 0444, grip_crc_check_show, NULL);
static struct device_attribute *sec_grip_attributes[] = {
&dev_attr_grip_firm_update,
&dev_attr_grip_firm_update_status,
&dev_attr_grip_firm_version_phone,
&dev_attr_grip_firm_version_panel,
&dev_attr_grip_md_version_phone,
&dev_attr_grip_md_version_panel,
&dev_attr_grip_threshold,
&dev_attr_grip2ch_threshold,
&dev_attr_grip_total_cap,
&dev_attr_grip_total_cap2ch,
&dev_attr_grip_sar_enable,
&dev_attr_grip_sw_reset,
&dev_attr_grip_earjack,
&dev_attr_grip,
&dev_attr_grip2ch,
&dev_attr_grip_baseline,
&dev_attr_grip2ch_baseline,
&dev_attr_grip_raw,
&dev_attr_grip2ch_raw,
&dev_attr_grip_gain,
&dev_attr_grip_check,
#ifdef CONFIG_SEC_FACTORY
&dev_attr_grip_irq_count,
#endif
&dev_attr_grip_ref_cap,
&dev_attr_name,
&dev_attr_vendor,
&dev_attr_grip_crc_check,
NULL,
};
static DEVICE_ATTR(enable, 0664, tc3xxk_enable_show, tc3xxk_sar_enable_store);
static struct attribute *tc3xxk_attributes[] = {
&dev_attr_enable.attr,
NULL
};
static struct attribute_group tc3xxk_attribute_group = {
.attrs = tc3xxk_attributes
};
#if defined (CONFIG_VBUS_NOTIFIER)
static int tc3xxk_vbus_notification(struct notifier_block *nb,
unsigned long cmd, void *data)
{
struct tc3xxk_data *tkey_data = container_of(nb, struct tc3xxk_data, vbus_nb);
struct i2c_client *client = tkey_data->client;
vbus_status_t vbus_type = *(vbus_status_t *)data;
int ret;
SENSOR_INFO("cmd=%lu, vbus_type=%d\n", cmd, vbus_type);
switch (vbus_type) {
case STATUS_VBUS_HIGH:
SENSOR_INFO("attach\n");
ret = tc3xxk_wake_up(client, TC300K_CMD_WAKE_UP);
ret = tc3xxk_mode_enable(client, TC300K_CMD_TA_ON);
if (ret < 0)
SENSOR_ERR("TA mode ON fail(%d)\n", ret);
break;
case STATUS_VBUS_LOW:
SENSOR_INFO("detach\n");
ret = tc3xxk_wake_up(client, TC300K_CMD_WAKE_UP);
ret = tc3xxk_mode_enable(client, TC300K_CMD_TA_OFF);
if (ret < 0)
SENSOR_ERR("TA mode OFF fail(%d)\n", ret);
break;
default:
break;
}
return 0;
}
#endif
static int tc3xxk_fw_check(struct tc3xxk_data *data)
{
int ret;
if (data->pdata->bringup) {
SENSOR_INFO("firmware update skip, bring up\n");
return 0;
}
ret = tc3xxk_get_fw_version(data, true);
if (ret < 0) {
SENSOR_ERR("i2c fail...[%d], addr[%d]\n",
ret, data->client->addr);
data->fw_ver = 0xFF;
SENSOR_INFO("firmware is blank or i2c fail, try flash firmware to grip\n");
}
if (data->fw_ver == 0xFF) {
SENSOR_INFO(
"fw version 0xFF, Excute firmware update!\n");
ret = tc3xxk_fw_update(data, FW_INKERNEL, true);
if (ret)
return -1;
} else {
ret = tc3xxk_fw_update(data, FW_INKERNEL, false);
if (ret)
return -1;
}
return 0;
}
static int tc3xxk_pinctrl_init(struct tc3xxk_data *data)
{
struct device *dev = &data->client->dev;
int i;
SENSOR_INFO("\n");
// IRQ
data->pinctrl_irq = devm_pinctrl_get(dev);
if (IS_ERR(data->pinctrl_irq)) {
SENSOR_INFO("Failed to get irq pinctrl\n");
data->pinctrl_irq = NULL;
goto i2c_pinctrl_get;
}
#if 0
for (i = 0; i < 2; ++i) {
data->pin_state[i] = pinctrl_lookup_state(data->pinctrl_irq, str_states[i]);
if (IS_ERR(data->pin_state[i])) {
SENSOR_INFO("Failed to get irq pinctrl state\n");
devm_pinctrl_put(data->pinctrl_irq);
data->pinctrl_irq = NULL;
goto i2c_pinctrl_get;
}
}
#endif
i2c_pinctrl_get:
/* for h/w i2c */
dev = data->client->dev.parent->parent;
SENSOR_INFO("use dev's parent\n");
// I2C
data->pinctrl_i2c = devm_pinctrl_get(dev);
if (IS_ERR(data->pinctrl_i2c)) {
SENSOR_ERR("Failed to get i2c pinctrl\n");
goto err_pinctrl_get_i2c;
}
for (i = 2; i < 4; ++i) {
data->pin_state[i] = pinctrl_lookup_state(data->pinctrl_i2c, str_states[i]);
if (IS_ERR(data->pin_state[i])) {
SENSOR_ERR("Failed to get i2c pinctrl state\n");
goto err_pinctrl_get_state_i2c;
}
}
return 0;
err_pinctrl_get_state_i2c:
devm_pinctrl_put(data->pinctrl_i2c);
err_pinctrl_get_i2c:
return -ENODEV;
}
static int tc3xxk_pinctrl(struct tc3xxk_data *data, int state)
{
struct pinctrl *pinctrl_i2c = data->pinctrl_i2c;
struct pinctrl *pinctrl_irq = data->pinctrl_irq;
int ret=0;
switch (state) {
case I_STATE_ON_IRQ:
case I_STATE_OFF_IRQ:
if (pinctrl_irq)
ret = pinctrl_select_state(pinctrl_irq, data->pin_state[state]);
break;
case I_STATE_ON_I2C:
case I_STATE_OFF_I2C:
if (pinctrl_i2c)
ret = pinctrl_select_state(pinctrl_i2c, data->pin_state[state]);
break;
}
if (ret < 0) {
SENSOR_ERR(
"Failed to configure tc3xxk_pinctrl state[%d]\n", state);
return ret;
}
return 0;
}
static void tc3xxk_config_gpio_i2c(struct tc3xxk_data *data, int onoff)
{
SENSOR_ERR("\n");
tc3xxk_pinctrl(data, onoff ? I_STATE_ON_I2C : I_STATE_OFF_I2C);
mdelay(100);
}
static int tc3xxk_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct tc3xxk_platform_data *pdata;
struct tc3xxk_data *data;
struct input_dev *input_dev;
int ret = 0;
SENSOR_INFO("\n");
#ifdef CONFIG_BATTERY_SAMSUNG
if (lpcharge == 1) {
SENSOR_ERR("Do not load driver due to : lpm %d\n", lpcharge);
return -ENODEV;
}
#endif
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
SENSOR_ERR("i2c_check_functionality fail\n");
return -EIO;
}
if (client->dev.of_node) {
pdata = devm_kzalloc(&client->dev,
sizeof(struct tc3xxk_platform_data),
GFP_KERNEL);
if (!pdata) {
SENSOR_ERR("Failed to allocate memory\n");
ret = -ENOMEM;
goto err_alloc_data;
}
ret = tc3xxk_parse_dt(&client->dev, pdata);
if (ret)
goto err_alloc_data;
}else
pdata = client->dev.platform_data;
data = kzalloc(sizeof(struct tc3xxk_data), GFP_KERNEL);
if (!data) {
SENSOR_ERR("Failed to allocate memory\n");
ret = -ENOMEM;
goto err_alloc_data;
}
data->pdata = pdata;
input_dev = input_allocate_device();
if (!input_dev) {
SENSOR_ERR("Failed to allocate memory for input device\n");
ret = -ENOMEM;
goto err_alloc_input;
}
data->input_dev = input_dev;
data->client = client;
if (data->pdata == NULL) {
SENSOR_ERR("failed to get platform data\n");
input_free_device(input_dev);
ret = -EINVAL;
goto err_platform_data;
}
data->irq = -1;
mutex_init(&data->lock_fac);
wake_lock_init(&data->grip_wake_lock, WAKE_LOCK_SUSPEND, "grip wake lock");
INIT_DELAYED_WORK(&data->debug_work, tc3xxk_debug_work_func);
pdata->power = tc3xxk_grip_power;
i2c_set_clientdata(client, data);
tc3xxk_gpio_request(data);
ret = tc3xxk_pinctrl_init(data);
if (ret < 0) {
SENSOR_ERR("Failed to init pinctrl: %d\n", ret);
goto err_pinctrl_init;
}
if(pdata->boot_on_ldo){
data->pdata->power(data, true);
} else {
data->pdata->power(data, true);
msleep(200);
}
data->enabled = true;
client->irq = gpio_to_irq(pdata->gpio_int);
ret = tc3xxk_fw_check(data);
if (ret) {
SENSOR_ERR("failed to firmware check(%d)\n", ret);
goto err_fw_check;
}
input_dev->name = MODULE_NAME;
input_dev->id.bustype = BUS_I2C;
data->grip_ev_val = grip_ev;
data->grip_num = ARRAY_SIZE(grip_ev)/2;
SENSOR_INFO( "number of grips = %d\n", data->grip_num);
input_set_capability(input_dev, EV_REL, REL_MISC);
input_set_drvdata(input_dev, data);
ret = input_register_device(input_dev);
if (ret) {
SENSOR_ERR("fail to register input_dev (%d)\n", ret);
goto err_register_input_dev;
}
ret = sensors_create_symlink(input_dev);
if (ret < 0) {
SENSOR_ERR("Failed to create sysfs symlink\n");
goto err_sysfs_symlink;
}
ret = sysfs_create_group(&input_dev->dev.kobj,
&tc3xxk_attribute_group);
if (ret < 0) {
SENSOR_ERR("Failed to create sysfs group\n");
goto err_sysfs_group;
}
ret = sensors_register(data->dev, data, sec_grip_attributes, MODULE_NAME);
if (ret) {
SENSOR_ERR("could not register grip_sensor(%d)\n", ret);
goto err_sensor_register;
}
data->dev = &client->dev;
#if defined (CONFIG_VBUS_NOTIFIER)
vbus_notifier_register(&data->vbus_nb, tc3xxk_vbus_notification,
VBUS_NOTIFY_DEV_CHARGER);
#endif
ret = request_threaded_irq(client->irq, NULL, tc3xxk_interrupt,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, MODEL_NAME, data);
if (ret < 0) {
SENSOR_ERR("fail to request irq (%d).\n",
pdata->gpio_int);
goto err_request_irq;
}
disable_irq(client->irq);
data->irq = pdata->gpio_int;
data->irq_check = false;
// default working on stop mode
ret = tc3xxk_wake_up(data->client, TC300K_CMD_WAKE_UP);
ret = tc3xxk_mode_enable(data->client, TC300K_CMD_SAR_DISABLE);
if (ret < 0) {
SENSOR_ERR("Change mode fail(%d)\n", ret);
}
ret = tc3xxk_mode_check(client);
if (ret >= 0) {
data->sar_enable = !!(ret & TC300K_MODE_SAR);
SENSOR_INFO("mode %d, sar %d\n", ret, data->sar_enable);
}
device_init_wakeup(&client->dev, true);
schedule_delayed_work(&data->debug_work, msecs_to_jiffies(20000));
SENSOR_INFO("done\n");
return 0;
err_request_irq:
sensors_unregister(data->dev, sec_grip_attributes);
err_sensor_register:
sysfs_remove_group(&input_dev->dev.kobj, &tc3xxk_attribute_group);
err_sysfs_group:
sensors_remove_symlink(input_dev);
err_sysfs_symlink:
input_unregister_device(input_dev);
input_dev = NULL;
err_register_input_dev:
err_fw_check:
data->pdata->power(data, false);
err_pinctrl_init:
mutex_destroy(&data->lock_fac);
wake_lock_destroy(&data->grip_wake_lock);
err_platform_data:
err_alloc_input:
kfree(data);
err_alloc_data:
SENSOR_ERR("failed\n");
return ret;
}
static int tc3xxk_remove(struct i2c_client *client)
{
struct tc3xxk_data *data = i2c_get_clientdata(client);
device_init_wakeup(&client->dev, false);
wake_lock_destroy(&data->grip_wake_lock);
free_irq(client->irq, data);
input_unregister_device(data->input_dev);
mutex_destroy(&data->lock_fac);
data->pdata->power(data, false);
gpio_free(data->pdata->gpio_int);
gpio_free(data->pdata->gpio_sda);
gpio_free(data->pdata->gpio_scl);
kfree(data);
return 0;
}
static void tc3xxk_shutdown(struct i2c_client *client)
{
struct tc3xxk_data *data = i2c_get_clientdata(client);
SENSOR_INFO("\n");
cancel_delayed_work_sync(&data->debug_work);
device_init_wakeup(&client->dev, false);
wake_lock_destroy(&data->grip_wake_lock);
disable_irq(client->irq);
data->pdata->power(data, false);
}
static int tc3xxk_suspend(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct tc3xxk_data *data = i2c_get_clientdata(client);
SENSOR_INFO("sar_enable(%d)\n", data->sar_enable);
cancel_delayed_work_sync(&data->debug_work);
return 0;
}
static int tc3xxk_resume(struct device *dev)
{
struct i2c_client *client = to_i2c_client(dev);
struct tc3xxk_data *data = i2c_get_clientdata(client);
SENSOR_INFO("sar_enable(%d)\n", data->sar_enable);
schedule_delayed_work(&data->debug_work, msecs_to_jiffies(1000));
return 0;
}
static const struct i2c_device_id tc3xxk_id[] = {
{MODEL_NAME, 0},
{ }
};
MODULE_DEVICE_TABLE(i2c, tc3xxk_id);
static struct of_device_id coreriver_match_table[] = {
{ .compatible = "coreriver,tc3xx-grip",},
{ },
};
static const struct dev_pm_ops tc3xxk_pm_ops = {
.suspend = tc3xxk_suspend,
.resume = tc3xxk_resume,
};
static struct i2c_driver tc3xxk_driver = {
.probe = tc3xxk_probe,
.remove = tc3xxk_remove,
.shutdown = tc3xxk_shutdown,
.driver = {
.name = MODEL_NAME,
.owner = THIS_MODULE,
.pm = &tc3xxk_pm_ops,
.of_match_table = coreriver_match_table,
},
.id_table = tc3xxk_id,
};
static int __init tc3xxk_init(void)
{
return i2c_add_driver(&tc3xxk_driver);
}
static void __exit tc3xxk_exit(void)
{
i2c_del_driver(&tc3xxk_driver);
}
module_init(tc3xxk_init);
module_exit(tc3xxk_exit);
MODULE_AUTHOR("Samsung Electronics");
MODULE_DESCRIPTION("Grip Sensor driver for Coreriver TC3XXK");
MODULE_LICENSE("GPL");