Skip to content

Commit b1451e5

Browse files
patilrachnajic23
authored andcommitted
iio: ti_am335x_adc: Fix wrong samples received on 1st read
Previously we tried to read data form ADC even before ADC sequencer finished sampling. This led to wrong samples. We now wait on ADC status register idle bit to be set. Signed-off-by: Patil, Rachna <rachna@ti.com> Signed-off-by: Zubair Lutfullah <zubair.lutfullah@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent a1a8e1d commit b1451e5

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

‎drivers/iio/adc/ti_am335x_adc.c‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ static void tiadc_step_config(struct tiadc_device *adc_dev)
6060
{
6161
unsigned int stepconfig;
6262
int i, steps;
63-
u32 step_en;
6463

6564
/*
6665
* There are 16 configurable steps and 8 analog input
@@ -86,8 +85,7 @@ static void tiadc_step_config(struct tiadc_device *adc_dev)
8685
adc_dev->channel_step[i] = steps;
8786
steps++;
8887
}
89-
step_en = get_adc_step_mask(adc_dev);
90-
am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
88+
9189
}
9290

9391
static const char * const chan_name_ain[] = {
@@ -142,10 +140,22 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
142140
int *val, int *val2, long mask)
143141
{
144142
struct tiadc_device *adc_dev = iio_priv(indio_dev);
145-
int i;
146-
unsigned int fifo1count, read;
143+
int i, map_val;
144+
unsigned int fifo1count, read, stepid;
147145
u32 step = UINT_MAX;
148146
bool found = false;
147+
u32 step_en;
148+
unsigned long timeout = jiffies + usecs_to_jiffies
149+
(IDLE_TIMEOUT * adc_dev->channels);
150+
step_en = get_adc_step_mask(adc_dev);
151+
am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
152+
153+
/* Wait for ADC sequencer to complete sampling */
154+
while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
155+
if (time_after(jiffies, timeout))
156+
return -EAGAIN;
157+
}
158+
map_val = chan->channel + TOTAL_CHANNELS;
149159

150160
/*
151161
* When the sub-system is first enabled,
@@ -170,12 +180,16 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
170180
fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
171181
for (i = 0; i < fifo1count; i++) {
172182
read = tiadc_readl(adc_dev, REG_FIFO1);
173-
if (read >> 16 == step) {
174-
*val = read & 0xfff;
183+
stepid = read & FIFOREAD_CHNLID_MASK;
184+
stepid = stepid >> 0x10;
185+
186+
if (stepid == map_val) {
187+
read = read & FIFOREAD_DATA_MASK;
175188
found = true;
189+
*val = read;
176190
}
177191
}
178-
am335x_tsc_se_update(adc_dev->mfd_tscadc);
192+
179193
if (found == false)
180194
return -EBUSY;
181195
return IIO_VAL_INT;

‎include/linux/mfd/ti_am335x_tscadc.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,27 @@
113113
#define CNTRLREG_8WIRE CNTRLREG_AFE_CTRL(3)
114114
#define CNTRLREG_TSCENB BIT(7)
115115

116+
/* FIFO READ Register */
117+
#define FIFOREAD_DATA_MASK (0xfff << 0)
118+
#define FIFOREAD_CHNLID_MASK (0xf << 16)
119+
120+
/* Sequencer Status */
121+
#define SEQ_STATUS BIT(5)
122+
116123
#define ADC_CLK 3000000
117124
#define MAX_CLK_DIV 7
118125
#define TOTAL_STEPS 16
119126
#define TOTAL_CHANNELS 8
120127

128+
/*
129+
* ADC runs at 3MHz, and it takes
130+
* 15 cycles to latch one data output.
131+
* Hence the idle time for ADC to
132+
* process one sample data would be
133+
* around 5 micro seconds.
134+
*/
135+
#define IDLE_TIMEOUT 5 /* microsec */
136+
121137
#define TSCADC_CELLS 2
122138

123139
struct ti_tscadc_dev {

0 commit comments

Comments
 (0)