1 // SPDX-License-Identifier: GPL-2.0+
3 * PTP hardware clock driver for the IDT ClockMatrix(TM) family of timing and
4 * synchronization devices.
6 * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
8 #include <linux/firmware.h>
10 #include <linux/module.h>
11 #include <linux/ptp_clock_kernel.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/timekeeping.h>
16 #include "ptp_private.h"
17 #include "ptp_clockmatrix.h"
19 MODULE_DESCRIPTION("Driver for IDT ClockMatrix(TM) family");
22 MODULE_VERSION("1.0");
23 MODULE_LICENSE("GPL");
25 #define SETTIME_CORRECTION (0)
27 static int char_array_to_timespec(u8 *buf,
29 struct timespec64 *ts)
35 if (count < TOD_BYTE_COUNT)
38 /* Sub-nanoseconds are in buf[0]. */
40 for (i = 0; i < 3; i++) {
46 for (i = 0; i < 5; i++) {
57 static int timespec_to_char_array(struct timespec64 const *ts,
65 if (count < TOD_BYTE_COUNT)
71 /* Sub-nanoseconds are in buf[0]. */
73 for (i = 1; i < 5; i++) {
78 for (i = 5; i < TOD_BYTE_COUNT; i++) {
87 static int idtcm_xfer(struct idtcm *idtcm,
93 struct i2c_client *client = idtcm->client;
94 struct i2c_msg msg[2];
97 msg[0].addr = client->addr;
100 msg[0].buf = ®addr;
102 msg[1].addr = client->addr;
103 msg[1].flags = write ? 0 : I2C_M_RD;
107 cnt = i2c_transfer(client->adapter, msg, 2);
110 dev_err(&client->dev, "i2c_transfer returned %d\n", cnt);
112 } else if (cnt != 2) {
113 dev_err(&client->dev,
114 "i2c_transfer sent only %d of %d messages\n", cnt, 2);
121 static int idtcm_page_offset(struct idtcm *idtcm, u8 val)
126 if (idtcm->page_offset == val)
134 err = idtcm_xfer(idtcm, PAGE_ADDR, buf, sizeof(buf), 1);
137 dev_err(&idtcm->client->dev, "failed to set page offset\n");
139 idtcm->page_offset = val;
144 static int _idtcm_rdwr(struct idtcm *idtcm,
154 hi = (regaddr >> 8) & 0xff;
157 err = idtcm_page_offset(idtcm, hi);
162 err = idtcm_xfer(idtcm, lo, buf, count, write);
167 static int idtcm_read(struct idtcm *idtcm,
173 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, false);
176 static int idtcm_write(struct idtcm *idtcm,
182 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, true);
185 static int _idtcm_gettime(struct idtcm_channel *channel,
186 struct timespec64 *ts)
188 struct idtcm *idtcm = channel->idtcm;
189 u8 buf[TOD_BYTE_COUNT];
193 err = idtcm_read(idtcm, channel->tod_read_primary,
194 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
198 trigger &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
199 trigger |= (1 << TOD_READ_TRIGGER_SHIFT);
200 trigger |= TOD_READ_TRIGGER_MODE;
202 err = idtcm_write(idtcm, channel->tod_read_primary,
203 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
208 if (idtcm->calculate_overhead_flag)
209 idtcm->start_time = ktime_get_raw();
211 err = idtcm_read(idtcm, channel->tod_read_primary,
212 TOD_READ_PRIMARY, buf, sizeof(buf));
217 err = char_array_to_timespec(buf, sizeof(buf), ts);
222 static int _sync_pll_output(struct idtcm *idtcm,
233 if ((qn == 0) && (qn_plus_1 == 0))
238 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
239 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
242 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
243 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
246 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
247 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
250 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
251 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
254 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
255 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
258 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
259 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
262 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
263 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
266 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
267 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
273 val = SYNCTRL1_MASTER_SYNC_RST;
275 /* Place master sync in reset */
276 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
280 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
284 /* Set sync trigger mask */
285 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
288 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
291 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
293 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
297 /* Place master sync out of reset */
298 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
299 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
304 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
306 struct idtcm *idtcm = channel->idtcm;
314 u16 output_mask = channel->output_mask;
316 switch (channel->dpll_n) {
318 sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
321 sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
324 sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
327 sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
333 for (pll = 0; pll < 8; pll++) {
335 qn = output_mask & 0x1;
336 output_mask = output_mask >> 1;
339 /* First 4 pll has 2 outputs */
340 qn_plus_1 = output_mask & 0x1;
341 output_mask = output_mask >> 1;
346 if ((qn != 0) || (qn_plus_1 != 0))
347 err = _sync_pll_output(idtcm, pll, sync_src, qn,
357 static int _idtcm_set_dpll_tod(struct idtcm_channel *channel,
358 struct timespec64 const *ts,
359 enum hw_tod_write_trig_sel wr_trig)
361 struct idtcm *idtcm = channel->idtcm;
363 u8 buf[TOD_BYTE_COUNT];
366 struct timespec64 local_ts = *ts;
367 s64 total_overhead_ns;
369 /* Configure HW TOD write trigger. */
370 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
377 cmd |= wr_trig | 0x08;
379 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
385 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
387 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
392 err = idtcm_write(idtcm, channel->hw_dpll_n,
393 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
399 /* ARM HW TOD write trigger. */
402 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
405 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
407 if (idtcm->calculate_overhead_flag) {
408 /* Assumption: I2C @ 400KHz */
409 total_overhead_ns = ktime_to_ns(ktime_get_raw()
411 + idtcm->tod_write_overhead_ns
412 + SETTIME_CORRECTION;
414 timespec64_add_ns(&local_ts, total_overhead_ns);
416 idtcm->calculate_overhead_flag = 0;
419 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
424 err = idtcm_write(idtcm, channel->hw_dpll_n,
425 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
431 static int _idtcm_settime(struct idtcm_channel *channel,
432 struct timespec64 const *ts,
433 enum hw_tod_write_trig_sel wr_trig)
435 struct idtcm *idtcm = channel->idtcm;
441 err = _idtcm_set_dpll_tod(channel, ts, wr_trig);
446 /* Wait for the operation to complete. */
447 for (i = 0; i < 10000; i++) {
448 err = idtcm_read(idtcm, channel->hw_dpll_n,
449 HW_DPLL_TOD_CTRL_1, &trig_sel,
455 if (trig_sel == 0x4a)
464 retval = idtcm_sync_pps_output(channel);
469 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
474 struct idtcm *idtcm = channel->idtcm;
478 for (i = 0; i < 4; i++) {
479 buf[i] = 0xff & (offset_ns);
483 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
489 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
494 struct idtcm *idtcm = channel->idtcm;
498 if (max_ffo_ppb & 0xff000000)
501 for (i = 0; i < 3; i++) {
502 buf[i] = 0xff & (max_ffo_ppb);
506 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
507 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
512 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
515 struct idtcm *idtcm = channel->idtcm;
519 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
527 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
528 PULL_IN_CTRL, &buf, sizeof(buf));
536 static int idtcm_do_phase_pull_in(struct idtcm_channel *channel,
542 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
547 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
552 err = idtcm_start_phase_pull_in(channel);
557 static int _idtcm_adjtime(struct idtcm_channel *channel, s64 delta)
560 struct idtcm *idtcm = channel->idtcm;
561 struct timespec64 ts;
564 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
565 err = idtcm_do_phase_pull_in(channel, delta, 0);
567 idtcm->calculate_overhead_flag = 1;
569 err = _idtcm_gettime(channel, &ts);
574 now = timespec64_to_ns(&ts);
577 ts = ns_to_timespec64(now);
579 err = _idtcm_settime(channel, &ts, HW_TOD_WR_TRIG_SEL_MSB);
585 static int idtcm_state_machine_reset(struct idtcm *idtcm)
588 u8 byte = SM_RESET_CMD;
590 err = idtcm_write(idtcm, RESET_CTRL, SM_RESET, &byte, sizeof(byte));
593 msleep_interruptible(POST_SM_RESET_DELAY_MS);
598 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
600 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
603 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
608 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
610 *product_id = (buf[1] << 8) | buf[0];
615 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
620 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
627 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
629 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
632 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
634 return idtcm_read(idtcm,
641 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
644 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
645 config_select, sizeof(u8));
648 static int process_pll_mask(struct idtcm *idtcm, u32 addr, u8 val, u8 *mask)
652 if (addr == PLL_MASK_ADDR) {
653 if ((val & 0xf0) || !(val & 0xf)) {
654 dev_err(&idtcm->client->dev,
655 "Invalid PLL mask 0x%hhx\n", val);
664 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
669 case OUTPUT_MASK_PLL0_ADDR:
670 SET_U16_LSB(idtcm->channel[0].output_mask, val);
672 case OUTPUT_MASK_PLL0_ADDR + 1:
673 SET_U16_MSB(idtcm->channel[0].output_mask, val);
675 case OUTPUT_MASK_PLL1_ADDR:
676 SET_U16_LSB(idtcm->channel[1].output_mask, val);
678 case OUTPUT_MASK_PLL1_ADDR + 1:
679 SET_U16_MSB(idtcm->channel[1].output_mask, val);
681 case OUTPUT_MASK_PLL2_ADDR:
682 SET_U16_LSB(idtcm->channel[2].output_mask, val);
684 case OUTPUT_MASK_PLL2_ADDR + 1:
685 SET_U16_MSB(idtcm->channel[2].output_mask, val);
687 case OUTPUT_MASK_PLL3_ADDR:
688 SET_U16_LSB(idtcm->channel[3].output_mask, val);
690 case OUTPUT_MASK_PLL3_ADDR + 1:
691 SET_U16_MSB(idtcm->channel[3].output_mask, val);
701 static int check_and_set_masks(struct idtcm *idtcm,
707 if (set_pll_output_mask(idtcm, regaddr, val)) {
708 /* Not an output mask, check for pll mask */
709 err = process_pll_mask(idtcm, regaddr, val, &idtcm->pll_mask);
715 static void display_pll_and_output_masks(struct idtcm *idtcm)
720 dev_dbg(&idtcm->client->dev, "pllmask = 0x%02x\n", idtcm->pll_mask);
722 for (i = 0; i < MAX_PHC_PLL; i++) {
725 if (mask & idtcm->pll_mask)
726 dev_dbg(&idtcm->client->dev,
727 "PLL%d output_mask = 0x%04x\n",
728 i, idtcm->channel[i].output_mask);
732 static int idtcm_load_firmware(struct idtcm *idtcm,
735 const struct firmware *fw;
736 struct idtcm_fwrc *rec;
743 dev_dbg(&idtcm->client->dev, "requesting firmware '%s'\n", FW_FILENAME);
745 err = request_firmware(&fw, FW_FILENAME, dev);
750 dev_dbg(&idtcm->client->dev, "firmware size %zu bytes\n", fw->size);
752 rec = (struct idtcm_fwrc *) fw->data;
755 idtcm_state_machine_reset(idtcm);
757 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
760 dev_err(&idtcm->client->dev,
761 "bad firmware, reserved field non-zero\n");
764 regaddr = rec->hiaddr << 8;
765 regaddr |= rec->loaddr;
768 loaddr = rec->loaddr;
772 err = check_and_set_masks(idtcm, regaddr, val);
776 /* Top (status registers) and bottom are read-only */
777 if ((regaddr < GPIO_USER_CONTROL)
778 || (regaddr >= SCRATCH))
781 /* Page size 128, last 4 bytes of page skipped */
782 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
783 || ((loaddr > 0xfb) && (loaddr <= 0xff)))
786 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
793 display_pll_and_output_masks(idtcm);
796 release_firmware(fw);
800 static int idtcm_pps_enable(struct idtcm_channel *channel, bool enable)
802 struct idtcm *idtcm = channel->idtcm;
808 * This assumes that the 1-PPS is on the second of the two
809 * output. But is this always true?
811 switch (channel->dpll_n) {
828 err = idtcm_read(idtcm, module, OUT_CTRL_1, &val, sizeof(val));
834 val |= SQUELCH_DISABLE;
836 val &= ~SQUELCH_DISABLE;
838 err = idtcm_write(idtcm, module, OUT_CTRL_1, &val, sizeof(val));
846 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
847 enum pll_mode pll_mode)
849 struct idtcm *idtcm = channel->idtcm;
853 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
854 &dpll_mode, sizeof(dpll_mode));
858 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
860 dpll_mode |= (pll_mode << PLL_MODE_SHIFT);
862 channel->pll_mode = pll_mode;
864 err = idtcm_write(idtcm, channel->dpll_n, DPLL_MODE,
865 &dpll_mode, sizeof(dpll_mode));
872 /* PTP Hardware Clock interface */
874 static int idtcm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
876 struct idtcm_channel *channel =
877 container_of(ptp, struct idtcm_channel, caps);
878 struct idtcm *idtcm = channel->idtcm;
885 if (channel->pll_mode != PLL_MODE_WRITE_FREQUENCY) {
886 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
892 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
901 * FCW = -------------
909 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
910 fcw = ppb * 1000000000000ULL;
912 fcw = div_u64(fcw, 111022);
917 for (i = 0; i < 6; i++) {
922 mutex_lock(&idtcm->reg_lock);
924 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
927 mutex_unlock(&idtcm->reg_lock);
931 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
933 struct idtcm_channel *channel =
934 container_of(ptp, struct idtcm_channel, caps);
935 struct idtcm *idtcm = channel->idtcm;
938 mutex_lock(&idtcm->reg_lock);
940 err = _idtcm_gettime(channel, ts);
942 mutex_unlock(&idtcm->reg_lock);
947 static int idtcm_settime(struct ptp_clock_info *ptp,
948 const struct timespec64 *ts)
950 struct idtcm_channel *channel =
951 container_of(ptp, struct idtcm_channel, caps);
952 struct idtcm *idtcm = channel->idtcm;
955 mutex_lock(&idtcm->reg_lock);
957 err = _idtcm_settime(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
959 mutex_unlock(&idtcm->reg_lock);
964 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
966 struct idtcm_channel *channel =
967 container_of(ptp, struct idtcm_channel, caps);
968 struct idtcm *idtcm = channel->idtcm;
971 mutex_lock(&idtcm->reg_lock);
973 err = _idtcm_adjtime(channel, delta);
975 mutex_unlock(&idtcm->reg_lock);
980 static int idtcm_enable(struct ptp_clock_info *ptp,
981 struct ptp_clock_request *rq, int on)
983 struct idtcm_channel *channel =
984 container_of(ptp, struct idtcm_channel, caps);
987 case PTP_CLK_REQ_PEROUT:
989 return idtcm_pps_enable(channel, false);
991 /* Only accept a 1-PPS aligned to the second. */
992 if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
993 rq->perout.period.nsec)
996 return idtcm_pps_enable(channel, true);
1004 static int idtcm_enable_tod(struct idtcm_channel *channel)
1006 struct idtcm *idtcm = channel->idtcm;
1007 struct timespec64 ts = {0, 0};
1011 err = idtcm_pps_enable(channel, false);
1016 * Start the TOD clock ticking.
1018 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1024 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1028 return _idtcm_settime(channel, &ts, HW_TOD_WR_TRIG_SEL_MSB);
1031 static void idtcm_display_version_info(struct idtcm *idtcm)
1039 char *fmt = "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d\n";
1041 idtcm_read_major_release(idtcm, &major);
1042 idtcm_read_minor_release(idtcm, &minor);
1043 idtcm_read_hotfix_release(idtcm, &hotfix);
1045 idtcm_read_product_id(idtcm, &product_id);
1046 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
1048 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
1050 dev_info(&idtcm->client->dev, fmt, major, minor, hotfix,
1051 product_id, hw_rev_id, config_select);
1054 static const struct ptp_clock_info idtcm_caps = {
1055 .owner = THIS_MODULE,
1058 .adjfreq = &idtcm_adjfreq,
1059 .adjtime = &idtcm_adjtime,
1060 .gettime64 = &idtcm_gettime,
1061 .settime64 = &idtcm_settime,
1062 .enable = &idtcm_enable,
1065 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
1067 struct idtcm_channel *channel;
1070 if (!(index < MAX_PHC_PLL))
1073 channel = &idtcm->channel[index];
1077 channel->dpll_freq = DPLL_FREQ_0;
1078 channel->dpll_n = DPLL_0;
1079 channel->tod_read_primary = TOD_READ_PRIMARY_0;
1080 channel->tod_write = TOD_WRITE_0;
1081 channel->tod_n = TOD_0;
1082 channel->hw_dpll_n = HW_DPLL_0;
1083 channel->dpll_phase = DPLL_PHASE_0;
1084 channel->dpll_ctrl_n = DPLL_CTRL_0;
1085 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
1088 channel->dpll_freq = DPLL_FREQ_1;
1089 channel->dpll_n = DPLL_1;
1090 channel->tod_read_primary = TOD_READ_PRIMARY_1;
1091 channel->tod_write = TOD_WRITE_1;
1092 channel->tod_n = TOD_1;
1093 channel->hw_dpll_n = HW_DPLL_1;
1094 channel->dpll_phase = DPLL_PHASE_1;
1095 channel->dpll_ctrl_n = DPLL_CTRL_1;
1096 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
1099 channel->dpll_freq = DPLL_FREQ_2;
1100 channel->dpll_n = DPLL_2;
1101 channel->tod_read_primary = TOD_READ_PRIMARY_2;
1102 channel->tod_write = TOD_WRITE_2;
1103 channel->tod_n = TOD_2;
1104 channel->hw_dpll_n = HW_DPLL_2;
1105 channel->dpll_phase = DPLL_PHASE_2;
1106 channel->dpll_ctrl_n = DPLL_CTRL_2;
1107 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
1110 channel->dpll_freq = DPLL_FREQ_3;
1111 channel->dpll_n = DPLL_3;
1112 channel->tod_read_primary = TOD_READ_PRIMARY_3;
1113 channel->tod_write = TOD_WRITE_3;
1114 channel->tod_n = TOD_3;
1115 channel->hw_dpll_n = HW_DPLL_3;
1116 channel->dpll_phase = DPLL_PHASE_3;
1117 channel->dpll_ctrl_n = DPLL_CTRL_3;
1118 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
1124 channel->idtcm = idtcm;
1126 channel->caps = idtcm_caps;
1127 snprintf(channel->caps.name, sizeof(channel->caps.name),
1128 "IDT CM PLL%u", index);
1130 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1134 err = idtcm_enable_tod(channel);
1138 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
1140 if (IS_ERR(channel->ptp_clock)) {
1141 err = PTR_ERR(channel->ptp_clock);
1142 channel->ptp_clock = NULL;
1146 if (!channel->ptp_clock)
1149 dev_info(&idtcm->client->dev, "PLL%d registered as ptp%d\n",
1150 index, channel->ptp_clock->index);
1155 static void ptp_clock_unregister_all(struct idtcm *idtcm)
1158 struct idtcm_channel *channel;
1160 for (i = 0; i < MAX_PHC_PLL; i++) {
1162 channel = &idtcm->channel[i];
1164 if (channel->ptp_clock)
1165 ptp_clock_unregister(channel->ptp_clock);
1169 static void set_default_masks(struct idtcm *idtcm)
1171 idtcm->pll_mask = DEFAULT_PLL_MASK;
1173 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
1174 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
1175 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
1176 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
1179 static int set_tod_write_overhead(struct idtcm *idtcm)
1189 char buf[TOD_BYTE_COUNT];
1191 struct idtcm_channel *channel = &idtcm->channel[2];
1193 /* Set page offset */
1194 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
1197 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
1199 start = ktime_get_raw();
1201 err = idtcm_write(idtcm, channel->hw_dpll_n,
1202 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
1207 stop = ktime_get_raw();
1209 total_ns += ktime_to_ns(stop - start);
1212 idtcm->tod_write_overhead_ns = div_s64(total_ns,
1213 TOD_WRITE_OVERHEAD_COUNT_MAX);
1218 static int idtcm_probe(struct i2c_client *client,
1219 const struct i2c_device_id *id)
1221 struct idtcm *idtcm;
1225 /* Unused for now */
1228 idtcm = devm_kzalloc(&client->dev, sizeof(struct idtcm), GFP_KERNEL);
1233 idtcm->client = client;
1234 idtcm->page_offset = 0xff;
1235 idtcm->calculate_overhead_flag = 0;
1237 set_default_masks(idtcm);
1239 mutex_init(&idtcm->reg_lock);
1240 mutex_lock(&idtcm->reg_lock);
1242 idtcm_display_version_info(idtcm);
1244 err = set_tod_write_overhead(idtcm);
1247 mutex_unlock(&idtcm->reg_lock);
1251 err = idtcm_load_firmware(idtcm, &client->dev);
1254 dev_warn(&idtcm->client->dev,
1255 "loading firmware failed with %d\n", err);
1257 if (idtcm->pll_mask) {
1258 for (i = 0; i < MAX_PHC_PLL; i++) {
1259 if (idtcm->pll_mask & (1 << i)) {
1260 err = idtcm_enable_channel(idtcm, i);
1266 dev_err(&idtcm->client->dev,
1267 "no PLLs flagged as PHCs, nothing to do\n");
1271 mutex_unlock(&idtcm->reg_lock);
1274 ptp_clock_unregister_all(idtcm);
1278 i2c_set_clientdata(client, idtcm);
1283 static int idtcm_remove(struct i2c_client *client)
1285 struct idtcm *idtcm = i2c_get_clientdata(client);
1287 ptp_clock_unregister_all(idtcm);
1289 mutex_destroy(&idtcm->reg_lock);
1295 static const struct of_device_id idtcm_dt_id[] = {
1296 { .compatible = "idt,8a34000" },
1297 { .compatible = "idt,8a34001" },
1298 { .compatible = "idt,8a34002" },
1299 { .compatible = "idt,8a34003" },
1300 { .compatible = "idt,8a34004" },
1301 { .compatible = "idt,8a34005" },
1302 { .compatible = "idt,8a34006" },
1303 { .compatible = "idt,8a34007" },
1304 { .compatible = "idt,8a34008" },
1305 { .compatible = "idt,8a34009" },
1306 { .compatible = "idt,8a34010" },
1307 { .compatible = "idt,8a34011" },
1308 { .compatible = "idt,8a34012" },
1309 { .compatible = "idt,8a34013" },
1310 { .compatible = "idt,8a34014" },
1311 { .compatible = "idt,8a34015" },
1312 { .compatible = "idt,8a34016" },
1313 { .compatible = "idt,8a34017" },
1314 { .compatible = "idt,8a34018" },
1315 { .compatible = "idt,8a34019" },
1316 { .compatible = "idt,8a34040" },
1317 { .compatible = "idt,8a34041" },
1318 { .compatible = "idt,8a34042" },
1319 { .compatible = "idt,8a34043" },
1320 { .compatible = "idt,8a34044" },
1321 { .compatible = "idt,8a34045" },
1322 { .compatible = "idt,8a34046" },
1323 { .compatible = "idt,8a34047" },
1324 { .compatible = "idt,8a34048" },
1325 { .compatible = "idt,8a34049" },
1328 MODULE_DEVICE_TABLE(of, idtcm_dt_id);
1331 static const struct i2c_device_id idtcm_i2c_id[] = {
1364 MODULE_DEVICE_TABLE(i2c, idtcm_i2c_id);
1366 static struct i2c_driver idtcm_driver = {
1368 .of_match_table = of_match_ptr(idtcm_dt_id),
1371 .probe = idtcm_probe,
1372 .remove = idtcm_remove,
1373 .id_table = idtcm_i2c_id,
1376 module_i2c_driver(idtcm_driver);