xref: /linux/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c (revision c26f4fbd58375bd6ef74f95eb73d61762ad97c59)
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/acpi.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/math64.h>
19 #include <linux/minmax.h>
20 #include <linux/pm.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
23 
24 #include <linux/iio/common/inv_sensors_timestamp.h>
25 #include <linux/iio/iio.h>
26 
27 #include "inv_mpu_iio.h"
28 #include "inv_mpu_magn.h"
29 
30 /*
31  * this is the gyro scale translated from dynamic range plus/minus
32  * {250, 500, 1000, 2000} to rad/s
33  */
34 static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
35 
36 /*
37  * this is the accel scale translated from dynamic range plus/minus
38  * {2, 4, 8, 16} to m/s^2
39  */
40 static const int accel_scale[] = {598, 1196, 2392, 4785};
41 
42 static const struct inv_mpu6050_reg_map reg_set_icm20602 = {
43 	.sample_rate_div	= INV_MPU6050_REG_SAMPLE_RATE_DIV,
44 	.lpf                    = INV_MPU6050_REG_CONFIG,
45 	.accel_lpf              = INV_MPU6500_REG_ACCEL_CONFIG_2,
46 	.user_ctrl              = INV_MPU6050_REG_USER_CTRL,
47 	.fifo_en                = INV_MPU6050_REG_FIFO_EN,
48 	.gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
49 	.accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
50 	.fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
51 	.fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
52 	.raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
53 	.raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
54 	.temperature            = INV_MPU6050_REG_TEMPERATURE,
55 	.int_enable             = INV_MPU6050_REG_INT_ENABLE,
56 	.int_status             = INV_MPU6050_REG_INT_STATUS,
57 	.pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
58 	.pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
59 	.int_pin_cfg            = INV_MPU6050_REG_INT_PIN_CFG,
60 	.accl_offset            = INV_MPU6500_REG_ACCEL_OFFSET,
61 	.gyro_offset            = INV_MPU6050_REG_GYRO_OFFSET,
62 	.i2c_if                 = INV_ICM20602_REG_I2C_IF,
63 };
64 
65 static const struct inv_mpu6050_reg_map reg_set_6500 = {
66 	.sample_rate_div	= INV_MPU6050_REG_SAMPLE_RATE_DIV,
67 	.lpf                    = INV_MPU6050_REG_CONFIG,
68 	.accel_lpf              = INV_MPU6500_REG_ACCEL_CONFIG_2,
69 	.user_ctrl              = INV_MPU6050_REG_USER_CTRL,
70 	.fifo_en                = INV_MPU6050_REG_FIFO_EN,
71 	.gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
72 	.accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
73 	.fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
74 	.fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
75 	.raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
76 	.raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
77 	.temperature            = INV_MPU6050_REG_TEMPERATURE,
78 	.int_enable             = INV_MPU6050_REG_INT_ENABLE,
79 	.int_status             = INV_MPU6050_REG_INT_STATUS,
80 	.pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
81 	.pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
82 	.int_pin_cfg		= INV_MPU6050_REG_INT_PIN_CFG,
83 	.accl_offset		= INV_MPU6500_REG_ACCEL_OFFSET,
84 	.gyro_offset		= INV_MPU6050_REG_GYRO_OFFSET,
85 	.i2c_if                 = 0,
86 };
87 
88 static const struct inv_mpu6050_reg_map reg_set_6050 = {
89 	.sample_rate_div	= INV_MPU6050_REG_SAMPLE_RATE_DIV,
90 	.lpf                    = INV_MPU6050_REG_CONFIG,
91 	.user_ctrl              = INV_MPU6050_REG_USER_CTRL,
92 	.fifo_en                = INV_MPU6050_REG_FIFO_EN,
93 	.gyro_config            = INV_MPU6050_REG_GYRO_CONFIG,
94 	.accl_config            = INV_MPU6050_REG_ACCEL_CONFIG,
95 	.fifo_count_h           = INV_MPU6050_REG_FIFO_COUNT_H,
96 	.fifo_r_w               = INV_MPU6050_REG_FIFO_R_W,
97 	.raw_gyro               = INV_MPU6050_REG_RAW_GYRO,
98 	.raw_accl               = INV_MPU6050_REG_RAW_ACCEL,
99 	.temperature            = INV_MPU6050_REG_TEMPERATURE,
100 	.int_enable             = INV_MPU6050_REG_INT_ENABLE,
101 	.pwr_mgmt_1             = INV_MPU6050_REG_PWR_MGMT_1,
102 	.pwr_mgmt_2             = INV_MPU6050_REG_PWR_MGMT_2,
103 	.int_pin_cfg		= INV_MPU6050_REG_INT_PIN_CFG,
104 	.accl_offset		= INV_MPU6050_REG_ACCEL_OFFSET,
105 	.gyro_offset		= INV_MPU6050_REG_GYRO_OFFSET,
106 	.i2c_if                 = 0,
107 };
108 
109 static const struct inv_mpu6050_chip_config chip_config_6050 = {
110 	.clk = INV_CLK_INTERNAL,
111 	.fsr = INV_MPU6050_FSR_2000DPS,
112 	.lpf = INV_MPU6050_FILTER_20HZ,
113 	.divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
114 	.gyro_en = true,
115 	.accl_en = true,
116 	.temp_en = true,
117 	.magn_en = false,
118 	.gyro_fifo_enable = false,
119 	.accl_fifo_enable = false,
120 	.temp_fifo_enable = false,
121 	.magn_fifo_enable = false,
122 	.accl_fs = INV_MPU6050_FS_02G,
123 	.user_ctrl = 0,
124 };
125 
126 static const struct inv_mpu6050_chip_config chip_config_6500 = {
127 	.clk = INV_CLK_PLL,
128 	.fsr = INV_MPU6050_FSR_2000DPS,
129 	.lpf = INV_MPU6050_FILTER_20HZ,
130 	.divider = INV_MPU6050_FIFO_RATE_TO_DIVIDER(50),
131 	.gyro_en = true,
132 	.accl_en = true,
133 	.temp_en = true,
134 	.magn_en = false,
135 	.gyro_fifo_enable = false,
136 	.accl_fifo_enable = false,
137 	.temp_fifo_enable = false,
138 	.magn_fifo_enable = false,
139 	.accl_fs = INV_MPU6050_FS_02G,
140 	.user_ctrl = 0,
141 };
142 
143 /* Indexed by enum inv_devices */
144 static const struct inv_mpu6050_hw hw_info[] = {
145 	{
146 		.whoami = INV_MPU6050_WHOAMI_VALUE,
147 		.name = "MPU6050",
148 		.reg = &reg_set_6050,
149 		.config = &chip_config_6050,
150 		.fifo_size = 1024,
151 		.temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
152 		.startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
153 	},
154 	{
155 		.whoami = INV_MPU6500_WHOAMI_VALUE,
156 		.name = "MPU6500",
157 		.reg = &reg_set_6500,
158 		.config = &chip_config_6500,
159 		.fifo_size = 512,
160 		.temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
161 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
162 	},
163 	{
164 		.whoami = INV_MPU6515_WHOAMI_VALUE,
165 		.name = "MPU6515",
166 		.reg = &reg_set_6500,
167 		.config = &chip_config_6500,
168 		.fifo_size = 512,
169 		.temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
170 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
171 	},
172 	{
173 		.whoami = INV_MPU6880_WHOAMI_VALUE,
174 		.name = "MPU6880",
175 		.reg = &reg_set_6500,
176 		.config = &chip_config_6500,
177 		.fifo_size = 4096,
178 		.temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
179 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
180 	},
181 	{
182 		.whoami = INV_MPU6000_WHOAMI_VALUE,
183 		.name = "MPU6000",
184 		.reg = &reg_set_6050,
185 		.config = &chip_config_6050,
186 		.fifo_size = 1024,
187 		.temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
188 		.startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
189 	},
190 	{
191 		.whoami = INV_MPU9150_WHOAMI_VALUE,
192 		.name = "MPU9150",
193 		.reg = &reg_set_6050,
194 		.config = &chip_config_6050,
195 		.fifo_size = 1024,
196 		.temp = {INV_MPU6050_TEMP_OFFSET, INV_MPU6050_TEMP_SCALE},
197 		.startup_time = {INV_MPU6050_GYRO_STARTUP_TIME, INV_MPU6050_ACCEL_STARTUP_TIME},
198 	},
199 	{
200 		.whoami = INV_MPU9250_WHOAMI_VALUE,
201 		.name = "MPU9250",
202 		.reg = &reg_set_6500,
203 		.config = &chip_config_6500,
204 		.fifo_size = 512,
205 		.temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
206 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
207 	},
208 	{
209 		.whoami = INV_MPU9255_WHOAMI_VALUE,
210 		.name = "MPU9255",
211 		.reg = &reg_set_6500,
212 		.config = &chip_config_6500,
213 		.fifo_size = 512,
214 		.temp = {INV_MPU6500_TEMP_OFFSET, INV_MPU6500_TEMP_SCALE},
215 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
216 	},
217 	{
218 		.whoami = INV_ICM20608_WHOAMI_VALUE,
219 		.name = "ICM20608",
220 		.reg = &reg_set_6500,
221 		.config = &chip_config_6500,
222 		.fifo_size = 512,
223 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
224 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
225 	},
226 	{
227 		.whoami = INV_ICM20608D_WHOAMI_VALUE,
228 		.name = "ICM20608D",
229 		.reg = &reg_set_6500,
230 		.config = &chip_config_6500,
231 		.fifo_size = 512,
232 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
233 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
234 	},
235 	{
236 		.whoami = INV_ICM20609_WHOAMI_VALUE,
237 		.name = "ICM20609",
238 		.reg = &reg_set_6500,
239 		.config = &chip_config_6500,
240 		.fifo_size = 4 * 1024,
241 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
242 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
243 	},
244 	{
245 		.whoami = INV_ICM20689_WHOAMI_VALUE,
246 		.name = "ICM20689",
247 		.reg = &reg_set_6500,
248 		.config = &chip_config_6500,
249 		.fifo_size = 4 * 1024,
250 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
251 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
252 	},
253 	{
254 		.whoami = INV_ICM20600_WHOAMI_VALUE,
255 		.name = "ICM20600",
256 		.reg = &reg_set_icm20602,
257 		.config = &chip_config_6500,
258 		.fifo_size = 1008,
259 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
260 		.startup_time = {INV_ICM20602_GYRO_STARTUP_TIME, INV_ICM20602_ACCEL_STARTUP_TIME},
261 	},
262 	{
263 		.whoami = INV_ICM20602_WHOAMI_VALUE,
264 		.name = "ICM20602",
265 		.reg = &reg_set_icm20602,
266 		.config = &chip_config_6500,
267 		.fifo_size = 1008,
268 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
269 		.startup_time = {INV_ICM20602_GYRO_STARTUP_TIME, INV_ICM20602_ACCEL_STARTUP_TIME},
270 	},
271 	{
272 		.whoami = INV_ICM20690_WHOAMI_VALUE,
273 		.name = "ICM20690",
274 		.reg = &reg_set_6500,
275 		.config = &chip_config_6500,
276 		.fifo_size = 1024,
277 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
278 		.startup_time = {INV_ICM20690_GYRO_STARTUP_TIME, INV_ICM20690_ACCEL_STARTUP_TIME},
279 	},
280 	{	.whoami = INV_IAM20380_WHOAMI_VALUE,
281 		.name = "IAM20380",
282 		.reg = &reg_set_6500,
283 		.config = &chip_config_6500,
284 		.fifo_size = 512,
285 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
286 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
287 	},
288 	{
289 		.whoami = INV_IAM20680_WHOAMI_VALUE,
290 		.name = "IAM20680",
291 		.reg = &reg_set_6500,
292 		.config = &chip_config_6500,
293 		.fifo_size = 512,
294 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
295 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
296 	},
297 	{
298 		.whoami = INV_IAM20680HP_WHOAMI_VALUE,
299 		.name = "IAM20680HP",
300 		.reg = &reg_set_6500,
301 		.config = &chip_config_6500,
302 		.fifo_size = 4 * 1024,
303 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
304 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
305 	},
306 	{
307 		.whoami = INV_IAM20680HT_WHOAMI_VALUE,
308 		.name = "IAM20680HT",
309 		.reg = &reg_set_6500,
310 		.config = &chip_config_6500,
311 		.fifo_size = 4 * 1024,
312 		.temp = {INV_ICM20608_TEMP_OFFSET, INV_ICM20608_TEMP_SCALE},
313 		.startup_time = {INV_MPU6500_GYRO_STARTUP_TIME, INV_MPU6500_ACCEL_STARTUP_TIME},
314 	},
315 };
316 
inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state * st,bool sleep,bool cycle,int clock,int temp_dis)317 static int inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state *st, bool sleep,
318 					bool cycle, int clock, int temp_dis)
319 {
320 	u8 val;
321 
322 	if (clock < 0)
323 		clock = st->chip_config.clk;
324 	if (temp_dis < 0)
325 		temp_dis = !st->chip_config.temp_en;
326 
327 	val = clock & INV_MPU6050_BIT_CLK_MASK;
328 	if (temp_dis)
329 		val |= INV_MPU6050_BIT_TEMP_DIS;
330 	if (sleep)
331 		val |= INV_MPU6050_BIT_SLEEP;
332 	if (cycle)
333 		val |= INV_MPU6050_BIT_CYCLE;
334 
335 	dev_dbg(regmap_get_device(st->map), "pwr_mgmt_1: 0x%x\n", val);
336 	return regmap_write(st->map, st->reg->pwr_mgmt_1, val);
337 }
338 
inv_mpu6050_clock_switch(struct inv_mpu6050_state * st,unsigned int clock)339 static int inv_mpu6050_clock_switch(struct inv_mpu6050_state *st,
340 				    unsigned int clock)
341 {
342 	int ret;
343 
344 	switch (st->chip_type) {
345 	case INV_MPU6050:
346 	case INV_MPU6000:
347 	case INV_MPU9150:
348 		/* old chips: switch clock manually */
349 		ret = inv_mpu6050_pwr_mgmt_1_write(st, false, false, clock, -1);
350 		if (ret)
351 			return ret;
352 		st->chip_config.clk = clock;
353 		break;
354 	default:
355 		/* automatic clock switching, nothing to do */
356 		break;
357 	}
358 
359 	return 0;
360 }
361 
inv_mpu6050_switch_engine(struct inv_mpu6050_state * st,bool en,unsigned int mask)362 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en,
363 			      unsigned int mask)
364 {
365 	unsigned int sleep, val;
366 	u8 pwr_mgmt2, user_ctrl;
367 	int ret;
368 
369 	/* delete useless requests */
370 	if (mask & INV_MPU6050_SENSOR_ACCL && en == st->chip_config.accl_en)
371 		mask &= ~INV_MPU6050_SENSOR_ACCL;
372 	if (mask & INV_MPU6050_SENSOR_GYRO && en == st->chip_config.gyro_en)
373 		mask &= ~INV_MPU6050_SENSOR_GYRO;
374 	if (mask & INV_MPU6050_SENSOR_TEMP && en == st->chip_config.temp_en)
375 		mask &= ~INV_MPU6050_SENSOR_TEMP;
376 	if (mask & INV_MPU6050_SENSOR_MAGN && en == st->chip_config.magn_en)
377 		mask &= ~INV_MPU6050_SENSOR_MAGN;
378 	if (mask & INV_MPU6050_SENSOR_WOM && en == st->chip_config.wom_en)
379 		mask &= ~INV_MPU6050_SENSOR_WOM;
380 
381 	/* force accel on if WoM is on and not going off */
382 	if (!en && (mask & INV_MPU6050_SENSOR_ACCL) && st->chip_config.wom_en &&
383 			!(mask & INV_MPU6050_SENSOR_WOM))
384 		mask &= ~INV_MPU6050_SENSOR_ACCL;
385 
386 	if (mask == 0)
387 		return 0;
388 
389 	/* turn on/off temperature sensor */
390 	if (mask & INV_MPU6050_SENSOR_TEMP) {
391 		ret = inv_mpu6050_pwr_mgmt_1_write(st, false, false, -1, !en);
392 		if (ret)
393 			return ret;
394 		st->chip_config.temp_en = en;
395 	}
396 
397 	/* update user_crtl for driving magnetometer */
398 	if (mask & INV_MPU6050_SENSOR_MAGN) {
399 		user_ctrl = st->chip_config.user_ctrl;
400 		if (en)
401 			user_ctrl |= INV_MPU6050_BIT_I2C_MST_EN;
402 		else
403 			user_ctrl &= ~INV_MPU6050_BIT_I2C_MST_EN;
404 		ret = regmap_write(st->map, st->reg->user_ctrl, user_ctrl);
405 		if (ret)
406 			return ret;
407 		st->chip_config.user_ctrl = user_ctrl;
408 		st->chip_config.magn_en = en;
409 	}
410 
411 	/* manage accel & gyro engines */
412 	if (mask & (INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO)) {
413 		/* compute power management 2 current value */
414 		pwr_mgmt2 = 0;
415 		if (!st->chip_config.accl_en)
416 			pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
417 		if (!st->chip_config.gyro_en)
418 			pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
419 
420 		/* update to new requested value */
421 		if (mask & INV_MPU6050_SENSOR_ACCL) {
422 			if (en)
423 				pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_ACCL_STBY;
424 			else
425 				pwr_mgmt2 |= INV_MPU6050_BIT_PWR_ACCL_STBY;
426 		}
427 		if (mask & INV_MPU6050_SENSOR_GYRO) {
428 			if (en)
429 				pwr_mgmt2 &= ~INV_MPU6050_BIT_PWR_GYRO_STBY;
430 			else
431 				pwr_mgmt2 |= INV_MPU6050_BIT_PWR_GYRO_STBY;
432 		}
433 
434 		/* switch clock to internal when turning gyro off */
435 		if (mask & INV_MPU6050_SENSOR_GYRO && !en) {
436 			ret = inv_mpu6050_clock_switch(st, INV_CLK_INTERNAL);
437 			if (ret)
438 				return ret;
439 		}
440 
441 		/* update sensors engine */
442 		dev_dbg(regmap_get_device(st->map), "pwr_mgmt_2: 0x%x\n",
443 			pwr_mgmt2);
444 		ret = regmap_write(st->map, st->reg->pwr_mgmt_2, pwr_mgmt2);
445 		if (ret)
446 			return ret;
447 		if (mask & INV_MPU6050_SENSOR_ACCL)
448 			st->chip_config.accl_en = en;
449 		if (mask & INV_MPU6050_SENSOR_GYRO)
450 			st->chip_config.gyro_en = en;
451 
452 		/* compute required time to have sensors stabilized */
453 		sleep = 0;
454 		if (en) {
455 			if (mask & INV_MPU6050_SENSOR_ACCL) {
456 				if (sleep < st->hw->startup_time.accel)
457 					sleep = st->hw->startup_time.accel;
458 			}
459 			if (mask & INV_MPU6050_SENSOR_GYRO) {
460 				if (sleep < st->hw->startup_time.gyro)
461 					sleep = st->hw->startup_time.gyro;
462 			}
463 		} else {
464 			if (mask & INV_MPU6050_SENSOR_GYRO) {
465 				if (sleep < INV_MPU6050_GYRO_DOWN_TIME)
466 					sleep = INV_MPU6050_GYRO_DOWN_TIME;
467 			}
468 		}
469 		if (sleep)
470 			msleep(sleep);
471 
472 		/* switch clock to PLL when turning gyro on */
473 		if (mask & INV_MPU6050_SENSOR_GYRO && en) {
474 			ret = inv_mpu6050_clock_switch(st, INV_CLK_PLL);
475 			if (ret)
476 				return ret;
477 		}
478 	}
479 
480 	/* enable/disable accel intelligence control */
481 	if (mask & INV_MPU6050_SENSOR_WOM) {
482 		val = en ? INV_MPU6500_BIT_ACCEL_INTEL_EN |
483 			   INV_MPU6500_BIT_ACCEL_INTEL_MODE : 0;
484 		ret = regmap_write(st->map, INV_MPU6500_REG_ACCEL_INTEL_CTRL, val);
485 		if (ret)
486 			return ret;
487 		st->chip_config.wom_en = en;
488 	}
489 
490 	return 0;
491 }
492 
inv_mpu6050_set_power_itg(struct inv_mpu6050_state * st,bool power_on)493 static int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st,
494 				     bool power_on)
495 {
496 	int result;
497 
498 	result = inv_mpu6050_pwr_mgmt_1_write(st, !power_on, false, -1, -1);
499 	if (result)
500 		return result;
501 
502 	if (power_on)
503 		usleep_range(INV_MPU6050_REG_UP_TIME_MIN,
504 			     INV_MPU6050_REG_UP_TIME_MAX);
505 
506 	return 0;
507 }
508 
inv_mpu6050_set_gyro_fsr(struct inv_mpu6050_state * st,enum inv_mpu6050_fsr_e val)509 static int inv_mpu6050_set_gyro_fsr(struct inv_mpu6050_state *st,
510 				    enum inv_mpu6050_fsr_e val)
511 {
512 	unsigned int gyro_shift;
513 	u8 data;
514 
515 	switch (st->chip_type) {
516 	case INV_ICM20690:
517 		gyro_shift = INV_ICM20690_GYRO_CONFIG_FSR_SHIFT;
518 		break;
519 	default:
520 		gyro_shift = INV_MPU6050_GYRO_CONFIG_FSR_SHIFT;
521 		break;
522 	}
523 
524 	data = val << gyro_shift;
525 	return regmap_write(st->map, st->reg->gyro_config, data);
526 }
527 
inv_mpu6050_set_accel_lpf_regs(struct inv_mpu6050_state * st,enum inv_mpu6050_filter_e val)528 static int inv_mpu6050_set_accel_lpf_regs(struct inv_mpu6050_state *st,
529 					  enum inv_mpu6050_filter_e val)
530 {
531 	switch (st->chip_type) {
532 	case INV_MPU6050:
533 	case INV_MPU6000:
534 	case INV_MPU9150:
535 		/* old chips, nothing to do */
536 		return 0;
537 	case INV_ICM20689:
538 	case INV_ICM20690:
539 	case INV_IAM20680HT:
540 	case INV_IAM20680HP:
541 		/* set FIFO size to maximum value */
542 		val |= INV_ICM20689_BITS_FIFO_SIZE_MAX;
543 		break;
544 	default:
545 		break;
546 	}
547 
548 	return regmap_write(st->map, st->reg->accel_lpf, val);
549 }
550 
551 /*
552  *  inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent
553  *
554  *  MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope
555  *  MPU6500 and above have a dedicated register for accelerometer
556  */
inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state * st,enum inv_mpu6050_filter_e val)557 static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st,
558 				    enum inv_mpu6050_filter_e val)
559 {
560 	int result;
561 
562 	result = regmap_write(st->map, st->reg->lpf, val);
563 	if (result)
564 		return result;
565 
566 	/* set accel lpf */
567 	return inv_mpu6050_set_accel_lpf_regs(st, val);
568 }
569 
570 /*
571  *  inv_mpu6050_init_config() - Initialize hardware, disable FIFO.
572  *
573  *  Initial configuration:
574  *  FSR: ± 2000DPS
575  *  DLPF: 20Hz
576  *  FIFO rate: 50Hz
577  *  Clock source: Gyro PLL
578  */
inv_mpu6050_init_config(struct iio_dev * indio_dev)579 static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
580 {
581 	int result;
582 	u8 d;
583 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
584 	struct inv_sensors_timestamp_chip timestamp;
585 
586 	result = inv_mpu6050_set_gyro_fsr(st, st->chip_config.fsr);
587 	if (result)
588 		return result;
589 
590 	result = inv_mpu6050_set_lpf_regs(st, st->chip_config.lpf);
591 	if (result)
592 		return result;
593 
594 	d = st->chip_config.divider;
595 	result = regmap_write(st->map, st->reg->sample_rate_div, d);
596 	if (result)
597 		return result;
598 
599 	d = (st->chip_config.accl_fs << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
600 	result = regmap_write(st->map, st->reg->accl_config, d);
601 	if (result)
602 		return result;
603 
604 	result = regmap_write(st->map, st->reg->int_pin_cfg, st->irq_mask);
605 	if (result)
606 		return result;
607 
608 	/* clock jitter is +/- 2% */
609 	timestamp.clock_period = NSEC_PER_SEC / INV_MPU6050_INTERNAL_FREQ_HZ;
610 	timestamp.jitter = 20;
611 	timestamp.init_period =
612 			NSEC_PER_SEC / INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
613 	inv_sensors_timestamp_init(&st->timestamp, &timestamp);
614 
615 	/* magn chip init, noop if not present in the chip */
616 	result = inv_mpu_magn_probe(st);
617 	if (result)
618 		return result;
619 
620 	return 0;
621 }
622 
inv_mpu6050_sensor_set(struct inv_mpu6050_state * st,int reg,int axis,int val)623 static int inv_mpu6050_sensor_set(struct inv_mpu6050_state  *st, int reg,
624 				int axis, int val)
625 {
626 	int ind;
627 	__be16 d = cpu_to_be16(val);
628 
629 	ind = (axis - IIO_MOD_X) * 2;
630 
631 	return regmap_bulk_write(st->map, reg + ind, &d, sizeof(d));
632 }
633 
inv_mpu6050_sensor_show(struct inv_mpu6050_state * st,int reg,int axis,int * val)634 static int inv_mpu6050_sensor_show(struct inv_mpu6050_state  *st, int reg,
635 				   int axis, int *val)
636 {
637 	int ind, result;
638 	__be16 d;
639 
640 	ind = (axis - IIO_MOD_X) * 2;
641 	result = regmap_bulk_read(st->map, reg + ind, &d, sizeof(d));
642 	if (result)
643 		return result;
644 	*val = (short)be16_to_cpup(&d);
645 
646 	return IIO_VAL_INT;
647 }
648 
inv_mpu6050_read_channel_data(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val)649 static int inv_mpu6050_read_channel_data(struct iio_dev *indio_dev,
650 					 struct iio_chan_spec const *chan,
651 					 int *val)
652 {
653 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
654 	struct device *pdev = regmap_get_device(st->map);
655 	unsigned int freq_hz, period_us, min_sleep_us, max_sleep_us;
656 	int result;
657 	int ret;
658 
659 	/* compute sample period */
660 	freq_hz = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
661 	period_us = 1000000 / freq_hz;
662 
663 	result = pm_runtime_resume_and_get(pdev);
664 	if (result)
665 		return result;
666 
667 	switch (chan->type) {
668 	case IIO_ANGL_VEL:
669 		if (!st->chip_config.gyro_en) {
670 			result = inv_mpu6050_switch_engine(st, true,
671 					INV_MPU6050_SENSOR_GYRO);
672 			if (result)
673 				goto error_power_off;
674 			/* need to wait 2 periods to have first valid sample */
675 			min_sleep_us = 2 * period_us;
676 			max_sleep_us = 2 * (period_us + period_us / 2);
677 			usleep_range(min_sleep_us, max_sleep_us);
678 		}
679 		ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
680 					      chan->channel2, val);
681 		break;
682 	case IIO_ACCEL:
683 		if (!st->chip_config.accl_en) {
684 			result = inv_mpu6050_switch_engine(st, true,
685 					INV_MPU6050_SENSOR_ACCL);
686 			if (result)
687 				goto error_power_off;
688 			/* wait 1 period for first sample availability */
689 			min_sleep_us = period_us;
690 			max_sleep_us = period_us + period_us / 2;
691 			usleep_range(min_sleep_us, max_sleep_us);
692 		}
693 		ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
694 					      chan->channel2, val);
695 		break;
696 	case IIO_TEMP:
697 		/* temperature sensor work only with accel and/or gyro */
698 		if (!st->chip_config.accl_en && !st->chip_config.gyro_en) {
699 			result = -EBUSY;
700 			goto error_power_off;
701 		}
702 		if (!st->chip_config.temp_en) {
703 			result = inv_mpu6050_switch_engine(st, true,
704 					INV_MPU6050_SENSOR_TEMP);
705 			if (result)
706 				goto error_power_off;
707 			/* wait 1 period for first sample availability */
708 			min_sleep_us = period_us;
709 			max_sleep_us = period_us + period_us / 2;
710 			usleep_range(min_sleep_us, max_sleep_us);
711 		}
712 		ret = inv_mpu6050_sensor_show(st, st->reg->temperature,
713 					      IIO_MOD_X, val);
714 		break;
715 	case IIO_MAGN:
716 		if (!st->chip_config.magn_en) {
717 			result = inv_mpu6050_switch_engine(st, true,
718 					INV_MPU6050_SENSOR_MAGN);
719 			if (result)
720 				goto error_power_off;
721 			/* frequency is limited for magnetometer */
722 			if (freq_hz > INV_MPU_MAGN_FREQ_HZ_MAX) {
723 				freq_hz = INV_MPU_MAGN_FREQ_HZ_MAX;
724 				period_us = 1000000 / freq_hz;
725 			}
726 			/* need to wait 2 periods to have first valid sample */
727 			min_sleep_us = 2 * period_us;
728 			max_sleep_us = 2 * (period_us + period_us / 2);
729 			usleep_range(min_sleep_us, max_sleep_us);
730 		}
731 		ret = inv_mpu_magn_read(st, chan->channel2, val);
732 		break;
733 	default:
734 		ret = -EINVAL;
735 		break;
736 	}
737 
738 	pm_runtime_mark_last_busy(pdev);
739 	pm_runtime_put_autosuspend(pdev);
740 
741 	return ret;
742 
743 error_power_off:
744 	pm_runtime_put_autosuspend(pdev);
745 	return result;
746 }
747 
748 static int
inv_mpu6050_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)749 inv_mpu6050_read_raw(struct iio_dev *indio_dev,
750 		     struct iio_chan_spec const *chan,
751 		     int *val, int *val2, long mask)
752 {
753 	struct inv_mpu6050_state  *st = iio_priv(indio_dev);
754 	int ret = 0;
755 
756 	switch (mask) {
757 	case IIO_CHAN_INFO_RAW:
758 		if (!iio_device_claim_direct(indio_dev))
759 			return -EBUSY;
760 		mutex_lock(&st->lock);
761 		ret = inv_mpu6050_read_channel_data(indio_dev, chan, val);
762 		mutex_unlock(&st->lock);
763 		iio_device_release_direct(indio_dev);
764 		return ret;
765 	case IIO_CHAN_INFO_SCALE:
766 		switch (chan->type) {
767 		case IIO_ANGL_VEL:
768 			mutex_lock(&st->lock);
769 			*val  = 0;
770 			*val2 = gyro_scale_6050[st->chip_config.fsr];
771 			mutex_unlock(&st->lock);
772 
773 			return IIO_VAL_INT_PLUS_NANO;
774 		case IIO_ACCEL:
775 			mutex_lock(&st->lock);
776 			*val = 0;
777 			*val2 = accel_scale[st->chip_config.accl_fs];
778 			mutex_unlock(&st->lock);
779 
780 			return IIO_VAL_INT_PLUS_MICRO;
781 		case IIO_TEMP:
782 			*val = st->hw->temp.scale / 1000000;
783 			*val2 = st->hw->temp.scale % 1000000;
784 			return IIO_VAL_INT_PLUS_MICRO;
785 		case IIO_MAGN:
786 			return inv_mpu_magn_get_scale(st, chan, val, val2);
787 		default:
788 			return -EINVAL;
789 		}
790 	case IIO_CHAN_INFO_OFFSET:
791 		switch (chan->type) {
792 		case IIO_TEMP:
793 			*val = st->hw->temp.offset;
794 			return IIO_VAL_INT;
795 		default:
796 			return -EINVAL;
797 		}
798 	case IIO_CHAN_INFO_CALIBBIAS:
799 		switch (chan->type) {
800 		case IIO_ANGL_VEL:
801 			mutex_lock(&st->lock);
802 			ret = inv_mpu6050_sensor_show(st, st->reg->gyro_offset,
803 						chan->channel2, val);
804 			mutex_unlock(&st->lock);
805 			return ret;
806 		case IIO_ACCEL:
807 			mutex_lock(&st->lock);
808 			ret = inv_mpu6050_sensor_show(st, st->reg->accl_offset,
809 						chan->channel2, val);
810 			mutex_unlock(&st->lock);
811 			return ret;
812 
813 		default:
814 			return -EINVAL;
815 		}
816 	default:
817 		return -EINVAL;
818 	}
819 }
820 
inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state * st,int val,int val2)821 static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val,
822 					int val2)
823 {
824 	int result, i;
825 
826 	if (val != 0)
827 		return -EINVAL;
828 
829 	for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
830 		if (gyro_scale_6050[i] == val2) {
831 			result = inv_mpu6050_set_gyro_fsr(st, i);
832 			if (result)
833 				return result;
834 
835 			st->chip_config.fsr = i;
836 			return 0;
837 		}
838 	}
839 
840 	return -EINVAL;
841 }
842 
inv_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)843 static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
844 				 struct iio_chan_spec const *chan, long mask)
845 {
846 	switch (mask) {
847 	case IIO_CHAN_INFO_SCALE:
848 		switch (chan->type) {
849 		case IIO_ANGL_VEL:
850 			return IIO_VAL_INT_PLUS_NANO;
851 		default:
852 			return IIO_VAL_INT_PLUS_MICRO;
853 		}
854 	default:
855 		return IIO_VAL_INT_PLUS_MICRO;
856 	}
857 
858 	return -EINVAL;
859 }
860 
inv_mpu6050_write_accel_scale(struct inv_mpu6050_state * st,int val,int val2)861 static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val,
862 					 int val2)
863 {
864 	int result, i;
865 	u8 d;
866 
867 	if (val != 0)
868 		return -EINVAL;
869 
870 	for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
871 		if (accel_scale[i] == val2) {
872 			d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
873 			result = regmap_write(st->map, st->reg->accl_config, d);
874 			if (result)
875 				return result;
876 
877 			st->chip_config.accl_fs = i;
878 			return 0;
879 		}
880 	}
881 
882 	return -EINVAL;
883 }
884 
inv_mpu6050_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)885 static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
886 				 struct iio_chan_spec const *chan,
887 				 int val, int val2, long mask)
888 {
889 	struct inv_mpu6050_state  *st = iio_priv(indio_dev);
890 	struct device *pdev = regmap_get_device(st->map);
891 	int result;
892 
893 	/*
894 	 * we should only update scale when the chip is disabled, i.e.
895 	 * not running
896 	 */
897 	if (!iio_device_claim_direct(indio_dev))
898 		return -EBUSY;
899 
900 	mutex_lock(&st->lock);
901 	result = pm_runtime_resume_and_get(pdev);
902 	if (result)
903 		goto error_write_raw_unlock;
904 
905 	switch (mask) {
906 	case IIO_CHAN_INFO_SCALE:
907 		switch (chan->type) {
908 		case IIO_ANGL_VEL:
909 			result = inv_mpu6050_write_gyro_scale(st, val, val2);
910 			break;
911 		case IIO_ACCEL:
912 			result = inv_mpu6050_write_accel_scale(st, val, val2);
913 			break;
914 		default:
915 			result = -EINVAL;
916 			break;
917 		}
918 		break;
919 	case IIO_CHAN_INFO_CALIBBIAS:
920 		switch (chan->type) {
921 		case IIO_ANGL_VEL:
922 			result = inv_mpu6050_sensor_set(st,
923 							st->reg->gyro_offset,
924 							chan->channel2, val);
925 			break;
926 		case IIO_ACCEL:
927 			result = inv_mpu6050_sensor_set(st,
928 							st->reg->accl_offset,
929 							chan->channel2, val);
930 			break;
931 		default:
932 			result = -EINVAL;
933 			break;
934 		}
935 		break;
936 	default:
937 		result = -EINVAL;
938 		break;
939 	}
940 
941 	pm_runtime_mark_last_busy(pdev);
942 	pm_runtime_put_autosuspend(pdev);
943 error_write_raw_unlock:
944 	mutex_unlock(&st->lock);
945 	iio_device_release_direct(indio_dev);
946 
947 	return result;
948 }
949 
inv_mpu6050_convert_wom_to_roc(unsigned int threshold,unsigned int freq_div)950 static u64 inv_mpu6050_convert_wom_to_roc(unsigned int threshold, unsigned int freq_div)
951 {
952 	/* 4mg per LSB converted in m/s² in micro (1000000) */
953 	const unsigned int convert = 4U * 9807U;
954 	u64 value;
955 
956 	value = threshold * convert;
957 
958 	/* compute the differential by multiplying by the frequency */
959 	return div_u64(value * INV_MPU6050_INTERNAL_FREQ_HZ, freq_div);
960 }
961 
inv_mpu6050_convert_roc_to_wom(u64 roc,unsigned int freq_div)962 static unsigned int inv_mpu6050_convert_roc_to_wom(u64 roc, unsigned int freq_div)
963 {
964 	/* 4mg per LSB converted in m/s² in micro (1000000) */
965 	const unsigned int convert = 4U * 9807U;
966 	u64 value;
967 
968 	/* return 0 only if roc is 0 */
969 	if (roc == 0)
970 		return 0;
971 
972 	value = div_u64(roc * freq_div, convert * INV_MPU6050_INTERNAL_FREQ_HZ);
973 
974 	/* limit value to 8 bits and prevent 0 */
975 	return min(255, max(1, value));
976 }
977 
inv_mpu6050_set_wom_int(struct inv_mpu6050_state * st,bool on)978 static int inv_mpu6050_set_wom_int(struct inv_mpu6050_state *st, bool on)
979 {
980 	unsigned int reg_val, val;
981 
982 	switch (st->chip_type) {
983 	case INV_MPU6050:
984 	case INV_MPU6500:
985 	case INV_MPU6515:
986 	case INV_MPU6880:
987 	case INV_MPU6000:
988 	case INV_MPU9150:
989 	case INV_MPU9250:
990 	case INV_MPU9255:
991 		reg_val = INV_MPU6500_BIT_WOM_INT_EN;
992 		break;
993 	default:
994 		reg_val = INV_ICM20608_BIT_WOM_INT_EN;
995 		break;
996 	}
997 
998 	val = on ? reg_val : 0;
999 
1000 	return regmap_update_bits(st->map, st->reg->int_enable, reg_val, val);
1001 }
1002 
inv_mpu6050_set_wom_threshold(struct inv_mpu6050_state * st,u64 value,unsigned int freq_div)1003 static int inv_mpu6050_set_wom_threshold(struct inv_mpu6050_state *st, u64 value,
1004 					 unsigned int freq_div)
1005 {
1006 	unsigned int threshold;
1007 	int result;
1008 
1009 	/* convert roc to wom threshold and convert back to handle clipping */
1010 	threshold = inv_mpu6050_convert_roc_to_wom(value, freq_div);
1011 	value = inv_mpu6050_convert_wom_to_roc(threshold, freq_div);
1012 
1013 	dev_dbg(regmap_get_device(st->map), "wom_threshold: 0x%x\n", threshold);
1014 
1015 	switch (st->chip_type) {
1016 	case INV_ICM20609:
1017 	case INV_ICM20689:
1018 	case INV_ICM20600:
1019 	case INV_ICM20602:
1020 	case INV_ICM20690:
1021 		st->data[0] = threshold;
1022 		st->data[1] = threshold;
1023 		st->data[2] = threshold;
1024 		result = regmap_bulk_write(st->map, INV_ICM20609_REG_ACCEL_WOM_X_THR,
1025 					   st->data, 3);
1026 		break;
1027 	default:
1028 		result = regmap_write(st->map, INV_MPU6500_REG_WOM_THRESHOLD, threshold);
1029 		break;
1030 	}
1031 	if (result)
1032 		return result;
1033 
1034 	st->chip_config.roc_threshold = value;
1035 
1036 	return 0;
1037 }
1038 
inv_mpu6050_set_lp_odr(struct inv_mpu6050_state * st,unsigned int freq_div,unsigned int * lp_div)1039 static int inv_mpu6050_set_lp_odr(struct inv_mpu6050_state *st, unsigned int freq_div,
1040 				  unsigned int *lp_div)
1041 {
1042 	static const unsigned int freq_dividers[] = {2, 4, 8, 16, 32, 64, 128, 256};
1043 	static const unsigned int reg_values[] = {
1044 		INV_MPU6050_LPOSC_500HZ, INV_MPU6050_LPOSC_250HZ,
1045 		INV_MPU6050_LPOSC_125HZ, INV_MPU6050_LPOSC_62HZ,
1046 		INV_MPU6050_LPOSC_31HZ, INV_MPU6050_LPOSC_16HZ,
1047 		INV_MPU6050_LPOSC_8HZ, INV_MPU6050_LPOSC_4HZ,
1048 	};
1049 	unsigned int val, i;
1050 
1051 	switch (st->chip_type) {
1052 	case INV_ICM20609:
1053 	case INV_ICM20689:
1054 	case INV_ICM20600:
1055 	case INV_ICM20602:
1056 	case INV_ICM20690:
1057 		/* nothing to do */
1058 		*lp_div = INV_MPU6050_FREQ_DIVIDER(st);
1059 		return 0;
1060 	default:
1061 		break;
1062 	}
1063 
1064 	/* found the nearest superior frequency divider */
1065 	i = ARRAY_SIZE(reg_values) - 1;
1066 	val = reg_values[i];
1067 	*lp_div = freq_dividers[i];
1068 	for (i = 0; i < ARRAY_SIZE(freq_dividers); ++i) {
1069 		if (freq_div <= freq_dividers[i]) {
1070 			val = reg_values[i];
1071 			*lp_div = freq_dividers[i];
1072 			break;
1073 		}
1074 	}
1075 
1076 	dev_dbg(regmap_get_device(st->map), "lp_odr: 0x%x\n", val);
1077 	return regmap_write(st->map, INV_MPU6500_REG_LP_ODR, val);
1078 }
1079 
inv_mpu6050_set_wom_lp(struct inv_mpu6050_state * st,bool on)1080 static int inv_mpu6050_set_wom_lp(struct inv_mpu6050_state *st, bool on)
1081 {
1082 	unsigned int lp_div;
1083 	int result;
1084 
1085 	if (on) {
1086 		/* set low power ODR */
1087 		result = inv_mpu6050_set_lp_odr(st, INV_MPU6050_FREQ_DIVIDER(st), &lp_div);
1088 		if (result)
1089 			return result;
1090 		/* disable accel low pass filter */
1091 		result = inv_mpu6050_set_accel_lpf_regs(st, INV_MPU6050_FILTER_NOLPF);
1092 		if (result)
1093 			return result;
1094 		/* update wom threshold with new low-power frequency divider */
1095 		result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold, lp_div);
1096 		if (result)
1097 			return result;
1098 		/* set cycle mode */
1099 		result = inv_mpu6050_pwr_mgmt_1_write(st, false, true, -1, -1);
1100 	} else {
1101 		/* disable cycle mode */
1102 		result = inv_mpu6050_pwr_mgmt_1_write(st, false, false, -1, -1);
1103 		if (result)
1104 			return result;
1105 		/* restore wom threshold */
1106 		result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold,
1107 						       INV_MPU6050_FREQ_DIVIDER(st));
1108 		if (result)
1109 			return result;
1110 		/* restore accel low pass filter */
1111 		result = inv_mpu6050_set_accel_lpf_regs(st, st->chip_config.lpf);
1112 	}
1113 
1114 	return result;
1115 }
1116 
inv_mpu6050_enable_wom(struct inv_mpu6050_state * st,bool en)1117 static int inv_mpu6050_enable_wom(struct inv_mpu6050_state *st, bool en)
1118 {
1119 	struct device *pdev = regmap_get_device(st->map);
1120 	unsigned int mask;
1121 	int result;
1122 
1123 	if (en) {
1124 		result = pm_runtime_resume_and_get(pdev);
1125 		if (result)
1126 			return result;
1127 
1128 		mask = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_WOM;
1129 		result = inv_mpu6050_switch_engine(st, true, mask);
1130 		if (result)
1131 			goto error_suspend;
1132 
1133 		result = inv_mpu6050_set_wom_int(st, true);
1134 		if (result)
1135 			goto error_suspend;
1136 	} else {
1137 		result = inv_mpu6050_set_wom_int(st, false);
1138 		if (result)
1139 			dev_err(pdev, "error %d disabling WoM interrupt bit", result);
1140 
1141 		/* disable only WoM and let accel be disabled by autosuspend */
1142 		result = inv_mpu6050_switch_engine(st, false, INV_MPU6050_SENSOR_WOM);
1143 		if (result) {
1144 			dev_err(pdev, "error %d disabling WoM force off", result);
1145 			/* force WoM off */
1146 			st->chip_config.wom_en = false;
1147 		}
1148 
1149 		pm_runtime_mark_last_busy(pdev);
1150 		pm_runtime_put_autosuspend(pdev);
1151 	}
1152 
1153 	return result;
1154 
1155 error_suspend:
1156 	pm_runtime_mark_last_busy(pdev);
1157 	pm_runtime_put_autosuspend(pdev);
1158 	return result;
1159 }
1160 
inv_mpu6050_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)1161 static int inv_mpu6050_read_event_config(struct iio_dev *indio_dev,
1162 					 const struct iio_chan_spec *chan,
1163 					 enum iio_event_type type,
1164 					 enum iio_event_direction dir)
1165 {
1166 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1167 
1168 	/* support only WoM (accel roc rising) event */
1169 	if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC ||
1170 	    dir != IIO_EV_DIR_RISING)
1171 		return -EINVAL;
1172 
1173 	guard(mutex)(&st->lock);
1174 
1175 	return st->chip_config.wom_en ? 1 : 0;
1176 }
1177 
inv_mpu6050_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)1178 static int inv_mpu6050_write_event_config(struct iio_dev *indio_dev,
1179 					  const struct iio_chan_spec *chan,
1180 					  enum iio_event_type type,
1181 					  enum iio_event_direction dir,
1182 					  bool state)
1183 {
1184 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1185 
1186 	/* support only WoM (accel roc rising) event */
1187 	if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC ||
1188 	    dir != IIO_EV_DIR_RISING)
1189 		return -EINVAL;
1190 
1191 	guard(mutex)(&st->lock);
1192 
1193 	if (st->chip_config.wom_en == state)
1194 		return 0;
1195 
1196 	return inv_mpu6050_enable_wom(st, state);
1197 }
1198 
inv_mpu6050_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)1199 static int inv_mpu6050_read_event_value(struct iio_dev *indio_dev,
1200 					const struct iio_chan_spec *chan,
1201 					enum iio_event_type type,
1202 					enum iio_event_direction dir,
1203 					enum iio_event_info info,
1204 					int *val, int *val2)
1205 {
1206 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1207 	u32 rem;
1208 
1209 	/* support only WoM (accel roc rising) event value */
1210 	if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC ||
1211 	    dir != IIO_EV_DIR_RISING || info != IIO_EV_INFO_VALUE)
1212 		return -EINVAL;
1213 
1214 	guard(mutex)(&st->lock);
1215 
1216 	/* return value in micro */
1217 	*val = div_u64_rem(st->chip_config.roc_threshold, 1000000U, &rem);
1218 	*val2 = rem;
1219 
1220 	return IIO_VAL_INT_PLUS_MICRO;
1221 }
1222 
inv_mpu6050_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)1223 static int inv_mpu6050_write_event_value(struct iio_dev *indio_dev,
1224 					 const struct iio_chan_spec *chan,
1225 					 enum iio_event_type type,
1226 					 enum iio_event_direction dir,
1227 					 enum iio_event_info info,
1228 					 int val, int val2)
1229 {
1230 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1231 	struct device *pdev = regmap_get_device(st->map);
1232 	u64 value;
1233 	int result;
1234 
1235 	/* support only WoM (accel roc rising) event value */
1236 	if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC ||
1237 	    dir != IIO_EV_DIR_RISING || info != IIO_EV_INFO_VALUE)
1238 		return -EINVAL;
1239 
1240 	if (val < 0 || val2 < 0)
1241 		return -EINVAL;
1242 
1243 	guard(mutex)(&st->lock);
1244 
1245 	result = pm_runtime_resume_and_get(pdev);
1246 	if (result)
1247 		return result;
1248 
1249 	value = (u64)val * 1000000ULL + (u64)val2;
1250 	result = inv_mpu6050_set_wom_threshold(st, value, INV_MPU6050_FREQ_DIVIDER(st));
1251 
1252 	pm_runtime_mark_last_busy(pdev);
1253 	pm_runtime_put_autosuspend(pdev);
1254 
1255 	return result;
1256 }
1257 
1258 /*
1259  *  inv_mpu6050_set_lpf() - set low pass filer based on fifo rate.
1260  *
1261  *                  Based on the Nyquist principle, the bandwidth of the low
1262  *                  pass filter must not exceed the signal sampling rate divided
1263  *                  by 2, or there would be aliasing.
1264  *                  This function basically search for the correct low pass
1265  *                  parameters based on the fifo rate, e.g, sampling frequency.
1266  *
1267  *  lpf is set automatically when setting sampling rate to avoid any aliases.
1268  */
inv_mpu6050_set_lpf(struct inv_mpu6050_state * st,int rate)1269 static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
1270 {
1271 	static const int hz[] = {400, 200, 90, 40, 20, 10};
1272 	static const int d[] = {
1273 		INV_MPU6050_FILTER_200HZ, INV_MPU6050_FILTER_100HZ,
1274 		INV_MPU6050_FILTER_45HZ, INV_MPU6050_FILTER_20HZ,
1275 		INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ
1276 	};
1277 	int i, result;
1278 	u8 data;
1279 
1280 	data = INV_MPU6050_FILTER_5HZ;
1281 	for (i = 0; i < ARRAY_SIZE(hz); ++i) {
1282 		if (rate >= hz[i]) {
1283 			data = d[i];
1284 			break;
1285 		}
1286 	}
1287 	result = inv_mpu6050_set_lpf_regs(st, data);
1288 	if (result)
1289 		return result;
1290 	st->chip_config.lpf = data;
1291 
1292 	return 0;
1293 }
1294 
1295 /*
1296  * inv_mpu6050_fifo_rate_store() - Set fifo rate.
1297  */
1298 static ssize_t
inv_mpu6050_fifo_rate_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1299 inv_mpu6050_fifo_rate_store(struct device *dev, struct device_attribute *attr,
1300 			    const char *buf, size_t count)
1301 {
1302 	int fifo_rate;
1303 	u32 fifo_period;
1304 	bool fifo_on;
1305 	u8 d;
1306 	int result;
1307 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1308 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1309 	struct device *pdev = regmap_get_device(st->map);
1310 
1311 	if (kstrtoint(buf, 10, &fifo_rate))
1312 		return -EINVAL;
1313 	if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
1314 	    fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
1315 		return -EINVAL;
1316 
1317 	/* compute the chip sample rate divider */
1318 	d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(fifo_rate);
1319 	/* compute back the fifo rate to handle truncation cases */
1320 	fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(d);
1321 	fifo_period = NSEC_PER_SEC / fifo_rate;
1322 
1323 	mutex_lock(&st->lock);
1324 	if (d == st->chip_config.divider) {
1325 		result = 0;
1326 		goto fifo_rate_fail_unlock;
1327 	}
1328 
1329 	fifo_on = st->chip_config.accl_fifo_enable ||
1330 		  st->chip_config.gyro_fifo_enable ||
1331 		  st->chip_config.magn_fifo_enable;
1332 	result = inv_sensors_timestamp_update_odr(&st->timestamp, fifo_period, fifo_on);
1333 	if (result)
1334 		goto fifo_rate_fail_unlock;
1335 
1336 	result = pm_runtime_resume_and_get(pdev);
1337 	if (result)
1338 		goto fifo_rate_fail_unlock;
1339 
1340 	result = regmap_write(st->map, st->reg->sample_rate_div, d);
1341 	if (result)
1342 		goto fifo_rate_fail_power_off;
1343 	st->chip_config.divider = d;
1344 
1345 	result = inv_mpu6050_set_lpf(st, fifo_rate);
1346 	if (result)
1347 		goto fifo_rate_fail_power_off;
1348 
1349 	/* update rate for magn, noop if not present in chip */
1350 	result = inv_mpu_magn_set_rate(st, fifo_rate);
1351 	if (result)
1352 		goto fifo_rate_fail_power_off;
1353 
1354 	/* update wom threshold since roc is dependent on sampling frequency */
1355 	result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold,
1356 					       INV_MPU6050_FREQ_DIVIDER(st));
1357 	if (result)
1358 		goto fifo_rate_fail_power_off;
1359 
1360 	pm_runtime_mark_last_busy(pdev);
1361 fifo_rate_fail_power_off:
1362 	pm_runtime_put_autosuspend(pdev);
1363 fifo_rate_fail_unlock:
1364 	mutex_unlock(&st->lock);
1365 	if (result)
1366 		return result;
1367 
1368 	return count;
1369 }
1370 
1371 /*
1372  * inv_fifo_rate_show() - Get the current sampling rate.
1373  */
1374 static ssize_t
inv_fifo_rate_show(struct device * dev,struct device_attribute * attr,char * buf)1375 inv_fifo_rate_show(struct device *dev, struct device_attribute *attr,
1376 		   char *buf)
1377 {
1378 	struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
1379 	unsigned fifo_rate;
1380 
1381 	mutex_lock(&st->lock);
1382 	fifo_rate = INV_MPU6050_DIVIDER_TO_FIFO_RATE(st->chip_config.divider);
1383 	mutex_unlock(&st->lock);
1384 
1385 	return scnprintf(buf, PAGE_SIZE, "%u\n", fifo_rate);
1386 }
1387 
1388 /*
1389  * inv_attr_show() - calling this function will show current
1390  *                    parameters.
1391  *
1392  * Deprecated in favor of IIO mounting matrix API.
1393  *
1394  * See inv_get_mount_matrix()
1395  */
inv_attr_show(struct device * dev,struct device_attribute * attr,char * buf)1396 static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr,
1397 			     char *buf)
1398 {
1399 	struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
1400 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1401 	s8 *m;
1402 
1403 	switch (this_attr->address) {
1404 	/*
1405 	 * In MPU6050, the two matrix are the same because gyro and accel
1406 	 * are integrated in one chip
1407 	 */
1408 	case ATTR_GYRO_MATRIX:
1409 	case ATTR_ACCL_MATRIX:
1410 		m = st->plat_data.orientation;
1411 
1412 		return scnprintf(buf, PAGE_SIZE,
1413 			"%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
1414 			m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1415 	default:
1416 		return -EINVAL;
1417 	}
1418 }
1419 
1420 /**
1421  * inv_mpu6050_validate_trigger() - validate_trigger callback for invensense
1422  *                                  MPU6050 device.
1423  * @indio_dev: The IIO device
1424  * @trig: The new trigger
1425  *
1426  * Returns: 0 if the 'trig' matches the trigger registered by the MPU6050
1427  * device, -EINVAL otherwise.
1428  */
inv_mpu6050_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)1429 static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
1430 					struct iio_trigger *trig)
1431 {
1432 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1433 
1434 	if (st->trig != trig)
1435 		return -EINVAL;
1436 
1437 	return 0;
1438 }
1439 
1440 static const struct iio_mount_matrix *
inv_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)1441 inv_get_mount_matrix(const struct iio_dev *indio_dev,
1442 		     const struct iio_chan_spec *chan)
1443 {
1444 	struct inv_mpu6050_state *data = iio_priv(indio_dev);
1445 	const struct iio_mount_matrix *matrix;
1446 
1447 	if (chan->type == IIO_MAGN)
1448 		matrix = &data->magn_orient;
1449 	else
1450 		matrix = &data->orientation;
1451 
1452 	return matrix;
1453 }
1454 
1455 static const struct iio_chan_spec_ext_info inv_ext_info[] = {
1456 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix),
1457 	{ }
1458 };
1459 
1460 static const struct iio_event_spec inv_wom_events[] = {
1461 	{
1462 		.type = IIO_EV_TYPE_ROC,
1463 		.dir = IIO_EV_DIR_RISING,
1464 		.mask_separate = BIT(IIO_EV_INFO_ENABLE) |
1465 				 BIT(IIO_EV_INFO_VALUE),
1466 	},
1467 };
1468 
1469 #define INV_MPU6050_CHAN(_type, _channel2, _index)                    \
1470 	{                                                             \
1471 		.type = _type,                                        \
1472 		.modified = 1,                                        \
1473 		.channel2 = _channel2,                                \
1474 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1475 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	      \
1476 				      BIT(IIO_CHAN_INFO_CALIBBIAS),   \
1477 		.scan_index = _index,                                 \
1478 		.scan_type = {                                        \
1479 				.sign = 's',                          \
1480 				.realbits = 16,                       \
1481 				.storagebits = 16,                    \
1482 				.shift = 0,                           \
1483 				.endianness = IIO_BE,                 \
1484 			     },                                       \
1485 		.ext_info = inv_ext_info,                             \
1486 	}
1487 
1488 #define INV_MPU6050_TEMP_CHAN(_index)				\
1489 	{							\
1490 		.type = IIO_TEMP,				\
1491 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)	\
1492 				| BIT(IIO_CHAN_INFO_OFFSET)	\
1493 				| BIT(IIO_CHAN_INFO_SCALE),	\
1494 		.scan_index = _index,				\
1495 		.scan_type = {					\
1496 			.sign = 's',				\
1497 			.realbits = 16,				\
1498 			.storagebits = 16,			\
1499 			.shift = 0,				\
1500 			.endianness = IIO_BE,			\
1501 		},						\
1502 	}
1503 
1504 #define INV_MPU6050_EVENT_CHAN(_type, _channel2, _events, _events_nb)	\
1505 {									\
1506 	.type = _type,							\
1507 	.modified = 1,							\
1508 	.channel2 = _channel2,						\
1509 	.event_spec = _events,						\
1510 	.num_event_specs = _events_nb,					\
1511 	.scan_index = -1,						\
1512 }
1513 
1514 static const struct iio_chan_spec inv_mpu6050_channels[] = {
1515 	IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
1516 
1517 	INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1518 
1519 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1520 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1521 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1522 
1523 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1524 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1525 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1526 };
1527 
1528 static const struct iio_chan_spec inv_iam20380_channels[] = {
1529 	IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
1530 
1531 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1532 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1533 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1534 };
1535 
1536 static const struct iio_chan_spec inv_mpu6500_channels[] = {
1537 	IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
1538 
1539 	INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1540 
1541 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1542 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1543 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1544 
1545 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1546 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1547 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1548 
1549 	INV_MPU6050_EVENT_CHAN(IIO_ACCEL, IIO_MOD_X_OR_Y_OR_Z,
1550 			       inv_wom_events, ARRAY_SIZE(inv_wom_events)),
1551 };
1552 
1553 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL	\
1554 	(BIT(INV_MPU6050_SCAN_ACCL_X)		\
1555 	| BIT(INV_MPU6050_SCAN_ACCL_Y)		\
1556 	| BIT(INV_MPU6050_SCAN_ACCL_Z))
1557 
1558 #define INV_MPU6050_SCAN_MASK_3AXIS_GYRO	\
1559 	(BIT(INV_MPU6050_SCAN_GYRO_X)		\
1560 	| BIT(INV_MPU6050_SCAN_GYRO_Y)		\
1561 	| BIT(INV_MPU6050_SCAN_GYRO_Z))
1562 
1563 #define INV_MPU6050_SCAN_MASK_TEMP		(BIT(INV_MPU6050_SCAN_TEMP))
1564 
1565 static const unsigned long inv_mpu_scan_masks[] = {
1566 	/* 3-axis accel */
1567 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1568 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1569 	/* 3-axis gyro */
1570 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1571 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1572 	/* 6-axis accel + gyro */
1573 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1574 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1575 		| INV_MPU6050_SCAN_MASK_TEMP,
1576 	0,
1577 };
1578 
1579 #define INV_MPU9X50_MAGN_CHAN(_chan2, _bits, _index)			\
1580 	{								\
1581 		.type = IIO_MAGN,					\
1582 		.modified = 1,						\
1583 		.channel2 = _chan2,					\
1584 		.info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) |	\
1585 				      BIT(IIO_CHAN_INFO_RAW),		\
1586 		.scan_index = _index,					\
1587 		.scan_type = {						\
1588 			.sign = 's',					\
1589 			.realbits = _bits,				\
1590 			.storagebits = 16,				\
1591 			.shift = 0,					\
1592 			.endianness = IIO_BE,				\
1593 		},							\
1594 		.ext_info = inv_ext_info,				\
1595 	}
1596 
1597 static const struct iio_chan_spec inv_mpu9150_channels[] = {
1598 	IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1599 
1600 	INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1601 
1602 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1603 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1604 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1605 
1606 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1607 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1608 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1609 
1610 	/* Magnetometer resolution is 13 bits */
1611 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 13, INV_MPU9X50_SCAN_MAGN_X),
1612 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 13, INV_MPU9X50_SCAN_MAGN_Y),
1613 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 13, INV_MPU9X50_SCAN_MAGN_Z),
1614 };
1615 
1616 static const struct iio_chan_spec inv_mpu9250_channels[] = {
1617 	IIO_CHAN_SOFT_TIMESTAMP(INV_MPU9X50_SCAN_TIMESTAMP),
1618 
1619 	INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP),
1620 
1621 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
1622 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
1623 	INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
1624 
1625 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
1626 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
1627 	INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
1628 
1629 	/* Magnetometer resolution is 16 bits */
1630 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_X, 16, INV_MPU9X50_SCAN_MAGN_X),
1631 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_Y, 16, INV_MPU9X50_SCAN_MAGN_Y),
1632 	INV_MPU9X50_MAGN_CHAN(IIO_MOD_Z, 16, INV_MPU9X50_SCAN_MAGN_Z),
1633 };
1634 
1635 #define INV_MPU9X50_SCAN_MASK_3AXIS_MAGN	\
1636 	(BIT(INV_MPU9X50_SCAN_MAGN_X)		\
1637 	| BIT(INV_MPU9X50_SCAN_MAGN_Y)		\
1638 	| BIT(INV_MPU9X50_SCAN_MAGN_Z))
1639 
1640 static const unsigned long inv_iam20380_scan_masks[] = {
1641 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1642 };
1643 
1644 static const unsigned long inv_mpu9x50_scan_masks[] = {
1645 	/* 3-axis accel */
1646 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL,
1647 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1648 	/* 3-axis gyro */
1649 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1650 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1651 	/* 3-axis magn */
1652 	INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1653 	INV_MPU9X50_SCAN_MASK_3AXIS_MAGN | INV_MPU6050_SCAN_MASK_TEMP,
1654 	/* 6-axis accel + gyro */
1655 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO,
1656 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1657 		| INV_MPU6050_SCAN_MASK_TEMP,
1658 	/* 6-axis accel + magn */
1659 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1660 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1661 		| INV_MPU6050_SCAN_MASK_TEMP,
1662 	/* 6-axis gyro + magn */
1663 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1664 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1665 		| INV_MPU6050_SCAN_MASK_TEMP,
1666 	/* 9-axis accel + gyro + magn */
1667 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1668 		| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN,
1669 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1670 		| INV_MPU9X50_SCAN_MASK_3AXIS_MAGN
1671 		| INV_MPU6050_SCAN_MASK_TEMP,
1672 	0,
1673 };
1674 
1675 static const unsigned long inv_icm20602_scan_masks[] = {
1676 	/* 3-axis accel + temp (mandatory) */
1677 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_TEMP,
1678 	/* 3-axis gyro + temp (mandatory) */
1679 	INV_MPU6050_SCAN_MASK_3AXIS_GYRO | INV_MPU6050_SCAN_MASK_TEMP,
1680 	/* 6-axis accel + gyro + temp (mandatory) */
1681 	INV_MPU6050_SCAN_MASK_3AXIS_ACCEL | INV_MPU6050_SCAN_MASK_3AXIS_GYRO
1682 		| INV_MPU6050_SCAN_MASK_TEMP,
1683 	0,
1684 };
1685 
1686 /*
1687  * The user can choose any frequency between INV_MPU6050_MIN_FIFO_RATE and
1688  * INV_MPU6050_MAX_FIFO_RATE, but only these frequencies are matched by the
1689  * low-pass filter. Specifically, each of these sampling rates are about twice
1690  * the bandwidth of a corresponding low-pass filter, which should eliminate
1691  * aliasing following the Nyquist principle. By picking a frequency different
1692  * from these, the user risks aliasing effects.
1693  */
1694 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
1695 static IIO_CONST_ATTR(in_anglvel_scale_available,
1696 					  "0.000133090 0.000266181 0.000532362 0.001064724");
1697 static IIO_CONST_ATTR(in_accel_scale_available,
1698 					  "0.000598 0.001196 0.002392 0.004785");
1699 static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
1700 	inv_mpu6050_fifo_rate_store);
1701 
1702 /* Deprecated: kept for userspace backward compatibility. */
1703 static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
1704 	ATTR_GYRO_MATRIX);
1705 static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
1706 	ATTR_ACCL_MATRIX);
1707 
1708 static struct attribute *inv_attributes[] = {
1709 	&iio_dev_attr_in_gyro_matrix.dev_attr.attr,  /* deprecated */
1710 	&iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */
1711 	&iio_dev_attr_sampling_frequency.dev_attr.attr,
1712 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
1713 	&iio_const_attr_in_accel_scale_available.dev_attr.attr,
1714 	&iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
1715 	NULL,
1716 };
1717 
1718 static const struct attribute_group inv_attribute_group = {
1719 	.attrs = inv_attributes
1720 };
1721 
inv_mpu6050_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)1722 static int inv_mpu6050_reg_access(struct iio_dev *indio_dev,
1723 				  unsigned int reg,
1724 				  unsigned int writeval,
1725 				  unsigned int *readval)
1726 {
1727 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
1728 	int ret;
1729 
1730 	mutex_lock(&st->lock);
1731 	if (readval)
1732 		ret = regmap_read(st->map, reg, readval);
1733 	else
1734 		ret = regmap_write(st->map, reg, writeval);
1735 	mutex_unlock(&st->lock);
1736 
1737 	return ret;
1738 }
1739 
1740 static const struct iio_info mpu_info = {
1741 	.read_raw = &inv_mpu6050_read_raw,
1742 	.write_raw = &inv_mpu6050_write_raw,
1743 	.write_raw_get_fmt = &inv_write_raw_get_fmt,
1744 	.attrs = &inv_attribute_group,
1745 	.read_event_config = inv_mpu6050_read_event_config,
1746 	.write_event_config = inv_mpu6050_write_event_config,
1747 	.read_event_value = inv_mpu6050_read_event_value,
1748 	.write_event_value = inv_mpu6050_write_event_value,
1749 	.validate_trigger = inv_mpu6050_validate_trigger,
1750 	.debugfs_reg_access = &inv_mpu6050_reg_access,
1751 };
1752 
1753 /*
1754  *  inv_check_and_setup_chip() - check and setup chip.
1755  */
inv_check_and_setup_chip(struct inv_mpu6050_state * st)1756 static int inv_check_and_setup_chip(struct inv_mpu6050_state *st)
1757 {
1758 	int result;
1759 	unsigned int regval, mask;
1760 	int i;
1761 
1762 	st->hw  = &hw_info[st->chip_type];
1763 	st->reg = hw_info[st->chip_type].reg;
1764 	memcpy(&st->chip_config, hw_info[st->chip_type].config,
1765 	       sizeof(st->chip_config));
1766 	st->data = devm_kzalloc(regmap_get_device(st->map), st->hw->fifo_size, GFP_KERNEL);
1767 	if (st->data == NULL)
1768 		return -ENOMEM;
1769 
1770 	/* check chip self-identification */
1771 	result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, &regval);
1772 	if (result)
1773 		return result;
1774 	if (regval != st->hw->whoami) {
1775 		/* check whoami against all possible values */
1776 		for (i = 0; i < INV_NUM_PARTS; ++i) {
1777 			if (regval == hw_info[i].whoami) {
1778 				dev_warn(regmap_get_device(st->map),
1779 					"whoami mismatch got 0x%02x (%s) expected 0x%02x (%s)\n",
1780 					regval, hw_info[i].name,
1781 					st->hw->whoami, st->hw->name);
1782 				break;
1783 			}
1784 		}
1785 		if (i >= INV_NUM_PARTS) {
1786 			dev_err(regmap_get_device(st->map),
1787 				"invalid whoami 0x%02x expected 0x%02x (%s)\n",
1788 				regval, st->hw->whoami, st->hw->name);
1789 			return -ENODEV;
1790 		}
1791 	}
1792 
1793 	/* reset to make sure previous state are not there */
1794 	result = regmap_write(st->map, st->reg->pwr_mgmt_1,
1795 			      INV_MPU6050_BIT_H_RESET);
1796 	if (result)
1797 		return result;
1798 	msleep(INV_MPU6050_POWER_UP_TIME);
1799 	switch (st->chip_type) {
1800 	case INV_MPU6000:
1801 	case INV_MPU6500:
1802 	case INV_MPU6515:
1803 	case INV_MPU6880:
1804 	case INV_MPU9250:
1805 	case INV_MPU9255:
1806 		/* reset signal path (required for spi connection) */
1807 		regval = INV_MPU6050_BIT_TEMP_RST | INV_MPU6050_BIT_ACCEL_RST |
1808 			 INV_MPU6050_BIT_GYRO_RST;
1809 		result = regmap_write(st->map, INV_MPU6050_REG_SIGNAL_PATH_RESET,
1810 				      regval);
1811 		if (result)
1812 			return result;
1813 		msleep(INV_MPU6050_POWER_UP_TIME);
1814 		break;
1815 	default:
1816 		break;
1817 	}
1818 
1819 	/*
1820 	 * Turn power on. After reset, the sleep bit could be on
1821 	 * or off depending on the OTP settings. Turning power on
1822 	 * make it in a definite state as well as making the hardware
1823 	 * state align with the software state
1824 	 */
1825 	result = inv_mpu6050_set_power_itg(st, true);
1826 	if (result)
1827 		return result;
1828 	mask = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
1829 			INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN;
1830 	result = inv_mpu6050_switch_engine(st, false, mask);
1831 	if (result)
1832 		goto error_power_off;
1833 
1834 	return 0;
1835 
1836 error_power_off:
1837 	inv_mpu6050_set_power_itg(st, false);
1838 	return result;
1839 }
1840 
inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state * st)1841 static int inv_mpu_core_enable_regulator_vddio(struct inv_mpu6050_state *st)
1842 {
1843 	int result;
1844 
1845 	result = regulator_enable(st->vddio_supply);
1846 	if (result) {
1847 		dev_err(regmap_get_device(st->map),
1848 			"Failed to enable vddio regulator: %d\n", result);
1849 	} else {
1850 		/* Give the device a little bit of time to start up. */
1851 		usleep_range(3000, 5000);
1852 	}
1853 
1854 	return result;
1855 }
1856 
inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state * st)1857 static int inv_mpu_core_disable_regulator_vddio(struct inv_mpu6050_state *st)
1858 {
1859 	int result;
1860 
1861 	result = regulator_disable(st->vddio_supply);
1862 	if (result)
1863 		dev_err(regmap_get_device(st->map),
1864 			"Failed to disable vddio regulator: %d\n", result);
1865 
1866 	return result;
1867 }
1868 
inv_mpu_core_disable_regulator_action(void * _data)1869 static void inv_mpu_core_disable_regulator_action(void *_data)
1870 {
1871 	struct inv_mpu6050_state *st = _data;
1872 	int result;
1873 
1874 	result = regulator_disable(st->vdd_supply);
1875 	if (result)
1876 		dev_err(regmap_get_device(st->map),
1877 			"Failed to disable vdd regulator: %d\n", result);
1878 
1879 	inv_mpu_core_disable_regulator_vddio(st);
1880 }
1881 
inv_mpu_pm_disable(void * data)1882 static void inv_mpu_pm_disable(void *data)
1883 {
1884 	struct device *dev = data;
1885 
1886 	pm_runtime_disable(dev);
1887 }
1888 
inv_mpu_core_probe(struct regmap * regmap,int irq,const char * name,int (* inv_mpu_bus_setup)(struct iio_dev *),int chip_type)1889 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
1890 		int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
1891 {
1892 	struct inv_mpu6050_state *st;
1893 	struct iio_dev *indio_dev;
1894 	struct inv_mpu6050_platform_data *pdata;
1895 	struct device *dev = regmap_get_device(regmap);
1896 	int result;
1897 	int irq_type;
1898 
1899 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1900 	if (!indio_dev)
1901 		return -ENOMEM;
1902 
1903 	BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS);
1904 	if (chip_type < 0 || chip_type >= INV_NUM_PARTS) {
1905 		dev_err(dev, "Bad invensense chip_type=%d name=%s\n",
1906 				chip_type, name);
1907 		return -ENODEV;
1908 	}
1909 	st = iio_priv(indio_dev);
1910 	mutex_init(&st->lock);
1911 	st->chip_type = chip_type;
1912 	st->irq = irq;
1913 	st->map = regmap;
1914 
1915 	st->level_shifter = device_property_read_bool(dev,
1916 						      "invensense,level-shifter");
1917 	pdata = dev_get_platdata(dev);
1918 	if (!pdata) {
1919 		result = iio_read_mount_matrix(dev, &st->orientation);
1920 		if (result) {
1921 			dev_err(dev, "Failed to retrieve mounting matrix %d\n",
1922 				result);
1923 			return result;
1924 		}
1925 	} else {
1926 		st->plat_data = *pdata;
1927 	}
1928 
1929 	if (irq > 0) {
1930 		irq_type = irq_get_trigger_type(irq);
1931 		if (!irq_type)
1932 			irq_type = IRQF_TRIGGER_RISING;
1933 	} else {
1934 		/* Doesn't really matter, use the default */
1935 		irq_type = IRQF_TRIGGER_RISING;
1936 	}
1937 
1938 	if (irq_type & IRQF_TRIGGER_RISING)	// rising or both-edge
1939 		st->irq_mask = INV_MPU6050_ACTIVE_HIGH;
1940 	else if (irq_type == IRQF_TRIGGER_FALLING)
1941 		st->irq_mask = INV_MPU6050_ACTIVE_LOW;
1942 	else if (irq_type == IRQF_TRIGGER_HIGH)
1943 		st->irq_mask = INV_MPU6050_ACTIVE_HIGH |
1944 			INV_MPU6050_LATCH_INT_EN;
1945 	else if (irq_type == IRQF_TRIGGER_LOW)
1946 		st->irq_mask = INV_MPU6050_ACTIVE_LOW |
1947 			INV_MPU6050_LATCH_INT_EN;
1948 	else {
1949 		dev_err(dev, "Invalid interrupt type 0x%x specified\n",
1950 			irq_type);
1951 		return -EINVAL;
1952 	}
1953 	device_set_wakeup_capable(dev, true);
1954 
1955 	st->vdd_supply = devm_regulator_get(dev, "vdd");
1956 	if (IS_ERR(st->vdd_supply))
1957 		return dev_err_probe(dev, PTR_ERR(st->vdd_supply),
1958 				     "Failed to get vdd regulator\n");
1959 
1960 	st->vddio_supply = devm_regulator_get(dev, "vddio");
1961 	if (IS_ERR(st->vddio_supply))
1962 		return dev_err_probe(dev, PTR_ERR(st->vddio_supply),
1963 				     "Failed to get vddio regulator\n");
1964 
1965 	result = regulator_enable(st->vdd_supply);
1966 	if (result) {
1967 		dev_err(dev, "Failed to enable vdd regulator: %d\n", result);
1968 		return result;
1969 	}
1970 	msleep(INV_MPU6050_POWER_UP_TIME);
1971 
1972 	result = inv_mpu_core_enable_regulator_vddio(st);
1973 	if (result) {
1974 		regulator_disable(st->vdd_supply);
1975 		return result;
1976 	}
1977 
1978 	result = devm_add_action_or_reset(dev, inv_mpu_core_disable_regulator_action,
1979 				 st);
1980 	if (result) {
1981 		dev_err(dev, "Failed to setup regulator cleanup action %d\n",
1982 			result);
1983 		return result;
1984 	}
1985 
1986 	/* fill magnetometer orientation */
1987 	result = inv_mpu_magn_set_orient(st);
1988 	if (result)
1989 		return result;
1990 
1991 	/* power is turned on inside check chip type*/
1992 	result = inv_check_and_setup_chip(st);
1993 	if (result)
1994 		return result;
1995 
1996 	result = inv_mpu6050_init_config(indio_dev);
1997 	if (result) {
1998 		dev_err(dev, "Could not initialize device.\n");
1999 		goto error_power_off;
2000 	}
2001 
2002 	dev_set_drvdata(dev, indio_dev);
2003 	/* name will be NULL when enumerated via ACPI */
2004 	if (name)
2005 		indio_dev->name = name;
2006 	else
2007 		indio_dev->name = dev_name(dev);
2008 
2009 	/* requires parent device set in indio_dev */
2010 	if (inv_mpu_bus_setup) {
2011 		result = inv_mpu_bus_setup(indio_dev);
2012 		if (result)
2013 			goto error_power_off;
2014 	}
2015 
2016 	/* chip init is done, turning on runtime power management */
2017 	result = pm_runtime_set_active(dev);
2018 	if (result)
2019 		goto error_power_off;
2020 	pm_runtime_get_noresume(dev);
2021 	pm_runtime_enable(dev);
2022 	pm_runtime_set_autosuspend_delay(dev, INV_MPU6050_SUSPEND_DELAY_MS);
2023 	pm_runtime_use_autosuspend(dev);
2024 	pm_runtime_put(dev);
2025 	result = devm_add_action_or_reset(dev, inv_mpu_pm_disable, dev);
2026 	if (result)
2027 		return result;
2028 
2029 	switch (chip_type) {
2030 	case INV_MPU6000:
2031 	case INV_MPU6050:
2032 		indio_dev->channels = inv_mpu6050_channels;
2033 		indio_dev->num_channels = ARRAY_SIZE(inv_mpu6050_channels);
2034 		indio_dev->available_scan_masks = inv_mpu_scan_masks;
2035 		break;
2036 	case INV_MPU9150:
2037 		indio_dev->channels = inv_mpu9150_channels;
2038 		indio_dev->num_channels = ARRAY_SIZE(inv_mpu9150_channels);
2039 		indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
2040 		break;
2041 	case INV_MPU9250:
2042 	case INV_MPU9255:
2043 		indio_dev->channels = inv_mpu9250_channels;
2044 		indio_dev->num_channels = ARRAY_SIZE(inv_mpu9250_channels);
2045 		indio_dev->available_scan_masks = inv_mpu9x50_scan_masks;
2046 		break;
2047 	case INV_IAM20380:
2048 		indio_dev->channels = inv_iam20380_channels;
2049 		indio_dev->num_channels = ARRAY_SIZE(inv_iam20380_channels);
2050 		indio_dev->available_scan_masks = inv_iam20380_scan_masks;
2051 		break;
2052 	case INV_ICM20600:
2053 	case INV_ICM20602:
2054 		indio_dev->channels = inv_mpu6500_channels;
2055 		indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels);
2056 		indio_dev->available_scan_masks = inv_icm20602_scan_masks;
2057 		break;
2058 	default:
2059 		indio_dev->channels = inv_mpu6500_channels;
2060 		indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels);
2061 		indio_dev->available_scan_masks = inv_mpu_scan_masks;
2062 		break;
2063 	}
2064 	/*
2065 	 * Use magnetometer inside the chip only if there is no i2c
2066 	 * auxiliary device in use. Otherwise Going back to 6-axis only.
2067 	 */
2068 	if (st->magn_disabled) {
2069 		switch (chip_type) {
2070 		case INV_MPU9150:
2071 			indio_dev->channels = inv_mpu6050_channels;
2072 			indio_dev->num_channels = ARRAY_SIZE(inv_mpu6050_channels);
2073 			indio_dev->available_scan_masks = inv_mpu_scan_masks;
2074 			break;
2075 		default:
2076 			indio_dev->channels = inv_mpu6500_channels;
2077 			indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels);
2078 			indio_dev->available_scan_masks = inv_mpu_scan_masks;
2079 			break;
2080 		}
2081 	}
2082 
2083 	indio_dev->info = &mpu_info;
2084 
2085 	if (irq > 0) {
2086 		/*
2087 		 * The driver currently only supports buffered capture with its
2088 		 * own trigger. So no IRQ, no trigger, no buffer
2089 		 */
2090 		result = devm_iio_triggered_buffer_setup(dev, indio_dev,
2091 							 iio_pollfunc_store_time,
2092 							 inv_mpu6050_read_fifo,
2093 							 NULL);
2094 		if (result) {
2095 			dev_err(dev, "configure buffer fail %d\n", result);
2096 			return result;
2097 		}
2098 
2099 		result = inv_mpu6050_probe_trigger(indio_dev, irq_type);
2100 		if (result) {
2101 			dev_err(dev, "trigger probe fail %d\n", result);
2102 			return result;
2103 		}
2104 	}
2105 
2106 	result = devm_iio_device_register(dev, indio_dev);
2107 	if (result) {
2108 		dev_err(dev, "IIO register fail %d\n", result);
2109 		return result;
2110 	}
2111 
2112 	return 0;
2113 
2114 error_power_off:
2115 	inv_mpu6050_set_power_itg(st, false);
2116 	return result;
2117 }
2118 EXPORT_SYMBOL_NS_GPL(inv_mpu_core_probe, "IIO_MPU6050");
2119 
inv_mpu_resume(struct device * dev)2120 static int inv_mpu_resume(struct device *dev)
2121 {
2122 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2123 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
2124 	bool wakeup;
2125 	int result;
2126 
2127 	guard(mutex)(&st->lock);
2128 
2129 	wakeup = device_may_wakeup(dev) && st->chip_config.wom_en;
2130 
2131 	if (wakeup) {
2132 		enable_irq(st->irq);
2133 		disable_irq_wake(st->irq);
2134 		result = inv_mpu6050_set_wom_lp(st, false);
2135 		if (result)
2136 			return result;
2137 	} else {
2138 		result = inv_mpu_core_enable_regulator_vddio(st);
2139 		if (result)
2140 			return result;
2141 		result = inv_mpu6050_set_power_itg(st, true);
2142 		if (result)
2143 			return result;
2144 	}
2145 
2146 	pm_runtime_disable(dev);
2147 	pm_runtime_set_active(dev);
2148 	pm_runtime_enable(dev);
2149 
2150 	result = inv_mpu6050_switch_engine(st, true, st->suspended_sensors);
2151 	if (result)
2152 		return result;
2153 
2154 	if (st->chip_config.wom_en && !wakeup) {
2155 		result = inv_mpu6050_set_wom_int(st, true);
2156 		if (result)
2157 			return result;
2158 	}
2159 
2160 	if (iio_buffer_enabled(indio_dev))
2161 		result = inv_mpu6050_prepare_fifo(st, true);
2162 
2163 	return result;
2164 }
2165 
inv_mpu_suspend(struct device * dev)2166 static int inv_mpu_suspend(struct device *dev)
2167 {
2168 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2169 	struct inv_mpu6050_state *st = iio_priv(indio_dev);
2170 	bool wakeup;
2171 	int result;
2172 
2173 	guard(mutex)(&st->lock);
2174 
2175 	st->suspended_sensors = 0;
2176 	if (pm_runtime_suspended(dev))
2177 		return 0;
2178 
2179 	if (iio_buffer_enabled(indio_dev)) {
2180 		result = inv_mpu6050_prepare_fifo(st, false);
2181 		if (result)
2182 			return result;
2183 	}
2184 
2185 	wakeup = device_may_wakeup(dev) && st->chip_config.wom_en;
2186 
2187 	if (st->chip_config.wom_en && !wakeup) {
2188 		result = inv_mpu6050_set_wom_int(st, false);
2189 		if (result)
2190 			return result;
2191 	}
2192 
2193 	if (st->chip_config.accl_en && !wakeup)
2194 		st->suspended_sensors |= INV_MPU6050_SENSOR_ACCL;
2195 	if (st->chip_config.gyro_en)
2196 		st->suspended_sensors |= INV_MPU6050_SENSOR_GYRO;
2197 	if (st->chip_config.temp_en)
2198 		st->suspended_sensors |= INV_MPU6050_SENSOR_TEMP;
2199 	if (st->chip_config.magn_en)
2200 		st->suspended_sensors |= INV_MPU6050_SENSOR_MAGN;
2201 	if (st->chip_config.wom_en && !wakeup)
2202 		st->suspended_sensors |= INV_MPU6050_SENSOR_WOM;
2203 	result = inv_mpu6050_switch_engine(st, false, st->suspended_sensors);
2204 	if (result)
2205 		return result;
2206 
2207 	if (wakeup) {
2208 		result = inv_mpu6050_set_wom_lp(st, true);
2209 		if (result)
2210 			return result;
2211 		enable_irq_wake(st->irq);
2212 		disable_irq(st->irq);
2213 	} else {
2214 		result = inv_mpu6050_set_power_itg(st, false);
2215 		if (result)
2216 			return result;
2217 		inv_mpu_core_disable_regulator_vddio(st);
2218 	}
2219 
2220 	return 0;
2221 }
2222 
inv_mpu_runtime_suspend(struct device * dev)2223 static int inv_mpu_runtime_suspend(struct device *dev)
2224 {
2225 	struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
2226 	unsigned int sensors;
2227 	int ret;
2228 
2229 	mutex_lock(&st->lock);
2230 
2231 	sensors = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO |
2232 			INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN |
2233 			INV_MPU6050_SENSOR_WOM;
2234 	ret = inv_mpu6050_switch_engine(st, false, sensors);
2235 	if (ret)
2236 		goto out_unlock;
2237 
2238 	ret = inv_mpu6050_set_power_itg(st, false);
2239 	if (ret)
2240 		goto out_unlock;
2241 
2242 	inv_mpu_core_disable_regulator_vddio(st);
2243 
2244 out_unlock:
2245 	mutex_unlock(&st->lock);
2246 	return ret;
2247 }
2248 
inv_mpu_runtime_resume(struct device * dev)2249 static int inv_mpu_runtime_resume(struct device *dev)
2250 {
2251 	struct inv_mpu6050_state *st = iio_priv(dev_get_drvdata(dev));
2252 	int ret;
2253 
2254 	ret = inv_mpu_core_enable_regulator_vddio(st);
2255 	if (ret)
2256 		return ret;
2257 
2258 	return inv_mpu6050_set_power_itg(st, true);
2259 }
2260 
2261 EXPORT_NS_GPL_DEV_PM_OPS(inv_mpu_pmops, IIO_MPU6050) = {
2262 	SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
2263 	RUNTIME_PM_OPS(inv_mpu_runtime_suspend, inv_mpu_runtime_resume, NULL)
2264 };
2265 
2266 MODULE_AUTHOR("Invensense Corporation");
2267 MODULE_DESCRIPTION("Invensense device MPU6050 driver");
2268 MODULE_LICENSE("GPL");
2269 MODULE_IMPORT_NS("IIO_INV_SENSORS_TIMESTAMP");
2270