Get rid of 'remove_new' relic from platform driver struct
[linux.git] / drivers / ptp / ptp_fc3.c
blobcfced36c70bcb2bca4bfd21b9524c32c4999dba9
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * PTP hardware clock driver for the FemtoClock3 family of timing and
4 * synchronization devices.
6 * Copyright (C) 2023 Integrated Device Technology, Inc., a Renesas Company.
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/string.h>
17 #include <linux/of.h>
18 #include <linux/bitfield.h>
19 #include <linux/mfd/rsmu.h>
20 #include <linux/mfd/idtRC38xxx_reg.h>
21 #include <linux/unaligned.h>
23 #include "ptp_private.h"
24 #include "ptp_fc3.h"
26 MODULE_DESCRIPTION("Driver for IDT FemtoClock3(TM) family");
27 MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
28 MODULE_VERSION("1.0");
29 MODULE_LICENSE("GPL");
32 * The name of the firmware file to be loaded
33 * over-rides any automatic selection
35 static char *firmware;
36 module_param(firmware, charp, 0);
38 static s64 ns2counters(struct idtfc3 *idtfc3, s64 nsec, u32 *sub_ns)
40 s64 sync;
41 s32 rem;
43 if (likely(nsec >= 0)) {
44 sync = div_u64_rem(nsec, idtfc3->ns_per_sync, &rem);
45 *sub_ns = rem;
46 } else {
47 sync = -div_u64_rem(-nsec - 1, idtfc3->ns_per_sync, &rem) - 1;
48 *sub_ns = idtfc3->ns_per_sync - rem - 1;
51 return sync * idtfc3->ns_per_sync;
54 static s64 tdc_meas2offset(struct idtfc3 *idtfc3, u64 meas_read)
56 s64 coarse, fine;
58 fine = sign_extend64(FIELD_GET(FINE_MEAS_MASK, meas_read), 12);
59 coarse = sign_extend64(FIELD_GET(COARSE_MEAS_MASK, meas_read), (39 - 13));
61 fine = div64_s64(fine * NSEC_PER_SEC, idtfc3->tdc_apll_freq * 62LL);
62 coarse = div64_s64(coarse * NSEC_PER_SEC, idtfc3->time_ref_freq);
64 return coarse + fine;
67 static s64 tdc_offset2phase(struct idtfc3 *idtfc3, s64 offset_ns)
69 if (offset_ns > idtfc3->ns_per_sync / 2)
70 offset_ns -= idtfc3->ns_per_sync;
72 return offset_ns * idtfc3->tdc_offset_sign;
75 static int idtfc3_set_lpf_mode(struct idtfc3 *idtfc3, u8 mode)
77 int err;
79 if (mode >= LPF_INVALID)
80 return -EINVAL;
82 if (idtfc3->lpf_mode == mode)
83 return 0;
85 err = regmap_bulk_write(idtfc3->regmap, LPF_MODE_CNFG, &mode, sizeof(mode));
86 if (err)
87 return err;
89 idtfc3->lpf_mode = mode;
91 return 0;
94 static int idtfc3_enable_lpf(struct idtfc3 *idtfc3, bool enable)
96 u8 val;
97 int err;
99 err = regmap_bulk_read(idtfc3->regmap, LPF_CTRL, &val, sizeof(val));
100 if (err)
101 return err;
103 if (enable == true)
104 val |= LPF_EN;
105 else
106 val &= ~LPF_EN;
108 return regmap_bulk_write(idtfc3->regmap, LPF_CTRL, &val, sizeof(val));
111 static int idtfc3_get_time_ref_freq(struct idtfc3 *idtfc3)
113 int err;
114 u8 buf[4];
115 u8 time_ref_div;
116 u8 time_clk_div;
118 err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_MEAS_DIV_CNFG, buf, sizeof(buf));
119 if (err)
120 return err;
121 time_ref_div = FIELD_GET(TIME_REF_DIV_MASK, get_unaligned_le32(buf)) + 1;
123 err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_COUNT, buf, 1);
124 if (err)
125 return err;
126 time_clk_div = (buf[0] & TIME_CLOCK_COUNT_MASK) + 1;
127 idtfc3->time_ref_freq = idtfc3->hw_param.time_clk_freq *
128 time_clk_div / time_ref_div;
130 return 0;
133 static int idtfc3_get_tdc_offset_sign(struct idtfc3 *idtfc3)
135 int err;
136 u8 buf[4];
137 u32 val;
138 u8 sig1, sig2;
140 err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_TDC_FANOUT_CNFG, buf, sizeof(buf));
141 if (err)
142 return err;
144 val = get_unaligned_le32(buf);
145 if ((val & TIME_SYNC_TO_TDC_EN) != TIME_SYNC_TO_TDC_EN) {
146 dev_err(idtfc3->dev, "TIME_SYNC_TO_TDC_EN is off !!!");
147 return -EINVAL;
150 sig1 = FIELD_GET(SIG1_MUX_SEL_MASK, val);
151 sig2 = FIELD_GET(SIG2_MUX_SEL_MASK, val);
153 if ((sig1 == sig2) || ((sig1 != TIME_SYNC) && (sig2 != TIME_SYNC))) {
154 dev_err(idtfc3->dev, "Invalid tdc_mux_sel sig1=%d sig2=%d", sig1, sig2);
155 return -EINVAL;
156 } else if (sig1 == TIME_SYNC) {
157 idtfc3->tdc_offset_sign = 1;
158 } else if (sig2 == TIME_SYNC) {
159 idtfc3->tdc_offset_sign = -1;
162 return 0;
165 static int idtfc3_lpf_bw(struct idtfc3 *idtfc3, u8 shift, u8 mult)
167 u8 val = FIELD_PREP(LPF_BW_SHIFT, shift) | FIELD_PREP(LPF_BW_MULT, mult);
169 return regmap_bulk_write(idtfc3->regmap, LPF_BW_CNFG, &val, sizeof(val));
172 static int idtfc3_enable_tdc(struct idtfc3 *idtfc3, bool enable, u8 meas_mode)
174 int err;
175 u8 val = 0;
177 /* Disable TDC first */
178 err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CTRL, &val, sizeof(val));
179 if (err)
180 return err;
182 if (enable == false)
183 return idtfc3_lpf_bw(idtfc3, LPF_BW_SHIFT_DEFAULT, LPF_BW_MULT_DEFAULT);
185 if (meas_mode >= MEAS_MODE_INVALID)
186 return -EINVAL;
188 /* Change TDC meas mode */
189 err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CNFG,
190 &meas_mode, sizeof(meas_mode));
191 if (err)
192 return err;
194 /* Enable TDC */
195 val = TDC_MEAS_EN;
196 if (meas_mode == CONTINUOUS)
197 val |= TDC_MEAS_START;
198 err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CTRL, &val, sizeof(val));
199 if (err)
200 return err;
202 return idtfc3_lpf_bw(idtfc3, LPF_BW_SHIFT_1PPS, LPF_BW_MULT_DEFAULT);
205 static bool get_tdc_meas(struct idtfc3 *idtfc3, s64 *offset_ns)
207 bool valid = false;
208 u8 buf[9];
209 u8 val;
210 int err;
212 while (true) {
213 err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_STS,
214 &val, sizeof(val));
215 if (err)
216 return false;
218 if (val & FIFO_EMPTY)
219 break;
221 err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_READ_REQ,
222 &buf, sizeof(buf));
223 if (err)
224 return false;
226 valid = true;
229 if (valid)
230 *offset_ns = tdc_meas2offset(idtfc3, get_unaligned_le64(&buf[1]));
232 return valid;
235 static int check_tdc_fifo_overrun(struct idtfc3 *idtfc3)
237 u8 val;
238 int err;
240 /* Check if FIFO is overrun */
241 err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_STS, &val, sizeof(val));
242 if (err)
243 return err;
245 if (!(val & FIFO_FULL))
246 return 0;
248 dev_warn(idtfc3->dev, "TDC FIFO overrun !!!");
250 err = idtfc3_enable_tdc(idtfc3, true, CONTINUOUS);
251 if (err)
252 return err;
254 return 0;
257 static int get_tdc_meas_continuous(struct idtfc3 *idtfc3)
259 int err;
260 s64 offset_ns;
261 struct ptp_clock_event event;
263 err = check_tdc_fifo_overrun(idtfc3);
264 if (err)
265 return err;
267 if (get_tdc_meas(idtfc3, &offset_ns) && offset_ns >= 0) {
268 event.index = 0;
269 event.offset = tdc_offset2phase(idtfc3, offset_ns);
270 event.type = PTP_CLOCK_EXTOFF;
271 ptp_clock_event(idtfc3->ptp_clock, &event);
274 return 0;
277 static int idtfc3_read_subcounter(struct idtfc3 *idtfc3)
279 u8 buf[5] = {0};
280 int err;
282 err = regmap_bulk_read(idtfc3->regmap, TOD_COUNTER_READ_REQ,
283 &buf, sizeof(buf));
284 if (err)
285 return err;
287 /* sync_counter_value is [31:82] and sub_sync_counter_value is [0:30] */
288 return get_unaligned_le32(&buf[1]) & SUB_SYNC_COUNTER_MASK;
291 static int idtfc3_tod_update_is_done(struct idtfc3 *idtfc3)
293 int err;
294 u8 req;
296 err = read_poll_timeout_atomic(regmap_bulk_read, err, !req, USEC_PER_MSEC,
297 idtfc3->tc_write_timeout, true, idtfc3->regmap,
298 TOD_SYNC_LOAD_REQ_CTRL, &req, 1);
299 if (err)
300 dev_err(idtfc3->dev, "TOD counter write timeout !!!");
302 return err;
305 static int idtfc3_write_subcounter(struct idtfc3 *idtfc3, u32 counter)
307 u8 buf[18] = {0};
308 int err;
310 /* sync_counter_value is [31:82] and sub_sync_counter_value is [0:30] */
311 put_unaligned_le32(counter & SUB_SYNC_COUNTER_MASK, &buf[0]);
313 buf[16] = SUB_SYNC_LOAD_ENABLE | SYNC_LOAD_ENABLE;
314 buf[17] = SYNC_LOAD_REQ;
316 err = regmap_bulk_write(idtfc3->regmap, TOD_SYNC_LOAD_VAL_CTRL,
317 &buf, sizeof(buf));
318 if (err)
319 return err;
321 return idtfc3_tod_update_is_done(idtfc3);
324 static int idtfc3_timecounter_update(struct idtfc3 *idtfc3, u32 counter, s64 ns)
326 int err;
328 err = idtfc3_write_subcounter(idtfc3, counter);
329 if (err)
330 return err;
332 /* Update time counter */
333 idtfc3->ns = ns;
334 idtfc3->last_counter = counter;
336 return 0;
339 static int idtfc3_timecounter_read(struct idtfc3 *idtfc3)
341 int now, delta;
343 now = idtfc3_read_subcounter(idtfc3);
344 if (now < 0)
345 return now;
347 /* calculate the delta since the last idtfc3_timecounter_read(): */
348 if (now >= idtfc3->last_counter)
349 delta = now - idtfc3->last_counter;
350 else
351 delta = idtfc3->sub_sync_count - idtfc3->last_counter + now;
353 /* Update time counter */
354 idtfc3->ns += delta * idtfc3->ns_per_counter;
355 idtfc3->last_counter = now;
357 return 0;
360 static int _idtfc3_gettime(struct idtfc3 *idtfc3, struct timespec64 *ts)
362 int err;
364 err = idtfc3_timecounter_read(idtfc3);
365 if (err)
366 return err;
368 *ts = ns_to_timespec64(idtfc3->ns);
370 return 0;
373 static int idtfc3_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
375 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
376 int err;
378 mutex_lock(idtfc3->lock);
379 err = _idtfc3_gettime(idtfc3, ts);
380 mutex_unlock(idtfc3->lock);
382 return err;
385 static int _idtfc3_settime(struct idtfc3 *idtfc3, const struct timespec64 *ts)
387 s64 offset_ns, now_ns;
388 u32 counter, sub_ns;
389 int now;
391 if (timespec64_valid(ts) == false) {
392 dev_err(idtfc3->dev, "%s: invalid timespec", __func__);
393 return -EINVAL;
396 now = idtfc3_read_subcounter(idtfc3);
397 if (now < 0)
398 return now;
400 offset_ns = (idtfc3->sub_sync_count - now) * idtfc3->ns_per_counter;
401 now_ns = timespec64_to_ns(ts);
402 (void)ns2counters(idtfc3, offset_ns + now_ns, &sub_ns);
404 counter = sub_ns / idtfc3->ns_per_counter;
405 return idtfc3_timecounter_update(idtfc3, counter, now_ns);
408 static int idtfc3_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts)
410 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
411 int err;
413 mutex_lock(idtfc3->lock);
414 err = _idtfc3_settime(idtfc3, ts);
415 mutex_unlock(idtfc3->lock);
417 return err;
420 static int _idtfc3_adjtime(struct idtfc3 *idtfc3, s64 delta)
423 * The TOD counter can be synchronously loaded with any value,
424 * to be loaded on the next Time Sync pulse
426 s64 sync_ns;
427 u32 sub_ns;
428 u32 counter;
430 if (idtfc3->ns + delta < 0) {
431 dev_err(idtfc3->dev, "%lld ns adj is too large", delta);
432 return -EINVAL;
435 sync_ns = ns2counters(idtfc3, delta + idtfc3->ns_per_sync, &sub_ns);
437 counter = sub_ns / idtfc3->ns_per_counter;
438 return idtfc3_timecounter_update(idtfc3, counter, idtfc3->ns + sync_ns +
439 counter * idtfc3->ns_per_counter);
442 static int idtfc3_adjtime(struct ptp_clock_info *ptp, s64 delta)
444 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
445 int err;
447 mutex_lock(idtfc3->lock);
448 err = _idtfc3_adjtime(idtfc3, delta);
449 mutex_unlock(idtfc3->lock);
451 return err;
454 static int _idtfc3_adjphase(struct idtfc3 *idtfc3, s32 delta)
456 u8 buf[8] = {0};
457 int err;
458 s64 pcw;
460 err = idtfc3_set_lpf_mode(idtfc3, LPF_WP);
461 if (err)
462 return err;
465 * Phase Control Word unit is: 10^9 / (TDC_APLL_FREQ * 124)
467 * delta * TDC_APLL_FREQ * 124
468 * PCW = ---------------------------
469 * 10^9
472 pcw = div_s64((s64)delta * idtfc3->tdc_apll_freq * 124, NSEC_PER_SEC);
474 put_unaligned_le64(pcw, buf);
476 return regmap_bulk_write(idtfc3->regmap, LPF_WR_PHASE_CTRL, buf, sizeof(buf));
479 static int idtfc3_adjphase(struct ptp_clock_info *ptp, s32 delta)
481 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
482 int err;
484 mutex_lock(idtfc3->lock);
485 err = _idtfc3_adjphase(idtfc3, delta);
486 mutex_unlock(idtfc3->lock);
488 return err;
491 static int _idtfc3_adjfine(struct idtfc3 *idtfc3, long scaled_ppm)
493 u8 buf[8] = {0};
494 int err;
495 s64 fcw;
497 err = idtfc3_set_lpf_mode(idtfc3, LPF_WF);
498 if (err)
499 return err;
502 * Frequency Control Word unit is: 2^-44 * 10^6 ppm
504 * adjfreq:
505 * ppb * 2^44
506 * FCW = ----------
507 * 10^9
509 * adjfine:
510 * ppm_16 * 2^28
511 * FCW = -------------
512 * 10^6
514 fcw = scaled_ppm * BIT(28);
515 fcw = div_s64(fcw, 1000000);
517 put_unaligned_le64(fcw, buf);
519 return regmap_bulk_write(idtfc3->regmap, LPF_WR_FREQ_CTRL, buf, sizeof(buf));
522 static int idtfc3_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
524 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
525 int err;
527 mutex_lock(idtfc3->lock);
528 err = _idtfc3_adjfine(idtfc3, scaled_ppm);
529 mutex_unlock(idtfc3->lock);
531 return err;
534 static int idtfc3_enable(struct ptp_clock_info *ptp,
535 struct ptp_clock_request *rq, int on)
537 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
538 int err = -EOPNOTSUPP;
540 mutex_lock(idtfc3->lock);
541 switch (rq->type) {
542 case PTP_CLK_REQ_PEROUT:
543 if (!on)
544 err = 0;
545 /* Only accept a 1-PPS aligned to the second. */
546 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
547 rq->perout.period.nsec)
548 err = -ERANGE;
549 else
550 err = 0;
551 break;
552 case PTP_CLK_REQ_EXTTS:
553 if (on) {
554 /* Only accept requests for external phase offset */
555 if ((rq->extts.flags & PTP_EXT_OFFSET) != (PTP_EXT_OFFSET))
556 err = -EOPNOTSUPP;
557 else
558 err = idtfc3_enable_tdc(idtfc3, true, CONTINUOUS);
559 } else {
560 err = idtfc3_enable_tdc(idtfc3, false, MEAS_MODE_INVALID);
562 break;
563 default:
564 break;
566 mutex_unlock(idtfc3->lock);
568 if (err)
569 dev_err(idtfc3->dev, "Failed in %s with err %d!", __func__, err);
571 return err;
574 static long idtfc3_aux_work(struct ptp_clock_info *ptp)
576 struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
577 static int tdc_get;
579 mutex_lock(idtfc3->lock);
580 tdc_get %= TDC_GET_PERIOD;
581 if ((tdc_get == 0) || (tdc_get == TDC_GET_PERIOD / 2))
582 idtfc3_timecounter_read(idtfc3);
583 get_tdc_meas_continuous(idtfc3);
584 tdc_get++;
585 mutex_unlock(idtfc3->lock);
587 return idtfc3->tc_update_period;
590 static const struct ptp_clock_info idtfc3_caps = {
591 .owner = THIS_MODULE,
592 .max_adj = MAX_FFO_PPB,
593 .n_per_out = 1,
594 .n_ext_ts = 1,
595 .adjphase = &idtfc3_adjphase,
596 .adjfine = &idtfc3_adjfine,
597 .adjtime = &idtfc3_adjtime,
598 .gettime64 = &idtfc3_gettime,
599 .settime64 = &idtfc3_settime,
600 .enable = &idtfc3_enable,
601 .do_aux_work = &idtfc3_aux_work,
604 static int idtfc3_hw_calibrate(struct idtfc3 *idtfc3)
606 int err = 0;
607 u8 val;
609 mdelay(10);
611 * Toggle TDC_DAC_RECAL_REQ:
612 * (1) set tdc_en to 1
613 * (2) set tdc_dac_recal_req to 0
614 * (3) set tdc_dac_recal_req to 1
616 val = TDC_EN;
617 err = regmap_bulk_write(idtfc3->regmap, TDC_CTRL,
618 &val, sizeof(val));
619 if (err)
620 return err;
621 val = TDC_EN | TDC_DAC_RECAL_REQ;
622 err = regmap_bulk_write(idtfc3->regmap, TDC_CTRL,
623 &val, sizeof(val));
624 if (err)
625 return err;
626 mdelay(10);
629 * Toggle APLL_REINIT:
630 * (1) set apll_reinit to 0
631 * (2) set apll_reinit to 1
633 val = 0;
634 err = regmap_bulk_write(idtfc3->regmap, SOFT_RESET_CTRL,
635 &val, sizeof(val));
636 if (err)
637 return err;
638 val = APLL_REINIT;
639 err = regmap_bulk_write(idtfc3->regmap, SOFT_RESET_CTRL,
640 &val, sizeof(val));
641 if (err)
642 return err;
643 mdelay(10);
645 return err;
648 static int idtfc3_init_timecounter(struct idtfc3 *idtfc3)
650 int err;
651 u32 period_ms;
653 period_ms = idtfc3->sub_sync_count * MSEC_PER_SEC /
654 idtfc3->hw_param.time_clk_freq;
656 idtfc3->tc_update_period = msecs_to_jiffies(period_ms / TDC_GET_PERIOD);
657 idtfc3->tc_write_timeout = period_ms * USEC_PER_MSEC;
659 err = idtfc3_timecounter_update(idtfc3, 0, 0);
660 if (err)
661 return err;
663 err = idtfc3_timecounter_read(idtfc3);
664 if (err)
665 return err;
667 ptp_schedule_worker(idtfc3->ptp_clock, idtfc3->tc_update_period);
669 return 0;
672 static int idtfc3_get_tdc_apll_freq(struct idtfc3 *idtfc3)
674 int err;
675 u8 tdc_fb_div_int;
676 u8 tdc_ref_div;
677 struct idtfc3_hw_param *param = &idtfc3->hw_param;
679 err = regmap_bulk_read(idtfc3->regmap, TDC_REF_DIV_CNFG,
680 &tdc_ref_div, sizeof(tdc_ref_div));
681 if (err)
682 return err;
684 err = regmap_bulk_read(idtfc3->regmap, TDC_FB_DIV_INT_CNFG,
685 &tdc_fb_div_int, sizeof(tdc_fb_div_int));
686 if (err)
687 return err;
689 tdc_fb_div_int &= TDC_FB_DIV_INT_MASK;
690 tdc_ref_div &= TDC_REF_DIV_CONFIG_MASK;
692 idtfc3->tdc_apll_freq = div_u64(param->xtal_freq * (u64)tdc_fb_div_int,
693 1 << tdc_ref_div);
695 return 0;
698 static int idtfc3_get_fod(struct idtfc3 *idtfc3)
700 int err;
701 u8 fod;
703 err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_SRC, &fod, sizeof(fod));
704 if (err)
705 return err;
707 switch (fod) {
708 case 0:
709 idtfc3->fod_n = FOD_0;
710 break;
711 case 1:
712 idtfc3->fod_n = FOD_1;
713 break;
714 case 2:
715 idtfc3->fod_n = FOD_2;
716 break;
717 default:
718 return -EINVAL;
721 return 0;
724 static int idtfc3_get_sync_count(struct idtfc3 *idtfc3)
726 int err;
727 u8 buf[4];
729 err = regmap_bulk_read(idtfc3->regmap, SUB_SYNC_GEN_CNFG, buf, sizeof(buf));
730 if (err)
731 return err;
733 idtfc3->sub_sync_count = (get_unaligned_le32(buf) & SUB_SYNC_COUNTER_MASK) + 1;
734 idtfc3->ns_per_counter = NSEC_PER_SEC / idtfc3->hw_param.time_clk_freq;
735 idtfc3->ns_per_sync = idtfc3->sub_sync_count * idtfc3->ns_per_counter;
737 return 0;
740 static int idtfc3_setup_hw_param(struct idtfc3 *idtfc3)
742 int err;
744 err = idtfc3_get_fod(idtfc3);
745 if (err)
746 return err;
748 err = idtfc3_get_sync_count(idtfc3);
749 if (err)
750 return err;
752 err = idtfc3_get_time_ref_freq(idtfc3);
753 if (err)
754 return err;
756 return idtfc3_get_tdc_apll_freq(idtfc3);
759 static int idtfc3_configure_hw(struct idtfc3 *idtfc3)
761 int err = 0;
763 err = idtfc3_hw_calibrate(idtfc3);
764 if (err)
765 return err;
767 err = idtfc3_enable_lpf(idtfc3, true);
768 if (err)
769 return err;
771 err = idtfc3_enable_tdc(idtfc3, false, MEAS_MODE_INVALID);
772 if (err)
773 return err;
775 err = idtfc3_get_tdc_offset_sign(idtfc3);
776 if (err)
777 return err;
779 return idtfc3_setup_hw_param(idtfc3);
782 static int idtfc3_set_overhead(struct idtfc3 *idtfc3)
784 s64 current_ns = 0;
785 s64 lowest_ns = 0;
786 int err;
787 u8 i;
788 ktime_t start;
789 ktime_t stop;
790 ktime_t diff;
792 char buf[18] = {0};
794 for (i = 0; i < 5; i++) {
795 start = ktime_get_raw();
797 err = regmap_bulk_write(idtfc3->regmap, TOD_SYNC_LOAD_VAL_CTRL,
798 &buf, sizeof(buf));
799 if (err)
800 return err;
802 stop = ktime_get_raw();
804 diff = ktime_sub(stop, start);
806 current_ns = ktime_to_ns(diff);
808 if (i == 0) {
809 lowest_ns = current_ns;
810 } else {
811 if (current_ns < lowest_ns)
812 lowest_ns = current_ns;
816 idtfc3->tod_write_overhead = lowest_ns;
818 return err;
821 static int idtfc3_enable_ptp(struct idtfc3 *idtfc3)
823 int err;
825 idtfc3->caps = idtfc3_caps;
826 snprintf(idtfc3->caps.name, sizeof(idtfc3->caps.name), "IDT FC3W");
827 idtfc3->ptp_clock = ptp_clock_register(&idtfc3->caps, NULL);
829 if (IS_ERR(idtfc3->ptp_clock)) {
830 err = PTR_ERR(idtfc3->ptp_clock);
831 idtfc3->ptp_clock = NULL;
832 return err;
835 err = idtfc3_set_overhead(idtfc3);
836 if (err)
837 return err;
839 err = idtfc3_init_timecounter(idtfc3);
840 if (err)
841 return err;
843 dev_info(idtfc3->dev, "TIME_SYNC_CHANNEL registered as ptp%d",
844 idtfc3->ptp_clock->index);
846 return 0;
849 static int idtfc3_load_firmware(struct idtfc3 *idtfc3)
851 char fname[128] = FW_FILENAME;
852 const struct firmware *fw;
853 struct idtfc3_fwrc *rec;
854 u16 addr;
855 u8 val;
856 int err;
857 s32 len;
859 idtfc3_default_hw_param(&idtfc3->hw_param);
861 if (firmware) /* module parameter */
862 snprintf(fname, sizeof(fname), "%s", firmware);
864 dev_info(idtfc3->dev, "requesting firmware '%s'\n", fname);
866 err = request_firmware(&fw, fname, idtfc3->dev);
868 if (err) {
869 dev_err(idtfc3->dev,
870 "requesting firmware failed with err %d!\n", err);
871 return err;
874 dev_dbg(idtfc3->dev, "firmware size %zu bytes\n", fw->size);
876 rec = (struct idtfc3_fwrc *)fw->data;
878 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
879 if (rec->reserved) {
880 dev_err(idtfc3->dev,
881 "bad firmware, reserved field non-zero\n");
882 err = -EINVAL;
883 } else {
884 val = rec->value;
885 addr = rec->hiaddr << 8 | rec->loaddr;
887 rec++;
889 err = idtfc3_set_hw_param(&idtfc3->hw_param, addr, val);
892 if (err != -EINVAL) {
893 err = 0;
895 /* Max register */
896 if (addr >= 0xE88)
897 continue;
899 err = regmap_bulk_write(idtfc3->regmap, addr,
900 &val, sizeof(val));
903 if (err)
904 goto out;
907 err = idtfc3_configure_hw(idtfc3);
908 out:
909 release_firmware(fw);
910 return err;
913 static int idtfc3_read_device_id(struct idtfc3 *idtfc3, u16 *device_id)
915 int err;
916 u8 buf[2] = {0};
918 err = regmap_bulk_read(idtfc3->regmap, DEVICE_ID,
919 &buf, sizeof(buf));
920 if (err) {
921 dev_err(idtfc3->dev, "%s failed with %d", __func__, err);
922 return err;
925 *device_id = get_unaligned_le16(buf);
927 return 0;
930 static int idtfc3_check_device_compatibility(struct idtfc3 *idtfc3)
932 int err;
933 u16 device_id;
935 err = idtfc3_read_device_id(idtfc3, &device_id);
936 if (err)
937 return err;
939 if ((device_id & DEVICE_ID_MASK) == 0) {
940 dev_err(idtfc3->dev, "invalid device");
941 return -EINVAL;
944 return 0;
947 static int idtfc3_probe(struct platform_device *pdev)
949 struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
950 struct idtfc3 *idtfc3;
951 int err;
953 idtfc3 = devm_kzalloc(&pdev->dev, sizeof(struct idtfc3), GFP_KERNEL);
955 if (!idtfc3)
956 return -ENOMEM;
958 idtfc3->dev = &pdev->dev;
959 idtfc3->mfd = pdev->dev.parent;
960 idtfc3->lock = &ddata->lock;
961 idtfc3->regmap = ddata->regmap;
963 mutex_lock(idtfc3->lock);
965 err = idtfc3_check_device_compatibility(idtfc3);
966 if (err) {
967 mutex_unlock(idtfc3->lock);
968 return err;
971 err = idtfc3_load_firmware(idtfc3);
972 if (err) {
973 if (err == -ENOENT) {
974 mutex_unlock(idtfc3->lock);
975 return -EPROBE_DEFER;
977 dev_warn(idtfc3->dev, "loading firmware failed with %d", err);
980 err = idtfc3_enable_ptp(idtfc3);
981 if (err) {
982 dev_err(idtfc3->dev, "idtfc3_enable_ptp failed with %d", err);
983 mutex_unlock(idtfc3->lock);
984 return err;
987 mutex_unlock(idtfc3->lock);
989 platform_set_drvdata(pdev, idtfc3);
991 return 0;
994 static void idtfc3_remove(struct platform_device *pdev)
996 struct idtfc3 *idtfc3 = platform_get_drvdata(pdev);
998 ptp_clock_unregister(idtfc3->ptp_clock);
1001 static struct platform_driver idtfc3_driver = {
1002 .driver = {
1003 .name = "rc38xxx-phc",
1005 .probe = idtfc3_probe,
1006 .remove = idtfc3_remove,
1009 module_platform_driver(idtfc3_driver);