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>
19 #include <linux/iio/iio.h>
21 #include "inv_icm42600.h"
22 #include "inv_icm42600_buffer.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_NS_GPL(inv_icm42600_regmap_config, IIO_ICM42600);
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,
90 [INV_CHIP_ICM42631] = {
91 .whoami = INV_ICM42600_WHOAMI_ICM42631,
93 .conf = &inv_icm42600_default_conf,
97 const struct iio_mount_matrix *
98 inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
99 const struct iio_chan_spec *chan)
101 const struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
103 return &st->orientation;
106 uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr)
108 static uint32_t odr_periods[INV_ICM42600_ODR_NB] = {
109 /* reserved values */
139 return odr_periods[odr];
142 static int inv_icm42600_set_pwr_mgmt0(struct inv_icm42600_state *st,
143 enum inv_icm42600_sensor_mode gyro,
144 enum inv_icm42600_sensor_mode accel,
145 bool temp, unsigned int *sleep_ms)
147 enum inv_icm42600_sensor_mode oldgyro = st->conf.gyro.mode;
148 enum inv_icm42600_sensor_mode oldaccel = st->conf.accel.mode;
149 bool oldtemp = st->conf.temp_en;
150 unsigned int sleepval;
154 /* if nothing changed, exit */
155 if (gyro == oldgyro && accel == oldaccel && temp == oldtemp)
158 val = INV_ICM42600_PWR_MGMT0_GYRO(gyro) |
159 INV_ICM42600_PWR_MGMT0_ACCEL(accel);
161 val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
162 ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
166 st->conf.gyro.mode = gyro;
167 st->conf.accel.mode = accel;
168 st->conf.temp_en = temp;
170 /* compute required wait time for sensors to stabilize */
172 /* temperature stabilization time */
173 if (temp && !oldtemp) {
174 if (sleepval < INV_ICM42600_TEMP_STARTUP_TIME_MS)
175 sleepval = INV_ICM42600_TEMP_STARTUP_TIME_MS;
177 /* accel startup time */
178 if (accel != oldaccel && oldaccel == INV_ICM42600_SENSOR_MODE_OFF) {
179 /* block any register write for at least 200 µs */
180 usleep_range(200, 300);
181 if (sleepval < INV_ICM42600_ACCEL_STARTUP_TIME_MS)
182 sleepval = INV_ICM42600_ACCEL_STARTUP_TIME_MS;
184 if (gyro != oldgyro) {
185 /* gyro startup time */
186 if (oldgyro == INV_ICM42600_SENSOR_MODE_OFF) {
187 /* block any register write for at least 200 µs */
188 usleep_range(200, 300);
189 if (sleepval < INV_ICM42600_GYRO_STARTUP_TIME_MS)
190 sleepval = INV_ICM42600_GYRO_STARTUP_TIME_MS;
192 } else if (gyro == INV_ICM42600_SENSOR_MODE_OFF) {
193 if (sleepval < INV_ICM42600_GYRO_STOP_TIME_MS)
194 sleepval = INV_ICM42600_GYRO_STOP_TIME_MS;
198 /* deferred sleep value if sleep pointer is provided or direct sleep */
200 *sleep_ms = sleepval;
207 int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
208 struct inv_icm42600_sensor_conf *conf,
209 unsigned int *sleep_ms)
211 struct inv_icm42600_sensor_conf *oldconf = &st->conf.accel;
215 /* Sanitize missing values with current values */
217 conf->mode = oldconf->mode;
219 conf->fs = oldconf->fs;
221 conf->odr = oldconf->odr;
222 if (conf->filter < 0)
223 conf->filter = oldconf->filter;
225 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
226 if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
227 val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->fs) |
228 INV_ICM42600_ACCEL_CONFIG0_ODR(conf->odr);
229 ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
232 oldconf->fs = conf->fs;
233 oldconf->odr = conf->odr;
236 /* set GYRO_ACCEL_CONFIG0 register (accel filter) */
237 if (conf->filter != oldconf->filter) {
238 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->filter) |
239 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(st->conf.gyro.filter);
240 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
243 oldconf->filter = conf->filter;
246 /* set PWR_MGMT0 register (accel sensor mode) */
247 return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode, conf->mode,
248 st->conf.temp_en, sleep_ms);
251 int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
252 struct inv_icm42600_sensor_conf *conf,
253 unsigned int *sleep_ms)
255 struct inv_icm42600_sensor_conf *oldconf = &st->conf.gyro;
259 /* sanitize missing values with current values */
261 conf->mode = oldconf->mode;
263 conf->fs = oldconf->fs;
265 conf->odr = oldconf->odr;
266 if (conf->filter < 0)
267 conf->filter = oldconf->filter;
269 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
270 if (conf->fs != oldconf->fs || conf->odr != oldconf->odr) {
271 val = INV_ICM42600_GYRO_CONFIG0_FS(conf->fs) |
272 INV_ICM42600_GYRO_CONFIG0_ODR(conf->odr);
273 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
276 oldconf->fs = conf->fs;
277 oldconf->odr = conf->odr;
280 /* set GYRO_ACCEL_CONFIG0 register (gyro filter) */
281 if (conf->filter != oldconf->filter) {
282 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(st->conf.accel.filter) |
283 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->filter);
284 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
287 oldconf->filter = conf->filter;
290 /* set PWR_MGMT0 register (gyro sensor mode) */
291 return inv_icm42600_set_pwr_mgmt0(st, conf->mode, st->conf.accel.mode,
292 st->conf.temp_en, sleep_ms);
297 int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
298 unsigned int *sleep_ms)
300 return inv_icm42600_set_pwr_mgmt0(st, st->conf.gyro.mode,
301 st->conf.accel.mode, enable,
305 int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
306 unsigned int writeval, unsigned int *readval)
308 struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
311 mutex_lock(&st->lock);
314 ret = regmap_read(st->map, reg, readval);
316 ret = regmap_write(st->map, reg, writeval);
318 mutex_unlock(&st->lock);
323 static int inv_icm42600_set_conf(struct inv_icm42600_state *st,
324 const struct inv_icm42600_conf *conf)
329 /* set PWR_MGMT0 register (gyro & accel sensor mode, temp enabled) */
330 val = INV_ICM42600_PWR_MGMT0_GYRO(conf->gyro.mode) |
331 INV_ICM42600_PWR_MGMT0_ACCEL(conf->accel.mode);
333 val |= INV_ICM42600_PWR_MGMT0_TEMP_DIS;
334 ret = regmap_write(st->map, INV_ICM42600_REG_PWR_MGMT0, val);
338 /* set GYRO_CONFIG0 register (gyro fullscale & odr) */
339 val = INV_ICM42600_GYRO_CONFIG0_FS(conf->gyro.fs) |
340 INV_ICM42600_GYRO_CONFIG0_ODR(conf->gyro.odr);
341 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_CONFIG0, val);
345 /* set ACCEL_CONFIG0 register (accel fullscale & odr) */
346 val = INV_ICM42600_ACCEL_CONFIG0_FS(conf->accel.fs) |
347 INV_ICM42600_ACCEL_CONFIG0_ODR(conf->accel.odr);
348 ret = regmap_write(st->map, INV_ICM42600_REG_ACCEL_CONFIG0, val);
352 /* set GYRO_ACCEL_CONFIG0 register (gyro & accel filters) */
353 val = INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(conf->accel.filter) |
354 INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(conf->gyro.filter);
355 ret = regmap_write(st->map, INV_ICM42600_REG_GYRO_ACCEL_CONFIG0, val);
359 /* update internal conf */
366 * inv_icm42600_setup() - check and setup chip
367 * @st: driver internal state
368 * @bus_setup: callback for setting up bus specific registers
370 * Returns 0 on success, a negative error code otherwise.
372 static int inv_icm42600_setup(struct inv_icm42600_state *st,
373 inv_icm42600_bus_setup bus_setup)
375 const struct inv_icm42600_hw *hw = &inv_icm42600_hw[st->chip];
376 const struct device *dev = regmap_get_device(st->map);
380 /* check chip self-identification value */
381 ret = regmap_read(st->map, INV_ICM42600_REG_WHOAMI, &val);
384 if (val != hw->whoami) {
385 dev_err(dev, "invalid whoami %#02x expected %#02x (%s)\n",
386 val, hw->whoami, hw->name);
391 /* reset to make sure previous state are not there */
392 ret = regmap_write(st->map, INV_ICM42600_REG_DEVICE_CONFIG,
393 INV_ICM42600_DEVICE_CONFIG_SOFT_RESET);
396 msleep(INV_ICM42600_RESET_TIME_MS);
398 ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &val);
401 if (!(val & INV_ICM42600_INT_STATUS_RESET_DONE)) {
402 dev_err(dev, "reset error, reset done bit not set\n");
406 /* set chip bus configuration */
411 /* sensor data in big-endian (default) */
412 ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
413 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN,
414 INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN);
418 return inv_icm42600_set_conf(st, hw->conf);
421 static irqreturn_t inv_icm42600_irq_timestamp(int irq, void *_data)
423 struct inv_icm42600_state *st = _data;
425 st->timestamp.gyro = iio_get_time_ns(st->indio_gyro);
426 st->timestamp.accel = iio_get_time_ns(st->indio_accel);
428 return IRQ_WAKE_THREAD;
431 static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
433 struct inv_icm42600_state *st = _data;
434 struct device *dev = regmap_get_device(st->map);
438 mutex_lock(&st->lock);
440 ret = regmap_read(st->map, INV_ICM42600_REG_INT_STATUS, &status);
445 if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
446 dev_warn(dev, "FIFO full data lost!\n");
448 /* FIFO threshold reached */
449 if (status & INV_ICM42600_INT_STATUS_FIFO_THS) {
450 ret = inv_icm42600_buffer_fifo_read(st, 0);
452 dev_err(dev, "FIFO read error %d\n", ret);
455 ret = inv_icm42600_buffer_fifo_parse(st);
457 dev_err(dev, "FIFO parsing error %d\n", ret);
461 mutex_unlock(&st->lock);
466 * inv_icm42600_irq_init() - initialize int pin and interrupt handler
467 * @st: driver internal state
469 * @irq_type: irq trigger type
470 * @open_drain: true if irq is open drain, false for push-pull
472 * Returns 0 on success, a negative error code otherwise.
474 static int inv_icm42600_irq_init(struct inv_icm42600_state *st, int irq,
475 int irq_type, bool open_drain)
477 struct device *dev = regmap_get_device(st->map);
481 /* configure INT1 interrupt: default is active low on edge */
483 case IRQF_TRIGGER_RISING:
484 case IRQF_TRIGGER_HIGH:
485 val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH;
488 val = INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW;
493 case IRQF_TRIGGER_LOW:
494 case IRQF_TRIGGER_HIGH:
495 val |= INV_ICM42600_INT_CONFIG_INT1_LATCHED;
502 val |= INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL;
504 ret = regmap_write(st->map, INV_ICM42600_REG_INT_CONFIG, val);
508 /* Deassert async reset for proper INT pin operation (cf datasheet) */
509 ret = regmap_update_bits(st->map, INV_ICM42600_REG_INT_CONFIG1,
510 INV_ICM42600_INT_CONFIG1_ASYNC_RESET, 0);
514 return devm_request_threaded_irq(dev, irq, inv_icm42600_irq_timestamp,
515 inv_icm42600_irq_handler, irq_type,
519 static int inv_icm42600_timestamp_setup(struct inv_icm42600_state *st)
523 /* enable timestamp register */
524 val = INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN |
525 INV_ICM42600_TMST_CONFIG_TMST_EN;
526 return regmap_update_bits(st->map, INV_ICM42600_REG_TMST_CONFIG,
527 INV_ICM42600_TMST_CONFIG_MASK, val);
530 static int inv_icm42600_enable_regulator_vddio(struct inv_icm42600_state *st)
534 ret = regulator_enable(st->vddio_supply);
538 /* wait a little for supply ramp */
539 usleep_range(3000, 4000);
544 static void inv_icm42600_disable_vdd_reg(void *_data)
546 struct inv_icm42600_state *st = _data;
547 const struct device *dev = regmap_get_device(st->map);
550 ret = regulator_disable(st->vdd_supply);
552 dev_err(dev, "failed to disable vdd error %d\n", ret);
555 static void inv_icm42600_disable_vddio_reg(void *_data)
557 struct inv_icm42600_state *st = _data;
558 const struct device *dev = regmap_get_device(st->map);
561 ret = regulator_disable(st->vddio_supply);
563 dev_err(dev, "failed to disable vddio error %d\n", ret);
566 static void inv_icm42600_disable_pm(void *_data)
568 struct device *dev = _data;
570 pm_runtime_put_sync(dev);
571 pm_runtime_disable(dev);
574 int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
575 inv_icm42600_bus_setup bus_setup)
577 struct device *dev = regmap_get_device(regmap);
578 struct inv_icm42600_state *st;
579 struct irq_data *irq_desc;
584 if (chip <= INV_CHIP_INVALID || chip >= INV_CHIP_NB) {
585 dev_err(dev, "invalid chip = %d\n", chip);
589 /* get irq properties, set trigger falling by default */
590 irq_desc = irq_get_irq_data(irq);
592 dev_err(dev, "could not find IRQ %d\n", irq);
596 irq_type = irqd_get_trigger_type(irq_desc);
598 irq_type = IRQF_TRIGGER_FALLING;
600 open_drain = device_property_read_bool(dev, "drive-open-drain");
602 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
606 dev_set_drvdata(dev, st);
607 mutex_init(&st->lock);
611 ret = iio_read_mount_matrix(dev, &st->orientation);
613 dev_err(dev, "failed to retrieve mounting matrix %d\n", ret);
617 st->vdd_supply = devm_regulator_get(dev, "vdd");
618 if (IS_ERR(st->vdd_supply))
619 return PTR_ERR(st->vdd_supply);
621 st->vddio_supply = devm_regulator_get(dev, "vddio");
622 if (IS_ERR(st->vddio_supply))
623 return PTR_ERR(st->vddio_supply);
625 ret = regulator_enable(st->vdd_supply);
628 msleep(INV_ICM42600_POWER_UP_TIME_MS);
630 ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vdd_reg, st);
634 ret = inv_icm42600_enable_regulator_vddio(st);
638 ret = devm_add_action_or_reset(dev, inv_icm42600_disable_vddio_reg, st);
642 /* setup chip registers */
643 ret = inv_icm42600_setup(st, bus_setup);
647 ret = inv_icm42600_timestamp_setup(st);
651 ret = inv_icm42600_buffer_init(st);
655 st->indio_gyro = inv_icm42600_gyro_init(st);
656 if (IS_ERR(st->indio_gyro))
657 return PTR_ERR(st->indio_gyro);
659 st->indio_accel = inv_icm42600_accel_init(st);
660 if (IS_ERR(st->indio_accel))
661 return PTR_ERR(st->indio_accel);
663 ret = inv_icm42600_irq_init(st, irq, irq_type, open_drain);
667 /* setup runtime power management */
668 ret = pm_runtime_set_active(dev);
671 pm_runtime_get_noresume(dev);
672 pm_runtime_enable(dev);
673 pm_runtime_set_autosuspend_delay(dev, INV_ICM42600_SUSPEND_DELAY_MS);
674 pm_runtime_use_autosuspend(dev);
677 return devm_add_action_or_reset(dev, inv_icm42600_disable_pm, dev);
679 EXPORT_SYMBOL_NS_GPL(inv_icm42600_core_probe, IIO_ICM42600);
682 * Suspend saves sensors state and turns everything off.
683 * Check first if runtime suspend has not already done the job.
685 static int inv_icm42600_suspend(struct device *dev)
687 struct inv_icm42600_state *st = dev_get_drvdata(dev);
690 mutex_lock(&st->lock);
692 st->suspended.gyro = st->conf.gyro.mode;
693 st->suspended.accel = st->conf.accel.mode;
694 st->suspended.temp = st->conf.temp_en;
695 if (pm_runtime_suspended(dev)) {
700 /* disable FIFO data streaming */
702 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
703 INV_ICM42600_FIFO_CONFIG_BYPASS);
708 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
709 INV_ICM42600_SENSOR_MODE_OFF, false,
714 regulator_disable(st->vddio_supply);
717 mutex_unlock(&st->lock);
722 * System resume gets the system back on and restores the sensors state.
723 * Manually put runtime power management in system active state.
725 static int inv_icm42600_resume(struct device *dev)
727 struct inv_icm42600_state *st = dev_get_drvdata(dev);
730 mutex_lock(&st->lock);
732 ret = inv_icm42600_enable_regulator_vddio(st);
736 pm_runtime_disable(dev);
737 pm_runtime_set_active(dev);
738 pm_runtime_enable(dev);
740 /* restore sensors state */
741 ret = inv_icm42600_set_pwr_mgmt0(st, st->suspended.gyro,
743 st->suspended.temp, NULL);
747 /* restore FIFO data streaming */
749 ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
750 INV_ICM42600_FIFO_CONFIG_STREAM);
753 mutex_unlock(&st->lock);
757 /* Runtime suspend will turn off sensors that are enabled by iio devices. */
758 static int inv_icm42600_runtime_suspend(struct device *dev)
760 struct inv_icm42600_state *st = dev_get_drvdata(dev);
763 mutex_lock(&st->lock);
765 /* disable all sensors */
766 ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
767 INV_ICM42600_SENSOR_MODE_OFF, false,
772 regulator_disable(st->vddio_supply);
775 mutex_unlock(&st->lock);
779 /* Sensors are enabled by iio devices, no need to turn them back on here. */
780 static int inv_icm42600_runtime_resume(struct device *dev)
782 struct inv_icm42600_state *st = dev_get_drvdata(dev);
785 mutex_lock(&st->lock);
787 ret = inv_icm42600_enable_regulator_vddio(st);
789 mutex_unlock(&st->lock);
793 EXPORT_NS_GPL_DEV_PM_OPS(inv_icm42600_pm_ops, IIO_ICM42600) = {
794 SYSTEM_SLEEP_PM_OPS(inv_icm42600_suspend, inv_icm42600_resume)
795 RUNTIME_PM_OPS(inv_icm42600_runtime_suspend,
796 inv_icm42600_runtime_resume, NULL)
799 MODULE_AUTHOR("InvenSense, Inc.");
800 MODULE_DESCRIPTION("InvenSense ICM-426xx device driver");
801 MODULE_LICENSE("GPL");
802 MODULE_IMPORT_NS(IIO_INV_SENSORS_TIMESTAMP);