@@ -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+
6381static u32 get_adc_step_bit (struct tiadc_device * adc_dev , int chan )
6482{
6583 return 1 << adc_dev -> channel_step [chan ];
@@ -181,7 +199,7 @@ static int tiadc_buffer_postenable(struct iio_dev *indio_dev)
181199 enb |= (get_adc_step_bit (adc_dev , bit ) << 1 );
182200 adc_dev -> buffer_en_ch_steps = enb ;
183201
184- am335x_tsc_se_set (adc_dev -> mfd_tscadc , enb );
202+ am335x_tsc_se_set_cache (adc_dev -> mfd_tscadc , enb );
185203
186204 tiadc_writel (adc_dev , REG_IRQSTATUS , IRQENB_FIFO1THRES
187205 | IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW );
@@ -199,6 +217,7 @@ static int tiadc_buffer_predisable(struct iio_dev *indio_dev)
199217 tiadc_writel (adc_dev , REG_IRQCLR , (IRQENB_FIFO1THRES |
200218 IRQENB_FIFO1OVRRUN | IRQENB_FIFO1UNDRFLW ));
201219 am335x_tsc_se_clr (adc_dev -> mfd_tscadc , adc_dev -> buffer_en_ch_steps );
220+ adc_dev -> buffer_en_ch_steps = 0 ;
202221
203222 /* Flush FIFO of leftover data in the time it takes to disable adc */
204223 fifo1count = tiadc_readl (adc_dev , REG_FIFO1CNT );
@@ -328,34 +347,43 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
328347 unsigned int fifo1count , read , stepid ;
329348 bool found = false;
330349 u32 step_en ;
331- unsigned long timeout = jiffies + usecs_to_jiffies
332- (IDLE_TIMEOUT * adc_dev -> channels );
350+ unsigned long timeout ;
333351
334352 if (iio_buffer_enabled (indio_dev ))
335353 return - EBUSY ;
336354
337- step_en = get_adc_step_mask (adc_dev );
338- am335x_tsc_se_set (adc_dev -> mfd_tscadc , step_en );
355+ step_en = get_adc_chan_step_mask (adc_dev , chan );
356+ if (!step_en )
357+ return - EINVAL ;
358+
359+ fifo1count = tiadc_readl (adc_dev , REG_FIFO1CNT );
360+ while (fifo1count -- )
361+ tiadc_readl (adc_dev , REG_FIFO1 );
362+
363+ am335x_tsc_se_set_once (adc_dev -> mfd_tscadc , step_en );
339364
340- /* Wait for ADC sequencer to complete sampling */
341- while (tiadc_readl (adc_dev , REG_ADCFSM ) & SEQ_STATUS ) {
342- if (time_after (jiffies , timeout ))
365+ timeout = jiffies + usecs_to_jiffies
366+ (IDLE_TIMEOUT * adc_dev -> channels );
367+ /* Wait for Fifo threshold interrupt */
368+ while (1 ) {
369+ fifo1count = tiadc_readl (adc_dev , REG_FIFO1CNT );
370+ if (fifo1count )
371+ break ;
372+
373+ if (time_after (jiffies , timeout )) {
374+ am335x_tsc_se_adc_done (adc_dev -> mfd_tscadc );
343375 return - EAGAIN ;
344376 }
377+ }
345378 map_val = chan -> channel + TOTAL_CHANNELS ;
346379
347380 /*
348- * When the sub-system is first enabled,
349- * the sequencer will always start with the
350- * lowest step (1) and continue until step (16).
351- * For ex: If we have enabled 4 ADC channels and
352- * currently use only 1 out of them, the
353- * sequencer still configures all the 4 steps,
354- * leading to 3 unwanted data.
355- * Hence we need to flush out this data.
381+ * We check the complete FIFO. We programmed just one entry but in case
382+ * something went wrong we left empty handed (-EAGAIN previously) and
383+ * then the value apeared somehow in the FIFO we would have two entries.
384+ * Therefore we read every item and keep only the latest version of the
385+ * requested channel.
356386 */
357-
358- fifo1count = tiadc_readl (adc_dev , REG_FIFO1CNT );
359387 for (i = 0 ; i < fifo1count ; i ++ ) {
360388 read = tiadc_readl (adc_dev , REG_FIFO1 );
361389 stepid = read & FIFOREAD_CHNLID_MASK ;
@@ -367,6 +395,7 @@ static int tiadc_read_raw(struct iio_dev *indio_dev,
367395 * val = (u16 ) read ;
368396 }
369397 }
398+ am335x_tsc_se_adc_done (adc_dev -> mfd_tscadc );
370399
371400 if (found == false)
372401 return - EBUSY ;
@@ -494,7 +523,8 @@ static int tiadc_resume(struct device *dev)
494523 tiadc_writel (adc_dev , REG_CTRL , restore );
495524
496525 tiadc_step_config (indio_dev );
497-
526+ am335x_tsc_se_set_cache (adc_dev -> mfd_tscadc ,
527+ adc_dev -> buffer_en_ch_steps );
498528 return 0 ;
499529}
500530
0 commit comments