blob: 1d104e728ef900698ca869150bf68fa3e3fd6df7 [file] [log] [blame]
Rhyland Klein7176ba22011-05-16 14:41:48 -07001/*
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
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>
27
28#include <linux/rfkill-gpio.h>
29
Rhyland Klein7176ba22011-05-16 14:41:48 -070030struct rfkill_gpio_data {
31 struct rfkill_gpio_platform_data *pdata;
32 struct rfkill *rfkill_dev;
33 char *reset_name;
34 char *shutdown_name;
Heikki Krogerusf02ae592013-10-16 13:53:40 +030035 struct clk *clk;
36 bool clk_enabled;
Rhyland Klein7176ba22011-05-16 14:41:48 -070037};
38
39static int rfkill_gpio_set_power(void *data, bool blocked)
40{
41 struct rfkill_gpio_data *rfkill = data;
42
43 if (blocked) {
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);
Heikki Krogerusf02ae592013-10-16 13:53:40 +030048 if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
49 clk_disable(rfkill->clk);
Rhyland Klein7176ba22011-05-16 14:41:48 -070050 } else {
Heikki Krogerusf02ae592013-10-16 13:53:40 +030051 if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
52 clk_enable(rfkill->clk);
Rhyland Klein7176ba22011-05-16 14:41:48 -070053 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);
57 }
58
Heikki Krogerusf02ae592013-10-16 13:53:40 +030059 rfkill->clk_enabled = blocked;
Rhyland Klein7176ba22011-05-16 14:41:48 -070060
61 return 0;
62}
63
64static const struct rfkill_ops rfkill_gpio_ops = {
65 .set_block = rfkill_gpio_set_power,
66};
67
68static int rfkill_gpio_probe(struct platform_device *pdev)
69{
70 struct rfkill_gpio_data *rfkill;
71 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
72 int ret = 0;
73 int len = 0;
74
75 if (!pdata) {
76 pr_warn("%s: No platform data specified\n", __func__);
77 return -EINVAL;
78 }
79
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__);
85 return -EINVAL;
86 }
87
Heikki Krogerus5e7ca392013-10-16 13:53:39 +030088 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
Rhyland Klein7176ba22011-05-16 14:41:48 -070089 if (!rfkill)
90 return -ENOMEM;
91
Sangwook Leee209c5a2011-09-29 12:57:17 +010092 if (pdata->gpio_runtime_setup) {
93 ret = pdata->gpio_runtime_setup(pdev);
94 if (ret) {
95 pr_warn("%s: can't set up gpio\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +030096 return ret;
Sangwook Leee209c5a2011-09-29 12:57:17 +010097 }
98 }
99
Rhyland Klein7176ba22011-05-16 14:41:48 -0700100 rfkill->pdata = pdata;
101
102 len = strlen(pdata->name);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300103 rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
104 if (!rfkill->reset_name)
105 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700106
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300107 rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
108 if (!rfkill->shutdown_name)
109 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700110
111 snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
112 snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
113
Heikki Krogerusf02ae592013-10-16 13:53:40 +0300114 rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700115
116 if (gpio_is_valid(pdata->reset_gpio)) {
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300117 ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio,
118 rfkill->reset_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700119 if (ret) {
120 pr_warn("%s: failed to get reset gpio.\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300121 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700122 }
123 }
124
125 if (gpio_is_valid(pdata->shutdown_gpio)) {
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300126 ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio,
127 rfkill->shutdown_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700128 if (ret) {
129 pr_warn("%s: failed to get shutdown gpio.\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300130 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700131 }
132 }
133
134 rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
Wei Yongjun06f95e62013-04-18 10:31:16 +0800135 &rfkill_gpio_ops, rfkill);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300136 if (!rfkill->rfkill_dev)
137 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700138
139 ret = rfkill_register(rfkill->rfkill_dev);
140 if (ret < 0)
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300141 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700142
143 platform_set_drvdata(pdev, rfkill);
144
145 dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
146
147 return 0;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700148}
149
150static int rfkill_gpio_remove(struct platform_device *pdev)
151{
152 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
Sangwook Leee209c5a2011-09-29 12:57:17 +0100153 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700154
Sangwook Leee209c5a2011-09-29 12:57:17 +0100155 if (pdata->gpio_runtime_close)
156 pdata->gpio_runtime_close(pdev);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700157 rfkill_unregister(rfkill->rfkill_dev);
158 rfkill_destroy(rfkill->rfkill_dev);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700159
160 return 0;
161}
162
163static struct platform_driver rfkill_gpio_driver = {
164 .probe = rfkill_gpio_probe,
Bill Pemberton9e2b29d2012-12-03 09:56:26 -0500165 .remove = rfkill_gpio_remove,
Rhyland Klein7176ba22011-05-16 14:41:48 -0700166 .driver = {
167 .name = "rfkill_gpio",
168 .owner = THIS_MODULE,
169 },
170};
171
Axel Lin98ef55f62011-11-28 17:15:04 +0800172module_platform_driver(rfkill_gpio_driver);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700173
174MODULE_DESCRIPTION("gpio rfkill");
175MODULE_AUTHOR("NVIDIA");
176MODULE_LICENSE("GPL");