Skip to content

Commit 8de877d

Browse files
Freeman LiuJonathan Cameron
authored andcommitted
iio: adc: sc27xx: Change to polling mode to read data
On Spreadtrum platform, the headphone will read one ADC channel multiple times to identify the headphone type, and the headphone identification is sensitive of the ADC reading time. And we found it will take longer time to reading ADC data by using interrupt mode comparing with the polling mode, thus we should change to polling mode to improve the efficiency of reading data, which can identify the headphone type successfully. Signed-off-by: Freeman Liu <freeman.liu@unisoc.com> Signed-off-by: Baolin Wang <baolin.wang@linaro.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 473d12f commit 8de877d

1 file changed

Lines changed: 27 additions & 54 deletions

File tree

‎drivers/iio/adc/sc27xx_adc.c‎

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <linux/hwspinlock.h>
55
#include <linux/iio/iio.h>
6-
#include <linux/interrupt.h>
76
#include <linux/module.h>
87
#include <linux/nvmem-consumer.h>
98
#include <linux/of.h>
@@ -46,14 +45,18 @@
4645
/* Bits definitions for SC27XX_ADC_INT_CLR registers */
4746
#define SC27XX_ADC_IRQ_CLR BIT(0)
4847

48+
/* Bits definitions for SC27XX_ADC_INT_RAW registers */
49+
#define SC27XX_ADC_IRQ_RAW BIT(0)
50+
4951
/* Mask definition for SC27XX_ADC_DATA register */
5052
#define SC27XX_ADC_DATA_MASK GENMASK(11, 0)
5153

5254
/* Timeout (ms) for the trylock of hardware spinlocks */
5355
#define SC27XX_ADC_HWLOCK_TIMEOUT 5000
5456

55-
/* Timeout (ms) for ADC data conversion according to ADC datasheet */
56-
#define SC27XX_ADC_RDY_TIMEOUT 100
57+
/* Timeout (us) for ADC data conversion according to ADC datasheet */
58+
#define SC27XX_ADC_RDY_TIMEOUT 1000000
59+
#define SC27XX_ADC_POLL_RAW_STATUS 500
5760

5861
/* Maximum ADC channel number */
5962
#define SC27XX_ADC_CHANNEL_MAX 32
@@ -72,10 +75,8 @@ struct sc27xx_adc_data {
7275
* subsystems which will access the unique ADC controller.
7376
*/
7477
struct hwspinlock *hwlock;
75-
struct completion completion;
7678
int channel_scale[SC27XX_ADC_CHANNEL_MAX];
7779
u32 base;
78-
int value;
7980
int irq;
8081
};
8182

