input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / gyro / ewtsa.c
1 /* drivers/input/sensors/gyro/Ewtsa.c
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: zhangaihui <zah@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 /** This define controls compilation of the master device interface */
35 /*#define EWTSA_MASTER_DEVICE*/
36 /* configurable */
37 #define GYRO_MOUNT_SWAP_XY      0   /* swap X, Y */
38 #define GYRO_MOUNT_REVERSE_X    0   /* reverse X */
39 #define GYRO_MOUNT_REVERSE_Y    0   /* reverse Y */
40 #define GYRO_MOUNT_REVERSE_Z    0   /* reverse Z */
41
42 /* macro defines */
43 /*#define CHIP_ID                 0x68*/
44 #define DEVICE_NAME             "ewtsa"
45 #define EWTSA_ON                1
46 #define EWTSA_OFF               0
47 #define SLEEP_PIN               14
48 #define DRDY_PIN                12
49 #define DIAG_PIN                11
50 #define MAX_VALUE               32768
51
52 /* ewtsa_delay parameter */
53 #define DELAY_THRES_MIN         1
54 #define DELAY_THRES_1           4
55 #define DELAY_THRES_2           9   /* msec x 90% */
56 #define DELAY_THRES_3           18
57 #define DELAY_THRES_4           45
58 #define DELAY_THRES_5           90
59 #define DELAY_THRES_6           128
60 #define DELAY_THRES_MAX         255
61 #define DELAY_DLPF_2            2
62 #define DELAY_DLPF_3            3
63 #define DELAY_DLPF_4            4
64 #define DELAY_DLPF_5            5
65 #define DELAY_DLPF_6            6
66 #define DELAY_INTMIN_THRES      9
67
68 #define DATA_RATE_1             0x01
69
70 /* ewtsa_sleep parameter */
71 #define SLEEP_OFF               0
72 #define SLEEP_ON                1
73
74 /* event mode */
75 #define EWTSA_POLLING_MODE    0
76 #define EWTSA_INTERUPT_MODE   1
77
78 /* ewtsa register address */
79 #define REG_SMPL                0x15
80 #define REG_FS_DLPF             0x16
81 #define REG_INT_CFG             0x17
82 #define REG_INT_STATUS          0x1A
83 #define REG_SELF_O_C            0x29
84 #define REG_PWR_MGM             0x3E
85 #define REG_MBURST_ALL          0xFF
86 #define GYRO_DATA_REG            0x1D
87
88 /* ewtsa register param */
89 #define SELF_O_C_ENABLE         0x00
90 #define SELF_O_C_DISABLE        0x01
91 #define SLEEP_CTRL_ACTIVATE     0x40
92 #define SLEEP_CTRL_SLEEP        0x00
93 #define INT_CFG_INT_ENABLE      0x01
94 #define INT_CFG_INT_DISABLE     0x00
95
96 /* ewtsa interrupt control */
97 #define EWSTA_INT_CLEAR         0x00
98 #define EWSTA_INT_SKIP          0x01
99
100 /* wait time(ms)*/
101 #define EWTSA_BOOST_TIME_0      500
102
103 /* sleep setting range */
104 #define EWTSA_SLP_MIN 0
105 #define EWTSA_SLP_MAX 1
106
107 /* delay setting range */
108 #define EWTSA_DLY_MIN 1
109 #define EWTSA_DLY_MAX 255
110
111 /* range setting range */
112 #define EWTSA_RNG_MIN 0
113 #define EWTSA_RNG_MAX 3
114
115 /* soc setting range */
116 #define EWTSA_SOC_MIN 0
117 #define EWTSA_SOC_MAX 1
118
119 /* event setting range */
120 #define EWTSA_EVE_MIN 0
121 #define EWTSA_EVE_MAX 1
122
123 /* init param */
124 #define SLEEP_INIT_VAL       (SLEEP_ON)
125 #define DELAY_INIT_VAL       10
126 #define RANGE_INIT_VAL       2 /*range 1000*/
127 #define DLPF_INIT_VAL        (DELAY_DLPF_2)
128 #define CALIB_FUNC_INIT_VAL  (EWTSA_ON)
129
130 /*config store counter num*/
131 #define CONFIG_COUNTER_MIN (6+9)
132 #define CONFIG_COUNTER_MAX (32+9)
133
134 /*command name */
135 #define COMMAND_NAME_SOC 0
136 #define COMMAND_NAME_DLY 1
137 #define COMMAND_NAME_RNG 2
138 #define COMMAND_NAME_EVE 3
139 #define COMMAND_NAME_SLP 4
140 #define COMMAND_NAME_NUM 5
141
142 #define EWTSA_delay  DELAY_INIT_VAL
143 #define EWTSA_range    RANGE_INIT_VAL
144 #define EWTSA_calib    EWTSA_ON
145
146 /****************operate according to sensor chip:start************/
147 static int i2c_read_byte(struct i2c_client *thisClient, unsigned char regAddr, char *pReadData)
148 {
149     int    ret = 0;
150
151     ret = i2c_master_send( thisClient, (char*)&regAddr, 1);
152     if(ret < 0)
153     {
154         printk("EWTSA send cAddress=0x%x error!\n", regAddr);
155         return ret;
156     }
157     ret = i2c_master_recv( thisClient, (char*)pReadData, 1);
158     if(ret < 0)
159     {
160         printk("EWTSAread *pReadData=0x%x error!\n", *pReadData);
161         return ret;
162     }
163
164     return 1;
165 }
166 static int i2c_write_byte(struct i2c_client *thisClient, unsigned char regAddr, unsigned char writeData)
167 {
168     char    write_data[2] = {0};
169     int    ret=0;
170
171     write_data[0] = regAddr;
172     write_data[1] = writeData;
173
174     ret = i2c_master_send(thisClient, write_data, 2);
175     if (ret < 0)
176     {
177         ret = i2c_master_send(thisClient, write_data, 2);
178         if (ret < 0)
179          {
180              printk("EWTSA send regAddr=0x%x error!\n", regAddr);
181              return ret;
182         }
183         return 1;
184     }
185
186     return 1;
187 }
188
189 static int ewtsa_system_restart(struct i2c_client *client)
190 {
191     int             err;
192      char   reg;
193      char   smpl , dlpf;
194
195     err = i2c_write_byte(client, ( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_DISABLE);
196     if (err < 0) {
197         return err;
198     }
199
200     ///Set SMPL register
201         if (EWTSA_delay <= ( unsigned char)DELAY_THRES_2) {
202             smpl = ( unsigned char)DELAY_INTMIN_THRES;
203         }else{
204             smpl = ( unsigned char)(EWTSA_delay - ( unsigned char)1);
205         }
206     err = i2c_write_byte(client, ( unsigned char)REG_SMPL, ( unsigned char)smpl);
207     if (err < 0) {
208         return err;
209     }
210
211     ///Set DLPF register
212     if (EWTSA_delay >= ( unsigned char)DELAY_THRES_6){
213         dlpf = ( unsigned char)DELAY_DLPF_6;
214     }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_5) {
215         dlpf = ( unsigned char)DELAY_DLPF_5;
216     }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_4){
217         dlpf = ( unsigned char)DELAY_DLPF_4;
218     }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_3) {
219         dlpf = ( unsigned char)DELAY_DLPF_3;
220     }else{
221         dlpf = ( unsigned char)DELAY_DLPF_2;
222     }
223
224     reg = ( unsigned char)(( unsigned char)(EWTSA_range << 3) | dlpf | ( unsigned char)0x80 ) ;
225
226     err = i2c_write_byte(client, REG_FS_DLPF, reg);
227     if (err < 0) {
228         return err;
229     }
230
231     if (EWTSA_calib==  EWTSA_ON) {
232         printk("EWTSA_set_calibration() start \n");
233         err =  i2c_write_byte(client,( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_ENABLE);
234         if (err < 0) {
235                 return err;
236         }
237         mdelay(500);
238         printk("EWTSA_set_calibration() end \n");
239
240     }
241
242     return 0;
243 }
244
245 static int ewtsa_disable(struct i2c_client *client)
246 {
247         struct sensor_private_data *sensor =
248             (struct sensor_private_data *) i2c_get_clientdata(client);
249
250         gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
251
252         DBG("%s: end \n",__func__);
253
254         return 0;
255 }
256
257 static int ewtsa_enable(struct i2c_client *client)
258 {
259         int err;
260         struct sensor_private_data *sensor =
261             (struct sensor_private_data *) i2c_get_clientdata(client);
262
263         gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
264         err = i2c_write_byte(client, ( unsigned char)REG_PWR_MGM, ( unsigned char)SLEEP_CTRL_ACTIVATE);////0x44
265         if (err < 0){
266                 //return err;
267                 err = ewtsa_system_restart(client);///restart; only when i2c error
268                 if (err < 0){
269                         return err;
270                 }
271         }
272
273         err = i2c_write_byte(client,  ( unsigned char) REG_INT_CFG, ( unsigned char)INT_CFG_INT_ENABLE);
274         if (err < 0) {
275                 return err;
276         }
277         DBG("%s: end \n",__func__);
278         return 0;
279 }
280
281 void gyro_dev_reset(struct i2c_client *client)
282 {
283         struct sensor_private_data *sensor =
284             (struct sensor_private_data *) i2c_get_clientdata(client);
285
286
287     DBG("%s\n",__func__);
288         gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
289         msleep(100);
290         gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
291         msleep(100);
292 }
293
294 static int sensor_active(struct i2c_client *client, int enable, int rate)
295 {
296         /*
297         struct sensor_private_data *sensor =
298             (struct sensor_private_data *) i2c_get_clientdata(client);
299         int status = 0;
300         */
301         int result = 0;
302         if(enable)
303         {
304                 result=ewtsa_enable(client);
305         }
306         else
307         {
308                 result=ewtsa_disable(client);
309         }
310
311         if(result)
312                 printk("%s:fail to active sensor\n",__func__);
313
314         return result;
315
316 }
317
318 static int sensor_init(struct i2c_client *client)
319 {
320         struct sensor_private_data *sensor =
321             (struct sensor_private_data *) i2c_get_clientdata(client);
322         int result = 0;
323         /*
324         unsigned char buf[5];
325         unsigned char data = 0;
326         int i = 0;
327         char pReadData=0;
328         */
329         sensor->status_cur = SENSOR_OFF;
330         gyro_dev_reset(client);
331         ewtsa_system_restart(client);
332         return result;
333 }
334
335
336 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
337 {
338         struct sensor_private_data *sensor =
339                 (struct sensor_private_data *) i2c_get_clientdata(client);
340
341         /* Report GYRO  information */
342         input_report_rel(sensor->input_dev, ABS_RX, axis->x);
343         input_report_rel(sensor->input_dev, ABS_RY, axis->y);
344         input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
345         input_sync(sensor->input_dev);
346         DBG("gyro x==%d  y==%d z==%d\n",axis->x,axis->y,axis->z);
347
348         return 0;
349 }
350
351 static int sensor_report_value(struct i2c_client *client)
352 {
353         struct sensor_private_data *sensor =
354                 (struct sensor_private_data *) i2c_get_clientdata(client);
355         struct sensor_platform_data *pdata = sensor->pdata;
356         int ret = 0;
357         int x = 0, y = 0, z = 0;
358         struct sensor_axis axis;
359         char buffer[6] = {0};
360         int i = 0;
361         /* int value = 0; */
362
363         memset(buffer, 0, 6);
364 #if 0
365         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
366         do {
367                 buffer[0] = sensor->ops->read_reg;
368                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
369                 if (ret < 0)
370                 return ret;
371         } while (0);
372 #else
373
374         for(i=0; i<6; i++)
375         {
376                 i2c_read_byte(client, sensor->ops->read_reg + i,&buffer[i]);
377         }
378 #endif
379
380         x = (short) (((buffer[0]) << 8) | buffer[1]);
381         y = (short) (((buffer[2]) << 8) | buffer[3]);
382         z = (short) (((buffer[4]) << 8) | buffer[5]);
383
384         //printk("%s: x=%d  y=%d z=%d \n",__func__, x,y,z);
385         if(pdata && pdata->orientation)
386         {
387                 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
388                 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
389                 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
390         }
391         else
392         {
393                 axis.x = x;
394                 axis.y = y;
395                 axis.z = z;
396         }
397
398         //filter gyro data
399         if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))
400         {
401                 gyro_report_value(client, &axis);
402
403                  /* »¥³âµØ»º´æÊý¾Ý. */
404                 mutex_lock(&(sensor->data_mutex) );
405                 sensor->axis = axis;
406                 mutex_unlock(&(sensor->data_mutex) );
407         }
408
409         return ret;
410 }
411
412
413 struct sensor_operate gyro_ewtsa_ops = {
414         .name                           = "ewtsa",
415         .type                           = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct
416         .id_i2c                         = GYRO_ID_EWTSA,                //i2c id number
417         .read_reg                       = GYRO_DATA_REG,                //read data
418         .read_len                       = 6,                            //data length
419         .id_reg                         = -1,           //read device id from this register
420         .id_data                        = -1,           //device id
421         .precision                      = 8,                            //8 bits
422         .ctrl_reg                       = REG_PWR_MGM,          //enable or disable
423         .int_status_reg                 = REG_INT_STATUS,                       //intterupt status register,if no exist then -1
424         .range                          = {-32768,32768},               //range
425         .trig                           = IRQF_TRIGGER_HIGH|IRQF_ONESHOT,
426         .active                         = sensor_active,
427         .init                           = sensor_init,
428         .report                         = sensor_report_value,
429 };
430
431 /****************operate according to sensor chip:end************/
432
433 //function name should not be changed
434 static struct sensor_operate *gyro_get_ops(void)
435 {
436         return &gyro_ewtsa_ops;
437 }
438
439
440 static int __init gyro_ewtsa_init(void)
441 {
442         struct sensor_operate *ops = gyro_get_ops();
443         int result = 0;
444         int type = ops->type;
445         result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);
446         return result;
447 }
448
449 static void __exit gyro_ewtsa_exit(void)
450 {
451         struct sensor_operate *ops = gyro_get_ops();
452         int type = ops->type;
453         sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);
454 }
455
456
457 module_init(gyro_ewtsa_init);
458 module_exit(gyro_ewtsa_exit);
459
460