Skip to contentSkip to conversation

dpll: zl3073x: add PTP clock support

PATCH Superseded Ivan Vecera 1 participant 6 messages Jul 30 – Jul 31

Subsystems (4)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ivan Vecera

author maintainer
#0 lore raw

v3 cover dpll: zl3073x: add PTP clock support

Add PTP hardware clock support to the zl3073x DPLL driver.

Patch 1 adds low-level channel operations for ToD read/write/adjust, output phase step, delta frequency offset write and TIE write as building blocks for PTP callbacks.

Patch 2 factors out the output pin frequency setting logic into an unlocked helper callable from internal contexts.

Patch 3 registers a PTP clock device for each DPLL channel with gettimex64, settime64, adjtime, adjfine, adjphase, getmaxphase and perout callbacks. Callback availability adapts to the current channel state - adjfine requires NCO pin connected, adjphase uses TIE write when tracking a reference, and adjtime selects the appropriate mechanism automatically.

Changes:
v3:
- Use PTP_1588_CLOCK instead of PTP_1588_CLOCK_OPTIONAL (Jakub)
- Factor out output pin frequency helper to a separate commit (Jakub)
- Remove spurious #include <linux/mutex.h> from dpll.h (Jakub)
v2:
- Squash low-level ops patches (1+3) into patch 1.
- Squash PTP callback patches (2+4) into patch 2, register PTP clock regardless of initial channel state.

Ivan Vecera (3):
dpll: zl3073x: add channel ToD, phase step and TIE operations
dpll: zl3073x: refactor output pin frequency set for internal use
dpll: zl3073x: add PTP clock support

 drivers/dpll/zl3073x/Kconfig |   6 +-
 drivers/dpll/zl3073x/chan.c  | 307 +++++++++++++++++++
 drivers/dpll/zl3073x/chan.h  |  48 +++
 drivers/dpll/zl3073x/core.c  |   8 +
 drivers/dpll/zl3073x/core.h  |   8 +
 drivers/dpll/zl3073x/dpll.c  | 575 +++++++++++++++++++++++++++++++++--
 drivers/dpll/zl3073x/dpll.h  |   5 +
 drivers/dpll/zl3073x/regs.h  |  56 ++++
 8 files changed, 977 insertions(+), 36 deletions(-)

base-commit: a5c6ae8de11ef0c3aedbe5e123354aad87d0d925

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v3 1/3 dpll: zl3073x: add channel ToD, phase step and TIE operations

Add low-level DPLL channel operations for ToD read/write/adjust, output phase step, delta frequency offset write and TIE (Time Interval Error) write. These serve as building blocks for the PTP clock callbacks added in the next patch.

ToD operations use a wait-before-write pattern to avoid blocking after each operation. tod_adjust additionally waits for completion since callers may follow with phase step operations.

The tod_ready_wait helper selects the poll timeout based on the current ToD command - write operations use a longer timeout (1000 ms) than reads (30 ms).

The ToD read captures system timestamps (ptp_system_timestamp) around the HW command and completion poll to support cross-timestamping.

The TIE write operation provides sub-picosecond resolution phase adjustment for modes where the DPLL is tracking a reference (AUTO and REFLOCK).

Add output step-time mask invariant to zl3073x_chan and zl3073x_chan_is_out_stepped() helper to check if an output participates in step-time operations.

---
 drivers/dpll/zl3073x/chan.c | 307 ++++++++++++++++++++++++++++++++++++
 drivers/dpll/zl3073x/chan.h |  48 ++++++
 drivers/dpll/zl3073x/core.c |   8 +
 drivers/dpll/zl3073x/core.h |   8 +
 drivers/dpll/zl3073x/regs.h |  56 +++++++
 5 files changed, 427 insertions(+)
