rfkill-gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2011, NVIDIA Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/gpio.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/rfkill.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/clk.h>
  25. #include <linux/slab.h>
  26. #include <linux/acpi.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/rfkill-gpio.h>
  29. struct rfkill_gpio_data {
  30. const char *name;
  31. enum rfkill_type type;
  32. struct gpio_desc *reset_gpio;
  33. struct gpio_desc *shutdown_gpio;
  34. struct rfkill *rfkill_dev;
  35. struct clk *clk;
  36. bool clk_enabled;
  37. };
  38. static int rfkill_gpio_set_power(void *data, bool blocked)
  39. {
  40. struct rfkill_gpio_data *rfkill = data;
  41. if (!blocked && !IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
  42. clk_enable(rfkill->clk);
  43. gpiod_set_value_cansleep(rfkill->shutdown_gpio, !blocked);
  44. gpiod_set_value_cansleep(rfkill->reset_gpio, !blocked);
  45. if (blocked && !IS_ERR(rfkill->clk) && rfkill->clk_enabled)
  46. clk_disable(rfkill->clk);
  47. rfkill->clk_enabled = !blocked;
  48. return 0;
  49. }
  50. static const struct rfkill_ops rfkill_gpio_ops = {
  51. .set_block = rfkill_gpio_set_power,
  52. };
  53. static int rfkill_gpio_acpi_probe(struct device *dev,
  54. struct rfkill_gpio_data *rfkill)
  55. {
  56. const struct acpi_device_id *id;
  57. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  58. if (!id)
  59. return -ENODEV;
  60. rfkill->name = dev_name(dev);
  61. rfkill->type = (unsigned)id->driver_data;
  62. return 0;
  63. }
  64. static int rfkill_gpio_probe(struct platform_device *pdev)
  65. {
  66. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  67. struct rfkill_gpio_data *rfkill;
  68. struct gpio_desc *gpio;
  69. int ret;
  70. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  71. if (!rfkill)
  72. return -ENOMEM;
  73. if (ACPI_HANDLE(&pdev->dev)) {
  74. ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
  75. if (ret)
  76. return ret;
  77. } else if (pdata) {
  78. rfkill->name = pdata->name;
  79. rfkill->type = pdata->type;
  80. } else {
  81. return -ENODEV;
  82. }
  83. rfkill->clk = devm_clk_get(&pdev->dev, NULL);
  84. gpio = devm_gpiod_get_index(&pdev->dev, "reset", 0);
  85. if (!IS_ERR(gpio)) {
  86. ret = gpiod_direction_output(gpio, 0);
  87. if (ret)
  88. return ret;
  89. rfkill->reset_gpio = gpio;
  90. }
  91. gpio = devm_gpiod_get_index(&pdev->dev, "shutdown", 1);
  92. if (!IS_ERR(gpio)) {
  93. ret = gpiod_direction_output(gpio, 0);
  94. if (ret)
  95. return ret;
  96. rfkill->shutdown_gpio = gpio;
  97. }
  98. /* Make sure at-least one of the GPIO is defined and that
  99. * a name is specified for this instance
  100. */
  101. if ((!rfkill->reset_gpio && !rfkill->shutdown_gpio) || !rfkill->name) {
  102. dev_err(&pdev->dev, "invalid platform data\n");
  103. return -EINVAL;
  104. }
  105. rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
  106. rfkill->type, &rfkill_gpio_ops,
  107. rfkill);
  108. if (!rfkill->rfkill_dev)
  109. return -ENOMEM;
  110. ret = rfkill_register(rfkill->rfkill_dev);
  111. if (ret < 0)
  112. return ret;
  113. platform_set_drvdata(pdev, rfkill);
  114. dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
  115. return 0;
  116. }
  117. static int rfkill_gpio_remove(struct platform_device *pdev)
  118. {
  119. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  120. rfkill_unregister(rfkill->rfkill_dev);
  121. rfkill_destroy(rfkill->rfkill_dev);
  122. return 0;
  123. }
  124. #ifdef CONFIG_ACPI
  125. static const struct acpi_device_id rfkill_acpi_match[] = {
  126. { "BCM2E1A", RFKILL_TYPE_BLUETOOTH },
  127. { "BCM2E39", RFKILL_TYPE_BLUETOOTH },
  128. { "BCM2E3D", RFKILL_TYPE_BLUETOOTH },
  129. { "BCM2E64", RFKILL_TYPE_BLUETOOTH },
  130. { "BCM4752", RFKILL_TYPE_GPS },
  131. { "LNV4752", RFKILL_TYPE_GPS },
  132. { },
  133. };
  134. MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
  135. #endif
  136. static struct platform_driver rfkill_gpio_driver = {
  137. .probe = rfkill_gpio_probe,
  138. .remove = rfkill_gpio_remove,
  139. .driver = {
  140. .name = "rfkill_gpio",
  141. .owner = THIS_MODULE,
  142. .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
  143. },
  144. };
  145. module_platform_driver(rfkill_gpio_driver);
  146. MODULE_DESCRIPTION("gpio rfkill");
  147. MODULE_AUTHOR("NVIDIA");
  148. MODULE_LICENSE("GPL");