1 /* drivers/input/sensors/gyro/Ewtsa.c
2 *
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: zhangaihui <zah@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 /** This define controls compilation of the master device interface */
35 /*#define EWTSA_MASTER_DEVICE*/
36 /* configurable */
37 #define GYRO_MOUNT_SWAP_XY 0 /* swap X, Y */
38 #define GYRO_MOUNT_REVERSE_X 0 /* reverse X */
39 #define GYRO_MOUNT_REVERSE_Y 0 /* reverse Y */
40 #define GYRO_MOUNT_REVERSE_Z 0 /* reverse Z */
41
42 /* macro defines */
43 /*#define CHIP_ID 0x68*/
44 #define DEVICE_NAME "ewtsa"
45 #define EWTSA_ON 1
46 #define EWTSA_OFF 0
47 #define SLEEP_PIN 14
48 #define DRDY_PIN 12
49 #define DIAG_PIN 11
50 #define MAX_VALUE 32768
51
52 /* ewtsa_delay parameter */
53 #define DELAY_THRES_MIN 1
54 #define DELAY_THRES_1 4
55 #define DELAY_THRES_2 9 /* msec x 90% */
56 #define DELAY_THRES_3 18
57 #define DELAY_THRES_4 45
58 #define DELAY_THRES_5 90
59 #define DELAY_THRES_6 128
60 #define DELAY_THRES_MAX 255
61 #define DELAY_DLPF_2 2
62 #define DELAY_DLPF_3 3
63 #define DELAY_DLPF_4 4
64 #define DELAY_DLPF_5 5
65 #define DELAY_DLPF_6 6
66 #define DELAY_INTMIN_THRES 9
67
68 #define DATA_RATE_1 0x01
69
70 /* ewtsa_sleep parameter */
71 #define SLEEP_OFF 0
72 #define SLEEP_ON 1
73
74 /* event mode */
75 #define EWTSA_POLLING_MODE 0
76 #define EWTSA_INTERUPT_MODE 1
77
78 /* ewtsa register address */
79 #define REG_SMPL 0x15
80 #define REG_FS_DLPF 0x16
81 #define REG_INT_CFG 0x17
82 #define REG_INT_STATUS 0x1A
83 #define REG_SELF_O_C 0x29
84 #define REG_PWR_MGM 0x3E
85 #define REG_MBURST_ALL 0xFF
86 #define GYRO_DATA_REG 0x1D
87
88 /* ewtsa register param */
89 #define SELF_O_C_ENABLE 0x00
90 #define SELF_O_C_DISABLE 0x01
91 #define SLEEP_CTRL_ACTIVATE 0x40
92 #define SLEEP_CTRL_SLEEP 0x00
93 #define INT_CFG_INT_ENABLE 0x01
94 #define INT_CFG_INT_DISABLE 0x00
95
96 /* ewtsa interrupt control */
97 #define EWSTA_INT_CLEAR 0x00
98 #define EWSTA_INT_SKIP 0x01
99
100 /* wait time(ms)*/
101 #define EWTSA_BOOST_TIME_0 500
102
103 /* sleep setting range */
104 #define EWTSA_SLP_MIN 0
105 #define EWTSA_SLP_MAX 1
106
107 /* delay setting range */
108 #define EWTSA_DLY_MIN 1
109 #define EWTSA_DLY_MAX 255
110
111 /* range setting range */
112 #define EWTSA_RNG_MIN 0
113 #define EWTSA_RNG_MAX 3
114
115 /* soc setting range */
116 #define EWTSA_SOC_MIN 0
117 #define EWTSA_SOC_MAX 1
118
119 /* event setting range */
120 #define EWTSA_EVE_MIN 0
121 #define EWTSA_EVE_MAX 1
122
123 /* init param */
124 #define SLEEP_INIT_VAL (SLEEP_ON)
125 #define DELAY_INIT_VAL 10
126 #define RANGE_INIT_VAL 2 /*range 1000*/
127 #define DLPF_INIT_VAL (DELAY_DLPF_2)
128 #define CALIB_FUNC_INIT_VAL (EWTSA_ON)
129
130 /*config store counter num*/
131 #define CONFIG_COUNTER_MIN (6+9)
132 #define CONFIG_COUNTER_MAX (32+9)
133
134 /*command name */
135 #define COMMAND_NAME_SOC 0
136 #define COMMAND_NAME_DLY 1
137 #define COMMAND_NAME_RNG 2
138 #define COMMAND_NAME_EVE 3
139 #define COMMAND_NAME_SLP 4
140 #define COMMAND_NAME_NUM 5
141
142 #define EWTSA_delay DELAY_INIT_VAL
143 #define EWTSA_range RANGE_INIT_VAL
144 #define EWTSA_calib EWTSA_ON
145
146 /****************operate according to sensor chip:start************/
i2c_read_byte(struct i2c_client * thisClient,unsigned char regAddr,char * pReadData)147 static int i2c_read_byte(struct i2c_client *thisClient, unsigned char regAddr, char *pReadData)
148 {
149 int ret = 0;
150
151 ret = i2c_master_send( thisClient, (char*)®Addr, 1);
152 if(ret < 0)
153 {
154 printk("EWTSA send cAddress=0x%x error!\n", regAddr);
155 return ret;
156 }
157 ret = i2c_master_recv( thisClient, (char*)pReadData, 1);
158 if(ret < 0)
159 {
160 printk("EWTSAread *pReadData=0x%x error!\n", *pReadData);
161 return ret;
162 }
163
164 return 1;
165 }
i2c_write_byte(struct i2c_client * thisClient,unsigned char regAddr,unsigned char writeData)166 static int i2c_write_byte(struct i2c_client *thisClient, unsigned char regAddr, unsigned char writeData)
167 {
168 char write_data[2] = {0};
169 int ret=0;
170
171 write_data[0] = regAddr;
172 write_data[1] = writeData;
173
174 ret = i2c_master_send(thisClient, write_data, 2);
175 if (ret < 0)
176 {
177 ret = i2c_master_send(thisClient, write_data, 2);
178 if (ret < 0)
179 {
180 printk("EWTSA send regAddr=0x%x error!\n", regAddr);
181 return ret;
182 }
183 return 1;
184 }
185
186 return 1;
187 }
188
ewtsa_system_restart(struct i2c_client * client)189 static int ewtsa_system_restart(struct i2c_client *client)
190 {
191 int err;
192 char reg;
193 char smpl , dlpf;
194
195 err = i2c_write_byte(client, ( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_DISABLE);
196 if (err < 0) {
197 return err;
198 }
199
200 ///Set SMPL register
201 if (EWTSA_delay <= ( unsigned char)DELAY_THRES_2) {
202 smpl = ( unsigned char)DELAY_INTMIN_THRES;
203 }else{
204 smpl = ( unsigned char)(EWTSA_delay - ( unsigned char)1);
205 }
206 err = i2c_write_byte(client, ( unsigned char)REG_SMPL, ( unsigned char)smpl);
207 if (err < 0) {
208 return err;
209 }
210
211 ///Set DLPF register
212 if (EWTSA_delay >= ( unsigned char)DELAY_THRES_6){
213 dlpf = ( unsigned char)DELAY_DLPF_6;
214 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_5) {
215 dlpf = ( unsigned char)DELAY_DLPF_5;
216 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_4){
217 dlpf = ( unsigned char)DELAY_DLPF_4;
218 }else if (EWTSA_delay >= ( unsigned char)DELAY_THRES_3) {
219 dlpf = ( unsigned char)DELAY_DLPF_3;
220 }else{
221 dlpf = ( unsigned char)DELAY_DLPF_2;
222 }
223
224 reg = ( unsigned char)(( unsigned char)(EWTSA_range << 3) | dlpf | ( unsigned char)0x80 ) ;
225
226 err = i2c_write_byte(client, REG_FS_DLPF, reg);
227 if (err < 0) {
228 return err;
229 }
230
231 if (EWTSA_calib== EWTSA_ON) {
232 printk("EWTSA_set_calibration() start \n");
233 err = i2c_write_byte(client,( unsigned char)REG_SELF_O_C, ( unsigned char)SELF_O_C_ENABLE);
234 if (err < 0) {
235 return err;
236 }
237 mdelay(500);
238 printk("EWTSA_set_calibration() end \n");
239
240 }
241
242 return 0;
243 }
244
ewtsa_disable(struct i2c_client * client)245 static int ewtsa_disable(struct i2c_client *client)
246 {
247 struct sensor_private_data *sensor =
248 (struct sensor_private_data *) i2c_get_clientdata(client);
249
250 gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
251
252 DBG("%s: end \n",__func__);
253
254 return 0;
255 }
256
ewtsa_enable(struct i2c_client * client)257 static int ewtsa_enable(struct i2c_client *client)
258 {
259 int err;
260 struct sensor_private_data *sensor =
261 (struct sensor_private_data *) i2c_get_clientdata(client);
262
263 gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
264 err = i2c_write_byte(client, ( unsigned char)REG_PWR_MGM, ( unsigned char)SLEEP_CTRL_ACTIVATE);////0x44
265 if (err < 0){
266 //return err;
267 err = ewtsa_system_restart(client);///restart; only when i2c error
268 if (err < 0){
269 return err;
270 }
271 }
272
273 err = i2c_write_byte(client, ( unsigned char) REG_INT_CFG, ( unsigned char)INT_CFG_INT_ENABLE);
274 if (err < 0) {
275 return err;
276 }
277 DBG("%s: end \n",__func__);
278 return 0;
279 }
280
gyro_dev_reset(struct i2c_client * client)281 void gyro_dev_reset(struct i2c_client *client)
282 {
283 struct sensor_private_data *sensor =
284 (struct sensor_private_data *) i2c_get_clientdata(client);
285
286
287 DBG("%s\n",__func__);
288 gpio_direction_output(sensor->pdata->standby_pin, GPIO_HIGH);
289 msleep(100);
290 gpio_direction_output(sensor->pdata->standby_pin, GPIO_LOW);
291 msleep(100);
292 }
293
sensor_active(struct i2c_client * client,int enable,int rate)294 static int sensor_active(struct i2c_client *client, int enable, int rate)
295 {
296 /*
297 struct sensor_private_data *sensor =
298 (struct sensor_private_data *) i2c_get_clientdata(client);
299 int status = 0;
300 */
301 int result = 0;
302 if(enable)
303 {
304 result=ewtsa_enable(client);
305 }
306 else
307 {
308 result=ewtsa_disable(client);
309 }
310
311 if(result)
312 printk("%s:fail to active sensor\n",__func__);
313
314 return result;
315
316 }
317
sensor_init(struct i2c_client * client)318 static int sensor_init(struct i2c_client *client)
319 {
320 struct sensor_private_data *sensor =
321 (struct sensor_private_data *) i2c_get_clientdata(client);
322 int result = 0;
323 /*
324 unsigned char buf[5];
325 unsigned char data = 0;
326 int i = 0;
327 char pReadData=0;
328 */
329 sensor->status_cur = SENSOR_OFF;
330 gyro_dev_reset(client);
331 ewtsa_system_restart(client);
332 return result;
333 }
334
335
gyro_report_value(struct i2c_client * client,struct sensor_axis * axis)336 static int gyro_report_value(struct i2c_client *client, struct sensor_axis *axis)
337 {
338 struct sensor_private_data *sensor =
339 (struct sensor_private_data *) i2c_get_clientdata(client);
340
341 if (sensor->status_cur == SENSOR_ON) {
342 /* Report GYRO information */
343 input_report_rel(sensor->input_dev, ABS_RX, axis->x);
344 input_report_rel(sensor->input_dev, ABS_RY, axis->y);
345 input_report_rel(sensor->input_dev, ABS_RZ, axis->z);
346 input_sync(sensor->input_dev);
347 }
348
349 return 0;
350 }
351
sensor_report_value(struct i2c_client * client)352 static int sensor_report_value(struct i2c_client *client)
353 {
354 struct sensor_private_data *sensor =
355 (struct sensor_private_data *) i2c_get_clientdata(client);
356 struct sensor_platform_data *pdata = sensor->pdata;
357 int ret = 0;
358 int x = 0, y = 0, z = 0;
359 struct sensor_axis axis;
360 char buffer[6] = {0};
361 int i = 0;
362 /* int value = 0; */
363
364 memset(buffer, 0, 6);
365 #if 0
366 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
367 do {
368 buffer[0] = sensor->ops->read_reg;
369 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
370 if (ret < 0)
371 return ret;
372 } while (0);
373 #else
374
375 for(i=0; i<6; i++)
376 {
377 i2c_read_byte(client, sensor->ops->read_reg + i,&buffer[i]);
378 }
379 #endif
380
381 x = (short) (((buffer[0]) << 8) | buffer[1]);
382 y = (short) (((buffer[2]) << 8) | buffer[3]);
383 z = (short) (((buffer[4]) << 8) | buffer[5]);
384
385 //printk("%s: x=%d y=%d z=%d \n",__func__, x,y,z);
386 if (pdata)
387 {
388 axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z;
389 axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z;
390 axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z;
391 }
392 else
393 {
394 axis.x = x;
395 axis.y = y;
396 axis.z = z;
397 }
398
399 gyro_report_value(client, &axis);
400
401 mutex_lock(&sensor->data_mutex);
402 sensor->axis = axis;
403 mutex_unlock(&sensor->data_mutex);
404
405 return ret;
406 }
407
408
409 static struct sensor_operate gyro_ewtsa_ops = {
410 .name = "ewtsa",
411 .type = SENSOR_TYPE_GYROSCOPE,
412 .id_i2c = GYRO_ID_EWTSA,
413 .read_reg = GYRO_DATA_REG,
414 .read_len = 6,
415 .id_reg = -1,
416 .id_data = -1,
417 .precision = 16,
418 .ctrl_reg = REG_PWR_MGM,
419 .int_status_reg = REG_INT_STATUS,
420 .range = {-32768, 32768},
421 .trig = IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
422 .active = sensor_active,
423 .init = sensor_init,
424 .report = sensor_report_value,
425 };
426
427 /****************operate according to sensor chip:end************/
gyro_ewtsa_probe(struct i2c_client * client,const struct i2c_device_id * devid)428 static int gyro_ewtsa_probe(struct i2c_client *client,
429 const struct i2c_device_id *devid)
430 {
431 return sensor_register_device(client, NULL, devid, &gyro_ewtsa_ops);
432 }
433
gyro_ewtsa_remove(struct i2c_client * client)434 static int gyro_ewtsa_remove(struct i2c_client *client)
435 {
436 return sensor_unregister_device(client, NULL, &gyro_ewtsa_ops);
437 }
438
439 static const struct i2c_device_id gyro_ewtsa_id[] = {
440 {"ewtsa_gyro", GYRO_ID_EWTSA},
441 {}
442 };
443
444 static struct i2c_driver gyro_ewtsa_driver = {
445 .probe = gyro_ewtsa_probe,
446 .remove = gyro_ewtsa_remove,
447 .shutdown = sensor_shutdown,
448 .id_table = gyro_ewtsa_id,
449 .driver = {
450 .name = "gyro_ewtsa",
451 #ifdef CONFIG_PM
452 .pm = &sensor_pm_ops,
453 #endif
454 },
455 };
456
457 module_i2c_driver(gyro_ewtsa_driver);
458
459 MODULE_AUTHOR("zhangaihui <zah@rock-chips.com>");
460 MODULE_DESCRIPTION("ewtsa 3-Axis Gyroscope driver");
461 MODULE_LICENSE("GPL");
462