1 // SPDX-License-Identifier: GPL-2.0
3 * Wrapper driver for SERDES used in J721E
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <dt-bindings/phy/phy.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/mux/consumer.h>
17 #include <linux/of_address.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/reset-controller.h>
24 #define WIZ_SERDES_CTRL 0x404
25 #define WIZ_SERDES_TOP_CTRL 0x408
26 #define WIZ_SERDES_RST 0x40c
27 #define WIZ_SERDES_TYPEC 0x410
28 #define WIZ_LANECTL(n) (0x480 + (0x40 * (n)))
30 #define WIZ_MAX_LANES 4
31 #define WIZ_MUX_NUM_CLOCKS 3
32 #define WIZ_DIV_NUM_CLOCKS_16G 2
33 #define WIZ_DIV_NUM_CLOCKS_10G 1
35 #define WIZ_SERDES_TYPEC_LN10_SWAP BIT(30)
37 enum wiz_lane_standard_mode
{
44 enum wiz_refclk_mux_sel
{
50 enum wiz_refclk_div_sel
{
55 static const struct reg_field por_en
= REG_FIELD(WIZ_SERDES_CTRL
, 31, 31);
56 static const struct reg_field phy_reset_n
= REG_FIELD(WIZ_SERDES_RST
, 31, 31);
57 static const struct reg_field pll1_refclk_mux_sel
=
58 REG_FIELD(WIZ_SERDES_RST
, 29, 29);
59 static const struct reg_field pll0_refclk_mux_sel
=
60 REG_FIELD(WIZ_SERDES_RST
, 28, 28);
61 static const struct reg_field refclk_dig_sel_16g
=
62 REG_FIELD(WIZ_SERDES_RST
, 24, 25);
63 static const struct reg_field refclk_dig_sel_10g
=
64 REG_FIELD(WIZ_SERDES_RST
, 24, 24);
65 static const struct reg_field pma_cmn_refclk_int_mode
=
66 REG_FIELD(WIZ_SERDES_TOP_CTRL
, 28, 29);
67 static const struct reg_field pma_cmn_refclk_mode
=
68 REG_FIELD(WIZ_SERDES_TOP_CTRL
, 30, 31);
69 static const struct reg_field pma_cmn_refclk_dig_div
=
70 REG_FIELD(WIZ_SERDES_TOP_CTRL
, 26, 27);
71 static const struct reg_field pma_cmn_refclk1_dig_div
=
72 REG_FIELD(WIZ_SERDES_TOP_CTRL
, 24, 25);
74 static const struct reg_field p_enable
[WIZ_MAX_LANES
] = {
75 REG_FIELD(WIZ_LANECTL(0), 30, 31),
76 REG_FIELD(WIZ_LANECTL(1), 30, 31),
77 REG_FIELD(WIZ_LANECTL(2), 30, 31),
78 REG_FIELD(WIZ_LANECTL(3), 30, 31),
81 enum p_enable
{ P_ENABLE
= 2, P_ENABLE_FORCE
= 1, P_ENABLE_DISABLE
= 0 };
83 static const struct reg_field p_align
[WIZ_MAX_LANES
] = {
84 REG_FIELD(WIZ_LANECTL(0), 29, 29),
85 REG_FIELD(WIZ_LANECTL(1), 29, 29),
86 REG_FIELD(WIZ_LANECTL(2), 29, 29),
87 REG_FIELD(WIZ_LANECTL(3), 29, 29),
90 static const struct reg_field p_raw_auto_start
[WIZ_MAX_LANES
] = {
91 REG_FIELD(WIZ_LANECTL(0), 28, 28),
92 REG_FIELD(WIZ_LANECTL(1), 28, 28),
93 REG_FIELD(WIZ_LANECTL(2), 28, 28),
94 REG_FIELD(WIZ_LANECTL(3), 28, 28),
97 static const struct reg_field p_standard_mode
[WIZ_MAX_LANES
] = {
98 REG_FIELD(WIZ_LANECTL(0), 24, 25),
99 REG_FIELD(WIZ_LANECTL(1), 24, 25),
100 REG_FIELD(WIZ_LANECTL(2), 24, 25),
101 REG_FIELD(WIZ_LANECTL(3), 24, 25),
104 static const struct reg_field typec_ln10_swap
=
105 REG_FIELD(WIZ_SERDES_TYPEC
, 30, 30);
109 struct regmap_field
*field
;
111 struct clk_init_data clk_data
;
114 #define to_wiz_clk_mux(_hw) container_of(_hw, struct wiz_clk_mux, hw)
116 struct wiz_clk_divider
{
118 struct regmap_field
*field
;
119 const struct clk_div_table
*table
;
120 struct clk_init_data clk_data
;
123 #define to_wiz_clk_div(_hw) container_of(_hw, struct wiz_clk_divider, hw)
125 struct wiz_clk_mux_sel
{
126 struct regmap_field
*field
;
128 const char *node_name
;
131 struct wiz_clk_div_sel
{
132 struct regmap_field
*field
;
133 const struct clk_div_table
*table
;
134 const char *node_name
;
137 static struct wiz_clk_mux_sel clk_mux_sel_16g
[] = {
140 * Mux value to be configured for each of the input clocks
141 * in the order populated in device tree
144 .node_name
= "pll0-refclk",
148 .node_name
= "pll1-refclk",
151 .table
= { 1, 3, 0, 2 },
152 .node_name
= "refclk-dig",
156 static struct wiz_clk_mux_sel clk_mux_sel_10g
[] = {
159 * Mux value to be configured for each of the input clocks
160 * in the order populated in device tree
163 .node_name
= "pll0-refclk",
167 .node_name
= "pll1-refclk",
171 .node_name
= "refclk-dig",
175 static const struct clk_div_table clk_div_table
[] = {
176 { .val
= 0, .div
= 1, },
177 { .val
= 1, .div
= 2, },
178 { .val
= 2, .div
= 4, },
179 { .val
= 3, .div
= 8, },
182 static struct wiz_clk_div_sel clk_div_sel
[] = {
184 .table
= clk_div_table
,
185 .node_name
= "cmn-refclk-dig-div",
188 .table
= clk_div_table
,
189 .node_name
= "cmn-refclk1-dig-div",
198 #define WIZ_TYPEC_DIR_DEBOUNCE_MIN 100 /* ms */
199 #define WIZ_TYPEC_DIR_DEBOUNCE_MAX 1000
202 struct regmap
*regmap
;
204 struct wiz_clk_mux_sel
*clk_mux_sel
;
205 struct wiz_clk_div_sel
*clk_div_sel
;
206 unsigned int clk_div_sel_num
;
207 struct regmap_field
*por_en
;
208 struct regmap_field
*phy_reset_n
;
209 struct regmap_field
*p_enable
[WIZ_MAX_LANES
];
210 struct regmap_field
*p_align
[WIZ_MAX_LANES
];
211 struct regmap_field
*p_raw_auto_start
[WIZ_MAX_LANES
];
212 struct regmap_field
*p_standard_mode
[WIZ_MAX_LANES
];
213 struct regmap_field
*pma_cmn_refclk_int_mode
;
214 struct regmap_field
*pma_cmn_refclk_mode
;
215 struct regmap_field
*pma_cmn_refclk_dig_div
;
216 struct regmap_field
*pma_cmn_refclk1_dig_div
;
217 struct regmap_field
*typec_ln10_swap
;
221 struct platform_device
*serdes_pdev
;
222 struct reset_controller_dev wiz_phy_reset_dev
;
223 struct gpio_desc
*gpio_typec_dir
;
225 u32 lane_phy_type
[WIZ_MAX_LANES
];
228 static int wiz_reset(struct wiz
*wiz
)
232 ret
= regmap_field_write(wiz
->por_en
, 0x1);
238 ret
= regmap_field_write(wiz
->por_en
, 0x0);
245 static int wiz_mode_select(struct wiz
*wiz
)
247 u32 num_lanes
= wiz
->num_lanes
;
248 enum wiz_lane_standard_mode mode
;
252 for (i
= 0; i
< num_lanes
; i
++) {
253 if (wiz
->lane_phy_type
[i
] == PHY_TYPE_DP
)
254 mode
= LANE_MODE_GEN1
;
256 mode
= LANE_MODE_GEN4
;
258 ret
= regmap_field_write(wiz
->p_standard_mode
[i
], mode
);
266 static int wiz_init_raw_interface(struct wiz
*wiz
, bool enable
)
268 u32 num_lanes
= wiz
->num_lanes
;
272 for (i
= 0; i
< num_lanes
; i
++) {
273 ret
= regmap_field_write(wiz
->p_align
[i
], enable
);
277 ret
= regmap_field_write(wiz
->p_raw_auto_start
[i
], enable
);
285 static int wiz_init(struct wiz
*wiz
)
287 struct device
*dev
= wiz
->dev
;
290 ret
= wiz_reset(wiz
);
292 dev_err(dev
, "WIZ reset failed\n");
296 ret
= wiz_mode_select(wiz
);
298 dev_err(dev
, "WIZ mode select failed\n");
302 ret
= wiz_init_raw_interface(wiz
, true);
304 dev_err(dev
, "WIZ interface initialization failed\n");
311 static int wiz_regfield_init(struct wiz
*wiz
)
313 struct wiz_clk_mux_sel
*clk_mux_sel
;
314 struct wiz_clk_div_sel
*clk_div_sel
;
315 struct regmap
*regmap
= wiz
->regmap
;
316 int num_lanes
= wiz
->num_lanes
;
317 struct device
*dev
= wiz
->dev
;
320 wiz
->por_en
= devm_regmap_field_alloc(dev
, regmap
, por_en
);
321 if (IS_ERR(wiz
->por_en
)) {
322 dev_err(dev
, "POR_EN reg field init failed\n");
323 return PTR_ERR(wiz
->por_en
);
326 wiz
->phy_reset_n
= devm_regmap_field_alloc(dev
, regmap
,
328 if (IS_ERR(wiz
->phy_reset_n
)) {
329 dev_err(dev
, "PHY_RESET_N reg field init failed\n");
330 return PTR_ERR(wiz
->phy_reset_n
);
333 wiz
->pma_cmn_refclk_int_mode
=
334 devm_regmap_field_alloc(dev
, regmap
, pma_cmn_refclk_int_mode
);
335 if (IS_ERR(wiz
->pma_cmn_refclk_int_mode
)) {
336 dev_err(dev
, "PMA_CMN_REFCLK_INT_MODE reg field init failed\n");
337 return PTR_ERR(wiz
->pma_cmn_refclk_int_mode
);
340 wiz
->pma_cmn_refclk_mode
=
341 devm_regmap_field_alloc(dev
, regmap
, pma_cmn_refclk_mode
);
342 if (IS_ERR(wiz
->pma_cmn_refclk_mode
)) {
343 dev_err(dev
, "PMA_CMN_REFCLK_MODE reg field init failed\n");
344 return PTR_ERR(wiz
->pma_cmn_refclk_mode
);
347 clk_div_sel
= &wiz
->clk_div_sel
[CMN_REFCLK_DIG_DIV
];
348 clk_div_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
349 pma_cmn_refclk_dig_div
);
350 if (IS_ERR(clk_div_sel
->field
)) {
351 dev_err(dev
, "PMA_CMN_REFCLK_DIG_DIV reg field init failed\n");
352 return PTR_ERR(clk_div_sel
->field
);
355 if (wiz
->type
== J721E_WIZ_16G
) {
356 clk_div_sel
= &wiz
->clk_div_sel
[CMN_REFCLK1_DIG_DIV
];
358 devm_regmap_field_alloc(dev
, regmap
,
359 pma_cmn_refclk1_dig_div
);
360 if (IS_ERR(clk_div_sel
->field
)) {
361 dev_err(dev
, "PMA_CMN_REFCLK1_DIG_DIV reg field init failed\n");
362 return PTR_ERR(clk_div_sel
->field
);
366 clk_mux_sel
= &wiz
->clk_mux_sel
[PLL0_REFCLK
];
367 clk_mux_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
368 pll0_refclk_mux_sel
);
369 if (IS_ERR(clk_mux_sel
->field
)) {
370 dev_err(dev
, "PLL0_REFCLK_SEL reg field init failed\n");
371 return PTR_ERR(clk_mux_sel
->field
);
374 clk_mux_sel
= &wiz
->clk_mux_sel
[PLL1_REFCLK
];
375 clk_mux_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
376 pll1_refclk_mux_sel
);
377 if (IS_ERR(clk_mux_sel
->field
)) {
378 dev_err(dev
, "PLL1_REFCLK_SEL reg field init failed\n");
379 return PTR_ERR(clk_mux_sel
->field
);
382 clk_mux_sel
= &wiz
->clk_mux_sel
[REFCLK_DIG
];
383 if (wiz
->type
== J721E_WIZ_10G
)
385 devm_regmap_field_alloc(dev
, regmap
,
389 devm_regmap_field_alloc(dev
, regmap
,
392 if (IS_ERR(clk_mux_sel
->field
)) {
393 dev_err(dev
, "REFCLK_DIG_SEL reg field init failed\n");
394 return PTR_ERR(clk_mux_sel
->field
);
397 for (i
= 0; i
< num_lanes
; i
++) {
398 wiz
->p_enable
[i
] = devm_regmap_field_alloc(dev
, regmap
,
400 if (IS_ERR(wiz
->p_enable
[i
])) {
401 dev_err(dev
, "P%d_ENABLE reg field init failed\n", i
);
402 return PTR_ERR(wiz
->p_enable
[i
]);
405 wiz
->p_align
[i
] = devm_regmap_field_alloc(dev
, regmap
,
407 if (IS_ERR(wiz
->p_align
[i
])) {
408 dev_err(dev
, "P%d_ALIGN reg field init failed\n", i
);
409 return PTR_ERR(wiz
->p_align
[i
]);
412 wiz
->p_raw_auto_start
[i
] =
413 devm_regmap_field_alloc(dev
, regmap
, p_raw_auto_start
[i
]);
414 if (IS_ERR(wiz
->p_raw_auto_start
[i
])) {
415 dev_err(dev
, "P%d_RAW_AUTO_START reg field init fail\n",
417 return PTR_ERR(wiz
->p_raw_auto_start
[i
]);
420 wiz
->p_standard_mode
[i
] =
421 devm_regmap_field_alloc(dev
, regmap
, p_standard_mode
[i
]);
422 if (IS_ERR(wiz
->p_standard_mode
[i
])) {
423 dev_err(dev
, "P%d_STANDARD_MODE reg field init fail\n",
425 return PTR_ERR(wiz
->p_standard_mode
[i
]);
429 wiz
->typec_ln10_swap
= devm_regmap_field_alloc(dev
, regmap
,
431 if (IS_ERR(wiz
->typec_ln10_swap
)) {
432 dev_err(dev
, "LN10_SWAP reg field init failed\n");
433 return PTR_ERR(wiz
->typec_ln10_swap
);
439 static u8
wiz_clk_mux_get_parent(struct clk_hw
*hw
)
441 struct wiz_clk_mux
*mux
= to_wiz_clk_mux(hw
);
442 struct regmap_field
*field
= mux
->field
;
445 regmap_field_read(field
, &val
);
446 return clk_mux_val_to_index(hw
, mux
->table
, 0, val
);
449 static int wiz_clk_mux_set_parent(struct clk_hw
*hw
, u8 index
)
451 struct wiz_clk_mux
*mux
= to_wiz_clk_mux(hw
);
452 struct regmap_field
*field
= mux
->field
;
455 val
= mux
->table
[index
];
456 return regmap_field_write(field
, val
);
459 static const struct clk_ops wiz_clk_mux_ops
= {
460 .set_parent
= wiz_clk_mux_set_parent
,
461 .get_parent
= wiz_clk_mux_get_parent
,
464 static int wiz_mux_clk_register(struct wiz
*wiz
, struct device_node
*node
,
465 struct regmap_field
*field
, u32
*table
)
467 struct device
*dev
= wiz
->dev
;
468 struct clk_init_data
*init
;
469 const char **parent_names
;
470 unsigned int num_parents
;
471 struct wiz_clk_mux
*mux
;
476 mux
= devm_kzalloc(dev
, sizeof(*mux
), GFP_KERNEL
);
480 num_parents
= of_clk_get_parent_count(node
);
481 if (num_parents
< 2) {
482 dev_err(dev
, "SERDES clock must have parents\n");
486 parent_names
= devm_kzalloc(dev
, (sizeof(char *) * num_parents
),
491 of_clk_parent_fill(node
, parent_names
, num_parents
);
493 snprintf(clk_name
, sizeof(clk_name
), "%s_%s", dev_name(dev
),
496 init
= &mux
->clk_data
;
498 init
->ops
= &wiz_clk_mux_ops
;
499 init
->flags
= CLK_SET_RATE_NO_REPARENT
;
500 init
->parent_names
= parent_names
;
501 init
->num_parents
= num_parents
;
502 init
->name
= clk_name
;
508 clk
= devm_clk_register(dev
, &mux
->hw
);
512 ret
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
514 dev_err(dev
, "Failed to add clock provider: %s\n", clk_name
);
519 static unsigned long wiz_clk_div_recalc_rate(struct clk_hw
*hw
,
520 unsigned long parent_rate
)
522 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
523 struct regmap_field
*field
= div
->field
;
526 regmap_field_read(field
, &val
);
528 return divider_recalc_rate(hw
, parent_rate
, val
, div
->table
, 0x0, 2);
531 static long wiz_clk_div_round_rate(struct clk_hw
*hw
, unsigned long rate
,
532 unsigned long *prate
)
534 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
536 return divider_round_rate(hw
, rate
, prate
, div
->table
, 2, 0x0);
539 static int wiz_clk_div_set_rate(struct clk_hw
*hw
, unsigned long rate
,
540 unsigned long parent_rate
)
542 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
543 struct regmap_field
*field
= div
->field
;
546 val
= divider_get_val(rate
, parent_rate
, div
->table
, 2, 0x0);
550 return regmap_field_write(field
, val
);
553 static const struct clk_ops wiz_clk_div_ops
= {
554 .recalc_rate
= wiz_clk_div_recalc_rate
,
555 .round_rate
= wiz_clk_div_round_rate
,
556 .set_rate
= wiz_clk_div_set_rate
,
559 static int wiz_div_clk_register(struct wiz
*wiz
, struct device_node
*node
,
560 struct regmap_field
*field
,
561 const struct clk_div_table
*table
)
563 struct device
*dev
= wiz
->dev
;
564 struct wiz_clk_divider
*div
;
565 struct clk_init_data
*init
;
566 const char **parent_names
;
571 div
= devm_kzalloc(dev
, sizeof(*div
), GFP_KERNEL
);
575 snprintf(clk_name
, sizeof(clk_name
), "%s_%s", dev_name(dev
),
578 parent_names
= devm_kzalloc(dev
, sizeof(char *), GFP_KERNEL
);
582 of_clk_parent_fill(node
, parent_names
, 1);
584 init
= &div
->clk_data
;
586 init
->ops
= &wiz_clk_div_ops
;
588 init
->parent_names
= parent_names
;
589 init
->num_parents
= 1;
590 init
->name
= clk_name
;
596 clk
= devm_clk_register(dev
, &div
->hw
);
600 ret
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
602 dev_err(dev
, "Failed to add clock provider: %s\n", clk_name
);
607 static void wiz_clock_cleanup(struct wiz
*wiz
, struct device_node
*node
)
609 struct wiz_clk_mux_sel
*clk_mux_sel
= wiz
->clk_mux_sel
;
610 struct device_node
*clk_node
;
613 for (i
= 0; i
< WIZ_MUX_NUM_CLOCKS
; i
++) {
614 clk_node
= of_get_child_by_name(node
, clk_mux_sel
[i
].node_name
);
615 of_clk_del_provider(clk_node
);
616 of_node_put(clk_node
);
620 static int wiz_clock_init(struct wiz
*wiz
, struct device_node
*node
)
622 struct wiz_clk_mux_sel
*clk_mux_sel
= wiz
->clk_mux_sel
;
623 struct device
*dev
= wiz
->dev
;
624 struct device_node
*clk_node
;
625 const char *node_name
;
631 clk
= devm_clk_get(dev
, "core_ref_clk");
633 dev_err(dev
, "core_ref_clk clock not found\n");
638 rate
= clk_get_rate(clk
);
639 if (rate
>= 100000000)
640 regmap_field_write(wiz
->pma_cmn_refclk_int_mode
, 0x1);
642 regmap_field_write(wiz
->pma_cmn_refclk_int_mode
, 0x3);
644 clk
= devm_clk_get(dev
, "ext_ref_clk");
646 dev_err(dev
, "ext_ref_clk clock not found\n");
651 rate
= clk_get_rate(clk
);
652 if (rate
>= 100000000)
653 regmap_field_write(wiz
->pma_cmn_refclk_mode
, 0x0);
655 regmap_field_write(wiz
->pma_cmn_refclk_mode
, 0x2);
657 for (i
= 0; i
< WIZ_MUX_NUM_CLOCKS
; i
++) {
658 node_name
= clk_mux_sel
[i
].node_name
;
659 clk_node
= of_get_child_by_name(node
, node_name
);
661 dev_err(dev
, "Unable to get %s node\n", node_name
);
666 ret
= wiz_mux_clk_register(wiz
, clk_node
, clk_mux_sel
[i
].field
,
667 clk_mux_sel
[i
].table
);
669 dev_err(dev
, "Failed to register %s clock\n",
671 of_node_put(clk_node
);
675 of_node_put(clk_node
);
678 for (i
= 0; i
< wiz
->clk_div_sel_num
; i
++) {
679 node_name
= clk_div_sel
[i
].node_name
;
680 clk_node
= of_get_child_by_name(node
, node_name
);
682 dev_err(dev
, "Unable to get %s node\n", node_name
);
687 ret
= wiz_div_clk_register(wiz
, clk_node
, clk_div_sel
[i
].field
,
688 clk_div_sel
[i
].table
);
690 dev_err(dev
, "Failed to register %s clock\n",
692 of_node_put(clk_node
);
696 of_node_put(clk_node
);
701 wiz_clock_cleanup(wiz
, node
);
706 static int wiz_phy_reset_assert(struct reset_controller_dev
*rcdev
,
709 struct device
*dev
= rcdev
->dev
;
710 struct wiz
*wiz
= dev_get_drvdata(dev
);
714 ret
= regmap_field_write(wiz
->phy_reset_n
, false);
718 ret
= regmap_field_write(wiz
->p_enable
[id
- 1], P_ENABLE_DISABLE
);
722 static int wiz_phy_reset_deassert(struct reset_controller_dev
*rcdev
,
725 struct device
*dev
= rcdev
->dev
;
726 struct wiz
*wiz
= dev_get_drvdata(dev
);
729 /* if typec-dir gpio was specified, set LN10 SWAP bit based on that */
730 if (id
== 0 && wiz
->gpio_typec_dir
) {
731 if (wiz
->typec_dir_delay
)
732 msleep_interruptible(wiz
->typec_dir_delay
);
734 if (gpiod_get_value_cansleep(wiz
->gpio_typec_dir
))
735 regmap_field_write(wiz
->typec_ln10_swap
, 1);
737 regmap_field_write(wiz
->typec_ln10_swap
, 0);
741 ret
= regmap_field_write(wiz
->phy_reset_n
, true);
745 if (wiz
->lane_phy_type
[id
- 1] == PHY_TYPE_DP
)
746 ret
= regmap_field_write(wiz
->p_enable
[id
- 1], P_ENABLE
);
748 ret
= regmap_field_write(wiz
->p_enable
[id
- 1], P_ENABLE_FORCE
);
753 static const struct reset_control_ops wiz_phy_reset_ops
= {
754 .assert = wiz_phy_reset_assert
,
755 .deassert
= wiz_phy_reset_deassert
,
758 static const struct regmap_config wiz_regmap_config
= {
765 static const struct of_device_id wiz_id_table
[] = {
767 .compatible
= "ti,j721e-wiz-16g", .data
= (void *)J721E_WIZ_16G
770 .compatible
= "ti,j721e-wiz-10g", .data
= (void *)J721E_WIZ_10G
774 MODULE_DEVICE_TABLE(of
, wiz_id_table
);
776 static int wiz_get_lane_phy_types(struct device
*dev
, struct wiz
*wiz
)
778 struct device_node
*serdes
, *subnode
;
780 serdes
= of_get_child_by_name(dev
->of_node
, "serdes");
782 dev_err(dev
, "%s: Getting \"serdes\"-node failed\n", __func__
);
786 for_each_child_of_node(serdes
, subnode
) {
787 u32 reg
, num_lanes
= 1, phy_type
= PHY_NONE
;
790 ret
= of_property_read_u32(subnode
, "reg", ®
);
793 "%s: Reading \"reg\" from \"%s\" failed: %d\n",
794 __func__
, subnode
->name
, ret
);
797 of_property_read_u32(subnode
, "cdns,num-lanes", &num_lanes
);
798 of_property_read_u32(subnode
, "cdns,phy-type", &phy_type
);
800 dev_dbg(dev
, "%s: Lanes %u-%u have phy-type %u\n", __func__
,
801 reg
, reg
+ num_lanes
- 1, phy_type
);
803 for (i
= reg
; i
< reg
+ num_lanes
; i
++)
804 wiz
->lane_phy_type
[i
] = phy_type
;
810 static int wiz_probe(struct platform_device
*pdev
)
812 struct reset_controller_dev
*phy_reset_dev
;
813 struct device
*dev
= &pdev
->dev
;
814 struct device_node
*node
= dev
->of_node
;
815 struct platform_device
*serdes_pdev
;
816 struct device_node
*child_node
;
817 struct regmap
*regmap
;
824 wiz
= devm_kzalloc(dev
, sizeof(*wiz
), GFP_KERNEL
);
828 wiz
->type
= (enum wiz_type
)of_device_get_match_data(dev
);
830 child_node
= of_get_child_by_name(node
, "serdes");
832 dev_err(dev
, "Failed to get SERDES child DT node\n");
836 ret
= of_address_to_resource(child_node
, 0, &res
);
838 dev_err(dev
, "Failed to get memory resource\n");
839 goto err_addr_to_resource
;
842 base
= devm_ioremap(dev
, res
.start
, resource_size(&res
));
845 goto err_addr_to_resource
;
848 regmap
= devm_regmap_init_mmio(dev
, base
, &wiz_regmap_config
);
849 if (IS_ERR(regmap
)) {
850 dev_err(dev
, "Failed to initialize regmap\n");
851 ret
= PTR_ERR(regmap
);
852 goto err_addr_to_resource
;
855 ret
= of_property_read_u32(node
, "num-lanes", &num_lanes
);
857 dev_err(dev
, "Failed to read num-lanes property\n");
858 goto err_addr_to_resource
;
861 if (num_lanes
> WIZ_MAX_LANES
) {
862 dev_err(dev
, "Cannot support %d lanes\n", num_lanes
);
864 goto err_addr_to_resource
;
867 wiz
->gpio_typec_dir
= devm_gpiod_get_optional(dev
, "typec-dir",
869 if (IS_ERR(wiz
->gpio_typec_dir
)) {
870 ret
= PTR_ERR(wiz
->gpio_typec_dir
);
871 if (ret
!= -EPROBE_DEFER
)
872 dev_err(dev
, "Failed to request typec-dir gpio: %d\n",
874 goto err_addr_to_resource
;
877 if (wiz
->gpio_typec_dir
) {
878 ret
= of_property_read_u32(node
, "typec-dir-debounce-ms",
879 &wiz
->typec_dir_delay
);
880 if (ret
&& ret
!= -EINVAL
) {
881 dev_err(dev
, "Invalid typec-dir-debounce property\n");
882 goto err_addr_to_resource
;
885 /* use min. debounce from Type-C spec if not provided in DT */
887 wiz
->typec_dir_delay
= WIZ_TYPEC_DIR_DEBOUNCE_MIN
;
889 if (wiz
->typec_dir_delay
< WIZ_TYPEC_DIR_DEBOUNCE_MIN
||
890 wiz
->typec_dir_delay
> WIZ_TYPEC_DIR_DEBOUNCE_MAX
) {
891 dev_err(dev
, "Invalid typec-dir-debounce property\n");
892 goto err_addr_to_resource
;
896 ret
= wiz_get_lane_phy_types(dev
, wiz
);
901 wiz
->regmap
= regmap
;
902 wiz
->num_lanes
= num_lanes
;
903 if (wiz
->type
== J721E_WIZ_10G
)
904 wiz
->clk_mux_sel
= clk_mux_sel_10g
;
906 wiz
->clk_mux_sel
= clk_mux_sel_16g
;
908 wiz
->clk_div_sel
= clk_div_sel
;
910 if (wiz
->type
== J721E_WIZ_10G
)
911 wiz
->clk_div_sel_num
= WIZ_DIV_NUM_CLOCKS_10G
;
913 wiz
->clk_div_sel_num
= WIZ_DIV_NUM_CLOCKS_16G
;
915 platform_set_drvdata(pdev
, wiz
);
917 ret
= wiz_regfield_init(wiz
);
919 dev_err(dev
, "Failed to initialize regfields\n");
920 goto err_addr_to_resource
;
923 phy_reset_dev
= &wiz
->wiz_phy_reset_dev
;
924 phy_reset_dev
->dev
= dev
;
925 phy_reset_dev
->ops
= &wiz_phy_reset_ops
,
926 phy_reset_dev
->owner
= THIS_MODULE
,
927 phy_reset_dev
->of_node
= node
;
928 /* Reset for each of the lane and one for the entire SERDES */
929 phy_reset_dev
->nr_resets
= num_lanes
+ 1;
931 ret
= devm_reset_controller_register(dev
, phy_reset_dev
);
933 dev_warn(dev
, "Failed to register reset controller\n");
934 goto err_addr_to_resource
;
937 pm_runtime_enable(dev
);
938 ret
= pm_runtime_get_sync(dev
);
940 dev_err(dev
, "pm_runtime_get_sync failed\n");
944 ret
= wiz_clock_init(wiz
, node
);
946 dev_warn(dev
, "Failed to initialize clocks\n");
950 serdes_pdev
= of_platform_device_create(child_node
, NULL
, dev
);
952 dev_WARN(dev
, "Unable to create SERDES platform device\n");
954 goto err_pdev_create
;
956 wiz
->serdes_pdev
= serdes_pdev
;
960 dev_err(dev
, "WIZ initialization failed\n");
964 of_node_put(child_node
);
968 of_platform_device_destroy(&serdes_pdev
->dev
, NULL
);
971 wiz_clock_cleanup(wiz
, node
);
975 pm_runtime_disable(dev
);
977 err_addr_to_resource
:
978 of_node_put(child_node
);
983 static int wiz_remove(struct platform_device
*pdev
)
985 struct device
*dev
= &pdev
->dev
;
986 struct device_node
*node
= dev
->of_node
;
987 struct platform_device
*serdes_pdev
;
990 wiz
= dev_get_drvdata(dev
);
991 serdes_pdev
= wiz
->serdes_pdev
;
993 of_platform_device_destroy(&serdes_pdev
->dev
, NULL
);
994 wiz_clock_cleanup(wiz
, node
);
996 pm_runtime_disable(dev
);
1001 static struct platform_driver wiz_driver
= {
1003 .remove
= wiz_remove
,
1006 .of_match_table
= wiz_id_table
,
1009 module_platform_driver(wiz_driver
);
1011 MODULE_AUTHOR("Texas Instruments Inc.");
1012 MODULE_DESCRIPTION("TI J721E WIZ driver");
1013 MODULE_LICENSE("GPL v2");