1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 Integrated Device Technology, Inc
6 #define pr_fmt(fmt) "IDT_82p33xxx: " fmt
8 #include <linux/firmware.h>
9 #include <linux/platform_device.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/bitops.h>
18 #include <linux/mfd/rsmu.h>
19 #include <linux/mfd/idt82p33_reg.h>
21 #include "ptp_private.h"
22 #include "ptp_idt82p33.h"
24 MODULE_DESCRIPTION("Driver for IDT 82p33xxx clock devices");
25 MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
26 MODULE_VERSION("1.0");
27 MODULE_LICENSE("GPL");
28 MODULE_FIRMWARE(FW_FILENAME
);
30 #define EXTTS_PERIOD_MS (95)
32 /* Module Parameters */
33 static u32 phase_snap_threshold
= SNAP_THRESHOLD_NS
;
34 module_param(phase_snap_threshold
, uint
, 0);
35 MODULE_PARM_DESC(phase_snap_threshold
,
36 "threshold (10000ns by default) below which adjtime would use double dco");
38 static char *firmware
;
39 module_param(firmware
, charp
, 0);
41 static struct ptp_pin_desc pin_config
[MAX_PHC_PLL
][MAX_TRIG_CLK
];
43 static inline int idt82p33_read(struct idt82p33
*idt82p33
, u16 regaddr
,
46 return regmap_bulk_read(idt82p33
->regmap
, regaddr
, buf
, count
);
49 static inline int idt82p33_write(struct idt82p33
*idt82p33
, u16 regaddr
,
52 return regmap_bulk_write(idt82p33
->regmap
, regaddr
, buf
, count
);
55 static void idt82p33_byte_array_to_timespec(struct timespec64
*ts
,
56 u8 buf
[TOD_BYTE_COUNT
])
63 for (i
= 0; i
< 3; i
++) {
69 for (i
= 0; i
< 5; i
++) {
78 static void idt82p33_timespec_to_byte_array(struct timespec64
const *ts
,
79 u8 buf
[TOD_BYTE_COUNT
])
88 for (i
= 0; i
< 4; i
++) {
93 for (i
= 4; i
< TOD_BYTE_COUNT
; i
++) {
99 static int idt82p33_dpll_set_mode(struct idt82p33_channel
*channel
,
102 struct idt82p33
*idt82p33
= channel
->idt82p33
;
106 if (channel
->pll_mode
== mode
)
109 err
= idt82p33_read(idt82p33
, channel
->dpll_mode_cnfg
,
110 &dpll_mode
, sizeof(dpll_mode
));
114 dpll_mode
&= ~(PLL_MODE_MASK
<< PLL_MODE_SHIFT
);
116 dpll_mode
|= (mode
<< PLL_MODE_SHIFT
);
118 err
= idt82p33_write(idt82p33
, channel
->dpll_mode_cnfg
,
119 &dpll_mode
, sizeof(dpll_mode
));
123 channel
->pll_mode
= mode
;
128 static int idt82p33_set_tod_trigger(struct idt82p33_channel
*channel
,
129 u8 trigger
, bool write
)
131 struct idt82p33
*idt82p33
= channel
->idt82p33
;
135 if (trigger
> WR_TRIG_SEL_MAX
)
138 err
= idt82p33_read(idt82p33
, channel
->dpll_tod_trigger
,
145 trigger
= (trigger
<< WRITE_TRIGGER_SHIFT
) |
146 (cfg
& READ_TRIGGER_MASK
);
148 trigger
= (trigger
<< READ_TRIGGER_SHIFT
) |
149 (cfg
& WRITE_TRIGGER_MASK
);
151 return idt82p33_write(idt82p33
, channel
->dpll_tod_trigger
,
152 &trigger
, sizeof(trigger
));
155 static int idt82p33_get_extts(struct idt82p33_channel
*channel
,
156 struct timespec64
*ts
)
158 struct idt82p33
*idt82p33
= channel
->idt82p33
;
159 u8 buf
[TOD_BYTE_COUNT
];
162 err
= idt82p33_read(idt82p33
, channel
->dpll_tod_sts
, buf
, sizeof(buf
));
167 /* Since trigger is not self clearing itself, we have to poll tod_sts */
168 if (memcmp(buf
, channel
->extts_tod_sts
, TOD_BYTE_COUNT
) == 0)
171 memcpy(channel
->extts_tod_sts
, buf
, TOD_BYTE_COUNT
);
173 idt82p33_byte_array_to_timespec(ts
, buf
);
175 if (channel
->discard_next_extts
) {
176 channel
->discard_next_extts
= false;
183 static int map_ref_to_tod_trig_sel(int ref
, u8
*trigger
)
189 *trigger
= HW_TOD_TRIG_SEL_IN12
;
192 *trigger
= HW_TOD_TRIG_SEL_IN13
;
195 *trigger
= HW_TOD_TRIG_SEL_IN14
;
204 static bool is_one_shot(u8 mask
)
206 /* Treat single bit PLL masks as continuous trigger */
207 if ((mask
== 1) || (mask
== 2))
213 static int arm_tod_read_with_trigger(struct idt82p33_channel
*channel
, u8 trigger
)
215 struct idt82p33
*idt82p33
= channel
->idt82p33
;
216 u8 buf
[TOD_BYTE_COUNT
];
219 /* Remember the current tod_sts before setting the trigger */
220 err
= idt82p33_read(idt82p33
, channel
->dpll_tod_sts
, buf
, sizeof(buf
));
225 memcpy(channel
->extts_tod_sts
, buf
, TOD_BYTE_COUNT
);
227 err
= idt82p33_set_tod_trigger(channel
, trigger
, false);
230 dev_err(idt82p33
->dev
, "%s: err = %d", __func__
, err
);
235 static int idt82p33_extts_enable(struct idt82p33_channel
*channel
,
236 struct ptp_clock_request
*rq
, int on
)
238 u8 index
= rq
->extts
.index
;
239 struct idt82p33
*idt82p33
;
240 u8 mask
= 1 << index
;
246 idt82p33
= channel
->idt82p33
;
247 old_mask
= idt82p33
->extts_mask
;
249 /* Reject requests with unsupported flags */
250 if (rq
->extts
.flags
& ~(PTP_ENABLE_FEATURE
|
256 /* Reject requests to enable time stamping on falling edge */
257 if ((rq
->extts
.flags
& PTP_ENABLE_FEATURE
) &&
258 (rq
->extts
.flags
& PTP_FALLING_EDGE
))
261 if (index
>= MAX_PHC_PLL
)
265 /* Return if it was already enabled */
266 if (idt82p33
->extts_mask
& mask
)
269 /* Use the pin configured for the channel */
270 ref
= ptp_find_pin(channel
->ptp_clock
, PTP_PF_EXTTS
, channel
->plln
);
273 dev_err(idt82p33
->dev
, "%s: No valid pin found for Pll%d!\n",
274 __func__
, channel
->plln
);
278 err
= map_ref_to_tod_trig_sel(ref
, &trigger
);
281 dev_err(idt82p33
->dev
,
282 "%s: Unsupported ref %d!\n", __func__
, ref
);
286 err
= arm_tod_read_with_trigger(&idt82p33
->channel
[index
], trigger
);
289 idt82p33
->extts_mask
|= mask
;
290 idt82p33
->channel
[index
].tod_trigger
= trigger
;
291 idt82p33
->event_channel
[index
] = channel
;
292 idt82p33
->extts_single_shot
= is_one_shot(idt82p33
->extts_mask
);
297 schedule_delayed_work(&idt82p33
->extts_work
,
298 msecs_to_jiffies(EXTTS_PERIOD_MS
));
301 idt82p33
->extts_mask
&= ~mask
;
302 idt82p33
->extts_single_shot
= is_one_shot(idt82p33
->extts_mask
);
304 if (idt82p33
->extts_mask
== 0)
305 cancel_delayed_work(&idt82p33
->extts_work
);
311 static int idt82p33_extts_check_channel(struct idt82p33
*idt82p33
, u8 todn
)
313 struct idt82p33_channel
*event_channel
;
314 struct ptp_clock_event event
;
315 struct timespec64 ts
;
318 err
= idt82p33_get_extts(&idt82p33
->channel
[todn
], &ts
);
320 event_channel
= idt82p33
->event_channel
[todn
];
321 event
.type
= PTP_CLOCK_EXTTS
;
323 event
.timestamp
= timespec64_to_ns(&ts
);
324 ptp_clock_event(event_channel
->ptp_clock
,
330 static u8
idt82p33_extts_enable_mask(struct idt82p33_channel
*channel
,
331 u8 extts_mask
, bool enable
)
333 struct idt82p33
*idt82p33
= channel
->idt82p33
;
334 u8 trigger
= channel
->tod_trigger
;
343 cancel_delayed_work_sync(&idt82p33
->extts_work
);
345 for (i
= 0; i
< MAX_PHC_PLL
; i
++) {
348 if ((extts_mask
& mask
) == 0)
352 err
= arm_tod_read_with_trigger(&idt82p33
->channel
[i
], trigger
);
354 dev_err(idt82p33
->dev
,
355 "%s: Arm ToD read trigger failed, err = %d",
358 err
= idt82p33_extts_check_channel(idt82p33
, i
);
359 if (err
== 0 && idt82p33
->extts_single_shot
)
360 /* trigger happened so we won't re-enable it */
366 schedule_delayed_work(&idt82p33
->extts_work
,
367 msecs_to_jiffies(EXTTS_PERIOD_MS
));
372 static int _idt82p33_gettime(struct idt82p33_channel
*channel
,
373 struct timespec64
*ts
)
375 struct idt82p33
*idt82p33
= channel
->idt82p33
;
376 u8 old_mask
= idt82p33
->extts_mask
;
377 u8 buf
[TOD_BYTE_COUNT
];
383 new_mask
= idt82p33_extts_enable_mask(channel
, old_mask
, false);
385 err
= idt82p33_set_tod_trigger(channel
, HW_TOD_RD_TRIG_SEL_LSB_TOD_STS
,
390 channel
->discard_next_extts
= true;
392 if (idt82p33
->calculate_overhead_flag
)
393 idt82p33
->start_time
= ktime_get_raw();
395 err
= idt82p33_read(idt82p33
, channel
->dpll_tod_sts
, buf
, sizeof(buf
));
400 /* Re-enable extts */
402 idt82p33_extts_enable_mask(channel
, new_mask
, true);
404 idt82p33_byte_array_to_timespec(ts
, buf
);
411 * Bits[7:4] Write 0x9, MSB write
412 * Bits[3:0] Read 0x9, LSB read
415 static int _idt82p33_settime(struct idt82p33_channel
*channel
,
416 struct timespec64
const *ts
)
418 struct idt82p33
*idt82p33
= channel
->idt82p33
;
419 struct timespec64 local_ts
= *ts
;
420 char buf
[TOD_BYTE_COUNT
];
421 s64 dynamic_overhead_ns
;
425 err
= idt82p33_set_tod_trigger(channel
, HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG
,
430 channel
->discard_next_extts
= true;
432 if (idt82p33
->calculate_overhead_flag
) {
433 dynamic_overhead_ns
= ktime_to_ns(ktime_get_raw())
434 - ktime_to_ns(idt82p33
->start_time
);
436 timespec64_add_ns(&local_ts
, dynamic_overhead_ns
);
438 idt82p33
->calculate_overhead_flag
= 0;
441 idt82p33_timespec_to_byte_array(&local_ts
, buf
);
444 * Store the new time value.
446 for (i
= 0; i
< TOD_BYTE_COUNT
; i
++) {
447 err
= idt82p33_write(idt82p33
, channel
->dpll_tod_cnfg
+ i
,
448 &buf
[i
], sizeof(buf
[i
]));
456 static int _idt82p33_adjtime_immediate(struct idt82p33_channel
*channel
,
459 struct idt82p33
*idt82p33
= channel
->idt82p33
;
460 struct timespec64 ts
;
464 idt82p33
->calculate_overhead_flag
= 1;
466 err
= _idt82p33_gettime(channel
, &ts
);
471 now_ns
= timespec64_to_ns(&ts
);
472 now_ns
+= delta_ns
+ idt82p33
->tod_write_overhead_ns
;
474 ts
= ns_to_timespec64(now_ns
);
476 err
= _idt82p33_settime(channel
, &ts
);
481 static int _idt82p33_adjtime_internal_triggered(struct idt82p33_channel
*channel
,
484 struct idt82p33
*idt82p33
= channel
->idt82p33
;
485 char buf
[TOD_BYTE_COUNT
];
486 struct timespec64 ts
;
487 const u8 delay_ns
= 32;
492 err
= _idt82p33_gettime(channel
, &ts
);
497 if (ts
.tv_nsec
> (NSEC_PER_SEC
- 5 * NSEC_PER_MSEC
)) {
498 /* Too close to miss next trigger, so skip it */
500 ns
= (ts
.tv_sec
+ 2) * NSEC_PER_SEC
+ delta_ns
+ delay_ns
;
502 ns
= (ts
.tv_sec
+ 1) * NSEC_PER_SEC
+ delta_ns
+ delay_ns
;
504 ts
= ns_to_timespec64(ns
);
505 idt82p33_timespec_to_byte_array(&ts
, buf
);
508 * Store the new time value.
510 err
= idt82p33_write(idt82p33
, channel
->dpll_tod_cnfg
, buf
, sizeof(buf
));
514 /* Schedule to implement the workaround in one second */
515 (void)div_s64_rem(delta_ns
, NSEC_PER_SEC
, &remainder
);
517 schedule_delayed_work(&channel
->adjtime_work
, HZ
);
519 return idt82p33_set_tod_trigger(channel
, HW_TOD_TRIG_SEL_TOD_PPS
, true);
522 static void idt82p33_adjtime_workaround(struct work_struct
*work
)
524 struct idt82p33_channel
*channel
= container_of(work
,
525 struct idt82p33_channel
,
527 struct idt82p33
*idt82p33
= channel
->idt82p33
;
529 mutex_lock(idt82p33
->lock
);
530 /* Workaround for TOD-to-output alignment issue */
531 _idt82p33_adjtime_internal_triggered(channel
, 0);
532 mutex_unlock(idt82p33
->lock
);
535 static int _idt82p33_adjfine(struct idt82p33_channel
*channel
, long scaled_ppm
)
537 struct idt82p33
*idt82p33
= channel
->idt82p33
;
538 unsigned char buf
[5] = {0};
543 * Frequency Control Word unit is: 1.6861512 * 10^-10 ppm
551 * scaled_ppm * 5^12 * 10^5
552 * FCW = ------------------------
556 fcw
= scaled_ppm
* 762939453125ULL;
557 fcw
= div_s64(fcw
, 8430756LL);
559 for (i
= 0; i
< 5; i
++) {
564 err
= idt82p33_dpll_set_mode(channel
, PLL_MODE_DCO
);
569 err
= idt82p33_write(idt82p33
, channel
->dpll_freq_cnfg
,
575 /* ppb = scaled_ppm * 125 / 2^13 */
576 static s32
idt82p33_ddco_scaled_ppm(long current_ppm
, s32 ddco_ppb
)
578 s64 scaled_ppm
= div_s64(((s64
)ddco_ppb
<< 13), 125);
579 s64 max_scaled_ppm
= div_s64(((s64
)DCO_MAX_PPB
<< 13), 125);
581 current_ppm
+= scaled_ppm
;
583 if (current_ppm
> max_scaled_ppm
)
584 current_ppm
= max_scaled_ppm
;
585 else if (current_ppm
< -max_scaled_ppm
)
586 current_ppm
= -max_scaled_ppm
;
588 return (s32
)current_ppm
;
591 static int idt82p33_stop_ddco(struct idt82p33_channel
*channel
)
595 err
= _idt82p33_adjfine(channel
, channel
->current_freq
);
599 channel
->ddco
= false;
604 static int idt82p33_start_ddco(struct idt82p33_channel
*channel
, s32 delta_ns
)
606 s32 current_ppm
= channel
->current_freq
;
607 u32 duration_ms
= MSEC_PER_SEC
;
611 /* If the ToD correction is less than 5 nanoseconds, then skip it.
612 * The error introduced by the ToD adjustment procedure would be bigger
613 * than the required ToD correction
615 if (abs(delta_ns
) < DDCO_THRESHOLD_NS
)
618 /* For most cases, keep ddco duration 1 second */
620 while (abs(ppb
) > DCO_MAX_PPB
) {
625 err
= _idt82p33_adjfine(channel
,
626 idt82p33_ddco_scaled_ppm(current_ppm
, ppb
));
630 /* schedule the worker to cancel ddco */
631 ptp_schedule_worker(channel
->ptp_clock
,
632 msecs_to_jiffies(duration_ms
) - 1);
633 channel
->ddco
= true;
638 static int idt82p33_measure_one_byte_write_overhead(
639 struct idt82p33_channel
*channel
, s64
*overhead_ns
)
641 struct idt82p33
*idt82p33
= channel
->idt82p33
;
651 for (i
= 0; i
< MAX_MEASURMENT_COUNT
; i
++) {
653 start
= ktime_get_raw();
655 err
= idt82p33_write(idt82p33
, channel
->dpll_tod_trigger
,
656 &trigger
, sizeof(trigger
));
658 stop
= ktime_get_raw();
663 total_ns
+= ktime_to_ns(stop
) - ktime_to_ns(start
);
666 *overhead_ns
= div_s64(total_ns
, MAX_MEASURMENT_COUNT
);
671 static int idt82p33_measure_one_byte_read_overhead(
672 struct idt82p33_channel
*channel
, s64
*overhead_ns
)
674 struct idt82p33
*idt82p33
= channel
->idt82p33
;
684 for (i
= 0; i
< MAX_MEASURMENT_COUNT
; i
++) {
686 start
= ktime_get_raw();
688 err
= idt82p33_read(idt82p33
, channel
->dpll_tod_trigger
,
689 &trigger
, sizeof(trigger
));
691 stop
= ktime_get_raw();
696 total_ns
+= ktime_to_ns(stop
) - ktime_to_ns(start
);
699 *overhead_ns
= div_s64(total_ns
, MAX_MEASURMENT_COUNT
);
704 static int idt82p33_measure_tod_write_9_byte_overhead(
705 struct idt82p33_channel
*channel
)
707 struct idt82p33
*idt82p33
= channel
->idt82p33
;
708 u8 buf
[TOD_BYTE_COUNT
];
715 idt82p33
->tod_write_overhead_ns
= 0;
717 for (i
= 0; i
< MAX_MEASURMENT_COUNT
; i
++) {
719 start
= ktime_get_raw();
721 /* Need one less byte for applicable overhead */
722 for (j
= 0; j
< (TOD_BYTE_COUNT
- 1); j
++) {
723 err
= idt82p33_write(idt82p33
,
724 channel
->dpll_tod_cnfg
+ i
,
725 &buf
[i
], sizeof(buf
[i
]));
730 stop
= ktime_get_raw();
732 total_ns
+= ktime_to_ns(stop
) - ktime_to_ns(start
);
735 idt82p33
->tod_write_overhead_ns
= div_s64(total_ns
,
736 MAX_MEASURMENT_COUNT
);
741 static int idt82p33_measure_settime_gettime_gap_overhead(
742 struct idt82p33_channel
*channel
, s64
*overhead_ns
)
744 struct timespec64 ts1
= {0, 0};
745 struct timespec64 ts2
;
750 err
= _idt82p33_settime(channel
, &ts1
);
755 err
= _idt82p33_gettime(channel
, &ts2
);
758 *overhead_ns
= timespec64_to_ns(&ts2
) - timespec64_to_ns(&ts1
);
763 static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel
*channel
)
765 s64 trailing_overhead_ns
, one_byte_write_ns
, gap_ns
, one_byte_read_ns
;
766 struct idt82p33
*idt82p33
= channel
->idt82p33
;
769 idt82p33
->tod_write_overhead_ns
= 0;
771 err
= idt82p33_measure_settime_gettime_gap_overhead(channel
, &gap_ns
);
774 dev_err(idt82p33
->dev
,
775 "Failed in %s with err %d!\n", __func__
, err
);
779 err
= idt82p33_measure_one_byte_write_overhead(channel
,
785 err
= idt82p33_measure_one_byte_read_overhead(channel
,
791 err
= idt82p33_measure_tod_write_9_byte_overhead(channel
);
796 trailing_overhead_ns
= gap_ns
- 2 * one_byte_write_ns
799 idt82p33
->tod_write_overhead_ns
-= trailing_overhead_ns
;
804 static int idt82p33_check_and_set_masks(struct idt82p33
*idt82p33
,
811 if (page
== PLLMASK_ADDR_HI
&& offset
== PLLMASK_ADDR_LO
) {
812 if ((val
& 0xfc) || !(val
& 0x3)) {
813 dev_err(idt82p33
->dev
,
814 "Invalid PLL mask 0x%x\n", val
);
817 idt82p33
->pll_mask
= val
;
819 } else if (page
== PLL0_OUTMASK_ADDR_HI
&&
820 offset
== PLL0_OUTMASK_ADDR_LO
) {
821 idt82p33
->channel
[0].output_mask
= val
;
822 } else if (page
== PLL1_OUTMASK_ADDR_HI
&&
823 offset
== PLL1_OUTMASK_ADDR_LO
) {
824 idt82p33
->channel
[1].output_mask
= val
;
830 static void idt82p33_display_masks(struct idt82p33
*idt82p33
)
834 dev_info(idt82p33
->dev
,
835 "pllmask = 0x%02x\n", idt82p33
->pll_mask
);
837 for (i
= 0; i
< MAX_PHC_PLL
; i
++) {
840 if (mask
& idt82p33
->pll_mask
)
841 dev_info(idt82p33
->dev
,
842 "PLL%d output_mask = 0x%04x\n",
843 i
, idt82p33
->channel
[i
].output_mask
);
847 static int idt82p33_sync_tod(struct idt82p33_channel
*channel
, bool enable
)
849 struct idt82p33
*idt82p33
= channel
->idt82p33
;
853 err
= idt82p33_read(idt82p33
, channel
->dpll_sync_cnfg
,
854 &sync_cnfg
, sizeof(sync_cnfg
));
858 sync_cnfg
&= ~SYNC_TOD
;
860 sync_cnfg
|= SYNC_TOD
;
862 return idt82p33_write(idt82p33
, channel
->dpll_sync_cnfg
,
863 &sync_cnfg
, sizeof(sync_cnfg
));
866 static long idt82p33_work_handler(struct ptp_clock_info
*ptp
)
868 struct idt82p33_channel
*channel
=
869 container_of(ptp
, struct idt82p33_channel
, caps
);
870 struct idt82p33
*idt82p33
= channel
->idt82p33
;
872 mutex_lock(idt82p33
->lock
);
873 (void)idt82p33_stop_ddco(channel
);
874 mutex_unlock(idt82p33
->lock
);
876 /* Return a negative value here to not reschedule */
880 static int idt82p33_output_enable(struct idt82p33_channel
*channel
,
881 bool enable
, unsigned int outn
)
883 struct idt82p33
*idt82p33
= channel
->idt82p33
;
887 err
= idt82p33_read(idt82p33
, OUT_MUX_CNFG(outn
), &val
, sizeof(val
));
891 val
&= ~SQUELCH_ENABLE
;
893 val
|= SQUELCH_ENABLE
;
895 return idt82p33_write(idt82p33
, OUT_MUX_CNFG(outn
), &val
, sizeof(val
));
898 static int idt82p33_perout_enable(struct idt82p33_channel
*channel
,
900 struct ptp_perout_request
*perout
)
902 /* Enable/disable individual output instead */
903 return idt82p33_output_enable(channel
, enable
, perout
->index
);
906 static int idt82p33_enable_tod(struct idt82p33_channel
*channel
)
908 struct idt82p33
*idt82p33
= channel
->idt82p33
;
909 struct timespec64 ts
= {0, 0};
912 err
= idt82p33_measure_tod_write_overhead(channel
);
915 dev_err(idt82p33
->dev
,
916 "Failed in %s with err %d!\n", __func__
, err
);
920 err
= _idt82p33_settime(channel
, &ts
);
925 return idt82p33_sync_tod(channel
, true);
928 static void idt82p33_ptp_clock_unregister_all(struct idt82p33
*idt82p33
)
930 struct idt82p33_channel
*channel
;
933 for (i
= 0; i
< MAX_PHC_PLL
; i
++) {
934 channel
= &idt82p33
->channel
[i
];
935 cancel_delayed_work_sync(&channel
->adjtime_work
);
936 if (channel
->ptp_clock
)
937 ptp_clock_unregister(channel
->ptp_clock
);
943 static int idt82p33_enable(struct ptp_clock_info
*ptp
,
944 struct ptp_clock_request
*rq
, int on
)
946 struct idt82p33_channel
*channel
=
947 container_of(ptp
, struct idt82p33_channel
, caps
);
948 struct idt82p33
*idt82p33
= channel
->idt82p33
;
949 int err
= -EOPNOTSUPP
;
951 mutex_lock(idt82p33
->lock
);
954 case PTP_CLK_REQ_PEROUT
:
956 err
= idt82p33_perout_enable(channel
, false,
958 /* Only accept a 1-PPS aligned to the second. */
959 else if (rq
->perout
.start
.nsec
|| rq
->perout
.period
.sec
!= 1 ||
960 rq
->perout
.period
.nsec
)
963 err
= idt82p33_perout_enable(channel
, true,
966 case PTP_CLK_REQ_EXTTS
:
967 err
= idt82p33_extts_enable(channel
, rq
, on
);
973 mutex_unlock(idt82p33
->lock
);
976 dev_err(idt82p33
->dev
,
977 "Failed in %s with err %d!\n", __func__
, err
);
981 static s32
idt82p33_getmaxphase(__always_unused
struct ptp_clock_info
*ptp
)
983 return WRITE_PHASE_OFFSET_LIMIT
;
986 static int idt82p33_adjwritephase(struct ptp_clock_info
*ptp
, s32 offset_ns
)
988 struct idt82p33_channel
*channel
=
989 container_of(ptp
, struct idt82p33_channel
, caps
);
990 struct idt82p33
*idt82p33
= channel
->idt82p33
;
995 /* Convert from phaseoffset_fs to register value */
996 offset_regval
= div_s64((s64
)(-offset_ns
) * 1000000000ll,
997 IDT_T0DPLL_PHASE_RESOL
);
999 val
[0] = offset_regval
& 0xFF;
1000 val
[1] = (offset_regval
>> 8) & 0xFF;
1001 val
[2] = (offset_regval
>> 16) & 0xFF;
1002 val
[3] = (offset_regval
>> 24) & 0x1F;
1003 val
[3] |= PH_OFFSET_EN
;
1005 mutex_lock(idt82p33
->lock
);
1007 err
= idt82p33_dpll_set_mode(channel
, PLL_MODE_WPH
);
1009 dev_err(idt82p33
->dev
,
1010 "Failed in %s with err %d!\n", __func__
, err
);
1014 err
= idt82p33_write(idt82p33
, channel
->dpll_phase_cnfg
, val
,
1018 mutex_unlock(idt82p33
->lock
);
1022 static int idt82p33_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
1024 struct idt82p33_channel
*channel
=
1025 container_of(ptp
, struct idt82p33_channel
, caps
);
1026 struct idt82p33
*idt82p33
= channel
->idt82p33
;
1029 if (channel
->ddco
== true)
1032 if (scaled_ppm
== channel
->current_freq
)
1035 mutex_lock(idt82p33
->lock
);
1036 err
= _idt82p33_adjfine(channel
, scaled_ppm
);
1039 channel
->current_freq
= scaled_ppm
;
1040 mutex_unlock(idt82p33
->lock
);
1043 dev_err(idt82p33
->dev
,
1044 "Failed in %s with err %d!\n", __func__
, err
);
1048 static int idt82p33_adjtime(struct ptp_clock_info
*ptp
, s64 delta_ns
)
1050 struct idt82p33_channel
*channel
=
1051 container_of(ptp
, struct idt82p33_channel
, caps
);
1052 struct idt82p33
*idt82p33
= channel
->idt82p33
;
1055 if (channel
->ddco
== true)
1058 mutex_lock(idt82p33
->lock
);
1060 if (abs(delta_ns
) < phase_snap_threshold
) {
1061 err
= idt82p33_start_ddco(channel
, delta_ns
);
1062 mutex_unlock(idt82p33
->lock
);
1066 /* Use more accurate internal 1pps triggered write first */
1067 err
= _idt82p33_adjtime_internal_triggered(channel
, delta_ns
);
1068 if (err
&& delta_ns
> IMMEDIATE_SNAP_THRESHOLD_NS
)
1069 err
= _idt82p33_adjtime_immediate(channel
, delta_ns
);
1071 mutex_unlock(idt82p33
->lock
);
1074 dev_err(idt82p33
->dev
,
1075 "Failed in %s with err %d!\n", __func__
, err
);
1079 static int idt82p33_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
1081 struct idt82p33_channel
*channel
=
1082 container_of(ptp
, struct idt82p33_channel
, caps
);
1083 struct idt82p33
*idt82p33
= channel
->idt82p33
;
1086 mutex_lock(idt82p33
->lock
);
1087 err
= _idt82p33_gettime(channel
, ts
);
1088 mutex_unlock(idt82p33
->lock
);
1091 dev_err(idt82p33
->dev
,
1092 "Failed in %s with err %d!\n", __func__
, err
);
1096 static int idt82p33_settime(struct ptp_clock_info
*ptp
,
1097 const struct timespec64
*ts
)
1099 struct idt82p33_channel
*channel
=
1100 container_of(ptp
, struct idt82p33_channel
, caps
);
1101 struct idt82p33
*idt82p33
= channel
->idt82p33
;
1104 mutex_lock(idt82p33
->lock
);
1105 err
= _idt82p33_settime(channel
, ts
);
1106 mutex_unlock(idt82p33
->lock
);
1109 dev_err(idt82p33
->dev
,
1110 "Failed in %s with err %d!\n", __func__
, err
);
1114 static int idt82p33_channel_init(struct idt82p33
*idt82p33
, u32 index
)
1116 struct idt82p33_channel
*channel
= &idt82p33
->channel
[index
];
1120 channel
->dpll_tod_cnfg
= DPLL1_TOD_CNFG
;
1121 channel
->dpll_tod_trigger
= DPLL1_TOD_TRIGGER
;
1122 channel
->dpll_tod_sts
= DPLL1_TOD_STS
;
1123 channel
->dpll_mode_cnfg
= DPLL1_OPERATING_MODE_CNFG
;
1124 channel
->dpll_freq_cnfg
= DPLL1_HOLDOVER_FREQ_CNFG
;
1125 channel
->dpll_phase_cnfg
= DPLL1_PHASE_OFFSET_CNFG
;
1126 channel
->dpll_sync_cnfg
= DPLL1_SYNC_EDGE_CNFG
;
1127 channel
->dpll_input_mode_cnfg
= DPLL1_INPUT_MODE_CNFG
;
1130 channel
->dpll_tod_cnfg
= DPLL2_TOD_CNFG
;
1131 channel
->dpll_tod_trigger
= DPLL2_TOD_TRIGGER
;
1132 channel
->dpll_tod_sts
= DPLL2_TOD_STS
;
1133 channel
->dpll_mode_cnfg
= DPLL2_OPERATING_MODE_CNFG
;
1134 channel
->dpll_freq_cnfg
= DPLL2_HOLDOVER_FREQ_CNFG
;
1135 channel
->dpll_phase_cnfg
= DPLL2_PHASE_OFFSET_CNFG
;
1136 channel
->dpll_sync_cnfg
= DPLL2_SYNC_EDGE_CNFG
;
1137 channel
->dpll_input_mode_cnfg
= DPLL2_INPUT_MODE_CNFG
;
1143 channel
->plln
= index
;
1144 channel
->current_freq
= 0;
1145 channel
->idt82p33
= idt82p33
;
1146 INIT_DELAYED_WORK(&channel
->adjtime_work
, idt82p33_adjtime_workaround
);
1151 static int idt82p33_verify_pin(struct ptp_clock_info
*ptp
, unsigned int pin
,
1152 enum ptp_pin_function func
, unsigned int chan
)
1159 case PTP_PF_PHYSYNC
:
1165 static void idt82p33_caps_init(u32 index
, struct ptp_clock_info
*caps
,
1166 struct ptp_pin_desc
*pin_cfg
, u8 max_pins
)
1168 struct ptp_pin_desc
*ppd
;
1171 caps
->owner
= THIS_MODULE
;
1172 caps
->max_adj
= DCO_MAX_PPB
;
1173 caps
->n_per_out
= MAX_PER_OUT
;
1174 caps
->n_ext_ts
= MAX_PHC_PLL
;
1175 caps
->n_pins
= max_pins
;
1176 caps
->adjphase
= idt82p33_adjwritephase
;
1177 caps
->getmaxphase
= idt82p33_getmaxphase
;
1178 caps
->adjfine
= idt82p33_adjfine
;
1179 caps
->adjtime
= idt82p33_adjtime
;
1180 caps
->gettime64
= idt82p33_gettime
;
1181 caps
->settime64
= idt82p33_settime
;
1182 caps
->enable
= idt82p33_enable
;
1183 caps
->verify
= idt82p33_verify_pin
;
1184 caps
->do_aux_work
= idt82p33_work_handler
;
1186 snprintf(caps
->name
, sizeof(caps
->name
), "IDT 82P33 PLL%u", index
);
1188 caps
->pin_config
= pin_cfg
;
1190 for (i
= 0; i
< max_pins
; ++i
) {
1194 ppd
->func
= PTP_PF_NONE
;
1196 snprintf(ppd
->name
, sizeof(ppd
->name
), "in%d", 12 + i
);
1200 static int idt82p33_enable_channel(struct idt82p33
*idt82p33
, u32 index
)
1202 struct idt82p33_channel
*channel
;
1205 if (!(index
< MAX_PHC_PLL
))
1208 channel
= &idt82p33
->channel
[index
];
1210 err
= idt82p33_channel_init(idt82p33
, index
);
1212 dev_err(idt82p33
->dev
,
1213 "Channel_init failed in %s with err %d!\n",
1218 idt82p33_caps_init(index
, &channel
->caps
,
1219 pin_config
[index
], MAX_TRIG_CLK
);
1221 channel
->ptp_clock
= ptp_clock_register(&channel
->caps
, NULL
);
1223 if (IS_ERR(channel
->ptp_clock
)) {
1224 err
= PTR_ERR(channel
->ptp_clock
);
1225 channel
->ptp_clock
= NULL
;
1229 if (!channel
->ptp_clock
)
1232 err
= idt82p33_dpll_set_mode(channel
, PLL_MODE_DCO
);
1234 dev_err(idt82p33
->dev
,
1235 "Dpll_set_mode failed in %s with err %d!\n",
1240 err
= idt82p33_enable_tod(channel
);
1242 dev_err(idt82p33
->dev
,
1243 "Enable_tod failed in %s with err %d!\n",
1248 dev_info(idt82p33
->dev
, "PLL%d registered as ptp%d\n",
1249 index
, channel
->ptp_clock
->index
);
1254 static int idt82p33_reset(struct idt82p33
*idt82p33
, bool cold
)
1257 u8 cfg
= SOFT_RESET_EN
;
1262 err
= idt82p33_read(idt82p33
, REG_SOFT_RESET
, &cfg
, sizeof(cfg
));
1264 dev_err(idt82p33
->dev
,
1265 "Soft reset failed with err %d!\n", err
);
1269 cfg
|= SOFT_RESET_EN
;
1272 err
= idt82p33_write(idt82p33
, REG_SOFT_RESET
, &cfg
, sizeof(cfg
));
1274 dev_err(idt82p33
->dev
,
1275 "Cold reset failed with err %d!\n", err
);
1279 static int idt82p33_load_firmware(struct idt82p33
*idt82p33
)
1281 char fname
[128] = FW_FILENAME
;
1282 const struct firmware
*fw
;
1283 struct idt82p33_fwrc
*rec
;
1284 u8 loaddr
, page
, val
;
1288 if (firmware
) /* module parameter */
1289 snprintf(fname
, sizeof(fname
), "%s", firmware
);
1291 dev_info(idt82p33
->dev
, "requesting firmware '%s'\n", fname
);
1293 err
= request_firmware(&fw
, fname
, idt82p33
->dev
);
1296 dev_err(idt82p33
->dev
,
1297 "Failed in %s with err %d!\n", __func__
, err
);
1301 dev_dbg(idt82p33
->dev
, "firmware size %zu bytes\n", fw
->size
);
1303 rec
= (struct idt82p33_fwrc
*) fw
->data
;
1305 for (len
= fw
->size
; len
> 0; len
-= sizeof(*rec
)) {
1307 if (rec
->reserved
) {
1308 dev_err(idt82p33
->dev
,
1309 "bad firmware, reserved field non-zero\n");
1313 loaddr
= rec
->loaddr
;
1318 err
= idt82p33_check_and_set_masks(idt82p33
, page
,
1323 /* Page size 128, last 4 bytes of page skipped */
1327 err
= idt82p33_write(idt82p33
, REG_ADDR(page
, loaddr
),
1335 idt82p33_display_masks(idt82p33
);
1337 release_firmware(fw
);
1341 static void idt82p33_extts_check(struct work_struct
*work
)
1343 struct idt82p33
*idt82p33
= container_of(work
, struct idt82p33
,
1345 struct idt82p33_channel
*channel
;
1350 if (idt82p33
->extts_mask
== 0)
1353 mutex_lock(idt82p33
->lock
);
1355 for (i
= 0; i
< MAX_PHC_PLL
; i
++) {
1358 if ((idt82p33
->extts_mask
& mask
) == 0)
1361 err
= idt82p33_extts_check_channel(idt82p33
, i
);
1364 /* trigger clears itself, so clear the mask */
1365 if (idt82p33
->extts_single_shot
) {
1366 idt82p33
->extts_mask
&= ~mask
;
1369 channel
= &idt82p33
->channel
[i
];
1370 arm_tod_read_with_trigger(channel
, channel
->tod_trigger
);
1375 if (idt82p33
->extts_mask
)
1376 schedule_delayed_work(&idt82p33
->extts_work
,
1377 msecs_to_jiffies(EXTTS_PERIOD_MS
));
1379 mutex_unlock(idt82p33
->lock
);
1382 static int idt82p33_probe(struct platform_device
*pdev
)
1384 struct rsmu_ddata
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
1385 struct idt82p33
*idt82p33
;
1389 idt82p33
= devm_kzalloc(&pdev
->dev
,
1390 sizeof(struct idt82p33
), GFP_KERNEL
);
1394 idt82p33
->dev
= &pdev
->dev
;
1395 idt82p33
->mfd
= pdev
->dev
.parent
;
1396 idt82p33
->lock
= &ddata
->lock
;
1397 idt82p33
->regmap
= ddata
->regmap
;
1398 idt82p33
->tod_write_overhead_ns
= 0;
1399 idt82p33
->calculate_overhead_flag
= 0;
1400 idt82p33
->pll_mask
= DEFAULT_PLL_MASK
;
1401 idt82p33
->channel
[0].output_mask
= DEFAULT_OUTPUT_MASK_PLL0
;
1402 idt82p33
->channel
[1].output_mask
= DEFAULT_OUTPUT_MASK_PLL1
;
1403 idt82p33
->extts_mask
= 0;
1404 INIT_DELAYED_WORK(&idt82p33
->extts_work
, idt82p33_extts_check
);
1406 mutex_lock(idt82p33
->lock
);
1408 /* cold reset before loading firmware */
1409 idt82p33_reset(idt82p33
, true);
1411 err
= idt82p33_load_firmware(idt82p33
);
1413 dev_warn(idt82p33
->dev
,
1414 "loading firmware failed with %d\n", err
);
1416 /* soft reset after loading firmware */
1417 idt82p33_reset(idt82p33
, false);
1419 if (idt82p33
->pll_mask
) {
1420 for (i
= 0; i
< MAX_PHC_PLL
; i
++) {
1421 if (idt82p33
->pll_mask
& (1 << i
))
1422 err
= idt82p33_enable_channel(idt82p33
, i
);
1424 err
= idt82p33_channel_init(idt82p33
, i
);
1426 dev_err(idt82p33
->dev
,
1427 "Failed in %s with err %d!\n",
1433 dev_err(idt82p33
->dev
,
1434 "no PLLs flagged as PHCs, nothing to do\n");
1438 mutex_unlock(idt82p33
->lock
);
1441 idt82p33_ptp_clock_unregister_all(idt82p33
);
1445 platform_set_drvdata(pdev
, idt82p33
);
1450 static void idt82p33_remove(struct platform_device
*pdev
)
1452 struct idt82p33
*idt82p33
= platform_get_drvdata(pdev
);
1454 cancel_delayed_work_sync(&idt82p33
->extts_work
);
1456 idt82p33_ptp_clock_unregister_all(idt82p33
);
1459 static struct platform_driver idt82p33_driver
= {
1461 .name
= "82p33x1x-phc",
1463 .probe
= idt82p33_probe
,
1464 .remove
= idt82p33_remove
,
1467 module_platform_driver(idt82p33_driver
);