rfkill-gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. char *reset_name;
  36. char *shutdown_name;
  37. struct clk *clk;
  38. bool clk_enabled;
  39. };
  40. static int rfkill_gpio_set_power(void *data, bool blocked)
  41. {
  42. struct rfkill_gpio_data *rfkill = data;
  43. if (blocked) {
  44. gpiod_set_value(rfkill->shutdown_gpio, 0);
  45. gpiod_set_value(rfkill->reset_gpio, 0);
  46. if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
  47. clk_disable(rfkill->clk);
  48. } else {
  49. if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
  50. clk_enable(rfkill->clk);
  51. gpiod_set_value(rfkill->reset_gpio, 1);
  52. gpiod_set_value(rfkill->shutdown_gpio, 1);
  53. }
  54. rfkill->clk_enabled = blocked;
  55. return 0;
  56. }
  57. static const struct rfkill_ops rfkill_gpio_ops = {
  58. .set_block = rfkill_gpio_set_power,
  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 0;
  70. }
  71. static int rfkill_gpio_probe(struct platform_device *pdev)
  72. {
  73. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  74. struct rfkill_gpio_data *rfkill;
  75. const char *clk_name = NULL;
  76. struct gpio_desc *gpio;
  77. int ret;
  78. int len;
  79. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  80. if (!rfkill)
  81. return -ENOMEM;
  82. if (ACPI_HANDLE(&pdev->dev)) {
  83. ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
  84. if (ret)
  85. return ret;
  86. } else if (pdata) {
  87. clk_name = pdata->power_clk_name;
  88. rfkill->name = pdata->name;
  89. rfkill->type = pdata->type;
  90. } else {
  91. return -ENODEV;
  92. }
  93. len = strlen(rfkill->name);
  94. rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
  95. if (!rfkill->reset_name)
  96. return -ENOMEM;
  97. rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
  98. if (!rfkill->shutdown_name)
  99. return -ENOMEM;
  100. snprintf(rfkill->reset_name, len + 6 , "%s_reset", rfkill->name);
  101. snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", rfkill->name);
  102. rfkill->clk = devm_clk_get(&pdev->dev, clk_name);
  103. gpio = devm_gpiod_get_index(&pdev->dev, rfkill->reset_name, 0);
  104. if (!IS_ERR(gpio)) {
  105. ret = gpiod_direction_output(gpio, 0);
  106. if (ret)
  107. return ret;
  108. rfkill->reset_gpio = gpio;
  109. }
  110. gpio = devm_gpiod_get_index(&pdev->dev, rfkill->shutdown_name, 1);
  111. if (!IS_ERR(gpio)) {
  112. ret = gpiod_direction_output(gpio, 0);
  113. if (ret)
  114. return ret;
  115. rfkill->shutdown_gpio = gpio;
  116. }
  117. /* Make sure at-least one of the GPIO is defined and that
  118. * a name is specified for this instance
  119. */
  120. if ((!rfkill->reset_gpio && !rfkill->shutdown_gpio) || !rfkill->name) {
  121. dev_err(&pdev->dev, "invalid platform data\n");
  122. return -EINVAL;
  123. }
  124. if (pdata && pdata->gpio_runtime_setup) {
  125. ret = pdata->gpio_runtime_setup(pdev);
  126. if (ret) {
  127. dev_err(&pdev->dev, "can't set up gpio\n");
  128. return ret;
  129. }
  130. }
  131. rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
  132. rfkill->type, &rfkill_gpio_ops,
  133. rfkill);
  134. if (!rfkill->rfkill_dev)
  135. return -ENOMEM;
  136. ret = rfkill_register(rfkill->rfkill_dev);
  137. if (ret < 0)
  138. return ret;
  139. platform_set_drvdata(pdev, rfkill);
  140. dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
  141. return 0;
  142. }
  143. static int rfkill_gpio_remove(struct platform_device *pdev)
  144. {
  145. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  146. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  147. if (pdata && pdata->gpio_runtime_close)
  148. pdata->gpio_runtime_close(pdev);
  149. rfkill_unregister(rfkill->rfkill_dev);
  150. rfkill_destroy(rfkill->rfkill_dev);
  151. return 0;
  152. }
  153. static const struct acpi_device_id rfkill_acpi_match[] = {
  154. { "BCM4752", RFKILL_TYPE_GPS },
  155. { },
  156. };
  157. static struct platform_driver rfkill_gpio_driver = {
  158. .probe = rfkill_gpio_probe,
  159. .remove = rfkill_gpio_remove,
  160. .driver = {
  161. .name = "rfkill_gpio",
  162. .owner = THIS_MODULE,
  163. .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
  164. },
  165. };
  166. module_platform_driver(rfkill_gpio_driver);
  167. MODULE_DESCRIPTION("gpio rfkill");
  168. MODULE_AUTHOR("NVIDIA");
  169. MODULE_LICENSE("GPL");