Merge tag 'xfs-5.5-fixes-2' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[platform/kernel/linux-starfive.git] / drivers / iio / imu / inv_mpu6050 / inv_mpu_core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
5
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/i2c.h>
9 #include <linux/err.h>
10 #include <linux/delay.h>
11 #include <linux/sysfs.h>
12 #include <linux/jiffies.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/iio/iio.h>
16 #include <linux/acpi.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include "inv_mpu_iio.h"
20 #include "inv_mpu_magn.h"
21
22 /*
23  * this is the gyro scale translated from dynamic range plus/minus
24  * {250, 500, 1000, 2000} to rad/s
25  */
26 static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
27
28 /*
29  * this is the accel scale translated from dynamic range plus/minus
30  * {2, 4, 8, 16} to m/s^2
31  */
32 static const int accel_scale[] = {598, 1196, 2392, 4785};
33
34 static const struct inv_mpu6050_reg_map reg_set_icm20602 = {
35         .sample_rate_div        = INV_MPU6050_REG_SAMPLE_RATE_DIV,
36         .lpf                    = INV_MPU6050_REG_CONFIG,
37         .accel_lpf              = INV_MPU6500_REG_ACCEL_CONFIG_2,
38         .user_ctrl              = INV_MPU6050_REG_USER_CTRL,
39         .fifo_en                = INV_MPU6050_REG_FIFO_EN,
40         .gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
41         .accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
42         .fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
43         .fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
44         .raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
45         .raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
46         .temperature            = INV_MPU6050_REG_TEMPERATURE,
47         .int_enable             = INV_MPU6050_REG_INT_ENABLE,
48         .int_status             = INV_MPU6050_REG_INT_STATUS,
49         .pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
50         .pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
51         .int_pin_cfg            = INV_MPU6050_REG_INT_PIN_CFG,
52         .accl_offset            = INV_MPU6500_REG_ACCEL_OFFSET,
53         .gyro_offset            = INV_MPU6050_REG_GYRO_OFFSET,
54         .i2c_if                 = INV_ICM20602_REG_I2C_IF,
55 };
56
57 static const struct inv_mpu6050_reg_map reg_set_6500 = {
58         .sample_rate_div        = INV_MPU6050_REG_SAMPLE_RATE_DIV,
59         .lpf                    = INV_MPU6050_REG_CONFIG,
60         .accel_lpf              = INV_MPU6500_REG_ACCEL_CONFIG_2,
61         .user_ctrl              = INV_MPU6050_REG_USER_CTRL,
62         .fifo_en                = INV_MPU6050_REG_FIFO_EN,
63         .gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
64         .accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
65         .fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
66         .fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
67         .raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
68         .raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
69         .temperature            = INV_MPU6050_REG_TEMPERATURE,
70         .int_enable             = INV_MPU6050_REG_INT_ENABLE,
71         .int_status             = INV_MPU6050_REG_INT_STATUS,
72         .pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
73         .pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
74         .int_pin_cfg            = INV_MPU6050_REG_INT_PIN_CFG,
75         .accl_offset            = INV_MPU6500_REG_ACCEL_OFFSET,
76         .gyro_offset            = INV_MPU6050_REG_GYRO_OFFSET,
77         .i2c_if                 = 0,
78 };
79
80 static const struct inv_mpu6050_reg_map reg_set_6050 = {
81         .sample_rate_div        = INV_MPU6050_REG_SAMPLE_RATE_DIV,
82         .lpf                    = INV_MPU6050_REG_CONFIG,
83         .user_ctrl              = INV_MPU6050_REG_USER_CTRL,
84         .fifo_en                = INV_MPU6050_REG_FIFO_EN,
85         .gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
86         .accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
87         .fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
88         .fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
89         .raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
90         .raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
91         .temperature            = INV_MPU6050_REG_TEMPERATURE,
92         .int_enable             = INV_MPU6050_REG_INT_ENABLE,
93         .pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
94         .pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
95         .int_pin_cfg            = INV_MPU6050_REG_INT_PIN_CFG,
96         .accl_offset            = INV_MPU6050_REG_ACCEL_OFFSET,
97         .gyro_offset            = INV_MPU6050_REG_GYRO_OFFSET,
98         .i2c_if                 = 0,
99 };
100
101 static const struct inv_mpu6050_chip_config chip_config_6050 = {
102         .fsr = INV_MPU6050_FSR_2000DPS,
103         .lpf = INV_MPU6050_FILTER_20HZ,
104         .divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(INV_MPU6050_INIT_FIFO_RATE),
105         .gyro_fifo_enable = false,
106         .accl_fifo_enable = false,
107         .magn_fifo_enable = false,
108         .accl_fs = INV_MPU6050_FS_02G,
109         .user_ctrl = 0,
110 };
111
112 /* Indexed by enum inv_devices */
113 static const struct inv_mpu6050_hw hw_info[] = {
114         {
115                 .whoami = INV_MPU6050_WHOAMI_VALUE,
116                 .name = "MPU6050",
117                 .reg = &reg_set_6050,
118                 .config = &chip_config_6050,
119                 .fifo_size = 1024,
120                 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
121         },
122         {
123                 .whoami = INV_MPU6500_WHOAMI_VALUE,
124                 .name = "MPU6500",
125                 .reg = &reg_set_6500,
126                 .config = &chip_config_6050,
127                 .fifo_size = 512,
128                 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
129         },
130         {
131                 .whoami = INV_MPU6515_WHOAMI_VALUE,
132                 .name = "MPU6515",
133                 .reg = &reg_set_6500,
134                 .config = &chip_config_6050,
135                 .fifo_size = 512,
136                 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
137         },
138         {
139                 .whoami = INV_MPU6000_WHOAMI_VALUE,
140                 .name = "MPU6000",
141                 .reg = &reg_set_6050,
142                 .config = &chip_config_6050,
143                 .fifo_size = 1024,
144                 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
145         },
146         {
147                 .whoami = INV_MPU9150_WHOAMI_VALUE,
148                 .name = "MPU9150",
149                 .reg = &reg_set_6050,
150                 .config = &chip_config_6050,
151                 .fifo_size = 1024,
152                 .temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
153         },
154         {
155                 .whoami = INV_MPU9250_WHOAMI_VALUE,
156                 .name = "MPU9250",
157                 .reg = &reg_set_6500,
158                 .config = &chip_config_6050,
159                 .fifo_size = 512,
160                 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
161         },
162         {
163                 .whoami = INV_MPU9255_WHOAMI_VALUE,
164                 .name = "MPU9255",
165                 .reg = &reg_set_6500,
166                 .config = &chip_config_6050,
167                 .fifo_size = 512,
168                 .temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
169         },
170         {
171                 .whoami = INV_ICM20608_WHOAMI_VALUE,
172                 .name = "ICM20608",
173                 .reg = &reg_set_6500,
174                 .config = &chip_config_6050,
175                 .fifo_size = 512,
176                 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
177         },
178         {
179                 .whoami = INV_ICM20602_WHOAMI_VALUE,
180                 .name = "ICM20602",
181                 .reg = &reg_set_icm20602,
182                 .config = &chip_config_6050,
183                 .fifo_size = 1008,
184                 .temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
185         },
186 };
187
188 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
189 {
190         unsigned int d, mgmt_1;
191         int result;
192         /*
193          * switch clock needs to be careful. Only when gyro is on, can
194          * clock source be switched to gyro. Otherwise, it must be set to
195          * internal clock
196          */
197         if (mask == INV_MPU6050_BIT_PWR_GYRO_STBY) {
198                 result = regmap_read(st->map, st->reg->pwr_mgmt_1, &mgmt_1);
199                 if (result)
200                         return result;
201
202                 mgmt_1 &= ~INV_MPU6050_BIT_CLK_MASK;
203         }
204
205         if ((mask == INV_MPU6050_BIT_PWR_GYRO_STBY) && (!en)) {
206                 /*
207                  * turning off gyro requires switch to internal clock first.
208                  * Then turn off gyro engine
209                  */
210                 mgmt_1 |= INV_CLK_INTERNAL;
211                 result = regmap_write(st->map, st->reg->pwr_mgmt_1, mgmt_1);
212                 if (result)
213                         return result;
214         }
215
216         result = regmap_read(st->map, st->reg->pwr_mgmt_2, &d);
217         if (result)
218                 return result;
219         if (en)
220                 d &= ~mask;
221         else
222                 d |= mask;
223         result = regmap_write(st->map, st->reg->pwr_mgmt_2, d);
224         if (result)
225                 return result;
226
227         if (en) {
228                 /* Wait for output to stabilize */
229                 msleep(INV_MPU6050_TEMP_UP_TIME);
230                 if (mask == INV_MPU6050_BIT_PWR_GYRO_STBY) {
231                         /* switch internal clock to PLL */
232                         mgmt_1 |= INV_CLK_PLL;
233                         result = regmap_write(st->map,
234                                               st->reg->pwr_mgmt_1, mgmt_1);
235                         if (result)
236                                 return result;
237                 }
238         }
239
240         return 0;
241 }
242
243 int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
244 {
245         int result;
246
247         if (power_on) {
248                 if (!st->powerup_count) {
249                         result = regmap_write(st->map, st->reg->pwr_mgmt_1, 0);
250                         if (result)
251                                 return result;
252                         usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
253                                      INV_MPU6050_REG_UP_TIME_MAX);
254                 }
255                 st->powerup_count++;
256         } else {
257                 if (st->powerup_count == 1) {
258                         result = regmap_write(st->map, st->reg->pwr_mgmt_1,
259                                               INV_MPU6050_BIT_SLEEP);
260                         if (result)
261                                 return result;
262                 }
263                 st->powerup_count--;
264         }
265
266         dev_dbg(regmap_get_device(st->map), "set power %d, count=%u\n",
267                 power_on, st->powerup_count);
268
269         return 0;
270 }
271 EXPORT_SYMBOL_GPL(inv_mpu6050_set_power_itg);
272
273 /**
274  *  inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
275  *
276  *  MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
277  *  MPU6500 and above have a dedicated register for accelerometer
278  */
279 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st,
280                                     enum inv_mpu6050_filter_e val)
281 {
282         int result;
283
284         result = regmap_write(st->map, st->reg->lpf, val);
285         if (result)
286                 return result;
287
288         switch (st->chip_type) {
289         case INV_MPU6050:
290         case INV_MPU6000:
291         case INV_MPU9150:
292                 /* old chips, nothing to do */
293                 result = 0;
294                 break;
295         default:
296                 /* set accel lpf */
297                 result = regmap_write(st->map, st->reg->accel_lpf, val);
298                 break;
299         }
300
301         return result;
302 }
303
304 /**
305  *  inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
306  *
307  *  Initial configuration:
308  *  FSR: Â± 2000DPS
309  *  DLPF: 20Hz
310  *  FIFO rate: 50Hz
311  *  Clock source: Gyro PLL
312  */
313 static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
314 {
315         int result;
316         u8 d;
317         struct inv_mpu6050_state *st = iio_priv(indio_dev);
318
319         result = inv_mpu6050_set_power_itg(st, true);
320         if (result)
321                 return result;
322         d = (INV_MPU6050_FSR_2000DPS << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
323         result = regmap_write(st->map, st->reg->gyro_config, d);
324         if (result)
325                 goto error_power_off;
326
327         result = inv_mpu6050_set_lpf_regs(st, INV_MPU6050_FILTER_20HZ);
328         if (result)
329                 goto error_power_off;
330
331         d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(INV_MPU6050_INIT_FIFO_RATE);
332         result = regmap_write(st->map, st->reg->sample_rate_div, d);
333         if (result)
334                 goto error_power_off;
335
336         d = (INV_MPU6050_FS_02G << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
337         result = regmap_write(st->map, st->reg->accl_config, d);
338         if (result)
339                 goto error_power_off;
340
341         result = regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
342         if (result)
343                 return result;
344
345         memcpy(&st->chip_config, hw_info[st->chip_type].config,
346                sizeof(struct inv_mpu6050_chip_config));
347
348         /*
349          * Internal chip period is 1ms (1kHz).
350          * Let's use at the beginning the theorical value before measuring
351          * with interrupt timestamps.
352          */
353         st->chip_period = NSEC_PER_MSEC;
354
355         /* magn chip init, noop if not present in the chip */
356         result = inv_mpu_magn_probe(st);
357         if (result)
358                 goto error_power_off;
359
360         return inv_mpu6050_set_power_itg(st, false);
361
362 error_power_off:
363         inv_mpu6050_set_power_itg(st, false);
364         return result;
365 }
366
367 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state  *st, int reg,
368                                 int axis, int val)
369 {
370         int ind, result;
371         __be16 d = cpu_to_be16(val);
372
373         ind = (axis - IIO_MOD_X) * 2;
374         result = regmap_bulk_write(st->map, reg + ind, (u8 *)&d, 2);
375         if (result)
376                 return -EINVAL;
377
378         return 0;
379 }
380
381 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state  *st, int reg,
382                                    int axis, int *val)
383 {
384         int ind, result;
385         __be16 d;
386
387         ind = (axis - IIO_MOD_X) * 2;
388         result = regmap_bulk_read(st->map, reg + ind, (u8 *)&d, 2);
389         if (result)
390                 return -EINVAL;
391         *val = (short)be16_to_cpup(&d);
392
393         return IIO_VAL_INT;
394 }
395
396 static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
397                                          struct iio_chan_spec const *chan,
398                                          int *val)
399 {
400         struct inv_mpu6050_state *st = iio_priv(indio_dev);
401         int result;
402         int ret;
403
404         result = inv_mpu6050_set_power_itg(st, true);
405         if (result)
406                 return result;
407
408         switch (chan->type) {
409         case IIO_ANGL_VEL:
410                 result = inv_mpu6050_switch_engine(st, true,
411                                 INV_MPU6050_BIT_PWR_GYRO_STBY);
412                 if (result)
413                         goto error_power_off;
414                 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
415                                               chan->channel2, val);
416                 result = inv_mpu6050_switch_engine(st, false,
417                                 INV_MPU6050_BIT_PWR_GYRO_STBY);
418                 if (result)
419                         goto error_power_off;
420                 break;
421         case IIO_ACCEL:
422                 result = inv_mpu6050_switch_engine(st, true,
423                                 INV_MPU6050_BIT_PWR_ACCL_STBY);
424                 if (result)
425                         goto error_power_off;
426                 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
427                                               chan->channel2, val);
428                 result = inv_mpu6050_switch_engine(st, false,
429                                 INV_MPU6050_BIT_PWR_ACCL_STBY);
430                 if (result)
431                         goto error_power_off;
432                 break;
433         case IIO_TEMP:
434                 /* wait for stablization */
435                 msleep(INV_MPU6050_SENSOR_UP_TIME);
436                 ret = inv_mpu6050_sensor_show(st, st->reg->temperature,
437                                               IIO_MOD_X, val);
438                 break;
439         case IIO_MAGN:
440                 ret = inv_mpu_magn_read(st, chan->channel2, val);
441                 break;
442         default:
443                 ret = -EINVAL;
444                 break;
445         }
446
447         result = inv_mpu6050_set_power_itg(st, false);
448         if (result)
449                 goto error_power_off;
450
451         return ret;
452
453 error_power_off:
454         inv_mpu6050_set_power_itg(st, false);
455         return result;
456 }
457
458 static int
459 inv_mpu6050_read_raw(struct iio_dev *indio_dev,
460                      struct iio_chan_spec const *chan,
461                      int *val, int *val2, long mask)
462 {
463         struct inv_mpu6050_state  *st = iio_priv(indio_dev);
464         int ret = 0;
465
466         switch (mask) {
467         case IIO_CHAN_INFO_RAW:
468                 ret = iio_device_claim_direct_mode(indio_dev);
469                 if (ret)
470                         return ret;
471                 mutex_lock(&st->lock);
472                 ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
473                 mutex_unlock(&st->lock);
474                 iio_device_release_direct_mode(indio_dev);
475                 return ret;
476         case IIO_CHAN_INFO_SCALE:
477                 switch (chan->type) {
478                 case IIO_ANGL_VEL:
479                         mutex_lock(&st->lock);
480                         *val  = 0;
481                         *val2 = gyro_scale_6050[st->chip_config.fsr];
482                         mutex_unlock(&st->lock);
483
484                         return IIO_VAL_INT_PLUS_NANO;
485                 case IIO_ACCEL:
486                         mutex_lock(&st->lock);
487                         *val = 0;
488                         *val2 = accel_scale[st->chip_config.accl_fs];
489                         mutex_unlock(&st->lock);
490
491                         return IIO_VAL_INT_PLUS_MICRO;
492                 case IIO_TEMP:
493                         *val = st->hw->temp.scale / 1000000;
494                         *val2 = st->hw->temp.scale % 1000000;
495                         return IIO_VAL_INT_PLUS_MICRO;
496                 case IIO_MAGN:
497                         return inv_mpu_magn_get_scale(st, chan, val, val2);
498                 default:
499                         return -EINVAL;
500                 }
501         case IIO_CHAN_INFO_OFFSET:
502                 switch (chan->type) {
503                 case IIO_TEMP:
504                         *val = st->hw->temp.offset;
505                         return IIO_VAL_INT;
506                 default:
507                         return -EINVAL;
508                 }
509         case IIO_CHAN_INFO_CALIBBIAS:
510                 switch (chan->type) {
511                 case IIO_ANGL_VEL:
512                         mutex_lock(&st->lock);
513                         ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
514                                                 chan->channel2, val);
515                         mutex_unlock(&st->lock);
516                         return IIO_VAL_INT;
517                 case IIO_ACCEL:
518                         mutex_lock(&st->lock);
519                         ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
520                                                 chan->channel2, val);
521                         mutex_unlock(&st->lock);
522                         return IIO_VAL_INT;
523
524                 default:
525                         return -EINVAL;
526                 }
527         default:
528                 return -EINVAL;
529         }
530 }
531
532 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val)
533 {
534         int result, i;
535         u8 d;
536
537         for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
538                 if (gyro_scale_6050[i] == val) {
539                         d = (i << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
540                         result = regmap_write(st->map, st->reg->gyro_config, d);
541                         if (result)
542                                 return result;
543
544                         st->chip_config.fsr = i;
545                         return 0;
546                 }
547         }
548
549         return -EINVAL;
550 }
551
552 static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
553                                  struct iio_chan_spec const *chan, long mask)
554 {
555         switch (mask) {
556         case IIO_CHAN_INFO_SCALE:
557                 switch (chan->type) {
558                 case IIO_ANGL_VEL:
559                         return IIO_VAL_INT_PLUS_NANO;
560                 default:
561                         return IIO_VAL_INT_PLUS_MICRO;
562                 }
563         default:
564                 return IIO_VAL_INT_PLUS_MICRO;
565         }
566
567         return -EINVAL;
568 }
569
570 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val)
571 {
572         int result, i;
573         u8 d;
574
575         for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
576                 if (accel_scale[i] == val) {
577                         d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
578                         result = regmap_write(st->map, st->reg->accl_config, d);
579                         if (result)
580                                 return result;
581
582                         st->chip_config.accl_fs = i;
583                         return 0;
584                 }
585         }
586
587         return -EINVAL;
588 }
589
590 static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
591                                  struct iio_chan_spec const *chan,
592                                  int val, int val2, long mask)
593 {
594         struct inv_mpu6050_state  *st = iio_priv(indio_dev);
595         int result;
596
597         /*
598          * we should only update scale when the chip is disabled, i.e.
599          * not running
600          */
601         result = iio_device_claim_direct_mode(indio_dev);
602         if (result)
603                 return result;
604
605         mutex_lock(&st->lock);
606         result = inv_mpu6050_set_power_itg(st, true);
607         if (result)
608                 goto error_write_raw_unlock;
609
610         switch (mask) {
611         case IIO_CHAN_INFO_SCALE:
612                 switch (chan->type) {
613                 case IIO_ANGL_VEL:
614                         result = inv_mpu6050_write_gyro_scale(st, val2);
615                         break;
616                 case IIO_ACCEL:
617                         result = inv_mpu6050_write_accel_scale(st, val2);
618                         break;
619                 default:
620                         result = -EINVAL;
621                         break;
622                 }
623                 break;
624         case IIO_CHAN_INFO_CALIBBIAS:
625                 switch (chan->type) {
626                 case IIO_ANGL_VEL:
627                         result = inv_mpu6050_sensor_set(st,
628                                                         st->reg->gyro_offset,
629                                                         chan->channel2, val);
630                         break;
631                 case IIO_ACCEL:
632                         result = inv_mpu6050_sensor_set(st,
633                                                         st->reg->accl_offset,
634                                                         chan->channel2, val);
635                         break;
636                 default:
637                         result = -EINVAL;
638                         break;
639                 }
640                 break;
641         default:
642                 result = -EINVAL;
643                 break;
644         }
645
646         result |= inv_mpu6050_set_power_itg(st, false);
647 error_write_raw_unlock:
648         mutex_unlock(&st->lock);
649         iio_device_release_direct_mode(indio_dev);
650
651         return result;
652 }
653
654 /**
655  *  inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
656  *
657  *                  Based on the Nyquist principle, the sampling rate must
658  *                  exceed twice of the bandwidth of the signal, or there
659  *                  would be alising. This function basically search for the
660  *                  correct low pass parameters based on the fifo rate, e.g,
661  *                  sampling frequency.
662  *
663  *  lpf is set automatically when setting sampling rate to avoid any aliases.
664  */
665 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
666 {
667         static const int hz[] = {188, 98, 42, 20, 10, 5};
668         static const int d[] = {
669                 INV_MPU6050_FILTER_188HZ, INV_MPU6050_FILTER_98HZ,
670                 INV_MPU6050_FILTER_42HZ, INV_MPU6050_FILTER_20HZ,
671                 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ
672         };
673         int i, h, result;
674         u8 data;
675
676         h = (rate >> 1);
677         i = 0;
678         while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
679                 i++;
680         data = d[i];
681         result = inv_mpu6050_set_lpf_regs(st, data);
682         if (result)
683                 return result;
684         st->chip_config.lpf = data;
685
686         return 0;
687 }
688
689 /**
690  * inv_mpu6050_fifo_rate_store() - Set fifo rate.
691  */
692 static ssize_t
693 inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
694                             const char *buf, size_t count)
695 {
696         int fifo_rate;
697         u8 d;
698         int result;
699         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
700         struct inv_mpu6050_state *st = iio_priv(indio_dev);
701
702         if (kstrtoint(buf, 10, &fifo_rate))
703                 return -EINVAL;
704         if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
705             fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
706                 return -EINVAL;
707
708         result = iio_device_claim_direct_mode(indio_dev);
709         if (result)
710                 return result;
711
712         /* compute the chip sample rate divider */
713         d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate);
714         /* compute back the fifo rate to handle truncation cases */
715         fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
716
717         mutex_lock(&st->lock);
718         if (d == st->chip_config.divider) {
719                 result = 0;
720                 goto fifo_rate_fail_unlock;
721         }
722         result = inv_mpu6050_set_power_itg(st, true);
723         if (result)
724                 goto fifo_rate_fail_unlock;
725
726         result = regmap_write(st->map, st->reg->sample_rate_div, d);
727         if (result)
728                 goto fifo_rate_fail_power_off;
729         st->chip_config.divider = d;
730
731         result = inv_mpu6050_set_lpf(st, fifo_rate);
732         if (result)
733                 goto fifo_rate_fail_power_off;
734
735         /* update rate for magn, noop if not present in chip */
736         result = inv_mpu_magn_set_rate(st, fifo_rate);
737         if (result)
738                 goto fifo_rate_fail_power_off;
739
740 fifo_rate_fail_power_off:
741         result |= inv_mpu6050_set_power_itg(st, false);
742 fifo_rate_fail_unlock:
743         mutex_unlock(&st->lock);
744         iio_device_release_direct_mode(indio_dev);
745         if (result)
746                 return result;
747
748         return count;
749 }
750
751 /**
752  * inv_fifo_rate_show() - Get the current sampling rate.
753  */
754 static ssize_t
755 inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
756                    char *buf)
757 {
758         struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
759         unsigned fifo_rate;
760
761         mutex_lock(&st->lock);
762         fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
763         mutex_unlock(&st->lock);
764
765         return scnprintf(buf, PAGE_SIZE, "%u\n", fifo_rate);
766 }
767
768 /**
769  * inv_attr_show() - calling this function will show current
770  *                    parameters.
771  *
772  * Deprecated in favor of IIO mounting matrix API.
773  *
774  * See inv_get_mount_matrix()
775  */
776 static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
777                              char *buf)
778 {
779         struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
780         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
781         s8 *m;
782
783         switch (this_attr->address) {
784         /*
785          * In MPU6050, the two matrix are the same because gyro and accel
786          * are integrated in one chip
787          */
788         case ATTR_GYRO_MATRIX:
789         case ATTR_ACCL_MATRIX:
790                 m = st->plat_data.orientation;
791
792                 return scnprintf(buf, PAGE_SIZE,
793                         "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
794                         m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
795         default:
796                 return -EINVAL;
797         }
798 }
799
800 /**
801  * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
802  *                                  MPU6050 device.
803  * @indio_dev: The IIO device
804  * @trig: The new trigger
805  *
806  * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
807  * device, -EINVAL otherwise.
808  */
809 static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
810                                         struct iio_trigger *trig)
811 {
812         struct inv_mpu6050_state *st = iio_priv(indio_dev);
813
814         if (st->trig != trig)
815                 return -EINVAL;
816
817         return 0;
818 }
819
820 static const struct iio_mount_matrix *
821 inv_get_mount_matrix(const struct iio_dev *indio_dev,
822                      const struct iio_chan_spec *chan)
823 {
824         struct inv_mpu6050_state *data = iio_priv(indio_dev);
825         const struct iio_mount_matrix *matrix;
826
827         if (chan->type == IIO_MAGN)
828                 matrix = &data->magn_orient;
829         else
830                 matrix = &data->orientation;
831
832         return matrix;
833 }
834
835 static const struct iio_chan_spec_ext_info inv_ext_info[] = {
836         IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
837         { }
838 };
839
840 #define INV_MPU6050_CHAN(_type, _channel2, _index)                    \
841         {                                                             \
842                 .type = _type,                                        \
843                 .modified = 1,                                        \
844                 .channel2 = _channel2,                                \
845                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
846                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |        \
847                                       BIT(IIO_CHAN_INFO_CALIBBIAS),   \
848                 .scan_index = _index,                                 \
849                 .scan_type = {                                        \
850                                 .sign = 's',                          \
851                                 .realbits = 16,                       \
852                                 .storagebits = 16,                    \
853                                 .shift = 0,                           \
854                                 .endianness = IIO_BE,                 \
855                              },                                       \
856                 .ext_info = inv_ext_info,                             \
857         }
858
859 static const struct iio_chan_spec inv_mpu_channels[] = {
860         IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
861         /*
862          * Note that temperature should only be via polled reading only,
863          * not the final scan elements output.
864          */
865         {
866                 .type = IIO_TEMP,
867                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
868                                 | BIT(IIO_CHAN_INFO_OFFSET)
869                                 | BIT(IIO_CHAN_INFO_SCALE),
870                 .scan_index = -1,
871         },
872         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
873         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
874         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
875
876         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
877         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
878         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
879 };
880
881 static const unsigned long inv_mpu_scan_masks[] = {
882         /* 3-axis accel */
883         BIT(INV_MPU6050_SCAN_ACCL_X)
884                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
885                 | BIT(INV_MPU6050_SCAN_ACCL_Z),
886         /* 3-axis gyro */
887         BIT(INV_MPU6050_SCAN_GYRO_X)
888                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
889                 | BIT(INV_MPU6050_SCAN_GYRO_Z),
890         /* 6-axis accel + gyro */
891         BIT(INV_MPU6050_SCAN_ACCL_X)
892                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
893                 | BIT(INV_MPU6050_SCAN_ACCL_Z)
894                 | BIT(INV_MPU6050_SCAN_GYRO_X)
895                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
896                 | BIT(INV_MPU6050_SCAN_GYRO_Z),
897         0,
898 };
899
900 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index)                    \
901         {                                                               \
902                 .type = IIO_MAGN,                                       \
903                 .modified = 1,                                          \
904                 .channel2 = _chan2,                                     \
905                 .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) |        \
906                                       BIT(IIO_CHAN_INFO_RAW),           \
907                 .scan_index = _index,                                   \
908                 .scan_type = {                                          \
909                         .sign = 's',                                    \
910                         .realbits = _bits,                              \
911                         .storagebits = 16,                              \
912                         .shift = 0,                                     \
913                         .endianness = IIO_BE,                           \
914                 },                                                      \
915                 .ext_info = inv_ext_info,                               \
916         }
917
918 static const struct iio_chan_spec inv_mpu9250_channels[] = {
919         IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
920         /*
921          * Note that temperature should only be via polled reading only,
922          * not the final scan elements output.
923          */
924         {
925                 .type = IIO_TEMP,
926                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
927                                 | BIT(IIO_CHAN_INFO_OFFSET)
928                                 | BIT(IIO_CHAN_INFO_SCALE),
929                 .scan_index = -1,
930         },
931         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
932         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
933         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
934
935         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
936         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
937         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
938
939         /* Magnetometer resolution is 16 bits */
940         INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 16, INV_MPU9X50_SCAN_MAGN_X),
941         INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 16, INV_MPU9X50_SCAN_MAGN_Y),
942         INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 16, INV_MPU9X50_SCAN_MAGN_Z),
943 };
944
945 static const unsigned long inv_mpu9x50_scan_masks[] = {
946         /* 3-axis accel */
947         BIT(INV_MPU6050_SCAN_ACCL_X)
948                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
949                 | BIT(INV_MPU6050_SCAN_ACCL_Z),
950         /* 3-axis gyro */
951         BIT(INV_MPU6050_SCAN_GYRO_X)
952                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
953                 | BIT(INV_MPU6050_SCAN_GYRO_Z),
954         /* 3-axis magn */
955         BIT(INV_MPU9X50_SCAN_MAGN_X)
956                 | BIT(INV_MPU9X50_SCAN_MAGN_Y)
957                 | BIT(INV_MPU9X50_SCAN_MAGN_Z),
958         /* 6-axis accel + gyro */
959         BIT(INV_MPU6050_SCAN_ACCL_X)
960                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
961                 | BIT(INV_MPU6050_SCAN_ACCL_Z)
962                 | BIT(INV_MPU6050_SCAN_GYRO_X)
963                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
964                 | BIT(INV_MPU6050_SCAN_GYRO_Z),
965         /* 6-axis accel + magn */
966         BIT(INV_MPU6050_SCAN_ACCL_X)
967                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
968                 | BIT(INV_MPU6050_SCAN_ACCL_Z)
969                 | BIT(INV_MPU9X50_SCAN_MAGN_X)
970                 | BIT(INV_MPU9X50_SCAN_MAGN_Y)
971                 | BIT(INV_MPU9X50_SCAN_MAGN_Z),
972         /* 6-axis gyro + magn */
973         BIT(INV_MPU6050_SCAN_GYRO_X)
974                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
975                 | BIT(INV_MPU6050_SCAN_GYRO_Z)
976                 | BIT(INV_MPU9X50_SCAN_MAGN_X)
977                 | BIT(INV_MPU9X50_SCAN_MAGN_Y)
978                 | BIT(INV_MPU9X50_SCAN_MAGN_Z),
979         /* 9-axis accel + gyro + magn */
980         BIT(INV_MPU6050_SCAN_ACCL_X)
981                 | BIT(INV_MPU6050_SCAN_ACCL_Y)
982                 | BIT(INV_MPU6050_SCAN_ACCL_Z)
983                 | BIT(INV_MPU6050_SCAN_GYRO_X)
984                 | BIT(INV_MPU6050_SCAN_GYRO_Y)
985                 | BIT(INV_MPU6050_SCAN_GYRO_Z)
986                 | BIT(INV_MPU9X50_SCAN_MAGN_X)
987                 | BIT(INV_MPU9X50_SCAN_MAGN_Y)
988                 | BIT(INV_MPU9X50_SCAN_MAGN_Z),
989         0,
990 };
991
992 static const struct iio_chan_spec inv_icm20602_channels[] = {
993         IIO_CHAN_SOFT_TIMESTAMP(INV_ICM20602_SCAN_TIMESTAMP),
994         {
995                 .type = IIO_TEMP,
996                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
997                                 | BIT(IIO_CHAN_INFO_OFFSET)
998                                 | BIT(IIO_CHAN_INFO_SCALE),
999                 .scan_index = INV_ICM20602_SCAN_TEMP,
1000                 .scan_type = {
1001                                 .sign = 's',
1002                                 .realbits = 16,
1003                                 .storagebits = 16,
1004                                 .shift = 0,
1005                                 .endianness = IIO_BE,
1006                              },
1007         },
1008
1009         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_ICM20602_SCAN_GYRO_X),
1010         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_ICM20602_SCAN_GYRO_Y),
1011         INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_ICM20602_SCAN_GYRO_Z),
1012
1013         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_ICM20602_SCAN_ACCL_Y),
1014         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_ICM20602_SCAN_ACCL_X),
1015         INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_ICM20602_SCAN_ACCL_Z),
1016 };
1017
1018 static const unsigned long inv_icm20602_scan_masks[] = {
1019         /* 3-axis accel + temp (mandatory) */
1020         BIT(INV_ICM20602_SCAN_ACCL_X)
1021                 | BIT(INV_ICM20602_SCAN_ACCL_Y)
1022                 | BIT(INV_ICM20602_SCAN_ACCL_Z)
1023                 | BIT(INV_ICM20602_SCAN_TEMP),
1024         /* 3-axis gyro + temp (mandatory) */
1025         BIT(INV_ICM20602_SCAN_GYRO_X)
1026                 | BIT(INV_ICM20602_SCAN_GYRO_Y)
1027                 | BIT(INV_ICM20602_SCAN_GYRO_Z)
1028                 | BIT(INV_ICM20602_SCAN_TEMP),
1029         /* 6-axis accel + gyro + temp (mandatory) */
1030         BIT(INV_ICM20602_SCAN_ACCL_X)
1031                 | BIT(INV_ICM20602_SCAN_ACCL_Y)
1032                 | BIT(INV_ICM20602_SCAN_ACCL_Z)
1033                 | BIT(INV_ICM20602_SCAN_GYRO_X)
1034                 | BIT(INV_ICM20602_SCAN_GYRO_Y)
1035                 | BIT(INV_ICM20602_SCAN_GYRO_Z)
1036                 | BIT(INV_ICM20602_SCAN_TEMP),
1037         0,
1038 };
1039
1040 /*
1041  * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1042  * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1043  * low-pass filter. Specifically, each of these sampling rates are about twice
1044  * the bandwidth of a corresponding low-pass filter, which should eliminate
1045  * aliasing following the Nyquist principle. By picking a frequency different
1046  * from these, the user risks aliasing effects.
1047  */
1048 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1049 static IIO_CONST_ATTR(in_anglvel_scale_available,
1050                                           "0.000133090 0.000266181 0.000532362 0.001064724");
1051 static IIO_CONST_ATTR(in_accel_scale_available,
1052                                           "0.000598 0.001196 0.002392 0.004785");
1053 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
1054         inv_mpu6050_fifo_rate_store);
1055
1056 /* Deprecated: kept for userspace backward compatibility. */
1057 static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
1058         ATTR_GYRO_MATRIX);
1059 static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
1060         ATTR_ACCL_MATRIX);
1061
1062 static struct attribute *inv_attributes[] = {
1063         &iio_dev_attr_in_gyro_matrix.dev_attr.attr,  /* deprecated */
1064         &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
1065         &iio_dev_attr_sampling_frequency.dev_attr.attr,
1066         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1067         &iio_const_attr_in_accel_scale_available.dev_attr.attr,
1068         &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
1069         NULL,
1070 };
1071
1072 static const struct attribute_group inv_attribute_group = {
1073         .attrs = inv_attributes
1074 };
1075
1076 static const struct iio_info mpu_info = {
1077         .read_raw = &inv_mpu6050_read_raw,
1078         .write_raw = &inv_mpu6050_write_raw,
1079         .write_raw_get_fmt = &inv_write_raw_get_fmt,
1080         .attrs = &inv_attribute_group,
1081         .validate_trigger = inv_mpu6050_validate_trigger,
1082 };
1083
1084 /**
1085  *  inv_check_and_setup_chip() - check and setup chip.
1086  */
1087 static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
1088 {
1089         int result;
1090         unsigned int regval;
1091         int i;
1092
1093         st->hw  = &hw_info[st->chip_type];
1094         st->reg = hw_info[st->chip_type].reg;
1095
1096         /* check chip self-identification */
1097         result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, &regval);
1098         if (result)
1099                 return result;
1100         if (regval != st->hw->whoami) {
1101                 /* check whoami against all possible values */
1102                 for (i = 0; i < INV_NUM_PARTS; ++i) {
1103                         if (regval == hw_info[i].whoami) {
1104                                 dev_warn(regmap_get_device(st->map),
1105                                         "whoami mismatch got %#02x (%s)"
1106                                         "expected %#02hhx (%s)\n",
1107                                         regval, hw_info[i].name,
1108                                         st->hw->whoami, st->hw->name);
1109                                 break;
1110                         }
1111                 }
1112                 if (i >= INV_NUM_PARTS) {
1113                         dev_err(regmap_get_device(st->map),
1114                                 "invalid whoami %#02x expected %#02hhx (%s)\n",
1115                                 regval, st->hw->whoami, st->hw->name);
1116                         return -ENODEV;
1117                 }
1118         }
1119
1120         /* reset to make sure previous state are not there */
1121         result = regmap_write(st->map, st->reg->pwr_mgmt_1,
1122                               INV_MPU6050_BIT_H_RESET);
1123         if (result)
1124                 return result;
1125         msleep(INV_MPU6050_POWER_UP_TIME);
1126
1127         /*
1128          * Turn power on. After reset, the sleep bit could be on
1129          * or off depending on the OTP settings. Turning power on
1130          * make it in a definite state as well as making the hardware
1131          * state align with the software state
1132          */
1133         result = inv_mpu6050_set_power_itg(st, true);
1134         if (result)
1135                 return result;
1136
1137         result = inv_mpu6050_switch_engine(st, false,
1138                                            INV_MPU6050_BIT_PWR_ACCL_STBY);
1139         if (result)
1140                 goto error_power_off;
1141         result = inv_mpu6050_switch_engine(st, false,
1142                                            INV_MPU6050_BIT_PWR_GYRO_STBY);
1143         if (result)
1144                 goto error_power_off;
1145
1146         return inv_mpu6050_set_power_itg(st, false);
1147
1148 error_power_off:
1149         inv_mpu6050_set_power_itg(st, false);
1150         return result;
1151 }
1152
1153 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state *st)
1154 {
1155         int result;
1156
1157         result = regulator_enable(st->vddio_supply);
1158         if (result) {
1159                 dev_err(regmap_get_device(st->map),
1160                         "Failed to enable vddio regulator: %d\n", result);
1161         } else {
1162                 /* Give the device a little bit of time to start up. */
1163                 usleep_range(35000, 70000);
1164         }
1165
1166         return result;
1167 }
1168
1169 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
1170 {
1171         int result;
1172
1173         result = regulator_disable(st->vddio_supply);
1174         if (result)
1175                 dev_err(regmap_get_device(st->map),
1176                         "Failed to disable vddio regulator: %d\n", result);
1177
1178         return result;
1179 }
1180
1181 static void inv_mpu_core_disable_regulator_action(void *_data)
1182 {
1183         struct inv_mpu6050_state *st = _data;
1184         int result;
1185
1186         result = regulator_disable(st->vdd_supply);
1187         if (result)
1188                 dev_err(regmap_get_device(st->map),
1189                         "Failed to disable vdd regulator: %d\n", result);
1190
1191         inv_mpu_core_disable_regulator_vddio(st);
1192 }
1193
1194 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
1195                 int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
1196 {
1197         struct inv_mpu6050_state *st;
1198         struct iio_dev *indio_dev;
1199         struct inv_mpu6050_platform_data *pdata;
1200         struct device *dev = regmap_get_device(regmap);
1201         int result;
1202         struct irq_data *desc;
1203         int irq_type;
1204
1205         indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1206         if (!indio_dev)
1207                 return -ENOMEM;
1208
1209         BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
1210         if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
1211                 dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
1212                                 chip_type, name);
1213                 return -ENODEV;
1214         }
1215         st = iio_priv(indio_dev);
1216         mutex_init(&st->lock);
1217         st->chip_type = chip_type;
1218         st->powerup_count = 0;
1219         st->irq = irq;
1220         st->map = regmap;
1221
1222         pdata = dev_get_platdata(dev);
1223         if (!pdata) {
1224                 result = iio_read_mount_matrix(dev, "mount-matrix",
1225                                                &st->orientation);
1226                 if (result) {
1227                         dev_err(dev, "Failed to retrieve mounting matrix %d\n",
1228                                 result);
1229                         return result;
1230                 }
1231         } else {
1232                 st->plat_data = *pdata;
1233         }
1234
1235         desc = irq_get_irq_data(irq);
1236         if (!desc) {
1237                 dev_err(dev, "Could not find IRQ %d\n", irq);
1238                 return -EINVAL;
1239         }
1240
1241         irq_type = irqd_get_trigger_type(desc);
1242         if (!irq_type)
1243                 irq_type = IRQF_TRIGGER_RISING;
1244         if (irq_type == IRQF_TRIGGER_RISING)
1245                 st->irq_mask = INV_MPU6050_ACTIVE_HIGH;
1246         else if (irq_type == IRQF_TRIGGER_FALLING)
1247                 st->irq_mask = INV_MPU6050_ACTIVE_LOW;
1248         else if (irq_type == IRQF_TRIGGER_HIGH)
1249                 st->irq_mask = INV_MPU6050_ACTIVE_HIGH |
1250                         INV_MPU6050_LATCH_INT_EN;
1251         else if (irq_type == IRQF_TRIGGER_LOW)
1252                 st->irq_mask = INV_MPU6050_ACTIVE_LOW |
1253                         INV_MPU6050_LATCH_INT_EN;
1254         else {
1255                 dev_err(dev, "Invalid interrupt type 0x%x specified\n",
1256                         irq_type);
1257                 return -EINVAL;
1258         }
1259
1260         st->vdd_supply = devm_regulator_get(dev, "vdd");
1261         if (IS_ERR(st->vdd_supply)) {
1262                 if (PTR_ERR(st->vdd_supply) != -EPROBE_DEFER)
1263                         dev_err(dev, "Failed to get vdd regulator %d\n",
1264                                 (int)PTR_ERR(st->vdd_supply));
1265
1266                 return PTR_ERR(st->vdd_supply);
1267         }
1268
1269         st->vddio_supply = devm_regulator_get(dev, "vddio");
1270         if (IS_ERR(st->vddio_supply)) {
1271                 if (PTR_ERR(st->vddio_supply) != -EPROBE_DEFER)
1272                         dev_err(dev, "Failed to get vddio regulator %d\n",
1273                                 (int)PTR_ERR(st->vddio_supply));
1274
1275                 return PTR_ERR(st->vddio_supply);
1276         }
1277
1278         result = regulator_enable(st->vdd_supply);
1279         if (result) {
1280                 dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
1281                 return result;
1282         }
1283
1284         result = inv_mpu_core_enable_regulator_vddio(st);
1285         if (result) {
1286                 regulator_disable(st->vdd_supply);
1287                 return result;
1288         }
1289
1290         result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
1291                                  st);
1292         if (result) {
1293                 dev_err(dev, "Failed to setup regulator cleanup action %d\n",
1294                         result);
1295                 return result;
1296         }
1297
1298         /* fill magnetometer orientation */
1299         result = inv_mpu_magn_set_orient(st);
1300         if (result)
1301                 return result;
1302
1303         /* power is turned on inside check chip type*/
1304         result = inv_check_and_setup_chip(st);
1305         if (result)
1306                 return result;
1307
1308         result = inv_mpu6050_init_config(indio_dev);
1309         if (result) {
1310                 dev_err(dev, "Could not initialize device.\n");
1311                 return result;
1312         }
1313
1314         dev_set_drvdata(dev, indio_dev);
1315         indio_dev->dev.parent = dev;
1316         /* name will be NULL when enumerated via ACPI */
1317         if (name)
1318                 indio_dev->name = name;
1319         else
1320                 indio_dev->name = dev_name(dev);
1321
1322         /* requires parent device set in indio_dev */
1323         if (inv_mpu_bus_setup)
1324                 inv_mpu_bus_setup(indio_dev);
1325
1326         switch (chip_type) {
1327         case INV_MPU9250:
1328         case INV_MPU9255:
1329                 /*
1330                  * Use magnetometer inside the chip only if there is no i2c
1331                  * auxiliary device in use.
1332                  */
1333                 if (!st->magn_disabled) {
1334                         indio_dev->channels = inv_mpu9250_channels;
1335                         indio_dev->num_channels = ARRAY_SIZE(inv_mpu9250_channels);
1336                         indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
1337                 } else {
1338                         indio_dev->channels = inv_mpu_channels;
1339                         indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1340                         indio_dev->available_scan_masks = inv_mpu_scan_masks;
1341                 }
1342                 break;
1343         case INV_ICM20602:
1344                 indio_dev->channels = inv_icm20602_channels;
1345                 indio_dev->num_channels = ARRAY_SIZE(inv_icm20602_channels);
1346                 indio_dev->available_scan_masks = inv_icm20602_scan_masks;
1347                 break;
1348         default:
1349                 indio_dev->channels = inv_mpu_channels;
1350                 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
1351                 indio_dev->available_scan_masks = inv_mpu_scan_masks;
1352                 break;
1353         }
1354
1355         indio_dev->info = &mpu_info;
1356         indio_dev->modes = INDIO_BUFFER_TRIGGERED;
1357
1358         result = devm_iio_triggered_buffer_setup(dev, indio_dev,
1359                                                  iio_pollfunc_store_time,
1360                                                  inv_mpu6050_read_fifo,
1361                                                  NULL);
1362         if (result) {
1363                 dev_err(dev, "configure buffer fail %d\n", result);
1364                 return result;
1365         }
1366         result = inv_mpu6050_probe_trigger(indio_dev, irq_type);
1367         if (result) {
1368                 dev_err(dev, "trigger probe fail %d\n", result);
1369                 return result;
1370         }
1371
1372         result = devm_iio_device_register(dev, indio_dev);
1373         if (result) {
1374                 dev_err(dev, "IIO register fail %d\n", result);
1375                 return result;
1376         }
1377
1378         return 0;
1379 }
1380 EXPORT_SYMBOL_GPL(inv_mpu_core_probe);
1381
1382 #ifdef CONFIG_PM_SLEEP
1383
1384 static int inv_mpu_resume(struct device *dev)
1385 {
1386         struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1387         int result;
1388
1389         mutex_lock(&st->lock);
1390         result = inv_mpu_core_enable_regulator_vddio(st);
1391         if (result)
1392                 goto out_unlock;
1393
1394         result = inv_mpu6050_set_power_itg(st, true);
1395 out_unlock:
1396         mutex_unlock(&st->lock);
1397
1398         return result;
1399 }
1400
1401 static int inv_mpu_suspend(struct device *dev)
1402 {
1403         struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
1404         int result;
1405
1406         mutex_lock(&st->lock);
1407         result = inv_mpu6050_set_power_itg(st, false);
1408         inv_mpu_core_disable_regulator_vddio(st);
1409         mutex_unlock(&st->lock);
1410
1411         return result;
1412 }
1413 #endif /* CONFIG_PM_SLEEP */
1414
1415 SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
1416 EXPORT_SYMBOL_GPL(inv_mpu_pmops);
1417
1418 MODULE_AUTHOR("Invensense Corporation");
1419 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
1420 MODULE_LICENSE("GPL");