1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Ingenic SoC CGU driver 4 * 5 * Copyright (c) 2013-2015 Imagination Technologies 6 * Author: Paul Burton <paul.burton@mips.com> 7 */ 8 9 #include <linux/bitops.h> 10 #include <linux/clk.h> 11 #include <linux/clk-provider.h> 12 #include <linux/clkdev.h> 13 #include <linux/delay.h> 14 #include <linux/io.h> 15 #include <linux/iopoll.h> 16 #include <linux/math64.h> 17 #include <linux/of.h> 18 #include <linux/of_address.h> 19 #include <linux/slab.h> 20 #include <linux/spinlock.h> 21 #include <linux/time.h> 22 23 #include "cgu.h" 24 25 #define MHZ (1000 * 1000) 26 27 static inline const struct ingenic_cgu_clk_info * 28 to_clk_info(struct ingenic_clk *clk) 29 { 30 return &clk->cgu->clock_info[clk->idx]; 31 } 32 33 /** 34 * ingenic_cgu_gate_get() - get the value of clock gate register bit 35 * @cgu: reference to the CGU whose registers should be read 36 * @info: info struct describing the gate bit 37 * 38 * Retrieves the state of the clock gate bit described by info. The 39 * caller must hold cgu->lock. 40 * 41 * Return: true if the gate bit is set, else false. 42 */ 43 static inline bool 44 ingenic_cgu_gate_get(struct ingenic_cgu *cgu, 45 const struct ingenic_cgu_gate_info *info) 46 { 47 return !!(readl(cgu->base + info->reg) & BIT(info->bit)) 48 ^ info->clear_to_gate; 49 } 50 51 /** 52 * ingenic_cgu_gate_set() - set the value of clock gate register bit 53 * @cgu: reference to the CGU whose registers should be modified 54 * @info: info struct describing the gate bit 55 * @val: non-zero to gate a clock, otherwise zero 56 * 57 * Sets the given gate bit in order to gate or ungate a clock. 58 * 59 * The caller must hold cgu->lock. 60 */ 61 static inline void 62 ingenic_cgu_gate_set(struct ingenic_cgu *cgu, 63 const struct ingenic_cgu_gate_info *info, bool val) 64 { 65 u32 clkgr = readl(cgu->base + info->reg); 66 67 if (val ^ info->clear_to_gate) 68 clkgr |= BIT(info->bit); 69 else 70 clkgr &= ~BIT(info->bit); 71 72 writel(clkgr, cgu->base + info->reg); 73 } 74 75 /* 76 * PLL operations 77 */ 78 79 static unsigned long 80 ingenic_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) 81 { 82 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 83 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 84 struct ingenic_cgu *cgu = ingenic_clk->cgu; 85 const struct ingenic_cgu_pll_info *pll_info; 86 unsigned m, n, od, od_enc = 0; 87 bool bypass; 88 u32 ctl; 89 90 BUG_ON(clk_info->type != CGU_CLK_PLL); 91 pll_info = &clk_info->pll; 92 93 ctl = readl(cgu->base + pll_info->reg); 94 95 m = (ctl >> pll_info->m_shift) & GENMASK(pll_info->m_bits - 1, 0); 96 m += pll_info->m_offset; 97 n = (ctl >> pll_info->n_shift) & GENMASK(pll_info->n_bits - 1, 0); 98 n += pll_info->n_offset; 99 100 if (pll_info->od_bits > 0) { 101 od_enc = ctl >> pll_info->od_shift; 102 od_enc &= GENMASK(pll_info->od_bits - 1, 0); 103 } 104 105 if (pll_info->bypass_bit >= 0) { 106 ctl = readl(cgu->base + pll_info->bypass_reg); 107 108 bypass = !!(ctl & BIT(pll_info->bypass_bit)); 109 110 if (bypass) 111 return parent_rate; 112 } 113 114 for (od = 0; od < pll_info->od_max; od++) 115 if (pll_info->od_encoding[od] == od_enc) 116 break; 117 118 /* if od_max = 0, od_bits should be 0 and od is fixed to 1. */ 119 if (pll_info->od_max == 0) 120 BUG_ON(pll_info->od_bits != 0); 121 else 122 BUG_ON(od == pll_info->od_max); 123 od++; 124 125 return div_u64((u64)parent_rate * m * pll_info->rate_multiplier, 126 n * od); 127 } 128 129 static void 130 ingenic_pll_calc_m_n_od(const struct ingenic_cgu_pll_info *pll_info, 131 unsigned long rate, unsigned long parent_rate, 132 unsigned int *pm, unsigned int *pn, unsigned int *pod) 133 { 134 unsigned int m, n, od = 1; 135 136 /* 137 * The frequency after the input divider must be between 10 and 50 MHz. 138 * The highest divider yields the best resolution. 139 */ 140 n = parent_rate / (10 * MHZ); 141 n = min_t(unsigned int, n, 1 << pll_info->n_bits); 142 n = max_t(unsigned int, n, pll_info->n_offset); 143 144 m = (rate / MHZ) * od * n / (parent_rate / MHZ); 145 m = min_t(unsigned int, m, 1 << pll_info->m_bits); 146 m = max_t(unsigned int, m, pll_info->m_offset); 147 148 *pm = m; 149 *pn = n; 150 *pod = od; 151 } 152 153 static unsigned long 154 ingenic_pll_calc(const struct ingenic_cgu_clk_info *clk_info, 155 unsigned long rate, unsigned long parent_rate, 156 unsigned int *pm, unsigned int *pn, unsigned int *pod) 157 { 158 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll; 159 unsigned int m, n, od; 160 161 if (pll_info->calc_m_n_od) 162 (*pll_info->calc_m_n_od)(pll_info, rate, parent_rate, &m, &n, &od); 163 else 164 ingenic_pll_calc_m_n_od(pll_info, rate, parent_rate, &m, &n, &od); 165 166 if (pm) 167 *pm = m; 168 if (pn) 169 *pn = n; 170 if (pod) 171 *pod = od; 172 173 return div_u64((u64)parent_rate * m * pll_info->rate_multiplier, 174 n * od); 175 } 176 177 static long 178 ingenic_pll_round_rate(struct clk_hw *hw, unsigned long req_rate, 179 unsigned long *prate) 180 { 181 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 182 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 183 184 return ingenic_pll_calc(clk_info, req_rate, *prate, NULL, NULL, NULL); 185 } 186 187 static inline int ingenic_pll_check_stable(struct ingenic_cgu *cgu, 188 const struct ingenic_cgu_pll_info *pll_info) 189 { 190 u32 ctl; 191 192 if (pll_info->stable_bit < 0) 193 return 0; 194 195 return readl_poll_timeout(cgu->base + pll_info->reg, ctl, 196 ctl & BIT(pll_info->stable_bit), 197 0, 100 * USEC_PER_MSEC); 198 } 199 200 static int 201 ingenic_pll_set_rate(struct clk_hw *hw, unsigned long req_rate, 202 unsigned long parent_rate) 203 { 204 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 205 struct ingenic_cgu *cgu = ingenic_clk->cgu; 206 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 207 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll; 208 unsigned long rate, flags; 209 unsigned int m, n, od; 210 int ret = 0; 211 u32 ctl; 212 213 rate = ingenic_pll_calc(clk_info, req_rate, parent_rate, 214 &m, &n, &od); 215 if (rate != req_rate) 216 pr_info("ingenic-cgu: request '%s' rate %luHz, actual %luHz\n", 217 clk_info->name, req_rate, rate); 218 219 spin_lock_irqsave(&cgu->lock, flags); 220 ctl = readl(cgu->base + pll_info->reg); 221 222 ctl &= ~(GENMASK(pll_info->m_bits - 1, 0) << pll_info->m_shift); 223 ctl |= (m - pll_info->m_offset) << pll_info->m_shift; 224 225 ctl &= ~(GENMASK(pll_info->n_bits - 1, 0) << pll_info->n_shift); 226 ctl |= (n - pll_info->n_offset) << pll_info->n_shift; 227 228 if (pll_info->od_bits > 0) { 229 ctl &= ~(GENMASK(pll_info->od_bits - 1, 0) << pll_info->od_shift); 230 ctl |= pll_info->od_encoding[od - 1] << pll_info->od_shift; 231 } 232 233 writel(ctl, cgu->base + pll_info->reg); 234 235 if (pll_info->set_rate_hook) 236 pll_info->set_rate_hook(pll_info, rate, parent_rate); 237 238 /* If the PLL is enabled, verify that it's stable */ 239 if (pll_info->enable_bit >= 0 && (ctl & BIT(pll_info->enable_bit))) 240 ret = ingenic_pll_check_stable(cgu, pll_info); 241 242 spin_unlock_irqrestore(&cgu->lock, flags); 243 244 return ret; 245 } 246 247 static int ingenic_pll_enable(struct clk_hw *hw) 248 { 249 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 250 struct ingenic_cgu *cgu = ingenic_clk->cgu; 251 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 252 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll; 253 unsigned long flags; 254 int ret; 255 u32 ctl; 256 257 if (pll_info->enable_bit < 0) 258 return 0; 259 260 spin_lock_irqsave(&cgu->lock, flags); 261 if (pll_info->bypass_bit >= 0) { 262 ctl = readl(cgu->base + pll_info->bypass_reg); 263 264 ctl &= ~BIT(pll_info->bypass_bit); 265 266 writel(ctl, cgu->base + pll_info->bypass_reg); 267 } 268 269 ctl = readl(cgu->base + pll_info->reg); 270 271 ctl |= BIT(pll_info->enable_bit); 272 273 writel(ctl, cgu->base + pll_info->reg); 274 275 ret = ingenic_pll_check_stable(cgu, pll_info); 276 spin_unlock_irqrestore(&cgu->lock, flags); 277 278 return ret; 279 } 280 281 static void ingenic_pll_disable(struct clk_hw *hw) 282 { 283 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 284 struct ingenic_cgu *cgu = ingenic_clk->cgu; 285 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 286 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll; 287 unsigned long flags; 288 u32 ctl; 289 290 if (pll_info->enable_bit < 0) 291 return; 292 293 spin_lock_irqsave(&cgu->lock, flags); 294 ctl = readl(cgu->base + pll_info->reg); 295 296 ctl &= ~BIT(pll_info->enable_bit); 297 298 writel(ctl, cgu->base + pll_info->reg); 299 spin_unlock_irqrestore(&cgu->lock, flags); 300 } 301 302 static int ingenic_pll_is_enabled(struct clk_hw *hw) 303 { 304 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 305 struct ingenic_cgu *cgu = ingenic_clk->cgu; 306 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 307 const struct ingenic_cgu_pll_info *pll_info = &clk_info->pll; 308 u32 ctl; 309 310 if (pll_info->enable_bit < 0) 311 return true; 312 313 ctl = readl(cgu->base + pll_info->reg); 314 315 return !!(ctl & BIT(pll_info->enable_bit)); 316 } 317 318 static const struct clk_ops ingenic_pll_ops = { 319 .recalc_rate = ingenic_pll_recalc_rate, 320 .round_rate = ingenic_pll_round_rate, 321 .set_rate = ingenic_pll_set_rate, 322 323 .enable = ingenic_pll_enable, 324 .disable = ingenic_pll_disable, 325 .is_enabled = ingenic_pll_is_enabled, 326 }; 327 328 /* 329 * Operations for all non-PLL clocks 330 */ 331 332 static u8 ingenic_clk_get_parent(struct clk_hw *hw) 333 { 334 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 335 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 336 struct ingenic_cgu *cgu = ingenic_clk->cgu; 337 u32 reg; 338 u8 i, hw_idx, idx = 0; 339 340 if (clk_info->type & CGU_CLK_MUX) { 341 reg = readl(cgu->base + clk_info->mux.reg); 342 hw_idx = (reg >> clk_info->mux.shift) & 343 GENMASK(clk_info->mux.bits - 1, 0); 344 345 /* 346 * Convert the hardware index to the parent index by skipping 347 * over any -1's in the parents array. 348 */ 349 for (i = 0; i < hw_idx; i++) { 350 if (clk_info->parents[i] != -1) 351 idx++; 352 } 353 } 354 355 return idx; 356 } 357 358 static int ingenic_clk_set_parent(struct clk_hw *hw, u8 idx) 359 { 360 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 361 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 362 struct ingenic_cgu *cgu = ingenic_clk->cgu; 363 unsigned long flags; 364 u8 curr_idx, hw_idx, num_poss; 365 u32 reg, mask; 366 367 if (clk_info->type & CGU_CLK_MUX) { 368 /* 369 * Convert the parent index to the hardware index by adding 370 * 1 for any -1 in the parents array preceding the given 371 * index. That is, we want the index of idx'th entry in 372 * clk_info->parents which does not equal -1. 373 */ 374 hw_idx = curr_idx = 0; 375 num_poss = 1 << clk_info->mux.bits; 376 for (; hw_idx < num_poss; hw_idx++) { 377 if (clk_info->parents[hw_idx] == -1) 378 continue; 379 if (curr_idx == idx) 380 break; 381 curr_idx++; 382 } 383 384 /* idx should always be a valid parent */ 385 BUG_ON(curr_idx != idx); 386 387 mask = GENMASK(clk_info->mux.bits - 1, 0); 388 mask <<= clk_info->mux.shift; 389 390 spin_lock_irqsave(&cgu->lock, flags); 391 392 /* write the register */ 393 reg = readl(cgu->base + clk_info->mux.reg); 394 reg &= ~mask; 395 reg |= hw_idx << clk_info->mux.shift; 396 writel(reg, cgu->base + clk_info->mux.reg); 397 398 spin_unlock_irqrestore(&cgu->lock, flags); 399 return 0; 400 } 401 402 return idx ? -EINVAL : 0; 403 } 404 405 static unsigned long 406 ingenic_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) 407 { 408 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 409 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 410 struct ingenic_cgu *cgu = ingenic_clk->cgu; 411 unsigned long rate = parent_rate; 412 u32 div_reg, div; 413 u8 parent; 414 415 if (clk_info->type & CGU_CLK_DIV) { 416 parent = ingenic_clk_get_parent(hw); 417 418 if (!(clk_info->div.bypass_mask & BIT(parent))) { 419 div_reg = readl(cgu->base + clk_info->div.reg); 420 div = (div_reg >> clk_info->div.shift) & 421 GENMASK(clk_info->div.bits - 1, 0); 422 423 if (clk_info->div.div_table) 424 div = clk_info->div.div_table[div]; 425 else 426 div = (div + 1) * clk_info->div.div; 427 428 rate /= div; 429 } 430 } else if (clk_info->type & CGU_CLK_FIXDIV) { 431 rate /= clk_info->fixdiv.div; 432 } 433 434 return rate; 435 } 436 437 static unsigned int 438 ingenic_clk_calc_hw_div(const struct ingenic_cgu_clk_info *clk_info, 439 unsigned int div) 440 { 441 unsigned int i, best_i = 0, best = (unsigned int)-1; 442 443 for (i = 0; i < (1 << clk_info->div.bits) 444 && clk_info->div.div_table[i]; i++) { 445 if (clk_info->div.div_table[i] >= div && 446 clk_info->div.div_table[i] < best) { 447 best = clk_info->div.div_table[i]; 448 best_i = i; 449 450 if (div == best) 451 break; 452 } 453 } 454 455 return best_i; 456 } 457 458 static unsigned 459 ingenic_clk_calc_div(struct clk_hw *hw, 460 const struct ingenic_cgu_clk_info *clk_info, 461 unsigned long parent_rate, unsigned long req_rate) 462 { 463 unsigned int div, hw_div; 464 u8 parent; 465 466 parent = ingenic_clk_get_parent(hw); 467 if (clk_info->div.bypass_mask & BIT(parent)) 468 return 1; 469 470 /* calculate the divide */ 471 div = DIV_ROUND_UP(parent_rate, req_rate); 472 473 if (clk_info->div.div_table) { 474 hw_div = ingenic_clk_calc_hw_div(clk_info, div); 475 476 return clk_info->div.div_table[hw_div]; 477 } 478 479 /* Impose hardware constraints */ 480 div = clamp_t(unsigned int, div, clk_info->div.div, 481 clk_info->div.div << clk_info->div.bits); 482 483 /* 484 * If the divider value itself must be divided before being written to 485 * the divider register, we must ensure we don't have any bits set that 486 * would be lost as a result of doing so. 487 */ 488 div = DIV_ROUND_UP(div, clk_info->div.div); 489 div *= clk_info->div.div; 490 491 return div; 492 } 493 494 static long 495 ingenic_clk_round_rate(struct clk_hw *hw, unsigned long req_rate, 496 unsigned long *parent_rate) 497 { 498 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 499 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 500 unsigned int div = 1; 501 502 if (clk_info->type & CGU_CLK_DIV) 503 div = ingenic_clk_calc_div(hw, clk_info, *parent_rate, req_rate); 504 else if (clk_info->type & CGU_CLK_FIXDIV) 505 div = clk_info->fixdiv.div; 506 else if (clk_hw_can_set_rate_parent(hw)) 507 *parent_rate = req_rate; 508 509 return DIV_ROUND_UP(*parent_rate, div); 510 } 511 512 static inline int ingenic_clk_check_stable(struct ingenic_cgu *cgu, 513 const struct ingenic_cgu_clk_info *clk_info) 514 { 515 u32 reg; 516 517 return readl_poll_timeout(cgu->base + clk_info->div.reg, reg, 518 !(reg & BIT(clk_info->div.busy_bit)), 519 0, 100 * USEC_PER_MSEC); 520 } 521 522 static int 523 ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate, 524 unsigned long parent_rate) 525 { 526 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 527 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 528 struct ingenic_cgu *cgu = ingenic_clk->cgu; 529 unsigned long rate, flags; 530 unsigned int hw_div, div; 531 u32 reg, mask; 532 int ret = 0; 533 534 if (clk_info->type & CGU_CLK_DIV) { 535 div = ingenic_clk_calc_div(hw, clk_info, parent_rate, req_rate); 536 rate = DIV_ROUND_UP(parent_rate, div); 537 538 if (rate != req_rate) 539 return -EINVAL; 540 541 if (clk_info->div.div_table) 542 hw_div = ingenic_clk_calc_hw_div(clk_info, div); 543 else 544 hw_div = ((div / clk_info->div.div) - 1); 545 546 spin_lock_irqsave(&cgu->lock, flags); 547 reg = readl(cgu->base + clk_info->div.reg); 548 549 /* update the divide */ 550 mask = GENMASK(clk_info->div.bits - 1, 0); 551 reg &= ~(mask << clk_info->div.shift); 552 reg |= hw_div << clk_info->div.shift; 553 554 /* clear the stop bit */ 555 if (clk_info->div.stop_bit != -1) 556 reg &= ~BIT(clk_info->div.stop_bit); 557 558 /* set the change enable bit */ 559 if (clk_info->div.ce_bit != -1) 560 reg |= BIT(clk_info->div.ce_bit); 561 562 /* update the hardware */ 563 writel(reg, cgu->base + clk_info->div.reg); 564 565 /* wait for the change to take effect */ 566 if (clk_info->div.busy_bit != -1) 567 ret = ingenic_clk_check_stable(cgu, clk_info); 568 569 spin_unlock_irqrestore(&cgu->lock, flags); 570 return ret; 571 } 572 573 return -EINVAL; 574 } 575 576 static int ingenic_clk_enable(struct clk_hw *hw) 577 { 578 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 579 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 580 struct ingenic_cgu *cgu = ingenic_clk->cgu; 581 unsigned long flags; 582 583 if (clk_info->type & CGU_CLK_GATE) { 584 /* ungate the clock */ 585 spin_lock_irqsave(&cgu->lock, flags); 586 ingenic_cgu_gate_set(cgu, &clk_info->gate, false); 587 spin_unlock_irqrestore(&cgu->lock, flags); 588 589 if (clk_info->gate.delay_us) 590 udelay(clk_info->gate.delay_us); 591 } 592 593 return 0; 594 } 595 596 static void ingenic_clk_disable(struct clk_hw *hw) 597 { 598 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 599 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 600 struct ingenic_cgu *cgu = ingenic_clk->cgu; 601 unsigned long flags; 602 603 if (clk_info->type & CGU_CLK_GATE) { 604 /* gate the clock */ 605 spin_lock_irqsave(&cgu->lock, flags); 606 ingenic_cgu_gate_set(cgu, &clk_info->gate, true); 607 spin_unlock_irqrestore(&cgu->lock, flags); 608 } 609 } 610 611 static int ingenic_clk_is_enabled(struct clk_hw *hw) 612 { 613 struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); 614 const struct ingenic_cgu_clk_info *clk_info = to_clk_info(ingenic_clk); 615 struct ingenic_cgu *cgu = ingenic_clk->cgu; 616 int enabled = 1; 617 618 if (clk_info->type & CGU_CLK_GATE) 619 enabled = !ingenic_cgu_gate_get(cgu, &clk_info->gate); 620 621 return enabled; 622 } 623 624 static const struct clk_ops ingenic_clk_ops = { 625 .get_parent = ingenic_clk_get_parent, 626 .set_parent = ingenic_clk_set_parent, 627 628 .recalc_rate = ingenic_clk_recalc_rate, 629 .round_rate = ingenic_clk_round_rate, 630 .set_rate = ingenic_clk_set_rate, 631 632 .enable = ingenic_clk_enable, 633 .disable = ingenic_clk_disable, 634 .is_enabled = ingenic_clk_is_enabled, 635 }; 636 637 /* 638 * Setup functions. 639 */ 640 641 static int ingenic_register_clock(struct ingenic_cgu *cgu, unsigned idx) 642 { 643 const struct ingenic_cgu_clk_info *clk_info = &cgu->clock_info[idx]; 644 struct clk_init_data clk_init; 645 struct ingenic_clk *ingenic_clk = NULL; 646 struct clk *clk, *parent; 647 const char *parent_names[4]; 648 unsigned caps, i, num_possible; 649 int err = -EINVAL; 650 651 BUILD_BUG_ON(ARRAY_SIZE(clk_info->parents) > ARRAY_SIZE(parent_names)); 652 653 if (clk_info->type == CGU_CLK_EXT) { 654 clk = of_clk_get_by_name(cgu->np, clk_info->name); 655 if (IS_ERR(clk)) { 656 pr_err("%s: no external clock '%s' provided\n", 657 __func__, clk_info->name); 658 err = -ENODEV; 659 goto out; 660 } 661 err = clk_register_clkdev(clk, clk_info->name, NULL); 662 if (err) { 663 clk_put(clk); 664 goto out; 665 } 666 cgu->clocks.clks[idx] = clk; 667 return 0; 668 } 669 670 if (!clk_info->type) { 671 pr_err("%s: no clock type specified for '%s'\n", __func__, 672 clk_info->name); 673 goto out; 674 } 675 676 ingenic_clk = kzalloc(sizeof(*ingenic_clk), GFP_KERNEL); 677 if (!ingenic_clk) { 678 err = -ENOMEM; 679 goto out; 680 } 681 682 ingenic_clk->hw.init = &clk_init; 683 ingenic_clk->cgu = cgu; 684 ingenic_clk->idx = idx; 685 686 clk_init.name = clk_info->name; 687 clk_init.flags = clk_info->flags; 688 clk_init.parent_names = parent_names; 689 690 caps = clk_info->type; 691 692 if (caps & CGU_CLK_DIV) { 693 caps &= ~CGU_CLK_DIV; 694 } else if (!(caps & CGU_CLK_CUSTOM)) { 695 /* pass rate changes to the parent clock */ 696 clk_init.flags |= CLK_SET_RATE_PARENT; 697 } 698 699 if (caps & (CGU_CLK_MUX | CGU_CLK_CUSTOM)) { 700 clk_init.num_parents = 0; 701 702 if (caps & CGU_CLK_MUX) 703 num_possible = 1 << clk_info->mux.bits; 704 else 705 num_possible = ARRAY_SIZE(clk_info->parents); 706 707 for (i = 0; i < num_possible; i++) { 708 if (clk_info->parents[i] == -1) 709 continue; 710 711 parent = cgu->clocks.clks[clk_info->parents[i]]; 712 parent_names[clk_init.num_parents] = 713 __clk_get_name(parent); 714 clk_init.num_parents++; 715 } 716 717 BUG_ON(!clk_init.num_parents); 718 BUG_ON(clk_init.num_parents > ARRAY_SIZE(parent_names)); 719 } else { 720 BUG_ON(clk_info->parents[0] == -1); 721 clk_init.num_parents = 1; 722 parent = cgu->clocks.clks[clk_info->parents[0]]; 723 parent_names[0] = __clk_get_name(parent); 724 } 725 726 if (caps & CGU_CLK_CUSTOM) { 727 clk_init.ops = clk_info->custom.clk_ops; 728 729 caps &= ~CGU_CLK_CUSTOM; 730 731 if (caps) { 732 pr_err("%s: custom clock may not be combined with type 0x%x\n", 733 __func__, caps); 734 goto out; 735 } 736 } else if (caps & CGU_CLK_PLL) { 737 clk_init.ops = &ingenic_pll_ops; 738 739 caps &= ~CGU_CLK_PLL; 740 741 if (caps) { 742 pr_err("%s: PLL may not be combined with type 0x%x\n", 743 __func__, caps); 744 goto out; 745 } 746 } else { 747 clk_init.ops = &ingenic_clk_ops; 748 } 749 750 /* nothing to do for gates or fixed dividers */ 751 caps &= ~(CGU_CLK_GATE | CGU_CLK_FIXDIV); 752 753 if (caps & CGU_CLK_MUX) { 754 if (!(caps & CGU_CLK_MUX_GLITCHFREE)) 755 clk_init.flags |= CLK_SET_PARENT_GATE; 756 757 caps &= ~(CGU_CLK_MUX | CGU_CLK_MUX_GLITCHFREE); 758 } 759 760 if (caps) { 761 pr_err("%s: unknown clock type 0x%x\n", __func__, caps); 762 goto out; 763 } 764 765 clk = clk_register(NULL, &ingenic_clk->hw); 766 if (IS_ERR(clk)) { 767 pr_err("%s: failed to register clock '%s'\n", __func__, 768 clk_info->name); 769 err = PTR_ERR(clk); 770 goto out; 771 } 772 773 err = clk_register_clkdev(clk, clk_info->name, NULL); 774 if (err) 775 goto out; 776 777 cgu->clocks.clks[idx] = clk; 778 out: 779 if (err) 780 kfree(ingenic_clk); 781 return err; 782 } 783 784 struct ingenic_cgu * 785 ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info, 786 unsigned num_clocks, struct device_node *np) 787 { 788 struct ingenic_cgu *cgu; 789 790 cgu = kzalloc(sizeof(*cgu), GFP_KERNEL); 791 if (!cgu) 792 goto err_out; 793 794 cgu->base = of_iomap(np, 0); 795 if (!cgu->base) { 796 pr_err("%s: failed to map CGU registers\n", __func__); 797 goto err_out_free; 798 } 799 800 cgu->np = np; 801 cgu->clock_info = clock_info; 802 cgu->clocks.clk_num = num_clocks; 803 804 spin_lock_init(&cgu->lock); 805 806 return cgu; 807 808 err_out_free: 809 kfree(cgu); 810 err_out: 811 return NULL; 812 } 813 814 int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu) 815 { 816 unsigned i; 817 int err; 818 819 cgu->clocks.clks = kcalloc(cgu->clocks.clk_num, sizeof(struct clk *), 820 GFP_KERNEL); 821 if (!cgu->clocks.clks) { 822 err = -ENOMEM; 823 goto err_out; 824 } 825 826 for (i = 0; i < cgu->clocks.clk_num; i++) { 827 err = ingenic_register_clock(cgu, i); 828 if (err) 829 goto err_out_unregister; 830 } 831 832 err = of_clk_add_provider(cgu->np, of_clk_src_onecell_get, 833 &cgu->clocks); 834 if (err) 835 goto err_out_unregister; 836 837 return 0; 838 839 err_out_unregister: 840 for (i = 0; i < cgu->clocks.clk_num; i++) { 841 if (!cgu->clocks.clks[i]) 842 continue; 843 if (cgu->clock_info[i].type & CGU_CLK_EXT) 844 clk_put(cgu->clocks.clks[i]); 845 else 846 clk_unregister(cgu->clocks.clks[i]); 847 } 848 kfree(cgu->clocks.clks); 849 err_out: 850 return err; 851 } 852