phy-can-transceiver.c 6.9 KB

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