Lines Matching +full:keep +full:- +full:vref +full:- +full:on
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2023 - 2024 Topic Embedded Products
94 /* For reading and writing registers, we need a 3-byte buffer */
96 /* Outputs status word and 'n' 24-bit samples, plus the command byte */
118 * 2 = DRDY during SPI transfer, start another transfer on completion
119 * >2 = Multiple DRDY during transfer, lost rdata_xfer_busy - 2 samples
173 .tx_buf = priv->cmd_buffer, in ads1298_write_cmd()
174 .rx_buf = priv->cmd_buffer, in ads1298_write_cmd()
183 priv->cmd_buffer[0] = command; in ads1298_write_cmd()
185 return spi_sync_transfer(priv->spi, &xfer, 1); in ads1298_write_cmd()
193 ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(chan_index), in ads1298_read_one()
198 /* Enable single-shot mode, so we don't need to send a STOP */ in ads1298_read_one()
199 ret = regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG4, in ads1298_read_one()
205 reinit_completion(&priv->completion); in ads1298_read_one()
209 dev_err(&priv->spi->dev, "CMD_START error: %d\n", ret); in ads1298_read_one()
214 ret = wait_for_completion_timeout(&priv->completion, msecs_to_jiffies(50)); in ads1298_read_one()
216 return -ETIMEDOUT; in ads1298_read_one()
227 ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, &cfg); in ads1298_get_samp_freq()
231 if (priv->clk) in ads1298_get_samp_freq()
232 rate = clk_get_rate(priv->clk); in ads1298_get_samp_freq()
236 return -EINVAL; in ads1298_get_samp_freq()
238 /* Data rate shift depends on HR/LP mode */ in ads1298_get_samp_freq()
255 if (priv->clk) in ads1298_set_samp_freq()
256 rate = clk_get_rate(priv->clk); in ads1298_set_samp_freq()
260 return -EINVAL; in ads1298_set_samp_freq()
262 return -EINVAL; in ads1298_set_samp_freq()
272 return regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG1, in ads1298_set_samp_freq()
286 if (priv->reg_vref) { in ads1298_get_scale()
287 ret = regulator_get_voltage(priv->reg_vref); in ads1298_get_scale()
293 ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG3, ®val); in ads1298_get_scale()
301 ret = regmap_read(priv->regmap, ADS1298_REG_CHnSET(channel), ®val); in ads1298_get_scale()
306 *val /= gain; /* Full scale is VREF / gain */ in ads1298_get_scale()
308 *val2 = ADS1298_BITS_PER_SAMPLE - 1; /* Signed, hence the -1 */ in ads1298_get_scale()
326 ret = ads1298_read_one(priv, chan->scan_index); in ads1298_read_raw()
333 *val = sign_extend32(get_unaligned_be24(priv->rx_buffer + chan->address), in ads1298_read_raw()
334 ADS1298_BITS_PER_SAMPLE - 1); in ads1298_read_raw()
337 return ads1298_get_scale(priv, chan->channel, val, val2); in ads1298_read_raw()
341 ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, val); in ads1298_read_raw()
348 return -EINVAL; in ads1298_read_raw()
362 return -EINVAL; in ads1298_write_raw()
370 .tx_buf = priv->cmd_buffer, in ads1298_reg_write()
371 .rx_buf = priv->cmd_buffer, in ads1298_reg_write()
380 priv->cmd_buffer[0] = ADS1298_CMD_WREG | reg; in ads1298_reg_write()
381 priv->cmd_buffer[1] = 0; /* Number of registers to be written - 1 */ in ads1298_reg_write()
382 priv->cmd_buffer[2] = val; in ads1298_reg_write()
384 return spi_sync_transfer(priv->spi, ®_write_xfer, 1); in ads1298_reg_write()
391 .tx_buf = priv->cmd_buffer, in ads1298_reg_read()
392 .rx_buf = priv->cmd_buffer, in ads1298_reg_read()
402 priv->cmd_buffer[0] = ADS1298_CMD_RREG | reg; in ads1298_reg_read()
403 priv->cmd_buffer[1] = 0; /* Number of registers to be read - 1 */ in ads1298_reg_read()
404 priv->cmd_buffer[2] = 0; in ads1298_reg_read()
406 ret = spi_sync_transfer(priv->spi, ®_read_xfer, 1); in ads1298_reg_read()
410 *val = priv->cmd_buffer[2]; in ads1298_reg_read()
421 return regmap_read(priv->regmap, reg, readval); in ads1298_reg_access()
423 return regmap_write(priv->regmap, reg, writeval); in ads1298_reg_access()
429 guard(spinlock_irqsave)(&priv->irq_busy_lock); in ads1298_rdata_unmark_busy()
430 priv->rdata_xfer_busy = 0; in ads1298_rdata_unmark_busy()
444 /* Configure power-down bits to match scan mask */ in ads1298_update_scan_mode()
445 for (i = 0; i < indio_dev->num_channels; i++) { in ads1298_update_scan_mode()
447 ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(i), in ads1298_update_scan_mode()
465 guard(spinlock_irqsave)(&priv->irq_busy_lock); in ads1298_rdata_release_busy_or_restart()
467 if (priv->rdata_xfer_busy > 1) { in ads1298_rdata_release_busy_or_restart()
473 spi_async(priv->spi, &priv->rdata_msg); in ads1298_rdata_release_busy_or_restart()
479 priv->rdata_xfer_busy = 1; in ads1298_rdata_release_busy_or_restart()
482 priv->rdata_xfer_busy = 0; in ads1298_rdata_release_busy_or_restart()
492 u32 *bounce = priv->bounce_buffer; in ads1298_rdata_complete()
500 complete(&priv->completion); in ads1298_rdata_complete()
507 &indio_dev->channels[scan_index]; in ads1298_rdata_complete()
508 const u8 *data = priv->rx_buffer + scan_chan->address; in ads1298_rdata_complete()
513 /* rx_buffer can be overwritten from this point on */ in ads1298_rdata_complete()
516 iio_push_to_buffers(indio_dev, priv->bounce_buffer); in ads1298_rdata_complete()
525 guard(spinlock_irqsave)(&priv->irq_busy_lock); in ads1298_interrupt()
527 wasbusy = priv->rdata_xfer_busy++; in ads1298_interrupt()
530 spi_async(priv->spi, &priv->rdata_msg); in ads1298_interrupt()
540 /* Disable single-shot mode */ in ads1298_buffer_postenable()
541 ret = regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG4, in ads1298_buffer_postenable()
588 struct device *dev = &priv->spi->dev; in ads1298_init()
598 ret = regmap_read(priv->regmap, ADS1298_REG_ID, &val); in ads1298_init()
602 /* Fill in name and channel count based on what the chip told us */ in ads1298_init()
603 indio_dev->num_channels = 4 + 2 * (val & ADS1298_MASK_ID_CHANNELS); in ads1298_init()
612 return dev_err_probe(dev, -ENODEV, "Unknown ID: 0x%x\n", val); in ads1298_init()
614 indio_dev->name = devm_kasprintf(dev, GFP_KERNEL, "ads129%u%s", in ads1298_init()
615 indio_dev->num_channels, suffix); in ads1298_init()
618 ret = regmap_write(priv->regmap, ADS1298_REG_CONFIG2, in ads1298_init()
627 if (!priv->reg_vref) { in ads1298_init()
630 /* Use 4V VREF when power supply is at least 4.4V */ in ads1298_init()
631 if (regulator_get_voltage(priv->reg_avdd) >= 4400000) in ads1298_init()
634 return regmap_write(priv->regmap, ADS1298_REG_CONFIG3, val); in ads1298_init()
641 struct device *dev = &spi->dev; in ads1298_probe()
647 return -ENOMEM; in ads1298_probe()
657 /* VREF can be supplied externally, otherwise use internal reference */ in ads1298_probe()
658 priv->reg_vref = devm_regulator_get_optional(dev, "vref"); in ads1298_probe()
659 if (IS_ERR(priv->reg_vref)) { in ads1298_probe()
660 if (PTR_ERR(priv->reg_vref) != -ENODEV) in ads1298_probe()
661 return dev_err_probe(dev, PTR_ERR(priv->reg_vref), in ads1298_probe()
662 "Failed to get vref regulator\n"); in ads1298_probe()
664 priv->reg_vref = NULL; in ads1298_probe()
666 ret = regulator_enable(priv->reg_vref); in ads1298_probe()
670 ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_vref); in ads1298_probe()
675 priv->clk = devm_clk_get_optional_enabled(dev, "clk"); in ads1298_probe()
676 if (IS_ERR(priv->clk)) in ads1298_probe()
677 return dev_err_probe(dev, PTR_ERR(priv->clk), "Failed to get clk\n"); in ads1298_probe()
679 priv->reg_avdd = devm_regulator_get(dev, "avdd"); in ads1298_probe()
680 if (IS_ERR(priv->reg_avdd)) in ads1298_probe()
681 return dev_err_probe(dev, PTR_ERR(priv->reg_avdd), in ads1298_probe()
684 ret = regulator_enable(priv->reg_avdd); in ads1298_probe()
688 ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_avdd); in ads1298_probe()
692 priv->spi = spi; in ads1298_probe()
693 init_completion(&priv->completion); in ads1298_probe()
694 spin_lock_init(&priv->irq_busy_lock); in ads1298_probe()
695 priv->regmap = devm_regmap_init(dev, NULL, priv, &ads1298_regmap_config); in ads1298_probe()
696 if (IS_ERR(priv->regmap)) in ads1298_probe()
697 return PTR_ERR(priv->regmap); in ads1298_probe()
699 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; in ads1298_probe()
700 indio_dev->channels = ads1298_channels; in ads1298_probe()
701 indio_dev->info = &ads1298_info; in ads1298_probe()
722 priv->tx_buffer[0] = ADS1298_CMD_RDATA; in ads1298_probe()
723 priv->rdata_xfer.tx_buf = priv->tx_buffer; in ads1298_probe()
724 priv->rdata_xfer.rx_buf = priv->rx_buffer; in ads1298_probe()
725 priv->rdata_xfer.len = ADS1298_SPI_RDATA_BUFFER_SIZE(indio_dev->num_channels); in ads1298_probe()
726 /* Must keep CS low for 4 clocks */ in ads1298_probe()
727 priv->rdata_xfer.delay.value = 2; in ads1298_probe()
728 priv->rdata_xfer.delay.unit = SPI_DELAY_UNIT_USECS; in ads1298_probe()
729 spi_message_init_with_transfers(&priv->rdata_msg, &priv->rdata_xfer, 1); in ads1298_probe()
730 priv->rdata_msg.complete = &ads1298_rdata_complete; in ads1298_probe()
731 priv->rdata_msg.context = indio_dev; in ads1298_probe()
733 ret = devm_request_irq(dev, spi->irq, &ads1298_interrupt, in ads1298_probe()
734 IRQF_TRIGGER_FALLING, indio_dev->name, in ads1298_probe()