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:37:24 EST
On Mon, Nov 21, 2022 at 09:11:50PM +0530, Arun Ramadoss wrote:
> +static int ksz_ptp_enable_perout(struct ksz_device *dev,
> + struct ptp_perout_request const *perout_request,
> + int on)
> +{
> + struct ksz_ptp_data *ptp_data = &dev->ptp_data;
> + u64 cycle_width_ns;
> + u64 pulse_width_ns;
> + int ret;
> +
> + if (perout_request->flags & ~KSZ_PEROUT_VALID_FLAGS)
> + return -EINVAL;
> +
> + if (ptp_data->tou_mode != KSZ_PTP_TOU_PEROUT &&
> + ptp_data->tou_mode != KSZ_PTP_TOU_IDLE)
> + return -EBUSY;
> +
> + ret = ksz_ptp_tou_reset(dev, KSZ_PER_OUT_TOU);
> + if (ret)
> + return ret;
> +
> + if (!on) {
> + ptp_data->tou_mode = KSZ_PTP_TOU_IDLE;
> + return 0; /* success */
> + }
> +
> + ptp_data->perout_target_time_first.tv_sec = perout_request->start.sec;
> + ptp_data->perout_target_time_first.tv_nsec = perout_request->start.nsec;
> +
> + ptp_data->perout_period.tv_sec = perout_request->period.sec;
> + ptp_data->perout_period.tv_nsec = perout_request->period.nsec;
> +
> + cycle_width_ns = timespec64_to_ns(&ptp_data->perout_period);
> + if ((cycle_width_ns & TRIG_CYCLE_WIDTH_M) != cycle_width_ns)
> + return -EINVAL;
> +
> + if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE)
> + pulse_width_ns = perout_request->on.sec * NSEC_PER_SEC +
> + perout_request->on.nsec;
> +
> + else
> + /* Use a duty cycle of 50%. Maximum pulse width supported by the
> + * hardware is a little bit more than 125 ms.
> + */
> + pulse_width_ns = min_t(u64,
> + (perout_request->period.sec * NSEC_PER_SEC
> + + perout_request->period.nsec) / 2
> + / 8 * 8,
> + 125000000LL);
CodyStyle nit: if/else bodies need {} because of two lines in 'else'.
Thanks,
Richard
> +
> + ret = ksz_ptp_tou_pulse_verify(pulse_width_ns);
> + if (ret)
> + return ret;
> +
> + ret = ksz_ptp_configure_perout(dev, cycle_width_ns,
> + pulse_width_ns,
> + &ptp_data->perout_target_time_first);
> + if (ret)
> + return ret;
> +
> + /* Activate trigger unit */
> + ret = ksz9477_ptp_tou_start(dev);
> + if (ret)
> + return ret;
> +
> + ptp_data->tou_mode = KSZ_PTP_TOU_PEROUT;
> +
> + return 0;
> +}