phy-can-transceiver.c 4.7 KB

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