Lines Matching +full:am65 +full:- +full:cpts

1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
9 #include <linux/clk-provider.h>
23 #include "am65-cpts.h"
186 #define am65_cpts_write32(c, v, r) writel(v, &(c)->reg->r)
187 #define am65_cpts_read32(c, r) readl(&(c)->reg->r)
189 static void am65_cpts_settime(struct am65_cpts *cpts, u64 start_tstamp) in am65_cpts_settime() argument
194 am65_cpts_write32(cpts, val, ts_load_val_hi); in am65_cpts_settime()
196 am65_cpts_write32(cpts, val, ts_load_val_lo); in am65_cpts_settime()
198 am65_cpts_write32(cpts, AM65_CPTS_TS_LOAD_EN, ts_load_en); in am65_cpts_settime()
201 static void am65_cpts_set_add_val(struct am65_cpts *cpts) in am65_cpts_set_add_val() argument
204 cpts->ts_add_val = (NSEC_PER_SEC / cpts->refclk_freq - 1) & 0x7; in am65_cpts_set_add_val()
206 am65_cpts_write32(cpts, cpts->ts_add_val, ts_add_val); in am65_cpts_set_add_val()
209 static void am65_cpts_disable(struct am65_cpts *cpts) in am65_cpts_disable() argument
211 am65_cpts_write32(cpts, 0, control); in am65_cpts_disable()
212 am65_cpts_write32(cpts, 0, int_enable); in am65_cpts_disable()
217 return (event->event1 & AM65_CPTS_EVENT_1_PORT_NUMBER_MASK) >> in am65_cpts_event_get_port()
223 return (event->event1 & AM65_CPTS_EVENT_1_EVENT_TYPE_MASK) >> in am65_cpts_event_get_type()
227 static int am65_cpts_cpts_purge_events(struct am65_cpts *cpts) in am65_cpts_cpts_purge_events() argument
233 list_for_each_safe(this, next, &cpts->events) { in am65_cpts_cpts_purge_events()
235 if (time_after(jiffies, event->tmo)) { in am65_cpts_cpts_purge_events()
236 list_del_init(&event->list); in am65_cpts_cpts_purge_events()
237 list_add(&event->list, &cpts->pool); in am65_cpts_cpts_purge_events()
243 dev_dbg(cpts->dev, "event pool cleaned up %d\n", removed); in am65_cpts_cpts_purge_events()
244 return removed ? 0 : -1; in am65_cpts_cpts_purge_events()
247 static bool am65_cpts_fifo_pop_event(struct am65_cpts *cpts, in am65_cpts_fifo_pop_event() argument
250 u32 r = am65_cpts_read32(cpts, intstat_raw); in am65_cpts_fifo_pop_event()
253 event->timestamp = am65_cpts_read32(cpts, event_0); in am65_cpts_fifo_pop_event()
254 event->event1 = am65_cpts_read32(cpts, event_1); in am65_cpts_fifo_pop_event()
255 event->event2 = am65_cpts_read32(cpts, event_2); in am65_cpts_fifo_pop_event()
256 event->timestamp |= (u64)am65_cpts_read32(cpts, event_3) << 32; in am65_cpts_fifo_pop_event()
257 am65_cpts_write32(cpts, AM65_CPTS_EVENT_POP, event_pop); in am65_cpts_fifo_pop_event()
263 static int am65_cpts_fifo_read(struct am65_cpts *cpts) in am65_cpts_fifo_read() argument
271 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_fifo_read()
273 event = list_first_entry_or_null(&cpts->pool, in am65_cpts_fifo_read()
277 if (am65_cpts_cpts_purge_events(cpts)) { in am65_cpts_fifo_read()
278 dev_err(cpts->dev, "cpts: event pool empty\n"); in am65_cpts_fifo_read()
279 ret = -1; in am65_cpts_fifo_read()
285 if (am65_cpts_fifo_pop_event(cpts, event)) in am65_cpts_fifo_read()
291 cpts->timestamp = event->timestamp; in am65_cpts_fifo_read()
292 dev_dbg(cpts->dev, "AM65_CPTS_EV_PUSH t:%llu\n", in am65_cpts_fifo_read()
293 cpts->timestamp); in am65_cpts_fifo_read()
297 event->tmo = jiffies + in am65_cpts_fifo_read()
300 list_del_init(&event->list); in am65_cpts_fifo_read()
301 list_add_tail(&event->list, &cpts->events); in am65_cpts_fifo_read()
303 dev_dbg(cpts->dev, in am65_cpts_fifo_read()
305 event->event1, event->event2, in am65_cpts_fifo_read()
306 event->timestamp); in am65_cpts_fifo_read()
310 pevent.index = am65_cpts_event_get_port(event) - 1; in am65_cpts_fifo_read()
311 pevent.timestamp = event->timestamp; in am65_cpts_fifo_read()
313 dev_dbg(cpts->dev, "AM65_CPTS_EV_HW p:%d t:%llu\n", in am65_cpts_fifo_read()
314 pevent.index, event->timestamp); in am65_cpts_fifo_read()
316 ptp_clock_event(cpts->ptp_clock, &pevent); in am65_cpts_fifo_read()
323 dev_dbg(cpts->dev, in am65_cpts_fifo_read()
326 event->event1, event->event2, in am65_cpts_fifo_read()
327 event->timestamp); in am65_cpts_fifo_read()
330 dev_err(cpts->dev, "cpts: unknown event type\n"); in am65_cpts_fifo_read()
331 ret = -1; in am65_cpts_fifo_read()
337 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_fifo_read()
340 ptp_schedule_worker(cpts->ptp_clock, 0); in am65_cpts_fifo_read()
345 static u64 am65_cpts_gettime(struct am65_cpts *cpts, in am65_cpts_gettime() argument
351 /* temporarily disable cpts interrupt to avoid intentional in am65_cpts_gettime()
352 * doubled read. Interrupt can be in-flight - it's Ok. in am65_cpts_gettime()
354 am65_cpts_write32(cpts, 0, int_enable); in am65_cpts_gettime()
357 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_gettime()
359 am65_cpts_write32(cpts, AM65_CPTS_TS_PUSH, ts_push); in am65_cpts_gettime()
360 am65_cpts_read32(cpts, ts_push); in am65_cpts_gettime()
362 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_gettime()
364 am65_cpts_fifo_read(cpts); in am65_cpts_gettime()
366 am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); in am65_cpts_gettime()
368 val = cpts->timestamp; in am65_cpts_gettime()
375 struct am65_cpts *cpts = dev_id; in am65_cpts_interrupt() local
377 if (am65_cpts_fifo_read(cpts)) in am65_cpts_interrupt()
378 dev_dbg(cpts->dev, "cpts: unable to obtain a time stamp\n"); in am65_cpts_interrupt()
386 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ptp_adjfreq() local
393 ppb = -ppb; in am65_cpts_ptp_adjfreq()
405 adj_period = div_u64(cpts->refclk_freq, ppb); in am65_cpts_ptp_adjfreq()
407 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjfreq()
409 val = am65_cpts_read32(cpts, control); in am65_cpts_ptp_adjfreq()
414 am65_cpts_write32(cpts, val, control); in am65_cpts_ptp_adjfreq()
417 am65_cpts_write32(cpts, val, ts_ppm_hi); in am65_cpts_ptp_adjfreq()
419 am65_cpts_write32(cpts, val, ts_ppm_low); in am65_cpts_ptp_adjfreq()
421 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjfreq()
428 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ptp_adjtime() local
431 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjtime()
432 ns = am65_cpts_gettime(cpts, NULL); in am65_cpts_ptp_adjtime()
434 am65_cpts_settime(cpts, ns); in am65_cpts_ptp_adjtime()
435 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjtime()
444 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ptp_gettimex() local
447 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_gettimex()
448 ns = am65_cpts_gettime(cpts, sts); in am65_cpts_ptp_gettimex()
449 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_gettimex()
455 u64 am65_cpts_ns_gettime(struct am65_cpts *cpts) in am65_cpts_ns_gettime() argument
460 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ns_gettime()
461 ns = am65_cpts_gettime(cpts, NULL); in am65_cpts_ns_gettime()
462 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ns_gettime()
471 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ptp_settime() local
475 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_settime()
476 am65_cpts_settime(cpts, ns); in am65_cpts_ptp_settime()
477 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_settime()
482 static void am65_cpts_extts_enable_hw(struct am65_cpts *cpts, u32 index, int on) in am65_cpts_extts_enable_hw() argument
486 v = am65_cpts_read32(cpts, control); in am65_cpts_extts_enable_hw()
489 cpts->hw_ts_enable |= BIT(index); in am65_cpts_extts_enable_hw()
492 cpts->hw_ts_enable &= ~BIT(index); in am65_cpts_extts_enable_hw()
494 am65_cpts_write32(cpts, v, control); in am65_cpts_extts_enable_hw()
497 static int am65_cpts_extts_enable(struct am65_cpts *cpts, u32 index, int on) in am65_cpts_extts_enable() argument
499 if (!!(cpts->hw_ts_enable & BIT(index)) == !!on) in am65_cpts_extts_enable()
502 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_extts_enable()
503 am65_cpts_extts_enable_hw(cpts, index, on); in am65_cpts_extts_enable()
504 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_extts_enable()
506 dev_dbg(cpts->dev, "%s: ExtTS:%u %s\n", in am65_cpts_extts_enable()
512 int am65_cpts_estf_enable(struct am65_cpts *cpts, int idx, in am65_cpts_estf_enable() argument
518 cycles = cfg->ns_period * cpts->refclk_freq; in am65_cpts_estf_enable()
521 return -EINVAL; in am65_cpts_estf_enable()
524 am65_cpts_write32(cpts, 0, estf[idx].length); in am65_cpts_estf_enable()
526 val = upper_32_bits(cfg->ns_start); in am65_cpts_estf_enable()
527 am65_cpts_write32(cpts, val, estf[idx].comp_hi); in am65_cpts_estf_enable()
528 val = lower_32_bits(cfg->ns_start); in am65_cpts_estf_enable()
529 am65_cpts_write32(cpts, val, estf[idx].comp_lo); in am65_cpts_estf_enable()
531 am65_cpts_write32(cpts, val, estf[idx].length); in am65_cpts_estf_enable()
533 dev_dbg(cpts->dev, "%s: ESTF:%u enabled\n", __func__, idx); in am65_cpts_estf_enable()
539 void am65_cpts_estf_disable(struct am65_cpts *cpts, int idx) in am65_cpts_estf_disable() argument
541 am65_cpts_write32(cpts, 0, estf[idx].length); in am65_cpts_estf_disable()
543 dev_dbg(cpts->dev, "%s: ESTF:%u disabled\n", __func__, idx); in am65_cpts_estf_disable()
547 static void am65_cpts_perout_enable_hw(struct am65_cpts *cpts, in am65_cpts_perout_enable_hw() argument
555 ts.tv_sec = req->period.sec; in am65_cpts_perout_enable_hw()
556 ts.tv_nsec = req->period.nsec; in am65_cpts_perout_enable_hw()
559 cycles = (ns_period * cpts->refclk_freq) / NSEC_PER_SEC; in am65_cpts_perout_enable_hw()
561 ts.tv_sec = req->start.sec; in am65_cpts_perout_enable_hw()
562 ts.tv_nsec = req->start.nsec; in am65_cpts_perout_enable_hw()
566 am65_cpts_write32(cpts, val, genf[req->index].comp_hi); in am65_cpts_perout_enable_hw()
568 am65_cpts_write32(cpts, val, genf[req->index].comp_lo); in am65_cpts_perout_enable_hw()
570 am65_cpts_write32(cpts, val, genf[req->index].length); in am65_cpts_perout_enable_hw()
572 cpts->genf_enable |= BIT(req->index); in am65_cpts_perout_enable_hw()
574 am65_cpts_write32(cpts, 0, genf[req->index].length); in am65_cpts_perout_enable_hw()
576 cpts->genf_enable &= ~BIT(req->index); in am65_cpts_perout_enable_hw()
580 static int am65_cpts_perout_enable(struct am65_cpts *cpts, in am65_cpts_perout_enable() argument
583 if (!!(cpts->genf_enable & BIT(req->index)) == !!on) in am65_cpts_perout_enable()
586 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_perout_enable()
587 am65_cpts_perout_enable_hw(cpts, req, on); in am65_cpts_perout_enable()
588 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_perout_enable()
590 dev_dbg(cpts->dev, "%s: GenF:%u %s\n", in am65_cpts_perout_enable()
591 __func__, req->index, on ? "enabled" : "disabled"); in am65_cpts_perout_enable()
599 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ptp_enable() local
601 switch (rq->type) { in am65_cpts_ptp_enable()
603 return am65_cpts_extts_enable(cpts, rq->extts.index, on); in am65_cpts_ptp_enable()
605 return am65_cpts_perout_enable(cpts, &rq->perout, on); in am65_cpts_ptp_enable()
610 return -EOPNOTSUPP; in am65_cpts_ptp_enable()
626 static bool am65_cpts_match_tx_ts(struct am65_cpts *cpts, in am65_cpts_match_tx_ts() argument
635 mtype_seqid = event->event1 & in am65_cpts_match_tx_ts()
642 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
643 skb_queue_splice_init(&cpts->txq, &txq_list); in am65_cpts_match_tx_ts()
644 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
646 /* no need to grab txq.lock as access is always done under cpts->lock */ in am65_cpts_match_tx_ts()
650 (struct am65_cpts_skb_cb_data *)skb->cb; in am65_cpts_match_tx_ts()
652 if (mtype_seqid == skb_cb->skb_mtype_seqid) { in am65_cpts_match_tx_ts()
653 u64 ns = event->timestamp; in am65_cpts_match_tx_ts()
661 dev_dbg(cpts->dev, in am65_cpts_match_tx_ts()
667 if (time_after(jiffies, skb_cb->tmo)) { in am65_cpts_match_tx_ts()
669 dev_dbg(cpts->dev, in am65_cpts_match_tx_ts()
677 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
678 skb_queue_splice(&txq_list, &cpts->txq); in am65_cpts_match_tx_ts()
679 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
684 static void am65_cpts_find_ts(struct am65_cpts *cpts) in am65_cpts_find_ts() argument
692 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_find_ts()
693 list_splice_init(&cpts->events, &events); in am65_cpts_find_ts()
694 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_find_ts()
698 if (am65_cpts_match_tx_ts(cpts, event) || in am65_cpts_find_ts()
699 time_after(jiffies, event->tmo)) { in am65_cpts_find_ts()
700 list_del_init(&event->list); in am65_cpts_find_ts()
701 list_add(&event->list, &events_free); in am65_cpts_find_ts()
705 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_find_ts()
706 list_splice_tail(&events, &cpts->events); in am65_cpts_find_ts()
707 list_splice_tail(&events_free, &cpts->pool); in am65_cpts_find_ts()
708 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_find_ts()
713 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); in am65_cpts_ts_work() local
715 long delay = -1; in am65_cpts_ts_work()
717 am65_cpts_find_ts(cpts); in am65_cpts_ts_work()
719 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_ts_work()
720 if (!skb_queue_empty(&cpts->txq)) in am65_cpts_ts_work()
722 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_ts_work()
728 * am65_cpts_rx_enable - enable rx timestamping
729 * @cpts: cpts handle
732 * This functions enables rx packets timestamping. The CPTS can timestamp all
735 void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en) in am65_cpts_rx_enable() argument
739 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_rx_enable()
740 val = am65_cpts_read32(cpts, control); in am65_cpts_rx_enable()
745 am65_cpts_write32(cpts, val, control); in am65_cpts_rx_enable()
746 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_rx_enable()
765 seqid = ntohs(hdr->sequence_id); in am65_skb_get_mtype_seqid()
775 * am65_cpts_tx_timestamp - save tx packet for timestamping
776 * @cpts: cpts handle
782 void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) in am65_cpts_tx_timestamp() argument
784 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; in am65_cpts_tx_timestamp()
786 if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) in am65_cpts_tx_timestamp()
794 skb_cb->tmo = jiffies + msecs_to_jiffies(100); in am65_cpts_tx_timestamp()
795 skb_queue_tail(&cpts->txq, skb); in am65_cpts_tx_timestamp()
796 ptp_schedule_worker(cpts->ptp_clock, 0); in am65_cpts_tx_timestamp()
801 * am65_cpts_prep_tx_timestamp - check and prepare tx packet for timestamping
802 * @cpts: cpts handle
806 * It checks if packet can be timestamped, fills internal cpts data
807 * in skb-cb and marks packet as SKBTX_IN_PROGRESS.
809 void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) in am65_cpts_prep_tx_timestamp() argument
811 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; in am65_cpts_prep_tx_timestamp()
814 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) in am65_cpts_prep_tx_timestamp()
817 ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in am65_cpts_prep_tx_timestamp()
820 skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_TX << in am65_cpts_prep_tx_timestamp()
823 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; in am65_cpts_prep_tx_timestamp()
827 int am65_cpts_phc_index(struct am65_cpts *cpts) in am65_cpts_phc_index() argument
829 return cpts->phc_index; in am65_cpts_phc_index()
835 struct am65_cpts *cpts = data; in cpts_free_clk_mux() local
837 of_clk_del_provider(cpts->clk_mux_np); in cpts_free_clk_mux()
838 clk_hw_unregister_mux(cpts->clk_mux_hw); in cpts_free_clk_mux()
839 of_node_put(cpts->clk_mux_np); in cpts_free_clk_mux()
842 static int cpts_of_mux_clk_setup(struct am65_cpts *cpts, in cpts_of_mux_clk_setup() argument
849 int ret = -EINVAL; in cpts_of_mux_clk_setup()
851 cpts->clk_mux_np = of_get_child_by_name(node, "refclk-mux"); in cpts_of_mux_clk_setup()
852 if (!cpts->clk_mux_np) in cpts_of_mux_clk_setup()
855 num_parents = of_clk_get_parent_count(cpts->clk_mux_np); in cpts_of_mux_clk_setup()
857 dev_err(cpts->dev, "mux-clock %pOF must have parents\n", in cpts_of_mux_clk_setup()
858 cpts->clk_mux_np); in cpts_of_mux_clk_setup()
862 parent_names = devm_kcalloc(cpts->dev, sizeof(char *), num_parents, in cpts_of_mux_clk_setup()
865 ret = -ENOMEM; in cpts_of_mux_clk_setup()
869 of_clk_parent_fill(cpts->clk_mux_np, parent_names, num_parents); in cpts_of_mux_clk_setup()
871 clk_mux_name = devm_kasprintf(cpts->dev, GFP_KERNEL, "%s.%pOFn", in cpts_of_mux_clk_setup()
872 dev_name(cpts->dev), cpts->clk_mux_np); in cpts_of_mux_clk_setup()
874 ret = -ENOMEM; in cpts_of_mux_clk_setup()
878 reg = &cpts->reg->rftclk_sel; in cpts_of_mux_clk_setup()
882 cpts->clk_mux_hw = clk_hw_register_mux(NULL, clk_mux_name, in cpts_of_mux_clk_setup()
885 if (IS_ERR(cpts->clk_mux_hw)) { in cpts_of_mux_clk_setup()
886 ret = PTR_ERR(cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
890 ret = of_clk_add_hw_provider(cpts->clk_mux_np, of_clk_hw_simple_get, in cpts_of_mux_clk_setup()
891 cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
895 ret = devm_add_action_or_reset(cpts->dev, cpts_free_clk_mux, cpts); in cpts_of_mux_clk_setup()
897 dev_err(cpts->dev, "failed to add clkmux reset action %d", ret); in cpts_of_mux_clk_setup()
902 clk_hw_unregister_mux(cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
904 of_node_put(cpts->clk_mux_np); in cpts_of_mux_clk_setup()
908 static int am65_cpts_of_parse(struct am65_cpts *cpts, struct device_node *node) in am65_cpts_of_parse() argument
912 if (!of_property_read_u32(node, "ti,cpts-ext-ts-inputs", &prop[0])) in am65_cpts_of_parse()
913 cpts->ext_ts_inputs = prop[0]; in am65_cpts_of_parse()
915 if (!of_property_read_u32(node, "ti,cpts-periodic-outputs", &prop[0])) in am65_cpts_of_parse()
916 cpts->genf_num = prop[0]; in am65_cpts_of_parse()
918 return cpts_of_mux_clk_setup(cpts, node); in am65_cpts_of_parse()
923 struct am65_cpts *cpts = data; in am65_cpts_release() local
925 ptp_clock_unregister(cpts->ptp_clock); in am65_cpts_release()
926 am65_cpts_disable(cpts); in am65_cpts_release()
927 clk_disable_unprepare(cpts->refclk); in am65_cpts_release()
933 struct am65_cpts *cpts; in am65_cpts_create() local
936 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); in am65_cpts_create()
937 if (!cpts) in am65_cpts_create()
938 return ERR_PTR(-ENOMEM); in am65_cpts_create()
940 cpts->dev = dev; in am65_cpts_create()
941 cpts->reg = (struct am65_cpts_regs __iomem *)regs; in am65_cpts_create()
943 cpts->irq = of_irq_get_byname(node, "cpts"); in am65_cpts_create()
944 if (cpts->irq <= 0) { in am65_cpts_create()
945 ret = cpts->irq ?: -ENXIO; in am65_cpts_create()
950 ret = am65_cpts_of_parse(cpts, node); in am65_cpts_create()
954 mutex_init(&cpts->ptp_clk_lock); in am65_cpts_create()
955 INIT_LIST_HEAD(&cpts->events); in am65_cpts_create()
956 INIT_LIST_HEAD(&cpts->pool); in am65_cpts_create()
957 spin_lock_init(&cpts->lock); in am65_cpts_create()
958 skb_queue_head_init(&cpts->txq); in am65_cpts_create()
961 list_add(&cpts->pool_data[i].list, &cpts->pool); in am65_cpts_create()
963 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in am65_cpts_create()
964 if (IS_ERR(cpts->refclk)) { in am65_cpts_create()
965 ret = PTR_ERR(cpts->refclk); in am65_cpts_create()
970 ret = clk_prepare_enable(cpts->refclk); in am65_cpts_create()
976 cpts->refclk_freq = clk_get_rate(cpts->refclk); in am65_cpts_create()
978 am65_ptp_info.max_adj = cpts->refclk_freq / AM65_CPTS_MIN_PPM; in am65_cpts_create()
979 cpts->ptp_info = am65_ptp_info; in am65_cpts_create()
981 if (cpts->ext_ts_inputs) in am65_cpts_create()
982 cpts->ptp_info.n_ext_ts = cpts->ext_ts_inputs; in am65_cpts_create()
983 if (cpts->genf_num) in am65_cpts_create()
984 cpts->ptp_info.n_per_out = cpts->genf_num; in am65_cpts_create()
986 am65_cpts_set_add_val(cpts); in am65_cpts_create()
988 am65_cpts_write32(cpts, AM65_CPTS_CONTROL_EN | in am65_cpts_create()
992 am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); in am65_cpts_create()
995 am65_cpts_settime(cpts, ktime_to_ns(ktime_get_real())); in am65_cpts_create()
997 cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev); in am65_cpts_create()
998 if (IS_ERR_OR_NULL(cpts->ptp_clock)) { in am65_cpts_create()
1000 PTR_ERR(cpts->ptp_clock)); in am65_cpts_create()
1001 ret = cpts->ptp_clock ? PTR_ERR(cpts->ptp_clock) : -ENODEV; in am65_cpts_create()
1004 cpts->phc_index = ptp_clock_index(cpts->ptp_clock); in am65_cpts_create()
1006 ret = devm_add_action_or_reset(dev, am65_cpts_release, cpts); in am65_cpts_create()
1012 ret = devm_request_threaded_irq(dev, cpts->irq, NULL, in am65_cpts_create()
1014 IRQF_ONESHOT, dev_name(dev), cpts); in am65_cpts_create()
1016 dev_err(cpts->dev, "error attaching irq %d\n", ret); in am65_cpts_create()
1020 dev_info(dev, "CPTS ver 0x%08x, freq:%u, add_val:%u\n", in am65_cpts_create()
1021 am65_cpts_read32(cpts, idver), in am65_cpts_create()
1022 cpts->refclk_freq, cpts->ts_add_val); in am65_cpts_create()
1024 return cpts; in am65_cpts_create()
1027 clk_disable_unprepare(cpts->refclk); in am65_cpts_create()
1034 struct device_node *node = pdev->dev.of_node; in am65_cpts_probe()
1035 struct device *dev = &pdev->dev; in am65_cpts_probe()
1036 struct am65_cpts *cpts; in am65_cpts_probe() local
1039 base = devm_platform_ioremap_resource_byname(pdev, "cpts"); in am65_cpts_probe()
1043 cpts = am65_cpts_create(dev, base, node); in am65_cpts_probe()
1044 return PTR_ERR_OR_ZERO(cpts); in am65_cpts_probe()
1048 { .compatible = "ti,am65-cpts", },
1049 { .compatible = "ti,j721e-cpts", },
1057 .name = "am65-cpts",
1065 MODULE_DESCRIPTION("TI K3 AM65 CPTS driver");