aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/adc/ti-ads1298.c
blob: 1d1eaba3d6d129476ffe637240e64adb8929087e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// SPDX-License-Identifier: GPL-2.0
/* TI ADS1298 chip family driver
 * Copyright (C) 2023 - 2024 Topic Embedded Products
 */

#include <linux/bitfield.h>
#include <linux/cleanup.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/log2.h>
#include <linux/math.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/units.h>

#include <linux/iio/iio.h>
#include <linux/iio/buffer.h>
#include <linux/iio/kfifo_buf.h>

#include <asm/unaligned.h>

/* Commands */
#define ADS1298_CMD_WAKEUP	0x02
#define ADS1298_CMD_STANDBY	0x04
#define ADS1298_CMD_RESET	0x06
#define ADS1298_CMD_START	0x08
#define ADS1298_CMD_STOP	0x0a
#define ADS1298_CMD_RDATAC	0x10
#define ADS1298_CMD_SDATAC	0x11
#define ADS1298_CMD_RDATA	0x12
#define ADS1298_CMD_RREG	0x20
#define ADS1298_CMD_WREG	0x40

/* Registers */
#define ADS1298_REG_ID		0x00
#define ADS1298_MASK_ID_FAMILY			GENMASK(7, 3)
#define ADS1298_MASK_ID_CHANNELS		GENMASK(2, 0)
#define ADS1298_ID_FAMILY_ADS129X		0x90
#define ADS1298_ID_FAMILY_ADS129XR		0xd0

#define ADS1298_REG_CONFIG1	0x01
#define ADS1298_MASK_CONFIG1_HR			BIT(7)
#define ADS1298_MASK_CONFIG1_DR			GENMASK(2, 0)
#define ADS1298_SHIFT_DR_HR			6
#define ADS1298_SHIFT_DR_LP			7
#define ADS1298_LOWEST_DR			0x06

#define ADS1298_REG_CONFIG2	0x02
#define ADS1298_MASK_CONFIG2_RESERVED		BIT(6)
#define ADS1298_MASK_CONFIG2_WCT_CHOP		BIT(5)
#define ADS1298_MASK_CONFIG2_INT_TEST		BIT(4)
#define ADS1298_MASK_CONFIG2_TEST_AMP		BIT(2)
#define ADS1298_MASK_CONFIG2_TEST_FREQ_DC	GENMASK(1, 0)
#define ADS1298_MASK_CONFIG2_TEST_FREQ_SLOW	0
#define ADS1298_MASK_CONFIG2_TEST_FREQ_FAST	BIT(0)

#define ADS1298_REG_CONFIG3	0x03
#define ADS1298_MASK_CONFIG3_PWR_REFBUF		BIT(7)
#define ADS1298_MASK_CONFIG3_RESERVED		BIT(6)
#define ADS1298_MASK_CONFIG3_VREF_4V		BIT(5)

#define ADS1298_REG_LOFF	0x04
#define ADS1298_REG_CHnSET(n)	(0x05 + n)
#define ADS1298_MASK_CH_PD		BIT(7)
#define ADS1298_MASK_CH_PGA		GENMASK(6, 4)
#define ADS1298_MASK_CH_MUX		GENMASK(2, 0)

#define ADS1298_REG_LOFF_STATP	0x12
#define ADS1298_REG_LOFF_STATN	0x13
#define ADS1298_REG_CONFIG4	0x17
#define ADS1298_MASK_CONFIG4_SINGLE_SHOT	BIT(3)

#define ADS1298_REG_WCT1	0x18
#define ADS1298_REG_WCT2	0x19

#define ADS1298_MAX_CHANNELS	8
#define ADS1298_BITS_PER_SAMPLE	24
#define ADS1298_CLK_RATE_HZ	2048000
#define ADS1298_CLOCKS_TO_USECS(x) \
		(DIV_ROUND_UP((x) * MICROHZ_PER_HZ, ADS1298_CLK_RATE_HZ))
