Lines Matching +full:threshold +full:- +full:detector
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 */
54 * is below a threshold for equivalent of 25cm drop
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
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
189 * @mot_det_mult_xz: Bit wise multipliers to calculate the threshold
191 * @mot_det_mult_y: Bit wise multipliers to calculate the threshold
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,
549 .scan_index = -1, /* Fake channel */
565 * __sca3000_get_base_freq() - obtain mode specific base frequency
581 switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) { in __sca3000_get_base_freq()
583 *base_freq = info->measurement_mode_freq; in __sca3000_get_base_freq()
586 *base_freq = info->option_mode_1_freq; in __sca3000_get_base_freq()
589 *base_freq = info->option_mode_2_freq; in __sca3000_get_base_freq()
592 ret = -EINVAL; in __sca3000_get_base_freq()
599 * sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
609 ret = __sca3000_get_base_freq(st, st->info, val); in sca3000_read_raw_samp_freq()
633 * sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
643 ret = __sca3000_get_base_freq(st, st->info, &base_freq); in sca3000_write_raw_samp_freq()
658 return -EINVAL; in sca3000_write_raw_samp_freq()
672 /* mask bottom 2 bits - only ones that are relevant */ in sca3000_read_3db_freq()
673 st->rx[0] &= SCA3000_REG_MODE_MODE_MASK; in sca3000_read_3db_freq()
674 switch (st->rx[0]) { in sca3000_read_3db_freq()
676 *val = st->info->measurement_mode_3db_freq; in sca3000_read_3db_freq()
679 return -EBUSY; in sca3000_read_3db_freq()
681 *val = st->info->option_mode_1_3db_freq; in sca3000_read_3db_freq()
684 *val = st->info->option_mode_2_3db_freq; in sca3000_read_3db_freq()
687 return -EINVAL; in sca3000_read_3db_freq()
696 if (val == st->info->measurement_mode_3db_freq) in sca3000_write_3db_freq()
698 else if (st->info->option_mode_1 && in sca3000_write_3db_freq()
699 (val == st->info->option_mode_1_3db_freq)) in sca3000_write_3db_freq()
701 else if (st->info->option_mode_2 && in sca3000_write_3db_freq()
702 (val == st->info->option_mode_2_3db_freq)) in sca3000_write_3db_freq()
705 return -EINVAL; in sca3000_write_3db_freq()
710 st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK; in sca3000_write_3db_freq()
711 st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK); in sca3000_write_3db_freq()
713 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]); in sca3000_write_3db_freq()
728 mutex_lock(&st->lock); in sca3000_read_raw()
729 if (chan->type == IIO_ACCEL) { in sca3000_read_raw()
730 if (st->mo_det_use_count) { in sca3000_read_raw()
731 mutex_unlock(&st->lock); in sca3000_read_raw()
732 return -EBUSY; in sca3000_read_raw()
734 address = sca3000_addresses[chan->address][0]; in sca3000_read_raw()
737 mutex_unlock(&st->lock); in sca3000_read_raw()
740 *val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >> in sca3000_read_raw()
741 chan->scan_type.shift, in sca3000_read_raw()
742 chan->scan_type.realbits - 1); in sca3000_read_raw()
749 mutex_unlock(&st->lock); in sca3000_read_raw()
752 *val = (be16_to_cpup((__be16 *)st->rx) >> in sca3000_read_raw()
753 chan->scan_type.shift) & in sca3000_read_raw()
754 GENMASK(chan->scan_type.realbits - 1, 0); in sca3000_read_raw()
756 mutex_unlock(&st->lock); in sca3000_read_raw()
760 if (chan->type == IIO_ACCEL) in sca3000_read_raw()
761 *val2 = st->info->scale; in sca3000_read_raw()
766 *val = -214; in sca3000_read_raw()
770 mutex_lock(&st->lock); in sca3000_read_raw()
772 mutex_unlock(&st->lock); in sca3000_read_raw()
775 mutex_lock(&st->lock); in sca3000_read_raw()
777 mutex_unlock(&st->lock); in sca3000_read_raw()
780 return -EINVAL; in sca3000_read_raw()
794 return -EINVAL; in sca3000_write_raw()
795 mutex_lock(&st->lock); in sca3000_write_raw()
797 mutex_unlock(&st->lock); in sca3000_write_raw()
801 return -EINVAL; in sca3000_write_raw()
802 mutex_lock(&st->lock); in sca3000_write_raw()
804 mutex_unlock(&st->lock); in sca3000_write_raw()
807 return -EINVAL; in sca3000_write_raw()
814 * sca3000_read_av_freq() - sysfs function to get available frequencies
819 * The later modes are only relevant to the ring buffer - and depend on current
832 mutex_lock(&st->lock); in sca3000_read_av_freq()
834 val = st->rx[0]; in sca3000_read_av_freq()
835 mutex_unlock(&st->lock); in sca3000_read_av_freq()
842 st->info->measurement_mode_freq, in sca3000_read_av_freq()
843 st->info->measurement_mode_freq / 2, in sca3000_read_av_freq()
844 st->info->measurement_mode_freq / 4); in sca3000_read_av_freq()
848 st->info->option_mode_1_freq, in sca3000_read_av_freq()
849 st->info->option_mode_1_freq / 2, in sca3000_read_av_freq()
850 st->info->option_mode_1_freq / 4); in sca3000_read_av_freq()
854 st->info->option_mode_2_freq, in sca3000_read_av_freq()
855 st->info->option_mode_2_freq / 2, in sca3000_read_av_freq()
856 st->info->option_mode_2_freq / 4); in sca3000_read_av_freq()
871 * sca3000_read_event_value() - query of a threshold or period
886 mutex_lock(&st->lock); in sca3000_read_event_value()
888 sca3000_addresses[chan->address][1]); in sca3000_read_event_value()
889 mutex_unlock(&st->lock); in sca3000_read_event_value()
893 if (chan->channel2 == IIO_MOD_Y) in sca3000_read_event_value()
895 ARRAY_SIZE(st->info->mot_det_mult_y)) in sca3000_read_event_value()
896 *val += st->info->mot_det_mult_y[i]; in sca3000_read_event_value()
899 ARRAY_SIZE(st->info->mot_det_mult_xz)) in sca3000_read_event_value()
900 *val += st->info->mot_det_mult_xz[i]; in sca3000_read_event_value()
908 return -EINVAL; in sca3000_read_event_value()
913 * sca3000_write_event_value() - control of threshold and period
921 * Here the threshold only.
937 if (chan->channel2 == IIO_MOD_Y) { in sca3000_write_event_value()
938 i = ARRAY_SIZE(st->info->mot_det_mult_y); in sca3000_write_event_value()
940 if (val >= st->info->mot_det_mult_y[--i]) { in sca3000_write_event_value()
942 val -= st->info->mot_det_mult_y[i]; in sca3000_write_event_value()
945 i = ARRAY_SIZE(st->info->mot_det_mult_xz); in sca3000_write_event_value()
947 if (val >= st->info->mot_det_mult_xz[--i]) { in sca3000_write_event_value()
949 val -= st->info->mot_det_mult_xz[i]; in sca3000_write_event_value()
953 mutex_lock(&st->lock); in sca3000_write_event_value()
955 sca3000_addresses[chan->address][1], in sca3000_write_event_value()
957 mutex_unlock(&st->lock); in sca3000_write_event_value()
981 .tx_buf = st->tx, in sca3000_read_data()
988 st->tx[0] = SCA3000_READ_REG(reg_address_high); in sca3000_read_data()
989 ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); in sca3000_read_data()
991 dev_err(&st->us->dev, "problem reading register\n"); in sca3000_read_data()
999 * sca3000_ring_int_process() - ring specific interrupt handling.
1008 mutex_lock(&st->lock); in sca3000_ring_int_process()
1015 num_available = st->rx[0]; in sca3000_ring_int_process()
1020 ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx, in sca3000_ring_int_process()
1032 iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2); in sca3000_ring_int_process()
1036 mutex_unlock(&st->lock); in sca3000_ring_int_process()
1040 * sca3000_event_handler() - handling ring and non ring events
1062 mutex_lock(&st->lock); in sca3000_event_handler()
1064 val = st->rx[0]; in sca3000_event_handler()
1065 mutex_unlock(&st->lock); in sca3000_event_handler()
1122 mutex_lock(&st->lock); in sca3000_read_event_config()
1128 switch (chan->channel2) { in sca3000_read_event_config()
1130 ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_read_event_config()
1139 if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_read_event_config()
1148 ret = !!(ret & sca3000_addresses[chan->address][2]); in sca3000_read_event_config()
1152 ret = -EINVAL; in sca3000_read_event_config()
1156 mutex_unlock(&st->lock); in sca3000_read_event_config()
1172 if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1174 st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1176 else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1178 st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1190 * First read the motion detector config to find out if in sca3000_motion_detect_set_state()
1205 st->mo_det_use_count++; in sca3000_motion_detect_set_state()
1213 st->mo_det_use_count--; in sca3000_motion_detect_set_state()
1221 if ((st->mo_det_use_count) && in sca3000_motion_detect_set_state()
1222 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1225 (st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1228 else if (!(st->mo_det_use_count) && in sca3000_motion_detect_set_state()
1229 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1232 st->rx[0] & SCA3000_REG_MODE_MODE_MASK); in sca3000_motion_detect_set_state()
1238 * sca3000_write_event_config() - simple on off control for motion detector
1247 * motion detector unit being enabled.
1248 * N.B. enabling motion detector stops normal data acquisition.
1261 mutex_lock(&st->lock); in sca3000_write_event_config()
1262 switch (chan->channel2) { in sca3000_write_event_config()
1271 chan->address, in sca3000_write_event_config()
1275 ret = -EINVAL; in sca3000_write_event_config()
1278 mutex_unlock(&st->lock); in sca3000_write_event_config()
1289 mutex_lock(&st->lock); in __sca3000_hw_ring_state_set()
1294 dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n"); in __sca3000_hw_ring_state_set()
1297 (st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1301 (st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1303 mutex_unlock(&st->lock); in __sca3000_hw_ring_state_set()
1309 * sca3000_hw_ring_preenable() - hw ring buffer preenable function
1322 mutex_lock(&st->lock); in sca3000_hw_ring_preenable()
1330 st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_preenable()
1334 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1339 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1354 mutex_lock(&st->lock); in sca3000_hw_ring_postdisable()
1361 st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_postdisable()
1363 mutex_unlock(&st->lock); in sca3000_hw_ring_postdisable()
1373 * sca3000_clean_setup() - get the device into a predictable state
1384 mutex_lock(&st->lock); in sca3000_clean_setup()
1423 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 in sca3000_clean_setup()
1430 (st->rx[0] & SCA3000_MODE_PROT_MASK)); in sca3000_clean_setup()
1433 mutex_unlock(&st->lock); in sca3000_clean_setup()
1453 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in sca3000_probe()
1455 return -ENOMEM; in sca3000_probe()
1459 st->us = spi; in sca3000_probe()
1460 mutex_init(&st->lock); in sca3000_probe()
1461 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) in sca3000_probe()
1462 ->driver_data]; in sca3000_probe()
1464 indio_dev->name = spi_get_device_id(spi)->name; in sca3000_probe()
1465 indio_dev->info = &sca3000_info; in sca3000_probe()
1466 if (st->info->temp_output) { in sca3000_probe()
1467 indio_dev->channels = sca3000_channels_with_temp; in sca3000_probe()
1468 indio_dev->num_channels = in sca3000_probe()
1471 indio_dev->channels = sca3000_channels; in sca3000_probe()
1472 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); in sca3000_probe()
1474 indio_dev->modes = INDIO_DIRECT_MODE; in sca3000_probe()
1476 ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev, in sca3000_probe()
1481 if (spi->irq) { in sca3000_probe()
1482 ret = request_threaded_irq(spi->irq, in sca3000_probe()
1502 if (spi->irq) in sca3000_probe()
1503 free_irq(spi->irq, indio_dev); in sca3000_probe()
1512 mutex_lock(&st->lock); in sca3000_stop_all_interrupts()
1517 (st->rx[0] & in sca3000_stop_all_interrupts()
1522 mutex_unlock(&st->lock); in sca3000_stop_all_interrupts()
1535 if (spi->irq) in sca3000_remove()
1536 free_irq(spi->irq, indio_dev); in sca3000_remove()