aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorMichael Hennerich <michael.hennerich@analog.com>2012-07-20 04:31:00 -0400
committerJonathan Cameron <jic23@kernel.org>2012-08-16 15:24:33 -0400
commit8857df3aceb7a8eb7558059b7da109e41dd1fb95 (patch)
treeba45e6d7779aab84f6a7c1ba37697d9914158b97 /drivers/iio
parentdfffd0d65fdf16d034681716dcbea74776f62e40 (diff)
iio: frequency: ADF4350: Fix potential reference div factor overflow.
With small channel spacing values and high reference frequencies it is possible to exceed the range of the 10-bit counter. Workaround by checking the range and widening some constrains. We don't use the REG1_PHASE value in this case the datasheet recommends to set it to 1 if not used. Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/frequency/adf4350.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index 59fbb3ae40e7..e35bb8f6fe75 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -129,7 +129,7 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
129{ 129{
130 struct adf4350_platform_data *pdata = st->pdata; 130 struct adf4350_platform_data *pdata = st->pdata;
131 u64 tmp; 131 u64 tmp;
132 u32 div_gcd, prescaler; 132 u32 div_gcd, prescaler, chspc;
133 u16 mdiv, r_cnt = 0; 133 u16 mdiv, r_cnt = 0;
134 u8 band_sel_div; 134 u8 band_sel_div;
135 135
@@ -158,14 +158,20 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
158 if (pdata->ref_div_factor) 158 if (pdata->ref_div_factor)
159 r_cnt = pdata->ref_div_factor - 1; 159 r_cnt = pdata->ref_div_factor - 1;
160 160
161 do { 161 chspc = st->chspc;
162 r_cnt = adf4350_tune_r_cnt(st, r_cnt);
163 162
164 st->r1_mod = st->fpfd / st->chspc; 163 do {
165 while (st->r1_mod > ADF4350_MAX_MODULUS) { 164 do {
166 r_cnt = adf4350_tune_r_cnt(st, r_cnt); 165 do {
167 st->r1_mod = st->fpfd / st->chspc; 166 r_cnt = adf4350_tune_r_cnt(st, r_cnt);
168 } 167 st->r1_mod = st->fpfd / chspc;
168 if (r_cnt > ADF4350_MAX_R_CNT) {
169 /* try higher spacing values */
170 chspc++;
171 r_cnt = 0;
172 }
173 } while ((st->r1_mod > ADF4350_MAX_MODULUS) && r_cnt);
174 } while (r_cnt == 0);
169 175
170 tmp = freq * (u64)st->r1_mod + (st->fpfd > 1); 176 tmp = freq * (u64)st->r1_mod + (st->fpfd > 1);
171 do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */ 177 do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */
@@ -194,7 +200,7 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
194 st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) | 200 st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) |
195 ADF4350_REG0_FRACT(st->r0_fract); 201 ADF4350_REG0_FRACT(st->r0_fract);
196 202
197 st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(0) | 203 st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(1) |
198 ADF4350_REG1_MOD(st->r1_mod) | 204 ADF4350_REG1_MOD(st->r1_mod) |
199 prescaler; 205 prescaler;
200 206