Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Sebastian Andrzej Siewior and committed by
Lee Jones
7ca6740c 3954b7bf

+103 -28
+46 -18
drivers/iio/adc/ti_am335x_adc.c
··· 60 60 return step_en; 61 61 } 62 62 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 + 63 81 static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan) 64 82 { 65 83 return 1 << adc_dev->channel_step[chan]; ··· 344 326 unsigned int fifo1count, read, stepid; 345 327 bool found = false; 346 328 u32 step_en; 347 - unsigned long timeout = jiffies + usecs_to_jiffies 348 - (IDLE_TIMEOUT * adc_dev->channels); 329 + unsigned long timeout; 349 330 350 331 if (iio_buffer_enabled(indio_dev)) 351 332 return -EBUSY; 352 333 353 - step_en = get_adc_step_mask(adc_dev); 334 + step_en = get_adc_chan_step_mask(adc_dev, chan); 335 + if (!step_en) 336 + return -EINVAL; 337 + 338 + fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 339 + while (fifo1count--) 340 + tiadc_readl(adc_dev, REG_FIFO1); 341 + 354 342 am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en); 355 343 356 - /* Wait for ADC sequencer to complete sampling */ 357 - while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) { 358 - if (time_after(jiffies, timeout)) 344 + timeout = jiffies + usecs_to_jiffies 345 + (IDLE_TIMEOUT * adc_dev->channels); 346 + /* Wait for Fifo threshold interrupt */ 347 + while (1) { 348 + fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 349 + if (fifo1count) 350 + break; 351 + 352 + if (time_after(jiffies, timeout)) { 353 + am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); 359 354 return -EAGAIN; 355 + } 360 356 } 361 357 map_val = chan->channel + TOTAL_CHANNELS; 362 358 363 359 /* 364 - * When the sub-system is first enabled, 365 - * the sequencer will always start with the 366 - * lowest step (1) and continue until step (16). 367 - * For ex: If we have enabled 4 ADC channels and 368 - * currently use only 1 out of them, the 369 - * sequencer still configures all the 4 steps, 370 - * leading to 3 unwanted data. 371 - * Hence we need to flush out this data. 360 + * We check the complete FIFO. We programmed just one entry but in case 361 + * something went wrong we left empty handed (-EAGAIN previously) and 362 + * then the value apeared somehow in the FIFO we would have two entries. 363 + * Therefore we read every item and keep only the latest version of the 364 + * requested channel. 372 365 */ 373 - 374 - fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 375 366 for (i = 0; i < fifo1count; i++) { 376 367 read = tiadc_readl(adc_dev, REG_FIFO1); 377 368 stepid = read & FIFOREAD_CHNLID_MASK; ··· 392 365 *val = (u16) read; 393 366 } 394 367 } 368 + am335x_tsc_se_adc_done(adc_dev->mfd_tscadc); 395 369 396 370 if (found == false) 397 371 return -EBUSY; ··· 520 492 tiadc_writel(adc_dev, REG_CTRL, restore); 521 493 522 494 tiadc_step_config(indio_dev); 523 - am335x_tsc_se_set(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps); 524 - 495 + am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, 496 + adc_dev->buffer_en_ch_steps); 525 497 return 0; 526 498 } 527 499
+53 -10
drivers/mfd/ti_am335x_tscadc.c
··· 24 24 #include <linux/pm_runtime.h> 25 25 #include <linux/of.h> 26 26 #include <linux/of_device.h> 27 + #include <linux/sched.h> 27 28 28 29 #include <linux/mfd/ti_am335x_tscadc.h> 29 30 ··· 49 48 .val_bits = 32, 50 49 }; 51 50 52 - static void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc) 53 - { 54 - tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache); 55 - } 56 - 57 51 void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val) 58 52 { 59 53 unsigned long flags; 60 54 61 55 spin_lock_irqsave(&tsadc->reg_lock, flags); 62 - tsadc->reg_se_cache |= val; 63 - am335x_tsc_se_update(tsadc); 56 + tsadc->reg_se_cache = val; 57 + if (tsadc->adc_waiting) 58 + wake_up(&tsadc->reg_se_wait); 59 + else if (!tsadc->adc_in_use) 60 + tscadc_writel(tsadc, REG_SE, val); 61 + 64 62 spin_unlock_irqrestore(&tsadc->reg_lock, flags); 65 63 } 66 64 EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache); 67 65 66 + static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tsadc) 67 + { 68 + DEFINE_WAIT(wait); 69 + u32 reg; 70 + 71 + /* 72 + * disable TSC steps so it does not run while the ADC is using it. If 73 + * write 0 while it is running (it just started or was already running) 74 + * then it completes all steps that were enabled and stops then. 75 + */ 76 + tscadc_writel(tsadc, REG_SE, 0); 77 + reg = tscadc_readl(tsadc, REG_ADCFSM); 78 + if (reg & SEQ_STATUS) { 79 + tsadc->adc_waiting = true; 80 + prepare_to_wait(&tsadc->reg_se_wait, &wait, 81 + TASK_UNINTERRUPTIBLE); 82 + spin_unlock_irq(&tsadc->reg_lock); 83 + 84 + schedule(); 85 + 86 + spin_lock_irq(&tsadc->reg_lock); 87 + finish_wait(&tsadc->reg_se_wait, &wait); 88 + 89 + reg = tscadc_readl(tsadc, REG_ADCFSM); 90 + WARN_ON(reg & SEQ_STATUS); 91 + tsadc->adc_waiting = false; 92 + } 93 + tsadc->adc_in_use = true; 94 + } 95 + 68 96 void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val) 97 + { 98 + spin_lock_irq(&tsadc->reg_lock); 99 + am335x_tscadc_need_adc(tsadc); 100 + 101 + tscadc_writel(tsadc, REG_SE, val); 102 + spin_unlock_irq(&tsadc->reg_lock); 103 + } 104 + EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once); 105 + 106 + void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc) 69 107 { 70 108 unsigned long flags; 71 109 72 110 spin_lock_irqsave(&tsadc->reg_lock, flags); 73 - tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache | val); 111 + tsadc->adc_in_use = false; 112 + tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache); 74 113 spin_unlock_irqrestore(&tsadc->reg_lock, flags); 75 114 } 76 - EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once); 115 + EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done); 77 116 78 117 void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val) 79 118 { ··· 121 80 122 81 spin_lock_irqsave(&tsadc->reg_lock, flags); 123 82 tsadc->reg_se_cache &= ~val; 124 - am335x_tsc_se_update(tsadc); 83 + tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache); 125 84 spin_unlock_irqrestore(&tsadc->reg_lock, flags); 126 85 } 127 86 EXPORT_SYMBOL_GPL(am335x_tsc_se_clr); ··· 229 188 } 230 189 231 190 spin_lock_init(&tscadc->reg_lock); 191 + init_waitqueue_head(&tscadc->reg_se_wait); 192 + 232 193 pm_runtime_enable(&pdev->dev); 233 194 pm_runtime_get_sync(&pdev->dev); 234 195
+4
include/linux/mfd/ti_am335x_tscadc.h
··· 159 159 int adc_cell; /* -1 if not used */ 160 160 struct mfd_cell cells[TSCADC_CELLS]; 161 161 u32 reg_se_cache; 162 + bool adc_waiting; 163 + bool adc_in_use; 164 + wait_queue_head_t reg_se_wait; 162 165 spinlock_t reg_lock; 163 166 unsigned int clk_div; 164 167 ··· 182 179 void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val); 183 180 void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val); 184 181 void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val); 182 + void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc); 185 183 186 184 #endif