Re: [PATCH 2/3] iio: dac: ad5706r: Add support for AD5706R DAC

Uwe Kleine-König
2026-02-20T10:51:52Z

[-- Attachment #1: Type: text/plain, Size: 1578 bytes --]

Hello,

On Fri, Feb 20, 2026 at 04:02:57PM +0800, Alexis Czezar Torreno wrote:
> +static int _set_pwm_duty_cycle(struct ad5706r_state *st, int duty_cycle)
> +{
> + struct pwm_state ldacb_pwm_state;
> + int ret;
> +
> + pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
> +
> + ldacb_pwm_state.duty_cycle = duty_cycle == 0 ? 0 :
> + DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency * 100 / duty_cycle);

This being integer math it would benefit when simplifying that to

10000000 * duty_cycle / st->sampling_frequency

. Consider st->sampling_frequency = 667 and duty_cycle = 99:

NANO / (st->sampling_frequency * 100 / duty_cycle) = 1484257.8710644676
DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency * 100 / duty_cycle) = 1485884
DIV_ROUND_CLOSEST_ULL(10000000 * duty_cycle, st->sampling_frequency) = 1484258

With duty_cycle <= 100 this doesn't even overflow.

> + ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
> + if (ret)
> + return ret;
> +
> + return 0;
> +}
> +
> +/* Device Attributes */
> +static ssize_t ad5706r_dev_addr_write(struct iio_dev *indio_dev,
> + uintptr_t private, const struct iio_chan_spec *chan,
> + const char *buf, size_t len)
> +{
> + struct ad5706r_state *st = iio_priv(indio_dev);
> + unsigned int reg_val;
> + int ret;
> +
> + ret = kstrtou32(buf, 10, &reg_val);
> + if (ret)
> + return ret;
> +
> + st->dev_addr = AD5706R_MASK_DEV_ADDR(reg_val);
> +
> + return ret ? ret : len;

This can be written as:

return ret ?: len;

It's a matter of taste which one you like better.

> +}
> +

Best regards
Uwe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]