Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * AD7190 AD7192 AD7193 AD7195 SPI ADC driver
   4 *
   5 * Copyright 2011-2015 Analog Devices Inc.
   6 */
   7
   8#include <linux/interrupt.h>
 
   9#include <linux/clk.h>
 
  10#include <linux/device.h>
  11#include <linux/kernel.h>
  12#include <linux/slab.h>
  13#include <linux/sysfs.h>
  14#include <linux/spi/spi.h>
  15#include <linux/regulator/consumer.h>
  16#include <linux/err.h>
  17#include <linux/sched.h>
  18#include <linux/delay.h>
  19#include <linux/of_device.h>
 
 
 
  20
  21#include <linux/iio/iio.h>
  22#include <linux/iio/sysfs.h>
  23#include <linux/iio/buffer.h>
  24#include <linux/iio/trigger.h>
  25#include <linux/iio/trigger_consumer.h>
  26#include <linux/iio/triggered_buffer.h>
  27#include <linux/iio/adc/ad_sigma_delta.h>
  28
  29/* Registers */
  30#define AD7192_REG_COMM		0 /* Communications Register (WO, 8-bit) */
  31#define AD7192_REG_STAT		0 /* Status Register	     (RO, 8-bit) */
  32#define AD7192_REG_MODE		1 /* Mode Register	     (RW, 24-bit */
  33#define AD7192_REG_CONF		2 /* Configuration Register  (RW, 24-bit) */
  34#define AD7192_REG_DATA		3 /* Data Register	     (RO, 24/32-bit) */
  35#define AD7192_REG_ID		4 /* ID Register	     (RO, 8-bit) */
  36#define AD7192_REG_GPOCON	5 /* GPOCON Register	     (RO, 8-bit) */
  37#define AD7192_REG_OFFSET	6 /* Offset Register	     (RW, 16-bit */
  38				  /* (AD7792)/24-bit (AD7192)) */
  39#define AD7192_REG_FULLSALE	7 /* Full-Scale Register */
  40				  /* (RW, 16-bit (AD7792)/24-bit (AD7192)) */
  41
  42/* Communications Register Bit Designations (AD7192_REG_COMM) */
  43#define AD7192_COMM_WEN		BIT(7) /* Write Enable */
  44#define AD7192_COMM_WRITE	0 /* Write Operation */
  45#define AD7192_COMM_READ	BIT(6) /* Read Operation */
  46#define AD7192_COMM_ADDR(x)	(((x) & 0x7) << 3) /* Register Address */
  47#define AD7192_COMM_CREAD	BIT(2) /* Continuous Read of Data Register */
  48
  49/* Status Register Bit Designations (AD7192_REG_STAT) */
  50#define AD7192_STAT_RDY		BIT(7) /* Ready */
  51#define AD7192_STAT_ERR		BIT(6) /* Error (Overrange, Underrange) */
  52#define AD7192_STAT_NOREF	BIT(5) /* Error no external reference */
  53#define AD7192_STAT_PARITY	BIT(4) /* Parity */
  54#define AD7192_STAT_CH3		BIT(2) /* Channel 3 */
  55#define AD7192_STAT_CH2		BIT(1) /* Channel 2 */
  56#define AD7192_STAT_CH1		BIT(0) /* Channel 1 */
  57
  58/* Mode Register Bit Designations (AD7192_REG_MODE) */
  59#define AD7192_MODE_SEL(x)	(((x) & 0x7) << 21) /* Operation Mode Select */
  60#define AD7192_MODE_SEL_MASK	(0x7 << 21) /* Operation Mode Select Mask */
  61#define AD7192_MODE_DAT_STA	BIT(20) /* Status Register transmission */
  62#define AD7192_MODE_CLKSRC(x)	(((x) & 0x3) << 18) /* Clock Source Select */
 
  63#define AD7192_MODE_SINC3	BIT(15) /* SINC3 Filter Select */
  64#define AD7192_MODE_ACX		BIT(14) /* AC excitation enable(AD7195 only)*/
  65#define AD7192_MODE_ENPAR	BIT(13) /* Parity Enable */
  66#define AD7192_MODE_CLKDIV	BIT(12) /* Clock divide by 2 (AD7190/2 only)*/
  67#define AD7192_MODE_SCYCLE	BIT(11) /* Single cycle conversion */
  68#define AD7192_MODE_REJ60	BIT(10) /* 50/60Hz notch filter */
  69#define AD7192_MODE_RATE(x)	((x) & 0x3FF) /* Filter Update Rate Select */
 
  70
  71/* Mode Register: AD7192_MODE_SEL options */
  72#define AD7192_MODE_CONT		0 /* Continuous Conversion Mode */
  73#define AD7192_MODE_SINGLE		1 /* Single Conversion Mode */
  74#define AD7192_MODE_IDLE		2 /* Idle Mode */
  75#define AD7192_MODE_PWRDN		3 /* Power-Down Mode */
  76#define AD7192_MODE_CAL_INT_ZERO	4 /* Internal Zero-Scale Calibration */
  77#define AD7192_MODE_CAL_INT_FULL	5 /* Internal Full-Scale Calibration */
  78#define AD7192_MODE_CAL_SYS_ZERO	6 /* System Zero-Scale Calibration */
  79#define AD7192_MODE_CAL_SYS_FULL	7 /* System Full-Scale Calibration */
  80
  81/* Mode Register: AD7192_MODE_CLKSRC options */
  82#define AD7192_CLK_EXT_MCLK1_2		0 /* External 4.92 MHz Clock connected*/
  83					  /* from MCLK1 to MCLK2 */
  84#define AD7192_CLK_EXT_MCLK2		1 /* External Clock applied to MCLK2 */
  85#define AD7192_CLK_INT			2 /* Internal 4.92 MHz Clock not */
  86					  /* available at the MCLK2 pin */
  87#define AD7192_CLK_INT_CO		3 /* Internal 4.92 MHz Clock available*/
  88					  /* at the MCLK2 pin */
  89
  90/* Configuration Register Bit Designations (AD7192_REG_CONF) */
  91
  92#define AD7192_CONF_CHOP	BIT(23) /* CHOP enable */
 
  93#define AD7192_CONF_REFSEL	BIT(20) /* REFIN1/REFIN2 Reference Select */
  94#define AD7192_CONF_CHAN(x)	((x) << 8) /* Channel select */
  95#define AD7192_CONF_CHAN_MASK	(0x7FF << 8) /* Channel select mask */
  96#define AD7192_CONF_BURN	BIT(7) /* Burnout current enable */
  97#define AD7192_CONF_REFDET	BIT(6) /* Reference detect enable */
  98#define AD7192_CONF_BUF		BIT(4) /* Buffered Mode Enable */
  99#define AD7192_CONF_UNIPOLAR	BIT(3) /* Unipolar/Bipolar Enable */
 100#define AD7192_CONF_GAIN(x)	((x) & 0x7) /* Gain Select */
 101
 102#define AD7192_CH_AIN1P_AIN2M	BIT(0) /* AIN1(+) - AIN2(-) */
 103#define AD7192_CH_AIN3P_AIN4M	BIT(1) /* AIN3(+) - AIN4(-) */
 104#define AD7192_CH_TEMP		BIT(2) /* Temp Sensor */
 105#define AD7192_CH_AIN2P_AIN2M	BIT(3) /* AIN2(+) - AIN2(-) */
 106#define AD7192_CH_AIN1		BIT(4) /* AIN1 - AINCOM */
 107#define AD7192_CH_AIN2		BIT(5) /* AIN2 - AINCOM */
 108#define AD7192_CH_AIN3		BIT(6) /* AIN3 - AINCOM */
 109#define AD7192_CH_AIN4		BIT(7) /* AIN4 - AINCOM */
 110
 111#define AD7193_CH_AIN1P_AIN2M	0x001  /* AIN1(+) - AIN2(-) */
 112#define AD7193_CH_AIN3P_AIN4M	0x002  /* AIN3(+) - AIN4(-) */
 113#define AD7193_CH_AIN5P_AIN6M	0x004  /* AIN5(+) - AIN6(-) */
 114#define AD7193_CH_AIN7P_AIN8M	0x008  /* AIN7(+) - AIN8(-) */
 115#define AD7193_CH_TEMP		0x100 /* Temp senseor */
 116#define AD7193_CH_AIN2P_AIN2M	0x200 /* AIN2(+) - AIN2(-) */
 117#define AD7193_CH_AIN1		0x401 /* AIN1 - AINCOM */
 118#define AD7193_CH_AIN2		0x402 /* AIN2 - AINCOM */
 119#define AD7193_CH_AIN3		0x404 /* AIN3 - AINCOM */
 120#define AD7193_CH_AIN4		0x408 /* AIN4 - AINCOM */
 121#define AD7193_CH_AIN5		0x410 /* AIN5 - AINCOM */
 122#define AD7193_CH_AIN6		0x420 /* AIN6 - AINCOM */
 123#define AD7193_CH_AIN7		0x440 /* AIN7 - AINCOM */
 124#define AD7193_CH_AIN8		0x480 /* AIN7 - AINCOM */
 125#define AD7193_CH_AINCOM	0x600 /* AINCOM - AINCOM */
 126
 
 
 
 
 
 
 
 
 
 
 
 
 
 127/* ID Register Bit Designations (AD7192_REG_ID) */
 128#define CHIPID_AD7190		0x4
 129#define CHIPID_AD7192		0x0
 130#define CHIPID_AD7193		0x2
 
 131#define CHIPID_AD7195		0x6
 132#define AD7192_ID_MASK		0x0F
 133
 134/* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
 135#define AD7192_GPOCON_BPDSW	BIT(6) /* Bridge power-down switch enable */
 136#define AD7192_GPOCON_GP32EN	BIT(5) /* Digital Output P3 and P2 enable */
 137#define AD7192_GPOCON_GP10EN	BIT(4) /* Digital Output P1 and P0 enable */
 138#define AD7192_GPOCON_P3DAT	BIT(3) /* P3 state */
 139#define AD7192_GPOCON_P2DAT	BIT(2) /* P2 state */
 140#define AD7192_GPOCON_P1DAT	BIT(1) /* P1 state */
 141#define AD7192_GPOCON_P0DAT	BIT(0) /* P0 state */
 142
 143#define AD7192_EXT_FREQ_MHZ_MIN	2457600
 144#define AD7192_EXT_FREQ_MHZ_MAX	5120000
 145#define AD7192_INT_FREQ_MHZ	4915200
 146
 147#define AD7192_NO_SYNC_FILTER	1
 148#define AD7192_SYNC3_FILTER	3
 149#define AD7192_SYNC4_FILTER	4
 150
 151/* NOTE:
 152 * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
 153 * In order to avoid contentions on the SPI bus, it's therefore necessary
 154 * to use spi bus locking.
 155 *
 156 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
 157 */
 158
 159enum {
 160	AD7192_SYSCALIB_ZERO_SCALE,
 161	AD7192_SYSCALIB_FULL_SCALE,
 162};
 163
 164enum {
 165	ID_AD7190,
 166	ID_AD7192,
 167	ID_AD7193,
 
 168	ID_AD7195,
 169};
 170
 171struct ad7192_chip_info {
 172	unsigned int			chip_id;
 173	const char			*name;
 
 
 
 
 
 174};
 175
 176struct ad7192_state {
 177	const struct ad7192_chip_info	*chip_info;
 178	struct regulator		*avdd;
 179	struct regulator		*dvdd;
 180	struct clk			*mclk;
 
 181	u16				int_vref_mv;
 
 182	u32				fclk;
 183	u32				f_order;
 184	u32				mode;
 185	u32				conf;
 186	u32				scale_avail[8][2];
 
 
 187	u8				gpocon;
 188	u8				clock_sel;
 189	struct mutex			lock;	/* protect sensor state */
 190	u8				syscalib_mode[8];
 191
 192	struct ad_sigma_delta		sd;
 193};
 194
 195static const char * const ad7192_syscalib_modes[] = {
 196	[AD7192_SYSCALIB_ZERO_SCALE] = "zero_scale",
 197	[AD7192_SYSCALIB_FULL_SCALE] = "full_scale",
 198};
 199
 200static int ad7192_set_syscalib_mode(struct iio_dev *indio_dev,
 201				    const struct iio_chan_spec *chan,
 202				    unsigned int mode)
 203{
 204	struct ad7192_state *st = iio_priv(indio_dev);
 205
 206	st->syscalib_mode[chan->channel] = mode;
 207
 208	return 0;
 209}
 210
 211static int ad7192_get_syscalib_mode(struct iio_dev *indio_dev,
 212				    const struct iio_chan_spec *chan)
 213{
 214	struct ad7192_state *st = iio_priv(indio_dev);
 215
 216	return st->syscalib_mode[chan->channel];
 217}
 218
 219static ssize_t ad7192_write_syscalib(struct iio_dev *indio_dev,
 220				     uintptr_t private,
 221				     const struct iio_chan_spec *chan,
 222				     const char *buf, size_t len)
 223{
 224	struct ad7192_state *st = iio_priv(indio_dev);
 225	bool sys_calib;
 226	int ret, temp;
 227
 228	ret = strtobool(buf, &sys_calib);
 229	if (ret)
 230		return ret;
 231
 232	temp = st->syscalib_mode[chan->channel];
 233	if (sys_calib) {
 234		if (temp == AD7192_SYSCALIB_ZERO_SCALE)
 235			ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_ZERO,
 236					      chan->address);
 237		else
 238			ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_FULL,
 239					      chan->address);
 240	}
 241
 242	return ret ? ret : len;
 243}
 244
 245static const struct iio_enum ad7192_syscalib_mode_enum = {
 246	.items = ad7192_syscalib_modes,
 247	.num_items = ARRAY_SIZE(ad7192_syscalib_modes),
 248	.set = ad7192_set_syscalib_mode,
 249	.get = ad7192_get_syscalib_mode
 250};
 251
 252static const struct iio_chan_spec_ext_info ad7192_calibsys_ext_info[] = {
 253	{
 254		.name = "sys_calibration",
 255		.write = ad7192_write_syscalib,
 256		.shared = IIO_SEPARATE,
 257	},
 258	IIO_ENUM("sys_calibration_mode", IIO_SEPARATE,
 259		 &ad7192_syscalib_mode_enum),
 260	IIO_ENUM_AVAILABLE("sys_calibration_mode", &ad7192_syscalib_mode_enum),
 261	{}
 
 262};
 263
 264static struct ad7192_state *ad_sigma_delta_to_ad7192(struct ad_sigma_delta *sd)
 265{
 266	return container_of(sd, struct ad7192_state, sd);
 267}
 268
 269static int ad7192_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
 270{
 271	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 272
 273	st->conf &= ~AD7192_CONF_CHAN_MASK;
 274	st->conf |= AD7192_CONF_CHAN(channel);
 275
 276	return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 277}
 278
 279static int ad7192_set_mode(struct ad_sigma_delta *sd,
 280			   enum ad_sigma_delta_mode mode)
 281{
 282	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 283
 284	st->mode &= ~AD7192_MODE_SEL_MASK;
 285	st->mode |= AD7192_MODE_SEL(mode);
 286
 287	return ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 288}
 289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 290static const struct ad_sigma_delta_info ad7192_sigma_delta_info = {
 291	.set_channel = ad7192_set_channel,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 292	.set_mode = ad7192_set_mode,
 293	.has_registers = true,
 294	.addr_shift = 3,
 295	.read_mask = BIT(6),
 
 
 296};
 297
 298static const struct ad_sd_calib_data ad7192_calib_arr[8] = {
 299	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
 300	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
 301	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
 302	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
 303	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
 304	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
 305	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
 306	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
 307};
 308
 309static int ad7192_calibrate_all(struct ad7192_state *st)
 310{
 311	return ad_sd_calibrate_all(&st->sd, ad7192_calib_arr,
 312				   ARRAY_SIZE(ad7192_calib_arr));
 313}
 314
 315static inline bool ad7192_valid_external_frequency(u32 freq)
 316{
 317	return (freq >= AD7192_EXT_FREQ_MHZ_MIN &&
 318		freq <= AD7192_EXT_FREQ_MHZ_MAX);
 319}
 320
 321static int ad7192_of_clock_select(struct ad7192_state *st)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 322{
 323	struct device_node *np = st->sd.spi->dev.of_node;
 324	unsigned int clock_sel;
 325
 326	clock_sel = AD7192_CLK_INT;
 
 
 327
 328	/* use internal clock */
 329	if (PTR_ERR(st->mclk) == -ENOENT) {
 330		if (of_property_read_bool(np, "adi,int-clock-output-enable"))
 331			clock_sel = AD7192_CLK_INT_CO;
 332	} else {
 333		if (of_property_read_bool(np, "adi,clock-xtal"))
 334			clock_sel = AD7192_CLK_EXT_MCLK1_2;
 335		else
 336			clock_sel = AD7192_CLK_EXT_MCLK2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 337	}
 338
 339	return clock_sel;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 340}
 341
 342static int ad7192_setup(struct ad7192_state *st, struct device_node *np)
 343{
 344	struct iio_dev *indio_dev = spi_get_drvdata(st->sd.spi);
 345	bool rej60_en, refin2_en;
 346	bool buf_en, bipolar, burnout_curr_en;
 347	unsigned long long scale_uv;
 348	int i, ret, id;
 349
 350	/* reset the serial interface */
 351	ret = ad_sd_reset(&st->sd, 48);
 352	if (ret < 0)
 353		return ret;
 354	usleep_range(500, 1000); /* Wait for at least 500us */
 355
 356	/* write/read test for device presence */
 357	ret = ad_sd_read_reg(&st->sd, AD7192_REG_ID, 1, &id);
 358	if (ret)
 359		return ret;
 360
 361	id &= AD7192_ID_MASK;
 362
 363	if (id != st->chip_info->chip_id)
 364		dev_warn(&st->sd.spi->dev, "device ID query failed (0x%X)\n",
 365			 id);
 366
 367	st->mode = AD7192_MODE_SEL(AD7192_MODE_IDLE) |
 368		AD7192_MODE_CLKSRC(st->clock_sel) |
 369		AD7192_MODE_RATE(480);
 370
 371	st->conf = AD7192_CONF_GAIN(0);
 372
 373	rej60_en = of_property_read_bool(np, "adi,rejection-60-Hz-enable");
 374	if (rej60_en)
 375		st->mode |= AD7192_MODE_REJ60;
 376
 377	refin2_en = of_property_read_bool(np, "adi,refin2-pins-enable");
 378	if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195)
 379		st->conf |= AD7192_CONF_REFSEL;
 380
 381	st->conf &= ~AD7192_CONF_CHOP;
 382	st->f_order = AD7192_NO_SYNC_FILTER;
 383
 384	buf_en = of_property_read_bool(np, "adi,buffer-enable");
 385	if (buf_en)
 386		st->conf |= AD7192_CONF_BUF;
 387
 388	bipolar = of_property_read_bool(np, "bipolar");
 389	if (!bipolar)
 390		st->conf |= AD7192_CONF_UNIPOLAR;
 391
 392	burnout_curr_en = of_property_read_bool(np,
 393						"adi,burnout-currents-enable");
 394	if (burnout_curr_en && buf_en) {
 395		st->conf |= AD7192_CONF_BURN;
 396	} else if (burnout_curr_en) {
 397		dev_warn(&st->sd.spi->dev,
 398			 "Can't enable burnout currents: see CHOP or buffer\n");
 399	}
 400
 401	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 402	if (ret)
 403		return ret;
 404
 405	ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 406	if (ret)
 407		return ret;
 408
 409	ret = ad7192_calibrate_all(st);
 410	if (ret)
 411		return ret;
 412
 413	/* Populate available ADC input ranges */
 414	for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
 415		scale_uv = ((u64)st->int_vref_mv * 100000000)
 416			>> (indio_dev->channels[0].scan_type.realbits -
 417			((st->conf & AD7192_CONF_UNIPOLAR) ? 0 : 1));
 418		scale_uv >>= i;
 419
 420		st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
 421		st->scale_avail[i][0] = scale_uv;
 422	}
 423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 424	return 0;
 425}
 426
 427static ssize_t ad7192_show_ac_excitation(struct device *dev,
 428					 struct device_attribute *attr,
 429					 char *buf)
 430{
 431	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 432	struct ad7192_state *st = iio_priv(indio_dev);
 433
 434	return sprintf(buf, "%d\n", !!(st->mode & AD7192_MODE_ACX));
 435}
 436
 437static ssize_t ad7192_show_bridge_switch(struct device *dev,
 438					 struct device_attribute *attr,
 439					 char *buf)
 440{
 441	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 442	struct ad7192_state *st = iio_priv(indio_dev);
 443
 444	return sprintf(buf, "%d\n", !!(st->gpocon & AD7192_GPOCON_BPDSW));
 
 445}
 446
 447static ssize_t ad7192_set(struct device *dev,
 448			  struct device_attribute *attr,
 449			  const char *buf,
 450			  size_t len)
 451{
 452	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 453	struct ad7192_state *st = iio_priv(indio_dev);
 454	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 455	int ret;
 456	bool val;
 457
 458	ret = strtobool(buf, &val);
 459	if (ret < 0)
 460		return ret;
 461
 462	ret = iio_device_claim_direct_mode(indio_dev);
 463	if (ret)
 464		return ret;
 465
 466	switch ((u32)this_attr->address) {
 467	case AD7192_REG_GPOCON:
 468		if (val)
 469			st->gpocon |= AD7192_GPOCON_BPDSW;
 470		else
 471			st->gpocon &= ~AD7192_GPOCON_BPDSW;
 472
 473		ad_sd_write_reg(&st->sd, AD7192_REG_GPOCON, 1, st->gpocon);
 474		break;
 475	case AD7192_REG_MODE:
 476		if (val)
 477			st->mode |= AD7192_MODE_ACX;
 478		else
 479			st->mode &= ~AD7192_MODE_ACX;
 480
 481		ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 482		break;
 483	default:
 484		ret = -EINVAL;
 485	}
 486
 487	iio_device_release_direct_mode(indio_dev);
 488
 489	return ret ? ret : len;
 490}
 491
 492static void ad7192_get_available_filter_freq(struct ad7192_state *st,
 493						    int *freq)
 494{
 495	unsigned int fadc;
 496
 497	/* Formulas for filter at page 25 of the datasheet */
 498	fadc = DIV_ROUND_CLOSEST(st->fclk,
 499				 AD7192_SYNC4_FILTER * AD7192_MODE_RATE(st->mode));
 500	freq[0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
 501
 502	fadc = DIV_ROUND_CLOSEST(st->fclk,
 503				 AD7192_SYNC3_FILTER * AD7192_MODE_RATE(st->mode));
 504	freq[1] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
 505
 506	fadc = DIV_ROUND_CLOSEST(st->fclk, AD7192_MODE_RATE(st->mode));
 507	freq[2] = DIV_ROUND_CLOSEST(fadc * 230, 1024);
 508	freq[3] = DIV_ROUND_CLOSEST(fadc * 272, 1024);
 509}
 510
 511static ssize_t ad7192_show_filter_avail(struct device *dev,
 512					struct device_attribute *attr,
 513					char *buf)
 514{
 515	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 516	struct ad7192_state *st = iio_priv(indio_dev);
 517	unsigned int freq_avail[4], i;
 518	size_t len = 0;
 519
 520	ad7192_get_available_filter_freq(st, freq_avail);
 
 521
 522	for (i = 0; i < ARRAY_SIZE(freq_avail); i++)
 523		len += scnprintf(buf + len, PAGE_SIZE - len,
 524				 "%d.%d ", freq_avail[i] / 1000,
 525				 freq_avail[i] % 1000);
 526
 527	buf[len - 1] = '\n';
 
 
 
 
 
 
 
 
 
 
 
 528
 529	return len;
 
 530}
 531
 532static IIO_DEVICE_ATTR(filter_low_pass_3db_frequency_available,
 533		       0444, ad7192_show_filter_avail, NULL, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 534
 535static IIO_DEVICE_ATTR(bridge_switch_en, 0644,
 536		       ad7192_show_bridge_switch, ad7192_set,
 537		       AD7192_REG_GPOCON);
 538
 539static IIO_DEVICE_ATTR(ac_excitation_en, 0644,
 540		       ad7192_show_ac_excitation, ad7192_set,
 541		       AD7192_REG_MODE);
 542
 543static struct attribute *ad7192_attributes[] = {
 544	&iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
 545	&iio_dev_attr_bridge_switch_en.dev_attr.attr,
 546	&iio_dev_attr_ac_excitation_en.dev_attr.attr,
 547	NULL
 548};
 549
 550static const struct attribute_group ad7192_attribute_group = {
 551	.attrs = ad7192_attributes,
 552};
 553
 554static struct attribute *ad7195_attributes[] = {
 555	&iio_dev_attr_filter_low_pass_3db_frequency_available.dev_attr.attr,
 556	&iio_dev_attr_bridge_switch_en.dev_attr.attr,
 
 557	NULL
 558};
 559
 560static const struct attribute_group ad7195_attribute_group = {
 561	.attrs = ad7195_attributes,
 562};
 563
 564static unsigned int ad7192_get_temp_scale(bool unipolar)
 565{
 566	return unipolar ? 2815 * 2 : 2815;
 567}
 568
 569static int ad7192_set_3db_filter_freq(struct ad7192_state *st,
 570				      int val, int val2)
 571{
 572	int freq_avail[4], i, ret, freq;
 573	unsigned int diff_new, diff_old;
 574	int idx = 0;
 575
 576	diff_old = U32_MAX;
 577	freq = val * 1000 + val2;
 578
 579	ad7192_get_available_filter_freq(st, freq_avail);
 580
 581	for (i = 0; i < ARRAY_SIZE(freq_avail); i++) {
 582		diff_new = abs(freq - freq_avail[i]);
 583		if (diff_new < diff_old) {
 584			diff_old = diff_new;
 585			idx = i;
 586		}
 587	}
 588
 589	switch (idx) {
 590	case 0:
 591		st->f_order = AD7192_SYNC4_FILTER;
 592		st->mode &= ~AD7192_MODE_SINC3;
 593
 594		st->conf |= AD7192_CONF_CHOP;
 595		break;
 596	case 1:
 597		st->f_order = AD7192_SYNC3_FILTER;
 598		st->mode |= AD7192_MODE_SINC3;
 599
 600		st->conf |= AD7192_CONF_CHOP;
 601		break;
 602	case 2:
 603		st->f_order = AD7192_NO_SYNC_FILTER;
 604		st->mode &= ~AD7192_MODE_SINC3;
 605
 606		st->conf &= ~AD7192_CONF_CHOP;
 607		break;
 608	case 3:
 609		st->f_order = AD7192_NO_SYNC_FILTER;
 610		st->mode |= AD7192_MODE_SINC3;
 611
 612		st->conf &= ~AD7192_CONF_CHOP;
 613		break;
 614	}
 615
 616	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 617	if (ret < 0)
 618		return ret;
 619
 620	return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 621}
 622
 623static int ad7192_get_3db_filter_freq(struct ad7192_state *st)
 624{
 625	unsigned int fadc;
 626
 627	fadc = DIV_ROUND_CLOSEST(st->fclk,
 628				 st->f_order * AD7192_MODE_RATE(st->mode));
 629
 630	if (st->conf & AD7192_CONF_CHOP)
 631		return DIV_ROUND_CLOSEST(fadc * 240, 1024);
 632	if (st->mode & AD7192_MODE_SINC3)
 633		return DIV_ROUND_CLOSEST(fadc * 272, 1024);
 634	else
 635		return DIV_ROUND_CLOSEST(fadc * 230, 1024);
 636}
 637
 638static int ad7192_read_raw(struct iio_dev *indio_dev,
 639			   struct iio_chan_spec const *chan,
 640			   int *val,
 641			   int *val2,
 642			   long m)
 643{
 644	struct ad7192_state *st = iio_priv(indio_dev);
 645	bool unipolar = !!(st->conf & AD7192_CONF_UNIPOLAR);
 
 646
 647	switch (m) {
 648	case IIO_CHAN_INFO_RAW:
 649		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
 650	case IIO_CHAN_INFO_SCALE:
 651		switch (chan->type) {
 652		case IIO_VOLTAGE:
 653			mutex_lock(&st->lock);
 654			*val = st->scale_avail[AD7192_CONF_GAIN(st->conf)][0];
 655			*val2 = st->scale_avail[AD7192_CONF_GAIN(st->conf)][1];
 656			mutex_unlock(&st->lock);
 657			return IIO_VAL_INT_PLUS_NANO;
 658		case IIO_TEMP:
 659			*val = 0;
 660			*val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
 661			return IIO_VAL_INT_PLUS_NANO;
 662		default:
 663			return -EINVAL;
 664		}
 665	case IIO_CHAN_INFO_OFFSET:
 666		if (!unipolar)
 667			*val = -(1 << (chan->scan_type.realbits - 1));
 668		else
 669			*val = 0;
 
 
 
 
 
 
 
 
 
 
 
 670		/* Kelvin to Celsius */
 671		if (chan->type == IIO_TEMP)
 672			*val -= 273 * ad7192_get_temp_scale(unipolar);
 673		return IIO_VAL_INT;
 
 
 
 674	case IIO_CHAN_INFO_SAMP_FREQ:
 675		*val = st->fclk /
 676			(st->f_order * 1024 * AD7192_MODE_RATE(st->mode));
 677		return IIO_VAL_INT;
 678	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 679		*val = ad7192_get_3db_filter_freq(st);
 680		*val2 = 1000;
 681		return IIO_VAL_FRACTIONAL;
 
 
 
 682	}
 683
 684	return -EINVAL;
 685}
 686
 687static int ad7192_write_raw(struct iio_dev *indio_dev,
 688			    struct iio_chan_spec const *chan,
 689			    int val,
 690			    int val2,
 691			    long mask)
 692{
 693	struct ad7192_state *st = iio_priv(indio_dev);
 694	int ret, i, div;
 695	unsigned int tmp;
 696
 697	ret = iio_device_claim_direct_mode(indio_dev);
 698	if (ret)
 699		return ret;
 700
 
 
 701	switch (mask) {
 702	case IIO_CHAN_INFO_SCALE:
 703		ret = -EINVAL;
 704		mutex_lock(&st->lock);
 705		for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
 706			if (val2 == st->scale_avail[i][1]) {
 707				ret = 0;
 708				tmp = st->conf;
 709				st->conf &= ~AD7192_CONF_GAIN(-1);
 710				st->conf |= AD7192_CONF_GAIN(i);
 711				if (tmp == st->conf)
 712					break;
 713				ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
 714						3, st->conf);
 715				ad7192_calibrate_all(st);
 716				break;
 717			}
 718		mutex_unlock(&st->lock);
 719		break;
 720	case IIO_CHAN_INFO_SAMP_FREQ:
 721		if (!val) {
 722			ret = -EINVAL;
 723			break;
 724		}
 725
 726		div = st->fclk / (val * st->f_order * 1024);
 727		if (div < 1 || div > 1023) {
 728			ret = -EINVAL;
 729			break;
 730		}
 731
 732		st->mode &= ~AD7192_MODE_RATE(-1);
 733		st->mode |= AD7192_MODE_RATE(div);
 734		ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 
 735		break;
 736	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 737		ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
 738		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 739	default:
 740		ret = -EINVAL;
 741	}
 742
 
 
 743	iio_device_release_direct_mode(indio_dev);
 744
 745	return ret;
 746}
 747
 748static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
 749				    struct iio_chan_spec const *chan,
 750				    long mask)
 751{
 752	switch (mask) {
 753	case IIO_CHAN_INFO_SCALE:
 754		return IIO_VAL_INT_PLUS_NANO;
 755	case IIO_CHAN_INFO_SAMP_FREQ:
 756		return IIO_VAL_INT;
 757	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 758		return IIO_VAL_INT_PLUS_MICRO;
 
 
 759	default:
 760		return -EINVAL;
 761	}
 762}
 763
 764static int ad7192_read_avail(struct iio_dev *indio_dev,
 765			     struct iio_chan_spec const *chan,
 766			     const int **vals, int *type, int *length,
 767			     long mask)
 768{
 769	struct ad7192_state *st = iio_priv(indio_dev);
 770
 771	switch (mask) {
 772	case IIO_CHAN_INFO_SCALE:
 773		*vals = (int *)st->scale_avail;
 774		*type = IIO_VAL_INT_PLUS_NANO;
 775		/* Values are stored in a 2D matrix  */
 776		*length = ARRAY_SIZE(st->scale_avail) * 2;
 777
 778		return IIO_AVAIL_LIST;
 
 
 
 
 
 
 
 
 
 
 
 
 779	}
 780
 781	return -EINVAL;
 782}
 783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 784static const struct iio_info ad7192_info = {
 785	.read_raw = ad7192_read_raw,
 786	.write_raw = ad7192_write_raw,
 787	.write_raw_get_fmt = ad7192_write_raw_get_fmt,
 788	.read_avail = ad7192_read_avail,
 789	.attrs = &ad7192_attribute_group,
 790	.validate_trigger = ad_sd_validate_trigger,
 
 
 
 
 
 
 
 
 
 791};
 792
 793static const struct iio_info ad7195_info = {
 794	.read_raw = ad7192_read_raw,
 795	.write_raw = ad7192_write_raw,
 796	.write_raw_get_fmt = ad7192_write_raw_get_fmt,
 797	.read_avail = ad7192_read_avail,
 798	.attrs = &ad7195_attribute_group,
 799	.validate_trigger = ad_sd_validate_trigger,
 
 800};
 801
 802#define __AD719x_CHANNEL(_si, _channel1, _channel2, _address, _extend_name, \
 803	_type, _mask_type_av, _ext_info) \
 804	{ \
 805		.type = (_type), \
 806		.differential = ((_channel2) == -1 ? 0 : 1), \
 807		.indexed = 1, \
 808		.channel = (_channel1), \
 809		.channel2 = (_channel2), \
 810		.address = (_address), \
 811		.extend_name = (_extend_name), \
 812		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 813			BIT(IIO_CHAN_INFO_OFFSET), \
 814		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 815		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
 816			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
 
 817		.info_mask_shared_by_type_available = (_mask_type_av), \
 
 
 
 818		.ext_info = (_ext_info), \
 819		.scan_index = (_si), \
 820		.scan_type = { \
 821			.sign = 'u', \
 822			.realbits = 24, \
 823			.storagebits = 32, \
 824			.endianness = IIO_BE, \
 825		}, \
 826	}
 827
 828#define AD719x_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
 829	__AD719x_CHANNEL(_si, _channel1, _channel2, _address, NULL, \
 830		IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE), \
 831		ad7192_calibsys_ext_info)
 832
 833#define AD719x_CHANNEL(_si, _channel1, _address) \
 834	__AD719x_CHANNEL(_si, _channel1, -1, _address, NULL, IIO_VOLTAGE, \
 835		BIT(IIO_CHAN_INFO_SCALE), ad7192_calibsys_ext_info)
 836
 837#define AD719x_SHORTED_CHANNEL(_si, _channel1, _address) \
 838	__AD719x_CHANNEL(_si, _channel1, -1, _address, "shorted", IIO_VOLTAGE, \
 839		BIT(IIO_CHAN_INFO_SCALE), ad7192_calibsys_ext_info)
 840
 841#define AD719x_TEMP_CHANNEL(_si, _address) \
 842	__AD719x_CHANNEL(_si, 0, -1, _address, NULL, IIO_TEMP, 0, NULL)
 
 
 
 
 
 
 
 
 
 
 
 843
 844static const struct iio_chan_spec ad7192_channels[] = {
 845	AD719x_DIFF_CHANNEL(0, 1, 2, AD7192_CH_AIN1P_AIN2M),
 846	AD719x_DIFF_CHANNEL(1, 3, 4, AD7192_CH_AIN3P_AIN4M),
 847	AD719x_TEMP_CHANNEL(2, AD7192_CH_TEMP),
 848	AD719x_SHORTED_CHANNEL(3, 2, AD7192_CH_AIN2P_AIN2M),
 849	AD719x_CHANNEL(4, 1, AD7192_CH_AIN1),
 850	AD719x_CHANNEL(5, 2, AD7192_CH_AIN2),
 851	AD719x_CHANNEL(6, 3, AD7192_CH_AIN3),
 852	AD719x_CHANNEL(7, 4, AD7192_CH_AIN4),
 853	IIO_CHAN_SOFT_TIMESTAMP(8),
 854};
 855
 856static const struct iio_chan_spec ad7193_channels[] = {
 857	AD719x_DIFF_CHANNEL(0, 1, 2, AD7193_CH_AIN1P_AIN2M),
 858	AD719x_DIFF_CHANNEL(1, 3, 4, AD7193_CH_AIN3P_AIN4M),
 859	AD719x_DIFF_CHANNEL(2, 5, 6, AD7193_CH_AIN5P_AIN6M),
 860	AD719x_DIFF_CHANNEL(3, 7, 8, AD7193_CH_AIN7P_AIN8M),
 861	AD719x_TEMP_CHANNEL(4, AD7193_CH_TEMP),
 862	AD719x_SHORTED_CHANNEL(5, 2, AD7193_CH_AIN2P_AIN2M),
 863	AD719x_CHANNEL(6, 1, AD7193_CH_AIN1),
 864	AD719x_CHANNEL(7, 2, AD7193_CH_AIN2),
 865	AD719x_CHANNEL(8, 3, AD7193_CH_AIN3),
 866	AD719x_CHANNEL(9, 4, AD7193_CH_AIN4),
 867	AD719x_CHANNEL(10, 5, AD7193_CH_AIN5),
 868	AD719x_CHANNEL(11, 6, AD7193_CH_AIN6),
 869	AD719x_CHANNEL(12, 7, AD7193_CH_AIN7),
 870	AD719x_CHANNEL(13, 8, AD7193_CH_AIN8),
 871	IIO_CHAN_SOFT_TIMESTAMP(14),
 872};
 873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 874static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
 875	[ID_AD7190] = {
 876		.chip_id = CHIPID_AD7190,
 877		.name = "ad7190",
 
 
 
 
 878	},
 879	[ID_AD7192] = {
 880		.chip_id = CHIPID_AD7192,
 881		.name = "ad7192",
 
 
 
 
 882	},
 883	[ID_AD7193] = {
 884		.chip_id = CHIPID_AD7193,
 885		.name = "ad7193",
 
 
 
 
 
 
 
 
 
 
 
 886	},
 887	[ID_AD7195] = {
 888		.chip_id = CHIPID_AD7195,
 889		.name = "ad7195",
 
 
 
 
 890	},
 891};
 892
 893static int ad7192_channels_config(struct iio_dev *indio_dev)
 894{
 895	struct ad7192_state *st = iio_priv(indio_dev);
 896
 897	switch (st->chip_info->chip_id) {
 898	case CHIPID_AD7193:
 899		indio_dev->channels = ad7193_channels;
 900		indio_dev->num_channels = ARRAY_SIZE(ad7193_channels);
 901		break;
 902	default:
 903		indio_dev->channels = ad7192_channels;
 904		indio_dev->num_channels = ARRAY_SIZE(ad7192_channels);
 905		break;
 906	}
 907
 908	return 0;
 909}
 910
 911static int ad7192_probe(struct spi_device *spi)
 912{
 
 913	struct ad7192_state *st;
 914	struct iio_dev *indio_dev;
 915	int ret, voltage_uv = 0;
 916
 917	if (!spi->irq) {
 918		dev_err(&spi->dev, "no IRQ?\n");
 919		return -ENODEV;
 920	}
 921
 922	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 923	if (!indio_dev)
 924		return -ENOMEM;
 925
 926	st = iio_priv(indio_dev);
 927
 928	mutex_init(&st->lock);
 929
 930	st->avdd = devm_regulator_get(&spi->dev, "avdd");
 931	if (IS_ERR(st->avdd))
 932		return PTR_ERR(st->avdd);
 933
 934	ret = regulator_enable(st->avdd);
 935	if (ret) {
 936		dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
 937		return ret;
 938	}
 939
 940	st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
 941	if (IS_ERR(st->dvdd)) {
 942		ret = PTR_ERR(st->dvdd);
 943		goto error_disable_avdd;
 
 
 
 
 
 
 
 
 
 
 
 
 944	}
 945
 946	ret = regulator_enable(st->dvdd);
 947	if (ret) {
 948		dev_err(&spi->dev, "Failed to enable specified DVdd supply\n");
 949		goto error_disable_avdd;
 950	}
 951
 952	voltage_uv = regulator_get_voltage(st->avdd);
 953
 954	if (voltage_uv > 0) {
 955		st->int_vref_mv = voltage_uv / 1000;
 956	} else {
 957		ret = voltage_uv;
 958		dev_err(&spi->dev, "Device tree error, reference voltage undefined\n");
 959		goto error_disable_avdd;
 960	}
 961
 962	spi_set_drvdata(spi, indio_dev);
 963	st->chip_info = of_device_get_match_data(&spi->dev);
 964	indio_dev->name = st->chip_info->name;
 965	indio_dev->modes = INDIO_DIRECT_MODE;
 966
 967	ret = ad7192_channels_config(indio_dev);
 968	if (ret < 0)
 969		goto error_disable_dvdd;
 970
 971	if (st->chip_info->chip_id == CHIPID_AD7195)
 972		indio_dev->info = &ad7195_info;
 973	else
 974		indio_dev->info = &ad7192_info;
 975
 976	ad_sd_init(&st->sd, indio_dev, spi, &ad7192_sigma_delta_info);
 977
 978	ret = ad_sd_setup_buffer_and_trigger(indio_dev);
 979	if (ret)
 980		goto error_disable_dvdd;
 981
 982	st->fclk = AD7192_INT_FREQ_MHZ;
 983
 984	st->mclk = devm_clk_get(&st->sd.spi->dev, "mclk");
 985	if (IS_ERR(st->mclk) && PTR_ERR(st->mclk) != -ENOENT) {
 986		ret = PTR_ERR(st->mclk);
 987		goto error_remove_trigger;
 
 
 
 
 
 988	}
 989
 990	st->clock_sel = ad7192_of_clock_select(st);
 991
 992	if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 ||
 993	    st->clock_sel == AD7192_CLK_EXT_MCLK2) {
 994		ret = clk_prepare_enable(st->mclk);
 995		if (ret < 0)
 996			goto error_remove_trigger;
 997
 998		st->fclk = clk_get_rate(st->mclk);
 999		if (!ad7192_valid_external_frequency(st->fclk)) {
1000			ret = -EINVAL;
1001			dev_err(&spi->dev,
1002				"External clock frequency out of bounds\n");
1003			goto error_disable_clk;
1004		}
 
 
 
1005	}
1006
1007	ret = ad7192_setup(st, spi->dev.of_node);
1008	if (ret)
1009		goto error_disable_clk;
1010
1011	ret = iio_device_register(indio_dev);
1012	if (ret < 0)
1013		goto error_disable_clk;
1014	return 0;
1015
1016error_disable_clk:
1017	clk_disable_unprepare(st->mclk);
1018error_remove_trigger:
1019	ad_sd_cleanup_buffer_and_trigger(indio_dev);
1020error_disable_dvdd:
1021	regulator_disable(st->dvdd);
1022error_disable_avdd:
1023	regulator_disable(st->avdd);
1024
1025	return ret;
1026}
1027
1028static int ad7192_remove(struct spi_device *spi)
1029{
1030	struct iio_dev *indio_dev = spi_get_drvdata(spi);
1031	struct ad7192_state *st = iio_priv(indio_dev);
1032
1033	iio_device_unregister(indio_dev);
1034	clk_disable_unprepare(st->mclk);
1035	ad_sd_cleanup_buffer_and_trigger(indio_dev);
1036
1037	regulator_disable(st->dvdd);
1038	regulator_disable(st->avdd);
 
1039
1040	return 0;
1041}
1042
1043static const struct of_device_id ad7192_of_match[] = {
1044	{ .compatible = "adi,ad7190", .data = &ad7192_chip_info_tbl[ID_AD7190] },
1045	{ .compatible = "adi,ad7192", .data = &ad7192_chip_info_tbl[ID_AD7192] },
1046	{ .compatible = "adi,ad7193", .data = &ad7192_chip_info_tbl[ID_AD7193] },
 
1047	{ .compatible = "adi,ad7195", .data = &ad7192_chip_info_tbl[ID_AD7195] },
1048	{}
1049};
1050MODULE_DEVICE_TABLE(of, ad7192_of_match);
1051
 
 
 
 
 
 
 
 
 
 
1052static struct spi_driver ad7192_driver = {
1053	.driver = {
1054		.name	= "ad7192",
1055		.of_match_table = ad7192_of_match,
1056	},
1057	.probe		= ad7192_probe,
1058	.remove		= ad7192_remove,
1059};
1060module_spi_driver(ad7192_driver);
1061
1062MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
1063MODULE_DESCRIPTION("Analog Devices AD7190, AD7192, AD7193, AD7195 ADC");
1064MODULE_LICENSE("GPL v2");
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * AD7192 and similar SPI ADC driver
   4 *
   5 * Copyright 2011-2015 Analog Devices Inc.
   6 */
   7
   8#include <linux/interrupt.h>
   9#include <linux/bitfield.h>
  10#include <linux/clk.h>
  11#include <linux/clk-provider.h>
  12#include <linux/device.h>
  13#include <linux/kernel.h>
  14#include <linux/slab.h>
  15#include <linux/sysfs.h>
  16#include <linux/spi/spi.h>
  17#include <linux/regulator/consumer.h>
  18#include <linux/err.h>
  19#include <linux/sched.h>
  20#include <linux/delay.h>
  21#include <linux/module.h>
  22#include <linux/mod_devicetable.h>
  23#include <linux/property.h>
  24#include <linux/units.h>
  25
  26#include <linux/iio/iio.h>
  27#include <linux/iio/sysfs.h>
  28#include <linux/iio/buffer.h>
  29#include <linux/iio/trigger.h>
  30#include <linux/iio/trigger_consumer.h>
  31#include <linux/iio/triggered_buffer.h>
  32#include <linux/iio/adc/ad_sigma_delta.h>
  33
  34/* Registers */
  35#define AD7192_REG_COMM		0 /* Communications Register (WO, 8-bit) */
  36#define AD7192_REG_STAT		0 /* Status Register	     (RO, 8-bit) */
  37#define AD7192_REG_MODE		1 /* Mode Register	     (RW, 24-bit */
  38#define AD7192_REG_CONF		2 /* Configuration Register  (RW, 24-bit) */
  39#define AD7192_REG_DATA		3 /* Data Register	     (RO, 24/32-bit) */
  40#define AD7192_REG_ID		4 /* ID Register	     (RO, 8-bit) */
  41#define AD7192_REG_GPOCON	5 /* GPOCON Register	     (RO, 8-bit) */
  42#define AD7192_REG_OFFSET	6 /* Offset Register	     (RW, 16-bit */
  43				  /* (AD7792)/24-bit (AD7192)) */
  44#define AD7192_REG_FULLSALE	7 /* Full-Scale Register */
  45				  /* (RW, 16-bit (AD7792)/24-bit (AD7192)) */
  46
  47/* Communications Register Bit Designations (AD7192_REG_COMM) */
  48#define AD7192_COMM_WEN		BIT(7) /* Write Enable */
  49#define AD7192_COMM_WRITE	0 /* Write Operation */
  50#define AD7192_COMM_READ	BIT(6) /* Read Operation */
  51#define AD7192_COMM_ADDR_MASK	GENMASK(5, 3) /* Register Address Mask */
  52#define AD7192_COMM_CREAD	BIT(2) /* Continuous Read of Data Register */
  53
  54/* Status Register Bit Designations (AD7192_REG_STAT) */
  55#define AD7192_STAT_RDY		BIT(7) /* Ready */
  56#define AD7192_STAT_ERR		BIT(6) /* Error (Overrange, Underrange) */
  57#define AD7192_STAT_NOREF	BIT(5) /* Error no external reference */
  58#define AD7192_STAT_PARITY	BIT(4) /* Parity */
  59#define AD7192_STAT_CH3		BIT(2) /* Channel 3 */
  60#define AD7192_STAT_CH2		BIT(1) /* Channel 2 */
  61#define AD7192_STAT_CH1		BIT(0) /* Channel 1 */
  62
  63/* Mode Register Bit Designations (AD7192_REG_MODE) */
  64#define AD7192_MODE_SEL_MASK	GENMASK(23, 21) /* Operation Mode Select Mask */
  65#define AD7192_MODE_STA_MASK	BIT(20) /* Status Register transmission Mask */
  66#define AD7192_MODE_CLKSRC_MASK	GENMASK(19, 18) /* Clock Source Select Mask */
  67#define AD7192_MODE_AVG_MASK	GENMASK(17, 16)
  68		  /* Fast Settling Filter Average Select Mask (AD7193 only) */
  69#define AD7192_MODE_SINC3	BIT(15) /* SINC3 Filter Select */
 
  70#define AD7192_MODE_ENPAR	BIT(13) /* Parity Enable */
  71#define AD7192_MODE_CLKDIV	BIT(12) /* Clock divide by 2 (AD7190/2 only)*/
  72#define AD7192_MODE_SCYCLE	BIT(11) /* Single cycle conversion */
  73#define AD7192_MODE_REJ60	BIT(10) /* 50/60Hz notch filter */
  74				  /* Filter Update Rate Select Mask */
  75#define AD7192_MODE_RATE_MASK	GENMASK(9, 0)
  76
  77/* Mode Register: AD7192_MODE_SEL options */
  78#define AD7192_MODE_CONT		0 /* Continuous Conversion Mode */
  79#define AD7192_MODE_SINGLE		1 /* Single Conversion Mode */
  80#define AD7192_MODE_IDLE		2 /* Idle Mode */
  81#define AD7192_MODE_PWRDN		3 /* Power-Down Mode */
  82#define AD7192_MODE_CAL_INT_ZERO	4 /* Internal Zero-Scale Calibration */
  83#define AD7192_MODE_CAL_INT_FULL	5 /* Internal Full-Scale Calibration */
  84#define AD7192_MODE_CAL_SYS_ZERO	6 /* System Zero-Scale Calibration */
  85#define AD7192_MODE_CAL_SYS_FULL	7 /* System Full-Scale Calibration */
  86
  87/* Mode Register: AD7192_MODE_CLKSRC options */
  88#define AD7192_CLK_EXT_MCLK1_2		0 /* External 4.92 MHz Clock connected*/
  89					  /* from MCLK1 to MCLK2 */
  90#define AD7192_CLK_EXT_MCLK2		1 /* External Clock applied to MCLK2 */
  91#define AD7192_CLK_INT			2 /* Internal 4.92 MHz Clock not */
  92					  /* available at the MCLK2 pin */
  93#define AD7192_CLK_INT_CO		3 /* Internal 4.92 MHz Clock available*/
  94					  /* at the MCLK2 pin */
  95
  96/* Configuration Register Bit Designations (AD7192_REG_CONF) */
  97
  98#define AD7192_CONF_CHOP	BIT(23) /* CHOP enable */
  99#define AD7192_CONF_ACX		BIT(22) /* AC excitation enable(AD7195 only) */
 100#define AD7192_CONF_REFSEL	BIT(20) /* REFIN1/REFIN2 Reference Select */
 101#define AD7192_CONF_CHAN_MASK	GENMASK(18, 8) /* Channel select mask */
 
 102#define AD7192_CONF_BURN	BIT(7) /* Burnout current enable */
 103#define AD7192_CONF_REFDET	BIT(6) /* Reference detect enable */
 104#define AD7192_CONF_BUF		BIT(4) /* Buffered Mode Enable */
 105#define AD7192_CONF_UNIPOLAR	BIT(3) /* Unipolar/Bipolar Enable */
 106#define AD7192_CONF_GAIN_MASK	GENMASK(2, 0) /* Gain Select */
 107
 108#define AD7192_CH_AIN1P_AIN2M	BIT(0) /* AIN1(+) - AIN2(-) */
 109#define AD7192_CH_AIN3P_AIN4M	BIT(1) /* AIN3(+) - AIN4(-) */
 110#define AD7192_CH_TEMP		BIT(2) /* Temp Sensor */
 111#define AD7192_CH_AIN2P_AIN2M	BIT(3) /* AIN2(+) - AIN2(-) */
 112#define AD7192_CH_AIN1		BIT(4) /* AIN1 - AINCOM */
 113#define AD7192_CH_AIN2		BIT(5) /* AIN2 - AINCOM */
 114#define AD7192_CH_AIN3		BIT(6) /* AIN3 - AINCOM */
 115#define AD7192_CH_AIN4		BIT(7) /* AIN4 - AINCOM */
 116
 117#define AD7193_CH_AIN1P_AIN2M	0x001  /* AIN1(+) - AIN2(-) */
 118#define AD7193_CH_AIN3P_AIN4M	0x002  /* AIN3(+) - AIN4(-) */
 119#define AD7193_CH_AIN5P_AIN6M	0x004  /* AIN5(+) - AIN6(-) */
 120#define AD7193_CH_AIN7P_AIN8M	0x008  /* AIN7(+) - AIN8(-) */
 121#define AD7193_CH_TEMP		0x100 /* Temp senseor */
 122#define AD7193_CH_AIN2P_AIN2M	0x200 /* AIN2(+) - AIN2(-) */
 123#define AD7193_CH_AIN1		0x401 /* AIN1 - AINCOM */
 124#define AD7193_CH_AIN2		0x402 /* AIN2 - AINCOM */
 125#define AD7193_CH_AIN3		0x404 /* AIN3 - AINCOM */
 126#define AD7193_CH_AIN4		0x408 /* AIN4 - AINCOM */
 127#define AD7193_CH_AIN5		0x410 /* AIN5 - AINCOM */
 128#define AD7193_CH_AIN6		0x420 /* AIN6 - AINCOM */
 129#define AD7193_CH_AIN7		0x440 /* AIN7 - AINCOM */
 130#define AD7193_CH_AIN8		0x480 /* AIN7 - AINCOM */
 131#define AD7193_CH_AINCOM	0x600 /* AINCOM - AINCOM */
 132
 133#define AD7194_CH_POS(x)	(((x) - 1) << 4)
 134#define AD7194_CH_NEG(x)	((x) - 1)
 135
 136/* 10th bit corresponds to CON18(Pseudo) */
 137#define AD7194_CH(p)		(BIT(10) | AD7194_CH_POS(p))
 138
 139#define AD7194_DIFF_CH(p, n)	(AD7194_CH_POS(p) | AD7194_CH_NEG(n))
 140#define AD7194_CH_TEMP		0x100
 141#define AD7194_CH_BASE_NR	2
 142#define AD7194_CH_AIN_START	1
 143#define AD7194_CH_AIN_NR	16
 144#define AD7194_CH_MAX_NR	272
 145
 146/* ID Register Bit Designations (AD7192_REG_ID) */
 147#define CHIPID_AD7190		0x4
 148#define CHIPID_AD7192		0x0
 149#define CHIPID_AD7193		0x2
 150#define CHIPID_AD7194		0x3
 151#define CHIPID_AD7195		0x6
 152#define AD7192_ID_MASK		GENMASK(3, 0)
 153
 154/* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
 155#define AD7192_GPOCON_BPDSW	BIT(6) /* Bridge power-down switch enable */
 156#define AD7192_GPOCON_GP32EN	BIT(5) /* Digital Output P3 and P2 enable */
 157#define AD7192_GPOCON_GP10EN	BIT(4) /* Digital Output P1 and P0 enable */
 158#define AD7192_GPOCON_P3DAT	BIT(3) /* P3 state */
 159#define AD7192_GPOCON_P2DAT	BIT(2) /* P2 state */
 160#define AD7192_GPOCON_P1DAT	BIT(1) /* P1 state */
 161#define AD7192_GPOCON_P0DAT	BIT(0) /* P0 state */
 162
 163#define AD7192_EXT_FREQ_MHZ_MIN	2457600
 164#define AD7192_EXT_FREQ_MHZ_MAX	5120000
 165#define AD7192_INT_FREQ_MHZ	4915200
 166
 167#define AD7192_NO_SYNC_FILTER	1
 168#define AD7192_SYNC3_FILTER	3
 169#define AD7192_SYNC4_FILTER	4
 170
 171/* NOTE:
 172 * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
 173 * In order to avoid contentions on the SPI bus, it's therefore necessary
 174 * to use spi bus locking.
 175 *
 176 * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
 177 */
 178
 179enum {
 180	AD7192_SYSCALIB_ZERO_SCALE,
 181	AD7192_SYSCALIB_FULL_SCALE,
 182};
 183
 184enum {
 185	ID_AD7190,
 186	ID_AD7192,
 187	ID_AD7193,
 188	ID_AD7194,
 189	ID_AD7195,
 190};
 191
 192struct ad7192_chip_info {
 193	unsigned int			chip_id;
 194	const char			*name;
 195	const struct iio_chan_spec	*channels;
 196	u8				num_channels;
 197	const struct ad_sigma_delta_info	*sigma_delta_info;
 198	const struct iio_info		*info;
 199	int (*parse_channels)(struct iio_dev *indio_dev);
 200};
 201
 202struct ad7192_state {
 203	const struct ad7192_chip_info	*chip_info;
 
 
 204	struct clk			*mclk;
 205	struct clk_hw			int_clk_hw;
 206	u16				int_vref_mv;
 207	u32				aincom_mv;
 208	u32				fclk;
 
 209	u32				mode;
 210	u32				conf;
 211	u32				scale_avail[8][2];
 212	u32				filter_freq_avail[4][2];
 213	u32				oversampling_ratio_avail[4];
 214	u8				gpocon;
 215	u8				clock_sel;
 216	struct mutex			lock;	/* protect sensor state */
 217	u8				syscalib_mode[8];
 218
 219	struct ad_sigma_delta		sd;
 220};
 221
 222static const char * const ad7192_syscalib_modes[] = {
 223	[AD7192_SYSCALIB_ZERO_SCALE] = "zero_scale",
 224	[AD7192_SYSCALIB_FULL_SCALE] = "full_scale",
 225};
 226
 227static int ad7192_set_syscalib_mode(struct iio_dev *indio_dev,
 228				    const struct iio_chan_spec *chan,
 229				    unsigned int mode)
 230{
 231	struct ad7192_state *st = iio_priv(indio_dev);
 232
 233	st->syscalib_mode[chan->channel] = mode;
 234
 235	return 0;
 236}
 237
 238static int ad7192_get_syscalib_mode(struct iio_dev *indio_dev,
 239				    const struct iio_chan_spec *chan)
 240{
 241	struct ad7192_state *st = iio_priv(indio_dev);
 242
 243	return st->syscalib_mode[chan->channel];
 244}
 245
 246static ssize_t ad7192_write_syscalib(struct iio_dev *indio_dev,
 247				     uintptr_t private,
 248				     const struct iio_chan_spec *chan,
 249				     const char *buf, size_t len)
 250{
 251	struct ad7192_state *st = iio_priv(indio_dev);
 252	bool sys_calib;
 253	int ret, temp;
 254
 255	ret = kstrtobool(buf, &sys_calib);
 256	if (ret)
 257		return ret;
 258
 259	temp = st->syscalib_mode[chan->channel];
 260	if (sys_calib) {
 261		if (temp == AD7192_SYSCALIB_ZERO_SCALE)
 262			ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_ZERO,
 263					      chan->address);
 264		else
 265			ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_FULL,
 266					      chan->address);
 267	}
 268
 269	return ret ? ret : len;
 270}
 271
 272static const struct iio_enum ad7192_syscalib_mode_enum = {
 273	.items = ad7192_syscalib_modes,
 274	.num_items = ARRAY_SIZE(ad7192_syscalib_modes),
 275	.set = ad7192_set_syscalib_mode,
 276	.get = ad7192_get_syscalib_mode
 277};
 278
 279static const struct iio_chan_spec_ext_info ad7192_calibsys_ext_info[] = {
 280	{
 281		.name = "sys_calibration",
 282		.write = ad7192_write_syscalib,
 283		.shared = IIO_SEPARATE,
 284	},
 285	IIO_ENUM("sys_calibration_mode", IIO_SEPARATE,
 286		 &ad7192_syscalib_mode_enum),
 287	IIO_ENUM_AVAILABLE("sys_calibration_mode", IIO_SHARED_BY_TYPE,
 288			   &ad7192_syscalib_mode_enum),
 289	{ }
 290};
 291
 292static struct ad7192_state *ad_sigma_delta_to_ad7192(struct ad_sigma_delta *sd)
 293{
 294	return container_of(sd, struct ad7192_state, sd);
 295}
 296
 297static int ad7192_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
 298{
 299	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 300
 301	st->conf &= ~AD7192_CONF_CHAN_MASK;
 302	st->conf |= FIELD_PREP(AD7192_CONF_CHAN_MASK, channel);
 303
 304	return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 305}
 306
 307static int ad7192_set_mode(struct ad_sigma_delta *sd,
 308			   enum ad_sigma_delta_mode mode)
 309{
 310	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 311
 312	st->mode &= ~AD7192_MODE_SEL_MASK;
 313	st->mode |= FIELD_PREP(AD7192_MODE_SEL_MASK, mode);
 314
 315	return ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 316}
 317
 318static int ad7192_append_status(struct ad_sigma_delta *sd, bool append)
 319{
 320	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 321	unsigned int mode = st->mode;
 322	int ret;
 323
 324	mode &= ~AD7192_MODE_STA_MASK;
 325	mode |= FIELD_PREP(AD7192_MODE_STA_MASK, append);
 326
 327	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, mode);
 328	if (ret < 0)
 329		return ret;
 330
 331	st->mode = mode;
 332
 333	return 0;
 334}
 335
 336static int ad7192_disable_all(struct ad_sigma_delta *sd)
 337{
 338	struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
 339	u32 conf = st->conf;
 340	int ret;
 341
 342	conf &= ~AD7192_CONF_CHAN_MASK;
 343
 344	ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, conf);
 345	if (ret < 0)
 346		return ret;
 347
 348	st->conf = conf;
 349
 350	return 0;
 351}
 352
 353static const struct ad_sigma_delta_info ad7192_sigma_delta_info = {
 354	.set_channel = ad7192_set_channel,
 355	.append_status = ad7192_append_status,
 356	.disable_all = ad7192_disable_all,
 357	.set_mode = ad7192_set_mode,
 358	.has_registers = true,
 359	.addr_shift = 3,
 360	.read_mask = BIT(6),
 361	.status_ch_mask = GENMASK(3, 0),
 362	.num_slots = 4,
 363	.irq_flags = IRQF_TRIGGER_FALLING,
 364};
 365
 366static const struct ad_sigma_delta_info ad7194_sigma_delta_info = {
 367	.set_channel = ad7192_set_channel,
 368	.append_status = ad7192_append_status,
 369	.disable_all = ad7192_disable_all,
 370	.set_mode = ad7192_set_mode,
 371	.has_registers = true,
 372	.addr_shift = 3,
 373	.read_mask = BIT(6),
 374	.status_ch_mask = GENMASK(3, 0),
 375	.irq_flags = IRQF_TRIGGER_FALLING,
 376};
 377
 378static const struct ad_sd_calib_data ad7192_calib_arr[8] = {
 379	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
 380	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
 381	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
 382	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
 383	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
 384	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
 385	{AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
 386	{AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
 387};
 388
 389static int ad7192_calibrate_all(struct ad7192_state *st)
 390{
 391	return ad_sd_calibrate_all(&st->sd, ad7192_calib_arr,
 392				   ARRAY_SIZE(ad7192_calib_arr));
 393}
 394
 395static inline bool ad7192_valid_external_frequency(u32 freq)
 396{
 397	return (freq >= AD7192_EXT_FREQ_MHZ_MIN &&
 398		freq <= AD7192_EXT_FREQ_MHZ_MAX);
 399}
 400
 401/*
 402 * Position 0 of ad7192_clock_names, xtal, corresponds to clock source
 403 * configuration AD7192_CLK_EXT_MCLK1_2 and position 1, mclk, corresponds to
 404 * AD7192_CLK_EXT_MCLK2
 405 */
 406static const char *const ad7192_clock_names[] = {
 407	"xtal",
 408	"mclk"
 409};
 410
 411static struct ad7192_state *clk_hw_to_ad7192(struct clk_hw *hw)
 412{
 413	return container_of(hw, struct ad7192_state, int_clk_hw);
 414}
 415
 416static unsigned long ad7192_clk_recalc_rate(struct clk_hw *hw,
 417					    unsigned long parent_rate)
 418{
 419	return AD7192_INT_FREQ_MHZ;
 420}
 421
 422static int ad7192_clk_output_is_enabled(struct clk_hw *hw)
 423{
 424	struct ad7192_state *st = clk_hw_to_ad7192(hw);
 425
 426	return st->clock_sel == AD7192_CLK_INT_CO;
 427}
 428
 429static int ad7192_clk_prepare(struct clk_hw *hw)
 430{
 431	struct ad7192_state *st = clk_hw_to_ad7192(hw);
 432	int ret;
 433
 434	st->mode &= ~AD7192_MODE_CLKSRC_MASK;
 435	st->mode |= AD7192_CLK_INT_CO;
 436
 437	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 438	if (ret)
 439		return ret;
 440
 441	st->clock_sel = AD7192_CLK_INT_CO;
 442
 443	return 0;
 444}
 445
 446static void ad7192_clk_unprepare(struct clk_hw *hw)
 447{
 448	struct ad7192_state *st = clk_hw_to_ad7192(hw);
 449	int ret;
 450
 451	st->mode &= ~AD7192_MODE_CLKSRC_MASK;
 452	st->mode |= AD7192_CLK_INT;
 453
 454	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 455	if (ret)
 456		return;
 457
 458	st->clock_sel = AD7192_CLK_INT;
 459}
 460
 461static const struct clk_ops ad7192_int_clk_ops = {
 462	.recalc_rate = ad7192_clk_recalc_rate,
 463	.is_enabled = ad7192_clk_output_is_enabled,
 464	.prepare = ad7192_clk_prepare,
 465	.unprepare = ad7192_clk_unprepare,
 466};
 467
 468static int ad7192_register_clk_provider(struct ad7192_state *st)
 469{
 470	struct device *dev = &st->sd.spi->dev;
 471	struct clk_init_data init = {};
 472	int ret;
 473
 474	if (!IS_ENABLED(CONFIG_COMMON_CLK))
 475		return 0;
 476
 477	if (!device_property_present(dev, "#clock-cells"))
 478		return 0;
 479
 480	init.name = devm_kasprintf(dev, GFP_KERNEL, "%s-clk",
 481				   fwnode_get_name(dev_fwnode(dev)));
 482	if (!init.name)
 483		return -ENOMEM;
 484
 485	init.ops = &ad7192_int_clk_ops;
 486
 487	st->int_clk_hw.init = &init;
 488	ret = devm_clk_hw_register(dev, &st->int_clk_hw);
 489	if (ret)
 490		return ret;
 491
 492	return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
 493					   &st->int_clk_hw);
 494}
 495
 496static int ad7192_clock_setup(struct ad7192_state *st)
 497{
 498	struct device *dev = &st->sd.spi->dev;
 499	int ret;
 500
 501	/*
 502	 * The following two if branches are kept for backward compatibility but
 503	 * the use of the two devicetree properties is highly discouraged. Clock
 504	 * configuration should be done according to the bindings.
 505	 */
 506
 507	if (device_property_read_bool(dev, "adi,int-clock-output-enable")) {
 508		st->clock_sel = AD7192_CLK_INT_CO;
 509		st->fclk = AD7192_INT_FREQ_MHZ;
 510		dev_warn(dev, "Property adi,int-clock-output-enable is deprecated! Check bindings!\n");
 511		return 0;
 512	}
 513
 514	if (device_property_read_bool(dev, "adi,clock-xtal")) {
 515		st->clock_sel = AD7192_CLK_EXT_MCLK1_2;
 516		st->mclk = devm_clk_get_enabled(dev, "mclk");
 517		if (IS_ERR(st->mclk))
 518			return dev_err_probe(dev, PTR_ERR(st->mclk),
 519					     "Failed to get mclk\n");
 520
 521		st->fclk = clk_get_rate(st->mclk);
 522		if (!ad7192_valid_external_frequency(st->fclk))
 523			return dev_err_probe(dev, -EINVAL,
 524					     "External clock frequency out of bounds\n");
 525
 526		dev_warn(dev, "Property adi,clock-xtal is deprecated! Check bindings!\n");
 527		return 0;
 528	}
 529
 530	ret = device_property_match_property_string(dev, "clock-names",
 531						    ad7192_clock_names,
 532						    ARRAY_SIZE(ad7192_clock_names));
 533	if (ret < 0) {
 534		st->clock_sel = AD7192_CLK_INT;
 535		st->fclk = AD7192_INT_FREQ_MHZ;
 536
 537		ret = ad7192_register_clk_provider(st);
 538		if (ret)
 539			return dev_err_probe(dev, ret,
 540					     "Failed to register clock provider\n");
 541		return 0;
 542	}
 543
 544	st->clock_sel = AD7192_CLK_EXT_MCLK1_2 + ret;
 545
 546	st->mclk = devm_clk_get_enabled(dev, ad7192_clock_names[ret]);
 547	if (IS_ERR(st->mclk))
 548		return dev_err_probe(dev, PTR_ERR(st->mclk),
 549				     "Failed to get clock source\n");
 550
 551	st->fclk = clk_get_rate(st->mclk);
 552	if (!ad7192_valid_external_frequency(st->fclk))
 553		return dev_err_probe(dev, -EINVAL,
 554				     "External clock frequency out of bounds\n");
 555
 556	return 0;
 557}
 558
 559static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev)
 560{
 561	struct ad7192_state *st = iio_priv(indio_dev);
 562	bool rej60_en, refin2_en;
 563	bool buf_en, bipolar, burnout_curr_en;
 564	unsigned long long scale_uv;
 565	int i, ret, id;
 566
 567	/* reset the serial interface */
 568	ret = ad_sd_reset(&st->sd, 48);
 569	if (ret < 0)
 570		return ret;
 571	usleep_range(500, 1000); /* Wait for at least 500us */
 572
 573	/* write/read test for device presence */
 574	ret = ad_sd_read_reg(&st->sd, AD7192_REG_ID, 1, &id);
 575	if (ret)
 576		return ret;
 577
 578	id = FIELD_GET(AD7192_ID_MASK, id);
 579
 580	if (id != st->chip_info->chip_id)
 581		dev_warn(dev, "device ID query failed (0x%X != 0x%X)\n",
 582			 id, st->chip_info->chip_id);
 583
 584	st->mode = FIELD_PREP(AD7192_MODE_SEL_MASK, AD7192_MODE_IDLE) |
 585		FIELD_PREP(AD7192_MODE_CLKSRC_MASK, st->clock_sel) |
 586		FIELD_PREP(AD7192_MODE_RATE_MASK, 480);
 587
 588	st->conf = FIELD_PREP(AD7192_CONF_GAIN_MASK, 0);
 589
 590	rej60_en = device_property_read_bool(dev, "adi,rejection-60-Hz-enable");
 591	if (rej60_en)
 592		st->mode |= AD7192_MODE_REJ60;
 593
 594	refin2_en = device_property_read_bool(dev, "adi,refin2-pins-enable");
 595	if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195)
 596		st->conf |= AD7192_CONF_REFSEL;
 597
 598	st->conf &= ~AD7192_CONF_CHOP;
 
 599
 600	buf_en = device_property_read_bool(dev, "adi,buffer-enable");
 601	if (buf_en)
 602		st->conf |= AD7192_CONF_BUF;
 603
 604	bipolar = device_property_read_bool(dev, "bipolar");
 605	if (!bipolar)
 606		st->conf |= AD7192_CONF_UNIPOLAR;
 607
 608	burnout_curr_en = device_property_read_bool(dev,
 609						    "adi,burnout-currents-enable");
 610	if (burnout_curr_en && buf_en) {
 611		st->conf |= AD7192_CONF_BURN;
 612	} else if (burnout_curr_en) {
 613		dev_warn(dev,
 614			 "Can't enable burnout currents: see CHOP or buffer\n");
 615	}
 616
 617	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 618	if (ret)
 619		return ret;
 620
 621	ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 622	if (ret)
 623		return ret;
 624
 625	ret = ad7192_calibrate_all(st);
 626	if (ret)
 627		return ret;
 628
 629	/* Populate available ADC input ranges */
 630	for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
 631		scale_uv = ((u64)st->int_vref_mv * 100000000)
 632			>> (indio_dev->channels[0].scan_type.realbits -
 633			!FIELD_GET(AD7192_CONF_UNIPOLAR, st->conf));
 634		scale_uv >>= i;
 635
 636		st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
 637		st->scale_avail[i][0] = scale_uv;
 638	}
 639
 640	st->oversampling_ratio_avail[0] = 1;
 641	st->oversampling_ratio_avail[1] = 2;
 642	st->oversampling_ratio_avail[2] = 8;
 643	st->oversampling_ratio_avail[3] = 16;
 644
 645	st->filter_freq_avail[0][0] = 600;
 646	st->filter_freq_avail[1][0] = 800;
 647	st->filter_freq_avail[2][0] = 2300;
 648	st->filter_freq_avail[3][0] = 2720;
 649
 650	st->filter_freq_avail[0][1] = 1000;
 651	st->filter_freq_avail[1][1] = 1000;
 652	st->filter_freq_avail[2][1] = 1000;
 653	st->filter_freq_avail[3][1] = 1000;
 654
 655	return 0;
 656}
 657
 658static ssize_t ad7192_show_ac_excitation(struct device *dev,
 659					 struct device_attribute *attr,
 660					 char *buf)
 661{
 662	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 663	struct ad7192_state *st = iio_priv(indio_dev);
 664
 665	return sysfs_emit(buf, "%ld\n", FIELD_GET(AD7192_CONF_ACX, st->conf));
 666}
 667
 668static ssize_t ad7192_show_bridge_switch(struct device *dev,
 669					 struct device_attribute *attr,
 670					 char *buf)
 671{
 672	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 673	struct ad7192_state *st = iio_priv(indio_dev);
 674
 675	return sysfs_emit(buf, "%ld\n",
 676			  FIELD_GET(AD7192_GPOCON_BPDSW, st->gpocon));
 677}
 678
 679static ssize_t ad7192_set(struct device *dev,
 680			  struct device_attribute *attr,
 681			  const char *buf,
 682			  size_t len)
 683{
 684	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 685	struct ad7192_state *st = iio_priv(indio_dev);
 686	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 687	int ret;
 688	bool val;
 689
 690	ret = kstrtobool(buf, &val);
 691	if (ret < 0)
 692		return ret;
 693
 694	ret = iio_device_claim_direct_mode(indio_dev);
 695	if (ret)
 696		return ret;
 697
 698	switch ((u32)this_attr->address) {
 699	case AD7192_REG_GPOCON:
 700		if (val)
 701			st->gpocon |= AD7192_GPOCON_BPDSW;
 702		else
 703			st->gpocon &= ~AD7192_GPOCON_BPDSW;
 704
 705		ad_sd_write_reg(&st->sd, AD7192_REG_GPOCON, 1, st->gpocon);
 706		break;
 707	case AD7192_REG_CONF:
 708		if (val)
 709			st->conf |= AD7192_CONF_ACX;
 710		else
 711			st->conf &= ~AD7192_CONF_ACX;
 712
 713		ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 714		break;
 715	default:
 716		ret = -EINVAL;
 717	}
 718
 719	iio_device_release_direct_mode(indio_dev);
 720
 721	return ret ? ret : len;
 722}
 723
 724static int ad7192_compute_f_order(struct ad7192_state *st, bool sinc3_en, bool chop_en)
 
 725{
 726	u8 avg_factor_selected, oversampling_ratio;
 727
 728	avg_factor_selected = FIELD_GET(AD7192_MODE_AVG_MASK, st->mode);
 729
 730	if (!avg_factor_selected && !chop_en)
 731		return 1;
 732
 733	oversampling_ratio = st->oversampling_ratio_avail[avg_factor_selected];
 734
 735	if (sinc3_en)
 736		return AD7192_SYNC3_FILTER + oversampling_ratio - 1;
 737
 738	return AD7192_SYNC4_FILTER + oversampling_ratio - 1;
 739}
 740
 741static int ad7192_get_f_order(struct ad7192_state *st)
 
 
 
 742{
 743	bool sinc3_en, chop_en;
 
 
 
 744
 745	sinc3_en = FIELD_GET(AD7192_MODE_SINC3, st->mode);
 746	chop_en = FIELD_GET(AD7192_CONF_CHOP, st->conf);
 747
 748	return ad7192_compute_f_order(st, sinc3_en, chop_en);
 749}
 
 
 750
 751static int ad7192_compute_f_adc(struct ad7192_state *st, bool sinc3_en,
 752				bool chop_en)
 753{
 754	unsigned int f_order = ad7192_compute_f_order(st, sinc3_en, chop_en);
 755
 756	return DIV_ROUND_CLOSEST(st->fclk,
 757				 f_order * FIELD_GET(AD7192_MODE_RATE_MASK, st->mode));
 758}
 759
 760static int ad7192_get_f_adc(struct ad7192_state *st)
 761{
 762	unsigned int f_order = ad7192_get_f_order(st);
 763
 764	return DIV_ROUND_CLOSEST(st->fclk,
 765				 f_order * FIELD_GET(AD7192_MODE_RATE_MASK, st->mode));
 766}
 767
 768static void ad7192_update_filter_freq_avail(struct ad7192_state *st)
 769{
 770	unsigned int fadc;
 771
 772	/* Formulas for filter at page 25 of the datasheet */
 773	fadc = ad7192_compute_f_adc(st, false, true);
 774	st->filter_freq_avail[0][0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
 775
 776	fadc = ad7192_compute_f_adc(st, true, true);
 777	st->filter_freq_avail[1][0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
 778
 779	fadc = ad7192_compute_f_adc(st, false, false);
 780	st->filter_freq_avail[2][0] = DIV_ROUND_CLOSEST(fadc * 230, 1024);
 781
 782	fadc = ad7192_compute_f_adc(st, true, false);
 783	st->filter_freq_avail[3][0] = DIV_ROUND_CLOSEST(fadc * 272, 1024);
 784}
 785
 786static IIO_DEVICE_ATTR(bridge_switch_en, 0644,
 787		       ad7192_show_bridge_switch, ad7192_set,
 788		       AD7192_REG_GPOCON);
 789
 790static IIO_DEVICE_ATTR(ac_excitation_en, 0644,
 791		       ad7192_show_ac_excitation, ad7192_set,
 792		       AD7192_REG_CONF);
 793
 794static struct attribute *ad7192_attributes[] = {
 
 795	&iio_dev_attr_bridge_switch_en.dev_attr.attr,
 
 796	NULL
 797};
 798
 799static const struct attribute_group ad7192_attribute_group = {
 800	.attrs = ad7192_attributes,
 801};
 802
 803static struct attribute *ad7195_attributes[] = {
 
 804	&iio_dev_attr_bridge_switch_en.dev_attr.attr,
 805	&iio_dev_attr_ac_excitation_en.dev_attr.attr,
 806	NULL
 807};
 808
 809static const struct attribute_group ad7195_attribute_group = {
 810	.attrs = ad7195_attributes,
 811};
 812
 813static unsigned int ad7192_get_temp_scale(bool unipolar)
 814{
 815	return unipolar ? 2815 * 2 : 2815;
 816}
 817
 818static int ad7192_set_3db_filter_freq(struct ad7192_state *st,
 819				      int val, int val2)
 820{
 821	int i, ret, freq;
 822	unsigned int diff_new, diff_old;
 823	int idx = 0;
 824
 825	diff_old = U32_MAX;
 826	freq = val * 1000 + val2;
 827
 828	for (i = 0; i < ARRAY_SIZE(st->filter_freq_avail); i++) {
 829		diff_new = abs(freq - st->filter_freq_avail[i][0]);
 
 
 830		if (diff_new < diff_old) {
 831			diff_old = diff_new;
 832			idx = i;
 833		}
 834	}
 835
 836	switch (idx) {
 837	case 0:
 
 838		st->mode &= ~AD7192_MODE_SINC3;
 839
 840		st->conf |= AD7192_CONF_CHOP;
 841		break;
 842	case 1:
 
 843		st->mode |= AD7192_MODE_SINC3;
 844
 845		st->conf |= AD7192_CONF_CHOP;
 846		break;
 847	case 2:
 
 848		st->mode &= ~AD7192_MODE_SINC3;
 849
 850		st->conf &= ~AD7192_CONF_CHOP;
 851		break;
 852	case 3:
 
 853		st->mode |= AD7192_MODE_SINC3;
 854
 855		st->conf &= ~AD7192_CONF_CHOP;
 856		break;
 857	}
 858
 859	ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 860	if (ret < 0)
 861		return ret;
 862
 863	return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
 864}
 865
 866static int ad7192_get_3db_filter_freq(struct ad7192_state *st)
 867{
 868	unsigned int fadc;
 869
 870	fadc = ad7192_get_f_adc(st);
 
 871
 872	if (FIELD_GET(AD7192_CONF_CHOP, st->conf))
 873		return DIV_ROUND_CLOSEST(fadc * 240, 1024);
 874	if (FIELD_GET(AD7192_MODE_SINC3, st->mode))
 875		return DIV_ROUND_CLOSEST(fadc * 272, 1024);
 876	else
 877		return DIV_ROUND_CLOSEST(fadc * 230, 1024);
 878}
 879
 880static int ad7192_read_raw(struct iio_dev *indio_dev,
 881			   struct iio_chan_spec const *chan,
 882			   int *val,
 883			   int *val2,
 884			   long m)
 885{
 886	struct ad7192_state *st = iio_priv(indio_dev);
 887	bool unipolar = FIELD_GET(AD7192_CONF_UNIPOLAR, st->conf);
 888	u8 gain = FIELD_GET(AD7192_CONF_GAIN_MASK, st->conf);
 889
 890	switch (m) {
 891	case IIO_CHAN_INFO_RAW:
 892		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
 893	case IIO_CHAN_INFO_SCALE:
 894		switch (chan->type) {
 895		case IIO_VOLTAGE:
 896			mutex_lock(&st->lock);
 897			*val = st->scale_avail[gain][0];
 898			*val2 = st->scale_avail[gain][1];
 899			mutex_unlock(&st->lock);
 900			return IIO_VAL_INT_PLUS_NANO;
 901		case IIO_TEMP:
 902			*val = 0;
 903			*val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
 904			return IIO_VAL_INT_PLUS_NANO;
 905		default:
 906			return -EINVAL;
 907		}
 908	case IIO_CHAN_INFO_OFFSET:
 909		if (!unipolar)
 910			*val = -(1 << (chan->scan_type.realbits - 1));
 911		else
 912			*val = 0;
 913
 914		switch (chan->type) {
 915		case IIO_VOLTAGE:
 916			/*
 917			 * Only applies to pseudo-differential inputs.
 918			 * AINCOM voltage has to be converted to "raw" units.
 919			 */
 920			if (st->aincom_mv && !chan->differential)
 921				*val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * NANO,
 922							      st->scale_avail[gain][1]);
 923			return IIO_VAL_INT;
 924		/* Kelvin to Celsius */
 925		case IIO_TEMP:
 926			*val -= 273 * ad7192_get_temp_scale(unipolar);
 927			return IIO_VAL_INT;
 928		default:
 929			return -EINVAL;
 930		}
 931	case IIO_CHAN_INFO_SAMP_FREQ:
 932		*val = DIV_ROUND_CLOSEST(ad7192_get_f_adc(st), 1024);
 
 933		return IIO_VAL_INT;
 934	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 935		*val = ad7192_get_3db_filter_freq(st);
 936		*val2 = 1000;
 937		return IIO_VAL_FRACTIONAL;
 938	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 939		*val = st->oversampling_ratio_avail[FIELD_GET(AD7192_MODE_AVG_MASK, st->mode)];
 940		return IIO_VAL_INT;
 941	}
 942
 943	return -EINVAL;
 944}
 945
 946static int ad7192_write_raw(struct iio_dev *indio_dev,
 947			    struct iio_chan_spec const *chan,
 948			    int val,
 949			    int val2,
 950			    long mask)
 951{
 952	struct ad7192_state *st = iio_priv(indio_dev);
 953	int ret, i, div;
 954	unsigned int tmp;
 955
 956	ret = iio_device_claim_direct_mode(indio_dev);
 957	if (ret)
 958		return ret;
 959
 960	mutex_lock(&st->lock);
 961
 962	switch (mask) {
 963	case IIO_CHAN_INFO_SCALE:
 964		ret = -EINVAL;
 
 965		for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
 966			if (val2 == st->scale_avail[i][1]) {
 967				ret = 0;
 968				tmp = st->conf;
 969				st->conf &= ~AD7192_CONF_GAIN_MASK;
 970				st->conf |= FIELD_PREP(AD7192_CONF_GAIN_MASK, i);
 971				if (tmp == st->conf)
 972					break;
 973				ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
 974						3, st->conf);
 975				ad7192_calibrate_all(st);
 976				break;
 977			}
 
 978		break;
 979	case IIO_CHAN_INFO_SAMP_FREQ:
 980		if (!val) {
 981			ret = -EINVAL;
 982			break;
 983		}
 984
 985		div = st->fclk / (val * ad7192_get_f_order(st) * 1024);
 986		if (div < 1 || div > 1023) {
 987			ret = -EINVAL;
 988			break;
 989		}
 990
 991		st->mode &= ~AD7192_MODE_RATE_MASK;
 992		st->mode |= FIELD_PREP(AD7192_MODE_RATE_MASK, div);
 993		ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
 994		ad7192_update_filter_freq_avail(st);
 995		break;
 996	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
 997		ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
 998		break;
 999	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1000		ret = -EINVAL;
1001		for (i = 0; i < ARRAY_SIZE(st->oversampling_ratio_avail); i++)
1002			if (val == st->oversampling_ratio_avail[i]) {
1003				ret = 0;
1004				tmp = st->mode;
1005				st->mode &= ~AD7192_MODE_AVG_MASK;
1006				st->mode |= FIELD_PREP(AD7192_MODE_AVG_MASK, i);
1007				if (tmp == st->mode)
1008					break;
1009				ad_sd_write_reg(&st->sd, AD7192_REG_MODE,
1010						3, st->mode);
1011				break;
1012			}
1013		ad7192_update_filter_freq_avail(st);
1014		break;
1015	default:
1016		ret = -EINVAL;
1017	}
1018
1019	mutex_unlock(&st->lock);
1020
1021	iio_device_release_direct_mode(indio_dev);
1022
1023	return ret;
1024}
1025
1026static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
1027				    struct iio_chan_spec const *chan,
1028				    long mask)
1029{
1030	switch (mask) {
1031	case IIO_CHAN_INFO_SCALE:
1032		return IIO_VAL_INT_PLUS_NANO;
1033	case IIO_CHAN_INFO_SAMP_FREQ:
1034		return IIO_VAL_INT;
1035	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
1036		return IIO_VAL_INT_PLUS_MICRO;
1037	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1038		return IIO_VAL_INT;
1039	default:
1040		return -EINVAL;
1041	}
1042}
1043
1044static int ad7192_read_avail(struct iio_dev *indio_dev,
1045			     struct iio_chan_spec const *chan,
1046			     const int **vals, int *type, int *length,
1047			     long mask)
1048{
1049	struct ad7192_state *st = iio_priv(indio_dev);
1050
1051	switch (mask) {
1052	case IIO_CHAN_INFO_SCALE:
1053		*vals = (int *)st->scale_avail;
1054		*type = IIO_VAL_INT_PLUS_NANO;
1055		/* Values are stored in a 2D matrix  */
1056		*length = ARRAY_SIZE(st->scale_avail) * 2;
1057
1058		return IIO_AVAIL_LIST;
1059	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
1060		*vals = (int *)st->filter_freq_avail;
1061		*type = IIO_VAL_FRACTIONAL;
1062		*length = ARRAY_SIZE(st->filter_freq_avail) * 2;
1063
1064		return IIO_AVAIL_LIST;
1065	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1066		*vals = (int *)st->oversampling_ratio_avail;
1067		*type = IIO_VAL_INT;
1068		*length = ARRAY_SIZE(st->oversampling_ratio_avail);
1069
1070		return IIO_AVAIL_LIST;
1071	}
1072
1073	return -EINVAL;
1074}
1075
1076static int ad7192_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask)
1077{
1078	struct ad7192_state *st = iio_priv(indio_dev);
1079	u32 conf = st->conf;
1080	int ret;
1081	int i;
1082
1083	conf &= ~AD7192_CONF_CHAN_MASK;
1084	for_each_set_bit(i, scan_mask, 8)
1085		conf |= FIELD_PREP(AD7192_CONF_CHAN_MASK, BIT(i));
1086
1087	ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, conf);
1088	if (ret < 0)
1089		return ret;
1090
1091	st->conf = conf;
1092
1093	return 0;
1094}
1095
1096static const struct iio_info ad7192_info = {
1097	.read_raw = ad7192_read_raw,
1098	.write_raw = ad7192_write_raw,
1099	.write_raw_get_fmt = ad7192_write_raw_get_fmt,
1100	.read_avail = ad7192_read_avail,
1101	.attrs = &ad7192_attribute_group,
1102	.validate_trigger = ad_sd_validate_trigger,
1103	.update_scan_mode = ad7192_update_scan_mode,
1104};
1105
1106static const struct iio_info ad7194_info = {
1107	.read_raw = ad7192_read_raw,
1108	.write_raw = ad7192_write_raw,
1109	.write_raw_get_fmt = ad7192_write_raw_get_fmt,
1110	.read_avail = ad7192_read_avail,
1111	.validate_trigger = ad_sd_validate_trigger,
1112};
1113
1114static const struct iio_info ad7195_info = {
1115	.read_raw = ad7192_read_raw,
1116	.write_raw = ad7192_write_raw,
1117	.write_raw_get_fmt = ad7192_write_raw_get_fmt,
1118	.read_avail = ad7192_read_avail,
1119	.attrs = &ad7195_attribute_group,
1120	.validate_trigger = ad_sd_validate_trigger,
1121	.update_scan_mode = ad7192_update_scan_mode,
1122};
1123
1124#define __AD719x_CHANNEL(_si, _channel1, _channel2, _address, _type, \
1125	_mask_all, _mask_type_av, _mask_all_av, _ext_info) \
1126	{ \
1127		.type = (_type), \
1128		.differential = ((_channel2) == -1 ? 0 : 1), \
1129		.indexed = 1, \
1130		.channel = (_channel1), \
1131		.channel2 = (_channel2), \
1132		.address = (_address), \
 
1133		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1134			BIT(IIO_CHAN_INFO_OFFSET), \
1135		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1136		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
1137			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
1138			(_mask_all), \
1139		.info_mask_shared_by_type_available = (_mask_type_av), \
1140		.info_mask_shared_by_all_available = \
1141			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
1142			(_mask_all_av), \
1143		.ext_info = (_ext_info), \
1144		.scan_index = (_si), \
1145		.scan_type = { \
1146			.sign = 'u', \
1147			.realbits = 24, \
1148			.storagebits = 32, \
1149			.endianness = IIO_BE, \
1150		}, \
1151	}
1152
1153#define AD719x_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
1154	__AD719x_CHANNEL(_si, _channel1, _channel2, _address, IIO_VOLTAGE, 0, \
1155		BIT(IIO_CHAN_INFO_SCALE), 0, ad7192_calibsys_ext_info)
 