drivers/dpll/zl3073x/chan.c +307 -0
index 4ec2cf53dad468..53032e782fd4af 100644
@@ -3,6 +3,7 @@
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/dev_printk.h>
+#include <linux/ptp_clock_kernel.h>
 #include <linux/string.h>
 #include <linux/types.h>
 
@@ -202,6 +203,11 @@ int zl3073x_chan_state_fetch(struct zl3073x_dev *zldev, u8 index)
 		zl3073x_chan_refsel_state_get(chan),
 		zl3073x_chan_refsel_ref_get(chan));
 
+	rc = zl3073x_read_u16(zldev, ZL_REG_OUTPUT_STEP_TIME_MASK,
+			      &chan->out_step_time_mask);
+	if (rc)
+		return rc;
+
 	guard(mutex)(&zldev->multiop_lock);
 
 	/* Read DPLL configuration from mailbox */
@@ -234,6 +240,307 @@ const struct zl3073x_chan *zl3073x_chan_state_get(struct zl3073x_dev *zldev,
 	return &zldev->chan[index];
 }
 
+/**
+ * zl3073x_chan_tod_ready_wait - wait for ToD semaphore to clear
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ *
+ * Polls the ToD control register until the semaphore bit is cleared,
+ * indicating the device has completed the previous ToD operation.
+ *
+ * Return: 0 on success, -EBUSY if semaphore not cleared, <0 on error
+ */
+static int zl3073x_chan_tod_ready_wait(struct zl3073x_dev *zldev, u8 ch)
+{
+	unsigned int timeout;
+	u8 tod_ctrl;
+	int rc;
+
+	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_TOD_CTRL(ch), &tod_ctrl);
+	if (rc)
+		return rc;
+
+	switch (FIELD_GET(ZL_DPLL_TOD_CTRL_CMD, tod_ctrl)) {
+	case ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ:
+		timeout = ZL_POLL_TOD_WR_TIMEOUT_US;
+		break;
+	default:
+		timeout = ZL_POLL_TOD_RD_TIMEOUT_US;
+		break;
+	}
+
+	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_DPLL_TOD_CTRL(ch),
+				  ZL_DPLL_TOD_CTRL_SEM, timeout);
+
+	return rc == -ETIMEDOUT ? -EBUSY : rc;
+}
+
+/**
+ * zl3073x_chan_tod_ctrl - issue ToD command
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @cmd: ToD command to execute
+ *
+ * Writes the semaphore and command to dpll_tod_ctrl. The caller must
+ * ensure the device is ready (semaphore clear) before calling and
+ * must wait for completion if needed.
+ *
+ * Return: 0 on success, <0 on error
+ */
+static int zl3073x_chan_tod_ctrl(struct zl3073x_dev *zldev, u8 ch, u8 cmd)
+{
+	return zl3073x_write_u8(zldev, ZL_REG_DPLL_TOD_CTRL(ch),
+				ZL_DPLL_TOD_CTRL_SEM | cmd);
+}
+
+/**
+ * zl3073x_chan_tod_read - read ToD registers after issuing a command
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @next_hz: if true, read predicted ToD at next 1 Hz; otherwise read current
+ * @ts: timespec to store the result
+ * @sts: optional system timestamp pair for cross-timestamping
+ *
+ * Context: Caller must serialize all zl3073x_chan_tod_* calls externally.
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_tod_read(struct zl3073x_dev *zldev, u8 ch,
+			  bool next_hz, struct timespec64 *ts,
+			  struct ptp_system_timestamp *sts)
+{
+	u32 nsec;
+	u64 sec;
+	u8 cmd;
+	int rc;
+
+	if (next_hz)
+		cmd = ZL_DPLL_TOD_CTRL_CMD_RD_NEXT_1HZ;
+	else
+		cmd = ZL_DPLL_TOD_CTRL_CMD_RD_CURRENT;
+
+	/* Wait for any previous ToD operation to complete */
+	rc = zl3073x_chan_tod_ready_wait(zldev, ch);
+	if (rc)
+		return rc;
+
+	ptp_read_system_prets(sts);
+	rc = zl3073x_chan_tod_ctrl(zldev, ch, cmd);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_chan_tod_ready_wait(zldev, ch);
+	if (rc)
+		return rc;
+	ptp_read_system_postts(sts);
+
+	rc = zl3073x_read_u48(zldev, ZL_REG_DPLL_TOD_SEC(ch), &sec);
+	if (rc)
+		return rc;
+
+	/* HW nanoseconds are always in [0, NSEC_PER_SEC) range */
+	rc = zl3073x_read_u32(zldev, ZL_REG_DPLL_TOD_NS(ch), &nsec);
+	if (rc)
+		return rc;
+
+	ts->tv_sec = sec;
+	ts->tv_nsec = nsec;
+
+	return 0;
+}
+
+/**
+ * zl3073x_chan_tod_write - write ToD registers and trigger 1 Hz update
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @ts: time to set
+ *
+ * Context: Caller must serialize all zl3073x_chan_tod_* calls externally.
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_tod_write(struct zl3073x_dev *zldev, u8 ch,
+			   struct timespec64 ts)
+{
+	int rc;
+
+	/* Wait for any previous ToD operation to complete */
+	rc = zl3073x_chan_tod_ready_wait(zldev, ch);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u48(zldev, ZL_REG_DPLL_TOD_SEC(ch), ts.tv_sec);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u32(zldev, ZL_REG_DPLL_TOD_NS(ch), ts.tv_nsec);
+	if (rc)
+		return rc;
+
+	return zl3073x_chan_tod_ctrl(zldev, ch,
+				    ZL_DPLL_TOD_CTRL_CMD_WR_NEXT_1HZ);
+}
+
+/**
+ * zl3073x_chan_tod_adjust - atomic ToD read-modify-write with rollover guard
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @delta: time adjustment to apply
+ *
+ * Reads the next-Hz ToD and current ToD, then checks whether enough time
+ * remains before the next 1 Hz rollover to safely complete the write.
+ * If less than 20 ms remains, waits for the rollover and increments the
+ * next-Hz seconds by one. Applies @delta and writes the result back.
+ *
+ * Context: Caller must serialize all zl3073x_chan_tod_* calls externally.
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_tod_adjust(struct zl3073x_dev *zldev, u8 ch,
+			    struct timespec64 delta)
+{
+	static const long threshold_ns = 20 * NSEC_PER_MSEC;
+	struct timespec64 ts_next, ts_cur, diff;
+	int rc;
+
+	/* Read predicted ToD at next 1 Hz tick */
+	rc = zl3073x_chan_tod_read(zldev, ch, true, &ts_next, NULL);
+	if (rc)
+		return rc;
+
+	/* Read current ToD to determine remaining margin */
+	rc = zl3073x_chan_tod_read(zldev, ch, false, &ts_cur, NULL);
+	if (rc)
+		return rc;
+
+	/* If too close to (or past) the next rollover, wait it out */
+	diff = timespec64_sub(ts_next, ts_cur);
+	if (diff.tv_sec < 0 || (!diff.tv_sec && diff.tv_nsec < threshold_ns)) {
+		if (!diff.tv_sec && diff.tv_nsec)
+			fsleep((unsigned long)diff.tv_nsec / NSEC_PER_USEC + 1);
+		ts_next.tv_sec++;
+	}
+
+	/* Apply delta to the next-Hz ToD */
+	ts_next = timespec64_add(ts_next, delta);
+	if (!timespec64_valid(&ts_next))
+		return -EINVAL;
+
+	/* Write adjusted ToD back and wait for completion */
+	rc = zl3073x_chan_tod_write(zldev, ch, ts_next);
+	if (rc)
+		return rc;
+
+	return zl3073x_chan_tod_ready_wait(zldev, ch);
+}
+
+/**
+ * zl3073x_chan_df_offset_set - write delta frequency offset to hardware
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @offset: frequency offset in 2^-48 steps
+ *
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_df_offset_set(struct zl3073x_dev *zldev, u8 ch, s64 offset)
+{
+	int rc;
+
+	rc = zl3073x_write_u48(zldev, ZL_REG_DPLL_DF_OFFSET(ch), offset);
+	if (!rc)
+		zldev->chan[ch].df_offset = offset;
+
+	return rc;
+}
+
+/**
+ * zl3073x_chan_tie_write - adjust DPLL phase using TIE write
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @delta_ns: phase adjustment in nanoseconds (must be within +-1s)
+ *
+ * Converts nanoseconds to TIE units (0.01 ps) and writes TIE data
+ * to the specified channel.
+ *
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns)
+{
+	s64 tie_data;
+	int rc;
+
+	guard(mutex)(&zldev->tie_lock);
+
+	/* Wait for any previous TIE operation to complete */
+	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_DPLL_TIE_CTRL,
+				  ZL_DPLL_TIE_CTRL_OP,
+				  ZL_POLL_TIE_WR_TIMEOUT_US);
+	if (rc)
+		return rc;
+
+	/* Convert ns to TIE units (0.01 ps = 10^-14 s) */
+	tie_data = delta_ns * 100000LL;
+
+	rc = zl3073x_write_u48(zldev, ZL_REG_DPLL_TIE_DATA(ch), tie_data);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_TIE_CTRL_MASK, BIT(ch));
+	if (rc)
+		return rc;
+
+	return zl3073x_write_u8(zldev, ZL_REG_DPLL_TIE_CTRL,
+				ZL_DPLL_TIE_CTRL_OP_WR);
+}
+
+/**
+ * zl3073x_chan_phase_step - execute one output phase step operation
+ * @zldev: pointer to zl3073x device
+ * @ch: DPLL channel index
+ * @out_mask: bitmask of outputs to step
+ * @step_cycles: phase step in synthesizer clock cycles
+ * @tod_step: also step the ToD counter
+ *
+ * All masked outputs must use synthesizers of the same frequency since
+ * the step value is in synthesizer clock cycles.
+ *
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_phase_step(struct zl3073x_dev *zldev, u8 ch,
+			    u16 out_mask, s32 step_cycles,
+			    bool tod_step)
+{
+	u8 ctrl;
+	int rc;
+
+	guard(mutex)(&zldev->phase_step_lock);
+
+	/* Wait for any previous phase step operation to complete */
+	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_OUTPUT_PHASE_STEP_CTRL,
+				  ZL_OUTPUT_PHASE_STEP_CTRL_OP,
+				  ZL_POLL_PHASE_STEP_TIMEOUT_US);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u32(zldev, ZL_REG_OUTPUT_PHASE_STEP_DATA,
+			       step_cycles);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u16(zldev, ZL_REG_OUTPUT_PHASE_STEP_MASK, out_mask);
+	if (rc)
+		return rc;
+
+	rc = zl3073x_write_u8(zldev, ZL_REG_OUTPUT_PHASE_STEP_NUMBER, 1);
+	if (rc)
+		return rc;
+
+	ctrl = FIELD_PREP(ZL_OUTPUT_PHASE_STEP_CTRL_DPLL, ch) |
+	       FIELD_PREP(ZL_OUTPUT_PHASE_STEP_CTRL_OP,
+			  ZL_OUTPUT_PHASE_STEP_CTRL_OP_WRITE);
+	if (tod_step)
+		ctrl |= ZL_OUTPUT_PHASE_STEP_CTRL_TOD_STEP;
+
+	return zl3073x_write_u8(zldev, ZL_REG_OUTPUT_PHASE_STEP_CTRL, ctrl);
+}
+
 /**
  * zl3073x_chan_state_set - commit DPLL channel state changes to hardware
  * @zldev: pointer to zl3073x_dev structure
drivers/dpll/zl3073x/chan.h +48 -0

86 lines not on this page · raw message

drivers/dpll/zl3073x/core.c +8 -0
index 7f5afaaae6342f..b2f9fc97fd1b68 100644
@@ -1033,6 +1033,14 @@ int zl3073x_dev_probe(struct zl3073x_dev *zldev)
 	 * and/or polls are required to be done atomically.
 	 */
 	rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock);
