f17a2a1f1e85aeb7a806328d7078b94b126558ed
[cascardo/linux.git] / drivers / clk / at91 / pmc.c
1 /*
2  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  */
10
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/regmap.h>
18
19 #include <asm/proc-fns.h>
20
21 #include "pmc.h"
22
23 void __iomem *at91_pmc_base;
24 EXPORT_SYMBOL_GPL(at91_pmc_base);
25
26 void at91rm9200_idle(void)
27 {
28         /*
29          * Disable the processor clock.  The processor will be automatically
30          * re-enabled by an interrupt or by a reset.
31          */
32         at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
33 }
34
35 void at91sam9_idle(void)
36 {
37         at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
38         cpu_do_idle();
39 }
40
41 int of_at91_get_clk_range(struct device_node *np, const char *propname,
42                           struct clk_range *range)
43 {
44         u32 min, max;
45         int ret;
46
47         ret = of_property_read_u32_index(np, propname, 0, &min);
48         if (ret)
49                 return ret;
50
51         ret = of_property_read_u32_index(np, propname, 1, &max);
52         if (ret)
53                 return ret;
54
55         if (range) {
56                 range->min = min;
57                 range->max = max;
58         }
59
60         return 0;
61 }
62 EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
63
64 static const struct at91_pmc_caps at91rm9200_caps = {
65         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
66                           AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
67                           AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
68                           AT91_PMC_PCK3RDY,
69 };
70
71 static const struct at91_pmc_caps at91sam9260_caps = {
72         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
73                           AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
74                           AT91_PMC_PCK1RDY,
75 };
76
77 static const struct at91_pmc_caps at91sam9g45_caps = {
78         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
79                           AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
80                           AT91_PMC_PCK1RDY,
81 };
82
83 static const struct at91_pmc_caps at91sam9n12_caps = {
84         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
85                           AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
86                           AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
87                           AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
88 };
89
90 static const struct at91_pmc_caps at91sam9x5_caps = {
91         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
92                           AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
93                           AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
94                           AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
95 };
96
97 static const struct at91_pmc_caps sama5d2_caps = {
98         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
99                           AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
100                           AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
101                           AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
102                           AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
103 };
104
105 static const struct at91_pmc_caps sama5d3_caps = {
106         .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
107                           AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
108                           AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
109                           AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
110                           AT91_PMC_CFDEV,
111 };
112
113 static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
114                                              struct regmap *regmap,
115                                              void __iomem *regbase,
116                                              const struct at91_pmc_caps *caps)
117 {
118         struct at91_pmc *pmc;
119
120         if (!regbase || !caps)
121                 return NULL;
122
123         at91_pmc_base = regbase;
124
125         pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
126         if (!pmc)
127                 return NULL;
128
129         pmc->regmap = regmap;
130         pmc->caps = caps;
131
132         regmap_write(pmc->regmap, AT91_PMC_IDR, 0xffffffff);
133
134         return pmc;
135 }
136
137 static void __init of_at91_pmc_setup(struct device_node *np,
138                                      const struct at91_pmc_caps *caps)
139 {
140         struct at91_pmc *pmc;
141         void __iomem *regbase = of_iomap(np, 0);
142         struct regmap *regmap;
143
144         regmap = syscon_node_to_regmap(np);
145         if (IS_ERR(regmap))
146                 panic("Could not retrieve syscon regmap");
147
148         pmc = at91_pmc_init(np, regmap, regbase, caps);
149         if (!pmc)
150                 return;
151 }
152
153 static void __init of_at91rm9200_pmc_setup(struct device_node *np)
154 {
155         of_at91_pmc_setup(np, &at91rm9200_caps);
156 }
157 CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
158                of_at91rm9200_pmc_setup);
159
160 static void __init of_at91sam9260_pmc_setup(struct device_node *np)
161 {
162         of_at91_pmc_setup(np, &at91sam9260_caps);
163 }
164 CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
165                of_at91sam9260_pmc_setup);
166
167 static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
168 {
169         of_at91_pmc_setup(np, &at91sam9g45_caps);
170 }
171 CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
172                of_at91sam9g45_pmc_setup);
173
174 static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
175 {
176         of_at91_pmc_setup(np, &at91sam9n12_caps);
177 }
178 CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
179                of_at91sam9n12_pmc_setup);
180
181 static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
182 {
183         of_at91_pmc_setup(np, &at91sam9x5_caps);
184 }
185 CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
186                of_at91sam9x5_pmc_setup);
187
188 static void __init of_sama5d2_pmc_setup(struct device_node *np)
189 {
190         of_at91_pmc_setup(np, &sama5d2_caps);
191 }
192 CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
193                of_sama5d2_pmc_setup);
194
195 static void __init of_sama5d3_pmc_setup(struct device_node *np)
196 {
197         of_at91_pmc_setup(np, &sama5d3_caps);
198 }
199 CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
200                of_sama5d3_pmc_setup);