1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Renesas Versaclock 3
5 * Copyright (C) 2023 Renesas Electronics Corp.
8 #include <linux/clk-provider.h>
10 #include <linux/limits.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
14 #define NUM_CONFIG_REGISTERS 37
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)
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))
25 #define VC3_PLL3_N_DIVIDER 0x4
26 #define VC3_PLL3_LOOP_FILTER_N_DIV_MSB 0x5
28 #define VC3_PLL3_CHARGE_PUMP_CTRL 0x6
29 #define VC3_PLL3_CHARGE_PUMP_CTRL_OUTDIV3_SRC_SEL BIT(7)
31 #define VC3_PLL1_CTRL_OUTDIV5 0x7
32 #define VC3_PLL1_CTRL_OUTDIV5_PLL1_MDIV_DOUBLER BIT(7)
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))
39 #define VC3_PLL1_VCO_N_DIVIDER 0x9
40 #define VC3_PLL1_LOOP_FILTER_N_DIV_MSB 0x0a
42 #define VC3_OUT_DIV1_DIV2_CTRL 0xf
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
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))
55 #define VC3_OUT_DIV3_DIV4_CTRL 0x1b
57 #define VC3_PLL_OP_CTRL 0x1c
58 #define VC3_PLL_OP_CTRL_PLL2_REFIN_SEL 6
60 #define VC3_OUTPUT_CTR 0x1d
61 #define VC3_OUTPUT_CTR_DIV4_SRC_SEL BIT(3)
63 #define VC3_SE2_CTRL_REG0 0x1f
64 #define VC3_SE2_CTRL_REG0_SE2_CLK_SEL BIT(6)
66 #define VC3_SE3_DIFF1_CTRL_REG 0x21
67 #define VC3_SE3_DIFF1_CTRL_REG_SE3_CLK_SEL BIT(6)
69 #define VC3_DIFF1_CTRL_REG 0x22
70 #define VC3_DIFF1_CTRL_REG_DIFF1_CLK_SEL BIT(7)
72 #define VC3_DIFF2_CTRL_REG 0x23
73 #define VC3_DIFF2_CTRL_REG_DIFF2_CLK_SEL BIT(7)
75 #define VC3_SE1_DIV4_CTRL 0x24
76 #define VC3_SE1_DIV4_CTRL_SE1_CLK_SEL BIT(3)
78 #define VC3_PLL1_VCO_MIN 300000000UL
79 #define VC3_PLL1_VCO_MAX 600000000UL
81 #define VC3_PLL2_VCO_MIN 400000000UL
82 #define VC3_PLL2_VCO_MAX 1200000000UL
84 #define VC3_PLL3_VCO_MIN 300000000UL
85 #define VC3_PLL3_VCO_MAX 800000000UL
87 #define VC3_2_POW_16 (U16_MAX + 1)
88 #define VC3_DIV_MASK(width) ((1 << (width)) - 1)
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,
138 struct vc3_clk_data {
143 struct vc3_pfd_data {
150 struct vc3_pll_data {
154 unsigned long vco_min;
155 unsigned long vco_max;
158 struct vc3_div_data {
160 const struct clk_div_table *table;
168 struct regmap *regmap;
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, },
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, },
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, },
211 static struct clk_hw *clk_out[6];
213 static unsigned char vc3_pfd_mux_get_parent(struct clk_hw *hw)
215 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
216 const struct vc3_clk_data *pfd_mux = vc3->data;
219 regmap_read(vc3->regmap, pfd_mux->offs, &src);
221 return !!(src & pfd_mux->bitmsk);
224 static int vc3_pfd_mux_set_parent(struct clk_hw *hw, u8 index)
226 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
227 const struct vc3_clk_data *pfd_mux = vc3->data;
229 regmap_update_bits(vc3->regmap, pfd_mux->offs, pfd_mux->bitmsk,
230 index ? pfd_mux->bitmsk : 0);
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,
240 static unsigned long vc3_pfd_recalc_rate(struct clk_hw *hw,
241 unsigned long parent_rate)
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;
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)
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)
270 mdiv = VC3_PLL2_M_DIV(prediv);
272 /* The bypass_prediv is set, PLL fed from Ref_in directly. */
273 if (prediv & pfd->mdiv1_bitmsk)
276 mdiv = VC3_PLL3_M_DIV(prediv);
279 if (prediv & pfd->mdiv2_bitmsk)
280 rate = parent_rate / 2;
282 rate = parent_rate / mdiv;
287 static long vc3_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
288 unsigned long *parent_rate)
290 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
291 const struct vc3_pfd_data *pfd = vc3->data;
294 /* PLL cannot operate with input clock above 50 MHz. */
298 /* CLKIN within range of PLL input, feed directly to PLL. */
299 if (*parent_rate <= 50000000)
302 idiv = DIV_ROUND_UP(*parent_rate, rate);
303 if (pfd->num == VC3_PFD1 || pfd->num == VC3_PFD3) {
311 return *parent_rate / idiv;
314 static int vc3_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
315 unsigned long parent_rate)
317 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
318 const struct vc3_pfd_data *pfd = vc3->data;
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,
326 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv2_bitmsk, 0);
330 idiv = DIV_ROUND_UP(parent_rate, rate);
331 /* We have dedicated div-2 predivider. */
333 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv2_bitmsk,
335 regmap_update_bits(vc3->regmap, pfd->offs, pfd->mdiv1_bitmsk, 0);
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);
342 div = VC3_PLL3_M_DIV(idiv);
344 regmap_write(vc3->regmap, pfd->offs, div);
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,
356 static unsigned long vc3_pll_recalc_rate(struct clk_hw *hw,
357 unsigned long parent_rate)
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;
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);
369 if (pll->num == VC3_PLL2) {
370 regmap_read(vc3->regmap, VC3_PLL2_FB_FRC_DIV_MSB, &val);
372 regmap_read(vc3->regmap, VC3_PLL2_FB_FRC_DIV_LSB, &val);
374 rate = (parent_rate *
375 (div_int * VC3_2_POW_16 + div_frc) / VC3_2_POW_16);
377 rate = parent_rate * div_int;
383 static long vc3_pll_round_rate(struct clk_hw *hw, unsigned long rate,
384 unsigned long *parent_rate)
386 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
387 const struct vc3_pll_data *pll = vc3->data;
390 if (rate < pll->vco_min)
392 if (rate > pll->vco_max)
395 vc3->div_int = rate / *parent_rate;
397 if (pll->num == VC3_PLL2) {
398 if (vc3->div_int > 0x7ff)
399 rate = *parent_rate * 0x7ff;
401 /* Determine best fractional part, which is 16 bit wide */
402 div_frc = rate % *parent_rate;
403 div_frc *= BIT(16) - 1;
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);
409 rate = *parent_rate * vc3->div_int;
415 static int vc3_pll_set_rate(struct clk_hw *hw, unsigned long rate,
416 unsigned long parent_rate)
418 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
419 const struct vc3_pll_data *pll = vc3->data;
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);
427 if (pll->num == VC3_PLL2) {
428 regmap_write(vc3->regmap, VC3_PLL2_FB_FRC_DIV_MSB,
430 regmap_write(vc3->regmap, VC3_PLL2_FB_FRC_DIV_LSB,
431 vc3->div_frc & 0xff);
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,
443 static unsigned char vc3_div_mux_get_parent(struct clk_hw *hw)
445 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
446 const struct vc3_clk_data *div_mux = vc3->data;
449 regmap_read(vc3->regmap, div_mux->offs, &src);
451 return !!(src & div_mux->bitmsk);
454 static int vc3_div_mux_set_parent(struct clk_hw *hw, u8 index)
456 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
457 const struct vc3_clk_data *div_mux = vc3->data;
459 regmap_update_bits(vc3->regmap, div_mux->offs, div_mux->bitmsk,
460 index ? div_mux->bitmsk : 0);
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,
471 static unsigned int vc3_get_div(const struct clk_div_table *table,
472 unsigned int val, unsigned long flag)
474 const struct clk_div_table *clkt;
476 for (clkt = table; clkt->div; clkt++)
477 if (clkt->val == val)
483 static unsigned long vc3_div_recalc_rate(struct clk_hw *hw,
484 unsigned long parent_rate)
486 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
487 const struct vc3_div_data *div_data = vc3->data;
490 regmap_read(vc3->regmap, div_data->offs, &val);
491 val >>= div_data->shift;
492 val &= VC3_DIV_MASK(div_data->width);
494 return divider_recalc_rate(hw, parent_rate, val, div_data->table,
495 div_data->flags, div_data->width);
498 static long vc3_div_round_rate(struct clk_hw *hw, unsigned long rate,
499 unsigned long *parent_rate)
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;
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);
514 return divider_round_rate(hw, rate, parent_rate, div_data->table,
515 div_data->width, div_data->flags);
518 static int vc3_div_set_rate(struct clk_hw *hw, unsigned long rate,
519 unsigned long parent_rate)
521 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
522 const struct vc3_div_data *div_data = vc3->data;
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);
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,
539 static int vc3_clk_mux_determine_rate(struct clk_hw *hw,
540 struct clk_rate_request *req)
545 ret = clk_mux_determine_rate_flags(hw, req, CLK_SET_RATE_PARENT);
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,
552 return clk_mux_determine_rate_flags(hw, req,
553 CLK_SET_RATE_PARENT);
561 static unsigned char vc3_clk_mux_get_parent(struct clk_hw *hw)
563 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
564 const struct vc3_clk_data *clk_mux = vc3->data;
567 regmap_read(vc3->regmap, clk_mux->offs, &val);
569 return !!(val & clk_mux->bitmsk);
572 static int vc3_clk_mux_set_parent(struct clk_hw *hw, u8 index)
574 struct vc3_hw_data *vc3 = container_of(hw, struct vc3_hw_data, hw);
575 const struct vc3_clk_data *clk_mux = vc3->data;
577 regmap_update_bits(vc3->regmap, clk_mux->offs,
578 clk_mux->bitmsk, index ? clk_mux->bitmsk : 0);
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,
588 static bool vc3_regmap_is_writeable(struct device *dev, unsigned int reg)
593 static const struct regmap_config vc3_regmap_config = {
596 .cache_type = REGCACHE_RBTREE,
597 .max_register = 0x24,
598 .writeable_reg = vc3_regmap_is_writeable,
601 static struct vc3_hw_data clk_div[5];
603 static const struct clk_parent_data pfd_mux_parent_data[] = {
605 { .hw = &clk_div[VC3_DIV2].hw }
608 static struct vc3_hw_data clk_pfd_mux[] = {
610 .data = &(struct vc3_clk_data) {
611 .offs = VC3_PLL_OP_CTRL,
612 .bitmsk = BIT(VC3_PLL_OP_CTRL_PLL2_REFIN_SEL)
614 .hw.init = &(struct clk_init_data){
616 .ops = &vc3_pfd_mux_ops,
617 .parent_data = pfd_mux_parent_data,
619 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
623 .data = &(struct vc3_clk_data) {
624 .offs = VC3_GENERAL_CTR,
625 .bitmsk = BIT(VC3_GENERAL_CTR_PLL3_REFIN_SEL)
627 .hw.init = &(struct clk_init_data){
629 .ops = &vc3_pfd_mux_ops,
630 .parent_data = pfd_mux_parent_data,
632 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
637 static struct vc3_hw_data clk_pfd[] = {
639 .data = &(struct vc3_pfd_data) {
641 .offs = VC3_PLL1_M_DIVIDER,
642 .mdiv1_bitmsk = VC3_PLL1_M_DIV1,
643 .mdiv2_bitmsk = VC3_PLL1_M_DIV2
645 .hw.init = &(struct clk_init_data){
648 .parent_data = &(const struct clk_parent_data) {
652 .flags = CLK_SET_RATE_PARENT
656 .data = &(struct vc3_pfd_data) {
658 .offs = VC3_PLL2_M_DIVIDER,
659 .mdiv1_bitmsk = VC3_PLL2_M_DIV1,
660 .mdiv2_bitmsk = VC3_PLL2_M_DIV2
662 .hw.init = &(struct clk_init_data){
665 .parent_hws = (const struct clk_hw *[]) {
666 &clk_pfd_mux[VC3_PFD2_MUX].hw
669 .flags = CLK_SET_RATE_PARENT
673 .data = &(struct vc3_pfd_data) {
675 .offs = VC3_PLL3_M_DIVIDER,
676 .mdiv1_bitmsk = VC3_PLL3_M_DIV1,
677 .mdiv2_bitmsk = VC3_PLL3_M_DIV2
679 .hw.init = &(struct clk_init_data){
682 .parent_hws = (const struct clk_hw *[]) {
683 &clk_pfd_mux[VC3_PFD3_MUX].hw
686 .flags = CLK_SET_RATE_PARENT
691 static struct vc3_hw_data clk_pll[] = {
693 .data = &(struct vc3_pll_data) {
695 .int_div_msb_offs = VC3_PLL1_LOOP_FILTER_N_DIV_MSB,
696 .int_div_lsb_offs = VC3_PLL1_VCO_N_DIVIDER,
697 .vco_min = VC3_PLL1_VCO_MIN,
698 .vco_max = VC3_PLL1_VCO_MAX
700 .hw.init = &(struct clk_init_data){
703 .parent_hws = (const struct clk_hw *[]) {
704 &clk_pfd[VC3_PFD1].hw
707 .flags = CLK_SET_RATE_PARENT
711 .data = &(struct vc3_pll_data) {
713 .int_div_msb_offs = VC3_PLL2_FB_INT_DIV_MSB,
714 .int_div_lsb_offs = VC3_PLL2_FB_INT_DIV_LSB,
715 .vco_min = VC3_PLL2_VCO_MIN,
716 .vco_max = VC3_PLL2_VCO_MAX
718 .hw.init = &(struct clk_init_data){
721 .parent_hws = (const struct clk_hw *[]) {
722 &clk_pfd[VC3_PFD2].hw
725 .flags = CLK_SET_RATE_PARENT
729 .data = &(struct vc3_pll_data) {
731 .int_div_msb_offs = VC3_PLL3_LOOP_FILTER_N_DIV_MSB,
732 .int_div_lsb_offs = VC3_PLL3_N_DIVIDER,
733 .vco_min = VC3_PLL3_VCO_MIN,
734 .vco_max = VC3_PLL3_VCO_MAX
736 .hw.init = &(struct clk_init_data){
739 .parent_hws = (const struct clk_hw *[]) {
740 &clk_pfd[VC3_PFD3].hw
743 .flags = CLK_SET_RATE_PARENT
748 static const struct clk_parent_data div_mux_parent_data[][2] = {
750 { .hw = &clk_pll[VC3_PLL1].hw },
754 { .hw = &clk_pll[VC3_PLL2].hw },
755 { .hw = &clk_pll[VC3_PLL3].hw }
758 { .hw = &clk_pll[VC3_PLL2].hw },
763 static struct vc3_hw_data clk_div_mux[] = {
765 .data = &(struct vc3_clk_data) {
766 .offs = VC3_GENERAL_CTR,
767 .bitmsk = VC3_GENERAL_CTR_DIV1_SRC_SEL
769 .hw.init = &(struct clk_init_data){
771 .ops = &vc3_div_mux_ops,
772 .parent_data = div_mux_parent_data[VC3_DIV1_MUX],
774 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
778 .data = &(struct vc3_clk_data) {
779 .offs = VC3_PLL3_CHARGE_PUMP_CTRL,
780 .bitmsk = VC3_PLL3_CHARGE_PUMP_CTRL_OUTDIV3_SRC_SEL
782 .hw.init = &(struct clk_init_data){
784 .ops = &vc3_div_mux_ops,
785 .parent_data = div_mux_parent_data[VC3_DIV3_MUX],
787 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
791 .data = &(struct vc3_clk_data) {
792 .offs = VC3_OUTPUT_CTR,
793 .bitmsk = VC3_OUTPUT_CTR_DIV4_SRC_SEL
795 .hw.init = &(struct clk_init_data){
797 .ops = &vc3_div_mux_ops,
798 .parent_data = div_mux_parent_data[VC3_DIV4_MUX],
800 .flags = CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT
805 static struct vc3_hw_data clk_div[] = {
807 .data = &(struct vc3_div_data) {
808 .offs = VC3_OUT_DIV1_DIV2_CTRL,
812 .flags = CLK_DIVIDER_READ_ONLY
814 .hw.init = &(struct clk_init_data){
817 .parent_hws = (const struct clk_hw *[]) {
818 &clk_div_mux[VC3_DIV1_MUX].hw
821 .flags = CLK_SET_RATE_PARENT
825 .data = &(struct vc3_div_data) {
826 .offs = VC3_OUT_DIV1_DIV2_CTRL,
827 .table = div245_divs,
830 .flags = CLK_DIVIDER_READ_ONLY
832 .hw.init = &(struct clk_init_data){
835 .parent_hws = (const struct clk_hw *[]) {
836 &clk_pll[VC3_PLL1].hw
839 .flags = CLK_SET_RATE_PARENT
843 .data = &(struct vc3_div_data) {
844 .offs = VC3_OUT_DIV3_DIV4_CTRL,
848 .flags = CLK_DIVIDER_READ_ONLY
850 .hw.init = &(struct clk_init_data){
853 .parent_hws = (const struct clk_hw *[]) {
854 &clk_div_mux[VC3_DIV3_MUX].hw
857 .flags = CLK_SET_RATE_PARENT
861 .data = &(struct vc3_div_data) {
862 .offs = VC3_OUT_DIV3_DIV4_CTRL,
863 .table = div245_divs,
866 .flags = CLK_DIVIDER_READ_ONLY
868 .hw.init = &(struct clk_init_data){
871 .parent_hws = (const struct clk_hw *[]) {
872 &clk_div_mux[VC3_DIV4_MUX].hw
875 .flags = CLK_SET_RATE_PARENT
879 .data = &(struct vc3_div_data) {
880 .offs = VC3_PLL1_CTRL_OUTDIV5,
881 .table = div245_divs,
884 .flags = CLK_DIVIDER_READ_ONLY
886 .hw.init = &(struct clk_init_data){
889 .parent_hws = (const struct clk_hw *[]) {
890 &clk_pll[VC3_PLL3].hw
893 .flags = CLK_SET_RATE_PARENT
898 static struct vc3_hw_data clk_mux[] = {
900 .data = &(struct vc3_clk_data) {
901 .offs = VC3_SE1_DIV4_CTRL,
902 .bitmsk = VC3_SE1_DIV4_CTRL_SE1_CLK_SEL
904 .hw.init = &(struct clk_init_data){
906 .ops = &vc3_clk_mux_ops,
907 .parent_hws = (const struct clk_hw *[]) {
908 &clk_div[VC3_DIV5].hw,
909 &clk_div[VC3_DIV4].hw
912 .flags = CLK_SET_RATE_PARENT
916 .data = &(struct vc3_clk_data) {
917 .offs = VC3_SE2_CTRL_REG0,
918 .bitmsk = VC3_SE2_CTRL_REG0_SE2_CLK_SEL
920 .hw.init = &(struct clk_init_data){
922 .ops = &vc3_clk_mux_ops,
923 .parent_hws = (const struct clk_hw *[]) {
924 &clk_div[VC3_DIV5].hw,
925 &clk_div[VC3_DIV4].hw
928 .flags = CLK_SET_RATE_PARENT
932 .data = &(struct vc3_clk_data) {
933 .offs = VC3_SE3_DIFF1_CTRL_REG,
934 .bitmsk = VC3_SE3_DIFF1_CTRL_REG_SE3_CLK_SEL
936 .hw.init = &(struct clk_init_data){
938 .ops = &vc3_clk_mux_ops,
939 .parent_hws = (const struct clk_hw *[]) {
940 &clk_div[VC3_DIV2].hw,
941 &clk_div[VC3_DIV4].hw
944 .flags = CLK_SET_RATE_PARENT
948 .data = &(struct vc3_clk_data) {
949 .offs = VC3_DIFF1_CTRL_REG,
950 .bitmsk = VC3_DIFF1_CTRL_REG_DIFF1_CLK_SEL
952 .hw.init = &(struct clk_init_data){
954 .ops = &vc3_clk_mux_ops,
955 .parent_hws = (const struct clk_hw *[]) {
956 &clk_div[VC3_DIV1].hw,
957 &clk_div[VC3_DIV3].hw
960 .flags = CLK_SET_RATE_PARENT
964 .data = &(struct vc3_clk_data) {
965 .offs = VC3_DIFF2_CTRL_REG,
966 .bitmsk = VC3_DIFF2_CTRL_REG_DIFF2_CLK_SEL
968 .hw.init = &(struct clk_init_data){
970 .ops = &vc3_clk_mux_ops,
971 .parent_hws = (const struct clk_hw *[]) {
972 &clk_div[VC3_DIV1].hw,
973 &clk_div[VC3_DIV3].hw
976 .flags = CLK_SET_RATE_PARENT
981 static struct clk_hw *vc3_of_clk_get(struct of_phandle_args *clkspec,
984 unsigned int idx = clkspec->args[0];
985 struct clk_hw **clkout_hw = data;
987 if (idx >= ARRAY_SIZE(clk_out)) {
988 pr_err("invalid clk index %u for provider %pOF\n", idx, clkspec->np);
989 return ERR_PTR(-EINVAL);
992 return clkout_hw[idx];
995 static int vc3_probe(struct i2c_client *client)
997 struct device *dev = &client->dev;
998 u8 settings[NUM_CONFIG_REGISTERS];
999 struct regmap *regmap;
1003 regmap = devm_regmap_init_i2c(client, &vc3_regmap_config);
1005 return dev_err_probe(dev, PTR_ERR(regmap),
1006 "failed to allocate register map\n");
1008 ret = of_property_read_u8_array(dev->of_node, "renesas,settings",
1009 settings, ARRAY_SIZE(settings));
1012 * A raw settings array was specified in the DT. Write the
1013 * settings to the device immediately.
1015 for (i = 0; i < NUM_CONFIG_REGISTERS; i++) {
1016 ret = regmap_write(regmap, i, settings[i]);
1018 dev_err(dev, "error writing to chip (%i)\n", ret);
1022 } else if (ret == -EOVERFLOW) {
1023 dev_err(&client->dev, "EOVERFLOW reg settings. ARRAY_SIZE: %zu\n",
1024 ARRAY_SIZE(settings));
1028 /* Register pfd muxes */
1029 for (i = 0; i < ARRAY_SIZE(clk_pfd_mux); i++) {
1030 clk_pfd_mux[i].regmap = regmap;
1031 ret = devm_clk_hw_register(dev, &clk_pfd_mux[i].hw);
1033 return dev_err_probe(dev, ret, "%s failed\n",
1034 clk_pfd_mux[i].hw.init->name);
1037 /* Register pfd's */
1038 for (i = 0; i < ARRAY_SIZE(clk_pfd); i++) {
1039 clk_pfd[i].regmap = regmap;
1040 ret = devm_clk_hw_register(dev, &clk_pfd[i].hw);
1042 return dev_err_probe(dev, ret, "%s failed\n",
1043 clk_pfd[i].hw.init->name);
1046 /* Register pll's */
1047 for (i = 0; i < ARRAY_SIZE(clk_pll); i++) {
1048 clk_pll[i].regmap = regmap;
1049 ret = devm_clk_hw_register(dev, &clk_pll[i].hw);
1051 return dev_err_probe(dev, ret, "%s failed\n",
1052 clk_pll[i].hw.init->name);
1055 /* Register divider muxes */
1056 for (i = 0; i < ARRAY_SIZE(clk_div_mux); i++) {
1057 clk_div_mux[i].regmap = regmap;
1058 ret = devm_clk_hw_register(dev, &clk_div_mux[i].hw);
1060 return dev_err_probe(dev, ret, "%s failed\n",
1061 clk_div_mux[i].hw.init->name);
1064 /* Register dividers */
1065 for (i = 0; i < ARRAY_SIZE(clk_div); i++) {
1066 clk_div[i].regmap = regmap;
1067 ret = devm_clk_hw_register(dev, &clk_div[i].hw);
1069 return dev_err_probe(dev, ret, "%s failed\n",
1070 clk_div[i].hw.init->name);
1073 /* Register clk muxes */
1074 for (i = 0; i < ARRAY_SIZE(clk_mux); i++) {
1075 clk_mux[i].regmap = regmap;
1076 ret = devm_clk_hw_register(dev, &clk_mux[i].hw);
1078 return dev_err_probe(dev, ret, "%s failed\n",
1079 clk_mux[i].hw.init->name);
1082 /* Register clk outputs */
1083 for (i = 0; i < ARRAY_SIZE(clk_out); i++) {
1104 return dev_err_probe(dev, -EINVAL, "invalid clk output %d\n", i);
1108 clk_out[i] = devm_clk_hw_register_fixed_factor_index(dev,
1109 name, 0, CLK_SET_RATE_PARENT, 1, 1);
1111 clk_out[i] = devm_clk_hw_register_fixed_factor_parent_hw(dev,
1112 name, &clk_mux[i - 1].hw, CLK_SET_RATE_PARENT, 1, 1);
1114 if (IS_ERR(clk_out[i]))
1115 return PTR_ERR(clk_out[i]);
1118 ret = devm_of_clk_add_hw_provider(dev, vc3_of_clk_get, clk_out);
1120 return dev_err_probe(dev, ret, "unable to add clk provider\n");
1125 static const struct of_device_id dev_ids[] = {
1126 { .compatible = "renesas,5p35023" },
1129 MODULE_DEVICE_TABLE(of, dev_ids);
1131 static struct i2c_driver vc3_driver = {
1134 .of_match_table = of_match_ptr(dev_ids),
1138 module_i2c_driver(vc3_driver);
1140 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
1141 MODULE_DESCRIPTION("Renesas VersaClock 3 driver");
1142 MODULE_LICENSE("GPL");