+	if (rc)
+		return dev_err_probe(zldev->dev, rc,
+				     "Failed to initialize mutex\n");
+	rc = devm_mutex_init(zldev->dev, &zldev->phase_step_lock);
+	if (rc)
+		return dev_err_probe(zldev->dev, rc,
+				     "Failed to initialize mutex\n");
+	rc = devm_mutex_init(zldev->dev, &zldev->tie_lock);
 	if (rc)
 		return dev_err_probe(zldev->dev, rc,
 				     "Failed to initialize mutex\n");
drivers/dpll/zl3073x/core.h +8 -0
index 78dc208f3eea2a..2dc836d0c8f6b3 100644
@@ -26,6 +26,10 @@ struct zl3073x_dpll;
 #define ZL_POLL_HWREG_TIMEOUT_US	(50 * USEC_PER_MSEC)
 #define ZL_POLL_MB_TIMEOUT_US		(30 * USEC_PER_MSEC)
 #define ZL_POLL_PHASE_ERR_TIMEOUT_US	(50 * USEC_PER_MSEC)
+#define ZL_POLL_PHASE_STEP_TIMEOUT_US	(3000 * USEC_PER_MSEC)
+#define ZL_POLL_TIE_WR_TIMEOUT_US	(1000 * USEC_PER_MSEC)
+#define ZL_POLL_TOD_RD_TIMEOUT_US	(30 * USEC_PER_MSEC)
+#define ZL_POLL_TOD_WR_TIMEOUT_US	(1000 * USEC_PER_MSEC)
 
 enum zl3073x_flags {
 	ZL3073X_FLAG_REF_PHASE_COMP_32_BIT,
@@ -55,6 +59,8 @@ struct zl3073x_chip_info {
  * @regmap: regmap to access device registers
  * @info: detected chip info
  * @multiop_lock: to serialize multiple register operations
+ * @tie_lock: to serialize TIE write operations
+ * @phase_step_lock: to serialize output phase step operations
  * @ref: array of input references' invariants
  * @out: array of outs' invariants
  * @synth: array of synths' invariants
@@ -71,6 +77,8 @@ struct zl3073x_dev {
 	struct regmap			*regmap;
 	const struct zl3073x_chip_info	*info;
 	struct mutex			multiop_lock;
+	struct mutex			tie_lock;
+	struct mutex			phase_step_lock;
 
 	/* Invariants */
 	struct zl3073x_ref	ref[ZL3073X_NUM_REFS];
drivers/dpll/zl3073x/regs.h +56 -0

86 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#2 lore raw

Re: [PATCH net-next v3 1/3] dpll: zl3073x: add channel ToD, phase step and TIE operations

Replies for Sashiko findings:

Quoted 7 lines

ZL_REG_OUTPUT_STEP_TIME_MASK looks device-global rather than per-channel
...
Would struct zl3073x_dev (next to clock_id, phase_avg_factor, freq_monitor) or struct zl3073x_out be a better home for it?

Related to that, zl3073x_chan_is_out_stepped(chan, out) takes a channel argument but returns the same answer for every channel.

Good catch. The register is indeed device-global. Will move it to struct zl3073x_dev, read it once during device init and change the helper to zl3073x_dev_is_out_stepped(zldev, out).

Quoted 5 lines

Can the diff.tv_sec < 0 path here end up writing the ToD with less than the documented 20 ms of margin?
...
For diff.tv_sec <= -2 ... the single ts_next.tv_sec++ leaves the target seconds value in the past

The two tod_read() calls are back-to-back regmap transactions, microseconds to a few milliseconds apart on I2C/SPI. A tick crossing between them is rare and leaves nearly 1 second of margin after the increment. The diff.tv_sec <= -2 case requires >1 second of preemption between two regmap reads which is not realistic.

That said, the code can be tightened to handle both cases correctly:

	if (!diff.tv_sec && diff.tv_nsec < threshold_ns) {
		fsleep((unsigned long)diff.tv_nsec / NSEC_PER_USEC + 1);
		ts_next.tv_sec++;
	} else if (diff.tv_sec < 0) {
		ts_next.tv_sec -= diff.tv_sec;
	}

The first branch handles the normal close-to-rollover case with a sleep. The second branch normalizes by the exact number of elapsed ticks, which covers both single and multi-tick crossings.

Quoted 4 lines

Is reporting a failure for an already committed write intended?
...
If a servo retries the same ADJ_SETOFFSET delta after that error, wouldn't the delta be applied twice?

Good point. Once WR_NEXT_1HZ is latched the write is committed and reporting an error risks double-application. Will drop the trailing tod_ready_wait() from tod_adjust() - every subsequent tod_* call starts with its own ready_wait anyway.

Quoted 1 line

Why is the item count here 8 rather than ZL3073X_MAX_CHANNELS?

Should be ZL3073X_MAX_CHANNELS for consistency with the other per-channel page 5 registers. Will fix.

Quoted 3 lines

regs.h is otherwise one section per hardware page in ascending page order ... Could the phase step defines go into the existing "Register Page 9, Synth and Output" section instead?

Agreed, will merge into the existing page 9 section.

Quoted 2 lines

The value column of the new ToD command defines does not line up with its neighbours

Will align.

Quoted 2 lines

the kernel-doc of zl3073x_chan_state_fetch() ... no longer mentions the added ZL_REG_OUTPUT_STEP_TIME_MASK read.

Will update the kernel-doc.

Ivan

author maintainer
#3 lore raw

PATCH v3 2/3 dpll: zl3073x: refactor output pin frequency set for internal use

In reply to Ivan Vecera

Extract the frequency-setting logic from the dpll_pin_ops callback into a separate unlocked helper __zl3073x_dpll_output_pin_frequency_set() that can be called internally while the per-DPLL lock is already held. The original callback becomes a thin wrapper that acquires the lock and delegates.

This prepares for the PTP clock support patch where ptp_enable() needs to set output pin frequency from an already-locked context.

---
 drivers/dpll/zl3073x/dpll.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/dpll/zl3073x/dpll.c +19 -8
index 0488ae6ac486c8..79433ba8bb4a4f 100644
@@ -935,21 +935,17 @@ zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin,
 }
 
 static int
-zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin,
-				      void *pin_priv,
-				      const struct dpll_device *dpll,
-				      void *dpll_priv, u64 frequency,
-				      struct netlink_ext_ack *extack)
+__zl3073x_dpll_output_pin_frequency_set(struct zl3073x_dpll *zldpll,
+					struct zl3073x_dpll_pin *pin,
+					u64 frequency)
 {
-	struct zl3073x_dpll *zldpll = dpll_priv;
 	struct zl3073x_dev *zldev = zldpll->dev;
-	struct zl3073x_dpll_pin *pin = pin_priv;
 	const struct zl3073x_synth *synth;
 	u32 new_div, synth_freq;
 	struct zl3073x_out out;
 	u8 out_id;
 
-	guard(mutex)(&zldpll->lock);
+	lockdep_assert_held(&zldpll->lock);
 
 	out_id = zl3073x_output_pin_out_get(pin->id);
 	out = *zl3073x_out_state_get(zldev, out_id);
@@ -1010,6 +1006,21 @@ zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin,
 	return zl3073x_out_state_set(zldev, out_id, &out);
 }
 
+static int
+zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin,
+				      void *pin_priv,
+				      const struct dpll_device *dpll,
+				      void *dpll_priv, u64 frequency,
+				      struct netlink_ext_ack *extack)
+{
+	struct zl3073x_dpll *zldpll = dpll_priv;
+
+	guard(mutex)(&zldpll->lock);
+
+	return __zl3073x_dpll_output_pin_frequency_set(zldpll, pin_priv,
+						       frequency);
+}
+
 static int
 zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin,
 					 void *pin_priv,
