blob: d8252fb81d61a684ba41ea90c65a7ce2b1eea9e1 [file] [log] [blame]
Bosch Sensortecface5922018-08-29 08:08:53 +02001/*!
2 * @section LICENSE
3 * (C) Copyright 2011~2016 Bosch Sensortec GmbH All Rights Reserved
4 *
5 * (C) Modification Copyright 2018 Robert Bosch Kft All Rights Reserved
6 *
7 * This software program is licensed subject to the GNU General
8 * Public License (GPL).Version 2,June 1991,
9 * available at http://www.fsf.org/copyleft/gpl.html
10 *
11 * Special: Description of the Software:
12 *
13 * This software module (hereinafter called "Software") and any
14 * information on application-sheets (hereinafter called "Information") is
15 * provided free of charge for the sole purpose to support your application
16 * work.
17 *
18 * As such, the Software is merely an experimental software, not tested for
19 * safety in the field and only intended for inspiration for further development
20 * and testing. Any usage in a safety-relevant field of use (like automotive,
21 * seafaring, spacefaring, industrial plants etc.) was not intended, so there are
22 * no precautions for such usage incorporated in the Software.
23 *
24 * The Software is specifically designed for the exclusive use for Bosch
25 * Sensortec products by personnel who have special experience and training. Do
26 * not use this Software if you do not have the proper experience or training.
27 *
28 * This Software package is provided as is and without any expressed or
29 * implied warranties, including without limitation, the implied warranties of
30 * merchantability and fitness for a particular purpose.
31 *
32 * Bosch Sensortec and their representatives and agents deny any liability for
33 * the functional impairment of this Software in terms of fitness, performance
34 * and safety. Bosch Sensortec and their representatives and agents shall not be
35 * liable for any direct or indirect damages or injury, except as otherwise
36 * stipulated in mandatory applicable law.
37 * The Information provided is believed to be accurate and reliable. Bosch
38 * Sensortec assumes no responsibility for the consequences of use of such
39 * Information nor for any infringement of patents or other rights of third
40 * parties which may result from its use.
41 *
42 *------------------------------------------------------------------------------
43 * The following Product Disclaimer does not apply to the BSX4-HAL-4.1NoFusion Software
44 * which is licensed under the Apache License, Version 2.0 as stated above.
45 * http://www.apache.org/licenses/LICENSE-2.0
46 *
47 * Product Disclaimer
48 *
49 * Common:
50 *
51 * Assessment of Products Returned from Field
52 *
53 * Returned products are considered good if they fulfill the specifications /
54 * test data for 0-mileage and field listed in this document.
55 *
56 * Engineering Samples
57 *
58 * Engineering samples are marked with (e) or (E). Samples may vary from the
59 * valid technical specifications of the series product contained in this
60 * data sheet. Therefore, they are not intended or fit for resale to
61 * third parties or for use in end products. Their sole purpose is internal
62 * client testing. The testing of an engineering sample may in no way replace
63 * the testing of a series product. Bosch assumes no liability for the use
64 * of engineering samples. The purchaser shall indemnify Bosch from all claims
65 * arising from the use of engineering samples.
66 *
67 * Intended use
68 *
69 * Provided that SMI130 is used within the conditions (environment, application,
70 * installation, loads) as described in this TCD and the corresponding
71 * agreed upon documents, Bosch ensures that the product complies with
72 * the agreed properties. Agreements beyond this require
73 * the written approval by Bosch. The product is considered fit for the intended
74 * use when the product successfully has passed the tests
75 * in accordance with the TCD and agreed upon documents.
76 *
77 * It is the responsibility of the customer to ensure the proper application
78 * of the product in the overall system/vehicle.
79 *
80 * Bosch does not assume any responsibility for changes to the environment
81 * of the product that deviate from the TCD and the agreed upon documents
82 * as well as all applications not released by Bosch
83 *
84 * The resale and/or use of products are at the purchaser’s own risk and
85 * responsibility. The examination and testing of the SMI130
86 * is the sole responsibility of the purchaser.
87 *
88 * The purchaser shall indemnify Bosch from all third party claims
89 * arising from any product use not covered by the parameters of
90 * this product data sheet or not approved by Bosch and reimburse Bosch
91 * for all costs and damages in connection with such claims.
92 *
93 * The purchaser must monitor the market for the purchased products,
94 * particularly with regard to product safety, and inform Bosch without delay
95 * of all security relevant incidents.
96 *
97 * Application Examples and Hints
98 *
99 * With respect to any application examples, advice, normal values
100 * and/or any information regarding the application of the device,
101 * Bosch hereby disclaims any and all warranties and liabilities of any kind,
102 * including without limitation warranties of
103 * non-infringement of intellectual property rights or copyrights
104 * of any third party.
105 * The information given in this document shall in no event be regarded
106 * as a guarantee of conditions or characteristics. They are provided
107 * for illustrative purposes only and no evaluation regarding infringement
108 * of intellectual property rights or copyrights or regarding functionality,
109 * performance or error has been made.
110 * @filename smi130_gyro_driver.c
111 * @date 2015/11/17 13:44
112 * @Modification Date 2018/08/28 18:20
113 * @id "836294d"
114 * @version 1.5.9
115 *
116 * @brief SMI130_GYRO Linux Driver
117 */
118#ifdef __KERNEL__
119#include <linux/kernel.h>
120#include <linux/unistd.h>
121#include <linux/types.h>
122#include <linux/string.h>
123#else
124#include <unistd.h>
125#include <sys/types.h>
126#include <string.h>
127#endif
128#include <linux/math64.h>
129#include <linux/version.h>
130#include <linux/module.h>
131#include <linux/init.h>
132#include <linux/i2c.h>
133#include <linux/interrupt.h>
134#include <linux/input.h>
135#include <linux/workqueue.h>
136#include <linux/mutex.h>
137#include <linux/slab.h>
138#include <linux/delay.h>
139#include <linux/gpio.h>
140#include <linux/of_gpio.h>
141#include <linux/of_irq.h>
142
143#ifdef CONFIG_HAS_EARLYSUSPEND
144#include <linux/earlysuspend.h>
145#endif
146
147#include "smi130_gyro.h"
148#include "bs_log.h"
149
150/* sensor specific */
151#define SENSOR_NAME "smi130_gyro"
152#define SMI130_GYRO_ENABLE_INT1 1
153#define SENSOR_CHIP_ID_SMI_GYRO (0x0f)
Puneet Yatnalbd8180a2019-01-30 15:33:10 +0530154#define CHECK_CHIP_ID_TIME_MAX 1
Bosch Sensortecface5922018-08-29 08:08:53 +0200155#define DRIVER_VERSION "0.0.53.0"
156#define SMI_GYRO_USE_FIFO 1
157#define SMI_GYRO_USE_BASIC_I2C_FUNC 1
158#define SMI_GYRO_REG_NAME(name) SMI130_GYRO_##name
159#define SMI_GYRO_VAL_NAME(name) SMI130_GYRO_##name
160#define SMI_GYRO_CALL_API(name) smi130_gyro_##name
161#define MSC_TIME 6
162
163#define SMI_GYRO_I2C_WRITE_DELAY_TIME 1
164
165/* generic */
Puneet Yatnalbd8180a2019-01-30 15:33:10 +0530166#define SMI_GYRO_MAX_RETRY_I2C_XFER (2)
Bosch Sensortecface5922018-08-29 08:08:53 +0200167#define SMI_GYRO_MAX_RETRY_WAKEUP (5)
168#define SMI_GYRO_MAX_RETRY_WAIT_DRDY (100)
169
170#define SMI_GYRO_DELAY_MIN (1)
171#define SMI_GYRO_DELAY_DEFAULT (200)
172
173#define SMI_GYRO_VALUE_MAX (32767)
174#define SMI_GYRO_VALUE_MIN (-32768)
175
176#define BYTES_PER_LINE (16)
177
178#define SMI_GYRO_SELF_TEST 0
179
180#define SMI_GYRO_SOFT_RESET_VALUE 0xB6
181
182#ifdef SMI_GYRO_USE_FIFO
183#define MAX_FIFO_F_LEVEL 100
184#define MAX_FIFO_F_BYTES 8
185#define SMI130_GYRO_FIFO_DAT_SEL_X 1
186#define SMI130_GYRO_FIFO_DAT_SEL_Y 2
187#define SMI130_GYRO_FIFO_DAT_SEL_Z 3
188#endif
189
190/*!
191 * @brief:BMI058 feature
192 * macro definition
193*/
194#ifdef CONFIG_SENSORS_BMI058
195/*! BMI058 X AXIS definition*/
196#define BMI058_X_AXIS SMI130_GYRO_Y_AXIS
197/*! BMI058 Y AXIS definition*/
198#define BMI058_Y_AXIS SMI130_GYRO_X_AXIS
199
200#define C_BMI058_One_U8X 1
201#define C_BMI058_Two_U8X 2
202#endif
203
204/*! Bosch sensor unknown place*/
205#define BOSCH_SENSOR_PLACE_UNKNOWN (-1)
206/*! Bosch sensor remapping table size P0~P7*/
207#define MAX_AXIS_REMAP_TAB_SZ 8
208
209
210struct bosch_sensor_specific {
211 char *name;
212 /* 0 to 7 */
213 int place;
214 int irq;
215 int (*irq_gpio_cfg)(void);
216};
217
218
219/*!
220 * we use a typedef to hide the detail,
221 * because this type might be changed
222 */
223struct bosch_sensor_axis_remap {
224 /* src means which source will be mapped to target x, y, z axis */
225 /* if an target OS axis is remapped from (-)x,
226 * src is 0, sign_* is (-)1 */
227 /* if an target OS axis is remapped from (-)y,
228 * src is 1, sign_* is (-)1 */
229 /* if an target OS axis is remapped from (-)z,
230 * src is 2, sign_* is (-)1 */
231 int src_x:3;
232 int src_y:3;
233 int src_z:3;
234
235 int sign_x:2;
236 int sign_y:2;
237 int sign_z:2;
238};
239
240
241struct bosch_sensor_data {
242 union {
243 int16_t v[3];
244 struct {
245 int16_t x;
246 int16_t y;
247 int16_t z;
248 };
249 };
250};
251
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530252#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
253#define SMI_GYRO_MAXSAMPLE 4000
254#define G_MAX 23920640
255struct smi_gyro_sample {
256 int xyz[3];
257 unsigned int tsec;
258 unsigned long long tnsec;
259};
260#endif
261
Bosch Sensortecface5922018-08-29 08:08:53 +0200262struct smi_gyro_client_data {
263 struct smi130_gyro_t device;
264 struct i2c_client *client;
265 struct input_dev *input;
266 struct delayed_work work;
267
268#ifdef CONFIG_HAS_EARLYSUSPEND
269 struct early_suspend early_suspend_handler;
270#endif
271
272 atomic_t delay;
273 uint8_t debug_level;
274 struct smi130_gyro_data_t value;
275 u8 enable:1;
276 unsigned int fifo_count;
277 unsigned char fifo_datasel;
278 uint64_t timestamp;
279 uint64_t base_time;
280 uint64_t fifo_time;
281 uint64_t gyro_count;
282 uint64_t time_odr;
283 /* controls not only reg, but also workqueue */
284 struct mutex mutex_op_mode;
285 struct mutex mutex_enable;
286 struct bosch_sensor_specific *bosch_pd;
287 struct work_struct report_data_work;
288 int is_timer_running;
289 struct hrtimer timer;
290 ktime_t work_delay_kt;
291 uint8_t gpio_pin;
292 int16_t IRQ;
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530293#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
294 bool read_gyro_boot_sample;
295 int gyro_bufsample_cnt;
296 bool gyro_buffer_smi130_samples;
Puneet Yatnalab29efa2019-08-05 13:13:31 +0530297 bool gyro_enable;
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530298 struct kmem_cache *smi_gyro_cachepool;
299 struct smi_gyro_sample *smi130_gyro_samplist[SMI_GYRO_MAXSAMPLE];
300 int max_buffer_time;
301 struct input_dev *gyrobuf_dev;
302 int report_evt_cnt;
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +0530303 struct mutex gyro_sensor_buff;
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530304#endif
Bosch Sensortecface5922018-08-29 08:08:53 +0200305};
306
307static struct i2c_client *smi_gyro_client;
308/* i2c operation for API */
309static int smi_gyro_i2c_read(struct i2c_client *client, u8 reg_addr,
310 u8 *data, u8 len);
311static int smi_gyro_i2c_write(struct i2c_client *client, u8 reg_addr,
312 u8 *data, u8 len);
313
314static void smi_gyro_dump_reg(struct i2c_client *client);
315static int smi_gyro_check_chip_id(struct i2c_client *client);
316
Bosch Sensortecface5922018-08-29 08:08:53 +0200317#ifdef CONFIG_HAS_EARLYSUSPEND
puneet88e66bc2018-12-07 10:44:31 +0530318static int smi_gyro_post_resume(struct i2c_client *client);
319static int smi_gyro_pre_suspend(struct i2c_client *client);
Bosch Sensortecface5922018-08-29 08:08:53 +0200320static void smi_gyro_early_suspend(struct early_suspend *handler);
321static void smi_gyro_late_resume(struct early_suspend *handler);
322#endif
323
324static void smi130_gyro_delay(SMI130_GYRO_U16 msec)
325{
326 if (msec <= 20)
327 usleep_range(msec * 1000, msec * 1000);
328 else
329 msleep(msec);
330}
331
332/*!
333* SMI130_GYRO sensor remapping function
334* need to give some parameter in BSP files first.
335*/
336static const struct bosch_sensor_axis_remap
337 bosch_axis_remap_tab_dft[MAX_AXIS_REMAP_TAB_SZ] = {
338 /* src_x src_y src_z sign_x sign_y sign_z */
339 { 0, 1, 2, 1, 1, 1 }, /* P0 */
340 { 1, 0, 2, 1, -1, 1 }, /* P1 */
341 { 0, 1, 2, -1, -1, 1 }, /* P2 */
342 { 1, 0, 2, -1, 1, 1 }, /* P3 */
343
344 { 0, 1, 2, -1, 1, -1 }, /* P4 */
345 { 1, 0, 2, -1, -1, -1 }, /* P5 */
346 { 0, 1, 2, 1, -1, -1 }, /* P6 */
347 { 1, 0, 2, 1, 1, -1 }, /* P7 */
348};
349
350static void bosch_remap_sensor_data(struct bosch_sensor_data *data,
351 const struct bosch_sensor_axis_remap *remap)
352{
353 struct bosch_sensor_data tmp;
354
355 tmp.x = data->v[remap->src_x] * remap->sign_x;
356 tmp.y = data->v[remap->src_y] * remap->sign_y;
357 tmp.z = data->v[remap->src_z] * remap->sign_z;
358
359 memcpy(data, &tmp, sizeof(*data));
360}
361
362static void bosch_remap_sensor_data_dft_tab(struct bosch_sensor_data *data,
363 int place)
364{
365/* sensor with place 0 needs not to be remapped */
366 if ((place <= 0) || (place >= MAX_AXIS_REMAP_TAB_SZ))
367 return;
368 bosch_remap_sensor_data(data, &bosch_axis_remap_tab_dft[place]);
369}
370
371static void smi130_gyro_remap_sensor_data(struct smi130_gyro_data_t *val,
372 struct smi_gyro_client_data *client_data)
373{
374 struct bosch_sensor_data bsd;
375 int place;
376
377 if ((NULL == client_data->bosch_pd) || (BOSCH_SENSOR_PLACE_UNKNOWN
378 == client_data->bosch_pd->place))
379 place = BOSCH_SENSOR_PLACE_UNKNOWN;
380 else
381 place = client_data->bosch_pd->place;
382
383#ifdef CONFIG_SENSORS_BMI058
384/*x,y need to be invesed becase of HW Register for BMI058*/
385 bsd.y = val->datax;
386 bsd.x = val->datay;
387 bsd.z = val->dataz;
388#else
389 bsd.x = val->datax;
390 bsd.y = val->datay;
391 bsd.z = val->dataz;
392#endif
393
394 bosch_remap_sensor_data_dft_tab(&bsd, place);
395
396 val->datax = bsd.x;
397 val->datay = bsd.y;
398 val->dataz = bsd.z;
399
400}
401
402static int smi_gyro_check_chip_id(struct i2c_client *client)
403{
404 int err = -1;
405 u8 chip_id = 0;
406 u8 read_count = 0;
407
408 while (read_count++ < CHECK_CHIP_ID_TIME_MAX) {
409 smi_gyro_i2c_read(client, SMI_GYRO_REG_NAME(CHIP_ID_ADDR), &chip_id, 1);
410 PINFO("read chip id result: %#x", chip_id);
411
412 if ((chip_id & 0xff) != SENSOR_CHIP_ID_SMI_GYRO) {
413 smi130_gyro_delay(1);
414 } else {
415 err = 0;
416 break;
417 }
418 }
419 return err;
420}
421
422static void smi_gyro_dump_reg(struct i2c_client *client)
423{
424 int i;
puneet597e4f12018-11-06 14:32:16 +0530425 u8 dbg_buf[64] = {0};
Bosch Sensortecface5922018-08-29 08:08:53 +0200426 u8 dbg_buf_str[64 * 3 + 1] = "";
427
428 for (i = 0; i < BYTES_PER_LINE; i++) {
429 dbg_buf[i] = i;
430 snprintf(dbg_buf_str + i * 3, 16, "%02x%c",
431 dbg_buf[i],
432 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
433 }
434 dev_dbg(&client->dev, "%s\n", dbg_buf_str);
435
436 smi_gyro_i2c_read(client, SMI_GYRO_REG_NAME(CHIP_ID_ADDR), dbg_buf, 64);
437 for (i = 0; i < 64; i++) {
438 snprintf(dbg_buf_str + i * 3, 16, "%02x%c",
439 dbg_buf[i],
440 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
441 }
442 dev_dbg(&client->dev, "%s\n", dbg_buf_str);
443}
444
445/*i2c read routine for API*/
446static int smi_gyro_i2c_read(struct i2c_client *client, u8 reg_addr,
447 u8 *data, u8 len)
448{
449#if !defined SMI_GYRO_USE_BASIC_I2C_FUNC
450 s32 dummy;
451 if (NULL == client)
452 return -ENODEV;
453
454 while (0 != len--) {
455#ifdef SMI_GYRO_SMBUS
456 dummy = i2c_smbus_read_byte_data(client, reg_addr);
457 if (dummy < 0) {
458 dev_err(&client->dev, "i2c bus read error");
459 return -EIO;
460 }
461 *data = (u8)(dummy & 0xff);
462#else
463 dummy = i2c_master_send(client, (char *)&reg_addr, 1);
464 if (dummy < 0)
465 return -EIO;
466
467 dummy = i2c_master_recv(client, (char *)data, 1);
468 if (dummy < 0)
469 return -EIO;
470#endif
471 reg_addr++;
472 data++;
473 }
474 return 0;
475#else
476 int retry;
477
478 struct i2c_msg msg[] = {
479 {
480 .addr = client->addr,
481 .flags = 0,
482 .len = 1,
483 .buf = &reg_addr,
484 },
485
486 {
487 .addr = client->addr,
488 .flags = I2C_M_RD,
489 .len = len,
490 .buf = data,
491 },
492 };
493
494 for (retry = 0; retry < SMI_GYRO_MAX_RETRY_I2C_XFER; retry++) {
495 if (i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)) > 0)
496 break;
497 else
498 smi130_gyro_delay(SMI_GYRO_I2C_WRITE_DELAY_TIME);
499 }
500
501 if (SMI_GYRO_MAX_RETRY_I2C_XFER <= retry) {
502 dev_err(&client->dev, "I2C xfer error");
503 return -EIO;
504 }
505
506 return 0;
507#endif
508}
509
510#ifdef SMI_GYRO_USE_FIFO
511static int smi_gyro_i2c_burst_read(struct i2c_client *client, u8 reg_addr,
512 u8 *data, u16 len)
513{
514 int retry;
515
516 struct i2c_msg msg[] = {
517 {
518 .addr = client->addr,
519 .flags = 0,
520 .len = 1,
521 .buf = &reg_addr,
522 },
523
524 {
525 .addr = client->addr,
526 .flags = I2C_M_RD,
527 .len = len,
528 .buf = data,
529 },
530 };
531
532 for (retry = 0; retry < SMI_GYRO_MAX_RETRY_I2C_XFER; retry++) {
533 if (i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)) > 0)
534 break;
535 else
536 smi130_gyro_delay(SMI_GYRO_I2C_WRITE_DELAY_TIME);
537 }
538
539 if (SMI_GYRO_MAX_RETRY_I2C_XFER <= retry) {
540 dev_err(&client->dev, "I2C xfer error");
541 return -EIO;
542 }
543
544 return 0;
545}
546#endif
547
548/*i2c write routine for */
549static int smi_gyro_i2c_write(struct i2c_client *client, u8 reg_addr,
550 u8 *data, u8 len)
551{
552#if !defined SMI_GYRO_USE_BASIC_I2C_FUNC
553 s32 dummy;
554
555#ifndef SMI_GYRO_SMBUS
556 u8 buffer[2];
557#endif
558
559 if (NULL == client)
560 return -ENODEV;
561
562 while (0 != len--) {
563#ifdef SMI_GYRO_SMBUS
564 dummy = i2c_smbus_write_byte_data(client, reg_addr, *data);
565#else
566 buffer[0] = reg_addr;
567 buffer[1] = *data;
568 dummy = i2c_master_send(client, (char *)buffer, 2);
569#endif
570 reg_addr++;
571 data++;
572 if (dummy < 0) {
573 dev_err(&client->dev, "error writing i2c bus");
574 return -EIO;
575 }
576
577 }
578 return 0;
579#else
580 u8 buffer[2];
581 int retry;
582 struct i2c_msg msg[] = {
583 {
584 .addr = client->addr,
585 .flags = 0,
586 .len = 2,
587 .buf = buffer,
588 },
589 };
590
591 while (0 != len--) {
592 buffer[0] = reg_addr;
593 buffer[1] = *data;
594 for (retry = 0; retry < SMI_GYRO_MAX_RETRY_I2C_XFER; retry++) {
595 if (i2c_transfer(client->adapter, msg,
596 ARRAY_SIZE(msg)) > 0) {
597 break;
598 } else {
599 smi130_gyro_delay(SMI_GYRO_I2C_WRITE_DELAY_TIME);
600 }
601 }
602 if (SMI_GYRO_MAX_RETRY_I2C_XFER <= retry) {
603 dev_err(&client->dev, "I2C xfer error");
604 return -EIO;
605 }
606 reg_addr++;
607 data++;
608 }
609
610 return 0;
611#endif
612}
613
614static int smi_gyro_i2c_read_wrapper(u8 dev_addr, u8 reg_addr, u8 *data, u8 len)
615{
616 int err;
617 err = smi_gyro_i2c_read(smi_gyro_client, reg_addr, data, len);
618 return err;
619}
620
621static int smi_gyro_i2c_write_wrapper(u8 dev_addr, u8 reg_addr, u8 *data, u8 len)
622{
623 int err;
624 err = smi_gyro_i2c_write(smi_gyro_client, reg_addr, data, len);
625 return err;
626}
627
628
629static void smi_gyro_work_func(struct work_struct *work)
630{
631 struct smi_gyro_client_data *client_data =
632 container_of((struct delayed_work *)work,
633 struct smi_gyro_client_data, work);
634
635 unsigned long delay =
636 msecs_to_jiffies(atomic_read(&client_data->delay));
637 struct smi130_gyro_data_t gyro_data;
638
639 SMI_GYRO_CALL_API(get_dataXYZ)(&gyro_data);
640 /*remapping for SMI130_GYRO sensor*/
641 smi130_gyro_remap_sensor_data(&gyro_data, client_data);
642
643 input_report_abs(client_data->input, ABS_X, gyro_data.datax);
644 input_report_abs(client_data->input, ABS_Y, gyro_data.datay);
645 input_report_abs(client_data->input, ABS_Z, gyro_data.dataz);
646 input_sync(client_data->input);
647
648 schedule_delayed_work(&client_data->work, delay);
649}
650
651static struct workqueue_struct *reportdata_wq;
652
653uint64_t smi130_gyro_get_alarm_timestamp(void)
654{
655 uint64_t ts_ap;
656 struct timespec tmp_time;
657 get_monotonic_boottime(&tmp_time);
658 ts_ap = (uint64_t)tmp_time.tv_sec * 1000000000 + tmp_time.tv_nsec;
659 return ts_ap;
660}
661#define ABS(x) ((x) > 0 ? (x) : -(x))
662
663static void smi130_gyro_work_func(struct work_struct *work)
664{
665 struct smi_gyro_client_data *smi130_gyro =
666 container_of(work,
667 struct smi_gyro_client_data, report_data_work);
668 int i;
669 struct smi130_gyro_data_t gyro_lsb;
670 unsigned char fifo_framecount;
671 signed char fifo_data_out[MAX_FIFO_F_LEVEL * MAX_FIFO_F_BYTES] = {0};
672 unsigned char f_len = 0;
673 uint64_t del;
674 uint64_t time_internal;
675 struct timespec ts;
676 int64_t drift_time = 0;
677 static uint64_t time_odr;
678 static uint32_t data_cnt;
679 static uint32_t pre_data_cnt;
680 static int64_t sample_drift_offset;
681 if (smi130_gyro->fifo_datasel)
682 /*Select one axis data output for every fifo frame*/
683 f_len = 2;
684 else
685 /*Select X Y Z axis data output for every fifo frame*/
686 f_len = 6;
687 if (SMI_GYRO_CALL_API(get_fifo_framecount)(&fifo_framecount) < 0) {
688 PERR("bm160_get_fifo_framecount err\n");
689 return;
690 }
691 if (fifo_framecount == 0)
692 return;
693 if (fifo_framecount > MAX_FIFO_F_LEVEL)
694 fifo_framecount = MAX_FIFO_F_LEVEL;
695 if (smi_gyro_i2c_burst_read(smi130_gyro->client, SMI130_GYRO_FIFO_DATA_ADDR,
696 fifo_data_out, fifo_framecount * f_len) < 0) {
697 PERR("smi130_gyro read fifo err\n");
698 return;
699 }
700 smi130_gyro->fifo_time = smi130_gyro_get_alarm_timestamp();
701 if (smi130_gyro->gyro_count == 0)
702 smi130_gyro->base_time = smi130_gyro->timestamp =
703 smi130_gyro->fifo_time - (fifo_framecount-1) * smi130_gyro->time_odr;
704
705 smi130_gyro->gyro_count += fifo_framecount;
706 del = smi130_gyro->fifo_time - smi130_gyro->base_time;
707 time_internal = div64_u64(del, smi130_gyro->gyro_count);
708 data_cnt++;
709 if (data_cnt == 1)
710 time_odr = smi130_gyro->time_odr;
711 if (time_internal > time_odr) {
712 if (time_internal - time_odr > div64_u64 (time_odr, 200))
713 time_internal = time_odr + div64_u64(time_odr, 200);
714 } else {
715 if (time_odr - time_internal > div64_u64(time_odr, 200))
716 time_internal = time_odr - div64_u64(time_odr, 200);
717 }
718
719 /* Select X Y Z axis data output for every frame */
720 for (i = 0; i < fifo_framecount; i++) {
721 if (smi130_gyro->debug_level & 0x01)
722 printk(KERN_INFO "smi_gyro time =%llu fifo_time = %llu time_internal = %llu smi_gyro->count= %llu count = %d",
723 smi130_gyro->timestamp, smi130_gyro->fifo_time,
724 time_internal, smi130_gyro->gyro_count, fifo_framecount);
725 ts = ns_to_timespec(smi130_gyro->timestamp);
726 gyro_lsb.datax =
727 ((unsigned char)fifo_data_out[i * f_len + 1] << 8
728 | (unsigned char)fifo_data_out[i * f_len + 0]);
729 gyro_lsb.datay =
730 ((unsigned char)fifo_data_out[i * f_len + 3] << 8
731 | (unsigned char)fifo_data_out[i * f_len + 2]);
732 gyro_lsb.dataz =
733 ((unsigned char)fifo_data_out[i * f_len + 5] << 8
734 | (unsigned char)fifo_data_out[i * f_len + 4]);
735 smi130_gyro_remap_sensor_data(&gyro_lsb, smi130_gyro);
736 input_event(smi130_gyro->input, EV_MSC, MSC_TIME,
737 ts.tv_sec);
738 input_event(smi130_gyro->input, EV_MSC, MSC_TIME,
739 ts.tv_nsec);
740 input_event(smi130_gyro->input, EV_MSC,
741 MSC_GESTURE, gyro_lsb.datax);
742 input_event(smi130_gyro->input, EV_MSC,
743 MSC_RAW, gyro_lsb.datay);
744 input_event(smi130_gyro->input, EV_MSC,
745 MSC_SCAN, gyro_lsb.dataz);
746 input_sync(smi130_gyro->input);
747 smi130_gyro->timestamp += time_internal - sample_drift_offset;
748 }
749 drift_time = smi130_gyro->timestamp - smi130_gyro->fifo_time;
750 if (data_cnt % 20 == 0) {
751 if (ABS(drift_time) > div64_u64(time_odr, 5)) {
752 sample_drift_offset =
753 div64_s64(drift_time, smi130_gyro->gyro_count - pre_data_cnt);
754 pre_data_cnt = smi130_gyro->gyro_count;
755 time_odr = time_internal;
756 }
757 }
758}
759
760
761static enum hrtimer_restart reportdata_timer_fun(
762 struct hrtimer *hrtimer)
763{
764 struct smi_gyro_client_data *client_data =
765 container_of(hrtimer, struct smi_gyro_client_data, timer);
766 int32_t delay = 0;
767 delay = 10;
768 queue_work(reportdata_wq, &(client_data->report_data_work));
769 client_data->work_delay_kt = ns_to_ktime(delay*1000000);
770 hrtimer_forward(hrtimer, ktime_get(), client_data->work_delay_kt);
771
772 return HRTIMER_RESTART;
773}
774
775static ssize_t smi_gyro_show_enable_timer(struct device *dev,
776 struct device_attribute *attr, char *buf)
777{
778 struct input_dev *input = to_input_dev(dev);
779 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
780
781 return snprintf(buf, 16, "%d\n", client_data->is_timer_running);
782}
783
784static ssize_t smi_gyro_store_enable_timer(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
787{
788 unsigned long data;
789 int error;
790 struct input_dev *input = to_input_dev(dev);
791 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
792 error = kstrtoul(buf, 10, &data);
793 if (error)
794 return error;
795 if (data) {
796 if (0 == client_data->is_timer_running) {
797 hrtimer_start(&client_data->timer,
798 ns_to_ktime(10000000),
799 HRTIMER_MODE_REL);
800 client_data->is_timer_running = 1;
801 client_data->base_time = 0;
802 client_data->timestamp = 0;
803 client_data->gyro_count = 0;
804 }
805 } else {
806 if (1 == client_data->is_timer_running) {
807 hrtimer_cancel(&client_data->timer);
808 client_data->is_timer_running = 0;
809 client_data->base_time = 0;
810 client_data->timestamp = 0;
811 client_data->gyro_count = 0;
812 }
813 }
814 return count;
815}
816
817static ssize_t smi130_gyro_show_debug_level(struct device *dev,
818 struct device_attribute *attr, char *buf)
819{
820 int err;
821 struct input_dev *input = to_input_dev(dev);
822 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
823 err = snprintf(buf, 8, "%d\n", client_data->debug_level);
824 return err;
825}
826static ssize_t smi130_gyro_store_debug_level(struct device *dev,
827 struct device_attribute *attr,
828 const char *buf, size_t count)
829{
830 int32_t ret = 0;
831 unsigned long data;
832 struct input_dev *input = to_input_dev(dev);
833 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
834 ret = kstrtoul(buf, 16, &data);
835 if (ret)
836 return ret;
837 client_data->debug_level = (uint8_t)data;
838 return count;
839}
840
841static int smi_gyro_set_soft_reset(struct i2c_client *client)
842{
843 int err = 0;
844 unsigned char data = SMI_GYRO_SOFT_RESET_VALUE;
845 err = smi_gyro_i2c_write(client, SMI130_GYRO_BGW_SOFTRESET_ADDR, &data, 1);
846 return err;
847}
848
849static ssize_t smi_gyro_show_chip_id(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
852 return snprintf(buf, 16, "%d\n", SENSOR_CHIP_ID_SMI_GYRO);
853}
854
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530855#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
856static inline int smi130_check_gyro_early_buff_enable_flag(
857 struct smi_gyro_client_data *client_data)
858{
859 if (client_data->gyro_buffer_smi130_samples == true)
860 return 1;
861 else
862 return 0;
863}
Puneet Yatnalab29efa2019-08-05 13:13:31 +0530864static void smi130_check_gyro_enable_flag(
865 struct smi_gyro_client_data *client_data, unsigned long data)
866{
867 if (data == SMI130_GYRO_MODE_NORMAL)
868 client_data->gyro_enable = true;
869 else
870 client_data->gyro_enable = false;
871}
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530872#else
873static inline int smi130_check_gyro_early_buff_enable_flag(
874 struct smi_gyro_client_data *client_data)
875{
876 return 0;
877}
Puneet Yatnalab29efa2019-08-05 13:13:31 +0530878static void smi130_check_gyro_enable_flag(
879 struct smi_gyro_client_data *client_data, unsigned long data)
880{
881}
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530882#endif
883
Bosch Sensortecface5922018-08-29 08:08:53 +0200884static ssize_t smi_gyro_show_op_mode(struct device *dev,
885 struct device_attribute *attr, char *buf)
886{
887 int ret;
888 struct input_dev *input = to_input_dev(dev);
889 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
890 u8 op_mode = 0xff;
891
892 mutex_lock(&client_data->mutex_op_mode);
893 SMI_GYRO_CALL_API(get_mode)(&op_mode);
894 mutex_unlock(&client_data->mutex_op_mode);
895
896 ret = snprintf(buf, 16, "%d\n", op_mode);
897
898 return ret;
899}
900
901static ssize_t smi_gyro_store_op_mode(struct device *dev,
902 struct device_attribute *attr,
903 const char *buf, size_t count)
904{
905 int err;
906 struct input_dev *input = to_input_dev(dev);
907 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
908
909 long op_mode;
910
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530911
Bosch Sensortecface5922018-08-29 08:08:53 +0200912 err = kstrtoul(buf, 10, &op_mode);
913 if (err)
914 return err;
Puneet Yatnalab29efa2019-08-05 13:13:31 +0530915
916 smi130_check_gyro_enable_flag(client_data, op_mode);
917
918 err = smi130_check_gyro_early_buff_enable_flag(client_data);
919 if (err)
920 return count;
921
Bosch Sensortecface5922018-08-29 08:08:53 +0200922 mutex_lock(&client_data->mutex_op_mode);
923
924 err = SMI_GYRO_CALL_API(set_mode)(op_mode);
925
926 mutex_unlock(&client_data->mutex_op_mode);
927
928 if (err)
929 return err;
930 else
931 return count;
932}
933
934
935
936static ssize_t smi_gyro_show_value(struct device *dev,
937 struct device_attribute *attr, char *buf)
938{
939 struct input_dev *input = to_input_dev(dev);
940 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
941 int count;
942
943 struct smi130_gyro_data_t value_data;
944 SMI_GYRO_CALL_API(get_dataXYZ)(&value_data);
945 /*SMI130_GYRO sensor raw data remapping*/
946 smi130_gyro_remap_sensor_data(&value_data, client_data);
947
948 count = snprintf(buf, 96, "%hd %hd %hd\n",
949 value_data.datax,
950 value_data.datay,
951 value_data.dataz);
952
953 return count;
954}
955
956static ssize_t smi_gyro_show_range(struct device *dev,
957 struct device_attribute *attr, char *buf)
958{
959 int err;
960 unsigned char range = 0;
961 SMI_GYRO_CALL_API(get_range_reg)(&range);
962 err = snprintf(buf, 16, "%d\n", range);
963 return err;
964}
965
966static ssize_t smi_gyro_store_range(struct device *dev,
967 struct device_attribute *attr,
968 const char *buf, size_t count)
969{
970 int err;
971 unsigned long range;
Puneet Yatnalcfd94842019-04-16 12:11:05 +0530972 struct input_dev *input = to_input_dev(dev);
973 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
974
975 err = smi130_check_gyro_early_buff_enable_flag(client_data);
976 if (err)
977 return count;
978
Bosch Sensortecface5922018-08-29 08:08:53 +0200979 err = kstrtoul(buf, 10, &range);
980 if (err)
981 return err;
982 SMI_GYRO_CALL_API(set_range_reg)(range);
983 return count;
984}
985
986/*
987decimation odr filter bandwidth bits
98820 100HZ 32HZ 7
98910 200Hz 64HZ 6
99020 100HZ 12HZ 5
99110 200hz 23HZ 4
9925 400HZ 47HZ 3
9932 1000HZ 116HZ 2
9940 2000HZ 230HZ 1
9950 2000HZ Unfiltered(523HZ) 0
996*/
997
998static const uint64_t odr_map[8] = {
999500000, 500000, 1000000, 2500000, 5000000, 10000000, 5000000, 10000000};
1000
1001static ssize_t smi_gyro_show_bandwidth(struct device *dev,
1002 struct device_attribute *attr, char *buf)
1003{
1004 int err;
1005 unsigned char bandwidth = 0;
1006 SMI_GYRO_CALL_API(get_bw)(&bandwidth);
1007 err = snprintf(buf, 16, "%d\n", bandwidth);
1008 return err;
1009}
1010
1011static ssize_t smi_gyro_store_bandwidth(struct device *dev,
1012 struct device_attribute *attr,
1013 const char *buf, size_t count)
1014{
1015 int err;
1016 struct input_dev *input = to_input_dev(dev);
1017 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1018 unsigned long bandwidth;
1019 u8 op_mode = 0xff;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301020
1021 err = smi130_check_gyro_early_buff_enable_flag(client_data);
1022 if (err)
1023 return count;
1024
Bosch Sensortecface5922018-08-29 08:08:53 +02001025 err = kstrtoul(buf, 10, &bandwidth);
1026 if (err)
1027 return err;
1028 /*
1029 set bandwidth only in the op_mode=0
1030 */
1031 err = SMI_GYRO_CALL_API(get_mode)(&op_mode);
1032 if (op_mode == 0) {
1033 err += SMI_GYRO_CALL_API(set_bw)(bandwidth);
1034 } else {
1035 err += SMI_GYRO_CALL_API(set_mode)(0);
1036 err += SMI_GYRO_CALL_API(set_bw)(bandwidth);
1037 smi130_gyro_delay(1);
1038 err += SMI_GYRO_CALL_API(set_mode)(2);
1039 smi130_gyro_delay(3);
1040 }
1041
1042 if (err)
1043 PERR("set failed");
1044 client_data->time_odr = odr_map[bandwidth];
1045 client_data->base_time = 0;
1046 client_data->gyro_count = 0;
1047 return count;
1048}
1049
1050
1051static ssize_t smi_gyro_show_enable(struct device *dev,
1052 struct device_attribute *attr, char *buf)
1053{
1054 struct input_dev *input = to_input_dev(dev);
1055 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1056 int err;
1057
1058 mutex_lock(&client_data->mutex_enable);
1059 err = snprintf(buf, 16, "%d\n", client_data->enable);
1060 mutex_unlock(&client_data->mutex_enable);
1061 return err;
1062}
1063
1064static ssize_t smi_gyro_store_enable(struct device *dev,
1065 struct device_attribute *attr,
1066 const char *buf, size_t count)
1067{
1068 unsigned long data;
1069 int err;
1070 struct input_dev *input = to_input_dev(dev);
1071 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1072
1073 err = kstrtoul(buf, 10, &data);
1074 if (err)
1075 return err;
1076
1077 data = data ? 1 : 0;
1078 mutex_lock(&client_data->mutex_enable);
1079 if (data != client_data->enable) {
1080 if (data) {
1081 schedule_delayed_work(
1082 &client_data->work,
1083 msecs_to_jiffies(atomic_read(
1084 &client_data->delay)));
1085 } else {
1086 cancel_delayed_work_sync(&client_data->work);
1087 }
1088
1089 client_data->enable = data;
1090 }
1091 mutex_unlock(&client_data->mutex_enable);
1092
1093 return count;
1094}
1095
1096static ssize_t smi_gyro_show_delay(struct device *dev,
1097 struct device_attribute *attr, char *buf)
1098{
1099 struct input_dev *input = to_input_dev(dev);
1100 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1101
1102 return snprintf(buf, 16, "%d\n", atomic_read(&client_data->delay));
1103
1104}
1105
1106static ssize_t smi_gyro_store_delay(struct device *dev,
1107 struct device_attribute *attr,
1108 const char *buf, size_t count)
1109{
1110 unsigned long data;
1111 int err;
1112 struct input_dev *input = to_input_dev(dev);
1113 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1114
1115 err = kstrtoul(buf, 10, &data);
1116 if (err)
1117 return err;
1118
1119 if (data == 0) {
1120 err = -EINVAL;
1121 return err;
1122 }
1123
1124 if (data < SMI_GYRO_DELAY_MIN)
1125 data = SMI_GYRO_DELAY_MIN;
1126
1127 atomic_set(&client_data->delay, data);
1128
1129 return count;
1130}
1131
1132
1133static ssize_t smi_gyro_store_fastoffset_en(struct device *dev,
1134 struct device_attribute *attr,
1135 const char *buf, size_t count)
1136{
1137 int err;
1138 unsigned long fastoffset_en;
1139 err = kstrtoul(buf, 10, &fastoffset_en);
1140 if (err)
1141 return err;
1142 if (fastoffset_en) {
1143
1144#ifdef CONFIG_SENSORS_BMI058
1145 SMI_GYRO_CALL_API(set_fast_offset_en_ch)(BMI058_X_AXIS, 1);
1146 SMI_GYRO_CALL_API(set_fast_offset_en_ch)(BMI058_Y_AXIS, 1);
1147#else
1148 SMI_GYRO_CALL_API(set_fast_offset_en_ch)(SMI130_GYRO_X_AXIS, 1);
1149 SMI_GYRO_CALL_API(set_fast_offset_en_ch)(SMI130_GYRO_Y_AXIS, 1);
1150#endif
1151
1152 SMI_GYRO_CALL_API(set_fast_offset_en_ch)(SMI130_GYRO_Z_AXIS, 1);
1153 SMI_GYRO_CALL_API(enable_fast_offset)();
1154 }
1155 return count;
1156}
1157
1158static ssize_t smi_gyro_store_slowoffset_en(struct device *dev,
1159 struct device_attribute *attr,
1160 const char *buf, size_t count)
1161{
1162 int err;
1163 unsigned long slowoffset_en;
1164 err = kstrtoul(buf, 10, &slowoffset_en);
1165 if (err)
1166 return err;
1167 if (slowoffset_en) {
1168 SMI_GYRO_CALL_API(set_slow_offset_th)(3);
1169 SMI_GYRO_CALL_API(set_slow_offset_dur)(0);
1170#ifdef CONFIG_SENSORS_BMI058
1171 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(BMI058_X_AXIS, 1);
1172 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(BMI058_Y_AXIS, 1);
1173#else
1174 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_X_AXIS, 1);
1175 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_Y_AXIS, 1);
1176#endif
1177 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_Z_AXIS, 1);
1178 } else {
1179#ifdef CONFIG_SENSORS_BMI058
1180 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(BMI058_X_AXIS, 0);
1181 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(BMI058_Y_AXIS, 0);
1182#else
1183 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_X_AXIS, 0);
1184 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_Y_AXIS, 0);
1185#endif
1186 SMI_GYRO_CALL_API(set_slow_offset_en_ch)(SMI130_GYRO_Z_AXIS, 0);
1187 }
1188
1189 return count;
1190}
1191
1192static ssize_t smi_gyro_show_selftest(struct device *dev,
1193 struct device_attribute *attr, char *buf)
1194{
1195 int err;
1196 unsigned char selftest;
1197 SMI_GYRO_CALL_API(selftest)(&selftest);
1198 err = snprintf(buf, 16, "%d\n", selftest);
1199 return err;
1200}
1201
1202static ssize_t smi_gyro_show_sleepdur(struct device *dev,
1203 struct device_attribute *attr, char *buf)
1204{
1205 int err;
1206 unsigned char sleepdur;
1207 SMI_GYRO_CALL_API(get_sleepdur)(&sleepdur);
1208 err = snprintf(buf, 16, "%d\n", sleepdur);
1209 return err;
1210}
1211
1212static ssize_t smi_gyro_store_sleepdur(struct device *dev,
1213 struct device_attribute *attr,
1214 const char *buf, size_t count)
1215{
1216 int err;
1217 unsigned long sleepdur;
1218 err = kstrtoul(buf, 10, &sleepdur);
1219 if (err)
1220 return err;
1221 SMI_GYRO_CALL_API(set_sleepdur)(sleepdur);
1222 return count;
1223}
1224
1225static ssize_t smi_gyro_show_autosleepdur(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1227{
1228 int err;
1229 unsigned char autosleepdur;
1230 SMI_GYRO_CALL_API(get_autosleepdur)(&autosleepdur);
1231 err = snprintf(buf, 16, "%d\n", autosleepdur);
1232 return err;
1233}
1234
1235static ssize_t smi_gyro_store_autosleepdur(struct device *dev,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
1238{
1239 int err;
1240 unsigned long autosleepdur;
1241 unsigned char bandwidth;
1242 err = kstrtoul(buf, 10, &autosleepdur);
1243 if (err)
1244 return err;
1245 SMI_GYRO_CALL_API(get_bw)(&bandwidth);
1246 SMI_GYRO_CALL_API(set_autosleepdur)(autosleepdur, bandwidth);
1247 return count;
1248}
1249
1250static ssize_t smi_gyro_show_place(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1252{
1253 struct input_dev *input = to_input_dev(dev);
1254 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1255 int place = BOSCH_SENSOR_PLACE_UNKNOWN;
1256
1257 if (NULL != client_data->bosch_pd)
1258 place = client_data->bosch_pd->place;
1259
1260 return snprintf(buf, 16, "%d\n", place);
1261}
1262
1263
1264#ifdef SMI_GYRO_DEBUG
1265static ssize_t smi_gyro_store_softreset(struct device *dev,
1266 struct device_attribute *attr,
1267 const char *buf, size_t count)
1268{
1269 int err;
1270 unsigned long softreset;
1271 err = kstrtoul(buf, 10, &softreset);
1272 if (err)
1273 return err;
1274 SMI_GYRO_CALL_API(set_soft_reset)();
1275 return count;
1276}
1277
1278static ssize_t smi_gyro_show_dumpreg(struct device *dev,
1279 struct device_attribute *attr, char *buf)
1280{
1281 size_t count = 0;
1282 u8 reg[0x40];
1283 int i;
1284 struct input_dev *input = to_input_dev(dev);
1285 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1286
1287 for (i = 0; i < 0x40; i++) {
1288 smi_gyro_i2c_read(client_data->client, i, reg+i, 1);
1289
1290 count += snprintf(&buf[count], 48, "0x%x: 0x%x\n", i, reg[i]);
1291 }
1292 return count;
1293}
1294#endif
1295
1296#ifdef SMI_GYRO_USE_FIFO
1297static ssize_t smi_gyro_show_fifo_mode(struct device *dev,
1298 struct device_attribute *attr, char *buf)
1299{
1300 int err;
1301 unsigned char fifo_mode;
1302 SMI_GYRO_CALL_API(get_fifo_mode)(&fifo_mode);
1303 err = snprintf(buf, 16, "%d\n", fifo_mode);
1304 return err;
1305}
1306
1307static ssize_t smi_gyro_store_fifo_mode(struct device *dev,
1308 struct device_attribute *attr,
1309 const char *buf, size_t count)
1310{
1311 int err;
1312 unsigned long fifo_mode;
1313 err = kstrtoul(buf, 10, &fifo_mode);
1314 if (err)
1315 return err;
1316 SMI_GYRO_CALL_API(set_fifo_mode)(fifo_mode);
1317 return count;
1318}
1319
1320static ssize_t smi_gyro_show_fifo_framecount(struct device *dev,
1321 struct device_attribute *attr, char *buf)
1322{
1323 int err;
1324 unsigned char fifo_framecount;
1325 SMI_GYRO_CALL_API(get_fifo_framecount)(&fifo_framecount);
1326 err = snprintf(buf, 32, "%d\n", fifo_framecount);
1327 return err;
1328}
1329
1330static ssize_t smi_gyro_store_fifo_framecount(struct device *dev,
1331 struct device_attribute *attr,
1332 const char *buf, size_t count)
1333{
1334 unsigned long data;
1335 int error;
1336 struct input_dev *input = to_input_dev(dev);
1337 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1338 error = kstrtoul(buf, 10, &data);
1339 if (error)
1340 return error;
1341 client_data->fifo_count = (unsigned int) data;
1342
1343 return count;
1344}
1345
1346static ssize_t smi_gyro_show_fifo_overrun(struct device *dev,
1347 struct device_attribute *attr, char *buf)
1348{
1349 int err;
1350 unsigned char fifo_overrun;
1351 SMI_GYRO_CALL_API(get_fifo_overrun)(&fifo_overrun);
1352 err = snprintf(buf, 16, "%d\n", fifo_overrun);
1353 return err;
1354}
1355
1356static ssize_t smi_gyro_show_fifo_data_frame(struct device *dev,
1357 struct device_attribute *attr, char *buf)
1358{
1359 unsigned char f_len = 0;
1360 unsigned char fifo_framecount;
1361 struct input_dev *input = to_input_dev(dev);
1362 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1363
1364 if (client_data->fifo_datasel)
1365 /*Select one axis data output for every fifo frame*/
1366 f_len = 2;
1367 else
1368 /*Select X Y Z axis data output for every fifo frame*/
1369 f_len = 6;
1370
1371 if (SMI_GYRO_CALL_API(get_fifo_framecount)(&fifo_framecount) < 0) {
1372 PERR("bm160_get_fifo_framecount err\n");
1373 return -EINVAL;
1374 }
1375 if (fifo_framecount == 0)
1376 return 0;
1377
1378 smi_gyro_i2c_burst_read(client_data->client, SMI130_GYRO_FIFO_DATA_ADDR,
1379 buf, fifo_framecount * f_len);
1380 return fifo_framecount * f_len;
1381}
1382
1383/*!
1384 * @brief show fifo_data_sel axis definition(Android definition, not sensor HW reg).
1385 * 0--> x, y, z axis fifo data for every frame
1386 * 1--> only x axis fifo data for every frame
1387 * 2--> only y axis fifo data for every frame
1388 * 3--> only z axis fifo data for every frame
1389 */
1390static ssize_t smi_gyro_show_fifo_data_sel(struct device *dev,
1391 struct device_attribute *attr, char *buf)
1392{
1393 int err;
1394 unsigned char fifo_data_sel;
1395 struct i2c_client *client = to_i2c_client(dev);
1396 struct smi_gyro_client_data *client_data = i2c_get_clientdata(client);
1397 signed char place = BOSCH_SENSOR_PLACE_UNKNOWN;
1398
1399 SMI_GYRO_CALL_API(get_fifo_data_sel)(&fifo_data_sel);
1400
1401 /*remapping fifo_dat_sel if define virtual place in BSP files*/
1402 if ((NULL != client_data->bosch_pd) &&
1403 (BOSCH_SENSOR_PLACE_UNKNOWN != client_data->bosch_pd->place)) {
1404 place = client_data->bosch_pd->place;
1405 /* sensor with place 0 needs not to be remapped */
1406 if ((place > 0) && (place < MAX_AXIS_REMAP_TAB_SZ)) {
1407 if (SMI130_GYRO_FIFO_DAT_SEL_X == fifo_data_sel)
1408 /* SMI130_GYRO_FIFO_DAT_SEL_X: 1, Y:2, Z:3;
1409 *bosch_axis_remap_tab_dft[i].src_x:0, y:1, z:2
1410 *so we need to +1*/
1411 fifo_data_sel =
1412 bosch_axis_remap_tab_dft[place].src_x + 1;
1413
1414 else if (SMI130_GYRO_FIFO_DAT_SEL_Y == fifo_data_sel)
1415 fifo_data_sel =
1416 bosch_axis_remap_tab_dft[place].src_y + 1;
1417 }
1418
1419 }
1420
1421 err = snprintf(buf, 16, "%d\n", fifo_data_sel);
1422 return err;
1423}
1424
1425/*!
1426 * @brief store fifo_data_sel axis definition(Android definition, not sensor HW reg).
1427 * 0--> x, y, z axis fifo data for every frame
1428 * 1--> only x axis fifo data for every frame
1429 * 2--> only y axis fifo data for every frame
1430 * 3--> only z axis fifo data for every frame
1431 */
1432static ssize_t smi_gyro_store_fifo_data_sel(struct device *dev,
1433 struct device_attribute *attr,
1434 const char *buf, size_t count)
1435
1436{
1437 int err;
1438 unsigned long fifo_data_sel;
1439
1440 struct input_dev *input = to_input_dev(dev);
1441 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1442 signed char place;
1443
1444 err = kstrtoul(buf, 10, &fifo_data_sel);
1445 if (err)
1446 return err;
1447
1448 /*save fifo_data_sel(android axis definition)*/
1449 client_data->fifo_datasel = (unsigned char) fifo_data_sel;
1450
1451 /*remaping fifo_dat_sel if define virtual place*/
1452 if ((NULL != client_data->bosch_pd) &&
1453 (BOSCH_SENSOR_PLACE_UNKNOWN != client_data->bosch_pd->place)) {
1454 place = client_data->bosch_pd->place;
1455 /* sensor with place 0 needs not to be remapped */
1456 if ((place > 0) && (place < MAX_AXIS_REMAP_TAB_SZ)) {
1457 /*Need X Y axis revesal sensor place: P1, P3, P5, P7 */
1458 /* SMI130_GYRO_FIFO_DAT_SEL_X: 1, Y:2, Z:3;
1459 * but bosch_axis_remap_tab_dft[i].src_x:0, y:1, z:2
1460 * so we need to +1*/
1461 if (SMI130_GYRO_FIFO_DAT_SEL_X == fifo_data_sel)
1462 fifo_data_sel =
1463 bosch_axis_remap_tab_dft[place].src_x + 1;
1464
1465 else if (SMI130_GYRO_FIFO_DAT_SEL_Y == fifo_data_sel)
1466 fifo_data_sel =
1467 bosch_axis_remap_tab_dft[place].src_y + 1;
1468 }
1469 }
1470
1471 if (SMI_GYRO_CALL_API(set_fifo_data_sel)(fifo_data_sel) < 0)
1472 return -EINVAL;
1473
1474 return count;
1475}
1476
1477static ssize_t smi_gyro_show_fifo_tag(struct device *dev,
1478 struct device_attribute *attr, char *buf)
1479{
1480 int err;
1481 unsigned char fifo_tag;
1482 SMI_GYRO_CALL_API(get_fifo_tag)(&fifo_tag);
1483 err = snprintf(buf, 16, "%d\n", fifo_tag);
1484 return err;
1485}
1486
1487static ssize_t smi_gyro_store_fifo_tag(struct device *dev,
1488 struct device_attribute *attr,
1489 const char *buf, size_t count)
1490
1491{
1492 int err;
1493 unsigned long fifo_tag;
1494 err = kstrtoul(buf, 10, &fifo_tag);
1495 if (err)
1496 return err;
1497 SMI_GYRO_CALL_API(set_fifo_tag)(fifo_tag);
1498 return count;
1499}
1500#endif
1501
1502static ssize_t smi130_gyro_driver_version_show(struct device *dev
1503 , struct device_attribute *attr, char *buf)
1504{
1505 struct input_dev *input = to_input_dev(dev);
1506 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1507 int ret;
1508
1509 if (client_data == NULL) {
1510 printk(KERN_ERR "Invalid client_data pointer");
1511 return -ENODEV;
1512 }
1513
1514 ret = snprintf(buf, 128, "Driver version: %s\n",
1515 DRIVER_VERSION);
1516 return ret;
1517}
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301518
1519#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
1520static int smi_gyro_read_bootsampl(struct smi_gyro_client_data *client_data,
1521 unsigned long enable_read)
1522{
1523 int i = 0;
1524
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301525 client_data->gyro_buffer_smi130_samples = false;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301526 if (enable_read) {
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301527 for (i = 0; i < client_data->gyro_bufsample_cnt; i++) {
1528 if (client_data->debug_level & 0x08)
1529 PINFO("gyro=%d,x=%d,y=%d,z=%d,sec=%d,ns=%lld\n",
1530 i, client_data->smi130_gyro_samplist[i]->xyz[0],
1531 client_data->smi130_gyro_samplist[i]->xyz[1],
1532 client_data->smi130_gyro_samplist[i]->xyz[2],
1533 client_data->smi130_gyro_samplist[i]->tsec,
1534 client_data->smi130_gyro_samplist[i]->tnsec);
1535 input_report_abs(client_data->gyrobuf_dev, ABS_X,
1536 client_data->smi130_gyro_samplist[i]->xyz[0]);
1537 input_report_abs(client_data->gyrobuf_dev, ABS_Y,
1538 client_data->smi130_gyro_samplist[i]->xyz[1]);
1539 input_report_abs(client_data->gyrobuf_dev, ABS_Z,
1540 client_data->smi130_gyro_samplist[i]->xyz[2]);
1541 input_report_abs(client_data->gyrobuf_dev, ABS_RX,
1542 client_data->smi130_gyro_samplist[i]->tsec);
1543 input_report_abs(client_data->gyrobuf_dev, ABS_RY,
1544 client_data->smi130_gyro_samplist[i]->tnsec);
1545 input_sync(client_data->gyrobuf_dev);
1546 }
1547 } else {
1548 /* clean up */
1549 if (client_data->gyro_bufsample_cnt != 0) {
1550 for (i = 0; i < SMI_GYRO_MAXSAMPLE; i++)
1551 kmem_cache_free(client_data->smi_gyro_cachepool,
1552 client_data->smi130_gyro_samplist[i]);
1553 kmem_cache_destroy(client_data->smi_gyro_cachepool);
1554 client_data->gyro_bufsample_cnt = 0;
1555 }
1556
1557 }
1558 /*SYN_CONFIG indicates end of data*/
1559 input_event(client_data->gyrobuf_dev, EV_SYN, SYN_CONFIG, 0xFFFFFFFF);
1560 input_sync(client_data->gyrobuf_dev);
1561 if (client_data->debug_level & 0x08)
1562 PINFO("End of gyro samples bufsample_cnt=%d\n",
1563 client_data->gyro_bufsample_cnt);
1564 return 0;
1565}
1566static ssize_t read_gyro_boot_sample_show(struct device *dev,
1567 struct device_attribute *attr,
1568 char *buf)
1569{
1570 struct input_dev *input = to_input_dev(dev);
1571 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1572
1573 return snprintf(buf, 16, "%u\n",
1574 client_data->read_gyro_boot_sample);
1575}
1576static ssize_t read_gyro_boot_sample_store(struct device *dev,
1577 struct device_attribute *attr,
1578 const char *buf, size_t count)
1579{
1580 int err;
1581 struct input_dev *input = to_input_dev(dev);
1582 struct smi_gyro_client_data *client_data = input_get_drvdata(input);
1583 unsigned long enable = 0;
1584
1585 err = kstrtoul(buf, 10, &enable);
1586 if (err)
1587 return err;
1588 if (enable > 1) {
1589 PERR("Invalid value of input, input=%ld\n", enable);
1590 return -EINVAL;
1591 }
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301592 mutex_lock(&client_data->gyro_sensor_buff);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301593 err = smi_gyro_read_bootsampl(client_data, enable);
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301594 mutex_unlock(&client_data->gyro_sensor_buff);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301595 if (err)
1596 return err;
1597 client_data->read_gyro_boot_sample = enable;
1598
1599 return count;
1600}
1601#endif
1602
1603
Bosch Sensortecface5922018-08-29 08:08:53 +02001604static DEVICE_ATTR(chip_id, S_IRUSR,
1605 smi_gyro_show_chip_id, NULL);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301606#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
1607static DEVICE_ATTR(read_gyro_boot_sample, 0644,
1608 read_gyro_boot_sample_show, read_gyro_boot_sample_store);
1609#endif
Bosch Sensortecface5922018-08-29 08:08:53 +02001610static DEVICE_ATTR(op_mode, S_IRUGO | S_IWUSR,
1611 smi_gyro_show_op_mode, smi_gyro_store_op_mode);
1612static DEVICE_ATTR(value, S_IRUSR,
1613 smi_gyro_show_value, NULL);
1614static DEVICE_ATTR(range, S_IRUGO | S_IWUSR,
1615 smi_gyro_show_range, smi_gyro_store_range);
1616static DEVICE_ATTR(bandwidth, S_IRUGO | S_IWUSR,
1617 smi_gyro_show_bandwidth, smi_gyro_store_bandwidth);
1618static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1619 smi_gyro_show_enable, smi_gyro_store_enable);
1620static DEVICE_ATTR(delay, S_IRUGO | S_IWUSR,
1621 smi_gyro_show_delay, smi_gyro_store_delay);
1622static DEVICE_ATTR(fastoffset_en, S_IWUSR,
1623 NULL, smi_gyro_store_fastoffset_en);
1624static DEVICE_ATTR(slowoffset_en, S_IWUSR,
1625 NULL, smi_gyro_store_slowoffset_en);
1626static DEVICE_ATTR(selftest, S_IRUGO,
1627 smi_gyro_show_selftest, NULL);
1628static DEVICE_ATTR(sleepdur, S_IRUGO | S_IWUSR,
1629 smi_gyro_show_sleepdur, smi_gyro_store_sleepdur);
1630static DEVICE_ATTR(autosleepdur, S_IRUGO | S_IWUSR,
1631 smi_gyro_show_autosleepdur, smi_gyro_store_autosleepdur);
1632static DEVICE_ATTR(place, S_IRUSR,
1633 smi_gyro_show_place, NULL);
1634static DEVICE_ATTR(enable_timer, S_IRUGO | S_IWUSR,
1635 smi_gyro_show_enable_timer, smi_gyro_store_enable_timer);
1636static DEVICE_ATTR(debug_level, S_IRUGO | S_IWUSR,
1637 smi130_gyro_show_debug_level, smi130_gyro_store_debug_level);
1638static DEVICE_ATTR(driver_version, S_IRUSR,
1639 smi130_gyro_driver_version_show, NULL);
1640#ifdef SMI_GYRO_DEBUG
1641static DEVICE_ATTR(softreset, S_IWUSR,
1642 NULL, smi_gyro_store_softreset);
1643static DEVICE_ATTR(regdump, S_IRUSR,
1644 smi_gyro_show_dumpreg, NULL);
1645#endif
1646#ifdef SMI_GYRO_USE_FIFO
1647static DEVICE_ATTR(fifo_mode, S_IRUGO | S_IWUSR,
1648 smi_gyro_show_fifo_mode, smi_gyro_store_fifo_mode);
1649static DEVICE_ATTR(fifo_framecount, S_IRUGO | S_IWUSR,
1650 smi_gyro_show_fifo_framecount, smi_gyro_store_fifo_framecount);
1651static DEVICE_ATTR(fifo_overrun, S_IRUGO,
1652 smi_gyro_show_fifo_overrun, NULL);
1653static DEVICE_ATTR(fifo_data_frame, S_IRUSR,
1654 smi_gyro_show_fifo_data_frame, NULL);
1655static DEVICE_ATTR(fifo_data_sel, S_IRUGO | S_IWUSR,
1656 smi_gyro_show_fifo_data_sel, smi_gyro_store_fifo_data_sel);
1657static DEVICE_ATTR(fifo_tag, S_IRUGO | S_IWUSR,
1658 smi_gyro_show_fifo_tag, smi_gyro_store_fifo_tag);
1659#endif
1660
1661static struct attribute *smi_gyro_attributes[] = {
1662 &dev_attr_chip_id.attr,
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301663#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
1664 &dev_attr_read_gyro_boot_sample.attr,
1665#endif
Bosch Sensortecface5922018-08-29 08:08:53 +02001666 &dev_attr_op_mode.attr,
1667 &dev_attr_value.attr,
1668 &dev_attr_range.attr,
1669 &dev_attr_bandwidth.attr,
1670 &dev_attr_enable.attr,
1671 &dev_attr_delay.attr,
1672 &dev_attr_fastoffset_en.attr,
1673 &dev_attr_slowoffset_en.attr,
1674 &dev_attr_selftest.attr,
1675 &dev_attr_sleepdur.attr,
1676 &dev_attr_autosleepdur.attr,
1677 &dev_attr_place.attr,
1678 &dev_attr_enable_timer.attr,
1679 &dev_attr_debug_level.attr,
1680 &dev_attr_driver_version.attr,
1681#ifdef SMI_GYRO_DEBUG
1682 &dev_attr_softreset.attr,
1683 &dev_attr_regdump.attr,
1684#endif
1685#ifdef SMI_GYRO_USE_FIFO
1686 &dev_attr_fifo_mode.attr,
1687 &dev_attr_fifo_framecount.attr,
1688 &dev_attr_fifo_overrun.attr,
1689 &dev_attr_fifo_data_frame.attr,
1690 &dev_attr_fifo_data_sel.attr,
1691 &dev_attr_fifo_tag.attr,
1692#endif
1693 NULL
1694};
1695
1696static struct attribute_group smi_gyro_attribute_group = {
1697 .attrs = smi_gyro_attributes
1698};
1699
1700
1701static int smi_gyro_input_init(struct smi_gyro_client_data *client_data)
1702{
1703 struct input_dev *dev;
1704 int err = 0;
1705
1706 dev = input_allocate_device();
1707 if (NULL == dev)
1708 return -ENOMEM;
1709
1710 dev->name = SENSOR_NAME;
1711 dev->id.bustype = BUS_I2C;
1712
1713 input_set_capability(dev, EV_ABS, ABS_MISC);
1714 input_set_abs_params(dev, ABS_X, SMI_GYRO_VALUE_MIN, SMI_GYRO_VALUE_MAX, 0, 0);
1715 input_set_abs_params(dev, ABS_Y, SMI_GYRO_VALUE_MIN, SMI_GYRO_VALUE_MAX, 0, 0);
1716 input_set_abs_params(dev, ABS_Z, SMI_GYRO_VALUE_MIN, SMI_GYRO_VALUE_MAX, 0, 0);
1717 input_set_capability(dev, EV_MSC, MSC_GESTURE);
1718 input_set_capability(dev, EV_MSC, MSC_RAW);
1719 input_set_capability(dev, EV_MSC, MSC_SCAN);
1720 input_set_capability(dev, EV_MSC, MSC_TIME);
1721 input_set_drvdata(dev, client_data);
1722
1723 err = input_register_device(dev);
1724 if (err < 0) {
1725 input_free_device(dev);
1726 return err;
1727 }
1728 client_data->input = dev;
1729
1730 return 0;
1731}
1732
1733static void smi_gyro_input_destroy(struct smi_gyro_client_data *client_data)
1734{
1735 struct input_dev *dev = client_data->input;
1736
1737 input_unregister_device(dev);
1738 input_free_device(dev);
1739}
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301740#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
1741static void store_gyro_boot_sample(struct smi_gyro_client_data *client_data,
1742 int x, int y, int z, struct timespec ts)
1743{
1744 if (false == client_data->gyro_buffer_smi130_samples)
1745 return;
1746 if (ts.tv_sec < client_data->max_buffer_time) {
1747 if (client_data->gyro_bufsample_cnt < SMI_GYRO_MAXSAMPLE) {
1748 client_data->smi130_gyro_samplist[client_data->
1749 gyro_bufsample_cnt]->xyz[0] = x;
1750 client_data->smi130_gyro_samplist[client_data->
1751 gyro_bufsample_cnt]->xyz[1] = y;
1752 client_data->smi130_gyro_samplist[client_data->
1753 gyro_bufsample_cnt]->xyz[2] = z;
1754 client_data->smi130_gyro_samplist[client_data->
1755 gyro_bufsample_cnt]->tsec = ts.tv_sec;
1756 client_data->smi130_gyro_samplist[client_data->
1757 gyro_bufsample_cnt]->tnsec = ts.tv_nsec;
1758 client_data->gyro_bufsample_cnt++;
1759 }
1760 } else {
1761 PINFO("End of GYRO buffering %d",
1762 client_data->gyro_bufsample_cnt);
1763 client_data->gyro_buffer_smi130_samples = false;
Puneet Yatnalab29efa2019-08-05 13:13:31 +05301764 if (client_data->gyro_enable == false) {
1765 smi130_gyro_set_mode(SMI130_GYRO_MODE_SUSPEND);
1766 smi130_gyro_delay(5);
1767 }
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301768 }
1769}
1770#else
1771static void store_gyro_boot_sample(struct smi_gyro_client_data *client_data,
1772 int x, int y, int z, struct timespec ts)
1773{
1774}
1775#endif
1776
1777
1778#ifdef CONFIG_ENABLE_SMI_ACC_GYRO_BUFFERING
1779static int smi130_gyro_early_buff_init(struct smi_gyro_client_data *client_data)
1780{
1781 int i = 0, err = 0;
1782
1783 client_data->gyro_bufsample_cnt = 0;
1784 client_data->report_evt_cnt = 5;
1785 client_data->max_buffer_time = 40;
1786
1787 client_data->smi_gyro_cachepool = kmem_cache_create("gyro_sensor_sample"
1788 , sizeof(struct smi_gyro_sample), 0,
1789 SLAB_HWCACHE_ALIGN, NULL);
1790 if (!client_data->smi_gyro_cachepool) {
1791 PERR("smi_gyro_cachepool cache create failed\n");
1792 err = -ENOMEM;
puneet597e4f12018-11-06 14:32:16 +05301793 return 0;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301794 }
1795
1796 for (i = 0; i < SMI_GYRO_MAXSAMPLE; i++) {
1797 client_data->smi130_gyro_samplist[i] =
1798 kmem_cache_alloc(client_data->smi_gyro_cachepool,
1799 GFP_KERNEL);
1800 if (!client_data->smi130_gyro_samplist[i]) {
1801 err = -ENOMEM;
puneet597e4f12018-11-06 14:32:16 +05301802 goto clean_exit1;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301803 }
1804 }
1805
1806
1807 client_data->gyrobuf_dev = input_allocate_device();
1808 if (!client_data->gyrobuf_dev) {
1809 err = -ENOMEM;
1810 PERR("input device allocation failed\n");
puneet597e4f12018-11-06 14:32:16 +05301811 goto clean_exit1;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301812 }
1813 client_data->gyrobuf_dev->name = "smi130_gyrobuf";
1814 client_data->gyrobuf_dev->id.bustype = BUS_I2C;
1815 input_set_events_per_packet(client_data->gyrobuf_dev,
1816 client_data->report_evt_cnt * SMI_GYRO_MAXSAMPLE);
1817 set_bit(EV_ABS, client_data->gyrobuf_dev->evbit);
1818 input_set_abs_params(client_data->gyrobuf_dev, ABS_X,
1819 -G_MAX, G_MAX, 0, 0);
1820 input_set_abs_params(client_data->gyrobuf_dev, ABS_Y,
1821 -G_MAX, G_MAX, 0, 0);
1822 input_set_abs_params(client_data->gyrobuf_dev, ABS_Z,
1823 -G_MAX, G_MAX, 0, 0);
1824 input_set_abs_params(client_data->gyrobuf_dev, ABS_RX,
1825 -G_MAX, G_MAX, 0, 0);
1826 input_set_abs_params(client_data->gyrobuf_dev, ABS_RY,
1827 -G_MAX, G_MAX, 0, 0);
1828 err = input_register_device(client_data->gyrobuf_dev);
1829 if (err) {
1830 PERR("unable to register input device %s\n",
1831 client_data->gyrobuf_dev->name);
puneet597e4f12018-11-06 14:32:16 +05301832 goto clean_exit2;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301833 }
1834
1835 client_data->gyro_buffer_smi130_samples = true;
Puneet Yatnalab29efa2019-08-05 13:13:31 +05301836 client_data->gyro_enable = false;
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301837
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301838 mutex_init(&client_data->gyro_sensor_buff);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301839 smi130_gyro_set_mode(SMI130_GYRO_MODE_NORMAL);
1840 smi130_gyro_delay(5);
1841
1842 smi130_gyro_set_bw(5);
1843 smi130_gyro_delay(5);
1844
1845 smi130_gyro_set_range_reg(4);
1846 smi130_gyro_delay(5);
1847
1848 smi130_gyro_set_mode(SMI130_GYRO_MODE_NORMAL);
1849 smi130_gyro_delay(5);
1850
1851 smi130_gyro_set_range_reg(4);
1852 smi130_gyro_delay(5);
1853
1854 smi130_gyro_set_data_en(SMI130_GYRO_ENABLE);
1855
1856 return 1;
1857
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301858clean_exit2:
puneet597e4f12018-11-06 14:32:16 +05301859 input_free_device(client_data->gyrobuf_dev);
1860clean_exit1:
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301861 for (i = 0; i < SMI_GYRO_MAXSAMPLE; i++)
1862 kmem_cache_free(client_data->smi_gyro_cachepool,
1863 client_data->smi130_gyro_samplist[i]);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301864 kmem_cache_destroy(client_data->smi_gyro_cachepool);
1865 return 0;
1866}
1867
1868static void smi130_gyro_input_cleanup(struct smi_gyro_client_data *client_data)
1869{
1870 int i = 0;
1871
1872 input_unregister_device(client_data->gyrobuf_dev);
1873 input_free_device(client_data->gyrobuf_dev);
1874 for (i = 0; i < SMI_GYRO_MAXSAMPLE; i++)
1875 kmem_cache_free(client_data->smi_gyro_cachepool,
1876 client_data->smi130_gyro_samplist[i]);
1877 kmem_cache_destroy(client_data->smi_gyro_cachepool);
1878}
1879
1880static int smi130_enable_int1(void)
1881{
1882 return smi130_gyro_set_data_en(SMI130_GYRO_DISABLE);
1883}
1884#else
1885static int smi130_gyro_early_buff_init(struct smi_gyro_client_data *client_data)
1886{
1887 return 1;
1888}
1889static void smi130_gyro_input_cleanup(struct smi_gyro_client_data *client_data)
1890{
1891}
1892static int smi130_enable_int1(void)
1893{
1894 return smi130_gyro_set_data_en(SMI130_GYRO_ENABLE);
1895}
1896#endif
1897
Bosch Sensortecface5922018-08-29 08:08:53 +02001898
1899#if defined(SMI130_GYRO_ENABLE_INT1) || defined(SMI130_GYRO_ENABLE_INT2)
puneet597e4f12018-11-06 14:32:16 +05301900static irqreturn_t smi130_gyro_irq_work_func(int irq, void *handle)
Bosch Sensortecface5922018-08-29 08:08:53 +02001901{
puneet597e4f12018-11-06 14:32:16 +05301902 struct smi_gyro_client_data *client_data = handle;
Bosch Sensortecface5922018-08-29 08:08:53 +02001903 struct smi130_gyro_data_t gyro_data;
1904 struct timespec ts;
1905 ts = ns_to_timespec(client_data->timestamp);
1906
1907 SMI_GYRO_CALL_API(get_dataXYZ)(&gyro_data);
1908 /*remapping for SMI130_GYRO sensor*/
1909 smi130_gyro_remap_sensor_data(&gyro_data, client_data);
1910 input_event(client_data->input, EV_MSC, MSC_TIME,
1911 ts.tv_sec);
1912 input_event(client_data->input, EV_MSC, MSC_TIME,
1913 ts.tv_nsec);
1914 input_event(client_data->input, EV_MSC,
1915 MSC_GESTURE, gyro_data.datax);
1916 input_event(client_data->input, EV_MSC,
1917 MSC_RAW, gyro_data.datay);
1918 input_event(client_data->input, EV_MSC,
1919 MSC_SCAN, gyro_data.dataz);
1920 input_sync(client_data->input);
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301921 mutex_lock(&client_data->gyro_sensor_buff);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05301922 store_gyro_boot_sample(client_data, gyro_data.datax,
1923 gyro_data.datay, gyro_data.dataz, ts);
Puneet Yatnal3cbaa6f2020-01-08 18:09:41 +05301924 mutex_unlock(&client_data->gyro_sensor_buff);
puneet597e4f12018-11-06 14:32:16 +05301925 return IRQ_HANDLED;
Bosch Sensortecface5922018-08-29 08:08:53 +02001926}
1927
1928static irqreturn_t smi_gyro_irq_handler(int irq, void *handle)
1929{
1930 struct smi_gyro_client_data *client_data = handle;
1931 client_data->timestamp= smi130_gyro_get_alarm_timestamp();
puneet597e4f12018-11-06 14:32:16 +05301932 return IRQ_WAKE_THREAD;
Bosch Sensortecface5922018-08-29 08:08:53 +02001933}
1934#endif
1935static int smi_gyro_probe(struct i2c_client *client, const struct i2c_device_id *id)
1936{
1937 int err = 0;
1938 struct smi_gyro_client_data *client_data = NULL;
1939 PINFO("function entrance");
1940
1941 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1942 PERR("i2c_check_functionality error!");
1943 err = -EIO;
1944 goto exit_err_clean;
1945 }
1946
1947 if (NULL == smi_gyro_client) {
1948 smi_gyro_client = client;
1949 } else {
1950 PERR("this driver does not support multiple clients");
1951 err = -EINVAL;
1952 goto exit_err_clean;
1953 }
1954
1955 /* check chip id */
1956 err = smi_gyro_check_chip_id(client);
1957 if (!err) {
1958 PINFO("Bosch Sensortec Device %s detected", SENSOR_NAME);
1959 } else {
1960 PERR("Bosch Sensortec Device not found, chip id mismatch");
1961 err = -1;
1962 goto exit_err_clean;
1963 }
1964
1965 /* do soft reset */
1966 smi130_gyro_delay(5);
1967 err = smi_gyro_set_soft_reset(client);
1968 if (err < 0) {
1969 PERR("erro soft reset!\n");
1970 err = -EINVAL;
1971 goto exit_err_clean;
1972 }
1973 smi130_gyro_delay(30);
1974
1975
1976 client_data = kzalloc(sizeof(struct smi_gyro_client_data), GFP_KERNEL);
1977 if (NULL == client_data) {
1978 PERR("no memory available");
1979 err = -ENOMEM;
1980 goto exit_err_clean;
1981 }
1982
1983 i2c_set_clientdata(client, client_data);
1984 client_data->client = client;
1985
1986 mutex_init(&client_data->mutex_op_mode);
1987 mutex_init(&client_data->mutex_enable);
1988
1989 /* input device init */
1990 err = smi_gyro_input_init(client_data);
1991 if (err < 0)
1992 goto exit_err_clean;
1993
1994 /* sysfs node creation */
1995 err = sysfs_create_group(&client_data->input->dev.kobj,
1996 &smi_gyro_attribute_group);
1997
1998 if (err < 0)
1999 goto exit_err_sysfs;
2000
2001 if (NULL != client->dev.platform_data) {
2002 client_data->bosch_pd = kzalloc(sizeof(*client_data->bosch_pd),
2003 GFP_KERNEL);
2004
2005 if (NULL != client_data->bosch_pd) {
2006 memcpy(client_data->bosch_pd, client->dev.platform_data,
2007 sizeof(*client_data->bosch_pd));
2008 PINFO("%s sensor driver set place: p%d",
2009 SENSOR_NAME,
2010 client_data->bosch_pd->place);
2011 }
2012 }
2013
2014 /* workqueue init */
2015 INIT_DELAYED_WORK(&client_data->work, smi_gyro_work_func);
2016 atomic_set(&client_data->delay, SMI_GYRO_DELAY_DEFAULT);
2017
2018 /* h/w init */
2019 client_data->device.bus_read = smi_gyro_i2c_read_wrapper;
2020 client_data->device.bus_write = smi_gyro_i2c_write_wrapper;
2021 client_data->device.delay_msec = smi130_gyro_delay;
2022 SMI_GYRO_CALL_API(init)(&client_data->device);
2023
2024 smi_gyro_dump_reg(client);
2025
2026 client_data->enable = 0;
2027 client_data->fifo_datasel = 0;
2028 client_data->fifo_count = 0;
2029
2030 /*workqueue init*/
2031 INIT_WORK(&client_data->report_data_work,
2032 smi130_gyro_work_func);
2033 reportdata_wq = create_singlethread_workqueue("smi130_gyro_wq");
2034 if (NULL == reportdata_wq)
2035 PERR("fail to create the reportdta_wq %d", -ENOMEM);
2036 hrtimer_init(&client_data->timer, CLOCK_MONOTONIC,
2037 HRTIMER_MODE_REL);
2038 client_data->timer.function = reportdata_timer_fun;
2039 client_data->work_delay_kt = ns_to_ktime(10000000);
2040 client_data->is_timer_running = 0;
2041 client_data->time_odr = 500000;
2042#ifdef SMI130_GYRO_ENABLE_INT1
2043 err = SMI_GYRO_CALL_API(set_mode)(SMI130_GYRO_MODE_NORMAL);
2044 smi130_gyro_delay(5);
2045 /*config the interrupt and map the interrupt*/
2046 /*high level trigger*/
2047 err += smi130_gyro_set_int_lvl(SMI130_GYRO_INT1_DATA, 1);
2048 smi130_gyro_delay(5);
2049 err += smi130_gyro_set_int_od(SMI130_GYRO_INT1, 0);
2050 smi130_gyro_delay(5);
2051 err += smi130_gyro_set_int_data(SMI130_GYRO_INT1_DATA, SMI130_GYRO_ENABLE);
2052 smi130_gyro_delay(5);
Puneet Yatnalcfd94842019-04-16 12:11:05 +05302053 err += smi130_enable_int1();
Bosch Sensortecface5922018-08-29 08:08:53 +02002054 smi130_gyro_delay(5);
2055 /*default odr is 100HZ*/
2056 err += SMI_GYRO_CALL_API(set_bw)(7);
2057 smi130_gyro_delay(5);
2058 if (err)
2059 PERR("config sensor data ready interrupt failed");
2060#endif
2061#ifdef SMI130_GYRO_ENABLE_INT2
2062 err = SMI_GYRO_CALL_API(set_mode)(SMI130_GYRO_MODE_NORMAL);
2063 /*config the interrupt and map the interrupt*/
2064 /*high level trigger*/
2065 err += smi130_gyro_set_int_lvl(SMI130_GYRO_INT2_DATA, 1);
2066 smi130_gyro_delay(3);
2067 err += smi130_gyro_set_int_od(SMI130_GYRO_INT2, 0);
2068 smi130_gyro_delay(5);
2069 err += smi130_gyro_set_int_data(SMI130_GYRO_INT2_DATA, SMI130_GYRO_ENABLE);
2070 smi130_gyro_delay(3);
2071 err += smi130_gyro_set_data_en(SMI130_GYRO_ENABLE);
2072 /*default odr is 100HZ*/
2073 err += SMI_GYRO_CALL_API(set_bw)(7);
2074 smi130_gyro_delay(5);
2075 if (err)
2076 PERR("config sensor data ready interrupt failed");
2077#endif
2078 err += SMI_GYRO_CALL_API(set_mode)(
2079 SMI_GYRO_VAL_NAME(MODE_SUSPEND));
2080 if (err < 0)
2081 goto exit_err_sysfs;
2082#ifdef CONFIG_HAS_EARLYSUSPEND
2083 client_data->early_suspend_handler.suspend = smi_gyro_early_suspend;
2084 client_data->early_suspend_handler.resume = smi_gyro_late_resume;
2085 register_early_suspend(&client_data->early_suspend_handler);
2086#endif
2087#if defined(SMI130_GYRO_ENABLE_INT1) || defined(SMI130_GYRO_ENABLE_INT2)
2088 client_data->gpio_pin = of_get_named_gpio_flags(
2089 client->dev.of_node,
2090 "smi130_gyro,gpio_irq", 0, NULL);
2091 PDEBUG("smi130_gyro qpio number:%d\n", client_data->gpio_pin);
2092 err = gpio_request_one(client_data->gpio_pin,
2093 GPIOF_IN, "bm160_interrupt");
2094 if (err < 0) {
2095 PDEBUG("requestgpio failed\n");
2096 client_data->gpio_pin = 0;
2097 }
2098 if (client_data->gpio_pin != 0) {
2099 err = gpio_direction_input(client_data->gpio_pin);
2100 if (err < 0) {
2101 PDEBUG("request failed\n");
2102 }
2103 client_data->IRQ = gpio_to_irq(client_data->gpio_pin);
puneet597e4f12018-11-06 14:32:16 +05302104 err = request_threaded_irq(client_data->IRQ,
2105 smi_gyro_irq_handler, smi130_gyro_irq_work_func,
2106 IRQF_TRIGGER_RISING, SENSOR_NAME, client_data);
Bosch Sensortecface5922018-08-29 08:08:53 +02002107 if (err < 0)
2108 PDEBUG("request handle failed\n");
2109 }
Bosch Sensortecface5922018-08-29 08:08:53 +02002110#endif
Puneet Yatnalcfd94842019-04-16 12:11:05 +05302111
2112 err = smi130_gyro_early_buff_init(client_data);
2113 if (!err)
2114 return err;
2115
Bosch Sensortecface5922018-08-29 08:08:53 +02002116 PINFO("sensor %s probed successfully", SENSOR_NAME);
2117
2118 dev_dbg(&client->dev,
2119 "i2c_client: %p client_data: %p i2c_device: %p input: %p",
2120 client, client_data, &client->dev, client_data->input);
2121
2122 return 0;
2123
2124exit_err_sysfs:
2125 if (err)
2126 smi_gyro_input_destroy(client_data);
2127
2128exit_err_clean:
2129 if (err) {
2130 if (client_data != NULL) {
2131 kfree(client_data);
2132 client_data = NULL;
2133 }
2134
2135 smi_gyro_client = NULL;
2136 }
2137
2138 return err;
2139}
2140
puneet88e66bc2018-12-07 10:44:31 +05302141#ifdef CONFIG_HAS_EARLYSUSPEND
Bosch Sensortecface5922018-08-29 08:08:53 +02002142static int smi_gyro_pre_suspend(struct i2c_client *client)
2143{
2144 int err = 0;
2145 struct smi_gyro_client_data *client_data =
2146 (struct smi_gyro_client_data *)i2c_get_clientdata(client);
2147 PINFO("function entrance");
2148
2149 mutex_lock(&client_data->mutex_enable);
2150 if (client_data->enable) {
2151 cancel_delayed_work_sync(&client_data->work);
2152 PINFO("cancel work");
2153 }
2154 mutex_unlock(&client_data->mutex_enable);
2155 if (client_data->is_timer_running) {
2156 hrtimer_cancel(&client_data->timer);
2157 client_data->base_time = 0;
2158 client_data->timestamp = 0;
2159 client_data->fifo_time = 0;
2160 client_data->gyro_count = 0;
2161 }
2162 return err;
2163}
2164
2165static int smi_gyro_post_resume(struct i2c_client *client)
2166{
2167 int err = 0;
2168 struct smi_gyro_client_data *client_data =
2169 (struct smi_gyro_client_data *)i2c_get_clientdata(client);
2170
2171 PINFO("function entrance");
2172 mutex_lock(&client_data->mutex_enable);
2173 if (client_data->enable) {
2174 schedule_delayed_work(&client_data->work,
2175 msecs_to_jiffies(
2176 atomic_read(&client_data->delay)));
2177 }
2178 mutex_unlock(&client_data->mutex_enable);
2179 if (client_data->is_timer_running) {
2180 hrtimer_start(&client_data->timer,
2181 ns_to_ktime(client_data->time_odr),
2182 HRTIMER_MODE_REL);
2183 client_data->base_time = 0;
2184 client_data->timestamp = 0;
2185 client_data->is_timer_running = 1;
2186 client_data->gyro_count = 0;
2187 }
2188 return err;
2189}
2190
Bosch Sensortecface5922018-08-29 08:08:53 +02002191static void smi_gyro_early_suspend(struct early_suspend *handler)
2192{
2193 int err = 0;
2194 struct smi_gyro_client_data *client_data =
2195 (struct smi_gyro_client_data *)container_of(handler,
2196 struct smi_gyro_client_data, early_suspend_handler);
2197 struct i2c_client *client = client_data->client;
2198
2199 PINFO("function entrance");
2200
2201 mutex_lock(&client_data->mutex_op_mode);
2202 if (client_data->enable) {
2203 err = smi_gyro_pre_suspend(client);
2204 err = SMI_GYRO_CALL_API(set_mode)(
2205 SMI_GYRO_VAL_NAME(MODE_SUSPEND));
2206 }
2207 mutex_unlock(&client_data->mutex_op_mode);
2208}
2209
2210static void smi_gyro_late_resume(struct early_suspend *handler)
2211{
2212
2213 int err = 0;
2214 struct smi_gyro_client_data *client_data =
2215 (struct smi_gyro_client_data *)container_of(handler,
2216 struct smi_gyro_client_data, early_suspend_handler);
2217 struct i2c_client *client = client_data->client;
2218
2219 PINFO("function entrance");
2220
2221 mutex_lock(&client_data->mutex_op_mode);
2222
2223 if (client_data->enable)
2224 err = SMI_GYRO_CALL_API(set_mode)(SMI_GYRO_VAL_NAME(MODE_NORMAL));
2225
2226 /* post resume operation */
2227 smi_gyro_post_resume(client);
2228
2229 mutex_unlock(&client_data->mutex_op_mode);
2230}
Bosch Sensortecface5922018-08-29 08:08:53 +02002231#endif
2232
2233void smi_gyro_shutdown(struct i2c_client *client)
2234{
2235 struct smi_gyro_client_data *client_data =
2236 (struct smi_gyro_client_data *)i2c_get_clientdata(client);
2237
2238 mutex_lock(&client_data->mutex_op_mode);
2239 SMI_GYRO_CALL_API(set_mode)(
2240 SMI_GYRO_VAL_NAME(MODE_DEEPSUSPEND));
2241 mutex_unlock(&client_data->mutex_op_mode);
2242}
2243
2244static int smi_gyro_remove(struct i2c_client *client)
2245{
2246 int err = 0;
2247 u8 op_mode;
2248
2249 struct smi_gyro_client_data *client_data =
2250 (struct smi_gyro_client_data *)i2c_get_clientdata(client);
2251
2252 if (NULL != client_data) {
2253#ifdef CONFIG_HAS_EARLYSUSPEND
2254 unregister_early_suspend(&client_data->early_suspend_handler);
2255#endif
Puneet Yatnalcfd94842019-04-16 12:11:05 +05302256 smi130_gyro_input_cleanup(client_data);
Bosch Sensortecface5922018-08-29 08:08:53 +02002257 mutex_lock(&client_data->mutex_op_mode);
2258 SMI_GYRO_CALL_API(get_mode)(&op_mode);
2259 if (SMI_GYRO_VAL_NAME(MODE_NORMAL) == op_mode) {
2260 cancel_delayed_work_sync(&client_data->work);
2261 PINFO("cancel work");
2262 }
2263 mutex_unlock(&client_data->mutex_op_mode);
2264
2265 err = SMI_GYRO_CALL_API(set_mode)(
2266 SMI_GYRO_VAL_NAME(MODE_SUSPEND));
2267 smi130_gyro_delay(SMI_GYRO_I2C_WRITE_DELAY_TIME);
2268
2269 sysfs_remove_group(&client_data->input->dev.kobj,
2270 &smi_gyro_attribute_group);
2271 smi_gyro_input_destroy(client_data);
2272 kfree(client_data);
Bosch Sensortecface5922018-08-29 08:08:53 +02002273 smi_gyro_client = NULL;
2274 }
2275
2276 return err;
2277}
2278
2279static const struct i2c_device_id smi_gyro_id[] = {
2280 { SENSOR_NAME, 0 },
2281 { }
2282};
2283
2284MODULE_DEVICE_TABLE(i2c, smi_gyro_id);
2285static const struct of_device_id smi130_gyro_of_match[] = {
2286 { .compatible = "smi130_gyro", },
2287 { }
2288};
2289MODULE_DEVICE_TABLE(i2c, smi130_gyro_of_match);
2290
2291static struct i2c_driver smi_gyro_driver = {
2292 .driver = {
2293 .owner = THIS_MODULE,
2294 .name = SENSOR_NAME,
2295 .of_match_table = smi130_gyro_of_match,
2296 },
2297 .class = I2C_CLASS_HWMON,
2298 .id_table = smi_gyro_id,
2299 .probe = smi_gyro_probe,
2300 .remove = smi_gyro_remove,
2301 .shutdown = smi_gyro_shutdown,
Bosch Sensortecface5922018-08-29 08:08:53 +02002302};
2303
2304static int __init SMI_GYRO_init(void)
2305{
2306 return i2c_add_driver(&smi_gyro_driver);
2307}
2308
2309static void __exit SMI_GYRO_exit(void)
2310{
2311 i2c_del_driver(&smi_gyro_driver);
2312}
2313
2314MODULE_AUTHOR("contact@bosch-sensortec.com>");
2315MODULE_DESCRIPTION("SMI_GYRO GYROSCOPE SENSOR DRIVER");
2316MODULE_LICENSE("GPL v2");
2317
2318module_init(SMI_GYRO_init);
2319module_exit(SMI_GYRO_exit);