Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * phy-can-transceiver.c - phy driver for CAN transceivers
0004  *
0005  * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com
0006  *
0007  */
0008 #include<linux/phy/phy.h>
0009 #include<linux/platform_device.h>
0010 #include<linux/module.h>
0011 #include<linux/gpio.h>
0012 #include<linux/gpio/consumer.h>
0013 #include <linux/mux/consumer.h>
0014 
0015 struct can_transceiver_data {
0016     u32 flags;
0017 #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
0018 #define CAN_TRANSCEIVER_EN_PRESENT  BIT(1)
0019 };
0020 
0021 struct can_transceiver_phy {
0022     struct phy *generic_phy;
0023     struct gpio_desc *standby_gpio;
0024     struct gpio_desc *enable_gpio;
0025     struct mux_state *mux_state;
0026 };
0027 
0028 /* Power on function */
0029 static int can_transceiver_phy_power_on(struct phy *phy)
0030 {
0031     struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
0032     int ret;
0033 
0034     if (can_transceiver_phy->mux_state) {
0035         ret = mux_state_select(can_transceiver_phy->mux_state);
0036         if (ret) {
0037             dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
0038             return ret;
0039         }
0040     }
0041     if (can_transceiver_phy->standby_gpio)
0042         gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
0043     if (can_transceiver_phy->enable_gpio)
0044         gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 1);
0045 
0046     return 0;
0047 }
0048 
0049 /* Power off function */
0050 static int can_transceiver_phy_power_off(struct phy *phy)
0051 {
0052     struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
0053 
0054     if (can_transceiver_phy->standby_gpio)
0055         gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
0056     if (can_transceiver_phy->enable_gpio)
0057         gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
0058     if (can_transceiver_phy->mux_state)
0059         mux_state_deselect(can_transceiver_phy->mux_state);
0060 
0061     return 0;
0062 }
0063 
0064 static const struct phy_ops can_transceiver_phy_ops = {
0065     .power_on   = can_transceiver_phy_power_on,
0066     .power_off  = can_transceiver_phy_power_off,
0067     .owner      = THIS_MODULE,
0068 };
0069 
0070 static const struct can_transceiver_data tcan1042_drvdata = {
0071     .flags = CAN_TRANSCEIVER_STB_PRESENT,
0072 };
0073 
0074 static const struct can_transceiver_data tcan1043_drvdata = {
0075     .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
0076 };
0077 
0078 static const struct of_device_id can_transceiver_phy_ids[] = {
0079     {
0080         .compatible = "ti,tcan1042",
0081         .data = &tcan1042_drvdata
0082     },
0083     {
0084         .compatible = "ti,tcan1043",
0085         .data = &tcan1043_drvdata
0086     },
0087     { }
0088 };
0089 MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
0090 
0091 static int can_transceiver_phy_probe(struct platform_device *pdev)
0092 {
0093     struct phy_provider *phy_provider;
0094     struct device *dev = &pdev->dev;
0095     struct can_transceiver_phy *can_transceiver_phy;
0096     const struct can_transceiver_data *drvdata;
0097     const struct of_device_id *match;
0098     struct phy *phy;
0099     struct gpio_desc *standby_gpio;
0100     struct gpio_desc *enable_gpio;
0101     u32 max_bitrate = 0;
0102 
0103     can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
0104     if (!can_transceiver_phy)
0105         return -ENOMEM;
0106 
0107     match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
0108     drvdata = match->data;
0109 
0110     if (of_property_read_bool(dev->of_node, "mux-states")) {
0111         struct mux_state *mux_state;
0112 
0113         mux_state = devm_mux_state_get(dev, NULL);
0114         if (IS_ERR(mux_state))
0115             return dev_err_probe(&pdev->dev, PTR_ERR(mux_state),
0116                          "failed to get mux\n");
0117         can_transceiver_phy->mux_state = mux_state;
0118     }
0119 
0120     phy = devm_phy_create(dev, dev->of_node,
0121                   &can_transceiver_phy_ops);
0122     if (IS_ERR(phy)) {
0123         dev_err(dev, "failed to create can transceiver phy\n");
0124         return PTR_ERR(phy);
0125     }
0126 
0127     device_property_read_u32(dev, "max-bitrate", &max_bitrate);
0128     if (!max_bitrate)
0129         dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
0130     phy->attrs.max_link_rate = max_bitrate;
0131 
0132     can_transceiver_phy->generic_phy = phy;
0133 
0134     if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
0135         standby_gpio = devm_gpiod_get_optional(dev, "standby", GPIOD_OUT_HIGH);
0136         if (IS_ERR(standby_gpio))
0137             return PTR_ERR(standby_gpio);
0138         can_transceiver_phy->standby_gpio = standby_gpio;
0139     }
0140 
0141     if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
0142         enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
0143         if (IS_ERR(enable_gpio))
0144             return PTR_ERR(enable_gpio);
0145         can_transceiver_phy->enable_gpio = enable_gpio;
0146     }
0147 
0148     phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
0149 
0150     phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
0151 
0152     return PTR_ERR_OR_ZERO(phy_provider);
0153 }
0154 
0155 static struct platform_driver can_transceiver_phy_driver = {
0156     .probe = can_transceiver_phy_probe,
0157     .driver = {
0158         .name = "can-transceiver-phy",
0159         .of_match_table = can_transceiver_phy_ids,
0160     },
0161 };
0162 
0163 module_platform_driver(can_transceiver_phy_driver);
0164 
0165 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
0166 MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
0167 MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
0168 MODULE_LICENSE("GPL v2");