Signature · 1 line
2.54.0
author maintainer
#4 lore raw

PATCH v3 3/3 dpll: zl3073x: add PTP clock support

In reply to Ivan Vecera

Add PTP clock support for the ZL3073x DPLL driver. A PTP clock device is registered for each DPLL channel regardless of the initial channel state, providing gettimex64, settime64, adjtime, adjfine, adjphase, getmaxphase and perout callbacks.

Callback availability depends on the current channel state:
- adjfine: when NCO pin is connected (returns -EOPNOTSUPP otherwise)
- adjphase: available when tracking a reference, uses TIE write
- adjtime: always available and uses
* phase step for sub-second deltas when NCO pin is connected
* TIE write when tracking a reference
* plain ToD read-modify-write otherwise
- gettime/settime: always available

The adjtime callback splits multi-second adjustments into a ToD read-modify-write for the seconds part and a sub-second mechanism (phase step or TIE write) for the remainder. On partial failure where seconds were already committed, success is returned to prevent the PTP servo from retrying and applying seconds again.

Output pins eligible for periodic output (single-ended, marked in output_step_time_mask, supporting 1 Hz) are registered as PTP perout channels. Enabling perout sets the output frequency to 1 Hz via the existing DPLL frequency_set path.

All PTP callbacks are serialized by the existing per-DPLL zldpll->lock mutex, which is also used by DPLL pin and device callbacks.

