GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / iio / gyro / mpu3050-core.c
1 /*
2  * MPU3050 gyroscope driver
3  *
4  * Copyright (C) 2016 Linaro Ltd.
5  * Author: Linus Walleij <linus.walleij@linaro.org>
6  *
7  * Based on the input subsystem driver, Copyright (C) 2011 Wistron Co.Ltd
8  * Joseph Lai <joseph_lai@wistron.com> and trimmed down by
9  * Alan Cox <alan@linux.intel.com> in turn based on bma023.c.
10  * Device behaviour based on a misc driver posted by Nathan Royer in 2011.
11  *
12  * TODO: add support for setting up the low pass 3dB frequency.
13  */
14
15 #include <linux/bitops.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/random.h>
28 #include <linux/slab.h>
29
30 #include "mpu3050.h"
31
32 #define MPU3050_CHIP_ID         0x68
33 #define MPU3050_CHIP_ID_MASK    0x7E
34
35 /*
36  * Register map: anything suffixed *_H is a big-endian high byte and always
37  * followed by the corresponding low byte (*_L) even though these are not
38  * explicitly included in the register definitions.
39  */
40 #define MPU3050_CHIP_ID_REG     0x00
41 #define MPU3050_PRODUCT_ID_REG  0x01
42 #define MPU3050_XG_OFFS_TC      0x05
43 #define MPU3050_YG_OFFS_TC      0x08
44 #define MPU3050_ZG_OFFS_TC      0x0B
45 #define MPU3050_X_OFFS_USR_H    0x0C
46 #define MPU3050_Y_OFFS_USR_H    0x0E
47 #define MPU3050_Z_OFFS_USR_H    0x10
48 #define MPU3050_FIFO_EN         0x12
49 #define MPU3050_AUX_VDDIO       0x13
50 #define MPU3050_SLV_ADDR        0x14
51 #define MPU3050_SMPLRT_DIV      0x15
52 #define MPU3050_DLPF_FS_SYNC    0x16
53 #define MPU3050_INT_CFG         0x17
54 #define MPU3050_AUX_ADDR        0x18
55 #define MPU3050_INT_STATUS      0x1A
56 #define MPU3050_TEMP_H          0x1B
57 #define MPU3050_XOUT_H          0x1D
58 #define MPU3050_YOUT_H          0x1F
59 #define MPU3050_ZOUT_H          0x21
60 #define MPU3050_DMP_CFG1        0x35
61 #define MPU3050_DMP_CFG2        0x36
62 #define MPU3050_BANK_SEL        0x37
63 #define MPU3050_MEM_START_ADDR  0x38
64 #define MPU3050_MEM_R_W         0x39
65 #define MPU3050_FIFO_COUNT_H    0x3A
66 #define MPU3050_FIFO_R          0x3C
67 #define MPU3050_USR_CTRL        0x3D
68 #define MPU3050_PWR_MGM         0x3E
69
70 /* MPU memory bank read options */
71 #define MPU3050_MEM_PRFTCH      BIT(5)
72 #define MPU3050_MEM_USER_BANK   BIT(4)
73 /* Bits 8-11 select memory bank */
74 #define MPU3050_MEM_RAM_BANK_0  0
75 #define MPU3050_MEM_RAM_BANK_1  1
76 #define MPU3050_MEM_RAM_BANK_2  2
77 #define MPU3050_MEM_RAM_BANK_3  3
78 #define MPU3050_MEM_OTP_BANK_0  4
79
80 #define MPU3050_AXIS_REGS(axis) (MPU3050_XOUT_H + (axis * 2))
81
82 /* Register bits */
83
84 /* FIFO Enable */
85 #define MPU3050_FIFO_EN_FOOTER          BIT(0)
86 #define MPU3050_FIFO_EN_AUX_ZOUT        BIT(1)
87 #define MPU3050_FIFO_EN_AUX_YOUT        BIT(2)
88 #define MPU3050_FIFO_EN_AUX_XOUT        BIT(3)
89 #define MPU3050_FIFO_EN_GYRO_ZOUT       BIT(4)
90 #define MPU3050_FIFO_EN_GYRO_YOUT       BIT(5)
91 #define MPU3050_FIFO_EN_GYRO_XOUT       BIT(6)
92 #define MPU3050_FIFO_EN_TEMP_OUT        BIT(7)
93
94 /*
95  * Digital Low Pass filter (DLPF)
96  * Full Scale (FS)
97  * and Synchronization
98  */
99 #define MPU3050_EXT_SYNC_NONE           0x00
100 #define MPU3050_EXT_SYNC_TEMP           0x20
101 #define MPU3050_EXT_SYNC_GYROX          0x40
102 #define MPU3050_EXT_SYNC_GYROY          0x60
103 #define MPU3050_EXT_SYNC_GYROZ          0x80
104 #define MPU3050_EXT_SYNC_ACCELX 0xA0
105 #define MPU3050_EXT_SYNC_ACCELY 0xC0
106 #define MPU3050_EXT_SYNC_ACCELZ 0xE0
107 #define MPU3050_EXT_SYNC_MASK           0xE0
108 #define MPU3050_EXT_SYNC_SHIFT          5
109
110 #define MPU3050_FS_250DPS               0x00
111 #define MPU3050_FS_500DPS               0x08
112 #define MPU3050_FS_1000DPS              0x10
113 #define MPU3050_FS_2000DPS              0x18
114 #define MPU3050_FS_MASK                 0x18
115 #define MPU3050_FS_SHIFT                3
116
117 #define MPU3050_DLPF_CFG_256HZ_NOLPF2   0x00
118 #define MPU3050_DLPF_CFG_188HZ          0x01
119 #define MPU3050_DLPF_CFG_98HZ           0x02
120 #define MPU3050_DLPF_CFG_42HZ           0x03
121 #define MPU3050_DLPF_CFG_20HZ           0x04
122 #define MPU3050_DLPF_CFG_10HZ           0x05
123 #define MPU3050_DLPF_CFG_5HZ            0x06
124 #define MPU3050_DLPF_CFG_2100HZ_NOLPF   0x07
125 #define MPU3050_DLPF_CFG_MASK           0x07
126 #define MPU3050_DLPF_CFG_SHIFT          0
127
128 /* Interrupt config */
129 #define MPU3050_INT_RAW_RDY_EN          BIT(0)
130 #define MPU3050_INT_DMP_DONE_EN         BIT(1)
131 #define MPU3050_INT_MPU_RDY_EN          BIT(2)
132 #define MPU3050_INT_ANYRD_2CLEAR        BIT(4)
133 #define MPU3050_INT_LATCH_EN            BIT(5)
134 #define MPU3050_INT_OPEN                BIT(6)
135 #define MPU3050_INT_ACTL                BIT(7)
136 /* Interrupt status */
137 #define MPU3050_INT_STATUS_RAW_RDY      BIT(0)
138 #define MPU3050_INT_STATUS_DMP_DONE     BIT(1)
139 #define MPU3050_INT_STATUS_MPU_RDY      BIT(2)
140 #define MPU3050_INT_STATUS_FIFO_OVFLW   BIT(7)
141 /* USR_CTRL */
142 #define MPU3050_USR_CTRL_FIFO_EN        BIT(6)
143 #define MPU3050_USR_CTRL_AUX_IF_EN      BIT(5)
144 #define MPU3050_USR_CTRL_AUX_IF_RST     BIT(3)
145 #define MPU3050_USR_CTRL_FIFO_RST       BIT(1)
146 #define MPU3050_USR_CTRL_GYRO_RST       BIT(0)
147 /* PWR_MGM */
148 #define MPU3050_PWR_MGM_PLL_X           0x01
149 #define MPU3050_PWR_MGM_PLL_Y           0x02
150 #define MPU3050_PWR_MGM_PLL_Z           0x03
151 #define MPU3050_PWR_MGM_CLKSEL_MASK     0x07
152 #define MPU3050_PWR_MGM_STBY_ZG         BIT(3)
153 #define MPU3050_PWR_MGM_STBY_YG         BIT(4)
154 #define MPU3050_PWR_MGM_STBY_XG         BIT(5)
155 #define MPU3050_PWR_MGM_SLEEP           BIT(6)
156 #define MPU3050_PWR_MGM_RESET           BIT(7)
157 #define MPU3050_PWR_MGM_MASK            0xff
158
159 /*
160  * Fullscale precision is (for finest precision) +/- 250 deg/s, so the full
161  * scale is actually 500 deg/s. All 16 bits are then used to cover this scale,
162  * in two's complement.
163  */
164 static unsigned int mpu3050_fs_precision[] = {
165         IIO_DEGREE_TO_RAD(250),
166         IIO_DEGREE_TO_RAD(500),
167         IIO_DEGREE_TO_RAD(1000),
168         IIO_DEGREE_TO_RAD(2000)
169 };
170
171 /*
172  * Regulator names
173  */
174 static const char mpu3050_reg_vdd[] = "vdd";
175 static const char mpu3050_reg_vlogic[] = "vlogic";
176
177 static unsigned int mpu3050_get_freq(struct mpu3050 *mpu3050)
178 {
179         unsigned int freq;
180
181         if (mpu3050->lpf == MPU3050_DLPF_CFG_256HZ_NOLPF2)
182                 freq = 8000;
183         else
184                 freq = 1000;
185         freq /= (mpu3050->divisor + 1);
186
187         return freq;
188 }
189
190 static int mpu3050_start_sampling(struct mpu3050 *mpu3050)
191 {
192         __be16 raw_val[3];
193         int ret;
194         int i;
195
196         /* Reset */
197         ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
198                                  MPU3050_PWR_MGM_RESET, MPU3050_PWR_MGM_RESET);
199         if (ret)
200                 return ret;
201
202         /* Turn on the Z-axis PLL */
203         ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
204                                  MPU3050_PWR_MGM_CLKSEL_MASK,
205                                  MPU3050_PWR_MGM_PLL_Z);
206         if (ret)
207                 return ret;
208
209         /* Write calibration offset registers */
210         for (i = 0; i < 3; i++)
211                 raw_val[i] = cpu_to_be16(mpu3050->calibration[i]);
212
213         ret = regmap_bulk_write(mpu3050->map, MPU3050_X_OFFS_USR_H, raw_val,
214                                 sizeof(raw_val));
215         if (ret)
216                 return ret;
217
218         /* Set low pass filter (sample rate), sync and full scale */
219         ret = regmap_write(mpu3050->map, MPU3050_DLPF_FS_SYNC,
220                            MPU3050_EXT_SYNC_NONE << MPU3050_EXT_SYNC_SHIFT |
221                            mpu3050->fullscale << MPU3050_FS_SHIFT |
222                            mpu3050->lpf << MPU3050_DLPF_CFG_SHIFT);
223         if (ret)
224                 return ret;
225
226         /* Set up sampling frequency */
227         ret = regmap_write(mpu3050->map, MPU3050_SMPLRT_DIV, mpu3050->divisor);
228         if (ret)
229                 return ret;
230
231         /*
232          * Max 50 ms start-up time after setting DLPF_FS_SYNC
233          * according to the data sheet, then wait for the next sample
234          * at this frequency T = 1000/f ms.
235          */
236         msleep(50 + 1000 / mpu3050_get_freq(mpu3050));
237
238         return 0;
239 }
240
241 static int mpu3050_set_8khz_samplerate(struct mpu3050 *mpu3050)
242 {
243         int ret;
244         u8 divisor;
245         enum mpu3050_lpf lpf;
246
247         lpf = mpu3050->lpf;
248         divisor = mpu3050->divisor;
249
250         mpu3050->lpf = LPF_256_HZ_NOLPF; /* 8 kHz base frequency */
251         mpu3050->divisor = 0; /* Divide by 1 */
252         ret = mpu3050_start_sampling(mpu3050);
253
254         mpu3050->lpf = lpf;
255         mpu3050->divisor = divisor;
256
257         return ret;
258 }
259
260 static int mpu3050_read_raw(struct iio_dev *indio_dev,
261                             struct iio_chan_spec const *chan,
262                             int *val, int *val2,
263                             long mask)
264 {
265         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
266         int ret;
267         __be16 raw_val;
268
269         switch (mask) {
270         case IIO_CHAN_INFO_OFFSET:
271                 switch (chan->type) {
272                 case IIO_TEMP:
273                         /*
274                          * The temperature scaling is (x+23000)/280 Celsius
275                          * for the "best fit straight line" temperature range
276                          * of -30C..85C.  The 23000 includes room temperature
277                          * offset of +35C, 280 is the precision scale and x is
278                          * the 16-bit signed integer reported by hardware.
279                          *
280                          * Temperature value itself represents temperature of
281                          * the sensor die.
282                          */
283                         *val = 23000;
284                         return IIO_VAL_INT;
285                 default:
286                         return -EINVAL;
287                 }
288         case IIO_CHAN_INFO_CALIBBIAS:
289                 switch (chan->type) {
290                 case IIO_ANGL_VEL:
291                         *val = mpu3050->calibration[chan->scan_index-1];
292                         return IIO_VAL_INT;
293                 default:
294                         return -EINVAL;
295                 }
296         case IIO_CHAN_INFO_SAMP_FREQ:
297                 *val = mpu3050_get_freq(mpu3050);
298                 return IIO_VAL_INT;
299         case IIO_CHAN_INFO_SCALE:
300                 switch (chan->type) {
301                 case IIO_TEMP:
302                         /* Millidegrees, see about temperature scaling above */
303                         *val = 1000;
304                         *val2 = 280;
305                         return IIO_VAL_FRACTIONAL;
306                 case IIO_ANGL_VEL:
307                         /*
308                          * Convert to the corresponding full scale in
309                          * radians. All 16 bits are used with sign to
310                          * span the available scale: to account for the one
311                          * missing value if we multiply by 1/S16_MAX, instead
312                          * multiply with 2/U16_MAX.
313                          */
314                         *val = mpu3050_fs_precision[mpu3050->fullscale] * 2;
315                         *val2 = U16_MAX;
316                         return IIO_VAL_FRACTIONAL;
317                 default:
318                         return -EINVAL;
319                 }
320         case IIO_CHAN_INFO_RAW:
321                 /* Resume device */
322                 pm_runtime_get_sync(mpu3050->dev);
323                 mutex_lock(&mpu3050->lock);
324
325                 ret = mpu3050_set_8khz_samplerate(mpu3050);
326                 if (ret)
327                         goto out_read_raw_unlock;
328
329                 switch (chan->type) {
330                 case IIO_TEMP:
331                         ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H,
332                                                &raw_val, sizeof(raw_val));
333                         if (ret) {
334                                 dev_err(mpu3050->dev,
335                                         "error reading temperature\n");
336                                 goto out_read_raw_unlock;
337                         }
338
339                         *val = (s16)be16_to_cpu(raw_val);
340                         ret = IIO_VAL_INT;
341
342                         goto out_read_raw_unlock;
343                 case IIO_ANGL_VEL:
344                         ret = regmap_bulk_read(mpu3050->map,
345                                        MPU3050_AXIS_REGS(chan->scan_index-1),
346                                        &raw_val,
347                                        sizeof(raw_val));
348                         if (ret) {
349                                 dev_err(mpu3050->dev,
350                                         "error reading axis data\n");
351                                 goto out_read_raw_unlock;
352                         }
353
354                         *val = be16_to_cpu(raw_val);
355                         ret = IIO_VAL_INT;
356
357                         goto out_read_raw_unlock;
358                 default:
359                         ret = -EINVAL;
360                         goto out_read_raw_unlock;
361                 }
362         default:
363                 break;
364         }
365
366         return -EINVAL;
367
368 out_read_raw_unlock:
369         mutex_unlock(&mpu3050->lock);
370         pm_runtime_mark_last_busy(mpu3050->dev);
371         pm_runtime_put_autosuspend(mpu3050->dev);
372
373         return ret;
374 }
375
376 static int mpu3050_write_raw(struct iio_dev *indio_dev,
377                              const struct iio_chan_spec *chan,
378                              int val, int val2, long mask)
379 {
380         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
381         /*
382          * Couldn't figure out a way to precalculate these at compile time.
383          */
384         unsigned int fs250 =
385                 DIV_ROUND_CLOSEST(mpu3050_fs_precision[0] * 1000000 * 2,
386                                   U16_MAX);
387         unsigned int fs500 =
388                 DIV_ROUND_CLOSEST(mpu3050_fs_precision[1] * 1000000 * 2,
389                                   U16_MAX);
390         unsigned int fs1000 =
391                 DIV_ROUND_CLOSEST(mpu3050_fs_precision[2] * 1000000 * 2,
392                                   U16_MAX);
393         unsigned int fs2000 =
394                 DIV_ROUND_CLOSEST(mpu3050_fs_precision[3] * 1000000 * 2,
395                                   U16_MAX);
396
397         switch (mask) {
398         case IIO_CHAN_INFO_CALIBBIAS:
399                 if (chan->type != IIO_ANGL_VEL)
400                         return -EINVAL;
401                 mpu3050->calibration[chan->scan_index-1] = val;
402                 return 0;
403         case IIO_CHAN_INFO_SAMP_FREQ:
404                 /*
405                  * The max samplerate is 8000 Hz, the minimum
406                  * 1000 / 256 ~= 4 Hz
407                  */
408                 if (val < 4 || val > 8000)
409                         return -EINVAL;
410
411                 /*
412                  * Above 1000 Hz we must turn off the digital low pass filter
413                  * so we get a base frequency of 8kHz to the divider
414                  */
415                 if (val > 1000) {
416                         mpu3050->lpf = LPF_256_HZ_NOLPF;
417                         mpu3050->divisor = DIV_ROUND_CLOSEST(8000, val) - 1;
418                         return 0;
419                 }
420
421                 mpu3050->lpf = LPF_188_HZ;
422                 mpu3050->divisor = DIV_ROUND_CLOSEST(1000, val) - 1;
423                 return 0;
424         case IIO_CHAN_INFO_SCALE:
425                 if (chan->type != IIO_ANGL_VEL)
426                         return -EINVAL;
427                 /*
428                  * We support +/-250, +/-500, +/-1000 and +/2000 deg/s
429                  * which means we need to round to the closest radians
430                  * which will be roughly +/-4.3, +/-8.7, +/-17.5, +/-35
431                  * rad/s. The scale is then for the 16 bits used to cover
432                  * it 2/(2^16) of that.
433                  */
434
435                 /* Just too large, set the max range */
436                 if (val != 0) {
437                         mpu3050->fullscale = FS_2000_DPS;
438                         return 0;
439                 }
440
441                 /*
442                  * Now we're dealing with fractions below zero in millirad/s
443                  * do some integer interpolation and match with the closest
444                  * fullscale in the table.
445                  */
446                 if (val2 <= fs250 ||
447                     val2 < ((fs500 + fs250) / 2))
448                         mpu3050->fullscale = FS_250_DPS;
449                 else if (val2 <= fs500 ||
450                          val2 < ((fs1000 + fs500) / 2))
451                         mpu3050->fullscale = FS_500_DPS;
452                 else if (val2 <= fs1000 ||
453                          val2 < ((fs2000 + fs1000) / 2))
454                         mpu3050->fullscale = FS_1000_DPS;
455                 else
456                         /* Catch-all */
457                         mpu3050->fullscale = FS_2000_DPS;
458                 return 0;
459         default:
460                 break;
461         }
462
463         return -EINVAL;
464 }
465
466 static irqreturn_t mpu3050_trigger_handler(int irq, void *p)
467 {
468         const struct iio_poll_func *pf = p;
469         struct iio_dev *indio_dev = pf->indio_dev;
470         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
471         int ret;
472         /*
473          * Temperature 1*16 bits
474          * Three axes 3*16 bits
475          * Timestamp 64 bits (4*16 bits)
476          * Sum total 8*16 bits
477          */
478         __be16 hw_values[8];
479         s64 timestamp;
480         unsigned int datums_from_fifo = 0;
481
482         /*
483          * If we're using the hardware trigger, get the precise timestamp from
484          * the top half of the threaded IRQ handler. Otherwise get the
485          * timestamp here so it will be close in time to the actual values
486          * read from the registers.
487          */
488         if (iio_trigger_using_own(indio_dev))
489                 timestamp = mpu3050->hw_timestamp;
490         else
491                 timestamp = iio_get_time_ns(indio_dev);
492
493         mutex_lock(&mpu3050->lock);
494
495         /* Using the hardware IRQ trigger? Check the buffer then. */
496         if (mpu3050->hw_irq_trigger) {
497                 __be16 raw_fifocnt;
498                 u16 fifocnt;
499                 /* X, Y, Z + temperature */
500                 unsigned int bytes_per_datum = 8;
501                 bool fifo_overflow = false;
502
503                 ret = regmap_bulk_read(mpu3050->map,
504                                        MPU3050_FIFO_COUNT_H,
505                                        &raw_fifocnt,
506                                        sizeof(raw_fifocnt));
507                 if (ret)
508                         goto out_trigger_unlock;
509                 fifocnt = be16_to_cpu(raw_fifocnt);
510
511                 if (fifocnt == 512) {
512                         dev_info(mpu3050->dev,
513                                  "FIFO overflow! Emptying and resetting FIFO\n");
514                         fifo_overflow = true;
515                         /* Reset and enable the FIFO */
516                         ret = regmap_update_bits(mpu3050->map,
517                                                  MPU3050_USR_CTRL,
518                                                  MPU3050_USR_CTRL_FIFO_EN |
519                                                  MPU3050_USR_CTRL_FIFO_RST,
520                                                  MPU3050_USR_CTRL_FIFO_EN |
521                                                  MPU3050_USR_CTRL_FIFO_RST);
522                         if (ret) {
523                                 dev_info(mpu3050->dev, "error resetting FIFO\n");
524                                 goto out_trigger_unlock;
525                         }
526                         mpu3050->pending_fifo_footer = false;
527                 }
528
529                 if (fifocnt)
530                         dev_dbg(mpu3050->dev,
531                                 "%d bytes in the FIFO\n",
532                                 fifocnt);
533
534                 while (!fifo_overflow && fifocnt > bytes_per_datum) {
535                         unsigned int toread;
536                         unsigned int offset;
537                         __be16 fifo_values[5];
538
539                         /*
540                          * If there is a FIFO footer in the pipe, first clear
541                          * that out. This follows the complex algorithm in the
542                          * datasheet that states that you may never leave the
543                          * FIFO empty after the first reading: you have to
544                          * always leave two footer bytes in it. The footer is
545                          * in practice just two zero bytes.
546                          */
547                         if (mpu3050->pending_fifo_footer) {
548                                 toread = bytes_per_datum + 2;
549                                 offset = 0;
550                         } else {
551                                 toread = bytes_per_datum;
552                                 offset = 1;
553                                 /* Put in some dummy value */
554                                 fifo_values[0] = 0xAAAA;
555                         }
556
557                         ret = regmap_bulk_read(mpu3050->map,
558                                                MPU3050_FIFO_R,
559                                                &fifo_values[offset],
560                                                toread);
561                         if (ret)
562                                 goto out_trigger_unlock;
563
564                         dev_dbg(mpu3050->dev,
565                                 "%04x %04x %04x %04x %04x\n",
566                                 fifo_values[0],
567                                 fifo_values[1],
568                                 fifo_values[2],
569                                 fifo_values[3],
570                                 fifo_values[4]);
571
572                         /* Index past the footer (fifo_values[0]) and push */
573                         iio_push_to_buffers_with_timestamp(indio_dev,
574                                                            &fifo_values[1],
575                                                            timestamp);
576
577                         fifocnt -= toread;
578                         datums_from_fifo++;
579                         mpu3050->pending_fifo_footer = true;
580
581                         /*
582                          * If we're emptying the FIFO, just make sure to
583                          * check if something new appeared.
584                          */
585                         if (fifocnt < bytes_per_datum) {
586                                 ret = regmap_bulk_read(mpu3050->map,
587                                                        MPU3050_FIFO_COUNT_H,
588                                                        &raw_fifocnt,
589                                                        sizeof(raw_fifocnt));
590                                 if (ret)
591                                         goto out_trigger_unlock;
592                                 fifocnt = be16_to_cpu(raw_fifocnt);
593                         }
594
595                         if (fifocnt < bytes_per_datum)
596                                 dev_dbg(mpu3050->dev,
597                                         "%d bytes left in the FIFO\n",
598                                         fifocnt);
599
600                         /*
601                          * At this point, the timestamp that triggered the
602                          * hardware interrupt is no longer valid for what
603                          * we are reading (the interrupt likely fired for
604                          * the value on the top of the FIFO), so set the
605                          * timestamp to zero and let userspace deal with it.
606                          */
607                         timestamp = 0;
608                 }
609         }
610
611         /*
612          * If we picked some datums from the FIFO that's enough, else
613          * fall through and just read from the current value registers.
614          * This happens in two cases:
615          *
616          * - We are using some other trigger (external, like an HRTimer)
617          *   than the sensor's own sample generator. In this case the
618          *   sensor is just set to the max sampling frequency and we give
619          *   the trigger a copy of the latest value every time we get here.
620          *
621          * - The hardware trigger is active but unused and we actually use
622          *   another trigger which calls here with a frequency higher
623          *   than what the device provides data. We will then just read
624          *   duplicate values directly from the hardware registers.
625          */
626         if (datums_from_fifo) {
627                 dev_dbg(mpu3050->dev,
628                         "read %d datums from the FIFO\n",
629                         datums_from_fifo);
630                 goto out_trigger_unlock;
631         }
632
633         ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values,
634                                sizeof(hw_values));
635         if (ret) {
636                 dev_err(mpu3050->dev,
637                         "error reading axis data\n");
638                 goto out_trigger_unlock;
639         }
640
641         iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp);
642
643 out_trigger_unlock:
644         mutex_unlock(&mpu3050->lock);
645         iio_trigger_notify_done(indio_dev->trig);
646
647         return IRQ_HANDLED;
648 }
649
650 static int mpu3050_buffer_preenable(struct iio_dev *indio_dev)
651 {
652         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
653
654         pm_runtime_get_sync(mpu3050->dev);
655
656         /* Unless we have OUR trigger active, run at full speed */
657         if (!mpu3050->hw_irq_trigger)
658                 return mpu3050_set_8khz_samplerate(mpu3050);
659
660         return 0;
661 }
662
663 static int mpu3050_buffer_postdisable(struct iio_dev *indio_dev)
664 {
665         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
666
667         pm_runtime_mark_last_busy(mpu3050->dev);
668         pm_runtime_put_autosuspend(mpu3050->dev);
669
670         return 0;
671 }
672
673 static const struct iio_buffer_setup_ops mpu3050_buffer_setup_ops = {
674         .preenable = mpu3050_buffer_preenable,
675         .postenable = iio_triggered_buffer_postenable,
676         .predisable = iio_triggered_buffer_predisable,
677         .postdisable = mpu3050_buffer_postdisable,
678 };
679
680 static const struct iio_mount_matrix *
681 mpu3050_get_mount_matrix(const struct iio_dev *indio_dev,
682                          const struct iio_chan_spec *chan)
683 {
684         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
685
686         return &mpu3050->orientation;
687 }
688
689 static const struct iio_chan_spec_ext_info mpu3050_ext_info[] = {
690         IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, mpu3050_get_mount_matrix),
691         { },
692 };
693
694 #define MPU3050_AXIS_CHANNEL(axis, index)                               \
695         {                                                               \
696                 .type = IIO_ANGL_VEL,                                   \
697                 .modified = 1,                                          \
698                 .channel2 = IIO_MOD_##axis,                             \
699                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
700                         BIT(IIO_CHAN_INFO_CALIBBIAS),                   \
701                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
702                 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
703                 .ext_info = mpu3050_ext_info,                           \
704                 .scan_index = index,                                    \
705                 .scan_type = {                                          \
706                         .sign = 's',                                    \
707                         .realbits = 16,                                 \
708                         .storagebits = 16,                              \
709                         .endianness = IIO_BE,                           \
710                 },                                                      \
711         }
712
713 static const struct iio_chan_spec mpu3050_channels[] = {
714         {
715                 .type = IIO_TEMP,
716                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
717                                       BIT(IIO_CHAN_INFO_SCALE) |
718                                       BIT(IIO_CHAN_INFO_OFFSET),
719                 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
720                 .scan_index = 0,
721                 .scan_type = {
722                         .sign = 's',
723                         .realbits = 16,
724                         .storagebits = 16,
725                         .endianness = IIO_BE,
726                 },
727         },
728         MPU3050_AXIS_CHANNEL(X, 1),
729         MPU3050_AXIS_CHANNEL(Y, 2),
730         MPU3050_AXIS_CHANNEL(Z, 3),
731         IIO_CHAN_SOFT_TIMESTAMP(4),
732 };
733
734 /* Four channels apart from timestamp, scan mask = 0x0f */
735 static const unsigned long mpu3050_scan_masks[] = { 0xf, 0 };
736
737 /*
738  * These are just the hardcoded factors resulting from the more elaborate
739  * calculations done with fractions in the scale raw get/set functions.
740  */
741 static IIO_CONST_ATTR(anglevel_scale_available,
742                       "0.000122070 "
743                       "0.000274658 "
744                       "0.000518798 "
745                       "0.001068115");
746
747 static struct attribute *mpu3050_attributes[] = {
748         &iio_const_attr_anglevel_scale_available.dev_attr.attr,
749         NULL,
750 };
751
752 static const struct attribute_group mpu3050_attribute_group = {
753         .attrs = mpu3050_attributes,
754 };
755
756 static const struct iio_info mpu3050_info = {
757         .driver_module = THIS_MODULE,
758         .read_raw = mpu3050_read_raw,
759         .write_raw = mpu3050_write_raw,
760         .attrs = &mpu3050_attribute_group,
761 };
762
763 /**
764  * mpu3050_read_mem() - read MPU-3050 internal memory
765  * @mpu3050: device to read from
766  * @bank: target bank
767  * @addr: target address
768  * @len: number of bytes
769  * @buf: the buffer to store the read bytes in
770  */
771 static int mpu3050_read_mem(struct mpu3050 *mpu3050,
772                             u8 bank,
773                             u8 addr,
774                             u8 len,
775                             u8 *buf)
776 {
777         int ret;
778
779         ret = regmap_write(mpu3050->map,
780                            MPU3050_BANK_SEL,
781                            bank);
782         if (ret)
783                 return ret;
784
785         ret = regmap_write(mpu3050->map,
786                            MPU3050_MEM_START_ADDR,
787                            addr);
788         if (ret)
789                 return ret;
790
791         return regmap_bulk_read(mpu3050->map,
792                                 MPU3050_MEM_R_W,
793                                 buf,
794                                 len);
795 }
796
797 static int mpu3050_hw_init(struct mpu3050 *mpu3050)
798 {
799         int ret;
800         u8 otp[8];
801
802         /* Reset */
803         ret = regmap_update_bits(mpu3050->map,
804                                  MPU3050_PWR_MGM,
805                                  MPU3050_PWR_MGM_RESET,
806                                  MPU3050_PWR_MGM_RESET);
807         if (ret)
808                 return ret;
809
810         /* Turn on the PLL */
811         ret = regmap_update_bits(mpu3050->map,
812                                  MPU3050_PWR_MGM,
813                                  MPU3050_PWR_MGM_CLKSEL_MASK,
814                                  MPU3050_PWR_MGM_PLL_Z);
815         if (ret)
816                 return ret;
817
818         /* Disable IRQs */
819         ret = regmap_write(mpu3050->map,
820                            MPU3050_INT_CFG,
821                            0);
822         if (ret)
823                 return ret;
824
825         /* Read out the 8 bytes of OTP (one-time-programmable) memory */
826         ret = mpu3050_read_mem(mpu3050,
827                                (MPU3050_MEM_PRFTCH |
828                                 MPU3050_MEM_USER_BANK |
829                                 MPU3050_MEM_OTP_BANK_0),
830                                0,
831                                sizeof(otp),
832                                otp);
833         if (ret)
834                 return ret;
835
836         /* This is device-unique data so it goes into the entropy pool */
837         add_device_randomness(otp, sizeof(otp));
838
839         dev_info(mpu3050->dev,
840                  "die ID: %04X, wafer ID: %02X, A lot ID: %04X, "
841                  "W lot ID: %03X, WP ID: %01X, rev ID: %02X\n",
842                  /* Die ID, bits 0-12 */
843                  (otp[1] << 8 | otp[0]) & 0x1fff,
844                  /* Wafer ID, bits 13-17 */
845                  ((otp[2] << 8 | otp[1]) & 0x03e0) >> 5,
846                  /* A lot ID, bits 18-33 */
847                  ((otp[4] << 16 | otp[3] << 8 | otp[2]) & 0x3fffc) >> 2,
848                  /* W lot ID, bits 34-45 */
849                  ((otp[5] << 8 | otp[4]) & 0x3ffc) >> 2,
850                  /* WP ID, bits 47-49 */
851                  ((otp[6] << 8 | otp[5]) & 0x0380) >> 7,
852                  /* rev ID, bits 50-55 */
853                  otp[6] >> 2);
854
855         return 0;
856 }
857
858 static int mpu3050_power_up(struct mpu3050 *mpu3050)
859 {
860         int ret;
861
862         ret = regulator_bulk_enable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
863         if (ret) {
864                 dev_err(mpu3050->dev, "cannot enable regulators\n");
865                 return ret;
866         }
867         /*
868          * 20-100 ms start-up time for register read/write according to
869          * the datasheet, be on the safe side and wait 200 ms.
870          */
871         msleep(200);
872
873         /* Take device out of sleep mode */
874         ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
875                                  MPU3050_PWR_MGM_SLEEP, 0);
876         if (ret) {
877                 dev_err(mpu3050->dev, "error setting power mode\n");
878                 return ret;
879         }
880         msleep(10);
881
882         return 0;
883 }
884
885 static int mpu3050_power_down(struct mpu3050 *mpu3050)
886 {
887         int ret;
888
889         /*
890          * Put MPU-3050 into sleep mode before cutting regulators.
891          * This is important, because we may not be the sole user
892          * of the regulator so the power may stay on after this, and
893          * then we would be wasting power unless we go to sleep mode
894          * first.
895          */
896         ret = regmap_update_bits(mpu3050->map, MPU3050_PWR_MGM,
897                                  MPU3050_PWR_MGM_SLEEP, MPU3050_PWR_MGM_SLEEP);
898         if (ret)
899                 dev_err(mpu3050->dev, "error putting to sleep\n");
900
901         ret = regulator_bulk_disable(ARRAY_SIZE(mpu3050->regs), mpu3050->regs);
902         if (ret)
903                 dev_err(mpu3050->dev, "error disabling regulators\n");
904
905         return 0;
906 }
907
908 static irqreturn_t mpu3050_irq_handler(int irq, void *p)
909 {
910         struct iio_trigger *trig = p;
911         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
912         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
913
914         if (!mpu3050->hw_irq_trigger)
915                 return IRQ_NONE;
916
917         /* Get the time stamp as close in time as possible */
918         mpu3050->hw_timestamp = iio_get_time_ns(indio_dev);
919
920         return IRQ_WAKE_THREAD;
921 }
922
923 static irqreturn_t mpu3050_irq_thread(int irq, void *p)
924 {
925         struct iio_trigger *trig = p;
926         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
927         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
928         unsigned int val;
929         int ret;
930
931         /* ACK IRQ and check if it was from us */
932         ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
933         if (ret) {
934                 dev_err(mpu3050->dev, "error reading IRQ status\n");
935                 return IRQ_HANDLED;
936         }
937         if (!(val & MPU3050_INT_STATUS_RAW_RDY))
938                 return IRQ_NONE;
939
940         iio_trigger_poll_chained(p);
941
942         return IRQ_HANDLED;
943 }
944
945 /**
946  * mpu3050_drdy_trigger_set_state() - set data ready interrupt state
947  * @trig: trigger instance
948  * @enable: true if trigger should be enabled, false to disable
949  */
950 static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
951                                           bool enable)
952 {
953         struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
954         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
955         unsigned int val;
956         int ret;
957
958         /* Disabling trigger: disable interrupt and return */
959         if (!enable) {
960                 /* Disable all interrupts */
961                 ret = regmap_write(mpu3050->map,
962                                    MPU3050_INT_CFG,
963                                    0);
964                 if (ret)
965                         dev_err(mpu3050->dev, "error disabling IRQ\n");
966
967                 /* Clear IRQ flag */
968                 ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
969                 if (ret)
970                         dev_err(mpu3050->dev, "error clearing IRQ status\n");
971
972                 /* Disable all things in the FIFO and reset it */
973                 ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
974                 if (ret)
975                         dev_err(mpu3050->dev, "error disabling FIFO\n");
976
977                 ret = regmap_write(mpu3050->map, MPU3050_USR_CTRL,
978                                    MPU3050_USR_CTRL_FIFO_RST);
979                 if (ret)
980                         dev_err(mpu3050->dev, "error resetting FIFO\n");
981
982                 pm_runtime_mark_last_busy(mpu3050->dev);
983                 pm_runtime_put_autosuspend(mpu3050->dev);
984                 mpu3050->hw_irq_trigger = false;
985
986                 return 0;
987         } else {
988                 /* Else we're enabling the trigger from this point */
989                 pm_runtime_get_sync(mpu3050->dev);
990                 mpu3050->hw_irq_trigger = true;
991
992                 /* Disable all things in the FIFO */
993                 ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
994                 if (ret)
995                         return ret;
996
997                 /* Reset and enable the FIFO */
998                 ret = regmap_update_bits(mpu3050->map, MPU3050_USR_CTRL,
999                                          MPU3050_USR_CTRL_FIFO_EN |
1000                                          MPU3050_USR_CTRL_FIFO_RST,
1001                                          MPU3050_USR_CTRL_FIFO_EN |
1002                                          MPU3050_USR_CTRL_FIFO_RST);
1003                 if (ret)
1004                         return ret;
1005
1006                 mpu3050->pending_fifo_footer = false;
1007
1008                 /* Turn on the FIFO for temp+X+Y+Z */
1009                 ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN,
1010                                    MPU3050_FIFO_EN_TEMP_OUT |
1011                                    MPU3050_FIFO_EN_GYRO_XOUT |
1012                                    MPU3050_FIFO_EN_GYRO_YOUT |
1013                                    MPU3050_FIFO_EN_GYRO_ZOUT |
1014                                    MPU3050_FIFO_EN_FOOTER);
1015                 if (ret)
1016                         return ret;
1017
1018                 /* Configure the sample engine */
1019                 ret = mpu3050_start_sampling(mpu3050);
1020                 if (ret)
1021                         return ret;
1022
1023                 /* Clear IRQ flag */
1024                 ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
1025                 if (ret)
1026                         dev_err(mpu3050->dev, "error clearing IRQ status\n");
1027
1028                 /* Give us interrupts whenever there is new data ready */
1029                 val = MPU3050_INT_RAW_RDY_EN;
1030
1031                 if (mpu3050->irq_actl)
1032                         val |= MPU3050_INT_ACTL;
1033                 if (mpu3050->irq_latch)
1034                         val |= MPU3050_INT_LATCH_EN;
1035                 if (mpu3050->irq_opendrain)
1036                         val |= MPU3050_INT_OPEN;
1037
1038                 ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
1039                 if (ret)
1040                         return ret;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static const struct iio_trigger_ops mpu3050_trigger_ops = {
1047         .owner = THIS_MODULE,
1048         .set_trigger_state = mpu3050_drdy_trigger_set_state,
1049 };
1050
1051 static int mpu3050_trigger_probe(struct iio_dev *indio_dev, int irq)
1052 {
1053         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
1054         unsigned long irq_trig;
1055         int ret;
1056
1057         mpu3050->trig = devm_iio_trigger_alloc(&indio_dev->dev,
1058                                                "%s-dev%d",
1059                                                indio_dev->name,
1060                                                indio_dev->id);
1061         if (!mpu3050->trig)
1062                 return -ENOMEM;
1063
1064         /* Check if IRQ is open drain */
1065         if (of_property_read_bool(mpu3050->dev->of_node, "drive-open-drain"))
1066                 mpu3050->irq_opendrain = true;
1067
1068         irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
1069         /*
1070          * Configure the interrupt generator hardware to supply whatever
1071          * the interrupt is configured for, edges low/high level low/high,
1072          * we can provide it all.
1073          */
1074         switch (irq_trig) {
1075         case IRQF_TRIGGER_RISING:
1076                 dev_info(&indio_dev->dev,
1077                          "pulse interrupts on the rising edge\n");
1078                 break;
1079         case IRQF_TRIGGER_FALLING:
1080                 mpu3050->irq_actl = true;
1081                 dev_info(&indio_dev->dev,
1082                          "pulse interrupts on the falling edge\n");
1083                 break;
1084         case IRQF_TRIGGER_HIGH:
1085                 mpu3050->irq_latch = true;
1086                 dev_info(&indio_dev->dev,
1087                          "interrupts active high level\n");
1088                 /*
1089                  * With level IRQs, we mask the IRQ until it is processed,
1090                  * but with edge IRQs (pulses) we can queue several interrupts
1091                  * in the top half.
1092                  */
1093                 irq_trig |= IRQF_ONESHOT;
1094                 break;
1095         case IRQF_TRIGGER_LOW:
1096                 mpu3050->irq_latch = true;
1097                 mpu3050->irq_actl = true;
1098                 irq_trig |= IRQF_ONESHOT;
1099                 dev_info(&indio_dev->dev,
1100                          "interrupts active low level\n");
1101                 break;
1102         default:
1103                 /* This is the most preferred mode, if possible */
1104                 dev_err(&indio_dev->dev,
1105                         "unsupported IRQ trigger specified (%lx), enforce "
1106                         "rising edge\n", irq_trig);
1107                 irq_trig = IRQF_TRIGGER_RISING;
1108                 break;
1109         }
1110
1111         /* An open drain line can be shared with several devices */
1112         if (mpu3050->irq_opendrain)
1113                 irq_trig |= IRQF_SHARED;
1114
1115         ret = request_threaded_irq(irq,
1116                                    mpu3050_irq_handler,
1117                                    mpu3050_irq_thread,
1118                                    irq_trig,
1119                                    mpu3050->trig->name,
1120                                    mpu3050->trig);
1121         if (ret) {
1122                 dev_err(mpu3050->dev,
1123                         "can't get IRQ %d, error %d\n", irq, ret);
1124                 return ret;
1125         }
1126
1127         mpu3050->irq = irq;
1128         mpu3050->trig->dev.parent = mpu3050->dev;
1129         mpu3050->trig->ops = &mpu3050_trigger_ops;
1130         iio_trigger_set_drvdata(mpu3050->trig, indio_dev);
1131
1132         ret = iio_trigger_register(mpu3050->trig);
1133         if (ret)
1134                 return ret;
1135
1136         indio_dev->trig = iio_trigger_get(mpu3050->trig);
1137
1138         return 0;
1139 }
1140
1141 int mpu3050_common_probe(struct device *dev,
1142                          struct regmap *map,
1143                          int irq,
1144                          const char *name)
1145 {
1146         struct iio_dev *indio_dev;
1147         struct mpu3050 *mpu3050;
1148         unsigned int val;
1149         int ret;
1150
1151         indio_dev = devm_iio_device_alloc(dev, sizeof(*mpu3050));
1152         if (!indio_dev)
1153                 return -ENOMEM;
1154         mpu3050 = iio_priv(indio_dev);
1155
1156         mpu3050->dev = dev;
1157         mpu3050->map = map;
1158         mutex_init(&mpu3050->lock);
1159         /* Default fullscale: 2000 degrees per second */
1160         mpu3050->fullscale = FS_2000_DPS;
1161         /* 1 kHz, divide by 100, default frequency = 10 Hz */
1162         mpu3050->lpf = MPU3050_DLPF_CFG_188HZ;
1163         mpu3050->divisor = 99;
1164
1165         /* Read the mounting matrix, if present */
1166         ret = of_iio_read_mount_matrix(dev, "mount-matrix",
1167                                        &mpu3050->orientation);
1168         if (ret)
1169                 return ret;
1170
1171         /* Fetch and turn on regulators */
1172         mpu3050->regs[0].supply = mpu3050_reg_vdd;
1173         mpu3050->regs[1].supply = mpu3050_reg_vlogic;
1174         ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(mpu3050->regs),
1175                                       mpu3050->regs);
1176         if (ret) {
1177                 dev_err(dev, "Cannot get regulators\n");
1178                 return ret;
1179         }
1180
1181         ret = mpu3050_power_up(mpu3050);
1182         if (ret)
1183                 return ret;
1184
1185         ret = regmap_read(map, MPU3050_CHIP_ID_REG, &val);
1186         if (ret) {
1187                 dev_err(dev, "could not read device ID\n");
1188                 ret = -ENODEV;
1189
1190                 goto err_power_down;
1191         }
1192
1193         if ((val & MPU3050_CHIP_ID_MASK) != MPU3050_CHIP_ID) {
1194                 dev_err(dev, "unsupported chip id %02x\n",
1195                                 (u8)(val & MPU3050_CHIP_ID_MASK));
1196                 ret = -ENODEV;
1197                 goto err_power_down;
1198         }
1199
1200         ret = regmap_read(map, MPU3050_PRODUCT_ID_REG, &val);
1201         if (ret) {
1202                 dev_err(dev, "could not read device ID\n");
1203                 ret = -ENODEV;
1204
1205                 goto err_power_down;
1206         }
1207         dev_info(dev, "found MPU-3050 part no: %d, version: %d\n",
1208                  ((val >> 4) & 0xf), (val & 0xf));
1209
1210         ret = mpu3050_hw_init(mpu3050);
1211         if (ret)
1212                 goto err_power_down;
1213
1214         indio_dev->dev.parent = dev;
1215         indio_dev->channels = mpu3050_channels;
1216         indio_dev->num_channels = ARRAY_SIZE(mpu3050_channels);
1217         indio_dev->info = &mpu3050_info;
1218         indio_dev->available_scan_masks = mpu3050_scan_masks;
1219         indio_dev->modes = INDIO_DIRECT_MODE;
1220         indio_dev->name = name;
1221
1222         ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
1223                                          mpu3050_trigger_handler,
1224                                          &mpu3050_buffer_setup_ops);
1225         if (ret) {
1226                 dev_err(dev, "triggered buffer setup failed\n");
1227                 goto err_power_down;
1228         }
1229
1230         ret = iio_device_register(indio_dev);
1231         if (ret) {
1232                 dev_err(dev, "device register failed\n");
1233                 goto err_cleanup_buffer;
1234         }
1235
1236         dev_set_drvdata(dev, indio_dev);
1237
1238         /* Check if we have an assigned IRQ to use as trigger */
1239         if (irq) {
1240                 ret = mpu3050_trigger_probe(indio_dev, irq);
1241                 if (ret)
1242                         dev_err(dev, "failed to register trigger\n");
1243         }
1244
1245         /* Enable runtime PM */
1246         pm_runtime_get_noresume(dev);
1247         pm_runtime_set_active(dev);
1248         pm_runtime_enable(dev);
1249         /*
1250          * Set autosuspend to two orders of magnitude larger than the
1251          * start-up time. 100ms start-up time means 10000ms autosuspend,
1252          * i.e. 10 seconds.
1253          */
1254         pm_runtime_set_autosuspend_delay(dev, 10000);
1255         pm_runtime_use_autosuspend(dev);
1256         pm_runtime_put(dev);
1257
1258         return 0;
1259
1260 err_cleanup_buffer:
1261         iio_triggered_buffer_cleanup(indio_dev);
1262 err_power_down:
1263         mpu3050_power_down(mpu3050);
1264
1265         return ret;
1266 }
1267 EXPORT_SYMBOL(mpu3050_common_probe);
1268
1269 int mpu3050_common_remove(struct device *dev)
1270 {
1271         struct iio_dev *indio_dev = dev_get_drvdata(dev);
1272         struct mpu3050 *mpu3050 = iio_priv(indio_dev);
1273
1274         pm_runtime_get_sync(dev);
1275         pm_runtime_put_noidle(dev);
1276         pm_runtime_disable(dev);
1277         iio_triggered_buffer_cleanup(indio_dev);
1278         if (mpu3050->irq)
1279                 free_irq(mpu3050->irq, mpu3050);
1280         iio_device_unregister(indio_dev);
1281         mpu3050_power_down(mpu3050);
1282
1283         return 0;
1284 }
1285 EXPORT_SYMBOL(mpu3050_common_remove);
1286
1287 #ifdef CONFIG_PM
1288 static int mpu3050_runtime_suspend(struct device *dev)
1289 {
1290         return mpu3050_power_down(iio_priv(dev_get_drvdata(dev)));
1291 }
1292
1293 static int mpu3050_runtime_resume(struct device *dev)
1294 {
1295         return mpu3050_power_up(iio_priv(dev_get_drvdata(dev)));
1296 }
1297 #endif /* CONFIG_PM */
1298
1299 const struct dev_pm_ops mpu3050_dev_pm_ops = {
1300         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1301                                 pm_runtime_force_resume)
1302         SET_RUNTIME_PM_OPS(mpu3050_runtime_suspend,
1303                            mpu3050_runtime_resume, NULL)
1304 };
1305 EXPORT_SYMBOL(mpu3050_dev_pm_ops);
1306
1307 MODULE_AUTHOR("Linus Walleij");
1308 MODULE_DESCRIPTION("MPU3050 gyroscope driver");
1309 MODULE_LICENSE("GPL");