/*
 * Read/write register commands require 4 clocks to decode, for speeds above
 * 2x the clock rate, this would require extra time between the command byte and
 * the data. Much simpler is to just limit the SPI transfer speed while doing
 * register access.
 */
#define ADS1298_SPI_BUS_SPEED_SLOW	ADS1298_CLK_RATE_HZ
/* For reading and writing registers, we need a 3-byte buffer */
#define ADS1298_SPI_CMD_BUFFER_SIZE	3
/* Outputs status word and 'n' 24-bit samples, plus the command byte */
#define ADS1298_SPI_RDATA_BUFFER_SIZE(n)	(((n) + 1) * 3 + 1)
#define ADS1298_SPI_RDATA_BUFFER_SIZE_MAX \
		ADS1298_SPI_RDATA_BUFFER_SIZE(ADS1298_MAX_CHANNELS)

struct ads1298_private {
	const struct ads1298_chip_info *chip_info;
	struct spi_device *spi;
	struct regulator *reg_avdd;
	struct regulator *reg_vref;
	struct clk *clk;
	struct regmap *regmap;
	struct completion completion;
	struct iio_trigger *trig;
	struct spi_transfer rdata_xfer;
	struct spi_message rdata_msg;
	spinlock_t irq_busy_lock; /* Handshake between SPI and DRDY irqs */
	/*
	 * rdata_xfer_busy increments when a DRDY occurs and decrements when SPI
	 * completion is reported. Hence its meaning is:
	 * 0 = Waiting for DRDY interrupt
	 * 1 = SPI transfer in progress
	 * 2 = DRDY during SPI transfer, start another transfer on completion
	 * >2 = Multiple DRDY during transfer, lost rdata_xfer_busy - 2 samples
	 */
	unsigned int rdata_xfer_busy;

	/* Temporary storage for demuxing data after SPI transfer */
	u32 bounce_buffer[ADS1298_MAX_CHANNELS];

	/* For synchronous SPI exchanges (read/write registers) */
	u8 cmd_buffer[ADS1298_SPI_CMD_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN);

	/* Buffer used for incoming SPI data */
	u8 rx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE_MAX];
	/* Contains the RDATA command and zeroes to clock out */
	u8 tx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE_MAX];
};

/* Three bytes per sample in RX buffer, starting at offset 4 */
#define ADS1298_OFFSET_IN_RX_BUFFER(index)	(3 * (index) + 4)

#define ADS1298_CHAN(index)				\
{							\
	.type = IIO_VOLTAGE,				\
	.indexed = 1,					\
	.channel = index,				\
	.address = ADS1298_OFFSET_IN_RX_BUFFER(index),	\
	.info_mask_separate =				\
		BIT(IIO_CHAN_INFO_RAW) |		\
		BIT(IIO_CHAN_INFO_SCALE),		\
	.info_mask_shared_by_all =			\
		BIT(IIO_CHAN_INFO_SAMP_FREQ) |		\
		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),	\
	.scan_index = index,				\
	.scan_type = {					\
		.sign = 's',				\
		.realbits = ADS1298_BITS_PER_SAMPLE,	\
		.storagebits = 32,			\
		.endianness = IIO_CPU,			\
	},						\
}

static const struct iio_chan_spec ads1298_channels[] = {
	ADS1298_CHAN(0),
	ADS1298_CHAN(1),
	ADS1298_CHAN(2),
	ADS1298_CHAN(3),
	ADS1298_CHAN(4),
	ADS1298_CHAN(5),
	ADS1298_CHAN(6),
	ADS1298_CHAN(7),
};

static int ads1298_write_cmd(struct ads1298_private *priv, u8 command)
{
	struct spi_transfer xfer = {
		.tx_buf = priv->cmd_buffer,
		.rx_buf = priv->cmd_buffer,
		.len = 1,
		.speed_hz = ADS1298_SPI_BUS_SPEED_SLOW,
		.delay = {
			.value = 2,
			.unit = SPI_DELAY_UNIT_USECS,
		},
	};

	priv->cmd_buffer[0] = command;

	return spi_sync_transfer(priv->spi, &xfer, 1);
}

