1 /* drivers/input/sensors/gyro/Ewtsa.c
\r
3 * Copyright (C) 2012-2015 ROCKCHIP.
\r
4 * Author: zhangaihui <zah@rock-chips.com>
\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
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
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 <mach/gpio.h>
\r
29 #include <mach/board.h>
\r
30 #ifdef CONFIG_HAS_EARLYSUSPEND
\r
31 #include <linux/earlysuspend.h>
\r
33 #include <linux/sensor-dev.h>
\r
35 /** This define controls compilation of the master device interface */
\r
36 /*#define EWTSA_MASTER_DEVICE*/
\r
38 #define GYRO_MOUNT_SWAP_XY 0 /* swap X, Y */
\r
39 #define GYRO_MOUNT_REVERSE_X 0 /* reverse X */
\r
40 #define GYRO_MOUNT_REVERSE_Y 0 /* reverse Y */
\r
41 #define GYRO_MOUNT_REVERSE_Z 0 /* reverse Z */
\r
44 /*#define CHIP_ID 0x68*/
\r
45 #define DEVICE_NAME "ewtsa"
\r
48 #define SLEEP_PIN 14
\r
51 #define MAX_VALUE 32768
\r
53 /* ewtsa_delay parameter */
\r
54 #define DELAY_THRES_MIN 1
\r
55 #define DELAY_THRES_1 4
\r
56 #define DELAY_THRES_2 9 /* msec x 90% */
\r
57 #define DELAY_THRES_3 18
\r
58 #define DELAY_THRES_4 45
\r
59 #define DELAY_THRES_5 90
\r
60 #define DELAY_THRES_6 128
\r
61 #define DELAY_THRES_MAX 255
\r
62 #define DELAY_DLPF_2 2
\r
63 #define DELAY_DLPF_3 3
\r
64 #define DELAY_DLPF_4 4
\r
65 #define DELAY_DLPF_5 5
\r
66 #define DELAY_DLPF_6 6
\r
67 #define DELAY_INTMIN_THRES 9
\r
69 #define DATA_RATE_1 0x01
\r
71 /* ewtsa_sleep parameter */
\r
76 #define EWTSA_POLLING_MODE 0
\r
77 #define EWTSA_INTERUPT_MODE 1
\r
79 /* ewtsa register address */
\r
80 #define REG_SMPL 0x15
\r
81 #define REG_FS_DLPF 0x16
\r
82 #define REG_INT_CFG 0x17
\r
83 #define REG_INT_STATUS 0x1A
\r
84 #define REG_SELF_O_C 0x29
\r
85 #define REG_PWR_MGM 0x3E
\r
86 #define REG_MBURST_ALL 0xFF
\r
87 #define GYRO_DATA_REG 0x1D
\r
89 /* ewtsa register param */
\r
90 #define SELF_O_C_ENABLE 0x00
\r
91 #define SELF_O_C_DISABLE 0x01
\r
92 #define SLEEP_CTRL_ACTIVATE 0x40
\r
93 #define SLEEP_CTRL_SLEEP 0x00
\r
94 #define INT_CFG_INT_ENABLE 0x01
\r
95 #define INT_CFG_INT_DISABLE 0x00
\r
97 /* ewtsa interrupt control */
\r
98 #define EWSTA_INT_CLEAR 0x00
\r
99 #define EWSTA_INT_SKIP 0x01
\r
102 #define EWTSA_BOOST_TIME_0 500
\r
104 /* sleep setting range */
\r
105 #define EWTSA_SLP_MIN 0
\r
106 #define EWTSA_SLP_MAX 1
\r
108 /* delay setting range */
\r
109 #define EWTSA_DLY_MIN 1
\r
110 #define EWTSA_DLY_MAX 255
\r
112 /* range setting range */
\r
113 #define EWTSA_RNG_MIN 0
\r
114 #define EWTSA_RNG_MAX 3
\r
116 /* soc setting range */
\r
117 #define EWTSA_SOC_MIN 0
\r
118 #define EWTSA_SOC_MAX 1
\r
120 /* event setting range */
\r
121 #define EWTSA_EVE_MIN 0
\r
122 #define EWTSA_EVE_MAX 1
\r
125 #define SLEEP_INIT_VAL (SLEEP_ON)
\r
126 #define DELAY_INIT_VAL 10
\r
127 #define RANGE_INIT_VAL 2 /*range 1000*/
\r
128 #define DLPF_INIT_VAL (DELAY_DLPF_2)
\r
129 #define CALIB_FUNC_INIT_VAL (EWTSA_ON)
\r
131 /*config store counter num*/
\r
132 #define CONFIG_COUNTER_MIN (6+9)
\r
133 #define CONFIG_COUNTER_MAX (32+9)
\r
136 #define COMMAND_NAME_SOC 0
\r
137 #define COMMAND_NAME_DLY 1
\r
138 #define COMMAND_NAME_RNG 2
\r
139 #define COMMAND_NAME_EVE 3
\r
140 #define COMMAND_NAME_SLP 4
\r
141 #define COMMAND_NAME_NUM 5
\r
143 #define EWTSA_delay DELAY_INIT_VAL
\r
144 #define EWTSA_range RANGE_INIT_VAL
\r
145 #define EWTSA_calib EWTSA_ON
\r
147 /****************operate according to sensor chip:start************/
\r
148 static int i2c_read_byte(struct i2c_client *thisClient, unsigned char regAddr, char *pReadData)
\r
152 ret = i2c_master_send( thisClient, (char*)®Addr, 1);
155 printk("EWTSA send cAddress=0x%x error!\n", regAddr);
\r
158 ret = i2c_master_recv( thisClient, (char*)pReadData, 1);
161 printk("EWTSAread *pReadData=0x%x error!\n", *pReadData);
\r
167 static int i2c_write_byte(struct i2c_client *thisClient, unsigned char regAddr, unsigned char writeData)
\r
169 char write_data[2] = {0};
172 write_data[0] = regAddr;
173 write_data[1] = writeData;
175 ret = i2c_master_send(thisClient, write_data, 2);
178 ret = i2c_master_send(thisClient, write_data, 2);
181 printk("EWTSA send regAddr=0x%x error!\n", regAddr);
\r
190 static int ewtsa_system_restart(struct i2c_client *client)
\r
196 err = i2c_write_byte(client, ( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_DISABLE);
\r
202 if (EWTSA_delay <= ( unsigned char)DELAY_THRES_2) {
\r
203 smpl = ( unsigned char)DELAY_INTMIN_THRES;
\r
205 smpl = ( unsigned char)(EWTSA_delay - ( unsigned char)1);
\r
207 err = i2c_write_byte(client, ( unsigned char)REG_SMPL, ( unsigned char)smpl);
\r
213 if (EWTSA_delay >= ( unsigned char)DELAY_THRES_6){
\r
214 dlpf = ( unsigned char)DELAY_DLPF_6;
\r
215 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_5) {
\r
216 dlpf = ( unsigned char)DELAY_DLPF_5;
\r
217 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_4){
\r
218 dlpf = ( unsigned char)DELAY_DLPF_4;
\r
219 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_3) {
\r
220 dlpf = ( unsigned char)DELAY_DLPF_3;
\r
222 dlpf = ( unsigned char)DELAY_DLPF_2;
\r
225 reg = ( unsigned char)(( unsigned char)(EWTSA_range << 3) | dlpf | ( unsigned char)0x80 ) ;
\r
227 err = i2c_write_byte(client, REG_FS_DLPF, reg);
\r
232 if (EWTSA_calib== EWTSA_ON) {
\r
233 printk("EWTSA_set_calibration() start \n");
\r
234 err = i2c_write_byte(client,( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_ENABLE);
\r
239 printk("EWTSA_set_calibration() end \n");
\r
246 static int ewtsa_disable(struct i2c_client *client)
\r
248 struct sensor_private_data *sensor =
\r
249 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
251 gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
\r
253 DBG("%s: end \n",__func__);
\r
258 static int ewtsa_enable(struct i2c_client *client)
\r
261 struct sensor_private_data *sensor =
\r
262 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
264 gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
\r
265 err = i2c_write_byte(client, ( unsigned char)REG_PWR_MGM, ( unsigned char)SLEEP_CTRL_ACTIVATE);////0x44
\r
268 err = ewtsa_system_restart(client);///restart; only when i2c error
\r
274 err = i2c_write_byte(client, ( unsigned char) REG_INT_CFG, ( unsigned char)INT_CFG_INT_ENABLE);
\r
278 DBG("%s: end \n",__func__);
\r
282 void gyro_dev_reset(struct i2c_client *client)
\r
284 struct sensor_private_data *sensor =
\r
285 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
288 DBG("%s\n",__func__);
\r
289 gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
\r
291 gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
\r
295 static int sensor_active(struct i2c_client *client, int enable, int rate)
\r
297 struct sensor_private_data *sensor =
\r
298 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
301 //register setting according to chip datasheet
\r
304 result=ewtsa_enable(client);
\r
308 result=ewtsa_disable(client);
\r
312 printk("%s:fail to active sensor\n",__func__);
\r
318 static int sensor_init(struct i2c_client *client)
\r
320 struct sensor_private_data *sensor =
\r
321 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
323 unsigned char buf[5];
\r
324 unsigned char data = 0;
\r
328 sensor->status_cur = SENSOR_OFF;
\r
329 gyro_dev_reset(client);
\r
330 ewtsa_system_restart(client);
\r
335 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
\r
337 struct sensor_private_data *sensor =
\r
338 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
340 /* Report GYRO information */
\r
341 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
\r
342 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
\r
343 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
\r
344 input_sync(sensor->input_dev);
\r
345 DBG("gyro x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
\r
350 static int sensor_report_value(struct i2c_client *client)
\r
352 struct sensor_private_data *sensor =
\r
353 (struct sensor_private_data *) i2c_get_clientdata(client);
\r
354 struct sensor_platform_data *pdata = sensor->pdata;
\r
356 int x = 0, y = 0, z = 0;
\r
357 struct sensor_axis axis;
\r
358 char buffer[6] = {0};
\r
362 memset(buffer, 0, 6);
\r
364 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
\r
366 buffer[0] = sensor->ops->read_reg;
\r
367 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
\r
375 i2c_read_byte(client, sensor->ops->read_reg + i,&buffer[i]);
\r
379 x = (short) (((buffer[0]) << 8) | buffer[1]);
\r
380 y = (short) (((buffer[2]) << 8) | buffer[3]);
\r
381 z = (short) (((buffer[4]) << 8) | buffer[5]);
\r
383 //printk("%s: x=%d y=%d z=%d \n",__func__, x,y,z);
\r
384 if(pdata && pdata->orientation)
386 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
387 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
388 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
398 if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))
\r
400 gyro_report_value(client, &axis);
\r
402 /* »¥³âµØ»º´æÊý¾Ý. */
\r
403 mutex_lock(&(sensor->data_mutex) );
\r
404 sensor->axis = axis;
\r
405 mutex_unlock(&(sensor->data_mutex) );
\r
412 struct sensor_operate gyro_ewtsa_ops = {
\r
414 .type = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct
\r
415 .id_i2c = GYRO_ID_EWTSA, //i2c id number
\r
416 .read_reg = GYRO_DATA_REG, //read data
\r
417 .read_len = 6, //data length
\r
418 .id_reg = -1, //read device id from this register
\r
419 .id_data = -1, //device id
\r
420 .precision = 8, //8 bits
\r
421 .ctrl_reg = REG_PWR_MGM, //enable or disable
\r
422 .int_status_reg = REG_INT_STATUS, //intterupt status register,if no exist then -1
\r
423 .range = {-32768,32768}, //range
\r
424 .trig = IRQF_TRIGGER_HIGH|IRQF_ONESHOT,
\r
425 .active = sensor_active,
\r
426 .init = sensor_init,
\r
427 .report = sensor_report_value,
\r
430 /****************operate according to sensor chip:end************/
\r
432 //function name should not be changed
\r
433 static struct sensor_operate *gyro_get_ops(void)
\r
435 return &gyro_ewtsa_ops;
\r
439 static int __init gyro_ewtsa_init(void)
\r
441 struct sensor_operate *ops = gyro_get_ops();
\r
443 int type = ops->type;
\r
444 result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);
\r
448 static void __exit gyro_ewtsa_exit(void)
\r
450 struct sensor_operate *ops = gyro_get_ops();
\r
451 int type = ops->type;
\r
452 sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);
\r
456 module_init(gyro_ewtsa_init);
\r
457 module_exit(gyro_ewtsa_exit);
\r