Lines Matching +full:gyro +full:- +full:pm
6 * SPDX-License-Identifier: Apache-2.0
13 #include <zephyr/pm/device.h>
35 const struct icm42670_config *cfg = dev->config; in icm42670_reg_read()
37 return cfg->bus_io->read(&cfg->bus, reg, buf, size); in icm42670_reg_read()
43 return icm42670_reg_read(serif->context, reg, rbuffer, rlen); in inv_io_hal_read_reg()
49 const struct icm42670_config *cfg = dev->config; in icm42670_reg_write()
51 return cfg->bus_io->write(&cfg->bus, reg, (uint8_t *)buf, size); in icm42670_reg_write()
57 return icm42670_reg_write(serif->context, reg, wbuffer, wlen); in inv_io_hal_write_reg()
280 if ((val->val1 == ICM42670_LOW_POWER_MODE) && in icm42670_set_accel_power_mode()
281 (drv_data->accel_pwr_mode != ICM42670_LOW_POWER_MODE)) { in icm42670_set_accel_power_mode()
282 if (drv_data->accel_hz != 0) { in icm42670_set_accel_power_mode()
283 if (drv_data->accel_hz <= 400) { in icm42670_set_accel_power_mode()
284 inv_imu_enable_accel_low_power_mode(&drv_data->driver); in icm42670_set_accel_power_mode()
287 return -EINVAL; in icm42670_set_accel_power_mode()
290 drv_data->accel_pwr_mode = val->val1; in icm42670_set_accel_power_mode()
291 } else if ((val->val1 == ICM42670_LOW_NOISE_MODE) && in icm42670_set_accel_power_mode()
292 (drv_data->accel_pwr_mode != ICM42670_LOW_NOISE_MODE)) { in icm42670_set_accel_power_mode()
293 if (drv_data->accel_hz != 0) { in icm42670_set_accel_power_mode()
294 if (drv_data->accel_hz >= 12) { in icm42670_set_accel_power_mode()
295 inv_imu_enable_accel_low_noise_mode(&drv_data->driver); in icm42670_set_accel_power_mode()
298 return -EINVAL; in icm42670_set_accel_power_mode()
301 drv_data->accel_pwr_mode = val->val1; in icm42670_set_accel_power_mode()
304 return -EINVAL; in icm42670_set_accel_power_mode()
311 if (val->val1 <= 1600 && val->val1 >= 1) { in icm42670_set_accel_odr()
312 if (drv_data->accel_hz == 0) { in icm42670_set_accel_odr()
314 &drv_data->driver, in icm42670_set_accel_odr()
315 convert_freq_to_bitfield(val->val1, &drv_data->accel_hz)); in icm42670_set_accel_odr()
316 if (drv_data->accel_pwr_mode == ICM42670_LOW_POWER_MODE) { in icm42670_set_accel_odr()
317 inv_imu_enable_accel_low_power_mode(&drv_data->driver); in icm42670_set_accel_odr()
318 } else if (drv_data->accel_pwr_mode == ICM42670_LOW_NOISE_MODE) { in icm42670_set_accel_odr()
319 inv_imu_enable_accel_low_noise_mode(&drv_data->driver); in icm42670_set_accel_odr()
323 &drv_data->driver, in icm42670_set_accel_odr()
324 convert_freq_to_bitfield(val->val1, &drv_data->accel_hz)); in icm42670_set_accel_odr()
326 } else if (val->val1 == 0) { in icm42670_set_accel_odr()
327 inv_imu_disable_accel(&drv_data->driver); in icm42670_set_accel_odr()
328 drv_data->accel_hz = val->val1; in icm42670_set_accel_odr()
331 return -EINVAL; in icm42670_set_accel_odr()
338 if (val->val1 > 16 || val->val1 < 2) { in icm42670_set_accel_fs()
340 return -EINVAL; in icm42670_set_accel_fs()
342 inv_imu_set_accel_fsr(&drv_data->driver, in icm42670_set_accel_fs()
343 convert_acc_fs_to_bitfield(val->val1, &drv_data->accel_fs)); in icm42670_set_accel_fs()
344 LOG_DBG("Set accel full scale to: %d G", drv_data->accel_fs); in icm42670_set_accel_fs()
361 if (val->val1 > 180) { in icm42670_accel_config()
363 return -EINVAL; in icm42670_accel_config()
365 inv_imu_set_accel_ln_bw(&drv_data->driver, convert_ln_bw_to_bitfield(val->val1)); in icm42670_accel_config()
368 if (val->val1 > 64 || val->val1 < 2) { in icm42670_accel_config()
370 return -EINVAL; in icm42670_accel_config()
372 inv_imu_set_accel_lp_avg(&drv_data->driver, convert_lp_avg_to_bitfield(val->val1)); in icm42670_accel_config()
375 return -EINVAL; in icm42670_accel_config()
382 if (val->val1 <= 1600 && val->val1 > 12) { in icm42670_set_gyro_odr()
383 if (drv_data->gyro_hz == 0) { in icm42670_set_gyro_odr()
385 &drv_data->driver, in icm42670_set_gyro_odr()
386 convert_freq_to_bitfield(val->val1, &drv_data->gyro_hz)); in icm42670_set_gyro_odr()
387 inv_imu_enable_gyro_low_noise_mode(&drv_data->driver); in icm42670_set_gyro_odr()
390 &drv_data->driver, in icm42670_set_gyro_odr()
391 convert_freq_to_bitfield(val->val1, &drv_data->gyro_hz)); in icm42670_set_gyro_odr()
393 } else if (val->val1 == 0) { in icm42670_set_gyro_odr()
394 inv_imu_disable_gyro(&drv_data->driver); in icm42670_set_gyro_odr()
395 drv_data->gyro_hz = val->val1; in icm42670_set_gyro_odr()
398 return -EINVAL; in icm42670_set_gyro_odr()
405 if (val->val1 > 2000 || val->val1 < 250) { in icm42670_set_gyro_fs()
407 return -EINVAL; in icm42670_set_gyro_fs()
409 inv_imu_set_gyro_fsr(&drv_data->driver, in icm42670_set_gyro_fs()
410 convert_gyr_fs_to_bitfield(val->val1, &drv_data->gyro_fs)); in icm42670_set_gyro_fs()
411 LOG_DBG("Set gyro fullscale to: %d dps", drv_data->gyro_fs); in icm42670_set_gyro_fs()
425 if (val->val1 > 180) { in icm42670_gyro_config()
427 return -EINVAL; in icm42670_gyro_config()
429 inv_imu_set_gyro_ln_bw(&drv_data->driver, convert_ln_bw_to_bitfield(val->val1)); in icm42670_gyro_config()
433 return -EINVAL; in icm42670_gyro_config()
440 struct icm42670_data *data = dev->data; in icm42670_sensor_init()
444 data->serif.context = (struct device *)dev; in icm42670_sensor_init()
445 data->serif.read_reg = inv_io_hal_read_reg; in icm42670_sensor_init()
446 data->serif.write_reg = inv_io_hal_write_reg; in icm42670_sensor_init()
447 data->serif.max_read = ICM42670_SERIAL_INTERFACE_MAX_READ; in icm42670_sensor_init()
448 data->serif.max_write = ICM42670_SERIAL_INTERFACE_MAX_WRITE; in icm42670_sensor_init()
450 data->serif.serif_type = UI_SPI4; in icm42670_sensor_init()
453 data->serif.serif_type = UI_I2C; in icm42670_sensor_init()
455 err = inv_imu_init(&data->driver, &data->serif, NULL); in icm42670_sensor_init()
461 err = inv_imu_get_who_am_i(&data->driver, &data->chip_id); in icm42670_sensor_init()
467 if (data->chip_id != data->imu_whoami) { in icm42670_sensor_init()
468 LOG_ERR("invalid WHO_AM_I value, was 0x%x but expected 0x%x for %s", data->chip_id, in icm42670_sensor_init()
469 data->imu_whoami, data->imu_name); in icm42670_sensor_init()
470 return -ENOTSUP; in icm42670_sensor_init()
473 LOG_DBG("\"%s\" %s OK", dev->name, data->imu_name); in icm42670_sensor_init()
479 struct icm42670_data *data = dev->data; in icm42670_turn_on_sensor()
480 const struct icm42670_config *cfg = dev->config; in icm42670_turn_on_sensor()
483 err = inv_imu_set_accel_fsr(&data->driver, in icm42670_turn_on_sensor()
484 (cfg->accel_fs << ACCEL_CONFIG0_ACCEL_UI_FS_SEL_POS)); in icm42670_turn_on_sensor()
485 data->accel_fs = in icm42670_turn_on_sensor()
486 convert_bitfield_to_acc_fs((cfg->accel_fs << ACCEL_CONFIG0_ACCEL_UI_FS_SEL_POS)); in icm42670_turn_on_sensor()
487 if ((err < 0) || (data->accel_fs == 0)) { in icm42670_turn_on_sensor()
489 return -EIO; in icm42670_turn_on_sensor()
491 LOG_DBG("Set accel full scale to: %d G", data->accel_fs); in icm42670_turn_on_sensor()
493 err = inv_imu_set_gyro_fsr(&data->driver, in icm42670_turn_on_sensor()
494 (cfg->gyro_fs << GYRO_CONFIG0_GYRO_UI_FS_SEL_POS)); in icm42670_turn_on_sensor()
495 data->gyro_fs = in icm42670_turn_on_sensor()
496 convert_bitfield_to_gyr_fs((cfg->gyro_fs << GYRO_CONFIG0_GYRO_UI_FS_SEL_POS)); in icm42670_turn_on_sensor()
497 if ((err < 0) || (data->gyro_fs == 0)) { in icm42670_turn_on_sensor()
498 LOG_ERR("Failed to configure gyro FSR"); in icm42670_turn_on_sensor()
499 return -EIO; in icm42670_turn_on_sensor()
501 LOG_DBG("Set gyro full scale to: %d dps", data->gyro_fs); in icm42670_turn_on_sensor()
503 err = inv_imu_set_accel_lp_avg(&data->driver, in icm42670_turn_on_sensor()
504 (cfg->accel_avg << ACCEL_CONFIG1_ACCEL_UI_AVG_POS)); in icm42670_turn_on_sensor()
505 err |= inv_imu_set_accel_ln_bw(&data->driver, in icm42670_turn_on_sensor()
506 (cfg->accel_filt_bw << ACCEL_CONFIG1_ACCEL_UI_FILT_BW_POS)); in icm42670_turn_on_sensor()
507 err |= inv_imu_set_gyro_ln_bw(&data->driver, in icm42670_turn_on_sensor()
508 (cfg->gyro_filt_bw << GYRO_CONFIG1_GYRO_UI_FILT_BW_POS)); in icm42670_turn_on_sensor()
511 return -EIO; in icm42670_turn_on_sensor()
514 if (cfg->accel_hz != 0) { in icm42670_turn_on_sensor()
516 &data->driver, cfg->accel_hz + ICM42670_CONVERT_ENUM_TO_ODR_POS); in icm42670_turn_on_sensor()
517 if ((cfg->accel_pwr_mode == ICM42670_LOW_NOISE_MODE) && in icm42670_turn_on_sensor()
518 (convert_dt_enum_to_freq(cfg->accel_hz) >= 12)) { in icm42670_turn_on_sensor()
519 err |= inv_imu_enable_accel_low_noise_mode(&data->driver); in icm42670_turn_on_sensor()
520 } else if ((cfg->accel_pwr_mode == ICM42670_LOW_POWER_MODE) && in icm42670_turn_on_sensor()
521 (convert_dt_enum_to_freq(cfg->accel_hz) <= 400)) { in icm42670_turn_on_sensor()
522 err |= inv_imu_enable_accel_low_power_mode(&data->driver); in icm42670_turn_on_sensor()
527 if (cfg->gyro_hz != 0) { in icm42670_turn_on_sensor()
528 err |= inv_imu_set_gyro_frequency(&data->driver, in icm42670_turn_on_sensor()
529 cfg->gyro_hz + ICM42670_CONVERT_ENUM_TO_ODR_POS); in icm42670_turn_on_sensor()
530 err |= inv_imu_enable_gyro_low_noise_mode(&data->driver); in icm42670_turn_on_sensor()
535 return -EIO; in icm42670_turn_on_sensor()
538 data->accel_pwr_mode = cfg->accel_pwr_mode; in icm42670_turn_on_sensor()
539 data->accel_hz = convert_dt_enum_to_freq(cfg->accel_hz); in icm42670_turn_on_sensor()
540 data->gyro_hz = convert_dt_enum_to_freq(cfg->gyro_hz); in icm42670_turn_on_sensor()
559 val->val1 = conv_val / 1000000; in icm42670_convert_accel()
560 val->val2 = conv_val % 1000000; in icm42670_convert_accel()
571 val->val1 = conv_val / 1000000; in icm42670_convert_gyro()
572 val->val2 = conv_val % 1000000; in icm42670_convert_gyro()
581 val->val1 = conv_val / 1000000; in icm42670_convert_temp()
582 val->val2 = conv_val % 1000000; in icm42670_convert_temp()
589 struct icm42670_data *data = dev->data; in icm42670_channel_get()
591 const struct icm42670_config *cfg = dev->config; in icm42670_channel_get()
597 icm42670_convert_accel(&val[0], data->accel_x, data->accel_fs); in icm42670_channel_get()
598 icm42670_convert_accel(&val[1], data->accel_y, data->accel_fs); in icm42670_channel_get()
599 icm42670_convert_accel(&val[2], data->accel_z, data->accel_fs); in icm42670_channel_get()
601 icm42670_convert_accel(val, data->accel_x, data->accel_fs); in icm42670_channel_get()
603 icm42670_convert_accel(val, data->accel_y, data->accel_fs); in icm42670_channel_get()
605 icm42670_convert_accel(val, data->accel_z, data->accel_fs); in icm42670_channel_get()
607 icm42670_convert_gyro(&val[0], data->gyro_x, data->gyro_fs); in icm42670_channel_get()
608 icm42670_convert_gyro(&val[1], data->gyro_y, data->gyro_fs); in icm42670_channel_get()
609 icm42670_convert_gyro(&val[2], data->gyro_z, data->gyro_fs); in icm42670_channel_get()
611 icm42670_convert_gyro(val, data->gyro_x, data->gyro_fs); in icm42670_channel_get()
613 icm42670_convert_gyro(val, data->gyro_y, data->gyro_fs); in icm42670_channel_get()
615 icm42670_convert_gyro(val, data->gyro_z, data->gyro_fs); in icm42670_channel_get()
617 icm42670_convert_temp(val, data->temp); in icm42670_channel_get()
620 if (cfg->apex == TDK_APEX_PEDOMETER) { in icm42670_channel_get()
621 val[0].val1 = data->pedometer_cnt; in icm42670_channel_get()
622 val[1].val1 = data->pedometer_activity; in icm42670_channel_get()
623 icm42670_apex_pedometer_cadence_convert(&val[2], data->pedometer_cadence, in icm42670_channel_get()
624 data->dmp_odr_hz); in icm42670_channel_get()
625 } else if (cfg->apex == TDK_APEX_WOM) { in icm42670_channel_get()
626 val[0].val1 = (data->apex_status & ICM42670_APEX_STATUS_MASK_WOM_X) ? 1 : 0; in icm42670_channel_get()
627 val[1].val1 = (data->apex_status & ICM42670_APEX_STATUS_MASK_WOM_Y) ? 1 : 0; in icm42670_channel_get()
628 val[2].val1 = (data->apex_status & ICM42670_APEX_STATUS_MASK_WOM_Z) ? 1 : 0; in icm42670_channel_get()
629 } else if ((cfg->apex == TDK_APEX_TILT) || (cfg->apex == TDK_APEX_SMD)) { in icm42670_channel_get()
630 val[0].val1 = data->apex_status; in icm42670_channel_get()
634 res = -ENOTSUP; in icm42670_channel_get()
645 struct icm42670_data *data = dev->data; in icm42670_fetch_from_fifo()
653 status |= inv_imu_read_reg(&data->driver, INT_STATUS, 1, &int_status); in icm42670_fetch_from_fifo()
663 status |= inv_imu_switch_on_mclk(&data->driver); in icm42670_fetch_from_fifo()
666 status |= inv_imu_get_frame_count(&data->driver, &packet_count); in icm42670_fetch_from_fifo()
670 status |= inv_imu_switch_off_mclk(&data->driver); in icm42670_fetch_from_fifo()
675 status |= inv_imu_read_reg(&data->driver, FIFO_DATA, packet_size * packet_count, in icm42670_fetch_from_fifo()
676 (uint8_t *)&data->driver.fifo_data); in icm42670_fetch_from_fifo()
680 status |= inv_imu_reset_fifo(&data->driver); in icm42670_fetch_from_fifo()
681 status |= inv_imu_switch_off_mclk(&data->driver); in icm42670_fetch_from_fifo()
689 &data->driver, &data->driver.fifo_data[fifo_idx], &event); in icm42670_fetch_from_fifo()
694 status |= inv_imu_reset_fifo(&data->driver); in icm42670_fetch_from_fifo()
695 status |= inv_imu_switch_off_mclk(&data->driver); in icm42670_fetch_from_fifo()
700 data->accel_x = event.accel[0]; in icm42670_fetch_from_fifo()
701 data->accel_y = event.accel[1]; in icm42670_fetch_from_fifo()
702 data->accel_z = event.accel[2]; in icm42670_fetch_from_fifo()
705 data->gyro_x = event.gyro[0]; in icm42670_fetch_from_fifo()
706 data->gyro_y = event.gyro[1]; in icm42670_fetch_from_fifo()
707 data->gyro_z = event.gyro[2]; in icm42670_fetch_from_fifo()
710 data->temp = event.temperature; in icm42670_fetch_from_fifo()
719 status |= inv_imu_switch_off_mclk(&data->driver); in icm42670_fetch_from_fifo()
732 struct icm42670_data *data = dev->data; in icm42670_sample_fetch_accel()
735 int res = inv_imu_read_reg(&data->driver, ACCEL_DATA_X1, ACCEL_DATA_SIZE, buffer); in icm42670_sample_fetch_accel()
741 data->accel_x = (int16_t)sys_get_be16(&buffer[0]); in icm42670_sample_fetch_accel()
742 data->accel_y = (int16_t)sys_get_be16(&buffer[2]); in icm42670_sample_fetch_accel()
743 data->accel_z = (int16_t)sys_get_be16(&buffer[4]); in icm42670_sample_fetch_accel()
750 struct icm42670_data *data = dev->data; in icm42670_sample_fetch_gyro()
753 int res = inv_imu_read_reg(&data->driver, GYRO_DATA_X1, GYRO_DATA_SIZE, buffer); in icm42670_sample_fetch_gyro()
759 data->gyro_x = (int16_t)sys_get_be16(&buffer[0]); in icm42670_sample_fetch_gyro()
760 data->gyro_y = (int16_t)sys_get_be16(&buffer[2]); in icm42670_sample_fetch_gyro()
761 data->gyro_z = (int16_t)sys_get_be16(&buffer[4]); in icm42670_sample_fetch_gyro()
768 struct icm42670_data *data = dev->data; in icm42670_sample_fetch_temp()
771 int res = inv_imu_read_reg(&data->driver, TEMP_DATA1, TEMP_DATA_SIZE, buffer); in icm42670_sample_fetch_temp()
777 data->temp = (int16_t)sys_get_be16(&buffer[0]); in icm42670_sample_fetch_temp()
784 struct icm42670_data *data = dev->data; in icm42670_fetch_from_registers()
793 int err = inv_imu_read_reg(&data->driver, INT_STATUS_DRDY, 1, &int_status); in icm42670_fetch_from_registers()
818 res = -ENOTSUP; in icm42670_fetch_from_registers()
826 res = -EIO; in icm42670_fetch_from_registers()
834 int status = -ENOTSUP; in icm42670_sample_fetch()
863 struct icm42670_data *drv_data = dev->data; in icm42670_attr_set()
872 if (val->val1 == TDK_APEX_PEDOMETER) { in icm42670_attr_set()
873 icm42670_apex_enable(&drv_data->driver); in icm42670_attr_set()
874 icm42670_apex_enable_pedometer(dev, &drv_data->driver); in icm42670_attr_set()
875 } else if (val->val1 == TDK_APEX_TILT) { in icm42670_attr_set()
876 icm42670_apex_enable(&drv_data->driver); in icm42670_attr_set()
877 icm42670_apex_enable_tilt(&drv_data->driver); in icm42670_attr_set()
878 } else if (val->val1 == TDK_APEX_SMD) { in icm42670_attr_set()
879 icm42670_apex_enable(&drv_data->driver); in icm42670_attr_set()
880 icm42670_apex_enable_smd(&drv_data->driver); in icm42670_attr_set()
881 } else if (val->val1 == TDK_APEX_WOM) { in icm42670_attr_set()
882 icm42670_apex_enable_wom(&drv_data->driver); in icm42670_attr_set()
889 return -EINVAL; in icm42670_attr_set()
902 return -EINVAL; in icm42670_attr_set()
913 const struct icm42670_data *data = dev->data; in icm42670_attr_get()
914 const struct icm42670_config *cfg = dev->config; in icm42670_attr_get()
927 val->val1 = data->accel_hz; in icm42670_attr_get()
929 val->val1 = data->accel_fs; in icm42670_attr_get()
932 res = -EINVAL; in icm42670_attr_get()
941 val->val1 = data->gyro_hz; in icm42670_attr_get()
943 val->val1 = data->gyro_fs; in icm42670_attr_get()
946 res = -EINVAL; in icm42670_attr_get()
952 val->val1 = cfg->apex; in icm42670_attr_get()
958 res = -EINVAL; in icm42670_attr_get()
969 const struct icm42670_config *cfg = dev->config; in icm42670_bus_check()
971 return cfg->bus_io->check(&cfg->bus); in icm42670_bus_check()
976 struct icm42670_data *data = dev->data; in icm42670_init()
981 return -ENODEV; in icm42670_init()
984 data->accel_x = 0; in icm42670_init()
985 data->accel_y = 0; in icm42670_init()
986 data->accel_z = 0; in icm42670_init()
987 data->gyro_x = 0; in icm42670_init()
988 data->gyro_y = 0; in icm42670_init()
989 data->gyro_z = 0; in icm42670_init()
990 data->temp = 0; in icm42670_init()
994 return -EIO; in icm42670_init()
1065 * Main instantiation macro, which selects the correct bus-specific