static int ads1298_read_one(struct ads1298_private *priv, int chan_index)
{
	int ret;

	/* Enable the channel */
	ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(chan_index),
				 ADS1298_MASK_CH_PD, 0);
	if (ret)
		return ret;

	/* Enable single-shot mode, so we don't need to send a STOP */
	ret = regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG4,
				 ADS1298_MASK_CONFIG4_SINGLE_SHOT,
				 ADS1298_MASK_CONFIG4_SINGLE_SHOT);
	if (ret)
		return ret;

	reinit_completion(&priv->completion);

	ret = ads1298_write_cmd(priv, ADS1298_CMD_START);
	if (ret < 0) {
		dev_err(&priv->spi->dev, "CMD_START error: %d\n", ret);
		return ret;
	}

	/* Cannot take longer than 40ms (250Hz) */
	ret = wait_for_completion_timeout(&priv->completion, msecs_to_jiffies(50));
	if (!ret)
		return -ETIMEDOUT;

	return 0;
}

static int ads1298_get_samp_freq(struct ads1298_private *priv, int *val)
{
	unsigned long rate;
	unsigned int cfg;
	int ret;

	ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, &cfg);
	if (ret)
		return ret;

	if (priv->clk)
		rate = clk_get_rate(priv->clk);
	else
		rate = ADS1298_CLK_RATE_HZ;
	if (!rate)
		return -EINVAL;

	/* Data rate shift depends on HR/LP mode */
	if (cfg & ADS1298_MASK_CONFIG1_HR)
		rate >>= ADS1298_SHIFT_DR_HR;
	else
		rate >>= ADS1298_SHIFT_DR_LP;

	*val = rate >> (cfg & ADS1298_MASK_CONFIG1_DR);

	return IIO_VAL_INT;
}

static int ads1298_set_samp_freq(struct ads1298_private *priv, int val)
{
	unsigned long rate;
	unsigned int factor;
	unsigned int cfg;

	if (priv->clk)
		rate = clk_get_rate(priv->clk);
	else
		rate = ADS1298_CLK_RATE_HZ;
	if (!rate)
		return -EINVAL;
	if (val <= 0)
		return -EINVAL;

	factor = (rate >> ADS1298_SHIFT_DR_HR) / val;
	if (factor >= BIT(ADS1298_SHIFT_DR_LP))
		cfg = ADS1298_LOWEST_DR;
	else if (factor)
		cfg = ADS1298_MASK_CONFIG1_HR | ilog2(factor); /* Use HR mode */
	else
		cfg = ADS1298_MASK_CONFIG1_HR; /* Fastest possible */

	return regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG1,
				  ADS1298_MASK_CONFIG1_HR | ADS1298_MASK_CONFIG1_DR,
				  cfg);
}

static const u8 ads1298_pga_settings[] = { 6, 1, 2, 3, 4, 8, 12 };

static int ads1298_get_scale(struct ads1298_private *priv,
			     int channel, int *val, int *val2)
{
	int ret;
	unsigned int regval;
	u8 gain;

	if (priv->reg_vref) {
		ret = regulator_get_voltage(priv->reg_vref);
		if (ret < 0)
			return ret;

		*val = ret / MILLI; /* Convert to millivolts */
	} else {
		ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG3, &regval);
		if (ret)
			return ret;

		/* Refererence in millivolts */
		*val = regval & ADS1298_MASK_CONFIG3_VREF_4V ? 4000 : 2400;
	}

	ret = regmap_read(priv->regmap, ADS1298_REG_CHnSET(channel), &regval);
	if (ret)
		return ret;

	gain = ads1298_pga_settings[FIELD_GET(ADS1298_MASK_CH_PGA, regval)];
	*val /= gain; /* Full scale is VREF / gain */

	*val2 = ADS1298_BITS_PER_SAMPLE - 1; /* Signed, hence the -1 */

	return IIO_VAL_FRACTIONAL_LOG2;
}

