1 /* drivers/input/sensors/access/mma8452.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
32 #include <linux/sensor-dev.h>
34 /* Default register settings */
35 #define RBUFF_SIZE 12 /* Rx buffer size */
37 #define MMA8452_REG_STATUS 0x0 //RO
38 #define MMA8452_REG_X_OUT_MSB 0x1 //RO
39 #define MMA8452_REG_X_OUT_LSB 0x2 //RO
40 #define MMA8452_REG_Y_OUT_MSB 0x3 //RO
41 #define MMA8452_REG_Y_OUT_LSB 0x4 //RO
42 #define MMA8452_REG_Z_OUT_MSB 0x5 //RO
43 #define MMA8452_REG_Z_OUT_LSB 0x6 //RO
44 #define MMA8452_REG_F_SETUP 0x9 //RW
46 #define MMA8452_REG_SYSMOD 0xB //RO
47 #define MMA8452_REG_INTSRC 0xC //RO
48 #define MMA8452_REG_WHO_AM_I 0xD //RO
49 #define MMA8452_REG_XYZ_DATA_CFG 0xE //RW
50 #define MMA8452_REG_HP_FILTER_CUTOFF 0xF //RW
51 #define MMA8452_REG_PL_STATUS 0x10 //RO
52 #define MMA8452_REG_PL_CFG 0x11 //RW
53 #define MMA8452_REG_PL_COUNT 0x12 //RW
54 #define MMA8452_REG_PL_BF_ZCOMP 0x13 //RW
55 #define MMA8452_REG_P_L_THS_REG 0x14 //RW
56 #define MMA8452_REG_FF_MT_CFG 0x15 //RW
57 #define MMA8452_REG_FF_MT_SRC 0x16 //RO
58 #define MMA8452_REG_FF_MT_THS 0x17 //RW
59 #define MMA8452_REG_FF_MT_COUNT 0x18 //RW
60 #define MMA8452_REG_TRANSIENT_CFG 0x1D //RW
61 #define MMA8452_REG_TRANSIENT_SRC 0x1E //RO
62 #define MMA8452_REG_TRANSIENT_THS 0x1F //RW
63 #define MMA8452_REG_TRANSIENT_COUNT 0x20 //RW
64 #define MMA8452_REG_PULSE_CFG 0x21 //RW
65 #define MMA8452_REG_PULSE_SRC 0x22 //RO
66 #define MMA8452_REG_PULSE_THSX 0x23 //RW
67 #define MMA8452_REG_PULSE_THSY 0x24 //RW
68 #define MMA8452_REG_PULSE_THSZ 0x25 //RW
69 #define MMA8452_REG_PULSE_TMLT 0x26 //RW
70 #define MMA8452_REG_PULSE_LTCY 0x27 //RW
71 #define MMA8452_REG_PULSE_WIND 0x28 //RW
72 #define MMA8452_REG_ASLP_COUNT 0x29 //RW
73 #define MMA8452_REG_CTRL_REG1 0x2A //RW
74 #define MMA8452_REG_CTRL_REG2 0x2B //RW
75 #define MMA8452_REG_CTRL_REG3 0x2C //RW
76 #define MMA8452_REG_CTRL_REG4 0x2D //RW
77 #define MMA8452_REG_CTRL_REG5 0x2E //RW
78 #define MMA8452_REG_OFF_X 0x2F //RW
79 #define MMA8452_REG_OFF_Y 0x30 //RW
80 #define MMA8452_REG_OFF_Z 0x31 //RW
83 #define MMA8452_RATE_800 0
84 #define MMA8452_RATE_400 1
85 #define MMA8452_RATE_200 2
86 #define MMA8452_RATE_100 3
87 #define MMA8452_RATE_50 4
88 #define MMA8452_RATE_12P5 5
89 #define MMA8452_RATE_6P25 6
90 #define MMA8452_RATE_1P56 7
91 #define MMA8452_RATE_SHIFT 3
94 #define MMA8452_ASLP_RATE_50 0
95 #define MMA8452_ASLP_RATE_12P5 1
96 #define MMA8452_ASLP_RATE_6P25 2
97 #define MMA8452_ASLP_RATE_1P56 3
98 #define MMA8452_ASLP_RATE_SHIFT 6
100 /*Auto-adapt mma845x series*/
101 /*Modified by Yick @ROCKCHIP
102 xieyi@rockchips.com*/
104 Range: unit(ug 1g=1 000 000 ug)
106 G would be defined on android HAL
107 Precision: bit wide of valid data
108 Boundary: Max positive count
109 Gravity_step: gravity value indicated by per count
111 #define FREAD_MASK 0 /* enabled(1<<1) only if reading MSB 8bits*/
112 #define MMA845X_RANGE 2000000
114 #define MMA8451_PRECISION 14
115 #define MMA8451_BOUNDARY (0x1 << (MMA8451_PRECISION - 1))
116 #define MMA8451_GRAVITY_STEP MMA845X_RANGE / MMA8451_BOUNDARY
119 #define MMA8452_PRECISION 12
120 #define MMA8452_BOUNDARY (0x1 << (MMA8452_PRECISION - 1))
121 #define MMA8452_GRAVITY_STEP MMA845X_RANGE / MMA8452_BOUNDARY
124 #define MMA8453_PRECISION 10
125 #define MMA8453_BOUNDARY (0x1 << (MMA8453_PRECISION - 1))
126 #define MMA8453_GRAVITY_STEP MMA845X_RANGE / MMA8453_BOUNDARY
129 #define MMA8653_PRECISION 10
130 #define MMA8653_BOUNDARY (0x1 << (MMA8653_PRECISION - 1))
131 #define MMA8653_GRAVITY_STEP MMA845X_RANGE / MMA8653_BOUNDARY
134 #define MMA8451_DEVID 0x1a
135 #define MMA8452_DEVID 0x2a
136 #define MMA8453_DEVID 0x3a
137 #define MMA8653_DEVID 0x5a
140 #define MMA8452_ENABLE 1
143 /****************operate according to sensor chip:start************/
145 static int sensor_active(struct i2c_client *client, int enable, int rate)
147 struct sensor_private_data *sensor =
148 (struct sensor_private_data *) i2c_get_clientdata(client);
152 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
154 //register setting according to chip datasheet
157 status = MMA8452_ENABLE; //mma8452
158 sensor->ops->ctrl_data |= status;
162 status = ~MMA8452_ENABLE; //mma8452
163 sensor->ops->ctrl_data &= status;
166 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
167 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
169 printk("%s:fail to active sensor\n",__func__);
175 static int sensor_init(struct i2c_client *client)
180 unsigned char id_reg = MMA8452_REG_WHO_AM_I;
181 unsigned char id_data = 0;
182 struct sensor_private_data *sensor =
183 (struct sensor_private_data *) i2c_get_clientdata(client);
185 ret = sensor->ops->active(client,0,0);
188 printk("%s:line=%d,error\n",__func__,__LINE__);
192 sensor->status_cur = SENSOR_OFF;
196 ret = sensor_rx_data(client, &id_reg, 1);
204 printk("%s:fail to read id,ret=%d\n",__func__, ret);
208 sensor->devid = id_data;
210 /* disable FIFO FMODE = 0*/
211 ret = sensor_write_reg(client,MMA8452_REG_F_SETUP,0);
212 DBG("%s: MMA8452_REG_F_SETUP:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_F_SETUP));
214 /* set full scale range to 2g */
215 ret = sensor_write_reg(client,MMA8452_REG_XYZ_DATA_CFG,0);
216 DBG("%s: MMA8452_REG_XYZ_DATA_CFG:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_XYZ_DATA_CFG));
218 /* set bus 8bit/14bit(FREAD = 1,FMODE = 0) ,data rate*/
219 tmp = (MMA8452_RATE_12P5<< MMA8452_RATE_SHIFT) | FREAD_MASK;
220 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG1,tmp);
222 sensor->ops->ctrl_data = tmp;
224 DBG("mma8452 MMA8452_REG_CTRL_REG1:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG1));
226 DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
228 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG3,5);
229 DBG("mma8452 MMA8452_REG_CTRL_REG3:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG3));
231 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG4,1);
232 DBG("mma8452 MMA8452_REG_CTRL_REG4:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG4));
234 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG5,1);
235 DBG("mma8452 MMA8452_REG_CTRL_REG5:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG5));
237 DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
242 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
245 struct sensor_private_data *sensor =
246 (struct sensor_private_data *) i2c_get_clientdata(client);
247 //int precision = sensor->ops->precision;
248 switch (sensor->devid) {
250 swap(high_byte,low_byte);
251 result = ((int)high_byte << (MMA8451_PRECISION-8))
252 | ((int)low_byte >> (16-MMA8451_PRECISION));
253 if (result < MMA8451_BOUNDARY)
254 result = result* MMA8451_GRAVITY_STEP;
256 result = ~( ((~result & (0x7fff>>(16-MMA8451_PRECISION)) ) + 1)
257 * MMA8451_GRAVITY_STEP) + 1;
261 swap(high_byte,low_byte);
262 result = ((int)high_byte << (MMA8452_PRECISION-8))
263 | ((int)low_byte >> (16-MMA8452_PRECISION));
264 if (result < MMA8452_BOUNDARY)
265 result = result* MMA8452_GRAVITY_STEP;
267 result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1)
268 * MMA8452_GRAVITY_STEP) + 1;
272 swap(high_byte,low_byte);
273 result = ((int)high_byte << (MMA8453_PRECISION-8))
274 | ((int)low_byte >> (16-MMA8453_PRECISION));
275 if (result < MMA8453_BOUNDARY)
276 result = result* MMA8453_GRAVITY_STEP;
278 result = ~( ((~result & (0x7fff>>(16-MMA8453_PRECISION)) ) + 1)
279 * MMA8453_GRAVITY_STEP) + 1;
283 swap(high_byte,low_byte);
284 result = ((int)high_byte << (MMA8653_PRECISION-8))
285 | ((int)low_byte >> (16-MMA8653_PRECISION));
286 if (result < MMA8653_BOUNDARY)
287 result = result* MMA8653_GRAVITY_STEP;
289 result = ~( ((~result & (0x7fff>>(16-MMA8653_PRECISION)) ) + 1)
290 * MMA8653_GRAVITY_STEP) + 1;
294 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
301 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
303 struct sensor_private_data *sensor =
304 (struct sensor_private_data *) i2c_get_clientdata(client);
306 /* Report acceleration sensor information */
307 input_report_abs(sensor->input_dev, ABS_X, axis->x);
308 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
309 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
310 input_sync(sensor->input_dev);
311 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
316 #define GSENSOR_MIN 10
317 static int sensor_report_value(struct i2c_client *client)
319 struct sensor_private_data *sensor =
320 (struct sensor_private_data *) i2c_get_clientdata(client);
321 struct sensor_platform_data *pdata = sensor->pdata;
324 struct sensor_axis axis;
325 char buffer[6] = {0};
328 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
330 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
334 memset(buffer, 0, 6);
336 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
338 *buffer = sensor->ops->read_reg;
339 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
345 //this gsensor need 6 bytes buffer
346 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
347 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
348 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
350 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
351 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
352 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
354 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
356 //Report event only while value is changed to save some power
357 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))
359 gsensor_report_value(client, &axis);
361 /* »¥³âµØ»º´æÊý¾Ý. */
362 mutex_lock(&(sensor->data_mutex) );
364 mutex_unlock(&(sensor->data_mutex) );
367 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
370 value = sensor_read_reg(client, sensor->ops->int_status_reg);
371 DBG("%s:sensor int status :0x%x\n",__func__,value);
378 struct sensor_operate gsensor_mma8452_ops = {
380 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
381 .id_i2c = ACCEL_ID_MMA845X, //i2c id number
382 .read_reg = MMA8452_REG_X_OUT_MSB, //read data
383 .read_len = 6, //data length
384 .id_reg = SENSOR_UNKNOW_DATA, //read device id from this register
385 .id_data = SENSOR_UNKNOW_DATA, //device id
386 .precision = MMA8452_PRECISION, //12 bit
387 .ctrl_reg = MMA8452_REG_CTRL_REG1, //enable or disable
388 .int_status_reg = MMA8452_REG_INTSRC, //intterupt status register
389 .range = {-MMA845X_RANGE,MMA845X_RANGE}, //range
390 .trig = IRQF_TRIGGER_LOW|IRQF_ONESHOT,
391 .active = sensor_active,
393 .report = sensor_report_value,
396 /****************operate according to sensor chip:end************/
398 //function name should not be changed
399 static struct sensor_operate *gsensor_get_ops(void)
401 return &gsensor_mma8452_ops;
405 static int __init gsensor_mma8452_init(void)
407 struct sensor_operate *ops = gsensor_get_ops();
409 int type = ops->type;
410 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
414 static void __exit gsensor_mma8452_exit(void)
416 struct sensor_operate *ops = gsensor_get_ops();
417 int type = ops->type;
418 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
422 module_init(gsensor_mma8452_init);
423 module_exit(gsensor_mma8452_exit);