| 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 */ |
| 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(mstate: 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(desc: can_transceiver_phy->silent_gpio, value: 0); |
| 53 | gpiod_set_value_cansleep(desc: can_transceiver_phy->standby_gpio, value: 0); |
| 54 | gpiod_set_value_cansleep(desc: can_transceiver_phy->enable_gpio, value: 1); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* Power off function */ |
| 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(desc: can_transceiver_phy->silent_gpio, value: 1); |
| 66 | gpiod_set_value_cansleep(desc: can_transceiver_phy->standby_gpio, value: 1); |
| 67 | gpiod_set_value_cansleep(desc: can_transceiver_phy->enable_gpio, value: 0); |
| 68 | if (priv->mux_state) |
| 69 | mux_state_deselect(mstate: 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 | |
| 129 | /* Temporary wrapper until the multiplexer subsystem supports optional muxes */ |
| 130 | static inline struct mux_state * |
| 131 | devm_mux_state_get_optional(struct device *dev, const char *mux_name) |
| 132 | { |
| 133 | if (!of_property_present(np: dev->of_node, propname: "mux-states" )) |
| 134 | return NULL; |
| 135 | |
| 136 | return devm_mux_state_get(dev, mux_name); |
| 137 | } |
| 138 | |
| 139 | static struct phy *can_transceiver_phy_xlate(struct device *dev, |
| 140 | const struct of_phandle_args *args) |
| 141 | { |
| 142 | struct can_transceiver_priv *priv = dev_get_drvdata(dev); |
| 143 | u32 idx; |
| 144 | |
| 145 | if (priv->num_ch == 1) |
| 146 | return priv->can_transceiver_phy[0].generic_phy; |
| 147 | |
| 148 | if (args->args_count != 1) |
| 149 | return ERR_PTR(error: -EINVAL); |
| 150 | |
| 151 | idx = args->args[0]; |
| 152 | if (idx >= priv->num_ch) |
| 153 | return ERR_PTR(error: -EINVAL); |
| 154 | |
| 155 | return priv->can_transceiver_phy[idx].generic_phy; |
| 156 | } |
| 157 | |
| 158 | static int can_transceiver_phy_probe(struct platform_device *pdev) |
| 159 | { |
| 160 | struct phy_provider *phy_provider; |
| 161 | struct device *dev = &pdev->dev; |
| 162 | struct can_transceiver_phy *can_transceiver_phy; |
| 163 | struct can_transceiver_priv *priv; |
| 164 | const struct can_transceiver_data *drvdata; |
| 165 | const struct of_device_id *match; |
| 166 | struct phy *phy; |
| 167 | struct gpio_desc *silent_gpio; |
| 168 | struct gpio_desc *standby_gpio; |
| 169 | struct gpio_desc *enable_gpio; |
| 170 | struct mux_state *mux_state; |
| 171 | u32 max_bitrate = 0; |
| 172 | int err, i, num_ch = 1; |
| 173 | |
| 174 | match = of_match_node(matches: can_transceiver_phy_ids, node: pdev->dev.of_node); |
| 175 | drvdata = match->data; |
| 176 | if (drvdata->flags & CAN_TRANSCEIVER_DUAL_CH) |
| 177 | num_ch = 2; |
| 178 | |
| 179 | priv = devm_kzalloc(dev, struct_size(priv, can_transceiver_phy, num_ch), GFP_KERNEL); |
| 180 | if (!priv) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | priv->num_ch = num_ch; |
| 184 | platform_set_drvdata(pdev, data: priv); |
| 185 | |
| 186 | mux_state = devm_mux_state_get_optional(dev, NULL); |
| 187 | if (IS_ERR(ptr: mux_state)) |
| 188 | return PTR_ERR(ptr: mux_state); |
| 189 | |
| 190 | priv->mux_state = mux_state; |
| 191 | |
| 192 | err = device_property_read_u32(dev, propname: "max-bitrate" , val: &max_bitrate); |
| 193 | if ((err != -EINVAL) && !max_bitrate) |
| 194 | dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n" ); |
| 195 | |
| 196 | for (i = 0; i < num_ch; i++) { |
| 197 | can_transceiver_phy = &priv->can_transceiver_phy[i]; |
| 198 | can_transceiver_phy->priv = priv; |
| 199 | |
| 200 | phy = devm_phy_create(dev, node: dev->of_node, ops: &can_transceiver_phy_ops); |
| 201 | if (IS_ERR(ptr: phy)) { |
| 202 | dev_err(dev, "failed to create can transceiver phy\n" ); |
| 203 | return PTR_ERR(ptr: phy); |
| 204 | } |
| 205 | |
| 206 | phy->attrs.max_link_rate = max_bitrate; |
| 207 | |
| 208 | can_transceiver_phy->generic_phy = phy; |
| 209 | can_transceiver_phy->priv = priv; |
| 210 | |
| 211 | if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) { |
| 212 | standby_gpio = devm_gpiod_get_index_optional(dev, con_id: "standby" , index: i, |
| 213 | flags: GPIOD_OUT_HIGH); |
| 214 | if (IS_ERR(ptr: standby_gpio)) |
| 215 | return PTR_ERR(ptr: standby_gpio); |
| 216 | can_transceiver_phy->standby_gpio = standby_gpio; |
| 217 | } |
| 218 | |
| 219 | if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) { |
| 220 | enable_gpio = devm_gpiod_get_index_optional(dev, con_id: "enable" , index: i, |
| 221 | flags: GPIOD_OUT_LOW); |
| 222 | if (IS_ERR(ptr: enable_gpio)) |
| 223 | return PTR_ERR(ptr: enable_gpio); |
| 224 | can_transceiver_phy->enable_gpio = enable_gpio; |
| 225 | } |
| 226 | |
| 227 | if (drvdata->flags & CAN_TRANSCEIVER_SILENT_PRESENT) { |
| 228 | silent_gpio = devm_gpiod_get_index_optional(dev, con_id: "silent" , index: i, |
| 229 | flags: GPIOD_OUT_LOW); |
| 230 | if (IS_ERR(ptr: silent_gpio)) |
| 231 | return PTR_ERR(ptr: silent_gpio); |
| 232 | can_transceiver_phy->silent_gpio = silent_gpio; |
| 233 | } |
| 234 | |
| 235 | phy_set_drvdata(phy: can_transceiver_phy->generic_phy, data: can_transceiver_phy); |
| 236 | |
| 237 | } |
| 238 | |
| 239 | phy_provider = devm_of_phy_provider_register(dev, can_transceiver_phy_xlate); |
| 240 | |
| 241 | return PTR_ERR_OR_ZERO(ptr: phy_provider); |
| 242 | } |
| 243 | |
| 244 | static struct platform_driver can_transceiver_phy_driver = { |
| 245 | .probe = can_transceiver_phy_probe, |
| 246 | .driver = { |
| 247 | .name = "can-transceiver-phy" , |
| 248 | .of_match_table = can_transceiver_phy_ids, |
| 249 | }, |
| 250 | }; |
| 251 | |
| 252 | module_platform_driver(can_transceiver_phy_driver); |
| 253 | |
| 254 | MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>" ); |
| 255 | MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>" ); |
| 256 | MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver" ); |
| 257 | MODULE_LICENSE("GPL v2" ); |
| 258 | |