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 <mach/gpio.h>
29 #include <mach/board.h>
30 #ifdef CONFIG_HAS_EARLYSUSPEND
31 #include <linux/earlysuspend.h>
33 #include <linux/mma8452.h>
34 #include <linux/sensor-dev.h>
37 #define SENSOR_DEBUG_TYPE SENSOR_TYPE_ACCEL
38 #define DBG(x...) if(sensor->pdata->type == SENSOR_DEBUG_TYPE) printk(x)
44 #define MMA8451_DEVID 0x1a
45 #define MMA8452_DEVID 0x2a
46 #define MMA8453_DEVID 0x3a
48 #define MMA8452_ENABLE 1
51 /****************operate according to sensor chip:start************/
53 static int sensor_active(struct i2c_client *client, int enable, int rate)
55 struct sensor_private_data *sensor =
56 (struct sensor_private_data *) i2c_get_clientdata(client);
60 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
62 //register setting according to chip datasheet
65 status = MMA8452_ENABLE; //mma8452
66 sensor->ops->ctrl_data |= status;
70 status = ~MMA8452_ENABLE; //mma8452
71 sensor->ops->ctrl_data &= status;
74 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
75 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
77 printk("%s:fail to active sensor\n",__func__);
83 static int sensor_init(struct i2c_client *client)
87 struct sensor_private_data *sensor =
88 (struct sensor_private_data *) i2c_get_clientdata(client);
90 ret = sensor->ops->active(client,0,0);
93 printk("%s:line=%d,error\n",__func__,__LINE__);
97 sensor->status_cur = SENSOR_OFF;
99 /* disable FIFO FMODE = 0*/
100 ret = sensor_write_reg(client,MMA8452_REG_F_SETUP,0);
101 DBG("%s: MMA8452_REG_F_SETUP:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_F_SETUP));
103 /* set full scale range to 2g */
104 ret = sensor_write_reg(client,MMA8452_REG_XYZ_DATA_CFG,0);
105 DBG("%s: MMA8452_REG_XYZ_DATA_CFG:%x\n",__func__, sensor_read_reg(client,MMA8452_REG_XYZ_DATA_CFG));
107 /* set bus 8bit/14bit(FREAD = 1,FMODE = 0) ,data rate*/
108 tmp = (MMA8452_RATE_12P5<< MMA8452_RATE_SHIFT) | FREAD_MASK;
109 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG1,tmp);
111 sensor->ops->ctrl_data = tmp;
113 DBG("mma8452 MMA8452_REG_CTRL_REG1:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG1));
115 DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
117 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG3,5);
118 DBG("mma8452 MMA8452_REG_CTRL_REG3:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG3));
120 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG4,1);
121 DBG("mma8452 MMA8452_REG_CTRL_REG4:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG4));
123 ret = sensor_write_reg(client,MMA8452_REG_CTRL_REG5,1);
124 DBG("mma8452 MMA8452_REG_CTRL_REG5:%x\n",sensor_read_reg(client,MMA8452_REG_CTRL_REG5));
126 DBG("mma8452 MMA8452_REG_SYSMOD:%x\n",sensor_read_reg(client,MMA8452_REG_SYSMOD));
128 printk("------sensor_chip_init\n");
133 static int sensor_convert_data(struct i2c_client *client, char high_byte, char low_byte)
136 struct sensor_private_data *sensor =
137 (struct sensor_private_data *) i2c_get_clientdata(client);
138 //int precision = sensor->ops->precision;
139 switch (sensor->devid) {
141 swap(high_byte,low_byte);
142 result = ((int)high_byte << (MMA8451_PRECISION-8))
143 | ((int)low_byte >> (16-MMA8451_PRECISION));
144 if (result < MMA8451_BOUNDARY)
145 result = result* MMA8451_GRAVITY_STEP;
147 result = ~( ((~result & (0x7fff>>(16-MMA8451_PRECISION)) ) + 1)
148 * MMA8451_GRAVITY_STEP) + 1;
152 swap(high_byte,low_byte);
153 result = ((int)high_byte << (MMA8452_PRECISION-8))
154 | ((int)low_byte >> (16-MMA8452_PRECISION));
155 if (result < MMA8452_BOUNDARY)
156 result = result* MMA8452_GRAVITY_STEP;
158 result = ~( ((~result & (0x7fff>>(16-MMA8452_PRECISION)) ) + 1)
159 * MMA8452_GRAVITY_STEP) + 1;
163 swap(high_byte,low_byte);
164 result = ((int)high_byte << (MMA8453_PRECISION-8))
165 | ((int)low_byte >> (16-MMA8453_PRECISION));
166 if (result < MMA8453_BOUNDARY)
167 result = result* MMA8453_GRAVITY_STEP;
169 result = ~( ((~result & (0x7fff>>(16-MMA8453_PRECISION)) ) + 1)
170 * MMA8453_GRAVITY_STEP) + 1;
174 printk(KERN_ERR "%s: devid wasn't set correctly\n",__func__);
181 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
183 struct sensor_private_data *sensor =
184 (struct sensor_private_data *) i2c_get_clientdata(client);
186 /* Report acceleration sensor information */
187 input_report_abs(sensor->input_dev, ABS_X, axis->x);
188 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
189 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
190 input_sync(sensor->input_dev);
191 DBG("Gsensor x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
196 #define GSENSOR_MIN 10
197 static int sensor_report_value(struct i2c_client *client)
199 struct sensor_private_data *sensor =
200 (struct sensor_private_data *) i2c_get_clientdata(client);
201 struct sensor_platform_data *pdata = sensor->pdata;
204 struct sensor_axis axis;
205 char buffer[6] = {0};
208 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
210 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
214 memset(buffer, 0, 6);
216 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
218 *buffer = sensor->ops->read_reg;
219 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
225 //this gsensor need 6 bytes buffer
226 x = sensor_convert_data(sensor->client, buffer[1], buffer[0]); //buffer[1]:high bit
227 y = sensor_convert_data(sensor->client, buffer[3], buffer[2]);
228 z = sensor_convert_data(sensor->client, buffer[5], buffer[4]);
230 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
231 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
232 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
234 DBG( "%s: axis = %d %d %d \n", __func__, axis.x, axis.y, axis.z);
236 //Report event only while value is changed to save some power
237 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))
239 gsensor_report_value(client, &axis);
241 /* »¥³âµØ»º´æÊý¾Ý. */
242 mutex_lock(&(sensor->data_mutex) );
244 mutex_unlock(&(sensor->data_mutex) );
247 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
250 value = sensor_read_reg(client, sensor->ops->int_status_reg);
251 DBG("%s:sensor int status :0x%x\n",__func__,value);
258 struct sensor_operate gsensor_mma8452_ops = {
260 .type = SENSOR_TYPE_ACCEL, //sensor type and it should be correct
261 .id_i2c = ACCEL_ID_MMA845X, //i2c id number
262 .read_reg = MMA8452_REG_X_OUT_MSB, //read data
263 .read_len = 6, //data length
264 .id_reg = MMA8452_REG_WHO_AM_I, //read device id from this register
265 .id_data = MMA8452_DEVID, //device id
266 .precision = MMA8452_PRECISION, //12 bit
267 .ctrl_reg = MMA8452_REG_CTRL_REG1, //enable or disable
268 .int_status_reg = MMA8452_REG_INTSRC, //intterupt status register
269 .range = {-MMA845X_RANGE,MMA845X_RANGE}, //range
270 .trig = IRQF_TRIGGER_LOW|IRQF_ONESHOT,
271 .active = sensor_active,
273 .report = sensor_report_value,
276 /****************operate according to sensor chip:end************/
278 //function name should not be changed
279 static struct sensor_operate *gsensor_get_ops(void)
281 return &gsensor_mma8452_ops;
285 static int __init gsensor_mma8452_init(void)
287 struct sensor_operate *ops = gsensor_get_ops();
289 int type = ops->type;
290 result = sensor_register_slave(type, NULL, NULL, gsensor_get_ops);
291 DBG("%s\n",__func__);
295 static void __exit gsensor_mma8452_exit(void)
297 struct sensor_operate *ops = gsensor_get_ops();
298 int type = ops->type;
299 sensor_unregister_slave(type, NULL, NULL, gsensor_get_ops);
303 module_init(gsensor_mma8452_init);
304 module_exit(gsensor_mma8452_exit);