Re: [RFC Patch net-next v2 8/8] net: dsa: microchip: ptp: add periodic output signal

From: Richard Cochran
Date: Tue Nov 22 2022 - 09:39:04 EST


On Mon, Nov 21, 2022 at 09:11:50PM +0530, Arun Ramadoss wrote:

> +static int ksz_ptp_restart_perout(struct ksz_device *dev)
> +{
> + struct ksz_ptp_data *ptp_data = &dev->ptp_data;
> + s64 now_ns, first_ns, period_ns, next_ns;
> + struct timespec64 now;
> + unsigned int count;
> + int ret;
> +
> + ret = _ksz_ptp_gettime(dev, &now);
> + if (ret)
> + return ret;
> +
> + now_ns = timespec64_to_ns(&now);
> + first_ns = timespec64_to_ns(&ptp_data->perout_target_time_first);
> +
> + /* Calculate next perout event based on start time and period */
> + period_ns = timespec64_to_ns(&ptp_data->perout_period);
> +
> + if (first_ns < now_ns) {
> + count = div_u64(now_ns - first_ns, period_ns);
> + next_ns = first_ns + count * period_ns;
> + } else {
> + next_ns = first_ns;
> + }
> +
> + /* Ensure 100 ms guard time prior next event */
> + while (next_ns < now_ns + 100000000)
> + next_ns += period_ns;
> +
> + /* Restart periodic output signal */
> + {

CodingStyle: avoid anonymous blocks. Move to helper function instead?

Thanks,
Richard


> + struct timespec64 next = ns_to_timespec64(next_ns);
> + struct ptp_perout_request perout_request = {
> + .start = {
> + .sec = next.tv_sec,
> + .nsec = next.tv_nsec
> + },
> + .period = {
> + .sec = ptp_data->perout_period.tv_sec,
> + .nsec = ptp_data->perout_period.tv_nsec
> + },
> + .index = 0,
> + .flags = 0, /* keep current values */
> + };
> + ret = ksz_ptp_enable_perout(dev, &perout_request, 1);
> + if (ret)
> + return ret;
> + }
> +
> + return 0;
> +}