static int ads1298_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long mask)
{
	struct ads1298_private *priv = iio_priv(indio_dev);
	int ret;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;

		ret = ads1298_read_one(priv, chan->scan_index);

		iio_device_release_direct_mode(indio_dev);

		if (ret)
			return ret;

		*val = sign_extend32(get_unaligned_be24(priv->rx_buffer + chan->address),
				     ADS1298_BITS_PER_SAMPLE - 1);
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		return ads1298_get_scale(priv, chan->channel, val, val2);
	case IIO_CHAN_INFO_SAMP_FREQ:
		return ads1298_get_samp_freq(priv, val);
	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
		ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, val);
		if (ret)
			return ret;

		*val = 16 << (*val & ADS1298_MASK_CONFIG1_DR);
		return IIO_VAL_INT;
	default:
		return -EINVAL;
	}
}

static int ads1298_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan, int val,
			     int val2, long mask)
{
	struct ads1298_private *priv = iio_priv(indio_dev);

	switch (mask) {
	case IIO_CHAN_INFO_SAMP_FREQ:
		return ads1298_set_samp_freq(priv, val);
	default:
		return -EINVAL;
	}
}

static int ads1298_reg_write(void *context, unsigned int reg, unsigned int val)
{
	struct ads1298_private *priv = context;
	struct spi_transfer reg_write_xfer = {
		.tx_buf = priv->cmd_buffer,
		.rx_buf = priv->cmd_buffer,
		.len = 3,
		.speed_hz = ADS1298_SPI_BUS_SPEED_SLOW,
		.delay = {
			.value = 2,
			.unit = SPI_DELAY_UNIT_USECS,
		},
	};

	priv->cmd_buffer[0] = ADS1298_CMD_WREG | reg;
	priv->cmd_buffer[1] = 0; /* Number of registers to be written - 1 */
	priv->cmd_buffer[2] = val;

	return spi_sync_transfer(priv->spi, &reg_write_xfer, 1);
}

static int ads1298_reg_read(void *context, unsigned int reg, unsigned int *val)
{
	struct ads1298_private *priv = context;
	struct spi_transfer reg_read_xfer = {
		.tx_buf = priv->cmd_buffer,
		.rx_buf = priv->cmd_buffer,
		.len = 3,
		.speed_hz = ADS1298_SPI_BUS_SPEED_SLOW,
		.delay = {
			.value = 2,
			.unit = SPI_DELAY_UNIT_USECS,
		},
	};
	int ret;

	priv->cmd_buffer[0] = ADS1298_CMD_RREG | reg;
	priv->cmd_buffer[1] = 0; /* Number of registers to be read - 1 */
	priv->cmd_buffer[2] = 0;

	ret = spi_sync_transfer(priv->spi, &reg_read_xfer, 1);
	if (ret)
		return ret;

	*val = priv->cmd_buffer[2];

	return 0;
}

static int ads1298_reg_access(struct iio_dev *indio_dev, unsigned int reg,
			      unsigned int writeval, unsigned int *readval)
{
	struct ads1298_private *priv = iio_priv(indio_dev);

	if (readval)
		return regmap_read(priv->regmap, reg, readval);

	return regmap_write(priv->regmap, reg, writeval);
}

static void ads1298_rdata_unmark_busy(struct ads1298_private *priv)
{
	/* Notify we're no longer waiting for the SPI transfer to complete */
	guard(spinlock_irqsave)(&priv->irq_busy_lock);
	priv->rdata_xfer_busy = 0;
}

static int ads1298_update_scan_mode(struct iio_dev *indio_dev,
				    const unsigned long *scan_mask)
{
	struct ads1298_private *priv = iio_priv(indio_dev);
	unsigned int val;
	int ret;
	int i;

	/* Make the interrupt routines start with a clean slate */
	ads1298_rdata_unmark_busy(priv);

	/* Configure power-down bits to match scan mask */
	for (i = 0; i < indio_dev->num_channels; i++) {
		val = test_bit(i, scan_mask) ? 0 : ADS1298_MASK_CH_PD;
		ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(i),
					 ADS1298_MASK_CH_PD, val);
		if (ret)
			return ret;
	}

	return 0;
}

