1 /* drivers/input/sensors/access/kxtik.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
5 *
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.
9 *
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.
14 *
15 */
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 <linux/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>
31 #endif
32 #include <linux/l3g4200d.h>
33 #include <linux/sensor-dev.h>
34
35
36 #define L3G4200D_ENABLE 0x08
37
38 /****************operate according to sensor chip:start************/
39
sensor_active(struct i2c_client * client,int enable,int rate)40 static int sensor_active(struct i2c_client *client, int enable, int rate)
41 {
42 struct sensor_private_data *sensor =
43 (struct sensor_private_data *) i2c_get_clientdata(client);
44 int result = 0;
45 int status = 0;
46
47 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
48
49 //register setting according to chip datasheet
50 if(enable)
51 {
52 status = L3G4200D_ENABLE; //l3g20d
53 sensor->ops->ctrl_data |= status;
54 }
55 else
56 {
57 status = ~L3G4200D_ENABLE; //l3g20d
58 sensor->ops->ctrl_data &= status;
59 }
60
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);
63 if(result)
64 printk("%s:fail to active sensor\n",__func__);
65
66 return result;
67
68 }
69
sensor_init(struct i2c_client * client)70 static int sensor_init(struct i2c_client *client)
71 {
72 struct sensor_private_data *sensor =
73 (struct sensor_private_data *) i2c_get_clientdata(client);
74 int result = 0;
75 unsigned char buf[5];
76 unsigned char data = 0;
77 int i = 0;
78
79 result = sensor->ops->active(client,0,0);
80 if(result)
81 {
82 printk("%s:line=%d,error\n",__func__,__LINE__);
83 return result;
84 }
85
86 sensor->status_cur = SENSOR_OFF;
87
88 buf[0] = 0x07; //27
89 buf[1] = 0x00;
90 buf[2] = 0x00;
91 buf[3] = 0x20; //0x00
92 buf[4] = 0x00;
93 for(i=0; i<5; i++)
94 {
95 result = sensor_write_reg(client, sensor->ops->ctrl_reg+i, buf[i]);
96 if(result)
97 {
98 printk("%s:line=%d,error\n",__func__,__LINE__);
99 return result;
100 }
101 }
102
103 result = sensor_read_reg(client, sensor->ops->ctrl_reg);
104 if (result >= 0)
105 data = result & 0x000F;
106
107 sensor->ops->ctrl_data = data + ODR100_BW12_5;
108 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
109 if(result)
110 {
111 printk("%s:line=%d,error\n",__func__,__LINE__);
112 return result;
113 }
114
115 return result;
116 }
117
118
gyro_report_value(struct i2c_client * client,struct sensor_axis * axis)119 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
120 {
121 struct sensor_private_data *sensor =
122 (struct sensor_private_data *) i2c_get_clientdata(client);
123
124 if (sensor->status_cur == SENSOR_ON) {
125 /* Report GYRO information */
126 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
127 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
128 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
129 input_sync(sensor->input_dev);
130 }
131
132 return 0;
133 }
134
sensor_report_value(struct i2c_client * client)135 static int sensor_report_value(struct i2c_client *client)
136 {
137 struct sensor_private_data *sensor =
138 (struct sensor_private_data *) i2c_get_clientdata(client);
139 struct sensor_platform_data *pdata = sensor->pdata;
140 int ret = 0;
141 int x = 0, y = 0, z = 0;
142 struct sensor_axis axis;
143 char buffer[6] = {0};
144 int i = 0;
145 int value = 0;
146
147 if(sensor->ops->read_len < 6) //sensor->ops->read_len = 6
148 {
149 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
150 return -1;
151 }
152
153 memset(buffer, 0, 6);
154 #if 0
155 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
156 do {
157 buffer[0] = sensor->ops->read_reg;
158 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
159 if (ret < 0)
160 return ret;
161 } while (0);
162 #else
163
164 for(i=0; i<6; i++)
165 {
166 //buffer[i] = sensor->ops->read_reg + i;
167 buffer[i] = sensor_read_reg(client, sensor->ops->read_reg + i);
168 }
169 #endif
170 x = (short) (((buffer[1]) << 8) | buffer[0]);
171 y = (short) (((buffer[3]) << 8) | buffer[2]);
172 z = (short) (((buffer[5]) << 8) | buffer[4]);
173
174 DBG("%s: x=%d y=%d z=%d \n",__func__, x,y,z);
175 if (pdata)
176 {
177 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
178 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
179 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
180 }
181 else
182 {
183 axis.x = x;
184 axis.y = y;
185 axis.z = z;
186 }
187
188 gyro_report_value(client, &axis);
189
190 mutex_lock(&sensor->data_mutex);
191 sensor->axis = axis;
192 mutex_unlock(&sensor->data_mutex);
193
194 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
195 {
196
197 value = sensor_read_reg(client, sensor->ops->int_status_reg);
198 DBG("%s:sensor int status :0x%x\n",__func__,value);
199 }
200
201 return ret;
202 }
203
204
205 static struct sensor_operate gyro_l3g20d_ops = {
206 .name = "l3g20d",
207 .type = SENSOR_TYPE_GYROSCOPE,
208 .id_i2c = GYRO_ID_L3G20D,
209 .read_reg = GYRO_DATA_REG,
210 .read_len = 6,
211 .id_reg = GYRO_WHO_AM_I,
212 .id_data = GYRO_DEVID_L3G20D,
213 .precision = 16,
214 .ctrl_reg = GYRO_CTRL_REG1,
215 .int_status_reg = GYRO_INT_SRC,
216 .range = {-32768, 32768},
217 .trig = IRQF_TRIGGER_LOW | IRQF_ONESHOT,
218 .active = sensor_active,
219 .init = sensor_init,
220 .report = sensor_report_value,
221 };
222
223 /****************operate according to sensor chip:end************/
gyro_l3g20d_probe(struct i2c_client * client,const struct i2c_device_id * devid)224 static int gyro_l3g20d_probe(struct i2c_client *client,
225 const struct i2c_device_id *devid)
226 {
227 return sensor_register_device(client, NULL, devid, &gyro_l3g20d_ops);
228 }
229
gyro_l3g20d_remove(struct i2c_client * client)230 static int gyro_l3g20d_remove(struct i2c_client *client)
231 {
232 return sensor_unregister_device(client, NULL, &gyro_l3g20d_ops);
233 }
234
235 static const struct i2c_device_id gyro_l3g20d_id[] = {
236 {"l3g20d_gyro", GYRO_ID_L3G20D},
237 {}
238 };
239
240 static struct i2c_driver gyro_l3g20d_driver = {
241 .probe = gyro_l3g20d_probe,
242 .remove = gyro_l3g20d_remove,
243 .shutdown = sensor_shutdown,
244 .id_table = gyro_l3g20d_id,
245 .driver = {
246 .name = "gyro_l3g20d",
247 #ifdef CONFIG_PM
248 .pm = &sensor_pm_ops,
249 #endif
250 },
251 };
252
253 module_i2c_driver(gyro_l3g20d_driver);
254
255 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
256 MODULE_DESCRIPTION("l3g20d 3-Axis Gyroscope driver");
257 MODULE_LICENSE("GPL");
258