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/of.h>
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>
15
16 struct can_transceiver_data {
17 u32 flags;
18 #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
19 #define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
20 #define CAN_TRANSCEIVER_DUAL_CH BIT(2)
21 #define CAN_TRANSCEIVER_SILENT_PRESENT BIT(3)
22 };
23
24 struct can_transceiver_phy {
25 struct phy *generic_phy;
26 struct gpio_desc *silent_gpio;
27 struct gpio_desc *standby_gpio;
28 struct gpio_desc *enable_gpio;
29 struct can_transceiver_priv *priv;
30 };
31
32 struct can_transceiver_priv {
33 struct mux_state *mux_state;
34 int num_ch;
35 struct can_transceiver_phy can_transceiver_phy[] __counted_by(num_ch);
36 };
37
38 /* Power on function */
can_transceiver_phy_power_on(struct phy * phy)39 static int can_transceiver_phy_power_on(struct phy *phy)
40 {
41 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
42 struct can_transceiver_priv *priv = can_transceiver_phy->priv;
43 int ret;
44
45 if (priv->mux_state) {
46 ret = mux_state_select(priv->mux_state);
47 if (ret) {
48 dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
49 return ret;
50 }
51 }
52 gpiod_set_value_cansleep(can_transceiver_phy->silent_gpio, 0);
53 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
54 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 1);
55
56 return 0;
57 }
58
59 /* Power off function */
can_transceiver_phy_power_off(struct phy * phy)60 static int can_transceiver_phy_power_off(struct phy *phy)
61 {
62 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
63 struct can_transceiver_priv *priv = can_transceiver_phy->priv;
64
65 gpiod_set_value_cansleep(can_transceiver_phy->silent_gpio, 1);
66 gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
67 gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
68 if (priv->mux_state)
69 mux_state_deselect(priv->mux_state);
70
71 return 0;
72 }
73
74 static const struct phy_ops can_transceiver_phy_ops = {
75 .power_on = can_transceiver_phy_power_on,
76 .power_off = can_transceiver_phy_power_off,
77 .owner = THIS_MODULE,
78 };
79
80 static const struct can_transceiver_data tcan1042_drvdata = {
81 .flags = CAN_TRANSCEIVER_STB_PRESENT,
82 };
83
84 static const struct can_transceiver_data tcan1043_drvdata = {
85 .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
86 };
87
88 static const struct can_transceiver_data tja1048_drvdata = {
89 .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_DUAL_CH,
90 };
91
92 static const struct can_transceiver_data tja1051_drvdata = {
93 .flags = CAN_TRANSCEIVER_SILENT_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
94 };
95
96 static const struct can_transceiver_data tja1057_drvdata = {
97 .flags = CAN_TRANSCEIVER_SILENT_PRESENT,
98 };
99
100 static const struct of_device_id can_transceiver_phy_ids[] = {
101 {
102 .compatible = "ti,tcan1042",
103 .data = &tcan1042_drvdata
104 },
105 {
106 .compatible = "ti,tcan1043",
107 .data = &tcan1043_drvdata
108 },
109 {
110 .compatible = "nxp,tja1048",
111 .data = &tja1048_drvdata
112 },
113 {
114 .compatible = "nxp,tja1051",
115 .data = &tja1051_drvdata
116 },
117 {
118 .compatible = "nxp,tja1057",
119 .data = &tja1057_drvdata
120 },
121 {
122 .compatible = "nxp,tjr1443",
123 .data = &tcan1043_drvdata
124 },
125 { }
126 };
127 MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
128
can_transceiver_phy_xlate(struct device * dev,const struct of_phandle_args * args)129 static struct phy *can_transceiver_phy_xlate(struct device *dev,
130 const struct of_phandle_args *args)
131 {
132 struct can_transceiver_priv *priv = dev_get_drvdata(dev);
133 u32 idx;
134
135 if (priv->num_ch == 1)
136 return priv->can_transceiver_phy[0].generic_phy;
137
138 if (args->args_count != 1)
139 return ERR_PTR(-EINVAL);
140
141 idx = args->args[0];
142 if (idx >= priv->num_ch)
143 return ERR_PTR(-EINVAL);
144
145 return priv->can_transceiver_phy[idx].generic_phy;
146 }
147
can_transceiver_phy_probe(struct platform_device * pdev)148 static int can_transceiver_phy_probe(struct platform_device *pdev)
149 {
150 struct phy_provider *phy_provider;
151 struct device *dev = &pdev->dev;
152 struct can_transceiver_phy *can_transceiver_phy;
153 struct can_transceiver_priv *priv;
154 const struct can_transceiver_data *drvdata;
155 const struct of_device_id *match;
156 struct phy *phy;
157 struct gpio_desc *silent_gpio;
158 struct gpio_desc *standby_gpio;
159 struct gpio_desc *enable_gpio;
160 struct mux_state *mux_state;
161 u32 max_bitrate = 0;
162 int err, i, num_ch = 1;
163
164 match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
165 drvdata = match->data;
166 if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH)
167 num_ch = 2;
168
169 priv = devm_kzalloc(dev, struct_size(priv, can_transceiver_phy, num_ch), GFP_KERNEL);
170 if (!priv)
171 return -ENOMEM;
172
173 priv->num_ch = num_ch;
174 platform_set_drvdata(pdev, priv);
175
176 mux_state = devm_mux_state_get_optional(dev, NULL);
177 if (IS_ERR(mux_state))
178 return PTR_ERR(mux_state);
179
180 priv->mux_state = mux_state;
181
182 err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
183 if ((err != -EINVAL) && !max_bitrate)
184 dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
185
186 for (i = 0; i < num_ch; i++) {
187 can_transceiver_phy = &priv->can_transceiver_phy[i];
188 can_transceiver_phy->priv = priv;
189
190 phy = devm_phy_create(dev, dev->of_node, &can_transceiver_phy_ops);
191 if (IS_ERR(phy)) {
192 dev_err(dev, "failed to create can transceiver phy\n");
193 return PTR_ERR(phy);
194 }
195
196 phy->attrs.max_link_rate = max_bitrate;
197
198 can_transceiver_phy->generic_phy = phy;
199 can_transceiver_phy->priv = priv;
200
201 if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
202 standby_gpio = devm_gpiod_get_index_optional(dev, "standby", i,
203 GPIOD_OUT_HIGH);
204 if (IS_ERR(standby_gpio))
205 return PTR_ERR(standby_gpio);
206 can_transceiver_phy->standby_gpio = standby_gpio;
207 }
208
209 if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
210 enable_gpio = devm_gpiod_get_index_optional(dev, "enable", i,
211 GPIOD_OUT_LOW);
212 if (IS_ERR(enable_gpio))
213 return PTR_ERR(enable_gpio);
214 can_transceiver_phy->enable_gpio = enable_gpio;
215 }
216
217 if (drvdata->flags & CAN_TRANSCEIVER_SILENT_PRESENT) {
218 silent_gpio = devm_gpiod_get_index_optional(dev, "silent", i,
219 GPIOD_OUT_LOW);
220 if (IS_ERR(silent_gpio))
221 return PTR_ERR(silent_gpio);
222 can_transceiver_phy->silent_gpio = silent_gpio;
223 }
224
225 phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
226
227 }
228
229 phy_provider = devm_of_phy_provider_register(dev, can_transceiver_phy_xlate);
230
231 return PTR_ERR_OR_ZERO(phy_provider);
232 }
233
234 static struct platform_driver can_transceiver_phy_driver = {
235 .probe = can_transceiver_phy_probe,
236 .driver = {
237 .name = "can-transceiver-phy",
238 .of_match_table = can_transceiver_phy_ids,
239 },
240 };
241
242 module_platform_driver(can_transceiver_phy_driver);
243
244 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
245 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
246 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
247 MODULE_LICENSE("GPL v2");
248