Lines Matching +full:spi +full:- +full:src +full:- +full:clk
1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments ADS131E0x 4-, 6- and 8-Channel ADCs
12 #include <linux/clk.h>
24 #include <linux/spi/spi.h>
92 struct spi_device *spi; member
94 struct clk *adc_clk;
166 ret = spi_write_then_read(st->spi, &cmd, 1, NULL, 0); in ads131e08_exec_cmd()
168 dev_err(&st->spi->dev, "Exec cmd(%02x) failed\n", cmd); in ads131e08_exec_cmd()
178 .tx_buf = &st->tx_buf, in ads131e08_read_reg()
181 .value = st->sdecode_delay_us, in ads131e08_read_reg()
185 .rx_buf = &st->rx_buf, in ads131e08_read_reg()
190 st->tx_buf[0] = ADS131E08_CMD_RREG(reg); in ads131e08_read_reg()
191 st->tx_buf[1] = 0; in ads131e08_read_reg()
193 ret = spi_sync_transfer(st->spi, transfer, ARRAY_SIZE(transfer)); in ads131e08_read_reg()
195 dev_err(&st->spi->dev, "Read register failed\n"); in ads131e08_read_reg()
199 return st->rx_buf[0]; in ads131e08_read_reg()
207 .tx_buf = &st->tx_buf, in ads131e08_write_reg()
210 .value = st->sdecode_delay_us, in ads131e08_write_reg()
216 st->tx_buf[0] = ADS131E08_CMD_WREG(reg); in ads131e08_write_reg()
217 st->tx_buf[1] = 0; in ads131e08_write_reg()
218 st->tx_buf[2] = value; in ads131e08_write_reg()
220 ret = spi_sync_transfer(st->spi, transfer, ARRAY_SIZE(transfer)); in ads131e08_write_reg()
222 dev_err(&st->spi->dev, "Write register failed\n"); in ads131e08_write_reg()
232 .tx_buf = &st->tx_buf, in ads131e08_read_data()
235 .rx_buf = &st->rx_buf, in ads131e08_read_data()
240 st->tx_buf[0] = ADS131E08_CMD_RDATA; in ads131e08_read_data()
242 ret = spi_sync_transfer(st->spi, transfer, ARRAY_SIZE(transfer)); in ads131e08_read_data()
244 dev_err(&st->spi->dev, "Read data failed\n"); in ads131e08_read_data()
259 dev_err(&st->spi->dev, "invalid data rate value\n"); in ads131e08_set_data_rate()
260 return -EINVAL; in ads131e08_set_data_rate()
275 st->data_rate = data_rate; in ads131e08_set_data_rate()
276 st->readback_len = ADS131E08_NUM_STATUS_BYTES + in ads131e08_set_data_rate()
277 ADS131E08_NUM_DATA_BYTES(st->data_rate) * in ads131e08_set_data_rate()
278 st->info->max_channels; in ads131e08_set_data_rate()
294 dev_err(&st->spi->dev, "invalid PGA gain value\n"); in ads131e08_pga_gain_to_field_value()
295 return -EINVAL; in ads131e08_pga_gain_to_field_value()
331 dev_err(&st->spi->dev, "invalid channel mux value\n"); in ads131e08_validate_channel_mux()
332 return -EINVAL; in ads131e08_validate_channel_mux()
377 if (!st->vref_reg) { in ads131e08_config_reference_voltage()
381 st->vref_mv == ADS131E08_VREF_4V_mV); in ads131e08_config_reference_voltage()
389 const struct iio_chan_spec *channel = indio_dev->channels; in ads131e08_initial_config()
398 udelay(st->reset_delay_us); in ads131e08_initial_config()
413 for (i = 0; i < indio_dev->num_channels; i++) { in ads131e08_initial_config()
414 ret = ads131e08_set_pga_gain(st, channel->channel, in ads131e08_initial_config()
415 st->channel_config[i].pga_gain); in ads131e08_initial_config()
419 ret = ads131e08_set_channel_mux(st, channel->channel, in ads131e08_initial_config()
420 st->channel_config[i].mux); in ads131e08_initial_config()
424 active_channels |= BIT(channel->channel); in ads131e08_initial_config()
429 for_each_clear_bit(i, &active_channels, st->info->max_channels) { in ads131e08_initial_config()
461 reinit_completion(&st->completion); in ads131e08_pool_data()
468 ret = wait_for_completion_timeout(&st->completion, timeout); in ads131e08_pool_data()
470 return -ETIMEDOUT; in ads131e08_pool_data()
472 ret = ads131e08_read_data(st, st->readback_len); in ads131e08_pool_data()
483 u8 num_bits, *src; in ads131e08_read_direct() local
490 src = st->rx_buf + ADS131E08_NUM_STATUS_BYTES + in ads131e08_read_direct()
491 channel->channel * ADS131E08_NUM_DATA_BYTES(st->data_rate); in ads131e08_read_direct()
493 num_bits = ADS131E08_NUM_DATA_BITS(st->data_rate); in ads131e08_read_direct()
494 *value = sign_extend32(get_unaligned_be32(src) >> (32 - num_bits), num_bits - 1); in ads131e08_read_direct()
520 if (st->vref_reg) { in ads131e08_read_raw()
521 ret = regulator_get_voltage(st->vref_reg); in ads131e08_read_raw()
527 *value = st->vref_mv; in ads131e08_read_raw()
530 *value /= st->channel_config[channel->address].pga_gain; in ads131e08_read_raw()
531 *value2 = ADS131E08_NUM_DATA_BITS(st->data_rate) - 1; in ads131e08_read_raw()
536 *value = st->data_rate; in ads131e08_read_raw()
541 return -EINVAL; in ads131e08_read_raw()
563 return -EINVAL; in ads131e08_write_raw()
616 struct iio_dev *indio_dev = pf->indio_dev; in ads131e08_trigger_handler()
619 u8 *src, *dest; in ads131e08_trigger_handler() local
629 unsigned int num_bytes = ADS131E08_NUM_DATA_BYTES(st->data_rate); in ads131e08_trigger_handler()
633 ret = ads131e08_read_data(st, st->readback_len); in ads131e08_trigger_handler()
641 src = st->rx_buf + ADS131E08_NUM_STATUS_BYTES + chn * num_bytes; in ads131e08_trigger_handler()
642 dest = st->tmp_buf.data + i * ADS131E08_NUM_STORAGE_BYTES; in ads131e08_trigger_handler()
646 * +---+---+---+---+ in ads131e08_trigger_handler()
648 * +---+---+---+---+ in ads131e08_trigger_handler()
652 * +---+---+---+---+ in ads131e08_trigger_handler()
654 * +---+---+---+---+ in ads131e08_trigger_handler()
657 memcpy(dest + tweek_offset, src, num_bytes); in ads131e08_trigger_handler()
664 *dest = *src & BIT(7) ? 0xff : 0x00; in ads131e08_trigger_handler()
669 iio_push_to_buffers_with_timestamp(indio_dev, st->tmp_buf.data, in ads131e08_trigger_handler()
673 iio_trigger_notify_done(indio_dev->trig); in ads131e08_trigger_handler()
684 iio_trigger_poll(st->trig); in ads131e08_interrupt()
686 complete(&st->completion); in ads131e08_interrupt()
695 struct device *dev = &st->spi->dev; in ads131e08_alloc_channels()
700 ret = device_property_read_u32(dev, "ti,vref-internal", &tmp); in ads131e08_alloc_channels()
706 st->vref_mv = ADS131E08_VREF_2V4_mV; in ads131e08_alloc_channels()
709 st->vref_mv = ADS131E08_VREF_4V_mV; in ads131e08_alloc_channels()
712 dev_err(&st->spi->dev, "invalid internal voltage reference\n"); in ads131e08_alloc_channels()
713 return -EINVAL; in ads131e08_alloc_channels()
718 dev_err(&st->spi->dev, "no channel children\n"); in ads131e08_alloc_channels()
719 return -ENODEV; in ads131e08_alloc_channels()
722 if (num_channels > st->info->max_channels) { in ads131e08_alloc_channels()
723 dev_err(&st->spi->dev, "num of channel children out of range\n"); in ads131e08_alloc_channels()
724 return -EINVAL; in ads131e08_alloc_channels()
727 channels = devm_kcalloc(&st->spi->dev, num_channels, in ads131e08_alloc_channels()
730 return -ENOMEM; in ads131e08_alloc_channels()
732 channel_config = devm_kcalloc(&st->spi->dev, num_channels, in ads131e08_alloc_channels()
735 return -ENOMEM; in ads131e08_alloc_channels()
781 indio_dev->channels = channels; in ads131e08_alloc_channels()
782 indio_dev->num_channels = num_channels; in ads131e08_alloc_channels()
783 st->channel_config = channel_config; in ads131e08_alloc_channels()
793 regulator_disable(st->vref_reg); in ads131e08_regulator_disable()
796 static int ads131e08_probe(struct spi_device *spi) in ads131e08_probe() argument
805 info = spi_get_device_match_data(spi); in ads131e08_probe()
807 dev_err(&spi->dev, "failed to get match data\n"); in ads131e08_probe()
808 return -ENODEV; in ads131e08_probe()
811 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in ads131e08_probe()
813 dev_err(&spi->dev, "failed to allocate IIO device\n"); in ads131e08_probe()
814 return -ENOMEM; in ads131e08_probe()
818 st->info = info; in ads131e08_probe()
819 st->spi = spi; in ads131e08_probe()
825 indio_dev->name = st->info->name; in ads131e08_probe()
826 indio_dev->info = &ads131e08_iio_info; in ads131e08_probe()
827 indio_dev->modes = INDIO_DIRECT_MODE; in ads131e08_probe()
829 init_completion(&st->completion); in ads131e08_probe()
831 if (spi->irq) { in ads131e08_probe()
832 ret = devm_request_irq(&spi->dev, spi->irq, in ads131e08_probe()
835 spi->dev.driver->name, indio_dev); in ads131e08_probe()
837 return dev_err_probe(&spi->dev, ret, in ads131e08_probe()
840 dev_err(&spi->dev, "data ready IRQ missing\n"); in ads131e08_probe()
841 return -ENODEV; in ads131e08_probe()
844 st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-dev%d", in ads131e08_probe()
845 indio_dev->name, iio_device_id(indio_dev)); in ads131e08_probe()
846 if (!st->trig) { in ads131e08_probe()
847 dev_err(&spi->dev, "failed to allocate IIO trigger\n"); in ads131e08_probe()
848 return -ENOMEM; in ads131e08_probe()
851 st->trig->ops = &ads131e08_trigger_ops; in ads131e08_probe()
852 st->trig->dev.parent = &spi->dev; in ads131e08_probe()
853 iio_trigger_set_drvdata(st->trig, indio_dev); in ads131e08_probe()
854 ret = devm_iio_trigger_register(&spi->dev, st->trig); in ads131e08_probe()
856 dev_err(&spi->dev, "failed to register IIO trigger\n"); in ads131e08_probe()
857 return -ENOMEM; in ads131e08_probe()
860 indio_dev->trig = iio_trigger_get(st->trig); in ads131e08_probe()
862 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, in ads131e08_probe()
865 dev_err(&spi->dev, "failed to setup IIO buffer\n"); in ads131e08_probe()
869 st->vref_reg = devm_regulator_get_optional(&spi->dev, "vref"); in ads131e08_probe()
870 if (!IS_ERR(st->vref_reg)) { in ads131e08_probe()
871 ret = regulator_enable(st->vref_reg); in ads131e08_probe()
873 dev_err(&spi->dev, in ads131e08_probe()
878 ret = devm_add_action_or_reset(&spi->dev, ads131e08_regulator_disable, st); in ads131e08_probe()
882 if (PTR_ERR(st->vref_reg) != -ENODEV) in ads131e08_probe()
883 return PTR_ERR(st->vref_reg); in ads131e08_probe()
885 st->vref_reg = NULL; in ads131e08_probe()
888 st->adc_clk = devm_clk_get_enabled(&spi->dev, "adc-clk"); in ads131e08_probe()
889 if (IS_ERR(st->adc_clk)) in ads131e08_probe()
890 return dev_err_probe(&spi->dev, PTR_ERR(st->adc_clk), in ads131e08_probe()
893 adc_clk_hz = clk_get_rate(st->adc_clk); in ads131e08_probe()
895 dev_err(&spi->dev, "failed to get the ADC clock rate\n"); in ads131e08_probe()
896 return -EINVAL; in ads131e08_probe()
900 st->sdecode_delay_us = DIV_ROUND_UP( in ads131e08_probe()
902 st->reset_delay_us = DIV_ROUND_UP( in ads131e08_probe()
907 dev_err(&spi->dev, "initial configuration failed\n"); in ads131e08_probe()
911 return devm_iio_device_register(&spi->dev, indio_dev); in ads131e08_probe()
931 MODULE_DEVICE_TABLE(spi, ads131e08_ids);