static const struct iio_info ads1298_info = {
	.read_raw = &ads1298_read_raw,
	.write_raw = &ads1298_write_raw,
	.update_scan_mode = &ads1298_update_scan_mode,
	.debugfs_reg_access = &ads1298_reg_access,
};

static void ads1298_rdata_release_busy_or_restart(struct ads1298_private *priv)
{
	guard(spinlock_irqsave)(&priv->irq_busy_lock);

	if (priv->rdata_xfer_busy > 1) {
		/*
		 * DRDY interrupt occurred before SPI completion. Start a new
		 * SPI transaction now to retrieve the data that wasn't latched
		 * into the ADS1298 chip's transfer buffer yet.
		 */
		spi_async(priv->spi, &priv->rdata_msg);
		/*
		 * If more than one DRDY took place, there was an overrun. Since
		 * the sample is already lost, reset the counter to 1 so that
		 * we will wait for a DRDY interrupt after this SPI transaction.
		 */
		priv->rdata_xfer_busy = 1;
	} else {
		/* No pending data, wait for DRDY */
		priv->rdata_xfer_busy = 0;
	}
}

/* Called from SPI completion interrupt handler */
static void ads1298_rdata_complete(void *context)
{
	struct iio_dev *indio_dev = context;
	struct ads1298_private *priv = iio_priv(indio_dev);
	int scan_index;
	u32 *bounce = priv->bounce_buffer;

	if (!iio_buffer_enabled(indio_dev)) {
		/*
		 * for a single transfer mode we're kept in direct_mode until
		 * completion, avoiding a race with buffered IO.
		 */
		ads1298_rdata_unmark_busy(priv);
		complete(&priv->completion);
		return;
	}

	/* Demux the channel data into our bounce buffer */
	for_each_set_bit(scan_index, indio_dev->active_scan_mask,
			 indio_dev->masklength) {
		const struct iio_chan_spec *scan_chan =
					&indio_dev->channels[scan_index];
		const u8 *data = priv->rx_buffer + scan_chan->address;

		*bounce++ = get_unaligned_be24(data);
	}

	/* rx_buffer can be overwritten from this point on */
	ads1298_rdata_release_busy_or_restart(priv);

	iio_push_to_buffers(indio_dev, priv->bounce_buffer);
}

static irqreturn_t ads1298_interrupt(int irq, void *dev_id)
{
	struct iio_dev *indio_dev = dev_id;
	struct ads1298_private *priv = iio_priv(indio_dev);
	unsigned int wasbusy;

	guard(spinlock_irqsave)(&priv->irq_busy_lock);

	wasbusy = priv->rdata_xfer_busy++;
	/* When no SPI transfer in transit, start one now */
	if (!wasbusy)
		spi_async(priv->spi, &priv->rdata_msg);

	return IRQ_HANDLED;
};

static int ads1298_buffer_postenable(struct iio_dev *indio_dev)
{
	struct ads1298_private *priv = iio_priv(indio_dev);
	int ret;

	/* Disable single-shot mode */
	ret = regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG4,
				 ADS1298_MASK_CONFIG4_SINGLE_SHOT, 0);
	if (ret)
		return ret;

	return ads1298_write_cmd(priv, ADS1298_CMD_START);
}

static int ads1298_buffer_predisable(struct iio_dev *indio_dev)
{
	struct ads1298_private *priv = iio_priv(indio_dev);

	return ads1298_write_cmd(priv, ADS1298_CMD_STOP);
}

static const struct iio_buffer_setup_ops ads1298_setup_ops = {
	.postenable = &ads1298_buffer_postenable,
	.predisable = &ads1298_buffer_predisable,
};

