blob: fc24b0b55a3d7dfb872408a00e42913372344562 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yoshinori Sato7b5bb892015-05-08 23:31:57 +09002/*
3 * H8S2678 clock driver
4 *
5 * Copyright 2015 Yoshinori Sato <ysato@users.sourceforge.jp>
6 */
7
Yoshinori Sato7b5bb892015-05-08 23:31:57 +09008#include <linux/clk-provider.h>
9#include <linux/err.h>
10#include <linux/device.h>
11#include <linux/of_address.h>
Guenter Roeckac0e1372015-08-31 20:27:32 -070012#include <linux/slab.h>
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090013
14static DEFINE_SPINLOCK(clklock);
15
16#define MAX_FREQ 33333333
17#define MIN_FREQ 8000000
18
19struct pll_clock {
20 struct clk_hw hw;
21 void __iomem *sckcr;
22 void __iomem *pllcr;
23};
24
25#define to_pll_clock(_hw) container_of(_hw, struct pll_clock, hw)
26
27static unsigned long pll_recalc_rate(struct clk_hw *hw,
28 unsigned long parent_rate)
29{
30 struct pll_clock *pll_clock = to_pll_clock(hw);
Stephen Boyd006cb8b2015-07-13 17:06:53 -070031 int mul = 1 << (readb(pll_clock->pllcr) & 3);
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090032
33 return parent_rate * mul;
34}
35
36static long pll_round_rate(struct clk_hw *hw, unsigned long rate,
37 unsigned long *prate)
38{
39 int i, m = -1;
40 long offset[3];
41
42 if (rate > MAX_FREQ)
43 rate = MAX_FREQ;
44 if (rate < MIN_FREQ)
45 rate = MIN_FREQ;
46
47 for (i = 0; i < 3; i++)
48 offset[i] = abs(rate - (*prate * (1 << i)));
49 for (i = 0; i < 3; i++)
50 if (m < 0)
51 m = i;
52 else
53 m = (offset[i] < offset[m])?i:m;
54
55 return *prate * (1 << m);
56}
57
58static int pll_set_rate(struct clk_hw *hw, unsigned long rate,
59 unsigned long parent_rate)
60{
61 int pll;
62 unsigned char val;
63 unsigned long flags;
64 struct pll_clock *pll_clock = to_pll_clock(hw);
65
66 pll = ((rate / parent_rate) / 2) & 0x03;
67 spin_lock_irqsave(&clklock, flags);
Stephen Boyd006cb8b2015-07-13 17:06:53 -070068 val = readb(pll_clock->sckcr);
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090069 val |= 0x08;
Stephen Boyd006cb8b2015-07-13 17:06:53 -070070 writeb(val, pll_clock->sckcr);
71 val = readb(pll_clock->pllcr);
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090072 val &= ~0x03;
73 val |= pll;
Stephen Boyd006cb8b2015-07-13 17:06:53 -070074 writeb(val, pll_clock->pllcr);
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090075 spin_unlock_irqrestore(&clklock, flags);
76 return 0;
77}
78
79static const struct clk_ops pll_ops = {
80 .recalc_rate = pll_recalc_rate,
81 .round_rate = pll_round_rate,
82 .set_rate = pll_set_rate,
83};
84
85static void __init h8s2678_pll_clk_setup(struct device_node *node)
86{
Stephen Boydebf3f9a2016-02-19 17:36:51 -080087 unsigned int num_parents;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090088 const char *clk_name = node->name;
89 const char *parent_name;
90 struct pll_clock *pll_clock;
91 struct clk_init_data init;
Stephen Boyd8b2bdc72016-08-16 15:37:57 -070092 int ret;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090093
94 num_parents = of_clk_get_parent_count(node);
Stephen Boydebf3f9a2016-02-19 17:36:51 -080095 if (!num_parents) {
Yoshinori Sato7b5bb892015-05-08 23:31:57 +090096 pr_err("%s: no parent found", clk_name);
97 return;
98 }
99
100
Stephen Boyd9298f022015-07-13 16:54:04 -0700101 pll_clock = kzalloc(sizeof(*pll_clock), GFP_KERNEL);
102 if (!pll_clock)
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900103 return;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900104
105 pll_clock->sckcr = of_iomap(node, 0);
106 if (pll_clock->sckcr == NULL) {
107 pr_err("%s: failed to map divide register", clk_name);
Dan Carpenterded515a2015-05-14 13:05:00 +0300108 goto free_clock;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900109 }
110
111 pll_clock->pllcr = of_iomap(node, 1);
112 if (pll_clock->pllcr == NULL) {
113 pr_err("%s: failed to map multiply register", clk_name);
Dan Carpenterded515a2015-05-14 13:05:00 +0300114 goto unmap_sckcr;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900115 }
116
117 parent_name = of_clk_get_parent_name(node, 0);
118 init.name = clk_name;
119 init.ops = &pll_ops;
120 init.flags = CLK_IS_BASIC;
121 init.parent_names = &parent_name;
122 init.num_parents = 1;
123 pll_clock->hw.init = &init;
124
Stephen Boyd8b2bdc72016-08-16 15:37:57 -0700125 ret = clk_hw_register(NULL, &pll_clock->hw);
126 if (ret) {
127 pr_err("%s: failed to register %s div clock (%d)\n",
128 __func__, clk_name, ret);
Dan Carpenterded515a2015-05-14 13:05:00 +0300129 goto unmap_pllcr;
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900130 }
Dan Carpenterded515a2015-05-14 13:05:00 +0300131
Stephen Boyd8b2bdc72016-08-16 15:37:57 -0700132 of_clk_add_hw_provider(node, of_clk_hw_simple_get, &pll_clock->hw);
Dan Carpenterded515a2015-05-14 13:05:00 +0300133 return;
134
135unmap_pllcr:
136 iounmap(pll_clock->pllcr);
137unmap_sckcr:
138 iounmap(pll_clock->sckcr);
139free_clock:
140 kfree(pll_clock);
Yoshinori Sato7b5bb892015-05-08 23:31:57 +0900141}
142
143CLK_OF_DECLARE(h8s2678_div_clk, "renesas,h8s2678-pll-clock",
144 h8s2678_pll_clk_setup);