Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / ptp / ptp_idt82p33.c
blobc1c959f7e52b9380486529c86c66348adb4c6c64
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2018 Integrated Device Technology, Inc
4 //
6 #define pr_fmt(fmt) "IDT_82p33xxx: " fmt
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/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])
40 time64_t sec;
41 s32 nsec;
42 u8 i;
44 nsec = buf[3];
45 for (i = 0; i < 3; i++) {
46 nsec <<= 8;
47 nsec |= buf[2 - i];
50 sec = buf[9];
51 for (i = 0; i < 5; i++) {
52 sec <<= 8;
53 sec |= buf[8 - i];
56 ts->tv_sec = sec;
57 ts->tv_nsec = nsec;
60 static void idt82p33_timespec_to_byte_array(struct timespec64 const *ts,
61 u8 buf[TOD_BYTE_COUNT])
63 time64_t sec;
64 s32 nsec;
65 u8 i;
67 nsec = ts->tv_nsec;
68 sec = ts->tv_sec;
70 for (i = 0; i < 4; i++) {
71 buf[i] = nsec & 0xff;
72 nsec >>= 8;
75 for (i = 4; i < TOD_BYTE_COUNT; i++) {
76 buf[i] = sec & 0xff;
77 sec >>= 8;
81 static int idt82p33_xfer_read(struct idt82p33 *idt82p33,
82 unsigned char regaddr,
83 unsigned char *buf,
84 unsigned int count)
86 struct i2c_client *client = idt82p33->client;
87 struct i2c_msg msg[2];
88 int cnt;
90 msg[0].addr = client->addr;
91 msg[0].flags = 0;
92 msg[0].len = 1;
93 msg[0].buf = &regaddr;
95 msg[1].addr = client->addr;
96 msg[1].flags = I2C_M_RD;
97 msg[1].len = count;
98 msg[1].buf = buf;
100 cnt = i2c_transfer(client->adapter, msg, 2);
101 if (cnt < 0) {
102 dev_err(&client->dev, "i2c_transfer returned %d\n", cnt);
103 return cnt;
104 } else if (cnt != 2) {
105 dev_err(&client->dev,
106 "i2c_transfer sent only %d of %d messages\n", cnt, 2);
107 return -EIO;
109 return 0;
112 static int idt82p33_xfer_write(struct idt82p33 *idt82p33,
113 u8 regaddr,
114 u8 *buf,
115 u16 count)
117 struct i2c_client *client = idt82p33->client;
118 /* we add 1 byte for device register */
119 u8 msg[IDT82P33_MAX_WRITE_COUNT + 1];
120 int err;
122 if (count > IDT82P33_MAX_WRITE_COUNT)
123 return -EINVAL;
125 msg[0] = regaddr;
126 memcpy(&msg[1], buf, count);
128 err = i2c_master_send(client, msg, count + 1);
129 if (err < 0) {
130 dev_err(&client->dev, "i2c_master_send returned %d\n", err);
131 return err;
134 return 0;
137 static int idt82p33_page_offset(struct idt82p33 *idt82p33, unsigned char val)
139 int err;
141 if (idt82p33->page_offset == val)
142 return 0;
144 err = idt82p33_xfer_write(idt82p33, PAGE_ADDR, &val, sizeof(val));
145 if (err)
146 dev_err(&idt82p33->client->dev,
147 "failed to set page offset %d\n", val);
148 else
149 idt82p33->page_offset = val;
151 return err;
154 static int idt82p33_rdwr(struct idt82p33 *idt82p33, unsigned int regaddr,
155 unsigned char *buf, unsigned int count, bool write)
157 u8 offset, page;
158 int err;
160 page = _PAGE(regaddr);
161 offset = _OFFSET(regaddr);
163 err = idt82p33_page_offset(idt82p33, page);
164 if (err)
165 return err;
167 if (write)
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,
186 enum pll_mode mode)
188 struct idt82p33 *idt82p33 = channel->idt82p33;
189 u8 dpll_mode;
190 int err;
192 if (channel->pll_mode == mode)
193 return 0;
195 err = idt82p33_read(idt82p33, channel->dpll_mode_cnfg,
196 &dpll_mode, sizeof(dpll_mode));
197 if (err)
198 return err;
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));
206 if (err)
207 return err;
209 channel->pll_mode = dpll_mode;
211 return 0;
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];
219 u8 trigger;
220 int err;
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));
229 if (err)
230 return err;
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));
237 if (err)
238 return err;
240 idt82p33_byte_array_to_timespec(ts, buf);
242 return 0;
246 * TOD Trigger:
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;
259 int err;
260 u8 i;
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));
268 if (err)
269 return err;
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]));
288 if (err)
289 return err;
292 return err;
295 static int _idt82p33_adjtime(struct idt82p33_channel *channel, s64 delta_ns)
297 struct idt82p33 *idt82p33 = channel->idt82p33;
298 struct timespec64 ts;
299 s64 now_ns;
300 int err;
302 idt82p33->calculate_overhead_flag = 1;
304 err = _idt82p33_gettime(channel, &ts);
306 if (err)
307 return err;
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);
316 return err;
319 static int _idt82p33_adjfine(struct idt82p33_channel *channel, long scaled_ppm)
321 struct idt82p33 *idt82p33 = channel->idt82p33;
322 unsigned char buf[5] = {0};
323 int err, i;
324 s64 fcw;
326 if (scaled_ppm == channel->current_freq_ppb)
327 return 0;
330 * Frequency Control Word unit is: 1.68 * 10^-10 ppm
332 * adjfreq:
333 * ppb * 10^9
334 * FCW = ----------
335 * 168
337 * adjfine:
338 * scaled_ppm * 5^12
339 * FCW = -------------
340 * 168 * 2^4
343 fcw = scaled_ppm * 244140625ULL;
344 fcw = div_s64(fcw, 2688);
346 for (i = 0; i < 5; i++) {
347 buf[i] = fcw & 0xff;
348 fcw >>= 8;
351 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
353 if (err)
354 return err;
356 err = idt82p33_write(idt82p33, channel->dpll_freq_cnfg,
357 buf, sizeof(buf));
359 if (err == 0)
360 channel->current_freq_ppb = scaled_ppm;
362 return err;
365 static int idt82p33_measure_one_byte_write_overhead(
366 struct idt82p33_channel *channel, s64 *overhead_ns)
368 struct idt82p33 *idt82p33 = channel->idt82p33;
369 ktime_t start, stop;
370 s64 total_ns;
371 u8 trigger;
372 int err;
373 u8 i;
375 total_ns = 0;
376 *overhead_ns = 0;
377 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
378 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
380 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
382 start = ktime_get_raw();
384 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
385 &trigger, sizeof(trigger));
387 stop = ktime_get_raw();
389 if (err)
390 return err;
392 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
395 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
397 return err;
400 static int idt82p33_measure_tod_write_9_byte_overhead(
401 struct idt82p33_channel *channel)
403 struct idt82p33 *idt82p33 = channel->idt82p33;
404 u8 buf[TOD_BYTE_COUNT];
405 ktime_t start, stop;
406 s64 total_ns;
407 int err = 0;
408 u8 i, j;
410 total_ns = 0;
411 idt82p33->tod_write_overhead_ns = 0;
413 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
415 start = ktime_get_raw();
417 /* Need one less byte for applicable overhead */
418 for (j = 0; j < (TOD_BYTE_COUNT - 1); j++) {
419 err = idt82p33_write(idt82p33,
420 channel->dpll_tod_cnfg + i,
421 &buf[i], sizeof(buf[i]));
422 if (err)
423 return err;
426 stop = ktime_get_raw();
428 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
431 idt82p33->tod_write_overhead_ns = div_s64(total_ns,
432 MAX_MEASURMENT_COUNT);
434 return err;
437 static int idt82p33_measure_settime_gettime_gap_overhead(
438 struct idt82p33_channel *channel, s64 *overhead_ns)
440 struct timespec64 ts1 = {0, 0};
441 struct timespec64 ts2;
442 int err;
444 *overhead_ns = 0;
446 err = _idt82p33_settime(channel, &ts1);
448 if (err)
449 return err;
451 err = _idt82p33_gettime(channel, &ts2);
453 if (!err)
454 *overhead_ns = timespec64_to_ns(&ts2) - timespec64_to_ns(&ts1);
456 return err;
459 static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel *channel)
461 s64 trailing_overhead_ns, one_byte_write_ns, gap_ns;
462 struct idt82p33 *idt82p33 = channel->idt82p33;
463 int err;
465 idt82p33->tod_write_overhead_ns = 0;
467 err = idt82p33_measure_settime_gettime_gap_overhead(channel, &gap_ns);
469 if (err) {
470 dev_err(&idt82p33->client->dev,
471 "Failed in %s with err %d!\n", __func__, err);
472 return err;
475 err = idt82p33_measure_one_byte_write_overhead(channel,
476 &one_byte_write_ns);
478 if (err)
479 return err;
481 err = idt82p33_measure_tod_write_9_byte_overhead(channel);
483 if (err)
484 return err;
486 trailing_overhead_ns = gap_ns - (2 * one_byte_write_ns);
488 idt82p33->tod_write_overhead_ns -= trailing_overhead_ns;
490 return err;
493 static int idt82p33_check_and_set_masks(struct idt82p33 *idt82p33,
494 u8 page,
495 u8 offset,
496 u8 val)
498 int err = 0;
500 if (page == PLLMASK_ADDR_HI && offset == PLLMASK_ADDR_LO) {
501 if ((val & 0xfc) || !(val & 0x3)) {
502 dev_err(&idt82p33->client->dev,
503 "Invalid PLL mask 0x%hhx\n", val);
504 err = -EINVAL;
505 } else {
506 idt82p33->pll_mask = val;
508 } else if (page == PLL0_OUTMASK_ADDR_HI &&
509 offset == PLL0_OUTMASK_ADDR_LO) {
510 idt82p33->channel[0].output_mask = val;
511 } else if (page == PLL1_OUTMASK_ADDR_HI &&
512 offset == PLL1_OUTMASK_ADDR_LO) {
513 idt82p33->channel[1].output_mask = val;
516 return err;
519 static void idt82p33_display_masks(struct idt82p33 *idt82p33)
521 u8 mask, i;
523 dev_info(&idt82p33->client->dev,
524 "pllmask = 0x%02x\n", idt82p33->pll_mask);
526 for (i = 0; i < MAX_PHC_PLL; i++) {
527 mask = 1 << i;
529 if (mask & idt82p33->pll_mask)
530 dev_info(&idt82p33->client->dev,
531 "PLL%d output_mask = 0x%04x\n",
532 i, idt82p33->channel[i].output_mask);
536 static int idt82p33_sync_tod(struct idt82p33_channel *channel, bool enable)
538 struct idt82p33 *idt82p33 = channel->idt82p33;
539 u8 sync_cnfg;
540 int err;
542 /* Turn it off after sync_tod_timeout seconds */
543 if (enable && sync_tod_timeout)
544 ptp_schedule_worker(channel->ptp_clock,
545 sync_tod_timeout * HZ);
547 err = idt82p33_read(idt82p33, channel->dpll_sync_cnfg,
548 &sync_cnfg, sizeof(sync_cnfg));
549 if (err)
550 return err;
552 sync_cnfg &= ~SYNC_TOD;
553 if (enable)
554 sync_cnfg |= SYNC_TOD;
556 return idt82p33_write(idt82p33, channel->dpll_sync_cnfg,
557 &sync_cnfg, sizeof(sync_cnfg));
560 static long idt82p33_sync_tod_work_handler(struct ptp_clock_info *ptp)
562 struct idt82p33_channel *channel =
563 container_of(ptp, struct idt82p33_channel, caps);
564 struct idt82p33 *idt82p33 = channel->idt82p33;
566 mutex_lock(&idt82p33->reg_lock);
568 (void)idt82p33_sync_tod(channel, false);
570 mutex_unlock(&idt82p33->reg_lock);
572 /* Return a negative value here to not reschedule */
573 return -1;
576 static int idt82p33_output_enable(struct idt82p33_channel *channel,
577 bool enable, unsigned int outn)
579 struct idt82p33 *idt82p33 = channel->idt82p33;
580 int err;
581 u8 val;
583 err = idt82p33_read(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
584 if (err)
585 return err;
586 if (enable)
587 val &= ~SQUELCH_ENABLE;
588 else
589 val |= SQUELCH_ENABLE;
591 return idt82p33_write(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
594 static int idt82p33_output_mask_enable(struct idt82p33_channel *channel,
595 bool enable)
597 u16 mask;
598 int err;
599 u8 outn;
601 mask = channel->output_mask;
602 outn = 0;
604 while (mask) {
605 if (mask & 0x1) {
606 err = idt82p33_output_enable(channel, enable, outn);
607 if (err)
608 return err;
611 mask >>= 0x1;
612 outn++;
615 return 0;
618 static int idt82p33_perout_enable(struct idt82p33_channel *channel,
619 bool enable,
620 struct ptp_perout_request *perout)
622 unsigned int flags = perout->flags;
624 /* Enable/disable output based on output_mask */
625 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
626 return idt82p33_output_mask_enable(channel, enable);
628 /* Enable/disable individual output instead */
629 return idt82p33_output_enable(channel, enable, perout->index);
632 static int idt82p33_enable_tod(struct idt82p33_channel *channel)
634 struct idt82p33 *idt82p33 = channel->idt82p33;
635 struct timespec64 ts = {0, 0};
636 int err;
637 u8 val;
639 val = 0;
640 err = idt82p33_write(idt82p33, channel->dpll_input_mode_cnfg,
641 &val, sizeof(val));
642 if (err)
643 return err;
645 err = idt82p33_measure_tod_write_overhead(channel);
647 if (err) {
648 dev_err(&idt82p33->client->dev,
649 "Failed in %s with err %d!\n", __func__, err);
650 return err;
653 err = _idt82p33_settime(channel, &ts);
655 if (err)
656 return err;
658 return idt82p33_sync_tod(channel, true);
661 static void idt82p33_ptp_clock_unregister_all(struct idt82p33 *idt82p33)
663 struct idt82p33_channel *channel;
664 u8 i;
666 for (i = 0; i < MAX_PHC_PLL; i++) {
668 channel = &idt82p33->channel[i];
670 if (channel->ptp_clock)
671 ptp_clock_unregister(channel->ptp_clock);
675 static int idt82p33_enable(struct ptp_clock_info *ptp,
676 struct ptp_clock_request *rq, int on)
678 struct idt82p33_channel *channel =
679 container_of(ptp, struct idt82p33_channel, caps);
680 struct idt82p33 *idt82p33 = channel->idt82p33;
681 int err;
683 err = -EOPNOTSUPP;
685 mutex_lock(&idt82p33->reg_lock);
687 if (rq->type == PTP_CLK_REQ_PEROUT) {
688 if (!on)
689 err = idt82p33_perout_enable(channel, false,
690 &rq->perout);
691 /* Only accept a 1-PPS aligned to the second. */
692 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
693 rq->perout.period.nsec) {
694 err = -ERANGE;
695 } else
696 err = idt82p33_perout_enable(channel, true,
697 &rq->perout);
700 mutex_unlock(&idt82p33->reg_lock);
702 return err;
705 static int idt82p33_adjwritephase(struct ptp_clock_info *ptp, s32 offset_ns)
707 struct idt82p33_channel *channel =
708 container_of(ptp, struct idt82p33_channel, caps);
709 struct idt82p33 *idt82p33 = channel->idt82p33;
710 s64 offset_regval, offset_fs;
711 u8 val[4] = {0};
712 int err;
714 offset_fs = (s64)(-offset_ns) * 1000000;
716 if (offset_fs > WRITE_PHASE_OFFSET_LIMIT)
717 offset_fs = WRITE_PHASE_OFFSET_LIMIT;
718 else if (offset_fs < -WRITE_PHASE_OFFSET_LIMIT)
719 offset_fs = -WRITE_PHASE_OFFSET_LIMIT;
721 /* Convert from phaseoffset_fs to register value */
722 offset_regval = div_s64(offset_fs * 1000, IDT_T0DPLL_PHASE_RESOL);
724 val[0] = offset_regval & 0xFF;
725 val[1] = (offset_regval >> 8) & 0xFF;
726 val[2] = (offset_regval >> 16) & 0xFF;
727 val[3] = (offset_regval >> 24) & 0x1F;
728 val[3] |= PH_OFFSET_EN;
730 mutex_lock(&idt82p33->reg_lock);
732 err = idt82p33_dpll_set_mode(channel, PLL_MODE_WPH);
733 if (err) {
734 dev_err(&idt82p33->client->dev,
735 "Failed in %s with err %d!\n", __func__, err);
736 goto out;
739 err = idt82p33_write(idt82p33, channel->dpll_phase_cnfg, val,
740 sizeof(val));
742 out:
743 mutex_unlock(&idt82p33->reg_lock);
744 return err;
747 static int idt82p33_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
749 struct idt82p33_channel *channel =
750 container_of(ptp, struct idt82p33_channel, caps);
751 struct idt82p33 *idt82p33 = channel->idt82p33;
752 int err;
754 mutex_lock(&idt82p33->reg_lock);
755 err = _idt82p33_adjfine(channel, scaled_ppm);
756 if (err)
757 dev_err(&idt82p33->client->dev,
758 "Failed in %s with err %d!\n", __func__, err);
759 mutex_unlock(&idt82p33->reg_lock);
761 return err;
764 static int idt82p33_adjtime(struct ptp_clock_info *ptp, s64 delta_ns)
766 struct idt82p33_channel *channel =
767 container_of(ptp, struct idt82p33_channel, caps);
768 struct idt82p33 *idt82p33 = channel->idt82p33;
769 int err;
771 mutex_lock(&idt82p33->reg_lock);
773 if (abs(delta_ns) < phase_snap_threshold) {
774 mutex_unlock(&idt82p33->reg_lock);
775 return 0;
778 err = _idt82p33_adjtime(channel, delta_ns);
780 if (err) {
781 mutex_unlock(&idt82p33->reg_lock);
782 dev_err(&idt82p33->client->dev,
783 "Adjtime failed in %s with err %d!\n", __func__, err);
784 return err;
787 err = idt82p33_sync_tod(channel, true);
788 if (err)
789 dev_err(&idt82p33->client->dev,
790 "Sync_tod failed in %s with err %d!\n", __func__, err);
792 mutex_unlock(&idt82p33->reg_lock);
794 return err;
797 static int idt82p33_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
799 struct idt82p33_channel *channel =
800 container_of(ptp, struct idt82p33_channel, caps);
801 struct idt82p33 *idt82p33 = channel->idt82p33;
802 int err;
804 mutex_lock(&idt82p33->reg_lock);
805 err = _idt82p33_gettime(channel, ts);
806 if (err)
807 dev_err(&idt82p33->client->dev,
808 "Failed in %s with err %d!\n", __func__, err);
809 mutex_unlock(&idt82p33->reg_lock);
811 return err;
814 static int idt82p33_settime(struct ptp_clock_info *ptp,
815 const struct timespec64 *ts)
817 struct idt82p33_channel *channel =
818 container_of(ptp, struct idt82p33_channel, caps);
819 struct idt82p33 *idt82p33 = channel->idt82p33;
820 int err;
822 mutex_lock(&idt82p33->reg_lock);
823 err = _idt82p33_settime(channel, ts);
824 if (err)
825 dev_err(&idt82p33->client->dev,
826 "Failed in %s with err %d!\n", __func__, err);
827 mutex_unlock(&idt82p33->reg_lock);
829 return err;
832 static int idt82p33_channel_init(struct idt82p33_channel *channel, int index)
834 switch (index) {
835 case 0:
836 channel->dpll_tod_cnfg = DPLL1_TOD_CNFG;
837 channel->dpll_tod_trigger = DPLL1_TOD_TRIGGER;
838 channel->dpll_tod_sts = DPLL1_TOD_STS;
839 channel->dpll_mode_cnfg = DPLL1_OPERATING_MODE_CNFG;
840 channel->dpll_freq_cnfg = DPLL1_HOLDOVER_FREQ_CNFG;
841 channel->dpll_phase_cnfg = DPLL1_PHASE_OFFSET_CNFG;
842 channel->dpll_sync_cnfg = DPLL1_SYNC_EDGE_CNFG;
843 channel->dpll_input_mode_cnfg = DPLL1_INPUT_MODE_CNFG;
844 break;
845 case 1:
846 channel->dpll_tod_cnfg = DPLL2_TOD_CNFG;
847 channel->dpll_tod_trigger = DPLL2_TOD_TRIGGER;
848 channel->dpll_tod_sts = DPLL2_TOD_STS;
849 channel->dpll_mode_cnfg = DPLL2_OPERATING_MODE_CNFG;
850 channel->dpll_freq_cnfg = DPLL2_HOLDOVER_FREQ_CNFG;
851 channel->dpll_phase_cnfg = DPLL2_PHASE_OFFSET_CNFG;
852 channel->dpll_sync_cnfg = DPLL2_SYNC_EDGE_CNFG;
853 channel->dpll_input_mode_cnfg = DPLL2_INPUT_MODE_CNFG;
854 break;
855 default:
856 return -EINVAL;
859 channel->current_freq_ppb = 0;
861 return 0;
864 static void idt82p33_caps_init(struct ptp_clock_info *caps)
866 caps->owner = THIS_MODULE;
867 caps->max_adj = 92000;
868 caps->n_per_out = 11;
869 caps->adjphase = idt82p33_adjwritephase;
870 caps->adjfine = idt82p33_adjfine;
871 caps->adjtime = idt82p33_adjtime;
872 caps->gettime64 = idt82p33_gettime;
873 caps->settime64 = idt82p33_settime;
874 caps->enable = idt82p33_enable;
875 caps->do_aux_work = idt82p33_sync_tod_work_handler;
878 static int idt82p33_enable_channel(struct idt82p33 *idt82p33, u32 index)
880 struct idt82p33_channel *channel;
881 int err;
883 if (!(index < MAX_PHC_PLL))
884 return -EINVAL;
886 channel = &idt82p33->channel[index];
888 err = idt82p33_channel_init(channel, index);
889 if (err) {
890 dev_err(&idt82p33->client->dev,
891 "Channel_init failed in %s with err %d!\n",
892 __func__, err);
893 return err;
896 channel->idt82p33 = idt82p33;
898 idt82p33_caps_init(&channel->caps);
899 snprintf(channel->caps.name, sizeof(channel->caps.name),
900 "IDT 82P33 PLL%u", index);
902 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
904 if (IS_ERR(channel->ptp_clock)) {
905 err = PTR_ERR(channel->ptp_clock);
906 channel->ptp_clock = NULL;
907 return err;
910 if (!channel->ptp_clock)
911 return -ENOTSUPP;
913 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
914 if (err) {
915 dev_err(&idt82p33->client->dev,
916 "Dpll_set_mode failed in %s with err %d!\n",
917 __func__, err);
918 return err;
921 err = idt82p33_enable_tod(channel);
922 if (err) {
923 dev_err(&idt82p33->client->dev,
924 "Enable_tod failed in %s with err %d!\n",
925 __func__, err);
926 return err;
929 dev_info(&idt82p33->client->dev, "PLL%d registered as ptp%d\n",
930 index, channel->ptp_clock->index);
932 return 0;
935 static int idt82p33_load_firmware(struct idt82p33 *idt82p33)
937 const struct firmware *fw;
938 struct idt82p33_fwrc *rec;
939 u8 loaddr, page, val;
940 int err;
941 s32 len;
943 dev_dbg(&idt82p33->client->dev,
944 "requesting firmware '%s'\n", FW_FILENAME);
946 err = request_firmware(&fw, FW_FILENAME, &idt82p33->client->dev);
948 if (err) {
949 dev_err(&idt82p33->client->dev,
950 "Failed in %s with err %d!\n", __func__, err);
951 return err;
954 dev_dbg(&idt82p33->client->dev, "firmware size %zu bytes\n", fw->size);
956 rec = (struct idt82p33_fwrc *) fw->data;
958 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
960 if (rec->reserved) {
961 dev_err(&idt82p33->client->dev,
962 "bad firmware, reserved field non-zero\n");
963 err = -EINVAL;
964 } else {
965 val = rec->value;
966 loaddr = rec->loaddr;
967 page = rec->hiaddr;
969 rec++;
971 err = idt82p33_check_and_set_masks(idt82p33, page,
972 loaddr, val);
975 if (err == 0) {
976 /* maximum 8 pages */
977 if (page >= PAGE_NUM)
978 continue;
980 /* Page size 128, last 4 bytes of page skipped */
981 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
982 || loaddr > 0xfb)
983 continue;
985 err = idt82p33_write(idt82p33, _ADDR(page, loaddr),
986 &val, sizeof(val));
989 if (err)
990 goto out;
993 idt82p33_display_masks(idt82p33);
994 out:
995 release_firmware(fw);
996 return err;
1000 static int idt82p33_probe(struct i2c_client *client,
1001 const struct i2c_device_id *id)
1003 struct idt82p33 *idt82p33;
1004 int err;
1005 u8 i;
1007 (void)id;
1009 idt82p33 = devm_kzalloc(&client->dev,
1010 sizeof(struct idt82p33), GFP_KERNEL);
1011 if (!idt82p33)
1012 return -ENOMEM;
1014 mutex_init(&idt82p33->reg_lock);
1016 idt82p33->client = client;
1017 idt82p33->page_offset = 0xff;
1018 idt82p33->tod_write_overhead_ns = 0;
1019 idt82p33->calculate_overhead_flag = 0;
1020 idt82p33->pll_mask = DEFAULT_PLL_MASK;
1021 idt82p33->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
1022 idt82p33->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
1024 mutex_lock(&idt82p33->reg_lock);
1026 err = idt82p33_load_firmware(idt82p33);
1028 if (err)
1029 dev_warn(&idt82p33->client->dev,
1030 "loading firmware failed with %d\n", err);
1032 if (idt82p33->pll_mask) {
1033 for (i = 0; i < MAX_PHC_PLL; i++) {
1034 if (idt82p33->pll_mask & (1 << i)) {
1035 err = idt82p33_enable_channel(idt82p33, i);
1036 if (err) {
1037 dev_err(&idt82p33->client->dev,
1038 "Failed in %s with err %d!\n",
1039 __func__, err);
1040 break;
1044 } else {
1045 dev_err(&idt82p33->client->dev,
1046 "no PLLs flagged as PHCs, nothing to do\n");
1047 err = -ENODEV;
1050 mutex_unlock(&idt82p33->reg_lock);
1052 if (err) {
1053 idt82p33_ptp_clock_unregister_all(idt82p33);
1054 return err;
1057 i2c_set_clientdata(client, idt82p33);
1059 return 0;
1062 static int idt82p33_remove(struct i2c_client *client)
1064 struct idt82p33 *idt82p33 = i2c_get_clientdata(client);
1066 idt82p33_ptp_clock_unregister_all(idt82p33);
1067 mutex_destroy(&idt82p33->reg_lock);
1069 return 0;
1072 #ifdef CONFIG_OF
1073 static const struct of_device_id idt82p33_dt_id[] = {
1074 { .compatible = "idt,82p33810" },
1075 { .compatible = "idt,82p33813" },
1076 { .compatible = "idt,82p33814" },
1077 { .compatible = "idt,82p33831" },
1078 { .compatible = "idt,82p33910" },
1079 { .compatible = "idt,82p33913" },
1080 { .compatible = "idt,82p33914" },
1081 { .compatible = "idt,82p33931" },
1084 MODULE_DEVICE_TABLE(of, idt82p33_dt_id);
1085 #endif
1087 static const struct i2c_device_id idt82p33_i2c_id[] = {
1088 { "idt82p33810", },
1089 { "idt82p33813", },
1090 { "idt82p33814", },
1091 { "idt82p33831", },
1092 { "idt82p33910", },
1093 { "idt82p33913", },
1094 { "idt82p33914", },
1095 { "idt82p33931", },
1098 MODULE_DEVICE_TABLE(i2c, idt82p33_i2c_id);
1100 static struct i2c_driver idt82p33_driver = {
1101 .driver = {
1102 .of_match_table = of_match_ptr(idt82p33_dt_id),
1103 .name = "idt82p33",
1105 .probe = idt82p33_probe,
1106 .remove = idt82p33_remove,
1107 .id_table = idt82p33_i2c_id,
1110 module_i2c_driver(idt82p33_driver);