1 // SPDX-License-Identifier: GPL-2.0
3 * phy-can-transceiver.c - phy driver for CAN transceivers
5 * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
9 #include <linux/phy/phy.h>
10 #include <linux/platform_device.h>
11 #include <linux/module.h>
12 #include <linux/gpio.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/mux/consumer.h>
16 struct can_transceiver_data
{
18 #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
19 #define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
22 struct can_transceiver_phy
{
23 struct phy
*generic_phy
;
24 struct gpio_desc
*standby_gpio
;
25 struct gpio_desc
*enable_gpio
;
26 struct mux_state
*mux_state
;
29 /* Power on function */
30 static int can_transceiver_phy_power_on(struct phy
*phy
)
32 struct can_transceiver_phy
*can_transceiver_phy
= phy_get_drvdata(phy
);
35 if (can_transceiver_phy
->mux_state
) {
36 ret
= mux_state_select(can_transceiver_phy
->mux_state
);
38 dev_err(&phy
->dev
, "Failed to select CAN mux: %d\n", ret
);
42 if (can_transceiver_phy
->standby_gpio
)
43 gpiod_set_value_cansleep(can_transceiver_phy
->standby_gpio
, 0);
44 if (can_transceiver_phy
->enable_gpio
)
45 gpiod_set_value_cansleep(can_transceiver_phy
->enable_gpio
, 1);
50 /* Power off function */
51 static int can_transceiver_phy_power_off(struct phy
*phy
)
53 struct can_transceiver_phy
*can_transceiver_phy
= phy_get_drvdata(phy
);
55 if (can_transceiver_phy
->standby_gpio
)
56 gpiod_set_value_cansleep(can_transceiver_phy
->standby_gpio
, 1);
57 if (can_transceiver_phy
->enable_gpio
)
58 gpiod_set_value_cansleep(can_transceiver_phy
->enable_gpio
, 0);
59 if (can_transceiver_phy
->mux_state
)
60 mux_state_deselect(can_transceiver_phy
->mux_state
);
65 static const struct phy_ops can_transceiver_phy_ops
= {
66 .power_on
= can_transceiver_phy_power_on
,
67 .power_off
= can_transceiver_phy_power_off
,
71 static const struct can_transceiver_data tcan1042_drvdata
= {
72 .flags
= CAN_TRANSCEIVER_STB_PRESENT
,
75 static const struct can_transceiver_data tcan1043_drvdata
= {
76 .flags
= CAN_TRANSCEIVER_STB_PRESENT
| CAN_TRANSCEIVER_EN_PRESENT
,
79 static const struct of_device_id can_transceiver_phy_ids
[] = {
81 .compatible
= "ti,tcan1042",
82 .data
= &tcan1042_drvdata
85 .compatible
= "ti,tcan1043",
86 .data
= &tcan1043_drvdata
89 .compatible
= "nxp,tjr1443",
90 .data
= &tcan1043_drvdata
94 MODULE_DEVICE_TABLE(of
, can_transceiver_phy_ids
);
96 static int can_transceiver_phy_probe(struct platform_device
*pdev
)
98 struct phy_provider
*phy_provider
;
99 struct device
*dev
= &pdev
->dev
;
100 struct can_transceiver_phy
*can_transceiver_phy
;
101 const struct can_transceiver_data
*drvdata
;
102 const struct of_device_id
*match
;
104 struct gpio_desc
*standby_gpio
;
105 struct gpio_desc
*enable_gpio
;
109 can_transceiver_phy
= devm_kzalloc(dev
, sizeof(struct can_transceiver_phy
), GFP_KERNEL
);
110 if (!can_transceiver_phy
)
113 match
= of_match_node(can_transceiver_phy_ids
, pdev
->dev
.of_node
);
114 drvdata
= match
->data
;
116 if (of_property_read_bool(dev
->of_node
, "mux-states")) {
117 struct mux_state
*mux_state
;
119 mux_state
= devm_mux_state_get(dev
, NULL
);
120 if (IS_ERR(mux_state
))
121 return dev_err_probe(&pdev
->dev
, PTR_ERR(mux_state
),
122 "failed to get mux\n");
123 can_transceiver_phy
->mux_state
= mux_state
;
126 phy
= devm_phy_create(dev
, dev
->of_node
,
127 &can_transceiver_phy_ops
);
129 dev_err(dev
, "failed to create can transceiver phy\n");
133 err
= device_property_read_u32(dev
, "max-bitrate", &max_bitrate
);
134 if ((err
!= -EINVAL
) && !max_bitrate
)
135 dev_warn(dev
, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
136 phy
->attrs
.max_link_rate
= max_bitrate
;
138 can_transceiver_phy
->generic_phy
= phy
;
140 if (drvdata
->flags
& CAN_TRANSCEIVER_STB_PRESENT
) {
141 standby_gpio
= devm_gpiod_get_optional(dev
, "standby", GPIOD_OUT_HIGH
);
142 if (IS_ERR(standby_gpio
))
143 return PTR_ERR(standby_gpio
);
144 can_transceiver_phy
->standby_gpio
= standby_gpio
;
147 if (drvdata
->flags
& CAN_TRANSCEIVER_EN_PRESENT
) {
148 enable_gpio
= devm_gpiod_get_optional(dev
, "enable", GPIOD_OUT_LOW
);
149 if (IS_ERR(enable_gpio
))
150 return PTR_ERR(enable_gpio
);
151 can_transceiver_phy
->enable_gpio
= enable_gpio
;
154 phy_set_drvdata(can_transceiver_phy
->generic_phy
, can_transceiver_phy
);
156 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
158 return PTR_ERR_OR_ZERO(phy_provider
);
161 static struct platform_driver can_transceiver_phy_driver
= {
162 .probe
= can_transceiver_phy_probe
,
164 .name
= "can-transceiver-phy",
165 .of_match_table
= can_transceiver_phy_ids
,
169 module_platform_driver(can_transceiver_phy_driver
);
171 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
172 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
173 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
174 MODULE_LICENSE("GPL v2");