---
 drivers/dpll/zl3073x/Kconfig |   6 +-
 drivers/dpll/zl3073x/dpll.c  | 548 +++++++++++++++++++++++++++++++++--
 drivers/dpll/zl3073x/dpll.h  |   5 +
 3 files changed, 531 insertions(+), 28 deletions(-)
drivers/dpll/zl3073x/Kconfig +3 -3
index 5bbca140058131..da1731ace15f02 100644
@@ -2,7 +2,7 @@
 
 config ZL3073X
 	tristate "Microchip Azurite DPLL/PTP/SyncE devices" if COMPILE_TEST
-	depends on NET
+	depends on NET && PTP_1588_CLOCK
 	select DPLL
 	select NET_DEVLINK
 	select REGMAP
@@ -16,7 +16,7 @@ config ZL3073X
 
 config ZL3073X_I2C
 	tristate "I2C bus implementation for Microchip Azurite devices"
-	depends on I2C && NET
+	depends on I2C && NET && PTP_1588_CLOCK
 	select REGMAP_I2C
 	select ZL3073X
 	help
@@ -28,7 +28,7 @@ config ZL3073X_I2C
 
 config ZL3073X_SPI
 	tristate "SPI bus implementation for Microchip Azurite devices"
-	depends on NET && SPI
+	depends on NET && SPI && PTP_1588_CLOCK
 	select REGMAP_SPI
 	select ZL3073X
 	help
