Skip to content

Commit 7ca6740

Browse files
Sebastian Andrzej SiewiorLee Jones
authored andcommitted
mfd: input: iio: ti_amm335x: Rework TSC/ADC synchronization
The ADC driver always programs all possible ADC values and discards them except for the value IIO asked for. On the am335x-evm the driver programs four values and it takes 500us to gather them. Reducing the number of conversations down to the (required) one also reduces the busy loop down to 125us. This leads to another error, namely the FIFOCOUNT register is sometimes (like one out of 10 attempts) not updated in time leading to EBUSY. The next read has the FIFOCOUNT register updated. Checking for the ADCSTAT register for being idle isn't a good choice either. The problem is that if TSC is used at the same time, the HW completes the conversation for ADC *and* before the driver noticed it, the HW begins to perform a TSC conversation and so the driver never seen the HW idle. The next time we would have two values in the FIFO but since the driver reads everything we always see the current one. So instead of polling for the IDLE bit in ADCStatus register, we should check the FIFOCOUNT register. It should be one instead of zero because we request one value. This change in turn leads to another error. Sometimes if TSC & ADC are used together the TSC starts generating interrupts even if nobody actually touched the touchscreen. The interrupts seem valid because TSC's FIFO is filled with values for each channel of the TSC. This condition stops after a few ADC reads but will occur again. Not good. On top of this (even without the changes I just mentioned) there is a ADC & TSC lockup condition which was reported to me by Jeff Lance including the following test case: A busy loop of "cat /sys/bus/iio/devices/iio\:device0/in_voltage4_raw" and a mug on touch screen. With this setup, the hardware will lockup after something between 20 minutes and it could take up to a couple of hours. During that lockup, the ADCSTAT register says 0x30 (or 0x70) which means STEP_ID = IDLE and FSM_BUSY = yes. That means the hardware says that it is idle and busy at the same time which is an invalid condition. For all this reasons I decided to rework this TSC/ADC part and add a handshake / synchronization here: First the ADC signals that it needs the HW and writes a 0 mask into the SE register. The HW (if active) will complete the current conversation and become idle. The TSC driver will gather the values from the FIFO (woken up by an interrupt) and won't "enable" another conversation. Instead it will wake up the ADC driver which is already waiting. The ADC driver will start "its" conversation and once it is done, it will enable the TSC steps so the TSC will work again. After this rework I haven't observed the lockup so far. Plus the busy loop has been reduced from 500us to 125us. The continues-read mode remains unchanged. Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Acked-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Lee Jones <lee.jones@linaro.org>
1 parent 3954b7b commit 7ca6740

3 files changed

Lines changed: 103 additions & 28 deletions

File tree

‎drivers/iio/adc/ti_am335x_adc.c‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
6060
return step_en;
6161
}
6262

63+
static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,
64+
struct iio_chan_spec const *chan)
65+
{
66+
int i;
67+
68+
for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
69+
if (chan->channel == adc_dev->channel_line[i]) {
70+
u32 step;
71+
72+
step = adc_dev->channel_step[i];
73+
/* +1 for the charger */
74+
return 1 << (step + 1);
75+
}
76+
}
77+
WARN_ON(1);
78+
return 0;
79+
}
80+
6381
static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)
6482
{
6583
return 1 << adc_dev->channel_step[chan];
@@ -326,34 +344,43 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
326344
unsigned int fifo1count, read, stepid;
327345
bool found = false;
328346
u32 step_en;
329-
unsigned long timeout = jiffies + usecs_to_jiffies
330-
(IDLE_TIMEOUT * adc_dev->channels);
347+
unsigned long timeout;
331348

332349
if (iio_buffer_enabled(indio_dev))
333350
return -EBUSY;
334351

335-
step_en = get_adc_step_mask(adc_dev);
352+
step_en = get_adc_chan_step_mask(adc_dev, chan);
353+
if (!step_en)
354+
return -EINVAL;
355+
356+
fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
357+
while (fifo1count--)
358+
tiadc_readl(adc_dev, REG_FIFO1);
359+
336360
am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);
337361

338-
/* Wait for ADC sequencer to complete sampling */
339-
while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
340-
if (time_after(jiffies, timeout))
362+
timeout = jiffies + usecs_to_jiffies
363+
(IDLE_TIMEOUT * adc_dev->channels);
364+
/* Wait for Fifo threshold interrupt */
365+
while (1) {
366+
fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
367+
if (fifo1count)
368+
break;
369+
370+
if (time_after(jiffies, timeout)) {
371+
am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
341372
return -EAGAIN;
373+
}
342374
}
343375
map_val = chan->channel + TOTAL_CHANNELS;
344376

345377
/*
346-
* When the sub-system is first enabled,
347-
* the sequencer will always start with the
348-
* lowest step (1) and continue until step (16).
349-
* For ex: If we have enabled 4 ADC channels and
350-
* currently use only 1 out of them, the
351-
* sequencer still configures all the 4 steps,
352-
* leading to 3 unwanted data.
353-
* Hence we need to flush out this data.
378+
* We check the complete FIFO. We programmed just one entry but in case
379+
* something went wrong we left empty handed (-EAGAIN previously) and
380+
* then the value apeared somehow in the FIFO we would have two entries.
381+
* Therefore we read every item and keep only the latest version of the
382+
* requested channel.
354383
*/
355-
356-
fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
357384
for (i = 0; i < fifo1count; i++) {
358385
read = tiadc_readl(adc_dev, REG_FIFO1);
359386
stepid = read & FIFOREAD_CHNLID_MASK;
@@ -365,6 +392,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
365392
*val = (u16) read;
366393
}
367394
}
395+
am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);
368396

