2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7 * kind, whether express or implied; without even the implied warranty
8 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
12 #include <linux/clk.h>
13 #include <linux/clkdev.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/string.h>
23 #define ADPLL_PLLSS_MMR_LOCK_OFFSET 0x00 /* Managed by MPPULL */
24 #define ADPLL_PLLSS_MMR_LOCK_ENABLED 0x1f125B64
25 #define ADPLL_PLLSS_MMR_UNLOCK_MAGIC 0x1eda4c3d
27 #define ADPLL_PWRCTRL_OFFSET 0x00
28 #define ADPLL_PWRCTRL_PONIN 5
29 #define ADPLL_PWRCTRL_PGOODIN 4
30 #define ADPLL_PWRCTRL_RET 3
31 #define ADPLL_PWRCTRL_ISORET 2
32 #define ADPLL_PWRCTRL_ISOSCAN 1
33 #define ADPLL_PWRCTRL_OFFMODE 0
35 #define ADPLL_CLKCTRL_OFFSET 0x04
36 #define ADPLL_CLKCTRL_CLKDCOLDOEN 29
37 #define ADPLL_CLKCTRL_IDLE 23
38 #define ADPLL_CLKCTRL_CLKOUTEN 20
39 #define ADPLL_CLKINPHIFSEL_ADPLL_S 19 /* REVISIT: which bit? */
40 #define ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ 19
41 #define ADPLL_CLKCTRL_ULOWCLKEN 18
42 #define ADPLL_CLKCTRL_CLKDCOLDOPWDNZ 17
43 #define ADPLL_CLKCTRL_M2PWDNZ 16
44 #define ADPLL_CLKCTRL_M3PWDNZ_ADPLL_S 15
45 #define ADPLL_CLKCTRL_LOWCURRSTDBY_ADPLL_S 13
46 #define ADPLL_CLKCTRL_LPMODE_ADPLL_S 12
47 #define ADPLL_CLKCTRL_REGM4XEN_ADPLL_S 10
48 #define ADPLL_CLKCTRL_SELFREQDCO_ADPLL_LJ 10
49 #define ADPLL_CLKCTRL_TINITZ 0
51 #define ADPLL_TENABLE_OFFSET 0x08
52 #define ADPLL_TENABLEDIV_OFFSET 0x8c
54 #define ADPLL_M2NDIV_OFFSET 0x10
55 #define ADPLL_M2NDIV_M2 16
56 #define ADPLL_M2NDIV_M2_ADPLL_S_WIDTH 5
57 #define ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH 7
59 #define ADPLL_MN2DIV_OFFSET 0x14
60 #define ADPLL_MN2DIV_N2 16
62 #define ADPLL_FRACDIV_OFFSET 0x18
63 #define ADPLL_FRACDIV_REGSD 24
64 #define ADPLL_FRACDIV_FRACTIONALM 0
65 #define ADPLL_FRACDIV_FRACTIONALM_MASK 0x3ffff
67 #define ADPLL_BWCTRL_OFFSET 0x1c
68 #define ADPLL_BWCTRL_BWCONTROL 1
69 #define ADPLL_BWCTRL_BW_INCR_DECRZ 0
71 #define ADPLL_RESERVED_OFFSET 0x20
73 #define ADPLL_STATUS_OFFSET 0x24
74 #define ADPLL_STATUS_PONOUT 31
75 #define ADPLL_STATUS_PGOODOUT 30
76 #define ADPLL_STATUS_LDOPWDN 29
77 #define ADPLL_STATUS_RECAL_BSTATUS3 28
78 #define ADPLL_STATUS_RECAL_OPPIN 27
79 #define ADPLL_STATUS_PHASELOCK 10
80 #define ADPLL_STATUS_FREQLOCK 9
81 #define ADPLL_STATUS_BYPASSACK 8
82 #define ADPLL_STATUS_LOSSREF 6
83 #define ADPLL_STATUS_CLKOUTENACK 5
84 #define ADPLL_STATUS_LOCK2 4
85 #define ADPLL_STATUS_M2CHANGEACK 3
86 #define ADPLL_STATUS_HIGHJITTER 1
87 #define ADPLL_STATUS_BYPASS 0
88 #define ADPLL_STATUS_PREPARED_MASK (BIT(ADPLL_STATUS_PHASELOCK) | \
89 BIT(ADPLL_STATUS_FREQLOCK))
91 #define ADPLL_M3DIV_OFFSET 0x28 /* Only on MPUPLL */
92 #define ADPLL_M3DIV_M3 0
93 #define ADPLL_M3DIV_M3_WIDTH 5
94 #define ADPLL_M3DIV_M3_MASK 0x1f
96 #define ADPLL_RAMPCTRL_OFFSET 0x2c /* Only on MPUPLL */
97 #define ADPLL_RAMPCTRL_CLKRAMPLEVEL 19
98 #define ADPLL_RAMPCTRL_CLKRAMPRATE 16
99 #define ADPLL_RAMPCTRL_RELOCK_RAMP_EN 0
101 #define MAX_ADPLL_INPUTS 3
102 #define MAX_ADPLL_OUTPUTS 4
103 #define ADPLL_MAX_RETRIES 5
105 #define to_dco(_hw) container_of(_hw, struct ti_adpll_dco_data, hw)
106 #define to_adpll(_hw) container_of(_hw, struct ti_adpll_data, dco)
107 #define to_clkout(_hw) container_of(_hw, struct ti_adpll_clkout_data, hw)
109 enum ti_adpll_clocks
{
123 #define TI_ADPLL_NR_CLOCKS (TI_ADPLL_M3 + 1)
125 enum ti_adpll_inputs
{
131 enum ti_adpll_s_outputs
{
132 TI_ADPLL_S_DCOCLKLDO
,
135 TI_ADPLL_S_CLKOUTHIF
,
138 enum ti_adpll_lj_outputs
{
139 TI_ADPLL_LJ_CLKDCOLDO
,
141 TI_ADPLL_LJ_CLKOUTLDO
,
144 struct ti_adpll_platform_data
{
145 const bool is_type_s
;
146 const int nr_max_inputs
;
147 const int nr_max_outputs
;
148 const int output_index
;
151 struct ti_adpll_clock
{
153 struct clk_lookup
*cl
;
154 void (*unregister
)(struct clk
*clk
);
157 struct ti_adpll_dco_data
{
161 struct ti_adpll_clkout_data
{
162 struct ti_adpll_data
*adpll
;
163 struct clk_gate gate
;
167 struct ti_adpll_data
{
169 const struct ti_adpll_platform_data
*c
;
170 struct device_node
*np
;
172 void __iomem
*iobase
;
174 spinlock_t lock
; /* For ADPLL shared register access */
175 const char *parent_names
[MAX_ADPLL_INPUTS
];
176 struct clk
*parent_clocks
[MAX_ADPLL_INPUTS
];
177 struct ti_adpll_clock
*clocks
;
178 struct clk_onecell_data outputs
;
179 struct ti_adpll_dco_data dco
;
182 static const char *ti_adpll_clk_get_name(struct ti_adpll_data
*d
,
189 if (output_index
>= 0) {
190 err
= of_property_read_string_index(d
->np
,
191 "clock-output-names",
197 name
= devm_kasprintf(d
->dev
, GFP_KERNEL
, "%08lx.adpll.%s",
204 #define ADPLL_MAX_CON_ID 16 /* See MAX_CON_ID */
206 static int ti_adpll_setup_clock(struct ti_adpll_data
*d
, struct clk
*clock
,
207 int index
, int output_index
, const char *name
,
208 void (*unregister
)(struct clk
*clk
))
210 struct clk_lookup
*cl
;
211 const char *postfix
= NULL
;
212 char con_id
[ADPLL_MAX_CON_ID
];
214 d
->clocks
[index
].clk
= clock
;
215 d
->clocks
[index
].unregister
= unregister
;
217 /* Separate con_id in format "pll040dcoclkldo" to fit MAX_CON_ID */
218 postfix
= strrchr(name
, '.');
219 if (postfix
&& strlen(postfix
) > 1) {
220 if (strlen(postfix
) > ADPLL_MAX_CON_ID
)
221 dev_warn(d
->dev
, "clock %s con_id lookup may fail\n",
223 snprintf(con_id
, 16, "pll%03lx%s", d
->pa
& 0xfff, postfix
+ 1);
224 cl
= clkdev_create(clock
, con_id
, NULL
);
227 d
->clocks
[index
].cl
= cl
;
229 dev_warn(d
->dev
, "no con_id for clock %s\n", name
);
232 if (output_index
< 0)
235 d
->outputs
.clks
[output_index
] = clock
;
236 d
->outputs
.clk_num
++;
241 static int ti_adpll_init_divider(struct ti_adpll_data
*d
,
242 enum ti_adpll_clocks index
,
243 int output_index
, char *name
,
244 struct clk
*parent_clock
,
247 u8 clk_divider_flags
)
249 const char *child_name
;
250 const char *parent_name
;
253 child_name
= ti_adpll_clk_get_name(d
, output_index
, name
);
257 parent_name
= __clk_get_name(parent_clock
);
258 clock
= clk_register_divider(d
->dev
, child_name
, parent_name
, 0,
259 reg
, shift
, width
, clk_divider_flags
,
262 dev_err(d
->dev
, "failed to register divider %s: %li\n",
263 name
, PTR_ERR(clock
));
264 return PTR_ERR(clock
);
267 return ti_adpll_setup_clock(d
, clock
, index
, output_index
, child_name
,
268 clk_unregister_divider
);
271 static int ti_adpll_init_mux(struct ti_adpll_data
*d
,
272 enum ti_adpll_clocks index
,
273 char *name
, struct clk
*clk0
,
278 const char *child_name
;
279 const char *parents
[2];
282 child_name
= ti_adpll_clk_get_name(d
, -ENODEV
, name
);
285 parents
[0] = __clk_get_name(clk0
);
286 parents
[1] = __clk_get_name(clk1
);
287 clock
= clk_register_mux(d
->dev
, child_name
, parents
, 2, 0,
288 reg
, shift
, 1, 0, &d
->lock
);
290 dev_err(d
->dev
, "failed to register mux %s: %li\n",
291 name
, PTR_ERR(clock
));
292 return PTR_ERR(clock
);
295 return ti_adpll_setup_clock(d
, clock
, index
, -ENODEV
, child_name
,
299 static int ti_adpll_init_gate(struct ti_adpll_data
*d
,
300 enum ti_adpll_clocks index
,
301 int output_index
, char *name
,
302 struct clk
*parent_clock
,
307 const char *child_name
;
308 const char *parent_name
;
311 child_name
= ti_adpll_clk_get_name(d
, output_index
, name
);
315 parent_name
= __clk_get_name(parent_clock
);
316 clock
= clk_register_gate(d
->dev
, child_name
, parent_name
, 0,
317 reg
, bit_idx
, clk_gate_flags
,
320 dev_err(d
->dev
, "failed to register gate %s: %li\n",
321 name
, PTR_ERR(clock
));
322 return PTR_ERR(clock
);
325 return ti_adpll_setup_clock(d
, clock
, index
, output_index
, child_name
,
326 clk_unregister_gate
);
329 static int ti_adpll_init_fixed_factor(struct ti_adpll_data
*d
,
330 enum ti_adpll_clocks index
,
332 struct clk
*parent_clock
,
336 const char *child_name
;
337 const char *parent_name
;
340 child_name
= ti_adpll_clk_get_name(d
, -ENODEV
, name
);
344 parent_name
= __clk_get_name(parent_clock
);
345 clock
= clk_register_fixed_factor(d
->dev
, child_name
, parent_name
,
348 return PTR_ERR(clock
);
350 return ti_adpll_setup_clock(d
, clock
, index
, -ENODEV
, child_name
,
354 static void ti_adpll_set_idle_bypass(struct ti_adpll_data
*d
)
359 spin_lock_irqsave(&d
->lock
, flags
);
360 v
= readl_relaxed(d
->regs
+ ADPLL_CLKCTRL_OFFSET
);
361 v
|= BIT(ADPLL_CLKCTRL_IDLE
);
362 writel_relaxed(v
, d
->regs
+ ADPLL_CLKCTRL_OFFSET
);
363 spin_unlock_irqrestore(&d
->lock
, flags
);
366 static void ti_adpll_clear_idle_bypass(struct ti_adpll_data
*d
)
371 spin_lock_irqsave(&d
->lock
, flags
);
372 v
= readl_relaxed(d
->regs
+ ADPLL_CLKCTRL_OFFSET
);
373 v
&= ~BIT(ADPLL_CLKCTRL_IDLE
);
374 writel_relaxed(v
, d
->regs
+ ADPLL_CLKCTRL_OFFSET
);
375 spin_unlock_irqrestore(&d
->lock
, flags
);
378 static bool ti_adpll_clock_is_bypass(struct ti_adpll_data
*d
)
382 v
= readl_relaxed(d
->regs
+ ADPLL_STATUS_OFFSET
);
384 return v
& BIT(ADPLL_STATUS_BYPASS
);
388 * Locked and bypass are not actually mutually exclusive: if you only care
389 * about the DCO clock and not CLKOUT you can clear M2PWDNZ before enabling
390 * the PLL, resulting in status (FREQLOCK | PHASELOCK | BYPASS) after lock.
392 static bool ti_adpll_is_locked(struct ti_adpll_data
*d
)
394 u32 v
= readl_relaxed(d
->regs
+ ADPLL_STATUS_OFFSET
);
396 return (v
& ADPLL_STATUS_PREPARED_MASK
) == ADPLL_STATUS_PREPARED_MASK
;
399 static int ti_adpll_wait_lock(struct ti_adpll_data
*d
)
401 int retries
= ADPLL_MAX_RETRIES
;
404 if (ti_adpll_is_locked(d
))
406 usleep_range(200, 300);
409 dev_err(d
->dev
, "pll failed to lock\n");
413 static int ti_adpll_prepare(struct clk_hw
*hw
)
415 struct ti_adpll_dco_data
*dco
= to_dco(hw
);
416 struct ti_adpll_data
*d
= to_adpll(dco
);
418 ti_adpll_clear_idle_bypass(d
);
419 ti_adpll_wait_lock(d
);
424 static void ti_adpll_unprepare(struct clk_hw
*hw
)
426 struct ti_adpll_dco_data
*dco
= to_dco(hw
);
427 struct ti_adpll_data
*d
= to_adpll(dco
);
429 ti_adpll_set_idle_bypass(d
);
432 static int ti_adpll_is_prepared(struct clk_hw
*hw
)
434 struct ti_adpll_dco_data
*dco
= to_dco(hw
);
435 struct ti_adpll_data
*d
= to_adpll(dco
);
437 return ti_adpll_is_locked(d
);
441 * Note that the DCO clock is never subject to bypass: if the PLL is off,
444 static unsigned long ti_adpll_recalc_rate(struct clk_hw
*hw
,
445 unsigned long parent_rate
)
447 struct ti_adpll_dco_data
*dco
= to_dco(hw
);
448 struct ti_adpll_data
*d
= to_adpll(dco
);
449 u32 frac_m
, divider
, v
;
453 if (ti_adpll_clock_is_bypass(d
))
456 spin_lock_irqsave(&d
->lock
, flags
);
457 frac_m
= readl_relaxed(d
->regs
+ ADPLL_FRACDIV_OFFSET
);
458 frac_m
&= ADPLL_FRACDIV_FRACTIONALM_MASK
;
459 rate
= (u64
)readw_relaxed(d
->regs
+ ADPLL_MN2DIV_OFFSET
) << 18;
462 divider
= (readw_relaxed(d
->regs
+ ADPLL_M2NDIV_OFFSET
) + 1) << 18;
463 spin_unlock_irqrestore(&d
->lock
, flags
);
465 do_div(rate
, divider
);
467 if (d
->c
->is_type_s
) {
468 v
= readl_relaxed(d
->regs
+ ADPLL_CLKCTRL_OFFSET
);
469 if (v
& BIT(ADPLL_CLKCTRL_REGM4XEN_ADPLL_S
))
477 /* PLL parent is always clkinp, bypass only affects the children */
478 static u8
ti_adpll_get_parent(struct clk_hw
*hw
)
483 static const struct clk_ops ti_adpll_ops
= {
484 .prepare
= ti_adpll_prepare
,
485 .unprepare
= ti_adpll_unprepare
,
486 .is_prepared
= ti_adpll_is_prepared
,
487 .recalc_rate
= ti_adpll_recalc_rate
,
488 .get_parent
= ti_adpll_get_parent
,
491 static int ti_adpll_init_dco(struct ti_adpll_data
*d
)
493 struct clk_init_data init
;
498 d
->outputs
.clks
= devm_kcalloc(d
->dev
,
500 sizeof(struct clk
*),
502 if (!d
->outputs
.clks
)
505 if (d
->c
->output_index
< 0)
510 init
.name
= ti_adpll_clk_get_name(d
, d
->c
->output_index
, postfix
);
514 init
.parent_names
= d
->parent_names
;
515 init
.num_parents
= d
->c
->nr_max_inputs
;
516 init
.ops
= &ti_adpll_ops
;
517 init
.flags
= CLK_GET_RATE_NOCACHE
;
518 d
->dco
.hw
.init
= &init
;
525 /* Internal input clock divider N2 */
526 err
= ti_adpll_init_divider(d
, TI_ADPLL_N2
, -ENODEV
, "n2",
527 d
->parent_clocks
[TI_ADPLL_CLKINP
],
528 d
->regs
+ ADPLL_MN2DIV_OFFSET
,
529 ADPLL_MN2DIV_N2
, width
, 0);
533 clock
= devm_clk_register(d
->dev
, &d
->dco
.hw
);
535 return PTR_ERR(clock
);
537 return ti_adpll_setup_clock(d
, clock
, TI_ADPLL_DCO
, d
->c
->output_index
,
541 static int ti_adpll_clkout_enable(struct clk_hw
*hw
)
543 struct ti_adpll_clkout_data
*co
= to_clkout(hw
);
544 struct clk_hw
*gate_hw
= &co
->gate
.hw
;
546 __clk_hw_set_clk(gate_hw
, hw
);
548 return clk_gate_ops
.enable(gate_hw
);
551 static void ti_adpll_clkout_disable(struct clk_hw
*hw
)
553 struct ti_adpll_clkout_data
*co
= to_clkout(hw
);
554 struct clk_hw
*gate_hw
= &co
->gate
.hw
;
556 __clk_hw_set_clk(gate_hw
, hw
);
557 clk_gate_ops
.disable(gate_hw
);
560 static int ti_adpll_clkout_is_enabled(struct clk_hw
*hw
)
562 struct ti_adpll_clkout_data
*co
= to_clkout(hw
);
563 struct clk_hw
*gate_hw
= &co
->gate
.hw
;
565 __clk_hw_set_clk(gate_hw
, hw
);
567 return clk_gate_ops
.is_enabled(gate_hw
);
570 /* Setting PLL bypass puts clkout and clkoutx2 into bypass */
571 static u8
ti_adpll_clkout_get_parent(struct clk_hw
*hw
)
573 struct ti_adpll_clkout_data
*co
= to_clkout(hw
);
574 struct ti_adpll_data
*d
= co
->adpll
;
576 return ti_adpll_clock_is_bypass(d
);
579 static int ti_adpll_init_clkout(struct ti_adpll_data
*d
,
580 enum ti_adpll_clocks index
,
581 int output_index
, int gate_bit
,
582 char *name
, struct clk
*clk0
,
585 struct ti_adpll_clkout_data
*co
;
586 struct clk_init_data init
;
588 const char *parent_names
[2];
589 const char *child_name
;
593 co
= devm_kzalloc(d
->dev
, sizeof(*co
), GFP_KERNEL
);
598 err
= of_property_read_string_index(d
->np
,
599 "clock-output-names",
605 ops
= devm_kzalloc(d
->dev
, sizeof(*ops
), GFP_KERNEL
);
609 init
.name
= child_name
;
613 parent_names
[0] = __clk_get_name(clk0
);
614 parent_names
[1] = __clk_get_name(clk1
);
615 init
.parent_names
= parent_names
;
616 init
.num_parents
= 2;
618 ops
->get_parent
= ti_adpll_clkout_get_parent
;
619 ops
->determine_rate
= __clk_mux_determine_rate
;
621 co
->gate
.lock
= &d
->lock
;
622 co
->gate
.reg
= d
->regs
+ ADPLL_CLKCTRL_OFFSET
;
623 co
->gate
.bit_idx
= gate_bit
;
624 ops
->enable
= ti_adpll_clkout_enable
;
625 ops
->disable
= ti_adpll_clkout_disable
;
626 ops
->is_enabled
= ti_adpll_clkout_is_enabled
;
629 clock
= devm_clk_register(d
->dev
, &co
->hw
);
631 dev_err(d
->dev
, "failed to register output %s: %li\n",
632 name
, PTR_ERR(clock
));
633 return PTR_ERR(clock
);
636 return ti_adpll_setup_clock(d
, clock
, index
, output_index
, child_name
,
640 static int ti_adpll_init_children_adpll_s(struct ti_adpll_data
*d
)
644 if (!d
->c
->is_type_s
)
647 /* Internal mux, sources from divider N2 or clkinpulow */
648 err
= ti_adpll_init_mux(d
, TI_ADPLL_BYPASS
, "bypass",
649 d
->clocks
[TI_ADPLL_N2
].clk
,
650 d
->parent_clocks
[TI_ADPLL_CLKINPULOW
],
651 d
->regs
+ ADPLL_CLKCTRL_OFFSET
,
652 ADPLL_CLKCTRL_ULOWCLKEN
);
656 /* Internal divider M2, sources DCO */
657 err
= ti_adpll_init_divider(d
, TI_ADPLL_M2
, -ENODEV
, "m2",
658 d
->clocks
[TI_ADPLL_DCO
].clk
,
659 d
->regs
+ ADPLL_M2NDIV_OFFSET
,
661 ADPLL_M2NDIV_M2_ADPLL_S_WIDTH
,
662 CLK_DIVIDER_ONE_BASED
);
666 /* Internal fixed divider, after M2 before clkout */
667 err
= ti_adpll_init_fixed_factor(d
, TI_ADPLL_DIV2
, "div2",
668 d
->clocks
[TI_ADPLL_M2
].clk
,
673 /* Output clkout with a mux and gate, sources from div2 or bypass */
674 err
= ti_adpll_init_clkout(d
, TI_ADPLL_CLKOUT
, TI_ADPLL_S_CLKOUT
,
675 ADPLL_CLKCTRL_CLKOUTEN
, "clkout",
676 d
->clocks
[TI_ADPLL_DIV2
].clk
,
677 d
->clocks
[TI_ADPLL_BYPASS
].clk
);
681 /* Output clkoutx2 with a mux and gate, sources from M2 or bypass */
682 err
= ti_adpll_init_clkout(d
, TI_ADPLL_CLKOUT2
, TI_ADPLL_S_CLKOUTX2
, 0,
683 "clkout2", d
->clocks
[TI_ADPLL_M2
].clk
,
684 d
->clocks
[TI_ADPLL_BYPASS
].clk
);
688 /* Internal mux, sources from DCO and clkinphif */
689 if (d
->parent_clocks
[TI_ADPLL_CLKINPHIF
]) {
690 err
= ti_adpll_init_mux(d
, TI_ADPLL_HIF
, "hif",
691 d
->clocks
[TI_ADPLL_DCO
].clk
,
692 d
->parent_clocks
[TI_ADPLL_CLKINPHIF
],
693 d
->regs
+ ADPLL_CLKCTRL_OFFSET
,
694 ADPLL_CLKINPHIFSEL_ADPLL_S
);
699 /* Output clkouthif with a divider M3, sources from hif */
700 err
= ti_adpll_init_divider(d
, TI_ADPLL_M3
, TI_ADPLL_S_CLKOUTHIF
, "m3",
701 d
->clocks
[TI_ADPLL_HIF
].clk
,
702 d
->regs
+ ADPLL_M3DIV_OFFSET
,
704 ADPLL_M3DIV_M3_WIDTH
,
705 CLK_DIVIDER_ONE_BASED
);
709 /* Output clock dcoclkldo is the DCO */
714 static int ti_adpll_init_children_adpll_lj(struct ti_adpll_data
*d
)
721 /* Output clkdcoldo, gated output of DCO */
722 err
= ti_adpll_init_gate(d
, TI_ADPLL_DCO_GATE
, TI_ADPLL_LJ_CLKDCOLDO
,
723 "clkdcoldo", d
->clocks
[TI_ADPLL_DCO
].clk
,
724 d
->regs
+ ADPLL_CLKCTRL_OFFSET
,
725 ADPLL_CLKCTRL_CLKDCOLDOEN
, 0);
729 /* Internal divider M2, sources from DCO */
730 err
= ti_adpll_init_divider(d
, TI_ADPLL_M2
, -ENODEV
,
731 "m2", d
->clocks
[TI_ADPLL_DCO
].clk
,
732 d
->regs
+ ADPLL_M2NDIV_OFFSET
,
734 ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH
,
735 CLK_DIVIDER_ONE_BASED
);
739 /* Output clkoutldo, gated output of M2 */
740 err
= ti_adpll_init_gate(d
, TI_ADPLL_M2_GATE
, TI_ADPLL_LJ_CLKOUTLDO
,
741 "clkoutldo", d
->clocks
[TI_ADPLL_M2
].clk
,
742 d
->regs
+ ADPLL_CLKCTRL_OFFSET
,
743 ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ
,
748 /* Internal mux, sources from divider N2 or clkinpulow */
749 err
= ti_adpll_init_mux(d
, TI_ADPLL_BYPASS
, "bypass",
750 d
->clocks
[TI_ADPLL_N2
].clk
,
751 d
->parent_clocks
[TI_ADPLL_CLKINPULOW
],
752 d
->regs
+ ADPLL_CLKCTRL_OFFSET
,
753 ADPLL_CLKCTRL_ULOWCLKEN
);
757 /* Output clkout, sources M2 or bypass */
758 err
= ti_adpll_init_clkout(d
, TI_ADPLL_CLKOUT
, TI_ADPLL_S_CLKOUT
,
759 ADPLL_CLKCTRL_CLKOUTEN
, "clkout",
760 d
->clocks
[TI_ADPLL_M2
].clk
,
761 d
->clocks
[TI_ADPLL_BYPASS
].clk
);
768 static void ti_adpll_free_resources(struct ti_adpll_data
*d
)
772 for (i
= TI_ADPLL_M3
; i
>= 0; i
--) {
773 struct ti_adpll_clock
*ac
= &d
->clocks
[i
];
775 if (!ac
|| IS_ERR_OR_NULL(ac
->clk
))
780 ac
->unregister(ac
->clk
);
784 /* MPU PLL manages the lock register for all PLLs */
785 static void ti_adpll_unlock_all(void __iomem
*reg
)
789 v
= readl_relaxed(reg
);
790 if (v
== ADPLL_PLLSS_MMR_LOCK_ENABLED
)
791 writel_relaxed(ADPLL_PLLSS_MMR_UNLOCK_MAGIC
, reg
);
794 static int ti_adpll_init_registers(struct ti_adpll_data
*d
)
796 int register_offset
= 0;
798 if (d
->c
->is_type_s
) {
800 ti_adpll_unlock_all(d
->iobase
+ ADPLL_PLLSS_MMR_LOCK_OFFSET
);
803 d
->regs
= d
->iobase
+ register_offset
+ ADPLL_PWRCTRL_OFFSET
;
808 static int ti_adpll_init_inputs(struct ti_adpll_data
*d
)
810 const char *error
= "need at least %i inputs";
814 nr_inputs
= of_clk_get_parent_count(d
->np
);
815 if (nr_inputs
< d
->c
->nr_max_inputs
) {
816 dev_err(d
->dev
, error
, nr_inputs
);
819 of_clk_parent_fill(d
->np
, d
->parent_names
, nr_inputs
);
821 clock
= devm_clk_get(d
->dev
, d
->parent_names
[0]);
823 dev_err(d
->dev
, "could not get clkinp\n");
824 return PTR_ERR(clock
);
826 d
->parent_clocks
[TI_ADPLL_CLKINP
] = clock
;
828 clock
= devm_clk_get(d
->dev
, d
->parent_names
[1]);
830 dev_err(d
->dev
, "could not get clkinpulow clock\n");
831 return PTR_ERR(clock
);
833 d
->parent_clocks
[TI_ADPLL_CLKINPULOW
] = clock
;
835 if (d
->c
->is_type_s
) {
836 clock
= devm_clk_get(d
->dev
, d
->parent_names
[2]);
838 dev_err(d
->dev
, "could not get clkinphif clock\n");
839 return PTR_ERR(clock
);
841 d
->parent_clocks
[TI_ADPLL_CLKINPHIF
] = clock
;
847 static const struct ti_adpll_platform_data ti_adpll_type_s
= {
849 .nr_max_inputs
= MAX_ADPLL_INPUTS
,
850 .nr_max_outputs
= MAX_ADPLL_OUTPUTS
,
851 .output_index
= TI_ADPLL_S_DCOCLKLDO
,
854 static const struct ti_adpll_platform_data ti_adpll_type_lj
= {
856 .nr_max_inputs
= MAX_ADPLL_INPUTS
- 1,
857 .nr_max_outputs
= MAX_ADPLL_OUTPUTS
- 1,
858 .output_index
= -EINVAL
,
861 static const struct of_device_id ti_adpll_match
[] = {
862 { .compatible
= "ti,dm814-adpll-s-clock", &ti_adpll_type_s
},
863 { .compatible
= "ti,dm814-adpll-lj-clock", &ti_adpll_type_lj
},
866 MODULE_DEVICE_TABLE(of
, ti_adpll_match
);
868 static int ti_adpll_probe(struct platform_device
*pdev
)
870 struct device_node
*node
= pdev
->dev
.of_node
;
871 struct device
*dev
= &pdev
->dev
;
872 const struct of_device_id
*match
;
873 const struct ti_adpll_platform_data
*pdata
;
874 struct ti_adpll_data
*d
;
875 struct resource
*res
;
878 match
= of_match_device(ti_adpll_match
, dev
);
884 d
= devm_kzalloc(dev
, sizeof(*d
), GFP_KERNEL
);
890 dev_set_drvdata(d
->dev
, d
);
891 spin_lock_init(&d
->lock
);
893 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
898 d
->iobase
= devm_ioremap_resource(dev
, res
);
899 if (IS_ERR(d
->iobase
)) {
900 dev_err(dev
, "could not get IO base: %li\n",
902 return PTR_ERR(d
->iobase
);
905 err
= ti_adpll_init_registers(d
);
909 err
= ti_adpll_init_inputs(d
);
913 d
->clocks
= devm_kcalloc(d
->dev
,
915 sizeof(struct ti_adpll_clock
),
920 err
= ti_adpll_init_dco(d
);
922 dev_err(dev
, "could not register dco: %i\n", err
);
926 err
= ti_adpll_init_children_adpll_s(d
);
929 err
= ti_adpll_init_children_adpll_lj(d
);
933 err
= of_clk_add_provider(d
->np
, of_clk_src_onecell_get
, &d
->outputs
);
941 ti_adpll_free_resources(d
);
946 static int ti_adpll_remove(struct platform_device
*pdev
)
948 struct ti_adpll_data
*d
= dev_get_drvdata(&pdev
->dev
);
950 ti_adpll_free_resources(d
);
955 static struct platform_driver ti_adpll_driver
= {
958 .of_match_table
= ti_adpll_match
,
960 .probe
= ti_adpll_probe
,
961 .remove
= ti_adpll_remove
,
964 static int __init
ti_adpll_init(void)
966 return platform_driver_register(&ti_adpll_driver
);
968 core_initcall(ti_adpll_init
);
970 static void __exit
ti_adpll_exit(void)
972 platform_driver_unregister(&ti_adpll_driver
);
974 module_exit(ti_adpll_exit
);
976 MODULE_DESCRIPTION("Clock driver for dm814x ADPLL");
977 MODULE_ALIAS("platform:dm814-adpll-clock");
978 MODULE_AUTHOR("Tony LIndgren <tony@atomide.com>");
979 MODULE_LICENSE("GPL v2");