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 | |
Stephen Boyd | 9dfefe8 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 17 | #include <linux/clk.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 18 | #include <linux/clk-provider.h> |
| 19 | #include <linux/clkdev.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_address.h> |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 22 | #include <linux/reset-controller.h> |
Stephen Boyd | 9dfefe8 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Chen-Yu Tsai | 7954dfa | 2014-11-26 15:16:52 +0800 | [diff] [blame] | 25 | #include <linux/log2.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 26 | |
| 27 | #include "clk-factors.h" |
| 28 | |
| 29 | static DEFINE_SPINLOCK(clk_lock); |
| 30 | |
Emilio López | 40a5dcb | 2013-12-23 00:32:32 -0300 | [diff] [blame] | 31 | /* Maximum number of parents our clocks have */ |
| 32 | #define SUNXI_MAX_PARENTS 5 |
| 33 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 34 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 35 | * 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] | 36 | * PLL1 rate is calculated as follows |
| 37 | * rate = (parent_rate * n * (k + 1) >> p) / (m + 1); |
| 38 | * parent_rate is always 24Mhz |
| 39 | */ |
| 40 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 41 | static void sun4i_get_pll1_factors(struct factors_request *req) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 42 | { |
| 43 | u8 div; |
| 44 | |
| 45 | /* Normalize value to a 6M multiple */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 46 | div = req->rate / 6000000; |
| 47 | req->rate = 6000000 * div; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 48 | |
| 49 | /* m is always zero for pll1 */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 50 | req->m = 0; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 51 | |
| 52 | /* k is 1 only on these cases */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 53 | if (req->rate >= 768000000 || req->rate == 42000000 || |
| 54 | req->rate == 54000000) |
| 55 | req->k = 1; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 56 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 57 | req->k = 0; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 58 | |
| 59 | /* p will be 3 for divs under 10 */ |
| 60 | if (div < 10) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 61 | req->p = 3; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 62 | |
| 63 | /* p will be 2 for divs between 10 - 20 and odd divs under 32 */ |
| 64 | else if (div < 20 || (div < 32 && (div & 1))) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 65 | req->p = 2; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 66 | |
| 67 | /* p will be 1 for even divs under 32, divs under 40 and odd pairs |
| 68 | * of divs between 40-62 */ |
| 69 | else if (div < 40 || (div < 64 && (div & 2))) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 70 | req->p = 1; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 71 | |
| 72 | /* any other entries have p = 0 */ |
| 73 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 74 | req->p = 0; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 75 | |
| 76 | /* calculate a suitable n based on k and p */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 77 | div <<= req->p; |
| 78 | div /= (req->k + 1); |
| 79 | req->n = div / 4; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 80 | } |
| 81 | |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 82 | /** |
| 83 | * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1 |
| 84 | * PLL1 rate is calculated as follows |
| 85 | * rate = parent_rate * (n + 1) * (k + 1) / (m + 1); |
| 86 | * parent_rate should always be 24MHz |
| 87 | */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 88 | static void sun6i_a31_get_pll1_factors(struct factors_request *req) |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 89 | { |
| 90 | /* |
| 91 | * We can operate only on MHz, this will make our life easier |
| 92 | * later. |
| 93 | */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 94 | u32 freq_mhz = req->rate / 1000000; |
| 95 | u32 parent_freq_mhz = req->parent_rate / 1000000; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 96 | |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 97 | /* |
| 98 | * Round down the frequency to the closest multiple of either |
| 99 | * 6 or 16 |
| 100 | */ |
| 101 | u32 round_freq_6 = round_down(freq_mhz, 6); |
| 102 | u32 round_freq_16 = round_down(freq_mhz, 16); |
| 103 | |
| 104 | if (round_freq_6 > round_freq_16) |
| 105 | freq_mhz = round_freq_6; |
| 106 | else |
| 107 | freq_mhz = round_freq_16; |
| 108 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 109 | req->rate = freq_mhz * 1000000; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 110 | |
| 111 | /* If the frequency is a multiple of 32 MHz, k is always 3 */ |
| 112 | if (!(freq_mhz % 32)) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 113 | req->k = 3; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 114 | /* If the frequency is a multiple of 9 MHz, k is always 2 */ |
| 115 | else if (!(freq_mhz % 9)) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 116 | req->k = 2; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 117 | /* If the frequency is a multiple of 8 MHz, k is always 1 */ |
| 118 | else if (!(freq_mhz % 8)) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 119 | req->k = 1; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 120 | /* Otherwise, we don't use the k factor */ |
| 121 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 122 | req->k = 0; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * If the frequency is a multiple of 2 but not a multiple of |
| 126 | * 3, m is 3. This is the first time we use 6 here, yet we |
| 127 | * will use it on several other places. |
| 128 | * We use this number because it's the lowest frequency we can |
| 129 | * generate (with n = 0, k = 0, m = 3), so every other frequency |
| 130 | * somehow relates to this frequency. |
| 131 | */ |
| 132 | if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 133 | req->m = 2; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 134 | /* |
| 135 | * If the frequency is a multiple of 6MHz, but the factor is |
| 136 | * odd, m will be 3 |
| 137 | */ |
| 138 | else if ((freq_mhz / 6) & 1) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 139 | req->m = 3; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 140 | /* Otherwise, we end up with m = 1 */ |
| 141 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 142 | req->m = 1; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 143 | |
| 144 | /* Calculate n thanks to the above factors we already got */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 145 | req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz) |
| 146 | - 1; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * If n end up being outbound, and that we can still decrease |
| 150 | * m, do it. |
| 151 | */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 152 | if ((req->n + 1) > 31 && (req->m + 1) > 1) { |
| 153 | req->n = (req->n + 1) / 2 - 1; |
| 154 | req->m = (req->m + 1) / 2 - 1; |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 157 | |
| 158 | /** |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 159 | * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1 |
| 160 | * PLL1 rate is calculated as follows |
| 161 | * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1); |
| 162 | * parent_rate is always 24Mhz |
| 163 | */ |
| 164 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 165 | static void sun8i_a23_get_pll1_factors(struct factors_request *req) |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 166 | { |
| 167 | u8 div; |
| 168 | |
| 169 | /* Normalize value to a 6M multiple */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 170 | div = req->rate / 6000000; |
| 171 | req->rate = 6000000 * div; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 172 | |
| 173 | /* m is always zero for pll1 */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 174 | req->m = 0; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 175 | |
| 176 | /* k is 1 only on these cases */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 177 | if (req->rate >= 768000000 || req->rate == 42000000 || |
| 178 | req->rate == 54000000) |
| 179 | req->k = 1; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 180 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 181 | req->k = 0; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 182 | |
| 183 | /* p will be 2 for divs under 20 and odd divs under 32 */ |
| 184 | if (div < 20 || (div < 32 && (div & 1))) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 185 | req->p = 2; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 186 | |
| 187 | /* p will be 1 for even divs under 32, divs under 40 and odd pairs |
| 188 | * of divs between 40-62 */ |
| 189 | else if (div < 40 || (div < 64 && (div & 2))) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 190 | req->p = 1; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 191 | |
| 192 | /* any other entries have p = 0 */ |
| 193 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 194 | req->p = 0; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 195 | |
| 196 | /* calculate a suitable n based on k and p */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 197 | div <<= req->p; |
| 198 | div /= (req->k + 1); |
| 199 | req->n = div / 4 - 1; |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 203 | * sun4i_get_pll5_factors() - calculates n, k factors for PLL5 |
| 204 | * PLL5 rate is calculated as follows |
| 205 | * rate = parent_rate * n * (k + 1) |
| 206 | * parent_rate is always 24Mhz |
| 207 | */ |
| 208 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 209 | static void sun4i_get_pll5_factors(struct factors_request *req) |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 210 | { |
| 211 | u8 div; |
| 212 | |
| 213 | /* Normalize value to a parent_rate multiple (24M) */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 214 | div = req->rate / req->parent_rate; |
| 215 | req->rate = req->parent_rate * div; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 216 | |
| 217 | if (div < 31) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 218 | req->k = 0; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 219 | else if (div / 2 < 31) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 220 | req->k = 1; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 221 | else if (div / 3 < 31) |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 222 | req->k = 2; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 223 | else |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 224 | req->k = 3; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 225 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 226 | req->n = DIV_ROUND_UP(div, (req->k + 1)); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 227 | } |
| 228 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 229 | /** |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 230 | * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2 |
| 231 | * PLL6x2 rate is calculated as follows |
| 232 | * rate = parent_rate * (n + 1) * (k + 1) |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 233 | * parent_rate is always 24Mhz |
| 234 | */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 235 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 236 | static void sun6i_a31_get_pll6_factors(struct factors_request *req) |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 237 | { |
| 238 | u8 div; |
| 239 | |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 240 | /* Normalize value to a parent_rate multiple (24M) */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 241 | div = req->rate / req->parent_rate; |
| 242 | req->rate = req->parent_rate * div; |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 243 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 244 | req->k = div / 32; |
| 245 | if (req->k > 3) |
| 246 | req->k = 3; |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 247 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 248 | req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1; |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 249 | } |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 250 | |
| 251 | /** |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 252 | * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB |
| 253 | * AHB rate is calculated as follows |
| 254 | * rate = parent_rate >> p |
| 255 | */ |
| 256 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 257 | static void sun5i_a13_get_ahb_factors(struct factors_request *req) |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 258 | { |
| 259 | u32 div; |
| 260 | |
| 261 | /* divide only */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 262 | if (req->parent_rate < req->rate) |
| 263 | req->rate = req->parent_rate; |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * user manual says valid speed is 8k ~ 276M, but tests show it |
| 267 | * can work at speeds up to 300M, just after reparenting to pll6 |
| 268 | */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 269 | if (req->rate < 8000) |
| 270 | req->rate = 8000; |
| 271 | if (req->rate > 300000000) |
| 272 | req->rate = 300000000; |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 273 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 274 | div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate)); |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 275 | |
| 276 | /* p = 0 ~ 3 */ |
| 277 | if (div > 3) |
| 278 | div = 3; |
| 279 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 280 | req->rate = req->parent_rate >> div; |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 281 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 282 | req->p = div; |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Chen-Yu Tsai | a78bb35 | 2016-01-25 21:15:45 +0800 | [diff] [blame] | 285 | #define SUN6I_AHB1_PARENT_PLL6 3 |
| 286 | |
| 287 | /** |
| 288 | * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB |
| 289 | * AHB rate is calculated as follows |
| 290 | * rate = parent_rate >> p |
| 291 | * |
| 292 | * if parent is pll6, then |
| 293 | * parent_rate = pll6 rate / (m + 1) |
| 294 | */ |
| 295 | |
| 296 | static void sun6i_get_ahb1_factors(struct factors_request *req) |
| 297 | { |
| 298 | u8 div, calcp, calcm = 1; |
| 299 | |
| 300 | /* |
| 301 | * clock can only divide, so we will never be able to achieve |
| 302 | * frequencies higher than the parent frequency |
| 303 | */ |
| 304 | if (req->parent_rate && req->rate > req->parent_rate) |
| 305 | req->rate = req->parent_rate; |
| 306 | |
| 307 | div = DIV_ROUND_UP(req->parent_rate, req->rate); |
| 308 | |
| 309 | /* calculate pre-divider if parent is pll6 */ |
| 310 | if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) { |
| 311 | if (div < 4) |
| 312 | calcp = 0; |
| 313 | else if (div / 2 < 4) |
| 314 | calcp = 1; |
| 315 | else if (div / 4 < 4) |
| 316 | calcp = 2; |
| 317 | else |
| 318 | calcp = 3; |
| 319 | |
| 320 | calcm = DIV_ROUND_UP(div, 1 << calcp); |
| 321 | } else { |
| 322 | calcp = __roundup_pow_of_two(div); |
| 323 | calcp = calcp > 3 ? 3 : calcp; |
| 324 | } |
| 325 | |
| 326 | req->rate = (req->parent_rate / calcm) >> calcp; |
| 327 | req->p = calcp; |
| 328 | req->m = calcm - 1; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and |
| 333 | * parent index |
| 334 | */ |
| 335 | static void sun6i_ahb1_recalc(struct factors_request *req) |
| 336 | { |
| 337 | req->rate = req->parent_rate; |
| 338 | |
| 339 | /* apply pre-divider first if parent is pll6 */ |
| 340 | if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) |
| 341 | req->rate /= req->m + 1; |
| 342 | |
| 343 | /* clk divider */ |
| 344 | req->rate >>= req->p; |
| 345 | } |
| 346 | |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 347 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 348 | * sun4i_get_apb1_factors() - calculates m, p factors for APB1 |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 349 | * APB1 rate is calculated as follows |
| 350 | * rate = (parent_rate >> p) / (m + 1); |
| 351 | */ |
| 352 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 353 | static void sun4i_get_apb1_factors(struct factors_request *req) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 354 | { |
| 355 | u8 calcm, calcp; |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 356 | int div; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 357 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 358 | if (req->parent_rate < req->rate) |
| 359 | req->rate = req->parent_rate; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 360 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 361 | div = DIV_ROUND_UP(req->parent_rate, req->rate); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 362 | |
| 363 | /* Invalid rate! */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 364 | if (div > 32) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 365 | return; |
| 366 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 367 | if (div <= 4) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 368 | calcp = 0; |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 369 | else if (div <= 8) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 370 | calcp = 1; |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 371 | else if (div <= 16) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 372 | calcp = 2; |
| 373 | else |
| 374 | calcp = 3; |
| 375 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 376 | calcm = (req->parent_rate >> calcp) - 1; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 377 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 378 | req->rate = (req->parent_rate >> calcp) / (calcm + 1); |
| 379 | req->m = calcm; |
| 380 | req->p = calcp; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
| 384 | |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 385 | |
| 386 | /** |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 387 | * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B |
| 388 | * CLK_OUT rate is calculated as follows |
| 389 | * rate = (parent_rate >> p) / (m + 1); |
| 390 | */ |
| 391 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 392 | static void sun7i_a20_get_out_factors(struct factors_request *req) |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 393 | { |
| 394 | u8 div, calcm, calcp; |
| 395 | |
| 396 | /* These clocks can only divide, so we will never be able to achieve |
| 397 | * frequencies higher than the parent frequency */ |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 398 | if (req->rate > req->parent_rate) |
| 399 | req->rate = req->parent_rate; |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 400 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 401 | div = DIV_ROUND_UP(req->parent_rate, req->rate); |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 402 | |
| 403 | if (div < 32) |
| 404 | calcp = 0; |
| 405 | else if (div / 2 < 32) |
| 406 | calcp = 1; |
| 407 | else if (div / 4 < 32) |
| 408 | calcp = 2; |
| 409 | else |
| 410 | calcp = 3; |
| 411 | |
| 412 | calcm = DIV_ROUND_UP(div, 1 << calcp); |
| 413 | |
Chen-Yu Tsai | cfa63688 | 2016-01-25 21:15:42 +0800 | [diff] [blame] | 414 | req->rate = (req->parent_rate >> calcp) / calcm; |
| 415 | req->m = calcm - 1; |
| 416 | req->p = calcp; |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Chen-Yu Tsai | e4c6d6c | 2014-02-10 18:35:47 +0800 | [diff] [blame] | 419 | /** |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 420 | * sunxi_factors_clk_setup() - Setup function for factor clocks |
| 421 | */ |
| 422 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 423 | static const struct clk_factors_config sun4i_pll1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 424 | .nshift = 8, |
| 425 | .nwidth = 5, |
| 426 | .kshift = 4, |
| 427 | .kwidth = 2, |
| 428 | .mshift = 0, |
| 429 | .mwidth = 2, |
| 430 | .pshift = 16, |
| 431 | .pwidth = 2, |
| 432 | }; |
| 433 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 434 | static const struct clk_factors_config sun6i_a31_pll1_config = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 435 | .nshift = 8, |
| 436 | .nwidth = 5, |
| 437 | .kshift = 4, |
| 438 | .kwidth = 2, |
| 439 | .mshift = 0, |
| 440 | .mwidth = 2, |
Hans de Goede | 76820fc | 2015-01-24 12:56:32 +0100 | [diff] [blame] | 441 | .n_start = 1, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 442 | }; |
| 443 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 444 | static const struct clk_factors_config sun8i_a23_pll1_config = { |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 445 | .nshift = 8, |
| 446 | .nwidth = 5, |
| 447 | .kshift = 4, |
| 448 | .kwidth = 2, |
| 449 | .mshift = 0, |
| 450 | .mwidth = 2, |
| 451 | .pshift = 16, |
| 452 | .pwidth = 2, |
| 453 | .n_start = 1, |
| 454 | }; |
| 455 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 456 | static const struct clk_factors_config sun4i_pll5_config = { |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 457 | .nshift = 8, |
| 458 | .nwidth = 5, |
| 459 | .kshift = 4, |
| 460 | .kwidth = 2, |
| 461 | }; |
| 462 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 463 | static const struct clk_factors_config sun6i_a31_pll6_config = { |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 464 | .nshift = 8, |
| 465 | .nwidth = 5, |
| 466 | .kshift = 4, |
| 467 | .kwidth = 2, |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 468 | .n_start = 1, |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 469 | }; |
| 470 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 471 | static const struct clk_factors_config sun5i_a13_ahb_config = { |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 472 | .pshift = 4, |
| 473 | .pwidth = 2, |
| 474 | }; |
| 475 | |
Chen-Yu Tsai | a78bb35 | 2016-01-25 21:15:45 +0800 | [diff] [blame] | 476 | static const struct clk_factors_config sun6i_ahb1_config = { |
| 477 | .mshift = 6, |
| 478 | .mwidth = 2, |
| 479 | .pshift = 4, |
| 480 | .pwidth = 2, |
| 481 | }; |
| 482 | |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 483 | static const struct clk_factors_config sun4i_apb1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 484 | .mshift = 0, |
| 485 | .mwidth = 5, |
| 486 | .pshift = 16, |
| 487 | .pwidth = 2, |
| 488 | }; |
| 489 | |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 490 | /* user manual says "n" but it's really "p" */ |
Chen-Yu Tsai | b3e919e | 2016-01-25 21:15:38 +0800 | [diff] [blame] | 491 | static const struct clk_factors_config sun7i_a20_out_config = { |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 492 | .mshift = 8, |
| 493 | .mwidth = 5, |
| 494 | .pshift = 20, |
| 495 | .pwidth = 2, |
| 496 | }; |
| 497 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 498 | static const struct factors_data sun4i_pll1_data __initconst = { |
Emilio López | d838ff3 | 2013-12-23 00:32:34 -0300 | [diff] [blame] | 499 | .enable = 31, |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 500 | .table = &sun4i_pll1_config, |
| 501 | .getter = sun4i_get_pll1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 502 | }; |
| 503 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 504 | static const struct factors_data sun6i_a31_pll1_data __initconst = { |
Emilio López | d838ff3 | 2013-12-23 00:32:34 -0300 | [diff] [blame] | 505 | .enable = 31, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 506 | .table = &sun6i_a31_pll1_config, |
| 507 | .getter = sun6i_a31_get_pll1_factors, |
| 508 | }; |
| 509 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 510 | static const struct factors_data sun8i_a23_pll1_data __initconst = { |
| 511 | .enable = 31, |
| 512 | .table = &sun8i_a23_pll1_config, |
| 513 | .getter = sun8i_a23_get_pll1_factors, |
| 514 | }; |
| 515 | |
Emilio López | 5a8ddf2 | 2014-03-19 15:19:30 -0300 | [diff] [blame] | 516 | static const struct factors_data sun7i_a20_pll4_data __initconst = { |
| 517 | .enable = 31, |
| 518 | .table = &sun4i_pll5_config, |
| 519 | .getter = sun4i_get_pll5_factors, |
| 520 | }; |
| 521 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 522 | static const struct factors_data sun4i_pll5_data __initconst = { |
| 523 | .enable = 31, |
| 524 | .table = &sun4i_pll5_config, |
| 525 | .getter = sun4i_get_pll5_factors, |
Chen-Yu Tsai | 667f542 | 2014-02-03 09:51:39 +0800 | [diff] [blame] | 526 | .name = "pll5", |
| 527 | }; |
| 528 | |
| 529 | static const struct factors_data sun4i_pll6_data __initconst = { |
| 530 | .enable = 31, |
| 531 | .table = &sun4i_pll5_config, |
| 532 | .getter = sun4i_get_pll5_factors, |
| 533 | .name = "pll6", |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 534 | }; |
| 535 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 536 | static const struct factors_data sun6i_a31_pll6_data __initconst = { |
| 537 | .enable = 31, |
| 538 | .table = &sun6i_a31_pll6_config, |
| 539 | .getter = sun6i_a31_get_pll6_factors, |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 540 | .name = "pll6x2", |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 541 | }; |
| 542 | |
Chen-Yu Tsai | 9f24309 | 2015-03-20 01:19:03 +0800 | [diff] [blame] | 543 | static const struct factors_data sun5i_a13_ahb_data __initconst = { |
| 544 | .mux = 6, |
| 545 | .muxmask = BIT(1) | BIT(0), |
| 546 | .table = &sun5i_a13_ahb_config, |
| 547 | .getter = sun5i_a13_get_ahb_factors, |
| 548 | }; |
| 549 | |
Chen-Yu Tsai | a78bb35 | 2016-01-25 21:15:45 +0800 | [diff] [blame] | 550 | static const struct factors_data sun6i_ahb1_data __initconst = { |
| 551 | .mux = 12, |
| 552 | .muxmask = BIT(1) | BIT(0), |
| 553 | .table = &sun6i_ahb1_config, |
| 554 | .getter = sun6i_get_ahb1_factors, |
| 555 | .recalc = sun6i_ahb1_recalc, |
| 556 | }; |
| 557 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 558 | static const struct factors_data sun4i_apb1_data __initconst = { |
Emilio López | 93746e7 | 2014-11-06 11:40:29 +0800 | [diff] [blame] | 559 | .mux = 24, |
| 560 | .muxmask = BIT(1) | BIT(0), |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 561 | .table = &sun4i_apb1_config, |
| 562 | .getter = sun4i_get_apb1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 563 | }; |
| 564 | |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 565 | static const struct factors_data sun7i_a20_out_data __initconst = { |
| 566 | .enable = 31, |
| 567 | .mux = 24, |
Chen-Yu Tsai | e94f8cb3 | 2014-10-20 22:10:26 +0800 | [diff] [blame] | 568 | .muxmask = BIT(1) | BIT(0), |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 569 | .table = &sun7i_a20_out_config, |
| 570 | .getter = sun7i_a20_get_out_factors, |
| 571 | }; |
| 572 | |
Emilio López | 5f4e0be | 2013-12-23 00:32:36 -0300 | [diff] [blame] | 573 | static struct clk * __init sunxi_factors_clk_setup(struct device_node *node, |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 574 | const struct factors_data *data) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 575 | { |
Hans de Goede | 7c74c22 | 2014-11-23 14:38:07 +0100 | [diff] [blame] | 576 | void __iomem *reg; |
| 577 | |
| 578 | reg = of_iomap(node, 0); |
| 579 | if (!reg) { |
| 580 | pr_err("Could not get registers for factors-clk: %s\n", |
| 581 | node->name); |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | return sunxi_factors_register(node, data, &clk_lock, reg); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 586 | } |
| 587 | |
Maxime Ripard | c087230 | 2016-02-02 09:07:22 +0100 | [diff] [blame] | 588 | static void __init sun4i_pll1_clk_setup(struct device_node *node) |
| 589 | { |
| 590 | sunxi_factors_clk_setup(node, &sun4i_pll1_data); |
| 591 | } |
| 592 | CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk", |
| 593 | sun4i_pll1_clk_setup); |
| 594 | |
| 595 | static void __init sun6i_pll1_clk_setup(struct device_node *node) |
| 596 | { |
| 597 | sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data); |
| 598 | } |
| 599 | CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk", |
| 600 | sun6i_pll1_clk_setup); |
| 601 | |
| 602 | static void __init sun8i_pll1_clk_setup(struct device_node *node) |
| 603 | { |
| 604 | sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data); |
| 605 | } |
| 606 | CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk", |
| 607 | sun8i_pll1_clk_setup); |
| 608 | |
| 609 | static void __init sun7i_pll4_clk_setup(struct device_node *node) |
| 610 | { |
| 611 | sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data); |
| 612 | } |
| 613 | CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk", |
| 614 | sun7i_pll4_clk_setup); |
| 615 | |
| 616 | static void __init sun5i_ahb_clk_setup(struct device_node *node) |
| 617 | { |
| 618 | sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data); |
| 619 | } |
| 620 | CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk", |
| 621 | sun5i_ahb_clk_setup); |
| 622 | |
Chen-Yu Tsai | a78bb35 | 2016-01-25 21:15:45 +0800 | [diff] [blame] | 623 | static void __init sun6i_ahb1_clk_setup(struct device_node *node) |
| 624 | { |
| 625 | sunxi_factors_clk_setup(node, &sun6i_ahb1_data); |
| 626 | } |
| 627 | CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk", |
| 628 | sun6i_ahb1_clk_setup); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 629 | |
Maxime Ripard | c087230 | 2016-02-02 09:07:22 +0100 | [diff] [blame] | 630 | static void __init sun4i_apb1_clk_setup(struct device_node *node) |
| 631 | { |
| 632 | sunxi_factors_clk_setup(node, &sun4i_apb1_data); |
| 633 | } |
| 634 | CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk", |
| 635 | sun4i_apb1_clk_setup); |
| 636 | |
| 637 | static void __init sun7i_out_clk_setup(struct device_node *node) |
| 638 | { |
| 639 | sunxi_factors_clk_setup(node, &sun7i_a20_out_data); |
| 640 | } |
| 641 | CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk", |
| 642 | sun7i_out_clk_setup); |
| 643 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * sunxi_mux_clk_setup() - Setup function for muxes |
| 647 | */ |
| 648 | |
| 649 | #define SUNXI_MUX_GATE_WIDTH 2 |
| 650 | |
| 651 | struct mux_data { |
| 652 | u8 shift; |
| 653 | }; |
| 654 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 655 | static const struct mux_data sun4i_cpu_mux_data __initconst = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 656 | .shift = 16, |
| 657 | }; |
| 658 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 659 | static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 660 | .shift = 12, |
| 661 | }; |
| 662 | |
Jens Kuske | ab6e23a | 2015-12-04 22:24:40 +0100 | [diff] [blame] | 663 | static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = { |
| 664 | .shift = 0, |
| 665 | }; |
| 666 | |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 667 | static struct clk * __init sunxi_mux_clk_setup(struct device_node *node, |
Maxime Ripard | 5b5226d | 2016-02-02 09:47:11 +0100 | [diff] [blame] | 668 | const struct mux_data *data) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 669 | { |
| 670 | struct clk *clk; |
| 671 | const char *clk_name = node->name; |
Emilio López | edaf3fb | 2013-12-23 00:32:33 -0300 | [diff] [blame] | 672 | const char *parents[SUNXI_MAX_PARENTS]; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 673 | void __iomem *reg; |
Dinh Nguyen | 8a53fb2 | 2015-07-06 22:59:05 -0500 | [diff] [blame] | 674 | int i; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 675 | |
| 676 | reg = of_iomap(node, 0); |
Andre Przywara | 72360b9 | 2016-02-16 10:46:06 +0000 | [diff] [blame^] | 677 | if (!reg) { |
| 678 | pr_err("Could not map registers for mux-clk: %s\n", |
| 679 | of_node_full_name(node)); |
| 680 | return NULL; |
| 681 | } |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 682 | |
Dinh Nguyen | 8a53fb2 | 2015-07-06 22:59:05 -0500 | [diff] [blame] | 683 | i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS); |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 684 | if (of_property_read_string(node, "clock-output-names", &clk_name)) { |
Andre Przywara | 72360b9 | 2016-02-16 10:46:06 +0000 | [diff] [blame^] | 685 | pr_err("%s: could not read clock-output-names from \"%s\"\n", |
| 686 | __func__, of_node_full_name(node)); |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 687 | goto out_unmap; |
| 688 | } |
Chen-Yu Tsai | f64111e | 2014-02-03 09:51:37 +0800 | [diff] [blame] | 689 | |
James Hogan | 819c1de | 2013-07-29 12:25:01 +0100 | [diff] [blame] | 690 | clk = clk_register_mux(NULL, clk_name, parents, i, |
Chen-Yu Tsai | 3ec72fa | 2015-01-06 10:35:12 +0800 | [diff] [blame] | 691 | CLK_SET_RATE_PARENT, reg, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 692 | data->shift, SUNXI_MUX_GATE_WIDTH, |
| 693 | 0, &clk_lock); |
| 694 | |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 695 | if (IS_ERR(clk)) { |
Andre Przywara | 72360b9 | 2016-02-16 10:46:06 +0000 | [diff] [blame^] | 696 | pr_err("%s: failed to register mux clock %s: %ld\n", __func__, |
| 697 | clk_name, PTR_ERR(clk)); |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 698 | goto out_unmap; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 699 | } |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 700 | |
Andre Przywara | 72360b9 | 2016-02-16 10:46:06 +0000 | [diff] [blame^] | 701 | if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) { |
| 702 | pr_err("%s: failed to add clock provider for %s\n", |
| 703 | __func__, clk_name); |
| 704 | clk_unregister_divider(clk); |
| 705 | goto out_unmap; |
| 706 | } |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 707 | |
| 708 | return clk; |
Andre Przywara | d221b7a | 2016-02-01 17:39:27 +0000 | [diff] [blame] | 709 | out_unmap: |
| 710 | iounmap(reg); |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 711 | return NULL; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 712 | } |
| 713 | |
Maxime Ripard | c087230 | 2016-02-02 09:07:22 +0100 | [diff] [blame] | 714 | static void __init sun4i_cpu_clk_setup(struct device_node *node) |
| 715 | { |
| 716 | struct clk *clk; |
| 717 | |
| 718 | clk = sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data); |
| 719 | if (!clk) |
| 720 | return; |
| 721 | |
| 722 | /* Protect CPU clock */ |
| 723 | __clk_get(clk); |
| 724 | clk_prepare_enable(clk); |
| 725 | } |
| 726 | CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk", |
| 727 | sun4i_cpu_clk_setup); |
| 728 | |
| 729 | static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node) |
| 730 | { |
| 731 | sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data); |
| 732 | } |
| 733 | CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk", |
| 734 | sun6i_ahb1_mux_clk_setup); |
| 735 | |
| 736 | static void __init sun8i_ahb2_clk_setup(struct device_node *node) |
| 737 | { |
| 738 | sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data); |
| 739 | } |
| 740 | CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk", |
| 741 | sun8i_ahb2_clk_setup); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 742 | |
| 743 | |
| 744 | /** |
| 745 | * sunxi_divider_clk_setup() - Setup function for simple divider clocks |
| 746 | */ |
| 747 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 748 | struct div_data { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 749 | u8 shift; |
| 750 | u8 pow; |
| 751 | u8 width; |
Chen-Yu Tsai | ea5671b | 2014-06-26 23:55:42 +0800 | [diff] [blame] | 752 | const struct clk_div_table *table; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 753 | }; |
| 754 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 755 | static const struct div_data sun4i_axi_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 756 | .shift = 0, |
| 757 | .pow = 0, |
| 758 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 759 | }; |
| 760 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 761 | static const struct clk_div_table sun8i_a23_axi_table[] __initconst = { |
| 762 | { .val = 0, .div = 1 }, |
| 763 | { .val = 1, .div = 2 }, |
| 764 | { .val = 2, .div = 3 }, |
| 765 | { .val = 3, .div = 4 }, |
| 766 | { .val = 4, .div = 4 }, |
| 767 | { .val = 5, .div = 4 }, |
| 768 | { .val = 6, .div = 4 }, |
| 769 | { .val = 7, .div = 4 }, |
| 770 | { } /* sentinel */ |
| 771 | }; |
| 772 | |
| 773 | static const struct div_data sun8i_a23_axi_data __initconst = { |
| 774 | .width = 3, |
| 775 | .table = sun8i_a23_axi_table, |
| 776 | }; |
| 777 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 778 | static const struct div_data sun4i_ahb_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 779 | .shift = 4, |
| 780 | .pow = 1, |
| 781 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 782 | }; |
| 783 | |
Chen-Yu Tsai | cfe4c93 | 2014-09-06 14:45:10 +0800 | [diff] [blame] | 784 | static const struct clk_div_table sun4i_apb0_table[] __initconst = { |
| 785 | { .val = 0, .div = 2 }, |
| 786 | { .val = 1, .div = 2 }, |
| 787 | { .val = 2, .div = 4 }, |
| 788 | { .val = 3, .div = 8 }, |
| 789 | { } /* sentinel */ |
| 790 | }; |
| 791 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 792 | static const struct div_data sun4i_apb0_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 793 | .shift = 8, |
| 794 | .pow = 1, |
| 795 | .width = 2, |
Chen-Yu Tsai | cfe4c93 | 2014-09-06 14:45:10 +0800 | [diff] [blame] | 796 | .table = sun4i_apb0_table, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 797 | }; |
| 798 | |
| 799 | static void __init sunxi_divider_clk_setup(struct device_node *node, |
Maxime Ripard | 5b5226d | 2016-02-02 09:47:11 +0100 | [diff] [blame] | 800 | const struct div_data *data) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 801 | { |
| 802 | struct clk *clk; |
| 803 | const char *clk_name = node->name; |
| 804 | const char *clk_parent; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 805 | void __iomem *reg; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 806 | |
| 807 | reg = of_iomap(node, 0); |
| 808 | |
| 809 | clk_parent = of_clk_get_parent_name(node, 0); |
| 810 | |
Chen-Yu Tsai | f64111e | 2014-02-03 09:51:37 +0800 | [diff] [blame] | 811 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 812 | |
Chen-Yu Tsai | ea5671b | 2014-06-26 23:55:42 +0800 | [diff] [blame] | 813 | clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0, |
| 814 | reg, data->shift, data->width, |
| 815 | data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0, |
| 816 | data->table, &clk_lock); |
Maxime Ripard | b0b6413 | 2016-02-02 09:37:15 +0100 | [diff] [blame] | 817 | if (clk) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 818 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 819 | } |
| 820 | |
Maxime Ripard | c087230 | 2016-02-02 09:07:22 +0100 | [diff] [blame] | 821 | static void __init sun4i_ahb_clk_setup(struct device_node *node) |
| 822 | { |
| 823 | sunxi_divider_clk_setup(node, &sun4i_ahb_data); |
| 824 | } |
| 825 | CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk", |
| 826 | sun4i_ahb_clk_setup); |
| 827 | |
| 828 | static void __init sun4i_apb0_clk_setup(struct device_node *node) |
| 829 | { |
| 830 | sunxi_divider_clk_setup(node, &sun4i_apb0_data); |
| 831 | } |
| 832 | CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk", |
| 833 | sun4i_apb0_clk_setup); |
| 834 | |
| 835 | static void __init sun4i_axi_clk_setup(struct device_node *node) |
| 836 | { |
| 837 | sunxi_divider_clk_setup(node, &sun4i_axi_data); |
| 838 | } |
| 839 | CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk", |
| 840 | sun4i_axi_clk_setup); |
| 841 | |
| 842 | static void __init sun8i_axi_clk_setup(struct device_node *node) |
| 843 | { |
| 844 | sunxi_divider_clk_setup(node, &sun8i_a23_axi_data); |
| 845 | } |
| 846 | CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk", |
| 847 | sun8i_axi_clk_setup); |
| 848 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 849 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 850 | |
| 851 | /** |
| 852 | * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks |
| 853 | */ |
| 854 | |
| 855 | #define SUNXI_GATES_MAX_SIZE 64 |
| 856 | |
| 857 | struct gates_data { |
| 858 | DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE); |
| 859 | }; |
| 860 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 861 | /** |
| 862 | * sunxi_divs_clk_setup() helper data |
| 863 | */ |
| 864 | |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 865 | #define SUNXI_DIVS_MAX_QTY 4 |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 866 | #define SUNXI_DIVISOR_WIDTH 2 |
| 867 | |
| 868 | struct divs_data { |
| 869 | const struct factors_data *factors; /* data for the factor clock */ |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 870 | int ndivs; /* number of outputs */ |
| 871 | /* |
| 872 | * List of outputs. Refer to the diagram for sunxi_divs_clk_setup(): |
| 873 | * self or base factor clock refers to the output from the pll |
| 874 | * itself. The remaining refer to fixed or configurable divider |
| 875 | * outputs. |
| 876 | */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 877 | struct { |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 878 | u8 self; /* is it the base factor clock? (only one) */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 879 | u8 fixed; /* is it a fixed divisor? if not... */ |
| 880 | struct clk_div_table *table; /* is it a table based divisor? */ |
| 881 | u8 shift; /* otherwise it's a normal divisor with this shift */ |
| 882 | u8 pow; /* is it power-of-two based? */ |
| 883 | u8 gate; /* is it independently gateable? */ |
| 884 | } div[SUNXI_DIVS_MAX_QTY]; |
| 885 | }; |
| 886 | |
| 887 | static struct clk_div_table pll6_sata_tbl[] = { |
| 888 | { .val = 0, .div = 6, }, |
| 889 | { .val = 1, .div = 12, }, |
| 890 | { .val = 2, .div = 18, }, |
| 891 | { .val = 3, .div = 24, }, |
| 892 | { } /* sentinel */ |
| 893 | }; |
| 894 | |
| 895 | static const struct divs_data pll5_divs_data __initconst = { |
| 896 | .factors = &sun4i_pll5_data, |
Chen-Yu Tsai | 13d52f6 | 2014-11-13 02:08:30 +0800 | [diff] [blame] | 897 | .ndivs = 2, |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 898 | .div = { |
| 899 | { .shift = 0, .pow = 0, }, /* M, DDR */ |
| 900 | { .shift = 16, .pow = 1, }, /* P, other */ |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 901 | /* No output for the base factor clock */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 902 | } |
| 903 | }; |
| 904 | |
| 905 | static const struct divs_data pll6_divs_data __initconst = { |
Chen-Yu Tsai | 667f542 | 2014-02-03 09:51:39 +0800 | [diff] [blame] | 906 | .factors = &sun4i_pll6_data, |
Chen-Yu Tsai | f101796 | 2015-03-25 01:22:08 +0800 | [diff] [blame] | 907 | .ndivs = 4, |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 908 | .div = { |
| 909 | { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */ |
| 910 | { .fixed = 2 }, /* P, other */ |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 911 | { .self = 1 }, /* base factor clock, 2x */ |
Chen-Yu Tsai | f101796 | 2015-03-25 01:22:08 +0800 | [diff] [blame] | 912 | { .fixed = 4 }, /* pll6 / 4, used as ahb input */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 913 | } |
| 914 | }; |
| 915 | |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 916 | static const struct divs_data sun6i_a31_pll6_divs_data __initconst = { |
| 917 | .factors = &sun6i_a31_pll6_data, |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 918 | .ndivs = 2, |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 919 | .div = { |
| 920 | { .fixed = 2 }, /* normal output */ |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 921 | { .self = 1 }, /* base factor clock, 2x */ |
Chen-Yu Tsai | 95e94c1 | 2014-11-13 02:08:31 +0800 | [diff] [blame] | 922 | } |
| 923 | }; |
| 924 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 925 | /** |
| 926 | * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks |
| 927 | * |
| 928 | * These clocks look something like this |
| 929 | * ________________________ |
| 930 | * | ___divisor 1---|----> to consumer |
| 931 | * parent >--| pll___/___divisor 2---|----> to consumer |
| 932 | * | \_______________|____> to consumer |
| 933 | * |________________________| |
| 934 | */ |
| 935 | |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 936 | static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node, |
Maxime Ripard | 5b5226d | 2016-02-02 09:47:11 +0100 | [diff] [blame] | 937 | const struct divs_data *data) |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 938 | { |
| 939 | struct clk_onecell_data *clk_data; |
Chen-Yu Tsai | 97e36b3 | 2014-02-03 09:51:40 +0800 | [diff] [blame] | 940 | const char *parent; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 941 | const char *clk_name; |
| 942 | struct clk **clks, *pclk; |
| 943 | struct clk_hw *gate_hw, *rate_hw; |
| 944 | const struct clk_ops *rate_ops; |
| 945 | struct clk_gate *gate = NULL; |
| 946 | struct clk_fixed_factor *fix_factor; |
| 947 | struct clk_divider *divider; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 948 | void __iomem *reg; |
Chen-Yu Tsai | 13d52f6 | 2014-11-13 02:08:30 +0800 | [diff] [blame] | 949 | int ndivs = SUNXI_DIVS_MAX_QTY, i = 0; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 950 | int flags, clkflags; |
| 951 | |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 952 | /* if number of children known, use it */ |
| 953 | if (data->ndivs) |
| 954 | ndivs = data->ndivs; |
| 955 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 956 | /* Set up factor clock that we will be dividing */ |
| 957 | pclk = sunxi_factors_clk_setup(node, data->factors); |
Chen-Yu Tsai | 97e36b3 | 2014-02-03 09:51:40 +0800 | [diff] [blame] | 958 | parent = __clk_get_name(pclk); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 959 | |
| 960 | reg = of_iomap(node, 0); |
| 961 | |
| 962 | clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL); |
| 963 | if (!clk_data) |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 964 | return NULL; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 965 | |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 966 | clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 967 | if (!clks) |
| 968 | goto free_clkdata; |
| 969 | |
| 970 | clk_data->clks = clks; |
| 971 | |
| 972 | /* It's not a good idea to have automatic reparenting changing |
| 973 | * our RAM clock! */ |
| 974 | clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT; |
| 975 | |
Chen-Yu Tsai | 13d52f6 | 2014-11-13 02:08:30 +0800 | [diff] [blame] | 976 | for (i = 0; i < ndivs; i++) { |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 977 | if (of_property_read_string_index(node, "clock-output-names", |
| 978 | i, &clk_name) != 0) |
| 979 | break; |
| 980 | |
Chen-Yu Tsai | 934fe5f | 2015-03-25 01:22:07 +0800 | [diff] [blame] | 981 | /* If this is the base factor clock, only update clks */ |
| 982 | if (data->div[i].self) { |
| 983 | clk_data->clks[i] = pclk; |
| 984 | continue; |
| 985 | } |
| 986 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 987 | gate_hw = NULL; |
| 988 | rate_hw = NULL; |
| 989 | rate_ops = NULL; |
| 990 | |
| 991 | /* If this leaf clock can be gated, create a gate */ |
| 992 | if (data->div[i].gate) { |
| 993 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 994 | if (!gate) |
| 995 | goto free_clks; |
| 996 | |
| 997 | gate->reg = reg; |
| 998 | gate->bit_idx = data->div[i].gate; |
| 999 | gate->lock = &clk_lock; |
| 1000 | |
| 1001 | gate_hw = &gate->hw; |
| 1002 | } |
| 1003 | |
| 1004 | /* Leaves can be fixed or configurable divisors */ |
| 1005 | if (data->div[i].fixed) { |
| 1006 | fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL); |
| 1007 | if (!fix_factor) |
| 1008 | goto free_gate; |
| 1009 | |
| 1010 | fix_factor->mult = 1; |
| 1011 | fix_factor->div = data->div[i].fixed; |
| 1012 | |
| 1013 | rate_hw = &fix_factor->hw; |
| 1014 | rate_ops = &clk_fixed_factor_ops; |
| 1015 | } else { |
| 1016 | divider = kzalloc(sizeof(*divider), GFP_KERNEL); |
| 1017 | if (!divider) |
| 1018 | goto free_gate; |
| 1019 | |
| 1020 | flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0; |
| 1021 | |
| 1022 | divider->reg = reg; |
| 1023 | divider->shift = data->div[i].shift; |
| 1024 | divider->width = SUNXI_DIVISOR_WIDTH; |
| 1025 | divider->flags = flags; |
| 1026 | divider->lock = &clk_lock; |
| 1027 | divider->table = data->div[i].table; |
| 1028 | |
| 1029 | rate_hw = ÷r->hw; |
| 1030 | rate_ops = &clk_divider_ops; |
| 1031 | } |
| 1032 | |
| 1033 | /* Wrap the (potential) gate and the divisor on a composite |
| 1034 | * clock to unify them */ |
| 1035 | clks[i] = clk_register_composite(NULL, clk_name, &parent, 1, |
| 1036 | NULL, NULL, |
| 1037 | rate_hw, rate_ops, |
| 1038 | gate_hw, &clk_gate_ops, |
| 1039 | clkflags); |
| 1040 | |
| 1041 | WARN_ON(IS_ERR(clk_data->clks[i])); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1042 | } |
| 1043 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1044 | /* Adjust to the real max */ |
| 1045 | clk_data->clk_num = i; |
| 1046 | |
| 1047 | of_clk_add_provider(node, of_clk_src_onecell_get, clk_data); |
| 1048 | |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 1049 | return clks; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1050 | |
| 1051 | free_gate: |
| 1052 | kfree(gate); |
| 1053 | free_clks: |
| 1054 | kfree(clks); |
| 1055 | free_clkdata: |
| 1056 | kfree(clk_data); |
Maxime Ripard | 96f185a | 2016-02-02 09:47:10 +0100 | [diff] [blame] | 1057 | return NULL; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1058 | } |
| 1059 | |
Maxime Ripard | c087230 | 2016-02-02 09:07:22 +0100 | [diff] [blame] | 1060 | static void __init sun4i_pll5_clk_setup(struct device_node *node) |
| 1061 | { |
| 1062 | struct clk **clks; |
| 1063 | |
| 1064 | clks = sunxi_divs_clk_setup(node, &pll5_divs_data); |
| 1065 | if (!clks) |
| 1066 | return; |
| 1067 | |
| 1068 | /* Protect PLL5_DDR */ |
| 1069 | __clk_get(clks[0]); |
| 1070 | clk_prepare_enable(clks[0]); |
| 1071 | } |
| 1072 | CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk", |
| 1073 | sun4i_pll5_clk_setup); |
| 1074 | |
| 1075 | static void __init sun4i_pll6_clk_setup(struct device_node *node) |
| 1076 | { |
| 1077 | sunxi_divs_clk_setup(node, &pll6_divs_data); |
| 1078 | } |
| 1079 | CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk", |
| 1080 | sun4i_pll6_clk_setup); |
| 1081 | |
| 1082 | static void __init sun6i_pll6_clk_setup(struct device_node *node) |
| 1083 | { |
| 1084 | sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data); |
| 1085 | } |
| 1086 | CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk", |
| 1087 | sun6i_pll6_clk_setup); |