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