diff options
author | Josh Wu <josh.wu@atmel.com> | 2013-08-27 07:28:00 -0400 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2013-09-18 14:43:20 -0400 |
commit | db10e201172f48dad29d81ee1fec96384f0eab35 (patch) | |
tree | 8d99b9b0c2db7151988ba3f5c961763587e558a2 | |
parent | c3cb718acc17f8e0d6b4b8d1f8ca9a20d1999159 (diff) |
iio: at91: fix adc_clk overflow
The adc_clk variable is currently defined using a 32-bits unsigned integer,
which will overflow under some very valid range of operations.
Such overflow will occur if, for example, the parent clock is set to a
20MHz frequency and the ADC startup time is larger than 215ns.
To fix this, introduce an intermediate variable holding the clock rate
in kHz.
Signed-off-by: Josh Wu <josh.wu@atmel.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r-- | drivers/iio/adc/at91_adc.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c index 84be63bdf038..0f16b553e063 100644 --- a/drivers/iio/adc/at91_adc.c +++ b/drivers/iio/adc/at91_adc.c | |||
@@ -556,7 +556,7 @@ static const struct iio_info at91_adc_info = { | |||
556 | 556 | ||
557 | static int at91_adc_probe(struct platform_device *pdev) | 557 | static int at91_adc_probe(struct platform_device *pdev) |
558 | { | 558 | { |
559 | unsigned int prsc, mstrclk, ticks, adc_clk, shtim; | 559 | unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim; |
560 | int ret; | 560 | int ret; |
561 | struct iio_dev *idev; | 561 | struct iio_dev *idev; |
562 | struct at91_adc_state *st; | 562 | struct at91_adc_state *st; |
@@ -649,6 +649,7 @@ static int at91_adc_probe(struct platform_device *pdev) | |||
649 | */ | 649 | */ |
650 | mstrclk = clk_get_rate(st->clk); | 650 | mstrclk = clk_get_rate(st->clk); |
651 | adc_clk = clk_get_rate(st->adc_clk); | 651 | adc_clk = clk_get_rate(st->adc_clk); |
652 | adc_clk_khz = adc_clk / 1000; | ||
652 | prsc = (mstrclk / (2 * adc_clk)) - 1; | 653 | prsc = (mstrclk / (2 * adc_clk)) - 1; |
653 | 654 | ||
654 | if (!st->startup_time) { | 655 | if (!st->startup_time) { |
@@ -662,15 +663,15 @@ static int at91_adc_probe(struct platform_device *pdev) | |||
662 | * defined in the electrical characteristics of the board, divided by 8. | 663 | * defined in the electrical characteristics of the board, divided by 8. |
663 | * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock | 664 | * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock |
664 | */ | 665 | */ |
665 | ticks = round_up((st->startup_time * adc_clk / | 666 | ticks = round_up((st->startup_time * adc_clk_khz / |
666 | 1000000) - 1, 8) / 8; | 667 | 1000) - 1, 8) / 8; |
667 | /* | 668 | /* |
668 | * a minimal Sample and Hold Time is necessary for the ADC to guarantee | 669 | * a minimal Sample and Hold Time is necessary for the ADC to guarantee |
669 | * the best converted final value between two channels selection | 670 | * the best converted final value between two channels selection |
670 | * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock | 671 | * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock |
671 | */ | 672 | */ |
672 | shtim = round_up((st->sample_hold_time * adc_clk / | 673 | shtim = round_up((st->sample_hold_time * adc_clk_khz / |
673 | 1000000) - 1, 1); | 674 | 1000) - 1, 1); |
674 | 675 | ||
675 | reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask; | 676 | reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask; |
676 | reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask; | 677 | reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask; |