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