1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * phy-can-transceiver.c - phy driver for CAN transceivers
4 *
5 * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
6 *
7 */
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
14 struct can_transceiver_data {
15 u32 flags;
16 #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
17 #define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
18 };
19
20 struct can_transceiver_phy {
21 struct phy *generic_phy;
22 struct gpio_desc *standby_gpio;
23 struct gpio_desc *enable_gpio;
24 };
25
26 /* Power on function */
can_transceiver_phy_power_on(struct phy * phy)27 static int can_transceiver_phy_power_on(struct phy *phy)
28 {
29 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
30
31 if (can_transceiver_phy->standby_gpio)
32 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
33 if (can_transceiver_phy->enable_gpio)
34 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 1);
35
36 return 0;
37 }
38
39 /* Power off function */
can_transceiver_phy_power_off(struct phy * phy)40 static int can_transceiver_phy_power_off(struct phy *phy)
41 {
42 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
43
44 if (can_transceiver_phy->standby_gpio)
45 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
46 if (can_transceiver_phy->enable_gpio)
47 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
48
49 return 0;
50 }
51
52 static const struct phy_ops can_transceiver_phy_ops = {
53 .power_on = can_transceiver_phy_power_on,
54 .power_off = can_transceiver_phy_power_off,
55 .owner = THIS_MODULE,
56 };
57
58 static const struct can_transceiver_data tcan1042_drvdata = {
59 .flags = CAN_TRANSCEIVER_STB_PRESENT,
60 };
61
62 static const struct can_transceiver_data tcan1043_drvdata = {
63 .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
64 };
65
66 static const struct of_device_id can_transceiver_phy_ids[] = {
67 {
68 .compatible = "ti,tcan1042",
69 .data = &tcan1042_drvdata
70 },
71 {
72 .compatible = "ti,tcan1043",
73 .data = &tcan1043_drvdata
74 },
75 { }
76 };
77 MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
78
can_transceiver_phy_probe(struct platform_device * pdev)79 static int can_transceiver_phy_probe(struct platform_device *pdev)
80 {
81 struct phy_provider *phy_provider;
82 struct device *dev = &pdev->dev;
83 struct can_transceiver_phy *can_transceiver_phy;
84 const struct can_transceiver_data *drvdata;
85 const struct of_device_id *match;
86 struct phy *phy;
87 struct gpio_desc *standby_gpio;
88 struct gpio_desc *enable_gpio;
89 u32 max_bitrate = 0;
90 int err;
91
92 can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
93 if (!can_transceiver_phy)
94 return -ENOMEM;
95
96 match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
97 drvdata = match->data;
98
99 phy = devm_phy_create(dev, dev->of_node,
100 &can_transceiver_phy_ops);
101 if (IS_ERR(phy)) {
102 dev_err(dev, "failed to create can transceiver phy\n");
103 return PTR_ERR(phy);
104 }
105
106 err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
107 if ((err != -EINVAL) && !max_bitrate)
108 dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
109 phy->attrs.max_link_rate = max_bitrate;
110
111 can_transceiver_phy->generic_phy = phy;
112
113 if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
114 standby_gpio = devm_gpiod_get(dev, "standby", GPIOD_OUT_HIGH);
115 if (IS_ERR(standby_gpio))
116 return PTR_ERR(standby_gpio);
117 can_transceiver_phy->standby_gpio = standby_gpio;
118 }
119
120 if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
121 enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
122 if (IS_ERR(enable_gpio))
123 return PTR_ERR(enable_gpio);
124 can_transceiver_phy->enable_gpio = enable_gpio;
125 }
126
127 phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
128
129 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
130
131 return PTR_ERR_OR_ZERO(phy_provider);
132 }
133
134 static struct platform_driver can_transceiver_phy_driver = {
135 .probe = can_transceiver_phy_probe,
136 .driver = {
137 .name = "can-transceiver-phy",
138 .of_match_table = can_transceiver_phy_ids,
139 },
140 };
141
142 module_platform_driver(can_transceiver_phy_driver);
143
144 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
145 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
146 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
147 MODULE_LICENSE("GPL v2");
148