static void ads1298_reg_disable(void *reg)
{
	regulator_disable(reg);
}

static const struct regmap_range ads1298_regmap_volatile_range[] = {
	regmap_reg_range(ADS1298_REG_LOFF_STATP, ADS1298_REG_LOFF_STATN),
};

static const struct regmap_access_table ads1298_regmap_volatile = {
	.yes_ranges = ads1298_regmap_volatile_range,
	.n_yes_ranges = ARRAY_SIZE(ads1298_regmap_volatile_range),
};

static const struct regmap_config ads1298_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.reg_read = ads1298_reg_read,
	.reg_write = ads1298_reg_write,
	.max_register = ADS1298_REG_WCT2,
	.volatile_table = &ads1298_regmap_volatile,
	.cache_type = REGCACHE_MAPLE,
};

static int ads1298_init(struct iio_dev *indio_dev)
{
	struct ads1298_private *priv = iio_priv(indio_dev);
	struct device *dev = &priv->spi->dev;
	const char *suffix;
	unsigned int val;
	int ret;

	/* Device initializes into RDATAC mode, which we don't want */
	ret = ads1298_write_cmd(priv, ADS1298_CMD_SDATAC);
	if (ret)
		return ret;

	ret = regmap_read(priv->regmap, ADS1298_REG_ID, &val);
	if (ret)
		return ret;

	/* Fill in name and channel count based on what the chip told us */
	indio_dev->num_channels = 4 + 2 * (val & ADS1298_MASK_ID_CHANNELS);
	switch (val & ADS1298_MASK_ID_FAMILY) {
	case ADS1298_ID_FAMILY_ADS129X:
		suffix = "";
		break;
	case ADS1298_ID_FAMILY_ADS129XR:
		suffix = "r";
		break;
	default:
		return dev_err_probe(dev, -ENODEV, "Unknown ID: 0x%x\n", val);
	}
	indio_dev->name = devm_kasprintf(dev, GFP_KERNEL, "ads129%u%s",
					 indio_dev->num_channels, suffix);

	/* Enable internal test signal, double amplitude, double frequency */
	ret = regmap_write(priv->regmap, ADS1298_REG_CONFIG2,
			   ADS1298_MASK_CONFIG2_RESERVED |
			   ADS1298_MASK_CONFIG2_INT_TEST |
			   ADS1298_MASK_CONFIG2_TEST_AMP |
			   ADS1298_MASK_CONFIG2_TEST_FREQ_FAST);
	if (ret)
		return ret;

	val = ADS1298_MASK_CONFIG3_RESERVED; /* Must write 1 always */
	if (!priv->reg_vref) {
		/* Enable internal reference */
		val |= ADS1298_MASK_CONFIG3_PWR_REFBUF;
		/* Use 4V VREF when power supply is at least 4.4V */
		if (regulator_get_voltage(priv->reg_avdd) >= 4400000)
			val |= ADS1298_MASK_CONFIG3_VREF_4V;
	}
	return regmap_write(priv->regmap, ADS1298_REG_CONFIG3, val);
}

