2929#include <linux/of_platform.h>
3030#include <linux/timex.h>
3131#include <linux/slab.h>
32+ #include <linux/clk.h>
3233
3334#include <linux/fsl/ptp_qoriq.h>
3435
@@ -317,6 +318,105 @@ static const struct ptp_clock_info ptp_qoriq_caps = {
317318 .enable = ptp_qoriq_enable ,
318319};
319320
321+ /**
322+ * qoriq_ptp_nominal_freq - calculate nominal frequency according to
323+ * reference clock frequency
324+ *
325+ * @clk_src: reference clock frequency
326+ *
327+ * The nominal frequency is the desired clock frequency.
328+ * It should be less than the reference clock frequency.
329+ * It should be a factor of 1000MHz.
330+ *
331+ * Return the nominal frequency
332+ */
333+ static u32 qoriq_ptp_nominal_freq (u32 clk_src )
334+ {
335+ u32 remainder = 0 ;
336+
337+ clk_src /= 1000000 ;
338+ remainder = clk_src % 100 ;
339+ if (remainder ) {
340+ clk_src -= remainder ;
341+ clk_src += 100 ;
342+ }
343+
344+ do {
345+ clk_src -= 100 ;
346+
347+ } while (1000 % clk_src );
348+
349+ return clk_src * 1000000 ;
350+ }
351+
352+ /**
353+ * qoriq_ptp_auto_config - calculate a set of default configurations
354+ *
355+ * @qoriq_ptp: pointer to qoriq_ptp
356+ * @node: pointer to device_node
357+ *
358+ * If below dts properties are not provided, this function will be
359+ * called to calculate a set of default configurations for them.
360+ * "fsl,tclk-period"
361+ * "fsl,tmr-prsc"
362+ * "fsl,tmr-add"
363+ * "fsl,tmr-fiper1"
364+ * "fsl,tmr-fiper2"
365+ * "fsl,max-adj"
366+ *
367+ * Return 0 if success
368+ */
369+ static int qoriq_ptp_auto_config (struct qoriq_ptp * qoriq_ptp ,
370+ struct device_node * node )
371+ {
372+ struct clk * clk ;
373+ u64 freq_comp ;
374+ u64 max_adj ;
375+ u32 nominal_freq ;
376+ u32 clk_src = 0 ;
377+
378+ qoriq_ptp -> cksel = DEFAULT_CKSEL ;
379+
380+ clk = of_clk_get (node , 0 );
381+ if (!IS_ERR (clk )) {
382+ clk_src = clk_get_rate (clk );
383+ clk_put (clk );
384+ }
385+
386+ if (clk_src <= 100000000UL ) {
387+ pr_err ("error reference clock value, or lower than 100MHz\n" );
388+ return - EINVAL ;
389+ }
390+
391+ nominal_freq = qoriq_ptp_nominal_freq (clk_src );
392+ if (!nominal_freq )
393+ return - EINVAL ;
394+
395+ qoriq_ptp -> tclk_period = 1000000000UL / nominal_freq ;
396+ qoriq_ptp -> tmr_prsc = DEFAULT_TMR_PRSC ;
397+
398+ /* Calculate initial frequency compensation value for TMR_ADD register.
399+ * freq_comp = ceil(2^32 / freq_ratio)
400+ * freq_ratio = reference_clock_freq / nominal_freq
401+ */
402+ freq_comp = ((u64 )1 << 32 ) * nominal_freq ;
403+ if (do_div (freq_comp , clk_src ))
404+ freq_comp ++ ;
405+
406+ qoriq_ptp -> tmr_add = freq_comp ;
407+ qoriq_ptp -> tmr_fiper1 = DEFAULT_FIPER1_PERIOD - qoriq_ptp -> tclk_period ;
408+ qoriq_ptp -> tmr_fiper2 = DEFAULT_FIPER2_PERIOD - qoriq_ptp -> tclk_period ;
409+
410+ /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
411+ * freq_ratio = reference_clock_freq / nominal_freq
412+ */
413+ max_adj = 1000000000ULL * (clk_src - nominal_freq );
414+ max_adj = max_adj / nominal_freq - 1 ;
415+ qoriq_ptp -> caps .max_adj = max_adj ;
416+
417+ return 0 ;
418+ }
419+
320420static int qoriq_ptp_probe (struct platform_device * dev )
321421{
322422 struct device_node * node = dev -> dev .of_node ;
@@ -332,7 +432,7 @@ static int qoriq_ptp_probe(struct platform_device *dev)
332432 if (!qoriq_ptp )
333433 goto no_memory ;
334434
335- err = - ENODEV ;
435+ err = - EINVAL ;
336436
337437 qoriq_ptp -> caps = ptp_qoriq_caps ;
338438
@@ -351,10 +451,14 @@ static int qoriq_ptp_probe(struct platform_device *dev)
351451 "fsl,tmr-fiper2" , & qoriq_ptp -> tmr_fiper2 ) ||
352452 of_property_read_u32 (node ,
353453 "fsl,max-adj" , & qoriq_ptp -> caps .max_adj )) {
354- pr_err ("device tree node missing required elements\n" );
355- goto no_node ;
454+ pr_warn ("device tree node missing required elements, try automatic configuration\n" );
455+
456+ if (qoriq_ptp_auto_config (qoriq_ptp , node ))
457+ goto no_config ;
356458 }
357459
460+ err = - ENODEV ;
461+
358462 qoriq_ptp -> irq = platform_get_irq (dev , 0 );
359463
360464 if (qoriq_ptp -> irq < 0 ) {
@@ -436,6 +540,7 @@ static int qoriq_ptp_probe(struct platform_device *dev)
436540 release_resource (qoriq_ptp -> rsrc );
437541no_resource :
438542 free_irq (qoriq_ptp -> irq , qoriq_ptp );
543+ no_config :
439544no_node :
440545 kfree (qoriq_ptp );
441546no_memory :
0 commit comments