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>
10 #include <linux/module.h>
11 #include <linux/ptp_clock_kernel.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/timekeeping.h>
15 #include <linux/bitops.h>
17 #include "ptp_private.h"
18 #include "ptp_idt82p33.h"
20 MODULE_DESCRIPTION("Driver for IDT 82p33xxx clock devices");
21 MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
22 MODULE_VERSION("1.0");
23 MODULE_LICENSE("GPL");
24 MODULE_FIRMWARE(FW_FILENAME);
26 /* Module Parameters */
27 static u32 sync_tod_timeout = SYNC_TOD_TIMEOUT_SEC;
28 module_param(sync_tod_timeout, uint, 0);
29 MODULE_PARM_DESC(sync_tod_timeout,
30 "duration in second to keep SYNC_TOD on (set to 0 to keep it always on)");
32 static u32 phase_snap_threshold = SNAP_THRESHOLD_NS;
33 module_param(phase_snap_threshold, uint, 0);
34 MODULE_PARM_DESC(phase_snap_threshold,
35 "threshold (150000ns by default) below which adjtime would ignore");
37 static void idt82p33_byte_array_to_timespec(struct timespec64 *ts,
38 u8 buf[TOD_BYTE_COUNT])
45 for (i = 0; i < 3; i++) {
51 for (i = 0; i < 5; i++) {
60 static void idt82p33_timespec_to_byte_array(struct timespec64 const *ts,
61 u8 buf[TOD_BYTE_COUNT])
70 for (i = 0; i < 4; i++) {
75 for (i = 4; i < TOD_BYTE_COUNT; i++) {
81 static int idt82p33_xfer_read(struct idt82p33 *idt82p33,
82 unsigned char regaddr,
86 struct i2c_client *client = idt82p33->client;
87 struct i2c_msg msg[2];
90 msg[0].addr = client->addr;
93 msg[0].buf = ®addr;
95 msg[1].addr = client->addr;
96 msg[1].flags = I2C_M_RD;
100 cnt = i2c_transfer(client->adapter, msg, 2);
102 dev_err(&client->dev, "i2c_transfer returned %d\n", cnt);
104 } else if (cnt != 2) {
105 dev_err(&client->dev,
106 "i2c_transfer sent only %d of %d messages\n", cnt, 2);
112 static int idt82p33_xfer_write(struct idt82p33 *idt82p33,
117 struct i2c_client *client = idt82p33->client;
118 /* we add 1 byte for device register */
119 u8 msg[IDT82P33_MAX_WRITE_COUNT + 1];
122 if (count > IDT82P33_MAX_WRITE_COUNT)
126 memcpy(&msg[1], buf, count);
128 err = i2c_master_send(client, msg, count + 1);
130 dev_err(&client->dev, "i2c_master_send returned %d\n", err);
137 static int idt82p33_page_offset(struct idt82p33 *idt82p33, unsigned char val)
141 if (idt82p33->page_offset == val)
144 err = idt82p33_xfer_write(idt82p33, PAGE_ADDR, &val, sizeof(val));
146 dev_err(&idt82p33->client->dev,
147 "failed to set page offset %d\n", val);
149 idt82p33->page_offset = val;
154 static int idt82p33_rdwr(struct idt82p33 *idt82p33, unsigned int regaddr,
155 unsigned char *buf, unsigned int count, bool write)
160 page = _PAGE(regaddr);
161 offset = _OFFSET(regaddr);
163 err = idt82p33_page_offset(idt82p33, page);
168 return idt82p33_xfer_write(idt82p33, offset, buf, count);
170 return idt82p33_xfer_read(idt82p33, offset, buf, count);
173 static int idt82p33_read(struct idt82p33 *idt82p33, unsigned int regaddr,
174 unsigned char *buf, unsigned int count)
176 return idt82p33_rdwr(idt82p33, regaddr, buf, count, false);
179 static int idt82p33_write(struct idt82p33 *idt82p33, unsigned int regaddr,
180 unsigned char *buf, unsigned int count)
182 return idt82p33_rdwr(idt82p33, regaddr, buf, count, true);
185 static int idt82p33_dpll_set_mode(struct idt82p33_channel *channel,
188 struct idt82p33 *idt82p33 = channel->idt82p33;
192 if (channel->pll_mode == mode)
195 err = idt82p33_read(idt82p33, channel->dpll_mode_cnfg,
196 &dpll_mode, sizeof(dpll_mode));
200 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
202 dpll_mode |= (mode << PLL_MODE_SHIFT);
204 err = idt82p33_write(idt82p33, channel->dpll_mode_cnfg,
205 &dpll_mode, sizeof(dpll_mode));
209 channel->pll_mode = dpll_mode;
214 static int _idt82p33_gettime(struct idt82p33_channel *channel,
215 struct timespec64 *ts)
217 struct idt82p33 *idt82p33 = channel->idt82p33;
218 u8 buf[TOD_BYTE_COUNT];
222 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
223 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
226 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
227 &trigger, sizeof(trigger));
232 if (idt82p33->calculate_overhead_flag)
233 idt82p33->start_time = ktime_get_raw();
235 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
240 idt82p33_byte_array_to_timespec(ts, buf);
247 * Bits[7:4] Write 0x9, MSB write
248 * Bits[3:0] Read 0x9, LSB read
251 static int _idt82p33_settime(struct idt82p33_channel *channel,
252 struct timespec64 const *ts)
254 struct idt82p33 *idt82p33 = channel->idt82p33;
255 struct timespec64 local_ts = *ts;
256 char buf[TOD_BYTE_COUNT];
257 s64 dynamic_overhead_ns;
258 unsigned char trigger;
262 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
263 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
265 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
266 &trigger, sizeof(trigger));
271 if (idt82p33->calculate_overhead_flag) {
272 dynamic_overhead_ns = ktime_to_ns(ktime_get_raw())
273 - ktime_to_ns(idt82p33->start_time);
275 timespec64_add_ns(&local_ts, dynamic_overhead_ns);
277 idt82p33->calculate_overhead_flag = 0;
280 idt82p33_timespec_to_byte_array(&local_ts, buf);
283 * Store the new time value.
285 for (i = 0; i < TOD_BYTE_COUNT; i++) {
286 err = idt82p33_write(idt82p33, channel->dpll_tod_cnfg + i,
287 &buf[i], sizeof(buf[i]));
295 static int _idt82p33_adjtime(struct idt82p33_channel *channel, s64 delta_ns)
297 struct idt82p33 *idt82p33 = channel->idt82p33;
298 struct timespec64 ts;
302 idt82p33->calculate_overhead_flag = 1;
304 err = _idt82p33_gettime(channel, &ts);
309 now_ns = timespec64_to_ns(&ts);
310 now_ns += delta_ns + idt82p33->tod_write_overhead_ns;
312 ts = ns_to_timespec64(now_ns);
314 err = _idt82p33_settime(channel, &ts);
319 static int _idt82p33_adjfine(struct idt82p33_channel *channel, long scaled_ppm)
321 struct idt82p33 *idt82p33 = channel->idt82p33;
322 unsigned char buf[5] = {0};
327 if (scaled_ppm == channel->current_freq_ppb)
331 * Frequency Control Word unit is: 1.68 * 10^-10 ppm
340 * FCW = -------------
343 if (scaled_ppm < 0) {
345 scaled_ppm = -scaled_ppm;
348 fcw = scaled_ppm * 244140625ULL;
349 fcw = div_u64(fcw, 2688);
354 for (i = 0; i < 5; i++) {
359 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
364 err = idt82p33_write(idt82p33, channel->dpll_freq_cnfg,
368 channel->current_freq_ppb = scaled_ppm;
373 static int idt82p33_measure_one_byte_write_overhead(
374 struct idt82p33_channel *channel, s64 *overhead_ns)
376 struct idt82p33 *idt82p33 = channel->idt82p33;
385 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
386 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
388 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
390 start = ktime_get_raw();
392 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
393 &trigger, sizeof(trigger));
395 stop = ktime_get_raw();
400 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
403 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
408 static int idt82p33_measure_tod_write_9_byte_overhead(
409 struct idt82p33_channel *channel)
411 struct idt82p33 *idt82p33 = channel->idt82p33;
412 u8 buf[TOD_BYTE_COUNT];
419 idt82p33->tod_write_overhead_ns = 0;
421 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
423 start = ktime_get_raw();
425 /* Need one less byte for applicable overhead */
426 for (j = 0; j < (TOD_BYTE_COUNT - 1); j++) {
427 err = idt82p33_write(idt82p33,
428 channel->dpll_tod_cnfg + i,
429 &buf[i], sizeof(buf[i]));
434 stop = ktime_get_raw();
436 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
439 idt82p33->tod_write_overhead_ns = div_s64(total_ns,
440 MAX_MEASURMENT_COUNT);
445 static int idt82p33_measure_settime_gettime_gap_overhead(
446 struct idt82p33_channel *channel, s64 *overhead_ns)
448 struct timespec64 ts1 = {0, 0};
449 struct timespec64 ts2;
454 err = _idt82p33_settime(channel, &ts1);
459 err = _idt82p33_gettime(channel, &ts2);
462 *overhead_ns = timespec64_to_ns(&ts2) - timespec64_to_ns(&ts1);
467 static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel *channel)
469 s64 trailing_overhead_ns, one_byte_write_ns, gap_ns;
470 struct idt82p33 *idt82p33 = channel->idt82p33;
473 idt82p33->tod_write_overhead_ns = 0;
475 err = idt82p33_measure_settime_gettime_gap_overhead(channel, &gap_ns);
478 dev_err(&idt82p33->client->dev,
479 "Failed in %s with err %d!\n", __func__, err);
483 err = idt82p33_measure_one_byte_write_overhead(channel,
489 err = idt82p33_measure_tod_write_9_byte_overhead(channel);
494 trailing_overhead_ns = gap_ns - (2 * one_byte_write_ns);
496 idt82p33->tod_write_overhead_ns -= trailing_overhead_ns;
501 static int idt82p33_check_and_set_masks(struct idt82p33 *idt82p33,
508 if (page == PLLMASK_ADDR_HI && offset == PLLMASK_ADDR_LO) {
509 if ((val & 0xfc) || !(val & 0x3)) {
510 dev_err(&idt82p33->client->dev,
511 "Invalid PLL mask 0x%hhx\n", val);
514 idt82p33->pll_mask = val;
516 } else if (page == PLL0_OUTMASK_ADDR_HI &&
517 offset == PLL0_OUTMASK_ADDR_LO) {
518 idt82p33->channel[0].output_mask = val;
519 } else if (page == PLL1_OUTMASK_ADDR_HI &&
520 offset == PLL1_OUTMASK_ADDR_LO) {
521 idt82p33->channel[1].output_mask = val;
527 static void idt82p33_display_masks(struct idt82p33 *idt82p33)
531 dev_info(&idt82p33->client->dev,
532 "pllmask = 0x%02x\n", idt82p33->pll_mask);
534 for (i = 0; i < MAX_PHC_PLL; i++) {
537 if (mask & idt82p33->pll_mask)
538 dev_info(&idt82p33->client->dev,
539 "PLL%d output_mask = 0x%04x\n",
540 i, idt82p33->channel[i].output_mask);
544 static int idt82p33_sync_tod(struct idt82p33_channel *channel, bool enable)
546 struct idt82p33 *idt82p33 = channel->idt82p33;
550 /* Turn it off after sync_tod_timeout seconds */
551 if (enable && sync_tod_timeout)
552 ptp_schedule_worker(channel->ptp_clock,
553 sync_tod_timeout * HZ);
555 err = idt82p33_read(idt82p33, channel->dpll_sync_cnfg,
556 &sync_cnfg, sizeof(sync_cnfg));
560 sync_cnfg &= ~SYNC_TOD;
562 sync_cnfg |= SYNC_TOD;
564 return idt82p33_write(idt82p33, channel->dpll_sync_cnfg,
565 &sync_cnfg, sizeof(sync_cnfg));
568 static long idt82p33_sync_tod_work_handler(struct ptp_clock_info *ptp)
570 struct idt82p33_channel *channel =
571 container_of(ptp, struct idt82p33_channel, caps);
572 struct idt82p33 *idt82p33 = channel->idt82p33;
574 mutex_lock(&idt82p33->reg_lock);
576 (void)idt82p33_sync_tod(channel, false);
578 mutex_unlock(&idt82p33->reg_lock);
580 /* Return a negative value here to not reschedule */
584 static int idt82p33_output_enable(struct idt82p33_channel *channel,
585 bool enable, unsigned int outn)
587 struct idt82p33 *idt82p33 = channel->idt82p33;
591 err = idt82p33_read(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
595 val &= ~SQUELCH_ENABLE;
597 val |= SQUELCH_ENABLE;
599 return idt82p33_write(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
602 static int idt82p33_output_mask_enable(struct idt82p33_channel *channel,
609 mask = channel->output_mask;
614 err = idt82p33_output_enable(channel, enable, outn);
626 static int idt82p33_perout_enable(struct idt82p33_channel *channel,
628 struct ptp_perout_request *perout)
630 unsigned int flags = perout->flags;
632 /* Enable/disable output based on output_mask */
633 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
634 return idt82p33_output_mask_enable(channel, enable);
636 /* Enable/disable individual output instead */
637 return idt82p33_output_enable(channel, enable, perout->index);
640 static int idt82p33_enable_tod(struct idt82p33_channel *channel)
642 struct idt82p33 *idt82p33 = channel->idt82p33;
643 struct timespec64 ts = {0, 0};
648 err = idt82p33_write(idt82p33, channel->dpll_input_mode_cnfg,
653 err = idt82p33_measure_tod_write_overhead(channel);
656 dev_err(&idt82p33->client->dev,
657 "Failed in %s with err %d!\n", __func__, err);
661 err = _idt82p33_settime(channel, &ts);
666 return idt82p33_sync_tod(channel, true);
669 static void idt82p33_ptp_clock_unregister_all(struct idt82p33 *idt82p33)
671 struct idt82p33_channel *channel;
674 for (i = 0; i < MAX_PHC_PLL; i++) {
676 channel = &idt82p33->channel[i];
678 if (channel->ptp_clock)
679 ptp_clock_unregister(channel->ptp_clock);
683 static int idt82p33_enable(struct ptp_clock_info *ptp,
684 struct ptp_clock_request *rq, int on)
686 struct idt82p33_channel *channel =
687 container_of(ptp, struct idt82p33_channel, caps);
688 struct idt82p33 *idt82p33 = channel->idt82p33;
693 mutex_lock(&idt82p33->reg_lock);
695 if (rq->type == PTP_CLK_REQ_PEROUT) {
697 err = idt82p33_perout_enable(channel, false,
699 /* Only accept a 1-PPS aligned to the second. */
700 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
701 rq->perout.period.nsec) {
704 err = idt82p33_perout_enable(channel, true,
708 mutex_unlock(&idt82p33->reg_lock);
713 static int idt82p33_adjwritephase(struct ptp_clock_info *ptp, s32 offset_ns)
715 struct idt82p33_channel *channel =
716 container_of(ptp, struct idt82p33_channel, caps);
717 struct idt82p33 *idt82p33 = channel->idt82p33;
718 s64 offset_regval, offset_fs;
722 offset_fs = (s64)(-offset_ns) * 1000000;
724 if (offset_fs > WRITE_PHASE_OFFSET_LIMIT)
725 offset_fs = WRITE_PHASE_OFFSET_LIMIT;
726 else if (offset_fs < -WRITE_PHASE_OFFSET_LIMIT)
727 offset_fs = -WRITE_PHASE_OFFSET_LIMIT;
729 /* Convert from phaseoffset_fs to register value */
730 offset_regval = div_s64(offset_fs * 1000, IDT_T0DPLL_PHASE_RESOL);
732 val[0] = offset_regval & 0xFF;
733 val[1] = (offset_regval >> 8) & 0xFF;
734 val[2] = (offset_regval >> 16) & 0xFF;
735 val[3] = (offset_regval >> 24) & 0x1F;
736 val[3] |= PH_OFFSET_EN;
738 mutex_lock(&idt82p33->reg_lock);
740 err = idt82p33_dpll_set_mode(channel, PLL_MODE_WPH);
742 dev_err(&idt82p33->client->dev,
743 "Failed in %s with err %d!\n", __func__, err);
747 err = idt82p33_write(idt82p33, channel->dpll_phase_cnfg, val,
751 mutex_unlock(&idt82p33->reg_lock);
755 static int idt82p33_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
757 struct idt82p33_channel *channel =
758 container_of(ptp, struct idt82p33_channel, caps);
759 struct idt82p33 *idt82p33 = channel->idt82p33;
762 mutex_lock(&idt82p33->reg_lock);
763 err = _idt82p33_adjfine(channel, scaled_ppm);
765 dev_err(&idt82p33->client->dev,
766 "Failed in %s with err %d!\n", __func__, err);
767 mutex_unlock(&idt82p33->reg_lock);
772 static int idt82p33_adjtime(struct ptp_clock_info *ptp, s64 delta_ns)
774 struct idt82p33_channel *channel =
775 container_of(ptp, struct idt82p33_channel, caps);
776 struct idt82p33 *idt82p33 = channel->idt82p33;
779 mutex_lock(&idt82p33->reg_lock);
781 if (abs(delta_ns) < phase_snap_threshold) {
782 mutex_unlock(&idt82p33->reg_lock);
786 err = _idt82p33_adjtime(channel, delta_ns);
789 mutex_unlock(&idt82p33->reg_lock);
790 dev_err(&idt82p33->client->dev,
791 "Adjtime failed in %s with err %d!\n", __func__, err);
795 err = idt82p33_sync_tod(channel, true);
797 dev_err(&idt82p33->client->dev,
798 "Sync_tod failed in %s with err %d!\n", __func__, err);
800 mutex_unlock(&idt82p33->reg_lock);
805 static int idt82p33_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
807 struct idt82p33_channel *channel =
808 container_of(ptp, struct idt82p33_channel, caps);
809 struct idt82p33 *idt82p33 = channel->idt82p33;
812 mutex_lock(&idt82p33->reg_lock);
813 err = _idt82p33_gettime(channel, ts);
815 dev_err(&idt82p33->client->dev,
816 "Failed in %s with err %d!\n", __func__, err);
817 mutex_unlock(&idt82p33->reg_lock);
822 static int idt82p33_settime(struct ptp_clock_info *ptp,
823 const struct timespec64 *ts)
825 struct idt82p33_channel *channel =
826 container_of(ptp, struct idt82p33_channel, caps);
827 struct idt82p33 *idt82p33 = channel->idt82p33;
830 mutex_lock(&idt82p33->reg_lock);
831 err = _idt82p33_settime(channel, ts);
833 dev_err(&idt82p33->client->dev,
834 "Failed in %s with err %d!\n", __func__, err);
835 mutex_unlock(&idt82p33->reg_lock);
840 static int idt82p33_channel_init(struct idt82p33_channel *channel, int index)
844 channel->dpll_tod_cnfg = DPLL1_TOD_CNFG;
845 channel->dpll_tod_trigger = DPLL1_TOD_TRIGGER;
846 channel->dpll_tod_sts = DPLL1_TOD_STS;
847 channel->dpll_mode_cnfg = DPLL1_OPERATING_MODE_CNFG;
848 channel->dpll_freq_cnfg = DPLL1_HOLDOVER_FREQ_CNFG;
849 channel->dpll_phase_cnfg = DPLL1_PHASE_OFFSET_CNFG;
850 channel->dpll_sync_cnfg = DPLL1_SYNC_EDGE_CNFG;
851 channel->dpll_input_mode_cnfg = DPLL1_INPUT_MODE_CNFG;
854 channel->dpll_tod_cnfg = DPLL2_TOD_CNFG;
855 channel->dpll_tod_trigger = DPLL2_TOD_TRIGGER;
856 channel->dpll_tod_sts = DPLL2_TOD_STS;
857 channel->dpll_mode_cnfg = DPLL2_OPERATING_MODE_CNFG;
858 channel->dpll_freq_cnfg = DPLL2_HOLDOVER_FREQ_CNFG;
859 channel->dpll_phase_cnfg = DPLL2_PHASE_OFFSET_CNFG;
860 channel->dpll_sync_cnfg = DPLL2_SYNC_EDGE_CNFG;
861 channel->dpll_input_mode_cnfg = DPLL2_INPUT_MODE_CNFG;
867 channel->current_freq_ppb = 0;
872 static void idt82p33_caps_init(struct ptp_clock_info *caps)
874 caps->owner = THIS_MODULE;
875 caps->max_adj = 92000;
876 caps->n_per_out = 11;
877 caps->adjphase = idt82p33_adjwritephase;
878 caps->adjfine = idt82p33_adjfine;
879 caps->adjtime = idt82p33_adjtime;
880 caps->gettime64 = idt82p33_gettime;
881 caps->settime64 = idt82p33_settime;
882 caps->enable = idt82p33_enable;
883 caps->do_aux_work = idt82p33_sync_tod_work_handler;
886 static int idt82p33_enable_channel(struct idt82p33 *idt82p33, u32 index)
888 struct idt82p33_channel *channel;
891 if (!(index < MAX_PHC_PLL))
894 channel = &idt82p33->channel[index];
896 err = idt82p33_channel_init(channel, index);
898 dev_err(&idt82p33->client->dev,
899 "Channel_init failed in %s with err %d!\n",
904 channel->idt82p33 = idt82p33;
906 idt82p33_caps_init(&channel->caps);
907 snprintf(channel->caps.name, sizeof(channel->caps.name),
908 "IDT 82P33 PLL%u", index);
910 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
912 if (IS_ERR(channel->ptp_clock)) {
913 err = PTR_ERR(channel->ptp_clock);
914 channel->ptp_clock = NULL;
918 if (!channel->ptp_clock)
921 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
923 dev_err(&idt82p33->client->dev,
924 "Dpll_set_mode failed in %s with err %d!\n",
929 err = idt82p33_enable_tod(channel);
931 dev_err(&idt82p33->client->dev,
932 "Enable_tod failed in %s with err %d!\n",
937 dev_info(&idt82p33->client->dev, "PLL%d registered as ptp%d\n",
938 index, channel->ptp_clock->index);
943 static int idt82p33_load_firmware(struct idt82p33 *idt82p33)
945 const struct firmware *fw;
946 struct idt82p33_fwrc *rec;
947 u8 loaddr, page, val;
951 dev_dbg(&idt82p33->client->dev,
952 "requesting firmware '%s'\n", FW_FILENAME);
954 err = request_firmware(&fw, FW_FILENAME, &idt82p33->client->dev);
957 dev_err(&idt82p33->client->dev,
958 "Failed in %s with err %d!\n", __func__, err);
962 dev_dbg(&idt82p33->client->dev, "firmware size %zu bytes\n", fw->size);
964 rec = (struct idt82p33_fwrc *) fw->data;
966 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
969 dev_err(&idt82p33->client->dev,
970 "bad firmware, reserved field non-zero\n");
974 loaddr = rec->loaddr;
979 err = idt82p33_check_and_set_masks(idt82p33, page,
984 /* maximum 8 pages */
985 if (page >= PAGE_NUM)
988 /* Page size 128, last 4 bytes of page skipped */
989 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
993 err = idt82p33_write(idt82p33, _ADDR(page, loaddr),
1001 idt82p33_display_masks(idt82p33);
1003 release_firmware(fw);
1008 static int idt82p33_probe(struct i2c_client *client,
1009 const struct i2c_device_id *id)
1011 struct idt82p33 *idt82p33;
1017 idt82p33 = devm_kzalloc(&client->dev,
1018 sizeof(struct idt82p33), GFP_KERNEL);
1022 mutex_init(&idt82p33->reg_lock);
1024 idt82p33->client = client;
1025 idt82p33->page_offset = 0xff;
1026 idt82p33->tod_write_overhead_ns = 0;
1027 idt82p33->calculate_overhead_flag = 0;
1028 idt82p33->pll_mask = DEFAULT_PLL_MASK;
1029 idt82p33->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
1030 idt82p33->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
1032 mutex_lock(&idt82p33->reg_lock);
1034 err = idt82p33_load_firmware(idt82p33);
1037 dev_warn(&idt82p33->client->dev,
1038 "loading firmware failed with %d\n", err);
1040 if (idt82p33->pll_mask) {
1041 for (i = 0; i < MAX_PHC_PLL; i++) {
1042 if (idt82p33->pll_mask & (1 << i)) {
1043 err = idt82p33_enable_channel(idt82p33, i);
1045 dev_err(&idt82p33->client->dev,
1046 "Failed in %s with err %d!\n",
1053 dev_err(&idt82p33->client->dev,
1054 "no PLLs flagged as PHCs, nothing to do\n");
1058 mutex_unlock(&idt82p33->reg_lock);
1061 idt82p33_ptp_clock_unregister_all(idt82p33);
1065 i2c_set_clientdata(client, idt82p33);
1070 static int idt82p33_remove(struct i2c_client *client)
1072 struct idt82p33 *idt82p33 = i2c_get_clientdata(client);
1074 idt82p33_ptp_clock_unregister_all(idt82p33);
1075 mutex_destroy(&idt82p33->reg_lock);
1081 static const struct of_device_id idt82p33_dt_id[] = {
1082 { .compatible = "idt,82p33810" },
1083 { .compatible = "idt,82p33813" },
1084 { .compatible = "idt,82p33814" },
1085 { .compatible = "idt,82p33831" },
1086 { .compatible = "idt,82p33910" },
1087 { .compatible = "idt,82p33913" },
1088 { .compatible = "idt,82p33914" },
1089 { .compatible = "idt,82p33931" },
1092 MODULE_DEVICE_TABLE(of, idt82p33_dt_id);
1095 static const struct i2c_device_id idt82p33_i2c_id[] = {
1106 MODULE_DEVICE_TABLE(i2c, idt82p33_i2c_id);
1108 static struct i2c_driver idt82p33_driver = {
1110 .of_match_table = of_match_ptr(idt82p33_dt_id),
1113 .probe = idt82p33_probe,
1114 .remove = idt82p33_remove,
1115 .id_table = idt82p33_i2c_id,
1118 module_i2c_driver(idt82p33_driver);