rfkill-gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/rfkill-gpio.h>
  27. struct rfkill_gpio_data {
  28. struct rfkill_gpio_platform_data *pdata;
  29. struct rfkill *rfkill_dev;
  30. char *reset_name;
  31. char *shutdown_name;
  32. struct clk *clk;
  33. bool clk_enabled;
  34. };
  35. static int rfkill_gpio_set_power(void *data, bool blocked)
  36. {
  37. struct rfkill_gpio_data *rfkill = data;
  38. if (blocked) {
  39. if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
  40. gpio_direction_output(rfkill->pdata->shutdown_gpio, 0);
  41. if (gpio_is_valid(rfkill->pdata->reset_gpio))
  42. gpio_direction_output(rfkill->pdata->reset_gpio, 0);
  43. if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
  44. clk_disable(rfkill->clk);
  45. } else {
  46. if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
  47. clk_enable(rfkill->clk);
  48. if (gpio_is_valid(rfkill->pdata->reset_gpio))
  49. gpio_direction_output(rfkill->pdata->reset_gpio, 1);
  50. if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
  51. gpio_direction_output(rfkill->pdata->shutdown_gpio, 1);
  52. }
  53. rfkill->clk_enabled = blocked;
  54. return 0;
  55. }
  56. static const struct rfkill_ops rfkill_gpio_ops = {
  57. .set_block = rfkill_gpio_set_power,
  58. };
  59. static int rfkill_gpio_probe(struct platform_device *pdev)
  60. {
  61. struct rfkill_gpio_data *rfkill;
  62. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  63. int ret = 0;
  64. int len = 0;
  65. if (!pdata) {
  66. pr_warn("%s: No platform data specified\n", __func__);
  67. return -EINVAL;
  68. }
  69. /* make sure at-least one of the GPIO is defined and that
  70. * a name is specified for this instance */
  71. if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) &&
  72. !gpio_is_valid(pdata->shutdown_gpio))) {
  73. pr_warn("%s: invalid platform data\n", __func__);
  74. return -EINVAL;
  75. }
  76. rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
  77. if (!rfkill)
  78. return -ENOMEM;
  79. if (pdata->gpio_runtime_setup) {
  80. ret = pdata->gpio_runtime_setup(pdev);
  81. if (ret) {
  82. pr_warn("%s: can't set up gpio\n", __func__);
  83. return ret;
  84. }
  85. }
  86. rfkill->pdata = pdata;
  87. len = strlen(pdata->name);
  88. rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
  89. if (!rfkill->reset_name)
  90. return -ENOMEM;
  91. rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
  92. if (!rfkill->shutdown_name)
  93. return -ENOMEM;
  94. snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
  95. snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
  96. rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name);
  97. if (gpio_is_valid(pdata->reset_gpio)) {
  98. ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio,
  99. rfkill->reset_name);
  100. if (ret) {
  101. pr_warn("%s: failed to get reset gpio.\n", __func__);
  102. return ret;
  103. }
  104. }
  105. if (gpio_is_valid(pdata->shutdown_gpio)) {
  106. ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio,
  107. rfkill->shutdown_name);
  108. if (ret) {
  109. pr_warn("%s: failed to get shutdown gpio.\n", __func__);
  110. return ret;
  111. }
  112. }
  113. rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
  114. &rfkill_gpio_ops, rfkill);
  115. if (!rfkill->rfkill_dev)
  116. return -ENOMEM;
  117. ret = rfkill_register(rfkill->rfkill_dev);
  118. if (ret < 0)
  119. return ret;
  120. platform_set_drvdata(pdev, rfkill);
  121. dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
  122. return 0;
  123. }
  124. static int rfkill_gpio_remove(struct platform_device *pdev)
  125. {
  126. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  127. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  128. if (pdata->gpio_runtime_close)
  129. pdata->gpio_runtime_close(pdev);
  130. rfkill_unregister(rfkill->rfkill_dev);
  131. rfkill_destroy(rfkill->rfkill_dev);
  132. return 0;
  133. }
  134. static struct platform_driver rfkill_gpio_driver = {
  135. .probe = rfkill_gpio_probe,
  136. .remove = rfkill_gpio_remove,
  137. .driver = {
  138. .name = "rfkill_gpio",
  139. .owner = THIS_MODULE,
  140. },
  141. };
  142. module_platform_driver(rfkill_gpio_driver);
  143. MODULE_DESCRIPTION("gpio rfkill");
  144. MODULE_AUTHOR("NVIDIA");
  145. MODULE_LICENSE("GPL");