drivers/dpll/zl3073x/dpll.c +523 -25

635 lines not on this page · raw message

drivers/dpll/zl3073x/dpll.h +5 -0
index faebc402ba1b70..993221dc63249d 100644
@@ -5,6 +5,7 @@
 
 #include <linux/dpll.h>
 #include <linux/list.h>
+#include <linux/ptp_clock_kernel.h>
 
 #include "core.h"
 
@@ -22,6 +23,8 @@
  * @type: DPLL type (PPS or EEC)
  * @lock_status: last saved DPLL lock status
  * @pins: list of pins
+ * @ptp_info: PTP clock info
+ * @ptp_clock: registered PTP clock (or NULL)
  */
 struct zl3073x_dpll {
 	struct list_head		list;
@@ -36,6 +39,8 @@ struct zl3073x_dpll {
 	enum dpll_type			type;
 	enum dpll_lock_status		lock_status;
 	struct list_head		pins;
+	struct ptp_clock_info		ptp_info;
+	struct ptp_clock		*ptp_clock;
 };
 
 struct zl3073x_dpll *zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch);
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

Replies to Sashiko's findings:

Quoted 3 lines

Is a hard dependency on PTP_1588_CLOCK required here?
...
Would "depends on NET && PTP_1588_CLOCK_OPTIONAL" work instead?

