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];
163 char *fmt = "i2c_transfer failed at %d in %s, at addr: %04X!\n";
165 msg[0].addr = client->addr;
168 msg[0].buf = ®addr;
170 msg[1].addr = client->addr;
171 msg[1].flags = I2C_M_RD;
175 cnt = i2c_transfer(client->adapter, msg, 2);
178 dev_err(&client->dev,
184 } else if (cnt != 2) {
185 dev_err(&client->dev,
186 "i2c_transfer sent only %d of %d messages\n", cnt, 2);
193 static int idtcm_xfer_write(struct idtcm *idtcm,
198 struct i2c_client *client = idtcm->client;
199 /* we add 1 byte for device register */
200 u8 msg[IDTCM_MAX_WRITE_COUNT + 1];
202 char *fmt = "i2c_master_send failed at %d in %s, at addr: %04X!\n";
204 if (count > IDTCM_MAX_WRITE_COUNT)
208 memcpy(&msg[1], buf, count);
210 cnt = i2c_master_send(client, msg, count + 1);
213 dev_err(&client->dev,
224 static int idtcm_page_offset(struct idtcm *idtcm, u8 val)
229 if (idtcm->page_offset == val)
237 err = idtcm_xfer_write(idtcm, PAGE_ADDR, buf, sizeof(buf));
240 idtcm->page_offset = 0xff;
241 dev_err(&idtcm->client->dev, "failed to set page offset\n");
243 idtcm->page_offset = val;
249 static int _idtcm_rdwr(struct idtcm *idtcm,
259 hi = (regaddr >> 8) & 0xff;
262 err = idtcm_page_offset(idtcm, hi);
268 return idtcm_xfer_write(idtcm, lo, buf, count);
270 return idtcm_xfer_read(idtcm, lo, buf, count);
273 static int idtcm_read(struct idtcm *idtcm,
279 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, false);
282 static int idtcm_write(struct idtcm *idtcm,
288 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, true);
291 static int clear_boot_status(struct idtcm *idtcm)
296 err = idtcm_write(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
301 static int read_boot_status(struct idtcm *idtcm, u32 *status)
306 err = idtcm_read(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
308 *status = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
313 static int wait_for_boot_status_ready(struct idtcm *idtcm)
316 u8 i = 30; /* 30 * 100ms = 3s */
320 err = read_boot_status(idtcm, &status);
333 dev_warn(&idtcm->client->dev, "%s timed out\n", __func__);
338 static int read_sys_apll_status(struct idtcm *idtcm, u8 *status)
340 return idtcm_read(idtcm, STATUS, DPLL_SYS_APLL_STATUS, status,
344 static int read_sys_dpll_status(struct idtcm *idtcm, u8 *status)
346 return idtcm_read(idtcm, STATUS, DPLL_SYS_STATUS, status, sizeof(u8));
349 static int wait_for_sys_apll_dpll_lock(struct idtcm *idtcm)
351 unsigned long timeout = jiffies + msecs_to_jiffies(LOCK_TIMEOUT_MS);
357 err = read_sys_apll_status(idtcm, &apll);
361 err = read_sys_dpll_status(idtcm, &dpll);
365 apll &= SYS_APLL_LOSS_LOCK_LIVE_MASK;
366 dpll &= DPLL_SYS_STATE_MASK;
368 if (apll == SYS_APLL_LOSS_LOCK_LIVE_LOCKED &&
369 dpll == DPLL_STATE_LOCKED) {
371 } else if (dpll == DPLL_STATE_FREERUN ||
372 dpll == DPLL_STATE_HOLDOVER ||
373 dpll == DPLL_STATE_OPEN_LOOP) {
374 dev_warn(&idtcm->client->dev,
375 "No wait state: DPLL_SYS_STATE %d", dpll);
379 msleep(LOCK_POLL_INTERVAL_MS);
380 } while (time_is_after_jiffies(timeout));
382 dev_warn(&idtcm->client->dev,
383 "%d ms lock timeout: SYS APLL Loss Lock %d SYS DPLL state %d",
384 LOCK_TIMEOUT_MS, apll, dpll);
389 static void wait_for_chip_ready(struct idtcm *idtcm)
391 if (wait_for_boot_status_ready(idtcm))
392 dev_warn(&idtcm->client->dev, "BOOT_STATUS != 0xA0");
394 if (wait_for_sys_apll_dpll_lock(idtcm))
395 dev_warn(&idtcm->client->dev,
396 "Continuing while SYS APLL/DPLL is not locked");
399 static int _idtcm_gettime(struct idtcm_channel *channel,
400 struct timespec64 *ts)
402 struct idtcm *idtcm = channel->idtcm;
403 u8 buf[TOD_BYTE_COUNT];
408 err = idtcm_read(idtcm, channel->tod_read_primary,
409 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
413 trigger &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
414 trigger |= (1 << TOD_READ_TRIGGER_SHIFT);
415 trigger &= ~TOD_READ_TRIGGER_MODE; /* single shot */
417 err = idtcm_write(idtcm, channel->tod_read_primary,
418 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
422 /* wait trigger to be 0 */
423 while (trigger & TOD_READ_TRIGGER_MASK) {
425 if (idtcm->calculate_overhead_flag)
426 idtcm->start_time = ktime_get_raw();
428 err = idtcm_read(idtcm, channel->tod_read_primary,
429 TOD_READ_PRIMARY_CMD, &trigger,
439 err = idtcm_read(idtcm, channel->tod_read_primary,
440 TOD_READ_PRIMARY, buf, sizeof(buf));
445 err = char_array_to_timespec(buf, sizeof(buf), ts);
450 static int _sync_pll_output(struct idtcm *idtcm,
462 if ((qn == 0) && (qn_plus_1 == 0))
467 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
468 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
471 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
472 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
475 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
476 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
479 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
480 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
483 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
484 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
487 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
488 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
491 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
492 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
495 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
496 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
502 val = SYNCTRL1_MASTER_SYNC_RST;
504 /* Place master sync in reset */
505 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
509 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
513 /* Set sync trigger mask */
514 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
517 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
520 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
522 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
526 /* PLL5 can have OUT8 as second additional output. */
527 if ((pll == 5) && (qn_plus_1 != 0)) {
528 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
529 &temp, sizeof(temp));
533 temp &= ~(Q9_TO_Q8_SYNC_TRIG);
535 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
536 &temp, sizeof(temp));
540 temp |= Q9_TO_Q8_SYNC_TRIG;
542 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
543 &temp, sizeof(temp));
548 /* PLL6 can have OUT11 as second additional output. */
549 if ((pll == 6) && (qn_plus_1 != 0)) {
550 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
551 &temp, sizeof(temp));
555 temp &= ~(Q10_TO_Q11_SYNC_TRIG);
557 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
558 &temp, sizeof(temp));
562 temp |= Q10_TO_Q11_SYNC_TRIG;
564 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
565 &temp, sizeof(temp));
570 /* Place master sync out of reset */
571 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
572 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
577 static int sync_source_dpll_tod_pps(u16 tod_addr, u8 *sync_src)
583 *sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
586 *sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
589 *sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
592 *sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
601 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
603 struct idtcm *idtcm = channel->idtcm;
614 u16 output_mask = channel->output_mask;
616 err = sync_source_dpll_tod_pps(channel->tod_n, &sync_src);
620 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
621 &temp, sizeof(temp));
625 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
626 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
629 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
630 &temp, sizeof(temp));
634 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
635 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
638 for (pll = 0; pll < 8; pll++) {
643 /* First 4 pll has 2 outputs */
644 qn = output_mask & 0x1;
645 output_mask = output_mask >> 1;
646 qn_plus_1 = output_mask & 0x1;
647 output_mask = output_mask >> 1;
648 } else if (pll == 4) {
650 qn = output_mask & 0x1;
651 output_mask = output_mask >> 1;
653 } else if (pll == 5) {
655 qn_plus_1 = output_mask & 0x1;
656 output_mask = output_mask >> 1;
658 qn = output_mask & 0x1;
659 output_mask = output_mask >> 1;
660 } else if (pll == 6) {
661 qn = output_mask & 0x1;
662 output_mask = output_mask >> 1;
664 qn_plus_1 = output_mask & 0x1;
665 output_mask = output_mask >> 1;
667 } else if (pll == 7) {
668 if (out11_mux == 0) {
669 qn = output_mask & 0x1;
670 output_mask = output_mask >> 1;
674 if ((qn != 0) || (qn_plus_1 != 0))
675 err = _sync_pll_output(idtcm, pll, sync_src, qn,
685 static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
686 struct timespec64 const *ts,
687 enum hw_tod_write_trig_sel wr_trig)
689 struct idtcm *idtcm = channel->idtcm;
691 u8 buf[TOD_BYTE_COUNT];
694 struct timespec64 local_ts = *ts;
695 s64 total_overhead_ns;
697 /* Configure HW TOD write trigger. */
698 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
705 cmd |= wr_trig | 0x08;
707 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
713 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
715 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
720 err = idtcm_write(idtcm, channel->hw_dpll_n,
721 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
727 /* ARM HW TOD write trigger. */
730 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
733 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
735 if (idtcm->calculate_overhead_flag) {
736 /* Assumption: I2C @ 400KHz */
737 ktime_t diff = ktime_sub(ktime_get_raw(),
739 total_overhead_ns = ktime_to_ns(diff)
740 + idtcm->tod_write_overhead_ns
741 + SETTIME_CORRECTION;
743 timespec64_add_ns(&local_ts, total_overhead_ns);
745 idtcm->calculate_overhead_flag = 0;
748 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
753 err = idtcm_write(idtcm, channel->hw_dpll_n,
754 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
760 static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
761 struct timespec64 const *ts,
762 enum scsr_tod_write_trig_sel wr_trig,
763 enum scsr_tod_write_type_sel wr_type)
765 struct idtcm *idtcm = channel->idtcm;
766 unsigned char buf[TOD_BYTE_COUNT], cmd;
767 struct timespec64 local_ts = *ts;
770 timespec64_add_ns(&local_ts, SETTIME_CORRECTION);
772 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
777 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
782 /* Trigger the write operation. */
783 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
788 cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
789 cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
790 cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
791 cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);
793 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
798 /* Wait for the operation to complete. */
800 /* pps trigger takes up to 1 sec to complete */
801 if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
804 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
809 if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
813 dev_err(&idtcm->client->dev,
814 "Timed out waiting for the write counter\n");
822 static int get_output_base_addr(u8 outn)
870 static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
871 struct timespec64 const *ts)
873 struct idtcm *idtcm = channel->idtcm;
876 err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
879 dev_err(&idtcm->client->dev,
880 "%s: Set HW ToD failed\n", __func__);
884 return idtcm_sync_pps_output(channel);
887 static int _idtcm_settime(struct idtcm_channel *channel,
888 struct timespec64 const *ts,
889 enum scsr_tod_write_type_sel wr_type)
891 return _idtcm_set_dpll_scsr_tod(channel, ts,
892 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
896 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
901 struct idtcm *idtcm = channel->idtcm;
905 for (i = 0; i < 4; i++) {
906 buf[i] = 0xff & (offset_ns);
910 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
916 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
921 struct idtcm *idtcm = channel->idtcm;
925 if (max_ffo_ppb & 0xff000000)
928 for (i = 0; i < 3; i++) {
929 buf[i] = 0xff & (max_ffo_ppb);
933 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
934 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
939 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
942 struct idtcm *idtcm = channel->idtcm;
946 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
954 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
955 PULL_IN_CTRL, &buf, sizeof(buf));
963 static int idtcm_do_phase_pull_in(struct idtcm_channel *channel,
969 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
974 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
979 err = idtcm_start_phase_pull_in(channel);
984 static int set_tod_write_overhead(struct idtcm_channel *channel)
986 struct idtcm *idtcm = channel->idtcm;
996 char buf[TOD_BYTE_COUNT] = {0};
998 /* Set page offset */
999 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
1002 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
1004 start = ktime_get_raw();
1006 err = idtcm_write(idtcm, channel->hw_dpll_n,
1007 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
1012 stop = ktime_get_raw();
1014 diff = ktime_sub(stop, start);
1016 current_ns = ktime_to_ns(diff);
1019 lowest_ns = current_ns;
1021 if (current_ns < lowest_ns)
1022 lowest_ns = current_ns;
1026 idtcm->tod_write_overhead_ns = lowest_ns;
1031 static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
1034 struct idtcm *idtcm = channel->idtcm;
1035 struct timespec64 ts;
1038 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
1039 err = idtcm_do_phase_pull_in(channel, delta, 0);
1041 idtcm->calculate_overhead_flag = 1;
1043 err = set_tod_write_overhead(channel);
1048 err = _idtcm_gettime(channel, &ts);
1053 now = timespec64_to_ns(&ts);
1056 ts = ns_to_timespec64(now);
1058 err = _idtcm_settime_deprecated(channel, &ts);
1064 static int idtcm_state_machine_reset(struct idtcm *idtcm)
1066 u8 byte = SM_RESET_CMD;
1071 clear_boot_status(idtcm);
1073 err = idtcm_write(idtcm, RESET_CTRL, SM_RESET, &byte, sizeof(byte));
1076 for (i = 0; i < 30; i++) {
1077 msleep_interruptible(100);
1078 read_boot_status(idtcm, &status);
1080 if (status == 0xA0) {
1081 dev_dbg(&idtcm->client->dev,
1082 "SM_RESET completed in %d ms\n",
1089 dev_err(&idtcm->client->dev, "Timed out waiting for CM_RESET to complete\n");
1095 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
1097 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1100 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
1105 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
1107 *product_id = (buf[1] << 8) | buf[0];
1112 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
1117 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
1124 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
1126 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
1129 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
1131 return idtcm_read(idtcm,
1138 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
1141 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
1142 config_select, sizeof(u8));
1145 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
1150 case TOD0_OUT_ALIGN_MASK_ADDR:
1151 SET_U16_LSB(idtcm->channel[0].output_mask, val);
1153 case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1154 SET_U16_MSB(idtcm->channel[0].output_mask, val);
1156 case TOD1_OUT_ALIGN_MASK_ADDR:
1157 SET_U16_LSB(idtcm->channel[1].output_mask, val);
1159 case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1160 SET_U16_MSB(idtcm->channel[1].output_mask, val);
1162 case TOD2_OUT_ALIGN_MASK_ADDR:
1163 SET_U16_LSB(idtcm->channel[2].output_mask, val);
1165 case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1166 SET_U16_MSB(idtcm->channel[2].output_mask, val);
1168 case TOD3_OUT_ALIGN_MASK_ADDR:
1169 SET_U16_LSB(idtcm->channel[3].output_mask, val);
1171 case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1172 SET_U16_MSB(idtcm->channel[3].output_mask, val);
1175 err = -EFAULT; /* Bad address */;
1182 static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
1184 if (index >= MAX_TOD) {
1185 dev_err(&idtcm->client->dev, "ToD%d not supported\n", index);
1189 if (pll >= MAX_PLL) {
1190 dev_err(&idtcm->client->dev, "Pll%d not supported\n", pll);
1194 idtcm->channel[index].pll = pll;
1199 static int check_and_set_masks(struct idtcm *idtcm,
1207 if ((val & 0xf0) || !(val & 0x0f)) {
1208 dev_err(&idtcm->client->dev,
1209 "Invalid TOD mask 0x%hhx\n", val);
1212 idtcm->tod_mask = val;
1215 case TOD0_PTP_PLL_ADDR:
1216 err = set_tod_ptp_pll(idtcm, 0, val);
1218 case TOD1_PTP_PLL_ADDR:
1219 err = set_tod_ptp_pll(idtcm, 1, val);
1221 case TOD2_PTP_PLL_ADDR:
1222 err = set_tod_ptp_pll(idtcm, 2, val);
1224 case TOD3_PTP_PLL_ADDR:
1225 err = set_tod_ptp_pll(idtcm, 3, val);
1228 err = set_pll_output_mask(idtcm, regaddr, val);
1235 static void display_pll_and_masks(struct idtcm *idtcm)
1240 dev_dbg(&idtcm->client->dev, "tod_mask = 0x%02x\n", idtcm->tod_mask);
1242 for (i = 0; i < MAX_TOD; i++) {
1245 if (mask & idtcm->tod_mask)
1246 dev_dbg(&idtcm->client->dev,
1247 "TOD%d pll = %d output_mask = 0x%04x\n",
1248 i, idtcm->channel[i].pll,
1249 idtcm->channel[i].output_mask);
1253 static int idtcm_load_firmware(struct idtcm *idtcm,
1256 char fname[128] = FW_FILENAME;
1257 const struct firmware *fw;
1258 struct idtcm_fwrc *rec;
1265 if (firmware) /* module parameter */
1266 snprintf(fname, sizeof(fname), "%s", firmware);
1268 dev_dbg(&idtcm->client->dev, "requesting firmware '%s'\n", fname);
1270 err = request_firmware(&fw, fname, dev);
1273 dev_err(&idtcm->client->dev,
1274 "Failed at line %d in func %s!\n",
1280 dev_dbg(&idtcm->client->dev, "firmware size %zu bytes\n", fw->size);
1282 rec = (struct idtcm_fwrc *) fw->data;
1284 if (contains_full_configuration(fw))
1285 idtcm_state_machine_reset(idtcm);
1287 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1289 if (rec->reserved) {
1290 dev_err(&idtcm->client->dev,
1291 "bad firmware, reserved field non-zero\n");
1294 regaddr = rec->hiaddr << 8;
1295 regaddr |= rec->loaddr;
1298 loaddr = rec->loaddr;
1302 err = check_and_set_masks(idtcm, regaddr, val);
1305 if (err != -EINVAL) {
1308 /* Top (status registers) and bottom are read-only */
1309 if ((regaddr < GPIO_USER_CONTROL)
1310 || (regaddr >= SCRATCH))
1313 /* Page size 128, last 4 bytes of page skipped */
1314 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
1318 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
1325 display_pll_and_masks(idtcm);
1328 release_firmware(fw);
1332 static int idtcm_output_enable(struct idtcm_channel *channel,
1333 bool enable, unsigned int outn)
1335 struct idtcm *idtcm = channel->idtcm;
1340 base = get_output_base_addr(outn);
1343 dev_err(&idtcm->client->dev,
1344 "%s - Unsupported out%d", __func__, outn);
1348 err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1354 val |= SQUELCH_DISABLE;
1356 val &= ~SQUELCH_DISABLE;
1358 return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1361 static int idtcm_output_mask_enable(struct idtcm_channel *channel,
1368 mask = channel->output_mask;
1375 err = idtcm_output_enable(channel, enable, outn);
1388 static int idtcm_perout_enable(struct idtcm_channel *channel,
1390 struct ptp_perout_request *perout)
1392 unsigned int flags = perout->flags;
1394 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
1395 return idtcm_output_mask_enable(channel, enable);
1397 /* Enable/disable individual output instead */
1398 return idtcm_output_enable(channel, enable, perout->index);
1401 static int idtcm_get_pll_mode(struct idtcm_channel *channel,
1402 enum pll_mode *pll_mode)
1404 struct idtcm *idtcm = channel->idtcm;
1408 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1409 &dpll_mode, sizeof(dpll_mode));
1413 *pll_mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;
1418 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
1419 enum pll_mode pll_mode)
1421 struct idtcm *idtcm = channel->idtcm;
1425 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1426 &dpll_mode, sizeof(dpll_mode));
1430 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
1432 dpll_mode |= (pll_mode << PLL_MODE_SHIFT);
1434 channel->pll_mode = pll_mode;
1436 err = idtcm_write(idtcm, channel->dpll_n, DPLL_MODE,
1437 &dpll_mode, sizeof(dpll_mode));
1444 /* PTP Hardware Clock interface */
1447 * @brief Maximum absolute value for write phase offset in picoseconds
1449 * Destination signed register is 32-bit register in resolution of 50ps
1451 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1453 static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
1455 struct idtcm *idtcm = channel->idtcm;
1463 if (channel->pll_mode != PLL_MODE_WRITE_PHASE) {
1465 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);
1471 offset_ps = (s64)delta_ns * 1000;
1474 * Check for 32-bit signed max * 50:
1476 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1478 if (offset_ps > MAX_ABS_WRITE_PHASE_PICOSECONDS)
1479 offset_ps = MAX_ABS_WRITE_PHASE_PICOSECONDS;
1480 else if (offset_ps < -MAX_ABS_WRITE_PHASE_PICOSECONDS)
1481 offset_ps = -MAX_ABS_WRITE_PHASE_PICOSECONDS;
1483 phase_50ps = div_s64(offset_ps, 50);
1485 for (i = 0; i < 4; i++) {
1486 buf[i] = phase_50ps & 0xff;
1490 err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
1496 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1498 struct idtcm *idtcm = channel->idtcm;
1504 if (channel->pll_mode != PLL_MODE_WRITE_FREQUENCY) {
1505 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1511 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
1520 * FCW = -------------
1524 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1525 fcw = scaled_ppm * 244140625ULL;
1527 fcw = div_s64(fcw, 1776);
1529 for (i = 0; i < 6; i++) {
1530 buf[i] = fcw & 0xff;
1534 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
1540 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1542 struct idtcm_channel *channel =
1543 container_of(ptp, struct idtcm_channel, caps);
1544 struct idtcm *idtcm = channel->idtcm;
1547 mutex_lock(&idtcm->reg_lock);
1549 err = _idtcm_gettime(channel, ts);
1552 dev_err(&idtcm->client->dev,
1553 "Failed at line %d in func %s!\n",
1557 mutex_unlock(&idtcm->reg_lock);
1562 static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
1563 const struct timespec64 *ts)
1565 struct idtcm_channel *channel =
1566 container_of(ptp, struct idtcm_channel, caps);
1567 struct idtcm *idtcm = channel->idtcm;
1570 mutex_lock(&idtcm->reg_lock);
1572 err = _idtcm_settime_deprecated(channel, ts);
1575 dev_err(&idtcm->client->dev,
1576 "Failed at line %d in func %s!\n",
1580 mutex_unlock(&idtcm->reg_lock);
1585 static int idtcm_settime(struct ptp_clock_info *ptp,
1586 const struct timespec64 *ts)
1588 struct idtcm_channel *channel =
1589 container_of(ptp, struct idtcm_channel, caps);
1590 struct idtcm *idtcm = channel->idtcm;
1593 mutex_lock(&idtcm->reg_lock);
1595 err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1598 dev_err(&idtcm->client->dev,
1599 "Failed at line %d in func %s!\n",
1603 mutex_unlock(&idtcm->reg_lock);
1608 static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1610 struct idtcm_channel *channel =
1611 container_of(ptp, struct idtcm_channel, caps);
1612 struct idtcm *idtcm = channel->idtcm;
1615 mutex_lock(&idtcm->reg_lock);
1617 err = _idtcm_adjtime_deprecated(channel, delta);
1620 dev_err(&idtcm->client->dev,
1621 "Failed at line %d in func %s!\n",
1625 mutex_unlock(&idtcm->reg_lock);
1630 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1632 struct idtcm_channel *channel =
1633 container_of(ptp, struct idtcm_channel, caps);
1634 struct idtcm *idtcm = channel->idtcm;
1635 struct timespec64 ts;
1636 enum scsr_tod_write_type_sel type;
1639 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1640 err = idtcm_do_phase_pull_in(channel, delta, 0);
1642 dev_err(&idtcm->client->dev,
1643 "Failed at line %d in func %s!\n",
1650 ts = ns_to_timespec64(delta);
1651 type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
1653 ts = ns_to_timespec64(-delta);
1654 type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
1657 mutex_lock(&idtcm->reg_lock);
1659 err = _idtcm_settime(channel, &ts, type);
1662 dev_err(&idtcm->client->dev,
1663 "Failed at line %d in func %s!\n",
1667 mutex_unlock(&idtcm->reg_lock);
1672 static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1674 struct idtcm_channel *channel =
1675 container_of(ptp, struct idtcm_channel, caps);
1677 struct idtcm *idtcm = channel->idtcm;
1681 mutex_lock(&idtcm->reg_lock);
1683 err = _idtcm_adjphase(channel, delta);
1686 dev_err(&idtcm->client->dev,
1687 "Failed at line %d in func %s!\n",
1691 mutex_unlock(&idtcm->reg_lock);
1696 static int idtcm_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1698 struct idtcm_channel *channel =
1699 container_of(ptp, struct idtcm_channel, caps);
1701 struct idtcm *idtcm = channel->idtcm;
1705 mutex_lock(&idtcm->reg_lock);
1707 err = _idtcm_adjfine(channel, scaled_ppm);
1710 dev_err(&idtcm->client->dev,
1711 "Failed at line %d in func %s!\n",
1715 mutex_unlock(&idtcm->reg_lock);
1720 static int idtcm_enable(struct ptp_clock_info *ptp,
1721 struct ptp_clock_request *rq, int on)
1725 struct idtcm_channel *channel =
1726 container_of(ptp, struct idtcm_channel, caps);
1729 case PTP_CLK_REQ_PEROUT:
1731 err = idtcm_perout_enable(channel, false, &rq->perout);
1733 dev_err(&channel->idtcm->client->dev,
1734 "Failed at line %d in func %s!\n",
1740 /* Only accept a 1-PPS aligned to the second. */
1741 if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
1742 rq->perout.period.nsec)
1745 err = idtcm_perout_enable(channel, true, &rq->perout);
1747 dev_err(&channel->idtcm->client->dev,
1748 "Failed at line %d in func %s!\n",
1759 static int _enable_pll_tod_sync(struct idtcm *idtcm,
1768 u16 out0 = 0, out1 = 0;
1770 if ((qn == 0) && (qn_plus_1 == 0))
1831 * Enable OUTPUT OUT_SYNC.
1834 err = idtcm_read(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1839 val &= ~OUT_SYNC_DISABLE;
1841 err = idtcm_write(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1848 err = idtcm_read(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1853 val &= ~OUT_SYNC_DISABLE;
1855 err = idtcm_write(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1861 /* enable dpll sync tod pps, must be set before dpll_mode */
1862 err = idtcm_read(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1866 val &= ~(TOD_SYNC_SOURCE_MASK << TOD_SYNC_SOURCE_SHIFT);
1867 val |= (sync_src << TOD_SYNC_SOURCE_SHIFT);
1870 return idtcm_write(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1873 static int idtcm_enable_tod_sync(struct idtcm_channel *channel)
1875 struct idtcm *idtcm = channel->idtcm;
1883 u16 output_mask = channel->output_mask;
1889 * set tod_out_sync_enable to 0.
1891 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1895 cfg &= ~TOD_OUT_SYNC_ENABLE;
1897 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1901 switch (channel->tod_n) {
1918 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
1919 &temp, sizeof(temp));
1923 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1924 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1927 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
1928 &temp, sizeof(temp));
1932 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1933 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1936 for (pll = 0; pll < 8; pll++) {
1941 /* First 4 pll has 2 outputs */
1942 qn = output_mask & 0x1;
1943 output_mask = output_mask >> 1;
1944 qn_plus_1 = output_mask & 0x1;
1945 output_mask = output_mask >> 1;
1946 } else if (pll == 4) {
1947 if (out8_mux == 0) {
1948 qn = output_mask & 0x1;
1949 output_mask = output_mask >> 1;
1951 } else if (pll == 5) {
1953 qn_plus_1 = output_mask & 0x1;
1954 output_mask = output_mask >> 1;
1956 qn = output_mask & 0x1;
1957 output_mask = output_mask >> 1;
1958 } else if (pll == 6) {
1959 qn = output_mask & 0x1;
1960 output_mask = output_mask >> 1;
1962 qn_plus_1 = output_mask & 0x1;
1963 output_mask = output_mask >> 1;
1965 } else if (pll == 7) {
1966 if (out11_mux == 0) {
1967 qn = output_mask & 0x1;
1968 output_mask = output_mask >> 1;
1972 if ((qn != 0) || (qn_plus_1 != 0))
1973 err = _enable_pll_tod_sync(idtcm, pll, sync_src, qn,
1983 static int idtcm_enable_tod(struct idtcm_channel *channel)
1985 struct idtcm *idtcm = channel->idtcm;
1986 struct timespec64 ts = {0, 0};
1991 * Start the TOD clock ticking.
1993 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1999 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
2003 if (idtcm->deprecated)
2004 return _idtcm_settime_deprecated(channel, &ts);
2006 return _idtcm_settime(channel, &ts,
2007 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
2010 static void idtcm_set_version_info(struct idtcm *idtcm)
2018 char *fmt = "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d\n";
2020 idtcm_read_major_release(idtcm, &major);
2021 idtcm_read_minor_release(idtcm, &minor);
2022 idtcm_read_hotfix_release(idtcm, &hotfix);
2024 idtcm_read_product_id(idtcm, &product_id);
2025 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
2027 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
2029 snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
2030 major, minor, hotfix);
2032 if (idtcm_strverscmp(idtcm->version, "4.8.7") >= 0)
2033 idtcm->deprecated = 0;
2035 idtcm->deprecated = 1;
2037 dev_info(&idtcm->client->dev, fmt, major, minor, hotfix,
2038 product_id, hw_rev_id, config_select);
2041 static const struct ptp_clock_info idtcm_caps = {
2042 .owner = THIS_MODULE,
2045 .adjphase = &idtcm_adjphase,
2046 .adjfine = &idtcm_adjfine,
2047 .adjtime = &idtcm_adjtime,
2048 .gettime64 = &idtcm_gettime,
2049 .settime64 = &idtcm_settime,
2050 .enable = &idtcm_enable,
2053 static const struct ptp_clock_info idtcm_caps_deprecated = {
2054 .owner = THIS_MODULE,
2057 .adjphase = &idtcm_adjphase,
2058 .adjfine = &idtcm_adjfine,
2059 .adjtime = &idtcm_adjtime_deprecated,
2060 .gettime64 = &idtcm_gettime,
2061 .settime64 = &idtcm_settime_deprecated,
2062 .enable = &idtcm_enable,
2065 static int configure_channel_pll(struct idtcm_channel *channel)
2069 switch (channel->pll) {
2071 channel->dpll_freq = DPLL_FREQ_0;
2072 channel->dpll_n = DPLL_0;
2073 channel->hw_dpll_n = HW_DPLL_0;
2074 channel->dpll_phase = DPLL_PHASE_0;
2075 channel->dpll_ctrl_n = DPLL_CTRL_0;
2076 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
2079 channel->dpll_freq = DPLL_FREQ_1;
2080 channel->dpll_n = DPLL_1;
2081 channel->hw_dpll_n = HW_DPLL_1;
2082 channel->dpll_phase = DPLL_PHASE_1;
2083 channel->dpll_ctrl_n = DPLL_CTRL_1;
2084 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
2087 channel->dpll_freq = DPLL_FREQ_2;
2088 channel->dpll_n = DPLL_2;
2089 channel->hw_dpll_n = HW_DPLL_2;
2090 channel->dpll_phase = DPLL_PHASE_2;
2091 channel->dpll_ctrl_n = DPLL_CTRL_2;
2092 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
2095 channel->dpll_freq = DPLL_FREQ_3;
2096 channel->dpll_n = DPLL_3;
2097 channel->hw_dpll_n = HW_DPLL_3;
2098 channel->dpll_phase = DPLL_PHASE_3;
2099 channel->dpll_ctrl_n = DPLL_CTRL_3;
2100 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
2103 channel->dpll_freq = DPLL_FREQ_4;
2104 channel->dpll_n = DPLL_4;
2105 channel->hw_dpll_n = HW_DPLL_4;
2106 channel->dpll_phase = DPLL_PHASE_4;
2107 channel->dpll_ctrl_n = DPLL_CTRL_4;
2108 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
2111 channel->dpll_freq = DPLL_FREQ_5;
2112 channel->dpll_n = DPLL_5;
2113 channel->hw_dpll_n = HW_DPLL_5;
2114 channel->dpll_phase = DPLL_PHASE_5;
2115 channel->dpll_ctrl_n = DPLL_CTRL_5;
2116 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
2119 channel->dpll_freq = DPLL_FREQ_6;
2120 channel->dpll_n = DPLL_6;
2121 channel->hw_dpll_n = HW_DPLL_6;
2122 channel->dpll_phase = DPLL_PHASE_6;
2123 channel->dpll_ctrl_n = DPLL_CTRL_6;
2124 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
2127 channel->dpll_freq = DPLL_FREQ_7;
2128 channel->dpll_n = DPLL_7;
2129 channel->hw_dpll_n = HW_DPLL_7;
2130 channel->dpll_phase = DPLL_PHASE_7;
2131 channel->dpll_ctrl_n = DPLL_CTRL_7;
2132 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
2141 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
2143 struct idtcm_channel *channel;
2146 if (!(index < MAX_TOD))
2149 channel = &idtcm->channel[index];
2151 /* Set pll addresses */
2152 err = configure_channel_pll(channel);
2156 /* Set tod addresses */
2159 channel->tod_read_primary = TOD_READ_PRIMARY_0;
2160 channel->tod_write = TOD_WRITE_0;
2161 channel->tod_n = TOD_0;
2164 channel->tod_read_primary = TOD_READ_PRIMARY_1;
2165 channel->tod_write = TOD_WRITE_1;
2166 channel->tod_n = TOD_1;
2169 channel->tod_read_primary = TOD_READ_PRIMARY_2;
2170 channel->tod_write = TOD_WRITE_2;
2171 channel->tod_n = TOD_2;
2174 channel->tod_read_primary = TOD_READ_PRIMARY_3;
2175 channel->tod_write = TOD_WRITE_3;
2176 channel->tod_n = TOD_3;
2182 channel->idtcm = idtcm;
2184 if (idtcm->deprecated)
2185 channel->caps = idtcm_caps_deprecated;
2187 channel->caps = idtcm_caps;
2189 snprintf(channel->caps.name, sizeof(channel->caps.name),
2190 "IDT CM TOD%u", index);
2192 if (!idtcm->deprecated) {
2193 err = idtcm_enable_tod_sync(channel);
2195 dev_err(&idtcm->client->dev,
2196 "Failed at line %d in func %s!\n",
2203 /* Sync pll mode with hardware */
2204 err = idtcm_get_pll_mode(channel, &channel->pll_mode);
2206 dev_err(&idtcm->client->dev,
2207 "Error: %s - Unable to read pll mode\n", __func__);
2211 err = idtcm_enable_tod(channel);
2213 dev_err(&idtcm->client->dev,
2214 "Failed at line %d in func %s!\n",
2220 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
2222 if (IS_ERR(channel->ptp_clock)) {
2223 err = PTR_ERR(channel->ptp_clock);
2224 channel->ptp_clock = NULL;
2228 if (!channel->ptp_clock)
2231 dev_info(&idtcm->client->dev, "PLL%d registered as ptp%d\n",
2232 index, channel->ptp_clock->index);
2237 static void ptp_clock_unregister_all(struct idtcm *idtcm)
2240 struct idtcm_channel *channel;
2242 for (i = 0; i < MAX_TOD; i++) {
2244 channel = &idtcm->channel[i];
2246 if (channel->ptp_clock)
2247 ptp_clock_unregister(channel->ptp_clock);
2251 static void set_default_masks(struct idtcm *idtcm)
2253 idtcm->tod_mask = DEFAULT_TOD_MASK;
2255 idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
2256 idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
2257 idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
2258 idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2260 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
2261 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
2262 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
2263 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
2266 static int idtcm_probe(struct i2c_client *client,
2267 const struct i2c_device_id *id)
2269 struct idtcm *idtcm;
2272 char *fmt = "Failed at %d in line %s with channel output %d!\n";
2274 /* Unused for now */
2277 idtcm = devm_kzalloc(&client->dev, sizeof(struct idtcm), GFP_KERNEL);
2282 idtcm->client = client;
2283 idtcm->page_offset = 0xff;
2284 idtcm->calculate_overhead_flag = 0;
2286 set_default_masks(idtcm);
2288 mutex_init(&idtcm->reg_lock);
2289 mutex_lock(&idtcm->reg_lock);
2291 idtcm_set_version_info(idtcm);
2293 err = idtcm_load_firmware(idtcm, &client->dev);
2296 dev_warn(&idtcm->client->dev,
2297 "loading firmware failed with %d\n", err);
2299 wait_for_chip_ready(idtcm);
2301 if (idtcm->tod_mask) {
2302 for (i = 0; i < MAX_TOD; i++) {
2303 if (idtcm->tod_mask & (1 << i)) {
2304 err = idtcm_enable_channel(idtcm, i);
2306 dev_err(&idtcm->client->dev,
2316 dev_err(&idtcm->client->dev,
2317 "no PLLs flagged as PHCs, nothing to do\n");
2321 mutex_unlock(&idtcm->reg_lock);
2324 ptp_clock_unregister_all(idtcm);
2328 i2c_set_clientdata(client, idtcm);
2333 static int idtcm_remove(struct i2c_client *client)
2335 struct idtcm *idtcm = i2c_get_clientdata(client);
2337 ptp_clock_unregister_all(idtcm);
2339 mutex_destroy(&idtcm->reg_lock);
2345 static const struct of_device_id idtcm_dt_id[] = {
2346 { .compatible = "idt,8a34000" },
2347 { .compatible = "idt,8a34001" },
2348 { .compatible = "idt,8a34002" },
2349 { .compatible = "idt,8a34003" },
2350 { .compatible = "idt,8a34004" },
2351 { .compatible = "idt,8a34005" },
2352 { .compatible = "idt,8a34006" },
2353 { .compatible = "idt,8a34007" },
2354 { .compatible = "idt,8a34008" },
2355 { .compatible = "idt,8a34009" },
2356 { .compatible = "idt,8a34010" },
2357 { .compatible = "idt,8a34011" },
2358 { .compatible = "idt,8a34012" },
2359 { .compatible = "idt,8a34013" },
2360 { .compatible = "idt,8a34014" },
2361 { .compatible = "idt,8a34015" },
2362 { .compatible = "idt,8a34016" },
2363 { .compatible = "idt,8a34017" },
2364 { .compatible = "idt,8a34018" },
2365 { .compatible = "idt,8a34019" },
2366 { .compatible = "idt,8a34040" },
2367 { .compatible = "idt,8a34041" },
2368 { .compatible = "idt,8a34042" },
2369 { .compatible = "idt,8a34043" },
2370 { .compatible = "idt,8a34044" },
2371 { .compatible = "idt,8a34045" },
2372 { .compatible = "idt,8a34046" },
2373 { .compatible = "idt,8a34047" },
2374 { .compatible = "idt,8a34048" },
2375 { .compatible = "idt,8a34049" },
2378 MODULE_DEVICE_TABLE(of, idtcm_dt_id);
2381 static const struct i2c_device_id idtcm_i2c_id[] = {
2414 MODULE_DEVICE_TABLE(i2c, idtcm_i2c_id);
2416 static struct i2c_driver idtcm_driver = {
2418 .of_match_table = of_match_ptr(idtcm_dt_id),
2421 .probe = idtcm_probe,
2422 .remove = idtcm_remove,
2423 .id_table = idtcm_i2c_id,
2426 module_i2c_driver(idtcm_driver);