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