2 * Copyright (C) 2012 Invensense, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/err.h>
18 #include <linux/delay.h>
19 #include <linux/sysfs.h>
20 #include <linux/jiffies.h>
21 #include <linux/irq.h>
22 #include <linux/interrupt.h>
23 #include <linux/kfifo.h>
24 #include <linux/spinlock.h>
25 #include <linux/iio/iio.h>
26 #include <linux/i2c-mux.h>
27 #include <linux/acpi.h>
28 #include "inv_mpu_iio.h"
31 * this is the gyro scale translated from dynamic range plus/minus
32 * {250, 500, 1000, 2000} to rad/s
34 static const int gyro_scale_6050
[] = {133090, 266181, 532362, 1064724};
37 * this is the accel scale translated from dynamic range plus/minus
38 * {2, 4, 8, 16} to m/s^2
40 static const int accel_scale
[] = {598, 1196, 2392, 4785};
42 static const struct inv_mpu6050_reg_map reg_set_6050
= {
43 .sample_rate_div
= INV_MPU6050_REG_SAMPLE_RATE_DIV
,
44 .lpf
= INV_MPU6050_REG_CONFIG
,
45 .user_ctrl
= INV_MPU6050_REG_USER_CTRL
,
46 .fifo_en
= INV_MPU6050_REG_FIFO_EN
,
47 .gyro_config
= INV_MPU6050_REG_GYRO_CONFIG
,
48 .accl_config
= INV_MPU6050_REG_ACCEL_CONFIG
,
49 .fifo_count_h
= INV_MPU6050_REG_FIFO_COUNT_H
,
50 .fifo_r_w
= INV_MPU6050_REG_FIFO_R_W
,
51 .raw_gyro
= INV_MPU6050_REG_RAW_GYRO
,
52 .raw_accl
= INV_MPU6050_REG_RAW_ACCEL
,
53 .temperature
= INV_MPU6050_REG_TEMPERATURE
,
54 .int_enable
= INV_MPU6050_REG_INT_ENABLE
,
55 .pwr_mgmt_1
= INV_MPU6050_REG_PWR_MGMT_1
,
56 .pwr_mgmt_2
= INV_MPU6050_REG_PWR_MGMT_2
,
57 .int_pin_cfg
= INV_MPU6050_REG_INT_PIN_CFG
,
60 static const struct inv_mpu6050_chip_config chip_config_6050
= {
61 .fsr
= INV_MPU6050_FSR_2000DPS
,
62 .lpf
= INV_MPU6050_FILTER_20HZ
,
63 .fifo_rate
= INV_MPU6050_INIT_FIFO_RATE
,
64 .gyro_fifo_enable
= false,
65 .accl_fifo_enable
= false,
66 .accl_fs
= INV_MPU6050_FS_02G
,
69 static const struct inv_mpu6050_hw hw_info
[INV_NUM_PARTS
] = {
74 .config
= &chip_config_6050
,
78 int inv_mpu6050_write_reg(struct inv_mpu6050_state
*st
, int reg
, u8 d
)
80 return i2c_smbus_write_i2c_block_data(st
->client
, reg
, 1, &d
);
84 * The i2c read/write needs to happen in unlocked mode. As the parent
85 * adapter is common. If we use locked versions, it will fail as
86 * the mux adapter will lock the parent i2c adapter, while calling
87 * select/deselect functions.
89 static int inv_mpu6050_write_reg_unlocked(struct inv_mpu6050_state
*st
,
94 struct i2c_msg msg
[1] = {
96 .addr
= st
->client
->addr
,
105 ret
= __i2c_transfer(st
->client
->adapter
, msg
, 1);
112 static int inv_mpu6050_select_bypass(struct i2c_adapter
*adap
, void *mux_priv
,
115 struct iio_dev
*indio_dev
= mux_priv
;
116 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
119 /* Use the same mutex which was used everywhere to protect power-op */
120 mutex_lock(&indio_dev
->mlock
);
121 if (!st
->powerup_count
) {
122 ret
= inv_mpu6050_write_reg_unlocked(st
, st
->reg
->pwr_mgmt_1
,
127 msleep(INV_MPU6050_REG_UP_TIME
);
131 ret
= inv_mpu6050_write_reg_unlocked(st
, st
->reg
->int_pin_cfg
,
133 INV_MPU6050_BIT_BYPASS_EN
);
136 mutex_unlock(&indio_dev
->mlock
);
141 static int inv_mpu6050_deselect_bypass(struct i2c_adapter
*adap
,
142 void *mux_priv
, u32 chan_id
)
144 struct iio_dev
*indio_dev
= mux_priv
;
145 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
147 mutex_lock(&indio_dev
->mlock
);
148 /* It doesn't really mattter, if any of the calls fails */
149 inv_mpu6050_write_reg_unlocked(st
, st
->reg
->int_pin_cfg
,
152 if (!st
->powerup_count
)
153 inv_mpu6050_write_reg_unlocked(st
, st
->reg
->pwr_mgmt_1
,
154 INV_MPU6050_BIT_SLEEP
);
155 mutex_unlock(&indio_dev
->mlock
);
160 int inv_mpu6050_switch_engine(struct inv_mpu6050_state
*st
, bool en
, u32 mask
)
165 /* switch clock needs to be careful. Only when gyro is on, can
166 clock source be switched to gyro. Otherwise, it must be set to
168 if (INV_MPU6050_BIT_PWR_GYRO_STBY
== mask
) {
169 result
= i2c_smbus_read_i2c_block_data(st
->client
,
170 st
->reg
->pwr_mgmt_1
, 1, &mgmt_1
);
174 mgmt_1
&= ~INV_MPU6050_BIT_CLK_MASK
;
177 if ((INV_MPU6050_BIT_PWR_GYRO_STBY
== mask
) && (!en
)) {
178 /* turning off gyro requires switch to internal clock first.
179 Then turn off gyro engine */
180 mgmt_1
|= INV_CLK_INTERNAL
;
181 result
= inv_mpu6050_write_reg(st
, st
->reg
->pwr_mgmt_1
, mgmt_1
);
186 result
= i2c_smbus_read_i2c_block_data(st
->client
,
187 st
->reg
->pwr_mgmt_2
, 1, &d
);
194 result
= inv_mpu6050_write_reg(st
, st
->reg
->pwr_mgmt_2
, d
);
199 /* Wait for output stabilize */
200 msleep(INV_MPU6050_TEMP_UP_TIME
);
201 if (INV_MPU6050_BIT_PWR_GYRO_STBY
== mask
) {
202 /* switch internal clock to PLL */
203 mgmt_1
|= INV_CLK_PLL
;
204 result
= inv_mpu6050_write_reg(st
,
205 st
->reg
->pwr_mgmt_1
, mgmt_1
);
214 int inv_mpu6050_set_power_itg(struct inv_mpu6050_state
*st
, bool power_on
)
219 /* Already under indio-dev->mlock mutex */
220 if (!st
->powerup_count
)
221 result
= inv_mpu6050_write_reg(st
, st
->reg
->pwr_mgmt_1
,
227 if (!st
->powerup_count
)
228 result
= inv_mpu6050_write_reg(st
, st
->reg
->pwr_mgmt_1
,
229 INV_MPU6050_BIT_SLEEP
);
236 msleep(INV_MPU6050_REG_UP_TIME
);
242 * inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
244 * Initial configuration:
248 * Clock source: Gyro PLL
250 static int inv_mpu6050_init_config(struct iio_dev
*indio_dev
)
254 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
256 result
= inv_mpu6050_set_power_itg(st
, true);
259 d
= (INV_MPU6050_FSR_2000DPS
<< INV_MPU6050_GYRO_CONFIG_FSR_SHIFT
);
260 result
= inv_mpu6050_write_reg(st
, st
->reg
->gyro_config
, d
);
264 d
= INV_MPU6050_FILTER_20HZ
;
265 result
= inv_mpu6050_write_reg(st
, st
->reg
->lpf
, d
);
269 d
= INV_MPU6050_ONE_K_HZ
/ INV_MPU6050_INIT_FIFO_RATE
- 1;
270 result
= inv_mpu6050_write_reg(st
, st
->reg
->sample_rate_div
, d
);
274 d
= (INV_MPU6050_FS_02G
<< INV_MPU6050_ACCL_CONFIG_FSR_SHIFT
);
275 result
= inv_mpu6050_write_reg(st
, st
->reg
->accl_config
, d
);
279 memcpy(&st
->chip_config
, hw_info
[st
->chip_type
].config
,
280 sizeof(struct inv_mpu6050_chip_config
));
281 result
= inv_mpu6050_set_power_itg(st
, false);
286 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state
*st
, int reg
,
292 ind
= (axis
- IIO_MOD_X
) * 2;
293 result
= i2c_smbus_read_i2c_block_data(st
->client
, reg
+ ind
, 2,
297 *val
= (short)be16_to_cpup(&d
);
302 static int inv_mpu6050_read_raw(struct iio_dev
*indio_dev
,
303 struct iio_chan_spec
const *chan
,
307 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
310 case IIO_CHAN_INFO_RAW
:
316 mutex_lock(&indio_dev
->mlock
);
317 if (!st
->chip_config
.enable
) {
318 result
= inv_mpu6050_set_power_itg(st
, true);
322 /* when enable is on, power is already on */
323 switch (chan
->type
) {
325 if (!st
->chip_config
.gyro_fifo_enable
||
326 !st
->chip_config
.enable
) {
327 result
= inv_mpu6050_switch_engine(st
, true,
328 INV_MPU6050_BIT_PWR_GYRO_STBY
);
332 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->raw_gyro
,
333 chan
->channel2
, val
);
334 if (!st
->chip_config
.gyro_fifo_enable
||
335 !st
->chip_config
.enable
) {
336 result
= inv_mpu6050_switch_engine(st
, false,
337 INV_MPU6050_BIT_PWR_GYRO_STBY
);
343 if (!st
->chip_config
.accl_fifo_enable
||
344 !st
->chip_config
.enable
) {
345 result
= inv_mpu6050_switch_engine(st
, true,
346 INV_MPU6050_BIT_PWR_ACCL_STBY
);
350 ret
= inv_mpu6050_sensor_show(st
, st
->reg
->raw_accl
,
351 chan
->channel2
, val
);
352 if (!st
->chip_config
.accl_fifo_enable
||
353 !st
->chip_config
.enable
) {
354 result
= inv_mpu6050_switch_engine(st
, false,
355 INV_MPU6050_BIT_PWR_ACCL_STBY
);
361 /* wait for stablization */
362 msleep(INV_MPU6050_SENSOR_UP_TIME
);
363 inv_mpu6050_sensor_show(st
, st
->reg
->temperature
,
371 if (!st
->chip_config
.enable
)
372 result
|= inv_mpu6050_set_power_itg(st
, false);
373 mutex_unlock(&indio_dev
->mlock
);
379 case IIO_CHAN_INFO_SCALE
:
380 switch (chan
->type
) {
383 *val2
= gyro_scale_6050
[st
->chip_config
.fsr
];
385 return IIO_VAL_INT_PLUS_NANO
;
388 *val2
= accel_scale
[st
->chip_config
.accl_fs
];
390 return IIO_VAL_INT_PLUS_MICRO
;
393 *val2
= INV_MPU6050_TEMP_SCALE
;
395 return IIO_VAL_INT_PLUS_MICRO
;
399 case IIO_CHAN_INFO_OFFSET
:
400 switch (chan
->type
) {
402 *val
= INV_MPU6050_TEMP_OFFSET
;
413 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state
*st
, int val
)
418 for (i
= 0; i
< ARRAY_SIZE(gyro_scale_6050
); ++i
) {
419 if (gyro_scale_6050
[i
] == val
) {
420 d
= (i
<< INV_MPU6050_GYRO_CONFIG_FSR_SHIFT
);
421 result
= inv_mpu6050_write_reg(st
,
422 st
->reg
->gyro_config
, d
);
426 st
->chip_config
.fsr
= i
;
434 static int inv_write_raw_get_fmt(struct iio_dev
*indio_dev
,
435 struct iio_chan_spec
const *chan
, long mask
)
438 case IIO_CHAN_INFO_SCALE
:
439 switch (chan
->type
) {
441 return IIO_VAL_INT_PLUS_NANO
;
443 return IIO_VAL_INT_PLUS_MICRO
;
446 return IIO_VAL_INT_PLUS_MICRO
;
451 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state
*st
, int val
)
456 for (i
= 0; i
< ARRAY_SIZE(accel_scale
); ++i
) {
457 if (accel_scale
[i
] == val
) {
458 d
= (i
<< INV_MPU6050_ACCL_CONFIG_FSR_SHIFT
);
459 result
= inv_mpu6050_write_reg(st
,
460 st
->reg
->accl_config
, d
);
464 st
->chip_config
.accl_fs
= i
;
472 static int inv_mpu6050_write_raw(struct iio_dev
*indio_dev
,
473 struct iio_chan_spec
const *chan
,
477 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
480 mutex_lock(&indio_dev
->mlock
);
481 /* we should only update scale when the chip is disabled, i.e.,
483 if (st
->chip_config
.enable
) {
485 goto error_write_raw
;
487 result
= inv_mpu6050_set_power_itg(st
, true);
489 goto error_write_raw
;
492 case IIO_CHAN_INFO_SCALE
:
493 switch (chan
->type
) {
495 result
= inv_mpu6050_write_gyro_scale(st
, val2
);
498 result
= inv_mpu6050_write_accel_scale(st
, val2
);
511 result
|= inv_mpu6050_set_power_itg(st
, false);
512 mutex_unlock(&indio_dev
->mlock
);
518 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
520 * Based on the Nyquist principle, the sampling rate must
521 * exceed twice of the bandwidth of the signal, or there
522 * would be alising. This function basically search for the
523 * correct low pass parameters based on the fifo rate, e.g,
524 * sampling frequency.
526 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state
*st
, int rate
)
528 const int hz
[] = {188, 98, 42, 20, 10, 5};
529 const int d
[] = {INV_MPU6050_FILTER_188HZ
, INV_MPU6050_FILTER_98HZ
,
530 INV_MPU6050_FILTER_42HZ
, INV_MPU6050_FILTER_20HZ
,
531 INV_MPU6050_FILTER_10HZ
, INV_MPU6050_FILTER_5HZ
};
537 while ((h
< hz
[i
]) && (i
< ARRAY_SIZE(d
) - 1))
540 result
= inv_mpu6050_write_reg(st
, st
->reg
->lpf
, data
);
543 st
->chip_config
.lpf
= data
;
549 * inv_mpu6050_fifo_rate_store() - Set fifo rate.
551 static ssize_t
inv_mpu6050_fifo_rate_store(struct device
*dev
,
552 struct device_attribute
*attr
, const char *buf
, size_t count
)
557 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
558 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
560 if (kstrtoint(buf
, 10, &fifo_rate
))
562 if (fifo_rate
< INV_MPU6050_MIN_FIFO_RATE
||
563 fifo_rate
> INV_MPU6050_MAX_FIFO_RATE
)
565 if (fifo_rate
== st
->chip_config
.fifo_rate
)
568 mutex_lock(&indio_dev
->mlock
);
569 if (st
->chip_config
.enable
) {
573 result
= inv_mpu6050_set_power_itg(st
, true);
577 d
= INV_MPU6050_ONE_K_HZ
/ fifo_rate
- 1;
578 result
= inv_mpu6050_write_reg(st
, st
->reg
->sample_rate_div
, d
);
581 st
->chip_config
.fifo_rate
= fifo_rate
;
583 result
= inv_mpu6050_set_lpf(st
, fifo_rate
);
588 result
|= inv_mpu6050_set_power_itg(st
, false);
589 mutex_unlock(&indio_dev
->mlock
);
597 * inv_fifo_rate_show() - Get the current sampling rate.
599 static ssize_t
inv_fifo_rate_show(struct device
*dev
,
600 struct device_attribute
*attr
, char *buf
)
602 struct inv_mpu6050_state
*st
= iio_priv(dev_to_iio_dev(dev
));
604 return sprintf(buf
, "%d\n", st
->chip_config
.fifo_rate
);
608 * inv_attr_show() - calling this function will show current
611 static ssize_t
inv_attr_show(struct device
*dev
,
612 struct device_attribute
*attr
, char *buf
)
614 struct inv_mpu6050_state
*st
= iio_priv(dev_to_iio_dev(dev
));
615 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
618 switch (this_attr
->address
) {
619 /* In MPU6050, the two matrix are the same because gyro and accel
620 are integrated in one chip */
621 case ATTR_GYRO_MATRIX
:
622 case ATTR_ACCL_MATRIX
:
623 m
= st
->plat_data
.orientation
;
625 return sprintf(buf
, "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
626 m
[0], m
[1], m
[2], m
[3], m
[4], m
[5], m
[6], m
[7], m
[8]);
633 * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
635 * @indio_dev: The IIO device
636 * @trig: The new trigger
638 * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
639 * device, -EINVAL otherwise.
641 static int inv_mpu6050_validate_trigger(struct iio_dev
*indio_dev
,
642 struct iio_trigger
*trig
)
644 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
646 if (st
->trig
!= trig
)
652 #define INV_MPU6050_CHAN(_type, _channel2, _index) \
656 .channel2 = _channel2, \
657 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
658 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
659 .scan_index = _index, \
665 .endianness = IIO_BE, \
669 static const struct iio_chan_spec inv_mpu_channels
[] = {
670 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP
),
672 * Note that temperature should only be via polled reading only,
673 * not the final scan elements output.
677 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
)
678 | BIT(IIO_CHAN_INFO_OFFSET
)
679 | BIT(IIO_CHAN_INFO_SCALE
),
682 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_X
, INV_MPU6050_SCAN_GYRO_X
),
683 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_GYRO_Y
),
684 INV_MPU6050_CHAN(IIO_ANGL_VEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_GYRO_Z
),
686 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_X
, INV_MPU6050_SCAN_ACCL_X
),
687 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Y
, INV_MPU6050_SCAN_ACCL_Y
),
688 INV_MPU6050_CHAN(IIO_ACCEL
, IIO_MOD_Z
, INV_MPU6050_SCAN_ACCL_Z
),
691 /* constant IIO attribute */
692 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
693 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO
| S_IWUSR
, inv_fifo_rate_show
,
694 inv_mpu6050_fifo_rate_store
);
695 static IIO_DEVICE_ATTR(in_gyro_matrix
, S_IRUGO
, inv_attr_show
, NULL
,
697 static IIO_DEVICE_ATTR(in_accel_matrix
, S_IRUGO
, inv_attr_show
, NULL
,
700 static struct attribute
*inv_attributes
[] = {
701 &iio_dev_attr_in_gyro_matrix
.dev_attr
.attr
,
702 &iio_dev_attr_in_accel_matrix
.dev_attr
.attr
,
703 &iio_dev_attr_sampling_frequency
.dev_attr
.attr
,
704 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
708 static const struct attribute_group inv_attribute_group
= {
709 .attrs
= inv_attributes
712 static const struct iio_info mpu_info
= {
713 .driver_module
= THIS_MODULE
,
714 .read_raw
= &inv_mpu6050_read_raw
,
715 .write_raw
= &inv_mpu6050_write_raw
,
716 .write_raw_get_fmt
= &inv_write_raw_get_fmt
,
717 .attrs
= &inv_attribute_group
,
718 .validate_trigger
= inv_mpu6050_validate_trigger
,
722 * inv_check_and_setup_chip() - check and setup chip.
724 static int inv_check_and_setup_chip(struct inv_mpu6050_state
*st
,
725 const struct i2c_device_id
*id
)
729 st
->chip_type
= INV_MPU6050
;
730 st
->hw
= &hw_info
[st
->chip_type
];
731 st
->reg
= hw_info
[st
->chip_type
].reg
;
733 /* reset to make sure previous state are not there */
734 result
= inv_mpu6050_write_reg(st
, st
->reg
->pwr_mgmt_1
,
735 INV_MPU6050_BIT_H_RESET
);
738 msleep(INV_MPU6050_POWER_UP_TIME
);
739 /* toggle power state. After reset, the sleep bit could be on
740 or off depending on the OTP settings. Toggling power would
741 make it in a definite state as well as making the hardware
742 state align with the software state */
743 result
= inv_mpu6050_set_power_itg(st
, false);
746 result
= inv_mpu6050_set_power_itg(st
, true);
750 result
= inv_mpu6050_switch_engine(st
, false,
751 INV_MPU6050_BIT_PWR_ACCL_STBY
);
754 result
= inv_mpu6050_switch_engine(st
, false,
755 INV_MPU6050_BIT_PWR_GYRO_STBY
);
763 * inv_mpu_probe() - probe function.
764 * @client: i2c client.
765 * @id: i2c device id.
767 * Returns 0 on success, a negative error code otherwise.
769 static int inv_mpu_probe(struct i2c_client
*client
,
770 const struct i2c_device_id
*id
)
772 struct inv_mpu6050_state
*st
;
773 struct iio_dev
*indio_dev
;
774 struct inv_mpu6050_platform_data
*pdata
;
777 if (!i2c_check_functionality(client
->adapter
,
778 I2C_FUNC_SMBUS_I2C_BLOCK
))
781 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
785 st
= iio_priv(indio_dev
);
787 st
->powerup_count
= 0;
788 pdata
= dev_get_platdata(&client
->dev
);
790 st
->plat_data
= *pdata
;
791 /* power is turned on inside check chip type*/
792 result
= inv_check_and_setup_chip(st
, id
);
796 result
= inv_mpu6050_init_config(indio_dev
);
798 dev_err(&client
->dev
,
799 "Could not initialize device.\n");
803 i2c_set_clientdata(client
, indio_dev
);
804 indio_dev
->dev
.parent
= &client
->dev
;
805 /* id will be NULL when enumerated via ACPI */
807 indio_dev
->name
= (char *)id
->name
;
809 indio_dev
->name
= (char *)dev_name(&client
->dev
);
810 indio_dev
->channels
= inv_mpu_channels
;
811 indio_dev
->num_channels
= ARRAY_SIZE(inv_mpu_channels
);
813 indio_dev
->info
= &mpu_info
;
814 indio_dev
->modes
= INDIO_BUFFER_TRIGGERED
;
816 result
= iio_triggered_buffer_setup(indio_dev
,
817 inv_mpu6050_irq_handler
,
818 inv_mpu6050_read_fifo
,
821 dev_err(&st
->client
->dev
, "configure buffer fail %d\n",
825 result
= inv_mpu6050_probe_trigger(indio_dev
);
827 dev_err(&st
->client
->dev
, "trigger probe fail %d\n", result
);
831 INIT_KFIFO(st
->timestamps
);
832 spin_lock_init(&st
->time_stamp_lock
);
833 result
= iio_device_register(indio_dev
);
835 dev_err(&st
->client
->dev
, "IIO register fail %d\n", result
);
836 goto out_remove_trigger
;
839 st
->mux_adapter
= i2c_add_mux_adapter(client
->adapter
,
843 inv_mpu6050_select_bypass
,
844 inv_mpu6050_deselect_bypass
);
845 if (!st
->mux_adapter
) {
847 goto out_unreg_device
;
850 result
= inv_mpu_acpi_create_mux_client(st
);
857 i2c_del_mux_adapter(st
->mux_adapter
);
859 iio_device_unregister(indio_dev
);
861 inv_mpu6050_remove_trigger(st
);
863 iio_triggered_buffer_cleanup(indio_dev
);
867 static int inv_mpu_remove(struct i2c_client
*client
)
869 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
870 struct inv_mpu6050_state
*st
= iio_priv(indio_dev
);
872 inv_mpu_acpi_delete_mux_client(st
);
873 i2c_del_mux_adapter(st
->mux_adapter
);
874 iio_device_unregister(indio_dev
);
875 inv_mpu6050_remove_trigger(st
);
876 iio_triggered_buffer_cleanup(indio_dev
);
880 #ifdef CONFIG_PM_SLEEP
882 static int inv_mpu_resume(struct device
*dev
)
884 return inv_mpu6050_set_power_itg(
885 iio_priv(i2c_get_clientdata(to_i2c_client(dev
))), true);
888 static int inv_mpu_suspend(struct device
*dev
)
890 return inv_mpu6050_set_power_itg(
891 iio_priv(i2c_get_clientdata(to_i2c_client(dev
))), false);
893 static SIMPLE_DEV_PM_OPS(inv_mpu_pmops
, inv_mpu_suspend
, inv_mpu_resume
);
895 #define INV_MPU6050_PMOPS (&inv_mpu_pmops)
897 #define INV_MPU6050_PMOPS NULL
898 #endif /* CONFIG_PM_SLEEP */
901 * device id table is used to identify what device can be
902 * supported by this driver
904 static const struct i2c_device_id inv_mpu_id
[] = {
905 {"mpu6050", INV_MPU6050
},
906 {"mpu6500", INV_MPU6500
},
910 MODULE_DEVICE_TABLE(i2c
, inv_mpu_id
);
912 static const struct acpi_device_id inv_acpi_match
[] = {
917 MODULE_DEVICE_TABLE(acpi
, inv_acpi_match
);
919 static struct i2c_driver inv_mpu_driver
= {
920 .probe
= inv_mpu_probe
,
921 .remove
= inv_mpu_remove
,
922 .id_table
= inv_mpu_id
,
924 .owner
= THIS_MODULE
,
925 .name
= "inv-mpu6050",
926 .pm
= INV_MPU6050_PMOPS
,
927 .acpi_match_table
= ACPI_PTR(inv_acpi_match
),
931 module_i2c_driver(inv_mpu_driver
);
933 MODULE_AUTHOR("Invensense Corporation");
934 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
935 MODULE_LICENSE("GPL");