From 229c55ede50bd06987ec888ad1833f04caf0e9e1 Mon Sep 17 00:00:00 2001 From: luowei Date: Wed, 26 Sep 2012 17:45:01 +0800 Subject: [PATCH] sensors:add light sensor isl29023 support --- drivers/input/sensors/lsensor/Kconfig | 4 + drivers/input/sensors/lsensor/Makefile | 3 +- drivers/input/sensors/lsensor/isl29023.c | 276 +++++++++++++++++++++++ drivers/input/sensors/sensor-dev.c | 37 ++- include/linux/sensor-dev.h | 1 + 5 files changed, 314 insertions(+), 7 deletions(-) create mode 100755 drivers/input/sensors/lsensor/isl29023.c mode change 100644 => 100755 include/linux/sensor-dev.h diff --git a/drivers/input/sensors/lsensor/Kconfig b/drivers/input/sensors/lsensor/Kconfig index 5cd38076dc3d..18bde7618f5d 100755 --- a/drivers/input/sensors/lsensor/Kconfig +++ b/drivers/input/sensors/lsensor/Kconfig @@ -17,6 +17,10 @@ config LS_AL3006 config LS_STK3171 bool "light sensor stk3171" + default n + +config LS_ISL29023 + bool "light sensor isl29023" default n endif diff --git a/drivers/input/sensors/lsensor/Makefile b/drivers/input/sensors/lsensor/Makefile index 98836f434c06..5127b4396f42 100755 --- a/drivers/input/sensors/lsensor/Makefile +++ b/drivers/input/sensors/lsensor/Makefile @@ -2,4 +2,5 @@ obj-$(CONFIG_LS_CM3217) += cm3217.o obj-$(CONFIG_LS_AL3006) += ls_al3006.o -obj-$(CONFIG_LS_STK3171) += ls_stk3171.o \ No newline at end of file +obj-$(CONFIG_LS_STK3171) += ls_stk3171.o +obj-$(CONFIG_LS_ISL29023) += isl29023.o \ No newline at end of file diff --git a/drivers/input/sensors/lsensor/isl29023.c b/drivers/input/sensors/lsensor/isl29023.c new file mode 100755 index 000000000000..d71c7cecbeb0 --- /dev/null +++ b/drivers/input/sensors/lsensor/isl29023.c @@ -0,0 +1,276 @@ +/* drivers/input/sensors/access/kxtik.c + * + * Copyright (C) 2012-2015 ROCKCHIP. + * Author: luowei + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include + +#if 0 +#define SENSOR_DEBUG_TYPE SENSOR_TYPE_LIGHT +#define DBG(x...) if(sensor->pdata->type == SENSOR_DEBUG_TYPE) printk(x) +#else +#define DBG(x...) +#endif + +#define ISL29023_REG_ADD_COMMAND1 0x00 +#define COMMMAND1_OPMODE_SHIFT 5 +#define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT) +#define COMMMAND1_OPMODE_POWER_DOWN (0 << COMMMAND1_OPMODE_SHIFT) +#define COMMMAND1_OPMODE_ALS_ONCE (1 << COMMMAND1_OPMODE_SHIFT) +#define COMMMAND1_OPMODE_IR_ONCE (2 << COMMMAND1_OPMODE_SHIFT) +#define COMMMAND1_OPMODE_ALS_CONTINUE (5 << COMMMAND1_OPMODE_SHIFT) +#define COMMMAND1_OPMODE_IR_CONTINUE (6 << COMMMAND1_OPMODE_SHIFT) + + +#define ISL29023_REG_ADD_COMMANDII 0x01 +#define COMMANDII_RESOLUTION_SHIFT 2 +#define COMMANDII_RESOLUTION_65536 (0x0 << COMMANDII_RESOLUTION_SHIFT) +#define COMMANDII_RESOLUTION_4096 (0x1 << COMMANDII_RESOLUTION_SHIFT) +#define COMMANDII_RESOLUTION_256 (0x2 << COMMANDII_RESOLUTION_SHIFT) +#define COMMANDII_RESOLUTION_16 (0x3 << COMMANDII_RESOLUTION_SHIFT) +#define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT) + +#define COMMANDII_RANGE_SHIFT 0 +#define COMMANDII_RANGE_1000 (0x0 << COMMANDII_RANGE_SHIFT) +#define COMMANDII_RANGE_4000 (0x1 << COMMANDII_RANGE_SHIFT) +#define COMMANDII_RANGE_16000 (0x2 << COMMANDII_RANGE_SHIFT) +#define COMMANDII_RANGE_64000 (0x3 << COMMANDII_RANGE_SHIFT) +#define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT) + + +#define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT) + +#define COMMANDII_SCHEME_SHIFT 7 +#define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT) + +#define ISL29023_REG_ADD_DATA_LSB 0x02 +#define ISL29023_REG_ADD_DATA_MSB 0x03 +#define ISL29023_MAX_REGS ISL29023_REG_ADD_DATA_MSB + +#define ISL29023_REG_LT_LSB 0x04 +#define ISL29023_REG_LT_MSB 0x05 +#define ISL29023_REG_HT_LSB 0x06 +#define ISL29023_REG_HT_MSB 0x07 + + +/****************operate according to sensor chip:start************/ + +static int sensor_active(struct i2c_client *client, int enable, int rate) +{ + struct sensor_private_data *sensor = + (struct sensor_private_data *) i2c_get_clientdata(client); + int result = 0; + int status = 0; + + sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg); + + //register setting according to chip datasheet + if(enable) + { + sensor->ops->ctrl_data &= 0x1f; + sensor->ops->ctrl_data |= COMMMAND1_OPMODE_ALS_CONTINUE; + } + else + { + sensor->ops->ctrl_data &= 0x1f; + //sensor->ops->ctrl_data |= COMMMAND1_OPMODE_POWER_DOWN; + } + + DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable); + result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data); + if(result) + printk("%s:fail to active sensor\n",__func__); + + if(enable) + sensor->ops->report(sensor->client); + + return result; + +} + + +static int sensor_init(struct i2c_client *client) +{ + struct sensor_private_data *sensor = + (struct sensor_private_data *) i2c_get_clientdata(client); + int result = 0; + + result = sensor->ops->active(client,0,0); + if(result) + { + printk("%s:line=%d,error\n",__func__,__LINE__); + return result; + } + + sensor->status_cur = SENSOR_OFF; + + result = sensor_write_reg(client, ISL29023_REG_ADD_COMMANDII, COMMANDII_RANGE_4000 | COMMANDII_RESOLUTION_4096); + if(result) + { + printk("%s:line=%d,error\n",__func__,__LINE__); + return result; + } + + return result; +} + + +static int light_report_value(struct input_dev *input, int data) +{ + unsigned char index = 0; + + if(data <= 2){ + index = 0;goto report; + } + else if(data <= 3){ + index = 2;goto report; + } + else if(data <= 5){ + index = 3;goto report; + } + else if(data <= 8){ + index = 4;goto report; + } + else if(data <= 11){ + index = 5;goto report; + } + else if(data <= 14){ + index = 6;goto report; + } + else if(data <= 17){ + index = 7;goto report; + } + else{ + index = 7;goto report; + } + +report: + input_report_abs(input, ABS_MISC, index); + input_sync(input); + + return index; +} + + +static int sensor_report_value(struct i2c_client *client) +{ + struct sensor_private_data *sensor = + (struct sensor_private_data *) i2c_get_clientdata(client); + int result = 0; + int value = 0; + char buffer[2] = {0}; + char index = 0; + + if(sensor->ops->read_len < 2) //sensor->ops->read_len = 2 + { + printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len); + return -1; + } + + memset(buffer, 0, 2); + + buffer[0] = sensor->ops->read_reg; + result = sensor_rx_data(client, buffer, sensor->ops->read_len); + if(result) + { + printk("%s:line=%d,error\n",__func__,__LINE__); + return result; + } + + value = (buffer[1] << 8) | buffer[0]; + + + index = light_report_value(sensor->input_dev, value); + + DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index); + + if(sensor->pdata->irq_enable) + { + if(sensor->ops->int_status_reg) + { + value = sensor_read_reg(client, sensor->ops->int_status_reg); + } + + } + + + return result; +} + +struct sensor_operate light_stk3171_ops = { + .name = "ls_isl29023", + .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct + .id_i2c = LIGHT_ID_ISL29023, //i2c id number + .read_reg = ISL29023_REG_ADD_DATA_LSB, //read data + .read_len = 2, //data length + .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register + .id_data = SENSOR_UNKNOW_DATA, //device id + .precision = 16, //8 bits + .ctrl_reg = ISL29023_REG_ADD_COMMAND1, //enable or disable + .int_status_reg = ISL29023_REG_ADD_COMMAND1, //intterupt status register + .range = {100,65535}, //range + .brightness ={10,255}, //brightness + .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT, + .active = sensor_active, + .init = sensor_init, + .report = sensor_report_value, +}; + +/****************operate according to sensor chip:end************/ + +//function name should not be changed +static struct sensor_operate *light_get_ops(void) +{ + return &light_stk3171_ops; +} + + +static int __init light_stk3171_init(void) +{ + struct sensor_operate *ops = light_get_ops(); + int result = 0; + int type = ops->type; + result = sensor_register_slave(type, NULL, NULL, light_get_ops); + //printk("%s\n",__func__); + return result; +} + +static void __exit light_stk3171_exit(void) +{ + struct sensor_operate *ops = light_get_ops(); + int type = ops->type; + sensor_unregister_slave(type, NULL, NULL, light_get_ops); +} + + +module_init(light_stk3171_init); +module_exit(light_stk3171_exit); + + diff --git a/drivers/input/sensors/sensor-dev.c b/drivers/input/sensors/sensor-dev.c index 4eb052a5065d..a3050cbac4d8 100755 --- a/drivers/input/sensors/sensor-dev.c +++ b/drivers/input/sensors/sensor-dev.c @@ -302,6 +302,8 @@ static int sensor_irq_init(struct i2c_client *client) client->irq = irq; if((sensor->pdata->type == SENSOR_TYPE_GYROSCOPE) || (sensor->pdata->type == SENSOR_TYPE_ACCEL)) disable_irq_nosync(client->irq);//disable irq + if(((sensor->pdata->type == SENSOR_TYPE_LIGHT) || ((sensor->pdata->type == SENSOR_TYPE_PROXIMITY))) && (!(sensor->ops->trig & IRQF_SHARED))) + disable_irq_nosync(client->irq);//disable irq printk("%s:use irq=%d\n",__func__,irq); } else if(!sensor->pdata->irq_enable) @@ -777,12 +779,17 @@ static long light_dev_ioctl(struct file *file, printk("%s:fail to active sensor,ret=%d\n",__func__,result); goto error; } - - if(!sensor->pdata->irq_enable) + if(sensor->pdata->irq_enable && (!(sensor->ops->trig & IRQF_SHARED))) + { + DBG("%s:enable irq,irq=%d\n",__func__,client->irq); + enable_irq(client->irq); //enable irq + } + else { PREPARE_DELAYED_WORK(&sensor->delaywork, sensor_delaywork_func); schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms)); } + sensor->status_cur = SENSOR_ON; } } @@ -795,8 +802,14 @@ static long light_dev_ioctl(struct file *file, goto error; } - if(!sensor->pdata->irq_enable) - cancel_delayed_work_sync(&sensor->delaywork); + if(sensor->pdata->irq_enable && (!(sensor->ops->trig & IRQF_SHARED))) + { + DBG("%s:disable irq,irq=%d\n",__func__,client->irq); + disable_irq_nosync(client->irq);//disable irq + } + else + cancel_delayed_work_sync(&sensor->delaywork); + sensor->status_cur = SENSOR_OFF; } } @@ -861,11 +874,17 @@ static long proximity_dev_ioctl(struct file *file, goto error; } - if(!sensor->pdata->irq_enable) + if(sensor->pdata->irq_enable && (!(sensor->ops->trig & IRQF_SHARED))) + { + DBG("%s:enable irq,irq=%d\n",__func__,client->irq); + enable_irq(client->irq); //enable irq + } + else { PREPARE_DELAYED_WORK(&sensor->delaywork, sensor_delaywork_func); schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms)); } + sensor->status_cur = SENSOR_ON; } } @@ -877,7 +896,12 @@ static long proximity_dev_ioctl(struct file *file, mutex_unlock(&sensor->operation_mutex); goto error; } - if(!sensor->pdata->irq_enable) + if(sensor->pdata->irq_enable && (!(sensor->ops->trig & IRQF_SHARED))) + { + DBG("%s:disable irq,irq=%d\n",__func__,client->irq); + disable_irq_nosync(client->irq);//disable irq + } + else cancel_delayed_work_sync(&sensor->delaywork); sensor->status_cur = SENSOR_OFF; } @@ -1377,6 +1401,7 @@ static const struct i2c_device_id sensor_id[] = { {"light_cm3217", LIGHT_ID_CM3217}, {"light_al3006", LIGHT_ID_AL3006}, {"ls_stk3171", LIGHT_ID_STK3171}, + {"ls_isl29023", LIGHT_ID_ISL29023}, /*proximity sensor*/ {"psensor", PROXIMITY_ID_ALL}, {"proximity_al3006", PROXIMITY_ID_AL3006}, diff --git a/include/linux/sensor-dev.h b/include/linux/sensor-dev.h old mode 100644 new mode 100755 index 60880b84495e..38f4490ddef2 --- a/include/linux/sensor-dev.h +++ b/include/linux/sensor-dev.h @@ -77,6 +77,7 @@ enum sensor_id { LIGHT_ID_CM3217, LIGHT_ID_AL3006, LIGHT_ID_STK3171, + LIGHT_ID_ISL29023, PROXIMITY_ID_ALL, PROXIMITY_ID_AL3006, -- 2.34.1