input: misc: Invensense MPU 5.0.1 driver
[linux-2.6.git] / drivers / input / misc / mpu / inv_gyro.c
1 /*
2 * Copyright (C) 2012 Invensense, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 */
14
15 /**
16  *  @addtogroup  DRIVERS
17  *  @brief       Hardware drivers.
18  *
19  *  @{
20  *      @file    inv_gyro.c
21  *      @brief   A sysfs device driver for Invensense devices
22  *      @details This driver currently works for the ITG3500, MPU6050, MPU9150
23  *               MPU3050
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/err.h>
31 #include <linux/delay.h>
32 #include <linux/sysfs.h>
33 #include <linux/jiffies.h>
34 #include <linux/irq.h>
35 #include <linux/interrupt.h>
36 #include <linux/kfifo.h>
37 #include <linux/byteorder/generic.h>
38 #ifdef CONFIG_HAS_EARLYSUSPEND
39 #include <linux/earlysuspend.h>
40 #endif
41 #include <linux/poll.h>
42 #include <linux/miscdevice.h>
43 #include <linux/spinlock.h>
44 #include "inv_gyro.h"
45
46 static struct inv_reg_map_s chip_reg = {
47         .who_am_i               = 0x75,
48         .sample_rate_div        = 0x19,
49         .lpf                    = 0x1A,
50         .product_id             = 0x0C,
51         .bank_sel               = 0x6D,
52         .user_ctrl              = 0x6A,
53         .fifo_en                = 0x23,
54         .gyro_config            = 0x1B,
55         .accl_config            = 0x1C,
56         .fifo_count_h           = 0x72,
57         .fifo_r_w               = 0x74,
58         .raw_gyro               = 0x43,
59         .raw_accl               = 0x3B,
60         .temperature            = 0x41,
61         .int_enable             = 0x38,
62         .int_status             = 0x3A,
63         .pwr_mgmt_1             = 0x6B,
64         .pwr_mgmt_2             = 0x6C,
65         .mem_start_addr         = 0x6E,
66         .mem_r_w                = 0x6F,
67         .prgm_strt_addrh        = 0x70
68 };
69 static const struct inv_hw_s hw_info[INV_NUM_PARTS] = {
70         {119, "ITG3500"},
71         { 63, "MPU3050"},
72         {118, "MPU6050"},
73         {118, "MPU9150"}
74 };
75
76 s64 get_time_ns(void)
77 {
78         struct timespec ts;
79         ktime_get_ts(&ts);
80         return timespec_to_ns(&ts);
81 }
82
83 /**
84  *  inv_i2c_read_base() - Read one or more bytes from the device registers.
85  *  @st:        Device driver instance.
86  *  @reg:       First device register to be read from.
87  *  @length:    Number of bytes to read.
88  *  @data:      Data read from device.
89  *  NOTE: The slave register will not increment when reading from the FIFO.
90  */
91 int inv_i2c_read_base(struct inv_gyro_state_s *st, unsigned short i2c_addr,
92                       unsigned char reg, unsigned short length,
93                       unsigned char *data)
94 {
95         struct i2c_msg msgs[2];
96         int res;
97
98         if (!data)
99                 return -EINVAL;
100
101         msgs[0].addr = i2c_addr;
102         msgs[0].flags = 0;      /* write */
103         msgs[0].buf = &reg;
104         msgs[0].len = 1;
105
106         msgs[1].addr = i2c_addr;
107         msgs[1].flags = I2C_M_RD;
108         msgs[1].buf = data;
109         msgs[1].len = length;
110
111         pr_debug("%s RD%02X%02X%02X\n", st->hw->name, i2c_addr, reg, length);
112         res = i2c_transfer(st->sl_handle, msgs, 2);
113         if (res < 2) {
114                 if (res >= 0)
115                         res = -EIO;
116                 return res;
117         }
118
119         return 0;
120 }
121
122 /**
123  *  inv_i2c_single_write_base() - Write a byte to a device register.
124  *  @st:        Device driver instance.
125  *  @reg:       Device register to be written to.
126  *  @data:      Byte to write to device.
127  */
128 int inv_i2c_single_write_base(struct inv_gyro_state_s *st,
129                               unsigned short i2c_addr, unsigned char reg,
130                               unsigned char data)
131 {
132         unsigned char tmp[2];
133         struct i2c_msg msg;
134         int res;
135
136         tmp[0] = reg;
137         tmp[1] = data;
138
139         msg.addr = i2c_addr;
140         msg.flags = 0;  /* write */
141         msg.buf = tmp;
142         msg.len = 2;
143
144         pr_debug("%s WS%02X%02X%02X\n", st->hw->name, i2c_addr, reg, data);
145         res = i2c_transfer(st->sl_handle, &msg, 1);
146         if (res < 1) {
147                 if (res == 0)
148                         res = -EIO;
149                 return res;
150         }
151
152         return 0;
153 }
154 /**
155  *  inv_clear_kfifo() - clear time stamp fifo
156  *  @st:        Device driver instance.
157  */
158 void inv_clear_kfifo(struct inv_gyro_state_s *st)
159 {
160         unsigned long flags;
161         spin_lock_irqsave(&st->time_stamp_lock, flags);
162         kfifo_reset(&st->trigger.timestamps);
163         spin_unlock_irqrestore(&st->time_stamp_lock, flags);
164 }
165
166 static int set_power_itg(struct inv_gyro_state_s *st, unsigned char power_on)
167 {
168         struct inv_reg_map_s *reg;
169         unsigned char data;
170         int result;
171
172         reg = st->reg;
173         if (power_on)
174                 data = 0;
175         else
176                 data = BIT_SLEEP;
177         data |= (st->chip_config.lpa_mode << 5);
178         if (st->chip_config.gyro_enable) {
179                 result = inv_i2c_single_write(st,
180                         reg->pwr_mgmt_1, data | INV_CLK_PLL);
181                 if (result)
182                         return result;
183
184                 st->chip_config.clk_src = INV_CLK_PLL;
185         } else {
186                 result = inv_i2c_single_write(st,
187                         reg->pwr_mgmt_1, data | INV_CLK_INTERNAL);
188                 if (result)
189                         return result;
190
191                 st->chip_config.clk_src = INV_CLK_INTERNAL;
192         }
193
194         if (power_on) {
195                 mdelay(POWER_UP_TIME);
196                 data = 0;
197                 if (0 == st->chip_config.accl_enable)
198                         data |= BIT_PWR_ACCL_STBY;
199                 if (0 == st->chip_config.gyro_enable)
200                         data |= BIT_PWR_GYRO_STBY;
201                 data |= (st->chip_config.lpa_freq << 6);
202                 result = inv_i2c_single_write(st, reg->pwr_mgmt_2, data);
203                 if (result)
204                         return result;
205
206                 mdelay(POWER_UP_TIME);
207                 st->chip_config.is_asleep = 0;
208         } else {
209                 st->chip_config.is_asleep = 1;
210         }
211         return 0;
212 }
213
214 /**
215  *  inv_set_power_state() - Turn device on/off.
216  *  @st:        Device driver instance.
217  *  @power_on:  1 to turn on, 0 to suspend.
218  */
219 int inv_set_power_state(struct inv_gyro_state_s *st, unsigned char power_on)
220 {
221         if (INV_MPU3050 == st->chip_type)
222                 return set_power_mpu3050(st, power_on);
223         else
224                 return set_power_itg(st, power_on);
225 }
226
227 /**
228  *  reset_fifo_itg() - Reset FIFO related registers.
229  *  @st:        Device driver instance.
230  */
231 static int reset_fifo_itg(struct inv_gyro_state_s *st)
232 {
233         struct inv_reg_map_s *reg;
234         int result;
235         unsigned char val;
236
237         reg = st->reg;
238         /* disable interrupt */
239         result = inv_i2c_single_write(st, reg->int_enable, 0);
240         if (result) {
241                 pr_err("%s failed\n", __func__);
242                 return result;
243         }
244
245         /* disable the sensor output to FIFO */
246         result = inv_i2c_single_write(st, reg->fifo_en, 0);
247         if (result)
248                 goto reset_fifo_fail;
249
250         /* disable fifo reading */
251         result = inv_i2c_single_write(st, reg->user_ctrl, 0);
252         if (result)
253                 goto reset_fifo_fail;
254
255         if (st->chip_config.dmp_on) {
256                 val = (BIT_FIFO_RST | BIT_DMP_RST);
257                 if (st->chip_config.compass_enable)
258                         val |= BIT_I2C_MST_RST;
259                 result = inv_i2c_single_write(st, reg->user_ctrl, val);
260                 if (result)
261                         goto reset_fifo_fail;
262
263                 mdelay(POWER_UP_TIME);
264                 st->last_isr_time = get_time_ns();
265                 result = inv_i2c_single_write(st, reg->int_enable,
266                                               BIT_DMP_INT_EN);
267                 if (result)
268                         return result;
269
270                 val = (BIT_DMP_EN | BIT_FIFO_EN);
271                 if (st->chip_config.compass_enable)
272                         val |= BIT_I2C_MST_EN;
273                 result = inv_i2c_single_write(st, reg->user_ctrl, val);
274                 if (result)
275                         goto reset_fifo_fail;
276
277         } else {
278                 /* reset FIFO and possibly reset I2C*/
279                 val = BIT_FIFO_RST;
280                 if (st->chip_config.compass_enable)
281                         val |= BIT_I2C_MST_RST;
282                 result = inv_i2c_single_write(st, reg->user_ctrl, val);
283                 if (result)
284                         goto reset_fifo_fail;
285
286                 mdelay(POWER_UP_TIME);
287                 st->last_isr_time = get_time_ns();
288                 /* enable interrupt */
289                 if (st->chip_config.accl_fifo_enable ||
290                         st->chip_config.gyro_fifo_enable ||
291                         st->chip_config.compass_enable){
292                         result = inv_i2c_single_write(st, reg->int_enable,
293                                                 BIT_DATA_RDY_EN);
294                         if (result)
295                                 return result;
296                 }
297
298                 /* enable FIFO reading and I2C master interface*/
299                 val = BIT_FIFO_EN;
300                 if (st->chip_config.compass_enable)
301                         val |= BIT_I2C_MST_EN;
302                 result = inv_i2c_single_write(st, reg->user_ctrl, val);
303                 if (result)
304                         goto reset_fifo_fail;
305
306                 /* enable sensor output to FIFO */
307                 val = 0;
308                 if (st->chip_config.gyro_fifo_enable)
309                         val |= BITS_GYRO_OUT;
310                 if (st->chip_config.accl_fifo_enable)
311                         val |= BIT_ACCEL_OUT;
312                 result = inv_i2c_single_write(st, reg->fifo_en, val);
313                 if (result)
314                         goto reset_fifo_fail;
315         }
316
317         return 0;
318
319 reset_fifo_fail:
320         if (st->chip_config.dmp_on)
321                 val = BIT_DMP_INT_EN;
322         else
323                 val = BIT_DATA_RDY_EN;
324         inv_i2c_single_write(st, reg->int_enable, val);
325         pr_err("%s failed\n", __func__);
326         return result;
327 }
328
329 /**
330  *  inv_reset_fifo() - Reset FIFO related registers.
331  *  @st:        Device driver instance.
332  */
333 int inv_reset_fifo(struct inv_gyro_state_s *st)
334 {
335         if (INV_MPU3050 == st->chip_type)
336                 return reset_fifo_mpu3050(st);
337         else
338                 return reset_fifo_itg(st);
339 }
340
341 /**
342  *  set_inv_enable() - Reset FIFO related registers.
343  *  @st:        Device driver instance.
344  *  @fifo_enable: enable/disable
345  */
346 int set_inv_enable(struct inv_gyro_state_s *st, unsigned long enable)
347 {
348         struct inv_reg_map_s *reg;
349         int result;
350
351         if (st->chip_config.is_asleep)
352                 return -EINVAL;
353
354         reg = st->reg;
355         if (enable) {
356                 result = inv_reset_fifo(st);
357                 if (result)
358                         return result;
359
360                 inv_clear_kfifo(st);
361                 st->chip_config.enable = 1;
362
363         } else {
364                 result = inv_i2c_single_write(st, reg->fifo_en, 0);
365                 if (result)
366                         return result;
367
368                 result = inv_i2c_single_write(st, reg->int_enable, 0);
369                 if (result)
370                         return result;
371
372                 /* disable fifo reading */
373                 if (INV_MPU3050 != st->chip_type) {
374                         result = inv_i2c_single_write(st, reg->user_ctrl, 0);
375                         if (result)
376                                 return result;
377                 }
378
379                 st->chip_config.enable = 0;
380         }
381
382         return 0;
383 }
384
385 static void inv_input_close(struct input_dev *d)
386 {
387         struct inv_gyro_state_s *st;
388
389         st = input_get_drvdata(d);
390         set_inv_enable(st, 0);
391         inv_set_power_state(st, 0);
392 }
393
394 /**
395  *  inv_setup_input() - internal setup input device.
396  *  @st:        Device driver instance.
397  *  @**idev_in  pointer to input device
398  *  @*client    i2c client
399  *  @*name      name of the input device.
400  */
401 static int inv_setup_input(struct inv_gyro_state_s *st,
402                            struct input_dev **idev_in,
403                            struct i2c_client *client, unsigned char *name)
404 {
405         int result;
406         struct input_dev *idev;
407
408         idev = input_allocate_device();
409         if (!idev) {
410                 result = -ENOMEM;
411                 return result;
412         }
413
414         /* Setup input device. */
415         idev->name = name;
416         idev->id.bustype = BUS_I2C;
417         idev->id.product = 'S';
418         idev->id.vendor     = ('I'<<8) | 'S';
419         idev->id.version    = 1;
420         idev->dev.parent = &client->dev;
421         /* Open and close method. */
422         if (strcmp(name, "INV_DMP") && strcmp(name, "INV_COMPASS"))
423                 idev->close = inv_input_close;
424         input_set_capability(idev, EV_REL, REL_X);
425         input_set_capability(idev, EV_REL, REL_Y);
426         input_set_capability(idev, EV_REL, REL_Z);
427         input_set_capability(idev, EV_REL, REL_RX);
428         input_set_capability(idev, EV_REL, REL_RY);
429         input_set_capability(idev, EV_REL, REL_RZ);
430         input_set_capability(idev, EV_REL, REL_MISC);
431         input_set_capability(idev, EV_REL, REL_WHEEL);
432         input_set_drvdata(idev, st);
433         result = input_register_device(idev);
434         if (result)
435                 input_free_device(idev);
436         *idev_in = idev;
437         return result;
438 }
439
440 /**
441  *  inv_init_config() - Initialize hardware, disable FIFO.
442  *  @st:        Device driver instance.
443  *  Initial configuration:
444  *  FSR: +/- 2000DPS
445  *  DLPF: 42Hz
446  *  FIFO rate: 50Hz
447  *  Clock source: Gyro PLL
448  */
449 static int inv_init_config(struct inv_gyro_state_s *st)
450 {
451         struct inv_reg_map_s *reg;
452         int result;
453
454         if (st->chip_config.is_asleep)
455                 return -EPERM;
456
457         reg = st->reg;
458         result = set_inv_enable(st, 0);
459         if (result)
460                 return result;
461
462         result = inv_i2c_single_write(st, reg->gyro_config,
463                 INV_FSR_2000DPS << 3);
464         if (result)
465                 return result;
466
467         st->chip_config.fsr = INV_FSR_2000DPS;
468         result = inv_i2c_single_write(st, reg->lpf, INV_FILTER_42HZ);
469         if (result)
470                 return result;
471
472         st->chip_config.lpf = INV_FILTER_42HZ;
473         result = inv_i2c_single_write(st, reg->sample_rate_div, 19);
474         if (result)
475                 return result;
476
477         st->chip_config.fifo_rate = 50;
478         st->irq_dur_us            = 20*1000;
479         st->chip_config.enable = 0;
480         st->chip_config.dmp_on = 0;
481         st->compass_divider = 0;
482         st->compass_counter = 0;
483         st->chip_config.compass_enable = 0;
484         st->chip_config.firmware_loaded = 0;
485         st->chip_config.prog_start_addr = DMP_START_ADDR;
486         st->chip_config.gyro_enable = 1;
487         st->chip_config.gyro_fifo_enable = 1;
488         if (INV_ITG3500 != st->chip_type) {
489                 st->chip_config.accl_enable = 1;
490                 st->chip_config.accl_fifo_enable = 1;
491                 st->chip_config.accl_fs = INV_FS_02G;
492                 result = inv_i2c_single_write(st, reg->accl_config,
493                         (INV_FS_02G << 3));
494                 if (result)
495                         return result;
496
497         } else {
498                 st->chip_config.accl_enable = 0;
499                 st->chip_config.accl_fifo_enable = 0;
500         }
501
502         return 0;
503 }
504
505 /**
506  *  inv_raw_gyro_show() - Read gyro data directly from registers.
507  */
508 static ssize_t inv_raw_gyro_show(struct device *dev,
509                                  struct device_attribute *attr, char *buf)
510 {
511         struct inv_gyro_state_s *st;
512         struct inv_reg_map_s *reg;
513         int result;
514         unsigned char data[6];
515
516         st = dev_get_drvdata(dev);
517         reg = st->reg;
518         if (st->chip_config.is_asleep)
519                 return -EPERM;
520
521         if (0 == st->chip_config.gyro_enable)
522                 return -EPERM;
523
524         result = inv_i2c_read(st, reg->raw_gyro, 6, data);
525         if (result) {
526                 printk(KERN_ERR "Could not read raw registers.\n");
527                 return result;
528         }
529
530         return sprintf(buf, "%d %d %d %lld\n",
531                 (signed short)(be16_to_cpup((short *)&data[0])),
532                 (signed short)(be16_to_cpup((short *)&data[2])),
533                 (signed short)(be16_to_cpup((short *)&data[4])),
534                 get_time_ns());
535 }
536
537 /**
538  *  inv_raw_accl_show() - Read accel data directly from registers.
539  */
540 static ssize_t inv_raw_accl_show(struct device *dev,
541                                  struct device_attribute *attr, char *buf)
542 {
543         struct inv_gyro_state_s *st;
544         struct inv_reg_map_s *reg;
545         int result;
546         unsigned char data[6];
547
548         st = dev_get_drvdata(dev);
549         reg = st->reg;
550         if (st->chip_config.is_asleep)
551                 return -EPERM;
552
553         if (0 == st->chip_config.accl_enable)
554                 return -EPERM;
555
556         result = inv_i2c_read(st, reg->raw_accl, 6, data);
557         if (result) {
558                 printk(KERN_ERR "Could not read raw registers.\n");
559                 return result;
560         }
561
562         return sprintf(buf, "%d %d %d %lld\n",
563                 ((signed short)(be16_to_cpup((short *)&data[0]))*
564                                 st->chip_info.multi),
565                 ((signed short)(be16_to_cpup((short *)&data[2]))*
566                                 st->chip_info.multi),
567                 ((signed short)(be16_to_cpup((short *)&data[4]))*
568                                 st->chip_info.multi),
569                 get_time_ns());
570 }
571
572 /**
573  *  inv_temperature_show() - Read temperature data directly from registers.
574  */
575 static ssize_t inv_temperature_show(struct device *dev,
576                                     struct device_attribute *attr, char *buf)
577 {
578         struct inv_gyro_state_s *st;
579         struct inv_reg_map_s *reg;
580         int result;
581         unsigned char data[2];
582
583         st = dev_get_drvdata(dev);
584         reg = st->reg;
585         if (st->chip_config.is_asleep)
586                 return -EPERM;
587
588         result = inv_i2c_read(st, reg->temperature, 2, data);
589         if (result) {
590                 printk(KERN_ERR "Could not read temperature register.\n");
591                 return result;
592         }
593
594         return sprintf(buf, "%d %lld\n",
595                 (signed short)(be16_to_cpup((short *)&data[0])),
596                 get_time_ns());
597 }
598
599 static int inv_set_lpf(struct inv_gyro_state_s *st, int rate)
600 {
601         short hz[6] = {188, 98, 42, 20, 10, 5};
602         int   d[6] = {INV_FILTER_188HZ, INV_FILTER_98HZ,
603                         INV_FILTER_42HZ, INV_FILTER_20HZ,
604                         INV_FILTER_10HZ, INV_FILTER_5HZ};
605         int i, h, data, result;
606         struct inv_reg_map_s *reg;
607
608         reg = st->reg;
609         h = (rate >> 1);
610         i = 0;
611         while ((h < hz[i]) && (i < 6))
612                 i++;
613         data = d[i];
614         if (INV_MPU3050 == st->chip_type) {
615                 if (st->mpu_slave != NULL) {
616                         result = st->mpu_slave->set_lpf(st, rate);
617                         if (result)
618                                 return result;
619                 }
620
621                 result = inv_i2c_single_write(st, reg->lpf,
622                         data | (st->chip_config.fsr << 3));
623
624         } else {
625                 result = inv_i2c_single_write(st, reg->lpf, data);
626         }
627
628         if (result)
629                 return result;
630
631         st->chip_config.lpf = data;
632         return 0;
633 }
634
635 /**
636  *  inv_fifo_rate_store() - Set fifo rate.
637  */
638 static ssize_t inv_fifo_rate_store(struct device *dev,
639                                    struct device_attribute *attr,
640                                    const char *buf, size_t count)
641 {
642         unsigned long fifo_rate;
643         unsigned char data;
644         int result;
645         struct inv_gyro_state_s *st;
646         struct inv_reg_map_s *reg;
647
648         st = dev_get_drvdata(dev);
649         reg = st->reg;
650         if (st->chip_config.is_asleep)
651                 return -EPERM;
652
653         if (kstrtoul(buf, 10, &fifo_rate))
654                 return -EINVAL;
655
656         if ((fifo_rate < MIN_FIFO_RATE) || (fifo_rate > MAX_FIFO_RATE))
657                 return -EINVAL;
658
659         if (fifo_rate == st->chip_config.fifo_rate)
660                 return count;
661
662         if (st->has_compass) {
663                 data = 10*fifo_rate/ONE_K_HZ;
664                 if (data > 0)
665                         data -= 1;
666                 st->compass_divider = data;
667                 st->compass_counter = 0;
668                 /* I2C_MST_DLY is set according to sample rate,
669                    AKM cannot be read or set at sample rate higher than 100Hz*/
670                 result = inv_i2c_single_write(st, REG_I2C_SLV4_CTRL, data);
671                 if (result)
672                         return result;
673         }
674
675         data = ONE_K_HZ / fifo_rate - 1;
676         result = inv_i2c_single_write(st, reg->sample_rate_div, data);
677         if (result)
678                 return result;
679
680         st->chip_config.fifo_rate = fifo_rate;
681         result = inv_set_lpf(st, fifo_rate);
682         if (result)
683                 return result;
684
685         st->irq_dur_us = (data + 1) * ONE_K_HZ;
686         st->last_isr_time = get_time_ns();
687         return count;
688 }
689
690 /**
691  *  inv_power_state_store() - Turn device on/off.
692  */
693 static ssize_t inv_power_state_store(struct device *dev,
694                                      struct device_attribute *attr,
695                                      const char *buf, size_t count)
696 {
697         int result;
698         unsigned long power_state;
699         struct inv_gyro_state_s *st;
700
701         st = dev_get_drvdata(dev);
702         if (kstrtoul(buf, 10, &power_state))
703                 return -EINVAL;
704
705         if (!power_state == st->chip_config.is_asleep)
706                 return count;
707
708         result = inv_set_power_state(st, power_state);
709         return count;
710 }
711
712 /**
713  *  inv_enable_store() - Enable/disable chip operation.
714  */
715 static ssize_t inv_enable_store(struct device *dev,
716                                 struct device_attribute *attr,
717                                 const char *buf, size_t count)
718 {
719         unsigned long enable;
720         struct inv_gyro_state_s *st;
721         int result;
722
723         st = dev_get_drvdata(dev);
724         if (st->chip_config.is_asleep)
725                 return -EPERM;
726
727         if (kstrtoul(buf, 10, &enable))
728                 return -EINVAL;
729
730         if (!enable == !st->chip_config.enable)
731                 return count;
732
733         result = set_inv_enable(st, enable);
734         if (result)
735                 return result;
736
737         return count;
738 }
739
740 /**
741  *  inv_accl_fifo_enable_store() - Enable/disable accl fifo output.
742  */
743 static ssize_t inv_accl_fifo_enable_store(struct device *dev,
744                                           struct device_attribute *attr,
745                                           const char *buf, size_t count)
746 {
747         unsigned long data, en;
748         int result;
749         struct inv_gyro_state_s *st;
750
751         st = dev_get_drvdata(dev);
752         if (st->chip_config.is_asleep)
753                 return -EPERM;
754
755         if (st->chip_config.enable)
756                 return -EPERM;
757
758         result = kstrtoul(buf, 10, &data);
759         if (result)
760                 return -EINVAL;
761
762         if (data)
763                 en = 1;
764         else
765                 en = 0;
766         if (en == st->chip_config.accl_fifo_enable)
767                 return count;
768
769         if (en && (0 == st->chip_config.accl_enable)) {
770                 result = inv_set_power_state(st, 0);
771                 if (result)
772                         return result;
773
774                 st->chip_config.accl_enable = en;
775                 result = inv_set_power_state(st, 1);
776                 if (result)
777                         return result;
778         }
779
780         st->chip_config.accl_fifo_enable = en;
781         return count;
782 }
783
784 /**
785  *  inv_gyro_fifo_enable_store() - Enable/disable gyro fifo output.
786  */
787 ssize_t inv_gyro_fifo_enable_store(struct device *dev,
788                                    struct device_attribute *attr,
789                                    const char *buf, size_t count)
790 {
791         unsigned long data, en;
792         int result;
793         struct inv_gyro_state_s *st;
794
795         st = dev_get_drvdata(dev);
796         if (st->chip_config.is_asleep)
797                 return -EPERM;
798
799         if (st->chip_config.enable)
800                 return -EPERM;
801
802         result = kstrtoul(buf, 10, &data);
803         if (result)
804                 return -EINVAL;
805
806         if (data)
807                 en = 1;
808         else
809                 en = 0;
810         if (en == st->chip_config.gyro_fifo_enable)
811                 return count;
812
813         if (en && (0 == st->chip_config.gyro_enable)) {
814                 result = inv_set_power_state(st, 0);
815                 if (result)
816                         return result;
817
818                 st->chip_config.gyro_enable = en;
819                 result = inv_set_power_state(st, 1);
820                 if (result)
821                         return result;
822         }
823
824         st->chip_config.gyro_fifo_enable = en;
825         return count;
826 }
827
828 /**
829  *  inv_gyro_enable_store() - Enable/disable gyro.
830  */
831 ssize_t inv_gyro_enable_store(struct device *dev,
832                               struct device_attribute *attr,
833                               const char *buf, size_t count)
834 {
835         unsigned long data, en;
836         struct inv_gyro_state_s *st;
837         int result;
838
839         st = dev_get_drvdata(dev);
840         if (st->chip_config.is_asleep)
841                 return -EPERM;
842
843         if (st->chip_config.enable)
844                 return -EPERM;
845
846         result = kstrtoul(buf, 10, &data);
847         if (result)
848                 return -EINVAL;
849
850         if (data)
851                 en = 1;
852         else
853                 en = 0;
854         if (en == st->chip_config.gyro_enable)
855                 return count;
856
857         if (0 == en)
858                 st->chip_config.gyro_fifo_enable = 0;
859         result = inv_set_power_state(st, 0);
860         if (result)
861                 return result;
862
863         st->chip_config.gyro_enable = en;
864         result = inv_set_power_state(st, 1);
865         if (result) {
866                 st->chip_config.gyro_enable ^= 1;
867                 return result;
868         }
869
870         return count;
871 }
872
873 /**
874  *  inv_accl_enable_store() - Enable/disable accl.
875  */
876 static ssize_t inv_accl_enable_store(struct device *dev,
877                                      struct device_attribute *attr,
878                                      const char *buf, size_t count)
879 {
880         unsigned long data, en;
881         struct inv_gyro_state_s *st;
882         int result;
883
884         st = dev_get_drvdata(dev);
885         if (st->chip_config.is_asleep)
886                 return -EPERM;
887
888         if (st->chip_config.enable)
889                 return -EPERM;
890
891         result = kstrtoul(buf, 10, &data);
892         if (result)
893                 return -EINVAL;
894
895         if (data)
896                 en = 1;
897         else
898                 en = 0;
899         if (en == st->chip_config.accl_enable)
900                 return count;
901
902         if (0 == en)
903                 st->chip_config.accl_fifo_enable = 0;
904         result = inv_set_power_state(st, 0);
905         if (result)
906                 return result;
907
908         st->chip_config.accl_enable = en;
909         result = inv_set_power_state(st, 1);
910         if (result) {
911                 st->chip_config.accl_enable ^= 1;
912                 return result;
913         }
914
915         return count;
916 }
917
918 /**
919  *  inv_gyro_fs_store() - Change the gyro full-scale range (and scale factor).
920  */
921 static ssize_t inv_gyro_fs_store(struct device *dev,
922                                  struct device_attribute *attr,
923                                  const char *buf, size_t count)
924 {
925         unsigned long fsr;
926         int result;
927         struct inv_gyro_state_s *st;
928         struct inv_reg_map_s *reg;
929
930         st = dev_get_drvdata(dev);
931         if (st->chip_config.is_asleep)
932                 return -EPERM;
933
934         result = kstrtoul(buf, 10, &fsr);
935         if (result)
936                 return -EINVAL;
937
938         if (fsr > 3)
939                 return -EINVAL;
940
941         if (fsr == st->chip_config.fsr)
942                 return count;
943
944         reg = st->reg;
945         if (INV_MPU3050 == st->chip_type)
946                 result = inv_i2c_single_write(st, reg->lpf,
947                         (fsr << 3) | st->chip_config.lpf);
948         else
949                 result = inv_i2c_single_write(st, reg->gyro_config,
950                         fsr << 3);
951         if (result)
952                 return result;
953
954         st->chip_config.fsr = fsr;
955         return count;
956 }
957
958 /**
959  *  inv_accl_fs_store() - Configure the accelerometer's scale range.
960  */
961 ssize_t inv_accl_fs_store(struct device *dev, struct device_attribute *attr,
962                           const char *buf, size_t count)
963 {
964         unsigned long fs;
965         int result;
966         struct inv_gyro_state_s *st;
967         struct inv_reg_map_s *reg;
968
969         st = dev_get_drvdata(dev);
970         if (st->chip_config.is_asleep)
971                 return -EPERM;
972
973         if (kstrtoul(buf, 10, &fs))
974                 return -EINVAL;
975
976         if (fs > 3)
977                 return -EINVAL;
978
979         if (fs == st->chip_config.accl_fs)
980                 return count;
981
982         reg = st->reg;
983         if ((INV_MPU3050 == st->chip_type) && (st->mpu_slave != NULL))
984                 result = st->mpu_slave->set_fs(st, fs);
985         else
986                 result = inv_i2c_single_write(st, reg->accl_config, (fs << 3));
987         if (result)
988                 return result;
989
990         /* reset fifo because the data could be mixed with old bad data */
991         st->chip_config.accl_fs = fs;
992         return count;
993 }
994
995 /**
996  * inv_firmware_loaded_store() -  calling this function will change
997  *                        firmware load
998  */
999 static ssize_t inv_firmware_loaded_store(struct device *dev,
1000                                          struct device_attribute *attr,
1001                                          const char *buf, size_t count)
1002 {
1003         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1004         unsigned long data, result;
1005
1006         result = kstrtoul(buf, 10, &data);
1007         if (result)
1008                 return result;
1009
1010         if (data != 0)
1011                 return -EINVAL;
1012
1013         st->chip_config.firmware_loaded = 0;
1014         st->chip_config.dmp_on = 0;
1015         return count;
1016 }
1017
1018 /**
1019  *  inv_lpa_mode_store() - store current low power settings
1020  */
1021 static ssize_t inv_lpa_mode_store(struct device *dev,
1022                                   struct device_attribute *attr,
1023                                   const char *buf, size_t count)
1024 {
1025         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1026         unsigned long result, lpa_mode;
1027         unsigned char d;
1028         struct inv_reg_map_s *reg;
1029
1030         if (st->chip_config.is_asleep)
1031                 return -EPERM;
1032
1033         result = kstrtoul(buf, 10, &lpa_mode);
1034         if (result)
1035                 return result;
1036
1037         reg = st->reg;
1038         result = inv_i2c_read(st, reg->pwr_mgmt_1, 1, &d);
1039         if (result)
1040                 return result;
1041
1042         d &= ~BIT_CYCLE;
1043         if (lpa_mode)
1044                 d |= BIT_CYCLE;
1045         result = inv_i2c_single_write(st, reg->pwr_mgmt_1, d);
1046         if (result)
1047                 return result;
1048
1049         st->chip_config.lpa_mode = lpa_mode;
1050         return count;
1051 }
1052
1053 /**
1054  *  inv_lpa_freq_store() - store current low power frequency setting.
1055  */
1056 static ssize_t inv_lpa_freq_store(struct device *dev,
1057                                   struct device_attribute *attr,
1058                                   const char *buf, size_t count)
1059 {
1060         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1061         unsigned long result, lpa_freq;
1062         unsigned char d;
1063         struct inv_reg_map_s *reg;
1064
1065         if (st->chip_config.is_asleep)
1066                 return -EPERM;
1067
1068         result = kstrtoul(buf, 10, &lpa_freq);
1069         if (result)
1070                 return result;
1071
1072         if (lpa_freq > 3)
1073                 return -EINVAL;
1074
1075         reg = st->reg;
1076         result = inv_i2c_read(st, reg->pwr_mgmt_2, 1, &d);
1077         if (result)
1078                 return result;
1079
1080         d &= ~BIT_LPA_FREQ;
1081         d |= (unsigned char)(lpa_freq << 6);
1082         result = inv_i2c_single_write(st, reg->pwr_mgmt_2, d);
1083         if (result)
1084                 return result;
1085
1086         st->chip_config.lpa_freq = lpa_freq;
1087         return count;
1088 }
1089
1090 /**
1091  * inv_compass_en_store() -  calling this function will store compass
1092  *                         enable
1093  */
1094 static ssize_t inv_compass_en_store(struct device *dev,
1095                                     struct device_attribute *attr,
1096                                     const char *buf, size_t count)
1097 {
1098         struct inv_gyro_state_s *st;
1099         unsigned long data, result, en;
1100
1101         st = dev_get_drvdata(dev);
1102         if (st->chip_config.is_asleep)
1103                 return -EPERM;
1104
1105         if (st->chip_config.enable)
1106                 return -EPERM;
1107
1108         result = kstrtoul(buf, 10, &data);
1109         if (result)
1110                 return result;
1111
1112         if (data)
1113                 en = 1;
1114         else
1115                 en = 0;
1116         if (en == st->chip_config.compass_enable)
1117                 return count;
1118
1119         st->chip_config.compass_enable = en;
1120         return count;
1121 }
1122
1123 /**
1124  *  inv_compass_scale_store() - show current compass scale settings
1125  */
1126 static ssize_t inv_compass_scale_store(struct device *dev,
1127                                        struct device_attribute *attr,
1128                                        const char *buf, size_t count)
1129 {
1130         struct inv_gyro_state_s *st;
1131         unsigned long data, result, en;
1132         char d;
1133
1134         st = dev_get_drvdata(dev);
1135         if (COMPASS_ID_AK8963 != st->plat_data.sec_slave_id)
1136                 return count;
1137
1138         if (st->chip_config.is_asleep)
1139                 return -EPERM;
1140
1141         result = kstrtoul(buf, 10, &data);
1142         if (result)
1143                 return result;
1144
1145         if (data)
1146                 en = 1;
1147         else
1148                 en = 0;
1149         if (st->compass_scale == en)
1150                 return count;
1151
1152         st->compass_scale = en;
1153         d = (1 | (st->compass_scale << 4));
1154         result = inv_i2c_single_write(st, REG_I2C_SLV1_DO, d);
1155         if (result)
1156                 return result;
1157
1158         return count;
1159 }
1160
1161 /**
1162  * inv_flick_lower_store() -  calling this function will store current
1163  *                        flick lower bound
1164  */
1165 static ssize_t inv_flick_lower_store(struct device *dev,
1166                                      struct device_attribute *attr,
1167                                      const char *buf, size_t count)
1168 {
1169         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1170         int result, data, out;
1171         unsigned char *p;
1172
1173         if (st->chip_config.is_asleep)
1174                 return -EPERM;
1175
1176         result = kstrtol(buf, 10, (long unsigned int *)&data);
1177         if (result)
1178                 return result;
1179
1180         out = cpu_to_be32p(&data);
1181         p = (unsigned char *)&out;
1182         result = mem_w_key(KEY_FLICK_LOWER, 4, p);
1183         if (result)
1184                 return result;
1185
1186         st->flick.lower = data;
1187         return count;
1188 }
1189
1190 /**
1191  * inv_flick_upper_store() -  calling this function will store current
1192  *                        flick upper bound
1193  */
1194 static ssize_t inv_flick_upper_store(struct device *dev,
1195                                      struct device_attribute *attr,
1196                                      const char *buf, size_t count)
1197 {
1198         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1199         unsigned int result, data, out;
1200         unsigned char *p;
1201
1202         if (st->chip_config.is_asleep)
1203                 return -EPERM;
1204
1205         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1206         if (result)
1207                 return result;
1208
1209         out = cpu_to_be32p(&data);
1210         p = (unsigned char *)&out;
1211         result = mem_w_key(KEY_FLICK_UPPER, 4, p);
1212         if (result)
1213                 return result;
1214
1215         st->flick.upper = data;
1216         return count;
1217 }
1218
1219 /**
1220  * inv_flick_counter_store() -  calling this function will store current
1221  *                        flick counter value
1222  */
1223 static ssize_t inv_flick_counter_store(struct device *dev,
1224                                        struct device_attribute *attr,
1225                                        const char *buf, size_t count)
1226 {
1227         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1228         unsigned int result, data, out;
1229         unsigned char *p;
1230
1231         if (st->chip_config.is_asleep)
1232                 return -EPERM;
1233
1234         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1235         if (result)
1236                 return result;
1237
1238         out = cpu_to_be32p(&data);
1239         p = (unsigned char *)&out;
1240         result = mem_w_key(KEY_FLICK_COUNTER, 4, p);
1241         if (result)
1242                 return result;
1243
1244         st->flick.counter = data;
1245         return count;
1246 }
1247
1248 /**
1249  * inv_flick_int_on_store() -  calling this function will store current
1250  *                        flick interrupt on value
1251  */
1252 static ssize_t inv_flick_int_on_store(struct device *dev,
1253                                       struct device_attribute *attr,
1254                                       const char *buf, size_t count)
1255 {
1256         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1257         unsigned long result, data;
1258         unsigned char d[4];
1259
1260         if (st->chip_config.is_asleep)
1261                 return -EPERM;
1262
1263         result = kstrtoul(buf, 10, &data);
1264         if (result)
1265                 return result;
1266
1267         if (data)
1268                 /* Use interrupt to signal when gesture was observed */
1269                 d[0] = DIND40+4;
1270         else
1271                 d[0] = DINAA0+8;
1272         result = mem_w_key(KEY_CGNOTICE_INTR, 1, d);
1273         if (result)
1274                 return result;
1275
1276         st->flick.int_on = data;
1277         return count;
1278 }
1279
1280 /**
1281  * inv_flick_axis_store() -  calling this function will store current
1282  *                        flick axis value
1283  */
1284 static ssize_t inv_flick_axis_store(struct device *dev,
1285                                     struct device_attribute *attr,
1286                                     const char *buf, size_t count)
1287 {
1288         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1289         unsigned long result, data;
1290         unsigned char d[4];
1291
1292         if (st->chip_config.is_asleep)
1293                 return -EPERM;
1294
1295         result = kstrtoul(buf, 10, &data);
1296         if (result)
1297                 return result;
1298
1299         if (data == 0)
1300                 d[0] = DINBC2;
1301         else if (data == 2)
1302                 d[2] = DINBC6;
1303         else
1304                 d[0] = DINBC4;
1305         result = mem_w_key(KEY_CFG_FLICK_IN, 1, d);
1306         if (result)
1307                 return result;
1308
1309         st->flick.axis = data;
1310         return count;
1311 }
1312
1313 /**
1314  * inv_flick_msg_on_store() -  calling this function will store current
1315  *                        flick message on value
1316  */
1317 static ssize_t inv_flick_msg_on_store(struct device *dev,
1318                                       struct device_attribute *attr,
1319                                       const char *buf, size_t count)
1320 {
1321         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1322         unsigned int result, data, out;
1323         unsigned char *p;
1324
1325         if (st->chip_config.is_asleep)
1326                 return -EPERM;
1327
1328         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1329         if (result)
1330                 return result;
1331
1332         if (data)
1333                 data = DATA_MSG_ON;
1334         out = cpu_to_be32p(&data);
1335         p = (unsigned char *)&out;
1336         result = mem_w_key(KEY_FLICK_MSG, 4, p);
1337         if (result)
1338                 return result;
1339
1340         st->flick.msg_on = data;
1341         return count;
1342 }
1343
1344 /**
1345  * inv_pedometer_steps_store() -  calling this function will store current
1346  *                        pedometer steps into MPU memory
1347  */
1348 static ssize_t inv_pedometer_steps_store(struct device *dev,
1349                                          struct device_attribute *attr,
1350                                          const char *buf, size_t count)
1351 {
1352         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1353         unsigned int result, data, out;
1354         unsigned char *p;
1355
1356         if (st->chip_config.is_asleep)
1357                 return -EPERM;
1358
1359         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1360         if (result)
1361                 return result;
1362
1363         out = cpu_to_be32p(&data);
1364         p = (unsigned char *)&out;
1365         result = mem_w_key(KEY_D_PEDSTD_STEPCTR, 4, p);
1366         if (result)
1367                 return result;
1368
1369         return count;
1370 }
1371
1372 /**
1373  * inv_pedometer_time_store() -  calling this function will store current
1374  *                        pedometer time into MPU memory
1375  */
1376 static ssize_t inv_pedometer_time_store(struct device *dev,
1377                                         struct device_attribute *attr,
1378                                         const char *buf, size_t count)
1379 {
1380         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1381         unsigned int result, data, out;
1382         unsigned char *p;
1383
1384         if (st->chip_config.is_asleep)
1385                 return -EPERM;
1386
1387         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1388         if (result)
1389                 return result;
1390
1391         out = cpu_to_be32p(&data);
1392         p = (unsigned char *)&out;
1393         result = mem_w_key(KEY_D_PEDSTD_TIMECTR, 4, p);
1394         if (result)
1395                 return result;
1396
1397         return count;
1398 }
1399
1400 /**
1401  * inv_key_store() -  calling this function will store authenticate key
1402  */
1403 static ssize_t inv_key_store(struct device *dev,
1404                              struct device_attribute *attr,
1405                              const char *buf, size_t count)
1406 {
1407         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1408         unsigned int result, data, out;
1409         unsigned char *p, d[4];
1410
1411         if (st->chip_config.is_asleep)
1412                 return -EPERM;
1413
1414         result = kstrtoul(buf, 10, (long unsigned int *)&data);
1415         if (result)
1416                 return result;
1417
1418         out = cpu_to_be32p(&data);
1419         p = (unsigned char *)&out;
1420         result = mem_w(D_AUTH_IN, 4, p);
1421         if (result)
1422                 return result;
1423
1424         result = mpu_memory_read(st->sl_handle, st->i2c_addr,
1425                 D_AUTH_IN, 4, d);
1426         return count;
1427 }
1428
1429 #ifdef CONFIG_HAS_EARLYSUSPEND
1430 /**
1431  *  inv_early_suspend_on_store() - set early_suspend_enable value
1432  */
1433 static ssize_t inv_early_suspend_en_store(struct device *dev,
1434                                           struct device_attribute *attr,
1435                                           const char *buf, size_t count)
1436 {
1437         struct inv_gyro_state_s *st;
1438         unsigned long data;
1439         int result;
1440
1441         st = dev_get_drvdata(dev);
1442         result = kstrtoul(buf, 10, &data);
1443         if (result)
1444                 return result;
1445
1446         atomic_set(&(st->early_suspend_enable), !!data);
1447         return count;
1448 }
1449 #endif
1450
1451 /**
1452  *  inv_gyro_fs_show() - Get the current gyro full-scale range.
1453  */
1454 static ssize_t inv_gyro_fs_show(struct device *dev,
1455                                 struct device_attribute *attr, char *buf)
1456 {
1457         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1458
1459         return sprintf(buf, "%d\n", (1 << st->chip_config.fsr)*250);
1460 }
1461
1462 /**
1463  *  inv_accl_fs_show() - Get the current gyro full-scale range.
1464  */
1465 ssize_t inv_accl_fs_show(struct device *dev,
1466                          struct device_attribute *attr, char *buf)
1467 {
1468         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1469
1470         return sprintf(buf, "%d\n", 2 << st->chip_config.accl_fs);
1471 }
1472
1473 /**
1474  *  inv_clk_src_show() - Show the device's clock source.
1475  */
1476 static ssize_t inv_clk_src_show(struct device *dev,
1477                                 struct device_attribute *attr, char *buf)
1478 {
1479         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1480
1481         switch (st->chip_config.clk_src) {
1482         case INV_CLK_INTERNAL:
1483                 return sprintf(buf, "INTERNAL\n");
1484
1485         case INV_CLK_PLL:
1486                 return sprintf(buf, "Gyro PLL\n");
1487
1488         default:
1489                 return sprintf(buf, "Oh no!\n");
1490         }
1491 }
1492
1493 /**
1494  *  inv_fifo_rate_show() - Get the current sampling rate.
1495  */
1496 static ssize_t inv_fifo_rate_show(struct device *dev,
1497                                   struct device_attribute *attr, char *buf)
1498 {
1499         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1500
1501         return sprintf(buf, "%d\n", st->chip_config.fifo_rate);
1502 }
1503
1504 /**
1505  *  inv_enable_show() - Check if the chip are enabled.
1506  */
1507 static ssize_t inv_enable_show(struct device *dev,
1508                                struct device_attribute *attr, char *buf)
1509 {
1510         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1511
1512         return sprintf(buf, "%d\n", st->chip_config.enable);
1513 }
1514
1515 /**
1516  *  inv_gyro_fifo_enable_show() - Check if gyro FIFO are enabled.
1517  */
1518 ssize_t inv_gyro_fifo_enable_show(struct device *dev,
1519                                   struct device_attribute *attr, char *buf)
1520 {
1521         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1522
1523         return sprintf(buf, "%d\n", st->chip_config.gyro_fifo_enable);
1524 }
1525
1526 /**
1527  *  inv_accl_fifo_enable_show() - Check if accl FIFO are enabled.
1528  */
1529 ssize_t inv_accl_fifo_enable_show(struct device *dev,
1530                                   struct device_attribute *attr, char *buf)
1531 {
1532         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1533
1534         return sprintf(buf, "%d\n", st->chip_config.accl_fifo_enable);
1535 }
1536
1537 /**
1538  *  inv_gyro_enable_show() - Check if the FIFO and ring buffer are enabled.
1539  */
1540 ssize_t inv_gyro_enable_show(struct device *dev,
1541                              struct device_attribute *attr, char *buf)
1542 {
1543         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1544
1545         return sprintf(buf, "%d\n", st->chip_config.gyro_enable);
1546 }
1547
1548 /**
1549  *  inv_accl_enable_show() - Check if the FIFO and ring buffer are enabled.
1550  */
1551 ssize_t inv_accl_enable_show(struct device *dev,
1552                              struct device_attribute *attr, char *buf)
1553 {
1554         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1555
1556         return sprintf(buf, "%d\n", st->chip_config.accl_enable);
1557 }
1558
1559 /**
1560  *  inv_power_state_show() - Check if the device is on or in sleep mode.
1561  */
1562 static ssize_t inv_power_state_show(struct device *dev,
1563                                     struct device_attribute *attr, char *buf)
1564 {
1565         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1566
1567         if (st->chip_config.is_asleep)
1568                 return sprintf(buf, "0\n");
1569
1570         else
1571                 return sprintf(buf, "1\n");
1572 }
1573
1574 /**
1575  *  inv_temp_scale_show() - Get the temperature scale factor in LSBs/degree C.
1576  */
1577 static ssize_t inv_temp_scale_show(struct device *dev,
1578                                    struct device_attribute *attr, char *buf)
1579 {
1580         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1581
1582         if (INV_MPU3050 == st->chip_type)
1583                 return sprintf(buf, "280\n");
1584
1585         else
1586                 return sprintf(buf, "340\n");
1587 }
1588
1589 /**
1590  *  inv_temp_offset_show() - Get the temperature offset in LSBs/degree C.
1591  */
1592 static ssize_t inv_temp_offset_show(struct device *dev,
1593                                     struct device_attribute *attr, char *buf)
1594 {
1595         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1596
1597         if (INV_MPU3050 == st->chip_type)
1598                 return sprintf(buf, "-13200\n");
1599
1600         else
1601                 return sprintf(buf, "-521\n");
1602 }
1603
1604 /**
1605  *  inv_lpa_mode_show() - show current low power settings
1606  */
1607 static ssize_t inv_lpa_mode_show(struct device *dev,
1608                                  struct device_attribute *attr, char *buf)
1609 {
1610         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1611
1612         return sprintf(buf, "%d\n", st->chip_config.lpa_mode);
1613 }
1614
1615 /**
1616  *  inv_lpa_freq_show() - show current low power frequency setting
1617  */
1618 static ssize_t inv_lpa_freq_show(struct device *dev,
1619                                  struct device_attribute *attr, char *buf)
1620 {
1621         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1622
1623         switch (st->chip_config.lpa_freq) {
1624         case 0:
1625                 return sprintf(buf, "1.25\n");
1626
1627         case 1:
1628                 return sprintf(buf, "5\n");
1629
1630         case 2:
1631                 return sprintf(buf, "20\n");
1632
1633         case 3:
1634                 return sprintf(buf, "40\n");
1635
1636         default:
1637                 return sprintf(buf, "0\n");
1638         }
1639 }
1640
1641 /**
1642  *  inv_compass_scale_show() - show current compass scale settings
1643  */
1644 static ssize_t inv_compass_scale_show(struct device *dev,
1645                                       struct device_attribute *attr, char *buf)
1646 {
1647         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1648         long scale;
1649
1650         if (COMPASS_ID_AK8975 == st->plat_data.sec_slave_id)
1651                 scale = DATA_AKM8975_SCALE;
1652         else if (COMPASS_ID_AK8972 == st->plat_data.sec_slave_id)
1653                 scale = DATA_AKM8972_SCALE;
1654         else if (COMPASS_ID_AK8963 == st->plat_data.sec_slave_id)
1655                 if (st->compass_scale)
1656                         scale = DATA_AKM8963_SCALE1;
1657                 else
1658                         scale = DATA_AKM8963_SCALE0;
1659         else
1660                 return -EINVAL;
1661
1662         scale *= (1L << 15);
1663         return sprintf(buf, "%ld\n", scale);
1664 }
1665
1666 #ifdef CONFIG_HAS_EARLYSUSPEND
1667 /**
1668  *  inv_early_suspend_en_show() - show the current status of
1669  *  early_suspend_enable
1670  */
1671 static ssize_t inv_early_suspend_en_show(struct device *dev,
1672                                          struct device_attribute *attr,
1673                                          char *buf)
1674 {
1675         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1676
1677         return sprintf(buf, "%d\n", atomic_read(&st->early_suspend_enable));
1678 }
1679 #endif
1680
1681 /**
1682  *  inv_reg_dump_show() - Register dump for testing.
1683  *  TODO: Only for testing.
1684  */
1685 static ssize_t inv_reg_dump_show(struct device *dev,
1686                                  struct device_attribute *attr, char *buf)
1687 {
1688         int ii;
1689         char data;
1690         ssize_t bytes_printed = 0;
1691         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1692
1693         for (ii = 0; ii < st->hw->num_reg; ii++) {
1694                 /* don't read fifo r/w register */
1695                 if (ii == st->reg->fifo_r_w)
1696                         data = 0;
1697                 else
1698                         inv_i2c_read(st, ii, 1, &data);
1699                 bytes_printed += sprintf(buf + bytes_printed, "%#2x: %#2x\n",
1700                                          ii, data);
1701         }
1702         return bytes_printed;
1703 }
1704
1705 /**
1706  * inv_self_test_show() - self test result. 0 for fail; 1 for success.
1707  *                        calling this function will trigger self test
1708  *                        and return test result.
1709  */
1710 static ssize_t inv_self_test_show(struct device *dev,
1711                                   struct device_attribute *attr, char *buf)
1712 {
1713         int result;
1714         int bias[3];
1715         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1716
1717         if (INV_MPU3050 == st->chip_type) {
1718                 bias[0] = bias[1] = bias[2] = 0;
1719                 result = 0;
1720         } else {
1721                 result = inv_hw_self_test(st, bias);
1722         }
1723         return sprintf(buf, "%d, %d, %d, %d\n",
1724                 bias[0], bias[1], bias[2], result);
1725 }
1726
1727 /**
1728  * inv_get_accl_bias_show() - show accl bias value
1729  */
1730 static ssize_t inv_get_accl_bias_show(struct device *dev,
1731                                       struct device_attribute *attr, char *buf)
1732 {
1733         int result;
1734         int bias[3];
1735         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1736
1737         result = inv_get_accl_bias(st, bias);
1738         if (result)
1739                 return -EINVAL;
1740
1741         return sprintf(buf, "%d, %d, %d\n", bias[0], bias[1], bias[2]);
1742 }
1743
1744 /**
1745  * inv_gyro_matrix_show() - show orientation matrix
1746  */
1747 static ssize_t inv_gyro_matrix_show(struct device *dev,
1748                                     struct device_attribute *attr, char *buf)
1749 {
1750         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1751         signed char *m;
1752
1753         m = st->plat_data.orientation;
1754         return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
1755                        m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1756 }
1757
1758 /**
1759  * inv_accl_matrix_show() - show orientation matrix
1760  */
1761 ssize_t inv_accl_matrix_show(struct device *dev,
1762                              struct device_attribute *attr, char *buf)
1763 {
1764         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1765         signed char *m;
1766
1767         if (st->plat_data.sec_slave_type == SECONDARY_SLAVE_TYPE_ACCEL)
1768                 m = st->plat_data.secondary_orientation;
1769         else
1770                 m = st->plat_data.orientation;
1771         return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
1772                        m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1773 }
1774
1775 /**
1776  * inv_compass_matrix_show() - show orientation matrix
1777  */
1778 static ssize_t inv_compass_matrix_show(struct device *dev,
1779                                        struct device_attribute *attr,
1780                                        char *buf)
1781 {
1782         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1783         signed char *m;
1784
1785         if (st->plat_data.sec_slave_type == SECONDARY_SLAVE_TYPE_COMPASS)
1786                 m = st->plat_data.secondary_orientation;
1787         else
1788                 return -1;
1789
1790         return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
1791                        m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1792 }
1793
1794 /**
1795  * inv_key_show() -  calling this function will show the key
1796  *
1797  */
1798 static ssize_t inv_key_show(struct device *dev, struct device_attribute *attr,
1799                             char *buf)
1800 {
1801         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1802         unsigned char *key;
1803         key = st->plat_data.key;
1804
1805         return sprintf(buf,
1806                         "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
1807                         key[0], key[1], key[2], key[3], key[4], key[5], key[6],
1808                         key[7], key[8], key[9], key[10], key[11], key[12],
1809                         key[13], key[14], key[15]);
1810 }
1811
1812 /**
1813  * inv_firmware_loaded_show() -  calling this function will show current
1814  *                        firmware load status
1815  */
1816 static ssize_t inv_firmware_loaded_show(struct device *dev,
1817                                         struct device_attribute *attr,
1818                                         char *buf)
1819 {
1820         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1821
1822         return sprintf(buf, "%d\n", st->chip_config.firmware_loaded);
1823 }
1824
1825 /**
1826  * inv_compass_en_show() -  calling this function will show compass
1827  *                         enable status
1828  */
1829 static ssize_t inv_compass_en_show(struct device *dev,
1830                                    struct device_attribute *attr, char *buf)
1831 {
1832         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1833
1834         return sprintf(buf, "%d\n", st->chip_config.compass_enable);
1835 }
1836
1837 /**
1838  * inv_flick_lower_show() -  calling this function will show current
1839  *                        flick lower bound
1840  */
1841 static ssize_t inv_flick_lower_show(struct device *dev,
1842                                     struct device_attribute *attr, char *buf)
1843 {
1844         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1845
1846         return sprintf(buf, "%d\n", st->flick.lower);
1847 }
1848
1849 /**
1850  * inv_flick_upper_show() -  calling this function will show current
1851  *                        flick upper bound
1852  */
1853 static ssize_t inv_flick_upper_show(struct device *dev,
1854                                     struct device_attribute *attr, char *buf)
1855 {
1856         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1857
1858         return sprintf(buf, "%d\n", st->flick.upper);
1859 }
1860
1861 /**
1862  * inv_flick_counter_show() -  calling this function will show current
1863  *                        flick counter value
1864  */
1865 static ssize_t inv_flick_counter_show(struct device *dev,
1866                                       struct device_attribute *attr, char *buf)
1867 {
1868         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1869
1870         return sprintf(buf, "%d\n", st->flick.counter);
1871 }
1872
1873 /**
1874  * inv_flick_int_on_show() -  calling this function will show current
1875  *                        flick interrupt on value
1876  */
1877 static ssize_t inv_flick_int_on_show(struct device *dev,
1878                                      struct device_attribute *attr, char *buf)
1879 {
1880         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1881
1882         return sprintf(buf, "%d\n", st->flick.int_on);
1883 }
1884
1885 /**
1886  * inv_flick_axis_show() -  calling this function will show current
1887  *                        flick axis value
1888  */
1889 static ssize_t inv_flick_axis_show(struct device *dev,
1890                                    struct device_attribute *attr, char *buf)
1891 {
1892         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1893
1894         return sprintf(buf, "%d\n", st->flick.axis);
1895 }
1896
1897 /**
1898  * inv_flick_msg_on_show() -  calling this function will show current
1899  *                        flick message on value
1900  */
1901 static ssize_t inv_flick_msg_on_show(struct device *dev,
1902                                      struct device_attribute *attr, char *buf)
1903 {
1904         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1905
1906         return sprintf(buf, "%d\n", st->flick.msg_on);
1907 }
1908
1909 /**
1910  * inv_pedometer_steps_show() -  calling this function will store current
1911  *                        pedometer steps into MPU memory
1912  */
1913 static ssize_t inv_pedometer_steps_show(struct device *dev,
1914                                         struct device_attribute *attr,
1915                                         char *buf)
1916 {
1917         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1918         int result, data;
1919         unsigned char d[4];
1920
1921         if (st->chip_config.is_asleep)
1922                 return -EPERM;
1923
1924         result = mpu_memory_read(st->sl_handle, st->i2c_addr,
1925                                  inv_dmp_get_address(KEY_D_PEDSTD_STEPCTR),
1926                                  4, d);
1927         if (result)
1928                 return result;
1929
1930         data = be32_to_cpup((int *)d);
1931         return sprintf(buf, "%d\n", data);
1932 }
1933
1934 /**
1935  * inv_pedometer_time_show() -  calling this function will store current
1936  *                        pedometer steps into MPU memory
1937  */
1938 static ssize_t inv_pedometer_time_show(struct device *dev,
1939                                        struct device_attribute *attr,
1940                                        char *buf)
1941 {
1942         struct inv_gyro_state_s *st = dev_get_drvdata(dev);
1943         int result, data;
1944         unsigned char d[4];
1945
1946         if (st->chip_config.is_asleep)
1947                 return -EPERM;
1948
1949         result = mpu_memory_read(st->sl_handle, st->i2c_addr,
1950                                  inv_dmp_get_address(KEY_D_PEDSTD_TIMECTR),
1951                                  4, d);
1952         if (result)
1953                 return result;
1954
1955         data = be32_to_cpup((int *)d);
1956         return sprintf(buf, "%d\n", data);
1957 }
1958
1959 static void inv_report_gyro_accl(struct inv_gyro_state_s *st, s64 t,
1960                                  unsigned char *data)
1961 {
1962         short x, y, z;
1963         int ind;
1964         struct inv_chip_config_s *conf;
1965
1966         conf = &st->chip_config;
1967         ind = 0;
1968         if (conf->accl_fifo_enable | conf->dmp_on) {
1969                 x = ((data[ind] << 8)|data[ind + 1])*st->chip_info.multi;
1970                 y = ((data[ind + 2] << 8)|data[ind + 3])*st->chip_info.multi;
1971                 z = ((data[ind + 4] << 8)|data[ind + 5])*st->chip_info.multi;
1972                 if (conf->accl_fifo_enable) {
1973                         /*it is possible that accl disabled when dmp is on*/
1974                         input_report_rel(st->idev, REL_RX,  x);
1975                         input_report_rel(st->idev, REL_RY,  y);
1976                         input_report_rel(st->idev, REL_RZ,  z);
1977                 }
1978                 ind += 6;
1979         }
1980         if (conf->gyro_fifo_enable | conf->dmp_on) {
1981                 x = (data[ind] << 8)     | data[ind + 1];
1982                 y = (data[ind + 2] << 8) | data[ind + 3];
1983                 z = (data[ind + 4] << 8) | data[ind + 5];
1984                 if (conf->gyro_fifo_enable) {
1985                         /*it is possible that gyro disabled when dmp is on*/
1986                         input_report_rel(st->idev, REL_X,  x);
1987                         input_report_rel(st->idev, REL_Y,  y);
1988                         input_report_rel(st->idev, REL_Z,  z);
1989                 }
1990                 ind += 6;
1991         }
1992         if (conf->dmp_on) {
1993                 /* report tap information */
1994                 if (data[ind + 1] & 1) {
1995                         input_report_rel(st->idev_dmp, REL_RX, data[ind+3]);
1996                         input_sync(st->idev_dmp);
1997                 }
1998                 /* report orientation information */
1999                 if (data[ind + 1] & 2) {
2000                         input_report_rel(st->idev_dmp, REL_RY, data[ind+2]);
2001                         input_sync(st->idev_dmp);
2002                 }
2003         }
2004         if (conf->accl_fifo_enable | conf->gyro_fifo_enable) {
2005                 input_report_rel(st->idev, REL_MISC, (unsigned int)(t >> 32));
2006                 input_report_rel(st->idev, REL_WHEEL,
2007                         (unsigned int)(t & 0xffffffff));
2008                 input_sync(st->idev);
2009         }
2010 }
2011
2012 static int inv_report_compass(struct inv_gyro_state_s *st, s64 t)
2013 {
2014         short x, y, z;
2015         int result;
2016         unsigned char data[8];
2017
2018         /*mpu_memory_read(st->sl_handle,
2019                 st->i2c_addr,
2020                 14,
2021                 10, data);*/
2022         /*divider and counter is used to decrease the speed of read in
2023                 high frequency sample rate*/
2024         if (st->compass_divider == st->compass_counter) {
2025                 /*read from external sensor data register */
2026                 result = inv_i2c_read(st, REG_EXT_SENS_DATA_00, 8, data);
2027                 if (result)
2028                         return result;
2029
2030                 /* data[7] is status 2 register */
2031                 /*for AKM8975, bit 2 and 3 should be all be zero*/
2032                 /* for AMK8963, bit 3 should be zero*/
2033                 if ((DATA_AKM_DRDY == data[0]) &&
2034                         (0 == (data[7] & DATA_AKM_STAT_MASK))) {
2035                         unsigned char *sens;
2036                         sens = st->chip_info.compass_sens;
2037                         x = (short)((data[2] << 8) | data[1]);
2038                         y = (short)((data[4] << 8) | data[3]);
2039                         z = (short)((data[6] << 8) | data[5]);
2040                         x = ((x * (sens[0] + 128)) >> 8);
2041                         y = ((y * (sens[1] + 128)) >> 8);
2042                         z = ((z * (sens[2] + 128)) >> 8);
2043                         input_report_rel(st->idev_compass, REL_X, x);
2044                         input_report_rel(st->idev_compass, REL_Y, y);
2045                         input_report_rel(st->idev_compass, REL_Z, z);
2046                         input_report_rel(st->idev_compass, REL_MISC,
2047                                          (unsigned int)(t >> 32));
2048                         input_report_rel(st->idev_compass, REL_WHEEL,
2049                                          (unsigned int)(t & 0xffffffff));
2050                         input_sync(st->idev_compass);
2051                 }
2052                 st->compass_counter = 0;
2053
2054         } else if (st->compass_divider != 0) {
2055                 st->compass_counter++;
2056         }
2057
2058         return 0;
2059 }
2060
2061 /**
2062  *  inv_read_fifo() - Transfer data from FIFO to ring buffer.
2063  */
2064 static irqreturn_t inv_read_fifo(int irq, void *dev_id)
2065 {
2066         struct inv_gyro_state_s *st;
2067         unsigned char bytes_per_datum;
2068         const unsigned short fifo_thresh = 500;
2069         int result;
2070         unsigned char data[16];
2071         unsigned short fifo_count;
2072         unsigned int copied;
2073         s64 timestamp;
2074         struct inv_reg_map_s *reg;
2075
2076         st = (struct inv_gyro_state_s *)dev_id;
2077         reg = st->reg;
2078         if (st->chip_config.is_asleep)
2079                 goto end_session;
2080
2081         if (!(st->chip_config.enable))
2082                 goto end_session;
2083
2084         if (!(st->chip_config.accl_fifo_enable |
2085                 st->chip_config.gyro_fifo_enable |
2086                 st->chip_config.dmp_on |
2087                 st->chip_config.compass_enable))
2088                 goto end_session;
2089
2090         if (st->chip_config.dmp_on && st->flick.int_on) {
2091                 /*dmp interrupt status */
2092                 inv_i2c_read(st, REG_DMP_INT_STATUS, 2, data);
2093                 if (data[0] & 8) {
2094                         input_report_rel(st->idev_dmp, REL_RZ, data[0]);
2095                         input_sync(st->idev_dmp);
2096                 }
2097         }
2098         if (st->chip_config.lpa_mode) {
2099                 result = inv_i2c_read(st, reg->raw_accl, 6, data);
2100                 if (result)
2101                         goto end_session;
2102
2103                 inv_report_gyro_accl(st, get_time_ns(), data);
2104                 goto end_session;
2105         }
2106
2107         if (st->chip_config.dmp_on)
2108                 bytes_per_datum = BYTES_FOR_DMP;
2109         else
2110                 bytes_per_datum = (st->chip_config.accl_fifo_enable +
2111                             st->chip_config.gyro_fifo_enable)*BYTES_PER_SENSOR;
2112         fifo_count = 0;
2113         if (bytes_per_datum != 0) {
2114                 result = inv_i2c_read(st, reg->fifo_count_h, 2, data);
2115                 if (result)
2116                         goto end_session;
2117
2118                 fifo_count = (data[0] << 8) + data[1];
2119                 if (fifo_count < bytes_per_datum)
2120                         goto end_session;
2121
2122                 if (fifo_count%2)
2123                         goto flush_fifo;
2124
2125                 if (fifo_count > fifo_thresh)
2126                         goto flush_fifo;
2127
2128                 /* Timestamp mismatch. */
2129                 if (kfifo_len(&st->trigger.timestamps) <
2130                         fifo_count / bytes_per_datum)
2131                         goto flush_fifo;
2132
2133                 if (kfifo_len(&st->trigger.timestamps) >
2134                         fifo_count / bytes_per_datum + TIME_STAMP_TOR) {
2135                         if (st->chip_config.dmp_on) {
2136                                 result = kfifo_to_user(&st->trigger.timestamps,
2137                                 &timestamp, sizeof(timestamp), &copied);
2138                                 if (result)
2139                                         goto flush_fifo;
2140
2141                         } else {
2142                                 goto flush_fifo;
2143                         }
2144                 }
2145         }
2146
2147         if (bytes_per_datum == 0) {
2148                 result = kfifo_to_user(&st->trigger.timestamps,
2149                         &timestamp, sizeof(timestamp), &copied);
2150                 if (result)
2151                         goto flush_fifo;
2152         }
2153
2154         while ((bytes_per_datum != 0) && (fifo_count >= bytes_per_datum)) {
2155                 result = inv_i2c_read(st, reg->fifo_r_w, bytes_per_datum,
2156                         data);
2157                 if (result)
2158                         goto flush_fifo;
2159
2160                 result = kfifo_to_user(&st->trigger.timestamps,
2161                         &timestamp, sizeof(timestamp), &copied);
2162                 if (result)
2163                         goto flush_fifo;
2164
2165                 inv_report_gyro_accl(st, timestamp, data);
2166                 fifo_count -= bytes_per_datum;
2167         }
2168
2169         if (st->chip_config.compass_enable)
2170                 inv_report_compass(st, timestamp);
2171
2172 end_session:
2173         return IRQ_HANDLED;
2174
2175 flush_fifo:
2176         /* Flush HW and SW FIFOs. */
2177         inv_reset_fifo(st);
2178         inv_clear_kfifo(st);
2179         return IRQ_HANDLED;
2180 }
2181
2182 /**
2183  *  inv_irq_handler() - Cache a timestamp at each data ready interrupt.
2184  */
2185 static irqreturn_t inv_irq_handler(int irq, void *dev_id)
2186 {
2187         struct inv_gyro_state_s *st;
2188         long long timestamp;
2189         int result, catch_up;
2190         unsigned int time_since_last_irq;
2191
2192         st = (struct inv_gyro_state_s *)dev_id;
2193         timestamp = get_time_ns();
2194         time_since_last_irq = ((unsigned int)(timestamp - st->last_isr_time)) /
2195                               ONE_K_HZ;
2196         spin_lock(&st->time_stamp_lock);
2197         catch_up = 0;
2198         while ((time_since_last_irq > st->irq_dur_us*2) &&
2199                (catch_up < MAX_CATCH_UP) && (0 == st->chip_config.lpa_mode)) {
2200                 st->last_isr_time += st->irq_dur_us * ONE_K_HZ;
2201                 result = kfifo_in(&st->trigger.timestamps,
2202                                   &st->last_isr_time, 1);
2203                 time_since_last_irq = ((unsigned int)(timestamp -
2204                                         st->last_isr_time)) / ONE_K_HZ;
2205                 catch_up++;
2206         }
2207         result = kfifo_in(&st->trigger.timestamps, &timestamp, 1);
2208         st->last_isr_time = timestamp;
2209         spin_unlock(&st->time_stamp_lock);
2210         return IRQ_WAKE_THREAD;
2211 }
2212
2213 #ifdef CONFIG_HAS_EARLYSUSPEND
2214 /**
2215  * inv_early_suspend - callback function for early_suspend
2216  */
2217 static void inv_early_suspend(struct early_suspend *h)
2218 {
2219         struct inv_gyro_state_s *st =
2220                        container_of(h, struct inv_gyro_state_s, early_suspend);
2221
2222         if (atomic_read(&st->early_suspend_enable) == 1)
2223                 inv_set_power_state(st, 0);
2224 }
2225
2226 /**
2227  * inv_late_resume - callback function for late_resume
2228  */
2229 static void inv_late_resume(struct early_suspend *h)
2230 {
2231         struct inv_gyro_state_s *st =
2232                        container_of(h, struct inv_gyro_state_s, early_suspend);
2233
2234         if (atomic_read(&st->early_suspend_enable) == 1)
2235                 inv_set_power_state(st, 1);
2236 }
2237 #endif
2238
2239 static DEVICE_ATTR(raw_gyro, S_IRUGO, inv_raw_gyro_show, NULL);
2240 static DEVICE_ATTR(raw_accl, S_IRUGO, inv_raw_accl_show, NULL);
2241 static DEVICE_ATTR(temperature, S_IRUGO, inv_temperature_show, NULL);
2242 static DEVICE_ATTR(fifo_rate, S_IRUGO | S_IWUSR, inv_fifo_rate_show,
2243                    inv_fifo_rate_store);
2244 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, inv_enable_show,
2245                    inv_enable_store);
2246 static DEVICE_ATTR(gyro_fifo_enable, S_IRUGO | S_IWUSR,
2247                    inv_gyro_fifo_enable_show, inv_gyro_fifo_enable_store);
2248 static DEVICE_ATTR(gyro_enable, S_IRUGO | S_IWUSR, inv_gyro_enable_show,
2249                    inv_gyro_enable_store);
2250 static DEVICE_ATTR(accl_fifo_enable, S_IRUGO | S_IWUSR,
2251                    inv_accl_fifo_enable_show, inv_accl_fifo_enable_store);
2252 static DEVICE_ATTR(accl_enable, S_IRUGO | S_IWUSR, inv_accl_enable_show,
2253                    inv_accl_enable_store);
2254 static DEVICE_ATTR(accl_fs, S_IRUGO | S_IWUSR, inv_accl_fs_show,
2255                    inv_accl_fs_store);
2256 static DEVICE_ATTR(gyro_fs, S_IRUGO | S_IWUSR, inv_gyro_fs_show,
2257                    inv_gyro_fs_store);
2258 static DEVICE_ATTR(clock_source, S_IRUGO, inv_clk_src_show, NULL);
2259 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, inv_power_state_show,
2260                    inv_power_state_store);
2261 static DEVICE_ATTR(firmware_loaded, S_IRUGO | S_IWUSR,
2262                    inv_firmware_loaded_show, inv_firmware_loaded_store);
2263 static DEVICE_ATTR(lpa_mode, S_IRUGO | S_IWUSR, inv_lpa_mode_show,
2264                    inv_lpa_mode_store);
2265 static DEVICE_ATTR(lpa_freq, S_IRUGO | S_IWUSR, inv_lpa_freq_show,
2266                    inv_lpa_freq_store);
2267 static DEVICE_ATTR(compass_enable, S_IRUGO | S_IWUSR, inv_compass_en_show,
2268                    inv_compass_en_store);
2269 static DEVICE_ATTR(compass_scale, S_IRUGO | S_IWUSR, inv_compass_scale_show,
2270                    inv_compass_scale_store);
2271 static DEVICE_ATTR(temp_scale, S_IRUGO, inv_temp_scale_show, NULL);
2272 static DEVICE_ATTR(temp_offset, S_IRUGO, inv_temp_offset_show, NULL);
2273 static DEVICE_ATTR(reg_dump, S_IRUGO, inv_reg_dump_show, NULL);
2274 static DEVICE_ATTR(self_test, S_IRUGO, inv_self_test_show, NULL);
2275 static DEVICE_ATTR(key, S_IRUGO | S_IWUSR, inv_key_show, inv_key_store);
2276 static DEVICE_ATTR(gyro_matrix, S_IRUGO, inv_gyro_matrix_show, NULL);
2277 static DEVICE_ATTR(accl_matrix, S_IRUGO, inv_accl_matrix_show, NULL);
2278 static DEVICE_ATTR(compass_matrix, S_IRUGO, inv_compass_matrix_show, NULL);
2279 static DEVICE_ATTR(accl_bias, S_IRUGO, inv_get_accl_bias_show, NULL);
2280 static DEVICE_ATTR(flick_lower, S_IRUGO | S_IWUSR, inv_flick_lower_show,
2281                    inv_flick_lower_store);
2282 static DEVICE_ATTR(flick_upper, S_IRUGO | S_IWUSR, inv_flick_upper_show,
2283                    inv_flick_upper_store);
2284 static DEVICE_ATTR(flick_counter, S_IRUGO | S_IWUSR, inv_flick_counter_show,
2285                    inv_flick_counter_store);
2286 static DEVICE_ATTR(flick_message_on, S_IRUGO | S_IWUSR, inv_flick_msg_on_show,
2287                    inv_flick_msg_on_store);
2288 static DEVICE_ATTR(flick_int_on, S_IRUGO | S_IWUSR, inv_flick_int_on_show,
2289                    inv_flick_int_on_store);
2290 static DEVICE_ATTR(flick_axis, S_IRUGO | S_IWUSR, inv_flick_axis_show,
2291                    inv_flick_axis_store);
2292 static DEVICE_ATTR(pedometer_time, S_IRUGO | S_IWUSR, inv_pedometer_time_show,
2293                    inv_pedometer_time_store);
2294 static DEVICE_ATTR(pedometer_steps, S_IRUGO | S_IWUSR,
2295                    inv_pedometer_steps_show, inv_pedometer_steps_store);
2296
2297 #ifdef CONFIG_HAS_EARLYSUSPEND
2298 static DEVICE_ATTR(early_suspend_enable, S_IRUGO | S_IWUSR,
2299         inv_early_suspend_en_show,
2300         inv_early_suspend_en_store);
2301 #endif
2302
2303 static struct device_attribute *inv_attributes[] = {
2304         &dev_attr_raw_gyro,
2305         &dev_attr_temperature,
2306         &dev_attr_fifo_rate,
2307         &dev_attr_enable,
2308         &dev_attr_clock_source,
2309         &dev_attr_power_state,
2310         &dev_attr_gyro_fs,
2311         &dev_attr_temp_scale,
2312         &dev_attr_temp_offset,
2313         &dev_attr_reg_dump,
2314         &dev_attr_self_test,
2315         &dev_attr_key,
2316         &dev_attr_gyro_matrix,
2317 #ifdef CONFIG_HAS_EARLYSUSPEND
2318         &dev_attr_early_suspend_enable,
2319 #endif
2320         NULL
2321 };
2322
2323 static struct device_attribute *inv_mpu6050_attributes[] = {
2324         &dev_attr_gyro_fifo_enable,
2325         &dev_attr_gyro_enable,
2326         &dev_attr_accl_fifo_enable,
2327         &dev_attr_accl_enable,
2328         &dev_attr_accl_fs,
2329         &dev_attr_accl_bias,
2330         &dev_attr_raw_accl,
2331         &dev_attr_accl_matrix,
2332         &dev_attr_firmware_loaded,
2333         &dev_attr_lpa_mode,
2334         &dev_attr_lpa_freq,
2335         &dev_attr_flick_lower,
2336         &dev_attr_flick_upper,
2337         &dev_attr_flick_counter,
2338         &dev_attr_flick_message_on,
2339         &dev_attr_flick_int_on,
2340         &dev_attr_flick_axis,
2341         &dev_attr_pedometer_time,
2342         &dev_attr_pedometer_steps,
2343         NULL
2344 };
2345
2346 static struct device_attribute *inv_compass_attributes[] = {
2347         &dev_attr_compass_enable,
2348         &dev_attr_compass_scale,
2349         &dev_attr_compass_matrix,
2350         NULL
2351 };
2352
2353 static int inv_setup_compass(struct inv_gyro_state_s *st)
2354 {
2355         int result;
2356         unsigned char data[4];
2357
2358         result = inv_i2c_read(st, 1, 1, data);
2359         if (result)
2360                 return result;
2361
2362         data[0] &= ~(0x80);
2363         data[0] |= (st->plat_data.level_shifter << 7);
2364         /*set up VDDIO register */
2365         result = inv_i2c_single_write(st, 1, data[0]);
2366         if (result)
2367                 return result;
2368
2369         /* set to bypass mode */
2370         result = inv_i2c_single_write(st, REG_INT_PIN_CFG, BIT_BYPASS_EN);
2371         if (result)
2372                 return result;
2373
2374         /*read secondary i2c ID register */
2375         result = inv_secondary_read(REG_AKM_ID, 2, data);
2376         if (result)
2377                 return result;
2378
2379         if (data[0] != DATA_AKM_ID)
2380                 return -ENXIO;
2381
2382         /*set AKM to Fuse ROM access mode */
2383         result = inv_secondary_write(REG_AKM_MODE, DATA_AKM_MODE_PW_FR);
2384         if (result)
2385                 return result;
2386
2387         result = inv_secondary_read(REG_AKM_SENSITIVITY, 3,
2388                                         st->chip_info.compass_sens);
2389         if (result)
2390                 return result;
2391
2392         /*revert to power down mode */
2393         result = inv_secondary_write(REG_AKM_MODE, DATA_AKM_MODE_PW_DN);
2394         if (result)
2395                 return result;
2396
2397         printk(KERN_ERR"senx=%d, seny=%d,senz=%d\n",
2398                 st->chip_info.compass_sens[0],
2399                 st->chip_info.compass_sens[1],
2400                 st->chip_info.compass_sens[2]);
2401         /*restore to non-bypass mode */
2402         result = inv_i2c_single_write(st, REG_INT_PIN_CFG, 0);
2403         if (result)
2404                 return result;
2405
2406         /*setup master mode and master clock and ES bit*/
2407         result = inv_i2c_single_write(st, REG_I2C_MST_CTRL, BIT_WAIT_FOR_ES);
2408         if (result)
2409                 return result;
2410
2411         /* slave 0 is used to read data from compass */
2412         /*read mode */
2413         result = inv_i2c_single_write(st, REG_I2C_SLV0_ADDR, BIT_I2C_READ|
2414                 st->plat_data.secondary_i2c_addr);
2415         if (result)
2416                 return result;
2417
2418         /* AKM status register address is 2 */
2419         result = inv_i2c_single_write(st, REG_I2C_SLV0_REG, REG_AKM_STATUS);
2420         if (result)
2421                 return result;
2422
2423         /* slave 0 is enabled at the beginning, read 8 bytes from here */
2424         result = inv_i2c_single_write(st, REG_I2C_SLV0_CTRL, BIT_SLV_EN | 8);
2425         if (result)
2426                 return result;
2427
2428         /*slave 1 is used for AKM mode change only*/
2429         /*write mode, slave address 0x0E */
2430         result = inv_i2c_single_write(st, REG_I2C_SLV1_ADDR,
2431                 st->plat_data.secondary_i2c_addr);
2432         if (result)
2433                 return result;
2434
2435         /* AKM mode register address is 0x0A */
2436         result = inv_i2c_single_write(st, REG_I2C_SLV1_REG, REG_AKM_MODE);
2437         if (result)
2438                 return result;
2439
2440         /* slave 1 is enabled, byte length is 1 */
2441         result = inv_i2c_single_write(st, REG_I2C_SLV1_CTRL, BIT_SLV_EN | 1);
2442         if (result)
2443                 return result;
2444
2445         /* output data for slave 1 is fixed, single measure mode*/
2446         st->compass_scale = 1;
2447         if (COMPASS_ID_AK8975 == st->plat_data.sec_slave_id) {
2448                 st->compass_st_upper[0] = DATA_AKM8975_ST_X_UP;
2449                 st->compass_st_upper[1] = DATA_AKM8975_ST_Y_UP;
2450                 st->compass_st_upper[2] = DATA_AKM8975_ST_Z_UP;
2451                 st->compass_st_lower[0] = DATA_AKM8975_ST_X_LW;
2452                 st->compass_st_lower[1] = DATA_AKM8975_ST_Y_LW;
2453                 st->compass_st_lower[2] = DATA_AKM8975_ST_Z_LW;
2454                 data[0] = 1;
2455         } else if (COMPASS_ID_AK8972 == st->plat_data.sec_slave_id) {
2456                 st->compass_st_upper[0] = DATA_AKM8972_ST_X_UP;
2457                 st->compass_st_upper[1] = DATA_AKM8972_ST_Y_UP;
2458                 st->compass_st_upper[2] = DATA_AKM8972_ST_Z_UP;
2459                 st->compass_st_lower[0] = DATA_AKM8972_ST_X_LW;
2460                 st->compass_st_lower[1] = DATA_AKM8972_ST_Y_LW;
2461                 st->compass_st_lower[2] = DATA_AKM8972_ST_Z_LW;
2462                 data[0] = 1;
2463         } else if (COMPASS_ID_AK8963 == st->plat_data.sec_slave_id) {
2464                 st->compass_st_upper[0] = DATA_AKM8963_ST_X_UP;
2465                 st->compass_st_upper[1] = DATA_AKM8963_ST_Y_UP;
2466                 st->compass_st_upper[2] = DATA_AKM8963_ST_Z_UP;
2467                 st->compass_st_lower[0] = DATA_AKM8963_ST_X_LW;
2468                 st->compass_st_lower[1] = DATA_AKM8963_ST_Y_LW;
2469                 st->compass_st_lower[2] = DATA_AKM8963_ST_Z_LW;
2470                 data[0] = (1 | (st->compass_scale << 4));
2471         }
2472         result = inv_i2c_single_write(st, REG_I2C_SLV1_DO, data[0]);
2473         if (result)
2474                 return result;
2475
2476         /* slave 0 and 1 timer action is enabled every sample*/
2477         result = inv_i2c_single_write(st, REG_I2C_MST_DELAY_CTRL,
2478                                 BIT_SLV0_DLY_EN | BIT_SLV1_DLY_EN);
2479         return result;
2480 }
2481
2482 static int inv_check_chip_type(struct inv_gyro_state_s *st,
2483                                const struct i2c_device_id *id)
2484 {
2485         struct inv_reg_map_s *reg;
2486         int result;
2487
2488         reg = st->reg;
2489         st->mpu_slave = NULL;
2490         if (!strcmp(id->name, "itg3500"))
2491                 st->chip_type = INV_ITG3500;
2492         else if (!strcmp(id->name, "mpu3050")) {
2493                 st->chip_type = INV_MPU3050;
2494                 inv_setup_reg_mpu3050(reg);
2495         } else if (!strcmp(id->name, "mpu6050"))
2496                 st->chip_type = INV_MPU6050;
2497         else if (!strcmp(id->name, "mpu9150"))
2498                 st->chip_type = INV_MPU9150;
2499         if (INV_MPU9150 == st->chip_type) {
2500                 st->plat_data.sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS;
2501                 st->plat_data.sec_slave_id = COMPASS_ID_AK8975;
2502                 /*is MPU9150 i2c address hard coded? */
2503                 /*st->plat_data.secondary_i2c_addr = 0xE;*/
2504                 st->has_compass = 1;
2505         }
2506         if (SECONDARY_SLAVE_TYPE_ACCEL == st->plat_data.sec_slave_type) {
2507                 if (st->plat_data.sec_slave_id == ACCEL_ID_KXTF9)
2508                         inv_register_kxtf9_slave(st);
2509         }
2510         if (SECONDARY_SLAVE_TYPE_COMPASS == st->plat_data.sec_slave_type)
2511                 st->has_compass = 1;
2512         else
2513                 st->has_compass = 0;
2514         st->chip_config.gyro_enable = 1;
2515         /*reset register to power up default*/
2516         result = inv_i2c_single_write(st, reg->pwr_mgmt_1, BIT_RESET);
2517         if (result)
2518                 return result;
2519
2520         mdelay(POWER_UP_TIME);
2521         result = inv_set_power_state(st, 1);
2522         if (result)
2523                 return result;
2524
2525         if (st->has_compass) {
2526                 result = inv_setup_compass(st);
2527                 if (result)
2528                         return result;
2529         }
2530
2531         return 0;
2532 }
2533
2534 static int inv_create_input(struct inv_gyro_state_s *st,
2535                 struct i2c_client *client){
2536         struct input_dev *idev;
2537         int result;
2538
2539         idev = NULL;
2540         result = inv_setup_input(st, &idev, client, st->hw->name);
2541         if (result)
2542                 return result;
2543
2544         st->idev = idev;
2545         if (INV_ITG3500 == st->chip_type)
2546                 return 0;
2547
2548         result = inv_setup_input(st, &idev, client, "INV_DMP");
2549         if (result) {
2550                 input_unregister_device(st->idev);
2551                 return result;
2552         }
2553
2554         st->idev_dmp = idev;
2555         if (!st->has_compass)
2556                 return 0;
2557
2558         if (st->has_compass) {
2559                 result = inv_setup_input(st, &idev, client, "INV_COMPASS");
2560                 if (result) {
2561                         input_unregister_device(st->idev);
2562                         input_unregister_device(st->idev_dmp);
2563                         return result;
2564                 }
2565
2566                 st->idev_compass = idev;
2567         }
2568
2569         return 0;
2570 }
2571
2572 int create_device_attributes(struct device *dev,
2573         struct device_attribute **attrs)
2574 {
2575         int i;
2576         int err = 0;
2577
2578         for (i = 0 ; NULL != attrs[i] ; ++i) {
2579                 err = sysfs_create_file(&dev->kobj, &attrs[i]->attr);
2580                 if (0 != err)
2581                         break;
2582         }
2583         if (0 != err) {
2584                 for (; i >= 0 ; --i)
2585                         sysfs_remove_file(&dev->kobj, &attrs[i]->attr);
2586         }
2587         return err;
2588 }
2589
2590 void remove_device_attributes(struct device *dev,
2591         struct device_attribute **attrs)
2592 {
2593         int i;
2594
2595         for (i = 0 ; NULL != attrs[i] ; ++i)
2596                 device_remove_file(dev, attrs[i]);
2597 }
2598
2599 static char const *const inv_class_name = "invensense";
2600 static char const *const inv_device_name = "mpu";
2601 static dev_t const inv_device_dev_t = MKDEV(MISC_MAJOR, MISC_DYNAMIC_MINOR);
2602 static struct bin_attribute dmp_firmware = {
2603         .attr = {
2604                 .name = "dmp_firmware",
2605                 .mode = S_IRUGO | S_IWUSR
2606         },
2607         .size = 4096,
2608         .read = inv_dmp_firmware_read,
2609         .write = inv_dmp_firmware_write,
2610 };
2611
2612 static int create_sysfs_interfaces(struct inv_gyro_state_s *st)
2613 {
2614         int result;
2615         result = 0;
2616
2617         st->inv_class = class_create(THIS_MODULE, inv_class_name);
2618         if (IS_ERR(st->inv_class)) {
2619                 result = PTR_ERR(st->inv_class);
2620                 goto exit_nullify_class;
2621         }
2622
2623         st->inv_dev = device_create(st->inv_class, &st->i2c->dev,
2624                         inv_device_dev_t, st, inv_device_name);
2625         if (IS_ERR(st->inv_dev)) {
2626                 result = PTR_ERR(st->inv_dev);
2627                 goto exit_destroy_class;
2628         }
2629
2630         result = create_device_attributes(st->inv_dev, inv_attributes);
2631         if (result < 0)
2632                 goto exit_destroy_device;
2633
2634         if (INV_ITG3500 == st->chip_type)
2635                 return 0;
2636
2637         result = sysfs_create_bin_file(&st->inv_dev->kobj, &dmp_firmware);
2638         if (result < 0)
2639                 goto exit_remove_device_attributes;
2640
2641         if (INV_MPU3050 == st->chip_type) {
2642                 result = inv_mpu3050_create_sysfs(st);
2643                 if (result)
2644                         goto exit_remove_bin_file;
2645
2646                 return 0;
2647         }
2648
2649         result = create_device_attributes(st->inv_dev, inv_mpu6050_attributes);
2650         if (result < 0)
2651                 goto exit_remove_bin_file;
2652
2653         if (!st->has_compass)
2654                 return 0;
2655
2656         result = create_device_attributes(st->inv_dev, inv_compass_attributes);
2657         if (result < 0)
2658                 goto exit_remove_6050_attributes;
2659
2660         return 0;
2661
2662 exit_remove_6050_attributes:
2663         remove_device_attributes(st->inv_dev, inv_mpu6050_attributes);
2664 exit_remove_bin_file:
2665         sysfs_remove_bin_file(&st->inv_dev->kobj, &dmp_firmware);
2666 exit_remove_device_attributes:
2667         remove_device_attributes(st->inv_dev, inv_attributes);
2668 exit_destroy_device:
2669         device_destroy(st->inv_class, inv_device_dev_t);
2670 exit_destroy_class:
2671         st->inv_dev = NULL;
2672         class_destroy(st->inv_class);
2673 exit_nullify_class:
2674         st->inv_class = NULL;
2675         return result;
2676 }
2677
2678 static void remove_sysfs_interfaces(struct inv_gyro_state_s *st)
2679 {
2680         remove_device_attributes(st->inv_dev, inv_attributes);
2681         if (INV_ITG3500 != st->chip_type)
2682                 sysfs_remove_bin_file(&st->inv_dev->kobj, &dmp_firmware);
2683         if ((INV_ITG3500 != st->chip_type) && (INV_MPU3050 != st->chip_type))
2684                 remove_device_attributes(st->inv_dev, inv_mpu6050_attributes);
2685         if (INV_MPU3050 == st->chip_type)
2686                 inv_mpu3050_remove_sysfs(st);
2687         if (st->has_compass)
2688                 remove_device_attributes(st->inv_dev, inv_compass_attributes);
2689         device_destroy(st->inv_class, inv_device_dev_t);
2690         class_destroy(st->inv_class);
2691         st->inv_dev = NULL;
2692         st->inv_class = NULL;
2693 }
2694
2695 static int inv_mod_probe(struct i2c_client *client,
2696                          const struct i2c_device_id *id)
2697 {
2698         struct inv_gyro_state_s *st;
2699         int result;
2700
2701         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2702                 result = -ENODEV;
2703                 goto out_no_free;
2704         }
2705
2706         st = kzalloc(sizeof(*st), GFP_KERNEL);
2707         if (!st) {
2708                 result = -ENOMEM;
2709                 goto out_no_free;
2710         }
2711
2712         /* Make state variables available to all _show and _store functions. */
2713         i2c_set_clientdata(client, st);
2714         st->i2c = client;
2715         st->sl_handle = client->adapter;
2716         st->reg = (struct inv_reg_map_s *)&chip_reg ;
2717         st->hw  = (struct inv_hw_s *)hw_info;
2718         st->i2c_addr = client->addr;
2719         st->plat_data =
2720                 *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
2721         /* power is turned on inside check chip type*/
2722         result = inv_check_chip_type(st, id);
2723         if (result)
2724                 goto out_free;
2725
2726         st->hw  = (struct inv_hw_s *)(hw_info  + st->chip_type);
2727         if (INV_MPU3050 == st->chip_type)
2728                 result = inv_init_config_mpu3050(st);
2729         else
2730                 result = inv_init_config(st);
2731         if (result) {
2732                 dev_err(&client->adapter->dev,
2733                         "Could not initialize device.\n");
2734                 goto out_free;
2735         }
2736
2737         if (INV_ITG3500 != st->chip_type && INV_MPU3050 != st->chip_type) {
2738                 result = inv_get_silicon_rev_mpu6050(st);
2739                 if (result) {
2740                         dev_err(&client->adapter->dev,
2741                                 "%s get silicon error.\n", st->hw->name);
2742                         goto out_free;
2743                 }
2744         }
2745
2746         result = inv_set_power_state(st, 0);
2747         if (result) {
2748                 dev_err(&client->adapter->dev,
2749                         "%s could not be turned off.\n", st->hw->name);
2750                 goto out_free;
2751         }
2752
2753         INIT_KFIFO(st->trigger.timestamps);
2754         result = create_sysfs_interfaces(st);
2755         if (result)
2756                 goto out_free_kfifo;
2757
2758         if (!client->irq) {
2759                 dev_err(&client->adapter->dev, "IRQ not assigned.\n");
2760                 result = -EPERM;
2761                 goto out_close_sysfs;
2762         }
2763
2764         st->trigger.irq = client->irq;
2765         if (INV_MPU3050 == st->chip_type)
2766                 result = request_threaded_irq(client->irq, inv_irq_handler,
2767                                               inv_read_fifo_mpu3050,
2768                                               IRQF_TRIGGER_RISING |
2769                                               IRQF_SHARED, "inv_irq", st);
2770         else
2771                 result = request_threaded_irq(client->irq, inv_irq_handler,
2772                                               inv_read_fifo,
2773                                               IRQF_TRIGGER_RISING |
2774                                               IRQF_SHARED, "inv_irq", st);
2775         if (result)
2776                 goto out_close_sysfs;
2777
2778         spin_lock_init(&st->time_stamp_lock);
2779
2780 #ifdef CONFIG_HAS_EARLYSUSPEND
2781         atomic_set(&st->early_suspend_enable, 1);
2782         st->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2783         st->early_suspend.suspend = inv_early_suspend;
2784         st->early_suspend.resume = inv_late_resume;
2785         register_early_suspend(&st->early_suspend);
2786 #endif
2787
2788         result = inv_create_input(st, client);
2789         if (result) {
2790 #ifdef CONFIG_HAS_EARLYSUSPEND
2791                 unregister_early_suspend(&st->early_suspend);
2792 #endif
2793                 free_irq(client->irq, st);
2794                 goto out_close_sysfs;
2795         }
2796
2797         pr_info("%s: Probe name %s\n", __func__, id->name);
2798         dev_info(&client->adapter->dev, "%s is ready to go!\n", st->hw->name);
2799         return 0;
2800
2801 out_close_sysfs:
2802         remove_sysfs_interfaces(st);
2803 out_free_kfifo:
2804         kfifo_free(&st->trigger.timestamps);
2805 out_free:
2806         result = inv_i2c_single_write(st, st->reg->pwr_mgmt_1, BIT_RESET);
2807         kfree(st);
2808 out_no_free:
2809         dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
2810         return -EIO;
2811 }
2812
2813 static int inv_mod_remove(struct i2c_client *client)
2814 {
2815         int result;
2816         struct inv_gyro_state_s *st = i2c_get_clientdata(client);
2817
2818         result = inv_set_power_state(st, 0);
2819         if (result)
2820                 dev_err(&client->adapter->dev, "%s could not be turned off.\n",
2821                         st->hw->name);
2822         remove_sysfs_interfaces(st);
2823         result = inv_i2c_single_write(st, st->reg->pwr_mgmt_1, BIT_RESET);
2824         kfifo_free(&st->trigger.timestamps);
2825         free_irq(client->irq, st);
2826         input_unregister_device(st->idev);
2827         if (INV_ITG3500 != st->chip_type)
2828                 input_unregister_device(st->idev_dmp);
2829         if (st->has_compass)
2830                 input_unregister_device(st->idev_compass);
2831 #ifdef CONFIG_HAS_EARLYSUSPEND
2832         unregister_early_suspend(&st->early_suspend);
2833 #endif
2834         kfree(st);
2835         dev_info(&client->adapter->dev, "Gyro module removed.\n");
2836         return 0;
2837 }
2838
2839 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
2840
2841 /* device id table is used to identify what device can be
2842  * supported by this driver
2843  */
2844 static struct i2c_device_id inv_mod_id[] = {
2845         {"itg3500", 0},
2846         {"mpu3050", 0},
2847         {"mpu6050", 0},
2848         {"mpu9150", 0},
2849         {}
2850 };
2851
2852 MODULE_DEVICE_TABLE(i2c, inv_mod_id);
2853
2854 static struct i2c_driver inv_mod_driver = {
2855         .class = I2C_CLASS_HWMON,
2856         .probe          =       inv_mod_probe,
2857         .remove         =       inv_mod_remove,
2858         .id_table       =       inv_mod_id,
2859         .driver = {
2860                 .owner  =       THIS_MODULE,
2861                 .name   =       "inv_dev",
2862         },
2863         .address_list = normal_i2c,
2864 };
2865
2866 static int __init inv_mod_init(void)
2867 {
2868         int result = i2c_add_driver(&inv_mod_driver);
2869
2870         if (result) {
2871                 pr_err("%s failed\n", __func__);
2872                 return result;
2873         }
2874
2875         return 0;
2876 }
2877
2878 static void __exit inv_mod_exit(void)
2879 {
2880         i2c_del_driver(&inv_mod_driver);
2881 }
2882
2883 module_init(inv_mod_init);
2884 module_exit(inv_mod_exit);
2885
2886 MODULE_AUTHOR("Invensense Corporation");
2887 MODULE_DESCRIPTION("Invensense device driver");
2888 MODULE_LICENSE("GPL");
2889 MODULE_ALIAS("inv_dev");
2890