1156
1157#define AD719x_CHANNEL(_si, _channel1, _address) \
1158	__AD719x_CHANNEL(_si, _channel1, -1, _address, IIO_VOLTAGE, 0, \
1159		BIT(IIO_CHAN_INFO_SCALE), 0, ad7192_calibsys_ext_info)
 
 
 
 
1160
1161#define AD719x_TEMP_CHANNEL(_si, _address) \
1162	__AD719x_CHANNEL(_si, 0, -1, _address, IIO_TEMP, 0, 0, 0, NULL)
1163
1164#define AD7193_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
1165	__AD719x_CHANNEL(_si, _channel1, _channel2, _address, \
1166		IIO_VOLTAGE, \
1167		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
1168		BIT(IIO_CHAN_INFO_SCALE), \
1169		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
1170		ad7192_calibsys_ext_info)
1171
1172#define AD7193_CHANNEL(_si, _channel1, _address) \
1173	AD7193_DIFF_CHANNEL(_si, _channel1, -1, _address)
1174
1175static const struct iio_chan_spec ad7192_channels[] = {
1176	AD719x_DIFF_CHANNEL(0, 1, 2, AD7192_CH_AIN1P_AIN2M),
1177	AD719x_DIFF_CHANNEL(1, 3, 4, AD7192_CH_AIN3P_AIN4M),
1178	AD719x_TEMP_CHANNEL(2, AD7192_CH_TEMP),
1179	AD719x_DIFF_CHANNEL(3, 2, 2, AD7192_CH_AIN2P_AIN2M),
1180	AD719x_CHANNEL(4, 1, AD7192_CH_AIN1),
1181	AD719x_CHANNEL(5, 2, AD7192_CH_AIN2),
1182	AD719x_CHANNEL(6, 3, AD7192_CH_AIN3),
1183	AD719x_CHANNEL(7, 4, AD7192_CH_AIN4),
1184	IIO_CHAN_SOFT_TIMESTAMP(8),
1185};
1186
1187static const struct iio_chan_spec ad7193_channels[] = {
1188	AD7193_DIFF_CHANNEL(0, 1, 2, AD7193_CH_AIN1P_AIN2M),
1189	AD7193_DIFF_CHANNEL(1, 3, 4, AD7193_CH_AIN3P_AIN4M),
1190	AD7193_DIFF_CHANNEL(2, 5, 6, AD7193_CH_AIN5P_AIN6M),
1191	AD7193_DIFF_CHANNEL(3, 7, 8, AD7193_CH_AIN7P_AIN8M),
1192	AD719x_TEMP_CHANNEL(4, AD7193_CH_TEMP),
1193	AD7193_DIFF_CHANNEL(5, 2, 2, AD7193_CH_AIN2P_AIN2M),
1194	AD7193_CHANNEL(6, 1, AD7193_CH_AIN1),
1195	AD7193_CHANNEL(7, 2, AD7193_CH_AIN2),
1196	AD7193_CHANNEL(8, 3, AD7193_CH_AIN3),
1197	AD7193_CHANNEL(9, 4, AD7193_CH_AIN4),
1198	AD7193_CHANNEL(10, 5, AD7193_CH_AIN5),
1199	AD7193_CHANNEL(11, 6, AD7193_CH_AIN6),
1200	AD7193_CHANNEL(12, 7, AD7193_CH_AIN7),
1201	AD7193_CHANNEL(13, 8, AD7193_CH_AIN8),
1202	IIO_CHAN_SOFT_TIMESTAMP(14),
1203};
1204
1205static bool ad7194_validate_ain_channel(struct device *dev, u32 ain)
1206{
1207	return in_range(ain, AD7194_CH_AIN_START, AD7194_CH_AIN_NR);
1208}
1209
1210static int ad7194_parse_channels(struct iio_dev *indio_dev)
1211{
1212	struct device *dev = indio_dev->dev.parent;
1213	struct iio_chan_spec *ad7194_channels;
1214	const struct iio_chan_spec ad7194_chan = AD7193_CHANNEL(0, 0, 0);
1215	const struct iio_chan_spec ad7194_chan_diff = AD7193_DIFF_CHANNEL(0, 0, 0, 0);
1216	const struct iio_chan_spec ad7194_chan_temp = AD719x_TEMP_CHANNEL(0, 0);
1217	const struct iio_chan_spec ad7194_chan_timestamp = IIO_CHAN_SOFT_TIMESTAMP(0);
1218	unsigned int num_channels, index = 0;
1219	u32 ain[2];
1220	int ret;
1221
1222	num_channels = device_get_child_node_count(dev);
1223	if (num_channels > AD7194_CH_MAX_NR)
1224		return dev_err_probe(dev, -EINVAL, "Too many channels: %u\n",
1225				     num_channels);
1226
1227	num_channels += AD7194_CH_BASE_NR;
1228
1229	ad7194_channels = devm_kcalloc(dev, num_channels,
1230				       sizeof(*ad7194_channels), GFP_KERNEL);
1231	if (!ad7194_channels)
1232		return -ENOMEM;
1233
1234	indio_dev->channels = ad7194_channels;
1235	indio_dev->num_channels = num_channels;
1236
1237	device_for_each_child_node_scoped(dev, child) {
1238		ret = fwnode_property_read_u32_array(child, "diff-channels",
1239						     ain, ARRAY_SIZE(ain));
1240		if (ret == 0) {
1241			if (!ad7194_validate_ain_channel(dev, ain[0]))
1242				return dev_err_probe(dev, -EINVAL,
1243						     "Invalid AIN channel: %u\n",
1244						     ain[0]);
1245
1246			if (!ad7194_validate_ain_channel(dev, ain[1]))
1247				return dev_err_probe(dev, -EINVAL,
1248						     "Invalid AIN channel: %u\n",
1249						     ain[1]);
1250
1251			*ad7194_channels = ad7194_chan_diff;
1252			ad7194_channels->scan_index = index++;
1253			ad7194_channels->channel = ain[0];
1254			ad7194_channels->channel2 = ain[1];
1255			ad7194_channels->address = AD7194_DIFF_CH(ain[0], ain[1]);
1256		} else {
1257			ret = fwnode_property_read_u32(child, "single-channel",
1258						       &ain[0]);
1259			if (ret)
1260				return dev_err_probe(dev, ret,
1261						     "Missing channel property\n");
1262
1263			if (!ad7194_validate_ain_channel(dev, ain[0]))
1264				return dev_err_probe(dev, -EINVAL,
1265						     "Invalid AIN channel: %u\n",
1266						     ain[0]);
1267
1268			*ad7194_channels = ad7194_chan;
1269			ad7194_channels->scan_index = index++;
1270			ad7194_channels->channel = ain[0];
1271			ad7194_channels->address = AD7194_CH(ain[0]);
1272		}
1273		ad7194_channels++;
1274	}
1275
1276	*ad7194_channels = ad7194_chan_temp;
1277	ad7194_channels->scan_index = index++;
1278	ad7194_channels->address = AD7194_CH_TEMP;
1279	ad7194_channels++;
1280
1281	*ad7194_channels = ad7194_chan_timestamp;
1282	ad7194_channels->scan_index = index;
1283
1284	return 0;
1285}
1286
1287static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
1288	[ID_AD7190] = {
1289		.chip_id = CHIPID_AD7190,
1290		.name = "ad7190",
1291		.channels = ad7192_channels,
1292		.num_channels = ARRAY_SIZE(ad7192_channels),
1293		.sigma_delta_info = &ad7192_sigma_delta_info,
1294		.info = &ad7192_info,
1295	},
1296	[ID_AD7192] = {
1297		.chip_id = CHIPID_AD7192,
1298		.name = "ad7192",
1299		.channels = ad7192_channels,
1300		.num_channels = ARRAY_SIZE(ad7192_channels),
1301		.sigma_delta_info = &ad7192_sigma_delta_info,
1302		.info = &ad7192_info,
1303	},
1304	[ID_AD7193] = {
1305		.chip_id = CHIPID_AD7193,
1306		.name = "ad7193",
1307		.channels = ad7193_channels,
1308		.num_channels = ARRAY_SIZE(ad7193_channels),
1309		.sigma_delta_info = &ad7192_sigma_delta_info,
1310		.info = &ad7192_info,
1311	},
1312	[ID_AD7194] = {
1313		.chip_id = CHIPID_AD7194,
1314		.name = "ad7194",
1315		.info = &ad7194_info,
1316		.sigma_delta_info = &ad7194_sigma_delta_info,
1317		.parse_channels = ad7194_parse_channels,
1318	},
1319	[ID_AD7195] = {
1320		.chip_id = CHIPID_AD7195,
1321		.name = "ad7195",
1322		.channels = ad7192_channels,
1323		.num_channels = ARRAY_SIZE(ad7192_channels),
1324		.sigma_delta_info = &ad7192_sigma_delta_info,
1325		.info = &ad7195_info,
1326	},
1327};
1328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1329static int ad7192_probe(struct spi_device *spi)
1330{
1331	struct device *dev = &spi->dev;
1332	struct ad7192_state *st;
1333	struct iio_dev *indio_dev;
1334	int ret, avdd_mv;
1335
1336	if (!spi->irq)
1337		return dev_err_probe(dev, -ENODEV, "Failed to get IRQ\n");
 
 
1338
1339	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1340	if (!indio_dev)
1341		return -ENOMEM;
1342
1343	st = iio_priv(indio_dev);
1344
1345	mutex_init(&st->lock);
1346
1347	/*
1348	 * Regulator aincom is optional to maintain compatibility with older DT.
1349	 * Newer firmware should provide a zero volt fixed supply if wired to
1350	 * ground.
1351	 */
1352	ret = devm_regulator_get_enable_read_voltage(dev, "aincom");
1353	if (ret < 0 && ret != -ENODEV)
1354		return dev_err_probe(dev, ret, "Failed to get AINCOM voltage\n");
1355
1356	st->aincom_mv = ret == -ENODEV ? 0 : ret / MILLI;
1357
1358	/* AVDD can optionally be used as reference voltage */
1359	ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
1360	if (ret == -ENODEV || ret == -EINVAL) {
1361		int ret2;
1362
1363		/*
1364		 * We get -EINVAL if avdd is a supply with unknown voltage. We
1365		 * still need to enable it since it is also a power supply.
1366		 */
1367		ret2 = devm_regulator_get_enable(dev, "avdd");
1368		if (ret2)
1369			return dev_err_probe(dev, ret2,
1370					     "Failed to enable AVDD supply\n");
1371	} else if (ret < 0) {
1372		return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
1373	}
1374
1375	avdd_mv = ret == -ENODEV || ret == -EINVAL ? 0 : ret / MILLI;
 
 
 
 
 
 
1376
1377	ret = devm_regulator_get_enable(dev, "dvdd");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1378	if (ret)
1379		return dev_err_probe(dev, ret, "Failed to enable specified DVdd supply\n");
1380
1381	/*
1382	 * This is either REFIN1 or REFIN2 depending on adi,refin2-pins-enable.
1383	 * If this supply is not present, fall back to AVDD as reference.
1384	 */
1385	ret = devm_regulator_get_enable_read_voltage(dev, "vref");
1386	if (ret == -ENODEV) {
1387		if (avdd_mv == 0)
1388			return dev_err_probe(dev, -ENODEV,
1389					     "No reference voltage available\n");
1390	} else if (ret < 0) {
1391		return ret;
1392	}
1393
1394	st->int_vref_mv = ret == -ENODEV ? avdd_mv : ret / MILLI;
1395
1396	st->chip_info = spi_get_device_match_data(spi);
1397	if (!st->chip_info)
1398		return -ENODEV;
 
 
1399
1400	indio_dev->name = st->chip_info->name;
1401	indio_dev->modes = INDIO_DIRECT_MODE;
1402	indio_dev->info = st->chip_info->info;
1403	if (st->chip_info->parse_channels) {
1404		ret = st->chip_info->parse_channels(indio_dev);
1405		if (ret)
1406			return ret;
1407	} else {
1408		indio_dev->channels = st->chip_info->channels;
1409		indio_dev->num_channels = st->chip_info->num_channels;
1410	}
1411
1412	ret = ad_sd_init(&st->sd, indio_dev, spi, st->chip_info->sigma_delta_info);
1413	if (ret)
1414		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1415
1416	ret = devm_ad_sd_setup_buffer_and_trigger(dev, indio_dev);
1417	if (ret)
1418		return ret;
 
1419
1420	ret = ad7192_clock_setup(st);
1421	if (ret)
1422		return ret;
1423
1424	ret = ad7192_setup(indio_dev, dev);
1425	if (ret)
1426		return ret;
1427
1428	return devm_iio_device_register(dev, indio_dev);
1429}
1430
1431static const struct of_device_id ad7192_of_match[] = {
1432	{ .compatible = "adi,ad7190", .data = &ad7192_chip_info_tbl[ID_AD7190] },
1433	{ .compatible = "adi,ad7192", .data = &ad7192_chip_info_tbl[ID_AD7192] },
1434	{ .compatible = "adi,ad7193", .data = &ad7192_chip_info_tbl[ID_AD7193] },
1435	{ .compatible = "adi,ad7194", .data = &ad7192_chip_info_tbl[ID_AD7194] },
1436	{ .compatible = "adi,ad7195", .data = &ad7192_chip_info_tbl[ID_AD7195] },
1437	{ }
1438};
1439MODULE_DEVICE_TABLE(of, ad7192_of_match);
1440
1441static const struct spi_device_id ad7192_ids[] = {
1442	{ "ad7190", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7190] },
1443	{ "ad7192", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7192] },
1444	{ "ad7193", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7193] },
1445	{ "ad7194", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7194] },
1446	{ "ad7195", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7195] },
1447	{ }
1448};
1449MODULE_DEVICE_TABLE(spi, ad7192_ids);
1450
1451static struct spi_driver ad7192_driver = {
1452	.driver = {
1453		.name	= "ad7192",
1454		.of_match_table = ad7192_of_match,
1455	},
1456	.probe		= ad7192_probe,
1457	.id_table	= ad7192_ids,
1458};
1459module_spi_driver(ad7192_driver);
1460
1461MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
1462MODULE_DESCRIPTION("Analog Devices AD7192 and similar ADC");
1463MODULE_LICENSE("GPL v2");
1464MODULE_IMPORT_NS("IIO_AD_SIGMA_DELTA");