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