Lines Matching +full:6 +full:- +full:axis
1 // SPDX-License-Identifier: GPL-2.0
3 * MEMSensing digital 3-Axis accelerometer
5 * MSA311 is a tri-axial, low-g accelerometer with I2C digital output for
6 * sensitivity consumer applications. It has dynamic user-selectable full
7 * scales range of +-2g/+-4g/+-8g/+-16g and allows acceleration measurements
11 * and is guaranteed to operate over -40C to +85C.
14 * - IIO interface
15 * - Different power modes: NORMAL, SUSPEND
16 * - ODR (Output Data Rate) selection
17 * - Scale selection
18 * - IIO triggered buffer
19 * - NEW_DATA interrupt + trigger
22 * - Motion Events: ACTIVE, TAP, ORIENT, FREEFALL
23 * - Low Power mode
133 [F_ORIENT_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 6, 6),
142 [F_TAP_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 6, 6),
150 [F_ORIENT_Z] = REG_FIELD(MSA311_ORIENT_STS_REG, 6, 6),
156 [F_Y_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 6, 6),
160 [F_PWR_MODE] = REG_FIELD(MSA311_PWR_MODE_REG, 6, 7),
168 [F_ORIENT_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 6, 6),
178 [F_INT1_ORIENT] = REG_FIELD(MSA311_INT_MAP_0_REG, 6, 6),
198 [F_TAP_SHOCK] = REG_FIELD(MSA311_TAP_DUR_REG, 6, 6),
203 [F_ORIENT_HYST] = REG_FIELD(MSA311_ORIENT_HY_REG, 4, 6),
215 * Axis data is 12-bit signed value, so
340 GENMASK(_field->msb, _field->lsb); \
344 * struct msa311_priv - MSA311 internal private state
351 * @chip_name: Chip name in the format "msa311-%02x" % partid
373 #define MSA311_ACCEL_CHANNEL(axis) { \ argument
376 .channel2 = IIO_MOD_##axis, \
382 .scan_index = MSA311_SI_##axis, \
390 .datasheet_name = "ACC_"#axis, \
401 * msa311_get_odr() - Read Output Data Rate (ODR) value from MSA311 accel
405 * This function should be called under msa311->lock.
407 * Return: 0 on success, -ERRNO in other failures
413 err = regmap_field_read(msa311->fields[F_ODR], odr); in msa311_get_odr()
419 * ODR can be equal to 1010-1111 for 1000Hz, but function returns 1010 in msa311_get_odr()
429 * msa311_set_odr() - Setup Output Data Rate (ODR) value for MSA311 accel
433 * This function should be called under msa311->lock. Possible ODR values:
434 * - 1Hz (not available in normal mode)
435 * - 1.95Hz (not available in normal mode)
436 * - 3.9Hz
437 * - 7.81Hz
438 * - 15.63Hz
439 * - 31.25Hz
440 * - 62.5Hz
441 * - 125Hz
442 * - 250Hz
443 * - 500Hz
444 * - 1000Hz
446 * Return: 0 on success, -EINVAL for bad ODR value in the certain power mode,
447 * -ERRNO in other failures
451 struct device *dev = msa311->dev; in msa311_set_odr()
456 err = regmap_field_read(msa311->fields[F_PWR_MODE], &pwr_mode); in msa311_set_odr()
472 return -EINVAL; in msa311_set_odr()
475 return regmap_field_write(msa311->fields[F_ODR], odr); in msa311_set_odr()
479 * msa311_wait_for_next_data() - Wait next accel data available after resume
482 * Return: 0 on success, -EINTR if msleep() was interrupted,
483 * -ERRNO in other failures
488 struct device *dev = msa311->dev; in msa311_wait_for_next_data()
516 return -EINTR; in msa311_wait_for_next_data()
522 * msa311_set_pwr_mode() - Install certain MSA311 power mode
526 * This function should be called under msa311->lock.
528 * Return: 0 on success, -ERRNO on failure
532 struct device *dev = msa311->dev; in msa311_set_pwr_mode()
537 return -EINVAL; in msa311_set_pwr_mode()
541 err = regmap_field_read(msa311->fields[F_PWR_MODE], &prev_mode); in msa311_set_pwr_mode()
545 err = regmap_field_write(msa311->fields[F_PWR_MODE], mode); in msa311_set_pwr_mode()
558 * msa311_get_axis() - Read MSA311 accel data for certain IIO channel axis spec
561 * @axis: Output accel axis data for requested IIO channel spec
563 * This function should be called under msa311->lock.
565 * Return: 0 on success, -EINVAL for unknown IIO channel specification,
566 * -ERRNO in other failures
570 __le16 *axis) in msa311_get_axis() argument
572 struct device *dev = msa311->dev; in msa311_get_axis()
575 if (chan->scan_index < MSA311_SI_X || chan->scan_index > MSA311_SI_Z) { in msa311_get_axis()
577 chan->scan_index); in msa311_get_axis()
578 return -EINVAL; in msa311_get_axis()
581 /* Axes data layout has 2 byte gap for each axis starting from X axis */ in msa311_get_axis()
582 axis_reg = MSA311_ACC_X_REG + (chan->scan_index << 1); in msa311_get_axis()
584 return regmap_bulk_read(msa311->regs, axis_reg, axis, sizeof(*axis)); in msa311_get_axis()
592 struct device *dev = msa311->dev; in msa311_read_raw_data()
593 __le16 axis; in msa311_read_raw_data() local
604 mutex_lock(&msa311->lock); in msa311_read_raw_data()
605 err = msa311_get_axis(msa311, chan, &axis); in msa311_read_raw_data()
606 mutex_unlock(&msa311->lock); in msa311_read_raw_data()
614 dev_err(dev, "can't get axis %s (%pe)\n", in msa311_read_raw_data()
615 chan->datasheet_name, ERR_PTR(err)); in msa311_read_raw_data()
620 * Axis data format is: in msa311_read_raw_data()
623 *val = sign_extend32(le16_to_cpu(axis) >> chan->scan_type.shift, in msa311_read_raw_data()
624 chan->scan_type.realbits - 1); in msa311_read_raw_data()
632 struct device *dev = msa311->dev; in msa311_read_scale()
636 mutex_lock(&msa311->lock); in msa311_read_scale()
637 err = regmap_field_read(msa311->fields[F_FS], &fs); in msa311_read_scale()
638 mutex_unlock(&msa311->lock); in msa311_read_scale()
654 struct device *dev = msa311->dev; in msa311_read_samp_freq()
658 mutex_lock(&msa311->lock); in msa311_read_samp_freq()
660 mutex_unlock(&msa311->lock); in msa311_read_samp_freq()
688 return -EINVAL; in msa311_read_raw()
713 return -EINVAL; in msa311_read_avail()
720 struct device *dev = msa311->dev; in msa311_write_scale()
732 err = -EINVAL; in msa311_write_scale()
736 mutex_lock(&msa311->lock); in msa311_write_scale()
737 err = regmap_field_write(msa311->fields[F_FS], fs); in msa311_write_scale()
738 mutex_unlock(&msa311->lock); in msa311_write_scale()
754 struct device *dev = msa311->dev; in msa311_write_samp_freq()
771 err = -EINVAL; in msa311_write_samp_freq()
775 mutex_lock(&msa311->lock); in msa311_write_samp_freq()
777 mutex_unlock(&msa311->lock); in msa311_write_samp_freq()
804 return -EINVAL; in msa311_write_raw()
813 struct device *dev = msa311->dev; in msa311_debugfs_reg_access()
816 if (reg > regmap_get_max_register(msa311->regs)) in msa311_debugfs_reg_access()
817 return -EINVAL; in msa311_debugfs_reg_access()
823 mutex_lock(&msa311->lock); in msa311_debugfs_reg_access()
826 err = regmap_read(msa311->regs, reg, readval); in msa311_debugfs_reg_access()
828 err = regmap_write(msa311->regs, reg, writeval); in msa311_debugfs_reg_access()
830 mutex_unlock(&msa311->lock); in msa311_debugfs_reg_access()
845 struct device *dev = msa311->dev; in msa311_buffer_preenable()
853 struct device *dev = msa311->dev; in msa311_buffer_postdisable()
865 struct device *dev = msa311->dev; in msa311_set_new_data_trig_state()
868 mutex_lock(&msa311->lock); in msa311_set_new_data_trig_state()
869 err = regmap_field_write(msa311->fields[F_NEW_DATA_INT_EN], state); in msa311_set_new_data_trig_state()
870 mutex_unlock(&msa311->lock); in msa311_set_new_data_trig_state()
882 return iio_trigger_get_drvdata(trig) == indio_dev ? 0 : -EINVAL; in msa311_validate_device()
888 struct msa311_priv *msa311 = iio_priv(pf->indio_dev); in msa311_buffer_thread()
889 struct iio_dev *indio_dev = pf->indio_dev; in msa311_buffer_thread()
891 struct device *dev = msa311->dev; in msa311_buffer_thread()
893 __le16 axis; in msa311_buffer_thread() local
901 mutex_lock(&msa311->lock); in msa311_buffer_thread()
903 for_each_set_bit(bit, indio_dev->active_scan_mask, in msa311_buffer_thread()
904 indio_dev->masklength) { in msa311_buffer_thread()
907 err = msa311_get_axis(msa311, chan, &axis); in msa311_buffer_thread()
909 mutex_unlock(&msa311->lock); in msa311_buffer_thread()
910 dev_err(dev, "can't get axis %s (%pe)\n", in msa311_buffer_thread()
911 chan->datasheet_name, ERR_PTR(err)); in msa311_buffer_thread()
915 buf.channels[i++] = axis; in msa311_buffer_thread()
918 mutex_unlock(&msa311->lock); in msa311_buffer_thread()
924 iio_trigger_notify_done(indio_dev->trig); in msa311_buffer_thread()
933 struct device *dev = msa311->dev; in msa311_irq_thread()
936 mutex_lock(&msa311->lock); in msa311_irq_thread()
943 err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN], in msa311_irq_thread()
946 mutex_unlock(&msa311->lock); in msa311_irq_thread()
954 iio_trigger_poll_nested(msa311->new_data_trig); in msa311_irq_thread()
978 struct device *dev = msa311->dev; in msa311_check_partid()
982 err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid); in msa311_check_partid()
990 msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL, in msa311_check_partid()
991 "msa311-%02x", partid); in msa311_check_partid()
992 if (!msa311->chip_name) in msa311_check_partid()
993 return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n"); in msa311_check_partid()
1000 struct device *dev = msa311->dev; in msa311_soft_reset()
1003 err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG, in msa311_soft_reset()
1014 struct device *dev = msa311->dev; in msa311_chip_init()
1018 err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G); in msa311_chip_init()
1023 err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG, in msa311_chip_init()
1030 err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG, in msa311_chip_init()
1037 err = regmap_update_bits(msa311->regs, MSA311_ODR_REG, in msa311_chip_init()
1054 struct device *dev = msa311->dev; in msa311_setup_interrupts()
1061 if (i2c->irq <= 0) in msa311_setup_interrupts()
1064 err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, in msa311_setup_interrupts()
1066 msa311->chip_name, indio_dev); in msa311_setup_interrupts()
1070 trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name); in msa311_setup_interrupts()
1072 return dev_err_probe(dev, -ENOMEM, in msa311_setup_interrupts()
1075 msa311->new_data_trig = trig; in msa311_setup_interrupts()
1076 msa311->new_data_trig->ops = &msa311_new_data_trig_ops; in msa311_setup_interrupts()
1077 iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev); in msa311_setup_interrupts()
1079 err = devm_iio_trigger_register(dev, msa311->new_data_trig); in msa311_setup_interrupts()
1084 err = regmap_field_write(msa311->fields[F_INT1_OD], in msa311_setup_interrupts()
1088 "can't enable push-pull interrupt\n"); in msa311_setup_interrupts()
1090 err = regmap_field_write(msa311->fields[F_INT1_LVL], in msa311_setup_interrupts()
1096 err = regmap_field_write(msa311->fields[F_LATCH_INT], in msa311_setup_interrupts()
1102 err = regmap_field_write(msa311->fields[F_RESET_INT], 1); in msa311_setup_interrupts()
1107 err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1); in msa311_setup_interrupts()
1117 struct regmap_field **fields = msa311->fields; in msa311_regmap_init()
1118 struct device *dev = msa311->dev; in msa311_regmap_init()
1128 msa311->regs = regmap; in msa311_regmap_init()
1132 msa311->regs, in msa311_regmap_init()
1134 if (IS_ERR(msa311->fields[i])) in msa311_regmap_init()
1135 return dev_err_probe(dev, PTR_ERR(msa311->fields[i]), in msa311_regmap_init()
1149 struct device *dev = &i2c->dev; in msa311_probe()
1156 return dev_err_probe(dev, -ENOMEM, in msa311_probe()
1160 msa311->dev = dev; in msa311_probe()
1167 mutex_init(&msa311->lock); in msa311_probe()
1216 indio_dev->modes = INDIO_DIRECT_MODE; in msa311_probe()
1217 indio_dev->channels = msa311_channels; in msa311_probe()
1218 indio_dev->num_channels = ARRAY_SIZE(msa311_channels); in msa311_probe()
1219 indio_dev->name = msa311->chip_name; in msa311_probe()
1220 indio_dev->info = &msa311_info; in msa311_probe()
1250 mutex_lock(&msa311->lock); in msa311_runtime_suspend()
1252 mutex_unlock(&msa311->lock); in msa311_runtime_suspend()
1266 mutex_lock(&msa311->lock); in msa311_runtime_resume()
1268 mutex_unlock(&msa311->lock); in msa311_runtime_resume()
1303 MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");