Skip to content

Commit cefe8a3

Browse files
committed
Merge tag 'iio-fixes-for-3.11b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-linus
Jonathan writes: Second round of IIO fixes for the 3.11 cycle. 1) Fix a long term race in the IIO trigger handling. This only effects cases where a single trigger is in use by multiple devices. 2) ti_am335x fix an issue with incorrect data due to reading before the sequencer is finished.
2 parents 0207379 + b1451e5 commit cefe8a3

4 files changed

Lines changed: 62 additions & 21 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;

‎drivers/iio/industrialio-trigger.c‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ static struct iio_trigger *iio_trigger_find_by_name(const char *name,
127127
void iio_trigger_poll(struct iio_trigger *trig, s64 time)
128128
{
129129
int i;
130-
if (!trig->use_count)
131-
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
132-
if (trig->subirqs[i].enabled) {
133-
trig->use_count++;
130+
131+
if (!atomic_read(&trig->use_count)) {
132+
atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
133+
134+
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
135+
if (trig->subirqs[i].enabled)
134136
generic_handle_irq(trig->subirq_base + i);
135-
}
137+
else
138+
iio_trigger_notify_done(trig);
139+
}
140+
}
136141
}
137142
EXPORT_SYMBOL(iio_trigger_poll);
138143

@@ -146,19 +151,24 @@ EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
146151
void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
147152
{
148153
int i;
149-
if (!trig->use_count)
150-
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
151-
if (trig->subirqs[i].enabled) {
152-
trig->use_count++;
154+
155+
if (!atomic_read(&trig->use_count)) {
156+
atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
157+
158+
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
159+
if (trig->subirqs[i].enabled)
153160
handle_nested_irq(trig->subirq_base + i);
154-
}
161+
else
162+
iio_trigger_notify_done(trig);
163+
}
164+
}
155165
}
156166
EXPORT_SYMBOL(iio_trigger_poll_chained);
157167

158168
void iio_trigger_notify_done(struct iio_trigger *trig)
159169
{
160-
trig->use_count--;
161-
if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
170+
if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
171+
trig->ops->try_reenable)
162172
if (trig->ops->try_reenable(trig))
163173
/* Missed an interrupt so launch new poll now */
164174
iio_trigger_poll(trig, 0);

‎include/linux/iio/trigger.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
#include <linux/irq.h>
1010
#include <linux/module.h>
11+
#include <linux/atomic.h>
1112

1213
#ifndef _IIO_TRIGGER_H_
1314
#define _IIO_TRIGGER_H_
@@ -61,7 +62,7 @@ struct iio_trigger {
6162

6263
struct list_head list;
6364
struct list_head alloc_list;
64-
int use_count;
65+
atomic_t use_count;
6566

6667
struct irq_chip subirq_chip;
6768
int subirq_base;

‎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)