1 // SPDX-License-Identifier: GPL-2.0+
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.
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>
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"
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
)
43 if (likely(nsec
>= 0)) {
44 sync
= div_u64_rem(nsec
, idtfc3
->ns_per_sync
, &rem
);
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
)
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
);
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
)
79 if (mode
>= LPF_INVALID
)
82 if (idtfc3
->lpf_mode
== mode
)
85 err
= regmap_bulk_write(idtfc3
->regmap
, LPF_MODE_CNFG
, &mode
, sizeof(mode
));
89 idtfc3
->lpf_mode
= mode
;
94 static int idtfc3_enable_lpf(struct idtfc3
*idtfc3
, bool enable
)
99 err
= regmap_bulk_read(idtfc3
->regmap
, LPF_CTRL
, &val
, sizeof(val
));
108 return regmap_bulk_write(idtfc3
->regmap
, LPF_CTRL
, &val
, sizeof(val
));
111 static int idtfc3_get_time_ref_freq(struct idtfc3
*idtfc3
)
118 err
= regmap_bulk_read(idtfc3
->regmap
, TIME_CLOCK_MEAS_DIV_CNFG
, buf
, sizeof(buf
));
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);
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
;
133 static int idtfc3_get_tdc_offset_sign(struct idtfc3
*idtfc3
)
140 err
= regmap_bulk_read(idtfc3
->regmap
, TIME_CLOCK_TDC_FANOUT_CNFG
, buf
, sizeof(buf
));
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 !!!");
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
);
156 } else if (sig1
== TIME_SYNC
) {
157 idtfc3
->tdc_offset_sign
= 1;
158 } else if (sig2
== TIME_SYNC
) {
159 idtfc3
->tdc_offset_sign
= -1;
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
)
177 /* Disable TDC first */
178 err
= regmap_bulk_write(idtfc3
->regmap
, TIME_CLOCK_MEAS_CTRL
, &val
, sizeof(val
));
183 return idtfc3_lpf_bw(idtfc3
, LPF_BW_SHIFT_DEFAULT
, LPF_BW_MULT_DEFAULT
);
185 if (meas_mode
>= MEAS_MODE_INVALID
)
188 /* Change TDC meas mode */
189 err
= regmap_bulk_write(idtfc3
->regmap
, TIME_CLOCK_MEAS_CNFG
,
190 &meas_mode
, sizeof(meas_mode
));
196 if (meas_mode
== CONTINUOUS
)
197 val
|= TDC_MEAS_START
;
198 err
= regmap_bulk_write(idtfc3
->regmap
, TIME_CLOCK_MEAS_CTRL
, &val
, sizeof(val
));
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
)
213 err
= regmap_bulk_read(idtfc3
->regmap
, TDC_FIFO_STS
,
218 if (val
& FIFO_EMPTY
)
221 err
= regmap_bulk_read(idtfc3
->regmap
, TDC_FIFO_READ_REQ
,
230 *offset_ns
= tdc_meas2offset(idtfc3
, get_unaligned_le64(&buf
[1]));
235 static int check_tdc_fifo_overrun(struct idtfc3
*idtfc3
)
240 /* Check if FIFO is overrun */
241 err
= regmap_bulk_read(idtfc3
->regmap
, TDC_FIFO_STS
, &val
, sizeof(val
));
245 if (!(val
& FIFO_FULL
))
248 dev_warn(idtfc3
->dev
, "TDC FIFO overrun !!!");
250 err
= idtfc3_enable_tdc(idtfc3
, true, CONTINUOUS
);
257 static int get_tdc_meas_continuous(struct idtfc3
*idtfc3
)
261 struct ptp_clock_event event
;
263 err
= check_tdc_fifo_overrun(idtfc3
);
267 if (get_tdc_meas(idtfc3
, &offset_ns
) && offset_ns
>= 0) {
269 event
.offset
= tdc_offset2phase(idtfc3
, offset_ns
);
270 event
.type
= PTP_CLOCK_EXTOFF
;
271 ptp_clock_event(idtfc3
->ptp_clock
, &event
);
277 static int idtfc3_read_subcounter(struct idtfc3
*idtfc3
)
282 err
= regmap_bulk_read(idtfc3
->regmap
, TOD_COUNTER_READ_REQ
,
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
)
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);
300 dev_err(idtfc3
->dev
, "TOD counter write timeout !!!");
305 static int idtfc3_write_subcounter(struct idtfc3
*idtfc3
, u32 counter
)
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
,
321 return idtfc3_tod_update_is_done(idtfc3
);
324 static int idtfc3_timecounter_update(struct idtfc3
*idtfc3
, u32 counter
, s64 ns
)
328 err
= idtfc3_write_subcounter(idtfc3
, counter
);
332 /* Update time counter */
334 idtfc3
->last_counter
= counter
;
339 static int idtfc3_timecounter_read(struct idtfc3
*idtfc3
)
343 now
= idtfc3_read_subcounter(idtfc3
);
347 /* calculate the delta since the last idtfc3_timecounter_read(): */
348 if (now
>= idtfc3
->last_counter
)
349 delta
= now
- idtfc3
->last_counter
;
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
;
360 static int _idtfc3_gettime(struct idtfc3
*idtfc3
, struct timespec64
*ts
)
364 err
= idtfc3_timecounter_read(idtfc3
);
368 *ts
= ns_to_timespec64(idtfc3
->ns
);
373 static int idtfc3_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
375 struct idtfc3
*idtfc3
= container_of(ptp
, struct idtfc3
, caps
);
378 mutex_lock(idtfc3
->lock
);
379 err
= _idtfc3_gettime(idtfc3
, ts
);
380 mutex_unlock(idtfc3
->lock
);
385 static int _idtfc3_settime(struct idtfc3
*idtfc3
, const struct timespec64
*ts
)
387 s64 offset_ns
, now_ns
;
391 if (timespec64_valid(ts
) == false) {
392 dev_err(idtfc3
->dev
, "%s: invalid timespec", __func__
);
396 now
= idtfc3_read_subcounter(idtfc3
);
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
);
413 mutex_lock(idtfc3
->lock
);
414 err
= _idtfc3_settime(idtfc3
, ts
);
415 mutex_unlock(idtfc3
->lock
);
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
430 if (idtfc3
->ns
+ delta
< 0) {
431 dev_err(idtfc3
->dev
, "%lld ns adj is too large", delta
);
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
);
447 mutex_lock(idtfc3
->lock
);
448 err
= _idtfc3_adjtime(idtfc3
, delta
);
449 mutex_unlock(idtfc3
->lock
);
454 static int _idtfc3_adjphase(struct idtfc3
*idtfc3
, s32 delta
)
460 err
= idtfc3_set_lpf_mode(idtfc3
, LPF_WP
);
465 * Phase Control Word unit is: 10^9 / (TDC_APLL_FREQ * 124)
467 * delta * TDC_APLL_FREQ * 124
468 * PCW = ---------------------------
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
);
484 mutex_lock(idtfc3
->lock
);
485 err
= _idtfc3_adjphase(idtfc3
, delta
);
486 mutex_unlock(idtfc3
->lock
);
491 static int _idtfc3_adjfine(struct idtfc3
*idtfc3
, long scaled_ppm
)
497 err
= idtfc3_set_lpf_mode(idtfc3
, LPF_WF
);
502 * Frequency Control Word unit is: 2^-44 * 10^6 ppm
511 * FCW = -------------
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
);
527 mutex_lock(idtfc3
->lock
);
528 err
= _idtfc3_adjfine(idtfc3
, scaled_ppm
);
529 mutex_unlock(idtfc3
->lock
);
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
);
542 case PTP_CLK_REQ_PEROUT
:
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
)
552 case PTP_CLK_REQ_EXTTS
:
554 /* Only accept requests for external phase offset */
555 if ((rq
->extts
.flags
& PTP_EXT_OFFSET
) != (PTP_EXT_OFFSET
))
558 err
= idtfc3_enable_tdc(idtfc3
, true, CONTINUOUS
);
560 err
= idtfc3_enable_tdc(idtfc3
, false, MEAS_MODE_INVALID
);
566 mutex_unlock(idtfc3
->lock
);
569 dev_err(idtfc3
->dev
, "Failed in %s with err %d!", __func__
, err
);
574 static long idtfc3_aux_work(struct ptp_clock_info
*ptp
)
576 struct idtfc3
*idtfc3
= container_of(ptp
, struct idtfc3
, caps
);
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
);
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
,
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
)
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
617 err
= regmap_bulk_write(idtfc3
->regmap
, TDC_CTRL
,
621 val
= TDC_EN
| TDC_DAC_RECAL_REQ
;
622 err
= regmap_bulk_write(idtfc3
->regmap
, TDC_CTRL
,
629 * Toggle APLL_REINIT:
630 * (1) set apll_reinit to 0
631 * (2) set apll_reinit to 1
634 err
= regmap_bulk_write(idtfc3
->regmap
, SOFT_RESET_CTRL
,
639 err
= regmap_bulk_write(idtfc3
->regmap
, SOFT_RESET_CTRL
,
648 static int idtfc3_init_timecounter(struct idtfc3
*idtfc3
)
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);
663 err
= idtfc3_timecounter_read(idtfc3
);
667 ptp_schedule_worker(idtfc3
->ptp_clock
, idtfc3
->tc_update_period
);
672 static int idtfc3_get_tdc_apll_freq(struct idtfc3
*idtfc3
)
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
));
684 err
= regmap_bulk_read(idtfc3
->regmap
, TDC_FB_DIV_INT_CNFG
,
685 &tdc_fb_div_int
, sizeof(tdc_fb_div_int
));
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
,
698 static int idtfc3_get_fod(struct idtfc3
*idtfc3
)
703 err
= regmap_bulk_read(idtfc3
->regmap
, TIME_CLOCK_SRC
, &fod
, sizeof(fod
));
709 idtfc3
->fod_n
= FOD_0
;
712 idtfc3
->fod_n
= FOD_1
;
715 idtfc3
->fod_n
= FOD_2
;
724 static int idtfc3_get_sync_count(struct idtfc3
*idtfc3
)
729 err
= regmap_bulk_read(idtfc3
->regmap
, SUB_SYNC_GEN_CNFG
, buf
, sizeof(buf
));
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
;
740 static int idtfc3_setup_hw_param(struct idtfc3
*idtfc3
)
744 err
= idtfc3_get_fod(idtfc3
);
748 err
= idtfc3_get_sync_count(idtfc3
);
752 err
= idtfc3_get_time_ref_freq(idtfc3
);
756 return idtfc3_get_tdc_apll_freq(idtfc3
);
759 static int idtfc3_configure_hw(struct idtfc3
*idtfc3
)
763 err
= idtfc3_hw_calibrate(idtfc3
);
767 err
= idtfc3_enable_lpf(idtfc3
, true);
771 err
= idtfc3_enable_tdc(idtfc3
, false, MEAS_MODE_INVALID
);
775 err
= idtfc3_get_tdc_offset_sign(idtfc3
);
779 return idtfc3_setup_hw_param(idtfc3
);
782 static int idtfc3_set_overhead(struct idtfc3
*idtfc3
)
794 for (i
= 0; i
< 5; i
++) {
795 start
= ktime_get_raw();
797 err
= regmap_bulk_write(idtfc3
->regmap
, TOD_SYNC_LOAD_VAL_CTRL
,
802 stop
= ktime_get_raw();
804 diff
= ktime_sub(stop
, start
);
806 current_ns
= ktime_to_ns(diff
);
809 lowest_ns
= current_ns
;
811 if (current_ns
< lowest_ns
)
812 lowest_ns
= current_ns
;
816 idtfc3
->tod_write_overhead
= lowest_ns
;
821 static int idtfc3_enable_ptp(struct idtfc3
*idtfc3
)
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
;
835 err
= idtfc3_set_overhead(idtfc3
);
839 err
= idtfc3_init_timecounter(idtfc3
);
843 dev_info(idtfc3
->dev
, "TIME_SYNC_CHANNEL registered as ptp%d",
844 idtfc3
->ptp_clock
->index
);
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
;
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
);
870 "requesting firmware failed with err %d!\n", 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
)) {
881 "bad firmware, reserved field non-zero\n");
885 addr
= rec
->hiaddr
<< 8 | rec
->loaddr
;
889 err
= idtfc3_set_hw_param(&idtfc3
->hw_param
, addr
, val
);
892 if (err
!= -EINVAL
) {
899 err
= regmap_bulk_write(idtfc3
->regmap
, addr
,
907 err
= idtfc3_configure_hw(idtfc3
);
909 release_firmware(fw
);
913 static int idtfc3_read_device_id(struct idtfc3
*idtfc3
, u16
*device_id
)
918 err
= regmap_bulk_read(idtfc3
->regmap
, DEVICE_ID
,
921 dev_err(idtfc3
->dev
, "%s failed with %d", __func__
, err
);
925 *device_id
= get_unaligned_le16(buf
);
930 static int idtfc3_check_device_compatibility(struct idtfc3
*idtfc3
)
935 err
= idtfc3_read_device_id(idtfc3
, &device_id
);
939 if ((device_id
& DEVICE_ID_MASK
) == 0) {
940 dev_err(idtfc3
->dev
, "invalid device");
947 static int idtfc3_probe(struct platform_device
*pdev
)
949 struct rsmu_ddata
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
950 struct idtfc3
*idtfc3
;
953 idtfc3
= devm_kzalloc(&pdev
->dev
, sizeof(struct idtfc3
), GFP_KERNEL
);
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
);
967 mutex_unlock(idtfc3
->lock
);
971 err
= idtfc3_load_firmware(idtfc3
);
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
);
982 dev_err(idtfc3
->dev
, "idtfc3_enable_ptp failed with %d", err
);
983 mutex_unlock(idtfc3
->lock
);
987 mutex_unlock(idtfc3
->lock
);
989 platform_set_drvdata(pdev
, idtfc3
);
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
= {
1003 .name
= "rc38xxx-phc",
1005 .probe
= idtfc3_probe
,
1006 .remove
= idtfc3_remove
,
1009 module_platform_driver(idtfc3_driver
);