369397
if (found == false)
370398
return -EBUSY;
@@ -492,8 +520,8 @@ static int tiadc_resume(struct device *dev)
492520
tiadc_writel(adc_dev, REG_CTRL, restore);
493521

494522
tiadc_step_config(indio_dev);
495-
am335x_tsc_se_set(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);
496-
523+
am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,
524+
adc_dev->buffer_en_ch_steps);
497525
return 0;
498526
}
499527

‎drivers/mfd/ti_am335x_tscadc.c‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/pm_runtime.h>
2525
#include <linux/of.h>
2626
#include <linux/of_device.h>
27+
#include <linux/sched.h>
2728

2829
#include <linux/mfd/ti_am335x_tscadc.h>
2930

@@ -48,39 +49,79 @@ static const struct regmap_config tscadc_regmap_config = {
4849
.val_bits = 32,
4950
};
5051

51-
static void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)
52-
{
53-
tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
54-
}
55-
5652
void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val)
5753
{
5854
unsigned long flags;
5955

6056
spin_lock_irqsave(&tsadc->reg_lock, flags);
61-
tsadc->reg_se_cache |= val;
62-
am335x_tsc_se_update(tsadc);
57+
tsadc->reg_se_cache = val;
58+
if (tsadc->adc_waiting)
59+
wake_up(&tsadc->reg_se_wait);
60+
else if (!tsadc->adc_in_use)
61+
tscadc_writel(tsadc, REG_SE, val);
62+
6363
spin_unlock_irqrestore(&tsadc->reg_lock, flags);
6464
}
6565
EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
6666

67+
static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tsadc)
68+
{
69+
DEFINE_WAIT(wait);
70+
u32 reg;
71+
72+
/*
73+
* disable TSC steps so it does not run while the ADC is using it. If
74+
* write 0 while it is running (it just started or was already running)
75+
* then it completes all steps that were enabled and stops then.
76+
*/
77+
tscadc_writel(tsadc, REG_SE, 0);
78+
reg = tscadc_readl(tsadc, REG_ADCFSM);
79+
if (reg & SEQ_STATUS) {
80+
tsadc->adc_waiting = true;
81+
prepare_to_wait(&tsadc->reg_se_wait, &wait,
82+
TASK_UNINTERRUPTIBLE);
83+
spin_unlock_irq(&tsadc->reg_lock);
84+
85+
schedule();
86+
87+
spin_lock_irq(&tsadc->reg_lock);
88+
finish_wait(&tsadc->reg_se_wait, &wait);
89+
90+
reg = tscadc_readl(tsadc, REG_ADCFSM);
91+
WARN_ON(reg & SEQ_STATUS);
92+
tsadc->adc_waiting = false;
93+
}
94+
tsadc->adc_in_use = true;
95+
}
96+
6797
void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val)
98+
{
99+
spin_lock_irq(&tsadc->reg_lock);
100+
am335x_tscadc_need_adc(tsadc);
101+
102+
tscadc_writel(tsadc, REG_SE, val);
103+
spin_unlock_irq(&tsadc->reg_lock);
104+
}
105+
EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
106+
107+
void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc)
68108
{
69109
unsigned long flags;
70110

71111
spin_lock_irqsave(&tsadc->reg_lock, flags);
72-
tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache | val);
112+
tsadc->adc_in_use = false;
113+
tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
73114
spin_unlock_irqrestore(&tsadc->reg_lock, flags);
74115
}
75-
EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
116+
EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);
76117

77118
void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
78119
{
79120
unsigned long flags;
80121

81122
spin_lock_irqsave(&tsadc->reg_lock, flags);
82123
tsadc->reg_se_cache &= ~val;
83-
am335x_tsc_se_update(tsadc);
124+
tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
84125
spin_unlock_irqrestore(&tsadc->reg_lock, flags);
85126
}
86127
EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
@@ -188,6 +229,8 @@ static int ti_tscadc_probe(struct platform_device *pdev)
188229
}
189230

190231
spin_lock_init(&tscadc->reg_lock);
232+
init_waitqueue_head(&tscadc->reg_se_wait);
233+
191234
pm_runtime_enable(&pdev->dev);
192235
pm_runtime_get_sync(&pdev->dev);
193236

‎include/linux/mfd/ti_am335x_tscadc.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ struct ti_tscadc_dev {
159159
int adc_cell; /* -1 if not used */
160160
struct mfd_cell cells[TSCADC_CELLS];
161161
u32 reg_se_cache;
162+
bool adc_waiting;
163+
bool adc_in_use;
164+
wait_queue_head_t reg_se_wait;
162165
spinlock_t reg_lock;
163166
unsigned int clk_div;
164167

@@ -179,5 +182,6 @@ static inline struct ti_tscadc_dev *ti_tscadc_dev_get(struct platform_device *p)
179182
void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val);
180183
void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val);
181184
void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val);
185+
void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc);
182186

183187
#endif

0 commit comments

Comments
 (0)