static int ads1298_probe(struct spi_device *spi)
{
	struct ads1298_private *priv;
	struct iio_dev *indio_dev;
	struct device *dev = &spi->dev;
	struct gpio_desc *reset_gpio;
	int ret;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
	if (!indio_dev)
		return -ENOMEM;

	priv = iio_priv(indio_dev);

	/* Reset to be asserted before enabling clock and power */
	reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
	if (IS_ERR(reset_gpio))
		return dev_err_probe(dev, PTR_ERR(reset_gpio),
				     "Cannot get reset GPIO\n");

	/* VREF can be supplied externally, otherwise use internal reference */
	priv->reg_vref = devm_regulator_get_optional(dev, "vref");
	if (IS_ERR(priv->reg_vref)) {
		if (PTR_ERR(priv->reg_vref) != -ENODEV)
			return dev_err_probe(dev, PTR_ERR(priv->reg_vref),
					     "Failed to get vref regulator\n");

		priv->reg_vref = NULL;
	} else {
		ret = regulator_enable(priv->reg_vref);
		if (ret)
			return ret;

		ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_vref);
		if (ret)
			return ret;
	}

	priv->clk = devm_clk_get_optional_enabled(dev, "clk");
	if (IS_ERR(priv->clk))
		return dev_err_probe(dev, PTR_ERR(priv->clk), "Failed to get clk\n");

	priv->reg_avdd = devm_regulator_get(dev, "avdd");
	if (IS_ERR(priv->reg_avdd))
		return dev_err_probe(dev, PTR_ERR(priv->reg_avdd),
				     "Failed to get avdd regulator\n");

	ret = regulator_enable(priv->reg_avdd);
	if (ret)
		return dev_err_probe(dev, ret, "Failed to enable avdd regulator\n");

	ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_avdd);
	if (ret)
		return ret;

	priv->spi = spi;
	init_completion(&priv->completion);
	spin_lock_init(&priv->irq_busy_lock);
	priv->regmap = devm_regmap_init(dev, NULL, priv, &ads1298_regmap_config);
	if (IS_ERR(priv->regmap))
		return PTR_ERR(priv->regmap);

	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
	indio_dev->channels = ads1298_channels;
	indio_dev->info = &ads1298_info;

	if (reset_gpio) {
		/*
		 * Deassert reset now that clock and power are active.
		 * Minimum reset pulsewidth is 2 clock cycles.
		 */
		fsleep(ADS1298_CLOCKS_TO_USECS(2));
		gpiod_set_value_cansleep(reset_gpio, 0);
	} else {
		ret = ads1298_write_cmd(priv, ADS1298_CMD_RESET);
		if (ret)
			return dev_err_probe(dev, ret, "RESET failed\n");
	}
	/* Wait 18 clock cycles for reset command to complete */
	fsleep(ADS1298_CLOCKS_TO_USECS(18));

	ret = ads1298_init(indio_dev);
	if (ret)
		return dev_err_probe(dev, ret, "Init failed\n");

	priv->tx_buffer[0] = ADS1298_CMD_RDATA;
	priv->rdata_xfer.tx_buf = priv->tx_buffer;
	priv->rdata_xfer.rx_buf = priv->rx_buffer;
	priv->rdata_xfer.len = ADS1298_SPI_RDATA_BUFFER_SIZE(indio_dev->num_channels);
	/* Must keep CS low for 4 clocks */
	priv->rdata_xfer.delay.value = 2;
	priv->rdata_xfer.delay.unit = SPI_DELAY_UNIT_USECS;
	spi_message_init_with_transfers(&priv->rdata_msg, &priv->rdata_xfer, 1);
	priv->rdata_msg.complete = &ads1298_rdata_complete;
	priv->rdata_msg.context = indio_dev;

	ret = devm_request_irq(dev, spi->irq, &ads1298_interrupt,
			       IRQF_TRIGGER_FALLING, indio_dev->name,
			       indio_dev);
	if (ret)
		return ret;

	ret = devm_iio_kfifo_buffer_setup(dev, indio_dev, &ads1298_setup_ops);
	if (ret)
		return ret;

	return devm_iio_device_register(dev, indio_dev);
}

static const struct spi_device_id ads1298_id[] = {
	{ "ads1298" },
	{ }
};
MODULE_DEVICE_TABLE(spi, ads1298_id);

static const struct of_device_id ads1298_of_table[] = {
	{ .compatible = "ti,ads1298" },
	{ }
};
MODULE_DEVICE_TABLE(of, ads1298_of_table);

static struct spi_driver ads1298_driver = {
	.driver = {
		.name	= "ads1298",
		.of_match_table = ads1298_of_table,
	},
	.probe		= ads1298_probe,
	.id_table	= ads1298_id,
};
module_spi_driver(ads1298_driver);

MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
MODULE_DESCRIPTION("TI ADS1298 ADC");
MODULE_LICENSE("GPL");