xref: /linux/drivers/phy/phy-can-transceiver.c (revision 21eeefe76919c904dd50d543bd6d3eee05d97e15)
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 };
21 
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;
27 };
28 
29 /* Power on function */
can_transceiver_phy_power_on(struct phy * phy)30 static int can_transceiver_phy_power_on(struct phy *phy)
31 {
32 	struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
33 	int ret;
34 
35 	if (can_transceiver_phy->mux_state) {
36 		ret = mux_state_select(can_transceiver_phy->mux_state);
37 		if (ret) {
38 			dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
39 			return ret;
40 		}
41 	}
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);
46 
47 	return 0;
48 }
49 
50 /* Power off function */
can_transceiver_phy_power_off(struct phy * phy)51 static int can_transceiver_phy_power_off(struct phy *phy)
52 {
53 	struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
54 
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);
61 
62 	return 0;
63 }
64 
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,
68 	.owner		= THIS_MODULE,
69 };
70 
71 static const struct can_transceiver_data tcan1042_drvdata = {
72 	.flags = CAN_TRANSCEIVER_STB_PRESENT,
73 };
74 
75 static const struct can_transceiver_data tcan1043_drvdata = {
76 	.flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
77 };
78 
79 static const struct of_device_id can_transceiver_phy_ids[] = {
80 	{
81 		.compatible = "ti,tcan1042",
82 		.data = &tcan1042_drvdata
83 	},
84 	{
85 		.compatible = "ti,tcan1043",
86 		.data = &tcan1043_drvdata
87 	},
88 	{
89 		.compatible = "nxp,tjr1443",
90 		.data = &tcan1043_drvdata
91 	},
92 	{ }
93 };
94 MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
95 
96 /* Temporary wrapper until the multiplexer subsystem supports optional muxes */
97 static inline struct mux_state *
devm_mux_state_get_optional(struct device * dev,const char * mux_name)98 devm_mux_state_get_optional(struct device *dev, const char *mux_name)
99 {
100 	if (!of_property_present(dev->of_node, "mux-states"))
101 		return NULL;
102 
103 	return devm_mux_state_get(dev, mux_name);
104 }
105 
can_transceiver_phy_probe(struct platform_device * pdev)106 static int can_transceiver_phy_probe(struct platform_device *pdev)
107 {
108 	struct phy_provider *phy_provider;
109 	struct device *dev = &pdev->dev;
110 	struct can_transceiver_phy *can_transceiver_phy;
111 	const struct can_transceiver_data *drvdata;
112 	const struct of_device_id *match;
113 	struct phy *phy;
114 	struct gpio_desc *standby_gpio;
115 	struct gpio_desc *enable_gpio;
116 	struct mux_state *mux_state;
117 	u32 max_bitrate = 0;
118 	int err;
119 
120 	can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
121 	if (!can_transceiver_phy)
122 		return -ENOMEM;
123 
124 	match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
125 	drvdata = match->data;
126 
127 	mux_state = devm_mux_state_get_optional(dev, NULL);
128 	if (IS_ERR(mux_state))
129 		return PTR_ERR(mux_state);
130 
131 	can_transceiver_phy->mux_state = mux_state;
132 
133 	phy = devm_phy_create(dev, dev->of_node,
134 			      &can_transceiver_phy_ops);
135 	if (IS_ERR(phy)) {
136 		dev_err(dev, "failed to create can transceiver phy\n");
137 		return PTR_ERR(phy);
138 	}
139 
140 	err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
141 	if ((err != -EINVAL) && !max_bitrate)
142 		dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
143 	phy->attrs.max_link_rate = max_bitrate;
144 
145 	can_transceiver_phy->generic_phy = phy;
146 
147 	if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
148 		standby_gpio = devm_gpiod_get_optional(dev, "standby", GPIOD_OUT_HIGH);
149 		if (IS_ERR(standby_gpio))
150 			return PTR_ERR(standby_gpio);
151 		can_transceiver_phy->standby_gpio = standby_gpio;
152 	}
153 
154 	if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
155 		enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
156 		if (IS_ERR(enable_gpio))
157 			return PTR_ERR(enable_gpio);
158 		can_transceiver_phy->enable_gpio = enable_gpio;
159 	}
160 
161 	phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
162 
163 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
164 
165 	return PTR_ERR_OR_ZERO(phy_provider);
166 }
167 
168 static struct platform_driver can_transceiver_phy_driver = {
169 	.probe = can_transceiver_phy_probe,
170 	.driver = {
171 		.name = "can-transceiver-phy",
172 		.of_match_table = can_transceiver_phy_ids,
173 	},
174 };
175 
176 module_platform_driver(can_transceiver_phy_driver);
177 
178 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
179 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
180 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
181 MODULE_LICENSE("GPL v2");
182