extcon-max3355.c 3.6 KB

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