This was changed from OPTIONAL to a hard dependency at Jakub's explicit request in v2 review [1]. His rationale is that OPTIONAL is meant for NICs where PTP is truly a side-feature, while this is a dedicated DPLL/PTP device. Non-PTP chip variants simply skip PTP clock registration.

[1] https://lore.kernel.org/netdev/[email protected]/

Quoted 8 lines

does the kernel-doc match the implementation?
The body has a third gating condition ... zl3073x_chan_is_out_stepped()

Can two perout channels end up aliasing the same physical output?
...
Related: for single-ended N-divided formats, is the P-pin channel usable at all? ... out.esync_n_period ... truncates to 0

Quoted 5 lines

Is reporting success for a disable request that does nothing the intended behaviour?

How are the two userspace interfaces that can now program this output pin meant to coordinate?

Will drop perout from this series entirely. The current pin-based design has the P/N aliasing and N-divided truncation issues you identified, and the disable path needs rework to use the hardware's glitchless stop mechanism (output_ctrl stop bit). Will rework perout as a follow-up with an output-based design: one perout channel per physical output, eligibility checks in the enable callback, proper stop/start via output_ctrl, and output divisor restore on disable.

Quoted 1 line

Does settime64 need to compensate for the next-1 Hz load latency?

This is a known hardware characteristic. settime64 is used for large absolute time sets (boot, initial synchronization) where sub-second precision is not critical. The PTP servo uses adjtime/adjphase for fine control, both of which handle the 1 Hz boundary correctly. This matches the behavior of other I2C/SPI-attached PHC drivers (e.g. ptp_clockmatrix) that have the same HW constraint.

