--- /dev/null
+/* drivers/input/sensors/access/kxtik.c\r
+ *\r
+ * Copyright (C) 2012-2015 ROCKCHIP.\r
+ * Author: luowei <lw@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
+#if 0\r
+#define SENSOR_DEBUG_TYPE SENSOR_TYPE_ACCEL\r
+#define DBG(x...) if(sensor->pdata->type == SENSOR_DEBUG_TYPE) printk(x)\r
+#else\r
+#define DBG(x...)\r
+#endif\r
+\r
+#define LIS3DH_INT_COUNT (0x0E)\r
+#define LIS3DH_WHO_AM_I (0x0F)\r
+\r
+/* full scale setting - register & mask */\r
+#define LIS3DH_TEMP_CFG_REG (0x1F)\r
+#define LIS3DH_CTRL_REG1 (0x20)\r
+#define LIS3DH_CTRL_REG2 (0x21)\r
+#define LIS3DH_CTRL_REG3 (0x22)\r
+#define LIS3DH_CTRL_REG4 (0x23)\r
+#define LIS3DH_CTRL_REG5 (0x24)\r
+#define LIS3DH_CTRL_REG6 (0x25)\r
+#define LIS3DH_REFERENCE (0x26)\r
+#define LIS3DH_STATUS_REG (0x27)\r
+#define LIS3DH_OUT_X_L (0x28)\r
+#define LIS3DH_OUT_X_H (0x29)\r
+#define LIS3DH_OUT_Y_L (0x2a)\r
+#define LIS3DH_OUT_Y_H (0x2b)\r
+#define LIS3DH_OUT_Z_L (0x2c)\r
+#define LIS3DH_OUT_Z_H (0x2d)\r
+#define LIS3DH_FIFO_CTRL_REG (0x2E)\r
+\r
+#define LIS3DH_INT1_CFG (0x30)\r
+#define LIS3DH_INT1_SRC (0x31)\r
+#define LIS3DH_INT1_THS (0x32)\r
+#define LIS3DH_INT1_DURATION (0x33)\r
+\r
+#define LIS3DH_DEVID (0x33) //chip id\r
+#define LIS3DH_ACC_DISABLE (0x08)\r
+\r
+#define LIS3DH_RANGE 2000000\r
+\r
+/* LIS3DH */\r
+#define LIS3DH_PRECISION 12\r
+#define LIS3DH_BOUNDARY (0x1 << (LIS3DH_PRECISION - 1))\r
+#define LIS3DH_GRAVITY_STEP (LIS3DH_RANGE / LIS3DH_BOUNDARY)\r
+\r
+\r
+struct sensor_reg_data {\r
+ char reg;\r
+ char data;\r
+};\r
+\r
+\r
+/****************operate according to sensor chip:start************/\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
+ \r
+ sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);\r
+ \r
+ //register setting according to chip datasheet \r
+ if(!enable)\r
+ { \r
+ status = LIS3DH_ACC_DISABLE; //lis3dh \r
+ sensor->ops->ctrl_data |= status; \r
+ }\r
+ else\r
+ {\r
+ status = ~LIS3DH_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
+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
+ struct sensor_reg_data reg_data[] = \r
+ { \r
+ {LIS3DH_CTRL_REG2,0X00}, \r
+ {LIS3DH_CTRL_REG4,0x08}, //High resolution output mode: 1, Normal mode \r
+ {LIS3DH_CTRL_REG6,0x40}, \r
+ {LIS3DH_TEMP_CFG_REG,0x00}, //\r
+ {LIS3DH_FIFO_CTRL_REG,0x00}, // \r
+ {LIS3DH_INT1_CFG,0xFF}, //6 direction position recognition \r
+ {LIS3DH_INT1_THS,0x7F}, //Interrupt 1 threshold \r
+ {LIS3DH_INT1_DURATION,0x7F}, //Duration value 0x00->ox7f\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, LIS3DH_CTRL_REG3, 0x40);//I1_AOI1 =1 if motion \r
+ if(result)\r
+ {\r
+ printk("%s:line=%d,error\n",__func__,__LINE__);\r
+ return result;\r
+ }\r
+\r
+ result = sensor_write_reg(client, LIS3DH_CTRL_REG5, 0x08);\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
+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
+ //int precision = sensor->ops->precision;\r
+ switch (sensor->devid) { \r
+ case LIS3DH_DEVID: \r
+ result = ((int)high_byte << 8) | (int)low_byte;\r
+ if (result < LIS3DH_BOUNDARY)\r
+ result = result* LIS3DH_GRAVITY_STEP;\r
+ else\r
+ result = ~( ((~result & (0x7fff>>(16-LIS3DH_PRECISION)) ) + 1) \r
+ * LIS3DH_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, LIS3DH_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
+\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_ops = {\r
+ .name = "lis3dh",\r
+ .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct\r
+ .id_i2c = ACCEL_ID_LIS3DH, //i2c id number\r
+ .read_reg = (LIS3DH_OUT_X_L | 0x80), //read data\r
+ .read_len = 6, //data length\r
+ .id_reg = LIS3DH_WHO_AM_I, //read device id from this register\r
+ .id_data = LIS3DH_DEVID, //device id\r
+ .precision = LIS3DH_PRECISION, //12 bits\r
+ .ctrl_reg = LIS3DH_CTRL_REG1, //enable or disable \r
+ .int_status_reg = LIS3DH_INT1_SRC, //intterupt status register\r
+ .range = {-LIS3DH_RANGE,LIS3DH_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
+/****************operate according to sensor chip:end************/\r
+\r
+//function name should not be changed\r
+struct sensor_operate *gsensor_get_ops(void)\r
+{\r
+ return &gsensor_ops;\r
+}\r
+\r
+EXPORT_SYMBOL(gsensor_get_ops);\r
+\r
+static int __init gsensor_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
+ printk("%s\n",__func__);\r
+ return result;\r
+}\r
+\r
+static void __exit gsensor_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_init);\r
+module_exit(gsensor_exit);\r
+\r
+\r