forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/clk/keystone/sci-clk.c
....@@ -1,7 +1,7 @@
11 /*
22 * SCI Clock driver for keystone based devices
33 *
4
- * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
4
+ * Copyright (C) 2015-2016 Texas Instruments Incorporated - https://www.ti.com/
55 * Tero Kristo <t-kristo@ti.com>
66 *
77 * This program is free software; you can redistribute it and/or modify
....@@ -23,6 +23,7 @@
2323 #include <linux/slab.h>
2424 #include <linux/soc/ti/ti_sci_protocol.h>
2525 #include <linux/bsearch.h>
26
+#include <linux/list_sort.h>
2627
2728 #define SCI_CLK_SSC_ENABLE BIT(0)
2829 #define SCI_CLK_ALLOW_FREQ_CHANGE BIT(1)
....@@ -52,14 +53,20 @@
5253 * @num_parents: Number of parents for this clock
5354 * @provider: Master clock provider
5455 * @flags: Flags for the clock
56
+ * @node: Link for handling clocks probed via DT
57
+ * @cached_req: Cached requested freq for determine rate calls
58
+ * @cached_res: Cached result freq for determine rate calls
5559 */
5660 struct sci_clk {
5761 struct clk_hw hw;
5862 u16 dev_id;
59
- u8 clk_id;
60
- u8 num_parents;
63
+ u32 clk_id;
64
+ u32 num_parents;
6165 struct sci_clk_provider *provider;
6266 u8 flags;
67
+ struct list_head node;
68
+ unsigned long cached_req;
69
+ unsigned long cached_res;
6370 };
6471
6572 #define to_sci_clk(_hw) container_of(_hw, struct sci_clk, hw)
....@@ -172,6 +179,11 @@
172179 int ret;
173180 u64 new_rate;
174181
182
+ if (clk->cached_req && clk->cached_req == req->rate) {
183
+ req->rate = clk->cached_res;
184
+ return 0;
185
+ }
186
+
175187 ret = clk->provider->ops->get_best_match_freq(clk->provider->sci,
176188 clk->dev_id,
177189 clk->clk_id,
....@@ -185,6 +197,9 @@
185197 clk->dev_id, clk->clk_id, ret);
186198 return ret;
187199 }
200
+
201
+ clk->cached_req = req->rate;
202
+ clk->cached_res = new_rate;
188203
189204 req->rate = new_rate;
190205
....@@ -206,7 +221,8 @@
206221 struct sci_clk *clk = to_sci_clk(hw);
207222
208223 return clk->provider->ops->set_freq(clk->provider->sci, clk->dev_id,
209
- clk->clk_id, rate, rate, rate);
224
+ clk->clk_id, rate / 10 * 9, rate,
225
+ rate / 10 * 11);
210226 }
211227
212228 /**
....@@ -218,11 +234,11 @@
218234 static u8 sci_clk_get_parent(struct clk_hw *hw)
219235 {
220236 struct sci_clk *clk = to_sci_clk(hw);
221
- u8 parent_id;
237
+ u32 parent_id = 0;
222238 int ret;
223239
224240 ret = clk->provider->ops->get_parent(clk->provider->sci, clk->dev_id,
225
- clk->clk_id, &parent_id);
241
+ clk->clk_id, (void *)&parent_id);
226242 if (ret) {
227243 dev_err(clk->provider->dev,
228244 "get-parent failed for dev=%d, clk=%d, ret=%d\n",
....@@ -230,7 +246,9 @@
230246 return 0;
231247 }
232248
233
- return parent_id - clk->clk_id - 1;
249
+ parent_id = parent_id - clk->clk_id - 1;
250
+
251
+ return (u8)parent_id;
234252 }
235253
236254 /**
....@@ -243,6 +261,8 @@
243261 static int sci_clk_set_parent(struct clk_hw *hw, u8 index)
244262 {
245263 struct sci_clk *clk = to_sci_clk(hw);
264
+
265
+ clk->cached_req = 0;
246266
247267 return clk->provider->ops->set_parent(clk->provider->sci, clk->dev_id,
248268 clk->clk_id,
....@@ -280,8 +300,8 @@
280300 int i;
281301 int ret = 0;
282302
283
- name = kasprintf(GFP_KERNEL, "%s:%d:%d", dev_name(provider->dev),
284
- sci_clk->dev_id, sci_clk->clk_id);
303
+ name = kasprintf(GFP_KERNEL, "clk:%d:%d", sci_clk->dev_id,
304
+ sci_clk->clk_id);
285305
286306 init.name = name;
287307
....@@ -306,8 +326,7 @@
306326 for (i = 0; i < sci_clk->num_parents; i++) {
307327 char *parent_name;
308328
309
- parent_name = kasprintf(GFP_KERNEL, "%s:%d:%d",
310
- dev_name(provider->dev),
329
+ parent_name = kasprintf(GFP_KERNEL, "clk:%d:%d",
311330 sci_clk->dev_id,
312331 sci_clk->clk_id + 1 + i);
313332 if (!parent_name) {
....@@ -404,22 +423,9 @@
404423 };
405424 MODULE_DEVICE_TABLE(of, ti_sci_clk_of_match);
406425
407
-/**
408
- * ti_sci_clk_probe - Probe function for the TI SCI clock driver
409
- * @pdev: platform device pointer to be probed
410
- *
411
- * Probes the TI SCI clock device. Allocates a new clock provider
412
- * and registers this to the common clock framework. Also applies
413
- * any required flags to the identified clocks via clock lists
414
- * supplied from DT. Returns 0 for success, negative error value
415
- * for failure.
416
- */
417
-static int ti_sci_clk_probe(struct platform_device *pdev)
426
+#ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
427
+static int ti_sci_scan_clocks_from_fw(struct sci_clk_provider *provider)
418428 {
419
- struct device *dev = &pdev->dev;
420
- struct device_node *np = dev->of_node;
421
- struct sci_clk_provider *provider;
422
- const struct ti_sci_handle *handle;
423429 int ret;
424430 int num_clks = 0;
425431 struct sci_clk **clks = NULL;
....@@ -428,24 +434,14 @@
428434 int max_clks = 0;
429435 int clk_id = 0;
430436 int dev_id = 0;
431
- u8 num_parents;
437
+ u32 num_parents = 0;
432438 int gap_size = 0;
433
-
434
- handle = devm_ti_sci_get_handle(dev);
435
- if (IS_ERR(handle))
436
- return PTR_ERR(handle);
437
-
438
- provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
439
- if (!provider)
440
- return -ENOMEM;
441
-
442
- provider->sci = handle;
443
- provider->ops = &handle->ops.clk_ops;
444
- provider->dev = dev;
439
+ struct device *dev = provider->dev;
445440
446441 while (1) {
447442 ret = provider->ops->get_num_parents(provider->sci, dev_id,
448
- clk_id, &num_parents);
443
+ clk_id,
444
+ (void *)&num_parents);
449445 if (ret) {
450446 gap_size++;
451447 if (!clk_id) {
....@@ -502,6 +498,188 @@
502498
503499 devm_kfree(dev, clks);
504500
501
+ return 0;
502
+}
503
+
504
+#else
505
+
506
+static int _cmp_sci_clk_list(void *priv, struct list_head *a,
507
+ struct list_head *b)
508
+{
509
+ struct sci_clk *ca = container_of(a, struct sci_clk, node);
510
+ struct sci_clk *cb = container_of(b, struct sci_clk, node);
511
+
512
+ return _cmp_sci_clk(ca, &cb);
513
+}
514
+
515
+static int ti_sci_scan_clocks_from_dt(struct sci_clk_provider *provider)
516
+{
517
+ struct device *dev = provider->dev;
518
+ struct device_node *np = NULL;
519
+ int ret;
520
+ int index;
521
+ struct of_phandle_args args;
522
+ struct list_head clks;
523
+ struct sci_clk *sci_clk, *prev;
524
+ int num_clks = 0;
525
+ int num_parents;
526
+ int clk_id;
527
+ const char * const clk_names[] = {
528
+ "clocks", "assigned-clocks", "assigned-clock-parents", NULL
529
+ };
530
+ const char * const *clk_name;
531
+
532
+ INIT_LIST_HEAD(&clks);
533
+
534
+ clk_name = clk_names;
535
+
536
+ while (*clk_name) {
537
+ np = of_find_node_with_property(np, *clk_name);
538
+ if (!np) {
539
+ clk_name++;
540
+ continue;
541
+ }
542
+
543
+ if (!of_device_is_available(np))
544
+ continue;
545
+
546
+ index = 0;
547
+
548
+ do {
549
+ ret = of_parse_phandle_with_args(np, *clk_name,
550
+ "#clock-cells", index,
551
+ &args);
552
+ if (ret)
553
+ break;
554
+
555
+ if (args.args_count == 2 && args.np == dev->of_node) {
556
+ sci_clk = devm_kzalloc(dev, sizeof(*sci_clk),
557
+ GFP_KERNEL);
558
+ if (!sci_clk)
559
+ return -ENOMEM;
560
+
561
+ sci_clk->dev_id = args.args[0];
562
+ sci_clk->clk_id = args.args[1];
563
+ sci_clk->provider = provider;
564
+ provider->ops->get_num_parents(provider->sci,
565
+ sci_clk->dev_id,
566
+ sci_clk->clk_id,
567
+ (void *)&sci_clk->num_parents);
568
+ list_add_tail(&sci_clk->node, &clks);
569
+
570
+ num_clks++;
571
+
572
+ num_parents = sci_clk->num_parents;
573
+ if (num_parents == 1)
574
+ num_parents = 0;
575
+
576
+ /*
577
+ * Linux kernel has inherent limitation
578
+ * of 255 clock parents at the moment.
579
+ * Right now, it is not expected that
580
+ * any mux clock from sci-clk driver
581
+ * would exceed that limit either, but
582
+ * the ABI basically provides that
583
+ * possibility. Print out a warning if
584
+ * this happens for any clock.
585
+ */
586
+ if (num_parents >= 255) {
587
+ dev_warn(dev, "too many parents for dev=%d, clk=%d (%d), cropping to 255.\n",
588
+ sci_clk->dev_id,
589
+ sci_clk->clk_id, num_parents);
590
+ num_parents = 255;
591
+ }
592
+
593
+ clk_id = args.args[1] + 1;
594
+
595
+ while (num_parents--) {
596
+ sci_clk = devm_kzalloc(dev,
597
+ sizeof(*sci_clk),
598
+ GFP_KERNEL);
599
+ if (!sci_clk)
600
+ return -ENOMEM;
601
+ sci_clk->dev_id = args.args[0];
602
+ sci_clk->clk_id = clk_id++;
603
+ sci_clk->provider = provider;
604
+ list_add_tail(&sci_clk->node, &clks);
605
+
606
+ num_clks++;
607
+ }
608
+ }
609
+
610
+ index++;
611
+ } while (args.np);
612
+ }
613
+
614
+ list_sort(NULL, &clks, _cmp_sci_clk_list);
615
+
616
+ provider->clocks = devm_kmalloc_array(dev, num_clks, sizeof(sci_clk),
617
+ GFP_KERNEL);
618
+ if (!provider->clocks)
619
+ return -ENOMEM;
620
+
621
+ num_clks = 0;
622
+ prev = NULL;
623
+
624
+ list_for_each_entry(sci_clk, &clks, node) {
625
+ if (prev && prev->dev_id == sci_clk->dev_id &&
626
+ prev->clk_id == sci_clk->clk_id)
627
+ continue;
628
+
629
+ provider->clocks[num_clks++] = sci_clk;
630
+ prev = sci_clk;
631
+ }
632
+
633
+ provider->num_clocks = num_clks;
634
+
635
+ return 0;
636
+}
637
+#endif
638
+
639
+/**
640
+ * ti_sci_clk_probe - Probe function for the TI SCI clock driver
641
+ * @pdev: platform device pointer to be probed
642
+ *
643
+ * Probes the TI SCI clock device. Allocates a new clock provider
644
+ * and registers this to the common clock framework. Also applies
645
+ * any required flags to the identified clocks via clock lists
646
+ * supplied from DT. Returns 0 for success, negative error value
647
+ * for failure.
648
+ */
649
+static int ti_sci_clk_probe(struct platform_device *pdev)
650
+{
651
+ struct device *dev = &pdev->dev;
652
+ struct device_node *np = dev->of_node;
653
+ struct sci_clk_provider *provider;
654
+ const struct ti_sci_handle *handle;
655
+ int ret;
656
+
657
+ handle = devm_ti_sci_get_handle(dev);
658
+ if (IS_ERR(handle))
659
+ return PTR_ERR(handle);
660
+
661
+ provider = devm_kzalloc(dev, sizeof(*provider), GFP_KERNEL);
662
+ if (!provider)
663
+ return -ENOMEM;
664
+
665
+ provider->sci = handle;
666
+ provider->ops = &handle->ops.clk_ops;
667
+ provider->dev = dev;
668
+
669
+#ifdef CONFIG_TI_SCI_CLK_PROBE_FROM_FW
670
+ ret = ti_sci_scan_clocks_from_fw(provider);
671
+ if (ret) {
672
+ dev_err(dev, "scan clocks from FW failed: %d\n", ret);
673
+ return ret;
674
+ }
675
+#else
676
+ ret = ti_sci_scan_clocks_from_dt(provider);
677
+ if (ret) {
678
+ dev_err(dev, "scan clocks from DT failed: %d\n", ret);
679
+ return ret;
680
+ }
681
+#endif
682
+
505683 ret = ti_sci_init_clocks(provider);
506684 if (ret) {
507685 pr_err("ti-sci-init-clocks failed.\n");