Lines Matching +full:spi +full:- +full:cs +full:- +full:high

1 // SPDX-License-Identifier: GPL-2.0+
3 * AD4000 SPI ADC driver
18 #include <linux/spi/spi.h>
34 #define AD4000_CFG_HIGHZ BIT(2) /* High impedance mode */
53 .shift = _storage_bits - _real_bits, \
82 .shift = _storage_bits - _real_bits, \
108 /* maps adi,sdi-pin property value to enum */
111 [AD4000_SDI_VIO] = "high",
112 [AD4000_SDI_CS] = "cs",
153 /* AD7946, AD7686, AD7688, AD7988-5, AD7693 */
177 /* AD7988-1 */
384 .dev_name = "ad7988-1",
390 .dev_name = "ad7988-5",
396 struct spi_device *spi; member
434 if (chan->scan_type.sign == 's') in ad4000_fill_scale_tbl()
435 scale_bits = chan->scan_type.realbits - 1; in ad4000_fill_scale_tbl()
437 scale_bits = chan->scan_type.realbits; in ad4000_fill_scale_tbl()
445 val = mult_frac(st->vref_mv, MICRO, st->gain_milli); in ad4000_fill_scale_tbl()
452 st->scale_tbl[0][0] = tmp0; /* Integer part */ in ad4000_fill_scale_tbl()
453 st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */ in ad4000_fill_scale_tbl()
456 st->scale_tbl[1][0] = tmp0; in ad4000_fill_scale_tbl()
459 if (chan->differential) in ad4000_fill_scale_tbl()
460 st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5); in ad4000_fill_scale_tbl()
462 st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10); in ad4000_fill_scale_tbl()
467 st->tx_buf[0] = AD4000_WRITE_COMMAND; in ad4000_write_reg()
468 st->tx_buf[1] = val; in ad4000_write_reg()
469 return spi_write(st->spi, st->tx_buf, ARRAY_SIZE(st->tx_buf)); in ad4000_write_reg()
475 .tx_buf = st->tx_buf, in ad4000_read_reg()
476 .rx_buf = st->rx_buf, in ad4000_read_reg()
481 st->tx_buf[0] = AD4000_READ_COMMAND; in ad4000_read_reg()
482 ret = spi_sync_transfer(st->spi, &t, 1); in ad4000_read_reg()
486 *val = st->rx_buf[1]; in ad4000_read_reg()
495 * In 4-wire mode, the CNV line is held high for the entire conversion in ad4000_convert_and_acquire()
497 * and, if provided, replaces controller CS. If CNV GPIO is not defined in ad4000_convert_and_acquire()
500 gpiod_set_value_cansleep(st->cnv_gpio, 1); in ad4000_convert_and_acquire()
501 ret = spi_sync(st->spi, &st->msg); in ad4000_convert_and_acquire()
502 gpiod_set_value_cansleep(st->cnv_gpio, 0); in ad4000_convert_and_acquire()
518 if (chan->scan_type.storagebits > 16) in ad4000_single_conversion()
519 sample = be32_to_cpu(st->scan.data.sample_buf32); in ad4000_single_conversion()
521 sample = be16_to_cpu(st->scan.data.sample_buf16); in ad4000_single_conversion()
523 sample >>= chan->scan_type.shift; in ad4000_single_conversion()
525 if (chan->scan_type.sign == 's') in ad4000_single_conversion()
526 *val = sign_extend32(sample, chan->scan_type.realbits - 1); in ad4000_single_conversion()
541 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) in ad4000_read_raw()
545 *val = st->scale_tbl[st->span_comp][0]; in ad4000_read_raw()
546 *val2 = st->scale_tbl[st->span_comp][1]; in ad4000_read_raw()
550 if (st->span_comp) in ad4000_read_raw()
551 *val = mult_frac(st->vref_mv, 1, 10); in ad4000_read_raw()
555 return -EINVAL; in ad4000_read_raw()
568 *vals = (int *)st->scale_tbl; in ad4000_read_avail()
573 return -EINVAL; in ad4000_read_avail()
599 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { in ad4000_write_raw()
600 guard(mutex)(&st->lock); in ad4000_write_raw()
606 span_comp_en = val2 == st->scale_tbl[1][1]; in ad4000_write_raw()
614 st->span_comp = span_comp_en; in ad4000_write_raw()
619 return -EINVAL; in ad4000_write_raw()
626 struct iio_dev *indio_dev = pf->indio_dev; in ad4000_trigger_handler()
634 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp); in ad4000_trigger_handler()
637 iio_trigger_notify_done(indio_dev->trig); in ad4000_trigger_handler()
654 * in "3-wire" mode, selected when the adi,sdi-pin device tree property is
655 * absent or set to "high". In this connection mode, the ADC SDI pin is
656 * connected to MOSI or to VIO and ADC CNV pin is connected either to a SPI
657 * controller CS or to a GPIO.
660 * If the CNV pin is connected to an SPI controller CS line (which is by default
666 * preparatory transfer to pulse the CS/CNV line.
671 struct spi_transfer *xfers = st->xfers; in ad4000_prepare_3wire_mode_message()
674 xfers[0].cs_change_delay.value = st->time_spec->t_conv_ns; in ad4000_prepare_3wire_mode_message()
677 xfers[1].rx_buf = &st->scan.data; in ad4000_prepare_3wire_mode_message()
678 xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); in ad4000_prepare_3wire_mode_message()
679 xfers[1].delay.value = st->time_spec->t_quiet2_ns; in ad4000_prepare_3wire_mode_message()
682 spi_message_init_with_transfers(&st->msg, st->xfers, 2); in ad4000_prepare_3wire_mode_message()
684 return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); in ad4000_prepare_3wire_mode_message()
689 * in "4-wire" mode, selected when the adi,sdi-pin device tree property is
690 * set to "cs". In this connection mode, the controller CS pin is connected to
692 * The GPIO connected to ADC CNV pin is set outside of the SPI transfer.
697 struct spi_transfer *xfers = st->xfers; in ad4000_prepare_4wire_mode_message()
700 * Dummy transfer to cause enough delay between CNV going high and SDI in ad4000_prepare_4wire_mode_message()
704 xfers[0].delay.value = st->time_spec->t_conv_ns; in ad4000_prepare_4wire_mode_message()
707 xfers[1].rx_buf = &st->scan.data; in ad4000_prepare_4wire_mode_message()
708 xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); in ad4000_prepare_4wire_mode_message()
710 spi_message_init_with_transfers(&st->msg, st->xfers, 2); in ad4000_prepare_4wire_mode_message()
712 return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); in ad4000_prepare_4wire_mode_message()
719 if (device_property_present(&st->spi->dev, "adi,high-z-input")) in ad4000_config()
725 static int ad4000_probe(struct spi_device *spi) in ad4000_probe() argument
728 struct device *dev = &spi->dev; in ad4000_probe()
735 return -ENOMEM; in ad4000_probe()
737 chip = spi_get_device_match_data(spi); in ad4000_probe()
739 return -EINVAL; in ad4000_probe()
742 st->spi = spi; in ad4000_probe()
743 st->time_spec = chip->time_spec; in ad4000_probe()
754 st->vref_mv = ret / 1000; in ad4000_probe()
756 st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_HIGH); in ad4000_probe()
757 if (IS_ERR(st->cnv_gpio)) in ad4000_probe()
758 return dev_err_probe(dev, PTR_ERR(st->cnv_gpio), in ad4000_probe()
761 ret = device_property_match_property_string(dev, "adi,sdi-pin", in ad4000_probe()
764 if (ret < 0 && ret != -EINVAL) in ad4000_probe()
766 "getting adi,sdi-pin property failed\n"); in ad4000_probe()
768 /* Default to usual SPI connections if pin properties are not present */ in ad4000_probe()
769 st->sdi_pin = ret == -EINVAL ? AD4000_SDI_MOSI : ret; in ad4000_probe()
770 switch (st->sdi_pin) { in ad4000_probe()
772 indio_dev->info = &ad4000_reg_access_info; in ad4000_probe()
773 indio_dev->channels = chip->reg_access_chan_spec; in ad4000_probe()
776 * In "3-wire mode", the ADC SDI line must be kept high when in ad4000_probe()
778 * Request the SPI controller to make MOSI idle high. in ad4000_probe()
780 spi->mode |= SPI_MOSI_IDLE_HIGH; in ad4000_probe()
781 ret = spi_setup(spi); in ad4000_probe()
785 ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]); in ad4000_probe()
795 indio_dev->info = &ad4000_info; in ad4000_probe()
796 indio_dev->channels = chip->chan_spec; in ad4000_probe()
797 ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]); in ad4000_probe()
803 indio_dev->info = &ad4000_info; in ad4000_probe()
804 indio_dev->channels = chip->chan_spec; in ad4000_probe()
805 ret = ad4000_prepare_4wire_mode_message(st, &indio_dev->channels[0]); in ad4000_probe()
811 return dev_err_probe(dev, -EPROTONOSUPPORT, in ad4000_probe()
815 return dev_err_probe(dev, -EINVAL, "Unrecognized connection mode\n"); in ad4000_probe()
818 indio_dev->name = chip->dev_name; in ad4000_probe()
819 indio_dev->num_channels = 2; in ad4000_probe()
821 ret = devm_mutex_init(dev, &st->lock); in ad4000_probe()
825 st->gain_milli = 1000; in ad4000_probe()
826 if (chip->has_hardware_gain) { in ad4000_probe()
827 ret = device_property_read_u16(dev, "adi,gain-milli", in ad4000_probe()
828 &st->gain_milli); in ad4000_probe()
831 gain_idx = find_closest(st->gain_milli, ad4000_gains, in ad4000_probe()
833 st->gain_milli = ad4000_gains[gain_idx]; in ad4000_probe()
840 ad4000_fill_scale_tbl(st, &indio_dev->channels[0]); in ad4000_probe()
881 { "ad7988-1", (kernel_ulong_t)&ad7988_1_chip_info },
882 { "ad7988-5", (kernel_ulong_t)&ad7988_5_chip_info },
885 MODULE_DEVICE_TABLE(spi, ad4000_id);
917 { .compatible = "adi,ad7988-1", .data = &ad7988_1_chip_info },
918 { .compatible = "adi,ad7988-5", .data = &ad7988_5_chip_info },