···6060 return step_en;6161}62626363+static u32 get_adc_chan_step_mask(struct tiadc_device *adc_dev,6464+ struct iio_chan_spec const *chan)6565+{6666+ int i;6767+6868+ for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {6969+ if (chan->channel == adc_dev->channel_line[i]) {7070+ u32 step;7171+7272+ step = adc_dev->channel_step[i];7373+ /* +1 for the charger */7474+ return 1 << (step + 1);7575+ }7676+ }7777+ WARN_ON(1);7878+ return 0;7979+}8080+6381static u32 get_adc_step_bit(struct tiadc_device *adc_dev, int chan)6482{6583 return 1 << adc_dev->channel_step[chan];···199181 enb |= (get_adc_step_bit(adc_dev, bit) << 1);200182 adc_dev->buffer_en_ch_steps = enb;201183202202- am335x_tsc_se_set(adc_dev->mfd_tscadc, enb);184184+ am335x_tsc_se_set_cache(adc_dev->mfd_tscadc, enb);203185204186 tiadc_writel(adc_dev, REG_IRQSTATUS, IRQENB_FIFO1THRES205187 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW);···217199 tiadc_writel(adc_dev, REG_IRQCLR, (IRQENB_FIFO1THRES |218200 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW));219201 am335x_tsc_se_clr(adc_dev->mfd_tscadc, adc_dev->buffer_en_ch_steps);202202+ adc_dev->buffer_en_ch_steps = 0;220203221204 /* Flush FIFO of leftover data in the time it takes to disable adc */222205 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);···347328 unsigned int fifo1count, read, stepid;348329 bool found = false;349330 u32 step_en;350350- unsigned long timeout = jiffies + usecs_to_jiffies351351- (IDLE_TIMEOUT * adc_dev->channels);331331+ unsigned long timeout;352332353333 if (iio_buffer_enabled(indio_dev))354334 return -EBUSY;355335356356- step_en = get_adc_step_mask(adc_dev);357357- am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);336336+ step_en = get_adc_chan_step_mask(adc_dev, chan);337337+ if (!step_en)338338+ return -EINVAL;358339359359- /* Wait for ADC sequencer to complete sampling */360360- while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {361361- if (time_after(jiffies, timeout))340340+ fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);341341+ while (fifo1count--)342342+ tiadc_readl(adc_dev, REG_FIFO1);343343+344344+ am335x_tsc_se_set_once(adc_dev->mfd_tscadc, step_en);345345+346346+ timeout = jiffies + usecs_to_jiffies347347+ (IDLE_TIMEOUT * adc_dev->channels);348348+ /* Wait for Fifo threshold interrupt */349349+ while (1) {350350+ fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);351351+ if (fifo1count)352352+ break;353353+354354+ if (time_after(jiffies, timeout)) {355355+ am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);362356 return -EAGAIN;363357 }358358+ }364359 map_val = chan->channel + TOTAL_CHANNELS;365360366361 /*367367- * When the sub-system is first enabled,368368- * the sequencer will always start with the369369- * lowest step (1) and continue until step (16).370370- * For ex: If we have enabled 4 ADC channels and371371- * currently use only 1 out of them, the372372- * sequencer still configures all the 4 steps,373373- * leading to 3 unwanted data.374374- * Hence we need to flush out this data.362362+ * We check the complete FIFO. We programmed just one entry but in case363363+ * something went wrong we left empty handed (-EAGAIN previously) and364364+ * then the value apeared somehow in the FIFO we would have two entries.365365+ * Therefore we read every item and keep only the latest version of the366366+ * requested channel.375367 */376376-377377- fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);378368 for (i = 0; i < fifo1count; i++) {379369 read = tiadc_readl(adc_dev, REG_FIFO1);380370 stepid = read & FIFOREAD_CHNLID_MASK;···395367 *val = (u16) read;396368 }397369 }370370+ am335x_tsc_se_adc_done(adc_dev->mfd_tscadc);398371399372 if (found == false)400373 return -EBUSY;···523494 tiadc_writel(adc_dev, REG_CTRL, restore);524495525496 tiadc_step_config(indio_dev);526526-497497+ am335x_tsc_se_set_cache(adc_dev->mfd_tscadc,498498+ adc_dev->buffer_en_ch_steps);527499 return 0;528500}529501
+2-2
drivers/input/touchscreen/ti_am335x_tsc.c
···198198 /* The steps1 … end and bit 0 for TS_Charge */199199 stepenable = (1 << (end_step + 2)) - 1;200200 ts_dev->step_mask = stepenable;201201- am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);201201+ am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);202202}203203204204static void titsc_read_coordinates(struct titsc *ts_dev,···322322323323 if (irqclr) {324324 titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);325325- am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);325325+ am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);326326 return IRQ_HANDLED;327327 }328328 return IRQ_NONE;
+63-14
drivers/mfd/ti_am335x_tscadc.c
···2424#include <linux/pm_runtime.h>2525#include <linux/of.h>2626#include <linux/of_device.h>2727+#include <linux/sched.h>27282829#include <linux/mfd/ti_am335x_tscadc.h>2930···4948 .val_bits = 32,5049};51505252-void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)5353-{5454- tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);5555-}5656-EXPORT_SYMBOL_GPL(am335x_tsc_se_update);5757-5858-void am335x_tsc_se_set(struct ti_tscadc_dev *tsadc, u32 val)5151+void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val)5952{6053 unsigned long flags;61546255 spin_lock_irqsave(&tsadc->reg_lock, flags);6363- tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);6464- tsadc->reg_se_cache |= val;6565- am335x_tsc_se_update(tsadc);5656+ tsadc->reg_se_cache = val;5757+ if (tsadc->adc_waiting)5858+ wake_up(&tsadc->reg_se_wait);5959+ else if (!tsadc->adc_in_use)6060+ tscadc_writel(tsadc, REG_SE, val);6161+6662 spin_unlock_irqrestore(&tsadc->reg_lock, flags);6763}6868-EXPORT_SYMBOL_GPL(am335x_tsc_se_set);6464+EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);6565+6666+static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tsadc)6767+{6868+ DEFINE_WAIT(wait);6969+ u32 reg;7070+7171+ /*7272+ * disable TSC steps so it does not run while the ADC is using it. If7373+ * write 0 while it is running (it just started or was already running)7474+ * then it completes all steps that were enabled and stops then.7575+ */7676+ tscadc_writel(tsadc, REG_SE, 0);7777+ reg = tscadc_readl(tsadc, REG_ADCFSM);7878+ if (reg & SEQ_STATUS) {7979+ tsadc->adc_waiting = true;8080+ prepare_to_wait(&tsadc->reg_se_wait, &wait,8181+ TASK_UNINTERRUPTIBLE);8282+ spin_unlock_irq(&tsadc->reg_lock);8383+8484+ schedule();8585+8686+ spin_lock_irq(&tsadc->reg_lock);8787+ finish_wait(&tsadc->reg_se_wait, &wait);8888+8989+ reg = tscadc_readl(tsadc, REG_ADCFSM);9090+ WARN_ON(reg & SEQ_STATUS);9191+ tsadc->adc_waiting = false;9292+ }9393+ tsadc->adc_in_use = true;9494+}9595+9696+void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val)9797+{9898+ spin_lock_irq(&tsadc->reg_lock);9999+ am335x_tscadc_need_adc(tsadc);100100+101101+ tscadc_writel(tsadc, REG_SE, val);102102+ spin_unlock_irq(&tsadc->reg_lock);103103+}104104+EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);105105+106106+void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc)107107+{108108+ unsigned long flags;109109+110110+ spin_lock_irqsave(&tsadc->reg_lock, flags);111111+ tsadc->adc_in_use = false;112112+ tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);113113+ spin_unlock_irqrestore(&tsadc->reg_lock, flags);114114+}115115+EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);6911670117void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)71118{72119 unsigned long flags;7312074121 spin_lock_irqsave(&tsadc->reg_lock, flags);7575- tsadc->reg_se_cache = tscadc_readl(tsadc, REG_SE);76122 tsadc->reg_se_cache &= ~val;7777- am335x_tsc_se_update(tsadc);123123+ tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);78124 spin_unlock_irqrestore(&tsadc->reg_lock, flags);79125}80126EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);···229181 }230182231183 spin_lock_init(&tscadc->reg_lock);184184+ init_waitqueue_head(&tscadc->reg_se_wait);185185+232186 pm_runtime_enable(&pdev->dev);233187 pm_runtime_get_sync(&pdev->dev);234188···352302353303 if (tscadc_dev->tsc_cell != -1)354304 tscadc_idle_config(tscadc_dev);355355- am335x_tsc_se_update(tscadc_dev);356305 restore = tscadc_readl(tscadc_dev, REG_CTRL);357306 tscadc_writel(tscadc_dev, REG_CTRL,358307 (restore | CNTRLREG_TSCSSENB));