extcon-max3355.c 3.5 KB

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