4 * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
7 #include <linux/clk-provider.h>
9 #include <linux/device.h>
10 #include <linux/of_address.h>
11 #include <linux/slab.h>
13 static DEFINE_SPINLOCK(clklock
);
15 #define MAX_FREQ 33333333
16 #define MIN_FREQ 8000000
24 #define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw)
26 static unsigned long pll_recalc_rate(struct clk_hw
*hw
,
27 unsigned long parent_rate
)
29 struct pll_clock
*pll_clock
= to_pll_clock(hw
);
30 int mul
= 1 << (readb(pll_clock
->pllcr
) & 3);
32 return parent_rate
* mul
;
35 static long pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
46 for (i
= 0; i
< 3; i
++)
47 offset
[i
] = abs(rate
- (*prate
* (1 << i
)));
48 for (i
= 0; i
< 3; i
++)
52 m
= (offset
[i
] < offset
[m
])?i
:m
;
54 return *prate
* (1 << m
);
57 static int pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
58 unsigned long parent_rate
)
63 struct pll_clock
*pll_clock
= to_pll_clock(hw
);
65 pll
= ((rate
/ parent_rate
) / 2) & 0x03;
66 spin_lock_irqsave(&clklock
, flags
);
67 val
= readb(pll_clock
->sckcr
);
69 writeb(val
, pll_clock
->sckcr
);
70 val
= readb(pll_clock
->pllcr
);
73 writeb(val
, pll_clock
->pllcr
);
74 spin_unlock_irqrestore(&clklock
, flags
);
78 static const struct clk_ops pll_ops
= {
79 .recalc_rate
= pll_recalc_rate
,
80 .round_rate
= pll_round_rate
,
81 .set_rate
= pll_set_rate
,
84 static void __init
h8s2678_pll_clk_setup(struct device_node
*node
)
86 unsigned int num_parents
;
87 const char *clk_name
= node
->name
;
88 const char *parent_name
;
89 struct pll_clock
*pll_clock
;
90 struct clk_init_data init
;
93 num_parents
= of_clk_get_parent_count(node
);
95 pr_err("%s: no parent found", clk_name
);
100 pll_clock
= kzalloc(sizeof(*pll_clock
), GFP_KERNEL
);
104 pll_clock
->sckcr
= of_iomap(node
, 0);
105 if (pll_clock
->sckcr
== NULL
) {
106 pr_err("%s: failed to map divide register", clk_name
);
110 pll_clock
->pllcr
= of_iomap(node
, 1);
111 if (pll_clock
->pllcr
== NULL
) {
112 pr_err("%s: failed to map multiply register", clk_name
);
116 parent_name
= of_clk_get_parent_name(node
, 0);
117 init
.name
= clk_name
;
119 init
.flags
= CLK_IS_BASIC
;
120 init
.parent_names
= &parent_name
;
121 init
.num_parents
= 1;
122 pll_clock
->hw
.init
= &init
;
124 ret
= clk_hw_register(NULL
, &pll_clock
->hw
);
126 pr_err("%s: failed to register %s div clock (%d)\n",
127 __func__
, clk_name
, ret
);
131 of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, &pll_clock
->hw
);
135 iounmap(pll_clock
->pllcr
);
137 iounmap(pll_clock
->sckcr
);
142 CLK_OF_DECLARE(h8s2678_div_clk
, "renesas,h8s2678-pll-clock",
143 h8s2678_pll_clk_setup
);