extcon-max3355.c 3.7 KB

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