rfkill-gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. enum rfkill_gpio_clk_state {
  28. UNSPECIFIED = 0,
  29. PWR_ENABLED,
  30. PWR_DISABLED
  31. };
  32. #define PWR_CLK_SET(_RF, _EN) \
  33. ((_RF)->pwr_clk_enabled = (!(_EN) ? PWR_ENABLED : PWR_DISABLED))
  34. #define PWR_CLK_ENABLED(_RF) ((_RF)->pwr_clk_enabled == PWR_ENABLED)
  35. #define PWR_CLK_DISABLED(_RF) ((_RF)->pwr_clk_enabled != PWR_ENABLED)
  36. struct rfkill_gpio_data {
  37. struct rfkill_gpio_platform_data *pdata;
  38. struct rfkill *rfkill_dev;
  39. char *reset_name;
  40. char *shutdown_name;
  41. enum rfkill_gpio_clk_state pwr_clk_enabled;
  42. struct clk *pwr_clk;
  43. };
  44. static int rfkill_gpio_set_power(void *data, bool blocked)
  45. {
  46. struct rfkill_gpio_data *rfkill = data;
  47. if (blocked) {
  48. if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
  49. gpio_direction_output(rfkill->pdata->shutdown_gpio, 0);
  50. if (gpio_is_valid(rfkill->pdata->reset_gpio))
  51. gpio_direction_output(rfkill->pdata->reset_gpio, 0);
  52. if (rfkill->pwr_clk && PWR_CLK_ENABLED(rfkill))
  53. clk_disable(rfkill->pwr_clk);
  54. } else {
  55. if (rfkill->pwr_clk && PWR_CLK_DISABLED(rfkill))
  56. clk_enable(rfkill->pwr_clk);
  57. if (gpio_is_valid(rfkill->pdata->reset_gpio))
  58. gpio_direction_output(rfkill->pdata->reset_gpio, 1);
  59. if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
  60. gpio_direction_output(rfkill->pdata->shutdown_gpio, 1);
  61. }
  62. if (rfkill->pwr_clk)
  63. PWR_CLK_SET(rfkill, blocked);
  64. return 0;
  65. }
  66. static const struct rfkill_ops rfkill_gpio_ops = {
  67. .set_block = rfkill_gpio_set_power,
  68. };
  69. static int rfkill_gpio_probe(struct platform_device *pdev)
  70. {
  71. struct rfkill_gpio_data *rfkill;
  72. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  73. int ret = 0;
  74. int len = 0;
  75. if (!pdata) {
  76. pr_warn("%s: No platform data specified\n", __func__);
  77. return -EINVAL;
  78. }
  79. /* make sure at-least one of the GPIO is defined and that
  80. * a name is specified for this instance */
  81. if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) &&
  82. !gpio_is_valid(pdata->shutdown_gpio))) {
  83. pr_warn("%s: invalid platform data\n", __func__);
  84. return -EINVAL;
  85. }
  86. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  87. if (!rfkill)
  88. return -ENOMEM;
  89. if (pdata->gpio_runtime_setup) {
  90. ret = pdata->gpio_runtime_setup(pdev);
  91. if (ret) {
  92. pr_warn("%s: can't set up gpio\n", __func__);
  93. goto fail_alloc;
  94. }
  95. }
  96. rfkill->pdata = pdata;
  97. len = strlen(pdata->name);
  98. rfkill->reset_name = kzalloc(len + 7, GFP_KERNEL);
  99. if (!rfkill->reset_name) {
  100. ret = -ENOMEM;
  101. goto fail_alloc;
  102. }
  103. rfkill->shutdown_name = kzalloc(len + 10, GFP_KERNEL);
  104. if (!rfkill->shutdown_name) {
  105. ret = -ENOMEM;
  106. goto fail_reset_name;
  107. }
  108. snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
  109. snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
  110. if (pdata->power_clk_name) {
  111. rfkill->pwr_clk = clk_get(&pdev->dev, pdata->power_clk_name);
  112. if (IS_ERR(rfkill->pwr_clk)) {
  113. pr_warn("%s: can't find pwr_clk.\n", __func__);
  114. goto fail_shutdown_name;
  115. }
  116. }
  117. if (gpio_is_valid(pdata->reset_gpio)) {
  118. ret = gpio_request(pdata->reset_gpio, rfkill->reset_name);
  119. if (ret) {
  120. pr_warn("%s: failed to get reset gpio.\n", __func__);
  121. goto fail_clock;
  122. }
  123. }
  124. if (gpio_is_valid(pdata->shutdown_gpio)) {
  125. ret = gpio_request(pdata->shutdown_gpio, rfkill->shutdown_name);
  126. if (ret) {
  127. pr_warn("%s: failed to get shutdown gpio.\n", __func__);
  128. goto fail_reset;
  129. }
  130. }
  131. rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
  132. &rfkill_gpio_ops, rfkill);
  133. if (!rfkill->rfkill_dev)
  134. goto fail_shutdown;
  135. ret = rfkill_register(rfkill->rfkill_dev);
  136. if (ret < 0)
  137. goto fail_rfkill;
  138. platform_set_drvdata(pdev, rfkill);
  139. dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
  140. return 0;
  141. fail_rfkill:
  142. rfkill_destroy(rfkill->rfkill_dev);
  143. fail_shutdown:
  144. if (gpio_is_valid(pdata->shutdown_gpio))
  145. gpio_free(pdata->shutdown_gpio);
  146. fail_reset:
  147. if (gpio_is_valid(pdata->reset_gpio))
  148. gpio_free(pdata->reset_gpio);
  149. fail_clock:
  150. if (rfkill->pwr_clk)
  151. clk_put(rfkill->pwr_clk);
  152. fail_shutdown_name:
  153. kfree(rfkill->shutdown_name);
  154. fail_reset_name:
  155. kfree(rfkill->reset_name);
  156. fail_alloc:
  157. kfree(rfkill);
  158. return ret;
  159. }
  160. static int rfkill_gpio_remove(struct platform_device *pdev)
  161. {
  162. struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
  163. struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
  164. if (pdata->gpio_runtime_close)
  165. pdata->gpio_runtime_close(pdev);
  166. rfkill_unregister(rfkill->rfkill_dev);
  167. rfkill_destroy(rfkill->rfkill_dev);
  168. if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
  169. gpio_free(rfkill->pdata->shutdown_gpio);
  170. if (gpio_is_valid(rfkill->pdata->reset_gpio))
  171. gpio_free(rfkill->pdata->reset_gpio);
  172. if (rfkill->pwr_clk && PWR_CLK_ENABLED(rfkill))
  173. clk_disable(rfkill->pwr_clk);
  174. if (rfkill->pwr_clk)
  175. clk_put(rfkill->pwr_clk);
  176. kfree(rfkill->shutdown_name);
  177. kfree(rfkill->reset_name);
  178. kfree(rfkill);
  179. return 0;
  180. }
  181. static struct platform_driver rfkill_gpio_driver = {
  182. .probe = rfkill_gpio_probe,
  183. .remove = __devexit_p(rfkill_gpio_remove),
  184. .driver = {
  185. .name = "rfkill_gpio",
  186. .owner = THIS_MODULE,
  187. },
  188. };
  189. module_platform_driver(rfkill_gpio_driver);
  190. MODULE_DESCRIPTION("gpio rfkill");
  191. MODULE_AUTHOR("NVIDIA");
  192. MODULE_LICENSE("GPL");