From ee68d84251e367195a64ec708c7ae31efc191b3a Mon Sep 17 00:00:00 2001 From: wuhao Date: Sat, 2 Feb 2013 20:03:53 +0800 Subject: [PATCH] rk3188 ds1006h: add light photoresistor --- arch/arm/mach-rk3188/board-rk3188-ds1006h.c | 19 +- drivers/input/sensors/lsensor/Kconfig | 7 +- drivers/input/sensors/lsensor/Makefile | 1 + .../input/sensors/lsensor/ls_photoresistor.c | 194 ++++++++++++++++++ drivers/input/sensors/sensor-dev.c | 3 +- include/linux/sensor-dev.h | 3 +- 6 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 drivers/input/sensors/lsensor/ls_photoresistor.c diff --git a/arch/arm/mach-rk3188/board-rk3188-ds1006h.c b/arch/arm/mach-rk3188/board-rk3188-ds1006h.c index 058183e48998..cab890ea34f3 100755 --- a/arch/arm/mach-rk3188/board-rk3188-ds1006h.c +++ b/arch/arm/mach-rk3188/board-rk3188-ds1006h.c @@ -265,6 +265,14 @@ static struct akm8963_platform_data akm_platform_data_8963 = { }; #endif +#if defined(CONFIG_LS_PHOTORESISTOR) +static struct sensor_platform_data light_photoresistor_info = { + .type = SENSOR_TYPE_LIGHT, + .irq_enable = 0, + .address = 2 ; + .poll_delay_ms = 200, +}; +#endif #if defined (CONFIG_COMPASS_AK8975) static struct sensor_platform_data akm8975_info = @@ -1155,6 +1163,7 @@ static struct i2c_board_info __initdata i2c0_info[] = { .flags = 0, }, #endif + #if defined (CONFIG_GS_LIS3DH) { .type = "gs_lis3dh", @@ -1173,7 +1182,15 @@ static struct i2c_board_info __initdata i2c0_info[] = { .platform_data = &akm_platform_data_8963, }, #endif - +#if defined (CONFIG_LS_PHOTORESISTOR) + { + .type = "ls_photoresistor", + .addr = 0x5e, + .flags = 0, + .irq = INVALID_GPIO, + .platform_data = &light_photoresistor_info, + }, +#endif #if defined (CONFIG_COMPASS_AK8975) { diff --git a/drivers/input/sensors/lsensor/Kconfig b/drivers/input/sensors/lsensor/Kconfig index 4c3aec1327c4..0e6cf2ff9181 100755 --- a/drivers/input/sensors/lsensor/Kconfig +++ b/drivers/input/sensors/lsensor/Kconfig @@ -25,6 +25,11 @@ config LS_ISL29023 config LS_AP321XX bool "light sensor ap321xx" - default n + default n + +config LS_PHOTORESISTOR + bool "light sensor photoresistor" + default n + endif diff --git a/drivers/input/sensors/lsensor/Makefile b/drivers/input/sensors/lsensor/Makefile index 1834c514b54e..48531def826d 100755 --- a/drivers/input/sensors/lsensor/Makefile +++ b/drivers/input/sensors/lsensor/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_LS_AL3006) += ls_al3006.o obj-$(CONFIG_LS_STK3171) += ls_stk3171.o obj-$(CONFIG_LS_ISL29023) += isl29023.o obj-$(CONFIG_LS_AP321XX) += ls_ap321xx.o +obj-$(CONFIG_LS_PHOTORESISTOR) += ls_photoresistor.o \ No newline at end of file diff --git a/drivers/input/sensors/lsensor/ls_photoresistor.c b/drivers/input/sensors/lsensor/ls_photoresistor.c new file mode 100644 index 000000000000..1abd2ff7f8b5 --- /dev/null +++ b/drivers/input/sensors/lsensor/ls_photoresistor.c @@ -0,0 +1,194 @@ +/* drivers/input/sensors/lsensor/ls_photoresistor.c + * + * Copyright (C) 2012-2015 ROCKCHIP. + * Author: ? + * + * 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 +#include +#ifdef CONFIG_HAS_EARLYSUSPEND +#include +#endif +#include + +#if 0 +#define SENSOR_DEBUG_TYPE SENSOR_TYPE_LIGHT +#define DBG(x...) printk(x) +#else +#define DBG(x...) printk(x) +#endif + +struct lsp_base_data { + + int adcvalue ; + int oldadcvalue ; + struct adc_client *adc_client ; +}; +struct lsp_base_data glspdata ; + + //{1000,900,700,400,200,100,40,0}; +static int LighSensorValue[8]={800,700,500,400,200,100,40,0}; +static int LighSensorLevel_TAB[8]={0,1,2,3,4,5,6,7}; + +/****************operate according to sensor chip:start************/ + +static int sensor_active(struct i2c_client *client, int enable, int rate) +{ + int ret = 0; + + DBG("light_photoresisto: sensor active is %s \n",( (enable)?"on":"off" ) ); + + if( enable ){//ture on + + }else{//ture off + + } + + return 0; +} + +static void lsp_adc_callback(struct adc_client *client, void *callback_param, int result) +{ + glspdata.adcvalue = result; + DBG("light_photoresisto: adc callback value is %d \n",glspdata.adcvalue); +} + +static int valuetoindex( int data) +{ + unsigned char index = 0; + + for( index = 0 ; index < ARRAY_SIZE(LighSensorLevel_TAB) ; index ++ ) + { + if(data > LighSensorValue[index]) + break; + } + + return index; +} + +static int sensor_report_value(struct i2c_client *client) +{ + int ret = 0 ; + struct sensor_private_data *sensor = + (struct sensor_private_data *) i2c_get_clientdata(client); + + if(glspdata.adc_client) + adc_async_read(glspdata.adc_client); + + if(glspdata.adcvalue==0||glspdata.adcvalue==1){// + DBG("light_photoresisto: adc value is (%d) invalid \n",glspdata.adcvalue); + return ret ; + } + + if(glspdata.adcvalue != glspdata.oldadcvalue) + { + DBG("light_photoresisto: input report value is %d \n",glspdata.adcvalue); + input_report_abs(sensor->input_dev, ABS_MISC, + valuetoindex(glspdata.adcvalue) ); + input_sync(sensor->input_dev); + + glspdata.oldadcvalue = glspdata.adcvalue; + } + + return ret; +} + +static int sensor_init(struct i2c_client *client) +{ + DBG("enter %s\n",__func__); + int error = 0 ; + struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client); + + //adc register + if( (sensor->pdata->address >= 0) && (sensor->pdata->address <=3) ){ + glspdata.adc_client = adc_register(sensor->pdata->address, lsp_adc_callback, "lsp_adc"); + if( !glspdata.adc_client ) { + printk("%s : light_photoresisto adc_register faile !\n",__func__); + error = -EINVAL; + goto fail1; + } + } + + return 0 ; + + fail1: + adc_unregister(glspdata.adc_client); + + return error; +} + +struct sensor_operate light_photoresistor_ops = { + .name = "light_photoresistor", + .type = SENSOR_TYPE_LIGHT, //sensor type and it should be correct + .id_i2c = LIGHT_ID_PHOTORESISTOR, //i2c id number + + .range = {0,10}, //range + .brightness ={1,255}, // brightness + .active = sensor_active, + .init = sensor_init, + .report = sensor_report_value, + + /*--------- INVALID ----------*/ + .read_reg = SENSOR_UNKNOW_DATA, //read data INVALID + .read_len = SENSOR_UNKNOW_DATA, //data length INVALID + .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register INVALID + .id_data = SENSOR_UNKNOW_DATA, //device id INVALID + .precision = SENSOR_UNKNOW_DATA, //8 bits INVALID + .ctrl_reg = SENSOR_UNKNOW_DATA, //enable or disable INVALID + .int_status_reg = SENSOR_UNKNOW_DATA, //intterupt status register INVALID + .trig = SENSOR_UNKNOW_DATA, + /*--------- INVALID end-------*/ +}; +/****************operate according to sensor chip:end************/ + +//function name should not be changed +static struct sensor_operate *light_get_ops(void) +{ + return &light_photoresistor_ops; +} + + +static int __init lsp_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); + DBG("%s\n",__func__); + return result; +} + +static void __exit lsp_exit(void) +{ + struct sensor_operate *ops = light_get_ops(); + int type = ops->type; + sensor_unregister_slave(type, NULL, NULL, light_get_ops); + if(glspdata.adc_client) + adc_unregister(glspdata.adc_client); +} + +module_init(lsp_init); +module_exit(lsp_exit); + diff --git a/drivers/input/sensors/sensor-dev.c b/drivers/input/sensors/sensor-dev.c index ea2b4172c57b..d24972d45a4e 100755 --- a/drivers/input/sensors/sensor-dev.c +++ b/drivers/input/sensors/sensor-dev.c @@ -1155,7 +1155,7 @@ int sensor_probe(struct i2c_client *client, const struct i2c_device_id *devid) struct sensor_platform_data *pdata; int result = 0; int type = 0; - dev_info(&client->adapter->dev, "%s: %s,0x%x\n", __func__, devid->name,(unsigned int)client); + dev_info(&client->adapter->dev, "-------------------------------------%s: %s,0x%x\n", __func__, devid->name,(unsigned int)client); if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { result = -ENODEV; @@ -1416,6 +1416,7 @@ static const struct i2c_device_id sensor_id[] = { {"ls_stk3171", LIGHT_ID_STK3171}, {"ls_isl29023", LIGHT_ID_ISL29023}, {"ls_ap321xx", LIGHT_ID_AP321XX}, + {"ls_photoresistor", LIGHT_ID_PHOTORESISTOR}, /*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 index 7841b1782fdd..5711d3efb2ee 100755 --- a/include/linux/sensor-dev.h +++ b/include/linux/sensor-dev.h @@ -80,7 +80,8 @@ enum sensor_id { LIGHT_ID_STK3171, LIGHT_ID_ISL29023, LIGHT_ID_AP321XX, - + LIGHT_ID_PHOTORESISTOR, + PROXIMITY_ID_ALL, PROXIMITY_ID_AL3006, PROXIMITY_ID_STK3171, -- 2.34.1