linux/drivers/iio/adc/ad7380.c

// SPDX-License-Identifier: GPL-2.0-only
/*
 * Analog Devices AD738x Simultaneous Sampling SAR ADCs
 *
 * Copyright 2017 Analog Devices Inc.
 * Copyright 2024 BayLibre, SAS
 *
 * Datasheets of supported parts:
 * ad7380/1 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7380-7381.pdf
 * ad7383/4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-7384.pdf
 * ad7386/7/8 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7386-7387-7388.pdf
 * ad7380-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7380-4.pdf
 * ad7381-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7381-4.pdf
 * ad7383/4-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-4-ad7384-4.pdf
 * ad7386/7/8-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7386-4-7387-4-7388-4.pdf
 */

#include <linux/align.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>

#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>

#define MAX_NUM_CHANNELS
/* 2.5V internal reference voltage */
#define AD7380_INTERNAL_REF_MV

/* reading and writing registers is more reliable at lower than max speed */
#define AD7380_REG_WR_SPEED_HZ

#define AD7380_REG_WR
#define AD7380_REG_REGADDR
#define AD7380_REG_DATA

#define AD7380_REG_ADDR_NOP
#define AD7380_REG_ADDR_CONFIG1
#define AD7380_REG_ADDR_CONFIG2
#define AD7380_REG_ADDR_ALERT
#define AD7380_REG_ADDR_ALERT_LOW_TH
#define AD7380_REG_ADDR_ALERT_HIGH_TH

#define AD7380_CONFIG1_CH
#define AD7380_CONFIG1_SEQ
#define AD7380_CONFIG1_OS_MODE
#define AD7380_CONFIG1_OSR
#define AD7380_CONFIG1_CRC_W
#define AD7380_CONFIG1_CRC_R
#define AD7380_CONFIG1_ALERTEN
#define AD7380_CONFIG1_RES
#define AD7380_CONFIG1_REFSEL
#define AD7380_CONFIG1_PMODE

#define AD7380_CONFIG2_SDO2
#define AD7380_CONFIG2_SDO
#define AD7380_CONFIG2_RESET

#define AD7380_CONFIG2_RESET_SOFT
#define AD7380_CONFIG2_RESET_HARD

#define AD7380_ALERT_LOW_TH
#define AD7380_ALERT_HIGH_TH

#define T_CONVERT_NS
#define T_CONVERT_0_NS
#define T_CONVERT_X_NS
#define T_POWERUP_US

struct ad7380_timing_specs {};

struct ad7380_chip_info {};

enum {};

/* Extended scan types for 12-bit unsigned chips. */
static const struct iio_scan_type ad7380_scan_type_12_u[] =;

/* Extended scan types for 14-bit signed chips. */
static const struct iio_scan_type ad7380_scan_type_14_s[] =;

/* Extended scan types for 14-bit unsigned chips. */
static const struct iio_scan_type ad7380_scan_type_14_u[] =;

/* Extended scan types for 16-bit signed_chips. */
static const struct iio_scan_type ad7380_scan_type_16_s[] =;

/* Extended scan types for 16-bit unsigned chips. */
static const struct iio_scan_type ad7380_scan_type_16_u[] =;

#define AD7380_CHANNEL(index, bits, diff, sign)

#define DEFINE_AD7380_2_CHANNEL(name, bits, diff, sign)

#define DEFINE_AD7380_4_CHANNEL(name, bits, diff, sign)

#define DEFINE_AD7380_8_CHANNEL(name, bits, diff, sign)

/* fully differential */
DEFINE_AD7380_2_CHANNEL();
DEFINE_AD7380_2_CHANNEL();
DEFINE_AD7380_4_CHANNEL();
DEFINE_AD7380_4_CHANNEL();
/* pseudo differential */
DEFINE_AD7380_2_CHANNEL();
DEFINE_AD7380_2_CHANNEL();
DEFINE_AD7380_4_CHANNEL();
DEFINE_AD7380_4_CHANNEL();

/* Single ended */
DEFINE_AD7380_4_CHANNEL();
DEFINE_AD7380_4_CHANNEL();
DEFINE_AD7380_4_CHANNEL();
DEFINE_AD7380_8_CHANNEL();
DEFINE_AD7380_8_CHANNEL();
DEFINE_AD7380_8_CHANNEL();

static const char * const ad7380_supplies[] =;

static const char * const ad7380_2_channel_vcm_supplies[] =;

static const char * const ad7380_4_channel_vcm_supplies[] =;

/* Since this is simultaneous sampling, we don't allow individual channels. */
static const unsigned long ad7380_2_channel_scan_masks[] =;

static const unsigned long ad7380_4_channel_scan_masks[] =;

