2 * Copyright (c) 2011, NVIDIA Corporation.
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.
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
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.
19 #include <linux/gpio.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/rfkill.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 #include <linux/slab.h>
28 #include <linux/rfkill-gpio.h>
30 struct rfkill_gpio_data {
31 struct rfkill_gpio_platform_data *pdata;
32 struct rfkill *rfkill_dev;
39 static int rfkill_gpio_set_power(void *data, bool blocked)
41 struct rfkill_gpio_data *rfkill = data;
44 if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
45 gpio_direction_output(rfkill->pdata->shutdown_gpio, 0);
46 if (gpio_is_valid(rfkill->pdata->reset_gpio))
47 gpio_direction_output(rfkill->pdata->reset_gpio, 0);
48 if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
49 clk_disable(rfkill->clk);
51 if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
52 clk_enable(rfkill->clk);
53 if (gpio_is_valid(rfkill->pdata->reset_gpio))
54 gpio_direction_output(rfkill->pdata->reset_gpio, 1);
55 if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
56 gpio_direction_output(rfkill->pdata->shutdown_gpio, 1);
59 rfkill->clk_enabled = blocked;
64 static const struct rfkill_ops rfkill_gpio_ops = {
65 .set_block = rfkill_gpio_set_power,
68 static int rfkill_gpio_probe(struct platform_device *pdev)
70 struct rfkill_gpio_data *rfkill;
71 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
76 pr_warn("%s: No platform data specified\n", __func__);
80 /* make sure at-least one of the GPIO is defined and that
81 * a name is specified for this instance */
82 if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) &&
83 !gpio_is_valid(pdata->shutdown_gpio))) {
84 pr_warn("%s: invalid platform data\n", __func__);
88 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
92 if (pdata->gpio_runtime_setup) {
93 ret = pdata->gpio_runtime_setup(pdev);
95 pr_warn("%s: can't set up gpio\n", __func__);
100 rfkill->pdata = pdata;
102 len = strlen(pdata->name);
103 rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
104 if (!rfkill->reset_name)
107 rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
108 if (!rfkill->shutdown_name)
111 snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
112 snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
114 rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name);
116 if (gpio_is_valid(pdata->reset_gpio)) {
117 ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio,
120 pr_warn("%s: failed to get reset gpio.\n", __func__);
125 if (gpio_is_valid(pdata->shutdown_gpio)) {
126 ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio,
127 rfkill->shutdown_name);
129 pr_warn("%s: failed to get shutdown gpio.\n", __func__);
134 rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
135 &rfkill_gpio_ops, rfkill);
136 if (!rfkill->rfkill_dev)
139 ret = rfkill_register(rfkill->rfkill_dev);
143 platform_set_drvdata(pdev, rfkill);
145 dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
150 static int rfkill_gpio_remove(struct platform_device *pdev)
152 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
153 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
155 if (pdata->gpio_runtime_close)
156 pdata->gpio_runtime_close(pdev);
157 rfkill_unregister(rfkill->rfkill_dev);
158 rfkill_destroy(rfkill->rfkill_dev);
163 static struct platform_driver rfkill_gpio_driver = {
164 .probe = rfkill_gpio_probe,
165 .remove = rfkill_gpio_remove,
167 .name = "rfkill_gpio",
168 .owner = THIS_MODULE,
172 module_platform_driver(rfkill_gpio_driver);
174 MODULE_DESCRIPTION("gpio rfkill");
175 MODULE_AUTHOR("NVIDIA");
176 MODULE_LICENSE("GPL");