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 /* Module Parameters */
31 static u32 phase_snap_threshold = SNAP_THRESHOLD_NS;
32 module_param(phase_snap_threshold, uint, 0);
33 MODULE_PARM_DESC(phase_snap_threshold,
34 "threshold (10000ns by default) below which adjtime would use double dco");
36 static char *firmware;
37 module_param(firmware, charp, 0);
39 static inline int idt82p33_read(struct idt82p33 *idt82p33, u16 regaddr,
42 return regmap_bulk_read(idt82p33->regmap, regaddr, buf, count);
45 static inline int idt82p33_write(struct idt82p33 *idt82p33, u16 regaddr,
48 return regmap_bulk_write(idt82p33->regmap, regaddr, buf, count);
51 static void idt82p33_byte_array_to_timespec(struct timespec64 *ts,
52 u8 buf[TOD_BYTE_COUNT])
59 for (i = 0; i < 3; i++) {
65 for (i = 0; i < 5; i++) {
74 static void idt82p33_timespec_to_byte_array(struct timespec64 const *ts,
75 u8 buf[TOD_BYTE_COUNT])
84 for (i = 0; i < 4; i++) {
89 for (i = 4; i < TOD_BYTE_COUNT; i++) {
95 static int idt82p33_dpll_set_mode(struct idt82p33_channel *channel,
98 struct idt82p33 *idt82p33 = channel->idt82p33;
102 if (channel->pll_mode == mode)
105 err = idt82p33_read(idt82p33, channel->dpll_mode_cnfg,
106 &dpll_mode, sizeof(dpll_mode));
110 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
112 dpll_mode |= (mode << PLL_MODE_SHIFT);
114 err = idt82p33_write(idt82p33, channel->dpll_mode_cnfg,
115 &dpll_mode, sizeof(dpll_mode));
119 channel->pll_mode = mode;
124 static int _idt82p33_gettime(struct idt82p33_channel *channel,
125 struct timespec64 *ts)
127 struct idt82p33 *idt82p33 = channel->idt82p33;
128 u8 buf[TOD_BYTE_COUNT];
132 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
133 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
136 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
137 &trigger, sizeof(trigger));
142 if (idt82p33->calculate_overhead_flag)
143 idt82p33->start_time = ktime_get_raw();
145 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
150 idt82p33_byte_array_to_timespec(ts, buf);
157 * Bits[7:4] Write 0x9, MSB write
158 * Bits[3:0] Read 0x9, LSB read
161 static int _idt82p33_settime(struct idt82p33_channel *channel,
162 struct timespec64 const *ts)
164 struct idt82p33 *idt82p33 = channel->idt82p33;
165 struct timespec64 local_ts = *ts;
166 char buf[TOD_BYTE_COUNT];
167 s64 dynamic_overhead_ns;
168 unsigned char trigger;
172 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
173 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
175 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
176 &trigger, sizeof(trigger));
181 if (idt82p33->calculate_overhead_flag) {
182 dynamic_overhead_ns = ktime_to_ns(ktime_get_raw())
183 - ktime_to_ns(idt82p33->start_time);
185 timespec64_add_ns(&local_ts, dynamic_overhead_ns);
187 idt82p33->calculate_overhead_flag = 0;
190 idt82p33_timespec_to_byte_array(&local_ts, buf);
193 * Store the new time value.
195 for (i = 0; i < TOD_BYTE_COUNT; i++) {
196 err = idt82p33_write(idt82p33, channel->dpll_tod_cnfg + i,
197 &buf[i], sizeof(buf[i]));
205 static int _idt82p33_adjtime(struct idt82p33_channel *channel, s64 delta_ns)
207 struct idt82p33 *idt82p33 = channel->idt82p33;
208 struct timespec64 ts;
212 idt82p33->calculate_overhead_flag = 1;
214 err = _idt82p33_gettime(channel, &ts);
219 now_ns = timespec64_to_ns(&ts);
220 now_ns += delta_ns + idt82p33->tod_write_overhead_ns;
222 ts = ns_to_timespec64(now_ns);
224 err = _idt82p33_settime(channel, &ts);
229 static int _idt82p33_adjfine(struct idt82p33_channel *channel, long scaled_ppm)
231 struct idt82p33 *idt82p33 = channel->idt82p33;
232 unsigned char buf[5] = {0};
236 if (scaled_ppm == channel->current_freq_ppb)
240 * Frequency Control Word unit is: 1.68 * 10^-10 ppm
249 * FCW = -------------
253 fcw = scaled_ppm * 244140625ULL;
254 fcw = div_s64(fcw, 2688);
256 for (i = 0; i < 5; i++) {
261 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
266 err = idt82p33_write(idt82p33, channel->dpll_freq_cnfg,
270 channel->current_freq_ppb = scaled_ppm;
275 static int idt82p33_measure_one_byte_write_overhead(
276 struct idt82p33_channel *channel, s64 *overhead_ns)
278 struct idt82p33 *idt82p33 = channel->idt82p33;
287 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
288 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
290 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
292 start = ktime_get_raw();
294 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
295 &trigger, sizeof(trigger));
297 stop = ktime_get_raw();
302 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
305 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
310 static int idt82p33_measure_tod_write_9_byte_overhead(
311 struct idt82p33_channel *channel)
313 struct idt82p33 *idt82p33 = channel->idt82p33;
314 u8 buf[TOD_BYTE_COUNT];
321 idt82p33->tod_write_overhead_ns = 0;
323 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
325 start = ktime_get_raw();
327 /* Need one less byte for applicable overhead */
328 for (j = 0; j < (TOD_BYTE_COUNT - 1); j++) {
329 err = idt82p33_write(idt82p33,
330 channel->dpll_tod_cnfg + i,
331 &buf[i], sizeof(buf[i]));
336 stop = ktime_get_raw();
338 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
341 idt82p33->tod_write_overhead_ns = div_s64(total_ns,
342 MAX_MEASURMENT_COUNT);
347 static int idt82p33_measure_settime_gettime_gap_overhead(
348 struct idt82p33_channel *channel, s64 *overhead_ns)
350 struct timespec64 ts1 = {0, 0};
351 struct timespec64 ts2;
356 err = _idt82p33_settime(channel, &ts1);
361 err = _idt82p33_gettime(channel, &ts2);
364 *overhead_ns = timespec64_to_ns(&ts2) - timespec64_to_ns(&ts1);
369 static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel *channel)
371 s64 trailing_overhead_ns, one_byte_write_ns, gap_ns;
372 struct idt82p33 *idt82p33 = channel->idt82p33;
375 idt82p33->tod_write_overhead_ns = 0;
377 err = idt82p33_measure_settime_gettime_gap_overhead(channel, &gap_ns);
380 dev_err(idt82p33->dev,
381 "Failed in %s with err %d!\n", __func__, err);
385 err = idt82p33_measure_one_byte_write_overhead(channel,
391 err = idt82p33_measure_tod_write_9_byte_overhead(channel);
396 trailing_overhead_ns = gap_ns - (2 * one_byte_write_ns);
398 idt82p33->tod_write_overhead_ns -= trailing_overhead_ns;
403 static int idt82p33_check_and_set_masks(struct idt82p33 *idt82p33,
410 if (page == PLLMASK_ADDR_HI && offset == PLLMASK_ADDR_LO) {
411 if ((val & 0xfc) || !(val & 0x3)) {
412 dev_err(idt82p33->dev,
413 "Invalid PLL mask 0x%x\n", val);
416 idt82p33->pll_mask = val;
418 } else if (page == PLL0_OUTMASK_ADDR_HI &&
419 offset == PLL0_OUTMASK_ADDR_LO) {
420 idt82p33->channel[0].output_mask = val;
421 } else if (page == PLL1_OUTMASK_ADDR_HI &&
422 offset == PLL1_OUTMASK_ADDR_LO) {
423 idt82p33->channel[1].output_mask = val;
429 static void idt82p33_display_masks(struct idt82p33 *idt82p33)
433 dev_info(idt82p33->dev,
434 "pllmask = 0x%02x\n", idt82p33->pll_mask);
436 for (i = 0; i < MAX_PHC_PLL; i++) {
439 if (mask & idt82p33->pll_mask)
440 dev_info(idt82p33->dev,
441 "PLL%d output_mask = 0x%04x\n",
442 i, idt82p33->channel[i].output_mask);
446 static int idt82p33_sync_tod(struct idt82p33_channel *channel, bool enable)
448 struct idt82p33 *idt82p33 = channel->idt82p33;
452 err = idt82p33_read(idt82p33, channel->dpll_sync_cnfg,
453 &sync_cnfg, sizeof(sync_cnfg));
457 sync_cnfg &= ~SYNC_TOD;
459 sync_cnfg |= SYNC_TOD;
461 return idt82p33_write(idt82p33, channel->dpll_sync_cnfg,
462 &sync_cnfg, sizeof(sync_cnfg));
465 static int idt82p33_output_enable(struct idt82p33_channel *channel,
466 bool enable, unsigned int outn)
468 struct idt82p33 *idt82p33 = channel->idt82p33;
472 err = idt82p33_read(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
476 val &= ~SQUELCH_ENABLE;
478 val |= SQUELCH_ENABLE;
480 return idt82p33_write(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
483 static int idt82p33_output_mask_enable(struct idt82p33_channel *channel,
490 mask = channel->output_mask;
495 err = idt82p33_output_enable(channel, enable, outn);
507 static int idt82p33_perout_enable(struct idt82p33_channel *channel,
509 struct ptp_perout_request *perout)
511 unsigned int flags = perout->flags;
513 /* Enable/disable output based on output_mask */
514 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
515 return idt82p33_output_mask_enable(channel, enable);
517 /* Enable/disable individual output instead */
518 return idt82p33_output_enable(channel, enable, perout->index);
521 static int idt82p33_enable_tod(struct idt82p33_channel *channel)
523 struct idt82p33 *idt82p33 = channel->idt82p33;
524 struct timespec64 ts = {0, 0};
527 err = idt82p33_measure_tod_write_overhead(channel);
530 dev_err(idt82p33->dev,
531 "Failed in %s with err %d!\n", __func__, err);
535 err = _idt82p33_settime(channel, &ts);
540 return idt82p33_sync_tod(channel, true);
543 static void idt82p33_ptp_clock_unregister_all(struct idt82p33 *idt82p33)
545 struct idt82p33_channel *channel;
548 for (i = 0; i < MAX_PHC_PLL; i++) {
550 channel = &idt82p33->channel[i];
552 if (channel->ptp_clock)
553 ptp_clock_unregister(channel->ptp_clock);
557 static int idt82p33_enable(struct ptp_clock_info *ptp,
558 struct ptp_clock_request *rq, int on)
560 struct idt82p33_channel *channel =
561 container_of(ptp, struct idt82p33_channel, caps);
562 struct idt82p33 *idt82p33 = channel->idt82p33;
563 int err = -EOPNOTSUPP;
565 mutex_lock(idt82p33->lock);
567 if (rq->type == PTP_CLK_REQ_PEROUT) {
569 err = idt82p33_perout_enable(channel, false,
571 /* Only accept a 1-PPS aligned to the second. */
572 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
573 rq->perout.period.nsec)
576 err = idt82p33_perout_enable(channel, true,
580 mutex_unlock(idt82p33->lock);
583 dev_err(idt82p33->dev,
584 "Failed in %s with err %d!\n", __func__, err);
588 static int idt82p33_adjwritephase(struct ptp_clock_info *ptp, s32 offset_ns)
590 struct idt82p33_channel *channel =
591 container_of(ptp, struct idt82p33_channel, caps);
592 struct idt82p33 *idt82p33 = channel->idt82p33;
593 s64 offset_regval, offset_fs;
597 offset_fs = (s64)(-offset_ns) * 1000000;
599 if (offset_fs > WRITE_PHASE_OFFSET_LIMIT)
600 offset_fs = WRITE_PHASE_OFFSET_LIMIT;
601 else if (offset_fs < -WRITE_PHASE_OFFSET_LIMIT)
602 offset_fs = -WRITE_PHASE_OFFSET_LIMIT;
604 /* Convert from phaseoffset_fs to register value */
605 offset_regval = div_s64(offset_fs * 1000, IDT_T0DPLL_PHASE_RESOL);
607 val[0] = offset_regval & 0xFF;
608 val[1] = (offset_regval >> 8) & 0xFF;
609 val[2] = (offset_regval >> 16) & 0xFF;
610 val[3] = (offset_regval >> 24) & 0x1F;
611 val[3] |= PH_OFFSET_EN;
613 mutex_lock(idt82p33->lock);
615 err = idt82p33_dpll_set_mode(channel, PLL_MODE_WPH);
617 dev_err(idt82p33->dev,
618 "Failed in %s with err %d!\n", __func__, err);
622 err = idt82p33_write(idt82p33, channel->dpll_phase_cnfg, val,
626 mutex_unlock(idt82p33->lock);
630 static int idt82p33_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
632 struct idt82p33_channel *channel =
633 container_of(ptp, struct idt82p33_channel, caps);
634 struct idt82p33 *idt82p33 = channel->idt82p33;
637 mutex_lock(idt82p33->lock);
638 err = _idt82p33_adjfine(channel, scaled_ppm);
639 mutex_unlock(idt82p33->lock);
641 dev_err(idt82p33->dev,
642 "Failed in %s with err %d!\n", __func__, err);
647 static int idt82p33_adjtime(struct ptp_clock_info *ptp, s64 delta_ns)
649 struct idt82p33_channel *channel =
650 container_of(ptp, struct idt82p33_channel, caps);
651 struct idt82p33 *idt82p33 = channel->idt82p33;
654 mutex_lock(idt82p33->lock);
656 if (abs(delta_ns) < phase_snap_threshold) {
657 mutex_unlock(idt82p33->lock);
661 err = _idt82p33_adjtime(channel, delta_ns);
663 mutex_unlock(idt82p33->lock);
666 dev_err(idt82p33->dev,
667 "Failed in %s with err %d!\n", __func__, err);
671 static int idt82p33_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
673 struct idt82p33_channel *channel =
674 container_of(ptp, struct idt82p33_channel, caps);
675 struct idt82p33 *idt82p33 = channel->idt82p33;
678 mutex_lock(idt82p33->lock);
679 err = _idt82p33_gettime(channel, ts);
680 mutex_unlock(idt82p33->lock);
683 dev_err(idt82p33->dev,
684 "Failed in %s with err %d!\n", __func__, err);
688 static int idt82p33_settime(struct ptp_clock_info *ptp,
689 const struct timespec64 *ts)
691 struct idt82p33_channel *channel =
692 container_of(ptp, struct idt82p33_channel, caps);
693 struct idt82p33 *idt82p33 = channel->idt82p33;
696 mutex_lock(idt82p33->lock);
697 err = _idt82p33_settime(channel, ts);
698 mutex_unlock(idt82p33->lock);
701 dev_err(idt82p33->dev,
702 "Failed in %s with err %d!\n", __func__, err);
706 static int idt82p33_channel_init(struct idt82p33_channel *channel, int index)
710 channel->dpll_tod_cnfg = DPLL1_TOD_CNFG;
711 channel->dpll_tod_trigger = DPLL1_TOD_TRIGGER;
712 channel->dpll_tod_sts = DPLL1_TOD_STS;
713 channel->dpll_mode_cnfg = DPLL1_OPERATING_MODE_CNFG;
714 channel->dpll_freq_cnfg = DPLL1_HOLDOVER_FREQ_CNFG;
715 channel->dpll_phase_cnfg = DPLL1_PHASE_OFFSET_CNFG;
716 channel->dpll_sync_cnfg = DPLL1_SYNC_EDGE_CNFG;
717 channel->dpll_input_mode_cnfg = DPLL1_INPUT_MODE_CNFG;
720 channel->dpll_tod_cnfg = DPLL2_TOD_CNFG;
721 channel->dpll_tod_trigger = DPLL2_TOD_TRIGGER;
722 channel->dpll_tod_sts = DPLL2_TOD_STS;
723 channel->dpll_mode_cnfg = DPLL2_OPERATING_MODE_CNFG;
724 channel->dpll_freq_cnfg = DPLL2_HOLDOVER_FREQ_CNFG;
725 channel->dpll_phase_cnfg = DPLL2_PHASE_OFFSET_CNFG;
726 channel->dpll_sync_cnfg = DPLL2_SYNC_EDGE_CNFG;
727 channel->dpll_input_mode_cnfg = DPLL2_INPUT_MODE_CNFG;
733 channel->current_freq_ppb = 0;
738 static void idt82p33_caps_init(struct ptp_clock_info *caps)
740 caps->owner = THIS_MODULE;
741 caps->max_adj = DCO_MAX_PPB;
742 caps->n_per_out = 11;
743 caps->adjphase = idt82p33_adjwritephase;
744 caps->adjfine = idt82p33_adjfine;
745 caps->adjtime = idt82p33_adjtime;
746 caps->gettime64 = idt82p33_gettime;
747 caps->settime64 = idt82p33_settime;
748 caps->enable = idt82p33_enable;
751 static int idt82p33_enable_channel(struct idt82p33 *idt82p33, u32 index)
753 struct idt82p33_channel *channel;
756 if (!(index < MAX_PHC_PLL))
759 channel = &idt82p33->channel[index];
761 err = idt82p33_channel_init(channel, index);
763 dev_err(idt82p33->dev,
764 "Channel_init failed in %s with err %d!\n",
769 channel->idt82p33 = idt82p33;
771 idt82p33_caps_init(&channel->caps);
772 snprintf(channel->caps.name, sizeof(channel->caps.name),
773 "IDT 82P33 PLL%u", index);
775 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
777 if (IS_ERR(channel->ptp_clock)) {
778 err = PTR_ERR(channel->ptp_clock);
779 channel->ptp_clock = NULL;
783 if (!channel->ptp_clock)
786 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
788 dev_err(idt82p33->dev,
789 "Dpll_set_mode failed in %s with err %d!\n",
794 err = idt82p33_enable_tod(channel);
796 dev_err(idt82p33->dev,
797 "Enable_tod failed in %s with err %d!\n",
802 dev_info(idt82p33->dev, "PLL%d registered as ptp%d\n",
803 index, channel->ptp_clock->index);
808 static int idt82p33_load_firmware(struct idt82p33 *idt82p33)
810 const struct firmware *fw;
811 struct idt82p33_fwrc *rec;
812 u8 loaddr, page, val;
816 dev_dbg(idt82p33->dev, "requesting firmware '%s'\n", FW_FILENAME);
818 err = request_firmware(&fw, FW_FILENAME, idt82p33->dev);
821 dev_err(idt82p33->dev,
822 "Failed in %s with err %d!\n", __func__, err);
826 dev_dbg(idt82p33->dev, "firmware size %zu bytes\n", fw->size);
828 rec = (struct idt82p33_fwrc *) fw->data;
830 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
833 dev_err(idt82p33->dev,
834 "bad firmware, reserved field non-zero\n");
838 loaddr = rec->loaddr;
843 err = idt82p33_check_and_set_masks(idt82p33, page,
848 /* Page size 128, last 4 bytes of page skipped */
852 err = idt82p33_write(idt82p33, REG_ADDR(page, loaddr),
860 idt82p33_display_masks(idt82p33);
862 release_firmware(fw);
867 static int idt82p33_probe(struct platform_device *pdev)
869 struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
870 struct idt82p33 *idt82p33;
874 idt82p33 = devm_kzalloc(&pdev->dev,
875 sizeof(struct idt82p33), GFP_KERNEL);
879 idt82p33->dev = &pdev->dev;
880 idt82p33->mfd = pdev->dev.parent;
881 idt82p33->lock = &ddata->lock;
882 idt82p33->regmap = ddata->regmap;
883 idt82p33->tod_write_overhead_ns = 0;
884 idt82p33->calculate_overhead_flag = 0;
885 idt82p33->pll_mask = DEFAULT_PLL_MASK;
886 idt82p33->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
887 idt82p33->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
889 mutex_lock(idt82p33->lock);
891 err = idt82p33_load_firmware(idt82p33);
894 dev_warn(idt82p33->dev,
895 "loading firmware failed with %d\n", err);
897 if (idt82p33->pll_mask) {
898 for (i = 0; i < MAX_PHC_PLL; i++) {
899 if (idt82p33->pll_mask & (1 << i)) {
900 err = idt82p33_enable_channel(idt82p33, i);
902 dev_err(idt82p33->dev,
903 "Failed in %s with err %d!\n",
910 dev_err(idt82p33->dev,
911 "no PLLs flagged as PHCs, nothing to do\n");
915 mutex_unlock(idt82p33->lock);
918 idt82p33_ptp_clock_unregister_all(idt82p33);
922 platform_set_drvdata(pdev, idt82p33);
927 static int idt82p33_remove(struct platform_device *pdev)
929 struct idt82p33 *idt82p33 = platform_get_drvdata(pdev);
931 idt82p33_ptp_clock_unregister_all(idt82p33);
936 static struct platform_driver idt82p33_driver = {
938 .name = "82p33x1x-phc",
940 .probe = idt82p33_probe,
941 .remove = idt82p33_remove,
944 module_platform_driver(idt82p33_driver);