1 /* drivers/input/sensors/access/kxtik.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/l3g4200d.h>
33 #include <linux/sensor-dev.h>
36 #define L3G4200D_ENABLE 0x08
38 /****************operate according to sensor chip:start************/
40 static int sensor_active(struct i2c_client *client, int enable, int rate)
42 struct sensor_private_data *sensor =
43 (struct sensor_private_data *) i2c_get_clientdata(client);
47 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
49 //register setting according to chip datasheet
52 status = L3G4200D_ENABLE; //l3g4200d
53 sensor->ops->ctrl_data |= status;
57 status = ~L3G4200D_ENABLE; //l3g4200d
58 sensor->ops->ctrl_data &= status;
61 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
62 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
64 printk("%s:fail to active sensor\n",__func__);
70 static int sensor_init(struct i2c_client *client)
72 struct sensor_private_data *sensor =
73 (struct sensor_private_data *) i2c_get_clientdata(client);
76 unsigned char data = 0;
79 result = sensor->ops->active(client,0,0);
82 printk("%s:line=%d,error\n",__func__,__LINE__);
86 sensor->status_cur = SENSOR_OFF;
95 result = sensor_write_reg(client, sensor->ops->ctrl_reg+i, buf[i]);
98 printk("%s:line=%d,error\n",__func__,__LINE__);
103 result = sensor_read_reg(client, sensor->ops->ctrl_reg);
105 data = result & 0x000F;
107 sensor->ops->ctrl_data = data + ODR100_BW12_5;
108 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
111 printk("%s:line=%d,error\n",__func__,__LINE__);
119 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
121 struct sensor_private_data *sensor =
122 (struct sensor_private_data *) i2c_get_clientdata(client);
124 /* Report GYRO information */
125 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
126 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
127 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
128 input_sync(sensor->input_dev);
129 DBG("gyro x==%d y==%d z==%d\n",axis->x,axis->y,axis->z);
134 static int sensor_report_value(struct i2c_client *client)
136 struct sensor_private_data *sensor =
137 (struct sensor_private_data *) i2c_get_clientdata(client);
138 struct sensor_platform_data *pdata = sensor->pdata;
140 int x = 0, y = 0, z = 0;
141 struct sensor_axis axis;
142 char buffer[6] = {0};
146 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
148 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
152 memset(buffer, 0, 6);
154 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
156 buffer[0] = sensor->ops->read_reg;
157 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
165 //buffer[i] = sensor->ops->read_reg + i;
166 buffer[i] = sensor_read_reg(client, sensor->ops->read_reg + i);
169 x = (short) (((buffer[1]) << 8) | buffer[0]);
170 y = (short) (((buffer[3]) << 8) | buffer[2]);
171 z = (short) (((buffer[5]) << 8) | buffer[4]);
173 DBG("%s: x=%d y=%d z=%d \n",__func__, x,y,z);
174 if(pdata && pdata->orientation)
176 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
177 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
178 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
188 if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min))
190 gyro_report_value(client, &axis);
192 /* »¥³âµØ»º´æÊý¾Ý. */
193 mutex_lock(&(sensor->data_mutex) );
195 mutex_unlock(&(sensor->data_mutex) );
198 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
201 value = sensor_read_reg(client, sensor->ops->int_status_reg);
202 DBG("%s:sensor int status :0x%x\n",__func__,value);
209 struct sensor_operate gyro_l3g4200d_ops = {
211 .type = SENSOR_TYPE_GYROSCOPE,//sensor type and it should be correct
212 .id_i2c = GYRO_ID_L3G4200D, //i2c id number
213 .read_reg = GYRO_DATA_REG, //read data
214 .read_len = 6, //data length
215 .id_reg = GYRO_WHO_AM_I, //read device id from this register
216 .id_data = GYRO_DEVID_L3G4200D, //device id
217 .precision = 8, //8 bits
218 .ctrl_reg = GYRO_CTRL_REG1, //enable or disable
219 .int_status_reg = GYRO_INT_SRC, //intterupt status register,if no exist then -1
220 .range = {-32768,32768}, //range
221 .trig = IRQF_TRIGGER_LOW|IRQF_ONESHOT,
222 .active = sensor_active,
224 .report = sensor_report_value,
227 /****************operate according to sensor chip:end************/
229 //function name should not be changed
230 static struct sensor_operate *gyro_get_ops(void)
232 return &gyro_l3g4200d_ops;
236 static int __init gyro_l3g4200d_init(void)
238 struct sensor_operate *ops = gyro_get_ops();
240 int type = ops->type;
241 result = sensor_register_slave(type, NULL, NULL, gyro_get_ops);
245 static void __exit gyro_l3g4200d_exit(void)
247 struct sensor_operate *ops = gyro_get_ops();
248 int type = ops->type;
249 sensor_unregister_slave(type, NULL, NULL, gyro_get_ops);
253 module_init(gyro_l3g4200d_init);
254 module_exit(gyro_l3g4200d_exit);