Lines Matching +full:3 +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
145 [F_ACTV_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 3, 3),
158 [F_ODR] = REG_FIELD(MSA311_ODR_REG, 0, 3),
163 [F_X_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 3, 3),
176 [F_FREEFALL_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 3, 3),
190 [F_LATCH_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 0, 3),
204 [F_ORIENT_BLOCKING] = REG_FIELD(MSA311_ORIENT_HY_REG, 2, 3),
207 [F_Z_BLOCKING] = REG_FIELD(MSA311_Z_BLOCK_REG, 0, 3),
215 * Axis data is 12-bit signed value, so
254 {1, 0}, {1, 950000}, {3, 900000}, {7, 810000}, {15, 630000},
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()
906 err = msa311_get_axis(msa311, chan, &axis); in msa311_buffer_thread()
908 mutex_unlock(&msa311->lock); in msa311_buffer_thread()
909 dev_err(dev, "can't get axis %s (%pe)\n", in msa311_buffer_thread()
910 chan->datasheet_name, ERR_PTR(err)); in msa311_buffer_thread()
914 buf.channels[i++] = axis; in msa311_buffer_thread()
917 mutex_unlock(&msa311->lock); in msa311_buffer_thread()
923 iio_trigger_notify_done(indio_dev->trig); in msa311_buffer_thread()
932 struct device *dev = msa311->dev; in msa311_irq_thread()
935 mutex_lock(&msa311->lock); in msa311_irq_thread()
942 err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN], in msa311_irq_thread()
945 mutex_unlock(&msa311->lock); in msa311_irq_thread()
953 iio_trigger_poll_nested(msa311->new_data_trig); in msa311_irq_thread()
977 struct device *dev = msa311->dev; in msa311_check_partid()
981 err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid); in msa311_check_partid()
989 msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL, in msa311_check_partid()
990 "msa311-%02x", partid); in msa311_check_partid()
991 if (!msa311->chip_name) in msa311_check_partid()
992 return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n"); in msa311_check_partid()
999 struct device *dev = msa311->dev; in msa311_soft_reset()
1002 err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG, in msa311_soft_reset()
1013 struct device *dev = msa311->dev; in msa311_chip_init()
1017 err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G); in msa311_chip_init()
1022 err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG, in msa311_chip_init()
1029 err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG, in msa311_chip_init()
1036 err = regmap_clear_bits(msa311->regs, MSA311_ODR_REG, in msa311_chip_init()
1053 struct device *dev = msa311->dev; in msa311_setup_interrupts()
1060 if (i2c->irq <= 0) in msa311_setup_interrupts()
1063 err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, in msa311_setup_interrupts()
1065 msa311->chip_name, indio_dev); in msa311_setup_interrupts()
1069 trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name); in msa311_setup_interrupts()
1071 return dev_err_probe(dev, -ENOMEM, in msa311_setup_interrupts()
1074 msa311->new_data_trig = trig; in msa311_setup_interrupts()
1075 msa311->new_data_trig->ops = &msa311_new_data_trig_ops; in msa311_setup_interrupts()
1076 iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev); in msa311_setup_interrupts()
1078 err = devm_iio_trigger_register(dev, msa311->new_data_trig); in msa311_setup_interrupts()
1083 err = regmap_field_write(msa311->fields[F_INT1_OD], in msa311_setup_interrupts()
1087 "can't enable push-pull interrupt\n"); in msa311_setup_interrupts()
1089 err = regmap_field_write(msa311->fields[F_INT1_LVL], in msa311_setup_interrupts()
1095 err = regmap_field_write(msa311->fields[F_LATCH_INT], in msa311_setup_interrupts()
1101 err = regmap_field_write(msa311->fields[F_RESET_INT], 1); in msa311_setup_interrupts()
1106 err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1); in msa311_setup_interrupts()
1116 struct regmap_field **fields = msa311->fields; in msa311_regmap_init()
1117 struct device *dev = msa311->dev; in msa311_regmap_init()
1127 msa311->regs = regmap; in msa311_regmap_init()
1131 msa311->regs, in msa311_regmap_init()
1133 if (IS_ERR(msa311->fields[i])) in msa311_regmap_init()
1134 return dev_err_probe(dev, PTR_ERR(msa311->fields[i]), in msa311_regmap_init()
1148 struct device *dev = &i2c->dev; in msa311_probe()
1155 return dev_err_probe(dev, -ENOMEM, in msa311_probe()
1159 msa311->dev = dev; in msa311_probe()
1166 mutex_init(&msa311->lock); in msa311_probe()
1215 indio_dev->modes = INDIO_DIRECT_MODE; in msa311_probe()
1216 indio_dev->channels = msa311_channels; in msa311_probe()
1217 indio_dev->num_channels = ARRAY_SIZE(msa311_channels); in msa311_probe()
1218 indio_dev->name = msa311->chip_name; in msa311_probe()
1219 indio_dev->info = &msa311_info; in msa311_probe()
1249 mutex_lock(&msa311->lock); in msa311_runtime_suspend()
1251 mutex_unlock(&msa311->lock); in msa311_runtime_suspend()
1265 mutex_lock(&msa311->lock); in msa311_runtime_resume()
1267 mutex_unlock(&msa311->lock); in msa311_runtime_resume()
1302 MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");