Lines Matching +full:motion +full:- +full:sensors

1 // SPDX-License-Identifier: GPL-2.0-only
3 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
44 /* Temp read untested - the e05 doesn't have the sensor */
62 * In motion detection mode the accelerations are band pass filtered
63 * (approx 1 - 25Hz) and then a programmable threshold used to trigger
82 /* Only available for SCA3000-D03 and SCA3000-D01 */
104 * Control which motion detector interrupts are on.
138 * Bypass - Bypass the low-pass filter in the signal channel so as to increase
141 * Narrow - Narrow low-pass filtering of the signal channel and half output
144 * Wide - Widen low-pass filtering of signal channel to increase bandwidth
153 * struct sca3000_state - device instance state information
157 * @mo_det_use_count: reference counter for the motion detection unit
160 * @tx: dma-able transmit buffer
161 * @rx: dma-able receive buffer
175 * struct sca3000_chip_info - model dependent parameters
176 * @scale: scale * 10^-6
177 * @temp_output: some devices have temperature sensors.
190 * for motion detection in the x and z axis.
192 * for motion detection in the y axis.
279 st->tx[0] = SCA3000_WRITE_REG(address); in sca3000_write_reg()
280 st->tx[1] = val; in sca3000_write_reg()
281 return spi_write(st->us, st->tx, 2); in sca3000_write_reg()
291 .tx_buf = st->tx, in sca3000_read_data_short()
294 .rx_buf = st->rx, in sca3000_read_data_short()
297 st->tx[0] = SCA3000_READ_REG(reg_address_high); in sca3000_read_data_short()
299 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); in sca3000_read_data_short()
303 * sca3000_reg_lock_on() - test if the ctrl register lock is on
316 return !(st->rx[0] & SCA3000_LOCKED); in sca3000_reg_lock_on()
320 * __sca3000_unlock_reg_lock() - unlock the control registers
333 .tx_buf = st->tx, in __sca3000_unlock_reg_lock()
337 .tx_buf = st->tx + 2, in __sca3000_unlock_reg_lock()
340 .tx_buf = st->tx + 4, in __sca3000_unlock_reg_lock()
343 st->tx[0] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); in __sca3000_unlock_reg_lock()
344 st->tx[1] = 0x00; in __sca3000_unlock_reg_lock()
345 st->tx[2] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); in __sca3000_unlock_reg_lock()
346 st->tx[3] = 0x50; in __sca3000_unlock_reg_lock()
347 st->tx[4] = SCA3000_WRITE_REG(SCA3000_REG_UNLOCK_ADDR); in __sca3000_unlock_reg_lock()
348 st->tx[5] = 0xA0; in __sca3000_unlock_reg_lock()
350 return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); in __sca3000_unlock_reg_lock()
354 * sca3000_write_ctrl_reg() - write to a lock protect ctrl register
392 * sca3000_read_ctrl_reg() - read from lock protected control register.
418 return st->rx[0]; in sca3000_read_ctrl_reg()
424 * sca3000_print_rev() - sysfs interface to read the chip revision number
434 mutex_lock(&st->lock); in sca3000_print_rev()
438 dev_info(&indio_dev->dev, in sca3000_print_rev()
440 st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK, in sca3000_print_rev()
441 st->rx[0] & SCA3000_REG_REVID_MINOR_MASK); in sca3000_print_rev()
443 mutex_unlock(&st->lock); in sca3000_print_rev()
457 len = sprintf(buf, "%d", st->info->measurement_mode_3db_freq); in sca3000_show_available_3db_freqs()
458 if (st->info->option_mode_1) in sca3000_show_available_3db_freqs()
460 st->info->option_mode_1_3db_freq); in sca3000_show_available_3db_freqs()
461 if (st->info->option_mode_2) in sca3000_show_available_3db_freqs()
463 st->info->option_mode_2_3db_freq); in sca3000_show_available_3db_freqs()
520 .scan_index = -1, /* Fake channel */
536 .scan_index = -1,
542 .scan_index = -1, /* Fake channel */
558 * __sca3000_get_base_freq() - obtain mode specific base frequency
574 switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) { in __sca3000_get_base_freq()
576 *base_freq = info->measurement_mode_freq; in __sca3000_get_base_freq()
579 *base_freq = info->option_mode_1_freq; in __sca3000_get_base_freq()
582 *base_freq = info->option_mode_2_freq; in __sca3000_get_base_freq()
585 ret = -EINVAL; in __sca3000_get_base_freq()
592 * sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
602 ret = __sca3000_get_base_freq(st, st->info, val); in sca3000_read_raw_samp_freq()
626 * sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
636 ret = __sca3000_get_base_freq(st, st->info, &base_freq); in sca3000_write_raw_samp_freq()
651 return -EINVAL; in sca3000_write_raw_samp_freq()
665 /* mask bottom 2 bits - only ones that are relevant */ in sca3000_read_3db_freq()
666 st->rx[0] &= SCA3000_REG_MODE_MODE_MASK; in sca3000_read_3db_freq()
667 switch (st->rx[0]) { in sca3000_read_3db_freq()
669 *val = st->info->measurement_mode_3db_freq; in sca3000_read_3db_freq()
672 return -EBUSY; in sca3000_read_3db_freq()
674 *val = st->info->option_mode_1_3db_freq; in sca3000_read_3db_freq()
677 *val = st->info->option_mode_2_3db_freq; in sca3000_read_3db_freq()
680 return -EINVAL; in sca3000_read_3db_freq()
689 if (val == st->info->measurement_mode_3db_freq) in sca3000_write_3db_freq()
691 else if (st->info->option_mode_1 && in sca3000_write_3db_freq()
692 (val == st->info->option_mode_1_3db_freq)) in sca3000_write_3db_freq()
694 else if (st->info->option_mode_2 && in sca3000_write_3db_freq()
695 (val == st->info->option_mode_2_3db_freq)) in sca3000_write_3db_freq()
698 return -EINVAL; in sca3000_write_3db_freq()
703 st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK; in sca3000_write_3db_freq()
704 st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK); in sca3000_write_3db_freq()
706 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]); in sca3000_write_3db_freq()
721 mutex_lock(&st->lock); in sca3000_read_raw()
722 if (chan->type == IIO_ACCEL) { in sca3000_read_raw()
723 if (st->mo_det_use_count) { in sca3000_read_raw()
724 mutex_unlock(&st->lock); in sca3000_read_raw()
725 return -EBUSY; in sca3000_read_raw()
727 address = sca3000_addresses[chan->address][0]; in sca3000_read_raw()
730 mutex_unlock(&st->lock); in sca3000_read_raw()
733 *val = (be16_to_cpup((__be16 *)st->rx) >> 3) & 0x1FFF; in sca3000_read_raw()
734 *val = ((*val) << (sizeof(*val) * 8 - 13)) >> in sca3000_read_raw()
735 (sizeof(*val) * 8 - 13); in sca3000_read_raw()
742 mutex_unlock(&st->lock); in sca3000_read_raw()
745 *val = ((st->rx[0] & 0x3F) << 3) | in sca3000_read_raw()
746 ((st->rx[1] & 0xE0) >> 5); in sca3000_read_raw()
748 mutex_unlock(&st->lock); in sca3000_read_raw()
752 if (chan->type == IIO_ACCEL) in sca3000_read_raw()
753 *val2 = st->info->scale; in sca3000_read_raw()
758 *val = -214; in sca3000_read_raw()
762 mutex_lock(&st->lock); in sca3000_read_raw()
764 mutex_unlock(&st->lock); in sca3000_read_raw()
767 mutex_lock(&st->lock); in sca3000_read_raw()
769 mutex_unlock(&st->lock); in sca3000_read_raw()
772 return -EINVAL; in sca3000_read_raw()
786 return -EINVAL; in sca3000_write_raw()
787 mutex_lock(&st->lock); in sca3000_write_raw()
789 mutex_unlock(&st->lock); in sca3000_write_raw()
793 return -EINVAL; in sca3000_write_raw()
794 mutex_lock(&st->lock); in sca3000_write_raw()
796 mutex_unlock(&st->lock); in sca3000_write_raw()
799 return -EINVAL; in sca3000_write_raw()
806 * sca3000_read_av_freq() - sysfs function to get available frequencies
811 * The later modes are only relevant to the ring buffer - and depend on current
824 mutex_lock(&st->lock); in sca3000_read_av_freq()
826 val = st->rx[0]; in sca3000_read_av_freq()
827 mutex_unlock(&st->lock); in sca3000_read_av_freq()
834 st->info->measurement_mode_freq, in sca3000_read_av_freq()
835 st->info->measurement_mode_freq / 2, in sca3000_read_av_freq()
836 st->info->measurement_mode_freq / 4); in sca3000_read_av_freq()
840 st->info->option_mode_1_freq, in sca3000_read_av_freq()
841 st->info->option_mode_1_freq / 2, in sca3000_read_av_freq()
842 st->info->option_mode_1_freq / 4); in sca3000_read_av_freq()
846 st->info->option_mode_2_freq, in sca3000_read_av_freq()
847 st->info->option_mode_2_freq / 2, in sca3000_read_av_freq()
848 st->info->option_mode_2_freq / 4); in sca3000_read_av_freq()
863 * sca3000_read_event_value() - query of a threshold or period
878 mutex_lock(&st->lock); in sca3000_read_event_value()
880 sca3000_addresses[chan->address][1]); in sca3000_read_event_value()
881 mutex_unlock(&st->lock); in sca3000_read_event_value()
885 if (chan->channel2 == IIO_MOD_Y) in sca3000_read_event_value()
887 ARRAY_SIZE(st->info->mot_det_mult_y)) in sca3000_read_event_value()
888 *val += st->info->mot_det_mult_y[i]; in sca3000_read_event_value()
891 ARRAY_SIZE(st->info->mot_det_mult_xz)) in sca3000_read_event_value()
892 *val += st->info->mot_det_mult_xz[i]; in sca3000_read_event_value()
900 return -EINVAL; in sca3000_read_event_value()
905 * sca3000_write_event_value() - control of threshold and period
929 if (chan->channel2 == IIO_MOD_Y) { in sca3000_write_event_value()
930 i = ARRAY_SIZE(st->info->mot_det_mult_y); in sca3000_write_event_value()
932 if (val >= st->info->mot_det_mult_y[--i]) { in sca3000_write_event_value()
934 val -= st->info->mot_det_mult_y[i]; in sca3000_write_event_value()
937 i = ARRAY_SIZE(st->info->mot_det_mult_xz); in sca3000_write_event_value()
939 if (val >= st->info->mot_det_mult_xz[--i]) { in sca3000_write_event_value()
941 val -= st->info->mot_det_mult_xz[i]; in sca3000_write_event_value()
945 mutex_lock(&st->lock); in sca3000_write_event_value()
947 sca3000_addresses[chan->address][1], in sca3000_write_event_value()
949 mutex_unlock(&st->lock); in sca3000_write_event_value()
973 .tx_buf = st->tx, in sca3000_read_data()
980 st->tx[0] = SCA3000_READ_REG(reg_address_high); in sca3000_read_data()
981 ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); in sca3000_read_data()
983 dev_err(&st->us->dev, "problem reading register\n"); in sca3000_read_data()
991 * sca3000_ring_int_process() - ring specific interrupt handling.
1000 mutex_lock(&st->lock); in sca3000_ring_int_process()
1007 num_available = st->rx[0]; in sca3000_ring_int_process()
1012 ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx, in sca3000_ring_int_process()
1024 iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2); in sca3000_ring_int_process()
1028 mutex_unlock(&st->lock); in sca3000_ring_int_process()
1032 * sca3000_event_handler() - handling ring and non ring events
1054 mutex_lock(&st->lock); in sca3000_event_handler()
1056 val = st->rx[0]; in sca3000_event_handler()
1057 mutex_unlock(&st->lock); in sca3000_event_handler()
1114 mutex_lock(&st->lock); in sca3000_read_event_config()
1120 switch (chan->channel2) { in sca3000_read_event_config()
1122 ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_read_event_config()
1128 * Motion detection mode cannot run at the same time as in sca3000_read_event_config()
1131 if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_read_event_config()
1140 ret = !!(ret & sca3000_addresses[chan->address][2]); in sca3000_read_event_config()
1144 ret = -EINVAL; in sca3000_read_event_config()
1148 mutex_unlock(&st->lock); in sca3000_read_event_config()
1164 if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1166 st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1168 else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1170 st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1182 * First read the motion detector config to find out if in sca3000_motion_detect_set_state()
1197 st->mo_det_use_count++; in sca3000_motion_detect_set_state()
1205 st->mo_det_use_count--; in sca3000_motion_detect_set_state()
1213 if ((st->mo_det_use_count) && in sca3000_motion_detect_set_state()
1214 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1217 (st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1220 else if (!(st->mo_det_use_count) && in sca3000_motion_detect_set_state()
1221 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1224 st->rx[0] & SCA3000_REG_MODE_MODE_MASK); in sca3000_motion_detect_set_state()
1230 * sca3000_write_event_config() - simple on off control for motion detector
1239 * motion detector unit being enabled.
1240 * N.B. enabling motion detector stops normal data acquisition.
1253 mutex_lock(&st->lock); in sca3000_write_event_config()
1254 switch (chan->channel2) { in sca3000_write_event_config()
1263 chan->address, in sca3000_write_event_config()
1267 ret = -EINVAL; in sca3000_write_event_config()
1270 mutex_unlock(&st->lock); in sca3000_write_event_config()
1281 mutex_lock(&st->lock); in __sca3000_hw_ring_state_set()
1286 dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n"); in __sca3000_hw_ring_state_set()
1289 (st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1293 (st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1295 mutex_unlock(&st->lock); in __sca3000_hw_ring_state_set()
1301 * sca3000_hw_ring_preenable() - hw ring buffer preenable function
1314 mutex_lock(&st->lock); in sca3000_hw_ring_preenable()
1322 st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_preenable()
1326 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1331 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1346 mutex_lock(&st->lock); in sca3000_hw_ring_postdisable()
1353 st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_postdisable()
1355 mutex_unlock(&st->lock); in sca3000_hw_ring_postdisable()
1365 * sca3000_clean_setup() - get the device into a predictable state
1376 mutex_lock(&st->lock); in sca3000_clean_setup()
1382 /* Turn off all motion detection channels */ in sca3000_clean_setup()
1415 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 in sca3000_clean_setup()
1422 (st->rx[0] & SCA3000_MODE_PROT_MASK)); in sca3000_clean_setup()
1425 mutex_unlock(&st->lock); in sca3000_clean_setup()
1445 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in sca3000_probe()
1447 return -ENOMEM; in sca3000_probe()
1451 st->us = spi; in sca3000_probe()
1452 mutex_init(&st->lock); in sca3000_probe()
1453 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) in sca3000_probe()
1454 ->driver_data]; in sca3000_probe()
1456 indio_dev->name = spi_get_device_id(spi)->name; in sca3000_probe()
1457 indio_dev->info = &sca3000_info; in sca3000_probe()
1458 if (st->info->temp_output) { in sca3000_probe()
1459 indio_dev->channels = sca3000_channels_with_temp; in sca3000_probe()
1460 indio_dev->num_channels = in sca3000_probe()
1463 indio_dev->channels = sca3000_channels; in sca3000_probe()
1464 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); in sca3000_probe()
1466 indio_dev->modes = INDIO_DIRECT_MODE; in sca3000_probe()
1468 ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev, in sca3000_probe()
1474 if (spi->irq) { in sca3000_probe()
1475 ret = request_threaded_irq(spi->irq, in sca3000_probe()
1495 if (spi->irq) in sca3000_probe()
1496 free_irq(spi->irq, indio_dev); in sca3000_probe()
1505 mutex_lock(&st->lock); in sca3000_stop_all_interrupts()
1510 (st->rx[0] & in sca3000_stop_all_interrupts()
1515 mutex_unlock(&st->lock); in sca3000_stop_all_interrupts()
1528 if (spi->irq) in sca3000_remove()
1529 free_irq(spi->irq, indio_dev); in sca3000_remove()