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
8 #include<linux/phy/phy.h>
9 #include<linux/platform_device.h>
10 #include<linux/module.h>
11 #include<linux/gpio.h>
12 #include<linux/gpio/consumer.h>
13 #include <linux/mux/consumer.h>
15 struct can_transceiver_data {
17 #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
18 #define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
21 struct can_transceiver_phy {
22 struct phy *generic_phy;
23 struct gpio_desc *standby_gpio;
24 struct gpio_desc *enable_gpio;
25 struct mux_state *mux_state;
28 /* Power on function */
29 static int can_transceiver_phy_power_on(struct phy *phy)
31 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
34 if (can_transceiver_phy->mux_state) {
35 ret = mux_state_select(can_transceiver_phy->mux_state);
37 dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
41 if (can_transceiver_phy->standby_gpio)
42 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
43 if (can_transceiver_phy->enable_gpio)
44 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 1);
49 /* Power off function */
50 static int can_transceiver_phy_power_off(struct phy *phy)
52 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
54 if (can_transceiver_phy->standby_gpio)
55 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
56 if (can_transceiver_phy->enable_gpio)
57 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
58 if (can_transceiver_phy->mux_state)
59 mux_state_deselect(can_transceiver_phy->mux_state);
64 static const struct phy_ops can_transceiver_phy_ops = {
65 .power_on = can_transceiver_phy_power_on,
66 .power_off = can_transceiver_phy_power_off,
70 static const struct can_transceiver_data tcan1042_drvdata = {
71 .flags = CAN_TRANSCEIVER_STB_PRESENT,
74 static const struct can_transceiver_data tcan1043_drvdata = {
75 .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
78 static const struct of_device_id can_transceiver_phy_ids[] = {
80 .compatible = "ti,tcan1042",
81 .data = &tcan1042_drvdata
84 .compatible = "ti,tcan1043",
85 .data = &tcan1043_drvdata
89 MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
91 static int can_transceiver_phy_probe(struct platform_device *pdev)
93 struct phy_provider *phy_provider;
94 struct device *dev = &pdev->dev;
95 struct can_transceiver_phy *can_transceiver_phy;
96 const struct can_transceiver_data *drvdata;
97 const struct of_device_id *match;
99 struct gpio_desc *standby_gpio;
100 struct gpio_desc *enable_gpio;
103 can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
104 if (!can_transceiver_phy)
107 match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
108 drvdata = match->data;
110 if (of_property_read_bool(dev->of_node, "mux-states")) {
111 struct mux_state *mux_state;
113 mux_state = devm_mux_state_get(dev, NULL);
114 if (IS_ERR(mux_state))
115 return dev_err_probe(&pdev->dev, PTR_ERR(mux_state),
116 "failed to get mux\n");
117 can_transceiver_phy->mux_state = mux_state;
120 phy = devm_phy_create(dev, dev->of_node,
121 &can_transceiver_phy_ops);
123 dev_err(dev, "failed to create can transceiver phy\n");
127 device_property_read_u32(dev, "max-bitrate", &max_bitrate);
129 dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
130 phy->attrs.max_link_rate = max_bitrate;
132 can_transceiver_phy->generic_phy = phy;
134 if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
135 standby_gpio = devm_gpiod_get_optional(dev, "standby", GPIOD_OUT_HIGH);
136 if (IS_ERR(standby_gpio))
137 return PTR_ERR(standby_gpio);
138 can_transceiver_phy->standby_gpio = standby_gpio;
141 if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
142 enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
143 if (IS_ERR(enable_gpio))
144 return PTR_ERR(enable_gpio);
145 can_transceiver_phy->enable_gpio = enable_gpio;
148 phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
150 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
152 return PTR_ERR_OR_ZERO(phy_provider);
155 static struct platform_driver can_transceiver_phy_driver = {
156 .probe = can_transceiver_phy_probe,
158 .name = "can-transceiver-phy",
159 .of_match_table = can_transceiver_phy_ids,
163 module_platform_driver(can_transceiver_phy_driver);
165 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
166 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
167 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
168 MODULE_LICENSE("GPL v2");