@@ -188,9 +189,7 @@ static int sc27xx_adc_read(struct sc27xx_adc_data *data, int channel,
188189
int scale, int *val)
189190
{
190191
int ret;
191-
u32 tmp;
192-
193-
reinit_completion(&data->completion);
192+
u32 tmp, value, status;
194193

195194
ret = hwspin_lock_timeout_raw(data->hwlock, SC27XX_ADC_HWLOCK_TIMEOUT);
196195
if (ret) {
@@ -203,6 +202,11 @@ static int sc27xx_adc_read(struct sc27xx_adc_data *data, int channel,
203202
if (ret)
204203
goto unlock_adc;
205204

205+
ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_CLR,
206+
SC27XX_ADC_IRQ_CLR, SC27XX_ADC_IRQ_CLR);
207+
if (ret)
208+
goto disable_adc;
209+
206210
/* Configure the channel id and scale */
207211
tmp = (scale << SC27XX_ADC_SCALE_SHIFT) & SC27XX_ADC_SCALE_MASK;
208212
tmp |= channel & SC27XX_ADC_CHN_ID_MASK;
@@ -226,48 +230,34 @@ static int sc27xx_adc_read(struct sc27xx_adc_data *data, int channel,
226230
if (ret)
227231
goto disable_adc;
228232

229-
ret = wait_for_completion_timeout(&data->completion,
230-
msecs_to_jiffies(SC27XX_ADC_RDY_TIMEOUT));
231-
if (!ret) {
232-
dev_err(data->dev, "read ADC data timeout\n");
233-
ret = -ETIMEDOUT;
234-
} else {
235-
ret = 0;
233+
ret = regmap_read_poll_timeout(data->regmap,
234+
data->base + SC27XX_ADC_INT_RAW,
235+
status, (status & SC27XX_ADC_IRQ_RAW),
236+
SC27XX_ADC_POLL_RAW_STATUS,
237+
SC27XX_ADC_RDY_TIMEOUT);
238+
if (ret) {
239+
dev_err(data->dev, "read adc timeout, status = 0x%x\n", status);
240+
goto disable_adc;
236241
}
237242

243+
ret = regmap_read(data->regmap, data->base + SC27XX_ADC_DATA, &value);
244+
if (ret)
245+
goto disable_adc;
246+
247+
value &= SC27XX_ADC_DATA_MASK;
248+
238249
disable_adc:
239250
regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
240251
SC27XX_ADC_EN, 0);
241252
unlock_adc:
242253
hwspin_unlock_raw(data->hwlock);
243254

244255
if (!ret)
245-
*val = data->value;
256+
*val = value;
246257

247258
return ret;
248259
}
249260

250-
static irqreturn_t sc27xx_adc_isr(int irq, void *dev_id)
251-
{
252-
struct sc27xx_adc_data *data = dev_id;
253-
int ret;
254-
255-
ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_CLR,
256-
SC27XX_ADC_IRQ_CLR, SC27XX_ADC_IRQ_CLR);
257-
if (ret)
258-
return IRQ_RETVAL(ret);
259-
260-
ret = regmap_read(data->regmap, data->base + SC27XX_ADC_DATA,
261-
&data->value);
262-
if (ret)
263-
return IRQ_RETVAL(ret);
264-
265-
data->value &= SC27XX_ADC_DATA_MASK;
266-
complete(&data->completion);
267-
268-
return IRQ_HANDLED;
269-
}
270-
271261
static void sc27xx_adc_volt_ratio(struct sc27xx_adc_data *data,
272262
int channel, int scale,
273263
u32 *div_numerator, u32 *div_denominator)
@@ -454,11 +444,6 @@ static int sc27xx_adc_enable(struct sc27xx_adc_data *data)
454444
if (ret)
455445
goto disable_adc;
456446

457-
ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_EN,
458-
SC27XX_ADC_IRQ_EN, SC27XX_ADC_IRQ_EN);
459-
if (ret)
460-
goto disable_clk;
461-
462447
/* ADC channel scales' calibration from nvmem device */
463448
ret = sc27xx_adc_scale_calibration(data, true);
464449
if (ret)
@@ -484,9 +469,6 @@ static void sc27xx_adc_disable(void *_data)
484469
{
485470
struct sc27xx_adc_data *data = _data;
486471

487-
regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_EN,
488-
SC27XX_ADC_IRQ_EN, 0);
489-
490472
/* Disable ADC work clock and controller clock */
491473
regmap_update_bits(data->regmap, SC27XX_ARM_CLK_EN,
492474
SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN, 0);
@@ -551,7 +533,6 @@ static int sc27xx_adc_probe(struct platform_device *pdev)
551533
return ret;
552534
}
553535

554-
init_completion(&sc27xx_data->completion);
555536
sc27xx_data->dev = dev;
556537

557538
ret = sc27xx_adc_enable(sc27xx_data);
@@ -566,14 +547,6 @@ static int sc27xx_adc_probe(struct platform_device *pdev)
566547
return ret;
567548
}
568549

569-
ret = devm_request_threaded_irq(dev, sc27xx_data->irq, NULL,
570-
sc27xx_adc_isr, IRQF_ONESHOT,
571-
pdev->name, sc27xx_data);
572-
if (ret) {
573-
dev_err(dev, "failed to request ADC irq\n");
574-
return ret;
575-
}
576-
577550
indio_dev->dev.parent = dev;
578551
indio_dev->name = dev_name(dev);
579552
indio_dev->modes = INDIO_DIRECT_MODE;

0 commit comments

Comments
 (0)