Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 STMicroelectronics R&D Ltd |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Authors: |
| 12 | * Stephen Gallimore <stephen.gallimore@st.com>, |
| 13 | * Pankaj Dev <pankaj.dev@st.com>. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/of_address.h> |
| 18 | #include <linux/clk-provider.h> |
| 19 | |
| 20 | #include "clkgen.h" |
| 21 | |
| 22 | /* |
| 23 | * Maximum input clock to the PLL before we divide it down by 2 |
| 24 | * although in reality in actual systems this has never been seen to |
| 25 | * be used. |
| 26 | */ |
| 27 | #define QUADFS_NDIV_THRESHOLD 30000000 |
| 28 | |
| 29 | #define PLL_BW_GOODREF (0L) |
| 30 | #define PLL_BW_VBADREF (1L) |
| 31 | #define PLL_BW_BADREF (2L) |
| 32 | #define PLL_BW_VGOODREF (3L) |
| 33 | |
| 34 | #define QUADFS_MAX_CHAN 4 |
| 35 | |
| 36 | struct stm_fs { |
| 37 | unsigned long ndiv; |
| 38 | unsigned long mdiv; |
| 39 | unsigned long pe; |
| 40 | unsigned long sdiv; |
| 41 | unsigned long nsdiv; |
| 42 | }; |
| 43 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 44 | static const struct stm_fs fs216c65_rtbl[] = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 45 | { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 312.5 Khz */ |
| 46 | { .mdiv = 0x17, .pe = 0x25ed, .sdiv = 0x1, .nsdiv = 0 }, /* 27 MHz */ |
| 47 | { .mdiv = 0x1a, .pe = 0x7b36, .sdiv = 0x2, .nsdiv = 1 }, /* 36.87 MHz */ |
| 48 | { .mdiv = 0x13, .pe = 0x0, .sdiv = 0x2, .nsdiv = 1 }, /* 48 MHz */ |
| 49 | { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x1, .nsdiv = 1 }, /* 108 MHz */ |
| 50 | }; |
| 51 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 52 | static const struct stm_fs fs432c65_rtbl[] = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 53 | { .mdiv = 0x1f, .pe = 0x0, .sdiv = 0x7, .nsdiv = 0 }, /* 625 Khz */ |
| 54 | { .mdiv = 0x11, .pe = 0x1c72, .sdiv = 0x2, .nsdiv = 1 }, /* 108 MHz */ |
| 55 | { .mdiv = 0x19, .pe = 0x121a, .sdiv = 0x0, .nsdiv = 1 }, /* 297 MHz */ |
| 56 | }; |
| 57 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 58 | static const struct stm_fs fs660c32_rtbl[] = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 59 | { .mdiv = 0x01, .pe = 0x2aaa, .sdiv = 0x8, .nsdiv = 0 }, /* 600 KHz */ |
| 60 | { .mdiv = 0x02, .pe = 0x3d33, .sdiv = 0x0, .nsdiv = 0 }, /* 148.5 Mhz */ |
| 61 | { .mdiv = 0x13, .pe = 0x5bcc, .sdiv = 0x0, .nsdiv = 1 }, /* 297 Mhz */ |
| 62 | { .mdiv = 0x0e, .pe = 0x1025, .sdiv = 0x0, .nsdiv = 1 }, /* 333 Mhz */ |
| 63 | { .mdiv = 0x0b, .pe = 0x715f, .sdiv = 0x0, .nsdiv = 1 }, /* 350 Mhz */ |
| 64 | }; |
| 65 | |
| 66 | struct clkgen_quadfs_data { |
| 67 | bool reset_present; |
| 68 | bool bwfilter_present; |
| 69 | bool lockstatus_present; |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 70 | bool powerup_polarity; |
| 71 | bool standby_polarity; |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 72 | bool nsdiv_present; |
Gabriel FERNANDEZ | fc755c8 | 2014-07-15 17:20:26 +0200 | [diff] [blame] | 73 | bool nrst_present; |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 74 | struct clkgen_field ndiv; |
| 75 | struct clkgen_field ref_bw; |
| 76 | struct clkgen_field nreset; |
| 77 | struct clkgen_field npda; |
| 78 | struct clkgen_field lock_status; |
| 79 | |
Gabriel FERNANDEZ | fc755c8 | 2014-07-15 17:20:26 +0200 | [diff] [blame] | 80 | struct clkgen_field nrst[QUADFS_MAX_CHAN]; |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 81 | struct clkgen_field nsb[QUADFS_MAX_CHAN]; |
| 82 | struct clkgen_field en[QUADFS_MAX_CHAN]; |
| 83 | struct clkgen_field mdiv[QUADFS_MAX_CHAN]; |
| 84 | struct clkgen_field pe[QUADFS_MAX_CHAN]; |
| 85 | struct clkgen_field sdiv[QUADFS_MAX_CHAN]; |
| 86 | struct clkgen_field nsdiv[QUADFS_MAX_CHAN]; |
| 87 | |
| 88 | const struct clk_ops *pll_ops; |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 89 | const struct stm_fs *rtbl; |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 90 | u8 rtbl_cnt; |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 91 | int (*get_rate)(unsigned long , const struct stm_fs *, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 92 | unsigned long *); |
| 93 | }; |
| 94 | |
| 95 | static const struct clk_ops st_quadfs_pll_c65_ops; |
| 96 | static const struct clk_ops st_quadfs_pll_c32_ops; |
| 97 | static const struct clk_ops st_quadfs_fs216c65_ops; |
| 98 | static const struct clk_ops st_quadfs_fs432c65_ops; |
| 99 | static const struct clk_ops st_quadfs_fs660c32_ops; |
| 100 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 101 | static int clk_fs216c65_get_rate(unsigned long, const struct stm_fs *, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 102 | unsigned long *); |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 103 | static int clk_fs432c65_get_rate(unsigned long, const struct stm_fs *, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 104 | unsigned long *); |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 105 | static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 106 | unsigned long *); |
| 107 | /* |
| 108 | * Values for all of the standalone instances of this clock |
| 109 | * generator found in STiH415 and STiH416 SYSCFG register banks. Note |
| 110 | * that the individual channel standby control bits (nsb) are in the |
| 111 | * first register along with the PLL control bits. |
| 112 | */ |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 113 | static const struct clkgen_quadfs_data st_fs216c65_416 = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 114 | /* 416 specific */ |
| 115 | .npda = CLKGEN_FIELD(0x0, 0x1, 14), |
| 116 | .nsb = { CLKGEN_FIELD(0x0, 0x1, 10), |
| 117 | CLKGEN_FIELD(0x0, 0x1, 11), |
| 118 | CLKGEN_FIELD(0x0, 0x1, 12), |
| 119 | CLKGEN_FIELD(0x0, 0x1, 13) }, |
| 120 | .nsdiv_present = true, |
| 121 | .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18), |
| 122 | CLKGEN_FIELD(0x0, 0x1, 19), |
| 123 | CLKGEN_FIELD(0x0, 0x1, 20), |
| 124 | CLKGEN_FIELD(0x0, 0x1, 21) }, |
| 125 | .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0), |
| 126 | CLKGEN_FIELD(0x14, 0x1f, 0), |
| 127 | CLKGEN_FIELD(0x24, 0x1f, 0), |
| 128 | CLKGEN_FIELD(0x34, 0x1f, 0) }, |
| 129 | .en = { CLKGEN_FIELD(0x10, 0x1, 0), |
| 130 | CLKGEN_FIELD(0x20, 0x1, 0), |
| 131 | CLKGEN_FIELD(0x30, 0x1, 0), |
| 132 | CLKGEN_FIELD(0x40, 0x1, 0) }, |
| 133 | .ndiv = CLKGEN_FIELD(0x0, 0x1, 15), |
| 134 | .bwfilter_present = true, |
| 135 | .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16), |
| 136 | .pe = { CLKGEN_FIELD(0x8, 0xffff, 0), |
| 137 | CLKGEN_FIELD(0x18, 0xffff, 0), |
| 138 | CLKGEN_FIELD(0x28, 0xffff, 0), |
| 139 | CLKGEN_FIELD(0x38, 0xffff, 0) }, |
| 140 | .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0), |
| 141 | CLKGEN_FIELD(0x1C, 0x7, 0), |
| 142 | CLKGEN_FIELD(0x2C, 0x7, 0), |
| 143 | CLKGEN_FIELD(0x3C, 0x7, 0) }, |
| 144 | .pll_ops = &st_quadfs_pll_c65_ops, |
| 145 | .rtbl = fs216c65_rtbl, |
| 146 | .rtbl_cnt = ARRAY_SIZE(fs216c65_rtbl), |
| 147 | .get_rate = clk_fs216c65_get_rate, |
| 148 | }; |
| 149 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 150 | static const struct clkgen_quadfs_data st_fs432c65_416 = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 151 | .npda = CLKGEN_FIELD(0x0, 0x1, 14), |
| 152 | .nsb = { CLKGEN_FIELD(0x0, 0x1, 10), |
| 153 | CLKGEN_FIELD(0x0, 0x1, 11), |
| 154 | CLKGEN_FIELD(0x0, 0x1, 12), |
| 155 | CLKGEN_FIELD(0x0, 0x1, 13) }, |
| 156 | .nsdiv_present = true, |
| 157 | .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18), |
| 158 | CLKGEN_FIELD(0x0, 0x1, 19), |
| 159 | CLKGEN_FIELD(0x0, 0x1, 20), |
| 160 | CLKGEN_FIELD(0x0, 0x1, 21) }, |
| 161 | .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0), |
| 162 | CLKGEN_FIELD(0x14, 0x1f, 0), |
| 163 | CLKGEN_FIELD(0x24, 0x1f, 0), |
| 164 | CLKGEN_FIELD(0x34, 0x1f, 0) }, |
| 165 | .en = { CLKGEN_FIELD(0x10, 0x1, 0), |
| 166 | CLKGEN_FIELD(0x20, 0x1, 0), |
| 167 | CLKGEN_FIELD(0x30, 0x1, 0), |
| 168 | CLKGEN_FIELD(0x40, 0x1, 0) }, |
| 169 | .ndiv = CLKGEN_FIELD(0x0, 0x1, 15), |
| 170 | .bwfilter_present = true, |
| 171 | .ref_bw = CLKGEN_FIELD(0x0, 0x3, 16), |
| 172 | .pe = { CLKGEN_FIELD(0x8, 0xffff, 0), |
| 173 | CLKGEN_FIELD(0x18, 0xffff, 0), |
| 174 | CLKGEN_FIELD(0x28, 0xffff, 0), |
| 175 | CLKGEN_FIELD(0x38, 0xffff, 0) }, |
| 176 | .sdiv = { CLKGEN_FIELD(0xC, 0x7, 0), |
| 177 | CLKGEN_FIELD(0x1C, 0x7, 0), |
| 178 | CLKGEN_FIELD(0x2C, 0x7, 0), |
| 179 | CLKGEN_FIELD(0x3C, 0x7, 0) }, |
| 180 | .pll_ops = &st_quadfs_pll_c65_ops, |
| 181 | .rtbl = fs432c65_rtbl, |
| 182 | .rtbl_cnt = ARRAY_SIZE(fs432c65_rtbl), |
| 183 | .get_rate = clk_fs432c65_get_rate, |
| 184 | }; |
| 185 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 186 | static const struct clkgen_quadfs_data st_fs660c32_E_416 = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 187 | .npda = CLKGEN_FIELD(0x0, 0x1, 14), |
| 188 | .nsb = { CLKGEN_FIELD(0x0, 0x1, 10), |
| 189 | CLKGEN_FIELD(0x0, 0x1, 11), |
| 190 | CLKGEN_FIELD(0x0, 0x1, 12), |
| 191 | CLKGEN_FIELD(0x0, 0x1, 13) }, |
| 192 | .nsdiv_present = true, |
| 193 | .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18), |
| 194 | CLKGEN_FIELD(0x0, 0x1, 19), |
| 195 | CLKGEN_FIELD(0x0, 0x1, 20), |
| 196 | CLKGEN_FIELD(0x0, 0x1, 21) }, |
| 197 | .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0), |
| 198 | CLKGEN_FIELD(0x14, 0x1f, 0), |
| 199 | CLKGEN_FIELD(0x24, 0x1f, 0), |
| 200 | CLKGEN_FIELD(0x34, 0x1f, 0) }, |
| 201 | .en = { CLKGEN_FIELD(0x10, 0x1, 0), |
| 202 | CLKGEN_FIELD(0x20, 0x1, 0), |
| 203 | CLKGEN_FIELD(0x30, 0x1, 0), |
| 204 | CLKGEN_FIELD(0x40, 0x1, 0) }, |
| 205 | .ndiv = CLKGEN_FIELD(0x0, 0x7, 15), |
| 206 | .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0), |
| 207 | CLKGEN_FIELD(0x18, 0x7fff, 0), |
| 208 | CLKGEN_FIELD(0x28, 0x7fff, 0), |
| 209 | CLKGEN_FIELD(0x38, 0x7fff, 0) }, |
| 210 | .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0), |
| 211 | CLKGEN_FIELD(0x1C, 0xf, 0), |
| 212 | CLKGEN_FIELD(0x2C, 0xf, 0), |
| 213 | CLKGEN_FIELD(0x3C, 0xf, 0) }, |
| 214 | .lockstatus_present = true, |
| 215 | .lock_status = CLKGEN_FIELD(0xAC, 0x1, 0), |
| 216 | .pll_ops = &st_quadfs_pll_c32_ops, |
| 217 | .rtbl = fs660c32_rtbl, |
| 218 | .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl), |
| 219 | .get_rate = clk_fs660c32_dig_get_rate, |
| 220 | }; |
| 221 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 222 | static const struct clkgen_quadfs_data st_fs660c32_F_416 = { |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 223 | .npda = CLKGEN_FIELD(0x0, 0x1, 14), |
| 224 | .nsb = { CLKGEN_FIELD(0x0, 0x1, 10), |
| 225 | CLKGEN_FIELD(0x0, 0x1, 11), |
| 226 | CLKGEN_FIELD(0x0, 0x1, 12), |
| 227 | CLKGEN_FIELD(0x0, 0x1, 13) }, |
| 228 | .nsdiv_present = true, |
| 229 | .nsdiv = { CLKGEN_FIELD(0x0, 0x1, 18), |
| 230 | CLKGEN_FIELD(0x0, 0x1, 19), |
| 231 | CLKGEN_FIELD(0x0, 0x1, 20), |
| 232 | CLKGEN_FIELD(0x0, 0x1, 21) }, |
| 233 | .mdiv = { CLKGEN_FIELD(0x4, 0x1f, 0), |
| 234 | CLKGEN_FIELD(0x14, 0x1f, 0), |
| 235 | CLKGEN_FIELD(0x24, 0x1f, 0), |
| 236 | CLKGEN_FIELD(0x34, 0x1f, 0) }, |
| 237 | .en = { CLKGEN_FIELD(0x10, 0x1, 0), |
| 238 | CLKGEN_FIELD(0x20, 0x1, 0), |
| 239 | CLKGEN_FIELD(0x30, 0x1, 0), |
| 240 | CLKGEN_FIELD(0x40, 0x1, 0) }, |
| 241 | .ndiv = CLKGEN_FIELD(0x0, 0x7, 15), |
| 242 | .pe = { CLKGEN_FIELD(0x8, 0x7fff, 0), |
| 243 | CLKGEN_FIELD(0x18, 0x7fff, 0), |
| 244 | CLKGEN_FIELD(0x28, 0x7fff, 0), |
| 245 | CLKGEN_FIELD(0x38, 0x7fff, 0) }, |
| 246 | .sdiv = { CLKGEN_FIELD(0xC, 0xf, 0), |
| 247 | CLKGEN_FIELD(0x1C, 0xf, 0), |
| 248 | CLKGEN_FIELD(0x2C, 0xf, 0), |
| 249 | CLKGEN_FIELD(0x3C, 0xf, 0) }, |
| 250 | .lockstatus_present = true, |
| 251 | .lock_status = CLKGEN_FIELD(0xEC, 0x1, 0), |
| 252 | .pll_ops = &st_quadfs_pll_c32_ops, |
| 253 | .rtbl = fs660c32_rtbl, |
| 254 | .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl), |
| 255 | .get_rate = clk_fs660c32_dig_get_rate, |
| 256 | }; |
| 257 | |
Gabriel FERNANDEZ | 51306d5 | 2014-07-15 17:20:27 +0200 | [diff] [blame^] | 258 | static const struct clkgen_quadfs_data st_fs660c32_C_407 = { |
| 259 | .nrst_present = true, |
| 260 | .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0), |
| 261 | CLKGEN_FIELD(0x2f0, 0x1, 1), |
| 262 | CLKGEN_FIELD(0x2f0, 0x1, 2), |
| 263 | CLKGEN_FIELD(0x2f0, 0x1, 3) }, |
| 264 | .npda = CLKGEN_FIELD(0x2f0, 0x1, 12), |
| 265 | .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8), |
| 266 | CLKGEN_FIELD(0x2f0, 0x1, 9), |
| 267 | CLKGEN_FIELD(0x2f0, 0x1, 10), |
| 268 | CLKGEN_FIELD(0x2f0, 0x1, 11) }, |
| 269 | .nsdiv_present = true, |
| 270 | .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24), |
| 271 | CLKGEN_FIELD(0x308, 0x1, 24), |
| 272 | CLKGEN_FIELD(0x30c, 0x1, 24), |
| 273 | CLKGEN_FIELD(0x310, 0x1, 24) }, |
| 274 | .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15), |
| 275 | CLKGEN_FIELD(0x308, 0x1f, 15), |
| 276 | CLKGEN_FIELD(0x30c, 0x1f, 15), |
| 277 | CLKGEN_FIELD(0x310, 0x1f, 15) }, |
| 278 | .en = { CLKGEN_FIELD(0x2fc, 0x1, 0), |
| 279 | CLKGEN_FIELD(0x2fc, 0x1, 1), |
| 280 | CLKGEN_FIELD(0x2fc, 0x1, 2), |
| 281 | CLKGEN_FIELD(0x2fc, 0x1, 3) }, |
| 282 | .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16), |
| 283 | .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0), |
| 284 | CLKGEN_FIELD(0x308, 0x7fff, 0), |
| 285 | CLKGEN_FIELD(0x30c, 0x7fff, 0), |
| 286 | CLKGEN_FIELD(0x310, 0x7fff, 0) }, |
| 287 | .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20), |
| 288 | CLKGEN_FIELD(0x308, 0xf, 20), |
| 289 | CLKGEN_FIELD(0x30c, 0xf, 20), |
| 290 | CLKGEN_FIELD(0x310, 0xf, 20) }, |
| 291 | .lockstatus_present = true, |
| 292 | .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24), |
| 293 | .powerup_polarity = 1, |
| 294 | .standby_polarity = 1, |
| 295 | .pll_ops = &st_quadfs_pll_c32_ops, |
| 296 | .rtbl = fs660c32_rtbl, |
| 297 | .rtbl_cnt = ARRAY_SIZE(fs660c32_rtbl), |
| 298 | .get_rate = clk_fs660c32_dig_get_rate, |
| 299 | }; |
| 300 | |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 301 | /** |
| 302 | * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor |
| 303 | * |
| 304 | * Traits of this clock: |
| 305 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 306 | * enable - clk_enable and clk_disable are functional & control the Fsyn |
| 307 | * rate - inherits rate from parent. set_rate/round_rate/recalc_rate |
| 308 | * parent - fixed parent. No clk_set_parent support |
| 309 | */ |
| 310 | |
| 311 | /** |
| 312 | * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of |
| 313 | * its parent clock, found inside a type of |
| 314 | * ST quad channel frequency synthesizer block |
| 315 | * |
| 316 | * @hw: handle between common and hardware-specific interfaces. |
| 317 | * @ndiv: regmap field for the ndiv control. |
| 318 | * @regs_base: base address of the configuration registers. |
| 319 | * @lock: spinlock. |
| 320 | * |
| 321 | */ |
| 322 | struct st_clk_quadfs_pll { |
| 323 | struct clk_hw hw; |
| 324 | void __iomem *regs_base; |
| 325 | spinlock_t *lock; |
| 326 | struct clkgen_quadfs_data *data; |
| 327 | u32 ndiv; |
| 328 | }; |
| 329 | |
| 330 | #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw) |
| 331 | |
| 332 | static int quadfs_pll_enable(struct clk_hw *hw) |
| 333 | { |
| 334 | struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw); |
| 335 | unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10); |
| 336 | |
| 337 | if (pll->lock) |
| 338 | spin_lock_irqsave(pll->lock, flags); |
| 339 | |
| 340 | /* |
| 341 | * Bring block out of reset if we have reset control. |
| 342 | */ |
| 343 | if (pll->data->reset_present) |
| 344 | CLKGEN_WRITE(pll, nreset, 1); |
| 345 | |
| 346 | /* |
| 347 | * Use a fixed input clock noise bandwidth filter for the moment |
| 348 | */ |
| 349 | if (pll->data->bwfilter_present) |
| 350 | CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF); |
| 351 | |
| 352 | |
| 353 | CLKGEN_WRITE(pll, ndiv, pll->ndiv); |
| 354 | |
| 355 | /* |
| 356 | * Power up the PLL |
| 357 | */ |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 358 | CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 359 | |
| 360 | if (pll->lock) |
| 361 | spin_unlock_irqrestore(pll->lock, flags); |
| 362 | |
| 363 | if (pll->data->lockstatus_present) |
| 364 | while (!CLKGEN_READ(pll, lock_status)) { |
| 365 | if (time_after(jiffies, timeout)) |
| 366 | return -ETIMEDOUT; |
| 367 | cpu_relax(); |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static void quadfs_pll_disable(struct clk_hw *hw) |
| 374 | { |
| 375 | struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw); |
| 376 | unsigned long flags = 0; |
| 377 | |
| 378 | if (pll->lock) |
| 379 | spin_lock_irqsave(pll->lock, flags); |
| 380 | |
| 381 | /* |
| 382 | * Powerdown the PLL and then put block into soft reset if we have |
| 383 | * reset control. |
| 384 | */ |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 385 | CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 386 | |
| 387 | if (pll->data->reset_present) |
| 388 | CLKGEN_WRITE(pll, nreset, 0); |
| 389 | |
| 390 | if (pll->lock) |
| 391 | spin_unlock_irqrestore(pll->lock, flags); |
| 392 | } |
| 393 | |
| 394 | static int quadfs_pll_is_enabled(struct clk_hw *hw) |
| 395 | { |
| 396 | struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw); |
| 397 | u32 npda = CLKGEN_READ(pll, npda); |
| 398 | |
| 399 | return !!npda; |
| 400 | } |
| 401 | |
| 402 | int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs, |
| 403 | unsigned long *rate) |
| 404 | { |
| 405 | unsigned long nd = fs->ndiv + 16; /* ndiv value */ |
| 406 | |
| 407 | *rate = input * nd; |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw, |
| 413 | unsigned long parent_rate) |
| 414 | { |
| 415 | struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw); |
| 416 | unsigned long rate = 0; |
| 417 | struct stm_fs params; |
| 418 | |
| 419 | params.ndiv = CLKGEN_READ(pll, ndiv); |
| 420 | if (clk_fs660c32_vco_get_rate(parent_rate, ¶ms, &rate)) |
| 421 | pr_err("%s:%s error calculating rate\n", |
| 422 | __clk_get_name(hw->clk), __func__); |
| 423 | |
| 424 | pll->ndiv = params.ndiv; |
| 425 | |
| 426 | return rate; |
| 427 | } |
| 428 | |
| 429 | int clk_fs660c32_vco_get_params(unsigned long input, |
| 430 | unsigned long output, struct stm_fs *fs) |
| 431 | { |
| 432 | /* Formula |
| 433 | VCO frequency = (fin x ndiv) / pdiv |
| 434 | ndiv = VCOfreq * pdiv / fin |
| 435 | */ |
| 436 | unsigned long pdiv = 1, n; |
| 437 | |
| 438 | /* Output clock range: 384Mhz to 660Mhz */ |
| 439 | if (output < 384000000 || output > 660000000) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | if (input > 40000000) |
| 443 | /* This means that PDIV would be 2 instead of 1. |
| 444 | Not supported today. */ |
| 445 | return -EINVAL; |
| 446 | |
| 447 | input /= 1000; |
| 448 | output /= 1000; |
| 449 | |
| 450 | n = output * pdiv / input; |
| 451 | if (n < 16) |
| 452 | n = 16; |
| 453 | fs->ndiv = n - 16; /* Converting formula value to reg value */ |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw, unsigned long rate |
| 459 | , unsigned long *prate) |
| 460 | { |
| 461 | struct stm_fs params; |
| 462 | |
| 463 | if (!clk_fs660c32_vco_get_params(*prate, rate, ¶ms)) |
| 464 | clk_fs660c32_vco_get_rate(*prate, ¶ms, &rate); |
| 465 | |
| 466 | pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n", |
| 467 | __func__, __clk_get_name(hw->clk), |
| 468 | rate, (unsigned int)params.sdiv, |
| 469 | (unsigned int)params.mdiv, |
| 470 | (unsigned int)params.pe, (unsigned int)params.nsdiv); |
| 471 | |
| 472 | return rate; |
| 473 | } |
| 474 | |
| 475 | static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate, |
| 476 | unsigned long parent_rate) |
| 477 | { |
| 478 | struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw); |
| 479 | struct stm_fs params; |
| 480 | long hwrate = 0; |
| 481 | unsigned long flags = 0; |
| 482 | |
| 483 | if (!rate || !parent_rate) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | if (!clk_fs660c32_vco_get_params(parent_rate, rate, ¶ms)) |
| 487 | clk_fs660c32_vco_get_rate(parent_rate, ¶ms, &hwrate); |
| 488 | |
| 489 | pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n", |
| 490 | __func__, __clk_get_name(hw->clk), |
| 491 | hwrate, (unsigned int)params.ndiv); |
| 492 | |
| 493 | if (!hwrate) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | pll->ndiv = params.ndiv; |
| 497 | |
| 498 | if (pll->lock) |
| 499 | spin_lock_irqsave(pll->lock, flags); |
| 500 | |
| 501 | CLKGEN_WRITE(pll, ndiv, pll->ndiv); |
| 502 | |
| 503 | if (pll->lock) |
| 504 | spin_unlock_irqrestore(pll->lock, flags); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static const struct clk_ops st_quadfs_pll_c65_ops = { |
| 510 | .enable = quadfs_pll_enable, |
| 511 | .disable = quadfs_pll_disable, |
| 512 | .is_enabled = quadfs_pll_is_enabled, |
| 513 | }; |
| 514 | |
| 515 | static const struct clk_ops st_quadfs_pll_c32_ops = { |
| 516 | .enable = quadfs_pll_enable, |
| 517 | .disable = quadfs_pll_disable, |
| 518 | .is_enabled = quadfs_pll_is_enabled, |
| 519 | .recalc_rate = quadfs_pll_fs660c32_recalc_rate, |
| 520 | .round_rate = quadfs_pll_fs660c32_round_rate, |
| 521 | .set_rate = quadfs_pll_fs660c32_set_rate, |
| 522 | }; |
| 523 | |
| 524 | static struct clk * __init st_clk_register_quadfs_pll( |
| 525 | const char *name, const char *parent_name, |
| 526 | struct clkgen_quadfs_data *quadfs, void __iomem *reg, |
| 527 | spinlock_t *lock) |
| 528 | { |
| 529 | struct st_clk_quadfs_pll *pll; |
| 530 | struct clk *clk; |
| 531 | struct clk_init_data init; |
| 532 | |
| 533 | /* |
| 534 | * Sanity check required pointers. |
| 535 | */ |
| 536 | if (WARN_ON(!name || !parent_name)) |
| 537 | return ERR_PTR(-EINVAL); |
| 538 | |
| 539 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 540 | if (!pll) |
| 541 | return ERR_PTR(-ENOMEM); |
| 542 | |
| 543 | init.name = name; |
| 544 | init.ops = quadfs->pll_ops; |
| 545 | init.flags = CLK_IS_BASIC; |
| 546 | init.parent_names = &parent_name; |
| 547 | init.num_parents = 1; |
| 548 | |
| 549 | pll->data = quadfs; |
| 550 | pll->regs_base = reg; |
| 551 | pll->lock = lock; |
| 552 | pll->hw.init = &init; |
| 553 | |
| 554 | clk = clk_register(NULL, &pll->hw); |
| 555 | |
| 556 | if (IS_ERR(clk)) |
| 557 | kfree(pll); |
| 558 | |
| 559 | return clk; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * DOC: A digital frequency synthesizer |
| 564 | * |
| 565 | * Traits of this clock: |
| 566 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 567 | * enable - clk_enable and clk_disable are functional |
| 568 | * rate - set rate is functional |
| 569 | * parent - fixed parent. No clk_set_parent support |
| 570 | */ |
| 571 | |
| 572 | /** |
| 573 | * struct st_clk_quadfs_fsynth - One clock output from a four channel digital |
| 574 | * frequency synthesizer (fsynth) block. |
| 575 | * |
| 576 | * @hw: handle between common and hardware-specific interfaces |
| 577 | * |
| 578 | * @nsb: regmap field in the output control register for the digital |
| 579 | * standby of this fsynth channel. This control is active low so |
| 580 | * the channel is in standby when the control bit is cleared. |
| 581 | * |
| 582 | * @nsdiv: regmap field in the output control register for |
| 583 | * for the optional divide by 3 of this fsynth channel. This control |
| 584 | * is active low so the divide by 3 is active when the control bit is |
| 585 | * cleared and the divide is bypassed when the bit is set. |
| 586 | */ |
| 587 | struct st_clk_quadfs_fsynth { |
| 588 | struct clk_hw hw; |
| 589 | void __iomem *regs_base; |
| 590 | spinlock_t *lock; |
| 591 | struct clkgen_quadfs_data *data; |
| 592 | |
| 593 | u32 chan; |
| 594 | /* |
| 595 | * Cached hardware values from set_rate so we can program the |
| 596 | * hardware in enable. There are two reasons for this: |
| 597 | * |
| 598 | * 1. The registers may not be writable until the parent has been |
| 599 | * enabled. |
| 600 | * |
| 601 | * 2. It restores the clock rate when a driver does an enable |
| 602 | * on PM restore, after a suspend to RAM has lost the hardware |
| 603 | * setup. |
| 604 | */ |
| 605 | u32 md; |
| 606 | u32 pe; |
| 607 | u32 sdiv; |
| 608 | u32 nsdiv; |
| 609 | }; |
| 610 | |
| 611 | #define to_quadfs_fsynth(_hw) \ |
| 612 | container_of(_hw, struct st_clk_quadfs_fsynth, hw) |
| 613 | |
| 614 | static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs) |
| 615 | { |
| 616 | /* |
| 617 | * Pulse the program enable register lsb to make the hardware take |
| 618 | * notice of the new md/pe values with a glitchless transition. |
| 619 | */ |
| 620 | CLKGEN_WRITE(fs, en[fs->chan], 1); |
| 621 | CLKGEN_WRITE(fs, en[fs->chan], 0); |
| 622 | } |
| 623 | |
| 624 | static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs) |
| 625 | { |
| 626 | unsigned long flags = 0; |
| 627 | |
| 628 | /* |
| 629 | * Ensure the md/pe parameters are ignored while we are |
| 630 | * reprogramming them so we can get a glitchless change |
| 631 | * when fine tuning the speed of a running clock. |
| 632 | */ |
| 633 | CLKGEN_WRITE(fs, en[fs->chan], 0); |
| 634 | |
| 635 | CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md); |
| 636 | CLKGEN_WRITE(fs, pe[fs->chan], fs->pe); |
| 637 | CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv); |
| 638 | |
| 639 | if (fs->lock) |
| 640 | spin_lock_irqsave(fs->lock, flags); |
| 641 | |
| 642 | if (fs->data->nsdiv_present) |
| 643 | CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv); |
| 644 | |
| 645 | if (fs->lock) |
| 646 | spin_unlock_irqrestore(fs->lock, flags); |
| 647 | } |
| 648 | |
| 649 | static int quadfs_fsynth_enable(struct clk_hw *hw) |
| 650 | { |
| 651 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 652 | unsigned long flags = 0; |
| 653 | |
| 654 | pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk)); |
| 655 | |
| 656 | quadfs_fsynth_program_rate(fs); |
| 657 | |
| 658 | if (fs->lock) |
| 659 | spin_lock_irqsave(fs->lock, flags); |
| 660 | |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 661 | CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 662 | |
Gabriel FERNANDEZ | fc755c8 | 2014-07-15 17:20:26 +0200 | [diff] [blame] | 663 | if (fs->data->nrst_present) |
| 664 | CLKGEN_WRITE(fs, nrst[fs->chan], 0); |
| 665 | |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 666 | if (fs->lock) |
| 667 | spin_unlock_irqrestore(fs->lock, flags); |
| 668 | |
| 669 | quadfs_fsynth_program_enable(fs); |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static void quadfs_fsynth_disable(struct clk_hw *hw) |
| 675 | { |
| 676 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 677 | unsigned long flags = 0; |
| 678 | |
| 679 | pr_debug("%s: %s\n", __func__, __clk_get_name(hw->clk)); |
| 680 | |
| 681 | if (fs->lock) |
| 682 | spin_lock_irqsave(fs->lock, flags); |
| 683 | |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 684 | CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 685 | |
| 686 | if (fs->lock) |
| 687 | spin_unlock_irqrestore(fs->lock, flags); |
| 688 | } |
| 689 | |
| 690 | static int quadfs_fsynth_is_enabled(struct clk_hw *hw) |
| 691 | { |
| 692 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 693 | u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]); |
| 694 | |
| 695 | pr_debug("%s: %s enable bit = 0x%x\n", |
| 696 | __func__, __clk_get_name(hw->clk), nsb); |
| 697 | |
Gabriel FERNANDEZ | 8f26df8 | 2014-07-15 17:20:25 +0200 | [diff] [blame] | 698 | return fs->data->standby_polarity ? !nsb : !!nsb; |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | #define P15 (uint64_t)(1 << 15) |
| 702 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 703 | static int clk_fs216c65_get_rate(unsigned long input, const struct stm_fs *fs, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 704 | unsigned long *rate) |
| 705 | { |
| 706 | uint64_t res; |
| 707 | unsigned long ns; |
| 708 | unsigned long nd = 8; /* ndiv stuck at 0 => val = 8 */ |
| 709 | unsigned long s; |
| 710 | long m; |
| 711 | |
| 712 | m = fs->mdiv - 32; |
| 713 | s = 1 << (fs->sdiv + 1); |
| 714 | ns = (fs->nsdiv ? 1 : 3); |
| 715 | |
| 716 | res = (uint64_t)(s * ns * P15 * (uint64_t)(m + 33)); |
| 717 | res = res - (s * ns * fs->pe); |
| 718 | *rate = div64_u64(P15 * nd * input * 32, res); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 723 | static int clk_fs432c65_get_rate(unsigned long input, const struct stm_fs *fs, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 724 | unsigned long *rate) |
| 725 | { |
| 726 | uint64_t res; |
| 727 | unsigned long nd = 16; /* ndiv value; stuck at 0 (30Mhz input) */ |
| 728 | long m; |
| 729 | unsigned long sd; |
| 730 | unsigned long ns; |
| 731 | |
| 732 | m = fs->mdiv - 32; |
| 733 | sd = 1 << (fs->sdiv + 1); |
| 734 | ns = (fs->nsdiv ? 1 : 3); |
| 735 | |
| 736 | res = (uint64_t)(sd * ns * P15 * (uint64_t)(m + 33)); |
| 737 | res = res - (sd * ns * fs->pe); |
| 738 | *rate = div64_u64(P15 * nd * input * 32, res); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | #define P20 (uint64_t)(1 << 20) |
| 744 | |
| 745 | static int clk_fs660c32_dig_get_rate(unsigned long input, |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 746 | const struct stm_fs *fs, unsigned long *rate) |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 747 | { |
| 748 | unsigned long s = (1 << fs->sdiv); |
| 749 | unsigned long ns; |
| 750 | uint64_t res; |
| 751 | |
| 752 | /* |
| 753 | * 'nsdiv' is a register value ('BIN') which is translated |
| 754 | * to a decimal value according to following rules. |
| 755 | * |
| 756 | * nsdiv ns.dec |
| 757 | * 0 3 |
| 758 | * 1 1 |
| 759 | */ |
| 760 | ns = (fs->nsdiv == 1) ? 1 : 3; |
| 761 | |
| 762 | res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns; |
| 763 | *rate = (unsigned long)div64_u64(input * P20 * 32, res); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs, |
| 769 | struct stm_fs *params) |
| 770 | { |
| 771 | /* |
| 772 | * Get the initial hardware values for recalc_rate |
| 773 | */ |
| 774 | params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]); |
| 775 | params->pe = CLKGEN_READ(fs, pe[fs->chan]); |
| 776 | params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]); |
| 777 | |
| 778 | if (fs->data->nsdiv_present) |
| 779 | params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]); |
| 780 | else |
| 781 | params->nsdiv = 1; |
| 782 | |
| 783 | /* |
| 784 | * If All are NULL then assume no clock rate is programmed. |
| 785 | */ |
| 786 | if (!params->mdiv && !params->pe && !params->sdiv) |
| 787 | return 1; |
| 788 | |
| 789 | fs->md = params->mdiv; |
| 790 | fs->pe = params->pe; |
| 791 | fs->sdiv = params->sdiv; |
| 792 | fs->nsdiv = params->nsdiv; |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate, |
| 798 | unsigned long prate, struct stm_fs *params) |
| 799 | { |
| 800 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 801 | int (*clk_fs_get_rate)(unsigned long , |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 802 | const struct stm_fs *, unsigned long *); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 803 | struct stm_fs prev_params; |
| 804 | unsigned long prev_rate, rate = 0; |
| 805 | unsigned long diff_rate, prev_diff_rate = ~0; |
| 806 | int index; |
| 807 | |
| 808 | clk_fs_get_rate = fs->data->get_rate; |
| 809 | |
| 810 | for (index = 0; index < fs->data->rtbl_cnt; index++) { |
| 811 | prev_rate = rate; |
| 812 | |
| 813 | *params = fs->data->rtbl[index]; |
| 814 | prev_params = *params; |
| 815 | |
| 816 | clk_fs_get_rate(prate, &fs->data->rtbl[index], &rate); |
| 817 | |
| 818 | diff_rate = abs(drate - rate); |
| 819 | |
| 820 | if (diff_rate > prev_diff_rate) { |
| 821 | rate = prev_rate; |
| 822 | *params = prev_params; |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | prev_diff_rate = diff_rate; |
| 827 | |
| 828 | if (drate == rate) |
| 829 | return rate; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | if (index == fs->data->rtbl_cnt) |
| 834 | *params = prev_params; |
| 835 | |
| 836 | return rate; |
| 837 | } |
| 838 | |
| 839 | static unsigned long quadfs_recalc_rate(struct clk_hw *hw, |
| 840 | unsigned long parent_rate) |
| 841 | { |
| 842 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 843 | unsigned long rate = 0; |
| 844 | struct stm_fs params; |
| 845 | int (*clk_fs_get_rate)(unsigned long , |
Gabriel FERNANDEZ | 4abb1b4 | 2014-07-15 17:20:18 +0200 | [diff] [blame] | 846 | const struct stm_fs *, unsigned long *); |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 847 | |
| 848 | clk_fs_get_rate = fs->data->get_rate; |
| 849 | |
| 850 | if (quadfs_fsynt_get_hw_value_for_recalc(fs, ¶ms)) |
| 851 | return 0; |
| 852 | |
| 853 | if (clk_fs_get_rate(parent_rate, ¶ms, &rate)) { |
| 854 | pr_err("%s:%s error calculating rate\n", |
| 855 | __clk_get_name(hw->clk), __func__); |
| 856 | } |
| 857 | |
| 858 | pr_debug("%s:%s rate %lu\n", __clk_get_name(hw->clk), __func__, rate); |
| 859 | |
| 860 | return rate; |
| 861 | } |
| 862 | |
| 863 | static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate, |
| 864 | unsigned long *prate) |
| 865 | { |
| 866 | struct stm_fs params; |
| 867 | |
| 868 | rate = quadfs_find_best_rate(hw, rate, *prate, ¶ms); |
| 869 | |
| 870 | pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n", |
| 871 | __func__, __clk_get_name(hw->clk), |
| 872 | rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv, |
| 873 | (unsigned int)params.pe, (unsigned int)params.nsdiv); |
| 874 | |
| 875 | return rate; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs, |
| 880 | struct stm_fs *params) |
| 881 | { |
| 882 | fs->md = params->mdiv; |
| 883 | fs->pe = params->pe; |
| 884 | fs->sdiv = params->sdiv; |
| 885 | fs->nsdiv = params->nsdiv; |
| 886 | |
| 887 | /* |
| 888 | * In some integrations you can only change the fsynth programming when |
| 889 | * the parent entity containing it is enabled. |
| 890 | */ |
| 891 | quadfs_fsynth_program_rate(fs); |
| 892 | quadfs_fsynth_program_enable(fs); |
| 893 | } |
| 894 | |
| 895 | static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate, |
| 896 | unsigned long parent_rate) |
| 897 | { |
| 898 | struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw); |
| 899 | struct stm_fs params; |
| 900 | long hwrate; |
| 901 | int uninitialized_var(i); |
| 902 | |
| 903 | if (!rate || !parent_rate) |
| 904 | return -EINVAL; |
| 905 | |
| 906 | memset(¶ms, 0, sizeof(struct stm_fs)); |
| 907 | |
| 908 | hwrate = quadfs_find_best_rate(hw, rate, parent_rate, ¶ms); |
| 909 | if (!hwrate) |
| 910 | return -EINVAL; |
| 911 | |
| 912 | quadfs_program_and_enable(fs, ¶ms); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | |
| 919 | static const struct clk_ops st_quadfs_ops = { |
| 920 | .enable = quadfs_fsynth_enable, |
| 921 | .disable = quadfs_fsynth_disable, |
| 922 | .is_enabled = quadfs_fsynth_is_enabled, |
| 923 | .round_rate = quadfs_round_rate, |
| 924 | .set_rate = quadfs_set_rate, |
| 925 | .recalc_rate = quadfs_recalc_rate, |
| 926 | }; |
| 927 | |
| 928 | static struct clk * __init st_clk_register_quadfs_fsynth( |
| 929 | const char *name, const char *parent_name, |
| 930 | struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan, |
| 931 | spinlock_t *lock) |
| 932 | { |
| 933 | struct st_clk_quadfs_fsynth *fs; |
| 934 | struct clk *clk; |
| 935 | struct clk_init_data init; |
| 936 | |
| 937 | /* |
| 938 | * Sanity check required pointers, note that nsdiv3 is optional. |
| 939 | */ |
| 940 | if (WARN_ON(!name || !parent_name)) |
| 941 | return ERR_PTR(-EINVAL); |
| 942 | |
| 943 | fs = kzalloc(sizeof(*fs), GFP_KERNEL); |
| 944 | if (!fs) |
| 945 | return ERR_PTR(-ENOMEM); |
| 946 | |
| 947 | init.name = name; |
| 948 | init.ops = &st_quadfs_ops; |
| 949 | init.flags = CLK_GET_RATE_NOCACHE | CLK_IS_BASIC; |
| 950 | init.parent_names = &parent_name; |
| 951 | init.num_parents = 1; |
| 952 | |
| 953 | fs->data = quadfs; |
| 954 | fs->regs_base = reg; |
| 955 | fs->chan = chan; |
| 956 | fs->lock = lock; |
| 957 | fs->hw.init = &init; |
| 958 | |
| 959 | clk = clk_register(NULL, &fs->hw); |
| 960 | |
| 961 | if (IS_ERR(clk)) |
| 962 | kfree(fs); |
| 963 | |
| 964 | return clk; |
| 965 | } |
| 966 | |
| 967 | static struct of_device_id quadfs_of_match[] = { |
| 968 | { |
| 969 | .compatible = "st,stih416-quadfs216", |
Gabriel FERNANDEZ | 79bb8aa1 | 2014-07-15 17:20:20 +0200 | [diff] [blame] | 970 | .data = &st_fs216c65_416 |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 971 | }, |
| 972 | { |
| 973 | .compatible = "st,stih416-quadfs432", |
Gabriel FERNANDEZ | 79bb8aa1 | 2014-07-15 17:20:20 +0200 | [diff] [blame] | 974 | .data = &st_fs432c65_416 |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 975 | }, |
| 976 | { |
| 977 | .compatible = "st,stih416-quadfs660-E", |
Gabriel FERNANDEZ | 79bb8aa1 | 2014-07-15 17:20:20 +0200 | [diff] [blame] | 978 | .data = &st_fs660c32_E_416 |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 979 | }, |
| 980 | { |
| 981 | .compatible = "st,stih416-quadfs660-F", |
Gabriel FERNANDEZ | 79bb8aa1 | 2014-07-15 17:20:20 +0200 | [diff] [blame] | 982 | .data = &st_fs660c32_F_416 |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 983 | }, |
Gabriel FERNANDEZ | 51306d5 | 2014-07-15 17:20:27 +0200 | [diff] [blame^] | 984 | { |
| 985 | .compatible = "st,stih407-quadfs660-C", |
| 986 | .data = &st_fs660c32_C_407 |
| 987 | }, |
| 988 | { |
| 989 | .compatible = "st,stih407-quadfs660-D", |
| 990 | .data = &st_fs660c32_D_407 |
| 991 | }, |
Gabriel FERNANDEZ | 5f7aa90 | 2014-02-27 16:24:17 +0100 | [diff] [blame] | 992 | {} |
| 993 | }; |
| 994 | |
| 995 | static void __init st_of_create_quadfs_fsynths( |
| 996 | struct device_node *np, const char *pll_name, |
| 997 | struct clkgen_quadfs_data *quadfs, void __iomem *reg, |
| 998 | spinlock_t *lock) |
| 999 | { |
| 1000 | struct clk_onecell_data *clk_data; |
| 1001 | int fschan; |
| 1002 | |
| 1003 | clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL); |
| 1004 | if (!clk_data) |
| 1005 | return; |
| 1006 | |
| 1007 | clk_data->clk_num = QUADFS_MAX_CHAN; |
| 1008 | clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *), |
| 1009 | GFP_KERNEL); |
| 1010 | |
| 1011 | if (!clk_data->clks) { |
| 1012 | kfree(clk_data); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) { |
| 1017 | struct clk *clk; |
| 1018 | const char *clk_name; |
| 1019 | |
| 1020 | if (of_property_read_string_index(np, "clock-output-names", |
| 1021 | fschan, &clk_name)) { |
| 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * If we read an empty clock name then the channel is unused |
| 1027 | */ |
| 1028 | if (*clk_name == '\0') |
| 1029 | continue; |
| 1030 | |
| 1031 | clk = st_clk_register_quadfs_fsynth(clk_name, pll_name, |
| 1032 | quadfs, reg, fschan, lock); |
| 1033 | |
| 1034 | /* |
| 1035 | * If there was an error registering this clock output, clean |
| 1036 | * up and move on to the next one. |
| 1037 | */ |
| 1038 | if (!IS_ERR(clk)) { |
| 1039 | clk_data->clks[fschan] = clk; |
| 1040 | pr_debug("%s: parent %s rate %u\n", |
| 1041 | __clk_get_name(clk), |
| 1042 | __clk_get_name(clk_get_parent(clk)), |
| 1043 | (unsigned int)clk_get_rate(clk)); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); |
| 1048 | } |
| 1049 | |
| 1050 | static void __init st_of_quadfs_setup(struct device_node *np) |
| 1051 | { |
| 1052 | const struct of_device_id *match; |
| 1053 | struct clk *clk; |
| 1054 | const char *pll_name, *clk_parent_name; |
| 1055 | void __iomem *reg; |
| 1056 | spinlock_t *lock; |
| 1057 | |
| 1058 | match = of_match_node(quadfs_of_match, np); |
| 1059 | if (WARN_ON(!match)) |
| 1060 | return; |
| 1061 | |
| 1062 | reg = of_iomap(np, 0); |
| 1063 | if (!reg) |
| 1064 | return; |
| 1065 | |
| 1066 | clk_parent_name = of_clk_get_parent_name(np, 0); |
| 1067 | if (!clk_parent_name) |
| 1068 | return; |
| 1069 | |
| 1070 | pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name); |
| 1071 | if (!pll_name) |
| 1072 | return; |
| 1073 | |
| 1074 | lock = kzalloc(sizeof(*lock), GFP_KERNEL); |
| 1075 | if (!lock) |
| 1076 | goto err_exit; |
| 1077 | |
| 1078 | spin_lock_init(lock); |
| 1079 | |
| 1080 | clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name, |
| 1081 | (struct clkgen_quadfs_data *) match->data, reg, lock); |
| 1082 | if (IS_ERR(clk)) |
| 1083 | goto err_exit; |
| 1084 | else |
| 1085 | pr_debug("%s: parent %s rate %u\n", |
| 1086 | __clk_get_name(clk), |
| 1087 | __clk_get_name(clk_get_parent(clk)), |
| 1088 | (unsigned int)clk_get_rate(clk)); |
| 1089 | |
| 1090 | st_of_create_quadfs_fsynths(np, pll_name, |
| 1091 | (struct clkgen_quadfs_data *)match->data, |
| 1092 | reg, lock); |
| 1093 | |
| 1094 | err_exit: |
| 1095 | kfree(pll_name); /* No longer need local copy of the PLL name */ |
| 1096 | } |
| 1097 | CLK_OF_DECLARE(quadfs, "st,quadfs", st_of_quadfs_setup); |