extcon-max3355.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Maxim Integrated MAX3355 USB OTG chip extcon driver
  4. *
  5. * Copyright (C) 2014-2015 Cogent Embedded, Inc.
  6. * Author: Sergei Shtylyov <[email protected]>
  7. */
  8. #include <linux/extcon-provider.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/platform_device.h>
  14. struct max3355_data {
  15. struct extcon_dev *edev;
  16. struct gpio_desc *id_gpiod;
  17. struct gpio_desc *shdn_gpiod;
  18. };
  19. static const unsigned int max3355_cable[] = {
  20. EXTCON_USB,
  21. EXTCON_USB_HOST,
  22. EXTCON_NONE,
  23. };
  24. static irqreturn_t max3355_id_irq(int irq, void *dev_id)
  25. {
  26. struct max3355_data *data = dev_id;
  27. int id = gpiod_get_value_cansleep(data->id_gpiod);
  28. if (id) {
  29. /*
  30. * ID = 1 means USB HOST cable detached.
  31. * As we don't have event for USB peripheral cable attached,
  32. * we simulate USB peripheral attach here.
  33. */
  34. extcon_set_state_sync(data->edev, EXTCON_USB_HOST, false);
  35. extcon_set_state_sync(data->edev, EXTCON_USB, true);
  36. } else {
  37. /*
  38. * ID = 0 means USB HOST cable attached.
  39. * As we don't have event for USB peripheral cable detached,
  40. * we simulate USB peripheral detach here.
  41. */
  42. extcon_set_state_sync(data->edev, EXTCON_USB, false);
  43. extcon_set_state_sync(data->edev, EXTCON_USB_HOST, true);
  44. }
  45. return IRQ_HANDLED;
  46. }
  47. static int max3355_probe(struct platform_device *pdev)
  48. {
  49. struct max3355_data *data;
  50. struct gpio_desc *gpiod;
  51. int irq, err;
  52. data = devm_kzalloc(&pdev->dev, sizeof(struct max3355_data),
  53. GFP_KERNEL);
  54. if (!data)
  55. return -ENOMEM;
  56. gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  57. if (IS_ERR(gpiod)) {
  58. dev_err(&pdev->dev, "failed to get ID_OUT GPIO\n");
  59. return PTR_ERR(gpiod);
  60. }
  61. data->id_gpiod = gpiod;
  62. gpiod = devm_gpiod_get(&pdev->dev, "maxim,shdn", GPIOD_OUT_HIGH);
  63. if (IS_ERR(gpiod)) {
  64. dev_err(&pdev->dev, "failed to get SHDN# GPIO\n");
  65. return PTR_ERR(gpiod);
  66. }
  67. data->shdn_gpiod = gpiod;
  68. data->edev = devm_extcon_dev_allocate(&pdev->dev, max3355_cable);
  69. if (IS_ERR(data->edev)) {
  70. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  71. return PTR_ERR(data->edev);
  72. }
  73. err = devm_extcon_dev_register(&pdev->dev, data->edev);
  74. if (err < 0) {
  75. dev_err(&pdev->dev, "failed to register extcon device\n");
  76. return err;
  77. }
  78. irq = gpiod_to_irq(data->id_gpiod);
  79. if (irq < 0) {
  80. dev_err(&pdev->dev, "failed to translate ID_OUT GPIO to IRQ\n");
  81. return irq;
  82. }
  83. err = devm_request_threaded_irq(&pdev->dev, irq, NULL, max3355_id_irq,
  84. IRQF_ONESHOT | IRQF_NO_SUSPEND |
  85. IRQF_TRIGGER_RISING |
  86. IRQF_TRIGGER_FALLING,
  87. pdev->name, data);
  88. if (err < 0) {
  89. dev_err(&pdev->dev, "failed to request ID_OUT IRQ\n");
  90. return err;
  91. }
  92. platform_set_drvdata(pdev, data);
  93. /* Perform initial detection */
  94. max3355_id_irq(irq, data);
  95. return 0;
  96. }
  97. static int max3355_remove(struct platform_device *pdev)
  98. {
  99. struct max3355_data *data = platform_get_drvdata(pdev);
  100. gpiod_set_value_cansleep(data->shdn_gpiod, 0);
  101. return 0;
  102. }
  103. static const struct of_device_id max3355_match_table[] = {
  104. { .compatible = "maxim,max3355", },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(of, max3355_match_table);
  108. static struct platform_driver max3355_driver = {
  109. .probe = max3355_probe,
  110. .remove = max3355_remove,
  111. .driver = {
  112. .name = "extcon-max3355",
  113. .of_match_table = max3355_match_table,
  114. },
  115. };
  116. module_platform_driver(max3355_driver);
  117. MODULE_AUTHOR("Sergei Shtylyov <[email protected]>");
  118. MODULE_DESCRIPTION("Maxim MAX3355 extcon driver");
  119. MODULE_LICENSE("GPL v2");