rfkill-gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. struct rfkill_gpio_data {
  29. const char *name;
  30. enum rfkill_type type;
  31. struct gpio_desc *reset_gpio;
  32. struct gpio_desc *shutdown_gpio;
  33. struct rfkill *rfkill_dev;
  34. struct clk *clk;
  35. bool clk_enabled;
  36. };
  37. static int rfkill_gpio_set_power(void *data, bool blocked)
  38. {
  39. struct rfkill_gpio_data *rfkill = data;
  40. if (!blocked && !IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
  41. clk_enable(rfkill->clk);
  42. gpiod_set_value_cansleep(rfkill->shutdown_gpio, !blocked);
  43. gpiod_set_value_cansleep(rfkill->reset_gpio, !blocked);
  44. if (blocked && !IS_ERR(rfkill->clk) && rfkill->clk_enabled)
  45. clk_disable(rfkill->clk);
  46. rfkill->clk_enabled = !blocked;
  47. return 0;
  48. }
  49. static const struct rfkill_ops rfkill_gpio_ops = {
  50. .set_block = rfkill_gpio_set_power,
  51. };
  52. static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
  53. static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
  54. static const struct acpi_gpio_mapping acpi_rfkill_default_gpios[] = {
  55. { "reset-gpios", &reset_gpios, 1 },
  56. { "shutdown-gpios", &shutdown_gpios, 1 },
  57. { },
  58. };
  59. static int rfkill_gpio_acpi_probe(struct device *dev,
  60. struct rfkill_gpio_data *rfkill)
  61. {
  62. const struct acpi_device_id *id;
  63. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  64. if (!id)
  65. return -ENODEV;
  66. rfkill->type = (unsigned)id->driver_data;
  67. return devm_acpi_dev_add_driver_gpios(dev, acpi_rfkill_default_gpios);
  68. }
  69. static int rfkill_gpio_probe(struct platform_device *pdev)
  70. {
  71. struct rfkill_gpio_data *rfkill;
  72. struct gpio_desc *gpio;
  73. const char *type_name;
  74. int ret;
  75. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  76. if (!rfkill)
  77. return -ENOMEM;
  78. device_property_read_string(&pdev->dev, "name", &rfkill->name);
  79. device_property_read_string(&pdev->dev, "type", &type_name);
  80. if (!rfkill->name)
  81. rfkill->name = dev_name(&pdev->dev);
  82. rfkill->type = rfkill_find_type(type_name);
  83. if (ACPI_HANDLE(&pdev->dev)) {
  84. ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
  85. if (ret)
  86. return ret;
  87. }
  88. rfkill->clk = devm_clk_get(&pdev->dev, NULL);
  89. gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
  90. if (IS_ERR(gpio))
  91. return PTR_ERR(gpio);
  92. rfkill->reset_gpio = gpio;
  93. gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_OUT_LOW);
  94. if (IS_ERR(gpio))
  95. return PTR_ERR(gpio);
  96. rfkill->shutdown_gpio = gpio;
  97. /* Make sure at-least one GPIO is defined for this instance */
  98. if (!rfkill->reset_gpio && !rfkill->shutdown_gpio) {
  99. dev_err(&pdev->dev, "invalid platform data\n");
  100. return -EINVAL;
  101. }
  102. rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
  103. rfkill->type, &rfkill_gpio_ops,
  104. rfkill);
  105. if (!rfkill->rfkill_dev)
  106. return -ENOMEM;
  107. ret = rfkill_register(rfkill->rfkill_dev);
  108. if (ret < 0)
  109. goto err_destroy;
  110. platform_set_drvdata(pdev, rfkill);
  111. dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
  112. return 0;
  113. err_destroy:
  114. rfkill_destroy(rfkill->rfkill_dev);
  115. return ret;
  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. { "BCM4752", RFKILL_TYPE_GPS },
  127. { "LNV4752", RFKILL_TYPE_GPS },
  128. { },
  129. };
  130. MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
  131. #endif
  132. static struct platform_driver rfkill_gpio_driver = {
  133. .probe = rfkill_gpio_probe,
  134. .remove = rfkill_gpio_remove,
  135. .driver = {
  136. .name = "rfkill_gpio",
  137. .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
  138. },
  139. };
  140. module_platform_driver(rfkill_gpio_driver);
  141. MODULE_DESCRIPTION("gpio rfkill");
  142. MODULE_AUTHOR("NVIDIA");
  143. MODULE_LICENSE("GPL");