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 static const struct reg_field p_align
[WIZ_MAX_LANES
] = {
82 REG_FIELD(WIZ_LANECTL(0), 29, 29),
83 REG_FIELD(WIZ_LANECTL(1), 29, 29),
84 REG_FIELD(WIZ_LANECTL(2), 29, 29),
85 REG_FIELD(WIZ_LANECTL(3), 29, 29),
88 static const struct reg_field p_raw_auto_start
[WIZ_MAX_LANES
] = {
89 REG_FIELD(WIZ_LANECTL(0), 28, 28),
90 REG_FIELD(WIZ_LANECTL(1), 28, 28),
91 REG_FIELD(WIZ_LANECTL(2), 28, 28),
92 REG_FIELD(WIZ_LANECTL(3), 28, 28),
95 static const struct reg_field p_standard_mode
[WIZ_MAX_LANES
] = {
96 REG_FIELD(WIZ_LANECTL(0), 24, 25),
97 REG_FIELD(WIZ_LANECTL(1), 24, 25),
98 REG_FIELD(WIZ_LANECTL(2), 24, 25),
99 REG_FIELD(WIZ_LANECTL(3), 24, 25),
102 static const struct reg_field typec_ln10_swap
=
103 REG_FIELD(WIZ_SERDES_TYPEC
, 30, 30);
107 struct regmap_field
*field
;
109 struct clk_init_data clk_data
;
112 #define to_wiz_clk_mux(_hw) container_of(_hw, struct wiz_clk_mux, hw)
114 struct wiz_clk_divider
{
116 struct regmap_field
*field
;
117 struct clk_div_table
*table
;
118 struct clk_init_data clk_data
;
121 #define to_wiz_clk_div(_hw) container_of(_hw, struct wiz_clk_divider, hw)
123 struct wiz_clk_mux_sel
{
124 struct regmap_field
*field
;
126 const char *node_name
;
129 struct wiz_clk_div_sel
{
130 struct regmap_field
*field
;
131 struct clk_div_table
*table
;
132 const char *node_name
;
135 static struct wiz_clk_mux_sel clk_mux_sel_16g
[] = {
138 * Mux value to be configured for each of the input clocks
139 * in the order populated in device tree
142 .node_name
= "pll0-refclk",
146 .node_name
= "pll1-refclk",
149 .table
= { 1, 3, 0, 2 },
150 .node_name
= "refclk-dig",
154 static struct wiz_clk_mux_sel clk_mux_sel_10g
[] = {
157 * Mux value to be configured for each of the input clocks
158 * in the order populated in device tree
161 .node_name
= "pll0-refclk",
165 .node_name
= "pll1-refclk",
169 .node_name
= "refclk-dig",
173 static struct clk_div_table clk_div_table
[] = {
174 { .val
= 0, .div
= 1, },
175 { .val
= 1, .div
= 2, },
176 { .val
= 2, .div
= 4, },
177 { .val
= 3, .div
= 8, },
180 static struct wiz_clk_div_sel clk_div_sel
[] = {
182 .table
= clk_div_table
,
183 .node_name
= "cmn-refclk-dig-div",
186 .table
= clk_div_table
,
187 .node_name
= "cmn-refclk1-dig-div",
196 #define WIZ_TYPEC_DIR_DEBOUNCE_MIN 100 /* ms */
197 #define WIZ_TYPEC_DIR_DEBOUNCE_MAX 1000
200 struct regmap
*regmap
;
202 struct wiz_clk_mux_sel
*clk_mux_sel
;
203 struct wiz_clk_div_sel
*clk_div_sel
;
204 unsigned int clk_div_sel_num
;
205 struct regmap_field
*por_en
;
206 struct regmap_field
*phy_reset_n
;
207 struct regmap_field
*p_enable
[WIZ_MAX_LANES
];
208 struct regmap_field
*p_align
[WIZ_MAX_LANES
];
209 struct regmap_field
*p_raw_auto_start
[WIZ_MAX_LANES
];
210 struct regmap_field
*p_standard_mode
[WIZ_MAX_LANES
];
211 struct regmap_field
*pma_cmn_refclk_int_mode
;
212 struct regmap_field
*pma_cmn_refclk_mode
;
213 struct regmap_field
*pma_cmn_refclk_dig_div
;
214 struct regmap_field
*pma_cmn_refclk1_dig_div
;
215 struct regmap_field
*typec_ln10_swap
;
219 struct platform_device
*serdes_pdev
;
220 struct reset_controller_dev wiz_phy_reset_dev
;
221 struct gpio_desc
*gpio_typec_dir
;
225 static int wiz_reset(struct wiz
*wiz
)
229 ret
= regmap_field_write(wiz
->por_en
, 0x1);
235 ret
= regmap_field_write(wiz
->por_en
, 0x0);
242 static int wiz_mode_select(struct wiz
*wiz
)
244 u32 num_lanes
= wiz
->num_lanes
;
248 for (i
= 0; i
< num_lanes
; i
++) {
249 ret
= regmap_field_write(wiz
->p_standard_mode
[i
],
258 static int wiz_init_raw_interface(struct wiz
*wiz
, bool enable
)
260 u32 num_lanes
= wiz
->num_lanes
;
264 for (i
= 0; i
< num_lanes
; i
++) {
265 ret
= regmap_field_write(wiz
->p_align
[i
], enable
);
269 ret
= regmap_field_write(wiz
->p_raw_auto_start
[i
], enable
);
277 static int wiz_init(struct wiz
*wiz
)
279 struct device
*dev
= wiz
->dev
;
282 ret
= wiz_reset(wiz
);
284 dev_err(dev
, "WIZ reset failed\n");
288 ret
= wiz_mode_select(wiz
);
290 dev_err(dev
, "WIZ mode select failed\n");
294 ret
= wiz_init_raw_interface(wiz
, true);
296 dev_err(dev
, "WIZ interface initialization failed\n");
303 static int wiz_regfield_init(struct wiz
*wiz
)
305 struct wiz_clk_mux_sel
*clk_mux_sel
;
306 struct wiz_clk_div_sel
*clk_div_sel
;
307 struct regmap
*regmap
= wiz
->regmap
;
308 int num_lanes
= wiz
->num_lanes
;
309 struct device
*dev
= wiz
->dev
;
312 wiz
->por_en
= devm_regmap_field_alloc(dev
, regmap
, por_en
);
313 if (IS_ERR(wiz
->por_en
)) {
314 dev_err(dev
, "POR_EN reg field init failed\n");
315 return PTR_ERR(wiz
->por_en
);
318 wiz
->phy_reset_n
= devm_regmap_field_alloc(dev
, regmap
,
320 if (IS_ERR(wiz
->phy_reset_n
)) {
321 dev_err(dev
, "PHY_RESET_N reg field init failed\n");
322 return PTR_ERR(wiz
->phy_reset_n
);
325 wiz
->pma_cmn_refclk_int_mode
=
326 devm_regmap_field_alloc(dev
, regmap
, pma_cmn_refclk_int_mode
);
327 if (IS_ERR(wiz
->pma_cmn_refclk_int_mode
)) {
328 dev_err(dev
, "PMA_CMN_REFCLK_INT_MODE reg field init failed\n");
329 return PTR_ERR(wiz
->pma_cmn_refclk_int_mode
);
332 wiz
->pma_cmn_refclk_mode
=
333 devm_regmap_field_alloc(dev
, regmap
, pma_cmn_refclk_mode
);
334 if (IS_ERR(wiz
->pma_cmn_refclk_mode
)) {
335 dev_err(dev
, "PMA_CMN_REFCLK_MODE reg field init failed\n");
336 return PTR_ERR(wiz
->pma_cmn_refclk_mode
);
339 clk_div_sel
= &wiz
->clk_div_sel
[CMN_REFCLK_DIG_DIV
];
340 clk_div_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
341 pma_cmn_refclk_dig_div
);
342 if (IS_ERR(clk_div_sel
->field
)) {
343 dev_err(dev
, "PMA_CMN_REFCLK_DIG_DIV reg field init failed\n");
344 return PTR_ERR(clk_div_sel
->field
);
347 if (wiz
->type
== J721E_WIZ_16G
) {
348 clk_div_sel
= &wiz
->clk_div_sel
[CMN_REFCLK1_DIG_DIV
];
350 devm_regmap_field_alloc(dev
, regmap
,
351 pma_cmn_refclk1_dig_div
);
352 if (IS_ERR(clk_div_sel
->field
)) {
353 dev_err(dev
, "PMA_CMN_REFCLK1_DIG_DIV reg field init failed\n");
354 return PTR_ERR(clk_div_sel
->field
);
358 clk_mux_sel
= &wiz
->clk_mux_sel
[PLL0_REFCLK
];
359 clk_mux_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
360 pll0_refclk_mux_sel
);
361 if (IS_ERR(clk_mux_sel
->field
)) {
362 dev_err(dev
, "PLL0_REFCLK_SEL reg field init failed\n");
363 return PTR_ERR(clk_mux_sel
->field
);
366 clk_mux_sel
= &wiz
->clk_mux_sel
[PLL1_REFCLK
];
367 clk_mux_sel
->field
= devm_regmap_field_alloc(dev
, regmap
,
368 pll1_refclk_mux_sel
);
369 if (IS_ERR(clk_mux_sel
->field
)) {
370 dev_err(dev
, "PLL1_REFCLK_SEL reg field init failed\n");
371 return PTR_ERR(clk_mux_sel
->field
);
374 clk_mux_sel
= &wiz
->clk_mux_sel
[REFCLK_DIG
];
375 if (wiz
->type
== J721E_WIZ_10G
)
377 devm_regmap_field_alloc(dev
, regmap
,
381 devm_regmap_field_alloc(dev
, regmap
,
384 if (IS_ERR(clk_mux_sel
->field
)) {
385 dev_err(dev
, "REFCLK_DIG_SEL reg field init failed\n");
386 return PTR_ERR(clk_mux_sel
->field
);
389 for (i
= 0; i
< num_lanes
; i
++) {
390 wiz
->p_enable
[i
] = devm_regmap_field_alloc(dev
, regmap
,
392 if (IS_ERR(wiz
->p_enable
[i
])) {
393 dev_err(dev
, "P%d_ENABLE reg field init failed\n", i
);
394 return PTR_ERR(wiz
->p_enable
[i
]);
397 wiz
->p_align
[i
] = devm_regmap_field_alloc(dev
, regmap
,
399 if (IS_ERR(wiz
->p_align
[i
])) {
400 dev_err(dev
, "P%d_ALIGN reg field init failed\n", i
);
401 return PTR_ERR(wiz
->p_align
[i
]);
404 wiz
->p_raw_auto_start
[i
] =
405 devm_regmap_field_alloc(dev
, regmap
, p_raw_auto_start
[i
]);
406 if (IS_ERR(wiz
->p_raw_auto_start
[i
])) {
407 dev_err(dev
, "P%d_RAW_AUTO_START reg field init fail\n",
409 return PTR_ERR(wiz
->p_raw_auto_start
[i
]);
412 wiz
->p_standard_mode
[i
] =
413 devm_regmap_field_alloc(dev
, regmap
, p_standard_mode
[i
]);
414 if (IS_ERR(wiz
->p_standard_mode
[i
])) {
415 dev_err(dev
, "P%d_STANDARD_MODE reg field init fail\n",
417 return PTR_ERR(wiz
->p_standard_mode
[i
]);
421 wiz
->typec_ln10_swap
= devm_regmap_field_alloc(dev
, regmap
,
423 if (IS_ERR(wiz
->typec_ln10_swap
)) {
424 dev_err(dev
, "LN10_SWAP reg field init failed\n");
425 return PTR_ERR(wiz
->typec_ln10_swap
);
431 static u8
wiz_clk_mux_get_parent(struct clk_hw
*hw
)
433 struct wiz_clk_mux
*mux
= to_wiz_clk_mux(hw
);
434 struct regmap_field
*field
= mux
->field
;
437 regmap_field_read(field
, &val
);
438 return clk_mux_val_to_index(hw
, mux
->table
, 0, val
);
441 static int wiz_clk_mux_set_parent(struct clk_hw
*hw
, u8 index
)
443 struct wiz_clk_mux
*mux
= to_wiz_clk_mux(hw
);
444 struct regmap_field
*field
= mux
->field
;
447 val
= mux
->table
[index
];
448 return regmap_field_write(field
, val
);
451 static const struct clk_ops wiz_clk_mux_ops
= {
452 .set_parent
= wiz_clk_mux_set_parent
,
453 .get_parent
= wiz_clk_mux_get_parent
,
456 static int wiz_mux_clk_register(struct wiz
*wiz
, struct device_node
*node
,
457 struct regmap_field
*field
, u32
*table
)
459 struct device
*dev
= wiz
->dev
;
460 struct clk_init_data
*init
;
461 const char **parent_names
;
462 unsigned int num_parents
;
463 struct wiz_clk_mux
*mux
;
468 mux
= devm_kzalloc(dev
, sizeof(*mux
), GFP_KERNEL
);
472 num_parents
= of_clk_get_parent_count(node
);
473 if (num_parents
< 2) {
474 dev_err(dev
, "SERDES clock must have parents\n");
478 parent_names
= devm_kzalloc(dev
, (sizeof(char *) * num_parents
),
483 of_clk_parent_fill(node
, parent_names
, num_parents
);
485 snprintf(clk_name
, sizeof(clk_name
), "%s_%s", dev_name(dev
),
488 init
= &mux
->clk_data
;
490 init
->ops
= &wiz_clk_mux_ops
;
491 init
->flags
= CLK_SET_RATE_NO_REPARENT
;
492 init
->parent_names
= parent_names
;
493 init
->num_parents
= num_parents
;
494 init
->name
= clk_name
;
500 clk
= devm_clk_register(dev
, &mux
->hw
);
504 ret
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
506 dev_err(dev
, "Failed to add clock provider: %s\n", clk_name
);
511 static unsigned long wiz_clk_div_recalc_rate(struct clk_hw
*hw
,
512 unsigned long parent_rate
)
514 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
515 struct regmap_field
*field
= div
->field
;
518 regmap_field_read(field
, &val
);
520 return divider_recalc_rate(hw
, parent_rate
, val
, div
->table
, 0x0, 2);
523 static long wiz_clk_div_round_rate(struct clk_hw
*hw
, unsigned long rate
,
524 unsigned long *prate
)
526 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
528 return divider_round_rate(hw
, rate
, prate
, div
->table
, 2, 0x0);
531 static int wiz_clk_div_set_rate(struct clk_hw
*hw
, unsigned long rate
,
532 unsigned long parent_rate
)
534 struct wiz_clk_divider
*div
= to_wiz_clk_div(hw
);
535 struct regmap_field
*field
= div
->field
;
538 val
= divider_get_val(rate
, parent_rate
, div
->table
, 2, 0x0);
542 return regmap_field_write(field
, val
);
545 static const struct clk_ops wiz_clk_div_ops
= {
546 .recalc_rate
= wiz_clk_div_recalc_rate
,
547 .round_rate
= wiz_clk_div_round_rate
,
548 .set_rate
= wiz_clk_div_set_rate
,
551 static int wiz_div_clk_register(struct wiz
*wiz
, struct device_node
*node
,
552 struct regmap_field
*field
,
553 struct clk_div_table
*table
)
555 struct device
*dev
= wiz
->dev
;
556 struct wiz_clk_divider
*div
;
557 struct clk_init_data
*init
;
558 const char **parent_names
;
563 div
= devm_kzalloc(dev
, sizeof(*div
), GFP_KERNEL
);
567 snprintf(clk_name
, sizeof(clk_name
), "%s_%s", dev_name(dev
),
570 parent_names
= devm_kzalloc(dev
, sizeof(char *), GFP_KERNEL
);
574 of_clk_parent_fill(node
, parent_names
, 1);
576 init
= &div
->clk_data
;
578 init
->ops
= &wiz_clk_div_ops
;
580 init
->parent_names
= parent_names
;
581 init
->num_parents
= 1;
582 init
->name
= clk_name
;
588 clk
= devm_clk_register(dev
, &div
->hw
);
592 ret
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
594 dev_err(dev
, "Failed to add clock provider: %s\n", clk_name
);
599 static void wiz_clock_cleanup(struct wiz
*wiz
, struct device_node
*node
)
601 struct wiz_clk_mux_sel
*clk_mux_sel
= wiz
->clk_mux_sel
;
602 struct device_node
*clk_node
;
605 for (i
= 0; i
< WIZ_MUX_NUM_CLOCKS
; i
++) {
606 clk_node
= of_get_child_by_name(node
, clk_mux_sel
[i
].node_name
);
607 of_clk_del_provider(clk_node
);
608 of_node_put(clk_node
);
612 static int wiz_clock_init(struct wiz
*wiz
, struct device_node
*node
)
614 struct wiz_clk_mux_sel
*clk_mux_sel
= wiz
->clk_mux_sel
;
615 struct device
*dev
= wiz
->dev
;
616 struct device_node
*clk_node
;
617 const char *node_name
;
623 clk
= devm_clk_get(dev
, "core_ref_clk");
625 dev_err(dev
, "core_ref_clk clock not found\n");
630 rate
= clk_get_rate(clk
);
631 if (rate
>= 100000000)
632 regmap_field_write(wiz
->pma_cmn_refclk_int_mode
, 0x1);
634 regmap_field_write(wiz
->pma_cmn_refclk_int_mode
, 0x3);
636 clk
= devm_clk_get(dev
, "ext_ref_clk");
638 dev_err(dev
, "ext_ref_clk clock not found\n");
643 rate
= clk_get_rate(clk
);
644 if (rate
>= 100000000)
645 regmap_field_write(wiz
->pma_cmn_refclk_mode
, 0x0);
647 regmap_field_write(wiz
->pma_cmn_refclk_mode
, 0x2);
649 for (i
= 0; i
< WIZ_MUX_NUM_CLOCKS
; i
++) {
650 node_name
= clk_mux_sel
[i
].node_name
;
651 clk_node
= of_get_child_by_name(node
, node_name
);
653 dev_err(dev
, "Unable to get %s node\n", node_name
);
658 ret
= wiz_mux_clk_register(wiz
, clk_node
, clk_mux_sel
[i
].field
,
659 clk_mux_sel
[i
].table
);
661 dev_err(dev
, "Failed to register %s clock\n",
663 of_node_put(clk_node
);
667 of_node_put(clk_node
);
670 for (i
= 0; i
< wiz
->clk_div_sel_num
; i
++) {
671 node_name
= clk_div_sel
[i
].node_name
;
672 clk_node
= of_get_child_by_name(node
, node_name
);
674 dev_err(dev
, "Unable to get %s node\n", node_name
);
679 ret
= wiz_div_clk_register(wiz
, clk_node
, clk_div_sel
[i
].field
,
680 clk_div_sel
[i
].table
);
682 dev_err(dev
, "Failed to register %s clock\n",
684 of_node_put(clk_node
);
688 of_node_put(clk_node
);
693 wiz_clock_cleanup(wiz
, node
);
698 static int wiz_phy_reset_assert(struct reset_controller_dev
*rcdev
,
701 struct device
*dev
= rcdev
->dev
;
702 struct wiz
*wiz
= dev_get_drvdata(dev
);
706 ret
= regmap_field_write(wiz
->phy_reset_n
, false);
710 ret
= regmap_field_write(wiz
->p_enable
[id
- 1], false);
714 static int wiz_phy_reset_deassert(struct reset_controller_dev
*rcdev
,
717 struct device
*dev
= rcdev
->dev
;
718 struct wiz
*wiz
= dev_get_drvdata(dev
);
721 /* if typec-dir gpio was specified, set LN10 SWAP bit based on that */
722 if (id
== 0 && wiz
->gpio_typec_dir
) {
723 if (wiz
->typec_dir_delay
)
724 msleep_interruptible(wiz
->typec_dir_delay
);
726 if (gpiod_get_value_cansleep(wiz
->gpio_typec_dir
))
727 regmap_field_write(wiz
->typec_ln10_swap
, 1);
729 regmap_field_write(wiz
->typec_ln10_swap
, 0);
733 ret
= regmap_field_write(wiz
->phy_reset_n
, true);
737 ret
= regmap_field_write(wiz
->p_enable
[id
- 1], true);
741 static const struct reset_control_ops wiz_phy_reset_ops
= {
742 .assert = wiz_phy_reset_assert
,
743 .deassert
= wiz_phy_reset_deassert
,
746 static struct regmap_config wiz_regmap_config
= {
753 static const struct of_device_id wiz_id_table
[] = {
755 .compatible
= "ti,j721e-wiz-16g", .data
= (void *)J721E_WIZ_16G
758 .compatible
= "ti,j721e-wiz-10g", .data
= (void *)J721E_WIZ_10G
762 MODULE_DEVICE_TABLE(of
, wiz_id_table
);
764 static int wiz_probe(struct platform_device
*pdev
)
766 struct reset_controller_dev
*phy_reset_dev
;
767 struct device
*dev
= &pdev
->dev
;
768 struct device_node
*node
= dev
->of_node
;
769 struct platform_device
*serdes_pdev
;
770 struct device_node
*child_node
;
771 struct regmap
*regmap
;
778 wiz
= devm_kzalloc(dev
, sizeof(*wiz
), GFP_KERNEL
);
782 wiz
->type
= (enum wiz_type
)of_device_get_match_data(dev
);
784 child_node
= of_get_child_by_name(node
, "serdes");
786 dev_err(dev
, "Failed to get SERDES child DT node\n");
790 ret
= of_address_to_resource(child_node
, 0, &res
);
792 dev_err(dev
, "Failed to get memory resource\n");
793 goto err_addr_to_resource
;
796 base
= devm_ioremap(dev
, res
.start
, resource_size(&res
));
798 goto err_addr_to_resource
;
800 regmap
= devm_regmap_init_mmio(dev
, base
, &wiz_regmap_config
);
801 if (IS_ERR(regmap
)) {
802 dev_err(dev
, "Failed to initialize regmap\n");
803 ret
= PTR_ERR(regmap
);
804 goto err_addr_to_resource
;
807 ret
= of_property_read_u32(node
, "num-lanes", &num_lanes
);
809 dev_err(dev
, "Failed to read num-lanes property\n");
810 goto err_addr_to_resource
;
813 if (num_lanes
> WIZ_MAX_LANES
) {
814 dev_err(dev
, "Cannot support %d lanes\n", num_lanes
);
815 goto err_addr_to_resource
;
818 wiz
->gpio_typec_dir
= devm_gpiod_get_optional(dev
, "typec-dir",
820 if (IS_ERR(wiz
->gpio_typec_dir
)) {
821 ret
= PTR_ERR(wiz
->gpio_typec_dir
);
822 if (ret
!= -EPROBE_DEFER
)
823 dev_err(dev
, "Failed to request typec-dir gpio: %d\n",
825 goto err_addr_to_resource
;
828 if (wiz
->gpio_typec_dir
) {
829 ret
= of_property_read_u32(node
, "typec-dir-debounce-ms",
830 &wiz
->typec_dir_delay
);
831 if (ret
&& ret
!= -EINVAL
) {
832 dev_err(dev
, "Invalid typec-dir-debounce property\n");
833 goto err_addr_to_resource
;
836 /* use min. debounce from Type-C spec if not provided in DT */
838 wiz
->typec_dir_delay
= WIZ_TYPEC_DIR_DEBOUNCE_MIN
;
840 if (wiz
->typec_dir_delay
< WIZ_TYPEC_DIR_DEBOUNCE_MIN
||
841 wiz
->typec_dir_delay
> WIZ_TYPEC_DIR_DEBOUNCE_MAX
) {
842 dev_err(dev
, "Invalid typec-dir-debounce property\n");
843 goto err_addr_to_resource
;
848 wiz
->regmap
= regmap
;
849 wiz
->num_lanes
= num_lanes
;
850 if (wiz
->type
== J721E_WIZ_10G
)
851 wiz
->clk_mux_sel
= clk_mux_sel_10g
;
853 wiz
->clk_mux_sel
= clk_mux_sel_16g
;
855 wiz
->clk_div_sel
= clk_div_sel
;
857 if (wiz
->type
== J721E_WIZ_10G
)
858 wiz
->clk_div_sel_num
= WIZ_DIV_NUM_CLOCKS_10G
;
860 wiz
->clk_div_sel_num
= WIZ_DIV_NUM_CLOCKS_16G
;
862 platform_set_drvdata(pdev
, wiz
);
864 ret
= wiz_regfield_init(wiz
);
866 dev_err(dev
, "Failed to initialize regfields\n");
867 goto err_addr_to_resource
;
870 phy_reset_dev
= &wiz
->wiz_phy_reset_dev
;
871 phy_reset_dev
->dev
= dev
;
872 phy_reset_dev
->ops
= &wiz_phy_reset_ops
,
873 phy_reset_dev
->owner
= THIS_MODULE
,
874 phy_reset_dev
->of_node
= node
;
875 /* Reset for each of the lane and one for the entire SERDES */
876 phy_reset_dev
->nr_resets
= num_lanes
+ 1;
878 ret
= devm_reset_controller_register(dev
, phy_reset_dev
);
880 dev_warn(dev
, "Failed to register reset controller\n");
881 goto err_addr_to_resource
;
884 pm_runtime_enable(dev
);
885 ret
= pm_runtime_get_sync(dev
);
887 dev_err(dev
, "pm_runtime_get_sync failed\n");
891 ret
= wiz_clock_init(wiz
, node
);
893 dev_warn(dev
, "Failed to initialize clocks\n");
897 serdes_pdev
= of_platform_device_create(child_node
, NULL
, dev
);
899 dev_WARN(dev
, "Unable to create SERDES platform device\n");
900 goto err_pdev_create
;
902 wiz
->serdes_pdev
= serdes_pdev
;
906 dev_err(dev
, "WIZ initialization failed\n");
910 of_node_put(child_node
);
914 of_platform_device_destroy(&serdes_pdev
->dev
, NULL
);
917 wiz_clock_cleanup(wiz
, node
);
921 pm_runtime_disable(dev
);
923 err_addr_to_resource
:
924 of_node_put(child_node
);
929 static int wiz_remove(struct platform_device
*pdev
)
931 struct device
*dev
= &pdev
->dev
;
932 struct device_node
*node
= dev
->of_node
;
933 struct platform_device
*serdes_pdev
;
936 wiz
= dev_get_drvdata(dev
);
937 serdes_pdev
= wiz
->serdes_pdev
;
939 of_platform_device_destroy(&serdes_pdev
->dev
, NULL
);
940 wiz_clock_cleanup(wiz
, node
);
942 pm_runtime_disable(dev
);
947 static struct platform_driver wiz_driver
= {
949 .remove
= wiz_remove
,
952 .of_match_table
= wiz_id_table
,
955 module_platform_driver(wiz_driver
);
957 MODULE_AUTHOR("Texas Instruments Inc.");
958 MODULE_DESCRIPTION("TI J721E WIZ driver");
959 MODULE_LICENSE("GPL v2");