phy-can-transceiver.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct can_transceiver_data {
  16. u32 flags;
  17. #define CAN_TRANSCEIVER_STB_PRESENT BIT(0)
  18. #define CAN_TRANSCEIVER_EN_PRESENT BIT(1)
  19. };
  20. struct can_transceiver_phy {
  21. struct phy *generic_phy;
  22. struct gpio_desc *standby_gpio;
  23. struct gpio_desc *enable_gpio;
  24. struct mux_state *mux_state;
  25. };
  26. /* Power on function */
  27. static int can_transceiver_phy_power_on(struct phy *phy)
  28. {
  29. struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
  30. int ret;
  31. if (can_transceiver_phy->mux_state) {
  32. ret = mux_state_select(can_transceiver_phy->mux_state);
  33. if (ret) {
  34. dev_err(&phy->dev, "Failed to select CAN mux: %d\n", ret);
  35. return ret;
  36. }
  37. }
  38. if (can_transceiver_phy->standby_gpio)
  39. gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 0);
  40. if (can_transceiver_phy->enable_gpio)
  41. gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 1);
  42. return 0;
  43. }
  44. /* Power off function */
  45. static int can_transceiver_phy_power_off(struct phy *phy)
  46. {
  47. struct can_transceiver_phy *can_transceiver_phy = phy_get_drvdata(phy);
  48. if (can_transceiver_phy->standby_gpio)
  49. gpiod_set_value_cansleep(can_transceiver_phy->standby_gpio, 1);
  50. if (can_transceiver_phy->enable_gpio)
  51. gpiod_set_value_cansleep(can_transceiver_phy->enable_gpio, 0);
  52. if (can_transceiver_phy->mux_state)
  53. mux_state_deselect(can_transceiver_phy->mux_state);
  54. return 0;
  55. }
  56. static const struct phy_ops can_transceiver_phy_ops = {
  57. .power_on = can_transceiver_phy_power_on,
  58. .power_off = can_transceiver_phy_power_off,
  59. .owner = THIS_MODULE,
  60. };
  61. static const struct can_transceiver_data tcan1042_drvdata = {
  62. .flags = CAN_TRANSCEIVER_STB_PRESENT,
  63. };
  64. static const struct can_transceiver_data tcan1043_drvdata = {
  65. .flags = CAN_TRANSCEIVER_STB_PRESENT | CAN_TRANSCEIVER_EN_PRESENT,
  66. };
  67. static const struct of_device_id can_transceiver_phy_ids[] = {
  68. {
  69. .compatible = "ti,tcan1042",
  70. .data = &tcan1042_drvdata
  71. },
  72. {
  73. .compatible = "ti,tcan1043",
  74. .data = &tcan1043_drvdata
  75. },
  76. {
  77. .compatible = "nxp,tjr1443",
  78. .data = &tcan1043_drvdata
  79. },
  80. { }
  81. };
  82. MODULE_DEVICE_TABLE(of, can_transceiver_phy_ids);
  83. static int can_transceiver_phy_probe(struct platform_device *pdev)
  84. {
  85. struct phy_provider *phy_provider;
  86. struct device *dev = &pdev->dev;
  87. struct can_transceiver_phy *can_transceiver_phy;
  88. const struct can_transceiver_data *drvdata;
  89. const struct of_device_id *match;
  90. struct phy *phy;
  91. struct gpio_desc *standby_gpio;
  92. struct gpio_desc *enable_gpio;
  93. u32 max_bitrate = 0;
  94. int err;
  95. can_transceiver_phy = devm_kzalloc(dev, sizeof(struct can_transceiver_phy), GFP_KERNEL);
  96. if (!can_transceiver_phy)
  97. return -ENOMEM;
  98. match = of_match_node(can_transceiver_phy_ids, pdev->dev.of_node);
  99. drvdata = match->data;
  100. if (of_property_read_bool(dev->of_node, "mux-states")) {
  101. struct mux_state *mux_state;
  102. mux_state = devm_mux_state_get(dev, NULL);
  103. if (IS_ERR(mux_state))
  104. return dev_err_probe(&pdev->dev, PTR_ERR(mux_state),
  105. "failed to get mux\n");
  106. can_transceiver_phy->mux_state = mux_state;
  107. }
  108. phy = devm_phy_create(dev, dev->of_node,
  109. &can_transceiver_phy_ops);
  110. if (IS_ERR(phy)) {
  111. dev_err(dev, "failed to create can transceiver phy\n");
  112. return PTR_ERR(phy);
  113. }
  114. err = device_property_read_u32(dev, "max-bitrate", &max_bitrate);
  115. if ((err != -EINVAL) && !max_bitrate)
  116. dev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit\n");
  117. phy->attrs.max_link_rate = max_bitrate;
  118. can_transceiver_phy->generic_phy = phy;
  119. if (drvdata->flags & CAN_TRANSCEIVER_STB_PRESENT) {
  120. standby_gpio = devm_gpiod_get_optional(dev, "standby", GPIOD_OUT_HIGH);
  121. if (IS_ERR(standby_gpio))
  122. return PTR_ERR(standby_gpio);
  123. can_transceiver_phy->standby_gpio = standby_gpio;
  124. }
  125. if (drvdata->flags & CAN_TRANSCEIVER_EN_PRESENT) {
  126. enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
  127. if (IS_ERR(enable_gpio))
  128. return PTR_ERR(enable_gpio);
  129. can_transceiver_phy->enable_gpio = enable_gpio;
  130. }
  131. phy_set_drvdata(can_transceiver_phy->generic_phy, can_transceiver_phy);
  132. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  133. return PTR_ERR_OR_ZERO(phy_provider);
  134. }
  135. static struct platform_driver can_transceiver_phy_driver = {
  136. .probe = can_transceiver_phy_probe,
  137. .driver = {
  138. .name = "can-transceiver-phy",
  139. .of_match_table = can_transceiver_phy_ids,
  140. },
  141. };
  142. module_platform_driver(can_transceiver_phy_driver);
  143. MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
  144. MODULE_AUTHOR("Aswath Govindraju <a-govindraju@ti.com>");
  145. MODULE_DESCRIPTION("CAN TRANSCEIVER PHY driver");
  146. MODULE_LICENSE("GPL v2");