1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Invensense, Inc.
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 #include <linux/iio/iio.h>
20 #include "inv_icm42600.h"
21 #include "inv_icm42600_buffer.h"
22 #include "inv_icm42600_timestamp.h"
24 static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
29 .selector_reg = INV_ICM42600_REG_BANK_SEL,
30 .selector_mask = INV_ICM42600_BANK_SEL_MASK,
37 const struct regmap_config inv_icm42600_regmap_config = {
40 .max_register = 0x4FFF,
41 .ranges = inv_icm42600_regmap_ranges,
42 .num_ranges = ARRAY_SIZE(inv_icm42600_regmap_ranges),
44 EXPORT_SYMBOL_GPL(inv_icm42600_regmap_config);
46 struct inv_icm42600_hw {
49 const struct inv_icm42600_conf *conf;
52 /* chip initial default configuration */
53 static const struct inv_icm42600_conf inv_icm42600_default_conf = {
55 .mode = INV_ICM42600_SENSOR_MODE_OFF,
56 .fs = INV_ICM42600_GYRO_FS_2000DPS,
57 .odr = INV_ICM42600_ODR_50HZ,
58 .filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
61 .mode = INV_ICM42600_SENSOR_MODE_OFF,
62 .fs = INV_ICM42600_ACCEL_FS_16G,
63 .odr = INV_ICM42600_ODR_50HZ,
64 .filter = INV_ICM42600_FILTER_BW_ODR_DIV_2,
69 static const struct inv_icm42600_hw inv_icm42600_hw[INV_CHIP_NB] = {
70 [INV_CHIP_ICM42600] = {
71 .whoami = INV_ICM42600_WHOAMI_ICM42600,
73 .conf = &inv_icm42600_default_conf,
75 [INV_CHIP_ICM42602] = {
76 .whoami = INV_ICM42600_WHOAMI_ICM42602,
78 .conf = &inv_icm42600_default_conf,
80 [INV_CHIP_ICM42605] = {
81 .whoami = INV_ICM42600_WHOAMI_ICM42605,
83 .conf = &inv_icm42600_default_conf,
85 [INV_CHIP_ICM42622] = {
86 .whoami = INV_ICM42600_WHOAMI_ICM42622,
88 .conf = &inv_icm42600_default_conf,
92 const struct iio_mount_matrix *
93 inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
94 const struct iio_chan_spec *chan)
96 const struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
98 return &st->orientation;
101 uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)
103 static uint32_t odr_periods[INV_ICM42600_ODR_NB] = {
104 /* reserved values */
134 return odr_periods[odr];
137 static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state *st,
138 enum inv_icm42600_sensor_mode gyro,
139 enum inv_icm42600_sensor_mode accel,
140 bool temp, unsigned int *sleep_ms)
142 enum inv_icm42600_sensor_mode oldgyro = st->conf.gyro.mode;
143 enum inv_icm42600_sensor_mode oldaccel = st->conf.accel.mode;
144 bool oldtemp = st->conf.temp_en;
145 unsigned int sleepval;
149 /* if nothing changed, exit */
150 if (gyro == oldgyro && accel == oldaccel && temp == oldtemp)
153 val = INV_ICM42600_PWR_MGMT0_GYRO(gyro) |
154 INV_ICM42600_PWR_MGMT0_ACCEL(accel);
156 val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
157 ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
161 st->conf.gyro.mode = gyro;
162 st->conf.accel.mode = accel;
163 st->conf.temp_en = temp;
165 /* compute required wait time for sensors to stabilize */
167 /* temperature stabilization time */
168 if (temp && !oldtemp) {
169 if (sleepval < INV_ICM42600_TEMP_STARTUP_TIME_MS)
170 sleepval = INV_ICM42600_TEMP_STARTUP_TIME_MS;
172 /* accel startup time */
173 if (accel != oldaccel && oldaccel == INV_ICM42600_SENSOR_MODE_OFF) {
174 /* block any register write for at least 200 µs */
175 usleep_range(200, 300);
176 if (sleepval < INV_ICM42600_ACCEL_STARTUP_TIME_MS)
177 sleepval = INV_ICM42600_ACCEL_STARTUP_TIME_MS;
179 if (gyro != oldgyro) {
180 /* gyro startup time */
181 if (oldgyro == INV_ICM42600_SENSOR_MODE_OFF) {
182 /* block any register write for at least 200 µs */
183 usleep_range(200, 300);
184 if (sleepval < INV_ICM42600_GYRO_STARTUP_TIME_MS)
185 sleepval = INV_ICM42600_GYRO_STARTUP_TIME_MS;
187 } else if (gyro == INV_ICM42600_SENSOR_MODE_OFF) {
188 if (sleepval < INV_ICM42600_GYRO_STOP_TIME_MS)
189 sleepval = INV_ICM42600_GYRO_STOP_TIME_MS;
193 /* deferred sleep value if sleep pointer is provided or direct sleep */
195 *sleep_ms = sleepval;
202 int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
203 struct inv_icm42600_sensor_conf *conf,
204 unsigned int *sleep_ms)
206 struct inv_icm42600_sensor_conf *oldconf = &st->conf.accel;
210 /* Sanitize missing values with current values */
212 conf->mode = oldconf->mode;
214 conf->fs = oldconf->fs;
216 conf->odr = oldconf->odr;
217 if (conf->filter < 0)
218 conf->filter = oldconf->filter;
220 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
221 if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
222 val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->fs) |
223 INV_ICM42600_ACCEL_CONFIG0_ODR(conf->odr);
224 ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
227 oldconf->fs = conf->fs;
228 oldconf->odr = conf->odr;
231 /* set GYRO_ACCEL_CONFIG0 register (accel filter) */
232 if (conf->filter != oldconf->filter) {
233 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->filter) |
234 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st->conf.gyro.filter);
235 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
238 oldconf->filter = conf->filter;
241 /* set PWR_MGMT0 register (accel sensor mode) */
242 return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode, conf->mode,
243 st->conf.temp_en, sleep_ms);
246 int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
247 struct inv_icm42600_sensor_conf *conf,
248 unsigned int *sleep_ms)
250 struct inv_icm42600_sensor_conf *oldconf = &st->conf.gyro;
254 /* sanitize missing values with current values */
256 conf->mode = oldconf->mode;
258 conf->fs = oldconf->fs;
260 conf->odr = oldconf->odr;
261 if (conf->filter < 0)
262 conf->filter = oldconf->filter;
264 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
265 if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
266 val = INV_ICM42600_GYRO_CONFIG0_FS(conf->fs) |
267 INV_ICM42600_GYRO_CONFIG0_ODR(conf->odr);
268 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
271 oldconf->fs = conf->fs;
272 oldconf->odr = conf->odr;
275 /* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
276 if (conf->filter != oldconf->filter) {
277 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st->conf.accel.filter) |
278 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->filter);
279 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
282 oldconf->filter = conf->filter;
285 /* set PWR_MGMT0 register (gyro sensor mode) */
286 return inv_icm42600_set_pwr_mgmt0(st, conf->mode, st->conf.accel.mode,
287 st->conf.temp_en, sleep_ms);
292 int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
293 unsigned int *sleep_ms)
295 return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode,
296 st->conf.accel.mode, enable,
300 int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
301 unsigned int writeval, unsigned int *readval)
303 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
306 mutex_lock(&st->lock);
309 ret = regmap_read(st->map, reg, readval);
311 ret = regmap_write(st->map, reg, writeval);
313 mutex_unlock(&st->lock);
318 static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
319 const struct inv_icm42600_conf *conf)
324 /* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
325 val = INV_ICM42600_PWR_MGMT0_GYRO(conf->gyro.mode) |
326 INV_ICM42600_PWR_MGMT0_ACCEL(conf->accel.mode);
328 val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
329 ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
333 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
334 val = INV_ICM42600_GYRO_CONFIG0_FS(conf->gyro.fs) |
335 INV_ICM42600_GYRO_CONFIG0_ODR(conf->gyro.odr);
336 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
340 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
341 val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->accel.fs) |
342 INV_ICM42600_ACCEL_CONFIG0_ODR(conf->accel.odr);
343 ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
347 /* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
348 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->accel.filter) |
349 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->gyro.filter);
350 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
354 /* update internal conf */
361 * inv_icm42600_setup() - check and setup chip
362 * @st: driver internal state
363 * @bus_setup: callback for setting up bus specific registers
365 * Returns 0 on success, a negative error code otherwise.
367 static int inv_icm42600_setup(struct inv_icm42600_state *st,
368 inv_icm42600_bus_setup bus_setup)
370 const struct inv_icm42600_hw *hw = &inv_icm42600_hw[st->chip];
371 const struct device *dev = regmap_get_device(st->map);
375 /* check chip self-identification value */
376 ret = regmap_read(st->map, INV_ICM42600_REG_WHOAMI, &val);
379 if (val != hw->whoami) {
380 dev_err(dev, "invalid whoami %#02x expected %#02x (%s)\n",
381 val, hw->whoami, hw->name);
386 /* reset to make sure previous state are not there */
387 ret = regmap_write(st->map, INV_ICM42600_REG_DEVICE_CONFIG,
388 INV_ICM42600_DEVICE_CONFIG_SOFT_RESET);
391 msleep(INV_ICM42600_RESET_TIME_MS);
393 ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &val);
396 if (!(val & INV_ICM42600_INT_STATUS_RESET_DONE)) {
397 dev_err(dev, "reset error, reset done bit not set\n");
401 /* set chip bus configuration */
406 /* sensor data in big-endian (default) */
407 ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
408 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN,
409 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN);
413 return inv_icm42600_set_conf(st, hw->conf);
416 static irqreturn_t inv_icm42600_irq_timestamp(int irq, void *_data)
418 struct inv_icm42600_state *st = _data;
420 st->timestamp.gyro = iio_get_time_ns(st->indio_gyro);
421 st->timestamp.accel = iio_get_time_ns(st->indio_accel);
423 return IRQ_WAKE_THREAD;
426 static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
428 struct inv_icm42600_state *st = _data;
429 struct device *dev = regmap_get_device(st->map);
433 mutex_lock(&st->lock);
435 ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &status);
440 if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
441 dev_warn(dev, "FIFO full data lost!\n");
443 /* FIFO threshold reached */
444 if (status & INV_ICM42600_INT_STATUS_FIFO_THS) {
445 ret = inv_icm42600_buffer_fifo_read(st, 0);
447 dev_err(dev, "FIFO read error %d\n", ret);
450 ret = inv_icm42600_buffer_fifo_parse(st);
452 dev_err(dev, "FIFO parsing error %d\n", ret);
456 mutex_unlock(&st->lock);
461 * inv_icm42600_irq_init() - initialize int pin and interrupt handler
462 * @st: driver internal state
464 * @irq_type: irq trigger type
465 * @open_drain: true if irq is open drain, false for push-pull
467 * Returns 0 on success, a negative error code otherwise.
469 static int inv_icm42600_irq_init(struct inv_icm42600_state *st, int irq,
470 int irq_type, bool open_drain)
472 struct device *dev = regmap_get_device(st->map);
476 /* configure INT1 interrupt: default is active low on edge */
478 case IRQF_TRIGGER_RISING:
479 case IRQF_TRIGGER_HIGH:
480 val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH;
483 val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW;
488 case IRQF_TRIGGER_LOW:
489 case IRQF_TRIGGER_HIGH:
490 val |= INV_ICM42600_INT_CONFIG_INT1_LATCHED;
497 val |= INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL;
499 ret = regmap_write(st->map, INV_ICM42600_REG_INT_CONFIG, val);
503 /* Deassert async reset for proper INT pin operation (cf datasheet) */
504 ret = regmap_update_bits(st->map, INV_ICM42600_REG_INT_CONFIG1,
505 INV_ICM42600_INT_CONFIG1_ASYNC_RESET, 0);
509 return devm_request_threaded_irq(dev, irq, inv_icm42600_irq_timestamp,
510 inv_icm42600_irq_handler, irq_type,
514 static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state *st)
518 ret = regulator_enable(st->vddio_supply);
522 /* wait a little for supply ramp */
523 usleep_range(3000, 4000);
528 static void inv_icm42600_disable_vdd_reg(void *_data)
530 struct inv_icm42600_state *st = _data;
531 const struct device *dev = regmap_get_device(st->map);
534 ret = regulator_disable(st->vdd_supply);
536 dev_err(dev, "failed to disable vdd error %d\n", ret);
539 static void inv_icm42600_disable_vddio_reg(void *_data)
541 struct inv_icm42600_state *st = _data;
542 const struct device *dev = regmap_get_device(st->map);
545 ret = regulator_disable(st->vddio_supply);
547 dev_err(dev, "failed to disable vddio error %d\n", ret);
550 static void inv_icm42600_disable_pm(void *_data)
552 struct device *dev = _data;
554 pm_runtime_put_sync(dev);
555 pm_runtime_disable(dev);
558 int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
559 inv_icm42600_bus_setup bus_setup)
561 struct device *dev = regmap_get_device(regmap);
562 struct inv_icm42600_state *st;
563 struct irq_data *irq_desc;
568 if (chip <= INV_CHIP_INVALID || chip >= INV_CHIP_NB) {
569 dev_err(dev, "invalid chip = %d\n", chip);
573 /* get irq properties, set trigger falling by default */
574 irq_desc = irq_get_irq_data(irq);
576 dev_err(dev, "could not find IRQ %d\n", irq);
580 irq_type = irqd_get_trigger_type(irq_desc);
582 irq_type = IRQF_TRIGGER_FALLING;
584 open_drain = device_property_read_bool(dev, "drive-open-drain");
586 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
590 dev_set_drvdata(dev, st);
591 mutex_init(&st->lock);
595 ret = iio_read_mount_matrix(dev, "mount-matrix", &st->orientation);
597 dev_err(dev, "failed to retrieve mounting matrix %d\n", ret);
601 st->vdd_supply = devm_regulator_get(dev, "vdd");
602 if (IS_ERR(st->vdd_supply))
603 return PTR_ERR(st->vdd_supply);
605 st->vddio_supply = devm_regulator_get(dev, "vddio");
606 if (IS_ERR(st->vddio_supply))
607 return PTR_ERR(st->vddio_supply);
609 ret = regulator_enable(st->vdd_supply);
612 msleep(INV_ICM42600_POWER_UP_TIME_MS);
614 ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vdd_reg, st);
618 ret = inv_icm42600_enable_regulator_vddio(st);
622 ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vddio_reg, st);
626 /* setup chip registers */
627 ret = inv_icm42600_setup(st, bus_setup);
631 ret = inv_icm42600_timestamp_setup(st);
635 ret = inv_icm42600_buffer_init(st);
639 st->indio_gyro = inv_icm42600_gyro_init(st);
640 if (IS_ERR(st->indio_gyro))
641 return PTR_ERR(st->indio_gyro);
643 st->indio_accel = inv_icm42600_accel_init(st);
644 if (IS_ERR(st->indio_accel))
645 return PTR_ERR(st->indio_accel);
647 ret = inv_icm42600_irq_init(st, irq, irq_type, open_drain);
651 /* setup runtime power management */
652 ret = pm_runtime_set_active(dev);
655 pm_runtime_get_noresume(dev);
656 pm_runtime_enable(dev);
657 pm_runtime_set_autosuspend_delay(dev, INV_ICM42600_SUSPEND_DELAY_MS);
658 pm_runtime_use_autosuspend(dev);
661 return devm_add_action_or_reset(dev, inv_icm42600_disable_pm, dev);
663 EXPORT_SYMBOL_GPL(inv_icm42600_core_probe);
666 * Suspend saves sensors state and turns everything off.
667 * Check first if runtime suspend has not already done the job.
669 static int __maybe_unused inv_icm42600_suspend(struct device *dev)
671 struct inv_icm42600_state *st = dev_get_drvdata(dev);
674 mutex_lock(&st->lock);
676 st->suspended.gyro = st->conf.gyro.mode;
677 st->suspended.accel = st->conf.accel.mode;
678 st->suspended.temp = st->conf.temp_en;
679 if (pm_runtime_suspended(dev)) {
684 /* disable FIFO data streaming */
686 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
687 INV_ICM42600_FIFO_CONFIG_BYPASS);
692 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
693 INV_ICM42600_SENSOR_MODE_OFF, false,
698 regulator_disable(st->vddio_supply);
701 mutex_unlock(&st->lock);
706 * System resume gets the system back on and restores the sensors state.
707 * Manually put runtime power management in system active state.
709 static int __maybe_unused inv_icm42600_resume(struct device *dev)
711 struct inv_icm42600_state *st = dev_get_drvdata(dev);
714 mutex_lock(&st->lock);
716 ret = inv_icm42600_enable_regulator_vddio(st);
720 pm_runtime_disable(dev);
721 pm_runtime_set_active(dev);
722 pm_runtime_enable(dev);
724 /* restore sensors state */
725 ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
727 st->suspended.temp, NULL);
731 /* restore FIFO data streaming */
733 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
734 INV_ICM42600_FIFO_CONFIG_STREAM);
737 mutex_unlock(&st->lock);
741 /* Runtime suspend will turn off sensors that are enabled by iio devices. */
742 static int __maybe_unused inv_icm42600_runtime_suspend(struct device *dev)
744 struct inv_icm42600_state *st = dev_get_drvdata(dev);
747 mutex_lock(&st->lock);
749 /* disable all sensors */
750 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
751 INV_ICM42600_SENSOR_MODE_OFF, false,
756 regulator_disable(st->vddio_supply);
759 mutex_unlock(&st->lock);
763 /* Sensors are enabled by iio devices, no need to turn them back on here. */
764 static int __maybe_unused inv_icm42600_runtime_resume(struct device *dev)
766 struct inv_icm42600_state *st = dev_get_drvdata(dev);
769 mutex_lock(&st->lock);
771 ret = inv_icm42600_enable_regulator_vddio(st);
773 mutex_unlock(&st->lock);
777 const struct dev_pm_ops inv_icm42600_pm_ops = {
778 SET_SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend, inv_icm42600_resume)
779 SET_RUNTIME_PM_OPS(inv_icm42600_runtime_suspend,
780 inv_icm42600_runtime_resume, NULL)
782 EXPORT_SYMBOL_GPL(inv_icm42600_pm_ops);
784 MODULE_AUTHOR("InvenSense, Inc.");
785 MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
786 MODULE_LICENSE("GPL");