--- /dev/null
+/* drivers/input/sensors/access/kxtik.c\r
+ *\r
+ * Copyright (C) 2012-2015 ROCKCHIP.\r
+ * Author: Bruins <xwj@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
+\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
+\r
+#define LSM303D_WHO_AM_I (0x0F)\r
+\r
+/* full scale setting - register & mask */\r
+#define LSM303D_CTRL_REG0 (0x1F)\r
+#define LSM303D_CTRL_REG1 (0x20)\r
+#define LSM303D_CTRL_REG2 (0x21)\r
+#define LSM303D_CTRL_REG3 (0x22)\r
+#define LSM303D_CTRL_REG4 (0x23)\r
+#define LSM303D_CTRL_REG5 (0x24)\r
+#define LSM303D_CTRL_REG6 (0x25)\r
+#define LSM303D_CTRL_REG7 (0x26)\r
+#define LSM303D_STATUS_REG (0x27)\r
+#define LSM303D_OUT_X_L (0x28)\r
+#define LSM303D_OUT_X_H (0x29)\r
+#define LSM303D_OUT_Y_L (0x2a)\r
+#define LSM303D_OUT_Y_H (0x2b)\r
+#define LSM303D_OUT_Z_L (0x2c)\r
+#define LSM303D_OUT_Z_H (0x2d)\r
+#define LSM303D_FIFO_CTRL_REG (0x2E)\r
+#define LSM303D_FIFO_SRC_REG (0X2F)\r
+\r
+#define LSM303D_IG_CFG1 (0x30)\r
+#define LSM303D_IG_SRC1 (0x31)\r
+#define LSM303D_IG_THS1 (0x32)\r
+#define LSM303D_IG_DURATION1 (0x33)\r
+\r
+#define LSM303D_IG_CFG2 (0x34)\r
+#define LSM303D_IG_SRC2 (0x35)\r
+#define LSM303D_IG_THS2 (0x36)\r
+#define LSM303D_IG_DURATION2 (0x37)\r
+\r
+\r
+#define LSM303D_DEVID (0x49) //chip id\r
+#define LSM303D_ACC_DISABLE (0x08)\r
+\r
+#define LSM303D_RANGE 2000000\r
+\r
+/* LSM303D */\r
+#define LSM303D_PRECISION 16\r
+#define LSM303D_BOUNDARY (0x1 << (LSM303D_PRECISION - 1))\r
+#define LSM303D_GRAVITY_STEP (LSM303D_RANGE / LSM303D_BOUNDARY)\r
+\r
+#define ODR3P25 0x10 /* 3.25Hz output data rate */\r
+#define ODR6P25 0x20 /* 6.25Hz output data rate */\r
+#define ODR12P5 0x30 /* 12.5Hz output data rate */\r
+#define ODR25 0x40 /* 25Hz output data rate */\r
+#define ODR50 0x50 /* 50Hz output data rate */\r
+#define ODR100 0x60 /* 100Hz output data rate */\r
+#define ODR200 0x70 /* 200Hz output data rate */\r
+#define ODR400 0x80 /* 400Hz output data rate */\r
+#define ODR800 0x90 /* 800Hz output data rate */\r
+#define ODR1600 0xA0 /* 1600Hz output data rate */\r
+\r
+\r
+struct sensor_reg_data {\r
+ char reg;\r
+ char data;\r
+};\r
+\r
+/****************operate according to sensor chip:start************/\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
+ \r
+ sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
+\r
+ sensor->ops->ctrl_data |= ODR100; //100HZ,if 0 then power down\r
+ \r
+ //register setting according to chip datasheet \r
+ if(!enable)\r
+ { \r
+ status = LSM303D_ACC_DISABLE; //lis3dh \r
+ sensor->ops->ctrl_data |= status; \r
+ }\r
+ else\r
+ {\r
+ status = ~LSM303D_ACC_DISABLE; //lis3dh\r
+ sensor->ops->ctrl_data &= status;\r
+ }\r
+\r
+ DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);\r
+ result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);\r
+ if(result)\r
+ printk("%s:fail to active sensor\n",__func__);\r
+ \r
+ return result;\r
+\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
+ int i;\r
+\r
+ struct sensor_reg_data reg_data[] = \r
+ {\r
+ {LSM303D_CTRL_REG0,0x00},\r
+ {LSM303D_CTRL_REG1,0x07},\r
+ {LSM303D_CTRL_REG2,0x00},\r
+ {LSM303D_CTRL_REG3,0x00},\r
+ {LSM303D_CTRL_REG4,0x00},\r
+ {LSM303D_CTRL_REG5,0x78}, //High resolution output mode:11,\r
+ {LSM303D_CTRL_REG6,0x20},\r
+ {LSM303D_CTRL_REG7,0x00},\r
+ {LSM303D_FIFO_CTRL_REG,0x00},\r
+ {LSM303D_IG_CFG1,0xFF}, //6 direction position recognition \r
+ {LSM303D_IG_THS1,0x7F}, //Interrupt 1 threshold \r
+ {LSM303D_IG_DURATION1,0x7F}, //Duration value 0x00->ox7f\r
+ \r
+ /*\r
+ {LSM303D_CTRL_REG7,0x00}, \r
+ {LSM303D_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode \r
+ {LSM303D_CTRL_REG6,0x40}, \r
+ \r
+ {LSM303D_FIFO_CTRL_REG,0x00}, // \r
+ {LSM303D_IG_CFG1,0xFF}, //6 direction position recognition \r
+ {LSM303D_IG_THS1,0x7F}, //Interrupt 1 threshold \r
+ {LSM303D_IG_DURATION1,0x7F}, //Duration value 0x00->ox7f\r
+ */\r
+ }; \r
+ \r
+ result = sensor->ops->active(client,0,0);\r
+ if(result)\r
+ {\r
+ printk("%s:line=%d,error\n",__func__,__LINE__);\r
+ return result;\r
+ }\r
+\r
+ sensor->status_cur = SENSOR_OFF;\r
+ \r
+ for(i=0;i<(sizeof(reg_data)/sizeof(struct sensor_reg_data));i++)\r
+ {\r
+ result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);\r
+ if(result)\r
+ {\r
+ printk("%s:line=%d,i=%d,error\n",__func__,__LINE__,i);\r
+ return result;\r
+ }\r
+ }\r
+\r
+ \r
+ if(sensor->pdata->irq_enable)\r
+ {\r
+\r
+ result = sensor_write_reg(client, LSM303D_CTRL_REG3, 0x20); \r
+ if(result)\r
+ {\r
+ printk("%s:line=%d,error\n",__func__,__LINE__);\r
+ return result;\r
+ }\r
+\r
+ i = sensor_read_reg(client,LSM303D_CTRL_REG5);\r
+ \r
+ result = sensor_write_reg(client, LSM303D_CTRL_REG5, (i|0x01));\r
+ if(result)\r
+ {\r
+ printk("%s:line=%d,error\n",__func__,__LINE__);\r
+ return result;\r
+ }\r
+\r
+ }\r
+ \r
+ return result;\r
+}\r
+\r
+\r
+static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)\r
+{\r
+ s64 result;\r
+ struct sensor_private_data *sensor =\r
+ (struct sensor_private_data *) i2c_get_clientdata(client); \r
+ \r
+ switch (sensor->devid) { \r
+ case LSM303D_DEVID: \r
+ result = ((int)high_byte << 8) | (int)low_byte;\r
+ if (result < LSM303D_BOUNDARY)\r
+ result = result* LSM303D_GRAVITY_STEP;\r
+ else\r
+ result = ~( ((~result & (0x7fff>>(16-LSM303D_PRECISION)) ) + 1) \r
+ * LSM303D_GRAVITY_STEP) + 1;\r
+ break;\r
+\r
+ default:\r
+ printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);\r
+ return -EFAULT;\r
+ }\r
+\r
+ return (int)result;\r
+}\r
+\r
+static int gsensor_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 acceleration sensor information */\r
+ input_report_abs(sensor->input_dev, ABS_X, axis->x);\r
+ input_report_abs(sensor->input_dev, ABS_Y, axis->y);\r
+ input_report_abs(sensor->input_dev, ABS_Z, axis->z);\r
+ input_sync(sensor->input_dev);\r
+ DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);\r
+\r
+ return 0;\r
+}\r
+\r
+#define GSENSOR_MIN 10\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,y,z;\r
+ struct sensor_axis axis; \r
+ char buffer[6] = {0}; \r
+ char value = 0;\r
+ \r
+ if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6\r
+ {\r
+ printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);\r
+ return -1;\r
+ }\r
+ \r
+ memset(buffer, 0, 6);\r
+\r
+ value = sensor_read_reg(client, LSM303D_STATUS_REG);\r
+ if((value & 0x0f) == 0)\r
+ {\r
+ printk("%s:line=%d,value=0x%x,data is not ready\n",__func__,__LINE__,value);\r
+ return -1;\r
+ }\r
+ \r
+ \r
+ /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ \r
+ do {\r
+ *buffer = 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
+\r
+ //this gsensor need 6 bytes buffer\r
+ x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit \r
+ y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);\r
+ z = sensor_convert_data(sensor->client, buffer[5], buffer[4]); \r
+\r
+ axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;\r
+ axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; \r
+ axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;\r
+\r
+ DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);\r
+ //printk( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);\r
+\r
+ //Report event only while value is changed to save some power\r
+ 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))\r
+ {\r
+ gsensor_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
+ if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register\r
+ {\r
+ \r
+ value = sensor_read_reg(client, sensor->ops->int_status_reg);\r
+ DBG("%s:sensor int status :0x%x\n",__func__,value);\r
+ }\r
+ \r
+ return ret;\r
+}\r
+\r
+struct sensor_operate gsensor_lsm303d_ops = {\r
+ .name = "lsm303d",\r
+ .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct\r
+ .id_i2c = ACCEL_ID_LSM303D, //i2c id number\r
+ .read_reg = (LSM303D_OUT_X_L | 0x80), //read data\r
+ .read_len = 6, //data length\r
+ .id_reg = LSM303D_WHO_AM_I, //read device id from this register\r
+ .id_data = LSM303D_DEVID, //device id\r
+ .precision = LSM303D_PRECISION, //16 bits\r
+ .ctrl_reg = LSM303D_CTRL_REG1, //enable or disable \r
+ .int_status_reg = LSM303D_IG_SRC1, //intterupt status register\r
+ .range = {-LSM303D_RANGE,LSM303D_RANGE}, //range\r
+ .trig = (IRQF_TRIGGER_LOW|IRQF_ONESHOT), \r
+ .active = sensor_active, \r
+ .init = sensor_init,\r
+ .report = sensor_report_value,\r
+};\r
+\r
+\r
+/****************operate according to sensor chip:end************/\r
+\r
+//function name should not be changed\r
+static struct sensor_operate *gsensor_get_ops(void)\r
+{\r
+ return &gsensor_lsm303d_ops;\r
+}\r
+\r
+\r
+static int __init gsensor_lis3dh_init(void)\r
+{\r
+ struct sensor_operate *ops = gsensor_get_ops();\r
+ int result = 0;\r
+ int type = ops->type;\r
+ result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);\r
+ return result;\r
+}\r
+\r
+static void __exit gsensor_lis3dh_exit(void)\r
+{\r
+ struct sensor_operate *ops = gsensor_get_ops();\r
+ int type = ops->type;\r
+ sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);\r
+}\r
+\r
+\r
+module_init(gsensor_lis3dh_init);\r
+module_exit(gsensor_lis3dh_exit);\r
+\r
+\r
+\r
+\r