1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 TDK-InvenSense, Inc.
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/regmap.h>
9 #include <linux/delay.h>
11 #include "inv_mpu_aux.h"
12 #include "inv_mpu_iio.h"
15 * i2c master auxiliary bus transfer function.
16 * Requires the i2c operations to be correctly setup before.
18 static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state
*st
)
20 /* use 50hz frequency for xfer */
21 const unsigned int freq
= 50;
22 const unsigned int period_ms
= 1000 / freq
;
24 unsigned int user_ctrl
;
28 d
= INV_MPU6050_FIFO_RATE_TO_DIVIDER(freq
);
29 ret
= regmap_write(st
->map
, st
->reg
->sample_rate_div
, d
);
33 /* start i2c master */
34 user_ctrl
= st
->chip_config
.user_ctrl
| INV_MPU6050_BIT_I2C_MST_EN
;
35 ret
= regmap_write(st
->map
, st
->reg
->user_ctrl
, user_ctrl
);
37 goto error_restore_rate
;
39 /* wait for xfer: 1 period + half-period margin */
40 msleep(period_ms
+ period_ms
/ 2);
43 user_ctrl
= st
->chip_config
.user_ctrl
;
44 ret
= regmap_write(st
->map
, st
->reg
->user_ctrl
, user_ctrl
);
48 /* restore sample rate */
49 d
= st
->chip_config
.divider
;
50 ret
= regmap_write(st
->map
, st
->reg
->sample_rate_div
, d
);
52 goto error_restore_rate
;
57 regmap_write(st
->map
, st
->reg
->user_ctrl
, st
->chip_config
.user_ctrl
);
59 regmap_write(st
->map
, st
->reg
->sample_rate_div
, st
->chip_config
.divider
);
64 * inv_mpu_aux_init() - init i2c auxiliary bus
65 * @st: driver internal state
67 * Returns 0 on success, a negative error code otherwise.
69 int inv_mpu_aux_init(const struct inv_mpu6050_state
*st
)
74 /* configure i2c master */
75 val
= INV_MPU6050_BITS_I2C_MST_CLK_400KHZ
|
76 INV_MPU6050_BIT_WAIT_FOR_ES
;
77 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_MST_CTRL
, val
);
81 /* configure i2c master delay */
82 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV4_CTRL
, 0);
86 val
= INV_MPU6050_BIT_I2C_SLV0_DLY_EN
|
87 INV_MPU6050_BIT_I2C_SLV1_DLY_EN
|
88 INV_MPU6050_BIT_I2C_SLV2_DLY_EN
|
89 INV_MPU6050_BIT_I2C_SLV3_DLY_EN
|
90 INV_MPU6050_BIT_DELAY_ES_SHADOW
;
91 return regmap_write(st
->map
, INV_MPU6050_REG_I2C_MST_DELAY_CTRL
, val
);
95 * inv_mpu_aux_read() - read register function for i2c auxiliary bus
96 * @st: driver internal state.
97 * @addr: chip i2c Address
98 * @reg: chip register address
99 * @val: buffer for storing read bytes
100 * @size: number of bytes to read
102 * Returns 0 on success, a negative error code otherwise.
104 int inv_mpu_aux_read(const struct inv_mpu6050_state
*st
, uint8_t addr
,
105 uint8_t reg
, uint8_t *val
, size_t size
)
113 /* setup i2c SLV0 control: i2c addr, register, enable + size */
114 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_ADDR(0),
115 INV_MPU6050_BIT_I2C_SLV_RNW
| addr
);
118 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_REG(0), reg
);
121 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0),
122 INV_MPU6050_BIT_SLV_EN
| size
);
127 ret
= inv_mpu_i2c_master_xfer(st
);
129 goto error_disable_i2c
;
131 /* disable i2c slave */
132 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
134 goto error_disable_i2c
;
136 /* check i2c status */
137 ret
= regmap_read(st
->map
, INV_MPU6050_REG_I2C_MST_STATUS
, &status
);
140 if (status
& INV_MPU6050_BIT_I2C_SLV0_NACK
)
143 /* read data in registers */
144 return regmap_bulk_read(st
->map
, INV_MPU6050_REG_EXT_SENS_DATA
,
148 regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
153 * inv_mpu_aux_write() - write register function for i2c auxiliary bus
154 * @st: driver internal state.
155 * @addr: chip i2c Address
156 * @reg: chip register address
157 * @val: 1 byte value to write
159 * Returns 0 on success, a negative error code otherwise.
161 int inv_mpu_aux_write(const struct inv_mpu6050_state
*st
, uint8_t addr
,
162 uint8_t reg
, uint8_t val
)
167 /* setup i2c SLV0 control: i2c addr, register, value, enable + size */
168 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_ADDR(0), addr
);
171 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_REG(0), reg
);
174 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_DO(0), val
);
177 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0),
178 INV_MPU6050_BIT_SLV_EN
| 1);
183 ret
= inv_mpu_i2c_master_xfer(st
);
185 goto error_disable_i2c
;
187 /* disable i2c slave */
188 ret
= regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
190 goto error_disable_i2c
;
192 /* check i2c status */
193 ret
= regmap_read(st
->map
, INV_MPU6050_REG_I2C_MST_STATUS
, &status
);
196 if (status
& INV_MPU6050_BIT_I2C_SLV0_NACK
)
202 regmap_write(st
->map
, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);