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/sensor-dev.h>
33
34 #define LIS3DH_INT_COUNT (0x0E)
35 #define LIS3DH_WHO_AM_I (0x0F)
36
37 /* full scale setting - register & mask */
38 #define LIS3DH_TEMP_CFG_REG (0x1F)
39 #define LIS3DH_CTRL_REG1 (0x20)
40 #define LIS3DH_CTRL_REG2 (0x21)
41 #define LIS3DH_CTRL_REG3 (0x22)
42 #define LIS3DH_CTRL_REG4 (0x23)
43 #define LIS3DH_CTRL_REG5 (0x24)
44 #define LIS3DH_CTRL_REG6 (0x25)
45 #define LIS3DH_REFERENCE (0x26)
46 #define LIS3DH_STATUS_REG (0x27)
47 #define LIS3DH_OUT_X_L (0x28)
48 #define LIS3DH_OUT_X_H (0x29)
49 #define LIS3DH_OUT_Y_L (0x2a)
50 #define LIS3DH_OUT_Y_H (0x2b)
51 #define LIS3DH_OUT_Z_L (0x2c)
52 #define LIS3DH_OUT_Z_H (0x2d)
53 #define LIS3DH_FIFO_CTRL_REG (0x2E)
54
55 #define LIS3DH_INT1_CFG (0x30)
56 #define LIS3DH_INT1_SRC (0x31)
57 #define LIS3DH_INT1_THS (0x32)
58 #define LIS3DH_INT1_DURATION (0x33)
59 #define LIS3DH_TT_CFG (0x38)
60 #define LIS3DH_TT_THS (0x3a)
61 #define LIS3DH_TT_LIM (0x3b)
62 #define LIS3DH_TT_TLAT (0x3c)
63 #define LIS3DH_TT_TW (0x3d)
64
65
66 #define LIS3DH_DEVID (0x33)
67 #define LIS3DH_ACC_DISABLE (0x08)
68
69 #define LIS3DH_RANGE 2000000
70
71 /* LIS3DH */
72 #define LIS3DH_PRECISION 16
73
74 #define LIS3DH_ACC_ODR1 0x10 /* 1Hz output data rate */
75 #define LIS3DH_ACC_ODR10 0x20 /* 10Hz output data rate */
76 #define LIS3DH_ACC_ODR25 0x30 /* 25Hz output data rate */
77 #define LIS3DH_ACC_ODR50 0x40 /* 50Hz output data rate */
78 #define LIS3DH_ACC_ODR100 0x50 /* 100Hz output data rate */
79 #define LIS3DH_ACC_ODR200 0x60 /* 200Hz output data rate */
80 #define LIS3DH_ACC_ODR400 0x70 /* 400Hz output data rate */
81 #define LIS3DH_ACC_ODR1250 0x90 /* 1250Hz output data rate */
82
83 struct sensor_reg_data {
84 char reg;
85 char data;
86 };
87
88 /****************operate according to sensor chip:start************/
89 /* odr table, hz */
90 struct odr_table {
91 unsigned int cutoff_ms;
92 unsigned int mask;
93 };
94
95 static struct odr_table lis3dh_acc_odr_table[] = {
96 {1, LIS3DH_ACC_ODR1250},
97 {3, LIS3DH_ACC_ODR400},
98 {5, LIS3DH_ACC_ODR200},
99 {10, LIS3DH_ACC_ODR100},
100 {20, LIS3DH_ACC_ODR50},
101 {40, LIS3DH_ACC_ODR25},
102 {100, LIS3DH_ACC_ODR10},
103 {1000, LIS3DH_ACC_ODR1},
104 };
105
lis3dh_select_odr(int want)106 static int lis3dh_select_odr(int want)
107 {
108 int i;
109 int max_index = ARRAY_SIZE(lis3dh_acc_odr_table);
110
111 for (i = max_index - 1; i >= 0; i--) {
112 if ((lis3dh_acc_odr_table[i].cutoff_ms <= want) ||
113 (i == 0))
114 break;
115 }
116
117 return lis3dh_acc_odr_table[i].mask;
118 }
119
sensor_active(struct i2c_client * client,int enable,int rate)120 static int sensor_active(struct i2c_client *client, int enable, int rate/*ms*/)
121 {
122 struct sensor_private_data *sensor =
123 (struct sensor_private_data *) i2c_get_clientdata(client);
124 int result = 0;
125 int status = 0;
126 int odr_rate = 0;
127
128 if (rate == 0) {
129 dev_err(&client->dev, "%s: rate == 0!!!\n", __func__);
130 return -1;
131 }
132
133 sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
134 result = lis3dh_select_odr(odr_rate);
135 sensor->ops->ctrl_data |= result;
136
137 if (!enable) {
138 status = LIS3DH_ACC_DISABLE;
139 sensor->ops->ctrl_data |= status;
140 } else {
141 sensor->ops->init(client);
142 status = ~LIS3DH_ACC_DISABLE;
143 sensor->ops->ctrl_data &= status;
144 }
145
146 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
147 if (result)
148 dev_err(&client->dev, "%s:fail to active sensor\n", __func__);
149
150 return result;
151 }
152
sensor_init(struct i2c_client * client)153 static int sensor_init(struct i2c_client *client)
154 {
155 int result = 0;
156 int i;
157
158 struct sensor_reg_data reg_data[] = {
159 {LIS3DH_CTRL_REG1, 0x07},
160 {LIS3DH_TEMP_CFG_REG, 0x00},
161 {LIS3DH_FIFO_CTRL_REG, 0x00},
162 {LIS3DH_TT_THS, 0x00},
163 {LIS3DH_TT_LIM, 0x00},
164 {LIS3DH_TT_TLAT, 0x00},
165 {LIS3DH_TT_TW, 0x00},
166 {LIS3DH_TT_CFG, 0x00},
167 {LIS3DH_INT1_THS, 0x7f},
168 {LIS3DH_INT1_DURATION, 0x7f},
169 {LIS3DH_INT1_CFG, 0xff},
170 {LIS3DH_CTRL_REG2, 0x00},
171 {LIS3DH_CTRL_REG3, 0x40},
172 {LIS3DH_CTRL_REG4, 0x08},
173 {LIS3DH_CTRL_REG5, 0x08},
174 {LIS3DH_CTRL_REG6, 0x40},
175 };
176
177 for (i = 0; i < (sizeof(reg_data) / sizeof(struct sensor_reg_data)); i++) {
178 result = sensor_write_reg(client, reg_data[i].reg, reg_data[i].data);
179 if (result) {
180 dev_err(&client->dev, "%s:line=%d,i=%d,error\n", __func__, __LINE__, i);
181 return result;
182 }
183 }
184
185 return result;
186 }
187
gsensor_report_value(struct i2c_client * client,struct sensor_axis * axis)188 static int gsensor_report_value(struct i2c_client *client, struct sensor_axis *axis)
189 {
190 struct sensor_private_data *sensor =
191 (struct sensor_private_data *) i2c_get_clientdata(client);
192
193 if (sensor->status_cur == SENSOR_ON) {
194 /* Report acceleration sensor information */
195 input_report_abs(sensor->input_dev, ABS_X, axis->x);
196 input_report_abs(sensor->input_dev, ABS_Y, axis->y);
197 input_report_abs(sensor->input_dev, ABS_Z, axis->z);
198 input_sync(sensor->input_dev);
199 }
200
201 return 0;
202 }
203
sensor_report_value(struct i2c_client * client)204 static int sensor_report_value(struct i2c_client *client)
205 {
206 struct sensor_private_data *sensor =
207 (struct sensor_private_data *) i2c_get_clientdata(client);
208 struct sensor_platform_data *pdata = sensor->pdata;
209 int ret = 0;
210 short x, y, z;
211 struct sensor_axis axis;
212 char buffer[6] = {0};
213
214 if (sensor->ops->read_len < 6) {
215 dev_err(&client->dev, "%s:lenth is error,len=%d\n", __func__, sensor->ops->read_len);
216 return -1;
217 }
218
219 memset(buffer, 0, 6);
220
221 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
222 do {
223 *buffer = sensor->ops->read_reg | 0x80;
224 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
225 if (ret < 0) {
226 dev_err(&client->dev, "lis3dh read data failed, ret = %d\n", ret);
227 return ret;
228 }
229 } while (0);
230
231 x = ((buffer[1] << 8) & 0xff00) + (buffer[0] & 0xFF);
232 y = ((buffer[3] << 8) & 0xff00) + (buffer[2] & 0xFF);
233 z = ((buffer[5] << 8) & 0xff00) + (buffer[4] & 0xFF);
234
235 axis.x = (pdata->orientation[0]) * x + (pdata->orientation[1]) * y + (pdata->orientation[2]) * z;
236 axis.y = (pdata->orientation[3]) * x + (pdata->orientation[4]) * y + (pdata->orientation[5]) * z;
237 axis.z = (pdata->orientation[6]) * x + (pdata->orientation[7]) * y + (pdata->orientation[8]) * z;
238
239 gsensor_report_value(client, &axis);
240
241 mutex_lock(&(sensor->data_mutex));
242 sensor->axis = axis;
243 mutex_unlock(&(sensor->data_mutex));
244
245 if ((sensor->pdata->irq_enable) && (sensor->ops->int_status_reg >= 0))
246 sensor_read_reg(client, sensor->ops->int_status_reg);
247
248 return ret;
249 }
250
251 static struct sensor_operate gsensor_lis3dh_ops = {
252 .name = "lis3dh",
253 .type = SENSOR_TYPE_ACCEL,
254 .id_i2c = ACCEL_ID_LIS3DH,
255 .read_reg = LIS3DH_OUT_X_L,
256 .read_len = 6,
257 .id_reg = LIS3DH_WHO_AM_I,
258 .id_data = LIS3DH_DEVID,
259 .precision = LIS3DH_PRECISION,
260 .ctrl_reg = LIS3DH_CTRL_REG1,
261 .int_status_reg = LIS3DH_INT1_SRC,
262 .range = {-32768, +32768},
263 .trig = (IRQF_TRIGGER_LOW | IRQF_ONESHOT),
264 .active = sensor_active,
265 .init = sensor_init,
266 .report = sensor_report_value,
267 };
268
269 /****************operate according to sensor chip:end************/
gsensor_lis3dh_probe(struct i2c_client * client,const struct i2c_device_id * devid)270 static int gsensor_lis3dh_probe(struct i2c_client *client,
271 const struct i2c_device_id *devid)
272 {
273 return sensor_register_device(client, NULL, devid, &gsensor_lis3dh_ops);
274 }
275
gsensor_lis3dh_remove(struct i2c_client * client)276 static int gsensor_lis3dh_remove(struct i2c_client *client)
277 {
278 return sensor_unregister_device(client, NULL, &gsensor_lis3dh_ops);
279 }
280
281 static const struct i2c_device_id gsensor_lis3dh_id[] = {
282 {"gs_lis3dh", ACCEL_ID_LIS3DH},
283 {}
284 };
285
286 static struct i2c_driver gsensor_lis3dh_driver = {
287 .probe = gsensor_lis3dh_probe,
288 .remove = gsensor_lis3dh_remove,
289 .shutdown = sensor_shutdown,
290 .id_table = gsensor_lis3dh_id,
291 .driver = {
292 .name = "gsensor_lis3dh",
293 #ifdef CONFIG_PM
294 .pm = &sensor_pm_ops,
295 #endif
296 },
297 };
298
299 module_i2c_driver(gsensor_lis3dh_driver);
300
301 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
302 MODULE_DESCRIPTION("lis3dh 3-Axis accelerometer driver");
303 MODULE_LICENSE("GPL");
304