blob: 5487b9dd1ead1aca1a650edfc0d0e85c576087dc [file] [log] [blame]
Aswath Govindrajua4a86d22021-05-10 10:40:05 +05301// 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>
Aswath Govindrajue4d43712022-04-08 16:43:16 +053013#include <linux/mux/consumer.h>
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053014
15struct can_transceiver_data {
16 u32 flags;
17#define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
18#define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
19};
20
21struct can_transceiver_phy {
22 struct phy *generic_phy;
23 struct gpio_desc *standby_gpio;
24 struct gpio_desc *enable_gpio;
Aswath Govindrajue4d43712022-04-08 16:43:16 +053025 struct mux_state *mux_state;
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053026};
27
28/* Power on function */
29static int can_transceiver_phy_power_on(struct phy *phy)
30{
31 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
Aswath Govindrajue4d43712022-04-08 16:43:16 +053032 int ret;
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053033
Aswath Govindrajue4d43712022-04-08 16:43:16 +053034 if (can_transceiver_phy->mux_state) {
35 ret = mux_state_select(can_transceiver_phy->mux_state);
36 if (ret) {
37 dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
38 return ret;
39 }
40 }
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053041 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);
45
46 return 0;
47}
48
49/* Power off function */
50static int can_transceiver_phy_power_off(struct phy *phy)
51{
52 struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
53
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);
Aswath Govindrajue4d43712022-04-08 16:43:16 +053058 if (can_transceiver_phy->mux_state)
59 mux_state_deselect(can_transceiver_phy->mux_state);
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053060
61 return 0;
62}
63
64static const struct phy_ops can_transceiver_phy_ops = {
65 .power_on = can_transceiver_phy_power_on,
66 .power_off = can_transceiver_phy_power_off,
67 .owner = THIS_MODULE,
68};
69
70static const struct can_transceiver_data tcan1042_drvdata = {
71 .flags = CAN_TRANSCEIVER_STB_PRESENT,
72};
73
74static const struct can_transceiver_data tcan1043_drvdata = {
75 .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
76};
77
78static const struct of_device_id can_transceiver_phy_ids[] = {
79 {
80 .compatible = "ti,tcan1042",
81 .data = &tcan1042_drvdata
82 },
83 {
84 .compatible = "ti,tcan1043",
85 .data = &tcan1043_drvdata
86 },
Geert Uytterhoevenb41499a2023-01-18 11:39:24 +010087 {
88 .compatible = "nxp,tjr1443",
89 .data = &tcan1043_drvdata
90 },
Aswath Govindrajua4a86d22021-05-10 10:40:05 +053091 { }
92};
93MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
94
95static int can_transceiver_phy_probe(struct platform_device *pdev)
96{
97 struct phy_provider *phy_provider;
98 struct device *dev = &pdev->dev;
99 struct can_transceiver_phy *can_transceiver_phy;
100 const struct can_transceiver_data *drvdata;
101 const struct of_device_id *match;
102 struct phy *phy;
103 struct gpio_desc *standby_gpio;
104 struct gpio_desc *enable_gpio;
105 u32 max_bitrate = 0;
Geert Uytterhoevenbc30c15f2023-01-18 11:29:58 +0100106 int err;
Aswath Govindrajua4a86d22021-05-10 10:40:05 +0530107
108 can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
109 if (!can_transceiver_phy)
110 return -ENOMEM;
111
112 match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
113 drvdata = match->data;
114
Aswath Govindrajue4d43712022-04-08 16:43:16 +0530115 if (of_property_read_bool(dev->of_node, "mux-states")) {
116 struct mux_state *mux_state;
117
118 mux_state = devm_mux_state_get(dev, NULL);
119 if (IS_ERR(mux_state))
120 return dev_err_probe(&pdev->dev, PTR_ERR(mux_state),
121 "failed to get mux\n");
122 can_transceiver_phy->mux_state = mux_state;
123 }
124
Aswath Govindrajua4a86d22021-05-10 10:40:05 +0530125 phy = devm_phy_create(dev, dev->of_node,
126 &can_transceiver_phy_ops);
127 if (IS_ERR(phy)) {
128 dev_err(dev, "failed to create can transceiver phy\n");
129 return PTR_ERR(phy);
130 }
131
Geert Uytterhoevenbc30c15f2023-01-18 11:29:58 +0100132 err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
133 if ((err != -EINVAL) && !max_bitrate)
Aswath Govindrajua4a86d22021-05-10 10:40:05 +0530134 dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
135 phy->attrs.max_link_rate = max_bitrate;
136
137 can_transceiver_phy->generic_phy = phy;
138
139 if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
Aswath Govindrajube24d2482021-11-02 16:51:20 +0530140 standby_gpio = devm_gpiod_get_optional(dev, "standby", GPIOD_OUT_HIGH);
Aswath Govindrajua4a86d22021-05-10 10:40:05 +0530141 if (IS_ERR(standby_gpio))
142 return PTR_ERR(standby_gpio);
143 can_transceiver_phy->standby_gpio = standby_gpio;
144 }
145
146 if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
Aswath Govindrajube24d2482021-11-02 16:51:20 +0530147 enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
Aswath Govindrajua4a86d22021-05-10 10:40:05 +0530148 if (IS_ERR(enable_gpio))
149 return PTR_ERR(enable_gpio);
150 can_transceiver_phy->enable_gpio = enable_gpio;
151 }
152
153 phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
154
155 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
156
157 return PTR_ERR_OR_ZERO(phy_provider);
158}
159
160static struct platform_driver can_transceiver_phy_driver = {
161 .probe = can_transceiver_phy_probe,
162 .driver = {
163 .name = "can-transceiver-phy",
164 .of_match_table = can_transceiver_phy_ids,
165 },
166};
167
168module_platform_driver(can_transceiver_phy_driver);
169
170MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
171MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
172MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
173MODULE_LICENSE("GPL v2");