ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / accel / mxc622x.c
1 /* drivers/input/sensors/access/mxc6225.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
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
32 #endif
33 #include <linux/sensor-dev.h>
34
35
36 #define MXC6225_ENABLE          1
37
38 #define MXC6225_REG_DATA       0x00
39 #define MXC6225_REG_TILT        0x3
40 #define MXC6225_REG_SRST        0x4
41 #define MXC6225_REG_SPCNT       0x5
42 #define MXC6225_REG_INTSU       0x6
43 #define MXC6225_REG_MODE        0x7
44 #define MXC6225_REG_SR          0x8
45 #define MXC6225_REG_PDET        0x9
46 #define MXC6225_REG_PD          0xa
47
48
49 #define MXC6225_RANGE                   2000000//1500000
50
51 /* LIS3DH */
52 #define MXC6225_PRECISION       8 // 8bit data
53 #define MXC6225_BOUNDARY                (0x1 << (MXC6225_PRECISION - 1))
54 #define MXC6225_GRAVITY_STEP    15625 //        (MXC6225_RANGE / MXC6225_BOUNDARY)
55
56 #define MXC6225_COUNT_AVERAGE   2
57
58 struct sensor_axis_average {
59                 int x_average;
60                 int y_average;
61                 int z_average;
62                 int count;
63 };
64
65 static struct sensor_axis_average axis_average;
66
67 /****************operate according to sensor chip:start************/
68
69 static int sensor_active(struct i2c_client *client, int enable, int rate)
70 {
71         struct sensor_private_data *sensor =
72             (struct sensor_private_data *) i2c_get_clientdata(client);  
73         int result = 0;
74         int status = 0;
75                 
76         sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
77         
78         //register setting according to chip datasheet          
79         if(enable)
80         {       
81                 status = MXC6225_ENABLE;        //mxc6225
82                 sensor->ops->ctrl_data |= status;       
83         }
84         else
85         {
86                 status = ~MXC6225_ENABLE;       //mxc6225
87                 sensor->ops->ctrl_data &= status;
88         }
89
90         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
91         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
92         if(result)
93                 printk("%s:fail to active sensor\n",__func__);
94         
95         return result;
96
97 }
98
99 static int sensor_init(struct i2c_client *client)
100 {       
101         struct sensor_private_data *sensor =
102             (struct sensor_private_data *) i2c_get_clientdata(client);  
103         int result = 0;
104         
105         result = sensor->ops->active(client,0,0);
106         if(result)
107         {
108                 printk("%s:line=%d,error\n",__func__,__LINE__);
109                 return result;
110         }
111         
112         sensor->status_cur = SENSOR_OFF;
113
114         //DBG("%s:MXC6225_REG_TILT=0x%x\n",__func__,sensor_read_reg(client, MXC6225_REG_TILT));
115
116         result = sensor_write_reg(client, MXC6225_REG_SR, (0x01<<5)| 0x02);     //32 Samples/Second Active and Auto-Sleep Mode
117         if(result)
118         {
119                 printk("%s:line=%d,error\n",__func__,__LINE__);
120                 return result;
121         }
122
123         if(sensor->pdata->irq_enable)   //open interrupt
124         {
125                 result = sensor_write_reg(client, MXC6225_REG_INTSU, 1<<4);//enable int,GINT=1
126                 if(result)
127                 {
128                         printk("%s:line=%d,error\n",__func__,__LINE__);
129                         return result;
130                 }
131         }
132         
133         sensor->ops->ctrl_data = 1<<8;  //Interrupt output INT is push-pull
134         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
135         if(result)
136         {
137                 printk("%s:line=%d,error\n",__func__,__LINE__);
138                 return result;
139         }
140
141         
142         memset(&axis_average, 0, sizeof(struct sensor_axis_average));
143
144         return result;
145 }
146
147
148 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
149 {
150     s64 result;
151         //struct sensor_private_data *sensor =
152         //    (struct sensor_private_data *) i2c_get_clientdata(client);        
153         //int precision = sensor->ops->precision;
154                 
155         result = (int)low_byte;
156         if (result < MXC6225_BOUNDARY)
157                 result = result* MXC6225_GRAVITY_STEP;
158         else
159                 result = ~(((~result & 0x7f) + 1) * MXC6225_GRAVITY_STEP) + 1;    
160
161         return (int)result;
162 }
163
164 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
165 {
166         struct sensor_private_data *sensor =
167             (struct sensor_private_data *) i2c_get_clientdata(client);  
168
169         /* Report acceleration sensor information */
170         input_report_abs(sensor->input_dev, ABS_X, axis->x);
171         input_report_abs(sensor->input_dev, ABS_Y, axis->y);
172         input_report_abs(sensor->input_dev, ABS_Z, axis->z);
173         input_sync(sensor->input_dev);
174         DBG("Gsensor x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
175
176         return 0;
177 }
178
179 #define GSENSOR_MIN             2
180 static int sensor_report_value(struct i2c_client *client)
181 {
182         struct sensor_private_data *sensor =
183                 (struct sensor_private_data *) i2c_get_clientdata(client);      
184         struct sensor_platform_data *pdata = sensor->pdata;
185         int ret = 0;
186         int x,y,z;
187         struct sensor_axis axis;
188         char buffer[3] = {0};   
189         char value = 0;
190         
191         if(sensor->ops->read_len < 3)   //sensor->ops->read_len = 3
192         {
193                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
194                 return -1;
195         }
196         
197         memset(buffer, 0, 3);
198         
199         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */   
200         do {
201                 *buffer = sensor->ops->read_reg;
202                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
203                 if (ret < 0)
204                 return ret;
205         } while (0);
206
207
208         //this gsensor need 6 bytes buffer
209         x = sensor_convert_data(sensor->client, 0, buffer[0]);  //buffer[1]:high bit 
210         y = sensor_convert_data(sensor->client, 0, buffer[1]);
211         z = sensor_convert_data(sensor->client, 0, buffer[2]);          
212
213         axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
214         axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; 
215         axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
216
217         
218         axis_average.x_average += axis.x;
219         axis_average.y_average += axis.y;
220         axis_average.z_average += axis.z;
221         axis_average.count++;
222         
223         if(axis_average.count >= MXC6225_COUNT_AVERAGE)
224         {
225                 axis.x = axis_average.x_average / axis_average.count;   
226                 axis.y = axis_average.y_average / axis_average.count;   
227                 axis.z = axis_average.z_average / axis_average.count;
228                 
229                 DBG( "%s: axis = %d  %d  %d \n", __func__, axis.x, axis.y, axis.z);
230                 
231                 memset(&axis_average, 0, sizeof(struct sensor_axis_average));
232                 
233                 //Report event only while value is changed to save some power
234                 if((abs(sensor->axis.x - axis.x) > GSENSOR_MIN) || (abs(sensor->axis.y - axis.y) > GSENSOR_MIN) || (abs(sensor->axis.z - axis.z) > GSENSOR_MIN))
235                 {
236                         gsensor_report_value(client, &axis);
237
238                         /* »¥³âµØ»º´æÊý¾Ý. */
239                         mutex_lock(&(sensor->data_mutex) );
240                         sensor->axis = axis;
241                         mutex_unlock(&(sensor->data_mutex) );
242                 }
243         }
244         
245         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
246         {
247                 
248                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
249                 DBG("%s:sensor int status :0x%x\n",__func__,value);
250         }
251         
252         return ret;
253 }
254
255
256 struct sensor_operate gsensor_mxc6225_ops = {
257         .name                           = "mxc6225",
258         .type                           = SENSOR_TYPE_ACCEL,                    //sensor type and it should be correct
259         .id_i2c                         = ACCEL_ID_MXC6225,                     //i2c id number
260         .read_reg                       = MXC6225_REG_DATA,                     //read data
261         .read_len                       = 3,                                    //data length
262         .id_reg                         = SENSOR_UNKNOW_DATA,                   //read device id from this register
263         .id_data                        = SENSOR_UNKNOW_DATA,                   //device id
264         .precision                      = MXC6225_PRECISION,                    //12 bit
265         .ctrl_reg                       = MXC6225_REG_MODE,                     //enable or disable     
266         .int_status_reg                 = SENSOR_UNKNOW_DATA,                   //intterupt status register
267         .range                          = {-MXC6225_RANGE,MXC6225_RANGE},       //range
268         .trig                           = IRQF_TRIGGER_LOW |IRQF_ONESHOT,               
269         .active                         = sensor_active,        
270         .init                           = sensor_init,
271         .report                         = sensor_report_value,
272 };
273
274 /****************operate according to sensor chip:end************/
275
276 //function name should not be changed
277 static struct sensor_operate *gsensor_get_ops(void)
278 {
279         return &gsensor_mxc6225_ops;
280 }
281
282
283 static int __init gsensor_mxc6225_init(void)
284 {
285         struct sensor_operate *ops = gsensor_get_ops();
286         int result = 0;
287         int type = ops->type;
288         
289         result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);      
290         return result;
291 }
292
293 static void __exit gsensor_mxc6225_exit(void)
294 {
295         struct sensor_operate *ops = gsensor_get_ops();
296         int type = ops->type;
297         sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
298 }
299
300
301 module_init(gsensor_mxc6225_init);
302 module_exit(gsensor_mxc6225_exit);
303
304
305