1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Renesas Versaclock 3 4 * 5 * Copyright (C) 2023 Renesas Electronics Corp. 6 */ 7 8 #include <linux/clk-provider.h> 9 #include <linux/i2c.h> 10 #include <linux/limits.h> 11 #include <linux/module.h> 12 #include <linux/regmap.h> 13 14 #define NUM_CONFIG_REGISTERS 37 15 16 #define VC3_GENERAL_CTR 0x0 17 #define VC3_GENERAL_CTR_DIV1_SRC_SEL BIT(3) 18 #define VC3_GENERAL_CTR_PLL3_REFIN_SEL BIT(2) 19 20 #define VC3_PLL3_M_DIVIDER 0x3 21 #define VC3_PLL3_M_DIV1 BIT(7) 22 #define VC3_PLL3_M_DIV2 BIT(6) 23 #define VC3_PLL3_M_DIV(n) ((n) & GENMASK(5, 0)) 24 25 #define VC3_PLL3_N_DIVIDER 0x4 26 #define VC3_PLL3_LOOP_FILTER_N_DIV_MSB 0x5 27 28 #define VC3_PLL3_CHARGE_PUMP_CTRL 0x6 29 #define VC3_PLL3_CHARGE_PUMP_CTRL_OUTDIV3_SRC_SEL BIT(7) 30 31 #define VC3_PLL1_CTRL_OUTDIV5 0x7 32 #define VC3_PLL1_CTRL_OUTDIV5_PLL1_MDIV_DOUBLER BIT(7) 33 34 #define VC3_PLL1_M_DIVIDER 0x8 35 #define VC3_PLL1_M_DIV1 BIT(7) 36 #define VC3_PLL1_M_DIV2 BIT(6) 37 #define VC3_PLL1_M_DIV(n) ((n) & GENMASK(5, 0)) 38 39 #define VC3_PLL1_VCO_N_DIVIDER 0x9 40 #define VC3_PLL1_LOOP_FILTER_N_DIV_MSB 0x0a 41 42 #define VC3_OUT_DIV1_DIV2_CTRL 0xf 43 44 #define VC3_PLL2_FB_INT_DIV_MSB 0x10 45 #define VC3_PLL2_FB_INT_DIV_LSB 0x11 46 #define VC3_PLL2_FB_FRC_DIV_MSB 0x12 47 #define VC3_PLL2_FB_FRC_DIV_LSB 0x13 48 49 #define VC3_PLL2_M_DIVIDER 0x1a 50 #define VC3_PLL2_MDIV_DOUBLER BIT(7) 51 #define VC3_PLL2_M_DIV1 BIT(6) 52 #define VC3_PLL2_M_DIV2 BIT(5) 53 #define VC3_PLL2_M_DIV(n) ((n) & GENMASK(4, 0)) 54 55 #define VC3_OUT_DIV3_DIV4_CTRL 0x1b 56 57 #define VC3_PLL_OP_CTRL 0x1c 58 #define VC3_PLL_OP_CTRL_PLL2_REFIN_SEL 6 59 60 #define VC3_OUTPUT_CTR 0x1d 61 #define VC3_OUTPUT_CTR_DIV4_SRC_SEL BIT(3) 62 63 #define VC3_SE2_CTRL_REG0 0x1f 64 #define VC3_SE2_CTRL_REG0_SE2_CLK_SEL BIT(6) 65 66 #define VC3_SE3_DIFF1_CTRL_REG 0x21 67 #define VC3_SE3_DIFF1_CTRL_REG_SE3_CLK_SEL BIT(6) 68 69 #define VC3_DIFF1_CTRL_REG 0x22 70 #define VC3_DIFF1_CTRL_REG_DIFF1_CLK_SEL BIT(7) 71 72 #define VC3_DIFF2_CTRL_REG 0x23 73 #define VC3_DIFF2_CTRL_REG_DIFF2_CLK_SEL BIT(7) 74 75 #define VC3_SE1_DIV4_CTRL 0x24 76 #define VC3_SE1_DIV4_CTRL_SE1_CLK_SEL BIT(3) 77 78 #define VC3_PLL1_VCO_MIN 300000000UL 79 #define VC3_PLL1_VCO_MAX 600000000UL 80 81 #define VC3_PLL2_VCO_MIN 400000000UL 82 #define VC3_PLL2_VCO_MAX 1200000000UL 83 84 #define VC3_PLL3_VCO_MIN 300000000UL 85 #define VC3_PLL3_VCO_MAX 800000000UL 86 87 #define VC3_2_POW_16 (U16_MAX + 1) 88 #define VC3_DIV_MASK(width) ((1 << (width)) - 1) 89 90 enum vc3_pfd_mux { 91 VC3_PFD2_MUX, 92 VC3_PFD3_MUX, 93 }; 94 95 enum vc3_pfd { 96 VC3_PFD1, 97 VC3_PFD2, 98 VC3_PFD3, 99 }; 100 101 enum vc3_pll { 102 VC3_PLL1, 103 VC3_PLL2, 104 VC3_PLL3, 105 }; 106 107 enum vc3_div_mux { 108 VC3_DIV1_MUX, 109 VC3_DIV3_MUX, 110 VC3_DIV4_MUX, 111 }; 112 113 enum vc3_div { 114 VC3_DIV1, 115 VC3_DIV2, 116 VC3_DIV3, 117 VC3_DIV4, 118 VC3_DIV5, 119 }; 120 121 enum vc3_clk { 122 VC3_REF, 123 VC3_SE1, 124 VC3_SE2, 125 VC3_SE3, 126 VC3_DIFF1, 127 VC3_DIFF2, 128 }; 129 130 enum vc3_clk_mux { 131 VC3_SE1_MUX = VC3_SE1 - 1, 132 VC3_SE2_MUX = VC3_SE2 - 1, 133 VC3_SE3_MUX = VC3_SE3 - 1, 134 VC3_DIFF1_MUX = VC3_DIFF1 - 1, 135 VC3_DIFF2_MUX = VC3_DIFF2 - 1, 136 }; 137 138 struct vc3_clk_data { 139 u8 offs; 140 u8 bitmsk; 141 }; 142 143 struct vc3_pfd_data { 144 u8 num; 145 u8 offs; 146 u8 mdiv1_bitmsk; 147 u8 mdiv2_bitmsk; 148 }; 149 150 struct vc3_pll_data { 151 u8 num; 152 u8 int_div_msb_offs; 153 u8 int_div_lsb_offs; 154 unsigned long vco_min; 155 unsigned long vco_max; 156 }; 157 158 struct vc3_div_data { 159 u8 offs; 160 const struct clk_div_table *table; 161 u8 shift; 162 u8 width; 163 u8 flags; 164 }; 165 166 struct vc3_hw_data { 167 struct clk_hw hw; 168 struct regmap *regmap; 169 const void *data; 170 171 u32 div_int; 172 u32 div_frc; 173 }; 174 175 static const struct clk_div_table div1_divs[] = { 176 { .val = 0, .div = 1, }, { .val = 1, .div = 4, }, 177 { .val = 2, .div = 5, }, { .val = 3, .div = 6, }, 178 { .val = 4, .div = 2, }, { .val = 5, .div = 8, }, 179 { .val = 6, .div = 10, }, { .val = 7, .div = 12, }, 180 { .val = 8, .div = 4, }, { .val = 9, .div = 16, }, 181 { .val = 10, .div = 20, }, { .val = 11, .div = 24, }, 182 { .val = 12, .div = 8, }, { .val = 13, .div = 32, }, 183 { .val = 14, .div = 40, }, { .val = 15, .div = 48, }, 184 {} 185 }; 186 187 static const struct clk_div_table div245_divs[] = { 188 { .val = 0, .div = 1, }, { .val = 1, .div = 3, }, 189 { .val = 2, .div = 5, }, { .val = 3, .div = 10, }, 190 { .val = 4, .div = 2, }, { .val = 5, .div = 6, }, 191 { .val = 6, .div = 10, }, { .val = 7, .div = 20, }, 192 { .val = 8, .div = 4, }, { .val = 9, .div = 12, }, 193 { .val = 10, .div = 20, }, { .val = 11, .div = 40, }, 194 { .val = 12, .div = 5, }, { .val = 13, .div = 15, }, 195 { .val = 14, .div = 25, }, { .val = 15, .div = 50, }, 196 {} 197 }; 198 199 static const struct clk_div_table div3_divs[] = { 200 { .val = 0, .div = 1, }, { .val = 1, .div = 3, }, 201 { .val = 2, .div = 5, }, { .val = 3, .div = 10, }, 202 { .val = 4, .div = 2, }, { .val = 5, .div = 6, }, 203 { .val = 6, .div = 10, }, { .val = 7, .div = 20, }, 204 { .val = 8, .div = 4, }, { .val = 9, .div = 12, }, 205 { .val = 10, .div = 20, }, { .val = 11, .div = 40, }, 206 { .val = 12, .div = 8, }, { .val = 13, .div = 24, }, 207 { .val = 14, .div = 40, }, { .val = 15, .div = 80, }, 208 {} 209 }; 210 211 static struct clk_hw *clk_out[6]; 212 213 static unsigned char vc3_pfd_mux_get_parent(struct clk_hw *hw) 214 { 215 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 216 const struct vc3_clk_data *pfd_mux = vc3->data; 217 u32 src; 218 219 regmap_read(vc3->regmap, pfd_mux->offs, &src); 220 221 return !!(src & pfd_mux->bitmsk); 222 } 223 224 static int vc3_pfd_mux_set_parent(struct clk_hw *hw, u8 index) 225 { 226 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 227 const struct vc3_clk_data *pfd_mux = vc3->data; 228 229 regmap_update_bits(vc3->regmap, pfd_mux->offs, pfd_mux->bitmsk, 230 index ? pfd_mux->bitmsk : 0); 231 return 0; 232 } 233 234 static const struct clk_ops vc3_pfd_mux_ops = { 235 .determine_rate = clk_hw_determine_rate_no_reparent, 236 .set_parent = vc3_pfd_mux_set_parent, 237 .get_parent = vc3_pfd_mux_get_parent, 238 }; 239 240 static unsigned long vc3_pfd_recalc_rate(struct clk_hw *hw, 241 unsigned long parent_rate) 242 { 243 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 244 const struct vc3_pfd_data *pfd = vc3->data; 245 unsigned int prediv, premul; 246 unsigned long rate; 247 u8 mdiv; 248 249 regmap_read(vc3->regmap, pfd->offs, &prediv); 250 if (pfd->num == VC3_PFD1) { 251 /* The bypass_prediv is set, PLL fed from Ref_in directly. */ 252 if (prediv & pfd->mdiv1_bitmsk) { 253 /* check doubler is set or not */ 254 regmap_read(vc3->regmap, VC3_PLL1_CTRL_OUTDIV5, &premul); 255 if (premul & VC3_PLL1_CTRL_OUTDIV5_PLL1_MDIV_DOUBLER) 256 parent_rate *= 2; 257 return parent_rate; 258 } 259 mdiv = VC3_PLL1_M_DIV(prediv); 260 } else if (pfd->num == VC3_PFD2) { 261 /* The bypass_prediv is set, PLL fed from Ref_in directly. */ 262 if (prediv & pfd->mdiv1_bitmsk) { 263 regmap_read(vc3->regmap, VC3_PLL2_M_DIVIDER, &premul); 264 /* check doubler is set or not */ 265 if (premul & VC3_PLL2_MDIV_DOUBLER) 266 parent_rate *= 2; 267 return parent_rate; 268 } 269 270 mdiv = VC3_PLL2_M_DIV(prediv); 271 } else { 272 /* The bypass_prediv is set, PLL fed from Ref_in directly. */ 273 if (prediv & pfd->mdiv1_bitmsk) 274 return parent_rate; 275 276 mdiv = VC3_PLL3_M_DIV(prediv); 277 } 278 279 if (prediv & pfd->mdiv2_bitmsk) 280 rate = parent_rate / 2; 281 else 282 rate = parent_rate / mdiv; 283 284 return rate; 285 } 286 287 static long vc3_pfd_round_rate(struct clk_hw *hw, unsigned long rate, 288 unsigned long *parent_rate) 289 { 290 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 291 const struct vc3_pfd_data *pfd = vc3->data; 292 unsigned long idiv; 293 294 /* PLL cannot operate with input clock above 50 MHz. */ 295 if (rate > 50000000) 296 return -EINVAL; 297 298 /* CLKIN within range of PLL input, feed directly to PLL. */ 299 if (*parent_rate <= 50000000) 300 return *parent_rate; 301 302 idiv = DIV_ROUND_UP(*parent_rate, rate); 303 if (pfd->num == VC3_PFD1 || pfd->num == VC3_PFD3) { 304 if (idiv > 63) 305 return -EINVAL; 306 } else { 307 if (idiv > 31) 308 return -EINVAL; 309 } 310 311 return *parent_rate / idiv; 312 } 313 314 static int vc3_pfd_set_rate(struct clk_hw *hw, unsigned long rate, 315 unsigned long parent_rate) 316 { 317 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 318 const struct vc3_pfd_data *pfd = vc3->data; 319 unsigned long idiv; 320 u8 div; 321 322 /* CLKIN within range of PLL input, feed directly to PLL. */ 323 if (parent_rate <= 50000000) { 324 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv1_bitmsk, 325 pfd->mdiv1_bitmsk); 326 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv2_bitmsk, 0); 327 return 0; 328 } 329 330 idiv = DIV_ROUND_UP(parent_rate, rate); 331 /* We have dedicated div-2 predivider. */ 332 if (idiv == 2) { 333 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv2_bitmsk, 334 pfd->mdiv2_bitmsk); 335 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv1_bitmsk, 0); 336 } else { 337 if (pfd->num == VC3_PFD1) 338 div = VC3_PLL1_M_DIV(idiv); 339 else if (pfd->num == VC3_PFD2) 340 div = VC3_PLL2_M_DIV(idiv); 341 else 342 div = VC3_PLL3_M_DIV(idiv); 343 344 regmap_write(vc3->regmap, pfd->offs, div); 345 } 346 347 return 0; 348 } 349 350 static const struct clk_ops vc3_pfd_ops = { 351 .recalc_rate = vc3_pfd_recalc_rate, 352 .round_rate = vc3_pfd_round_rate, 353 .set_rate = vc3_pfd_set_rate, 354 }; 355 356 static unsigned long vc3_pll_recalc_rate(struct clk_hw *hw, 357 unsigned long parent_rate) 358 { 359 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 360 const struct vc3_pll_data *pll = vc3->data; 361 u32 div_int, div_frc, val; 362 unsigned long rate; 363 364 regmap_read(vc3->regmap, pll->int_div_msb_offs, &val); 365 div_int = (val & GENMASK(2, 0)) << 8; 366 regmap_read(vc3->regmap, pll->int_div_lsb_offs, &val); 367 div_int |= val; 368 369 if (pll->num == VC3_PLL2) { 370 regmap_read(vc3->regmap, VC3_PLL2_FB_FRC_DIV_MSB, &val); 371 div_frc = val << 8; 372 regmap_read(vc3->regmap, VC3_PLL2_FB_FRC_DIV_LSB, &val); 373 div_frc |= val; 374 rate = (parent_rate * 375 (div_int * VC3_2_POW_16 + div_frc) / VC3_2_POW_16); 376 } else { 377 rate = parent_rate * div_int; 378 } 379 380 return rate; 381 } 382 383 static long vc3_pll_round_rate(struct clk_hw *hw, unsigned long rate, 384 unsigned long *parent_rate) 385 { 386 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 387 const struct vc3_pll_data *pll = vc3->data; 388 u64 div_frc; 389 390 if (rate < pll->vco_min) 391 rate = pll->vco_min; 392 if (rate > pll->vco_max) 393 rate = pll->vco_max; 394 395 vc3->div_int = rate / *parent_rate; 396 397 if (pll->num == VC3_PLL2) { 398 if (vc3->div_int > 0x7ff) 399 rate = *parent_rate * 0x7ff; 400 401 /* Determine best fractional part, which is 16 bit wide */ 402 div_frc = rate % *parent_rate; 403 div_frc *= BIT(16) - 1; 404 405 vc3->div_frc = min_t(u64, div64_ul(div_frc, *parent_rate), U16_MAX); 406 rate = (*parent_rate * 407 (vc3->div_int * VC3_2_POW_16 + vc3->div_frc) / VC3_2_POW_16); 408 } else { 409 rate = *parent_rate * vc3->div_int; 410 } 411 412 return rate; 413 } 414 415 static int vc3_pll_set_rate(struct clk_hw *hw, unsigned long rate, 416 unsigned long parent_rate) 417 { 418 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 419 const struct vc3_pll_data *pll = vc3->data; 420 u32 val; 421 422 regmap_read(vc3->regmap, pll->int_div_msb_offs, &val); 423 val = (val & 0xf8) | ((vc3->div_int >> 8) & 0x7); 424 regmap_write(vc3->regmap, pll->int_div_msb_offs, val); 425 regmap_write(vc3->regmap, pll->int_div_lsb_offs, vc3->div_int & 0xff); 426 427 if (pll->num == VC3_PLL2) { 428 regmap_write(vc3->regmap, VC3_PLL2_FB_FRC_DIV_MSB, 429 vc3->div_frc >> 8); 430 regmap_write(vc3->regmap, VC3_PLL2_FB_FRC_DIV_LSB, 431 vc3->div_frc & 0xff); 432 } 433 434 return 0; 435 } 436 437 static const struct clk_ops vc3_pll_ops = { 438 .recalc_rate = vc3_pll_recalc_rate, 439 .round_rate = vc3_pll_round_rate, 440 .set_rate = vc3_pll_set_rate, 441 }; 442 443 static unsigned char vc3_div_mux_get_parent(struct clk_hw *hw) 444 { 445 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 446 const struct vc3_clk_data *div_mux = vc3->data; 447 u32 src; 448 449 regmap_read(vc3->regmap, div_mux->offs, &src); 450 451 return !!(src & div_mux->bitmsk); 452 } 453 454 static int vc3_div_mux_set_parent(struct clk_hw *hw, u8 index) 455 { 456 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 457 const struct vc3_clk_data *div_mux = vc3->data; 458 459 regmap_update_bits(vc3->regmap, div_mux->offs, div_mux->bitmsk, 460 index ? div_mux->bitmsk : 0); 461 462 return 0; 463 } 464 465 static const struct clk_ops vc3_div_mux_ops = { 466 .determine_rate = clk_hw_determine_rate_no_reparent, 467 .set_parent = vc3_div_mux_set_parent, 468 .get_parent = vc3_div_mux_get_parent, 469 }; 470 471 static unsigned int vc3_get_div(const struct clk_div_table *table, 472 unsigned int val, unsigned long flag) 473 { 474 const struct clk_div_table *clkt; 475 476 for (clkt = table; clkt->div; clkt++) 477 if (clkt->val == val) 478 return clkt->div; 479 480 return 0; 481 } 482 483 static unsigned long vc3_div_recalc_rate(struct clk_hw *hw, 484 unsigned long parent_rate) 485 { 486 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 487 const struct vc3_div_data *div_data = vc3->data; 488 unsigned int val; 489 490 regmap_read(vc3->regmap, div_data->offs, &val); 491 val >>= div_data->shift; 492 val &= VC3_DIV_MASK(div_data->width); 493 494 return divider_recalc_rate(hw, parent_rate, val, div_data->table, 495 div_data->flags, div_data->width); 496 } 497 498 static long vc3_div_round_rate(struct clk_hw *hw, unsigned long rate, 499 unsigned long *parent_rate) 500 { 501 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 502 const struct vc3_div_data *div_data = vc3->data; 503 unsigned int bestdiv; 504 505 /* if read only, just return current value */ 506 if (div_data->flags & CLK_DIVIDER_READ_ONLY) { 507 regmap_read(vc3->regmap, div_data->offs, &bestdiv); 508 bestdiv >>= div_data->shift; 509 bestdiv &= VC3_DIV_MASK(div_data->width); 510 bestdiv = vc3_get_div(div_data->table, bestdiv, div_data->flags); 511 return DIV_ROUND_UP(*parent_rate, bestdiv); 512 } 513 514 return divider_round_rate(hw, rate, parent_rate, div_data->table, 515 div_data->width, div_data->flags); 516 } 517 518 static int vc3_div_set_rate(struct clk_hw *hw, unsigned long rate, 519 unsigned long parent_rate) 520 { 521 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 522 const struct vc3_div_data *div_data = vc3->data; 523 unsigned int value; 524 525 value = divider_get_val(rate, parent_rate, div_data->table, 526 div_data->width, div_data->flags); 527 regmap_update_bits(vc3->regmap, div_data->offs, 528 VC3_DIV_MASK(div_data->width) << div_data->shift, 529 value << div_data->shift); 530 return 0; 531 } 532 533 static const struct clk_ops vc3_div_ops = { 534 .recalc_rate = vc3_div_recalc_rate, 535 .round_rate = vc3_div_round_rate, 536 .set_rate = vc3_div_set_rate, 537 }; 538 539 static int vc3_clk_mux_determine_rate(struct clk_hw *hw, 540 struct clk_rate_request *req) 541 { 542 int ret; 543 int frc; 544 545 ret = clk_mux_determine_rate_flags(hw, req, CLK_SET_RATE_PARENT); 546 if (ret) { 547 /* The below check is equivalent to (best_parent_rate/rate) */ 548 if (req->best_parent_rate >= req->rate) { 549 frc = DIV_ROUND_CLOSEST_ULL(req->best_parent_rate, 550 req->rate); 551 req->rate *= frc; 552 return clk_mux_determine_rate_flags(hw, req, 553 CLK_SET_RATE_PARENT); 554 } 555 ret = 0; 556 } 557 558 return ret; 559 } 560 561 static unsigned char vc3_clk_mux_get_parent(struct clk_hw *hw) 562 { 563 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 564 const struct vc3_clk_data *clk_mux = vc3->data; 565 u32 val; 566 567 regmap_read(vc3->regmap, clk_mux->offs, &val); 568 569 return !!(val & clk_mux->bitmsk); 570 } 571 572 static int vc3_clk_mux_set_parent(struct clk_hw *hw, u8 index) 573 { 574 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw); 575 const struct vc3_clk_data *clk_mux = vc3->data; 576 577 regmap_update_bits(vc3->regmap, clk_mux->offs, 578 clk_mux->bitmsk, index ? clk_mux->bitmsk : 0); 579 return 0; 580 } 581 582 static const struct clk_ops vc3_clk_mux_ops = { 583 .determine_rate = vc3_clk_mux_determine_rate, 584 .set_parent = vc3_clk_mux_set_parent, 585 .get_parent = vc3_clk_mux_get_parent, 586 }; 587 588 static const struct regmap_config vc3_regmap_config = { 589 .reg_bits = 8, 590 .val_bits = 8, 591 .cache_type = REGCACHE_MAPLE, 592 .max_register = 0x24, 593 }; 594 595 static struct vc3_hw_data clk_div[5]; 596 597 static const struct clk_parent_data pfd_mux_parent_data[] = { 598 { .index = 0, }, 599 { .hw = &clk_div[VC3_DIV2].hw } 600 }; 601 602 static struct vc3_hw_data clk_pfd_mux[] = { 603 [VC3_PFD2_MUX] = { 604 .data = &(struct vc3_clk_data) { 605 .offs = VC3_PLL_OP_CTRL, 606 .bitmsk = BIT(VC3_PLL_OP_CTRL_PLL2_REFIN_SEL) 607 }, 608 .hw.init = &(struct clk_init_data){ 609 .name = "pfd2_mux", 610 .ops = &vc3_pfd_mux_ops, 611 .parent_data = pfd_mux_parent_data, 612 .num_parents = 2, 613 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT 614 } 615 }, 616 [VC3_PFD3_MUX] = { 617 .data = &(struct vc3_clk_data) { 618 .offs = VC3_GENERAL_CTR, 619 .bitmsk = BIT(VC3_GENERAL_CTR_PLL3_REFIN_SEL) 620 }, 621 .hw.init = &(struct clk_init_data){ 622 .name = "pfd3_mux", 623 .ops = &vc3_pfd_mux_ops, 624 .parent_data = pfd_mux_parent_data, 625 .num_parents = 2, 626 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT 627 } 628 } 629 }; 630 631 static struct vc3_hw_data clk_pfd[] = { 632 [VC3_PFD1] = { 633 .data = &(struct vc3_pfd_data) { 634 .num = VC3_PFD1, 635 .offs = VC3_PLL1_M_DIVIDER, 636 .mdiv1_bitmsk = VC3_PLL1_M_DIV1, 637 .mdiv2_bitmsk = VC3_PLL1_M_DIV2 638 }, 639 .hw.init = &(struct clk_init_data){ 640 .name = "pfd1", 641 .ops = &vc3_pfd_ops, 642 .parent_data = &(const struct clk_parent_data) { 643 .index = 0 644 }, 645 .num_parents = 1, 646 .flags = CLK_SET_RATE_PARENT 647 } 648 }, 649 [VC3_PFD2] = { 650 .data = &(struct vc3_pfd_data) { 651 .num = VC3_PFD2, 652 .offs = VC3_PLL2_M_DIVIDER, 653 .mdiv1_bitmsk = VC3_PLL2_M_DIV1, 654 .mdiv2_bitmsk = VC3_PLL2_M_DIV2 655 }, 656 .hw.init = &(struct clk_init_data){ 657 .name = "pfd2", 658 .ops = &vc3_pfd_ops, 659 .parent_hws = (const struct clk_hw *[]) { 660 &clk_pfd_mux[VC3_PFD2_MUX].hw 661 }, 662 .num_parents = 1, 663 .flags = CLK_SET_RATE_PARENT 664 } 665 }, 666 [VC3_PFD3] = { 667 .data = &(struct vc3_pfd_data) { 668 .num = VC3_PFD3, 669 .offs = VC3_PLL3_M_DIVIDER, 670 .mdiv1_bitmsk = VC3_PLL3_M_DIV1, 671 .mdiv2_bitmsk = VC3_PLL3_M_DIV2 672 }, 673 .hw.init = &(struct clk_init_data){ 674 .name = "pfd3", 675 .ops = &vc3_pfd_ops, 676 .parent_hws = (const struct clk_hw *[]) { 677 &clk_pfd_mux[VC3_PFD3_MUX].hw 678 }, 679 .num_parents = 1, 680 .flags = CLK_SET_RATE_PARENT 681 } 682 } 683 }; 684 685 static struct vc3_hw_data clk_pll[] = { 686 [VC3_PLL1] = { 687 .data = &(struct vc3_pll_data) { 688 .num = VC3_PLL1, 689 .int_div_msb_offs = VC3_PLL1_LOOP_FILTER_N_DIV_MSB, 690 .int_div_lsb_offs = VC3_PLL1_VCO_N_DIVIDER, 691 .vco_min = VC3_PLL1_VCO_MIN, 692 .vco_max = VC3_PLL1_VCO_MAX 693 }, 694 .hw.init = &(struct clk_init_data){ 695 .name = "pll1", 696 .ops = &vc3_pll_ops, 697 .parent_hws = (const struct clk_hw *[]) { 698 &clk_pfd[VC3_PFD1].hw 699 }, 700 .num_parents = 1, 701 .flags = CLK_SET_RATE_PARENT 702 } 703 }, 704 [VC3_PLL2] = { 705 .data = &(struct vc3_pll_data) { 706 .num = VC3_PLL2, 707 .int_div_msb_offs = VC3_PLL2_FB_INT_DIV_MSB, 708 .int_div_lsb_offs = VC3_PLL2_FB_INT_DIV_LSB, 709 .vco_min = VC3_PLL2_VCO_MIN, 710 .vco_max = VC3_PLL2_VCO_MAX 711 }, 712 .hw.init = &(struct clk_init_data){ 713 .name = "pll2", 714 .ops = &vc3_pll_ops, 715 .parent_hws = (const struct clk_hw *[]) { 716 &clk_pfd[VC3_PFD2].hw 717 }, 718 .num_parents = 1, 719 .flags = CLK_SET_RATE_PARENT 720 } 721 }, 722 [VC3_PLL3] = { 723 .data = &(struct vc3_pll_data) { 724 .num = VC3_PLL3, 725 .int_div_msb_offs = VC3_PLL3_LOOP_FILTER_N_DIV_MSB, 726 .int_div_lsb_offs = VC3_PLL3_N_DIVIDER, 727 .vco_min = VC3_PLL3_VCO_MIN, 728 .vco_max = VC3_PLL3_VCO_MAX 729 }, 730 .hw.init = &(struct clk_init_data){ 731 .name = "pll3", 732 .ops = &vc3_pll_ops, 733 .parent_hws = (const struct clk_hw *[]) { 734 &clk_pfd[VC3_PFD3].hw 735 }, 736 .num_parents = 1, 737 .flags = CLK_SET_RATE_PARENT 738 } 739 } 740 }; 741 742 static const struct clk_parent_data div_mux_parent_data[][2] = { 743 [VC3_DIV1_MUX] = { 744 { .hw = &clk_pll[VC3_PLL1].hw }, 745 { .index = 0 } 746 }, 747 [VC3_DIV3_MUX] = { 748 { .hw = &clk_pll[VC3_PLL2].hw }, 749 { .hw = &clk_pll[VC3_PLL3].hw } 750 }, 751 [VC3_DIV4_MUX] = { 752 { .hw = &clk_pll[VC3_PLL2].hw }, 753 { .index = 0 } 754 } 755 }; 756 757 static struct vc3_hw_data clk_div_mux[] = { 758 [VC3_DIV1_MUX] = { 759 .data = &(struct vc3_clk_data) { 760 .offs = VC3_GENERAL_CTR, 761 .bitmsk = VC3_GENERAL_CTR_DIV1_SRC_SEL 762 }, 763 .hw.init = &(struct clk_init_data){ 764 .name = "div1_mux", 765 .ops = &vc3_div_mux_ops, 766 .parent_data = div_mux_parent_data[VC3_DIV1_MUX], 767 .num_parents = 2, 768 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT 769 } 770 }, 771 [VC3_DIV3_MUX] = { 772 .data = &(struct vc3_clk_data) { 773 .offs = VC3_PLL3_CHARGE_PUMP_CTRL, 774 .bitmsk = VC3_PLL3_CHARGE_PUMP_CTRL_OUTDIV3_SRC_SEL 775 }, 776 .hw.init = &(struct clk_init_data){ 777 .name = "div3_mux", 778 .ops = &vc3_div_mux_ops, 779 .parent_data = div_mux_parent_data[VC3_DIV3_MUX], 780 .num_parents = 2, 781 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT 782 } 783 }, 784 [VC3_DIV4_MUX] = { 785 .data = &(struct vc3_clk_data) { 786 .offs = VC3_OUTPUT_CTR, 787 .bitmsk = VC3_OUTPUT_CTR_DIV4_SRC_SEL 788 }, 789 .hw.init = &(struct clk_init_data){ 790 .name = "div4_mux", 791 .ops = &vc3_div_mux_ops, 792 .parent_data = div_mux_parent_data[VC3_DIV4_MUX], 793 .num_parents = 2, 794 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT 795 } 796 } 797 }; 798 799 static struct vc3_hw_data clk_div[] = { 800 [VC3_DIV1] = { 801 .data = &(struct vc3_div_data) { 802 .offs = VC3_OUT_DIV1_DIV2_CTRL, 803 .table = div1_divs, 804 .shift = 4, 805 .width = 4, 806 .flags = CLK_DIVIDER_READ_ONLY 807 }, 808 .hw.init = &(struct clk_init_data){ 809 .name = "div1", 810 .ops = &vc3_div_ops, 811 .parent_hws = (const struct clk_hw *[]) { 812 &clk_div_mux[VC3_DIV1_MUX].hw 813 }, 814 .num_parents = 1, 815 .flags = CLK_SET_RATE_PARENT 816 } 817 }, 818 [VC3_DIV2] = { 819 .data = &(struct vc3_div_data) { 820 .offs = VC3_OUT_DIV1_DIV2_CTRL, 821 .table = div245_divs, 822 .shift = 0, 823 .width = 4, 824 .flags = CLK_DIVIDER_READ_ONLY 825 }, 826 .hw.init = &(struct clk_init_data){ 827 .name = "div2", 828 .ops = &vc3_div_ops, 829 .parent_hws = (const struct clk_hw *[]) { 830 &clk_pll[VC3_PLL1].hw 831 }, 832 .num_parents = 1, 833 .flags = CLK_SET_RATE_PARENT 834 } 835 }, 836 [VC3_DIV3] = { 837 .data = &(struct vc3_div_data) { 838 .offs = VC3_OUT_DIV3_DIV4_CTRL, 839 .table = div3_divs, 840 .shift = 4, 841 .width = 4, 842 .flags = CLK_DIVIDER_READ_ONLY 843 }, 844 .hw.init = &(struct clk_init_data){ 845 .name = "div3", 846 .ops = &vc3_div_ops, 847 .parent_hws = (const struct clk_hw *[]) { 848 &clk_div_mux[VC3_DIV3_MUX].hw 849 }, 850 .num_parents = 1, 851 .flags = CLK_SET_RATE_PARENT 852 } 853 }, 854 [VC3_DIV4] = { 855 .data = &(struct vc3_div_data) { 856 .offs = VC3_OUT_DIV3_DIV4_CTRL, 857 .table = div245_divs, 858 .shift = 0, 859 .width = 4, 860 .flags = CLK_DIVIDER_READ_ONLY 861 }, 862 .hw.init = &(struct clk_init_data){ 863 .name = "div4", 864 .ops = &vc3_div_ops, 865 .parent_hws = (const struct clk_hw *[]) { 866 &clk_div_mux[VC3_DIV4_MUX].hw 867 }, 868 .num_parents = 1, 869 .flags = CLK_SET_RATE_PARENT 870 } 871 }, 872 [VC3_DIV5] = { 873 .data = &(struct vc3_div_data) { 874 .offs = VC3_PLL1_CTRL_OUTDIV5, 875 .table = div245_divs, 876 .shift = 0, 877 .width = 4, 878 .flags = CLK_DIVIDER_READ_ONLY 879 }, 880 .hw.init = &(struct clk_init_data){ 881 .name = "div5", 882 .ops = &vc3_div_ops, 883 .parent_hws = (const struct clk_hw *[]) { 884 &clk_pll[VC3_PLL3].hw 885 }, 886 .num_parents = 1, 887 .flags = CLK_SET_RATE_PARENT 888 } 889 } 890 }; 891 892 static struct vc3_hw_data clk_mux[] = { 893 [VC3_SE1_MUX] = { 894 .data = &(struct vc3_clk_data) { 895 .offs = VC3_SE1_DIV4_CTRL, 896 .bitmsk = VC3_SE1_DIV4_CTRL_SE1_CLK_SEL 897 }, 898 .hw.init = &(struct clk_init_data){ 899 .name = "se1_mux", 900 .ops = &vc3_clk_mux_ops, 901 .parent_hws = (const struct clk_hw *[]) { 902 &clk_div[VC3_DIV5].hw, 903 &clk_div[VC3_DIV4].hw 904 }, 905 .num_parents = 2, 906 .flags = CLK_SET_RATE_PARENT 907 } 908 }, 909 [VC3_SE2_MUX] = { 910 .data = &(struct vc3_clk_data) { 911 .offs = VC3_SE2_CTRL_REG0, 912 .bitmsk = VC3_SE2_CTRL_REG0_SE2_CLK_SEL 913 }, 914 .hw.init = &(struct clk_init_data){ 915 .name = "se2_mux", 916 .ops = &vc3_clk_mux_ops, 917 .parent_hws = (const struct clk_hw *[]) { 918 &clk_div[VC3_DIV5].hw, 919 &clk_div[VC3_DIV4].hw 920 }, 921 .num_parents = 2, 922 .flags = CLK_SET_RATE_PARENT 923 } 924 }, 925 [VC3_SE3_MUX] = { 926 .data = &(struct vc3_clk_data) { 927 .offs = VC3_SE3_DIFF1_CTRL_REG, 928 .bitmsk = VC3_SE3_DIFF1_CTRL_REG_SE3_CLK_SEL 929 }, 930 .hw.init = &(struct clk_init_data){ 931 .name = "se3_mux", 932 .ops = &vc3_clk_mux_ops, 933 .parent_hws = (const struct clk_hw *[]) { 934 &clk_div[VC3_DIV2].hw, 935 &clk_div[VC3_DIV4].hw 936 }, 937 .num_parents = 2, 938 .flags = CLK_SET_RATE_PARENT 939 } 940 }, 941 [VC3_DIFF1_MUX] = { 942 .data = &(struct vc3_clk_data) { 943 .offs = VC3_DIFF1_CTRL_REG, 944 .bitmsk = VC3_DIFF1_CTRL_REG_DIFF1_CLK_SEL 945 }, 946 .hw.init = &(struct clk_init_data){ 947 .name = "diff1_mux", 948 .ops = &vc3_clk_mux_ops, 949 .parent_hws = (const struct clk_hw *[]) { 950 &clk_div[VC3_DIV1].hw, 951 &clk_div[VC3_DIV3].hw 952 }, 953 .num_parents = 2, 954 .flags = CLK_SET_RATE_PARENT 955 } 956 }, 957 [VC3_DIFF2_MUX] = { 958 .data = &(struct vc3_clk_data) { 959 .offs = VC3_DIFF2_CTRL_REG, 960 .bitmsk = VC3_DIFF2_CTRL_REG_DIFF2_CLK_SEL 961 }, 962 .hw.init = &(struct clk_init_data){ 963 .name = "diff2_mux", 964 .ops = &vc3_clk_mux_ops, 965 .parent_hws = (const struct clk_hw *[]) { 966 &clk_div[VC3_DIV1].hw, 967 &clk_div[VC3_DIV3].hw 968 }, 969 .num_parents = 2, 970 .flags = CLK_SET_RATE_PARENT 971 } 972 } 973 }; 974 975 static struct clk_hw *vc3_of_clk_get(struct of_phandle_args *clkspec, 976 void *data) 977 { 978 unsigned int idx = clkspec->args[0]; 979 struct clk_hw **clkout_hw = data; 980 981 if (idx >= ARRAY_SIZE(clk_out)) { 982 pr_err("invalid clk index %u for provider %pOF\n", idx, clkspec->np); 983 return ERR_PTR(-EINVAL); 984 } 985 986 return clkout_hw[idx]; 987 } 988 989 static int vc3_probe(struct i2c_client *client) 990 { 991 struct device *dev = &client->dev; 992 u8 settings[NUM_CONFIG_REGISTERS]; 993 struct regmap *regmap; 994 const char *name; 995 int ret, i; 996 997 regmap = devm_regmap_init_i2c(client, &vc3_regmap_config); 998 if (IS_ERR(regmap)) 999 return dev_err_probe(dev, PTR_ERR(regmap), 1000 "failed to allocate register map\n"); 1001 1002 ret = of_property_read_u8_array(dev->of_node, "renesas,settings", 1003 settings, ARRAY_SIZE(settings)); 1004 if (!ret) { 1005 /* 1006 * A raw settings array was specified in the DT. Write the 1007 * settings to the device immediately. 1008 */ 1009 for (i = 0; i < NUM_CONFIG_REGISTERS; i++) { 1010 ret = regmap_write(regmap, i, settings[i]); 1011 if (ret) { 1012 dev_err(dev, "error writing to chip (%i)\n", ret); 1013 return ret; 1014 } 1015 } 1016 } else if (ret == -EOVERFLOW) { 1017 dev_err(&client->dev, "EOVERFLOW reg settings. ARRAY_SIZE: %zu\n", 1018 ARRAY_SIZE(settings)); 1019 return ret; 1020 } 1021 1022 /* Register pfd muxes */ 1023 for (i = 0; i < ARRAY_SIZE(clk_pfd_mux); i++) { 1024 clk_pfd_mux[i].regmap = regmap; 1025 ret = devm_clk_hw_register(dev, &clk_pfd_mux[i].hw); 1026 if (ret) 1027 return dev_err_probe(dev, ret, "%s failed\n", 1028 clk_pfd_mux[i].hw.init->name); 1029 } 1030 1031 /* Register pfd's */ 1032 for (i = 0; i < ARRAY_SIZE(clk_pfd); i++) { 1033 clk_pfd[i].regmap = regmap; 1034 ret = devm_clk_hw_register(dev, &clk_pfd[i].hw); 1035 if (ret) 1036 return dev_err_probe(dev, ret, "%s failed\n", 1037 clk_pfd[i].hw.init->name); 1038 } 1039 1040 /* Register pll's */ 1041 for (i = 0; i < ARRAY_SIZE(clk_pll); i++) { 1042 clk_pll[i].regmap = regmap; 1043 ret = devm_clk_hw_register(dev, &clk_pll[i].hw); 1044 if (ret) 1045 return dev_err_probe(dev, ret, "%s failed\n", 1046 clk_pll[i].hw.init->name); 1047 } 1048 1049 /* Register divider muxes */ 1050 for (i = 0; i < ARRAY_SIZE(clk_div_mux); i++) { 1051 clk_div_mux[i].regmap = regmap; 1052 ret = devm_clk_hw_register(dev, &clk_div_mux[i].hw); 1053 if (ret) 1054 return dev_err_probe(dev, ret, "%s failed\n", 1055 clk_div_mux[i].hw.init->name); 1056 } 1057 1058 /* Register dividers */ 1059 for (i = 0; i < ARRAY_SIZE(clk_div); i++) { 1060 clk_div[i].regmap = regmap; 1061 ret = devm_clk_hw_register(dev, &clk_div[i].hw); 1062 if (ret) 1063 return dev_err_probe(dev, ret, "%s failed\n", 1064 clk_div[i].hw.init->name); 1065 } 1066 1067 /* Register clk muxes */ 1068 for (i = 0; i < ARRAY_SIZE(clk_mux); i++) { 1069 clk_mux[i].regmap = regmap; 1070 ret = devm_clk_hw_register(dev, &clk_mux[i].hw); 1071 if (ret) 1072 return dev_err_probe(dev, ret, "%s failed\n", 1073 clk_mux[i].hw.init->name); 1074 } 1075 1076 /* Register clk outputs */ 1077 for (i = 0; i < ARRAY_SIZE(clk_out); i++) { 1078 switch (i) { 1079 case VC3_DIFF2: 1080 name = "diff2"; 1081 break; 1082 case VC3_DIFF1: 1083 name = "diff1"; 1084 break; 1085 case VC3_SE3: 1086 name = "se3"; 1087 break; 1088 case VC3_SE2: 1089 name = "se2"; 1090 break; 1091 case VC3_SE1: 1092 name = "se1"; 1093 break; 1094 case VC3_REF: 1095 name = "ref"; 1096 break; 1097 default: 1098 return dev_err_probe(dev, -EINVAL, "invalid clk output %d\n", i); 1099 } 1100 1101 if (i == VC3_REF) 1102 clk_out[i] = devm_clk_hw_register_fixed_factor_index(dev, 1103 name, 0, CLK_SET_RATE_PARENT, 1, 1); 1104 else 1105 clk_out[i] = devm_clk_hw_register_fixed_factor_parent_hw(dev, 1106 name, &clk_mux[i - 1].hw, CLK_SET_RATE_PARENT, 1, 1); 1107 1108 if (IS_ERR(clk_out[i])) 1109 return PTR_ERR(clk_out[i]); 1110 } 1111 1112 ret = devm_of_clk_add_hw_provider(dev, vc3_of_clk_get, clk_out); 1113 if (ret) 1114 return dev_err_probe(dev, ret, "unable to add clk provider\n"); 1115 1116 return ret; 1117 } 1118 1119 static const struct of_device_id dev_ids[] = { 1120 { .compatible = "renesas,5p35023" }, 1121 { /* Sentinel */ } 1122 }; 1123 MODULE_DEVICE_TABLE(of, dev_ids); 1124 1125 static struct i2c_driver vc3_driver = { 1126 .driver = { 1127 .name = "vc3", 1128 .of_match_table = of_match_ptr(dev_ids), 1129 }, 1130 .probe = vc3_probe, 1131 }; 1132 module_i2c_driver(vc3_driver); 1133 1134 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>"); 1135 MODULE_DESCRIPTION("Renesas VersaClock 3 driver"); 1136 MODULE_LICENSE("GPL"); 1137