rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/export.h> |
| 19 | #include <linux/clk-provider.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/math64.h> |
| 23 | |
| 24 | #include <asm/div64.h> |
| 25 | |
| 26 | #include "clk-rcg.h" |
| 27 | #include "common.h" |
| 28 | |
| 29 | #define CMD_REG 0x0 |
| 30 | #define CMD_UPDATE BIT(0) |
| 31 | #define CMD_ROOT_EN BIT(1) |
| 32 | #define CMD_DIRTY_CFG BIT(4) |
| 33 | #define CMD_DIRTY_N BIT(5) |
| 34 | #define CMD_DIRTY_M BIT(6) |
| 35 | #define CMD_DIRTY_D BIT(7) |
| 36 | #define CMD_ROOT_OFF BIT(31) |
| 37 | |
| 38 | #define CFG_REG 0x4 |
| 39 | #define CFG_SRC_DIV_SHIFT 0 |
| 40 | #define CFG_SRC_SEL_SHIFT 8 |
| 41 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT) |
| 42 | #define CFG_MODE_SHIFT 12 |
| 43 | #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT) |
| 44 | #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT) |
| 45 | |
| 46 | #define M_REG 0x8 |
| 47 | #define N_REG 0xc |
| 48 | #define D_REG 0x10 |
| 49 | |
| 50 | enum freq_policy { |
| 51 | FLOOR, |
| 52 | CEIL, |
| 53 | }; |
| 54 | |
| 55 | static int clk_rcg2_is_enabled(struct clk_hw *hw) |
| 56 | { |
| 57 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 58 | u32 cmd; |
| 59 | int ret; |
| 60 | |
| 61 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | |
| 65 | return (cmd & CMD_ROOT_OFF) == 0; |
| 66 | } |
| 67 | |
| 68 | static u8 clk_rcg2_get_parent(struct clk_hw *hw) |
| 69 | { |
| 70 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 71 | int num_parents = clk_hw_get_num_parents(hw); |
| 72 | u32 cfg; |
| 73 | int i, ret; |
| 74 | |
| 75 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 76 | if (ret) |
| 77 | goto err; |
| 78 | |
| 79 | cfg &= CFG_SRC_SEL_MASK; |
| 80 | cfg >>= CFG_SRC_SEL_SHIFT; |
| 81 | |
| 82 | for (i = 0; i < num_parents; i++) |
| 83 | if (cfg == rcg->parent_map[i].cfg) |
| 84 | return i; |
| 85 | |
| 86 | err: |
| 87 | pr_debug("%s: Clock %s has invalid parent, using default.\n", |
| 88 | __func__, clk_hw_get_name(hw)); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int update_config(struct clk_rcg2 *rcg) |
| 93 | { |
| 94 | int count, ret; |
| 95 | u32 cmd; |
| 96 | struct clk_hw *hw = &rcg->clkr.hw; |
| 97 | const char *name = clk_hw_get_name(hw); |
| 98 | |
| 99 | ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, |
| 100 | CMD_UPDATE, CMD_UPDATE); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | |
| 104 | /* Wait for update to take effect */ |
| 105 | for (count = 500; count > 0; count--) { |
| 106 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | if (!(cmd & CMD_UPDATE)) |
| 110 | return 0; |
| 111 | udelay(1); |
| 112 | } |
| 113 | |
| 114 | WARN(1, "%s: rcg didn't update its configuration.", name); |
| 115 | return -EBUSY; |
| 116 | } |
| 117 | |
| 118 | static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) |
| 119 | { |
| 120 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 121 | int ret; |
| 122 | u32 cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT; |
| 123 | |
| 124 | ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 125 | CFG_SRC_SEL_MASK, cfg); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | |
| 129 | return update_config(rcg); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Calculate m/n:d rate |
| 134 | * |
| 135 | * parent_rate m |
| 136 | * rate = ----------- x --- |
| 137 | * hid_div n |
| 138 | */ |
| 139 | static unsigned long |
| 140 | calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div) |
| 141 | { |
| 142 | if (hid_div) { |
| 143 | rate *= 2; |
| 144 | rate /= hid_div + 1; |
| 145 | } |
| 146 | |
| 147 | if (mode) { |
| 148 | u64 tmp = rate; |
| 149 | tmp *= m; |
| 150 | do_div(tmp, n); |
| 151 | rate = tmp; |
| 152 | } |
| 153 | |
| 154 | return rate; |
| 155 | } |
| 156 | |
| 157 | static unsigned long |
| 158 | clk_rcg2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) |
| 159 | { |
| 160 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 161 | u32 cfg, hid_div, m = 0, n = 0, mode = 0, mask; |
| 162 | |
| 163 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 164 | |
| 165 | if (rcg->mnd_width) { |
| 166 | mask = BIT(rcg->mnd_width) - 1; |
| 167 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + M_REG, &m); |
| 168 | m &= mask; |
| 169 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + N_REG, &n); |
| 170 | n = ~n; |
| 171 | n &= mask; |
| 172 | n += m; |
| 173 | mode = cfg & CFG_MODE_MASK; |
| 174 | mode >>= CFG_MODE_SHIFT; |
| 175 | } |
| 176 | |
| 177 | mask = BIT(rcg->hid_width) - 1; |
| 178 | hid_div = cfg >> CFG_SRC_DIV_SHIFT; |
| 179 | hid_div &= mask; |
| 180 | |
| 181 | return calc_rate(parent_rate, m, n, mode, hid_div); |
| 182 | } |
| 183 | |
| 184 | static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f, |
| 185 | struct clk_rate_request *req, |
| 186 | enum freq_policy policy) |
| 187 | { |
| 188 | unsigned long clk_flags, rate = req->rate; |
| 189 | struct clk_hw *p; |
| 190 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 191 | int index; |
| 192 | |
| 193 | switch (policy) { |
| 194 | case FLOOR: |
| 195 | f = qcom_find_freq_floor(f, rate); |
| 196 | break; |
| 197 | case CEIL: |
| 198 | f = qcom_find_freq(f, rate); |
| 199 | break; |
| 200 | default: |
| 201 | return -EINVAL; |
| 202 | }; |
| 203 | |
| 204 | if (!f) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | index = qcom_find_src_index(hw, rcg->parent_map, f->src); |
| 208 | if (index < 0) |
| 209 | return index; |
| 210 | |
| 211 | clk_flags = clk_hw_get_flags(hw); |
| 212 | p = clk_hw_get_parent_by_index(hw, index); |
| 213 | if (!p) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | if (clk_flags & CLK_SET_RATE_PARENT) { |
| 217 | if (f->pre_div) { |
| 218 | if (!rate) |
| 219 | rate = req->rate; |
| 220 | rate /= 2; |
| 221 | rate *= f->pre_div + 1; |
| 222 | } |
| 223 | |
| 224 | if (f->n) { |
| 225 | u64 tmp = rate; |
| 226 | tmp = tmp * f->n; |
| 227 | do_div(tmp, f->m); |
| 228 | rate = tmp; |
| 229 | } |
| 230 | } else { |
| 231 | rate = clk_hw_get_rate(p); |
| 232 | } |
| 233 | req->best_parent_hw = p; |
| 234 | req->best_parent_rate = rate; |
| 235 | req->rate = f->freq; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int clk_rcg2_determine_rate(struct clk_hw *hw, |
| 241 | struct clk_rate_request *req) |
| 242 | { |
| 243 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 244 | |
| 245 | return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, CEIL); |
| 246 | } |
| 247 | |
| 248 | static int clk_rcg2_determine_floor_rate(struct clk_hw *hw, |
| 249 | struct clk_rate_request *req) |
| 250 | { |
| 251 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 252 | |
| 253 | return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, FLOOR); |
| 254 | } |
| 255 | |
| 256 | static int clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f) |
| 257 | { |
| 258 | u32 cfg, mask; |
| 259 | struct clk_hw *hw = &rcg->clkr.hw; |
| 260 | int ret, index = qcom_find_src_index(hw, rcg->parent_map, f->src); |
| 261 | |
| 262 | if (index < 0) |
| 263 | return index; |
| 264 | |
| 265 | if (rcg->mnd_width && f->n) { |
| 266 | mask = BIT(rcg->mnd_width) - 1; |
| 267 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 268 | rcg->cmd_rcgr + M_REG, mask, f->m); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 273 | rcg->cmd_rcgr + N_REG, mask, ~(f->n - f->m)); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 278 | rcg->cmd_rcgr + D_REG, mask, ~f->n); |
| 279 | if (ret) |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | mask = BIT(rcg->hid_width) - 1; |
| 284 | mask |= CFG_SRC_SEL_MASK | CFG_MODE_MASK; |
| 285 | cfg = f->pre_div << CFG_SRC_DIV_SHIFT; |
| 286 | cfg |= rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT; |
| 287 | if (rcg->mnd_width && f->n && (f->m != f->n)) |
| 288 | cfg |= CFG_MODE_DUAL_EDGE; |
| 289 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 290 | rcg->cmd_rcgr + CFG_REG, mask, cfg); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | |
| 294 | return update_config(rcg); |
| 295 | } |
| 296 | |
| 297 | static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate, |
| 298 | enum freq_policy policy) |
| 299 | { |
| 300 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 301 | const struct freq_tbl *f; |
| 302 | |
| 303 | switch (policy) { |
| 304 | case FLOOR: |
| 305 | f = qcom_find_freq_floor(rcg->freq_tbl, rate); |
| 306 | break; |
| 307 | case CEIL: |
| 308 | f = qcom_find_freq(rcg->freq_tbl, rate); |
| 309 | break; |
| 310 | default: |
| 311 | return -EINVAL; |
| 312 | }; |
| 313 | |
| 314 | if (!f) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | return clk_rcg2_configure(rcg, f); |
| 318 | } |
| 319 | |
| 320 | static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate, |
| 321 | unsigned long parent_rate) |
| 322 | { |
| 323 | return __clk_rcg2_set_rate(hw, rate, CEIL); |
| 324 | } |
| 325 | |
| 326 | static int clk_rcg2_set_floor_rate(struct clk_hw *hw, unsigned long rate, |
| 327 | unsigned long parent_rate) |
| 328 | { |
| 329 | return __clk_rcg2_set_rate(hw, rate, FLOOR); |
| 330 | } |
| 331 | |
| 332 | static int clk_rcg2_set_rate_and_parent(struct clk_hw *hw, |
| 333 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 334 | { |
| 335 | return __clk_rcg2_set_rate(hw, rate, CEIL); |
| 336 | } |
| 337 | |
| 338 | static int clk_rcg2_set_floor_rate_and_parent(struct clk_hw *hw, |
| 339 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 340 | { |
| 341 | return __clk_rcg2_set_rate(hw, rate, FLOOR); |
| 342 | } |
| 343 | |
| 344 | const struct clk_ops clk_rcg2_ops = { |
| 345 | .is_enabled = clk_rcg2_is_enabled, |
| 346 | .get_parent = clk_rcg2_get_parent, |
| 347 | .set_parent = clk_rcg2_set_parent, |
| 348 | .recalc_rate = clk_rcg2_recalc_rate, |
| 349 | .determine_rate = clk_rcg2_determine_rate, |
| 350 | .set_rate = clk_rcg2_set_rate, |
| 351 | .set_rate_and_parent = clk_rcg2_set_rate_and_parent, |
| 352 | }; |
| 353 | EXPORT_SYMBOL_GPL(clk_rcg2_ops); |
| 354 | |
| 355 | const struct clk_ops clk_rcg2_floor_ops = { |
| 356 | .is_enabled = clk_rcg2_is_enabled, |
| 357 | .get_parent = clk_rcg2_get_parent, |
| 358 | .set_parent = clk_rcg2_set_parent, |
| 359 | .recalc_rate = clk_rcg2_recalc_rate, |
| 360 | .determine_rate = clk_rcg2_determine_floor_rate, |
| 361 | .set_rate = clk_rcg2_set_floor_rate, |
| 362 | .set_rate_and_parent = clk_rcg2_set_floor_rate_and_parent, |
| 363 | }; |
| 364 | EXPORT_SYMBOL_GPL(clk_rcg2_floor_ops); |
| 365 | |
| 366 | static int clk_rcg2_shared_force_enable(struct clk_hw *hw, unsigned long rate) |
| 367 | { |
| 368 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 369 | const char *name = clk_hw_get_name(hw); |
| 370 | int ret, count; |
| 371 | |
| 372 | /* force enable RCG */ |
| 373 | ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, |
| 374 | CMD_ROOT_EN, CMD_ROOT_EN); |
| 375 | if (ret) |
| 376 | return ret; |
| 377 | |
| 378 | /* wait for RCG to turn ON */ |
| 379 | for (count = 500; count > 0; count--) { |
| 380 | ret = clk_rcg2_is_enabled(hw); |
| 381 | if (ret) |
| 382 | break; |
| 383 | udelay(1); |
| 384 | } |
| 385 | if (!count) |
| 386 | pr_err("%s: RCG did not turn on\n", name); |
| 387 | |
| 388 | /* set clock rate */ |
| 389 | ret = __clk_rcg2_set_rate(hw, rate, CEIL); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | |
| 393 | /* clear force enable RCG */ |
| 394 | return regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, |
| 395 | CMD_ROOT_EN, 0); |
| 396 | } |
| 397 | |
| 398 | static int clk_rcg2_shared_set_rate(struct clk_hw *hw, unsigned long rate, |
| 399 | unsigned long parent_rate) |
| 400 | { |
| 401 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 402 | |
| 403 | /* cache the rate */ |
| 404 | rcg->current_freq = rate; |
| 405 | |
| 406 | if (!__clk_is_enabled(hw->clk)) |
| 407 | return 0; |
| 408 | |
| 409 | return clk_rcg2_shared_force_enable(hw, rcg->current_freq); |
| 410 | } |
| 411 | |
| 412 | static unsigned long |
| 413 | clk_rcg2_shared_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) |
| 414 | { |
| 415 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 416 | |
| 417 | return rcg->current_freq = clk_rcg2_recalc_rate(hw, parent_rate); |
| 418 | } |
| 419 | |
| 420 | static int clk_rcg2_shared_enable(struct clk_hw *hw) |
| 421 | { |
| 422 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 423 | |
| 424 | return clk_rcg2_shared_force_enable(hw, rcg->current_freq); |
| 425 | } |
| 426 | |
| 427 | static void clk_rcg2_shared_disable(struct clk_hw *hw) |
| 428 | { |
| 429 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 430 | |
| 431 | /* switch to XO, which is the lowest entry in the freq table */ |
| 432 | clk_rcg2_shared_set_rate(hw, rcg->freq_tbl[0].freq, 0); |
| 433 | } |
| 434 | |
| 435 | const struct clk_ops clk_rcg2_shared_ops = { |
| 436 | .enable = clk_rcg2_shared_enable, |
| 437 | .disable = clk_rcg2_shared_disable, |
| 438 | .get_parent = clk_rcg2_get_parent, |
| 439 | .recalc_rate = clk_rcg2_shared_recalc_rate, |
| 440 | .determine_rate = clk_rcg2_determine_rate, |
| 441 | .set_rate = clk_rcg2_shared_set_rate, |
| 442 | }; |
| 443 | EXPORT_SYMBOL_GPL(clk_rcg2_shared_ops); |
| 444 | |
| 445 | struct frac_entry { |
| 446 | int num; |
| 447 | int den; |
| 448 | }; |
| 449 | |
| 450 | static const struct frac_entry frac_table_675m[] = { /* link rate of 270M */ |
| 451 | { 52, 295 }, /* 119 M */ |
| 452 | { 11, 57 }, /* 130.25 M */ |
| 453 | { 63, 307 }, /* 138.50 M */ |
| 454 | { 11, 50 }, /* 148.50 M */ |
| 455 | { 47, 206 }, /* 154 M */ |
| 456 | { 31, 100 }, /* 205.25 M */ |
| 457 | { 107, 269 }, /* 268.50 M */ |
| 458 | { }, |
| 459 | }; |
| 460 | |
| 461 | static struct frac_entry frac_table_810m[] = { /* Link rate of 162M */ |
| 462 | { 31, 211 }, /* 119 M */ |
| 463 | { 32, 199 }, /* 130.25 M */ |
| 464 | { 63, 307 }, /* 138.50 M */ |
| 465 | { 11, 60 }, /* 148.50 M */ |
| 466 | { 50, 263 }, /* 154 M */ |
| 467 | { 31, 120 }, /* 205.25 M */ |
| 468 | { 119, 359 }, /* 268.50 M */ |
| 469 | { }, |
| 470 | }; |
| 471 | |
| 472 | static int clk_edp_pixel_set_rate(struct clk_hw *hw, unsigned long rate, |
| 473 | unsigned long parent_rate) |
| 474 | { |
| 475 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 476 | struct freq_tbl f = *rcg->freq_tbl; |
| 477 | const struct frac_entry *frac; |
| 478 | int delta = 100000; |
| 479 | s64 src_rate = parent_rate; |
| 480 | s64 request; |
| 481 | u32 mask = BIT(rcg->hid_width) - 1; |
| 482 | u32 hid_div; |
| 483 | |
| 484 | if (src_rate == 810000000) |
| 485 | frac = frac_table_810m; |
| 486 | else |
| 487 | frac = frac_table_675m; |
| 488 | |
| 489 | for (; frac->num; frac++) { |
| 490 | request = rate; |
| 491 | request *= frac->den; |
| 492 | request = div_s64(request, frac->num); |
| 493 | if ((src_rate < (request - delta)) || |
| 494 | (src_rate > (request + delta))) |
| 495 | continue; |
| 496 | |
| 497 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 498 | &hid_div); |
| 499 | f.pre_div = hid_div; |
| 500 | f.pre_div >>= CFG_SRC_DIV_SHIFT; |
| 501 | f.pre_div &= mask; |
| 502 | f.m = frac->num; |
| 503 | f.n = frac->den; |
| 504 | |
| 505 | return clk_rcg2_configure(rcg, &f); |
| 506 | } |
| 507 | |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | |
| 511 | static int clk_edp_pixel_set_rate_and_parent(struct clk_hw *hw, |
| 512 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 513 | { |
| 514 | /* Parent index is set statically in frequency table */ |
| 515 | return clk_edp_pixel_set_rate(hw, rate, parent_rate); |
| 516 | } |
| 517 | |
| 518 | static int clk_edp_pixel_determine_rate(struct clk_hw *hw, |
| 519 | struct clk_rate_request *req) |
| 520 | { |
| 521 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 522 | const struct freq_tbl *f = rcg->freq_tbl; |
| 523 | const struct frac_entry *frac; |
| 524 | int delta = 100000; |
| 525 | s64 request; |
| 526 | u32 mask = BIT(rcg->hid_width) - 1; |
| 527 | u32 hid_div; |
| 528 | int index = qcom_find_src_index(hw, rcg->parent_map, f->src); |
| 529 | |
| 530 | /* Force the correct parent */ |
| 531 | req->best_parent_hw = clk_hw_get_parent_by_index(hw, index); |
| 532 | req->best_parent_rate = clk_hw_get_rate(req->best_parent_hw); |
| 533 | |
| 534 | if (req->best_parent_rate == 810000000) |
| 535 | frac = frac_table_810m; |
| 536 | else |
| 537 | frac = frac_table_675m; |
| 538 | |
| 539 | for (; frac->num; frac++) { |
| 540 | request = req->rate; |
| 541 | request *= frac->den; |
| 542 | request = div_s64(request, frac->num); |
| 543 | if ((req->best_parent_rate < (request - delta)) || |
| 544 | (req->best_parent_rate > (request + delta))) |
| 545 | continue; |
| 546 | |
| 547 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 548 | &hid_div); |
| 549 | hid_div >>= CFG_SRC_DIV_SHIFT; |
| 550 | hid_div &= mask; |
| 551 | |
| 552 | req->rate = calc_rate(req->best_parent_rate, |
| 553 | frac->num, frac->den, |
| 554 | !!frac->den, hid_div); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
| 561 | const struct clk_ops clk_edp_pixel_ops = { |
| 562 | .is_enabled = clk_rcg2_is_enabled, |
| 563 | .get_parent = clk_rcg2_get_parent, |
| 564 | .set_parent = clk_rcg2_set_parent, |
| 565 | .recalc_rate = clk_rcg2_recalc_rate, |
| 566 | .set_rate = clk_edp_pixel_set_rate, |
| 567 | .set_rate_and_parent = clk_edp_pixel_set_rate_and_parent, |
| 568 | .determine_rate = clk_edp_pixel_determine_rate, |
| 569 | }; |
| 570 | EXPORT_SYMBOL_GPL(clk_edp_pixel_ops); |
| 571 | |
| 572 | static int clk_byte_determine_rate(struct clk_hw *hw, |
| 573 | struct clk_rate_request *req) |
| 574 | { |
| 575 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 576 | const struct freq_tbl *f = rcg->freq_tbl; |
| 577 | int index = qcom_find_src_index(hw, rcg->parent_map, f->src); |
| 578 | unsigned long parent_rate, div; |
| 579 | u32 mask = BIT(rcg->hid_width) - 1; |
| 580 | struct clk_hw *p; |
| 581 | |
| 582 | if (req->rate == 0) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | req->best_parent_hw = p = clk_hw_get_parent_by_index(hw, index); |
| 586 | req->best_parent_rate = parent_rate = clk_hw_round_rate(p, req->rate); |
| 587 | |
| 588 | div = DIV_ROUND_UP((2 * parent_rate), req->rate) - 1; |
| 589 | div = min_t(u32, div, mask); |
| 590 | |
| 591 | req->rate = calc_rate(parent_rate, 0, 0, 0, div); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int clk_byte_set_rate(struct clk_hw *hw, unsigned long rate, |
| 597 | unsigned long parent_rate) |
| 598 | { |
| 599 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 600 | struct freq_tbl f = *rcg->freq_tbl; |
| 601 | unsigned long div; |
| 602 | u32 mask = BIT(rcg->hid_width) - 1; |
| 603 | |
| 604 | div = DIV_ROUND_UP((2 * parent_rate), rate) - 1; |
| 605 | div = min_t(u32, div, mask); |
| 606 | |
| 607 | f.pre_div = div; |
| 608 | |
| 609 | return clk_rcg2_configure(rcg, &f); |
| 610 | } |
| 611 | |
| 612 | static int clk_byte_set_rate_and_parent(struct clk_hw *hw, |
| 613 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 614 | { |
| 615 | /* Parent index is set statically in frequency table */ |
| 616 | return clk_byte_set_rate(hw, rate, parent_rate); |
| 617 | } |
| 618 | |
| 619 | const struct clk_ops clk_byte_ops = { |
| 620 | .is_enabled = clk_rcg2_is_enabled, |
| 621 | .get_parent = clk_rcg2_get_parent, |
| 622 | .set_parent = clk_rcg2_set_parent, |
| 623 | .recalc_rate = clk_rcg2_recalc_rate, |
| 624 | .set_rate = clk_byte_set_rate, |
| 625 | .set_rate_and_parent = clk_byte_set_rate_and_parent, |
| 626 | .determine_rate = clk_byte_determine_rate, |
| 627 | }; |
| 628 | EXPORT_SYMBOL_GPL(clk_byte_ops); |
| 629 | |
| 630 | static int clk_byte2_determine_rate(struct clk_hw *hw, |
| 631 | struct clk_rate_request *req) |
| 632 | { |
| 633 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 634 | unsigned long parent_rate, div; |
| 635 | u32 mask = BIT(rcg->hid_width) - 1; |
| 636 | struct clk_hw *p; |
| 637 | unsigned long rate = req->rate; |
| 638 | |
| 639 | if (rate == 0) |
| 640 | return -EINVAL; |
| 641 | |
| 642 | p = req->best_parent_hw; |
| 643 | req->best_parent_rate = parent_rate = clk_hw_round_rate(p, rate); |
| 644 | |
| 645 | div = DIV_ROUND_UP((2 * parent_rate), rate) - 1; |
| 646 | div = min_t(u32, div, mask); |
| 647 | |
| 648 | req->rate = calc_rate(parent_rate, 0, 0, 0, div); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int clk_byte2_set_rate(struct clk_hw *hw, unsigned long rate, |
| 654 | unsigned long parent_rate) |
| 655 | { |
| 656 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 657 | struct freq_tbl f = { 0 }; |
| 658 | unsigned long div; |
| 659 | int i, num_parents = clk_hw_get_num_parents(hw); |
| 660 | u32 mask = BIT(rcg->hid_width) - 1; |
| 661 | u32 cfg; |
| 662 | |
| 663 | div = DIV_ROUND_UP((2 * parent_rate), rate) - 1; |
| 664 | div = min_t(u32, div, mask); |
| 665 | |
| 666 | f.pre_div = div; |
| 667 | |
| 668 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 669 | cfg &= CFG_SRC_SEL_MASK; |
| 670 | cfg >>= CFG_SRC_SEL_SHIFT; |
| 671 | |
| 672 | for (i = 0; i < num_parents; i++) { |
| 673 | if (cfg == rcg->parent_map[i].cfg) { |
| 674 | f.src = rcg->parent_map[i].src; |
| 675 | return clk_rcg2_configure(rcg, &f); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return -EINVAL; |
| 680 | } |
| 681 | |
| 682 | static int clk_byte2_set_rate_and_parent(struct clk_hw *hw, |
| 683 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 684 | { |
| 685 | /* Read the hardware to determine parent during set_rate */ |
| 686 | return clk_byte2_set_rate(hw, rate, parent_rate); |
| 687 | } |
| 688 | |
| 689 | const struct clk_ops clk_byte2_ops = { |
| 690 | .is_enabled = clk_rcg2_is_enabled, |
| 691 | .get_parent = clk_rcg2_get_parent, |
| 692 | .set_parent = clk_rcg2_set_parent, |
| 693 | .recalc_rate = clk_rcg2_recalc_rate, |
| 694 | .set_rate = clk_byte2_set_rate, |
| 695 | .set_rate_and_parent = clk_byte2_set_rate_and_parent, |
| 696 | .determine_rate = clk_byte2_determine_rate, |
| 697 | }; |
| 698 | EXPORT_SYMBOL_GPL(clk_byte2_ops); |
| 699 | |
| 700 | static const struct frac_entry frac_table_pixel[] = { |
| 701 | { 3, 8 }, |
| 702 | { 2, 9 }, |
| 703 | { 4, 9 }, |
| 704 | { 1, 1 }, |
| 705 | { } |
| 706 | }; |
| 707 | |
| 708 | static int clk_pixel_determine_rate(struct clk_hw *hw, |
| 709 | struct clk_rate_request *req) |
| 710 | { |
| 711 | unsigned long request, src_rate; |
| 712 | int delta = 100000; |
| 713 | const struct frac_entry *frac = frac_table_pixel; |
| 714 | |
| 715 | for (; frac->num; frac++) { |
| 716 | request = (req->rate * frac->den) / frac->num; |
| 717 | |
| 718 | src_rate = clk_hw_round_rate(req->best_parent_hw, request); |
| 719 | if ((src_rate < (request - delta)) || |
| 720 | (src_rate > (request + delta))) |
| 721 | continue; |
| 722 | |
| 723 | req->best_parent_rate = src_rate; |
| 724 | req->rate = (src_rate * frac->num) / frac->den; |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | return -EINVAL; |
| 729 | } |
| 730 | |
| 731 | static int clk_pixel_set_rate(struct clk_hw *hw, unsigned long rate, |
| 732 | unsigned long parent_rate) |
| 733 | { |
| 734 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 735 | struct freq_tbl f = { 0 }; |
| 736 | const struct frac_entry *frac = frac_table_pixel; |
| 737 | unsigned long request; |
| 738 | int delta = 100000; |
| 739 | u32 mask = BIT(rcg->hid_width) - 1; |
| 740 | u32 hid_div, cfg; |
| 741 | int i, num_parents = clk_hw_get_num_parents(hw); |
| 742 | |
| 743 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 744 | cfg &= CFG_SRC_SEL_MASK; |
| 745 | cfg >>= CFG_SRC_SEL_SHIFT; |
| 746 | |
| 747 | for (i = 0; i < num_parents; i++) |
| 748 | if (cfg == rcg->parent_map[i].cfg) { |
| 749 | f.src = rcg->parent_map[i].src; |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | for (; frac->num; frac++) { |
| 754 | request = (rate * frac->den) / frac->num; |
| 755 | |
| 756 | if ((parent_rate < (request - delta)) || |
| 757 | (parent_rate > (request + delta))) |
| 758 | continue; |
| 759 | |
| 760 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 761 | &hid_div); |
| 762 | f.pre_div = hid_div; |
| 763 | f.pre_div >>= CFG_SRC_DIV_SHIFT; |
| 764 | f.pre_div &= mask; |
| 765 | f.m = frac->num; |
| 766 | f.n = frac->den; |
| 767 | |
| 768 | return clk_rcg2_configure(rcg, &f); |
| 769 | } |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | |
| 773 | static int clk_pixel_set_rate_and_parent(struct clk_hw *hw, unsigned long rate, |
| 774 | unsigned long parent_rate, u8 index) |
| 775 | { |
| 776 | return clk_pixel_set_rate(hw, rate, parent_rate); |
| 777 | } |
| 778 | |
| 779 | const struct clk_ops clk_pixel_ops = { |
| 780 | .is_enabled = clk_rcg2_is_enabled, |
| 781 | .get_parent = clk_rcg2_get_parent, |
| 782 | .set_parent = clk_rcg2_set_parent, |
| 783 | .recalc_rate = clk_rcg2_recalc_rate, |
| 784 | .set_rate = clk_pixel_set_rate, |
| 785 | .set_rate_and_parent = clk_pixel_set_rate_and_parent, |
| 786 | .determine_rate = clk_pixel_determine_rate, |
| 787 | }; |
| 788 | EXPORT_SYMBOL_GPL(clk_pixel_ops); |
| 789 | |
| 790 | static int clk_gfx3d_determine_rate(struct clk_hw *hw, |
| 791 | struct clk_rate_request *req) |
| 792 | { |
| 793 | struct clk_rate_request parent_req = { }; |
| 794 | struct clk_hw *p2, *p8, *p9, *xo; |
| 795 | unsigned long p9_rate; |
| 796 | int ret; |
| 797 | |
| 798 | xo = clk_hw_get_parent_by_index(hw, 0); |
| 799 | if (req->rate == clk_hw_get_rate(xo)) { |
| 800 | req->best_parent_hw = xo; |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | p9 = clk_hw_get_parent_by_index(hw, 2); |
| 805 | p2 = clk_hw_get_parent_by_index(hw, 3); |
| 806 | p8 = clk_hw_get_parent_by_index(hw, 4); |
| 807 | |
| 808 | /* PLL9 is a fixed rate PLL */ |
| 809 | p9_rate = clk_hw_get_rate(p9); |
| 810 | |
| 811 | parent_req.rate = req->rate = min(req->rate, p9_rate); |
| 812 | if (req->rate == p9_rate) { |
| 813 | req->rate = req->best_parent_rate = p9_rate; |
| 814 | req->best_parent_hw = p9; |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | if (req->best_parent_hw == p9) { |
| 819 | /* Are we going back to a previously used rate? */ |
| 820 | if (clk_hw_get_rate(p8) == req->rate) |
| 821 | req->best_parent_hw = p8; |
| 822 | else |
| 823 | req->best_parent_hw = p2; |
| 824 | } else if (req->best_parent_hw == p8) { |
| 825 | req->best_parent_hw = p2; |
| 826 | } else { |
| 827 | req->best_parent_hw = p8; |
| 828 | } |
| 829 | |
| 830 | ret = __clk_determine_rate(req->best_parent_hw, &parent_req); |
| 831 | if (ret) |
| 832 | return ret; |
| 833 | |
| 834 | req->rate = req->best_parent_rate = parent_req.rate; |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | static int clk_gfx3d_set_rate_and_parent(struct clk_hw *hw, unsigned long rate, |
| 840 | unsigned long parent_rate, u8 index) |
| 841 | { |
| 842 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 843 | u32 cfg; |
| 844 | int ret; |
| 845 | |
| 846 | /* Just mux it, we don't use the division or m/n hardware */ |
| 847 | cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT; |
| 848 | ret = regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, cfg); |
| 849 | if (ret) |
| 850 | return ret; |
| 851 | |
| 852 | return update_config(rcg); |
| 853 | } |
| 854 | |
| 855 | static int clk_gfx3d_set_rate(struct clk_hw *hw, unsigned long rate, |
| 856 | unsigned long parent_rate) |
| 857 | { |
| 858 | /* |
| 859 | * We should never get here; clk_gfx3d_determine_rate() should always |
| 860 | * make us use a different parent than what we're currently using, so |
| 861 | * clk_gfx3d_set_rate_and_parent() should always be called. |
| 862 | */ |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | const struct clk_ops clk_gfx3d_ops = { |
| 867 | .is_enabled = clk_rcg2_is_enabled, |
| 868 | .get_parent = clk_rcg2_get_parent, |
| 869 | .set_parent = clk_rcg2_set_parent, |
| 870 | .recalc_rate = clk_rcg2_recalc_rate, |
| 871 | .set_rate = clk_gfx3d_set_rate, |
| 872 | .set_rate_and_parent = clk_gfx3d_set_rate_and_parent, |
| 873 | .determine_rate = clk_gfx3d_determine_rate, |
| 874 | }; |
| 875 | EXPORT_SYMBOL_GPL(clk_gfx3d_ops); |