rfkill-gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/mod_devicetable.h>
  23. #include <linux/rfkill.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/slab.h>
  27. #include <linux/acpi.h>
  28. #include <linux/gpio/consumer.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->type = (unsigned)id->driver_data;
  68. return devm_acpi_dev_add_driver_gpios(dev, acpi_rfkill_default_gpios);
  69. }
  70. static int rfkill_gpio_probe(struct platform_device *pdev)
  71. {
  72. struct rfkill_gpio_data *rfkill;
  73. struct gpio_desc *gpio;
  74. const char *type_name;
  75. int ret;
  76. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  77. if (!rfkill)
  78. return -ENOMEM;
  79. device_property_read_string(&pdev->dev, "name", &rfkill->name);
  80. device_property_read_string(&pdev->dev, "type", &type_name);
  81. if (!rfkill->name)
  82. rfkill->name = dev_name(&pdev->dev);
  83. rfkill->type = rfkill_find_type(type_name);
  84. if (ACPI_HANDLE(&pdev->dev)) {
  85. ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
  86. if (ret)
  87. return ret;
  88. }
  89. rfkill->clk = devm_clk_get(&pdev->dev, NULL);
  90. gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
  91. if (IS_ERR(gpio))
  92. return PTR_ERR(gpio);
  93. rfkill->reset_gpio = gpio;
  94. gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_OUT_LOW);
  95. if (IS_ERR(gpio))
  96. return PTR_ERR(gpio);
  97. rfkill->shutdown_gpio = gpio;
  98. /* Make sure at-least one GPIO is defined for this instance */
  99. if (!rfkill->reset_gpio && !rfkill->shutdown_gpio) {
  100. dev_err(&pdev->dev, "invalid platform data\n");
  101. return -EINVAL;
  102. }
  103. rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
  104. rfkill->type, &rfkill_gpio_ops,
  105. rfkill);
  106. if (!rfkill->rfkill_dev)
  107. return -ENOMEM;
  108. ret = rfkill_register(rfkill->rfkill_dev);
  109. if (ret < 0)
  110. goto err_destroy;
  111. platform_set_drvdata(pdev, rfkill);
  112. dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
  113. return 0;
  114. err_destroy:
  115. rfkill_destroy(rfkill->rfkill_dev);
  116. return ret;
  117. }
  118. static int rfkill_gpio_remove(struct platform_device *pdev)
  119. {
  120. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  121. rfkill_unregister(rfkill->rfkill_dev);
  122. rfkill_destroy(rfkill->rfkill_dev);
  123. return 0;
  124. }
  125. #ifdef CONFIG_ACPI
  126. static const struct acpi_device_id rfkill_acpi_match[] = {
  127. { "BCM4752", RFKILL_TYPE_GPS },
  128. { "LNV4752", RFKILL_TYPE_GPS },
  129. { },
  130. };
  131. MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
  132. #endif
  133. static struct platform_driver rfkill_gpio_driver = {
  134. .probe = rfkill_gpio_probe,
  135. .remove = rfkill_gpio_remove,
  136. .driver = {
  137. .name = "rfkill_gpio",
  138. .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
  139. },
  140. };
  141. module_platform_driver(rfkill_gpio_driver);
  142. MODULE_DESCRIPTION("gpio rfkill");
  143. MODULE_AUTHOR("NVIDIA");
  144. MODULE_LICENSE("GPL");