Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Emilio López |
| 3 | * |
| 4 | * Emilio López <emilio@elopez.com.ar> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/clk-provider.h> |
| 18 | #include <linux/clkdev.h> |
| 19 | #include <linux/clk/sunxi.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_address.h> |
| 22 | |
| 23 | #include "clk-factors.h" |
| 24 | |
| 25 | static DEFINE_SPINLOCK(clk_lock); |
| 26 | |
| 27 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 28 | * sun4i_osc_clk_setup() - Setup function for gatable oscillator |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #define SUNXI_OSC24M_GATE 0 |
| 32 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 33 | static void __init sun4i_osc_clk_setup(struct device_node *node) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 34 | { |
| 35 | struct clk *clk; |
Emilio López | 38e4aa0 | 2013-04-10 15:02:57 -0700 | [diff] [blame] | 36 | struct clk_fixed_rate *fixed; |
| 37 | struct clk_gate *gate; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 38 | const char *clk_name = node->name; |
Emilio López | 38e4aa0 | 2013-04-10 15:02:57 -0700 | [diff] [blame] | 39 | u32 rate; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 40 | |
Emilio López | 38e4aa0 | 2013-04-10 15:02:57 -0700 | [diff] [blame] | 41 | /* allocate fixed-rate and gate clock structs */ |
| 42 | fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL); |
| 43 | if (!fixed) |
| 44 | return; |
| 45 | gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL); |
| 46 | if (!gate) { |
| 47 | kfree(fixed); |
| 48 | return; |
| 49 | } |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 50 | |
Emilio López | 38e4aa0 | 2013-04-10 15:02:57 -0700 | [diff] [blame] | 51 | if (of_property_read_u32(node, "clock-frequency", &rate)) |
| 52 | return; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 53 | |
Emilio López | 38e4aa0 | 2013-04-10 15:02:57 -0700 | [diff] [blame] | 54 | /* set up gate and fixed rate properties */ |
| 55 | gate->reg = of_iomap(node, 0); |
| 56 | gate->bit_idx = SUNXI_OSC24M_GATE; |
| 57 | gate->lock = &clk_lock; |
| 58 | fixed->fixed_rate = rate; |
| 59 | |
| 60 | clk = clk_register_composite(NULL, clk_name, |
| 61 | NULL, 0, |
| 62 | NULL, NULL, |
| 63 | &fixed->hw, &clk_fixed_rate_ops, |
| 64 | &gate->hw, &clk_gate_ops, |
| 65 | CLK_IS_ROOT); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 66 | |
Axel Lin | ee85e9b | 2013-07-12 16:15:15 +0800 | [diff] [blame] | 67 | if (!IS_ERR(clk)) { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 68 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 69 | clk_register_clkdev(clk, clk_name, NULL); |
| 70 | } |
| 71 | } |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 72 | CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 73 | |
| 74 | |
| 75 | |
| 76 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 77 | * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1 |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 78 | * PLL1 rate is calculated as follows |
| 79 | * rate = (parent_rate * n * (k + 1) >> p) / (m + 1); |
| 80 | * parent_rate is always 24Mhz |
| 81 | */ |
| 82 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 83 | static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 84 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 85 | { |
| 86 | u8 div; |
| 87 | |
| 88 | /* Normalize value to a 6M multiple */ |
| 89 | div = *freq / 6000000; |
| 90 | *freq = 6000000 * div; |
| 91 | |
| 92 | /* we were called to round the frequency, we can now return */ |
| 93 | if (n == NULL) |
| 94 | return; |
| 95 | |
| 96 | /* m is always zero for pll1 */ |
| 97 | *m = 0; |
| 98 | |
| 99 | /* k is 1 only on these cases */ |
| 100 | if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000) |
| 101 | *k = 1; |
| 102 | else |
| 103 | *k = 0; |
| 104 | |
| 105 | /* p will be 3 for divs under 10 */ |
| 106 | if (div < 10) |
| 107 | *p = 3; |
| 108 | |
| 109 | /* p will be 2 for divs between 10 - 20 and odd divs under 32 */ |
| 110 | else if (div < 20 || (div < 32 && (div & 1))) |
| 111 | *p = 2; |
| 112 | |
| 113 | /* p will be 1 for even divs under 32, divs under 40 and odd pairs |
| 114 | * of divs between 40-62 */ |
| 115 | else if (div < 40 || (div < 64 && (div & 2))) |
| 116 | *p = 1; |
| 117 | |
| 118 | /* any other entries have p = 0 */ |
| 119 | else |
| 120 | *p = 0; |
| 121 | |
| 122 | /* calculate a suitable n based on k and p */ |
| 123 | div <<= *p; |
| 124 | div /= (*k + 1); |
| 125 | *n = div / 4; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 131 | * sun4i_get_apb1_factors() - calculates m, p factors for APB1 |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 132 | * APB1 rate is calculated as follows |
| 133 | * rate = (parent_rate >> p) / (m + 1); |
| 134 | */ |
| 135 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 136 | static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 137 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 138 | { |
| 139 | u8 calcm, calcp; |
| 140 | |
| 141 | if (parent_rate < *freq) |
| 142 | *freq = parent_rate; |
| 143 | |
| 144 | parent_rate = (parent_rate + (*freq - 1)) / *freq; |
| 145 | |
| 146 | /* Invalid rate! */ |
| 147 | if (parent_rate > 32) |
| 148 | return; |
| 149 | |
| 150 | if (parent_rate <= 4) |
| 151 | calcp = 0; |
| 152 | else if (parent_rate <= 8) |
| 153 | calcp = 1; |
| 154 | else if (parent_rate <= 16) |
| 155 | calcp = 2; |
| 156 | else |
| 157 | calcp = 3; |
| 158 | |
| 159 | calcm = (parent_rate >> calcp) - 1; |
| 160 | |
| 161 | *freq = (parent_rate >> calcp) / (calcm + 1); |
| 162 | |
| 163 | /* we were called to round the frequency, we can now return */ |
| 164 | if (n == NULL) |
| 165 | return; |
| 166 | |
| 167 | *m = calcm; |
| 168 | *p = calcp; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * sunxi_factors_clk_setup() - Setup function for factor clocks |
| 175 | */ |
| 176 | |
| 177 | struct factors_data { |
| 178 | struct clk_factors_config *table; |
| 179 | void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p); |
| 180 | }; |
| 181 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 182 | static struct clk_factors_config sun4i_pll1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 183 | .nshift = 8, |
| 184 | .nwidth = 5, |
| 185 | .kshift = 4, |
| 186 | .kwidth = 2, |
| 187 | .mshift = 0, |
| 188 | .mwidth = 2, |
| 189 | .pshift = 16, |
| 190 | .pwidth = 2, |
| 191 | }; |
| 192 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 193 | static struct clk_factors_config sun4i_apb1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 194 | .mshift = 0, |
| 195 | .mwidth = 5, |
| 196 | .pshift = 16, |
| 197 | .pwidth = 2, |
| 198 | }; |
| 199 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 200 | static const __initconst struct factors_data sun4i_pll1_data = { |
| 201 | .table = &sun4i_pll1_config, |
| 202 | .getter = sun4i_get_pll1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 203 | }; |
| 204 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 205 | static const __initconst struct factors_data sun4i_apb1_data = { |
| 206 | .table = &sun4i_apb1_config, |
| 207 | .getter = sun4i_get_apb1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static void __init sunxi_factors_clk_setup(struct device_node *node, |
| 211 | struct factors_data *data) |
| 212 | { |
| 213 | struct clk *clk; |
| 214 | const char *clk_name = node->name; |
| 215 | const char *parent; |
| 216 | void *reg; |
| 217 | |
| 218 | reg = of_iomap(node, 0); |
| 219 | |
| 220 | parent = of_clk_get_parent_name(node, 0); |
| 221 | |
Emilio López | 5a4fe9b | 2013-03-27 18:20:42 -0300 | [diff] [blame] | 222 | clk = clk_register_factors(NULL, clk_name, parent, 0, reg, |
| 223 | data->table, data->getter, &clk_lock); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 224 | |
Axel Lin | ee85e9b | 2013-07-12 16:15:15 +0800 | [diff] [blame] | 225 | if (!IS_ERR(clk)) { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 226 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 227 | clk_register_clkdev(clk, clk_name, NULL); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * sunxi_mux_clk_setup() - Setup function for muxes |
| 235 | */ |
| 236 | |
| 237 | #define SUNXI_MUX_GATE_WIDTH 2 |
| 238 | |
| 239 | struct mux_data { |
| 240 | u8 shift; |
| 241 | }; |
| 242 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 243 | static const __initconst struct mux_data sun4i_cpu_mux_data = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 244 | .shift = 16, |
| 245 | }; |
| 246 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 247 | static const __initconst struct mux_data sun4i_apb1_mux_data = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 248 | .shift = 24, |
| 249 | }; |
| 250 | |
| 251 | static void __init sunxi_mux_clk_setup(struct device_node *node, |
| 252 | struct mux_data *data) |
| 253 | { |
| 254 | struct clk *clk; |
| 255 | const char *clk_name = node->name; |
Emilio López | 918d7f6 | 2013-03-27 18:20:43 -0300 | [diff] [blame] | 256 | const char *parents[5]; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 257 | void *reg; |
| 258 | int i = 0; |
| 259 | |
| 260 | reg = of_iomap(node, 0); |
| 261 | |
| 262 | while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL) |
| 263 | i++; |
| 264 | |
James Hogan | 819c1de | 2013-07-29 12:25:01 +0100 | [diff] [blame] | 265 | clk = clk_register_mux(NULL, clk_name, parents, i, |
| 266 | CLK_SET_RATE_NO_REPARENT, reg, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 267 | data->shift, SUNXI_MUX_GATE_WIDTH, |
| 268 | 0, &clk_lock); |
| 269 | |
| 270 | if (clk) { |
| 271 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 272 | clk_register_clkdev(clk, clk_name, NULL); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * sunxi_divider_clk_setup() - Setup function for simple divider clocks |
| 280 | */ |
| 281 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 282 | struct div_data { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame^] | 283 | u8 shift; |
| 284 | u8 pow; |
| 285 | u8 width; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 286 | }; |
| 287 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 288 | static const __initconst struct div_data sun4i_axi_data = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame^] | 289 | .shift = 0, |
| 290 | .pow = 0, |
| 291 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 292 | }; |
| 293 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 294 | static const __initconst struct div_data sun4i_ahb_data = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame^] | 295 | .shift = 4, |
| 296 | .pow = 1, |
| 297 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 298 | }; |
| 299 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 300 | static const __initconst struct div_data sun4i_apb0_data = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame^] | 301 | .shift = 8, |
| 302 | .pow = 1, |
| 303 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | static void __init sunxi_divider_clk_setup(struct device_node *node, |
| 307 | struct div_data *data) |
| 308 | { |
| 309 | struct clk *clk; |
| 310 | const char *clk_name = node->name; |
| 311 | const char *clk_parent; |
| 312 | void *reg; |
| 313 | |
| 314 | reg = of_iomap(node, 0); |
| 315 | |
| 316 | clk_parent = of_clk_get_parent_name(node, 0); |
| 317 | |
| 318 | clk = clk_register_divider(NULL, clk_name, clk_parent, 0, |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame^] | 319 | reg, data->shift, data->width, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 320 | data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0, |
| 321 | &clk_lock); |
| 322 | if (clk) { |
| 323 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 324 | clk_register_clkdev(clk, clk_name, NULL); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks |
| 332 | */ |
| 333 | |
| 334 | #define SUNXI_GATES_MAX_SIZE 64 |
| 335 | |
| 336 | struct gates_data { |
| 337 | DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE); |
| 338 | }; |
| 339 | |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 340 | static const __initconst struct gates_data sun4i_axi_gates_data = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 341 | .mask = {1}, |
| 342 | }; |
| 343 | |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 344 | static const __initconst struct gates_data sun4i_ahb_gates_data = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 345 | .mask = {0x7F77FFF, 0x14FB3F}, |
| 346 | }; |
| 347 | |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 348 | static const __initconst struct gates_data sun5i_a10s_ahb_gates_data = { |
| 349 | .mask = {0x147667e7, 0x185915}, |
| 350 | }; |
| 351 | |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 352 | static const __initconst struct gates_data sun5i_a13_ahb_gates_data = { |
| 353 | .mask = {0x107067e7, 0x185111}, |
| 354 | }; |
| 355 | |
| 356 | static const __initconst struct gates_data sun4i_apb0_gates_data = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 357 | .mask = {0x4EF}, |
| 358 | }; |
| 359 | |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 360 | static const __initconst struct gates_data sun5i_a10s_apb0_gates_data = { |
| 361 | .mask = {0x469}, |
| 362 | }; |
| 363 | |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 364 | static const __initconst struct gates_data sun5i_a13_apb0_gates_data = { |
| 365 | .mask = {0x61}, |
| 366 | }; |
| 367 | |
| 368 | static const __initconst struct gates_data sun4i_apb1_gates_data = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 369 | .mask = {0xFF00F7}, |
| 370 | }; |
| 371 | |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 372 | static const __initconst struct gates_data sun5i_a10s_apb1_gates_data = { |
| 373 | .mask = {0xf0007}, |
| 374 | }; |
| 375 | |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 376 | static const __initconst struct gates_data sun5i_a13_apb1_gates_data = { |
| 377 | .mask = {0xa0007}, |
| 378 | }; |
| 379 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 380 | static void __init sunxi_gates_clk_setup(struct device_node *node, |
| 381 | struct gates_data *data) |
| 382 | { |
| 383 | struct clk_onecell_data *clk_data; |
| 384 | const char *clk_parent; |
| 385 | const char *clk_name; |
| 386 | void *reg; |
| 387 | int qty; |
| 388 | int i = 0; |
| 389 | int j = 0; |
| 390 | int ignore; |
| 391 | |
| 392 | reg = of_iomap(node, 0); |
| 393 | |
| 394 | clk_parent = of_clk_get_parent_name(node, 0); |
| 395 | |
| 396 | /* Worst-case size approximation and memory allocation */ |
| 397 | qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE); |
| 398 | clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL); |
| 399 | if (!clk_data) |
| 400 | return; |
| 401 | clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL); |
| 402 | if (!clk_data->clks) { |
| 403 | kfree(clk_data); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) { |
| 408 | of_property_read_string_index(node, "clock-output-names", |
| 409 | j, &clk_name); |
| 410 | |
| 411 | /* No driver claims this clock, but it should remain gated */ |
| 412 | ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0; |
| 413 | |
| 414 | clk_data->clks[i] = clk_register_gate(NULL, clk_name, |
| 415 | clk_parent, ignore, |
| 416 | reg + 4 * (i/32), i % 32, |
| 417 | 0, &clk_lock); |
| 418 | WARN_ON(IS_ERR(clk_data->clks[i])); |
| 419 | |
| 420 | j++; |
| 421 | } |
| 422 | |
| 423 | /* Adjust to the real max */ |
| 424 | clk_data->clk_num = i; |
| 425 | |
| 426 | of_clk_add_provider(node, of_clk_src_onecell_get, clk_data); |
| 427 | } |
| 428 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 429 | /* Matches for factors clocks */ |
| 430 | static const __initconst struct of_device_id clk_factors_match[] = { |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 431 | {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,}, |
| 432 | {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 433 | {} |
| 434 | }; |
| 435 | |
| 436 | /* Matches for divider clocks */ |
| 437 | static const __initconst struct of_device_id clk_div_match[] = { |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 438 | {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,}, |
| 439 | {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,}, |
| 440 | {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 441 | {} |
| 442 | }; |
| 443 | |
| 444 | /* Matches for mux clocks */ |
| 445 | static const __initconst struct of_device_id clk_mux_match[] = { |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 446 | {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,}, |
| 447 | {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 448 | {} |
| 449 | }; |
| 450 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 451 | /* Matches for gate clocks */ |
| 452 | static const __initconst struct of_device_id clk_gates_match[] = { |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 453 | {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,}, |
| 454 | {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 455 | {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 456 | {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,}, |
| 457 | {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 458 | {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 459 | {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,}, |
| 460 | {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 461 | {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 462 | {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,}, |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 463 | {} |
| 464 | }; |
| 465 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 466 | static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match, |
| 467 | void *function) |
| 468 | { |
| 469 | struct device_node *np; |
| 470 | const struct div_data *data; |
| 471 | const struct of_device_id *match; |
| 472 | void (*setup_function)(struct device_node *, const void *) = function; |
| 473 | |
| 474 | for_each_matching_node(np, clk_match) { |
| 475 | match = of_match_node(clk_match, np); |
| 476 | data = match->data; |
| 477 | setup_function(np, data); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void __init sunxi_init_clocks(void) |
| 482 | { |
Emilio López | 431807f | 2013-07-22 22:01:05 -0300 | [diff] [blame] | 483 | /* Register all the simple and basic clocks on DT */ |
| 484 | of_clk_init(NULL); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 485 | |
| 486 | /* Register factor clocks */ |
| 487 | of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup); |
| 488 | |
| 489 | /* Register divider clocks */ |
| 490 | of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup); |
| 491 | |
| 492 | /* Register mux clocks */ |
| 493 | of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup); |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 494 | |
| 495 | /* Register gate clocks */ |
| 496 | of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 497 | } |