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