/*
 * Single ended parts have a 2:1 multiplexer in front of each ADC.
 *
 * From an IIO point of view, all inputs are exported, i.e ad7386/7/8
 * export 4 channels and ad7386-4/7-4/8-4 export 8 channels.
 *
 * Inputs AinX0 of multiplexers correspond to the first half of IIO channels
 * (i.e 0-1 or 0-3) and inputs AinX1 correspond to second half (i.e 2-3 or
 * 4-7). Example for AD7386/7/8 (2 channels parts):
 *
 *           IIO   | AD7386/7/8
 *                 |         +----------------------------
 *                 |         |     _____        ______
 *                 |         |    |     |      |      |
 *        voltage0 | AinA0 --|--->|     |      |      |
 *                 |         |    | mux |----->| ADCA |---
 *        voltage2 | AinA1 --|--->|     |      |      |
 *                 |         |    |_____|      |_____ |
 *                 |         |     _____        ______
 *                 |         |    |     |      |      |
 *        voltage1 | AinB0 --|--->|     |      |      |
 *                 |         |    | mux |----->| ADCB |---
 *        voltage3 | AinB1 --|--->|     |      |      |
 *                 |         |    |_____|      |______|
 *                 |         |
 *                 |         +----------------------------
 *
 * Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate
 * scan masks.
 * When sequencer mode is enabled, chip automatically cycles through
 * AinX0 and AinX1 channels. From an IIO point of view, we ca enable all
 * channels, at the cost of an extra read, thus dividing the maximum rate by
 * two.
 */
enum {};

static const unsigned long ad7380_2x2_channel_scan_masks[] =;

static const unsigned long ad7380_2x4_channel_scan_masks[] =;

static const struct ad7380_timing_specs ad7380_timing =;

static const struct ad7380_timing_specs ad7380_4_timing =;

/*
 * Available oversampling ratios. The indices correspond with the bit value
 * expected by the chip.  The available ratios depend on the averaging mode,
 * only normal averaging is supported for now.
 */
static const int ad7380_oversampling_ratios[] =;

static const struct ad7380_chip_info ad7380_chip_info =;

static const struct ad7380_chip_info ad7381_chip_info =;

static const struct ad7380_chip_info ad7383_chip_info =;

static const struct ad7380_chip_info ad7384_chip_info =;

static const struct ad7380_chip_info ad7386_chip_info =;

static const struct ad7380_chip_info ad7387_chip_info =;

static const struct ad7380_chip_info ad7388_chip_info =;

static const struct ad7380_chip_info ad7380_4_chip_info =;

static const struct ad7380_chip_info ad7381_4_chip_info =;

static const struct ad7380_chip_info ad7383_4_chip_info =;

static const struct ad7380_chip_info ad7384_4_chip_info =;

static const struct ad7380_chip_info ad7386_4_chip_info =;

static const struct ad7380_chip_info ad7387_4_chip_info =;

static const struct ad7380_chip_info ad7388_4_chip_info =;

struct ad7380_state {};

static int ad7380_regmap_reg_write(void *context, unsigned int reg,
				   unsigned int val)
{}

static int ad7380_regmap_reg_read(void *context, unsigned int reg,
				  unsigned int *val)
{}

static const struct regmap_config ad7380_regmap_config =;

static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg,
				     u32 writeval, u32 *readval)
{}

/*
 * When switching channel, the ADC require an additional settling time.
 * According to the datasheet, data is value on the third CS low. We already
 * have an extra toggle before each read (either direct reads or buffered reads)
 * to sample correct data, so we just add a single CS toggle at the end of the
 * register write.
 */
static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch)
{}

/**
 * ad7380_update_xfers - update the SPI transfers base on the current scan type
 * @st:		device instance specific state
 * @scan_type:	current scan type
 */
static void ad7380_update_xfers(struct ad7380_state *st,
				const struct iio_scan_type *scan_type)
{}

static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev)
{}

static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev)
{}

static const struct iio_buffer_setup_ops ad7380_buffer_setup_ops =;

static irqreturn_t ad7380_trigger_handler(int irq, void *p)
{}

static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index,
			      const struct iio_scan_type *scan_type, int *val)
{}

static int ad7380_read_raw(struct iio_dev *indio_dev,
			   struct iio_chan_spec const *chan,
			   int *val, int *val2, long info)
{}

static int ad7380_read_avail(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     const int **vals, int *type, int *length,
			     long mask)
{}

/**
 * ad7380_osr_to_regval - convert ratio to OSR register value
 * @ratio: ratio to check
 *
 * Check if ratio is present in the list of available ratios and return the
 * corresponding value that needs to be written to the register to select that
 * ratio.
 *
 * Returns: register value (0 to 7) or -EINVAL if there is not an exact match
 */
static int ad7380_osr_to_regval(int ratio)
{}

static int ad7380_write_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan, int val,
			    int val2, long mask)
{}

static int ad7380_get_current_scan_type(const struct iio_dev *indio_dev,
					const struct iio_chan_spec *chan)
{}

static const struct iio_info ad7380_info =;

static int ad7380_init(struct ad7380_state *st, bool external_ref_en)
{}

static int ad7380_probe(struct spi_device *spi)
{}

static const struct of_device_id ad7380_of_match_table[] =;

static const struct spi_device_id ad7380_id_table[] =;
MODULE_DEVICE_TABLE(spi, ad7380_id_table);

static struct spi_driver ad7380_driver =;
module_spi_driver();

MODULE_AUTHOR();
MODULE_DESCRIPTION();
MODULE_LICENSE();