Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / ptp / ptp_clockmatrix.c
blob051511f5e1f28e32e093baf0002db3fe1bc68497
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
7 */
8 #include <linux/firmware.h>
9 #include <linux/i2c.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;
40 s32 count = 0;
41 u16 regaddr;
42 u8 loaddr;
43 s32 len;
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;
55 loaddr = rec->loaddr;
57 rec++;
59 /* Top (status registers) and bottom are read-only */
60 if (regaddr < GPIO_USER_CONTROL || regaddr >= SCRATCH)
61 continue;
63 /* Page size 128, last 4 bytes of page skipped */
64 if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
65 continue;
67 count++;
70 return (count >= full_count);
73 static int char_array_to_timespec(u8 *buf,
74 u8 count,
75 struct timespec64 *ts)
77 u8 i;
78 u64 nsec;
79 time64_t sec;
81 if (count < TOD_BYTE_COUNT)
82 return 1;
84 /* Sub-nanoseconds are in buf[0]. */
85 nsec = buf[4];
86 for (i = 0; i < 3; i++) {
87 nsec <<= 8;
88 nsec |= buf[3 - i];
91 sec = buf[10];
92 for (i = 0; i < 5; i++) {
93 sec <<= 8;
94 sec |= buf[9 - i];
97 ts->tv_sec = sec;
98 ts->tv_nsec = nsec;
100 return 0;
103 static int timespec_to_char_array(struct timespec64 const *ts,
104 u8 *buf,
105 u8 count)
107 u8 i;
108 s32 nsec;
109 time64_t sec;
111 if (count < TOD_BYTE_COUNT)
112 return 1;
114 nsec = ts->tv_nsec;
115 sec = ts->tv_sec;
117 /* Sub-nanoseconds are in buf[0]. */
118 buf[0] = 0;
119 for (i = 1; i < 5; i++) {
120 buf[i] = nsec & 0xff;
121 nsec >>= 8;
124 for (i = 5; i < TOD_BYTE_COUNT; i++) {
126 buf[i] = sec & 0xff;
127 sec >>= 8;
130 return 0;
133 static int idtcm_strverscmp(const char *version1, const char *version2)
135 u8 ver1[3], ver2[3];
136 int i;
138 if (sscanf(version1, "%hhu.%hhu.%hhu",
139 &ver1[0], &ver1[1], &ver1[2]) != 3)
140 return -1;
141 if (sscanf(version2, "%hhu.%hhu.%hhu",
142 &ver2[0], &ver2[1], &ver2[2]) != 3)
143 return -1;
145 for (i = 0; i < 3; i++) {
146 if (ver1[i] > ver2[i])
147 return 1;
148 if (ver1[i] < ver2[i])
149 return -1;
152 return 0;
155 static int idtcm_xfer_read(struct idtcm *idtcm,
156 u8 regaddr,
157 u8 *buf,
158 u16 count)
160 struct i2c_client *client = idtcm->client;
161 struct i2c_msg msg[2];
162 int cnt;
163 char *fmt = "i2c_transfer failed at %d in %s, at addr: %04X!\n";
165 msg[0].addr = client->addr;
166 msg[0].flags = 0;
167 msg[0].len = 1;
168 msg[0].buf = &regaddr;
170 msg[1].addr = client->addr;
171 msg[1].flags = I2C_M_RD;
172 msg[1].len = count;
173 msg[1].buf = buf;
175 cnt = i2c_transfer(client->adapter, msg, 2);
177 if (cnt < 0) {
178 dev_err(&client->dev,
179 fmt,
180 __LINE__,
181 __func__,
182 regaddr);
183 return cnt;
184 } else if (cnt != 2) {
185 dev_err(&client->dev,
186 "i2c_transfer sent only %d of %d messages\n", cnt, 2);
187 return -EIO;
190 return 0;
193 static int idtcm_xfer_write(struct idtcm *idtcm,
194 u8 regaddr,
195 u8 *buf,
196 u16 count)
198 struct i2c_client *client = idtcm->client;
199 /* we add 1 byte for device register */
200 u8 msg[IDTCM_MAX_WRITE_COUNT + 1];
201 int cnt;
202 char *fmt = "i2c_master_send failed at %d in %s, at addr: %04X!\n";
204 if (count > IDTCM_MAX_WRITE_COUNT)
205 return -EINVAL;
207 msg[0] = regaddr;
208 memcpy(&msg[1], buf, count);
210 cnt = i2c_master_send(client, msg, count + 1);
212 if (cnt < 0) {
213 dev_err(&client->dev,
214 fmt,
215 __LINE__,
216 __func__,
217 regaddr);
218 return cnt;
221 return 0;
224 static int idtcm_page_offset(struct idtcm *idtcm, u8 val)
226 u8 buf[4];
227 int err;
229 if (idtcm->page_offset == val)
230 return 0;
232 buf[0] = 0x0;
233 buf[1] = val;
234 buf[2] = 0x10;
235 buf[3] = 0x20;
237 err = idtcm_xfer_write(idtcm, PAGE_ADDR, buf, sizeof(buf));
239 if (err) {
240 idtcm->page_offset = 0xff;
241 dev_err(&idtcm->client->dev, "failed to set page offset\n");
242 } else {
243 idtcm->page_offset = val;
246 return err;
249 static int _idtcm_rdwr(struct idtcm *idtcm,
250 u16 regaddr,
251 u8 *buf,
252 u16 count,
253 bool write)
255 u8 hi;
256 u8 lo;
257 int err;
259 hi = (regaddr >> 8) & 0xff;
260 lo = regaddr & 0xff;
262 err = idtcm_page_offset(idtcm, hi);
264 if (err)
265 return err;
267 if (write)
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,
274 u16 module,
275 u16 regaddr,
276 u8 *buf,
277 u16 count)
279 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, false);
282 static int idtcm_write(struct idtcm *idtcm,
283 u16 module,
284 u16 regaddr,
285 u8 *buf,
286 u16 count)
288 return _idtcm_rdwr(idtcm, module + regaddr, buf, count, true);
291 static int clear_boot_status(struct idtcm *idtcm)
293 int err;
294 u8 buf[4] = {0};
296 err = idtcm_write(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
298 return err;
301 static int read_boot_status(struct idtcm *idtcm, u32 *status)
303 int err;
304 u8 buf[4] = {0};
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];
310 return err;
313 static int wait_for_boot_status_ready(struct idtcm *idtcm)
315 u32 status = 0;
316 u8 i = 30; /* 30 * 100ms = 3s */
317 int err;
319 do {
320 err = read_boot_status(idtcm, &status);
322 if (err)
323 return err;
325 if (status == 0xA0)
326 return 0;
328 msleep(100);
329 i--;
331 } while (i);
333 dev_warn(&idtcm->client->dev, "%s timed out\n", __func__);
335 return -EBUSY;
338 static int _idtcm_gettime(struct idtcm_channel *channel,
339 struct timespec64 *ts)
341 struct idtcm *idtcm = channel->idtcm;
342 u8 buf[TOD_BYTE_COUNT];
343 u8 timeout = 10;
344 u8 trigger;
345 int err;
347 err = idtcm_read(idtcm, channel->tod_read_primary,
348 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
349 if (err)
350 return err;
352 trigger &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
353 trigger |= (1 << TOD_READ_TRIGGER_SHIFT);
354 trigger &= ~TOD_READ_TRIGGER_MODE; /* single shot */
356 err = idtcm_write(idtcm, channel->tod_read_primary,
357 TOD_READ_PRIMARY_CMD, &trigger, sizeof(trigger));
358 if (err)
359 return err;
361 /* wait trigger to be 0 */
362 while (trigger & TOD_READ_TRIGGER_MASK) {
364 if (idtcm->calculate_overhead_flag)
365 idtcm->start_time = ktime_get_raw();
367 err = idtcm_read(idtcm, channel->tod_read_primary,
368 TOD_READ_PRIMARY_CMD, &trigger,
369 sizeof(trigger));
371 if (err)
372 return err;
374 if (--timeout == 0)
375 return -EIO;
378 err = idtcm_read(idtcm, channel->tod_read_primary,
379 TOD_READ_PRIMARY, buf, sizeof(buf));
381 if (err)
382 return err;
384 err = char_array_to_timespec(buf, sizeof(buf), ts);
386 return err;
389 static int _sync_pll_output(struct idtcm *idtcm,
390 u8 pll,
391 u8 sync_src,
392 u8 qn,
393 u8 qn_plus_1)
395 int err;
396 u8 val;
397 u16 sync_ctrl0;
398 u16 sync_ctrl1;
399 u8 temp;
401 if ((qn == 0) && (qn_plus_1 == 0))
402 return 0;
404 switch (pll) {
405 case 0:
406 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
407 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
408 break;
409 case 1:
410 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
411 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
412 break;
413 case 2:
414 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
415 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
416 break;
417 case 3:
418 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
419 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
420 break;
421 case 4:
422 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
423 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
424 break;
425 case 5:
426 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
427 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
428 break;
429 case 6:
430 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
431 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
432 break;
433 case 7:
434 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
435 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
436 break;
437 default:
438 return -EINVAL;
441 val = SYNCTRL1_MASTER_SYNC_RST;
443 /* Place master sync in reset */
444 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
445 if (err)
446 return err;
448 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
449 if (err)
450 return err;
452 /* Set sync trigger mask */
453 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
455 if (qn)
456 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
458 if (qn_plus_1)
459 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
461 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
462 if (err)
463 return err;
465 /* PLL5 can have OUT8 as second additional output. */
466 if ((pll == 5) && (qn_plus_1 != 0)) {
467 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
468 &temp, sizeof(temp));
469 if (err)
470 return err;
472 temp &= ~(Q9_TO_Q8_SYNC_TRIG);
474 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
475 &temp, sizeof(temp));
476 if (err)
477 return err;
479 temp |= Q9_TO_Q8_SYNC_TRIG;
481 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
482 &temp, sizeof(temp));
483 if (err)
484 return err;
487 /* PLL6 can have OUT11 as second additional output. */
488 if ((pll == 6) && (qn_plus_1 != 0)) {
489 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
490 &temp, sizeof(temp));
491 if (err)
492 return err;
494 temp &= ~(Q10_TO_Q11_SYNC_TRIG);
496 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
497 &temp, sizeof(temp));
498 if (err)
499 return err;
501 temp |= Q10_TO_Q11_SYNC_TRIG;
503 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
504 &temp, sizeof(temp));
505 if (err)
506 return err;
509 /* Place master sync out of reset */
510 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
511 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
513 return err;
516 static int sync_source_dpll_tod_pps(u16 tod_addr, u8 *sync_src)
518 int err = 0;
520 switch (tod_addr) {
521 case TOD_0:
522 *sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
523 break;
524 case TOD_1:
525 *sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
526 break;
527 case TOD_2:
528 *sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
529 break;
530 case TOD_3:
531 *sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
532 break;
533 default:
534 err = -EINVAL;
537 return err;
540 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
542 struct idtcm *idtcm = channel->idtcm;
544 u8 pll;
545 u8 sync_src;
546 u8 qn;
547 u8 qn_plus_1;
548 int err = 0;
549 u8 out8_mux = 0;
550 u8 out11_mux = 0;
551 u8 temp;
553 u16 output_mask = channel->output_mask;
555 err = sync_source_dpll_tod_pps(channel->tod_n, &sync_src);
556 if (err)
557 return err;
559 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
560 &temp, sizeof(temp));
561 if (err)
562 return err;
564 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
565 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
566 out8_mux = 1;
568 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
569 &temp, sizeof(temp));
570 if (err)
571 return err;
573 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
574 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
575 out11_mux = 1;
577 for (pll = 0; pll < 8; pll++) {
578 qn = 0;
579 qn_plus_1 = 0;
581 if (pll < 4) {
582 /* First 4 pll has 2 outputs */
583 qn = output_mask & 0x1;
584 output_mask = output_mask >> 1;
585 qn_plus_1 = output_mask & 0x1;
586 output_mask = output_mask >> 1;
587 } else if (pll == 4) {
588 if (out8_mux == 0) {
589 qn = output_mask & 0x1;
590 output_mask = output_mask >> 1;
592 } else if (pll == 5) {
593 if (out8_mux) {
594 qn_plus_1 = output_mask & 0x1;
595 output_mask = output_mask >> 1;
597 qn = output_mask & 0x1;
598 output_mask = output_mask >> 1;
599 } else if (pll == 6) {
600 qn = output_mask & 0x1;
601 output_mask = output_mask >> 1;
602 if (out11_mux) {
603 qn_plus_1 = output_mask & 0x1;
604 output_mask = output_mask >> 1;
606 } else if (pll == 7) {
607 if (out11_mux == 0) {
608 qn = output_mask & 0x1;
609 output_mask = output_mask >> 1;
613 if ((qn != 0) || (qn_plus_1 != 0))
614 err = _sync_pll_output(idtcm, pll, sync_src, qn,
615 qn_plus_1);
617 if (err)
618 return err;
621 return err;
624 static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
625 struct timespec64 const *ts,
626 enum hw_tod_write_trig_sel wr_trig)
628 struct idtcm *idtcm = channel->idtcm;
630 u8 buf[TOD_BYTE_COUNT];
631 u8 cmd;
632 int err;
633 struct timespec64 local_ts = *ts;
634 s64 total_overhead_ns;
636 /* Configure HW TOD write trigger. */
637 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
638 &cmd, sizeof(cmd));
640 if (err)
641 return err;
643 cmd &= ~(0x0f);
644 cmd |= wr_trig | 0x08;
646 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
647 &cmd, sizeof(cmd));
649 if (err)
650 return err;
652 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
654 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
656 if (err)
657 return err;
659 err = idtcm_write(idtcm, channel->hw_dpll_n,
660 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
662 if (err)
663 return err;
666 /* ARM HW TOD write trigger. */
667 cmd &= ~(0x08);
669 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
670 &cmd, sizeof(cmd));
672 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
674 if (idtcm->calculate_overhead_flag) {
675 /* Assumption: I2C @ 400KHz */
676 ktime_t diff = ktime_sub(ktime_get_raw(),
677 idtcm->start_time);
678 total_overhead_ns = ktime_to_ns(diff)
679 + idtcm->tod_write_overhead_ns
680 + SETTIME_CORRECTION;
682 timespec64_add_ns(&local_ts, total_overhead_ns);
684 idtcm->calculate_overhead_flag = 0;
687 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
689 if (err)
690 return err;
692 err = idtcm_write(idtcm, channel->hw_dpll_n,
693 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
696 return err;
699 static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
700 struct timespec64 const *ts,
701 enum scsr_tod_write_trig_sel wr_trig,
702 enum scsr_tod_write_type_sel wr_type)
704 struct idtcm *idtcm = channel->idtcm;
705 unsigned char buf[TOD_BYTE_COUNT], cmd;
706 struct timespec64 local_ts = *ts;
707 int err, count = 0;
709 timespec64_add_ns(&local_ts, SETTIME_CORRECTION);
711 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
713 if (err)
714 return err;
716 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
717 buf, sizeof(buf));
718 if (err)
719 return err;
721 /* Trigger the write operation. */
722 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
723 &cmd, sizeof(cmd));
724 if (err)
725 return err;
727 cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
728 cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
729 cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
730 cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);
732 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
733 &cmd, sizeof(cmd));
734 if (err)
735 return err;
737 /* Wait for the operation to complete. */
738 while (1) {
739 /* pps trigger takes up to 1 sec to complete */
740 if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
741 msleep(50);
743 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
744 &cmd, sizeof(cmd));
745 if (err)
746 return err;
748 if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
749 break;
751 if (++count > 20) {
752 dev_err(&idtcm->client->dev,
753 "Timed out waiting for the write counter\n");
754 return -EIO;
758 return 0;
761 static int get_output_base_addr(u8 outn)
763 int base;
765 switch (outn) {
766 case 0:
767 base = OUTPUT_0;
768 break;
769 case 1:
770 base = OUTPUT_1;
771 break;
772 case 2:
773 base = OUTPUT_2;
774 break;
775 case 3:
776 base = OUTPUT_3;
777 break;
778 case 4:
779 base = OUTPUT_4;
780 break;
781 case 5:
782 base = OUTPUT_5;
783 break;
784 case 6:
785 base = OUTPUT_6;
786 break;
787 case 7:
788 base = OUTPUT_7;
789 break;
790 case 8:
791 base = OUTPUT_8;
792 break;
793 case 9:
794 base = OUTPUT_9;
795 break;
796 case 10:
797 base = OUTPUT_10;
798 break;
799 case 11:
800 base = OUTPUT_11;
801 break;
802 default:
803 base = -EINVAL;
806 return base;
809 static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
810 struct timespec64 const *ts)
812 struct idtcm *idtcm = channel->idtcm;
813 int err;
815 err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
817 if (err) {
818 dev_err(&idtcm->client->dev,
819 "%s: Set HW ToD failed\n", __func__);
820 return err;
823 return idtcm_sync_pps_output(channel);
826 static int _idtcm_settime(struct idtcm_channel *channel,
827 struct timespec64 const *ts,
828 enum scsr_tod_write_type_sel wr_type)
830 return _idtcm_set_dpll_scsr_tod(channel, ts,
831 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
832 wr_type);
835 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
836 s32 offset_ns)
838 int err;
839 int i;
840 struct idtcm *idtcm = channel->idtcm;
842 u8 buf[4];
844 for (i = 0; i < 4; i++) {
845 buf[i] = 0xff & (offset_ns);
846 offset_ns >>= 8;
849 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
850 buf, sizeof(buf));
852 return err;
855 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
856 u32 max_ffo_ppb)
858 int err;
859 u8 i;
860 struct idtcm *idtcm = channel->idtcm;
862 u8 buf[3];
864 if (max_ffo_ppb & 0xff000000)
865 max_ffo_ppb = 0;
867 for (i = 0; i < 3; i++) {
868 buf[i] = 0xff & (max_ffo_ppb);
869 max_ffo_ppb >>= 8;
872 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
873 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
875 return err;
878 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
880 int err;
881 struct idtcm *idtcm = channel->idtcm;
883 u8 buf;
885 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
886 &buf, sizeof(buf));
888 if (err)
889 return err;
891 if (buf == 0) {
892 buf = 0x01;
893 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
894 PULL_IN_CTRL, &buf, sizeof(buf));
895 } else {
896 err = -EBUSY;
899 return err;
902 static int idtcm_do_phase_pull_in(struct idtcm_channel *channel,
903 s32 offset_ns,
904 u32 max_ffo_ppb)
906 int err;
908 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
910 if (err)
911 return err;
913 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
915 if (err)
916 return err;
918 err = idtcm_start_phase_pull_in(channel);
920 return err;
923 static int set_tod_write_overhead(struct idtcm_channel *channel)
925 struct idtcm *idtcm = channel->idtcm;
926 s64 current_ns = 0;
927 s64 lowest_ns = 0;
928 int err;
929 u8 i;
931 ktime_t start;
932 ktime_t stop;
933 ktime_t diff;
935 char buf[TOD_BYTE_COUNT] = {0};
937 /* Set page offset */
938 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
939 buf, sizeof(buf));
941 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
943 start = ktime_get_raw();
945 err = idtcm_write(idtcm, channel->hw_dpll_n,
946 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
948 if (err)
949 return err;
951 stop = ktime_get_raw();
953 diff = ktime_sub(stop, start);
955 current_ns = ktime_to_ns(diff);
957 if (i == 0) {
958 lowest_ns = current_ns;
959 } else {
960 if (current_ns < lowest_ns)
961 lowest_ns = current_ns;
965 idtcm->tod_write_overhead_ns = lowest_ns;
967 return err;
970 static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
972 int err;
973 struct idtcm *idtcm = channel->idtcm;
974 struct timespec64 ts;
975 s64 now;
977 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
978 err = idtcm_do_phase_pull_in(channel, delta, 0);
979 } else {
980 idtcm->calculate_overhead_flag = 1;
982 err = set_tod_write_overhead(channel);
984 if (err)
985 return err;
987 err = _idtcm_gettime(channel, &ts);
989 if (err)
990 return err;
992 now = timespec64_to_ns(&ts);
993 now += delta;
995 ts = ns_to_timespec64(now);
997 err = _idtcm_settime_deprecated(channel, &ts);
1000 return err;
1003 static int idtcm_state_machine_reset(struct idtcm *idtcm)
1005 u8 byte = SM_RESET_CMD;
1006 u32 status = 0;
1007 int err;
1008 u8 i;
1010 clear_boot_status(idtcm);
1012 err = idtcm_write(idtcm, RESET_CTRL, SM_RESET, &byte, sizeof(byte));
1014 if (!err) {
1015 for (i = 0; i < 30; i++) {
1016 msleep_interruptible(100);
1017 read_boot_status(idtcm, &status);
1019 if (status == 0xA0) {
1020 dev_dbg(&idtcm->client->dev,
1021 "SM_RESET completed in %d ms\n",
1022 i * 100);
1023 break;
1027 if (!status)
1028 dev_err(&idtcm->client->dev, "Timed out waiting for CM_RESET to complete\n");
1031 return err;
1034 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
1036 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1039 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
1041 int err;
1042 u8 buf[2] = {0};
1044 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
1046 *product_id = (buf[1] << 8) | buf[0];
1048 return err;
1051 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
1053 int err;
1054 u8 buf = 0;
1056 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
1058 *major = buf >> 1;
1060 return err;
1063 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
1065 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
1068 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
1070 return idtcm_read(idtcm,
1071 GENERAL_STATUS,
1072 HOTFIX_REL,
1073 hotfix,
1074 sizeof(u8));
1077 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
1078 u8 *config_select)
1080 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
1081 config_select, sizeof(u8));
1084 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
1086 int err = 0;
1088 switch (addr) {
1089 case TOD0_OUT_ALIGN_MASK_ADDR:
1090 SET_U16_LSB(idtcm->channel[0].output_mask, val);
1091 break;
1092 case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1093 SET_U16_MSB(idtcm->channel[0].output_mask, val);
1094 break;
1095 case TOD1_OUT_ALIGN_MASK_ADDR:
1096 SET_U16_LSB(idtcm->channel[1].output_mask, val);
1097 break;
1098 case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1099 SET_U16_MSB(idtcm->channel[1].output_mask, val);
1100 break;
1101 case TOD2_OUT_ALIGN_MASK_ADDR:
1102 SET_U16_LSB(idtcm->channel[2].output_mask, val);
1103 break;
1104 case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1105 SET_U16_MSB(idtcm->channel[2].output_mask, val);
1106 break;
1107 case TOD3_OUT_ALIGN_MASK_ADDR:
1108 SET_U16_LSB(idtcm->channel[3].output_mask, val);
1109 break;
1110 case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1111 SET_U16_MSB(idtcm->channel[3].output_mask, val);
1112 break;
1113 default:
1114 err = -EFAULT; /* Bad address */;
1115 break;
1118 return err;
1121 static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
1123 if (index >= MAX_TOD) {
1124 dev_err(&idtcm->client->dev, "ToD%d not supported\n", index);
1125 return -EINVAL;
1128 if (pll >= MAX_PLL) {
1129 dev_err(&idtcm->client->dev, "Pll%d not supported\n", pll);
1130 return -EINVAL;
1133 idtcm->channel[index].pll = pll;
1135 return 0;
1138 static int check_and_set_masks(struct idtcm *idtcm,
1139 u16 regaddr,
1140 u8 val)
1142 int err = 0;
1144 switch (regaddr) {
1145 case TOD_MASK_ADDR:
1146 if ((val & 0xf0) || !(val & 0x0f)) {
1147 dev_err(&idtcm->client->dev,
1148 "Invalid TOD mask 0x%hhx\n", val);
1149 err = -EINVAL;
1150 } else {
1151 idtcm->tod_mask = val;
1153 break;
1154 case TOD0_PTP_PLL_ADDR:
1155 err = set_tod_ptp_pll(idtcm, 0, val);
1156 break;
1157 case TOD1_PTP_PLL_ADDR:
1158 err = set_tod_ptp_pll(idtcm, 1, val);
1159 break;
1160 case TOD2_PTP_PLL_ADDR:
1161 err = set_tod_ptp_pll(idtcm, 2, val);
1162 break;
1163 case TOD3_PTP_PLL_ADDR:
1164 err = set_tod_ptp_pll(idtcm, 3, val);
1165 break;
1166 default:
1167 err = set_pll_output_mask(idtcm, regaddr, val);
1168 break;
1171 return err;
1174 static void display_pll_and_masks(struct idtcm *idtcm)
1176 u8 i;
1177 u8 mask;
1179 dev_dbg(&idtcm->client->dev, "tod_mask = 0x%02x\n", idtcm->tod_mask);
1181 for (i = 0; i < MAX_TOD; i++) {
1182 mask = 1 << i;
1184 if (mask & idtcm->tod_mask)
1185 dev_dbg(&idtcm->client->dev,
1186 "TOD%d pll = %d output_mask = 0x%04x\n",
1187 i, idtcm->channel[i].pll,
1188 idtcm->channel[i].output_mask);
1192 static int idtcm_load_firmware(struct idtcm *idtcm,
1193 struct device *dev)
1195 char fname[128] = FW_FILENAME;
1196 const struct firmware *fw;
1197 struct idtcm_fwrc *rec;
1198 u32 regaddr;
1199 int err;
1200 s32 len;
1201 u8 val;
1202 u8 loaddr;
1204 if (firmware) /* module parameter */
1205 snprintf(fname, sizeof(fname), "%s", firmware);
1207 dev_dbg(&idtcm->client->dev, "requesting firmware '%s'\n", fname);
1209 err = request_firmware(&fw, fname, dev);
1211 if (err) {
1212 dev_err(&idtcm->client->dev,
1213 "Failed at line %d in func %s!\n",
1214 __LINE__,
1215 __func__);
1216 return err;
1219 dev_dbg(&idtcm->client->dev, "firmware size %zu bytes\n", fw->size);
1221 rec = (struct idtcm_fwrc *) fw->data;
1223 if (contains_full_configuration(fw))
1224 idtcm_state_machine_reset(idtcm);
1226 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1228 if (rec->reserved) {
1229 dev_err(&idtcm->client->dev,
1230 "bad firmware, reserved field non-zero\n");
1231 err = -EINVAL;
1232 } else {
1233 regaddr = rec->hiaddr << 8;
1234 regaddr |= rec->loaddr;
1236 val = rec->value;
1237 loaddr = rec->loaddr;
1239 rec++;
1241 err = check_and_set_masks(idtcm, regaddr, val);
1244 if (err != -EINVAL) {
1245 err = 0;
1247 /* Top (status registers) and bottom are read-only */
1248 if ((regaddr < GPIO_USER_CONTROL)
1249 || (regaddr >= SCRATCH))
1250 continue;
1252 /* Page size 128, last 4 bytes of page skipped */
1253 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
1254 || loaddr > 0xfb)
1255 continue;
1257 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
1260 if (err)
1261 goto out;
1264 display_pll_and_masks(idtcm);
1266 out:
1267 release_firmware(fw);
1268 return err;
1271 static int idtcm_output_enable(struct idtcm_channel *channel,
1272 bool enable, unsigned int outn)
1274 struct idtcm *idtcm = channel->idtcm;
1275 int base;
1276 int err;
1277 u8 val;
1279 base = get_output_base_addr(outn);
1281 if (!(base > 0)) {
1282 dev_err(&idtcm->client->dev,
1283 "%s - Unsupported out%d", __func__, outn);
1284 return base;
1287 err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1289 if (err)
1290 return err;
1292 if (enable)
1293 val |= SQUELCH_DISABLE;
1294 else
1295 val &= ~SQUELCH_DISABLE;
1297 return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1300 static int idtcm_output_mask_enable(struct idtcm_channel *channel,
1301 bool enable)
1303 u16 mask;
1304 int err;
1305 u8 outn;
1307 mask = channel->output_mask;
1308 outn = 0;
1310 while (mask) {
1312 if (mask & 0x1) {
1314 err = idtcm_output_enable(channel, enable, outn);
1316 if (err)
1317 return err;
1320 mask >>= 0x1;
1321 outn++;
1324 return 0;
1327 static int idtcm_perout_enable(struct idtcm_channel *channel,
1328 bool enable,
1329 struct ptp_perout_request *perout)
1331 unsigned int flags = perout->flags;
1333 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
1334 return idtcm_output_mask_enable(channel, enable);
1336 /* Enable/disable individual output instead */
1337 return idtcm_output_enable(channel, enable, perout->index);
1340 static int idtcm_get_pll_mode(struct idtcm_channel *channel,
1341 enum pll_mode *pll_mode)
1343 struct idtcm *idtcm = channel->idtcm;
1344 int err;
1345 u8 dpll_mode;
1347 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1348 &dpll_mode, sizeof(dpll_mode));
1349 if (err)
1350 return err;
1352 *pll_mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;
1354 return 0;
1357 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
1358 enum pll_mode pll_mode)
1360 struct idtcm *idtcm = channel->idtcm;
1361 int err;
1362 u8 dpll_mode;
1364 err = idtcm_read(idtcm, channel->dpll_n, DPLL_MODE,
1365 &dpll_mode, sizeof(dpll_mode));
1366 if (err)
1367 return err;
1369 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
1371 dpll_mode |= (pll_mode << PLL_MODE_SHIFT);
1373 channel->pll_mode = pll_mode;
1375 err = idtcm_write(idtcm, channel->dpll_n, DPLL_MODE,
1376 &dpll_mode, sizeof(dpll_mode));
1377 if (err)
1378 return err;
1380 return 0;
1383 /* PTP Hardware Clock interface */
1386 * @brief Maximum absolute value for write phase offset in picoseconds
1388 * Destination signed register is 32-bit register in resolution of 50ps
1390 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1392 static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
1394 struct idtcm *idtcm = channel->idtcm;
1396 int err;
1397 u8 i;
1398 u8 buf[4] = {0};
1399 s32 phase_50ps;
1400 s64 offset_ps;
1402 if (channel->pll_mode != PLL_MODE_WRITE_PHASE) {
1404 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);
1406 if (err)
1407 return err;
1410 offset_ps = (s64)delta_ns * 1000;
1413 * Check for 32-bit signed max * 50:
1415 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1417 if (offset_ps > MAX_ABS_WRITE_PHASE_PICOSECONDS)
1418 offset_ps = MAX_ABS_WRITE_PHASE_PICOSECONDS;
1419 else if (offset_ps < -MAX_ABS_WRITE_PHASE_PICOSECONDS)
1420 offset_ps = -MAX_ABS_WRITE_PHASE_PICOSECONDS;
1422 phase_50ps = div_s64(offset_ps, 50);
1424 for (i = 0; i < 4; i++) {
1425 buf[i] = phase_50ps & 0xff;
1426 phase_50ps >>= 8;
1429 err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
1430 buf, sizeof(buf));
1432 return err;
1435 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1437 struct idtcm *idtcm = channel->idtcm;
1438 u8 i;
1439 int err;
1440 u8 buf[6] = {0};
1441 s64 fcw;
1443 if (channel->pll_mode != PLL_MODE_WRITE_FREQUENCY) {
1444 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1445 if (err)
1446 return err;
1450 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
1452 * adjfreq:
1453 * ppb * 10^9
1454 * FCW = ----------
1455 * 111
1457 * adjfine:
1458 * ppm_16 * 5^12
1459 * FCW = -------------
1460 * 111 * 2^4
1463 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1464 fcw = scaled_ppm * 244140625ULL;
1466 fcw = div_s64(fcw, 1776);
1468 for (i = 0; i < 6; i++) {
1469 buf[i] = fcw & 0xff;
1470 fcw >>= 8;
1473 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
1474 buf, sizeof(buf));
1476 return err;
1479 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1481 struct idtcm_channel *channel =
1482 container_of(ptp, struct idtcm_channel, caps);
1483 struct idtcm *idtcm = channel->idtcm;
1484 int err;
1486 mutex_lock(&idtcm->reg_lock);
1488 err = _idtcm_gettime(channel, ts);
1490 if (err)
1491 dev_err(&idtcm->client->dev,
1492 "Failed at line %d in func %s!\n",
1493 __LINE__,
1494 __func__);
1496 mutex_unlock(&idtcm->reg_lock);
1498 return err;
1501 static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
1502 const struct timespec64 *ts)
1504 struct idtcm_channel *channel =
1505 container_of(ptp, struct idtcm_channel, caps);
1506 struct idtcm *idtcm = channel->idtcm;
1507 int err;
1509 mutex_lock(&idtcm->reg_lock);
1511 err = _idtcm_settime_deprecated(channel, ts);
1513 if (err)
1514 dev_err(&idtcm->client->dev,
1515 "Failed at line %d in func %s!\n",
1516 __LINE__,
1517 __func__);
1519 mutex_unlock(&idtcm->reg_lock);
1521 return err;
1524 static int idtcm_settime(struct ptp_clock_info *ptp,
1525 const struct timespec64 *ts)
1527 struct idtcm_channel *channel =
1528 container_of(ptp, struct idtcm_channel, caps);
1529 struct idtcm *idtcm = channel->idtcm;
1530 int err;
1532 mutex_lock(&idtcm->reg_lock);
1534 err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1536 if (err)
1537 dev_err(&idtcm->client->dev,
1538 "Failed at line %d in func %s!\n",
1539 __LINE__,
1540 __func__);
1542 mutex_unlock(&idtcm->reg_lock);
1544 return err;
1547 static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1549 struct idtcm_channel *channel =
1550 container_of(ptp, struct idtcm_channel, caps);
1551 struct idtcm *idtcm = channel->idtcm;
1552 int err;
1554 mutex_lock(&idtcm->reg_lock);
1556 err = _idtcm_adjtime_deprecated(channel, delta);
1558 if (err)
1559 dev_err(&idtcm->client->dev,
1560 "Failed at line %d in func %s!\n",
1561 __LINE__,
1562 __func__);
1564 mutex_unlock(&idtcm->reg_lock);
1566 return err;
1569 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1571 struct idtcm_channel *channel =
1572 container_of(ptp, struct idtcm_channel, caps);
1573 struct idtcm *idtcm = channel->idtcm;
1574 struct timespec64 ts;
1575 enum scsr_tod_write_type_sel type;
1576 int err;
1578 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1579 err = idtcm_do_phase_pull_in(channel, delta, 0);
1580 if (err)
1581 dev_err(&idtcm->client->dev,
1582 "Failed at line %d in func %s!\n",
1583 __LINE__,
1584 __func__);
1585 return err;
1588 if (delta >= 0) {
1589 ts = ns_to_timespec64(delta);
1590 type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
1591 } else {
1592 ts = ns_to_timespec64(-delta);
1593 type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
1596 mutex_lock(&idtcm->reg_lock);
1598 err = _idtcm_settime(channel, &ts, type);
1600 if (err)
1601 dev_err(&idtcm->client->dev,
1602 "Failed at line %d in func %s!\n",
1603 __LINE__,
1604 __func__);
1606 mutex_unlock(&idtcm->reg_lock);
1608 return err;
1611 static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1613 struct idtcm_channel *channel =
1614 container_of(ptp, struct idtcm_channel, caps);
1616 struct idtcm *idtcm = channel->idtcm;
1618 int err;
1620 mutex_lock(&idtcm->reg_lock);
1622 err = _idtcm_adjphase(channel, delta);
1624 if (err)
1625 dev_err(&idtcm->client->dev,
1626 "Failed at line %d in func %s!\n",
1627 __LINE__,
1628 __func__);
1630 mutex_unlock(&idtcm->reg_lock);
1632 return err;
1635 static int idtcm_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1637 struct idtcm_channel *channel =
1638 container_of(ptp, struct idtcm_channel, caps);
1640 struct idtcm *idtcm = channel->idtcm;
1642 int err;
1644 mutex_lock(&idtcm->reg_lock);
1646 err = _idtcm_adjfine(channel, scaled_ppm);
1648 if (err)
1649 dev_err(&idtcm->client->dev,
1650 "Failed at line %d in func %s!\n",
1651 __LINE__,
1652 __func__);
1654 mutex_unlock(&idtcm->reg_lock);
1656 return err;
1659 static int idtcm_enable(struct ptp_clock_info *ptp,
1660 struct ptp_clock_request *rq, int on)
1662 int err;
1664 struct idtcm_channel *channel =
1665 container_of(ptp, struct idtcm_channel, caps);
1667 switch (rq->type) {
1668 case PTP_CLK_REQ_PEROUT:
1669 if (!on) {
1670 err = idtcm_perout_enable(channel, false, &rq->perout);
1671 if (err)
1672 dev_err(&channel->idtcm->client->dev,
1673 "Failed at line %d in func %s!\n",
1674 __LINE__,
1675 __func__);
1676 return err;
1679 /* Only accept a 1-PPS aligned to the second. */
1680 if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
1681 rq->perout.period.nsec)
1682 return -ERANGE;
1684 err = idtcm_perout_enable(channel, true, &rq->perout);
1685 if (err)
1686 dev_err(&channel->idtcm->client->dev,
1687 "Failed at line %d in func %s!\n",
1688 __LINE__,
1689 __func__);
1690 return err;
1691 default:
1692 break;
1695 return -EOPNOTSUPP;
1698 static int _enable_pll_tod_sync(struct idtcm *idtcm,
1699 u8 pll,
1700 u8 sync_src,
1701 u8 qn,
1702 u8 qn_plus_1)
1704 int err;
1705 u8 val;
1706 u16 dpll;
1707 u16 out0 = 0, out1 = 0;
1709 if ((qn == 0) && (qn_plus_1 == 0))
1710 return 0;
1712 switch (pll) {
1713 case 0:
1714 dpll = DPLL_0;
1715 if (qn)
1716 out0 = OUTPUT_0;
1717 if (qn_plus_1)
1718 out1 = OUTPUT_1;
1719 break;
1720 case 1:
1721 dpll = DPLL_1;
1722 if (qn)
1723 out0 = OUTPUT_2;
1724 if (qn_plus_1)
1725 out1 = OUTPUT_3;
1726 break;
1727 case 2:
1728 dpll = DPLL_2;
1729 if (qn)
1730 out0 = OUTPUT_4;
1731 if (qn_plus_1)
1732 out1 = OUTPUT_5;
1733 break;
1734 case 3:
1735 dpll = DPLL_3;
1736 if (qn)
1737 out0 = OUTPUT_6;
1738 if (qn_plus_1)
1739 out1 = OUTPUT_7;
1740 break;
1741 case 4:
1742 dpll = DPLL_4;
1743 if (qn)
1744 out0 = OUTPUT_8;
1745 break;
1746 case 5:
1747 dpll = DPLL_5;
1748 if (qn)
1749 out0 = OUTPUT_9;
1750 if (qn_plus_1)
1751 out1 = OUTPUT_8;
1752 break;
1753 case 6:
1754 dpll = DPLL_6;
1755 if (qn)
1756 out0 = OUTPUT_10;
1757 if (qn_plus_1)
1758 out1 = OUTPUT_11;
1759 break;
1760 case 7:
1761 dpll = DPLL_7;
1762 if (qn)
1763 out0 = OUTPUT_11;
1764 break;
1765 default:
1766 return -EINVAL;
1770 * Enable OUTPUT OUT_SYNC.
1772 if (out0) {
1773 err = idtcm_read(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1775 if (err)
1776 return err;
1778 val &= ~OUT_SYNC_DISABLE;
1780 err = idtcm_write(idtcm, out0, OUT_CTRL_1, &val, sizeof(val));
1782 if (err)
1783 return err;
1786 if (out1) {
1787 err = idtcm_read(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1789 if (err)
1790 return err;
1792 val &= ~OUT_SYNC_DISABLE;
1794 err = idtcm_write(idtcm, out1, OUT_CTRL_1, &val, sizeof(val));
1796 if (err)
1797 return err;
1800 /* enable dpll sync tod pps, must be set before dpll_mode */
1801 err = idtcm_read(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1802 if (err)
1803 return err;
1805 val &= ~(TOD_SYNC_SOURCE_MASK << TOD_SYNC_SOURCE_SHIFT);
1806 val |= (sync_src << TOD_SYNC_SOURCE_SHIFT);
1807 val |= TOD_SYNC_EN;
1809 return idtcm_write(idtcm, dpll, DPLL_TOD_SYNC_CFG, &val, sizeof(val));
1812 static int idtcm_enable_tod_sync(struct idtcm_channel *channel)
1814 struct idtcm *idtcm = channel->idtcm;
1816 u8 pll;
1817 u8 sync_src;
1818 u8 qn;
1819 u8 qn_plus_1;
1820 u8 cfg;
1821 int err = 0;
1822 u16 output_mask = channel->output_mask;
1823 u8 out8_mux = 0;
1824 u8 out11_mux = 0;
1825 u8 temp;
1828 * set tod_out_sync_enable to 0.
1830 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1831 if (err)
1832 return err;
1834 cfg &= ~TOD_OUT_SYNC_ENABLE;
1836 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1837 if (err)
1838 return err;
1840 switch (channel->tod_n) {
1841 case TOD_0:
1842 sync_src = 0;
1843 break;
1844 case TOD_1:
1845 sync_src = 1;
1846 break;
1847 case TOD_2:
1848 sync_src = 2;
1849 break;
1850 case TOD_3:
1851 sync_src = 3;
1852 break;
1853 default:
1854 return -EINVAL;
1857 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
1858 &temp, sizeof(temp));
1859 if (err)
1860 return err;
1862 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1863 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1864 out8_mux = 1;
1866 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
1867 &temp, sizeof(temp));
1868 if (err)
1869 return err;
1871 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
1872 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
1873 out11_mux = 1;
1875 for (pll = 0; pll < 8; pll++) {
1876 qn = 0;
1877 qn_plus_1 = 0;
1879 if (pll < 4) {
1880 /* First 4 pll has 2 outputs */
1881 qn = output_mask & 0x1;
1882 output_mask = output_mask >> 1;
1883 qn_plus_1 = output_mask & 0x1;
1884 output_mask = output_mask >> 1;
1885 } else if (pll == 4) {
1886 if (out8_mux == 0) {
1887 qn = output_mask & 0x1;
1888 output_mask = output_mask >> 1;
1890 } else if (pll == 5) {
1891 if (out8_mux) {
1892 qn_plus_1 = output_mask & 0x1;
1893 output_mask = output_mask >> 1;
1895 qn = output_mask & 0x1;
1896 output_mask = output_mask >> 1;
1897 } else if (pll == 6) {
1898 qn = output_mask & 0x1;
1899 output_mask = output_mask >> 1;
1900 if (out11_mux) {
1901 qn_plus_1 = output_mask & 0x1;
1902 output_mask = output_mask >> 1;
1904 } else if (pll == 7) {
1905 if (out11_mux == 0) {
1906 qn = output_mask & 0x1;
1907 output_mask = output_mask >> 1;
1911 if ((qn != 0) || (qn_plus_1 != 0))
1912 err = _enable_pll_tod_sync(idtcm, pll, sync_src, qn,
1913 qn_plus_1);
1915 if (err)
1916 return err;
1919 return err;
1922 static int idtcm_enable_tod(struct idtcm_channel *channel)
1924 struct idtcm *idtcm = channel->idtcm;
1925 struct timespec64 ts = {0, 0};
1926 u8 cfg;
1927 int err;
1930 * Start the TOD clock ticking.
1932 err = idtcm_read(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1933 if (err)
1934 return err;
1936 cfg |= TOD_ENABLE;
1938 err = idtcm_write(idtcm, channel->tod_n, TOD_CFG, &cfg, sizeof(cfg));
1939 if (err)
1940 return err;
1942 if (idtcm->deprecated)
1943 return _idtcm_settime_deprecated(channel, &ts);
1944 else
1945 return _idtcm_settime(channel, &ts,
1946 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1949 static void idtcm_set_version_info(struct idtcm *idtcm)
1951 u8 major;
1952 u8 minor;
1953 u8 hotfix;
1954 u16 product_id;
1955 u8 hw_rev_id;
1956 u8 config_select;
1957 char *fmt = "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d\n";
1959 idtcm_read_major_release(idtcm, &major);
1960 idtcm_read_minor_release(idtcm, &minor);
1961 idtcm_read_hotfix_release(idtcm, &hotfix);
1963 idtcm_read_product_id(idtcm, &product_id);
1964 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
1966 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
1968 snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
1969 major, minor, hotfix);
1971 if (idtcm_strverscmp(idtcm->version, "4.8.7") >= 0)
1972 idtcm->deprecated = 0;
1973 else
1974 idtcm->deprecated = 1;
1976 dev_info(&idtcm->client->dev, fmt, major, minor, hotfix,
1977 product_id, hw_rev_id, config_select);
1980 static const struct ptp_clock_info idtcm_caps = {
1981 .owner = THIS_MODULE,
1982 .max_adj = 244000,
1983 .n_per_out = 12,
1984 .adjphase = &idtcm_adjphase,
1985 .adjfine = &idtcm_adjfine,
1986 .adjtime = &idtcm_adjtime,
1987 .gettime64 = &idtcm_gettime,
1988 .settime64 = &idtcm_settime,
1989 .enable = &idtcm_enable,
1992 static const struct ptp_clock_info idtcm_caps_deprecated = {
1993 .owner = THIS_MODULE,
1994 .max_adj = 244000,
1995 .n_per_out = 12,
1996 .adjphase = &idtcm_adjphase,
1997 .adjfine = &idtcm_adjfine,
1998 .adjtime = &idtcm_adjtime_deprecated,
1999 .gettime64 = &idtcm_gettime,
2000 .settime64 = &idtcm_settime_deprecated,
2001 .enable = &idtcm_enable,
2004 static int configure_channel_pll(struct idtcm_channel *channel)
2006 int err = 0;
2008 switch (channel->pll) {
2009 case 0:
2010 channel->dpll_freq = DPLL_FREQ_0;
2011 channel->dpll_n = DPLL_0;
2012 channel->hw_dpll_n = HW_DPLL_0;
2013 channel->dpll_phase = DPLL_PHASE_0;
2014 channel->dpll_ctrl_n = DPLL_CTRL_0;
2015 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
2016 break;
2017 case 1:
2018 channel->dpll_freq = DPLL_FREQ_1;
2019 channel->dpll_n = DPLL_1;
2020 channel->hw_dpll_n = HW_DPLL_1;
2021 channel->dpll_phase = DPLL_PHASE_1;
2022 channel->dpll_ctrl_n = DPLL_CTRL_1;
2023 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
2024 break;
2025 case 2:
2026 channel->dpll_freq = DPLL_FREQ_2;
2027 channel->dpll_n = DPLL_2;
2028 channel->hw_dpll_n = HW_DPLL_2;
2029 channel->dpll_phase = DPLL_PHASE_2;
2030 channel->dpll_ctrl_n = DPLL_CTRL_2;
2031 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
2032 break;
2033 case 3:
2034 channel->dpll_freq = DPLL_FREQ_3;
2035 channel->dpll_n = DPLL_3;
2036 channel->hw_dpll_n = HW_DPLL_3;
2037 channel->dpll_phase = DPLL_PHASE_3;
2038 channel->dpll_ctrl_n = DPLL_CTRL_3;
2039 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
2040 break;
2041 case 4:
2042 channel->dpll_freq = DPLL_FREQ_4;
2043 channel->dpll_n = DPLL_4;
2044 channel->hw_dpll_n = HW_DPLL_4;
2045 channel->dpll_phase = DPLL_PHASE_4;
2046 channel->dpll_ctrl_n = DPLL_CTRL_4;
2047 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
2048 break;
2049 case 5:
2050 channel->dpll_freq = DPLL_FREQ_5;
2051 channel->dpll_n = DPLL_5;
2052 channel->hw_dpll_n = HW_DPLL_5;
2053 channel->dpll_phase = DPLL_PHASE_5;
2054 channel->dpll_ctrl_n = DPLL_CTRL_5;
2055 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
2056 break;
2057 case 6:
2058 channel->dpll_freq = DPLL_FREQ_6;
2059 channel->dpll_n = DPLL_6;
2060 channel->hw_dpll_n = HW_DPLL_6;
2061 channel->dpll_phase = DPLL_PHASE_6;
2062 channel->dpll_ctrl_n = DPLL_CTRL_6;
2063 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
2064 break;
2065 case 7:
2066 channel->dpll_freq = DPLL_FREQ_7;
2067 channel->dpll_n = DPLL_7;
2068 channel->hw_dpll_n = HW_DPLL_7;
2069 channel->dpll_phase = DPLL_PHASE_7;
2070 channel->dpll_ctrl_n = DPLL_CTRL_7;
2071 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
2072 break;
2073 default:
2074 err = -EINVAL;
2077 return err;
2080 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
2082 struct idtcm_channel *channel;
2083 int err;
2085 if (!(index < MAX_TOD))
2086 return -EINVAL;
2088 channel = &idtcm->channel[index];
2090 /* Set pll addresses */
2091 err = configure_channel_pll(channel);
2092 if (err)
2093 return err;
2095 /* Set tod addresses */
2096 switch (index) {
2097 case 0:
2098 channel->tod_read_primary = TOD_READ_PRIMARY_0;
2099 channel->tod_write = TOD_WRITE_0;
2100 channel->tod_n = TOD_0;
2101 break;
2102 case 1:
2103 channel->tod_read_primary = TOD_READ_PRIMARY_1;
2104 channel->tod_write = TOD_WRITE_1;
2105 channel->tod_n = TOD_1;
2106 break;
2107 case 2:
2108 channel->tod_read_primary = TOD_READ_PRIMARY_2;
2109 channel->tod_write = TOD_WRITE_2;
2110 channel->tod_n = TOD_2;
2111 break;
2112 case 3:
2113 channel->tod_read_primary = TOD_READ_PRIMARY_3;
2114 channel->tod_write = TOD_WRITE_3;
2115 channel->tod_n = TOD_3;
2116 break;
2117 default:
2118 return -EINVAL;
2121 channel->idtcm = idtcm;
2123 if (idtcm->deprecated)
2124 channel->caps = idtcm_caps_deprecated;
2125 else
2126 channel->caps = idtcm_caps;
2128 snprintf(channel->caps.name, sizeof(channel->caps.name),
2129 "IDT CM TOD%u", index);
2131 if (!idtcm->deprecated) {
2132 err = idtcm_enable_tod_sync(channel);
2133 if (err) {
2134 dev_err(&idtcm->client->dev,
2135 "Failed at line %d in func %s!\n",
2136 __LINE__,
2137 __func__);
2138 return err;
2142 /* Sync pll mode with hardware */
2143 err = idtcm_get_pll_mode(channel, &channel->pll_mode);
2144 if (err) {
2145 dev_err(&idtcm->client->dev,
2146 "Error: %s - Unable to read pll mode\n", __func__);
2147 return err;
2150 err = idtcm_enable_tod(channel);
2151 if (err) {
2152 dev_err(&idtcm->client->dev,
2153 "Failed at line %d in func %s!\n",
2154 __LINE__,
2155 __func__);
2156 return err;
2159 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
2161 if (IS_ERR(channel->ptp_clock)) {
2162 err = PTR_ERR(channel->ptp_clock);
2163 channel->ptp_clock = NULL;
2164 return err;
2167 if (!channel->ptp_clock)
2168 return -ENOTSUPP;
2170 dev_info(&idtcm->client->dev, "PLL%d registered as ptp%d\n",
2171 index, channel->ptp_clock->index);
2173 return 0;
2176 static void ptp_clock_unregister_all(struct idtcm *idtcm)
2178 u8 i;
2179 struct idtcm_channel *channel;
2181 for (i = 0; i < MAX_TOD; i++) {
2183 channel = &idtcm->channel[i];
2185 if (channel->ptp_clock)
2186 ptp_clock_unregister(channel->ptp_clock);
2190 static void set_default_masks(struct idtcm *idtcm)
2192 idtcm->tod_mask = DEFAULT_TOD_MASK;
2194 idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
2195 idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
2196 idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
2197 idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2199 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
2200 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
2201 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
2202 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
2205 static int idtcm_probe(struct i2c_client *client,
2206 const struct i2c_device_id *id)
2208 struct idtcm *idtcm;
2209 int err;
2210 u8 i;
2211 char *fmt = "Failed at %d in line %s with channel output %d!\n";
2213 /* Unused for now */
2214 (void)id;
2216 idtcm = devm_kzalloc(&client->dev, sizeof(struct idtcm), GFP_KERNEL);
2218 if (!idtcm)
2219 return -ENOMEM;
2221 idtcm->client = client;
2222 idtcm->page_offset = 0xff;
2223 idtcm->calculate_overhead_flag = 0;
2225 set_default_masks(idtcm);
2227 mutex_init(&idtcm->reg_lock);
2228 mutex_lock(&idtcm->reg_lock);
2230 idtcm_set_version_info(idtcm);
2232 err = idtcm_load_firmware(idtcm, &client->dev);
2234 if (err)
2235 dev_warn(&idtcm->client->dev,
2236 "loading firmware failed with %d\n", err);
2238 if (wait_for_boot_status_ready(idtcm))
2239 dev_warn(&idtcm->client->dev, "BOOT_STATUS != 0xA0\n");
2241 if (idtcm->tod_mask) {
2242 for (i = 0; i < MAX_TOD; i++) {
2243 if (idtcm->tod_mask & (1 << i)) {
2244 err = idtcm_enable_channel(idtcm, i);
2245 if (err) {
2246 dev_err(&idtcm->client->dev,
2247 fmt,
2248 __LINE__,
2249 __func__,
2251 break;
2255 } else {
2256 dev_err(&idtcm->client->dev,
2257 "no PLLs flagged as PHCs, nothing to do\n");
2258 err = -ENODEV;
2261 mutex_unlock(&idtcm->reg_lock);
2263 if (err) {
2264 ptp_clock_unregister_all(idtcm);
2265 return err;
2268 i2c_set_clientdata(client, idtcm);
2270 return 0;
2273 static int idtcm_remove(struct i2c_client *client)
2275 struct idtcm *idtcm = i2c_get_clientdata(client);
2277 ptp_clock_unregister_all(idtcm);
2279 mutex_destroy(&idtcm->reg_lock);
2281 return 0;
2284 #ifdef CONFIG_OF
2285 static const struct of_device_id idtcm_dt_id[] = {
2286 { .compatible = "idt,8a34000" },
2287 { .compatible = "idt,8a34001" },
2288 { .compatible = "idt,8a34002" },
2289 { .compatible = "idt,8a34003" },
2290 { .compatible = "idt,8a34004" },
2291 { .compatible = "idt,8a34005" },
2292 { .compatible = "idt,8a34006" },
2293 { .compatible = "idt,8a34007" },
2294 { .compatible = "idt,8a34008" },
2295 { .compatible = "idt,8a34009" },
2296 { .compatible = "idt,8a34010" },
2297 { .compatible = "idt,8a34011" },
2298 { .compatible = "idt,8a34012" },
2299 { .compatible = "idt,8a34013" },
2300 { .compatible = "idt,8a34014" },
2301 { .compatible = "idt,8a34015" },
2302 { .compatible = "idt,8a34016" },
2303 { .compatible = "idt,8a34017" },
2304 { .compatible = "idt,8a34018" },
2305 { .compatible = "idt,8a34019" },
2306 { .compatible = "idt,8a34040" },
2307 { .compatible = "idt,8a34041" },
2308 { .compatible = "idt,8a34042" },
2309 { .compatible = "idt,8a34043" },
2310 { .compatible = "idt,8a34044" },
2311 { .compatible = "idt,8a34045" },
2312 { .compatible = "idt,8a34046" },
2313 { .compatible = "idt,8a34047" },
2314 { .compatible = "idt,8a34048" },
2315 { .compatible = "idt,8a34049" },
2318 MODULE_DEVICE_TABLE(of, idtcm_dt_id);
2319 #endif
2321 static const struct i2c_device_id idtcm_i2c_id[] = {
2322 { "8a34000" },
2323 { "8a34001" },
2324 { "8a34002" },
2325 { "8a34003" },
2326 { "8a34004" },
2327 { "8a34005" },
2328 { "8a34006" },
2329 { "8a34007" },
2330 { "8a34008" },
2331 { "8a34009" },
2332 { "8a34010" },
2333 { "8a34011" },
2334 { "8a34012" },
2335 { "8a34013" },
2336 { "8a34014" },
2337 { "8a34015" },
2338 { "8a34016" },
2339 { "8a34017" },
2340 { "8a34018" },
2341 { "8a34019" },
2342 { "8a34040" },
2343 { "8a34041" },
2344 { "8a34042" },
2345 { "8a34043" },
2346 { "8a34044" },
2347 { "8a34045" },
2348 { "8a34046" },
2349 { "8a34047" },
2350 { "8a34048" },
2351 { "8a34049" },
2354 MODULE_DEVICE_TABLE(i2c, idtcm_i2c_id);
2356 static struct i2c_driver idtcm_driver = {
2357 .driver = {
2358 .of_match_table = of_match_ptr(idtcm_dt_id),
2359 .name = "idtcm",
2361 .probe = idtcm_probe,
2362 .remove = idtcm_remove,
2363 .id_table = idtcm_i2c_id,
2366 module_i2c_driver(idtcm_driver);