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/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/timekeeping.h>
16 #include <linux/string.h>
18 #include "ptp_private.h"
19 #include "ptp_clockmatrix.h"
21 MODULE_DESCRIPTION("Driver for IDT ClockMatrix(TM) family");
22 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
23 MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
24 MODULE_VERSION("1.0");
25 MODULE_LICENSE("GPL");
28 * The name of the firmware file to be loaded
29 * over-rides any automatic selection
31 static char *firmware;
32 module_param(firmware, charp, 0);
34 #define SETTIME_CORRECTION (0)
36 static int contains_full_configuration(const struct firmware *fw)
38 s32 full_count = FULL_FW_CFG_BYTES - FULL_FW_CFG_SKIPPED_BYTES;
39 struct idtcm_fwrc *rec = (struct idtcm_fwrc *)fw->data;
45 /* If the firmware contains 'full configuration' SM_RESET can be used
46 * to ensure proper configuration.
48 * Full configuration is defined as the number of programmable
49 * bytes within the configuration range minus page offset addr range.
51 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
52 regaddr = rec->hiaddr << 8;
53 regaddr |= rec->loaddr;
59 /* Top (status registers) and bottom are read-only */
60 if (regaddr < GPIO_USER_CONTROL || regaddr >= SCRATCH)
63 /* Page size 128, last 4 bytes of page skipped */
64 if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
70 return (count >= full_count);
73 static int char_array_to_timespec(u8 *buf,
75 struct timespec64 *ts)
81 if (count < TOD_BYTE_COUNT)
84 /* Sub-nanoseconds are in buf[0]. */
86 for (i = 0; i < 3; i++) {
92 for (i = 0; i < 5; i++) {
103 static int timespec_to_char_array(struct timespec64 const *ts,
111 if (count < TOD_BYTE_COUNT)
117 /* Sub-nanoseconds are in buf[0]. */
119 for (i = 1; i < 5; i++) {
120 buf[i] = nsec & 0xff;
124 for (i = 5; i < TOD_BYTE_COUNT; i++) {
133 static int idtcm_strverscmp(const char *version1, const char *version2)
138 if (sscanf(version1, "%hhu.%hhu.%hhu",
139 &ver1[0], &ver1[1], &ver1[2]) != 3)
141 if (sscanf(version2, "%hhu.%hhu.%hhu",
142 &ver2[0], &ver2[1], &ver2[2]) != 3)
145 for (i = 0; i < 3; i++) {
146 if (ver1[i] > ver2[i])
148 if (ver1[i] < ver2[i])
155 static int idtcm_xfer_read(struct idtcm *idtcm,
160 struct i2c_client *client = idtcm->client;
161 struct i2c_msg msg[2];
164 msg[0].addr = client->addr;
167 msg[0].buf = ®addr;
169 msg[1].addr = client->addr;
170 msg[1].flags = I2C_M_RD;
174 cnt = i2c_transfer(client->adapter, msg, 2);
177 dev_err(&client->dev,
178 "i2c_transfer failed at %d in %s, at addr: %04x!",
179 __LINE__, __func__, regaddr);
181 } else if (cnt != 2) {
182 dev_err(&client->dev,
183 "i2c_transfer sent only %d of %d messages", cnt, 2);
190 static int idtcm_xfer_write(struct idtcm *idtcm,
195 struct i2c_client *client = idtcm->client;
196 /* we add 1 byte for device register */
197 u8 msg[IDTCM_MAX_WRITE_COUNT + 1];
200 if (count > IDTCM_MAX_WRITE_COUNT)
204 memcpy(&msg[1], buf, count);
206 cnt = i2c_master_send(client, msg, count + 1);
209 dev_err(&client->dev,
210 "i2c_master_send failed at %d in %s, at addr: %04x!",
211 __LINE__, __func__, regaddr);
218 static int idtcm_page_offset(struct idtcm *idtcm, u8 val)
223 if (idtcm->page_offset == val)
231 err = idtcm_xfer_write(idtcm, PAGE_ADDR, buf, sizeof(buf));
233 idtcm->page_offset = 0xff;
234 dev_err(&idtcm->client->dev, "failed to set page offset");
236 idtcm->page_offset = val;
242 static int _idtcm_rdwr(struct idtcm *idtcm,
252 hi = (regaddr >> 8) & 0xff;
255 err = idtcm_page_offset(idtcm, hi);
260 return idtcm_xfer_write(idtcm, lo, buf, count);
262 return idtcm_xfer_read(idtcm, lo, buf, count);
265 static int idtcm_read(struct idtcm *idtcm,
271 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, false);
274 static int idtcm_write(struct idtcm *idtcm,
280 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, true);
283 static int clear_boot_status(struct idtcm *idtcm)
288 err = idtcm_write(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
293 static int read_boot_status(struct idtcm *idtcm, u32 *status)
298 err = idtcm_read(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
300 *status = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
305 static int wait_for_boot_status_ready(struct idtcm *idtcm)
308 u8 i = 30; /* 30 * 100ms = 3s */
312 err = read_boot_status(idtcm, &status);
324 dev_warn(&idtcm->client->dev, "%s timed out", __func__);
329 static int read_sys_apll_status(struct idtcm *idtcm, u8 *status)
331 return idtcm_read(idtcm, STATUS, DPLL_SYS_APLL_STATUS, status,
335 static int read_sys_dpll_status(struct idtcm *idtcm, u8 *status)
337 return idtcm_read(idtcm, STATUS, DPLL_SYS_STATUS, status, sizeof(u8));
340 static int wait_for_sys_apll_dpll_lock(struct idtcm *idtcm)
342 unsigned long timeout = jiffies + msecs_to_jiffies(LOCK_TIMEOUT_MS);
348 err = read_sys_apll_status(idtcm, &apll);
352 err = read_sys_dpll_status(idtcm, &dpll);
356 apll &= SYS_APLL_LOSS_LOCK_LIVE_MASK;
357 dpll &= DPLL_SYS_STATE_MASK;
359 if (apll == SYS_APLL_LOSS_LOCK_LIVE_LOCKED &&
360 dpll == DPLL_STATE_LOCKED) {
362 } else if (dpll == DPLL_STATE_FREERUN ||
363 dpll == DPLL_STATE_HOLDOVER ||
364 dpll == DPLL_STATE_OPEN_LOOP) {
365 dev_warn(&idtcm->client->dev,
366 "No wait state: DPLL_SYS_STATE %d", dpll);
370 msleep(LOCK_POLL_INTERVAL_MS);
371 } while (time_is_after_jiffies(timeout));
373 dev_warn(&idtcm->client->dev,
374 "%d ms lock timeout: SYS APLL Loss Lock %d SYS DPLL state %d",
375 LOCK_TIMEOUT_MS, apll, dpll);
380 static void wait_for_chip_ready(struct idtcm *idtcm)
382 if (wait_for_boot_status_ready(idtcm))
383 dev_warn(&idtcm->client->dev, "BOOT_STATUS != 0xA0");
385 if (wait_for_sys_apll_dpll_lock(idtcm))
386 dev_warn(&idtcm->client->dev,
387 "Continuing while SYS APLL/DPLL is not locked");
390 static int _idtcm_gettime(struct idtcm_channel *channel,
391 struct timespec64 *ts)
393 struct idtcm *idtcm = channel->idtcm;
394 u8 buf[TOD_BYTE_COUNT];
399 err = idtcm_read(idtcm, channel->tod_read_primary,
400 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
404 trigger &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
405 trigger |= (1 << TOD_READ_TRIGGER_SHIFT);
406 trigger &= ~TOD_READ_TRIGGER_MODE; /* single shot */
408 err = idtcm_write(idtcm, channel->tod_read_primary,
409 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
413 /* wait trigger to be 0 */
414 while (trigger & TOD_READ_TRIGGER_MASK) {
415 if (idtcm->calculate_overhead_flag)
416 idtcm->start_time = ktime_get_raw();
418 err = idtcm_read(idtcm, channel->tod_read_primary,
419 TOD_READ_PRIMARY_CMD, &trigger,
428 err = idtcm_read(idtcm, channel->tod_read_primary,
429 TOD_READ_PRIMARY, buf, sizeof(buf));
433 err = char_array_to_timespec(buf, sizeof(buf), ts);
438 static int _sync_pll_output(struct idtcm *idtcm,
450 if ((qn == 0) && (qn_plus_1 == 0))
455 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
456 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
459 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
460 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
463 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
464 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
467 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
468 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
471 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
472 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
475 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
476 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
479 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
480 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
483 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
484 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
490 val = SYNCTRL1_MASTER_SYNC_RST;
492 /* Place master sync in reset */
493 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
497 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
501 /* Set sync trigger mask */
502 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
505 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
508 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
510 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
514 /* PLL5 can have OUT8 as second additional output. */
515 if ((pll == 5) && (qn_plus_1 != 0)) {
516 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
517 &temp, sizeof(temp));
521 temp &= ~(Q9_TO_Q8_SYNC_TRIG);
523 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
524 &temp, sizeof(temp));
528 temp |= Q9_TO_Q8_SYNC_TRIG;
530 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
531 &temp, sizeof(temp));
536 /* PLL6 can have OUT11 as second additional output. */
537 if ((pll == 6) && (qn_plus_1 != 0)) {
538 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
539 &temp, sizeof(temp));
543 temp &= ~(Q10_TO_Q11_SYNC_TRIG);
545 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
546 &temp, sizeof(temp));
550 temp |= Q10_TO_Q11_SYNC_TRIG;
552 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
553 &temp, sizeof(temp));
558 /* Place master sync out of reset */
559 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
560 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
565 static int sync_source_dpll_tod_pps(u16 tod_addr, u8 *sync_src)
571 *sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
574 *sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
577 *sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
580 *sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
589 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
591 struct idtcm *idtcm = channel->idtcm;
600 u16 output_mask = channel->output_mask;
602 err = sync_source_dpll_tod_pps(channel->tod_n, &sync_src);
606 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
607 &temp, sizeof(temp));
611 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
612 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
615 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
616 &temp, sizeof(temp));
620 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
621 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
624 for (pll = 0; pll < 8; pll++) {
629 /* First 4 pll has 2 outputs */
630 qn = output_mask & 0x1;
631 output_mask = output_mask >> 1;
632 qn_plus_1 = output_mask & 0x1;
633 output_mask = output_mask >> 1;
634 } else if (pll == 4) {
636 qn = output_mask & 0x1;
637 output_mask = output_mask >> 1;
639 } else if (pll == 5) {
641 qn_plus_1 = output_mask & 0x1;
642 output_mask = output_mask >> 1;
644 qn = output_mask & 0x1;
645 output_mask = output_mask >> 1;
646 } else if (pll == 6) {
647 qn = output_mask & 0x1;
648 output_mask = output_mask >> 1;
650 qn_plus_1 = output_mask & 0x1;
651 output_mask = output_mask >> 1;
653 } else if (pll == 7) {
654 if (out11_mux == 0) {
655 qn = output_mask & 0x1;
656 output_mask = output_mask >> 1;
660 if ((qn != 0) || (qn_plus_1 != 0))
661 err = _sync_pll_output(idtcm, pll, sync_src, qn,
671 static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
672 struct timespec64 const *ts,
673 enum hw_tod_write_trig_sel wr_trig)
675 struct idtcm *idtcm = channel->idtcm;
676 u8 buf[TOD_BYTE_COUNT];
679 struct timespec64 local_ts = *ts;
680 s64 total_overhead_ns;
682 /* Configure HW TOD write trigger. */
683 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
689 cmd |= wr_trig | 0x08;
691 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
696 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
697 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
701 err = idtcm_write(idtcm, channel->hw_dpll_n,
702 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
707 /* ARM HW TOD write trigger. */
710 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
713 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
714 if (idtcm->calculate_overhead_flag) {
715 /* Assumption: I2C @ 400KHz */
716 ktime_t diff = ktime_sub(ktime_get_raw(),
718 total_overhead_ns = ktime_to_ns(diff)
719 + idtcm->tod_write_overhead_ns
720 + SETTIME_CORRECTION;
722 timespec64_add_ns(&local_ts, total_overhead_ns);
724 idtcm->calculate_overhead_flag = 0;
727 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
731 err = idtcm_write(idtcm, channel->hw_dpll_n,
732 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
738 static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
739 struct timespec64 const *ts,
740 enum scsr_tod_write_trig_sel wr_trig,
741 enum scsr_tod_write_type_sel wr_type)
743 struct idtcm *idtcm = channel->idtcm;
744 unsigned char buf[TOD_BYTE_COUNT], cmd;
745 struct timespec64 local_ts = *ts;
748 timespec64_add_ns(&local_ts, SETTIME_CORRECTION);
750 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
754 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
759 /* Trigger the write operation. */
760 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
765 cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
766 cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
767 cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
768 cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);
770 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
775 /* Wait for the operation to complete. */
777 /* pps trigger takes up to 1 sec to complete */
778 if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
781 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
786 if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
790 dev_err(&idtcm->client->dev,
791 "Timed out waiting for the write counter");
799 static int get_output_base_addr(u8 outn)
847 static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
848 struct timespec64 const *ts)
850 struct idtcm *idtcm = channel->idtcm;
853 err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
855 dev_err(&idtcm->client->dev,
856 "%s: Set HW ToD failed", __func__);
860 return idtcm_sync_pps_output(channel);
863 static int _idtcm_settime(struct idtcm_channel *channel,
864 struct timespec64 const *ts,
865 enum scsr_tod_write_type_sel wr_type)
867 return _idtcm_set_dpll_scsr_tod(channel, ts,
868 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
872 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
877 struct idtcm *idtcm = channel->idtcm;
880 for (i = 0; i < 4; i++) {
881 buf[i] = 0xff & (offset_ns);
885 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
891 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
896 struct idtcm *idtcm = channel->idtcm;
899 if (max_ffo_ppb & 0xff000000)
902 for (i = 0; i < 3; i++) {
903 buf[i] = 0xff & (max_ffo_ppb);
907 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
908 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
913 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
916 struct idtcm *idtcm = channel->idtcm;
919 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
926 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
927 PULL_IN_CTRL, &buf, sizeof(buf));
935 static int idtcm_do_phase_pull_in(struct idtcm_channel *channel,
941 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
945 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
949 err = idtcm_start_phase_pull_in(channel);
954 static int set_tod_write_overhead(struct idtcm_channel *channel)
956 struct idtcm *idtcm = channel->idtcm;
965 char buf[TOD_BYTE_COUNT] = {0};
967 /* Set page offset */
968 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
971 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
972 start = ktime_get_raw();
974 err = idtcm_write(idtcm, channel->hw_dpll_n,
975 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
979 stop = ktime_get_raw();
981 diff = ktime_sub(stop, start);
983 current_ns = ktime_to_ns(diff);
986 lowest_ns = current_ns;
988 if (current_ns < lowest_ns)
989 lowest_ns = current_ns;
993 idtcm->tod_write_overhead_ns = lowest_ns;
998 static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
1001 struct idtcm *idtcm = channel->idtcm;
1002 struct timespec64 ts;
1005 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
1006 err = idtcm_do_phase_pull_in(channel, delta, 0);
1008 idtcm->calculate_overhead_flag = 1;
1010 err = set_tod_write_overhead(channel);
1014 err = _idtcm_gettime(channel, &ts);
1018 now = timespec64_to_ns(&ts);
1021 ts = ns_to_timespec64(now);
1023 err = _idtcm_settime_deprecated(channel, &ts);
1029 static int idtcm_state_machine_reset(struct idtcm *idtcm)
1031 u8 byte = SM_RESET_CMD;
1036 clear_boot_status(idtcm);
1038 err = idtcm_write(idtcm, RESET_CTRL, SM_RESET, &byte, sizeof(byte));
1041 for (i = 0; i < 30; i++) {
1042 msleep_interruptible(100);
1043 read_boot_status(idtcm, &status);
1045 if (status == 0xA0) {
1046 dev_dbg(&idtcm->client->dev,
1047 "SM_RESET completed in %d ms", i * 100);
1053 dev_err(&idtcm->client->dev,
1054 "Timed out waiting for CM_RESET to complete");
1060 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
1062 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1065 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
1070 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
1072 *product_id = (buf[1] << 8) | buf[0];
1077 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
1082 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
1089 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
1091 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
1094 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
1096 return idtcm_read(idtcm,
1103 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
1106 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
1107 config_select, sizeof(u8));
1110 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
1115 case TOD0_OUT_ALIGN_MASK_ADDR:
1116 SET_U16_LSB(idtcm->channel[0].output_mask, val);
1118 case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1119 SET_U16_MSB(idtcm->channel[0].output_mask, val);
1121 case TOD1_OUT_ALIGN_MASK_ADDR:
1122 SET_U16_LSB(idtcm->channel[1].output_mask, val);
1124 case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1125 SET_U16_MSB(idtcm->channel[1].output_mask, val);
1127 case TOD2_OUT_ALIGN_MASK_ADDR:
1128 SET_U16_LSB(idtcm->channel[2].output_mask, val);
1130 case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1131 SET_U16_MSB(idtcm->channel[2].output_mask, val);
1133 case TOD3_OUT_ALIGN_MASK_ADDR:
1134 SET_U16_LSB(idtcm->channel[3].output_mask, val);
1136 case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1137 SET_U16_MSB(idtcm->channel[3].output_mask, val);
1140 err = -EFAULT; /* Bad address */;
1147 static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
1149 if (index >= MAX_TOD) {
1150 dev_err(&idtcm->client->dev, "ToD%d not supported", index);
1154 if (pll >= MAX_PLL) {
1155 dev_err(&idtcm->client->dev, "Pll%d not supported", pll);
1159 idtcm->channel[index].pll = pll;
1164 static int check_and_set_masks(struct idtcm *idtcm,
1172 if ((val & 0xf0) || !(val & 0x0f)) {
1173 dev_err(&idtcm->client->dev, "Invalid TOD mask 0x%02x", val);
1176 idtcm->tod_mask = val;
1179 case TOD0_PTP_PLL_ADDR:
1180 err = set_tod_ptp_pll(idtcm, 0, val);
1182 case TOD1_PTP_PLL_ADDR:
1183 err = set_tod_ptp_pll(idtcm, 1, val);
1185 case TOD2_PTP_PLL_ADDR:
1186 err = set_tod_ptp_pll(idtcm, 2, val);
1188 case TOD3_PTP_PLL_ADDR:
1189 err = set_tod_ptp_pll(idtcm, 3, val);
1192 err = set_pll_output_mask(idtcm, regaddr, val);
1199 static void display_pll_and_masks(struct idtcm *idtcm)
1204 dev_dbg(&idtcm->client->dev, "tod_mask = 0x%02x", idtcm->tod_mask);
1206 for (i = 0; i < MAX_TOD; i++) {
1209 if (mask & idtcm->tod_mask)
1210 dev_dbg(&idtcm->client->dev,
1211 "TOD%d pll = %d output_mask = 0x%04x",
1212 i, idtcm->channel[i].pll,
1213 idtcm->channel[i].output_mask);
1217 static int idtcm_load_firmware(struct idtcm *idtcm,
1220 char fname[128] = FW_FILENAME;
1221 const struct firmware *fw;
1222 struct idtcm_fwrc *rec;
1229 if (firmware) /* module parameter */
1230 snprintf(fname, sizeof(fname), "%s", firmware);
1232 dev_dbg(&idtcm->client->dev, "requesting firmware '%s'", fname);
1234 err = request_firmware(&fw, fname, dev);
1236 dev_err(&idtcm->client->dev,
1237 "Failed at line %d in %s!", __LINE__, __func__);
1241 dev_dbg(&idtcm->client->dev, "firmware size %zu bytes", fw->size);
1243 rec = (struct idtcm_fwrc *) fw->data;
1245 if (contains_full_configuration(fw))
1246 idtcm_state_machine_reset(idtcm);
1248 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1249 if (rec->reserved) {
1250 dev_err(&idtcm->client->dev,
1251 "bad firmware, reserved field non-zero");
1254 regaddr = rec->hiaddr << 8;
1255 regaddr |= rec->loaddr;
1258 loaddr = rec->loaddr;
1262 err = check_and_set_masks(idtcm, regaddr, val);
1265 if (err != -EINVAL) {
1268 /* Top (status registers) and bottom are read-only */
1269 if ((regaddr < GPIO_USER_CONTROL)
1270 || (regaddr >= SCRATCH))
1273 /* Page size 128, last 4 bytes of page skipped */
1274 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
1278 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
1285 display_pll_and_masks(idtcm);
1288 release_firmware(fw);
1292 static int idtcm_output_enable(struct idtcm_channel *channel,
1293 bool enable, unsigned int outn)
1295 struct idtcm *idtcm = channel->idtcm;
1300 base = get_output_base_addr(outn);
1303 dev_err(&idtcm->client->dev,
1304 "%s - Unsupported out%d", __func__, outn);
1308 err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1313 val |= SQUELCH_DISABLE;
1315 val &= ~SQUELCH_DISABLE;
1317 return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1320 static int idtcm_output_mask_enable(struct idtcm_channel *channel,
1327 mask = channel->output_mask;
1332 err = idtcm_output_enable(channel, enable, outn);
1344 static int idtcm_perout_enable(struct idtcm_channel *channel,
1346 struct ptp_perout_request *perout)
1348 struct idtcm *idtcm = channel->idtcm;
1349 unsigned int flags = perout->flags;
1350 struct timespec64 ts = {0, 0};
1353 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
1354 err = idtcm_output_mask_enable(channel, enable);
1356 err = idtcm_output_enable(channel, enable, perout->index);
1359 dev_err(&idtcm->client->dev, "Unable to set output enable");
1363 /* Align output to internal 1 PPS */
1364 return _idtcm_settime(channel, &ts, SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS);
1367 static int idtcm_get_pll_mode(struct idtcm_channel *channel,
1368 enum pll_mode *pll_mode)
1370 struct idtcm *idtcm = channel->idtcm;
1374 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1375 &dpll_mode, sizeof(dpll_mode));
1379 *pll_mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;
1384 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
1385 enum pll_mode pll_mode)
1387 struct idtcm *idtcm = channel->idtcm;
1391 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1392 &dpll_mode, sizeof(dpll_mode));
1396 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
1398 dpll_mode |= (pll_mode << PLL_MODE_SHIFT);
1400 channel->pll_mode = pll_mode;
1402 err = idtcm_write(idtcm, channel->dpll_n, DPLL_MODE,
1403 &dpll_mode, sizeof(dpll_mode));
1410 /* PTP Hardware Clock interface */
1413 * @brief Maximum absolute value for write phase offset in picoseconds
1415 * Destination signed register is 32-bit register in resolution of 50ps
1417 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1419 static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
1421 struct idtcm *idtcm = channel->idtcm;
1428 if (channel->pll_mode != PLL_MODE_WRITE_PHASE) {
1429 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);
1434 offset_ps = (s64)delta_ns * 1000;
1437 * Check for 32-bit signed max * 50:
1439 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1441 if (offset_ps > MAX_ABS_WRITE_PHASE_PICOSECONDS)
1442 offset_ps = MAX_ABS_WRITE_PHASE_PICOSECONDS;
1443 else if (offset_ps < -MAX_ABS_WRITE_PHASE_PICOSECONDS)
1444 offset_ps = -MAX_ABS_WRITE_PHASE_PICOSECONDS;
1446 phase_50ps = div_s64(offset_ps, 50);
1448 for (i = 0; i < 4; i++) {
1449 buf[i] = phase_50ps & 0xff;
1453 err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
1459 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1461 struct idtcm *idtcm = channel->idtcm;
1467 if (channel->pll_mode != PLL_MODE_WRITE_FREQUENCY) {
1468 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1474 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
1483 * FCW = -------------
1487 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1488 fcw = scaled_ppm * 244140625ULL;
1490 fcw = div_s64(fcw, 1776);
1492 for (i = 0; i < 6; i++) {
1493 buf[i] = fcw & 0xff;
1497 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
1503 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1505 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1506 struct idtcm *idtcm = channel->idtcm;
1509 mutex_lock(&idtcm->reg_lock);
1511 err = _idtcm_gettime(channel, ts);
1513 dev_err(&idtcm->client->dev, "Failed at line %d in %s!",
1514 __LINE__, __func__);
1516 mutex_unlock(&idtcm->reg_lock);
1521 static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
1522 const struct timespec64 *ts)
1524 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1525 struct idtcm *idtcm = channel->idtcm;
1528 mutex_lock(&idtcm->reg_lock);
1530 err = _idtcm_settime_deprecated(channel, ts);
1532 dev_err(&idtcm->client->dev,
1533 "Failed at line %d in %s!", __LINE__, __func__);
1535 mutex_unlock(&idtcm->reg_lock);
1540 static int idtcm_settime(struct ptp_clock_info *ptp,
1541 const struct timespec64 *ts)
1543 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1544 struct idtcm *idtcm = channel->idtcm;
1547 mutex_lock(&idtcm->reg_lock);
1549 err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1551 dev_err(&idtcm->client->dev,
1552 "Failed at line %d in %s!", __LINE__, __func__);
1554 mutex_unlock(&idtcm->reg_lock);
1559 static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1561 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1562 struct idtcm *idtcm = channel->idtcm;
1565 mutex_lock(&idtcm->reg_lock);
1567 err = _idtcm_adjtime_deprecated(channel, delta);
1569 dev_err(&idtcm->client->dev,
1570 "Failed at line %d in %s!", __LINE__, __func__);
1572 mutex_unlock(&idtcm->reg_lock);
1577 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1579 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1580 struct idtcm *idtcm = channel->idtcm;
1581 struct timespec64 ts;
1582 enum scsr_tod_write_type_sel type;
1585 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1586 err = idtcm_do_phase_pull_in(channel, delta, 0);
1588 dev_err(&idtcm->client->dev,
1589 "Failed at line %d in %s!", __LINE__, __func__);
1594 ts = ns_to_timespec64(delta);
1595 type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
1597 ts = ns_to_timespec64(-delta);
1598 type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
1601 mutex_lock(&idtcm->reg_lock);
1603 err = _idtcm_settime(channel, &ts, type);
1605 dev_err(&idtcm->client->dev,
1606 "Failed at line %d in %s!", __LINE__, __func__);
1608 mutex_unlock(&idtcm->reg_lock);
1613 static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1615 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1616 struct idtcm *idtcm = channel->idtcm;
1619 mutex_lock(&idtcm->reg_lock);
1621 err = _idtcm_adjphase(channel, delta);
1623 dev_err(&idtcm->client->dev,
1624 "Failed at line %d in %s!", __LINE__, __func__);
1626 mutex_unlock(&idtcm->reg_lock);
1631 static int idtcm_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1633 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1634 struct idtcm *idtcm = channel->idtcm;
1637 mutex_lock(&idtcm->reg_lock);
1639 err = _idtcm_adjfine(channel, scaled_ppm);
1641 dev_err(&idtcm->client->dev,
1642 "Failed at line %d in %s!", __LINE__, __func__);
1644 mutex_unlock(&idtcm->reg_lock);
1649 static int idtcm_enable(struct ptp_clock_info *ptp,
1650 struct ptp_clock_request *rq, int on)
1653 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1656 case PTP_CLK_REQ_PEROUT:
1658 err = idtcm_perout_enable(channel, false, &rq->perout);
1660 dev_err(&channel->idtcm->client->dev,
1661 "Failed at line %d in %s!",
1662 __LINE__, __func__);
1666 /* Only accept a 1-PPS aligned to the second. */
1667 if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
1668 rq->perout.period.nsec)
1671 err = idtcm_perout_enable(channel, true, &rq->perout);
1673 dev_err(&channel->idtcm->client->dev,
1674 "Failed at line %d in %s!", __LINE__, __func__);
1683 static int _enable_pll_tod_sync(struct idtcm *idtcm,
1692 u16 out0 = 0, out1 = 0;
1694 if ((qn == 0) && (qn_plus_1 == 0))
1755 * Enable OUTPUT OUT_SYNC.
1758 err = idtcm_read(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1762 val &= ~OUT_SYNC_DISABLE;
1764 err = idtcm_write(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1770 err = idtcm_read(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1774 val &= ~OUT_SYNC_DISABLE;
1776 err = idtcm_write(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1781 /* enable dpll sync tod pps, must be set before dpll_mode */
1782 err = idtcm_read(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1786 val &= ~(TOD_SYNC_SOURCE_MASK << TOD_SYNC_SOURCE_SHIFT);
1787 val |= (sync_src << TOD_SYNC_SOURCE_SHIFT);
1790 return idtcm_write(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1793 static int idtcm_enable_tod_sync(struct idtcm_channel *channel)
1795 struct idtcm *idtcm = channel->idtcm;
1802 u16 output_mask = channel->output_mask;
1808 * set tod_out_sync_enable to 0.
1810 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1814 cfg &= ~TOD_OUT_SYNC_ENABLE;
1816 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1820 switch (channel->tod_n) {
1837 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE, &temp, sizeof(temp));
1841 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1842 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1845 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE, &temp, sizeof(temp));
1849 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1850 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1853 for (pll = 0; pll < 8; pll++) {
1858 /* First 4 pll has 2 outputs */
1859 qn = output_mask & 0x1;
1860 output_mask = output_mask >> 1;
1861 qn_plus_1 = output_mask & 0x1;
1862 output_mask = output_mask >> 1;
1863 } else if (pll == 4) {
1864 if (out8_mux == 0) {
1865 qn = output_mask & 0x1;
1866 output_mask = output_mask >> 1;
1868 } else if (pll == 5) {
1870 qn_plus_1 = output_mask & 0x1;
1871 output_mask = output_mask >> 1;
1873 qn = output_mask & 0x1;
1874 output_mask = output_mask >> 1;
1875 } else if (pll == 6) {
1876 qn = output_mask & 0x1;
1877 output_mask = output_mask >> 1;
1879 qn_plus_1 = output_mask & 0x1;
1880 output_mask = output_mask >> 1;
1882 } else if (pll == 7) {
1883 if (out11_mux == 0) {
1884 qn = output_mask & 0x1;
1885 output_mask = output_mask >> 1;
1889 if ((qn != 0) || (qn_plus_1 != 0))
1890 err = _enable_pll_tod_sync(idtcm, pll, sync_src, qn,
1899 static int idtcm_enable_tod(struct idtcm_channel *channel)
1901 struct idtcm *idtcm = channel->idtcm;
1902 struct timespec64 ts = {0, 0};
1907 * Start the TOD clock ticking.
1909 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1915 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1919 if (idtcm->deprecated)
1920 return _idtcm_settime_deprecated(channel, &ts);
1922 return _idtcm_settime(channel, &ts,
1923 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1926 static void idtcm_set_version_info(struct idtcm *idtcm)
1935 idtcm_read_major_release(idtcm, &major);
1936 idtcm_read_minor_release(idtcm, &minor);
1937 idtcm_read_hotfix_release(idtcm, &hotfix);
1939 idtcm_read_product_id(idtcm, &product_id);
1940 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
1942 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
1944 snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
1945 major, minor, hotfix);
1947 if (idtcm_strverscmp(idtcm->version, "4.8.7") >= 0)
1948 idtcm->deprecated = 0;
1950 idtcm->deprecated = 1;
1952 dev_info(&idtcm->client->dev,
1953 "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d",
1954 major, minor, hotfix,
1955 product_id, hw_rev_id, config_select);
1958 static const struct ptp_clock_info idtcm_caps = {
1959 .owner = THIS_MODULE,
1962 .adjphase = &idtcm_adjphase,
1963 .adjfine = &idtcm_adjfine,
1964 .adjtime = &idtcm_adjtime,
1965 .gettime64 = &idtcm_gettime,
1966 .settime64 = &idtcm_settime,
1967 .enable = &idtcm_enable,
1970 static const struct ptp_clock_info idtcm_caps_deprecated = {
1971 .owner = THIS_MODULE,
1974 .adjphase = &idtcm_adjphase,
1975 .adjfine = &idtcm_adjfine,
1976 .adjtime = &idtcm_adjtime_deprecated,
1977 .gettime64 = &idtcm_gettime,
1978 .settime64 = &idtcm_settime_deprecated,
1979 .enable = &idtcm_enable,
1982 static int configure_channel_pll(struct idtcm_channel *channel)
1986 switch (channel->pll) {
1988 channel->dpll_freq = DPLL_FREQ_0;
1989 channel->dpll_n = DPLL_0;
1990 channel->hw_dpll_n = HW_DPLL_0;
1991 channel->dpll_phase = DPLL_PHASE_0;
1992 channel->dpll_ctrl_n = DPLL_CTRL_0;
1993 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
1996 channel->dpll_freq = DPLL_FREQ_1;
1997 channel->dpll_n = DPLL_1;
1998 channel->hw_dpll_n = HW_DPLL_1;
1999 channel->dpll_phase = DPLL_PHASE_1;
2000 channel->dpll_ctrl_n = DPLL_CTRL_1;
2001 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
2004 channel->dpll_freq = DPLL_FREQ_2;
2005 channel->dpll_n = DPLL_2;
2006 channel->hw_dpll_n = HW_DPLL_2;
2007 channel->dpll_phase = DPLL_PHASE_2;
2008 channel->dpll_ctrl_n = DPLL_CTRL_2;
2009 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
2012 channel->dpll_freq = DPLL_FREQ_3;
2013 channel->dpll_n = DPLL_3;
2014 channel->hw_dpll_n = HW_DPLL_3;
2015 channel->dpll_phase = DPLL_PHASE_3;
2016 channel->dpll_ctrl_n = DPLL_CTRL_3;
2017 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
2020 channel->dpll_freq = DPLL_FREQ_4;
2021 channel->dpll_n = DPLL_4;
2022 channel->hw_dpll_n = HW_DPLL_4;
2023 channel->dpll_phase = DPLL_PHASE_4;
2024 channel->dpll_ctrl_n = DPLL_CTRL_4;
2025 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
2028 channel->dpll_freq = DPLL_FREQ_5;
2029 channel->dpll_n = DPLL_5;
2030 channel->hw_dpll_n = HW_DPLL_5;
2031 channel->dpll_phase = DPLL_PHASE_5;
2032 channel->dpll_ctrl_n = DPLL_CTRL_5;
2033 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
2036 channel->dpll_freq = DPLL_FREQ_6;
2037 channel->dpll_n = DPLL_6;
2038 channel->hw_dpll_n = HW_DPLL_6;
2039 channel->dpll_phase = DPLL_PHASE_6;
2040 channel->dpll_ctrl_n = DPLL_CTRL_6;
2041 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
2044 channel->dpll_freq = DPLL_FREQ_7;
2045 channel->dpll_n = DPLL_7;
2046 channel->hw_dpll_n = HW_DPLL_7;
2047 channel->dpll_phase = DPLL_PHASE_7;
2048 channel->dpll_ctrl_n = DPLL_CTRL_7;
2049 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
2058 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
2060 struct idtcm_channel *channel;
2063 if (!(index < MAX_TOD))
2066 channel = &idtcm->channel[index];
2068 /* Set pll addresses */
2069 err = configure_channel_pll(channel);
2073 /* Set tod addresses */
2076 channel->tod_read_primary = TOD_READ_PRIMARY_0;
2077 channel->tod_write = TOD_WRITE_0;
2078 channel->tod_n = TOD_0;
2081 channel->tod_read_primary = TOD_READ_PRIMARY_1;
2082 channel->tod_write = TOD_WRITE_1;
2083 channel->tod_n = TOD_1;
2086 channel->tod_read_primary = TOD_READ_PRIMARY_2;
2087 channel->tod_write = TOD_WRITE_2;
2088 channel->tod_n = TOD_2;
2091 channel->tod_read_primary = TOD_READ_PRIMARY_3;
2092 channel->tod_write = TOD_WRITE_3;
2093 channel->tod_n = TOD_3;
2099 channel->idtcm = idtcm;
2101 if (idtcm->deprecated)
2102 channel->caps = idtcm_caps_deprecated;
2104 channel->caps = idtcm_caps;
2106 snprintf(channel->caps.name, sizeof(channel->caps.name),
2107 "IDT CM TOD%u", index);
2109 if (!idtcm->deprecated) {
2110 err = idtcm_enable_tod_sync(channel);
2112 dev_err(&idtcm->client->dev,
2113 "Failed at line %d in %s!", __LINE__, __func__);
2118 /* Sync pll mode with hardware */
2119 err = idtcm_get_pll_mode(channel, &channel->pll_mode);
2121 dev_err(&idtcm->client->dev,
2122 "Error: %s - Unable to read pll mode", __func__);
2126 err = idtcm_enable_tod(channel);
2128 dev_err(&idtcm->client->dev,
2129 "Failed at line %d in %s!", __LINE__, __func__);
2133 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
2135 if (IS_ERR(channel->ptp_clock)) {
2136 err = PTR_ERR(channel->ptp_clock);
2137 channel->ptp_clock = NULL;
2141 if (!channel->ptp_clock)
2144 dev_info(&idtcm->client->dev, "PLL%d registered as ptp%d",
2145 index, channel->ptp_clock->index);
2150 static void ptp_clock_unregister_all(struct idtcm *idtcm)
2153 struct idtcm_channel *channel;
2155 for (i = 0; i < MAX_TOD; i++) {
2156 channel = &idtcm->channel[i];
2158 if (channel->ptp_clock)
2159 ptp_clock_unregister(channel->ptp_clock);
2163 static void set_default_masks(struct idtcm *idtcm)
2165 idtcm->tod_mask = DEFAULT_TOD_MASK;
2167 idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
2168 idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
2169 idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
2170 idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2172 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
2173 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
2174 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
2175 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
2178 static int idtcm_probe(struct i2c_client *client,
2179 const struct i2c_device_id *id)
2181 struct idtcm *idtcm;
2185 /* Unused for now */
2188 idtcm = devm_kzalloc(&client->dev, sizeof(struct idtcm), GFP_KERNEL);
2193 idtcm->client = client;
2194 idtcm->page_offset = 0xff;
2195 idtcm->calculate_overhead_flag = 0;
2197 set_default_masks(idtcm);
2199 mutex_init(&idtcm->reg_lock);
2200 mutex_lock(&idtcm->reg_lock);
2202 idtcm_set_version_info(idtcm);
2204 err = idtcm_load_firmware(idtcm, &client->dev);
2206 dev_warn(&idtcm->client->dev, "loading firmware failed with %d", err);
2208 wait_for_chip_ready(idtcm);
2210 if (idtcm->tod_mask) {
2211 for (i = 0; i < MAX_TOD; i++) {
2212 if (idtcm->tod_mask & (1 << i)) {
2213 err = idtcm_enable_channel(idtcm, i);
2215 dev_err(&idtcm->client->dev,
2216 "idtcm_enable_channel %d failed!", i);
2222 dev_err(&idtcm->client->dev,
2223 "no PLLs flagged as PHCs, nothing to do");
2227 mutex_unlock(&idtcm->reg_lock);
2230 ptp_clock_unregister_all(idtcm);
2234 i2c_set_clientdata(client, idtcm);
2239 static int idtcm_remove(struct i2c_client *client)
2241 struct idtcm *idtcm = i2c_get_clientdata(client);
2243 ptp_clock_unregister_all(idtcm);
2245 mutex_destroy(&idtcm->reg_lock);
2251 static const struct of_device_id idtcm_dt_id[] = {
2252 { .compatible = "idt,8a34000" },
2253 { .compatible = "idt,8a34001" },
2254 { .compatible = "idt,8a34002" },
2255 { .compatible = "idt,8a34003" },
2256 { .compatible = "idt,8a34004" },
2257 { .compatible = "idt,8a34005" },
2258 { .compatible = "idt,8a34006" },
2259 { .compatible = "idt,8a34007" },
2260 { .compatible = "idt,8a34008" },
2261 { .compatible = "idt,8a34009" },
2262 { .compatible = "idt,8a34010" },
2263 { .compatible = "idt,8a34011" },
2264 { .compatible = "idt,8a34012" },
2265 { .compatible = "idt,8a34013" },
2266 { .compatible = "idt,8a34014" },
2267 { .compatible = "idt,8a34015" },
2268 { .compatible = "idt,8a34016" },
2269 { .compatible = "idt,8a34017" },
2270 { .compatible = "idt,8a34018" },
2271 { .compatible = "idt,8a34019" },
2272 { .compatible = "idt,8a34040" },
2273 { .compatible = "idt,8a34041" },
2274 { .compatible = "idt,8a34042" },
2275 { .compatible = "idt,8a34043" },
2276 { .compatible = "idt,8a34044" },
2277 { .compatible = "idt,8a34045" },
2278 { .compatible = "idt,8a34046" },
2279 { .compatible = "idt,8a34047" },
2280 { .compatible = "idt,8a34048" },
2281 { .compatible = "idt,8a34049" },
2284 MODULE_DEVICE_TABLE(of, idtcm_dt_id);
2287 static const struct i2c_device_id idtcm_i2c_id[] = {
2320 MODULE_DEVICE_TABLE(i2c, idtcm_i2c_id);
2322 static struct i2c_driver idtcm_driver = {
2324 .of_match_table = of_match_ptr(idtcm_dt_id),
2327 .probe = idtcm_probe,
2328 .remove = idtcm_remove,
2329 .id_table = idtcm_i2c_id,
2332 module_i2c_driver(idtcm_driver);