Lines Matching +full:rx +full:- +full:device
1 // SPDX-License-Identifier: GPL-2.0-only
3 * sca3000_core.c -- support VTI sca3000 series accelerometers via SPI
12 #include <linux/device.h>
44 /* Temp read untested - the e05 doesn't have the sensor */
63 * (approx 1 - 25Hz) and then a programmable threshold used to trigger
82 /* Only available for SCA3000-D03 and SCA3000-D01 */
89 * BE VERY CAREFUL WITH THIS, IF 3 BITS ARE NOT SET the device
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
154 * @us: the associated spi device
159 * and the underlying device state.
160 * @tx: dma-able transmit buffer
161 * @rx: dma-able receive buffer
170 u8 rx[384] __aligned(IIO_DMA_MINALIGN);
175 * struct sca3000_chip_info - model dependent parameters
176 * @scale: scale * 10^-6
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
304 * @st: Driver specific device instance data.
316 return !(st->rx[0] & SCA3000_LOCKED); in sca3000_reg_lock_on()
320 * __sca3000_unlock_reg_lock() - unlock the control registers
321 * @st: Driver specific device instance data.
323 * Note the device does not appear to support doing this in a single transfer.
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
355 * @st: Driver specific device instance data.
389 * sca3000_read_ctrl_reg() - read from lock protected control register.
390 * @st: Driver specific device instance data.
415 return st->rx[0]; in sca3000_read_ctrl_reg()
419 * sca3000_print_rev() - sysfs interface to read the chip revision number
420 * @indio_dev: Device instance specific generic IIO data.
421 * Driver specific device instance data can be obtained via
429 mutex_lock(&st->lock); in sca3000_print_rev()
433 dev_info(&indio_dev->dev, in sca3000_print_rev()
435 st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK, in sca3000_print_rev()
436 st->rx[0] & SCA3000_REG_REVID_MINOR_MASK); in sca3000_print_rev()
438 mutex_unlock(&st->lock); in sca3000_print_rev()
444 sca3000_show_available_3db_freqs(struct device *dev, in sca3000_show_available_3db_freqs()
452 len = sprintf(buf, "%d", st->info->measurement_mode_3db_freq); in sca3000_show_available_3db_freqs()
453 if (st->info->option_mode_1) in sca3000_show_available_3db_freqs()
455 st->info->option_mode_1_3db_freq); in sca3000_show_available_3db_freqs()
456 if (st->info->option_mode_2) in sca3000_show_available_3db_freqs()
458 st->info->option_mode_2_3db_freq); in sca3000_show_available_3db_freqs()
515 .scan_index = -1, /* Fake channel */
531 .scan_index = -1,
544 .scan_index = -1, /* Fake channel */
560 * __sca3000_get_base_freq() - obtain mode specific base frequency
561 * @st: Private driver specific device instance specific state.
577 switch (SCA3000_REG_MODE_MODE_MASK & st->rx[0]) { in __sca3000_get_base_freq()
579 *base_freq = info->measurement_mode_freq; in __sca3000_get_base_freq()
582 *base_freq = info->option_mode_1_freq; in __sca3000_get_base_freq()
585 *base_freq = info->option_mode_2_freq; in __sca3000_get_base_freq()
588 ret = -EINVAL; in __sca3000_get_base_freq()
594 * sca3000_read_raw_samp_freq() - read_raw handler for IIO_CHAN_INFO_SAMP_FREQ
595 * @st: Private driver specific device instance specific state.
604 ret = __sca3000_get_base_freq(st, st->info, val); in sca3000_read_raw_samp_freq()
628 * sca3000_write_raw_samp_freq() - write_raw handler for IIO_CHAN_INFO_SAMP_FREQ
629 * @st: Private driver specific device instance specific state.
638 ret = __sca3000_get_base_freq(st, st->info, &base_freq); in sca3000_write_raw_samp_freq()
653 return -EINVAL; in sca3000_write_raw_samp_freq()
667 /* mask bottom 2 bits - only ones that are relevant */ in sca3000_read_3db_freq()
668 st->rx[0] &= SCA3000_REG_MODE_MODE_MASK; in sca3000_read_3db_freq()
669 switch (st->rx[0]) { in sca3000_read_3db_freq()
671 *val = st->info->measurement_mode_3db_freq; in sca3000_read_3db_freq()
674 return -EBUSY; in sca3000_read_3db_freq()
676 *val = st->info->option_mode_1_3db_freq; in sca3000_read_3db_freq()
679 *val = st->info->option_mode_2_3db_freq; in sca3000_read_3db_freq()
682 return -EINVAL; in sca3000_read_3db_freq()
691 if (val == st->info->measurement_mode_3db_freq) in sca3000_write_3db_freq()
693 else if (st->info->option_mode_1 && in sca3000_write_3db_freq()
694 (val == st->info->option_mode_1_3db_freq)) in sca3000_write_3db_freq()
696 else if (st->info->option_mode_2 && in sca3000_write_3db_freq()
697 (val == st->info->option_mode_2_3db_freq)) in sca3000_write_3db_freq()
700 return -EINVAL; in sca3000_write_3db_freq()
705 st->rx[0] &= ~SCA3000_REG_MODE_MODE_MASK; in sca3000_write_3db_freq()
706 st->rx[0] |= (mode & SCA3000_REG_MODE_MODE_MASK); in sca3000_write_3db_freq()
708 return sca3000_write_reg(st, SCA3000_REG_MODE_ADDR, st->rx[0]); in sca3000_write_3db_freq()
723 mutex_lock(&st->lock); in sca3000_read_raw()
724 if (chan->type == IIO_ACCEL) { in sca3000_read_raw()
725 if (st->mo_det_use_count) { in sca3000_read_raw()
726 mutex_unlock(&st->lock); in sca3000_read_raw()
727 return -EBUSY; in sca3000_read_raw()
729 address = sca3000_addresses[chan->address][0]; in sca3000_read_raw()
732 mutex_unlock(&st->lock); in sca3000_read_raw()
735 *val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >> in sca3000_read_raw()
736 chan->scan_type.shift, in sca3000_read_raw()
737 chan->scan_type.realbits - 1); in sca3000_read_raw()
744 mutex_unlock(&st->lock); in sca3000_read_raw()
747 *val = (be16_to_cpup((__be16 *)st->rx) >> in sca3000_read_raw()
748 chan->scan_type.shift) & in sca3000_read_raw()
749 GENMASK(chan->scan_type.realbits - 1, 0); in sca3000_read_raw()
751 mutex_unlock(&st->lock); in sca3000_read_raw()
755 if (chan->type == IIO_ACCEL) in sca3000_read_raw()
756 *val2 = st->info->scale; in sca3000_read_raw()
761 *val = -214; in sca3000_read_raw()
765 mutex_lock(&st->lock); in sca3000_read_raw()
767 mutex_unlock(&st->lock); in sca3000_read_raw()
770 mutex_lock(&st->lock); in sca3000_read_raw()
772 mutex_unlock(&st->lock); in sca3000_read_raw()
775 return -EINVAL; in sca3000_read_raw()
789 return -EINVAL; in sca3000_write_raw()
790 mutex_lock(&st->lock); in sca3000_write_raw()
792 mutex_unlock(&st->lock); in sca3000_write_raw()
796 return -EINVAL; in sca3000_write_raw()
797 mutex_lock(&st->lock); in sca3000_write_raw()
799 mutex_unlock(&st->lock); in sca3000_write_raw()
802 return -EINVAL; in sca3000_write_raw()
809 * sca3000_read_av_freq() - sysfs function to get available frequencies
810 * @dev: Device structure for this device.
814 * The later modes are only relevant to the ring buffer - and depend on current
819 static ssize_t sca3000_read_av_freq(struct device *dev, in sca3000_read_av_freq()
827 mutex_lock(&st->lock); in sca3000_read_av_freq()
829 val = st->rx[0]; in sca3000_read_av_freq()
830 mutex_unlock(&st->lock); in sca3000_read_av_freq()
837 st->info->measurement_mode_freq, in sca3000_read_av_freq()
838 st->info->measurement_mode_freq / 2, in sca3000_read_av_freq()
839 st->info->measurement_mode_freq / 4); in sca3000_read_av_freq()
843 st->info->option_mode_1_freq, in sca3000_read_av_freq()
844 st->info->option_mode_1_freq / 2, in sca3000_read_av_freq()
845 st->info->option_mode_1_freq / 4); in sca3000_read_av_freq()
849 st->info->option_mode_2_freq, in sca3000_read_av_freq()
850 st->info->option_mode_2_freq / 2, in sca3000_read_av_freq()
851 st->info->option_mode_2_freq / 4); in sca3000_read_av_freq()
864 * sca3000_read_event_value() - query of a threshold or period
879 mutex_lock(&st->lock); in sca3000_read_event_value()
881 sca3000_addresses[chan->address][1]); in sca3000_read_event_value()
882 mutex_unlock(&st->lock); in sca3000_read_event_value()
886 if (chan->channel2 == IIO_MOD_Y) in sca3000_read_event_value()
888 ARRAY_SIZE(st->info->mot_det_mult_y)) in sca3000_read_event_value()
889 *val += st->info->mot_det_mult_y[i]; in sca3000_read_event_value()
892 ARRAY_SIZE(st->info->mot_det_mult_xz)) in sca3000_read_event_value()
893 *val += st->info->mot_det_mult_xz[i]; in sca3000_read_event_value()
901 return -EINVAL; in sca3000_read_event_value()
906 * sca3000_write_event_value() - control of threshold and period
907 * @indio_dev: Device instance specific IIO information.
930 if (chan->channel2 == IIO_MOD_Y) { in sca3000_write_event_value()
931 i = ARRAY_SIZE(st->info->mot_det_mult_y); in sca3000_write_event_value()
933 if (val >= st->info->mot_det_mult_y[--i]) { in sca3000_write_event_value()
935 val -= st->info->mot_det_mult_y[i]; in sca3000_write_event_value()
938 i = ARRAY_SIZE(st->info->mot_det_mult_xz); in sca3000_write_event_value()
940 if (val >= st->info->mot_det_mult_xz[--i]) { in sca3000_write_event_value()
942 val -= st->info->mot_det_mult_xz[i]; in sca3000_write_event_value()
946 mutex_lock(&st->lock); in sca3000_write_event_value()
948 sca3000_addresses[chan->address][1], in sca3000_write_event_value()
950 mutex_unlock(&st->lock); in sca3000_write_event_value()
967 u8 *rx, in sca3000_read_data() argument
974 .tx_buf = st->tx, in sca3000_read_data()
977 .rx_buf = rx, in sca3000_read_data()
981 st->tx[0] = SCA3000_READ_REG(reg_address_high); in sca3000_read_data()
982 ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer)); in sca3000_read_data()
984 dev_err(&st->us->dev, "problem reading register\n"); in sca3000_read_data()
992 * sca3000_ring_int_process() - ring specific interrupt handling.
994 * @indio_dev: Device instance specific IIO device structure.
1001 mutex_lock(&st->lock); in sca3000_ring_int_process()
1008 num_available = st->rx[0]; in sca3000_ring_int_process()
1013 ret = sca3000_read_data(st, SCA3000_REG_RING_OUT_ADDR, st->rx, in sca3000_ring_int_process()
1025 iio_push_to_buffers(indio_dev, st->rx + i * 3 * 2); in sca3000_ring_int_process()
1029 mutex_unlock(&st->lock); in sca3000_ring_int_process()
1033 * sca3000_event_handler() - handling ring and non ring events
1035 * @private: struct iio_device pointer for the device.
1055 mutex_lock(&st->lock); in sca3000_event_handler()
1057 val = st->rx[0]; in sca3000_event_handler()
1058 mutex_unlock(&st->lock); in sca3000_event_handler()
1115 mutex_lock(&st->lock); in sca3000_read_event_config()
1121 switch (chan->channel2) { in sca3000_read_event_config()
1123 ret = !!(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_read_event_config()
1132 if ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_read_event_config()
1141 ret = !!(ret & sca3000_addresses[chan->address][2]); in sca3000_read_event_config()
1145 ret = -EINVAL; in sca3000_read_event_config()
1149 mutex_unlock(&st->lock); in sca3000_read_event_config()
1165 if (state && !(st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1167 st->rx[0] | SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1169 else if (!state && (st->rx[0] & SCA3000_REG_MODE_FREE_FALL_DETECT)) in sca3000_freefall_set_state()
1171 st->rx[0] & ~SCA3000_REG_MODE_FREE_FALL_DETECT); in sca3000_freefall_set_state()
1198 st->mo_det_use_count++; in sca3000_motion_detect_set_state()
1206 st->mo_det_use_count--; in sca3000_motion_detect_set_state()
1214 if ((st->mo_det_use_count) && in sca3000_motion_detect_set_state()
1215 ((st->rx[0] & SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1218 (st->rx[0] & ~SCA3000_REG_MODE_MODE_MASK) in sca3000_motion_detect_set_state()
1221 else 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()
1231 * sca3000_write_event_config() - simple on off control for motion detector
1232 * @indio_dev: IIO device instance specific structure. Data specific to this
1254 mutex_lock(&st->lock); in sca3000_write_event_config()
1255 switch (chan->channel2) { in sca3000_write_event_config()
1264 chan->address, in sca3000_write_event_config()
1268 ret = -EINVAL; in sca3000_write_event_config()
1271 mutex_unlock(&st->lock); in sca3000_write_event_config()
1282 mutex_lock(&st->lock); in __sca3000_hw_ring_state_set()
1287 dev_info(&indio_dev->dev, "supposedly enabling ring buffer\n"); in __sca3000_hw_ring_state_set()
1290 (st->rx[0] | SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1294 (st->rx[0] & ~SCA3000_REG_MODE_RING_BUF_ENABLE)); in __sca3000_hw_ring_state_set()
1296 mutex_unlock(&st->lock); in __sca3000_hw_ring_state_set()
1302 * sca3000_hw_ring_preenable() - hw ring buffer preenable function
1303 * @indio_dev: structure representing the IIO device. Device instance
1315 mutex_lock(&st->lock); in sca3000_hw_ring_preenable()
1323 st->rx[0] | SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_preenable()
1327 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1332 mutex_unlock(&st->lock); in sca3000_hw_ring_preenable()
1347 mutex_lock(&st->lock); in sca3000_hw_ring_postdisable()
1354 st->rx[0] & ~SCA3000_REG_INT_MASK_RING_HALF); in sca3000_hw_ring_postdisable()
1356 mutex_unlock(&st->lock); in sca3000_hw_ring_postdisable()
1366 * sca3000_clean_setup() - get the device into a predictable state
1367 * @st: Device instance specific private data structure
1377 mutex_lock(&st->lock); in sca3000_clean_setup()
1416 * Ring in 12 bit mode - it is fine to overwrite reserved bits 3,5 in sca3000_clean_setup()
1423 (st->rx[0] & SCA3000_MODE_PROT_MASK)); in sca3000_clean_setup()
1426 mutex_unlock(&st->lock); in sca3000_clean_setup()
1446 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in sca3000_probe()
1448 return -ENOMEM; in sca3000_probe()
1452 st->us = spi; in sca3000_probe()
1453 mutex_init(&st->lock); in sca3000_probe()
1454 st->info = &sca3000_spi_chip_info_tbl[spi_get_device_id(spi) in sca3000_probe()
1455 ->driver_data]; in sca3000_probe()
1457 indio_dev->name = spi_get_device_id(spi)->name; in sca3000_probe()
1458 indio_dev->info = &sca3000_info; in sca3000_probe()
1459 if (st->info->temp_output) { in sca3000_probe()
1460 indio_dev->channels = sca3000_channels_with_temp; in sca3000_probe()
1461 indio_dev->num_channels = in sca3000_probe()
1464 indio_dev->channels = sca3000_channels; in sca3000_probe()
1465 indio_dev->num_channels = ARRAY_SIZE(sca3000_channels); in sca3000_probe()
1467 indio_dev->modes = INDIO_DIRECT_MODE; in sca3000_probe()
1469 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()