--- /dev/null
+/* drivers/input/sensors/gyro/Ewtsa.c\r
+ *\r
+ * Copyright (C) 2012-2015 ROCKCHIP.\r
+ * Author: zhangaihui <zah@rock-chips.com>\r
+ *\r
+ * This software is licensed under the terms of the GNU General Public\r
+ * License version 2, as published by the Free Software Foundation, and\r
+ * may be copied, distributed, and modified under those terms.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ */\r
+#include <linux/interrupt.h>\r
+#include <linux/i2c.h>\r
+#include <linux/slab.h>\r
+#include <linux/irq.h>\r
+#include <linux/miscdevice.h>\r
+#include <linux/gpio.h>\r
+#include <asm/uaccess.h>\r
+#include <asm/atomic.h>\r
+#include <linux/delay.h>\r
+#include <linux/input.h>\r
+#include <linux/workqueue.h>\r
+#include <linux/freezer.h>\r
+#include <mach/gpio.h>\r
+#include <mach/board.h> \r
+#ifdef CONFIG_HAS_EARLYSUSPEND\r
+#include <linux/earlysuspend.h>\r
+#endif\r
+#include <linux/sensor-dev.h>\r
+\r
+/** This define controls compilation of the master device interface */\r
+/*#define EWTSA_MASTER_DEVICE*/\r
+/* configurable */\r
+#define GYRO_MOUNT_SWAP_XY 0 /* swap X, Y */\r
+#define GYRO_MOUNT_REVERSE_X 0 /* reverse X */\r
+#define GYRO_MOUNT_REVERSE_Y 0 /* reverse Y */\r
+#define GYRO_MOUNT_REVERSE_Z 0 /* reverse Z */\r
+\r
+/* macro defines */\r
+/*#define CHIP_ID 0x68*/\r
+#define DEVICE_NAME "ewtsa"\r
+#define EWTSA_ON 1\r
+#define EWTSA_OFF 0\r
+#define SLEEP_PIN 14\r
+#define DRDY_PIN 12\r
+#define DIAG_PIN 11\r
+#define MAX_VALUE 32768\r
+\r
+/* ewtsa_delay parameter */\r
+#define DELAY_THRES_MIN 1\r
+#define DELAY_THRES_1 4\r
+#define DELAY_THRES_2 9 /* msec x 90% */\r
+#define DELAY_THRES_3 18\r
+#define DELAY_THRES_4 45\r
+#define DELAY_THRES_5 90\r
+#define DELAY_THRES_6 128\r
+#define DELAY_THRES_MAX 255\r
+#define DELAY_DLPF_2 2\r
+#define DELAY_DLPF_3 3\r
+#define DELAY_DLPF_4 4\r
+#define DELAY_DLPF_5 5\r
+#define DELAY_DLPF_6 6\r
+#define DELAY_INTMIN_THRES 9\r
+\r
+#define DATA_RATE_1 0x01\r
+\r
+/* ewtsa_sleep parameter */\r
+#define SLEEP_OFF 0\r
+#define SLEEP_ON 1\r
+\r
+/* event mode */\r
+#define EWTSA_POLLING_MODE 0\r
+#define EWTSA_INTERUPT_MODE 1\r
+\r
+/* ewtsa register address */\r
+#define REG_SMPL 0x15\r
+#define REG_FS_DLPF 0x16\r
+#define REG_INT_CFG 0x17\r
+#define REG_INT_STATUS 0x1A\r
+#define REG_SELF_O_C 0x29\r
+#define REG_PWR_MGM 0x3E\r
+#define REG_MBURST_ALL 0xFF\r
+#define GYRO_DATA_REG 0x1D\r
+\r
+/* ewtsa register param */\r
+#define SELF_O_C_ENABLE 0x00\r
+#define SELF_O_C_DISABLE 0x01\r
+#define SLEEP_CTRL_ACTIVATE 0x40\r
+#define SLEEP_CTRL_SLEEP 0x00\r
+#define INT_CFG_INT_ENABLE 0x01\r
+#define INT_CFG_INT_DISABLE 0x00\r
+\r
+/* ewtsa interrupt control */\r
+#define EWSTA_INT_CLEAR 0x00\r
+#define EWSTA_INT_SKIP 0x01\r
+\r
+/* wait time(ms)*/\r
+#define EWTSA_BOOST_TIME_0 500\r
+\r
+/* sleep setting range */\r
+#define EWTSA_SLP_MIN 0\r
+#define EWTSA_SLP_MAX 1\r
+\r
+/* delay setting range */\r
+#define EWTSA_DLY_MIN 1\r
+#define EWTSA_DLY_MAX 255\r
+\r
+/* range setting range */\r
+#define EWTSA_RNG_MIN 0\r
+#define EWTSA_RNG_MAX 3\r
+\r
+/* soc setting range */\r
+#define EWTSA_SOC_MIN 0\r
+#define EWTSA_SOC_MAX 1\r
+\r
+/* event setting range */\r
+#define EWTSA_EVE_MIN 0\r
+#define EWTSA_EVE_MAX 1\r
+\r
+/* init param */\r
+#define SLEEP_INIT_VAL (SLEEP_ON)\r
+#define DELAY_INIT_VAL 10\r
+#define RANGE_INIT_VAL 2 /*range 1000*/\r
+#define DLPF_INIT_VAL (DELAY_DLPF_2)\r
+#define CALIB_FUNC_INIT_VAL (EWTSA_ON)\r
+\r
+/*config store counter num*/\r
+#define CONFIG_COUNTER_MIN (6+9)\r
+#define CONFIG_COUNTER_MAX (32+9)\r
+\r
+/*command name */\r
+#define COMMAND_NAME_SOC 0\r
+#define COMMAND_NAME_DLY 1\r
+#define COMMAND_NAME_RNG 2\r
+#define COMMAND_NAME_EVE 3\r
+#define COMMAND_NAME_SLP 4\r
+#define COMMAND_NAME_NUM 5\r
+\r
+#define EWTSA_delay DELAY_INIT_VAL\r
+#define EWTSA_range RANGE_INIT_VAL\r
+#define EWTSA_calib EWTSA_ON\r
+\r
+/****************operate according to sensor chip:start************/\r
+static int i2c_read_byte(struct i2c_client *thisClient, unsigned char regAddr, char *pReadData)\r
+{
+ int ret = 0;
+
+ ret = i2c_master_send( thisClient, (char*)®Addr, 1);
+ if(ret < 0)
+ {
+ printk("EWTSA send cAddress=0x%x error!\n", regAddr);\r
+ return ret;
+ }
+ ret = i2c_master_recv( thisClient, (char*)pReadData, 1);
+ if(ret < 0)
+ {
+ printk("EWTSAread *pReadData=0x%x error!\n", *pReadData);\r
+ return ret;
+ }
+
+ return 1;
+}\r
+static int i2c_write_byte(struct i2c_client *thisClient, unsigned char regAddr, unsigned char writeData)\r
+{
+ char write_data[2] = {0};
+ int ret=0;
+
+ write_data[0] = regAddr;
+ write_data[1] = writeData;
+
+ ret = i2c_master_send(thisClient, write_data, 2);
+ if (ret < 0)
+ {
+ ret = i2c_master_send(thisClient, write_data, 2);
+ if (ret < 0)
+ {
+ printk("EWTSA send regAddr=0x%x error!\n", regAddr);\r
+ return ret;
+ }
+ return 1;
+ }
+
+ return 1;
+}\r
+\r
+static int ewtsa_system_restart(struct i2c_client *client)\r
+{
+ int err;
+ char reg;\r
+ char smpl , dlpf;\r
+\r
+ err = i2c_write_byte(client, ( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_DISABLE);\r
+ if (err < 0) {
+ return err;
+ }
+
+ ///Set SMPL register
+ if (EWTSA_delay <= ( unsigned char)DELAY_THRES_2) {\r
+ smpl = ( unsigned char)DELAY_INTMIN_THRES;\r
+ }else{
+ smpl = ( unsigned char)(EWTSA_delay - ( unsigned char)1);\r
+ }\r
+ err = i2c_write_byte(client, ( unsigned char)REG_SMPL, ( unsigned char)smpl);\r
+ if (err < 0) {
+ return err;
+ }
+
+ ///Set DLPF register
+ if (EWTSA_delay >= ( unsigned char)DELAY_THRES_6){\r
+ dlpf = ( unsigned char)DELAY_DLPF_6;\r
+ }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_5) {\r
+ dlpf = ( unsigned char)DELAY_DLPF_5;\r
+ }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_4){\r
+ dlpf = ( unsigned char)DELAY_DLPF_4;\r
+ }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_3) {\r
+ dlpf = ( unsigned char)DELAY_DLPF_3;\r
+ }else{
+ dlpf = ( unsigned char)DELAY_DLPF_2;\r
+ }
+
+ reg = ( unsigned char)(( unsigned char)(EWTSA_range << 3) | dlpf | ( unsigned char)0x80 ) ;\r
+\r
+ err = i2c_write_byte(client, REG_FS_DLPF, reg);\r
+ if (err < 0) {
+ return err;
+ }
+
+ if (EWTSA_calib== EWTSA_ON) { \r
+ printk("EWTSA_set_calibration() start \n");\r
+ err = i2c_write_byte(client,( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_ENABLE);\r
+ if (err < 0) {\r
+ return err;\r
+ }\r
+ mdelay(500); \r
+ printk("EWTSA_set_calibration() end \n");\r
+ \r
+ }
+
+ return 0;\r
+}
+\r
+static int ewtsa_disable(struct i2c_client *client)\r
+{
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ \r
+ gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);\r
+\r
+ DBG("%s: end \n",__func__); \r
+\r
+ return 0; \r
+}
+\r
+static int ewtsa_enable(struct i2c_client *client)\r
+{
+ int err;\r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ \r
+ gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);\r
+ err = i2c_write_byte(client, ( unsigned char)REG_PWR_MGM, ( unsigned char)SLEEP_CTRL_ACTIVATE);////0x44\r
+ if (err < 0){\r
+ //return err;\r
+ err = ewtsa_system_restart(client);///restart; only when i2c error\r
+ if (err < 0){\r
+ return err;\r
+ }\r
+ }\r
+\r
+ err = i2c_write_byte(client, ( unsigned char) REG_INT_CFG, ( unsigned char)INT_CFG_INT_ENABLE);\r
+ if (err < 0) {\r
+ return err;\r
+ }\r
+ DBG("%s: end \n",__func__); \r
+ return 0;\r
+}
+\r
+void gyro_dev_reset(struct i2c_client *client)\r
+{
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+\r
+ \r
+ DBG("%s\n",__func__);\r
+ gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);\r
+ msleep(100); \r
+ gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);\r
+ msleep(100); \r
+}\r
+\r
+static int sensor_active(struct i2c_client *client, int enable, int rate)\r
+{\r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ int result = 0;\r
+ int status = 0;\r
+ //register setting according to chip datasheet \r
+ if(enable)\r
+ { \r
+ result=ewtsa_enable(client);\r
+ }\r
+ else\r
+ {\r
+ result=ewtsa_disable(client);\r
+ }\r
+\r
+ if(result)\r
+ printk("%s:fail to active sensor\n",__func__);\r
+\r
+ return result;\r
+\r
+}\r
+\r
+static int sensor_init(struct i2c_client *client)\r
+{ \r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ int result = 0; \r
+ unsigned char buf[5]; \r
+ unsigned char data = 0;\r
+ int i = 0;\r
+ char pReadData=0;\r
+ \r
+ sensor->status_cur = SENSOR_OFF;\r
+ gyro_dev_reset(client);\r
+ ewtsa_system_restart(client);\r
+ return result;\r
+}\r
+\r
+\r
+static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)\r
+{\r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+\r
+ /* Report GYRO information */\r
+ input_report_rel(sensor->input_dev, ABS_RX, axis->x);\r
+ input_report_rel(sensor->input_dev, ABS_RY, axis->y);\r
+ input_report_rel(sensor->input_dev, ABS_RZ, axis->z);\r
+ input_sync(sensor->input_dev);\r
+ DBG("gyro x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);\r
+\r
+ return 0;\r
+}\r
+\r
+static int sensor_report_value(struct i2c_client *client)\r
+{\r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ struct sensor_platform_data *pdata = sensor->pdata;\r
+ int ret = 0;\r
+ int x = 0, y = 0, z = 0;\r
+ struct sensor_axis axis;\r
+ char buffer[6] = {0}; \r
+ int i = 0;\r
+ int value = 0;\r
+\r
+ memset(buffer, 0, 6);\r
+#if 0 \r
+ /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ \r
+ do {\r
+ buffer[0] = sensor->ops->read_reg;\r
+ ret = sensor_rx_data(client, buffer, sensor->ops->read_len);\r
+ if (ret < 0)\r
+ return ret;\r
+ } while (0);\r
+#else\r
+\r
+ for(i=0; i<6; i++)\r
+ {
+ i2c_read_byte(client, sensor->ops->read_reg + i,&buffer[i]);\r
+ }\r
+#endif\r
+ \r
+ x = (short) (((buffer[0]) << 8) | buffer[1]);\r
+ y = (short) (((buffer[2]) << 8) | buffer[3]);\r
+ z = (short) (((buffer[4]) << 8) | buffer[5]);\r
+
+ //printk("%s: x=%d y=%d z=%d \n",__func__, x,y,z);\r
+ if(pdata && pdata->orientation)
+ {
+ axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
+ axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
+ axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
+ }
+ else
+ {
+ axis.x = x;
+ axis.y = y;
+ axis.z = z;
+ }
+
+ //filter gyro data
+ if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))\r
+ { \r
+ gyro_report_value(client, &axis); \r
+\r
+ /* »¥³âµØ»º´æÊý¾Ý. */\r
+ mutex_lock(&(sensor->data_mutex) );\r
+ sensor->axis = axis;\r
+ mutex_unlock(&(sensor->data_mutex) );\r
+ } \r
+ \r
+ return ret;\r
+}\r
+\r
+\r
+struct sensor_operate gyro_ewtsa_ops = {\r
+ .name = "ewtsa",\r
+ .type = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct\r
+ .id_i2c = GYRO_ID_EWTSA, //i2c id number\r
+ .read_reg = GYRO_DATA_REG, //read data\r
+ .read_len = 6, //data length\r
+ .id_reg = -1, //read device id from this register\r
+ .id_data = -1, //device id\r
+ .precision = 8, //8 bits\r
+ .ctrl_reg = REG_PWR_MGM, //enable or disable \r
+ .int_status_reg = REG_INT_STATUS, //intterupt status register,if no exist then -1\r
+ .range = {-32768,32768}, //range\r
+ .trig = IRQF_TRIGGER_HIGH|IRQF_ONESHOT, \r
+ .active = sensor_active, \r
+ .init = sensor_init,\r
+ .report = sensor_report_value,\r
+};\r
+\r
+/****************operate according to sensor chip:end************/\r
+\r
+//function name should not be changed\r
+static struct sensor_operate *gyro_get_ops(void)\r
+{\r
+ return &gyro_ewtsa_ops;\r
+}\r
+\r
+\r
+static int __init gyro_ewtsa_init(void)\r
+{\r
+ struct sensor_operate *ops = gyro_get_ops();\r
+ int result = 0;\r
+ int type = ops->type;\r
+ result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);\r
+ return result;\r
+}\r
+\r
+static void __exit gyro_ewtsa_exit(void)\r
+{\r
+ struct sensor_operate *ops = gyro_get_ops();\r
+ int type = ops->type;\r
+ sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);\r
+}\r
+\r
+\r
+module_init(gyro_ewtsa_init);\r
+module_exit(gyro_ewtsa_exit);\r
+\r
+\r