Skip to content

Commit ddd999a

Browse files
ukleineknunojsa
authored andcommitted
iio: adc: ltc2387: Specify PWM parameters in nanoseconds
With the reference clock running at less than 1 GHz two different settings for the used PWM differ by more than 1 ns. So there is no advantage to differ from the upstream pwm subsystem and give the illusion of pico second resolution. With the goal to drop support for time_unit, don't explicitly set .time_unit = PWM_UNIT_NSEC but rely on this being the default. Also improve precision in the period calculation by not multiplying the result of two divisions but a single (reduced) division. Also given the new types of divisor and divident, use the cheaper DIV_ROUND_CLOSEST() division instead of DIV_ROUND_CLOSEST_ULL(). The error introduced by using nanoseconds instead of picoseconds isn't zero in corner cases, but with ref_clk_rate < 500 MHz doesn't increase the order of the error. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
1 parent 41161d8 commit ddd999a

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

drivers/iio/adc/ltc2387.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,15 @@ struct ltc2387_dev {
153153

154154
static int ltc2387_set_sampling_freq(struct ltc2387_dev *ltc, int freq)
155155
{
156-
unsigned long long target, ref_clk_period_ps;
156+
unsigned long long ref_clk_period_ns;
157157
struct pwm_state clk_en_state, cnv_state;
158158
int ret, clk_en_time;
159159

160-
target = DIV_ROUND_CLOSEST_ULL(ltc->ref_clk_rate, freq);
161-
ref_clk_period_ps = DIV_ROUND_CLOSEST_ULL(1000000000000ULL,
162-
ltc->ref_clk_rate);
160+
ref_clk_period_ns = DIV_ROUND_CLOSEST(NSEC_PER_SEC, ltc->ref_clk_rate);
163161

164162
cnv_state = (struct pwm_state) {
165-
.period = ref_clk_period_ps * target,
166-
.duty_cycle = ref_clk_period_ps,
167-
.time_unit = PWM_UNIT_PSEC,
163+
.period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, freq),
164+
.duty_cycle = ref_clk_period_ns,
168165
.enabled = true,
169166
};
170167

@@ -180,17 +177,16 @@ static int ltc2387_set_sampling_freq(struct ltc2387_dev *ltc, int freq)
180177

181178
clk_en_state = (struct pwm_state) {
182179
.period = cnv_state.period,
183-
.duty_cycle = ref_clk_period_ps * clk_en_time,
184-
.phase = cnv_state.phase + LTC2387_T_FIRSTCLK_NS * PSEC_PER_NSEC,
185-
.time_unit = PWM_UNIT_PSEC,
180+
.duty_cycle = ref_clk_period_ns * clk_en_time,
181+
.phase = LTC2387_T_FIRSTCLK_NS,
186182
.enabled = true,
187183
};
188184

189185
ret = pwm_apply_state(ltc->clk_en, &clk_en_state);
190186
if (ret < 0)
191187
return ret;
192188

193-
ltc->sampling_freq = DIV_ROUND_CLOSEST_ULL(ltc->ref_clk_rate, target);
189+
ltc->sampling_freq = freq;
194190

195191
return 0;
196192
}

0 commit comments

Comments
 (0)