013b67015aa431f0a904939b7a023fbc39e3eb27
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / lsensor / cm3232.c
1 /* drivers/input/sensors/access/kxtik.c\r
2  *\r
3  * Copyright (C) 2012-2015 ROCKCHIP.\r
4  * Author: luowei <lw@rock-chips.com>\r
5  *\r
6  * This software is licensed under the terms of the GNU General Public\r
7  * License version 2, as published by the Free Software Foundation, and\r
8  * may be copied, distributed, and modified under those terms.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  */\r
16 #include <linux/interrupt.h>\r
17 #include <linux/i2c.h>\r
18 #include <linux/slab.h>\r
19 #include <linux/irq.h>\r
20 #include <linux/miscdevice.h>\r
21 #include <linux/gpio.h>\r
22 #include <asm/uaccess.h>\r
23 #include <asm/atomic.h>\r
24 #include <linux/delay.h>\r
25 #include <linux/input.h>\r
26 #include <linux/workqueue.h>\r
27 #include <linux/freezer.h>\r
28 #include <linux/of_gpio.h>\r
29 #ifdef CONFIG_HAS_EARLYSUSPEND\r
30 #include <linux/earlysuspend.h>\r
31 #endif\r
32 #include <linux/sensor-dev.h>\r
33 \r
34 \r
35 #define CM3232_CLOSE    0x01\r
36 \r
37 \r
38 #define CM3232_ADDR_COM 0\r
39 #define CM3232_ADDR_DATA 50     \r
40 \r
41 #define CM3232_DRV_NAME "cm3232"\r
42 //command code\r
43 #define COMMAND_CTRL            0\r
44 #define COMMAND_ALS_DATA        50              //ALS: 15:8 MSB 8bits data\r
45                                                 //7:0 LSB 8bits data\r
46 \r
47 //ctrl bit\r
48 #define ALS_RESET(x)                    (((x)&1)<<6) //0 = Reset disable; 1 = Reset enable\r
49 #define ALS_IT(x)                       (((x)&7)<<2) //ALS integration time setting\r
50 #define HIGH_SENSITIVITY(x)             (((x)&1)<<1) //0 = Normal mode; 1 = High sensitivity mode\r
51 #define SHUT_DOWN(x)                    (((x)&1)<<0) //ALS shut down setting: 0 = ALS Power on ; 1 = ALS Shut down\r
52 \r
53 #define ALS_IT100MS     0               //100ms\r
54 #define ALS_IT200MS     1               //200ms\r
55 #define ALS_IT400MS     2               //400ms\r
56 #define ALS_IT800MS     3               //800ms\r
57 #define ALS_IT1600MS    4               //1600ms\r
58 #define ALS_IT3200MS    5               //3200ms\r
59 \r
60 \r
61 /****************operate according to sensor chip:start************/\r
62 \r
63 static int sensor_active(struct i2c_client *client, int enable, int rate)\r
64 {\r
65         struct sensor_private_data *sensor =\r
66             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
67         int result = 0;\r
68         //int status = 0;\r
69         \r
70         //sensor->client->addr = sensor->ops->ctrl_reg; \r
71         //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
72         //printk("%s:  client addr = %#x\n\n", __func__, client->addr); \r
73         //register setting according to chip datasheet          \r
74         if (enable) {\r
75                 sensor->ops->ctrl_data = ALS_RESET(1);  \r
76                 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
77                 if(result) {\r
78                         printk("%s:fail to active sensor\n",__func__);\r
79                         return -1;\r
80                 }\r
81         }\r
82 \r
83         if(enable)\r
84         {       \r
85                 sensor->ops->ctrl_data = ALS_IT(ALS_IT200MS) | HIGH_SENSITIVITY(1);\r
86         }\r
87         else\r
88         {\r
89                 sensor->ops->ctrl_data = SHUT_DOWN(1);\r
90         }\r
91 \r
92         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
93 \r
94         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
95         if(result)\r
96                 printk("%s:fail to active sensor\n",__func__);\r
97         \r
98         return result;\r
99 \r
100 }\r
101 \r
102 \r
103 static int sensor_init(struct i2c_client *client)\r
104 {       \r
105         struct sensor_private_data *sensor =\r
106             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
107         int result = 0;\r
108         \r
109         result = sensor->ops->active(client,0,0);\r
110         if(result)\r
111         {\r
112                 printk("%s:line=%d,error\n",__func__,__LINE__);\r
113                 return result;\r
114         }\r
115         \r
116         sensor->status_cur = SENSOR_OFF;\r
117         return result;\r
118 }\r
119 \r
120 \r
121 static int light_report_value(struct input_dev *input, int data)\r
122 {\r
123         unsigned char index = 0;\r
124         \r
125         if(data <= 10){\r
126                 index = 0;goto report;\r
127         }\r
128         else if(data <= 160){\r
129                 index = 1;goto report;\r
130         }\r
131         else if(data <= 225){\r
132                 index = 2;goto report;\r
133         }\r
134         else if(data <= 320){\r
135                 index = 3;goto report;\r
136         }\r
137         else if(data <= 640){\r
138                 index = 4;goto report;\r
139         }\r
140         else if(data <= 1280){\r
141                 index = 5;goto report;\r
142         }\r
143         else if(data <= 2600){\r
144                 index = 6;goto report;\r
145         }\r
146         else{\r
147                 index = 7;goto report;\r
148         }\r
149 \r
150 report:\r
151         input_report_abs(input, ABS_MISC, index);\r
152         input_sync(input);\r
153         \r
154         return index;\r
155 }\r
156 \r
157 \r
158 static int sensor_report_value(struct i2c_client *client)\r
159 {\r
160         struct sensor_private_data *sensor =\r
161             (struct sensor_private_data *) i2c_get_clientdata(client);  \r
162         int result = 0;\r
163         //char msb = 0, lsb = 0;\r
164         char data[2] = {0};\r
165         unsigned short value = 0;\r
166         int index = 0;\r
167         \r
168         //sensor->client->addr = CM3232_ADDR_DATA;\r
169         data[0] = CM3232_ADDR_DATA;\r
170         sensor_rx_data(sensor->client, data, 2);\r
171         value = (data[1] << 8) | data[0] ;\r
172 \r
173         DBG("%s:result=%d\n",__func__,value);\r
174         //printk("%s:result=%d\n",__func__,value);\r
175         index = light_report_value(sensor->input_dev, value);   \r
176         DBG("%s:%s result=0x%x,index=%d\n",__func__,sensor->ops->name, value,index);\r
177 \r
178         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register\r
179         {\r
180                 \r
181                 result= sensor_read_reg(client, sensor->ops->int_status_reg);\r
182                 if(result)\r
183                 {\r
184                         printk("%s:fail to clear sensor int status,ret=0x%x\n",__func__,result);\r
185                 }\r
186         }\r
187         \r
188         return result;\r
189 }\r
190 \r
191 \r
192 struct sensor_operate light_cm3232_ops = {\r
193         .name                           = "cm3232",\r
194         .type                           = SENSOR_TYPE_LIGHT,    //sensor type and it should be correct\r
195         .id_i2c                         = LIGHT_ID_CM3232,      //i2c id number\r
196         .read_reg                       = CM3232_ADDR_DATA,     //read data\r
197         .read_len                       = 2,                    //data length\r
198         .id_reg                         = SENSOR_UNKNOW_DATA,   //read device id from this register\r
199         .id_data                        = SENSOR_UNKNOW_DATA,   //device id\r
200         .precision                      = 8,                    //8 bits\r
201         .ctrl_reg                       = CM3232_ADDR_COM,      //enable or disable \r
202         .int_status_reg                 = SENSOR_UNKNOW_DATA,   //intterupt status register\r
203         .range                          = {100,65535},          //range\r
204         .brightness                     = {10,255},             // brightness\r
205         .trig                           = SENSOR_UNKNOW_DATA,           \r
206         .active                         = sensor_active,        \r
207         .init                           = sensor_init,\r
208         .report                         = sensor_report_value,\r
209 };\r
210 \r
211 /****************operate according to sensor chip:end************/\r
212 \r
213 //function name should not be changed\r
214 static struct sensor_operate *light_get_ops(void)\r
215 {\r
216         return &light_cm3232_ops;\r
217 }\r
218 \r
219 \r
220 static int __init light_cm3232_init(void)\r
221 {\r
222         struct sensor_operate *ops = light_get_ops();\r
223         int result = 0;\r
224         int type = ops->type;\r
225         result = sensor_register_slave(type, NULL, NULL, light_get_ops);\r
226         return result;\r
227 }\r
228 \r
229 static void __exit light_cm3232_exit(void)\r
230 {\r
231         struct sensor_operate *ops = light_get_ops();\r
232         int type = ops->type;\r
233         sensor_unregister_slave(type, NULL, NULL, light_get_ops);\r
234 }\r
235 \r
236 \r
237 module_init(light_cm3232_init);\r
238 module_exit(light_cm3232_exit);\r
239 \r
240 \r