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 const char *base_name = "adpll";
200 buf = devm_kzalloc(d->dev, 8 + 1 + strlen(base_name) + 1 +
201 strlen(postfix), GFP_KERNEL);
204 sprintf(buf, "%08lx.%s.%s", d->pa, base_name, postfix);
211 #define ADPLL_MAX_CON_ID 16 /* See MAX_CON_ID */
213 static int ti_adpll_setup_clock(struct ti_adpll_data *d, struct clk *clock,
214 int index, int output_index, const char *name,
215 void (*unregister)(struct clk *clk))
217 struct clk_lookup *cl;
218 const char *postfix = NULL;
219 char con_id[ADPLL_MAX_CON_ID];
221 d->clocks[index].clk = clock;
222 d->clocks[index].unregister = unregister;
224 /* Separate con_id in format "pll040dcoclkldo" to fit MAX_CON_ID */
225 postfix = strrchr(name, '.');
226 if (postfix && strlen(postfix) > 1) {
227 if (strlen(postfix) > ADPLL_MAX_CON_ID)
228 dev_warn(d->dev, "clock %s con_id lookup may fail\n",
230 snprintf(con_id, 16, "pll%03lx%s", d->pa & 0xfff, postfix + 1);
231 cl = clkdev_create(clock, con_id, NULL);
234 d->clocks[index].cl = cl;
236 dev_warn(d->dev, "no con_id for clock %s\n", name);
239 if (output_index < 0)
242 d->outputs.clks[output_index] = clock;
243 d->outputs.clk_num++;
248 static int ti_adpll_init_divider(struct ti_adpll_data *d,
249 enum ti_adpll_clocks index,
250 int output_index, char *name,
251 struct clk *parent_clock,
254 u8 clk_divider_flags)
256 const char *child_name;
257 const char *parent_name;
260 child_name = ti_adpll_clk_get_name(d, output_index, name);
264 parent_name = __clk_get_name(parent_clock);
265 clock = clk_register_divider(d->dev, child_name, parent_name, 0,
266 reg, shift, width, clk_divider_flags,
269 dev_err(d->dev, "failed to register divider %s: %li\n",
270 name, PTR_ERR(clock));
271 return PTR_ERR(clock);
274 return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
275 clk_unregister_divider);
278 static int ti_adpll_init_mux(struct ti_adpll_data *d,
279 enum ti_adpll_clocks index,
280 char *name, struct clk *clk0,
285 const char *child_name;
286 const char *parents[2];
289 child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
292 parents[0] = __clk_get_name(clk0);
293 parents[1] = __clk_get_name(clk1);
294 clock = clk_register_mux(d->dev, child_name, parents, 2, 0,
295 reg, shift, 1, 0, &d->lock);
297 dev_err(d->dev, "failed to register mux %s: %li\n",
298 name, PTR_ERR(clock));
299 return PTR_ERR(clock);
302 return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
306 static int ti_adpll_init_gate(struct ti_adpll_data *d,
307 enum ti_adpll_clocks index,
308 int output_index, char *name,
309 struct clk *parent_clock,
314 const char *child_name;
315 const char *parent_name;
318 child_name = ti_adpll_clk_get_name(d, output_index, name);
322 parent_name = __clk_get_name(parent_clock);
323 clock = clk_register_gate(d->dev, child_name, parent_name, 0,
324 reg, bit_idx, clk_gate_flags,
327 dev_err(d->dev, "failed to register gate %s: %li\n",
328 name, PTR_ERR(clock));
329 return PTR_ERR(clock);
332 return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
333 clk_unregister_gate);
336 static int ti_adpll_init_fixed_factor(struct ti_adpll_data *d,
337 enum ti_adpll_clocks index,
339 struct clk *parent_clock,
343 const char *child_name;
344 const char *parent_name;
347 child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
351 parent_name = __clk_get_name(parent_clock);
352 clock = clk_register_fixed_factor(d->dev, child_name, parent_name,
355 return PTR_ERR(clock);
357 return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
361 static void ti_adpll_set_idle_bypass(struct ti_adpll_data *d)
366 spin_lock_irqsave(&d->lock, flags);
367 v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
368 v |= BIT(ADPLL_CLKCTRL_IDLE);
369 writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
370 spin_unlock_irqrestore(&d->lock, flags);
373 static void ti_adpll_clear_idle_bypass(struct ti_adpll_data *d)
378 spin_lock_irqsave(&d->lock, flags);
379 v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
380 v &= ~BIT(ADPLL_CLKCTRL_IDLE);
381 writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
382 spin_unlock_irqrestore(&d->lock, flags);
385 static bool ti_adpll_clock_is_bypass(struct ti_adpll_data *d)
389 v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
391 return v & BIT(ADPLL_STATUS_BYPASS);
395 * Locked and bypass are not actually mutually exclusive: if you only care
396 * about the DCO clock and not CLKOUT you can clear M2PWDNZ before enabling
397 * the PLL, resulting in status (FREQLOCK | PHASELOCK | BYPASS) after lock.
399 static bool ti_adpll_is_locked(struct ti_adpll_data *d)
401 u32 v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
403 return (v & ADPLL_STATUS_PREPARED_MASK) == ADPLL_STATUS_PREPARED_MASK;
406 static int ti_adpll_wait_lock(struct ti_adpll_data *d)
408 int retries = ADPLL_MAX_RETRIES;
411 if (ti_adpll_is_locked(d))
413 usleep_range(200, 300);
416 dev_err(d->dev, "pll failed to lock\n");
420 static int ti_adpll_prepare(struct clk_hw *hw)
422 struct ti_adpll_dco_data *dco = to_dco(hw);
423 struct ti_adpll_data *d = to_adpll(dco);
425 ti_adpll_clear_idle_bypass(d);
426 ti_adpll_wait_lock(d);
431 static void ti_adpll_unprepare(struct clk_hw *hw)
433 struct ti_adpll_dco_data *dco = to_dco(hw);
434 struct ti_adpll_data *d = to_adpll(dco);
436 ti_adpll_set_idle_bypass(d);
439 static int ti_adpll_is_prepared(struct clk_hw *hw)
441 struct ti_adpll_dco_data *dco = to_dco(hw);
442 struct ti_adpll_data *d = to_adpll(dco);
444 return ti_adpll_is_locked(d);
448 * Note that the DCO clock is never subject to bypass: if the PLL is off,
451 static unsigned long ti_adpll_recalc_rate(struct clk_hw *hw,
452 unsigned long parent_rate)
454 struct ti_adpll_dco_data *dco = to_dco(hw);
455 struct ti_adpll_data *d = to_adpll(dco);
456 u32 frac_m, divider, v;
460 if (ti_adpll_clock_is_bypass(d))
463 spin_lock_irqsave(&d->lock, flags);
464 frac_m = readl_relaxed(d->regs + ADPLL_FRACDIV_OFFSET);
465 frac_m &= ADPLL_FRACDIV_FRACTIONALM_MASK;
466 rate = (u64)readw_relaxed(d->regs + ADPLL_MN2DIV_OFFSET) << 18;
469 divider = (readw_relaxed(d->regs + ADPLL_M2NDIV_OFFSET) + 1) << 18;
470 spin_unlock_irqrestore(&d->lock, flags);
472 do_div(rate, divider);
474 if (d->c->is_type_s) {
475 v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
476 if (v & BIT(ADPLL_CLKCTRL_REGM4XEN_ADPLL_S))
484 /* PLL parent is always clkinp, bypass only affects the children */
485 static u8 ti_adpll_get_parent(struct clk_hw *hw)
490 static const struct clk_ops ti_adpll_ops = {
491 .prepare = ti_adpll_prepare,
492 .unprepare = ti_adpll_unprepare,
493 .is_prepared = ti_adpll_is_prepared,
494 .recalc_rate = ti_adpll_recalc_rate,
495 .get_parent = ti_adpll_get_parent,
498 static int ti_adpll_init_dco(struct ti_adpll_data *d)
500 struct clk_init_data init;
505 d->outputs.clks = devm_kcalloc(d->dev,
507 sizeof(struct clk *),
509 if (!d->outputs.clks)
512 if (d->c->output_index < 0)
517 init.name = ti_adpll_clk_get_name(d, d->c->output_index, postfix);
521 init.parent_names = d->parent_names;
522 init.num_parents = d->c->nr_max_inputs;
523 init.ops = &ti_adpll_ops;
524 init.flags = CLK_GET_RATE_NOCACHE;
525 d->dco.hw.init = &init;
532 /* Internal input clock divider N2 */
533 err = ti_adpll_init_divider(d, TI_ADPLL_N2, -ENODEV, "n2",
534 d->parent_clocks[TI_ADPLL_CLKINP],
535 d->regs + ADPLL_MN2DIV_OFFSET,
536 ADPLL_MN2DIV_N2, width, 0);
540 clock = devm_clk_register(d->dev, &d->dco.hw);
542 return PTR_ERR(clock);
544 return ti_adpll_setup_clock(d, clock, TI_ADPLL_DCO, d->c->output_index,
548 static int ti_adpll_clkout_enable(struct clk_hw *hw)
550 struct ti_adpll_clkout_data *co = to_clkout(hw);
551 struct clk_hw *gate_hw = &co->gate.hw;
553 __clk_hw_set_clk(gate_hw, hw);
555 return clk_gate_ops.enable(gate_hw);
558 static void ti_adpll_clkout_disable(struct clk_hw *hw)
560 struct ti_adpll_clkout_data *co = to_clkout(hw);
561 struct clk_hw *gate_hw = &co->gate.hw;
563 __clk_hw_set_clk(gate_hw, hw);
564 clk_gate_ops.disable(gate_hw);
567 static int ti_adpll_clkout_is_enabled(struct clk_hw *hw)
569 struct ti_adpll_clkout_data *co = to_clkout(hw);
570 struct clk_hw *gate_hw = &co->gate.hw;
572 __clk_hw_set_clk(gate_hw, hw);
574 return clk_gate_ops.is_enabled(gate_hw);
577 /* Setting PLL bypass puts clkout and clkoutx2 into bypass */
578 static u8 ti_adpll_clkout_get_parent(struct clk_hw *hw)
580 struct ti_adpll_clkout_data *co = to_clkout(hw);
581 struct ti_adpll_data *d = co->adpll;
583 return ti_adpll_clock_is_bypass(d);
586 static int ti_adpll_init_clkout(struct ti_adpll_data *d,
587 enum ti_adpll_clocks index,
588 int output_index, int gate_bit,
589 char *name, struct clk *clk0,
592 struct ti_adpll_clkout_data *co;
593 struct clk_init_data init;
595 const char *parent_names[2];
596 const char *child_name;
600 co = devm_kzalloc(d->dev, sizeof(*co), GFP_KERNEL);
605 err = of_property_read_string_index(d->np,
606 "clock-output-names",
612 ops = devm_kzalloc(d->dev, sizeof(*ops), GFP_KERNEL);
616 init.name = child_name;
620 parent_names[0] = __clk_get_name(clk0);
621 parent_names[1] = __clk_get_name(clk1);
622 init.parent_names = parent_names;
623 init.num_parents = 2;
625 ops->get_parent = ti_adpll_clkout_get_parent;
626 ops->determine_rate = __clk_mux_determine_rate;
628 co->gate.lock = &d->lock;
629 co->gate.reg = d->regs + ADPLL_CLKCTRL_OFFSET;
630 co->gate.bit_idx = gate_bit;
631 ops->enable = ti_adpll_clkout_enable;
632 ops->disable = ti_adpll_clkout_disable;
633 ops->is_enabled = ti_adpll_clkout_is_enabled;
636 clock = devm_clk_register(d->dev, &co->hw);
638 dev_err(d->dev, "failed to register output %s: %li\n",
639 name, PTR_ERR(clock));
640 return PTR_ERR(clock);
643 return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
647 static int ti_adpll_init_children_adpll_s(struct ti_adpll_data *d)
651 if (!d->c->is_type_s)
654 /* Internal mux, sources from divider N2 or clkinpulow */
655 err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
656 d->clocks[TI_ADPLL_N2].clk,
657 d->parent_clocks[TI_ADPLL_CLKINPULOW],
658 d->regs + ADPLL_CLKCTRL_OFFSET,
659 ADPLL_CLKCTRL_ULOWCLKEN);
663 /* Internal divider M2, sources DCO */
664 err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV, "m2",
665 d->clocks[TI_ADPLL_DCO].clk,
666 d->regs + ADPLL_M2NDIV_OFFSET,
668 ADPLL_M2NDIV_M2_ADPLL_S_WIDTH,
669 CLK_DIVIDER_ONE_BASED);
673 /* Internal fixed divider, after M2 before clkout */
674 err = ti_adpll_init_fixed_factor(d, TI_ADPLL_DIV2, "div2",
675 d->clocks[TI_ADPLL_M2].clk,
680 /* Output clkout with a mux and gate, sources from div2 or bypass */
681 err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
682 ADPLL_CLKCTRL_CLKOUTEN, "clkout",
683 d->clocks[TI_ADPLL_DIV2].clk,
684 d->clocks[TI_ADPLL_BYPASS].clk);
688 /* Output clkoutx2 with a mux and gate, sources from M2 or bypass */
689 err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT2, TI_ADPLL_S_CLKOUTX2, 0,
690 "clkout2", d->clocks[TI_ADPLL_M2].clk,
691 d->clocks[TI_ADPLL_BYPASS].clk);
695 /* Internal mux, sources from DCO and clkinphif */
696 if (d->parent_clocks[TI_ADPLL_CLKINPHIF]) {
697 err = ti_adpll_init_mux(d, TI_ADPLL_HIF, "hif",
698 d->clocks[TI_ADPLL_DCO].clk,
699 d->parent_clocks[TI_ADPLL_CLKINPHIF],
700 d->regs + ADPLL_CLKCTRL_OFFSET,
701 ADPLL_CLKINPHIFSEL_ADPLL_S);
706 /* Output clkouthif with a divider M3, sources from hif */
707 err = ti_adpll_init_divider(d, TI_ADPLL_M3, TI_ADPLL_S_CLKOUTHIF, "m3",
708 d->clocks[TI_ADPLL_HIF].clk,
709 d->regs + ADPLL_M3DIV_OFFSET,
711 ADPLL_M3DIV_M3_WIDTH,
712 CLK_DIVIDER_ONE_BASED);
716 /* Output clock dcoclkldo is the DCO */
721 static int ti_adpll_init_children_adpll_lj(struct ti_adpll_data *d)
728 /* Output clkdcoldo, gated output of DCO */
729 err = ti_adpll_init_gate(d, TI_ADPLL_DCO_GATE, TI_ADPLL_LJ_CLKDCOLDO,
730 "clkdcoldo", d->clocks[TI_ADPLL_DCO].clk,
731 d->regs + ADPLL_CLKCTRL_OFFSET,
732 ADPLL_CLKCTRL_CLKDCOLDOEN, 0);
736 /* Internal divider M2, sources from DCO */
737 err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV,
738 "m2", d->clocks[TI_ADPLL_DCO].clk,
739 d->regs + ADPLL_M2NDIV_OFFSET,
741 ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH,
742 CLK_DIVIDER_ONE_BASED);
746 /* Output clkoutldo, gated output of M2 */
747 err = ti_adpll_init_gate(d, TI_ADPLL_M2_GATE, TI_ADPLL_LJ_CLKOUTLDO,
748 "clkoutldo", d->clocks[TI_ADPLL_M2].clk,
749 d->regs + ADPLL_CLKCTRL_OFFSET,
750 ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ,
755 /* Internal mux, sources from divider N2 or clkinpulow */
756 err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
757 d->clocks[TI_ADPLL_N2].clk,
758 d->parent_clocks[TI_ADPLL_CLKINPULOW],
759 d->regs + ADPLL_CLKCTRL_OFFSET,
760 ADPLL_CLKCTRL_ULOWCLKEN);
764 /* Output clkout, sources M2 or bypass */
765 err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
766 ADPLL_CLKCTRL_CLKOUTEN, "clkout",
767 d->clocks[TI_ADPLL_M2].clk,
768 d->clocks[TI_ADPLL_BYPASS].clk);
775 static void ti_adpll_free_resources(struct ti_adpll_data *d)
779 for (i = TI_ADPLL_M3; i >= 0; i--) {
780 struct ti_adpll_clock *ac = &d->clocks[i];
782 if (!ac || IS_ERR_OR_NULL(ac->clk))
787 ac->unregister(ac->clk);
791 /* MPU PLL manages the lock register for all PLLs */
792 static void ti_adpll_unlock_all(void __iomem *reg)
796 v = readl_relaxed(reg);
797 if (v == ADPLL_PLLSS_MMR_LOCK_ENABLED)
798 writel_relaxed(ADPLL_PLLSS_MMR_UNLOCK_MAGIC, reg);
801 static int ti_adpll_init_registers(struct ti_adpll_data *d)
803 int register_offset = 0;
805 if (d->c->is_type_s) {
807 ti_adpll_unlock_all(d->iobase + ADPLL_PLLSS_MMR_LOCK_OFFSET);
810 d->regs = d->iobase + register_offset + ADPLL_PWRCTRL_OFFSET;
815 static int ti_adpll_init_inputs(struct ti_adpll_data *d)
817 const char *error = "need at least %i inputs";
821 nr_inputs = of_clk_get_parent_count(d->np);
822 if (nr_inputs < d->c->nr_max_inputs) {
823 dev_err(d->dev, error, nr_inputs);
826 of_clk_parent_fill(d->np, d->parent_names, nr_inputs);
828 clock = devm_clk_get(d->dev, d->parent_names[0]);
830 dev_err(d->dev, "could not get clkinp\n");
831 return PTR_ERR(clock);
833 d->parent_clocks[TI_ADPLL_CLKINP] = clock;
835 clock = devm_clk_get(d->dev, d->parent_names[1]);
837 dev_err(d->dev, "could not get clkinpulow clock\n");
838 return PTR_ERR(clock);
840 d->parent_clocks[TI_ADPLL_CLKINPULOW] = clock;
842 if (d->c->is_type_s) {
843 clock = devm_clk_get(d->dev, d->parent_names[2]);
845 dev_err(d->dev, "could not get clkinphif clock\n");
846 return PTR_ERR(clock);
848 d->parent_clocks[TI_ADPLL_CLKINPHIF] = clock;
854 static const struct ti_adpll_platform_data ti_adpll_type_s = {
856 .nr_max_inputs = MAX_ADPLL_INPUTS,
857 .nr_max_outputs = MAX_ADPLL_OUTPUTS,
858 .output_index = TI_ADPLL_S_DCOCLKLDO,
861 static const struct ti_adpll_platform_data ti_adpll_type_lj = {
863 .nr_max_inputs = MAX_ADPLL_INPUTS - 1,
864 .nr_max_outputs = MAX_ADPLL_OUTPUTS - 1,
865 .output_index = -EINVAL,
868 static const struct of_device_id ti_adpll_match[] = {
869 { .compatible = "ti,dm814-adpll-s-clock", &ti_adpll_type_s },
870 { .compatible = "ti,dm814-adpll-lj-clock", &ti_adpll_type_lj },
873 MODULE_DEVICE_TABLE(of, ti_adpll_match);
875 static int ti_adpll_probe(struct platform_device *pdev)
877 struct device_node *node = pdev->dev.of_node;
878 struct device *dev = &pdev->dev;
879 const struct of_device_id *match;
880 const struct ti_adpll_platform_data *pdata;
881 struct ti_adpll_data *d;
882 struct resource *res;
885 match = of_match_device(ti_adpll_match, dev);
891 d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
897 dev_set_drvdata(d->dev, d);
898 spin_lock_init(&d->lock);
900 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
905 d->iobase = devm_ioremap_resource(dev, res);
906 if (IS_ERR(d->iobase)) {
907 dev_err(dev, "could not get IO base: %li\n",
909 return PTR_ERR(d->iobase);
912 err = ti_adpll_init_registers(d);
916 err = ti_adpll_init_inputs(d);
920 d->clocks = devm_kcalloc(d->dev,
922 sizeof(struct ti_adpll_clock),
927 err = ti_adpll_init_dco(d);
929 dev_err(dev, "could not register dco: %i\n", err);
933 err = ti_adpll_init_children_adpll_s(d);
936 err = ti_adpll_init_children_adpll_lj(d);
940 err = of_clk_add_provider(d->np, of_clk_src_onecell_get, &d->outputs);
948 ti_adpll_free_resources(d);
953 static int ti_adpll_remove(struct platform_device *pdev)
955 struct ti_adpll_data *d = dev_get_drvdata(&pdev->dev);
957 ti_adpll_free_resources(d);
962 static struct platform_driver ti_adpll_driver = {
965 .of_match_table = ti_adpll_match,
967 .probe = ti_adpll_probe,
968 .remove = ti_adpll_remove,
971 static int __init ti_adpll_init(void)
973 return platform_driver_register(&ti_adpll_driver);
975 core_initcall(ti_adpll_init);
977 static void __exit ti_adpll_exit(void)
979 platform_driver_unregister(&ti_adpll_driver);
981 module_exit(ti_adpll_exit);
983 MODULE_DESCRIPTION("Clock driver for dm814x ADPLL");
984 MODULE_ALIAS("platform:dm814-adpll-clock");
986 MODULE_LICENSE("GPL v2");