From: Oleg Zadorozhnyi <Oleg…@devoxsoftware.com> Register each PLL as a DPLL device with its lock status and mode callbacks, and the XO as a pin on each. Inputs and outputs are registered by the patches that add their state callbacks. Probe also reports the EEPROM profile and its CRC. A kthread worker polls the status registers twice a second into a cache the callbacks answer from, so a netlink call does not become a burst of I2C transfers. A tick is about a hundred transfers, acknowledges the notification latches and notifies the core of what changed; a transition that begins and ends inside one period is not reported. INTRB, where wired, acknowledges them and schedules a tick early. The sleep PM operations stop and restart the poll. As the part may lose power over suspend, resume waits the start-up time, reads the input pair modes, the select mode of each PLL and the output routing again, and notifies what changed. A PLL that arbitrates by priority is reported AUTOMATIC, one on manual input select MANUAL. The clock_id is the nine-byte die ID folded to 64 bits; a blank or duplicate ID gives one from the SiTime OUI, the I2C bus number and the 7-bit address. Signed-off-by: Oleg Zadorozhnyi <Oleg…@devoxsoftware.com> Co-developed-by: Ali Rouhi <arou…@sitime.com> Signed-off-by: Ali Rouhi <arou…@sitime.com> --- drivers/dpll/sit9531x/Kconfig | 4 + drivers/dpll/sit9531x/Makefile | 2 +- drivers/dpll/sit9531x/core.c | 1596 +++++++++++++++++++++++++++++++- drivers/dpll/sit9531x/core.h | 137 ++- drivers/dpll/sit9531x/dpll.c | 279 ++++++ drivers/dpll/sit9531x/dpll.h | 60 ++ drivers/dpll/sit9531x/regs.h | 173 ++++ 7 files changed, 2199 insertions(+), 52 deletions(-) create mode 100644 drivers/dpll/sit9531x/dpll.c create mode 100644 drivers/dpll/sit9531x/dpll.h diff --git a/drivers/dpll/sit9531x/Kconfig b/drivers/dpll/sit9531x/Kconfig index ac5b7f31a034..47aea8674327 100644 --- a/drivers/dpll/sit9531x/Kconfig +++ b/drivers/dpll/sit9531x/Kconfig @@ -9,5 +9,9 @@ config SIT9531X_DPLL Driver for SiTime SiT9531x family clock generators (SiT95317, SiT95316). + This driver registers each on-chip PLL as a DPLL device + and exposes input/output clocks as DPLL pins, providing + runtime configuration via Generic Netlink. + To compile this driver as a module, choose M here: the module will be called sit9531x. diff --git a/drivers/dpll/sit9531x/Makefile b/drivers/dpll/sit9531x/Makefile index 819af61123f5..b97d2656a460 100644 --- a/drivers/dpll/sit9531x/Makefile +++ b/drivers/dpll/sit9531x/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_SIT9531X_DPLL) += sit9531x.o -sit9531x-y := core.o prop.o +sit9531x-y := core.o dpll.o prop.o diff --git a/drivers/dpll/sit9531x/core.c b/drivers/dpll/sit9531x/core.c index 6b9f3609f61c..4ff9e251eb77 100644 --- a/drivers/dpll/sit9531x/core.c +++ b/drivers/dpll/sit9531x/core.c @@ -6,9 +6,11 @@ * Author: Ali Rouhi <arou…@sitime.com> * Author: Oleg Zadorozhnyi <Oleg…@devoxsoftware.com> * - * I2C probe, paged regmap configuration and register access helpers. + * Core I2C probe, regmap configuration, hardware state management, DPLL + * device and pin registration, and periodic work thread. */ +#include <linux/bitfield.h> #include <linux/bits.h> #include <linux/clk.h> #include <linux/delay.h> @@ -16,96 +18,1345 @@ #include <linux/device.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h> +#include <linux/interrupt.h> #include <linux/kernel.h> +#include <linux/kthread.h> #include <linux/module.h> +#include <linux/pm.h> #include <linux/property.h> #include <linux/regmap.h> #include <linux/regulator/consumer.h> +#include <linux/slab.h> +#include <linux/string.h> #include "core.h" +#include "dpll.h" +#include "prop.h" #include "regs.h" +/* + * Extra pin positions: the crystal and the INTSYNC destination as inputs, the + * INTSYNC source as an output. + */ +#define SIT9531X_NUM_INPUT_PINS (SIT9531X_MAX_INPUTS + 2) +#define SIT9531X_NUM_OUTPUT_PINS (SIT9531X_MAX_OUTPUTS + 1) +#define SIT9531X_NUM_PINS_TOTAL (SIT9531X_NUM_INPUT_PINS + \ + SIT9531X_NUM_OUTPUT_PINS) + #define SIT9531X_CHIP(_id, _nin, _nout, _name, _map) \ { .id = (_id), .num_inputs = (_nin), .num_outputs = (_nout), \ .name = (_name), .clkout_map = (_map) } -/* Per-variant output index -> physical slot mapping */ -static const u8 clkout_map_95317[] = {0, 3, 4, 5, 7, 8, 9, 11}; -static const u8 clkout_map_95316[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; +/* Per-variant output index -> physical slot mapping */ +static const u8 clkout_map_95317[] = {0, 3, 4, 5, 7, 8, 9, 11}; +static const u8 clkout_map_95316[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + +static const struct sit9531x_chip_info sit9531x_chip_ids[] = { + SIT9531X_CHIP(SIT9531X_VARIANT_ID_95317, 8, 8, "SiT95317", + clkout_map_95317), + SIT9531X_CHIP(SIT9531X_VARIANT_ID_95316, 8, 12, "SiT95316", + clkout_map_95316), +}; + +/* + * One 256-register page is visible at a time, selected by offset 0xFF on every + * page. The map covers that window, uncached; the accessors select the page. + */ +static const struct regmap_config sit9531x_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = SIT9531X_PAGE_SEL, + .cache_type = REGCACHE_NONE, +}; + +/* Caller holds page_lock. */ +static int sit9531x_page_select(struct sit9531x_dev *sitdev, int page) +{ + int rc; + + if (sitdev->page == page) + return 0; + + sitdev->page = -1; + rc = regmap_write(sitdev->regmap, SIT9531X_PAGE_SEL, page); + if (rc) + return rc; + sitdev->page = page; + + return 0; +} + +static void sit9531x_page_cache_drop(struct sit9531x_dev *sitdev) +{ + mutex_lock(&sitdev->page_lock); + sitdev->page = -1; + mutex_unlock(&sitdev->page_lock); +} + +/* Caller holds page_lock. */ +static int sit9531x_read_locked(struct sit9531x_dev *sitdev, unsigned int reg, + unsigned int *val) +{ + int rc; + + rc = sit9531x_page_select(sitdev, SIT9531X_REG_PAGE(reg)); + if (rc) + return rc; + rc = regmap_read(sitdev->regmap, SIT9531X_REG_OFFSET(reg), val); + if (rc) + sitdev->page = -1; + + return rc; +} + +/* Caller holds page_lock. */ +static int sit9531x_write_locked(struct sit9531x_dev *sitdev, + unsigned int reg, u8 val) +{ + int rc; + + rc = sit9531x_page_select(sitdev, SIT9531X_REG_PAGE(reg)); + if (rc) + return rc; + rc = regmap_write(sitdev->regmap, SIT9531X_REG_OFFSET(reg), val); + if (rc) + sitdev->page = -1; + else if (SIT9531X_REG_OFFSET(reg) == SIT9531X_PAGE_SEL) + sitdev->page = val; + + return rc; +} + +/* @reg is a SIT9531X_REG(page, offset) value. */ +int sit9531x_read_u8(struct sit9531x_dev *sitdev, unsigned int reg, + u8 *val) +{ + unsigned int tmp; + int rc; + + mutex_lock(&sitdev->page_lock); + rc = sit9531x_read_locked(sitdev, reg, &tmp); + mutex_unlock(&sitdev->page_lock); + if (rc) { + dev_err_ratelimited(sitdev->dev, + "Failed to read page 0x%02x reg 0x%02x: %d\n", + SIT9531X_REG_PAGE(reg), + SIT9531X_REG_OFFSET(reg), rc); + return rc; + } + *val = (u8)tmp; + + return 0; +} + +/* @reg is a SIT9531X_REG(page, offset) value. */ +int sit9531x_write_u8(struct sit9531x_dev *sitdev, unsigned int reg, + u8 val) +{ + int rc; + + mutex_lock(&sitdev->page_lock); + rc = sit9531x_write_locked(sitdev, reg, val); + mutex_unlock(&sitdev->page_lock); + if (rc) + dev_err_ratelimited(sitdev->dev, + "Failed to write page 0x%02x reg 0x%02x: %d\n", + SIT9531X_REG_PAGE(reg), + SIT9531X_REG_OFFSET(reg), rc); + + return rc; +} + +int sit9531x_read_pll_u8(struct sit9531x_dev *sitdev, u8 pll_idx, + u8 offset, u8 *val) +{ + u8 page = sit9531x_pll_page(pll_idx); + + return sit9531x_read_u8(sitdev, SIT9531X_REG(page, offset), val); +} + +static void sit9531x_input_get_regs(const struct sit9531x_dev *sitdev, + u8 index, + unsigned int *force_reg, + unsigned int *state_reg) +{ + if (sitdev->ref[index].sig_mode == SIT9531X_MODE_DE) { + *force_reg = SIT9531X_REG_IN_DE_FORCE; + *state_reg = SIT9531X_REG_IN_DE_STATE; + } else if (sit9531x_input_is_n(index)) { + *force_reg = SIT9531X_REG_IN_SEN_FORCE; + *state_reg = SIT9531X_REG_IN_SEN_STATE; + } else { + *force_reg = SIT9531X_REG_IN_SEP_FORCE; + *state_reg = SIT9531X_REG_IN_SEP_STATE; + } +} + +/* + * Output Hi-Z control: each slot has three force/state pairs on Page 0x03, + * for the differential path and the CMOS OutP and OutN pads. The tri-state + * buffers sit after the output driver, independent of the pad selection. + * ODRn_MISC0 bits 3:2 distinguish a CMOS output from a differential one. + * + * slot 0-7 : + * DIFF 0xF2/0xF3 CMOS OutP 0xF8/0xF9 CMOS OutN 0xF6/0xF7 + * slot 8-11: + * DIFF 0xF4/0xF5 bits 3:0 CMOS OutP 0xFA/0xFB + * CMOS OutN 0xF4/0xF5 bits 7:4 + * + * MASK = 1 takes control of the Hi-Z state; STATE = 0 forces Hi-Z, 1 drives. + */ + +enum { + SIT9531X_HIZ_DIFF, + SIT9531X_HIZ_CMOS_P, + SIT9531X_HIZ_CMOS_N, + SIT9531X_HIZ_PAIRS, +}; + +struct sit9531x_hiz_pair { + unsigned int mask; + unsigned int state; + u8 bit; +}; + +static void sit9531x_output_get_hiz_regs(u8 slot, + struct sit9531x_hiz_pair *p) +{ + struct sit9531x_hiz_pair *cmos_p = &p[SIT9531X_HIZ_CMOS_P]; + struct sit9531x_hiz_pair *cmos_n = &p[SIT9531X_HIZ_CMOS_N]; + struct sit9531x_hiz_pair *diff = &p[SIT9531X_HIZ_DIFF]; + + if (slot <= 7) { + diff->mask = SIT9531X_REG_HIZ_DIFF_07_MASK; + diff->state = SIT9531X_REG_HIZ_DIFF_07_STATE; + diff->bit = slot; + cmos_p->mask = SIT9531X_REG_HIZ_SE_07_MASK; + cmos_p->state = SIT9531X_REG_HIZ_SE_07_STATE; + cmos_p->bit = slot; + cmos_n->mask = SIT9531X_REG_HIZ_SEN_07_MASK; + cmos_n->state = SIT9531X_REG_HIZ_SEN_07_STATE; + cmos_n->bit = slot; + } else { + diff->mask = SIT9531X_REG_HIZ_DIFF_811_MASK; + diff->state = SIT9531X_REG_HIZ_DIFF_811_STATE; + diff->bit = slot - 8; + cmos_p->mask = SIT9531X_REG_HIZ_SE_811_MASK; + cmos_p->state = SIT9531X_REG_HIZ_SE_811_STATE; + cmos_p->bit = slot - 8; + cmos_n->mask = SIT9531X_REG_HIZ_DIFF_811_MASK; + cmos_n->state = SIT9531X_REG_HIZ_DIFF_811_STATE; + cmos_n->bit = slot - 8 + SIT9531X_HIZ_SEN_811_SHIFT; + } +} + +/* + * Read whether an output is wired CMOS. Only the Hi-Z pairs for its wiring + * control the pad; the other pairs hold what the profile left. Caller must hold + * sitdev->multiop_lock. + */ +static int sit9531x_output_mode_fetch(struct sit9531x_dev *sitdev, u8 out_idx) +{ + const struct sit9531x_chip_info *info = sitdev->info; + u8 slot, page, reg, val; + int rc; + + slot = info->clkout_map[out_idx]; + page = (slot > SIT9531X_PAGE_OUTSYS0_SLOT_MAX) ? + SIT9531X_PAGE_OUTSYS1 : SIT9531X_PAGE_OUTSYS0; + reg = SIT9531X_OUT_MISC0_BASE + + SIT9531X_OUT_MISC0_STRIDE * (slot % 6); + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG(page, reg), &val); + if (rc) + return rc; + + sitdev->out[out_idx].cmos = !!(val & (SIT9531X_OUT_CMOS_ENP | + SIT9531X_OUT_CMOS_ENN)); + + return 0; +} + +static int sit9531x_hiz_pair_forced(struct sit9531x_dev *sitdev, + const struct sit9531x_hiz_pair *p, + bool *forced) +{ + u8 mask, state; + int rc; + + rc = sit9531x_read_u8(sitdev, p->mask, &mask); + if (rc) + return rc; + rc = sit9531x_read_u8(sitdev, p->state, &state); + if (rc) + return rc; + + *forced = (mask & BIT(p->bit)) && !(state & BIT(p->bit)); + + return 0; +} + +static int sit9531x_output_forced_hiz(struct sit9531x_dev *sitdev, + u8 out_idx, bool *muted, bool *part) +{ + struct sit9531x_hiz_pair p[SIT9531X_HIZ_PAIRS]; + bool pad_p, pad_n; + int rc; + + sit9531x_output_get_hiz_regs(sitdev->info->clkout_map[out_idx], p); + + *part = false; + if (!sitdev->out[out_idx].cmos) + return sit9531x_hiz_pair_forced(sitdev, + &p[SIT9531X_HIZ_DIFF], muted); + + rc = sit9531x_hiz_pair_forced(sitdev, &p[SIT9531X_HIZ_CMOS_P], &pad_p); + if (!rc) + rc = sit9531x_hiz_pair_forced(sitdev, &p[SIT9531X_HIZ_CMOS_N], + &pad_n); + if (rc) + return rc; + + *muted = pad_p && pad_n; + *part = pad_p != pad_n; + + return 0; +} + +static u16 sit9531x_prio_reg(u8 pll_idx, u8 slot) +{ + return SIT9531X_REG(SIT9531X_PAGE_PRIOSYS, + SIT9531X_PRIO_BASE_REG + + SIT9531X_PRIO_REGS_PER_PLL * pll_idx + + slot / SIT9531X_PRIO_SLOTS_PER_REG); +} + +/* Slot 2n is the high nibble of its register, 2n + 1 the low one. */ +static u8 sit9531x_prio_slot_get(u8 val, u8 slot) +{ + if (slot & 1) + return val & SIT9531X_PRIO_NIBBLE_MASK; + + return val >> SIT9531X_PRIO_HI_SHIFT; +} + +static void sit9531x_prio_mask_build(struct sit9531x_dev *sitdev, u8 pll_idx, + const u8 *srcs) +{ + u16 mask = 0; + u8 slot; + + for (slot = 0; slot < SIT9531X_PRIO_MAX_SLOTS; slot++) { + u8 src = srcs[slot] & SIT9531X_PRIO_NIBBLE_MASK; + + if (sit9531x_prio_src_usable(src)) + mask |= BIT(src); + } + + sitdev->chan[pll_idx].prio_mask = mask; +} + +/* Caller must hold sitdev->multiop_lock. */ +static int sit9531x_prio_table_read(struct sit9531x_dev *sitdev, u8 pll_idx, + u8 *srcs) +{ + u8 val, slot; + int rc; + + for (slot = 0; slot < SIT9531X_PRIO_MAX_SLOTS; slot++) { + rc = sit9531x_read_u8(sitdev, + sit9531x_prio_reg(pll_idx, slot), &val); + if (rc) + return rc; + + srcs[slot] = sit9531x_prio_slot_get(val, slot); + } + + return 0; +} + +/* The latches INTRB reports, each with its sticky bits */ +static const struct { + unsigned int reg; + u8 bits; +} sit9531x_notifs[] = { + { SIT9531X_REG_OUTER_LOL_NOTIF, GENMASK(3, 0) }, + { SIT9531X_REG_HO_FREEZE_NOTIF, GENMASK(7, 6) | GENMASK(3, 0) }, + { SIT9531X_REG_PLL_INNER_LOL_NOTIF, GENMASK(7, 6) | GENMASK(3, 0) }, + { SIT9531X_REG_CMON_NOTIF, GENMASK(5, 4) | GENMASK(1, 0) }, + { SIT9531X_REG_EEPROM_NOTIF, GENMASK(7, 0) }, + { SIT9531X_REG_EEPROM_BUS_NOTIF, GENMASK(4, 0) }, + { SIT9531X_CLKMON_P_NOTIF_01, GENMASK(7, 0) }, + { SIT9531X_CLKMON_P_NOTIF_23, GENMASK(7, 0) }, + { SIT9531X_CLKMON_N_NOTIF_01, GENMASK(7, 0) }, + { SIT9531X_CLKMON_N_NOTIF_23, GENMASK(7, 0) }, +}; + +/* + * Write back the set bits of each write-1-to-clear latch; @latched is true if + * any was set. Caller must hold sitdev->multiop_lock. + */ +static int sit9531x_clear_notifications(struct sit9531x_dev *sitdev, + bool *latched) +{ + unsigned int i; + u8 val; + int rc; + + lockdep_assert_held(&sitdev->multiop_lock); + + *latched = false; + for (i = 0; i < ARRAY_SIZE(sit9531x_notifs); i++) { + rc = sit9531x_read_u8(sitdev, sit9531x_notifs[i].reg, &val); + if (rc) + return rc; + val &= sit9531x_notifs[i].bits; + if (!val) + continue; + *latched = true; + rc = sit9531x_write_u8(sitdev, sit9531x_notifs[i].reg, val); + if (rc) + return rc; + } + + return 0; +} + +/* Read the lane's receiver state from the Page 0x02 force and state bits */ +static int sit9531x_ref_state_fetch(struct sit9531x_dev *sitdev, u8 index) +{ + unsigned int force_reg, state_reg; + struct sit9531x_ref *ref; + u8 pair, force, state; + int rc; + + /* The XO (index SIT9531X_MAX_INPUTS) has no receiver to read. */ + if (index >= SIT9531X_MAX_INPUTS) + return -EINVAL; + + ref = &sitdev->ref[index]; + pair = sit9531x_input_pair(index); + + /* + * A lane is disabled only while its force bit overrides it off; with + * the force bit clear the loaded configuration sets it. + */ + sit9531x_input_get_regs(sitdev, index, &force_reg, &state_reg); + + rc = sit9531x_read_u8(sitdev, force_reg, &force); + if (rc) + return rc; + rc = sit9531x_read_u8(sitdev, state_reg, &state); + if (rc) + return rc; + + ref->enabled = !((force & BIT(pair)) && !(state & BIT(pair))); + + return 0; +} + +/* + * Read CLKINx_INPUT_MODE into both lanes of the pair; a pair with neither SE + * lane enabled is differential. + */ +static int sit9531x_input_mode_fetch(struct sit9531x_dev *sitdev, u8 pair) +{ + enum sit9531x_signal_mode sig_mode; + u8 mode; + int rc; + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_IN_MODE(pair), &mode); + if (rc) + return rc; + + if (mode & (SIT9531X_IN_MODE_SE_P_EN | SIT9531X_IN_MODE_SE_N_EN)) + sig_mode = SIT9531X_MODE_SE; + else + sig_mode = SIT9531X_MODE_DE; + + sitdev->ref[pair * 2].sig_mode = sig_mode; + sitdev->ref[pair * 2 + 1].sig_mode = sig_mode; + + dev_dbg(sitdev->dev, "CLKIN%u mode reg 0x%02x -> %s\n", pair, mode, + sig_mode == SIT9531X_MODE_DE ? "differential" : "single-ended"); + + return 0; +} + +/* Read the PLL active-state bit (PLL page reg 0x02 bit 0). */ +static int sit9531x_pll_is_active(struct sit9531x_dev *sitdev, u8 pll_idx, + bool *active) +{ + int rc; + u8 v; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, SIT9531X_PLL_REG_ACTIVE, &v); + if (rc) + return rc; + + *active = !!(v & SIT9531X_PLL_ACTIVE_BIT); + + return 0; +} + +/* + * Manual active select (PLL_CONFIG1F_PLL bit 6) uses the table writes and is + * reported AUTOMATIC. Manual input select (MISCINNER_PLL bit 5) uses the + * input-select pins, or GPIO_INPUT_FUNC_CTRL5..8 when bit 4 is set, and is + * reported MANUAL. Caller must hold sitdev->multiop_lock. + */ +static int sit9531x_chan_select_fetch(struct sit9531x_dev *sitdev, u8 pll_idx) +{ + struct sit9531x_chan *chan = &sitdev->chan[pll_idx]; + bool manual_input = false; + u8 cfg, misc, sel; + int rc; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, SIT9531X_PLL_REG_CONFIG1F, + &cfg); + if (rc) + return rc; + if (!(cfg & SIT9531X_PLL_CONFIG1F_MANUAL_SEL)) + goto record; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, SIT9531X_PLL_REG_MISCINNER, + &misc); + if (rc) + return rc; + if (!(misc & SIT9531X_PLL_MISCINNER_MAN_IN_SEL)) { + dev_dbg(sitdev->dev, + "PLL%c: manual active select, the selection uses the priorities\n", + 'A' + pll_idx); + goto record; + } + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_MAN_IN_SEL(pll_idx), &sel); + if (rc) + return rc; + manual_input = true; + if (sel & SIT9531X_MAN_IN_SEL_FROM_REG) { + sel = FIELD_GET(SIT9531X_MAN_IN_SEL_MASK, sel); + dev_dbg(sitdev->dev, + "PLL%c: the profile selects its reference from the register, input code %u; reported as manual\n", + 'A' + pll_idx, sel); + } else { + dev_dbg(sitdev->dev, + "PLL%c: the profile selects its reference from the input-select pins; reported as manual\n", + 'A' + pll_idx); + } + +record: + chan->manual_input = manual_input; + + return 0; +} + +/* + * Read the PLL's run, lock and holdover state, mode, selection and priority + * table. + */ +static int sit9531x_chan_state_fetch(struct sit9531x_dev *sitdev, u8 pll_idx) +{ + u8 status, outer_lol, input_sel, inner_lol, ho_freeze, activesel_reg; + struct sit9531x_chan *chan = &sitdev->chan[pll_idx]; + u8 srcs[SIT9531X_PRIO_MAX_SLOTS]; + u8 pll_status_1; + bool active; + int rc; + + /* + * On an inactive PLL the loss-of-lock bit stays clear and reads as + * locked. + */ + rc = sit9531x_pll_is_active(sitdev, pll_idx, &active); + if (rc) + return rc; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, + SIT9531X_PLL_REG_STATUS, &status); + if (rc) + return rc; + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_OUTER_LOL_STATUS, + &outer_lol); + if (rc) + return rc; + + /* + * The active selection is CLK_ACTIVESEL_PLL, the low nibble of the last + * register of the PLL's Page 1 priority block. PLL-page 0x29 is a + * config register. + */ + activesel_reg = SIT9531X_PRIO_BASE_REG + + SIT9531X_PRIO_REGS_PER_PLL * pll_idx + + SIT9531X_PRIO_ACTIVESEL_OFF; + rc = sit9531x_read_u8(sitdev, + SIT9531X_REG(SIT9531X_PAGE_PRIOSYS, + activesel_reg), + &input_sel); + if (rc) + return rc; + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_PLL_INNER_LOL_STATUS, + &inner_lol); + if (rc) + return rc; + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_HO_FREEZE_STATUS, + &ho_freeze); + if (rc) + return rc; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, SIT9531X_PLL_REG_STATUS_1, + &pll_status_1); + if (rc) + return rc; + + rc = sit9531x_prio_table_read(sitdev, pll_idx, srcs); + if (rc) + return rc; + + sit9531x_prio_mask_build(sitdev, pll_idx, srcs); + + /* STATUS_1_GENERIC reports loss of lock, so invert it. */ + chan->active = active; + chan->locked = active && !(outer_lol & BIT(pll_idx)); + chan->mode = !!(status & SIT9531X_PLL_STATUS_OUTER_DIS); + chan->selected_ref = + sit9531x_hw_src_input(input_sel & SIT9531X_PRIO_NIBBLE_MASK); + chan->inner_lol = !!(inner_lol & BIT(pll_idx)); + chan->ho_freeze = !!(ho_freeze & BIT(pll_idx)); + chan->ho_valid = !!(pll_status_1 & SIT9531X_PLL_STATUS_1_HO_VALID); + chan->stale = false; + + return 0; +} + +/* + * Read routing from the PLL output map registers and drive state from the Hi-Z + * force bits. + */ +static int sit9531x_out_state_fetch(struct sit9531x_dev *sitdev, u8 index) +{ + struct sit9531x_out *out = &sitdev->out[index]; + u8 map_lo, map_hi, slot, bit, claimed; + bool muted, part; + int pll_idx; + u16 mask; + int rc; + + slot = sitdev->info->clkout_map[index]; + + rc = sit9531x_output_mode_fetch(sitdev, index); + if (rc) + return rc; + + rc = sit9531x_output_forced_hiz(sitdev, index, &muted, &part); + if (rc) + return rc; + + sitdev->out[index].part_muted = part; + + /* + * Each PLL page holds a 12-bit output-enable mask: OUT_MAP_LO bits 0-7 + * and OUT_MAP_HI[3:0] bits 8-11. PLLA and PLLB map slot n to bit n, + * PLLC and PLLD to bit 11 - n, as in the documented procedures. + */ + claimed = 0; + for (pll_idx = 0; pll_idx < SIT9531X_NUM_PLLS; pll_idx++) { + rc = sit9531x_read_pll_u8(sitdev, pll_idx, + SIT9531X_PLL_REG_OUT_MAP_LO, &map_lo); + if (rc) + return rc; + + rc = sit9531x_read_pll_u8(sitdev, pll_idx, + SIT9531X_PLL_REG_OUT_MAP_HI, &map_hi); + if (rc) + return rc; + + mask = map_lo | (map_hi & GENMASK(3, 0)) << 8; + bit = pll_idx < 2 ? slot : SIT9531X_MAX_OUTPUTS - 1 - slot; + if (mask & BIT(bit)) + claimed |= BIT(pll_idx); + } + + if (!claimed) { + out->pll_idx = 0; + out->routed = false; + out->enabled = false; + return 0; + } + + out->pll_idx = __ffs(claimed); + out->routed = true; + out->enabled = !muted; + + if (claimed & (claimed - 1)) + dev_warn(sitdev->dev, + "OUT%u is in the output-enable mask of more than one PLL (0x%x); taking PLL%c\n", + index, claimed, 'A' + out->pll_idx); + + return 0; +} + +/* + * Build ref->pll_mask from the priority tables of the PLLs that select by + * priority; it decides when a receiver may be powered down. Needs the select + * modes. Caller must hold sitdev->multiop_lock. + */ +static int sit9531x_ref_pll_mask_fetch(struct sit9531x_dev *sitdev) +{ + u8 srcs[SIT9531X_PRIO_MAX_SLOTS]; + u8 pll_idx, slot, index; + int rc; + + for (pll_idx = 0; pll_idx < SIT9531X_NUM_PLLS; pll_idx++) { + /* A PLL on manual input select does not use its table */ + if (sitdev->chan[pll_idx].manual_input) + continue; + + rc = sit9531x_prio_table_read(sitdev, pll_idx, srcs); + if (rc) + return rc; + + for (slot = 0; slot < SIT9531X_PRIO_MAX_SLOTS; slot++) { + index = sit9531x_hw_src_input(srcs[slot]); + if (index >= sitdev->info->num_inputs) + continue; + + /* + * A differential pair exposes only its P lane; count an + * N-lane entry as the P lane. + */ + if (sit9531x_input_is_n(index) && + sitdev->ref[index].sig_mode == SIT9531X_MODE_DE) + index--; + + sitdev->ref[index].pll_mask |= BIT(pll_idx); + } + } + + return 0; +} + +static int sit9531x_dev_state_fetch(struct sit9531x_dev *sitdev) +{ + int rc; + u8 i; + + /* Detect SE/DE configuration before any per-lane access */ + for (i = 0; i < sitdev->info->num_inputs / 2; i++) { + rc = sit9531x_input_mode_fetch(sitdev, i); + if (rc) { + dev_err(sitdev->dev, + "Failed to fetch CLKIN%u mode: %d\n", i, rc); + return rc; + } + } + + for (i = 0; i < sitdev->info->num_inputs; i++) { + rc = sit9531x_ref_state_fetch(sitdev, i); + if (rc) { + dev_err(sitdev->dev, + "Failed to fetch input %u state: %d\n", i, rc); + return rc; + } + } + + for (i = 0; i < sitdev->info->num_outputs; i++) { + mutex_lock(&sitdev->multiop_lock); + rc = sit9531x_out_state_fetch(sitdev, i); + mutex_unlock(&sitdev->multiop_lock); + if (rc) { + dev_err(sitdev->dev, + "Failed to fetch output %u state: %d\n", i, rc); + return rc; + } + } + + mutex_lock(&sitdev->multiop_lock); + for (i = 0; i < SIT9531X_NUM_PLLS; i++) { + rc = sit9531x_chan_select_fetch(sitdev, i); + if (!rc) + rc = sit9531x_chan_state_fetch(sitdev, i); + if (rc) + break; + } + mutex_unlock(&sitdev->multiop_lock); + if (rc) { + dev_err(sitdev->dev, "Failed to fetch PLL%c state: %d\n", + 'A' + i, rc); + return rc; + } + + mutex_lock(&sitdev->multiop_lock); + rc = sit9531x_ref_pll_mask_fetch(sitdev); + mutex_unlock(&sitdev->multiop_lock); + if (rc) { + dev_err(sitdev->dev, + "Failed to fetch input priority tables: %d\n", rc); + return rc; + } + + return 0; +} + +static void sit9531x_dev_ref_states_update(struct sit9531x_dev *sitdev) +{ + int i, rc; + + for (i = 0; i < sitdev->info->num_inputs; i++) { + rc = sit9531x_ref_state_fetch(sitdev, i); + if (rc) + dev_warn(sitdev->dev, + "Failed to get REF%u status: %d\n", i, rc); + } +} + +static void sit9531x_dev_chan_states_update(struct sit9531x_dev *sitdev) +{ + int i, rc; + + for (i = 0; i < SIT9531X_NUM_PLLS; i++) { + rc = sit9531x_chan_state_fetch(sitdev, i); + if (rc) { + sitdev->chan[i].stale = true; + dev_warn(sitdev->dev, + "Failed to get PLL%c state: %d\n", + 'A' + i, rc); + } + } +} + +static void sit9531x_dev_periodic_work(struct kthread_work *work) +{ + struct sit9531x_dev *sitdev = container_of(work, struct sit9531x_dev, + work.work); + struct sit9531x_dpll *sitdpll; + bool latched; + int rc; + + /* + * Release multiop_lock before the change checks: their notifications + * take DPLL-subsystem locks, which nest outside it. + */ + mutex_lock(&sitdev->multiop_lock); + sit9531x_dev_ref_states_update(sitdev); + sit9531x_dev_chan_states_update(sitdev); + mutex_unlock(&sitdev->multiop_lock); + + list_for_each_entry(sitdpll, &sitdev->dplls, list) + sit9531x_dpll_changes_check(sitdpll); + + /* + * Clear the W1C notification latches on pages 0 and 6 after acting on + * them; while set they keep INTRB asserted. + */ + mutex_lock(&sitdev->multiop_lock); + rc = sit9531x_clear_notifications(sitdev, &latched); + mutex_unlock(&sitdev->multiop_lock); + if (rc) + dev_warn_ratelimited(sitdev->dev, + "Failed to clear notifications: %d\n", + rc); + + kthread_queue_delayed_work(sitdev->kworker, &sitdev->work, + msecs_to_jiffies(SIT9531X_STATUS_POLL_MS)); +} + +/* Queue an immediate poll tick; polling continues as a fallback. */ +static irqreturn_t sit9531x_irq_thread_fn(int irq, void *data) +{ + struct sit9531x_dev *sitdev = data; + bool latched; + int rc; + + /* + * Acknowledge here: IRQF_ONESHOT unmasks the line on return, and a + * still-asserted INTRB would fire again at once. + */ + mutex_lock(&sitdev->multiop_lock); + rc = sit9531x_clear_notifications(sitdev, &latched); + mutex_unlock(&sitdev->multiop_lock); + if (rc) { + dev_warn_ratelimited(sitdev->dev, + "IRQ: failed to clear notifications: %d\n", + rc); + if (++sitdev->irq_ack_fails < SIT9531X_IRQ_ACK_TRIES) + return IRQ_NONE; + + dev_err(sitdev->dev, + "IRQ %d disabled: notifications cannot be cleared, polling only\n", + irq); + disable_irq_nosync(irq); + return IRQ_NONE; + } + + sitdev->irq_ack_fails = 0; + /* No latch set: the core counts the interrupt as unhandled */ + if (!latched) + return IRQ_NONE; + + kthread_mod_delayed_work(sitdev->kworker, &sitdev->work, 0); + return IRQ_HANDLED; +} + +/* + * Report the loaded EEPROM profile and warn on a bad load. Caller must hold + * sitdev->multiop_lock. + */ +static void sit9531x_eeprom_state_report(struct sit9531x_dev *sitdev) +{ + u32 rec_crc = 0, cal_crc = 0, prof_id = 0; + u8 notif, v; + int rc, i; + + lockdep_assert_held(&sitdev->multiop_lock); + + /* Profile id: three bytes, least significant first. */ + for (i = 2; i >= 0; i--) { + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_PROFILE_ID + i, &v); + if (rc) + return; + prof_id = prof_id << 8 | v; + } + + dev_dbg(sitdev->dev, "profile id %u\n", prof_id); + + /* Both CRCs: four bytes, most significant first. */ + for (i = 0; i < 4; i++) { + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_REC_CRC + i, &v); + if (rc) + return; + rec_crc = rec_crc << 8 | v; + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_CAL_CRC + i, &v); + if (rc) + return; + cal_crc = cal_crc << 8 | v; + } + + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_EEPROM_NOTIF, &notif); + if (rc) + return; + + /* + * A clean load sets only the read-done bit, and the poll acknowledges + * it with the other latches, so a later probe reads it clear. Both + * CRCs zero means no EEPROM read happened: the configuration was + * pushed over I2C. + */ + if (!rec_crc && !cal_crc) + dev_dbg(sitdev->dev, + "no EEPROM profile (configuration pushed over I2C)\n"); + else if (rec_crc != cal_crc) + dev_warn(sitdev->dev, + "EEPROM CRC mismatch: stored %08x, computed %08x\n", + rec_crc, cal_crc); + else if (notif & ~SIT9531X_EEPROM_READ_DONE) + dev_warn(sitdev->dev, + "EEPROM read reported defects (notify %02x)\n", + notif); + else + dev_dbg(sitdev->dev, "EEPROM profile loaded, CRC %08x\n", + cal_crc); +} + +/* + * Report which PLLs are active; an unused PLL never goes active. Caller must + * hold sitdev->multiop_lock. + */ +static void sit9531x_pll_states_report(struct sit9531x_dev *sitdev) +{ + unsigned int idx, i; + bool routed; + + lockdep_assert_held(&sitdev->multiop_lock); + + for (i = 0; i < SIT9531X_NUM_PLLS; i++) { + if (sitdev->chan[i].active) { + dev_dbg(sitdev->dev, "PLL%c active\n", 'A' + i); + continue; + } + + routed = false; + for (idx = 0; idx < sitdev->info->num_outputs; idx++) { + const struct sit9531x_out *out; + + out = sit9531x_out_state_get(sitdev, idx); + if (out->routed && out->pll_idx == i) { + routed = true; + break; + } + } + + if (routed) + dev_warn(sitdev->dev, + "PLL%c drives outputs but is not in its active state\n", + 'A' + i); + else + dev_dbg(sitdev->dev, "PLL%c unused by the loaded configuration\n", + 'A' + i); + } +} + +static int sit9531x_dev_start(struct sit9531x_dev *sitdev) +{ + struct sit9531x_dpll *sitdpll; + int rc; + + rc = sit9531x_dev_state_fetch(sitdev); + if (rc) + return rc; + + mutex_lock(&sitdev->multiop_lock); + sit9531x_eeprom_state_report(sitdev); + sit9531x_pll_states_report(sitdev); + mutex_unlock(&sitdev->multiop_lock); -static const struct sit9531x_chip_info sit9531x_chip_ids[] = { - SIT9531X_CHIP(SIT9531X_VARIANT_ID_95317, 8, 8, "SiT95317", - clkout_map_95317), - SIT9531X_CHIP(SIT9531X_VARIANT_ID_95316, 8, 12, "SiT95316", - clkout_map_95316), -}; + list_for_each_entry(sitdpll, &sitdev->dplls, list) { + rc = sit9531x_dpll_register(sitdpll); + if (rc) { + rc = dev_err_probe(sitdev->dev, rc, + "Failed to register DPLL%u\n", + sitdpll->id); + goto err_unregister; + } + } -/* - * One 256-register page is visible at a time, selected by offset 0xFF on every - * page. The map covers that window, uncached; the accessors select the page. - */ -static const struct regmap_config sit9531x_regmap_config = { - .reg_bits = 8, - .val_bits = 8, - .max_register = SIT9531X_PAGE_SEL, - .cache_type = REGCACHE_NONE, -}; + kthread_queue_delayed_work(sitdev->kworker, &sitdev->work, 0); -/* Caller holds page_lock. */ -static int sit9531x_page_select(struct sit9531x_dev *sitdev, int page) + return 0; + +err_unregister: + list_for_each_entry_continue_reverse(sitdpll, &sitdev->dplls, list) + sit9531x_dpll_unregister(sitdpll); + + return rc; +} + +static void sit9531x_dev_stop(struct sit9531x_dev *sitdev) +{ + struct sit9531x_dpll *sitdpll; + + kthread_cancel_delayed_work_sync(&sitdev->work); + + list_for_each_entry(sitdpll, &sitdev->dplls, list) { + if (sitdpll->dpll_dev) + sit9531x_dpll_unregister(sitdpll); + } +} + +static struct sit9531x_dpll_pin * +sit9531x_dpll_pin_alloc(struct sit9531x_dpll *sitdpll, + enum dpll_pin_direction dir, u8 id) +{ + struct sit9531x_dpll_pin *pin; + + pin = kzalloc_obj(*pin, GFP_KERNEL); + if (!pin) + return ERR_PTR(-ENOMEM); + + pin->dpll = sitdpll; + pin->dir = dir; + pin->id = id; + + return pin; +} + +/* @index is the pin's absolute index in the clock_id namespace. */ +static int sit9531x_dpll_pin_register(struct sit9531x_dpll_pin *pin, + u32 index) { + struct sit9531x_dpll *sitdpll = pin->dpll; + struct sit9531x_pin_props *props; int rc; - if (sitdev->page == page) - return 0; + props = sit9531x_pin_props_get(sitdpll->dev, pin->dir, pin->id); + if (IS_ERR(props)) + return PTR_ERR(props); - sitdev->page = -1; - rc = regmap_write(sitdev->regmap, SIT9531X_PAGE_SEL, page); + strscpy(pin->label, props->package_label, sizeof(pin->label)); + pin->fwnode = fwnode_handle_get(props->fwnode); + + pin->dpll_pin = dpll_pin_get(sitdpll->dev->clock_id, index, + THIS_MODULE, &props->dpll_props, + &pin->tracker); + if (IS_ERR(pin->dpll_pin)) { + rc = PTR_ERR(pin->dpll_pin); + goto err_pin_get; + } + dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode); + + rc = dpll_pin_register(sitdpll->dpll_dev, pin->dpll_pin, + sit9531x_dpll_pin_ops_get(pin), pin); if (rc) - return rc; - sitdev->page = page; + goto err_register; + + sit9531x_pin_props_put(props); return 0; + +err_register: + dpll_pin_put(pin->dpll_pin, &pin->tracker); +err_pin_get: + pin->dpll_pin = NULL; + fwnode_handle_put(pin->fwnode); + pin->fwnode = NULL; + sit9531x_pin_props_put(props); + + return rc; } -/* Caller holds page_lock. */ -static int sit9531x_read_locked(struct sit9531x_dev *sitdev, unsigned int reg, - unsigned int *val) +static void sit9531x_dpll_pin_unregister(struct sit9531x_dpll_pin *pin) +{ + struct sit9531x_dpll *sitdpll = pin->dpll; + struct dpll_pin *dpll_pin = pin->dpll_pin; + + /* + * Clear before unregistering: callbacks test it under the DPLL device + * lock, which dpll_pin_unregister() also takes. + */ + pin->dpll_pin = NULL; + + dpll_pin_unregister(sitdpll->dpll_dev, dpll_pin, + sit9531x_dpll_pin_ops_get(pin), pin); + dpll_pin_put(dpll_pin, &pin->tracker); + + fwnode_handle_put(pin->fwnode); + pin->fwnode = NULL; +} + +static void sit9531x_dpll_pins_unregister(struct sit9531x_dpll *sitdpll) +{ + struct sit9531x_dev *sitdev = sitdpll->dev; + struct sit9531x_dpll_pin *pin, *next; + LIST_HEAD(dead); + + /* + * Not under multiop_lock: the unregister takes the DPLL device lock, + * which nests outside it. + */ + list_for_each_entry(pin, &sitdpll->pins, list) + sit9531x_dpll_pin_unregister(pin); + + /* + * Detached under multiop_lock, as every list change is; the poll is + * already canceled or not yet queued. + */ + mutex_lock(&sitdev->multiop_lock); + list_splice_init(&sitdpll->pins, &dead); + mutex_unlock(&sitdev->multiop_lock); + + list_for_each_entry_safe(pin, next, &dead, list) { + list_del(&pin->list); + kfree(pin); + } +} + +/* Check if the pin class has a state callback, which the DPLL core requires */ +static bool sit9531x_dpll_pin_is_registrable(struct sit9531x_dpll *sitdpll, + enum dpll_pin_direction dir, + u8 index) +{ + if (dir != DPLL_PIN_DIRECTION_INPUT) + return false; + + return index == SIT9531X_MAX_INPUTS; +} + +static int sit9531x_dpll_pins_register(struct sit9531x_dpll *sitdpll) { + struct sit9531x_dpll_pin *pin; + enum dpll_pin_direction dir; + u8 id, index; int rc; - rc = sit9531x_page_select(sitdev, SIT9531X_REG_PAGE(reg)); - if (rc) + for (index = 0; index < SIT9531X_NUM_PINS_TOTAL; index++) { + if (index < SIT9531X_NUM_INPUT_PINS) { + id = index; + dir = DPLL_PIN_DIRECTION_INPUT; + } else { + id = index - SIT9531X_NUM_INPUT_PINS; + dir = DPLL_PIN_DIRECTION_OUTPUT; + } + + if (!sit9531x_dpll_pin_is_registrable(sitdpll, dir, id)) + continue; + + pin = sit9531x_dpll_pin_alloc(sitdpll, dir, id); + if (IS_ERR(pin)) { + rc = PTR_ERR(pin); + goto error; + } + + rc = sit9531x_dpll_pin_register(pin, index); + if (rc) { + kfree(pin); + goto error; + } + + /* List changes are made under multiop_lock */ + mutex_lock(&sitdpll->dev->multiop_lock); + list_add(&pin->list, &sitdpll->pins); + mutex_unlock(&sitdpll->dev->multiop_lock); + } + + return 0; + +error: + sit9531x_dpll_pins_unregister(sitdpll); + return rc; +} + +static int sit9531x_dpll_device_register(struct sit9531x_dpll *sitdpll) +{ + struct sit9531x_dev *sitdev = sitdpll->dev; + int rc; + + sitdpll->ops = sit9531x_dpll_device_ops; + + sitdpll->dpll_dev = dpll_device_get(sitdev->clock_id, sitdpll->id, + THIS_MODULE, &sitdpll->tracker); + if (IS_ERR(sitdpll->dpll_dev)) { + rc = PTR_ERR(sitdpll->dpll_dev); + sitdpll->dpll_dev = NULL; return rc; - rc = regmap_read(sitdev->regmap, SIT9531X_REG_OFFSET(reg), val); - if (rc) - sitdev->page = -1; + } + + rc = dpll_device_register(sitdpll->dpll_dev, + sit9531x_prop_dpll_type_get(sitdev, + sitdpll->id), + &sitdpll->ops, sitdpll); + if (rc) { + dpll_device_put(sitdpll->dpll_dev, &sitdpll->tracker); + sitdpll->dpll_dev = NULL; + } return rc; } -/* @reg is a SIT9531X_REG(page, offset) value. */ -int sit9531x_read_u8(struct sit9531x_dev *sitdev, unsigned int reg, - u8 *val) +static void sit9531x_dpll_device_unregister(struct sit9531x_dpll *sitdpll) +{ + dpll_device_unregister(sitdpll->dpll_dev, &sitdpll->ops, sitdpll); + dpll_device_put(sitdpll->dpll_dev, &sitdpll->tracker); + sitdpll->dpll_dev = NULL; +} + +struct sit9531x_dpll *sit9531x_dpll_alloc(struct sit9531x_dev *sitdev, u8 ch) +{ + struct sit9531x_dpll *sitdpll; + + sitdpll = kzalloc_obj(*sitdpll, GFP_KERNEL); + if (!sitdpll) + return ERR_PTR(-ENOMEM); + + sitdpll->dev = sitdev; + sitdpll->id = ch; + sitdpll->lock_status = DPLL_LOCK_STATUS_UNLOCKED; + sitdpll->lock_status_error = DPLL_LOCK_STATUS_ERROR_NONE; + INIT_LIST_HEAD(&sitdpll->pins); + + return sitdpll; +} + +void sit9531x_dpll_free(struct sit9531x_dpll *sitdpll) +{ + kfree(sitdpll); +} + +int sit9531x_dpll_register(struct sit9531x_dpll *sitdpll) { - unsigned int tmp; int rc; - mutex_lock(&sitdev->page_lock); - rc = sit9531x_read_locked(sitdev, reg, &tmp); - mutex_unlock(&sitdev->page_lock); + rc = sit9531x_dpll_device_register(sitdpll); + if (rc) + return rc; + + sit9531x_dpll_lock_status_baseline(sitdpll); + + rc = sit9531x_dpll_pins_register(sitdpll); if (rc) { - dev_err_ratelimited(sitdev->dev, - "Failed to read page 0x%02x reg 0x%02x: %d\n", - SIT9531X_REG_PAGE(reg), - SIT9531X_REG_OFFSET(reg), rc); + sit9531x_dpll_device_unregister(sitdpll); return rc; } - *val = (u8)tmp; return 0; } +void sit9531x_dpll_unregister(struct sit9531x_dpll *sitdpll) +{ + sit9531x_dpll_pins_unregister(sitdpll); + sit9531x_dpll_device_unregister(sitdpll); +} + +static void sit9531x_dpll_list_free(struct sit9531x_dev *sitdev) +{ + struct sit9531x_dpll *sitdpll, *next; + + list_for_each_entry_safe(sitdpll, next, &sitdev->dplls, list) { + list_del(&sitdpll->list); + sit9531x_dpll_free(sitdpll); + } +} + +/* Runs only once the device is fully started, see the caller. */ +static void sit9531x_dev_dpll_fini(void *ptr) +{ + struct sit9531x_dev *sitdev = ptr; + + sit9531x_dev_stop(sitdev); + kthread_destroy_worker(sitdev->kworker); + sit9531x_dpll_list_free(sitdev); +} + +static int sit9531x_devm_dpll_init(struct sit9531x_dev *sitdev) +{ + struct kthread_worker *kworker; + struct sit9531x_dpll *sitdpll; + unsigned int i; + int rc; + + INIT_LIST_HEAD(&sitdev->dplls); + kthread_init_delayed_work(&sitdev->work, sit9531x_dev_periodic_work); + + for (i = 0; i < SIT9531X_NUM_PLLS; i++) { + sitdpll = sit9531x_dpll_alloc(sitdev, i); + if (IS_ERR(sitdpll)) { + rc = dev_err_probe(sitdev->dev, PTR_ERR(sitdpll), + "Failed to alloc DPLL%u\n", i); + goto err_free_dplls; + } + + list_add_tail(&sitdpll->list, &sitdev->dplls); + } + + kworker = kthread_run_worker(0, "sit9531x-%s", dev_name(sitdev->dev)); + if (IS_ERR(kworker)) { + rc = PTR_ERR(kworker); + goto err_free_dplls; + } + sitdev->kworker = kworker; + + rc = sit9531x_dev_start(sitdev); + if (rc) { + rc = dev_err_probe(sitdev->dev, rc, "Failed to start device\n"); + goto err_destroy_worker; + } + + /* Added last: only now is every field the cleanup touches valid. */ + return devm_add_action_or_reset(sitdev->dev, sit9531x_dev_dpll_fini, + sitdev); + +err_destroy_worker: + kthread_destroy_worker(sitdev->kworker); +err_free_dplls: + sit9531x_dpll_list_free(sitdev); + + return rc; +} + +/* See SIT9531X_REG_VARIANT_ID: one byte, reg 0x03 is not part of it. */ static int sit9531x_read_variant_id(struct sit9531x_dev *sitdev, u8 *id) { return sit9531x_read_u8(sitdev, SIT9531X_REG_VARIANT_ID, id); @@ -123,6 +1374,132 @@ static const struct sit9531x_chip_info *sit9531x_match_variant(u8 id) return NULL; } +/* + * Build the clock id from the SiTime OUI, the I2C bus number and the I2C + * address. + */ +static int sit9531x_bus_clock_id(struct sit9531x_dev *sitdev) +{ + int adap = i2c_adapter_id(sitdev->client->adapter); + + /* + * The part answers on 7-bit addresses; a 10-bit one would overlap the + * bus field of the clock id. + */ + if (sitdev->client->flags & I2C_CLIENT_TEN) + return dev_err_probe(sitdev->dev, -EINVAL, + "10-bit I2C address 0x%03x is not supported\n", + sitdev->client->addr); + + /* + * The OUI with its EUI-64 filler takes the top 40 bits; the bus gets 16 + * bits. + */ + if (adap > U16_MAX) + return dev_err_probe(sitdev->dev, -EINVAL, + "I2C adapter number %d does not fit the clock id\n", + adap); + + sitdev->clock_id = SIT9531X_OUI << 24 | (u64)adap << 8 | + sitdev->client->addr; + + return 0; +} + +/* Clock ids of the bound parts; parts on one id would share DPLL devices */ +static LIST_HEAD(sit9531x_clock_ids); +static DEFINE_MUTEX(sit9531x_clock_ids_lock); + +static bool sit9531x_clock_id_used(u64 id) +{ + const struct sit9531x_dev *other; + + lockdep_assert_held(&sit9531x_clock_ids_lock); + + list_for_each_entry(other, &sit9531x_clock_ids, clock_id_node) + if (other->clock_id == id) + return true; + + return false; +} + +static void sit9531x_clock_id_put(void *data) +{ + struct sit9531x_dev *sitdev = data; + + mutex_lock(&sit9531x_clock_ids_lock); + list_del(&sitdev->clock_id_node); + mutex_unlock(&sit9531x_clock_ids_lock); +} + +/* + * Take the clock id for this part until unbind; an id another bound part + * holds gives the id from the I2C bus and address instead. + */ +static int sit9531x_clock_id_take(struct sit9531x_dev *sitdev) +{ + int rc = 0; + + mutex_lock(&sit9531x_clock_ids_lock); + if (sit9531x_clock_id_used(sitdev->clock_id)) { + dev_warn(sitdev->dev, + "clock id 0x%016llx is in use by another part, clock id from the I2C bus and address\n", + sitdev->clock_id); + rc = sit9531x_bus_clock_id(sitdev); + if (!rc && sit9531x_clock_id_used(sitdev->clock_id)) + rc = dev_err_probe(sitdev->dev, -EBUSY, + "clock id 0x%016llx is in use by another part\n", + sitdev->clock_id); + } + if (!rc) + list_add(&sitdev->clock_id_node, &sit9531x_clock_ids); + mutex_unlock(&sit9531x_clock_ids_lock); + if (rc) + return rc; + + return devm_add_action_or_reset(sitdev->dev, sit9531x_clock_id_put, + sitdev); +} + +/* + * Build the clock id from the die ID: its last 8 bytes, with the first byte + * XORed into the most significant one. + */ +static int sit9531x_derive_clock_id(struct sit9531x_dev *sitdev) +{ + u8 die[SIT9531X_DIE_ID_LEN]; + u64 id = 0; + int rc; + u8 i; + + for (i = 0; i < SIT9531X_DIE_ID_LEN; i++) { + rc = sit9531x_read_u8(sitdev, SIT9531X_REG_DIE_ID + i, &die[i]); + if (rc) + return dev_err_probe(sitdev->dev, rc, + "Failed to read the die ID\n"); + if (i) + id = id << 8 | die[i]; + } + id ^= (u64)die[0] << 56; + + /* Blank id bytes, or an id of zero or all ones */ + if (!id || id == U64_MAX || !memchr_inv(&die[1], 0, sizeof(die) - 1) || + !memchr_inv(&die[1], 0xff, sizeof(die) - 1)) { + dev_info(sitdev->dev, + "die ID %*phN not usable, clock id from the I2C bus and address\n", + (int)sizeof(die), die); + rc = sit9531x_bus_clock_id(sitdev); + if (rc) + return rc; + } else { + dev_dbg(sitdev->dev, "die ID %*phN, clock id 0x%016llx\n", + (int)sizeof(die), die, id); + sitdev->clock_id = id; + } + + return sit9531x_clock_id_take(sitdev); +} + static const char * const sit9531x_core_supplies[] = { "vdd", "vddin" }; static const char * const sit95316_out_supplies[] = { @@ -230,6 +1607,10 @@ static int sit9531x_dev_probe(struct sit9531x_dev *sitdev) "Unknown variant ID: 0x%02x\n", variant_id); + rc = sit9531x_derive_clock_id(sitdev); + if (rc) + return rc; + rc = devm_mutex_init(sitdev->dev, &sitdev->multiop_lock); if (rc) return dev_err_probe(sitdev->dev, rc, @@ -239,6 +1620,27 @@ static int sit9531x_dev_probe(struct sit9531x_dev *sitdev) sitdev->info->name, sitdev->info->num_inputs, sitdev->info->num_outputs); + /* Before the IRQ: the handler queues work on sitdev->kworker. */ + rc = sit9531x_devm_dpll_init(sitdev); + if (rc) + return rc; + + /* + * Without an "interrupts" property client->irq is 0 and only the poll + * runs. + */ + sitdev->irq = sitdev->client->irq; + if (sitdev->irq > 0) { + rc = devm_request_threaded_irq(sitdev->dev, sitdev->irq, + NULL, sit9531x_irq_thread_fn, + IRQF_ONESHOT, + dev_name(sitdev->dev), sitdev); + if (rc) + return dev_err_probe(sitdev->dev, rc, + "Failed to request IRQ %d\n", + sitdev->irq); + } + return 0; } @@ -276,10 +1678,108 @@ static const struct of_device_id sit9531x_of_match[] = { }; MODULE_DEVICE_TABLE(of, sit9531x_of_match); +/* + * Stop the poll and the IRQ across suspend so no paged I2C sequence reaches a + * suspended adapter; the poll worker is not freezable. + */ +static int sit9531x_suspend(struct device *dev) +{ + struct sit9531x_dev *sitdev = dev_get_drvdata(dev); + + if (sitdev->irq > 0) + disable_irq(sitdev->irq); + kthread_cancel_delayed_work_sync(&sitdev->work); + + return 0; +} + +static int sit9531x_resume(struct device *dev) +{ + struct sit9531x_dev *sitdev = dev_get_drvdata(dev); + struct sit9531x_dpll *sitdpll; + u8 modes_moved = 0; + int rc; + u8 i; + + /* + * The part may have lost power and reloaded its profile: wait for it as + * probe does. It then starts on page 0, not the cached page. + */ + fsleep(SIT9531X_START_US); + sit9531x_page_cache_drop(sitdev); + + mutex_lock(&sitdev->multiop_lock); + /* Read again the configuration probe read */ + for (i = 0; i < sitdev->info->num_inputs / 2; i++) { + enum sit9531x_signal_mode mode = sitdev->ref[i * 2].sig_mode; + + rc = sit9531x_input_mode_fetch(sitdev, i); + if (rc) + dev_warn(sitdev->dev, + "CLKIN%u: mode not read on resume: %d\n", + i, rc); + else if (sitdev->ref[i * 2].sig_mode != mode) + dev_warn(sitdev->dev, + "CLKIN%u: signal mode changed over suspend\n", + i); + } + for (i = 0; i < SIT9531X_NUM_PLLS; i++) { + struct sit9531x_chan *chan = &sitdev->chan[i]; + bool manual = chan->manual_input; + + rc = sit9531x_chan_select_fetch(sitdev, i); + if (!rc && chan->manual_input != manual) { + dev_warn(sitdev->dev, + "PLL%c: select mode changed over suspend\n", + 'A' + i); + modes_moved |= BIT(i); + } + if (!rc) + rc = sit9531x_chan_state_fetch(sitdev, i); + if (rc) { + dev_warn(sitdev->dev, + "PLL%c: state not read on resume: %d\n", + 'A' + i, rc); + chan->stale = true; + } + } + for (i = 0; i < sitdev->info->num_outputs; i++) { + struct sit9531x_out *out = &sitdev->out[i]; + bool routed = out->routed; + u8 pll_idx = out->pll_idx; + + rc = sit9531x_out_state_fetch(sitdev, i); + if (rc) + dev_warn(sitdev->dev, + "out%u: state not read on resume: %d\n", + i, rc); + if (out->routed != routed || out->pll_idx != pll_idx) + dev_warn(sitdev->dev, + "out%u: routing changed over suspend\n", + i); + } + mutex_unlock(&sitdev->multiop_lock); + + /* The poll notifies a device whose mode changed */ + list_for_each_entry(sitdpll, &sitdev->dplls, list) + if (modes_moved & BIT(sitdpll->id)) + sitdpll->ntf_pending = true; + + if (sitdev->irq > 0) + enable_irq(sitdev->irq); + kthread_queue_delayed_work(sitdev->kworker, &sitdev->work, 0); + + return 0; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(sit9531x_pm_ops, + sit9531x_suspend, sit9531x_resume); + static struct i2c_driver sit9531x_i2c_driver = { .driver = { .name = "sit9531x", .of_match_table = sit9531x_of_match, + .pm = pm_sleep_ptr(&sit9531x_pm_ops), }, .probe = sit9531x_i2c_probe, }; diff --git a/drivers/dpll/sit9531x/core.h b/drivers/dpll/sit9531x/core.h index 0b2dd3074122..f9bc9cce4ee7 100644 --- a/drivers/dpll/sit9531x/core.h +++ b/drivers/dpll/sit9531x/core.h @@ -15,6 +15,8 @@ #include <linux/gpio/consumer.h> #include <linux/i2c.h> +#include <linux/kthread.h> +#include <linux/list.h> #include <linux/mutex.h> #include <linux/regmap.h> #include <linux/types.h> @@ -23,6 +25,7 @@ #define SIT9531X_NUM_PLLS 4 #define SIT9531X_MAX_INPUTS 8 +#define SIT9531X_NUM_INPUT_PAIRS (SIT9531X_MAX_INPUTS / 2) #define SIT9531X_MAX_OUTPUTS 12 /* * Output rates of the datasheet, Table 6: continuous up to 800 MHz, above it @@ -37,9 +40,18 @@ */ #define SIT9531X_INTSYNC_PIN_ID (SIT9531X_MAX_INPUTS + 1) #define SIT9531X_INTSYNC_OUT_PIN_ID SIT9531X_MAX_OUTPUTS +#define SIT9531X_STATUS_POLL_MS 500 /* TSTART: no I2C access is valid until 10 ms after the supplies are up */ #define SIT9531X_START_US 10000 +/* selected_ref value when the active source is not a registered input */ +#define SIT9531X_REF_INVALID 0xFF + +/* SiTime IEEE OUI for EUI-64 generation */ +#define SIT9531X_OUI 0x0090C2FFFEULL + +struct sit9531x_dpll; + /* * struct sit9531x_chip_info - chip variant identification * @id: variant ID byte read from the device @@ -69,26 +81,92 @@ enum sit9531x_signal_mode { /* * struct sit9531x_ref - input reference state * @freq: configured frequency in Hz - * @sig_mode: signal mode of the input pair + * @enabled: input receiver is on + * @pll_mask: PLLs this input feeds (bit 0 = PLLA) + * @sig_mode: signal mode of the input pair, read from CLKINx_INPUT_MODE at + * probe and on resume */ struct sit9531x_ref { u64 freq; + bool enabled; + u8 pll_mask; enum sit9531x_signal_mode sig_mode; }; +/* + * struct sit9531x_out - output state + * @freq: output frequency in Hz + * @enabled: output is driving, not forced into Hi-Z + * @cmos: single-ended; the CMOS pad Hi-Z pairs apply, not the differential one + * @part_muted: single-ended with one pad forced and the other not; reported + * as driven + * @routed: mapped to @pll_idx by the loaded configuration; an output + * unrouted at probe has no pin + * @pll_idx: PLL driving this output (0-3) + */ +struct sit9531x_out { + u64 freq; + bool enabled; + bool cmos; + bool part_muted; + bool routed; + u8 pll_idx; +}; + +/* + * struct sit9531x_chan - per-PLL channel state + * @active: PLL reached its active state; an unused PLL never does and its + * loss-of-lock bit stays clear + * @locked: PLL is active and its outer loop reports lock + * @stale: last state read failed; the fields hold the previous sample and + * are not reported + * @mode: 0 = sync (outer loop enabled), 1 = free-run + * @selected_ref: logical index of the selected reference (INTSYNC: + * SIT9531X_INTSYNC_PIN_ID), or SIT9531X_REF_INVALID for a reserved code + * @inner_lol: inner loop loss of lock + * @ho_freeze: holdover freeze active + * @ho_valid: holdover memory holds a valid estimate + * @manual_input: PLL uses its manual input select (pins or + * GPIO_INPUT_FUNC_CTRL5..8), which table writes do not change; reported + * as MANUAL + * @prio_mask: bit per canonical source in the priority table read back + * from hardware + */ +struct sit9531x_chan { + bool active; + bool locked; + bool stale; + u8 mode; + u8 selected_ref; + bool inner_lol; + bool ho_freeze; + bool ho_valid; + bool manual_input; + u16 prio_mask; +}; + /* * struct sit9531x_dev - SiT9531x device instance + * @info: chip variant info * @dev: parent device * @client: I2C client backing @regmap * @regmap: the 256-byte register window - * @info: chip variant info + * @dplls: registered DPLL devices * @multiop_lock: serializes multi-register operations * @page_lock: held across a page select and its transfer; nests inside * @multiop_lock * @page: current page, -1 when unknown * @ref: input reference states + * @out: output states + * @chan: per-PLL channel states * @xtal_freq: crystal frequency in Hz + * @kworker: poll worker + * @work: poll work + * @clock_id: clock identifier, from the die ID or the I2C bus and address + * @clock_id_node: entry in the list of the clock ids of the bound parts * @reset_gpio: optional reset line, NULL if absent + * @irq: optional INTRB IRQ, 0 if not wired + * @irq_ack_fails: consecutive failed INTRB acknowledgements */ struct sit9531x_dev { struct device *dev; @@ -101,12 +179,36 @@ struct sit9531x_dev { struct mutex page_lock; int page; + u8 irq_ack_fails; struct sit9531x_ref ref[SIT9531X_MAX_INPUTS + 1]; /* +1 for xtal */ + struct sit9531x_out out[SIT9531X_MAX_OUTPUTS]; + struct sit9531x_chan chan[SIT9531X_NUM_PLLS]; u32 xtal_freq; + struct list_head dplls; + + struct kthread_worker *kworker; + struct kthread_delayed_work work; + + u64 clock_id; + struct list_head clock_id_node; + struct gpio_desc *reset_gpio; + int irq; }; +int sit9531x_read_u8(struct sit9531x_dev *sitdev, unsigned int reg, + u8 *val); +int sit9531x_write_u8(struct sit9531x_dev *sitdev, unsigned int reg, + u8 val); +int sit9531x_read_pll_u8(struct sit9531x_dev *sitdev, u8 pll_idx, + u8 offset, u8 *val); + +static inline u8 sit9531x_pll_page(u8 pll_idx) +{ + return SIT9531X_PAGE_PLLA + pll_idx; +} + /* * Input index 2n is the P lane and 2n + 1 the N lane of pair n; index * SIT9531X_MAX_INPUTS is the XO. @@ -122,6 +224,35 @@ static inline bool sit9531x_input_is_n(u8 index) return index & 1; } -int sit9531x_read_u8(struct sit9531x_dev *sitdev, unsigned int reg, u8 *val); +static inline u8 sit9531x_hw_src_input(u8 src) +{ + if (src < SIT9531X_NUM_INPUT_PAIRS) + return src * 2; + if (src == SIT9531X_PRIO_SRC_OCXO) + return SIT9531X_MAX_INPUTS; + if (src == SIT9531X_PRIO_SRC_INTSYNC) + return SIT9531X_INTSYNC_PIN_ID; + if (src >= SIT9531X_PRIO_SRC_N_BASE && + src < SIT9531X_PRIO_SRC_N_BASE + SIT9531X_NUM_INPUT_PAIRS) + return (src - SIT9531X_PRIO_SRC_N_BASE) * 2 + 1; + return SIT9531X_REF_INVALID; +} + +static inline bool sit9531x_prio_src_usable(u8 src) +{ + return sit9531x_hw_src_input(src) != SIT9531X_REF_INVALID; +} + +static inline const struct sit9531x_out * +sit9531x_out_state_get(const struct sit9531x_dev *sitdev, u8 index) +{ + return &sitdev->out[index]; +} + +static inline const struct sit9531x_chan * +sit9531x_chan_state_get(const struct sit9531x_dev *sitdev, u8 pll_idx) +{ + return &sitdev->chan[pll_idx]; +} #endif /* _SIT9531X_CORE_H */ diff --git a/drivers/dpll/sit9531x/dpll.c b/drivers/dpll/sit9531x/dpll.c new file mode 100644 index 000000000000..e9952e578fac --- /dev/null +++ b/drivers/dpll/sit9531x/dpll.c @@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SiTime SiT9531x DPLL subsystem callbacks and registration + * + * Copyright (C) 2026 SiTime Corp. + * Author: Ali Rouhi <arou…@sitime.com> + * Author: Oleg Zadorozhnyi <Oleg…@devoxsoftware.com> + * + * DPLL device ops, pin ops (separate input/output) and periodic change + * detection. + */ + +#include <linux/dpll.h> +#include <linux/err.h> +#include <linux/kthread.h> +#include <linux/list.h> +#include <linux/netlink.h> +#include <linux/slab.h> + +#include "core.h" +#include "dpll.h" +#include "prop.h" +#include "regs.h" + +static bool sit9531x_dpll_is_input_pin(const struct sit9531x_dpll_pin *pin) +{ + return pin->dir == DPLL_PIN_DIRECTION_INPUT; +} + +static bool +sit9531x_dpll_is_xo_pin(const struct sit9531x_dpll_pin *pin) +{ + return sit9531x_dpll_is_input_pin(pin) && + pin->id == SIT9531X_MAX_INPUTS; +} + +/* + * Report the lock status from the last poll: outer LOL (page 0 reg 0x06), PLL + * mode (PLL page reg 0x31), inner LOL (reg 0x92), holdover freeze (reg 0x0A) + * and holdover-valid (PLL page reg 0x06). + */ +static int +sit9531x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv, + enum dpll_lock_status *status, + enum dpll_lock_status_error *status_error, + struct netlink_ext_ack *extack) +{ + struct sit9531x_dpll *sitdpll = dpll_priv; + struct sit9531x_dev *sitdev = sitdpll->dev; + const struct sit9531x_chan *chan; + + if (status_error) + *status_error = DPLL_LOCK_STATUS_ERROR_NONE; + + chan = sit9531x_chan_state_get(sitdev, sitdpll->id); + + mutex_lock(&sitdev->multiop_lock); + + if (chan->stale) { + *status = DPLL_LOCK_STATUS_UNLOCKED; + if (status_error) + *status_error = DPLL_LOCK_STATUS_ERROR_UNDEFINED; + } else if (!chan->active) { + /* + * An unused PLL never goes active and its loss-of-lock bit is + * not driven. + */ + *status = DPLL_LOCK_STATUS_UNLOCKED; + } else if (chan->inner_lol) { + /* + * The core reports the error detail only with an unlocked or + * holdover status. + */ + *status = chan->ho_freeze ? DPLL_LOCK_STATUS_HOLDOVER : + DPLL_LOCK_STATUS_UNLOCKED; + } else if (chan->mode) { + /* + * In free-run the outer loop is disabled and its loss-of-lock + * bit is meaningless. + */ + *status = DPLL_LOCK_STATUS_UNLOCKED; + } else if (chan->ho_freeze) { + /* + * Test freeze before lock: a frozen PLL tracks no reference + * whatever its outer loss-of-lock bit reads. + */ + *status = DPLL_LOCK_STATUS_HOLDOVER; + } else if (chan->locked) { + if (chan->ho_valid) + *status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ; + else + *status = DPLL_LOCK_STATUS_LOCKED; + } else { + *status = DPLL_LOCK_STATUS_UNLOCKED; + } + + /* An unused PLL does not drive its inner loss-of-lock bit either. */ + if (status_error && chan->active && chan->inner_lol) + *status_error = DPLL_LOCK_STATUS_ERROR_UNDEFINED; + + mutex_unlock(&sitdev->multiop_lock); + + return 0; +} + +/* + * Record the lock status the create notification carried as the poll baseline. + * Call once the device is registered. + */ +void sit9531x_dpll_lock_status_baseline(struct sit9531x_dpll *sitdpll) +{ + sit9531x_dpll_lock_status_get(sitdpll->dpll_dev, sitdpll, + &sitdpll->lock_status, + &sitdpll->lock_status_error, NULL); +} + +/* + * Report AUTOMATIC for a PLL on its priority table, also on manual active + * select, which uses the same nibble, and MANUAL on manual input select + * (MISCINNER_PLL bit 5), where the select pins or GPIO_INPUT_FUNC_CTRL5..8 + * choose the input. Free-run (PLL page reg 0x31[5]) shows in the lock status. + */ +static int +sit9531x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv, + enum dpll_mode *mode, struct netlink_ext_ack *extack) +{ + struct sit9531x_dpll *sitdpll = dpll_priv; + + /* Written at probe and on resume, when no request runs */ + if (sit9531x_chan_state_get(sitdpll->dev, sitdpll->id)->manual_input) + *mode = DPLL_MODE_MANUAL; + else + *mode = DPLL_MODE_AUTOMATIC; + + return 0; +} + +static int +sit9531x_dpll_supported_modes_get(const struct dpll_device *dpll, + void *dpll_priv, unsigned long *modes, + struct netlink_ext_ack *extack) +{ + struct sit9531x_dpll *sitdpll = dpll_priv; + + if (sit9531x_chan_state_get(sitdpll->dev, sitdpll->id)->manual_input) + __set_bit(DPLL_MODE_MANUAL, modes); + else + __set_bit(DPLL_MODE_AUTOMATIC, modes); + + return 0; +} + +const struct dpll_device_ops sit9531x_dpll_device_ops = { + .lock_status_get = sit9531x_dpll_lock_status_get, + .mode_get = sit9531x_dpll_mode_get, + .supported_modes_get = sit9531x_dpll_supported_modes_get, +}; + +static int +sit9531x_dpll_input_pin_direction_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_direction *direction, + struct netlink_ext_ack *extack) +{ + *direction = DPLL_PIN_DIRECTION_INPUT; + return 0; +} + +static const struct dpll_pin_ops sit9531x_dpll_input_pin_ops = { + .direction_get = sit9531x_dpll_input_pin_direction_get, +}; + +/* The XO feeds every PLL and is always connected. */ + +static int +sit9531x_dpll_xo_pin_state_on_dpll_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_state *state, + struct netlink_ext_ack *extack) +{ + *state = DPLL_PIN_STATE_CONNECTED; + return 0; +} + +static const struct dpll_pin_ops sit9531x_dpll_xo_pin_ops = { + .direction_get = sit9531x_dpll_input_pin_direction_get, + .state_on_dpll_get = sit9531x_dpll_xo_pin_state_on_dpll_get, +}; + +static int +sit9531x_dpll_output_pin_direction_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_direction *direction, + struct netlink_ext_ack *extack) +{ + *direction = DPLL_PIN_DIRECTION_OUTPUT; + return 0; +} + +static const struct dpll_pin_ops sit9531x_dpll_output_pin_ops = { + .direction_get = sit9531x_dpll_output_pin_direction_get, +}; + +const struct dpll_pin_ops * +sit9531x_dpll_pin_ops_get(const struct sit9531x_dpll_pin *pin) +{ + if (!sit9531x_dpll_is_input_pin(pin)) + return &sit9531x_dpll_output_pin_ops; + if (sit9531x_dpll_is_xo_pin(pin)) + return &sit9531x_dpll_xo_pin_ops; + return &sit9531x_dpll_input_pin_ops; +} + +/* + * Send notifications for changed lock status and pin state; called from + * sit9531x_dev_periodic_work(). + */ +void sit9531x_dpll_changes_check(struct sit9531x_dpll *sitdpll) +{ + enum dpll_lock_status_error status_error = DPLL_LOCK_STATUS_ERROR_NONE; + struct sit9531x_dev *sitdev = sitdpll->dev; + enum dpll_lock_status lock_status; + struct sit9531x_dpll_pin *pin; + int rc; + + rc = sit9531x_dpll_lock_status_get(sitdpll->dpll_dev, sitdpll, + &lock_status, &status_error, NULL); + if (rc) { + dev_err(sitdev->dev, "Failed to get DPLL%u lock status: %d\n", + sitdpll->id, rc); + return; + } + + /* + * The core publishes the error detail with the status, so a change in + * either is notified. + */ + if (sitdpll->lock_status != lock_status || + sitdpll->lock_status_error != status_error) { + sitdpll->lock_status = lock_status; + sitdpll->lock_status_error = status_error; + sitdpll->ntf_pending = true; + } + + if (sitdpll->ntf_pending && !dpll_device_change_ntf(sitdpll->dpll_dev)) + sitdpll->ntf_pending = false; + + list_for_each_entry(pin, &sitdpll->pins, list) { + const struct dpll_pin_ops *ops; + enum dpll_pin_state state; + bool changed; + + /* Only references change state without a request */ + if (!sit9531x_dpll_is_input_pin(pin) || + sit9531x_dpll_is_xo_pin(pin)) + continue; + + ops = sit9531x_dpll_pin_ops_get(pin); + rc = ops->state_on_dpll_get(pin->dpll_pin, pin, + sitdpll->dpll_dev, sitdpll, + &state, NULL); + if (rc) + continue; + + changed = pin->seen && state != pin->pin_state; + pin->pin_state = state; + pin->seen = true; + if (changed) { + dev_dbg(sitdev->dev, "%s state changed\n", pin->label); + dpll_pin_change_ntf(pin->dpll_pin); + } + } +} diff --git a/drivers/dpll/sit9531x/dpll.h b/drivers/dpll/sit9531x/dpll.h new file mode 100644 index 000000000000..92d434200333 --- /dev/null +++ b/drivers/dpll/sit9531x/dpll.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * SiTime SiT9531x DPLL subsystem interface + * + * Copyright (C) 2026 SiTime Corp. + * Author: Ali Rouhi <arou…@sitime.com> + * Author: Oleg Zadorozhnyi <Oleg…@devoxsoftware.com> + * + * DPLL device and pin structures, and function declarations for + * the DPLL registration and callback layer. + */ + +#ifndef _SIT9531X_DPLL_H +#define _SIT9531X_DPLL_H + +#include <linux/dpll.h> +#include <linux/list.h> +#include <linux/types.h> + +struct sit9531x_dev; + +struct sit9531x_dpll_pin { + struct list_head list; + struct sit9531x_dpll *dpll; + struct dpll_pin *dpll_pin; + dpll_tracker tracker; + struct fwnode_handle *fwnode; + char label[8]; /* "IN0", "OUT3" */ + enum dpll_pin_direction dir; + u8 id; /* hardware index */ + enum dpll_pin_state pin_state; + bool seen; /* baseline taken by the poll */ +}; + +struct sit9531x_dpll { + struct list_head list; + struct sit9531x_dev *dev; + struct dpll_device *dpll_dev; + dpll_tracker tracker; + struct dpll_device_ops ops; /* per-instance copy */ + struct list_head pins; + u8 id; /* 0 = PLLA .. 3 = PLLD */ + enum dpll_lock_status lock_status; + enum dpll_lock_status_error lock_status_error; + bool ntf_pending; +}; + +extern const struct dpll_device_ops sit9531x_dpll_device_ops; +const struct dpll_pin_ops * +sit9531x_dpll_pin_ops_get(const struct sit9531x_dpll_pin *pin); + +struct sit9531x_dpll *sit9531x_dpll_alloc(struct sit9531x_dev *sitdev, u8 ch); +void sit9531x_dpll_free(struct sit9531x_dpll *sitdpll); +int sit9531x_dpll_register(struct sit9531x_dpll *sitdpll); +void sit9531x_dpll_unregister(struct sit9531x_dpll *sitdpll); + +void sit9531x_dpll_lock_status_baseline(struct sit9531x_dpll *sitdpll); +void sit9531x_dpll_changes_check(struct sit9531x_dpll *sitdpll); + +#endif /* _SIT9531X_DPLL_H */ diff --git a/drivers/dpll/sit9531x/regs.h b/drivers/dpll/sit9531x/regs.h index ff2ba0aeda82..52515e7e591c 100644 --- a/drivers/dpll/sit9531x/regs.h +++ b/drivers/dpll/sit9531x/regs.h @@ -20,9 +20,182 @@ #define SIT9531X_REG_PAGE(_reg) ((_reg) >> 8) #define SIT9531X_REG_OFFSET(_reg) ((_reg) & 0xFF) +#define SIT9531X_PAGE_OUTSYS0 0x03 +#define SIT9531X_PAGE_OUTSYS1 0x04 +#define SIT9531X_PAGE_PLLA 0x0A + /* Reg 0x03 holds an unrelated revision byte, not part of the variant ID */ #define SIT9531X_REG_VARIANT_ID SIT9531X_REG(0x00, 0x02) +/* Die ID: 9 bytes, most significant first, read one byte at a time */ +#define SIT9531X_REG_DIE_ID SIT9531X_REG(0x00, 0x41) +#define SIT9531X_DIE_ID_LEN 9 + +/* PLL inner loop loss-of-lock */ +#define SIT9531X_REG_PLL_INNER_LOL_STATUS SIT9531X_REG(0x00, 0x92) +#define SIT9531X_REG_PLL_INNER_LOL_NOTIF SIT9531X_REG(0x00, 0x93) + +/* Clock monitor PLL / XO status */ +#define SIT9531X_REG_CMON_NOTIF SIT9531X_REG(0x00, 0x9E) + +/* PLL outer-loop loss-of-lock */ +#define SIT9531X_REG_OUTER_LOL_STATUS SIT9531X_REG(0x00, 0x06) +#define SIT9531X_REG_OUTER_LOL_NOTIF SIT9531X_REG(0x00, 0x07) + +/* PLL holdover freeze status */ +#define SIT9531X_REG_HO_FREEZE_STATUS SIT9531X_REG(0x00, 0x0A) +#define SIT9531X_REG_HO_FREEZE_NOTIF SIT9531X_REG(0x00, 0x0B) + +/* + * Priority table: six registers per PLL from 0x16 + 6 * PLL, holding slots + * 0-10 and the active-reference nibble; slot 2n in [7:4], 2n+1 in [3:0]. + * + * Source encoding (4-bit): + * 0-3 = IN0P-IN3P, 5 = OCXO, 6 = INTSYNC, 7-10 = IN0N-IN3N + * + * Codes 4 and 11 take the place of a fifth input pair; they read back as no + * valid reference. + */ +#define SIT9531X_PAGE_PRIOSYS 0x01 +#define SIT9531X_PRIO_BASE_REG 0x16 +#define SIT9531X_PRIO_REGS_PER_PLL 6 +#define SIT9531X_PRIO_SLOTS_PER_REG 2 +/* + * CLK_SPARE0SEL_PLL..CLK_SPARE10SEL_PLL; the twelfth nibble is + * CLK_ACTIVESEL_PLL. + */ +#define SIT9531X_PRIO_MAX_SLOTS 11 +#define SIT9531X_PRIO_NIBBLE_MASK 0x0F +#define SIT9531X_PRIO_HI_SHIFT 4 +#define SIT9531X_PRIO_SRC_OCXO 5 +#define SIT9531X_PRIO_SRC_INTSYNC 6 +#define SIT9531X_PRIO_SRC_N_BASE 7 +/* + * Low nibble of the last priority register: CLK_ACTIVESEL_PLL, the PLL's active + * reference, in the same encoding. + */ +#define SIT9531X_PRIO_ACTIVESEL_OFF 5 + +/* One bit per input PAIR (bit 0 = CLKIN0, ..., bit 3 = CLKIN3) */ +#define SIT9531X_REG_IN_DE_FORCE SIT9531X_REG(0x02, 0xE8) +#define SIT9531X_REG_IN_DE_STATE SIT9531X_REG(0x02, 0xE9) +#define SIT9531X_REG_IN_SEP_FORCE SIT9531X_REG(0x02, 0xEA) +#define SIT9531X_REG_IN_SEP_STATE SIT9531X_REG(0x02, 0xEB) +#define SIT9531X_REG_IN_SEN_FORCE SIT9531X_REG(0x02, 0xF2) +#define SIT9531X_REG_IN_SEN_STATE SIT9531X_REG(0x02, 0xF3) + +/* + * SE_P_EN/SE_N_EN set: that lane is single-ended; both clear: the pair is + * differential. + */ +#define SIT9531X_REG_IN_MODE(_pair) \ + SIT9531X_REG(0x02, 0x1B + 0x10 * (_pair)) +#define SIT9531X_IN_MODE_SE_P_EN BIT(0) +#define SIT9531X_IN_MODE_SE_N_EN BIT(1) + +#define SIT9531X_REG_HIZ_DIFF_07_MASK SIT9531X_REG(0x03, 0xF2) +#define SIT9531X_REG_HIZ_DIFF_07_STATE SIT9531X_REG(0x03, 0xF3) +#define SIT9531X_REG_HIZ_DIFF_811_MASK SIT9531X_REG(0x03, 0xF4) +#define SIT9531X_REG_HIZ_DIFF_811_STATE SIT9531X_REG(0x03, 0xF5) +#define SIT9531X_REG_HIZ_SE_07_MASK SIT9531X_REG(0x03, 0xF8) +#define SIT9531X_REG_HIZ_SE_07_STATE SIT9531X_REG(0x03, 0xF9) +#define SIT9531X_REG_HIZ_SE_811_MASK SIT9531X_REG(0x03, 0xFA) +#define SIT9531X_REG_HIZ_SE_811_STATE SIT9531X_REG(0x03, 0xFB) +/* + * The SE pairs force the OutP pad of a CMOS output; the OutN pad uses 0xF6/0xF7 + * for slots 0-7 and 0xF4/0xF5 bits 7:4 for slots 8-11. + */ +#define SIT9531X_REG_HIZ_SEN_07_MASK SIT9531X_REG(0x03, 0xF6) +#define SIT9531X_REG_HIZ_SEN_07_STATE SIT9531X_REG(0x03, 0xF7) +#define SIT9531X_HIZ_SEN_811_SHIFT 4 + +/* + * ODRn_MISC0: bit 2 enables CMOS on the OutP pad, bit 3 on the OutN pad. + * Either set: single-ended (one pad, or both as CMOS); neither: differential. + */ +#define SIT9531X_OUT_MISC0_BASE 0x1E +#define SIT9531X_OUT_MISC0_STRIDE 0x10 +#define SIT9531X_OUT_CMOS_ENP BIT(2) +#define SIT9531X_OUT_CMOS_ENN BIT(3) + +/* + * Output divider: 34 bits in 5 bytes, LSB at the slot base, MSB in base-4 + * [1:0]. Slots 0-5 on page 3, 6-11 on page 4; clkout_map[] gives the slot. + */ +#define SIT9531X_PAGE_OUTSYS0_SLOT_MAX 5 /* slots 0-5 on Page 0x03 */ + +/* + * PLL page regs 0x10-0x15 hold the loop-filter coefficients (3 normal, 3 + * fast-lock); they are GUI generated and must not be changed at runtime. + */ + +/* + * Output-enable mask, twelve bits: bits 0-7 in LO, bits 8-11 in HI[3:0]. + * OUTn is bit n on PLLA and PLLB and bit 11 - n on PLLC and PLLD. + */ +#define SIT9531X_PLL_REG_OUT_MAP_HI 0x27 +#define SIT9531X_PLL_REG_OUT_MAP_LO 0x28 +#define SIT9531X_PLL_REG_STATUS 0x31 + +#define SIT9531X_PLL_REG_ACTIVE 0x02 +#define SIT9531X_PLL_ACTIVE_BIT BIT(0) /* PLL reached active state */ + +#define SIT9531X_PLL_STATUS_OUTER_DIS BIT(5) + +/* + * PLL_CONFIG1F_PLL bit 6: manual active select, no priority arbitration. The + * register is GUI generated; the driver only reads it. + */ +#define SIT9531X_PLL_REG_CONFIG1F 0x1F +#define SIT9531X_PLL_CONFIG1F_MANUAL_SEL BIT(6) + +/* + * MISCINNER_PLL bit 5: in manual active select, use the manual input select. + * The register is GUI generated; the driver only reads it. + */ +#define SIT9531X_PLL_REG_MISCINNER 0x18 +#define SIT9531X_PLL_MISCINNER_MAN_IN_SEL BIT(5) + +/* + * GPIO_INPUT_FUNC_CTRL5..8, one per PLL: with bit 4 set the manual input is the + * low nibble, otherwise the input-select pins. Bit 6 enables the indirect + * choice and bit 5 picks active or spare; the driver does not decode them. + */ +#define SIT9531X_REG_MAN_IN_SEL(_pll) SIT9531X_REG(0x00, 0xE8 + (_pll)) +#define SIT9531X_MAN_IN_SEL_FROM_REG BIT(4) +#define SIT9531X_MAN_IN_SEL_MASK GENMASK(3, 0) + +/* + * HO_VALID: holdover memory has been acquired. HO_FREEZE (page 0, reg 0x0A) + * means the PLL is in holdover. + */ +#define SIT9531X_PLL_REG_STATUS_1 0x06 +#define SIT9531X_PLL_STATUS_1_HO_VALID BIT(2) + +#define SIT9531X_CLKMON_P_NOTIF_01 SIT9531X_REG(0x06, 0x03) +#define SIT9531X_CLKMON_P_NOTIF_23 SIT9531X_REG(0x06, 0x07) + +#define SIT9531X_CLKMON_N_NOTIF_01 SIT9531X_REG(0x06, 0x93) +#define SIT9531X_CLKMON_N_NOTIF_23 SIT9531X_REG(0x06, 0x97) + +/* + * The device compares the CRC stored in the EEPROM with the one it computes. + * NOTIFY_4 bits are sticky; a good load leaves only the read-done bit set. + */ +/* 4 bytes each, MSB first */ +#define SIT9531X_REG_REC_CRC SIT9531X_REG(0x00, 0x8A) +#define SIT9531X_REG_CAL_CRC SIT9531X_REG(0x00, 0x8E) +#define SIT9531X_REG_EEPROM_NOTIF SIT9531X_REG(0x00, 0x97) +#define SIT9531X_EEPROM_READ_DONE BIT(0) +/* NOTIFY_5: EEPROM start, stop, data and timeout defects, sticky */ +#define SIT9531X_REG_EEPROM_BUS_NOTIF SIT9531X_REG(0x00, 0x9B) + +/* Profile identifier of the loaded configuration, page 1: 3 bytes, LSB first */ +#define SIT9531X_REG_PROFILE_ID SIT9531X_REG(0x01, 0x44) + +/* Consecutive failed INTRB acknowledgements before the IRQ is disabled */ +#define SIT9531X_IRQ_ACK_TRIES 8 + #define SIT9531X_VARIANT_ID_95317 0x17 #define SIT9531X_VARIANT_ID_95316 0x31
-- 2.39.2 (Apple Git-143)