Quoted 2 lines

Can delta * synth_freq overflow s64 here?
For delta == S64_MIN the negation wraps, so abs() returns S64_MIN

Real bug. abs(S64_MIN) is undefined behavior and wraps to S64_MIN on two's complement, bypassing the >= NSEC_PER_SEC split. Will add an explicit guard at the top of adjtime.

Quoted 4 lines

Should a partial phase step really report success?
...
There, clock_adjtime(ADJ_SETOFFSET) reports success while up to ~999 ms of the requested step was never applied.

This is intentional. The seconds part is already committed via ToD read-modify-write and cannot be rolled back. Reporting an error would cause the PTP servo to retry the full delta, applying the seconds adjustment again. The sub-second residual self-corrects in the next servo cycle. A dev_warn is emitted so the condition is not silent.

The same reasoning applies to the partial phase step case: the ToD counter and some output groups are already stepped, so reporting success with a warning is the lesser evil compared to a retry that doubles the seconds adjustment.

Quoted 3 lines

Does first_synth_freq match what the comment describes?
...
On a multi-channel device those can be different synths

The code is correct - first_synth_freq is the lowest-ID synth assigned to this DPLL channel, which is the one the firmware uses for the ToD-only conversion. Will clarify the comment.

Regarding the empty output mask with tod_step: this is a documented firmware feature. When output mask is 0 and tod_step is set, the firmware steps only the ToD counter using the first assigned synth's clock period for the cycle-to-time conversion.

Thanks,
Ivan