Loading...
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 Integrated Device Technology, Inc
4//
5
6#define pr_fmt(fmt) "IDT_82p33xxx: " fmt
7
8#include <linux/firmware.h>
9#include <linux/i2c.h>
10#include <linux/module.h>
11#include <linux/ptp_clock_kernel.h>
12#include <linux/delay.h>
13#include <linux/kernel.h>
14#include <linux/timekeeping.h>
15#include <linux/bitops.h>
16
17#include "ptp_private.h"
18#include "ptp_idt82p33.h"
19
20MODULE_DESCRIPTION("Driver for IDT 82p33xxx clock devices");
21MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
22MODULE_VERSION("1.0");
23MODULE_LICENSE("GPL");
24
25/* Module Parameters */
26static u32 sync_tod_timeout = SYNC_TOD_TIMEOUT_SEC;
27module_param(sync_tod_timeout, uint, 0);
28MODULE_PARM_DESC(sync_tod_timeout,
29"duration in second to keep SYNC_TOD on (set to 0 to keep it always on)");
30
31static u32 phase_snap_threshold = SNAP_THRESHOLD_NS;
32module_param(phase_snap_threshold, uint, 0);
33MODULE_PARM_DESC(phase_snap_threshold,
34"threshold (150000ns by default) below which adjtime would ignore");
35
36static void idt82p33_byte_array_to_timespec(struct timespec64 *ts,
37 u8 buf[TOD_BYTE_COUNT])
38{
39 time64_t sec;
40 s32 nsec;
41 u8 i;
42
43 nsec = buf[3];
44 for (i = 0; i < 3; i++) {
45 nsec <<= 8;
46 nsec |= buf[2 - i];
47 }
48
49 sec = buf[9];
50 for (i = 0; i < 5; i++) {
51 sec <<= 8;
52 sec |= buf[8 - i];
53 }
54
55 ts->tv_sec = sec;
56 ts->tv_nsec = nsec;
57}
58
59static void idt82p33_timespec_to_byte_array(struct timespec64 const *ts,
60 u8 buf[TOD_BYTE_COUNT])
61{
62 time64_t sec;
63 s32 nsec;
64 u8 i;
65
66 nsec = ts->tv_nsec;
67 sec = ts->tv_sec;
68
69 for (i = 0; i < 4; i++) {
70 buf[i] = nsec & 0xff;
71 nsec >>= 8;
72 }
73
74 for (i = 4; i < TOD_BYTE_COUNT; i++) {
75 buf[i] = sec & 0xff;
76 sec >>= 8;
77 }
78}
79
80static int idt82p33_xfer(struct idt82p33 *idt82p33,
81 unsigned char regaddr,
82 unsigned char *buf,
83 unsigned int count,
84 int write)
85{
86 struct i2c_client *client = idt82p33->client;
87 struct i2c_msg msg[2];
88 int cnt;
89
90 msg[0].addr = client->addr;
91 msg[0].flags = 0;
92 msg[0].len = 1;
93 msg[0].buf = ®addr;
94
95 msg[1].addr = client->addr;
96 msg[1].flags = write ? 0 : I2C_M_RD;
97 msg[1].len = count;
98 msg[1].buf = buf;
99
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;
108 }
109 return 0;
110}
111
112static int idt82p33_page_offset(struct idt82p33 *idt82p33, unsigned char val)
113{
114 int err;
115
116 if (idt82p33->page_offset == val)
117 return 0;
118
119 err = idt82p33_xfer(idt82p33, PAGE_ADDR, &val, sizeof(val), 1);
120 if (err)
121 dev_err(&idt82p33->client->dev,
122 "failed to set page offset %d\n", val);
123 else
124 idt82p33->page_offset = val;
125
126 return err;
127}
128
129static int idt82p33_rdwr(struct idt82p33 *idt82p33, unsigned int regaddr,
130 unsigned char *buf, unsigned int count, bool write)
131{
132 u8 offset, page;
133 int err;
134
135 page = _PAGE(regaddr);
136 offset = _OFFSET(regaddr);
137
138 err = idt82p33_page_offset(idt82p33, page);
139 if (err)
140 goto out;
141
142 err = idt82p33_xfer(idt82p33, offset, buf, count, write);
143out:
144 return err;
145}
146
147static int idt82p33_read(struct idt82p33 *idt82p33, unsigned int regaddr,
148 unsigned char *buf, unsigned int count)
149{
150 return idt82p33_rdwr(idt82p33, regaddr, buf, count, false);
151}
152
153static int idt82p33_write(struct idt82p33 *idt82p33, unsigned int regaddr,
154 unsigned char *buf, unsigned int count)
155{
156 return idt82p33_rdwr(idt82p33, regaddr, buf, count, true);
157}
158
159static int idt82p33_dpll_set_mode(struct idt82p33_channel *channel,
160 enum pll_mode mode)
161{
162 struct idt82p33 *idt82p33 = channel->idt82p33;
163 u8 dpll_mode;
164 int err;
165
166 if (channel->pll_mode == mode)
167 return 0;
168
169 err = idt82p33_read(idt82p33, channel->dpll_mode_cnfg,
170 &dpll_mode, sizeof(dpll_mode));
171 if (err)
172 return err;
173
174 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
175
176 dpll_mode |= (mode << PLL_MODE_SHIFT);
177
178 err = idt82p33_write(idt82p33, channel->dpll_mode_cnfg,
179 &dpll_mode, sizeof(dpll_mode));
180 if (err)
181 return err;
182
183 channel->pll_mode = dpll_mode;
184
185 return 0;
186}
187
188static int _idt82p33_gettime(struct idt82p33_channel *channel,
189 struct timespec64 *ts)
190{
191 struct idt82p33 *idt82p33 = channel->idt82p33;
192 u8 buf[TOD_BYTE_COUNT];
193 u8 trigger;
194 int err;
195
196 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
197 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
198
199
200 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
201 &trigger, sizeof(trigger));
202
203 if (err)
204 return err;
205
206 if (idt82p33->calculate_overhead_flag)
207 idt82p33->start_time = ktime_get_raw();
208
209 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
210
211 if (err)
212 return err;
213
214 idt82p33_byte_array_to_timespec(ts, buf);
215
216 return 0;
217}
218
219/*
220 * TOD Trigger:
221 * Bits[7:4] Write 0x9, MSB write
222 * Bits[3:0] Read 0x9, LSB read
223 */
224
225static int _idt82p33_settime(struct idt82p33_channel *channel,
226 struct timespec64 const *ts)
227{
228 struct idt82p33 *idt82p33 = channel->idt82p33;
229 struct timespec64 local_ts = *ts;
230 char buf[TOD_BYTE_COUNT];
231 s64 dynamic_overhead_ns;
232 unsigned char trigger;
233 int err;
234 u8 i;
235
236 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
237 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
238
239 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
240 &trigger, sizeof(trigger));
241
242 if (err)
243 return err;
244
245 if (idt82p33->calculate_overhead_flag) {
246 dynamic_overhead_ns = ktime_to_ns(ktime_get_raw())
247 - ktime_to_ns(idt82p33->start_time);
248
249 timespec64_add_ns(&local_ts, dynamic_overhead_ns);
250
251 idt82p33->calculate_overhead_flag = 0;
252 }
253
254 idt82p33_timespec_to_byte_array(&local_ts, buf);
255
256 /*
257 * Store the new time value.
258 */
259 for (i = 0; i < TOD_BYTE_COUNT; i++) {
260 err = idt82p33_write(idt82p33, channel->dpll_tod_cnfg + i,
261 &buf[i], sizeof(buf[i]));
262 if (err)
263 return err;
264 }
265
266 return err;
267}
268
269static int _idt82p33_adjtime(struct idt82p33_channel *channel, s64 delta_ns)
270{
271 struct idt82p33 *idt82p33 = channel->idt82p33;
272 struct timespec64 ts;
273 s64 now_ns;
274 int err;
275
276 idt82p33->calculate_overhead_flag = 1;
277
278 err = _idt82p33_gettime(channel, &ts);
279
280 if (err)
281 return err;
282
283 now_ns = timespec64_to_ns(&ts);
284 now_ns += delta_ns + idt82p33->tod_write_overhead_ns;
285
286 ts = ns_to_timespec64(now_ns);
287
288 err = _idt82p33_settime(channel, &ts);
289
290 return err;
291}
292
293static int _idt82p33_adjfine(struct idt82p33_channel *channel, long scaled_ppm)
294{
295 struct idt82p33 *idt82p33 = channel->idt82p33;
296 unsigned char buf[5] = {0};
297 int neg_adj = 0;
298 int err, i;
299 s64 fcw;
300
301 if (scaled_ppm == channel->current_freq_ppb)
302 return 0;
303
304 /*
305 * Frequency Control Word unit is: 1.68 * 10^-10 ppm
306 *
307 * adjfreq:
308 * ppb * 10^9
309 * FCW = ----------
310 * 168
311 *
312 * adjfine:
313 * scaled_ppm * 5^12
314 * FCW = -------------
315 * 168 * 2^4
316 */
317 if (scaled_ppm < 0) {
318 neg_adj = 1;
319 scaled_ppm = -scaled_ppm;
320 }
321
322 fcw = scaled_ppm * 244140625ULL;
323 fcw = div_u64(fcw, 2688);
324
325 if (neg_adj)
326 fcw = -fcw;
327
328 for (i = 0; i < 5; i++) {
329 buf[i] = fcw & 0xff;
330 fcw >>= 8;
331 }
332
333 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
334
335 if (err)
336 return err;
337
338 err = idt82p33_write(idt82p33, channel->dpll_freq_cnfg,
339 buf, sizeof(buf));
340
341 if (err == 0)
342 channel->current_freq_ppb = scaled_ppm;
343
344 return err;
345}
346
347static int idt82p33_measure_one_byte_write_overhead(
348 struct idt82p33_channel *channel, s64 *overhead_ns)
349{
350 struct idt82p33 *idt82p33 = channel->idt82p33;
351 ktime_t start, stop;
352 s64 total_ns;
353 u8 trigger;
354 int err;
355 u8 i;
356
357 total_ns = 0;
358 *overhead_ns = 0;
359 trigger = TOD_TRIGGER(HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
360 HW_TOD_RD_TRIG_SEL_LSB_TOD_STS);
361
362 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
363
364 start = ktime_get_raw();
365
366 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
367 &trigger, sizeof(trigger));
368
369 stop = ktime_get_raw();
370
371 if (err)
372 return err;
373
374 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
375 }
376
377 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
378
379 return err;
380}
381
382static int idt82p33_measure_tod_write_9_byte_overhead(
383 struct idt82p33_channel *channel)
384{
385 struct idt82p33 *idt82p33 = channel->idt82p33;
386 u8 buf[TOD_BYTE_COUNT];
387 ktime_t start, stop;
388 s64 total_ns;
389 int err = 0;
390 u8 i, j;
391
392 total_ns = 0;
393 idt82p33->tod_write_overhead_ns = 0;
394
395 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
396
397 start = ktime_get_raw();
398
399 /* Need one less byte for applicable overhead */
400 for (j = 0; j < (TOD_BYTE_COUNT - 1); j++) {
401 err = idt82p33_write(idt82p33,
402 channel->dpll_tod_cnfg + i,
403 &buf[i], sizeof(buf[i]));
404 if (err)
405 return err;
406 }
407
408 stop = ktime_get_raw();
409
410 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
411 }
412
413 idt82p33->tod_write_overhead_ns = div_s64(total_ns,
414 MAX_MEASURMENT_COUNT);
415
416 return err;
417}
418
419static int idt82p33_measure_settime_gettime_gap_overhead(
420 struct idt82p33_channel *channel, s64 *overhead_ns)
421{
422 struct timespec64 ts1 = {0, 0};
423 struct timespec64 ts2;
424 int err;
425
426 *overhead_ns = 0;
427
428 err = _idt82p33_settime(channel, &ts1);
429
430 if (err)
431 return err;
432
433 err = _idt82p33_gettime(channel, &ts2);
434
435 if (!err)
436 *overhead_ns = timespec64_to_ns(&ts2) - timespec64_to_ns(&ts1);
437
438 return err;
439}
440
441static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel *channel)
442{
443 s64 trailing_overhead_ns, one_byte_write_ns, gap_ns;
444 struct idt82p33 *idt82p33 = channel->idt82p33;
445 int err;
446
447 idt82p33->tod_write_overhead_ns = 0;
448
449 err = idt82p33_measure_settime_gettime_gap_overhead(channel, &gap_ns);
450
451 if (err)
452 return err;
453
454 err = idt82p33_measure_one_byte_write_overhead(channel,
455 &one_byte_write_ns);
456
457 if (err)
458 return err;
459
460 err = idt82p33_measure_tod_write_9_byte_overhead(channel);
461
462 if (err)
463 return err;
464
465 trailing_overhead_ns = gap_ns - (2 * one_byte_write_ns);
466
467 idt82p33->tod_write_overhead_ns -= trailing_overhead_ns;
468
469 return err;
470}
471
472static int idt82p33_check_and_set_masks(struct idt82p33 *idt82p33,
473 u8 page,
474 u8 offset,
475 u8 val)
476{
477 int err = 0;
478
479 if (page == PLLMASK_ADDR_HI && offset == PLLMASK_ADDR_LO) {
480 if ((val & 0xfc) || !(val & 0x3)) {
481 dev_err(&idt82p33->client->dev,
482 "Invalid PLL mask 0x%hhx\n", val);
483 err = -EINVAL;
484 } else {
485 idt82p33->pll_mask = val;
486 }
487 } else if (page == PLL0_OUTMASK_ADDR_HI &&
488 offset == PLL0_OUTMASK_ADDR_LO) {
489 idt82p33->channel[0].output_mask = val;
490 } else if (page == PLL1_OUTMASK_ADDR_HI &&
491 offset == PLL1_OUTMASK_ADDR_LO) {
492 idt82p33->channel[1].output_mask = val;
493 }
494
495 return err;
496}
497
498static void idt82p33_display_masks(struct idt82p33 *idt82p33)
499{
500 u8 mask, i;
501
502 dev_info(&idt82p33->client->dev,
503 "pllmask = 0x%02x\n", idt82p33->pll_mask);
504
505 for (i = 0; i < MAX_PHC_PLL; i++) {
506 mask = 1 << i;
507
508 if (mask & idt82p33->pll_mask)
509 dev_info(&idt82p33->client->dev,
510 "PLL%d output_mask = 0x%04x\n",
511 i, idt82p33->channel[i].output_mask);
512 }
513}
514
515static int idt82p33_sync_tod(struct idt82p33_channel *channel, bool enable)
516{
517 struct idt82p33 *idt82p33 = channel->idt82p33;
518 u8 sync_cnfg;
519 int err;
520
521 if (enable == channel->sync_tod_on) {
522 if (enable && sync_tod_timeout) {
523 mod_delayed_work(system_wq, &channel->sync_tod_work,
524 sync_tod_timeout * HZ);
525 }
526 return 0;
527 }
528
529 err = idt82p33_read(idt82p33, channel->dpll_sync_cnfg,
530 &sync_cnfg, sizeof(sync_cnfg));
531 if (err)
532 return err;
533
534 sync_cnfg &= ~SYNC_TOD;
535
536 if (enable)
537 sync_cnfg |= SYNC_TOD;
538
539 err = idt82p33_write(idt82p33, channel->dpll_sync_cnfg,
540 &sync_cnfg, sizeof(sync_cnfg));
541 if (err)
542 return err;
543
544 channel->sync_tod_on = enable;
545
546 if (enable && sync_tod_timeout) {
547 mod_delayed_work(system_wq, &channel->sync_tod_work,
548 sync_tod_timeout * HZ);
549 }
550
551 return 0;
552}
553
554static void idt82p33_sync_tod_work_handler(struct work_struct *work)
555{
556 struct idt82p33_channel *channel =
557 container_of(work, struct idt82p33_channel, sync_tod_work.work);
558 struct idt82p33 *idt82p33 = channel->idt82p33;
559
560 mutex_lock(&idt82p33->reg_lock);
561
562 (void)idt82p33_sync_tod(channel, false);
563
564 mutex_unlock(&idt82p33->reg_lock);
565}
566
567static int idt82p33_pps_enable(struct idt82p33_channel *channel, bool enable)
568{
569 struct idt82p33 *idt82p33 = channel->idt82p33;
570 u8 mask, outn, val;
571 int err;
572
573 mask = channel->output_mask;
574 outn = 0;
575
576 while (mask) {
577 if (mask & 0x1) {
578 err = idt82p33_read(idt82p33, OUT_MUX_CNFG(outn),
579 &val, sizeof(val));
580 if (err)
581 return err;
582
583 if (enable)
584 val &= ~SQUELCH_ENABLE;
585 else
586 val |= SQUELCH_ENABLE;
587
588 err = idt82p33_write(idt82p33, OUT_MUX_CNFG(outn),
589 &val, sizeof(val));
590
591 if (err)
592 return err;
593 }
594 mask >>= 0x1;
595 outn++;
596 }
597
598 return 0;
599}
600
601static int idt82p33_enable_tod(struct idt82p33_channel *channel)
602{
603 struct idt82p33 *idt82p33 = channel->idt82p33;
604 struct timespec64 ts = {0, 0};
605 int err;
606 u8 val;
607
608 val = 0;
609 err = idt82p33_write(idt82p33, channel->dpll_input_mode_cnfg,
610 &val, sizeof(val));
611 if (err)
612 return err;
613
614 err = idt82p33_pps_enable(channel, false);
615
616 if (err)
617 return err;
618
619 err = idt82p33_measure_tod_write_overhead(channel);
620
621 if (err)
622 return err;
623
624 err = _idt82p33_settime(channel, &ts);
625
626 if (err)
627 return err;
628
629 return idt82p33_sync_tod(channel, true);
630}
631
632static void idt82p33_ptp_clock_unregister_all(struct idt82p33 *idt82p33)
633{
634 struct idt82p33_channel *channel;
635 u8 i;
636
637 for (i = 0; i < MAX_PHC_PLL; i++) {
638
639 channel = &idt82p33->channel[i];
640
641 if (channel->ptp_clock) {
642 ptp_clock_unregister(channel->ptp_clock);
643 cancel_delayed_work_sync(&channel->sync_tod_work);
644 }
645 }
646}
647
648static int idt82p33_enable(struct ptp_clock_info *ptp,
649 struct ptp_clock_request *rq, int on)
650{
651 struct idt82p33_channel *channel =
652 container_of(ptp, struct idt82p33_channel, caps);
653 struct idt82p33 *idt82p33 = channel->idt82p33;
654 int err;
655
656 err = -EOPNOTSUPP;
657
658 mutex_lock(&idt82p33->reg_lock);
659
660 if (rq->type == PTP_CLK_REQ_PEROUT) {
661 if (!on)
662 err = idt82p33_pps_enable(channel, false);
663
664 /* Only accept a 1-PPS aligned to the second. */
665 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
666 rq->perout.period.nsec) {
667 err = -ERANGE;
668 } else
669 err = idt82p33_pps_enable(channel, true);
670 }
671
672 mutex_unlock(&idt82p33->reg_lock);
673
674 return err;
675}
676
677static int idt82p33_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
678{
679 struct idt82p33_channel *channel =
680 container_of(ptp, struct idt82p33_channel, caps);
681 struct idt82p33 *idt82p33 = channel->idt82p33;
682 int err;
683
684 mutex_lock(&idt82p33->reg_lock);
685 err = _idt82p33_adjfine(channel, scaled_ppm);
686 mutex_unlock(&idt82p33->reg_lock);
687
688 return err;
689}
690
691static int idt82p33_adjtime(struct ptp_clock_info *ptp, s64 delta_ns)
692{
693 struct idt82p33_channel *channel =
694 container_of(ptp, struct idt82p33_channel, caps);
695 struct idt82p33 *idt82p33 = channel->idt82p33;
696 int err;
697
698 mutex_lock(&idt82p33->reg_lock);
699
700 if (abs(delta_ns) < phase_snap_threshold) {
701 mutex_unlock(&idt82p33->reg_lock);
702 return 0;
703 }
704
705 err = _idt82p33_adjtime(channel, delta_ns);
706
707 if (err) {
708 mutex_unlock(&idt82p33->reg_lock);
709 return err;
710 }
711
712 err = idt82p33_sync_tod(channel, true);
713
714 mutex_unlock(&idt82p33->reg_lock);
715
716 return err;
717}
718
719static int idt82p33_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
720{
721 struct idt82p33_channel *channel =
722 container_of(ptp, struct idt82p33_channel, caps);
723 struct idt82p33 *idt82p33 = channel->idt82p33;
724 int err;
725
726 mutex_lock(&idt82p33->reg_lock);
727 err = _idt82p33_gettime(channel, ts);
728 mutex_unlock(&idt82p33->reg_lock);
729
730 return err;
731}
732
733static int idt82p33_settime(struct ptp_clock_info *ptp,
734 const struct timespec64 *ts)
735{
736 struct idt82p33_channel *channel =
737 container_of(ptp, struct idt82p33_channel, caps);
738 struct idt82p33 *idt82p33 = channel->idt82p33;
739 int err;
740
741 mutex_lock(&idt82p33->reg_lock);
742 err = _idt82p33_settime(channel, ts);
743 mutex_unlock(&idt82p33->reg_lock);
744
745 return err;
746}
747
748static int idt82p33_channel_init(struct idt82p33_channel *channel, int index)
749{
750 switch (index) {
751 case 0:
752 channel->dpll_tod_cnfg = DPLL1_TOD_CNFG;
753 channel->dpll_tod_trigger = DPLL1_TOD_TRIGGER;
754 channel->dpll_tod_sts = DPLL1_TOD_STS;
755 channel->dpll_mode_cnfg = DPLL1_OPERATING_MODE_CNFG;
756 channel->dpll_freq_cnfg = DPLL1_HOLDOVER_FREQ_CNFG;
757 channel->dpll_phase_cnfg = DPLL1_PHASE_OFFSET_CNFG;
758 channel->dpll_sync_cnfg = DPLL1_SYNC_EDGE_CNFG;
759 channel->dpll_input_mode_cnfg = DPLL1_INPUT_MODE_CNFG;
760 break;
761 case 1:
762 channel->dpll_tod_cnfg = DPLL2_TOD_CNFG;
763 channel->dpll_tod_trigger = DPLL2_TOD_TRIGGER;
764 channel->dpll_tod_sts = DPLL2_TOD_STS;
765 channel->dpll_mode_cnfg = DPLL2_OPERATING_MODE_CNFG;
766 channel->dpll_freq_cnfg = DPLL2_HOLDOVER_FREQ_CNFG;
767 channel->dpll_phase_cnfg = DPLL2_PHASE_OFFSET_CNFG;
768 channel->dpll_sync_cnfg = DPLL2_SYNC_EDGE_CNFG;
769 channel->dpll_input_mode_cnfg = DPLL2_INPUT_MODE_CNFG;
770 break;
771 default:
772 return -EINVAL;
773 }
774
775 INIT_DELAYED_WORK(&channel->sync_tod_work,
776 idt82p33_sync_tod_work_handler);
777 channel->sync_tod_on = false;
778 channel->current_freq_ppb = 0;
779
780 return 0;
781}
782
783static void idt82p33_caps_init(struct ptp_clock_info *caps)
784{
785 caps->owner = THIS_MODULE;
786 caps->max_adj = 92000;
787 caps->adjfine = idt82p33_adjfine;
788 caps->adjtime = idt82p33_adjtime;
789 caps->gettime64 = idt82p33_gettime;
790 caps->settime64 = idt82p33_settime;
791 caps->enable = idt82p33_enable;
792}
793
794static int idt82p33_enable_channel(struct idt82p33 *idt82p33, u32 index)
795{
796 struct idt82p33_channel *channel;
797 int err;
798
799 if (!(index < MAX_PHC_PLL))
800 return -EINVAL;
801
802 channel = &idt82p33->channel[index];
803
804 err = idt82p33_channel_init(channel, index);
805 if (err)
806 return err;
807
808 channel->idt82p33 = idt82p33;
809
810 idt82p33_caps_init(&channel->caps);
811 snprintf(channel->caps.name, sizeof(channel->caps.name),
812 "IDT 82P33 PLL%u", index);
813 channel->caps.n_per_out = hweight8(channel->output_mask);
814
815 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
816 if (err)
817 return err;
818
819 err = idt82p33_enable_tod(channel);
820 if (err)
821 return err;
822
823 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
824
825 if (IS_ERR(channel->ptp_clock)) {
826 err = PTR_ERR(channel->ptp_clock);
827 channel->ptp_clock = NULL;
828 return err;
829 }
830
831 if (!channel->ptp_clock)
832 return -ENOTSUPP;
833
834 dev_info(&idt82p33->client->dev, "PLL%d registered as ptp%d\n",
835 index, channel->ptp_clock->index);
836
837 return 0;
838}
839
840static int idt82p33_load_firmware(struct idt82p33 *idt82p33)
841{
842 const struct firmware *fw;
843 struct idt82p33_fwrc *rec;
844 u8 loaddr, page, val;
845 int err;
846 s32 len;
847
848 dev_dbg(&idt82p33->client->dev,
849 "requesting firmware '%s'\n", FW_FILENAME);
850
851 err = request_firmware(&fw, FW_FILENAME, &idt82p33->client->dev);
852
853 if (err)
854 return err;
855
856 dev_dbg(&idt82p33->client->dev, "firmware size %zu bytes\n", fw->size);
857
858 rec = (struct idt82p33_fwrc *) fw->data;
859
860 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
861
862 if (rec->reserved) {
863 dev_err(&idt82p33->client->dev,
864 "bad firmware, reserved field non-zero\n");
865 err = -EINVAL;
866 } else {
867 val = rec->value;
868 loaddr = rec->loaddr;
869 page = rec->hiaddr;
870
871 rec++;
872
873 err = idt82p33_check_and_set_masks(idt82p33, page,
874 loaddr, val);
875 }
876
877 if (err == 0) {
878 /* maximum 8 pages */
879 if (page >= PAGE_NUM)
880 continue;
881
882 /* Page size 128, last 4 bytes of page skipped */
883 if (((loaddr > 0x7b) && (loaddr <= 0x7f))
884 || loaddr > 0xfb)
885 continue;
886
887 err = idt82p33_write(idt82p33, _ADDR(page, loaddr),
888 &val, sizeof(val));
889 }
890
891 if (err)
892 goto out;
893 }
894
895 idt82p33_display_masks(idt82p33);
896out:
897 release_firmware(fw);
898 return err;
899}
900
901
902static int idt82p33_probe(struct i2c_client *client,
903 const struct i2c_device_id *id)
904{
905 struct idt82p33 *idt82p33;
906 int err;
907 u8 i;
908
909 (void)id;
910
911 idt82p33 = devm_kzalloc(&client->dev,
912 sizeof(struct idt82p33), GFP_KERNEL);
913 if (!idt82p33)
914 return -ENOMEM;
915
916 mutex_init(&idt82p33->reg_lock);
917
918 idt82p33->client = client;
919 idt82p33->page_offset = 0xff;
920 idt82p33->tod_write_overhead_ns = 0;
921 idt82p33->calculate_overhead_flag = 0;
922 idt82p33->pll_mask = DEFAULT_PLL_MASK;
923 idt82p33->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
924 idt82p33->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
925
926 mutex_lock(&idt82p33->reg_lock);
927
928 err = idt82p33_load_firmware(idt82p33);
929
930 if (err)
931 dev_warn(&idt82p33->client->dev,
932 "loading firmware failed with %d\n", err);
933
934 if (idt82p33->pll_mask) {
935 for (i = 0; i < MAX_PHC_PLL; i++) {
936 if (idt82p33->pll_mask & (1 << i)) {
937 err = idt82p33_enable_channel(idt82p33, i);
938 if (err)
939 break;
940 }
941 }
942 } else {
943 dev_err(&idt82p33->client->dev,
944 "no PLLs flagged as PHCs, nothing to do\n");
945 err = -ENODEV;
946 }
947
948 mutex_unlock(&idt82p33->reg_lock);
949
950 if (err) {
951 idt82p33_ptp_clock_unregister_all(idt82p33);
952 return err;
953 }
954
955 i2c_set_clientdata(client, idt82p33);
956
957 return 0;
958}
959
960static int idt82p33_remove(struct i2c_client *client)
961{
962 struct idt82p33 *idt82p33 = i2c_get_clientdata(client);
963
964 idt82p33_ptp_clock_unregister_all(idt82p33);
965 mutex_destroy(&idt82p33->reg_lock);
966
967 return 0;
968}
969
970#ifdef CONFIG_OF
971static const struct of_device_id idt82p33_dt_id[] = {
972 { .compatible = "idt,82p33810" },
973 { .compatible = "idt,82p33813" },
974 { .compatible = "idt,82p33814" },
975 { .compatible = "idt,82p33831" },
976 { .compatible = "idt,82p33910" },
977 { .compatible = "idt,82p33913" },
978 { .compatible = "idt,82p33914" },
979 { .compatible = "idt,82p33931" },
980 {},
981};
982MODULE_DEVICE_TABLE(of, idt82p33_dt_id);
983#endif
984
985static const struct i2c_device_id idt82p33_i2c_id[] = {
986 { "idt82p33810", },
987 { "idt82p33813", },
988 { "idt82p33814", },
989 { "idt82p33831", },
990 { "idt82p33910", },
991 { "idt82p33913", },
992 { "idt82p33914", },
993 { "idt82p33931", },
994 {},
995};
996MODULE_DEVICE_TABLE(i2c, idt82p33_i2c_id);
997
998static struct i2c_driver idt82p33_driver = {
999 .driver = {
1000 .of_match_table = of_match_ptr(idt82p33_dt_id),
1001 .name = "idt82p33",
1002 },
1003 .probe = idt82p33_probe,
1004 .remove = idt82p33_remove,
1005 .id_table = idt82p33_i2c_id,
1006};
1007
1008module_i2c_driver(idt82p33_driver);
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 Integrated Device Technology, Inc
4//
5
6#define pr_fmt(fmt) "IDT_82p33xxx: " fmt
7
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>
17#include <linux/of.h>
18#include <linux/mfd/rsmu.h>
19#include <linux/mfd/idt82p33_reg.h>
20
21#include "ptp_private.h"
22#include "ptp_idt82p33.h"
23
24MODULE_DESCRIPTION("Driver for IDT 82p33xxx clock devices");
25MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
26MODULE_VERSION("1.0");
27MODULE_LICENSE("GPL");
28MODULE_FIRMWARE(FW_FILENAME);
29
30#define EXTTS_PERIOD_MS (95)
31
32/* Module Parameters */
33static u32 phase_snap_threshold = SNAP_THRESHOLD_NS;
34module_param(phase_snap_threshold, uint, 0);
35MODULE_PARM_DESC(phase_snap_threshold,
36"threshold (10000ns by default) below which adjtime would use double dco");
37
38static char *firmware;
39module_param(firmware, charp, 0);
40
41static struct ptp_pin_desc pin_config[MAX_PHC_PLL][MAX_TRIG_CLK];
42
43static inline int idt82p33_read(struct idt82p33 *idt82p33, u16 regaddr,
44 u8 *buf, u16 count)
45{
46 return regmap_bulk_read(idt82p33->regmap, regaddr, buf, count);
47}
48
49static inline int idt82p33_write(struct idt82p33 *idt82p33, u16 regaddr,
50 u8 *buf, u16 count)
51{
52 return regmap_bulk_write(idt82p33->regmap, regaddr, buf, count);
53}
54
55static void idt82p33_byte_array_to_timespec(struct timespec64 *ts,
56 u8 buf[TOD_BYTE_COUNT])
57{
58 time64_t sec;
59 s32 nsec;
60 u8 i;
61
62 nsec = buf[3];
63 for (i = 0; i < 3; i++) {
64 nsec <<= 8;
65 nsec |= buf[2 - i];
66 }
67
68 sec = buf[9];
69 for (i = 0; i < 5; i++) {
70 sec <<= 8;
71 sec |= buf[8 - i];
72 }
73
74 ts->tv_sec = sec;
75 ts->tv_nsec = nsec;
76}
77
78static void idt82p33_timespec_to_byte_array(struct timespec64 const *ts,
79 u8 buf[TOD_BYTE_COUNT])
80{
81 time64_t sec;
82 s32 nsec;
83 u8 i;
84
85 nsec = ts->tv_nsec;
86 sec = ts->tv_sec;
87
88 for (i = 0; i < 4; i++) {
89 buf[i] = nsec & 0xff;
90 nsec >>= 8;
91 }
92
93 for (i = 4; i < TOD_BYTE_COUNT; i++) {
94 buf[i] = sec & 0xff;
95 sec >>= 8;
96 }
97}
98
99static int idt82p33_dpll_set_mode(struct idt82p33_channel *channel,
100 enum pll_mode mode)
101{
102 struct idt82p33 *idt82p33 = channel->idt82p33;
103 u8 dpll_mode;
104 int err;
105
106 if (channel->pll_mode == mode)
107 return 0;
108
109 err = idt82p33_read(idt82p33, channel->dpll_mode_cnfg,
110 &dpll_mode, sizeof(dpll_mode));
111 if (err)
112 return err;
113
114 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
115
116 dpll_mode |= (mode << PLL_MODE_SHIFT);
117
118 err = idt82p33_write(idt82p33, channel->dpll_mode_cnfg,
119 &dpll_mode, sizeof(dpll_mode));
120 if (err)
121 return err;
122
123 channel->pll_mode = mode;
124
125 return 0;
126}
127
128static int idt82p33_set_tod_trigger(struct idt82p33_channel *channel,
129 u8 trigger, bool write)
130{
131 struct idt82p33 *idt82p33 = channel->idt82p33;
132 int err;
133 u8 cfg;
134
135 if (trigger > WR_TRIG_SEL_MAX)
136 return -EINVAL;
137
138 err = idt82p33_read(idt82p33, channel->dpll_tod_trigger,
139 &cfg, sizeof(cfg));
140
141 if (err)
142 return err;
143
144 if (write == true)
145 trigger = (trigger << WRITE_TRIGGER_SHIFT) |
146 (cfg & READ_TRIGGER_MASK);
147 else
148 trigger = (trigger << READ_TRIGGER_SHIFT) |
149 (cfg & WRITE_TRIGGER_MASK);
150
151 return idt82p33_write(idt82p33, channel->dpll_tod_trigger,
152 &trigger, sizeof(trigger));
153}
154
155static int idt82p33_get_extts(struct idt82p33_channel *channel,
156 struct timespec64 *ts)
157{
158 struct idt82p33 *idt82p33 = channel->idt82p33;
159 u8 buf[TOD_BYTE_COUNT];
160 int err;
161
162 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
163
164 if (err)
165 return err;
166
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)
169 return -EAGAIN;
170
171 memcpy(channel->extts_tod_sts, buf, TOD_BYTE_COUNT);
172
173 idt82p33_byte_array_to_timespec(ts, buf);
174
175 if (channel->discard_next_extts) {
176 channel->discard_next_extts = false;
177 return -EAGAIN;
178 }
179
180 return 0;
181}
182
183static int map_ref_to_tod_trig_sel(int ref, u8 *trigger)
184{
185 int err = 0;
186
187 switch (ref) {
188 case 0:
189 *trigger = HW_TOD_TRIG_SEL_IN12;
190 break;
191 case 1:
192 *trigger = HW_TOD_TRIG_SEL_IN13;
193 break;
194 case 2:
195 *trigger = HW_TOD_TRIG_SEL_IN14;
196 break;
197 default:
198 err = -EINVAL;
199 }
200
201 return err;
202}
203
204static bool is_one_shot(u8 mask)
205{
206 /* Treat single bit PLL masks as continuous trigger */
207 if ((mask == 1) || (mask == 2))
208 return false;
209 else
210 return true;
211}
212
213static int arm_tod_read_with_trigger(struct idt82p33_channel *channel, u8 trigger)
214{
215 struct idt82p33 *idt82p33 = channel->idt82p33;
216 u8 buf[TOD_BYTE_COUNT];
217 int err;
218
219 /* Remember the current tod_sts before setting the trigger */
220 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
221
222 if (err)
223 return err;
224
225 memcpy(channel->extts_tod_sts, buf, TOD_BYTE_COUNT);
226
227 err = idt82p33_set_tod_trigger(channel, trigger, false);
228
229 if (err)
230 dev_err(idt82p33->dev, "%s: err = %d", __func__, err);
231
232 return err;
233}
234
235static int idt82p33_extts_enable(struct idt82p33_channel *channel,
236 struct ptp_clock_request *rq, int on)
237{
238 u8 index = rq->extts.index;
239 struct idt82p33 *idt82p33;
240 u8 mask = 1 << index;
241 int err = 0;
242 u8 old_mask;
243 u8 trigger;
244 int ref;
245
246 idt82p33 = channel->idt82p33;
247 old_mask = idt82p33->extts_mask;
248
249 /* Reject requests with unsupported flags */
250 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
251 PTP_RISING_EDGE |
252 PTP_FALLING_EDGE |
253 PTP_STRICT_FLAGS))
254 return -EOPNOTSUPP;
255
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))
259 return -EOPNOTSUPP;
260
261 if (index >= MAX_PHC_PLL)
262 return -EINVAL;
263
264 if (on) {
265 /* Return if it was already enabled */
266 if (idt82p33->extts_mask & mask)
267 return 0;
268
269 /* Use the pin configured for the channel */
270 ref = ptp_find_pin(channel->ptp_clock, PTP_PF_EXTTS, channel->plln);
271
272 if (ref < 0) {
273 dev_err(idt82p33->dev, "%s: No valid pin found for Pll%d!\n",
274 __func__, channel->plln);
275 return -EBUSY;
276 }
277
278 err = map_ref_to_tod_trig_sel(ref, &trigger);
279
280 if (err) {
281 dev_err(idt82p33->dev,
282 "%s: Unsupported ref %d!\n", __func__, ref);
283 return err;
284 }
285
286 err = arm_tod_read_with_trigger(&idt82p33->channel[index], trigger);
287
288 if (err == 0) {
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);
293
294 if (old_mask)
295 return 0;
296
297 schedule_delayed_work(&idt82p33->extts_work,
298 msecs_to_jiffies(EXTTS_PERIOD_MS));
299 }
300 } else {
301 idt82p33->extts_mask &= ~mask;
302 idt82p33->extts_single_shot = is_one_shot(idt82p33->extts_mask);
303
304 if (idt82p33->extts_mask == 0)
305 cancel_delayed_work(&idt82p33->extts_work);
306 }
307
308 return err;
309}
310
311static int idt82p33_extts_check_channel(struct idt82p33 *idt82p33, u8 todn)
312{
313 struct idt82p33_channel *event_channel;
314 struct ptp_clock_event event;
315 struct timespec64 ts;
316 int err;
317
318 err = idt82p33_get_extts(&idt82p33->channel[todn], &ts);
319 if (err == 0) {
320 event_channel = idt82p33->event_channel[todn];
321 event.type = PTP_CLOCK_EXTTS;
322 event.index = todn;
323 event.timestamp = timespec64_to_ns(&ts);
324 ptp_clock_event(event_channel->ptp_clock,
325 &event);
326 }
327 return err;
328}
329
330static u8 idt82p33_extts_enable_mask(struct idt82p33_channel *channel,
331 u8 extts_mask, bool enable)
332{
333 struct idt82p33 *idt82p33 = channel->idt82p33;
334 u8 trigger = channel->tod_trigger;
335 u8 mask;
336 int err;
337 int i;
338
339 if (extts_mask == 0)
340 return 0;
341
342 if (enable == false)
343 cancel_delayed_work_sync(&idt82p33->extts_work);
344
345 for (i = 0; i < MAX_PHC_PLL; i++) {
346 mask = 1 << i;
347
348 if ((extts_mask & mask) == 0)
349 continue;
350
351 if (enable) {
352 err = arm_tod_read_with_trigger(&idt82p33->channel[i], trigger);
353 if (err)
354 dev_err(idt82p33->dev,
355 "%s: Arm ToD read trigger failed, err = %d",
356 __func__, err);
357 } else {
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 */
361 extts_mask &= ~mask;
362 }
363 }
364
365 if (enable)
366 schedule_delayed_work(&idt82p33->extts_work,
367 msecs_to_jiffies(EXTTS_PERIOD_MS));
368
369 return extts_mask;
370}
371
372static int _idt82p33_gettime(struct idt82p33_channel *channel,
373 struct timespec64 *ts)
374{
375 struct idt82p33 *idt82p33 = channel->idt82p33;
376 u8 old_mask = idt82p33->extts_mask;
377 u8 buf[TOD_BYTE_COUNT];
378 u8 new_mask = 0;
379 int err;
380
381 /* Disable extts */
382 if (old_mask)
383 new_mask = idt82p33_extts_enable_mask(channel, old_mask, false);
384
385 err = idt82p33_set_tod_trigger(channel, HW_TOD_RD_TRIG_SEL_LSB_TOD_STS,
386 false);
387 if (err)
388 return err;
389
390 channel->discard_next_extts = true;
391
392 if (idt82p33->calculate_overhead_flag)
393 idt82p33->start_time = ktime_get_raw();
394
395 err = idt82p33_read(idt82p33, channel->dpll_tod_sts, buf, sizeof(buf));
396
397 if (err)
398 return err;
399
400 /* Re-enable extts */
401 if (new_mask)
402 idt82p33_extts_enable_mask(channel, new_mask, true);
403
404 idt82p33_byte_array_to_timespec(ts, buf);
405
406 return 0;
407}
408
409/*
410 * TOD Trigger:
411 * Bits[7:4] Write 0x9, MSB write
412 * Bits[3:0] Read 0x9, LSB read
413 */
414
415static int _idt82p33_settime(struct idt82p33_channel *channel,
416 struct timespec64 const *ts)
417{
418 struct idt82p33 *idt82p33 = channel->idt82p33;
419 struct timespec64 local_ts = *ts;
420 char buf[TOD_BYTE_COUNT];
421 s64 dynamic_overhead_ns;
422 int err;
423 u8 i;
424
425 err = idt82p33_set_tod_trigger(channel, HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
426 true);
427 if (err)
428 return err;
429
430 channel->discard_next_extts = true;
431
432 if (idt82p33->calculate_overhead_flag) {
433 dynamic_overhead_ns = ktime_to_ns(ktime_get_raw())
434 - ktime_to_ns(idt82p33->start_time);
435
436 timespec64_add_ns(&local_ts, dynamic_overhead_ns);
437
438 idt82p33->calculate_overhead_flag = 0;
439 }
440
441 idt82p33_timespec_to_byte_array(&local_ts, buf);
442
443 /*
444 * Store the new time value.
445 */
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]));
449 if (err)
450 return err;
451 }
452
453 return err;
454}
455
456static int _idt82p33_adjtime_immediate(struct idt82p33_channel *channel,
457 s64 delta_ns)
458{
459 struct idt82p33 *idt82p33 = channel->idt82p33;
460 struct timespec64 ts;
461 s64 now_ns;
462 int err;
463
464 idt82p33->calculate_overhead_flag = 1;
465
466 err = _idt82p33_gettime(channel, &ts);
467
468 if (err)
469 return err;
470
471 now_ns = timespec64_to_ns(&ts);
472 now_ns += delta_ns + idt82p33->tod_write_overhead_ns;
473
474 ts = ns_to_timespec64(now_ns);
475
476 err = _idt82p33_settime(channel, &ts);
477
478 return err;
479}
480
481static int _idt82p33_adjtime_internal_triggered(struct idt82p33_channel *channel,
482 s64 delta_ns)
483{
484 struct idt82p33 *idt82p33 = channel->idt82p33;
485 char buf[TOD_BYTE_COUNT];
486 struct timespec64 ts;
487 const u8 delay_ns = 32;
488 s32 remainder;
489 s64 ns;
490 int err;
491
492 err = _idt82p33_gettime(channel, &ts);
493
494 if (err)
495 return err;
496
497 if (ts.tv_nsec > (NSEC_PER_SEC - 5 * NSEC_PER_MSEC)) {
498 /* Too close to miss next trigger, so skip it */
499 mdelay(6);
500 ns = (ts.tv_sec + 2) * NSEC_PER_SEC + delta_ns + delay_ns;
501 } else
502 ns = (ts.tv_sec + 1) * NSEC_PER_SEC + delta_ns + delay_ns;
503
504 ts = ns_to_timespec64(ns);
505 idt82p33_timespec_to_byte_array(&ts, buf);
506
507 /*
508 * Store the new time value.
509 */
510 err = idt82p33_write(idt82p33, channel->dpll_tod_cnfg, buf, sizeof(buf));
511 if (err)
512 return err;
513
514 /* Schedule to implement the workaround in one second */
515 (void)div_s64_rem(delta_ns, NSEC_PER_SEC, &remainder);
516 if (remainder != 0)
517 schedule_delayed_work(&channel->adjtime_work, HZ);
518
519 return idt82p33_set_tod_trigger(channel, HW_TOD_TRIG_SEL_TOD_PPS, true);
520}
521
522static void idt82p33_adjtime_workaround(struct work_struct *work)
523{
524 struct idt82p33_channel *channel = container_of(work,
525 struct idt82p33_channel,
526 adjtime_work.work);
527 struct idt82p33 *idt82p33 = channel->idt82p33;
528
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);
533}
534
535static int _idt82p33_adjfine(struct idt82p33_channel *channel, long scaled_ppm)
536{
537 struct idt82p33 *idt82p33 = channel->idt82p33;
538 unsigned char buf[5] = {0};
539 int err, i;
540 s64 fcw;
541
542 /*
543 * Frequency Control Word unit is: 1.6861512 * 10^-10 ppm
544 *
545 * adjfreq:
546 * ppb * 10^14
547 * FCW = -----------
548 * 16861512
549 *
550 * adjfine:
551 * scaled_ppm * 5^12 * 10^5
552 * FCW = ------------------------
553 * 16861512 * 2^4
554 */
555
556 fcw = scaled_ppm * 762939453125ULL;
557 fcw = div_s64(fcw, 8430756LL);
558
559 for (i = 0; i < 5; i++) {
560 buf[i] = fcw & 0xff;
561 fcw >>= 8;
562 }
563
564 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
565
566 if (err)
567 return err;
568
569 err = idt82p33_write(idt82p33, channel->dpll_freq_cnfg,
570 buf, sizeof(buf));
571
572 return err;
573}
574
575/* ppb = scaled_ppm * 125 / 2^13 */
576static s32 idt82p33_ddco_scaled_ppm(long current_ppm, s32 ddco_ppb)
577{
578 s64 scaled_ppm = div_s64(((s64)ddco_ppb << 13), 125);
579 s64 max_scaled_ppm = div_s64(((s64)DCO_MAX_PPB << 13), 125);
580
581 current_ppm += scaled_ppm;
582
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;
587
588 return (s32)current_ppm;
589}
590
591static int idt82p33_stop_ddco(struct idt82p33_channel *channel)
592{
593 int err;
594
595 err = _idt82p33_adjfine(channel, channel->current_freq);
596 if (err)
597 return err;
598
599 channel->ddco = false;
600
601 return 0;
602}
603
604static int idt82p33_start_ddco(struct idt82p33_channel *channel, s32 delta_ns)
605{
606 s32 current_ppm = channel->current_freq;
607 u32 duration_ms = MSEC_PER_SEC;
608 s32 ppb;
609 int err;
610
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
614 */
615 if (abs(delta_ns) < DDCO_THRESHOLD_NS)
616 return 0;
617
618 /* For most cases, keep ddco duration 1 second */
619 ppb = delta_ns;
620 while (abs(ppb) > DCO_MAX_PPB) {
621 duration_ms *= 2;
622 ppb /= 2;
623 }
624
625 err = _idt82p33_adjfine(channel,
626 idt82p33_ddco_scaled_ppm(current_ppm, ppb));
627 if (err)
628 return err;
629
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;
634
635 return 0;
636}
637
638static int idt82p33_measure_one_byte_write_overhead(
639 struct idt82p33_channel *channel, s64 *overhead_ns)
640{
641 struct idt82p33 *idt82p33 = channel->idt82p33;
642 ktime_t start, stop;
643 u8 trigger = 0;
644 s64 total_ns;
645 int err;
646 u8 i;
647
648 total_ns = 0;
649 *overhead_ns = 0;
650
651 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
652
653 start = ktime_get_raw();
654
655 err = idt82p33_write(idt82p33, channel->dpll_tod_trigger,
656 &trigger, sizeof(trigger));
657
658 stop = ktime_get_raw();
659
660 if (err)
661 return err;
662
663 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
664 }
665
666 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
667
668 return err;
669}
670
671static int idt82p33_measure_one_byte_read_overhead(
672 struct idt82p33_channel *channel, s64 *overhead_ns)
673{
674 struct idt82p33 *idt82p33 = channel->idt82p33;
675 ktime_t start, stop;
676 u8 trigger = 0;
677 s64 total_ns;
678 int err;
679 u8 i;
680
681 total_ns = 0;
682 *overhead_ns = 0;
683
684 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
685
686 start = ktime_get_raw();
687
688 err = idt82p33_read(idt82p33, channel->dpll_tod_trigger,
689 &trigger, sizeof(trigger));
690
691 stop = ktime_get_raw();
692
693 if (err)
694 return err;
695
696 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
697 }
698
699 *overhead_ns = div_s64(total_ns, MAX_MEASURMENT_COUNT);
700
701 return err;
702}
703
704static int idt82p33_measure_tod_write_9_byte_overhead(
705 struct idt82p33_channel *channel)
706{
707 struct idt82p33 *idt82p33 = channel->idt82p33;
708 u8 buf[TOD_BYTE_COUNT];
709 ktime_t start, stop;
710 s64 total_ns;
711 int err = 0;
712 u8 i, j;
713
714 total_ns = 0;
715 idt82p33->tod_write_overhead_ns = 0;
716
717 for (i = 0; i < MAX_MEASURMENT_COUNT; i++) {
718
719 start = ktime_get_raw();
720
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]));
726 if (err)
727 return err;
728 }
729
730 stop = ktime_get_raw();
731
732 total_ns += ktime_to_ns(stop) - ktime_to_ns(start);
733 }
734
735 idt82p33->tod_write_overhead_ns = div_s64(total_ns,
736 MAX_MEASURMENT_COUNT);
737
738 return err;
739}
740
741static int idt82p33_measure_settime_gettime_gap_overhead(
742 struct idt82p33_channel *channel, s64 *overhead_ns)
743{
744 struct timespec64 ts1 = {0, 0};
745 struct timespec64 ts2;
746 int err;
747
748 *overhead_ns = 0;
749
750 err = _idt82p33_settime(channel, &ts1);
751
752 if (err)
753 return err;
754
755 err = _idt82p33_gettime(channel, &ts2);
756
757 if (!err)
758 *overhead_ns = timespec64_to_ns(&ts2) - timespec64_to_ns(&ts1);
759
760 return err;
761}
762
763static int idt82p33_measure_tod_write_overhead(struct idt82p33_channel *channel)
764{
765 s64 trailing_overhead_ns, one_byte_write_ns, gap_ns, one_byte_read_ns;
766 struct idt82p33 *idt82p33 = channel->idt82p33;
767 int err;
768
769 idt82p33->tod_write_overhead_ns = 0;
770
771 err = idt82p33_measure_settime_gettime_gap_overhead(channel, &gap_ns);
772
773 if (err) {
774 dev_err(idt82p33->dev,
775 "Failed in %s with err %d!\n", __func__, err);
776 return err;
777 }
778
779 err = idt82p33_measure_one_byte_write_overhead(channel,
780 &one_byte_write_ns);
781
782 if (err)
783 return err;
784
785 err = idt82p33_measure_one_byte_read_overhead(channel,
786 &one_byte_read_ns);
787
788 if (err)
789 return err;
790
791 err = idt82p33_measure_tod_write_9_byte_overhead(channel);
792
793 if (err)
794 return err;
795
796 trailing_overhead_ns = gap_ns - 2 * one_byte_write_ns
797 - one_byte_read_ns;
798
799 idt82p33->tod_write_overhead_ns -= trailing_overhead_ns;
800
801 return err;
802}
803
804static int idt82p33_check_and_set_masks(struct idt82p33 *idt82p33,
805 u8 page,
806 u8 offset,
807 u8 val)
808{
809 int err = 0;
810
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);
815 err = -EINVAL;
816 } else {
817 idt82p33->pll_mask = val;
818 }
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;
825 }
826
827 return err;
828}
829
830static void idt82p33_display_masks(struct idt82p33 *idt82p33)
831{
832 u8 mask, i;
833
834 dev_info(idt82p33->dev,
835 "pllmask = 0x%02x\n", idt82p33->pll_mask);
836
837 for (i = 0; i < MAX_PHC_PLL; i++) {
838 mask = 1 << i;
839
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);
844 }
845}
846
847static int idt82p33_sync_tod(struct idt82p33_channel *channel, bool enable)
848{
849 struct idt82p33 *idt82p33 = channel->idt82p33;
850 u8 sync_cnfg;
851 int err;
852
853 err = idt82p33_read(idt82p33, channel->dpll_sync_cnfg,
854 &sync_cnfg, sizeof(sync_cnfg));
855 if (err)
856 return err;
857
858 sync_cnfg &= ~SYNC_TOD;
859 if (enable)
860 sync_cnfg |= SYNC_TOD;
861
862 return idt82p33_write(idt82p33, channel->dpll_sync_cnfg,
863 &sync_cnfg, sizeof(sync_cnfg));
864}
865
866static long idt82p33_work_handler(struct ptp_clock_info *ptp)
867{
868 struct idt82p33_channel *channel =
869 container_of(ptp, struct idt82p33_channel, caps);
870 struct idt82p33 *idt82p33 = channel->idt82p33;
871
872 mutex_lock(idt82p33->lock);
873 (void)idt82p33_stop_ddco(channel);
874 mutex_unlock(idt82p33->lock);
875
876 /* Return a negative value here to not reschedule */
877 return -1;
878}
879
880static int idt82p33_output_enable(struct idt82p33_channel *channel,
881 bool enable, unsigned int outn)
882{
883 struct idt82p33 *idt82p33 = channel->idt82p33;
884 int err;
885 u8 val;
886
887 err = idt82p33_read(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
888 if (err)
889 return err;
890 if (enable)
891 val &= ~SQUELCH_ENABLE;
892 else
893 val |= SQUELCH_ENABLE;
894
895 return idt82p33_write(idt82p33, OUT_MUX_CNFG(outn), &val, sizeof(val));
896}
897
898static int idt82p33_perout_enable(struct idt82p33_channel *channel,
899 bool enable,
900 struct ptp_perout_request *perout)
901{
902 /* Enable/disable individual output instead */
903 return idt82p33_output_enable(channel, enable, perout->index);
904}
905
906static int idt82p33_enable_tod(struct idt82p33_channel *channel)
907{
908 struct idt82p33 *idt82p33 = channel->idt82p33;
909 struct timespec64 ts = {0, 0};
910 int err;
911
912 err = idt82p33_measure_tod_write_overhead(channel);
913
914 if (err) {
915 dev_err(idt82p33->dev,
916 "Failed in %s with err %d!\n", __func__, err);
917 return err;
918 }
919
920 err = _idt82p33_settime(channel, &ts);
921
922 if (err)
923 return err;
924
925 return idt82p33_sync_tod(channel, true);
926}
927
928static void idt82p33_ptp_clock_unregister_all(struct idt82p33 *idt82p33)
929{
930 struct idt82p33_channel *channel;
931 u8 i;
932
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);
938 }
939}
940
941
942
943static int idt82p33_enable(struct ptp_clock_info *ptp,
944 struct ptp_clock_request *rq, int on)
945{
946 struct idt82p33_channel *channel =
947 container_of(ptp, struct idt82p33_channel, caps);
948 struct idt82p33 *idt82p33 = channel->idt82p33;
949 int err = -EOPNOTSUPP;
950
951 mutex_lock(idt82p33->lock);
952
953 switch (rq->type) {
954 case PTP_CLK_REQ_PEROUT:
955 if (!on)
956 err = idt82p33_perout_enable(channel, false,
957 &rq->perout);
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)
961 err = -ERANGE;
962 else
963 err = idt82p33_perout_enable(channel, true,
964 &rq->perout);
965 break;
966 case PTP_CLK_REQ_EXTTS:
967 err = idt82p33_extts_enable(channel, rq, on);
968 break;
969 default:
970 break;
971 }
972
973 mutex_unlock(idt82p33->lock);
974
975 if (err)
976 dev_err(idt82p33->dev,
977 "Failed in %s with err %d!\n", __func__, err);
978 return err;
979}
980
981static int idt82p33_adjwritephase(struct ptp_clock_info *ptp, s32 offset_ns)
982{
983 struct idt82p33_channel *channel =
984 container_of(ptp, struct idt82p33_channel, caps);
985 struct idt82p33 *idt82p33 = channel->idt82p33;
986 s64 offset_regval, offset_fs;
987 u8 val[4] = {0};
988 int err;
989
990 offset_fs = (s64)(-offset_ns) * 1000000;
991
992 if (offset_fs > WRITE_PHASE_OFFSET_LIMIT)
993 offset_fs = WRITE_PHASE_OFFSET_LIMIT;
994 else if (offset_fs < -WRITE_PHASE_OFFSET_LIMIT)
995 offset_fs = -WRITE_PHASE_OFFSET_LIMIT;
996
997 /* Convert from phaseoffset_fs to register value */
998 offset_regval = div_s64(offset_fs * 1000, IDT_T0DPLL_PHASE_RESOL);
999
1000 val[0] = offset_regval & 0xFF;
1001 val[1] = (offset_regval >> 8) & 0xFF;
1002 val[2] = (offset_regval >> 16) & 0xFF;
1003 val[3] = (offset_regval >> 24) & 0x1F;
1004 val[3] |= PH_OFFSET_EN;
1005
1006 mutex_lock(idt82p33->lock);
1007
1008 err = idt82p33_dpll_set_mode(channel, PLL_MODE_WPH);
1009 if (err) {
1010 dev_err(idt82p33->dev,
1011 "Failed in %s with err %d!\n", __func__, err);
1012 goto out;
1013 }
1014
1015 err = idt82p33_write(idt82p33, channel->dpll_phase_cnfg, val,
1016 sizeof(val));
1017
1018out:
1019 mutex_unlock(idt82p33->lock);
1020 return err;
1021}
1022
1023static int idt82p33_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1024{
1025 struct idt82p33_channel *channel =
1026 container_of(ptp, struct idt82p33_channel, caps);
1027 struct idt82p33 *idt82p33 = channel->idt82p33;
1028 int err;
1029
1030 if (channel->ddco == true)
1031 return 0;
1032
1033 if (scaled_ppm == channel->current_freq)
1034 return 0;
1035
1036 mutex_lock(idt82p33->lock);
1037 err = _idt82p33_adjfine(channel, scaled_ppm);
1038
1039 if (err == 0)
1040 channel->current_freq = scaled_ppm;
1041 mutex_unlock(idt82p33->lock);
1042
1043 if (err)
1044 dev_err(idt82p33->dev,
1045 "Failed in %s with err %d!\n", __func__, err);
1046 return err;
1047}
1048
1049static int idt82p33_adjtime(struct ptp_clock_info *ptp, s64 delta_ns)
1050{
1051 struct idt82p33_channel *channel =
1052 container_of(ptp, struct idt82p33_channel, caps);
1053 struct idt82p33 *idt82p33 = channel->idt82p33;
1054 int err;
1055
1056 if (channel->ddco == true)
1057 return -EBUSY;
1058
1059 mutex_lock(idt82p33->lock);
1060
1061 if (abs(delta_ns) < phase_snap_threshold) {
1062 err = idt82p33_start_ddco(channel, delta_ns);
1063 mutex_unlock(idt82p33->lock);
1064 return err;
1065 }
1066
1067 /* Use more accurate internal 1pps triggered write first */
1068 err = _idt82p33_adjtime_internal_triggered(channel, delta_ns);
1069 if (err && delta_ns > IMMEDIATE_SNAP_THRESHOLD_NS)
1070 err = _idt82p33_adjtime_immediate(channel, delta_ns);
1071
1072 mutex_unlock(idt82p33->lock);
1073
1074 if (err)
1075 dev_err(idt82p33->dev,
1076 "Failed in %s with err %d!\n", __func__, err);
1077 return err;
1078}
1079
1080static int idt82p33_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1081{
1082 struct idt82p33_channel *channel =
1083 container_of(ptp, struct idt82p33_channel, caps);
1084 struct idt82p33 *idt82p33 = channel->idt82p33;
1085 int err;
1086
1087 mutex_lock(idt82p33->lock);
1088 err = _idt82p33_gettime(channel, ts);
1089 mutex_unlock(idt82p33->lock);
1090
1091 if (err)
1092 dev_err(idt82p33->dev,
1093 "Failed in %s with err %d!\n", __func__, err);
1094 return err;
1095}
1096
1097static int idt82p33_settime(struct ptp_clock_info *ptp,
1098 const struct timespec64 *ts)
1099{
1100 struct idt82p33_channel *channel =
1101 container_of(ptp, struct idt82p33_channel, caps);
1102 struct idt82p33 *idt82p33 = channel->idt82p33;
1103 int err;
1104
1105 mutex_lock(idt82p33->lock);
1106 err = _idt82p33_settime(channel, ts);
1107 mutex_unlock(idt82p33->lock);
1108
1109 if (err)
1110 dev_err(idt82p33->dev,
1111 "Failed in %s with err %d!\n", __func__, err);
1112 return err;
1113}
1114
1115static int idt82p33_channel_init(struct idt82p33 *idt82p33, u32 index)
1116{
1117 struct idt82p33_channel *channel = &idt82p33->channel[index];
1118
1119 switch (index) {
1120 case 0:
1121 channel->dpll_tod_cnfg = DPLL1_TOD_CNFG;
1122 channel->dpll_tod_trigger = DPLL1_TOD_TRIGGER;
1123 channel->dpll_tod_sts = DPLL1_TOD_STS;
1124 channel->dpll_mode_cnfg = DPLL1_OPERATING_MODE_CNFG;
1125 channel->dpll_freq_cnfg = DPLL1_HOLDOVER_FREQ_CNFG;
1126 channel->dpll_phase_cnfg = DPLL1_PHASE_OFFSET_CNFG;
1127 channel->dpll_sync_cnfg = DPLL1_SYNC_EDGE_CNFG;
1128 channel->dpll_input_mode_cnfg = DPLL1_INPUT_MODE_CNFG;
1129 break;
1130 case 1:
1131 channel->dpll_tod_cnfg = DPLL2_TOD_CNFG;
1132 channel->dpll_tod_trigger = DPLL2_TOD_TRIGGER;
1133 channel->dpll_tod_sts = DPLL2_TOD_STS;
1134 channel->dpll_mode_cnfg = DPLL2_OPERATING_MODE_CNFG;
1135 channel->dpll_freq_cnfg = DPLL2_HOLDOVER_FREQ_CNFG;
1136 channel->dpll_phase_cnfg = DPLL2_PHASE_OFFSET_CNFG;
1137 channel->dpll_sync_cnfg = DPLL2_SYNC_EDGE_CNFG;
1138 channel->dpll_input_mode_cnfg = DPLL2_INPUT_MODE_CNFG;
1139 break;
1140 default:
1141 return -EINVAL;
1142 }
1143
1144 channel->plln = index;
1145 channel->current_freq = 0;
1146 channel->idt82p33 = idt82p33;
1147 INIT_DELAYED_WORK(&channel->adjtime_work, idt82p33_adjtime_workaround);
1148
1149 return 0;
1150}
1151
1152static int idt82p33_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
1153 enum ptp_pin_function func, unsigned int chan)
1154{
1155 switch (func) {
1156 case PTP_PF_NONE:
1157 case PTP_PF_EXTTS:
1158 break;
1159 case PTP_PF_PEROUT:
1160 case PTP_PF_PHYSYNC:
1161 return -1;
1162 }
1163 return 0;
1164}
1165
1166static void idt82p33_caps_init(u32 index, struct ptp_clock_info *caps,
1167 struct ptp_pin_desc *pin_cfg, u8 max_pins)
1168{
1169 struct ptp_pin_desc *ppd;
1170 int i;
1171
1172 caps->owner = THIS_MODULE;
1173 caps->max_adj = DCO_MAX_PPB;
1174 caps->n_per_out = MAX_PER_OUT;
1175 caps->n_ext_ts = MAX_PHC_PLL,
1176 caps->n_pins = max_pins,
1177 caps->adjphase = idt82p33_adjwritephase,
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;
1185
1186 snprintf(caps->name, sizeof(caps->name), "IDT 82P33 PLL%u", index);
1187
1188 caps->pin_config = pin_cfg;
1189
1190 for (i = 0; i < max_pins; ++i) {
1191 ppd = &pin_cfg[i];
1192
1193 ppd->index = i;
1194 ppd->func = PTP_PF_NONE;
1195 ppd->chan = index;
1196 snprintf(ppd->name, sizeof(ppd->name), "in%d", 12 + i);
1197 }
1198}
1199
1200static int idt82p33_enable_channel(struct idt82p33 *idt82p33, u32 index)
1201{
1202 struct idt82p33_channel *channel;
1203 int err;
1204
1205 if (!(index < MAX_PHC_PLL))
1206 return -EINVAL;
1207
1208 channel = &idt82p33->channel[index];
1209
1210 err = idt82p33_channel_init(idt82p33, index);
1211 if (err) {
1212 dev_err(idt82p33->dev,
1213 "Channel_init failed in %s with err %d!\n",
1214 __func__, err);
1215 return err;
1216 }
1217
1218 idt82p33_caps_init(index, &channel->caps,
1219 pin_config[index], MAX_TRIG_CLK);
1220
1221 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
1222
1223 if (IS_ERR(channel->ptp_clock)) {
1224 err = PTR_ERR(channel->ptp_clock);
1225 channel->ptp_clock = NULL;
1226 return err;
1227 }
1228
1229 if (!channel->ptp_clock)
1230 return -ENOTSUPP;
1231
1232 err = idt82p33_dpll_set_mode(channel, PLL_MODE_DCO);
1233 if (err) {
1234 dev_err(idt82p33->dev,
1235 "Dpll_set_mode failed in %s with err %d!\n",
1236 __func__, err);
1237 return err;
1238 }
1239
1240 err = idt82p33_enable_tod(channel);
1241 if (err) {
1242 dev_err(idt82p33->dev,
1243 "Enable_tod failed in %s with err %d!\n",
1244 __func__, err);
1245 return err;
1246 }
1247
1248 dev_info(idt82p33->dev, "PLL%d registered as ptp%d\n",
1249 index, channel->ptp_clock->index);
1250
1251 return 0;
1252}
1253
1254static int idt82p33_reset(struct idt82p33 *idt82p33, bool cold)
1255{
1256 int err;
1257 u8 cfg = SOFT_RESET_EN;
1258
1259 if (cold == true)
1260 goto cold_reset;
1261
1262 err = idt82p33_read(idt82p33, REG_SOFT_RESET, &cfg, sizeof(cfg));
1263 if (err) {
1264 dev_err(idt82p33->dev,
1265 "Soft reset failed with err %d!\n", err);
1266 return err;
1267 }
1268
1269 cfg |= SOFT_RESET_EN;
1270
1271cold_reset:
1272 err = idt82p33_write(idt82p33, REG_SOFT_RESET, &cfg, sizeof(cfg));
1273 if (err)
1274 dev_err(idt82p33->dev,
1275 "Cold reset failed with err %d!\n", err);
1276 return err;
1277}
1278
1279static int idt82p33_load_firmware(struct idt82p33 *idt82p33)
1280{
1281 char fname[128] = FW_FILENAME;
1282 const struct firmware *fw;
1283 struct idt82p33_fwrc *rec;
1284 u8 loaddr, page, val;
1285 int err;
1286 s32 len;
1287
1288 if (firmware) /* module parameter */
1289 snprintf(fname, sizeof(fname), "%s", firmware);
1290
1291 dev_info(idt82p33->dev, "requesting firmware '%s'\n", fname);
1292
1293 err = request_firmware(&fw, fname, idt82p33->dev);
1294
1295 if (err) {
1296 dev_err(idt82p33->dev,
1297 "Failed in %s with err %d!\n", __func__, err);
1298 return err;
1299 }
1300
1301 dev_dbg(idt82p33->dev, "firmware size %zu bytes\n", fw->size);
1302
1303 rec = (struct idt82p33_fwrc *) fw->data;
1304
1305 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1306
1307 if (rec->reserved) {
1308 dev_err(idt82p33->dev,
1309 "bad firmware, reserved field non-zero\n");
1310 err = -EINVAL;
1311 } else {
1312 val = rec->value;
1313 loaddr = rec->loaddr;
1314 page = rec->hiaddr;
1315
1316 rec++;
1317
1318 err = idt82p33_check_and_set_masks(idt82p33, page,
1319 loaddr, val);
1320 }
1321
1322 if (err == 0) {
1323 /* Page size 128, last 4 bytes of page skipped */
1324 if (loaddr > 0x7b)
1325 continue;
1326
1327 err = idt82p33_write(idt82p33, REG_ADDR(page, loaddr),
1328 &val, sizeof(val));
1329 }
1330
1331 if (err)
1332 goto out;
1333 }
1334
1335 idt82p33_display_masks(idt82p33);
1336out:
1337 release_firmware(fw);
1338 return err;
1339}
1340
1341static void idt82p33_extts_check(struct work_struct *work)
1342{
1343 struct idt82p33 *idt82p33 = container_of(work, struct idt82p33,
1344 extts_work.work);
1345 struct idt82p33_channel *channel;
1346 int err;
1347 u8 mask;
1348 int i;
1349
1350 if (idt82p33->extts_mask == 0)
1351 return;
1352
1353 mutex_lock(idt82p33->lock);
1354
1355 for (i = 0; i < MAX_PHC_PLL; i++) {
1356 mask = 1 << i;
1357
1358 if ((idt82p33->extts_mask & mask) == 0)
1359 continue;
1360
1361 err = idt82p33_extts_check_channel(idt82p33, i);
1362
1363 if (err == 0) {
1364 /* trigger clears itself, so clear the mask */
1365 if (idt82p33->extts_single_shot) {
1366 idt82p33->extts_mask &= ~mask;
1367 } else {
1368 /* Re-arm */
1369 channel = &idt82p33->channel[i];
1370 arm_tod_read_with_trigger(channel, channel->tod_trigger);
1371 }
1372 }
1373 }
1374
1375 if (idt82p33->extts_mask)
1376 schedule_delayed_work(&idt82p33->extts_work,
1377 msecs_to_jiffies(EXTTS_PERIOD_MS));
1378
1379 mutex_unlock(idt82p33->lock);
1380}
1381
1382static int idt82p33_probe(struct platform_device *pdev)
1383{
1384 struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
1385 struct idt82p33 *idt82p33;
1386 int err;
1387 u8 i;
1388
1389 idt82p33 = devm_kzalloc(&pdev->dev,
1390 sizeof(struct idt82p33), GFP_KERNEL);
1391 if (!idt82p33)
1392 return -ENOMEM;
1393
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);
1405
1406 mutex_lock(idt82p33->lock);
1407
1408 /* cold reset before loading firmware */
1409 idt82p33_reset(idt82p33, true);
1410
1411 err = idt82p33_load_firmware(idt82p33);
1412 if (err)
1413 dev_warn(idt82p33->dev,
1414 "loading firmware failed with %d\n", err);
1415
1416 /* soft reset after loading firmware */
1417 idt82p33_reset(idt82p33, false);
1418
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);
1423 else
1424 err = idt82p33_channel_init(idt82p33, i);
1425 if (err) {
1426 dev_err(idt82p33->dev,
1427 "Failed in %s with err %d!\n",
1428 __func__, err);
1429 break;
1430 }
1431 }
1432 } else {
1433 dev_err(idt82p33->dev,
1434 "no PLLs flagged as PHCs, nothing to do\n");
1435 err = -ENODEV;
1436 }
1437
1438 mutex_unlock(idt82p33->lock);
1439
1440 if (err) {
1441 idt82p33_ptp_clock_unregister_all(idt82p33);
1442 return err;
1443 }
1444
1445 platform_set_drvdata(pdev, idt82p33);
1446
1447 return 0;
1448}
1449
1450static int idt82p33_remove(struct platform_device *pdev)
1451{
1452 struct idt82p33 *idt82p33 = platform_get_drvdata(pdev);
1453
1454 cancel_delayed_work_sync(&idt82p33->extts_work);
1455
1456 idt82p33_ptp_clock_unregister_all(idt82p33);
1457
1458 return 0;
1459}
1460
1461static struct platform_driver idt82p33_driver = {
1462 .driver = {
1463 .name = "82p33x1x-phc",
1464 },
1465 .probe = idt82p33_probe,
1466 .remove = idt82p33_remove,
1467};
1468
1469module_platform_driver(idt82p33_driver);