xref: /linux/drivers/iio/imu/inv_icm42600/inv_icm42600_core.c (revision 0cbe10470b4473ab5e290f1d39033fdb6d6c69c9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2020 Invensense, Inc.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/delay.h>
11 #include <linux/mutex.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
18 
19 #include <linux/iio/iio.h>
20 
21 #include "inv_icm42600.h"
22 #include "inv_icm42600_buffer.h"
23 
24 static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
25 	{
26 		.name = "user banks",
27 		.range_min = 0x0000,
28 		.range_max = 0x4FFF,
29 		.selector_reg = INV_ICM42600_REG_BANK_SEL,
30 		.selector_mask = INV_ICM42600_BANK_SEL_MASK,
31 		.selector_shift = 0,
32 		.window_start = 0,
33 		.window_len = 0x1000,
34 	},
35 };
36 
37 static const struct regmap_range inv_icm42600_regmap_volatile_yes_ranges[] = {
38 	/* Sensor data registers */
39 	regmap_reg_range(0x001D, 0x002A),
40 	/* INT status, FIFO, APEX data */
41 	regmap_reg_range(0x002D, 0x0038),
42 	/* Signal path reset */
43 	regmap_reg_range(0x004B, 0x004B),
44 	/* FIFO lost packets */
45 	regmap_reg_range(0x006C, 0x006D),
46 	/* Timestamp value */
47 	regmap_reg_range(0x1062, 0x1064),
48 };
49 
50 static const struct regmap_range inv_icm42600_regmap_volatile_no_ranges[] = {
51 	regmap_reg_range(0x0000, 0x001C),
52 	regmap_reg_range(0x006E, 0x1061),
53 	regmap_reg_range(0x1065, 0x4FFF),
54 };
55 
56 static const struct regmap_access_table inv_icm42600_regmap_volatile_accesses[] = {
57 	{
58 		.yes_ranges = inv_icm42600_regmap_volatile_yes_ranges,
59 		.n_yes_ranges = ARRAY_SIZE(inv_icm42600_regmap_volatile_yes_ranges),
60 		.no_ranges = inv_icm42600_regmap_volatile_no_ranges,
61 		.n_no_ranges = ARRAY_SIZE(inv_icm42600_regmap_volatile_no_ranges),
62 	},
63 };
64 
65 static const struct regmap_range inv_icm42600_regmap_rd_noinc_no_ranges[] = {
66 	regmap_reg_range(0x0000, INV_ICM42600_REG_FIFO_DATA - 1),
67 	regmap_reg_range(INV_ICM42600_REG_FIFO_DATA + 1, 0x4FFF),
68 };
69 
70 static const struct regmap_access_table inv_icm42600_regmap_rd_noinc_accesses[] = {
71 	{
72 		.no_ranges = inv_icm42600_regmap_rd_noinc_no_ranges,
73 		.n_no_ranges = ARRAY_SIZE(inv_icm42600_regmap_rd_noinc_no_ranges),
74 	},
75 };
76 
77 const struct regmap_config inv_icm42600_regmap_config = {
78 	.name = "inv_icm42600",
79 	.reg_bits = 8,
80 	.val_bits = 8,
81 	.max_register = 0x4FFF,
82 	.ranges = inv_icm42600_regmap_ranges,
83 	.num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
84 	.volatile_table = inv_icm42600_regmap_volatile_accesses,
85 	.rd_noinc_table = inv_icm42600_regmap_rd_noinc_accesses,
86 	.cache_type = REGCACHE_RBTREE,
87 };
88 EXPORT_SYMBOL_NS_GPL(inv_icm42600_regmap_config, "IIO_ICM42600");
89 
90 /* define specific regmap for SPI not supporting burst write */
91 const struct regmap_config inv_icm42600_spi_regmap_config = {
92 	.name = "inv_icm42600",
93 	.reg_bits = 8,
94 	.val_bits = 8,
95 	.max_register = 0x4FFF,
96 	.ranges = inv_icm42600_regmap_ranges,
97 	.num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
98 	.volatile_table = inv_icm42600_regmap_volatile_accesses,
99 	.rd_noinc_table = inv_icm42600_regmap_rd_noinc_accesses,
100 	.cache_type = REGCACHE_RBTREE,
101 	.use_single_write = true,
102 };
103 EXPORT_SYMBOL_NS_GPL(inv_icm42600_spi_regmap_config, "IIO_ICM42600");
104 
105 struct inv_icm42600_hw {
106 	uint8_t whoami;
107 	const char *name;
108 	const struct inv_icm42600_conf *conf;
109 };
110 
111 /* chip initial default configuration */
112 static const struct inv_icm42600_conf inv_icm42600_default_conf = {
113 	.gyro = {
114 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
115 		.fs = INV_ICM42600_GYRO_FS_2000DPS,
116 		.odr = INV_ICM42600_ODR_50HZ,
117 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
118 	},
119 	.accel = {
120 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
121 		.fs = INV_ICM42600_ACCEL_FS_16G,
122 		.odr = INV_ICM42600_ODR_50HZ,
123 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
124 	},
125 	.temp_en = false,
126 };
127 
128 static const struct inv_icm42600_conf inv_icm42686_default_conf = {
129 	.gyro = {
130 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
131 		.fs = INV_ICM42686_GYRO_FS_4000DPS,
132 		.odr = INV_ICM42600_ODR_50HZ,
133 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
134 	},
135 	.accel = {
136 		.mode = INV_ICM42600_SENSOR_MODE_OFF,
137 		.fs = INV_ICM42686_ACCEL_FS_32G,
138 		.odr = INV_ICM42600_ODR_50HZ,
139 		.filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
140 	},
141 	.temp_en = false,
142 };
143 
144 static const struct inv_icm42600_hw inv_icm42600_hw[INV_CHIP_NB] = {
145 	[INV_CHIP_ICM42600] = {
146 		.whoami = INV_ICM42600_WHOAMI_ICM42600,
147 		.name = "icm42600",
148 		.conf = &inv_icm42600_default_conf,
149 	},
150 	[INV_CHIP_ICM42602] = {
151 		.whoami = INV_ICM42600_WHOAMI_ICM42602,
152 		.name = "icm42602",
153 		.conf = &inv_icm42600_default_conf,
154 	},
155 	[INV_CHIP_ICM42605] = {
156 		.whoami = INV_ICM42600_WHOAMI_ICM42605,
157 		.name = "icm42605",
158 		.conf = &inv_icm42600_default_conf,
159 	},
160 	[INV_CHIP_ICM42686] = {
161 		.whoami = INV_ICM42600_WHOAMI_ICM42686,
162 		.name = "icm42686",
163 		.conf = &inv_icm42686_default_conf,
164 	},
165 	[INV_CHIP_ICM42622] = {
166 		.whoami = INV_ICM42600_WHOAMI_ICM42622,
167 		.name = "icm42622",
168 		.conf = &inv_icm42600_default_conf,
169 	},
170 	[INV_CHIP_ICM42688] = {
171 		.whoami = INV_ICM42600_WHOAMI_ICM42688,
172 		.name = "icm42688",
173 		.conf = &inv_icm42600_default_conf,
174 	},
175 	[INV_CHIP_ICM42631] = {
176 		.whoami = INV_ICM42600_WHOAMI_ICM42631,
177 		.name = "icm42631",
178 		.conf = &inv_icm42600_default_conf,
179 	},
180 };
181 
182 const struct iio_mount_matrix *
inv_icm42600_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)183 inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
184 			      const struct iio_chan_spec *chan)
185 {
186 	const struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
187 
188 	return &st->orientation;
189 }
190 
inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)191 uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)
192 {
193 	static uint32_t odr_periods[INV_ICM42600_ODR_NB] = {
194 		/* reserved values */
195 		0, 0, 0,
196 		/* 8kHz */
197 		125000,
198 		/* 4kHz */
199 		250000,
200 		/* 2kHz */
201 		500000,
202 		/* 1kHz */
203 		1000000,
204 		/* 200Hz */
205 		5000000,
206 		/* 100Hz */
207 		10000000,
208 		/* 50Hz */
209 		20000000,
210 		/* 25Hz */
211 		40000000,
212 		/* 12.5Hz */
213 		80000000,
214 		/* 6.25Hz */
215 		160000000,
216 		/* 3.125Hz */
217 		320000000,
218 		/* 1.5625Hz */
219 		640000000,
220 		/* 500Hz */
221 		2000000,
222 	};
223 
224 	return odr_periods[odr];
225 }
226 
inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state * st,enum inv_icm42600_sensor_mode gyro,enum inv_icm42600_sensor_mode accel,bool temp,unsigned int * sleep_ms)227 static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state *st,
228 				      enum inv_icm42600_sensor_mode gyro,
229 				      enum inv_icm42600_sensor_mode accel,
230 				      bool temp, unsigned int *sleep_ms)
231 {
232 	enum inv_icm42600_sensor_mode oldgyro = st->conf.gyro.mode;
233 	enum inv_icm42600_sensor_mode oldaccel = st->conf.accel.mode;
234 	bool oldtemp = st->conf.temp_en;
235 	unsigned int sleepval;
236 	unsigned int val;
237 	int ret;
238 
239 	/* if nothing changed, exit */
240 	if (gyro == oldgyro && accel == oldaccel && temp == oldtemp)
241 		return 0;
242 
243 	val = INV_ICM42600_PWR_MGMT0_GYRO(gyro) |
244 	      INV_ICM42600_PWR_MGMT0_ACCEL(accel);
245 	if (!temp)
246 		val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
247 	ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
248 	if (ret)
249 		return ret;
250 
251 	st->conf.gyro.mode = gyro;
252 	st->conf.accel.mode = accel;
253 	st->conf.temp_en = temp;
254 
255 	/* compute required wait time for sensors to stabilize */
256 	sleepval = 0;
257 	/* temperature stabilization time */
258 	if (temp && !oldtemp) {
259 		if (sleepval < INV_ICM42600_TEMP_STARTUP_TIME_MS)
260 			sleepval = INV_ICM42600_TEMP_STARTUP_TIME_MS;
261 	}
262 	/* accel startup time */
263 	if (accel != oldaccel && oldaccel == INV_ICM42600_SENSOR_MODE_OFF) {
264 		/* block any register write for at least 200 µs */
265 		usleep_range(200, 300);
266 		if (sleepval < INV_ICM42600_ACCEL_STARTUP_TIME_MS)
267 			sleepval = INV_ICM42600_ACCEL_STARTUP_TIME_MS;
268 	}
269 	if (gyro != oldgyro) {
270 		/* gyro startup time */
271 		if (oldgyro == INV_ICM42600_SENSOR_MODE_OFF) {
272 			/* block any register write for at least 200 µs */
273 			usleep_range(200, 300);
274 			if (sleepval < INV_ICM42600_GYRO_STARTUP_TIME_MS)
275 				sleepval = INV_ICM42600_GYRO_STARTUP_TIME_MS;
276 		/* gyro stop time */
277 		} else if (gyro == INV_ICM42600_SENSOR_MODE_OFF) {
278 			if (sleepval < INV_ICM42600_GYRO_STOP_TIME_MS)
279 				sleepval =  INV_ICM42600_GYRO_STOP_TIME_MS;
280 		}
281 	}
282 
283 	/* deferred sleep value if sleep pointer is provided or direct sleep */
284 	if (sleep_ms)
285 		*sleep_ms = sleepval;
286 	else if (sleepval)
287 		msleep(sleepval);
288 
289 	return 0;
290 }
291 
inv_icm42600_set_accel_conf(struct inv_icm42600_state * st,struct inv_icm42600_sensor_conf * conf,unsigned int * sleep_ms)292 int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
293 				struct inv_icm42600_sensor_conf *conf,
294 				unsigned int *sleep_ms)
295 {
296 	struct inv_icm42600_sensor_conf *oldconf = &st->conf.accel;
297 	unsigned int val;
298 	int ret;
299 
300 	/* Sanitize missing values with current values */
301 	if (conf->mode < 0)
302 		conf->mode = oldconf->mode;
303 	if (conf->fs < 0)
304 		conf->fs = oldconf->fs;
305 	if (conf->odr < 0)
306 		conf->odr = oldconf->odr;
307 	if (conf->filter < 0)
308 		conf->filter = oldconf->filter;
309 
310 	/* force power mode against ODR when sensor is on */
311 	switch (conf->mode) {
312 	case INV_ICM42600_SENSOR_MODE_LOW_POWER:
313 	case INV_ICM42600_SENSOR_MODE_LOW_NOISE:
314 		if (conf->odr <= INV_ICM42600_ODR_1KHZ_LN) {
315 			conf->mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
316 			conf->filter = INV_ICM42600_FILTER_BW_ODR_DIV_2;
317 		} else if (conf->odr >= INV_ICM42600_ODR_6_25HZ_LP &&
318 			   conf->odr <= INV_ICM42600_ODR_1_5625HZ_LP) {
319 			conf->mode = INV_ICM42600_SENSOR_MODE_LOW_POWER;
320 			conf->filter = INV_ICM42600_FILTER_AVG_16X;
321 		}
322 		break;
323 	default:
324 		break;
325 	}
326 
327 	/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
328 	if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
329 		val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->fs) |
330 		      INV_ICM42600_ACCEL_CONFIG0_ODR(conf->odr);
331 		ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
332 		if (ret)
333 			return ret;
334 		oldconf->fs = conf->fs;
335 		oldconf->odr = conf->odr;
336 	}
337 
338 	/* set GYRO_ACCEL_CONFIG0 register (accel filter) */
339 	if (conf->filter != oldconf->filter) {
340 		val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->filter) |
341 		      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st->conf.gyro.filter);
342 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
343 		if (ret)
344 			return ret;
345 		oldconf->filter = conf->filter;
346 	}
347 
348 	/* set PWR_MGMT0 register (accel sensor mode) */
349 	return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode, conf->mode,
350 					  st->conf.temp_en, sleep_ms);
351 }
352 
inv_icm42600_set_gyro_conf(struct inv_icm42600_state * st,struct inv_icm42600_sensor_conf * conf,unsigned int * sleep_ms)353 int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
354 			       struct inv_icm42600_sensor_conf *conf,
355 			       unsigned int *sleep_ms)
356 {
357 	struct inv_icm42600_sensor_conf *oldconf = &st->conf.gyro;
358 	unsigned int val;
359 	int ret;
360 
361 	/* sanitize missing values with current values */
362 	if (conf->mode < 0)
363 		conf->mode = oldconf->mode;
364 	if (conf->fs < 0)
365 		conf->fs = oldconf->fs;
366 	if (conf->odr < 0)
367 		conf->odr = oldconf->odr;
368 	if (conf->filter < 0)
369 		conf->filter = oldconf->filter;
370 
371 	/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
372 	if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
373 		val = INV_ICM42600_GYRO_CONFIG0_FS(conf->fs) |
374 		      INV_ICM42600_GYRO_CONFIG0_ODR(conf->odr);
375 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
376 		if (ret)
377 			return ret;
378 		oldconf->fs = conf->fs;
379 		oldconf->odr = conf->odr;
380 	}
381 
382 	/* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
383 	if (conf->filter != oldconf->filter) {
384 		val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st->conf.accel.filter) |
385 		      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->filter);
386 		ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
387 		if (ret)
388 			return ret;
389 		oldconf->filter = conf->filter;
390 	}
391 
392 	/* set PWR_MGMT0 register (gyro sensor mode) */
393 	return inv_icm42600_set_pwr_mgmt0(st, conf->mode, st->conf.accel.mode,
394 					  st->conf.temp_en, sleep_ms);
395 
396 	return 0;
397 }
398 
inv_icm42600_set_temp_conf(struct inv_icm42600_state * st,bool enable,unsigned int * sleep_ms)399 int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
400 			       unsigned int *sleep_ms)
401 {
402 	return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode,
403 					  st->conf.accel.mode, enable,
404 					  sleep_ms);
405 }
406 
inv_icm42600_debugfs_reg(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)407 int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
408 			     unsigned int writeval, unsigned int *readval)
409 {
410 	struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
411 	int ret;
412 
413 	mutex_lock(&st->lock);
414 
415 	if (readval)
416 		ret = regmap_read(st->map, reg, readval);
417 	else
418 		ret = regmap_write(st->map, reg, writeval);
419 
420 	mutex_unlock(&st->lock);
421 
422 	return ret;
423 }
424 
inv_icm42600_set_conf(struct inv_icm42600_state * st,const struct inv_icm42600_conf * conf)425 static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
426 				 const struct inv_icm42600_conf *conf)
427 {
428 	unsigned int val;
429 	int ret;
430 
431 	/* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
432 	val = INV_ICM42600_PWR_MGMT0_GYRO(conf->gyro.mode) |
433 	      INV_ICM42600_PWR_MGMT0_ACCEL(conf->accel.mode);
434 	if (!conf->temp_en)
435 		val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
436 	ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
437 	if (ret)
438 		return ret;
439 
440 	/* set GYRO_CONFIG0 register (gyro fullscale & odr) */
441 	val = INV_ICM42600_GYRO_CONFIG0_FS(conf->gyro.fs) |
442 	      INV_ICM42600_GYRO_CONFIG0_ODR(conf->gyro.odr);
443 	ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
444 	if (ret)
445 		return ret;
446 
447 	/* set ACCEL_CONFIG0 register (accel fullscale & odr) */
448 	val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->accel.fs) |
449 	      INV_ICM42600_ACCEL_CONFIG0_ODR(conf->accel.odr);
450 	ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
451 	if (ret)
452 		return ret;
453 
454 	/* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
455 	val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->accel.filter) |
456 	      INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->gyro.filter);
457 	ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
458 	if (ret)
459 		return ret;
460 
461 	/* update internal conf */
462 	st->conf = *conf;
463 
464 	return 0;
465 }
466 
467 /**
468  *  inv_icm42600_setup() - check and setup chip
469  *  @st:	driver internal state
470  *  @bus_setup:	callback for setting up bus specific registers
471  *
472  *  Returns 0 on success, a negative error code otherwise.
473  */
inv_icm42600_setup(struct inv_icm42600_state * st,inv_icm42600_bus_setup bus_setup)474 static int inv_icm42600_setup(struct inv_icm42600_state *st,
475 			      inv_icm42600_bus_setup bus_setup)
476 {
477 	const struct inv_icm42600_hw *hw = &inv_icm42600_hw[st->chip];
478 	const struct device *dev = regmap_get_device(st->map);
479 	unsigned int val;
480 	int ret;
481 
482 	/* check chip self-identification value */
483 	ret = regmap_read(st->map, INV_ICM42600_REG_WHOAMI, &val);
484 	if (ret)
485 		return ret;
486 	if (val != hw->whoami) {
487 		dev_err(dev, "invalid whoami %#02x expected %#02x (%s)\n",
488 			val, hw->whoami, hw->name);
489 		return -ENODEV;
490 	}
491 	st->name = hw->name;
492 
493 	/* reset to make sure previous state are not there */
494 	ret = regmap_write(st->map, INV_ICM42600_REG_DEVICE_CONFIG,
495 			   INV_ICM42600_DEVICE_CONFIG_SOFT_RESET);
496 	if (ret)
497 		return ret;
498 	msleep(INV_ICM42600_RESET_TIME_MS);
499 
500 	ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &val);
501 	if (ret)
502 		return ret;
503 	if (!(val & INV_ICM42600_INT_STATUS_RESET_DONE)) {
504 		dev_err(dev, "reset error, reset done bit not set\n");
505 		return -ENODEV;
506 	}
507 
508 	/* set chip bus configuration */
509 	ret = bus_setup(st);
510 	if (ret)
511 		return ret;
512 
513 	/* sensor data in big-endian (default) */
514 	ret = regmap_set_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
515 			      INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN);
516 	if (ret)
517 		return ret;
518 
519 	/*
520 	 * Use RC clock for accel low-power to fix glitches when switching
521 	 * gyro on/off while accel low-power is on.
522 	 */
523 	ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG1,
524 				 INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC,
525 				 INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC);
526 	if (ret)
527 		return ret;
528 
529 	return inv_icm42600_set_conf(st, hw->conf);
530 }
531 
inv_icm42600_irq_timestamp(int irq,void * _data)532 static irqreturn_t inv_icm42600_irq_timestamp(int irq, void *_data)
533 {
534 	struct inv_icm42600_state *st = _data;
535 
536 	st->timestamp.gyro = iio_get_time_ns(st->indio_gyro);
537 	st->timestamp.accel = iio_get_time_ns(st->indio_accel);
538 
539 	return IRQ_WAKE_THREAD;
540 }
541 
inv_icm42600_irq_handler(int irq,void * _data)542 static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
543 {
544 	struct inv_icm42600_state *st = _data;
545 	struct device *dev = regmap_get_device(st->map);
546 	unsigned int status;
547 	int ret;
548 
549 	mutex_lock(&st->lock);
550 
551 	ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &status);
552 	if (ret)
553 		goto out_unlock;
554 
555 	/* FIFO full */
556 	if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
557 		dev_warn(dev, "FIFO full data lost!\n");
558 
559 	/* FIFO threshold reached */
560 	if (status & INV_ICM42600_INT_STATUS_FIFO_THS) {
561 		ret = inv_icm42600_buffer_fifo_read(st, 0);
562 		if (ret) {
563 			dev_err(dev, "FIFO read error %d\n", ret);
564 			goto out_unlock;
565 		}
566 		ret = inv_icm42600_buffer_fifo_parse(st);
567 		if (ret)
568 			dev_err(dev, "FIFO parsing error %d\n", ret);
569 	}
570 
571 out_unlock:
572 	mutex_unlock(&st->lock);
573 	return IRQ_HANDLED;
574 }
575 
576 /**
577  * inv_icm42600_irq_init() - initialize int pin and interrupt handler
578  * @st:		driver internal state
579  * @irq:	irq number
580  * @irq_type:	irq trigger type
581  * @open_drain:	true if irq is open drain, false for push-pull
582  *
583  * Returns 0 on success, a negative error code otherwise.
584  */
inv_icm42600_irq_init(struct inv_icm42600_state * st,int irq,int irq_type,bool open_drain)585 static int inv_icm42600_irq_init(struct inv_icm42600_state *st, int irq,
586 				 int irq_type, bool open_drain)
587 {
588 	struct device *dev = regmap_get_device(st->map);
589 	unsigned int val;
590 	int ret;
591 
592 	/* configure INT1 interrupt: default is active low on edge */
593 	switch (irq_type) {
594 	case IRQF_TRIGGER_RISING:
595 	case IRQF_TRIGGER_HIGH:
596 		val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH;
597 		break;
598 	default:
599 		val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW;
600 		break;
601 	}
602 
603 	switch (irq_type) {
604 	case IRQF_TRIGGER_LOW:
605 	case IRQF_TRIGGER_HIGH:
606 		val |= INV_ICM42600_INT_CONFIG_INT1_LATCHED;
607 		break;
608 	default:
609 		break;
610 	}
611 
612 	if (!open_drain)
613 		val |= INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL;
614 
615 	ret = regmap_write(st->map, INV_ICM42600_REG_INT_CONFIG, val);
616 	if (ret)
617 		return ret;
618 
619 	/* Deassert async reset for proper INT pin operation (cf datasheet) */
620 	ret = regmap_clear_bits(st->map, INV_ICM42600_REG_INT_CONFIG1,
621 				INV_ICM42600_INT_CONFIG1_ASYNC_RESET);
622 	if (ret)
623 		return ret;
624 
625 	irq_type |= IRQF_ONESHOT;
626 	return devm_request_threaded_irq(dev, irq, inv_icm42600_irq_timestamp,
627 					 inv_icm42600_irq_handler, irq_type,
628 					 "inv_icm42600", st);
629 }
630 
inv_icm42600_timestamp_setup(struct inv_icm42600_state * st)631 static int inv_icm42600_timestamp_setup(struct inv_icm42600_state *st)
632 {
633 	unsigned int val;
634 
635 	/* enable timestamp register */
636 	val = INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN |
637 	      INV_ICM42600_TMST_CONFIG_TMST_EN;
638 	return regmap_update_bits(st->map, INV_ICM42600_REG_TMST_CONFIG,
639 				  INV_ICM42600_TMST_CONFIG_MASK, val);
640 }
641 
inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state * st)642 static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state *st)
643 {
644 	int ret;
645 
646 	ret = regulator_enable(st->vddio_supply);
647 	if (ret)
648 		return ret;
649 
650 	/* wait a little for supply ramp */
651 	usleep_range(3000, 4000);
652 
653 	return 0;
654 }
655 
inv_icm42600_disable_vdd_reg(void * _data)656 static void inv_icm42600_disable_vdd_reg(void *_data)
657 {
658 	struct inv_icm42600_state *st = _data;
659 	const struct device *dev = regmap_get_device(st->map);
660 	int ret;
661 
662 	ret = regulator_disable(st->vdd_supply);
663 	if (ret)
664 		dev_err(dev, "failed to disable vdd error %d\n", ret);
665 }
666 
inv_icm42600_disable_vddio_reg(void * _data)667 static void inv_icm42600_disable_vddio_reg(void *_data)
668 {
669 	struct inv_icm42600_state *st = _data;
670 	const struct device *dev = regmap_get_device(st->map);
671 	int ret;
672 
673 	ret = regulator_disable(st->vddio_supply);
674 	if (ret)
675 		dev_err(dev, "failed to disable vddio error %d\n", ret);
676 }
677 
inv_icm42600_disable_pm(void * _data)678 static void inv_icm42600_disable_pm(void *_data)
679 {
680 	struct device *dev = _data;
681 
682 	pm_runtime_put_sync(dev);
683 	pm_runtime_disable(dev);
684 }
685 
inv_icm42600_core_probe(struct regmap * regmap,int chip,int irq,inv_icm42600_bus_setup bus_setup)686 int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
687 			    inv_icm42600_bus_setup bus_setup)
688 {
689 	struct device *dev = regmap_get_device(regmap);
690 	struct inv_icm42600_state *st;
691 	int irq_type;
692 	bool open_drain;
693 	int ret;
694 
695 	if (chip <= INV_CHIP_INVALID || chip >= INV_CHIP_NB) {
696 		dev_err(dev, "invalid chip = %d\n", chip);
697 		return -ENODEV;
698 	}
699 
700 	irq_type = irq_get_trigger_type(irq);
701 	if (!irq_type)
702 		irq_type = IRQF_TRIGGER_FALLING;
703 
704 	open_drain = device_property_read_bool(dev, "drive-open-drain");
705 
706 	st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
707 	if (!st)
708 		return -ENOMEM;
709 
710 	dev_set_drvdata(dev, st);
711 	mutex_init(&st->lock);
712 	st->chip = chip;
713 	st->map = regmap;
714 
715 	ret = iio_read_mount_matrix(dev, &st->orientation);
716 	if (ret) {
717 		dev_err(dev, "failed to retrieve mounting matrix %d\n", ret);
718 		return ret;
719 	}
720 
721 	st->vdd_supply = devm_regulator_get(dev, "vdd");
722 	if (IS_ERR(st->vdd_supply))
723 		return PTR_ERR(st->vdd_supply);
724 
725 	st->vddio_supply = devm_regulator_get(dev, "vddio");
726 	if (IS_ERR(st->vddio_supply))
727 		return PTR_ERR(st->vddio_supply);
728 
729 	ret = regulator_enable(st->vdd_supply);
730 	if (ret)
731 		return ret;
732 	msleep(INV_ICM42600_POWER_UP_TIME_MS);
733 
734 	ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vdd_reg, st);
735 	if (ret)
736 		return ret;
737 
738 	ret = inv_icm42600_enable_regulator_vddio(st);
739 	if (ret)
740 		return ret;
741 
742 	ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vddio_reg, st);
743 	if (ret)
744 		return ret;
745 
746 	/* setup chip registers */
747 	ret = inv_icm42600_setup(st, bus_setup);
748 	if (ret)
749 		return ret;
750 
751 	ret = inv_icm42600_timestamp_setup(st);
752 	if (ret)
753 		return ret;
754 
755 	ret = inv_icm42600_buffer_init(st);
756 	if (ret)
757 		return ret;
758 
759 	st->indio_gyro = inv_icm42600_gyro_init(st);
760 	if (IS_ERR(st->indio_gyro))
761 		return PTR_ERR(st->indio_gyro);
762 
763 	st->indio_accel = inv_icm42600_accel_init(st);
764 	if (IS_ERR(st->indio_accel))
765 		return PTR_ERR(st->indio_accel);
766 
767 	ret = inv_icm42600_irq_init(st, irq, irq_type, open_drain);
768 	if (ret)
769 		return ret;
770 
771 	/* setup runtime power management */
772 	ret = pm_runtime_set_active(dev);
773 	if (ret)
774 		return ret;
775 	pm_runtime_get_noresume(dev);
776 	pm_runtime_enable(dev);
777 	pm_runtime_set_autosuspend_delay(dev, INV_ICM42600_SUSPEND_DELAY_MS);
778 	pm_runtime_use_autosuspend(dev);
779 	pm_runtime_put(dev);
780 
781 	return devm_add_action_or_reset(dev, inv_icm42600_disable_pm, dev);
782 }
783 EXPORT_SYMBOL_NS_GPL(inv_icm42600_core_probe, "IIO_ICM42600");
784 
785 /*
786  * Suspend saves sensors state and turns everything off.
787  * Check first if runtime suspend has not already done the job.
788  */
inv_icm42600_suspend(struct device * dev)789 static int inv_icm42600_suspend(struct device *dev)
790 {
791 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
792 	int ret;
793 
794 	mutex_lock(&st->lock);
795 
796 	st->suspended.gyro = st->conf.gyro.mode;
797 	st->suspended.accel = st->conf.accel.mode;
798 	st->suspended.temp = st->conf.temp_en;
799 	if (pm_runtime_suspended(dev)) {
800 		ret = 0;
801 		goto out_unlock;
802 	}
803 
804 	/* disable FIFO data streaming */
805 	if (st->fifo.on) {
806 		ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
807 				   INV_ICM42600_FIFO_CONFIG_BYPASS);
808 		if (ret)
809 			goto out_unlock;
810 	}
811 
812 	ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
813 					 INV_ICM42600_SENSOR_MODE_OFF, false,
814 					 NULL);
815 	if (ret)
816 		goto out_unlock;
817 
818 	regulator_disable(st->vddio_supply);
819 
820 out_unlock:
821 	mutex_unlock(&st->lock);
822 	return ret;
823 }
824 
825 /*
826  * System resume gets the system back on and restores the sensors state.
827  * Manually put runtime power management in system active state.
828  */
inv_icm42600_resume(struct device * dev)829 static int inv_icm42600_resume(struct device *dev)
830 {
831 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
832 	struct inv_icm42600_sensor_state *gyro_st = iio_priv(st->indio_gyro);
833 	struct inv_icm42600_sensor_state *accel_st = iio_priv(st->indio_accel);
834 	int ret;
835 
836 	mutex_lock(&st->lock);
837 
838 	ret = inv_icm42600_enable_regulator_vddio(st);
839 	if (ret)
840 		goto out_unlock;
841 
842 	pm_runtime_disable(dev);
843 	pm_runtime_set_active(dev);
844 	pm_runtime_enable(dev);
845 
846 	/* restore sensors state */
847 	ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
848 					 st->suspended.accel,
849 					 st->suspended.temp, NULL);
850 	if (ret)
851 		goto out_unlock;
852 
853 	/* restore FIFO data streaming */
854 	if (st->fifo.on) {
855 		inv_sensors_timestamp_reset(&gyro_st->ts);
856 		inv_sensors_timestamp_reset(&accel_st->ts);
857 		ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
858 				   INV_ICM42600_FIFO_CONFIG_STREAM);
859 	}
860 
861 out_unlock:
862 	mutex_unlock(&st->lock);
863 	return ret;
864 }
865 
866 /* Runtime suspend will turn off sensors that are enabled by iio devices. */
inv_icm42600_runtime_suspend(struct device * dev)867 static int inv_icm42600_runtime_suspend(struct device *dev)
868 {
869 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
870 	int ret;
871 
872 	mutex_lock(&st->lock);
873 
874 	/* disable all sensors */
875 	ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
876 					 INV_ICM42600_SENSOR_MODE_OFF, false,
877 					 NULL);
878 	if (ret)
879 		goto error_unlock;
880 
881 	regulator_disable(st->vddio_supply);
882 
883 error_unlock:
884 	mutex_unlock(&st->lock);
885 	return ret;
886 }
887 
888 /* Sensors are enabled by iio devices, no need to turn them back on here. */
inv_icm42600_runtime_resume(struct device * dev)889 static int inv_icm42600_runtime_resume(struct device *dev)
890 {
891 	struct inv_icm42600_state *st = dev_get_drvdata(dev);
892 	int ret;
893 
894 	mutex_lock(&st->lock);
895 
896 	ret = inv_icm42600_enable_regulator_vddio(st);
897 
898 	mutex_unlock(&st->lock);
899 	return ret;
900 }
901 
902 EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops, IIO_ICM42600) = {
903 	SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend, inv_icm42600_resume)
904 	RUNTIME_PM_OPS(inv_icm42600_runtime_suspend,
905 		       inv_icm42600_runtime_resume, NULL)
906 };
907 
908 MODULE_AUTHOR("InvenSense, Inc.");
909 MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
910 MODULE_LICENSE("GPL");
911 MODULE_IMPORT_NS("IIO_INV_SENSORS_TIMESTAMP");
912