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