4 * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
8 #include <linux/clkdev.h>
9 #include <linux/clk-provider.h>
10 #include <linux/err.h>
11 #include <linux/device.h>
12 #include <linux/of_address.h>
14 static DEFINE_SPINLOCK(clklock);
16 #define MAX_FREQ 33333333
17 #define MIN_FREQ 8000000
25 #define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw)
27 static unsigned long pll_recalc_rate(struct clk_hw *hw,
28 unsigned long parent_rate)
30 struct pll_clock *pll_clock = to_pll_clock(hw);
31 int mul = 1 << (ctrl_inb((unsigned long)pll_clock->pllcr) & 3);
33 return parent_rate * mul;
36 static long pll_round_rate(struct clk_hw *hw, unsigned long rate,
47 for (i = 0; i < 3; i++)
48 offset[i] = abs(rate - (*prate * (1 << i)));
49 for (i = 0; i < 3; i++)
53 m = (offset[i] < offset[m])?i:m;
55 return *prate * (1 << m);
58 static int pll_set_rate(struct clk_hw *hw, unsigned long rate,
59 unsigned long parent_rate)
64 struct pll_clock *pll_clock = to_pll_clock(hw);
66 pll = ((rate / parent_rate) / 2) & 0x03;
67 spin_lock_irqsave(&clklock, flags);
68 val = ctrl_inb((unsigned long)pll_clock->sckcr);
70 ctrl_outb(val, (unsigned long)pll_clock->sckcr);
71 val = ctrl_inb((unsigned long)pll_clock->pllcr);
74 ctrl_outb(val, (unsigned long)pll_clock->pllcr);
75 spin_unlock_irqrestore(&clklock, flags);
79 static const struct clk_ops pll_ops = {
80 .recalc_rate = pll_recalc_rate,
81 .round_rate = pll_round_rate,
82 .set_rate = pll_set_rate,
85 static void __init h8s2678_pll_clk_setup(struct device_node *node)
87 unsigned int num_parents;
89 const char *clk_name = node->name;
90 const char *parent_name;
91 struct pll_clock *pll_clock;
92 struct clk_init_data init;
94 num_parents = of_clk_get_parent_count(node);
95 if (num_parents < 1) {
96 pr_err("%s: no parent found", clk_name);
101 pll_clock = kzalloc(sizeof(struct pll_clock), GFP_KERNEL);
103 pr_err("%s: failed to alloc memory", clk_name);
107 pll_clock->sckcr = of_iomap(node, 0);
108 if (pll_clock->sckcr == NULL) {
109 pr_err("%s: failed to map divide register", clk_name);
113 pll_clock->pllcr = of_iomap(node, 1);
114 if (pll_clock->pllcr == NULL) {
115 pr_err("%s: failed to map multiply register", clk_name);
119 parent_name = of_clk_get_parent_name(node, 0);
120 init.name = clk_name;
122 init.flags = CLK_IS_BASIC;
123 init.parent_names = &parent_name;
124 init.num_parents = 1;
125 pll_clock->hw.init = &init;
127 clk = clk_register(NULL, &pll_clock->hw);
129 pr_err("%s: failed to register %s div clock (%ld)\n",
130 __func__, clk_name, PTR_ERR(clk));
134 of_clk_add_provider(node, of_clk_src_simple_get, clk);
138 iounmap(pll_clock->pllcr);
140 iounmap(pll_clock->sckcr);
145 CLK_OF_DECLARE(h8s2678_div_clk, "renesas,h8s2678-pll-clock",
146 h8s2678_pll_clk_setup);