1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
*/
/*
* CMN PLL block expects the reference clock from on-board Wi-Fi block,
* and supplies fixed rate clocks as output to the networking hardware
* blocks and to GCC. The networking related blocks include PPE (packet
* process engine), the externally connected PHY or switch devices, and
* the PCS.
*
* On the IPQ9574 SoC, there are three clocks with 50 MHZ and one clock
* with 25 MHZ which are output from the CMN PLL to Ethernet PHY (or switch),
* and one clock with 353 MHZ to PPE. The other fixed rate output clocks
* are supplied to GCC (24 MHZ as XO and 32 KHZ as sleep clock), and to PCS
* with 31.25 MHZ.
*
* On the IPQ5424 SoC, there is an output clock from CMN PLL to PPE at 375 MHZ,
* and an output clock to NSS (network subsystem) at 300 MHZ. The other output
* clocks from CMN PLL on IPQ5424 are the same as IPQ9574.
*
* On the IPQ5332 SoC, the CMN PLL provides a single 50 MHZ clock output to
* the Ethernet PHY (or switch) via the UNIPHY (PCS). It also supplies a 200
* MHZ clock to the PPE. The remaining fixed-rate clocks to the GCC and PCS
* are the same as those in the IPQ9574 SoC.
*
* +---------+
* | GCC |
* +--+---+--+
* AHB CLK| |SYS CLK
* V V
* +-------+---+------+
* | +-------------> eth0-50mhz
* REF CLK | IPQ9574 |
* -------->+ +-------------> eth1-50mhz
* | CMN PLL block |
* | +-------------> eth2-50mhz
* | |
* +----+----+----+---+-------------> eth-25mhz
* | | |
* V V V
* GCC PCS NSS/PPE
*/
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/clk-provider.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_clock.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <dt-bindings/clock/qcom,ipq-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq5018-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq5210-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq5332-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq5424-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq6018-cmn-pll.h>
#include <dt-bindings/clock/qcom,ipq8074-cmn-pll.h>
#include "clk-regmap.h"
#include "clk-regmap-divider.h"
#define CMN_PLL_REFCLK_SRC_SELECTION 0x28
#define CMN_PLL_REFCLK_SRC_DIV GENMASK(9, 8)
#define CMN_PLL_LOCKED 0x64
#define CMN_PLL_CLKS_LOCKED BIT(8)
#define CMN_PLL_NSS_PPE_FREQ_CTRL 0x98
#define CMN_PLL_NSS_CLK_SEL GENMASK(13, 8)
#define CMN_PLL_PPE_CLK_SEL GENMASK(5, 0)
#define CMN_PLL_PON_CONFIG 0x42c
#define CMN_PLL_PON_MODE_SEL BIT(9)
#define CMN_PLL_PON_EN BIT(8)
#define CMN_PLL_PON_DIV_CTRL GENMASK(7, 0)
#define CMN_PLL_GEPHY_312P5M_125M_SEL BIT(10)
#define CMN_PLL_POWER_ON_AND_RESET 0x780
#define CMN_ANA_EN_SW_RSTN BIT(6)
#define CMN_PLL_REFCLK_CONFIG 0x784
#define CMN_PLL_REFCLK_EXTERNAL BIT(9)
#define CMN_PLL_REFCLK_DIV GENMASK(8, 4)
#define CMN_PLL_REFCLK_INDEX GENMASK(3, 0)
#define CMN_PLL_CTRL 0x78c
#define CMN_PLL_CTRL_LOCK_DETECT_EN BIT(15)
#define CMN_PLL_DIVIDER_CTRL 0x794
#define CMN_PLL_DIVIDER_CTRL_FACTOR GENMASK(9, 0)
/* Clock gate enable bits. */
#define CMN_PLL_OUTPUT_RELATED_1 0x79c
#define CLK25M_EN_BIT 15
#define CLK50M_EN_BIT3 14
/* Documented as CLK250M_EN in the register description; gates ephy-50mhz. */
#define CLK250M_EN_BIT 13
#define CLK31P25M_EN_BIT 12
#define CLK50M_EN_BIT1 11
#define CLK50M_EN_BIT2 10
/**
* enum cmn_pll_clk_type - CMN PLL output clock registration type
* @CMN_PLL_CLK_FIXED_RATE: plain fixed rate clock
* @CMN_PLL_CLK_FIXED_GATE: fixed rate clock with a hardware gate
* @CMN_PLL_CLK_NSS: NSS clock with configurable divider
* @CMN_PLL_CLK_PPE: PPE clock with configurable divider
* @CMN_PLL_CLK_PON: PON reference clock
* @CMN_PLL_CLK_EPHY_RAW: EPHY-RAW clock
*/
enum cmn_pll_clk_type {
CMN_PLL_CLK_FIXED_RATE,
CMN_PLL_CLK_FIXED_GATE,
CMN_PLL_CLK_NSS,
CMN_PLL_CLK_PPE,
CMN_PLL_CLK_PON,
CMN_PLL_CLK_EPHY_RAW,
};
/**
* struct cmn_pll_fixed_output_clk - CMN PLL output clocks information
* @id: Clock specifier to be supplied
* @name: Clock name to be registered
* @type: Clock registration type
* @rate: Clock rate
* @enable_bit: Enable bit in CMN_PLL_OUTPUT_RELATED_1 for gate clock,
* -1 for non-gated clocks.
*/
struct cmn_pll_fixed_output_clk {
unsigned int id;
const char *name;
enum cmn_pll_clk_type type;
unsigned long rate;
int enable_bit;
};
/**
* struct clk_cmn_pll - CMN PLL hardware specific data
* @regmap: hardware regmap.
* @hw: handle between common and hardware-specific interfaces
* @div2_hw: fixed /2 clock derived from the CMN PLL output; present on
* every supported SoC.
*/
struct clk_cmn_pll {
struct regmap *regmap;
struct clk_hw hw;
struct clk_hw *div2_hw;
};
/**
* struct clk_fixed_gate - fixed rate clock with a hardware gate
* @clkr: handle between common and hardware-specific interfaces; its
* enable_reg/enable_mask are set to CMN_PLL_OUTPUT_RELATED_1 and
* this clock's gate bit, and reused via clk_enable_regmap() and
* friends. That register is shared across multiple gate clocks,
* but regmap already serializes read-modify-write access to a
* given register, so no additional locking is needed here.
* @rate: fixed clock rate.
*/
struct clk_fixed_gate {
struct clk_regmap clkr;
unsigned long rate;
};
#define CLK_PLL_OUTPUT_RAW(_id, _name, _type, _rate, _bit) { \
.id = _id, \
.name = _name, \
.type = _type, \
.rate = _rate, \
.enable_bit = _bit, \
}
#define CLK_PLL_OUTPUT(_id, _name, _rate) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_FIXED_RATE, _rate, -1)
#define CLK_PLL_GATE(_id, _name, _rate, _bit) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_FIXED_GATE, _rate, _bit)
#define CLK_PLL_NSS(_id, _name) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_NSS, 0, -1)
#define CLK_PLL_PPE(_id, _name) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_PPE, 0, -1)
#define CLK_PLL_PON(_id, _name) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_PON, 0, -1)
#define CLK_PLL_EPHY_RAW(_id, _name) \
CLK_PLL_OUTPUT_RAW(_id, _name, CMN_PLL_CLK_EPHY_RAW, 0, -1)
#define to_clk_cmn_pll(_hw) container_of(_hw, struct clk_cmn_pll, hw)
#define to_clk_fixed_gate(_hw) \
container_of(to_clk_regmap(_hw), struct clk_fixed_gate, clkr)
static const struct regmap_config ipq_cmn_pll_regmap_config = {
.reg_bits = 32,
.reg_stride = 4,
.val_bits = 32,
.max_register = 0x7fc,
};
static const struct cmn_pll_fixed_output_clk ipq5018_output_clks[] = {
CLK_PLL_OUTPUT(IPQ5018_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
CLK_PLL_OUTPUT(IPQ5018_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
CLK_PLL_OUTPUT(IPQ5018_ETH_50MHZ_CLK, "eth-50mhz", 50000000UL),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq5210_output_clks[] = {
CLK_PLL_OUTPUT(IPQ5210_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
CLK_PLL_OUTPUT(IPQ5210_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
CLK_PLL_GATE(IPQ5210_PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL, CLK31P25M_EN_BIT),
CLK_PLL_GATE(IPQ5210_ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL, CLK50M_EN_BIT1),
CLK_PLL_GATE(IPQ5210_ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL, CLK50M_EN_BIT2),
CLK_PLL_GATE(IPQ5210_ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL, CLK50M_EN_BIT3),
CLK_PLL_GATE(IPQ5210_EPHY_50MHZ_CLK, "ephy-50mhz", 50000000UL, CLK250M_EN_BIT),
CLK_PLL_GATE(IPQ5210_ETH_25MHZ_CLK, "eth-25mhz", 25000000UL, CLK25M_EN_BIT),
CLK_PLL_NSS(IPQ5210_NSS_CLK, "nss"),
CLK_PLL_PPE(IPQ5210_PPE_CLK, "ppe"),
CLK_PLL_PON(IPQ5210_PON_CLK, "pon"),
CLK_PLL_EPHY_RAW(IPQ5210_EPHY_RAW_CLK, "ephy-raw"),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq6018_output_clks[] = {
CLK_PLL_OUTPUT(IPQ6018_BIAS_PLL_CC_CLK, "bias_pll_cc_clk", 300000000UL),
CLK_PLL_OUTPUT(IPQ6018_BIAS_PLL_NSS_NOC_CLK, "bias_pll_nss_noc_clk", 416500000UL),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq8074_output_clks[] = {
CLK_PLL_OUTPUT(IPQ8074_BIAS_PLL_CC_CLK, "bias_pll_cc_clk", 300000000UL),
CLK_PLL_OUTPUT(IPQ8074_BIAS_PLL_NSS_NOC_CLK, "bias_pll_nss_noc_clk", 416500000UL),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq5332_output_clks[] = {
CLK_PLL_OUTPUT(IPQ5332_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
CLK_PLL_OUTPUT(IPQ5332_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
CLK_PLL_OUTPUT(IPQ5332_PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
CLK_PLL_OUTPUT(IPQ5332_NSS_300MHZ_CLK, "nss-300mhz", 300000000UL),
CLK_PLL_OUTPUT(IPQ5332_PPE_200MHZ_CLK, "ppe-200mhz", 200000000UL),
CLK_PLL_OUTPUT(IPQ5332_ETH_50MHZ_CLK, "eth-50mhz", 50000000UL),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq5424_output_clks[] = {
CLK_PLL_OUTPUT(IPQ5424_XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
CLK_PLL_OUTPUT(IPQ5424_SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
CLK_PLL_OUTPUT(IPQ5424_PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
CLK_PLL_OUTPUT(IPQ5424_NSS_300MHZ_CLK, "nss-300mhz", 300000000UL),
CLK_PLL_OUTPUT(IPQ5424_PPE_375MHZ_CLK, "ppe-375mhz", 375000000UL),
CLK_PLL_OUTPUT(IPQ5424_ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL),
CLK_PLL_OUTPUT(IPQ5424_ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL),
CLK_PLL_OUTPUT(IPQ5424_ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL),
CLK_PLL_OUTPUT(IPQ5424_ETH_25MHZ_CLK, "eth-25mhz", 25000000UL),
{ /* Sentinel */ }
};
static const struct cmn_pll_fixed_output_clk ipq9574_output_clks[] = {
CLK_PLL_OUTPUT(XO_24MHZ_CLK, "xo-24mhz", 24000000UL),
CLK_PLL_OUTPUT(SLEEP_32KHZ_CLK, "sleep-32khz", 32000UL),
CLK_PLL_OUTPUT(PCS_31P25MHZ_CLK, "pcs-31p25mhz", 31250000UL),
CLK_PLL_OUTPUT(NSS_1200MHZ_CLK, "nss-1200mhz", 1200000000UL),
CLK_PLL_OUTPUT(PPE_353MHZ_CLK, "ppe-353mhz", 353000000UL),
CLK_PLL_OUTPUT(ETH0_50MHZ_CLK, "eth0-50mhz", 50000000UL),
CLK_PLL_OUTPUT(ETH1_50MHZ_CLK, "eth1-50mhz", 50000000UL),
CLK_PLL_OUTPUT(ETH2_50MHZ_CLK, "eth2-50mhz", 50000000UL),
CLK_PLL_OUTPUT(ETH_25MHZ_CLK, "eth-25mhz", 25000000UL),
{ /* Sentinel */ }
};
/*
* CMN PLL has the single parent clock, which supports the several
* possible parent clock rates, each parent clock rate is reflected
* by the specific reference index value in the hardware.
*/
static int ipq_cmn_pll_find_freq_index(unsigned long parent_rate)
{
int index = -EINVAL;
switch (parent_rate) {
case 25000000:
index = 3;
break;
case 31250000:
index = 4;
break;
case 40000000:
index = 6;
break;
case 48000000:
case 96000000:
/*
* Parent clock rate 48 MHZ and 96 MHZ take the same value
* of reference clock index. 96 MHZ needs the source clock
* divider to be programmed as 2.
*/
index = 7;
break;
case 50000000:
index = 8;
break;
default:
break;
}
return index;
}
static unsigned long clk_cmn_pll_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
u32 val, factor, ref_div;
/*
* The value of CMN_PLL_DIVIDER_CTRL_FACTOR is automatically adjusted
* by HW according to the parent clock rate.
*/
regmap_read(cmn_pll->regmap, CMN_PLL_DIVIDER_CTRL, &val);
factor = FIELD_GET(CMN_PLL_DIVIDER_CTRL_FACTOR, val);
if (WARN_ON(factor == 0))
factor = 1;
regmap_read(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG, &val);
ref_div = FIELD_GET(CMN_PLL_REFCLK_DIV, val);
if (WARN_ON(ref_div == 0))
ref_div = 1;
return div_u64((u64)parent_rate * 2 * factor, ref_div);
}
static int clk_cmn_pll_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
int ret;
/* Validate the rate of the single parent clock. */
ret = ipq_cmn_pll_find_freq_index(req->best_parent_rate);
return ret < 0 ? ret : 0;
}
/*
* This function is used to initialize the CMN PLL to enable the fixed
* rate output clocks. It is expected to be configured once.
*/
static int clk_cmn_pll_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long parent_rate)
{
struct clk_cmn_pll *cmn_pll = to_clk_cmn_pll(hw);
int ret, index;
u32 val;
/*
* Configure the reference input clock selection as per the given
* parent clock. The output clock rates are always of fixed value.
*/
index = ipq_cmn_pll_find_freq_index(parent_rate);
if (index < 0)
return index;
ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
CMN_PLL_REFCLK_INDEX,
FIELD_PREP(CMN_PLL_REFCLK_INDEX, index));
if (ret)
return ret;
/*
* Update the source clock rate selection and source clock
* divider as 2 when the parent clock rate is 96 MHZ.
*/
if (parent_rate == 96000000) {
ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_CONFIG,
CMN_PLL_REFCLK_DIV,
FIELD_PREP(CMN_PLL_REFCLK_DIV, 2));
if (ret)
return ret;
ret = regmap_update_bits(cmn_pll->regmap, CMN_PLL_REFCLK_SRC_SELECTION,
CMN_PLL_REFCLK_SRC_DIV,
FIELD_PREP(CMN_PLL_REFCLK_SRC_DIV, 0));
if (ret)
return ret;
}
/* Enable PLL locked detect. */
ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_CTRL,
CMN_PLL_CTRL_LOCK_DETECT_EN);
if (ret)
return ret;
/*
* Reset the CMN PLL block to ensure the updated configurations
* take effect.
*/
ret = regmap_clear_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
CMN_ANA_EN_SW_RSTN);
if (ret)
return ret;
usleep_range(1000, 1200);
ret = regmap_set_bits(cmn_pll->regmap, CMN_PLL_POWER_ON_AND_RESET,
CMN_ANA_EN_SW_RSTN);
if (ret)
return ret;
/* Stability check of CMN PLL output clocks. */
return regmap_read_poll_timeout(cmn_pll->regmap, CMN_PLL_LOCKED, val,
(val & CMN_PLL_CLKS_LOCKED),
100, 100 * USEC_PER_MSEC);
}
static const struct clk_ops clk_cmn_pll_ops = {
.recalc_rate = clk_cmn_pll_recalc_rate,
.determine_rate = clk_cmn_pll_determine_rate,
.set_rate = clk_cmn_pll_set_rate,
};
static struct clk_hw *ipq_cmn_pll_clk_hw_register(struct platform_device *pdev)
{
struct clk_parent_data pdata = { .index = 0 };
struct device *dev = &pdev->dev;
struct clk_init_data init = {};
struct clk_cmn_pll *cmn_pll;
struct regmap *regmap;
void __iomem *base;
int ret;
base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(base))
return ERR_CAST(base);
regmap = devm_regmap_init_mmio(dev, base, &ipq_cmn_pll_regmap_config);
if (IS_ERR(regmap))
return ERR_CAST(regmap);
cmn_pll = devm_kzalloc(dev, sizeof(*cmn_pll), GFP_KERNEL);
if (!cmn_pll)
return ERR_PTR(-ENOMEM);
init.name = "cmn_pll";
init.parent_data = &pdata;
init.num_parents = 1;
init.ops = &clk_cmn_pll_ops;
cmn_pll->hw.init = &init;
cmn_pll->regmap = regmap;
ret = devm_clk_hw_register(dev, &cmn_pll->hw);
if (ret)
return ERR_PTR(ret);
return &cmn_pll->hw;
}
static struct clk_hw *ipq_cmn_pll_regmap_div_register(struct device *dev,
const char *name,
struct clk_hw *parent_hw,
u32 field_mask)
{
struct clk_parent_data pdata = { .hw = parent_hw };
struct clk_regmap_div *div_clk;
int ret;
div_clk = devm_kzalloc(dev, sizeof(*div_clk), GFP_KERNEL);
if (!div_clk)
return ERR_PTR(-ENOMEM);
div_clk->reg = CMN_PLL_NSS_PPE_FREQ_CTRL;
div_clk->shift = __ffs(field_mask);
div_clk->width = hweight32(field_mask);
/*
* CMN_PLL_NSS_CLK_SEL / CMN_PLL_PPE_CLK_SEL reset to a valid, non-zero
* divider in hardware. CLK_DIVIDER_ALLOW_ZERO is deliberately not set:
* a divider field read back as 0 is genuinely invalid and should trip
* the core clk-divider's zero-divisor WARN rather than be silently
* tolerated.
*/
div_clk->flags = CLK_DIVIDER_ONE_BASED;
div_clk->clkr.hw.init = &(struct clk_init_data){
.name = name,
.parent_data = &pdata,
.num_parents = 1,
.ops = &clk_regmap_div_ops,
};
ret = devm_clk_register_regmap(dev, &div_clk->clkr);
if (ret)
return ERR_PTR(ret);
return &div_clk->clkr.hw;
}
/*
* PON (Passive Optical Network) reference clock operations.
* The PON refclk's parent is cmn_pll_div2 (CMN PLL rate / 2); it is
* then divided by a configurable 8-bit divider (1-255).
*/
static unsigned long clk_pon_clk_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
struct clk_regmap *pon_clk = to_clk_regmap(hw);
u32 val, div;
int ret;
ret = regmap_read(pon_clk->regmap, CMN_PLL_PON_CONFIG, &val);
if (ret)
return 0;
/* Check if in UNIPHY mode (bit 9 = 0) - fixed 31.25 MHz */
if (!(val & CMN_PLL_PON_MODE_SEL))
return 31250000UL;
/* PON mode: calculate from divider */
div = FIELD_GET(CMN_PLL_PON_DIV_CTRL, val);
if (!div)
return 0;
return DIV_ROUND_CLOSEST_ULL((u64)parent_rate, div);
}
static int clk_pon_clk_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
unsigned long div, pon_rate, uniphy_rate = 31250000UL;
bool uniphy_rate_valid, pon_rate_valid;
if (!req->rate || !req->best_parent_rate)
return -EINVAL;
div = DIV64_U64_ROUND_CLOSEST((u64)req->best_parent_rate, req->rate);
/* Clamp to valid range (1-255) */
div = clamp_t(unsigned long, div, 1, 255);
pon_rate = DIV_ROUND_CLOSEST_ULL((u64)req->best_parent_rate, div);
uniphy_rate_valid = uniphy_rate >= req->min_rate &&
uniphy_rate <= req->max_rate;
pon_rate_valid = pon_rate >= req->min_rate &&
pon_rate <= req->max_rate;
if (!uniphy_rate_valid && !pon_rate_valid)
return -EINVAL;
/* Pick whichever mode gets closer to the requested rate */
if (uniphy_rate_valid && pon_rate_valid) {
unsigned long diff_uniphy, diff_pon;
diff_uniphy = abs_diff(req->rate, uniphy_rate);
diff_pon = abs_diff(req->rate, pon_rate);
req->rate = diff_uniphy < diff_pon ? uniphy_rate : pon_rate;
} else {
req->rate = uniphy_rate_valid ? uniphy_rate : pon_rate;
}
return 0;
}
static int clk_pon_clk_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long parent_rate)
{
struct clk_regmap *pon_clk = to_clk_regmap(hw);
unsigned long div;
if (rate == 0)
return -EINVAL;
/*
* An exact request for 31.25 MHz is always satisfiable by UNIPHY
* mode, even though PON mode with a suitable divider can produce
* the same frequency for some parent rates. Preferring UNIPHY here
* is safe: this function is only ever called with a rate produced
* by clk_pon_clk_determine_rate() through the standard
* clk_set_rate() path, and either mode yields the identical output
* rate for this value.
*/
if (rate == 31250000UL)
return regmap_clear_bits(pon_clk->regmap, CMN_PLL_PON_CONFIG,
CMN_PLL_PON_MODE_SEL);
div = DIV64_U64_ROUND_CLOSEST((u64)parent_rate, rate);
if (div == 0 || div > 255)
return -EINVAL;
/* Switch to PON mode and program the divider in a single write */
return regmap_update_bits(pon_clk->regmap, CMN_PLL_PON_CONFIG,
CMN_PLL_PON_MODE_SEL | CMN_PLL_PON_DIV_CTRL,
CMN_PLL_PON_MODE_SEL |
FIELD_PREP(CMN_PLL_PON_DIV_CTRL, div));
}
static const struct clk_ops clk_pon_clk_ops = {
.enable = clk_enable_regmap,
.disable = clk_disable_regmap,
.is_enabled = clk_is_enabled_regmap,
.recalc_rate = clk_pon_clk_recalc_rate,
.determine_rate = clk_pon_clk_determine_rate,
.set_rate = clk_pon_clk_set_rate,
};
static struct clk_hw *ipq_cmn_pll_pon_clk_register(struct device *dev,
const char *name,
struct clk_hw *parent_hw)
{
struct clk_parent_data pdata = { .hw = parent_hw };
struct clk_init_data init = {};
struct clk_regmap *pon_clk;
int ret;
pon_clk = devm_kzalloc(dev, sizeof(*pon_clk), GFP_KERNEL);
if (!pon_clk)
return ERR_PTR(-ENOMEM);
init.name = name;
init.parent_data = &pdata;
init.num_parents = 1;
init.ops = &clk_pon_clk_ops;
/*
* The PON reference clock may already be enabled by bootloader
* or consumed by hardware without an in-kernel client driver.
* Add CLK_IGNORE_UNUSED so the clock framework does not disable
* it when no consumer has claimed it.
*/
init.flags = CLK_IGNORE_UNUSED;
pon_clk->hw.init = &init;
pon_clk->enable_reg = CMN_PLL_PON_CONFIG;
pon_clk->enable_mask = CMN_PLL_PON_EN;
ret = devm_clk_register_regmap(dev, pon_clk);
if (ret)
return ERR_PTR(ret);
return &pon_clk->hw;
}
/*
* EPHY-RAW clock operations for IPQ5210.
* The output clock rate is selected via bit 10 of CMN_PLL_PON_CONFIG:
* 0: 125 MHz (for 1G link speed)
* 1: 312.5 MHz (for 2.5G link speed)
*/
static unsigned long clk_ephy_raw_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
struct clk_regmap *ephy_raw_clk = to_clk_regmap(hw);
u32 val;
int ret;
ret = regmap_read(ephy_raw_clk->regmap, CMN_PLL_PON_CONFIG, &val);
if (WARN_ON_ONCE(ret))
return 0;
if (val & CMN_PLL_GEPHY_312P5M_125M_SEL)
return 312500000UL;
return 125000000UL;
}
static int clk_ephy_raw_determine_rate(struct clk_hw *hw,
struct clk_rate_request *req)
{
unsigned long rate_125m = 125000000UL, rate_312p5m = 312500000UL;
bool rate_125m_valid, rate_312p5m_valid;
rate_125m_valid = rate_125m >= req->min_rate &&
rate_125m <= req->max_rate;
rate_312p5m_valid = rate_312p5m >= req->min_rate &&
rate_312p5m <= req->max_rate;
if (!rate_125m_valid && !rate_312p5m_valid)
return -EINVAL;
/* Pick whichever of the two supported rates is closer to the request */
if (rate_125m_valid && rate_312p5m_valid) {
unsigned long diff_125m, diff_312p5m;
diff_125m = abs_diff(req->rate, rate_125m);
diff_312p5m = abs_diff(req->rate, rate_312p5m);
req->rate = diff_125m < diff_312p5m ? rate_125m : rate_312p5m;
} else {
req->rate = rate_125m_valid ? rate_125m : rate_312p5m;
}
return 0;
}
static int clk_ephy_raw_set_rate(struct clk_hw *hw, unsigned long rate,
unsigned long parent_rate)
{
struct clk_regmap *ephy_raw_clk = to_clk_regmap(hw);
if (rate == 125000000UL)
return regmap_clear_bits(ephy_raw_clk->regmap,
CMN_PLL_PON_CONFIG,
CMN_PLL_GEPHY_312P5M_125M_SEL);
if (rate == 312500000UL)
return regmap_set_bits(ephy_raw_clk->regmap,
CMN_PLL_PON_CONFIG,
CMN_PLL_GEPHY_312P5M_125M_SEL);
return -EINVAL;
}
static const struct clk_ops clk_ephy_raw_ops = {
.recalc_rate = clk_ephy_raw_recalc_rate,
.determine_rate = clk_ephy_raw_determine_rate,
.set_rate = clk_ephy_raw_set_rate,
};
static struct clk_hw *ipq_cmn_pll_ephy_raw_register(struct device *dev,
const char *name,
struct clk_hw *parent_hw)
{
struct clk_parent_data pdata = { .hw = parent_hw };
struct clk_regmap *ephy_raw_clk;
struct clk_init_data init = {};
int ret;
ephy_raw_clk = devm_kzalloc(dev, sizeof(*ephy_raw_clk), GFP_KERNEL);
if (!ephy_raw_clk)
return ERR_PTR(-ENOMEM);
init.name = name;
init.parent_data = &pdata;
init.num_parents = 1;
init.ops = &clk_ephy_raw_ops;
ephy_raw_clk->hw.init = &init;
ret = devm_clk_register_regmap(dev, ephy_raw_clk);
if (ret)
return ERR_PTR(ret);
return &ephy_raw_clk->hw;
}
static unsigned long clk_fixed_gate_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
struct clk_fixed_gate *gate_clk = to_clk_fixed_gate(hw);
return gate_clk->rate;
}
static const struct clk_ops clk_fixed_gate_ops = {
.enable = clk_enable_regmap,
.disable = clk_disable_regmap,
.is_enabled = clk_is_enabled_regmap,
.recalc_rate = clk_fixed_gate_recalc_rate,
};
static struct clk_hw *ipq_cmn_pll_register_fixed_gate(struct device *dev,
const char *name,
struct clk_hw *parent_hw,
int enable_bit,
unsigned long rate)
{
struct clk_parent_data pdata = { .hw = parent_hw };
struct clk_fixed_gate *gate_clk;
struct clk_init_data init = {};
int ret;
gate_clk = devm_kzalloc(dev, sizeof(*gate_clk), GFP_KERNEL);
if (!gate_clk)
return ERR_PTR(-ENOMEM);
init.name = name;
init.parent_data = &pdata;
init.num_parents = 1;
init.ops = &clk_fixed_gate_ops;
/*
* These gated clocks may be relied on by external hardware or
* bootloader-enabled paths without an in-kernel client driver.
* Add CLK_IGNORE_UNUSED so the clock framework does not disable
* them when no consumer has claimed them.
*/
init.flags = CLK_IGNORE_UNUSED;
gate_clk->clkr.hw.init = &init;
gate_clk->clkr.enable_reg = CMN_PLL_OUTPUT_RELATED_1;
gate_clk->clkr.enable_mask = BIT(enable_bit);
gate_clk->rate = rate;
ret = devm_clk_register_regmap(dev, &gate_clk->clkr);
if (ret)
return ERR_PTR(ret);
return &gate_clk->clkr.hw;
}
static int ipq_cmn_pll_register_clks(struct platform_device *pdev)
{
const struct cmn_pll_fixed_output_clk *p, *fixed_clk;
struct clk_hw_onecell_data *hw_data;
struct device *dev = &pdev->dev;
struct clk_cmn_pll *cmn_pll;
struct clk_hw *cmn_pll_hw;
unsigned int num_clks;
struct clk_hw *hw;
int ret, i;
fixed_clk = device_get_match_data(dev);
if (!fixed_clk)
return -EINVAL;
num_clks = 0;
for (p = fixed_clk; p->name; p++)
num_clks++;
hw_data = devm_kzalloc(dev, struct_size(hw_data, hws, num_clks + 1),
GFP_KERNEL);
if (!hw_data)
return -ENOMEM;
hw_data->num = num_clks + 1;
/*
* Register the CMN PLL clock, which is the parent clock of
* the fixed rate output clocks.
*/
cmn_pll_hw = ipq_cmn_pll_clk_hw_register(pdev);
if (IS_ERR(cmn_pll_hw))
return PTR_ERR(cmn_pll_hw);
cmn_pll = to_clk_cmn_pll(cmn_pll_hw);
cmn_pll->div2_hw = devm_clk_hw_register_fixed_factor_parent_hw(dev,
"cmn_pll_div2",
cmn_pll_hw,
0, 1, 2);
if (IS_ERR(cmn_pll->div2_hw))
return PTR_ERR(cmn_pll->div2_hw);
/* Register the fixed rate output clocks. */
for (i = 0; i < num_clks; i++) {
hw = ERR_PTR(-EINVAL);
switch (fixed_clk[i].type) {
case CMN_PLL_CLK_FIXED_RATE: {
struct clk_parent_data pdata = { .hw = cmn_pll_hw };
hw = devm_clk_hw_register_fixed_rate_parent_data(dev,
fixed_clk[i].name,
&pdata, 0,
fixed_clk[i].rate);
break;
}
case CMN_PLL_CLK_FIXED_GATE:
hw = ipq_cmn_pll_register_fixed_gate(dev,
fixed_clk[i].name,
cmn_pll->div2_hw,
fixed_clk[i].enable_bit,
fixed_clk[i].rate);
break;
case CMN_PLL_CLK_NSS:
hw = ipq_cmn_pll_regmap_div_register(dev,
fixed_clk[i].name,
cmn_pll->div2_hw,
CMN_PLL_NSS_CLK_SEL);
break;
case CMN_PLL_CLK_PPE:
hw = ipq_cmn_pll_regmap_div_register(dev,
fixed_clk[i].name,
cmn_pll->div2_hw,
CMN_PLL_PPE_CLK_SEL);
break;
case CMN_PLL_CLK_PON:
hw = ipq_cmn_pll_pon_clk_register(dev,
fixed_clk[i].name,
cmn_pll->div2_hw);
break;
case CMN_PLL_CLK_EPHY_RAW:
hw = ipq_cmn_pll_ephy_raw_register(dev,
fixed_clk[i].name,
cmn_pll->div2_hw);
break;
}
if (IS_ERR(hw))
return PTR_ERR(hw);
hw_data->hws[fixed_clk[i].id] = hw;
}
/*
* Provide the CMN PLL clock. The clock rate of CMN PLL
* is configured to 12 GHZ by DT property assigned-clock-rates-u64.
*/
hw_data->hws[CMN_PLL_CLK] = cmn_pll_hw;
ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_data);
if (ret)
return ret;
platform_set_drvdata(pdev, hw_data);
return 0;
}
static int ipq_cmn_pll_clk_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
int ret;
ret = devm_pm_runtime_enable(dev);
if (ret)
return ret;
ret = devm_pm_clk_create(dev);
if (ret)
return ret;
/*
* To access the CMN PLL registers, the GCC AHB & SYS clocks
* of CMN PLL block need to be enabled.
*/
ret = pm_clk_add(dev, "ahb");
if (ret)
return dev_err_probe(dev, ret, "Failed to add AHB clock\n");
ret = pm_clk_add(dev, "sys");
if (ret)
return dev_err_probe(dev, ret, "Failed to add SYS clock\n");
ret = pm_runtime_resume_and_get(dev);
if (ret)
return ret;
/* Register CMN PLL clock and fixed rate output clocks. */
ret = ipq_cmn_pll_register_clks(pdev);
pm_runtime_put(dev);
if (ret)
return dev_err_probe(dev, ret,
"Failed to register CMN PLL clocks\n");
return 0;
}
static const struct dev_pm_ops ipq_cmn_pll_pm_ops = {
SET_RUNTIME_PM_OPS(pm_clk_suspend, pm_clk_resume, NULL)
};
static const struct of_device_id ipq_cmn_pll_clk_ids[] = {
{ .compatible = "qcom,ipq5018-cmn-pll", .data = &ipq5018_output_clks },
{ .compatible = "qcom,ipq5210-cmn-pll", .data = &ipq5210_output_clks },
{ .compatible = "qcom,ipq5332-cmn-pll", .data = &ipq5332_output_clks },
{ .compatible = "qcom,ipq5424-cmn-pll", .data = &ipq5424_output_clks },
{ .compatible = "qcom,ipq6018-cmn-pll", .data = &ipq6018_output_clks },
{ .compatible = "qcom,ipq8074-cmn-pll", .data = &ipq8074_output_clks },
{ .compatible = "qcom,ipq9574-cmn-pll", .data = &ipq9574_output_clks },
{ }
};
MODULE_DEVICE_TABLE(of, ipq_cmn_pll_clk_ids);
static struct platform_driver ipq_cmn_pll_clk_driver = {
.probe = ipq_cmn_pll_clk_probe,
.driver = {
.name = "ipq_cmn_pll",
.of_match_table = ipq_cmn_pll_clk_ids,
.pm = &ipq_cmn_pll_pm_ops,
},
};
module_platform_driver(ipq_cmn_pll_clk_driver);
MODULE_DESCRIPTION("Qualcomm Technologies, Inc. IPQ CMN PLL Driver");
MODULE_LICENSE("GPL");
|