rfkill-gpio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 const struct acpi_gpio_params reset_gpios = { 0, 0, false };
  54. static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
  55. static const struct acpi_gpio_mapping acpi_rfkill_default_gpios[] = {
  56. { "reset-gpios", &reset_gpios, 1 },
  57. { "shutdown-gpios", &shutdown_gpios, 1 },
  58. { },
  59. };
  60. static int rfkill_gpio_acpi_probe(struct device *dev,
  61. struct rfkill_gpio_data *rfkill)
  62. {
  63. const struct acpi_device_id *id;
  64. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  65. if (!id)
  66. return -ENODEV;
  67. rfkill->name = dev_name(dev);
  68. rfkill->type = (unsigned)id->driver_data;
  69. return acpi_dev_add_driver_gpios(ACPI_COMPANION(dev),
  70. acpi_rfkill_default_gpios);
  71. }
  72. static int rfkill_gpio_probe(struct platform_device *pdev)
  73. {
  74. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  75. struct rfkill_gpio_data *rfkill;
  76. struct gpio_desc *gpio;
  77. int ret;
  78. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  79. if (!rfkill)
  80. return -ENOMEM;
  81. if (ACPI_HANDLE(&pdev->dev)) {
  82. ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
  83. if (ret)
  84. return ret;
  85. } else if (pdata) {
  86. rfkill->name = pdata->name;
  87. rfkill->type = pdata->type;
  88. } else {
  89. return -ENODEV;
  90. }
  91. rfkill->clk = devm_clk_get(&pdev->dev, NULL);
  92. gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
  93. if (IS_ERR(gpio))
  94. return PTR_ERR(gpio);
  95. rfkill->reset_gpio = gpio;
  96. gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_OUT_LOW);
  97. if (IS_ERR(gpio))
  98. return PTR_ERR(gpio);
  99. rfkill->shutdown_gpio = gpio;
  100. /* Make sure at-least one of the GPIO is defined and that
  101. * a name is specified for this instance
  102. */
  103. if ((!rfkill->reset_gpio && !rfkill->shutdown_gpio) || !rfkill->name) {
  104. dev_err(&pdev->dev, "invalid platform data\n");
  105. return -EINVAL;
  106. }
  107. rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
  108. rfkill->type, &rfkill_gpio_ops,
  109. rfkill);
  110. if (!rfkill->rfkill_dev)
  111. return -ENOMEM;
  112. ret = rfkill_register(rfkill->rfkill_dev);
  113. if (ret < 0)
  114. goto err_destroy;
  115. platform_set_drvdata(pdev, rfkill);
  116. dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
  117. return 0;
  118. err_destroy:
  119. rfkill_destroy(rfkill->rfkill_dev);
  120. return ret;
  121. }
  122. static int rfkill_gpio_remove(struct platform_device *pdev)
  123. {
  124. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  125. rfkill_unregister(rfkill->rfkill_dev);
  126. rfkill_destroy(rfkill->rfkill_dev);
  127. acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pdev->dev));
  128. return 0;
  129. }
  130. #ifdef CONFIG_ACPI
  131. static const struct acpi_device_id rfkill_acpi_match[] = {
  132. { "BCM2E1A", RFKILL_TYPE_BLUETOOTH },
  133. { "BCM2E3D", RFKILL_TYPE_BLUETOOTH },
  134. { "BCM2E40", RFKILL_TYPE_BLUETOOTH },
  135. { "BCM2E64", RFKILL_TYPE_BLUETOOTH },
  136. { "BCM4752", RFKILL_TYPE_GPS },
  137. { "LNV4752", RFKILL_TYPE_GPS },
  138. { },
  139. };
  140. MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
  141. #endif
  142. static struct platform_driver rfkill_gpio_driver = {
  143. .probe = rfkill_gpio_probe,
  144. .remove = rfkill_gpio_remove,
  145. .driver = {
  146. .name = "rfkill_gpio",
  147. .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
  148. },
  149. };
  150. module_platform_driver(rfkill_gpio_driver);
  151. MODULE_DESCRIPTION("gpio rfkill");
  152. MODULE_AUTHOR("NVIDIA");
  153. MODULE_LICENSE("GPL");