rk3188 ds1006h: add light photoresistor
authorwuhao <wuhao@wuhao@rock-chips.com>
Sat, 2 Feb 2013 12:03:53 +0000 (20:03 +0800)
committerwuhao <wuhao@wuhao@rock-chips.com>
Sat, 2 Feb 2013 12:05:07 +0000 (20:05 +0800)
arch/arm/mach-rk3188/board-rk3188-ds1006h.c
drivers/input/sensors/lsensor/Kconfig
drivers/input/sensors/lsensor/Makefile
drivers/input/sensors/lsensor/ls_photoresistor.c [new file with mode: 0644]
drivers/input/sensors/sensor-dev.c
include/linux/sensor-dev.h

index 058183e48998d4684648c71b7e1bcf7b7c27c067..cab890ea34f3ea48b1deca5a8d0fdf39d60284a6 100755 (executable)
@@ -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)
        {
index 4c3aec1327c4251ae85222ff2a00b706ba9627eb..0e6cf2ff91812de79cf500966d72e6b7b7747c98 100755 (executable)
@@ -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
 
index 1834c514b54ee41dd22a9833b0c5696967b62f27..48531def826dc59a1c1b1e1b1d49c9e7722b86d4 100755 (executable)
@@ -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 (file)
index 0000000..1abd2ff
--- /dev/null
@@ -0,0 +1,194 @@
+/* drivers/input/sensors/lsensor/ls_photoresistor.c
+ *
+ * Copyright (C) 2012-2015 ROCKCHIP.
+ * Author:  ?<?@rock-chips.com>
+ *
+ * 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 <linux/interrupt.h>
+#include <linux/i2c.h>
+#include <linux/slab.h>
+#include <linux/irq.h>
+#include <linux/miscdevice.h>
+#include <linux/gpio.h>
+#include <asm/uaccess.h>
+#include <asm/atomic.h>
+#include <linux/delay.h>
+#include <linux/input.h>
+#include <linux/workqueue.h>
+#include <linux/freezer.h>
+#include <mach/gpio.h>
+#include <mach/board.h> 
+#include <linux/adc.h>
+#ifdef CONFIG_HAS_EARLYSUSPEND
+#include <linux/earlysuspend.h>
+#endif
+#include <linux/sensor-dev.h>
+
+#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);
+
index ea2b4172c57bf92c7329ba74e07f4d05b003a3d2..d24972d45a4e3dd7f63cf99e596fffef05d14264 100755 (executable)
@@ -1155,7 +1155,7 @@ int sensor_probe(struct i2c_client *client, const struct i2c_device_id *devid)
        struct sensor_platform_data *pdata;\r
        int result = 0;\r
        int type = 0;\r
-       dev_info(&client->adapter->dev, "%s: %s,0x%x\n", __func__, devid->name,(unsigned int)client);\r
+       dev_info(&client->adapter->dev, "-------------------------------------%s: %s,0x%x\n", __func__, devid->name,(unsigned int)client);\r
 \r
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {\r
                result = -ENODEV;\r
@@ -1416,6 +1416,7 @@ static const struct i2c_device_id sensor_id[] = {
        {"ls_stk3171", LIGHT_ID_STK3171},\r
        {"ls_isl29023", LIGHT_ID_ISL29023},\r
        {"ls_ap321xx", LIGHT_ID_AP321XX},\r
+        {"ls_photoresistor", LIGHT_ID_PHOTORESISTOR},\r
        /*proximity sensor*/\r
        {"psensor", PROXIMITY_ID_ALL},\r
        {"proximity_al3006", PROXIMITY_ID_AL3006},      \r
index 7841b1782fdd062a2d0de41f7308ee3cbabe8283..5711d3efb2eeda0cc0d4a051663c22f70c228868 100755 (executable)
@@ -80,7 +80,8 @@ enum sensor_id {
        LIGHT_ID_STK3171,\r
        LIGHT_ID_ISL29023,\r
        LIGHT_ID_AP321XX,\r
-       \r
+        LIGHT_ID_PHOTORESISTOR,        \r
+\r
        PROXIMITY_ID_ALL,\r
        PROXIMITY_ID_AL3006,\r
        PROXIMITY_ID_STK3171,\r