blob: 070580619e3d9b2224f2890fad7ddf32b0cdd808 [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
30enum rfkill_gpio_clk_state {
31 UNSPECIFIED = 0,
32 PWR_ENABLED,
33 PWR_DISABLED
34};
35
36#define PWR_CLK_SET(_RF, _EN) \
37 ((_RF)->pwr_clk_enabled = (!(_EN) ? PWR_ENABLED : PWR_DISABLED))
38#define PWR_CLK_ENABLED(_RF) ((_RF)->pwr_clk_enabled == PWR_ENABLED)
39#define PWR_CLK_DISABLED(_RF) ((_RF)->pwr_clk_enabled != PWR_ENABLED)
40
41struct rfkill_gpio_data {
42 struct rfkill_gpio_platform_data *pdata;
43 struct rfkill *rfkill_dev;
44 char *reset_name;
45 char *shutdown_name;
46 enum rfkill_gpio_clk_state pwr_clk_enabled;
47 struct clk *pwr_clk;
48};
49
50static int rfkill_gpio_set_power(void *data, bool blocked)
51{
52 struct rfkill_gpio_data *rfkill = data;
53
54 if (blocked) {
55 if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
56 gpio_direction_output(rfkill->pdata->shutdown_gpio, 0);
57 if (gpio_is_valid(rfkill->pdata->reset_gpio))
58 gpio_direction_output(rfkill->pdata->reset_gpio, 0);
59 if (rfkill->pwr_clk && PWR_CLK_ENABLED(rfkill))
60 clk_disable(rfkill->pwr_clk);
61 } else {
62 if (rfkill->pwr_clk && PWR_CLK_DISABLED(rfkill))
63 clk_enable(rfkill->pwr_clk);
64 if (gpio_is_valid(rfkill->pdata->reset_gpio))
65 gpio_direction_output(rfkill->pdata->reset_gpio, 1);
66 if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
67 gpio_direction_output(rfkill->pdata->shutdown_gpio, 1);
68 }
69
70 if (rfkill->pwr_clk)
71 PWR_CLK_SET(rfkill, blocked);
72
73 return 0;
74}
75
76static const struct rfkill_ops rfkill_gpio_ops = {
77 .set_block = rfkill_gpio_set_power,
78};
79
80static int rfkill_gpio_probe(struct platform_device *pdev)
81{
82 struct rfkill_gpio_data *rfkill;
83 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
84 int ret = 0;
85 int len = 0;
86
87 if (!pdata) {
88 pr_warn("%s: No platform data specified\n", __func__);
89 return -EINVAL;
90 }
91
92 /* make sure at-least one of the GPIO is defined and that
93 * a name is specified for this instance */
94 if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) &&
95 !gpio_is_valid(pdata->shutdown_gpio))) {
96 pr_warn("%s: invalid platform data\n", __func__);
97 return -EINVAL;
98 }
99
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300100 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700101 if (!rfkill)
102 return -ENOMEM;
103
Sangwook Leee209c5a2011-09-29 12:57:17 +0100104 if (pdata->gpio_runtime_setup) {
105 ret = pdata->gpio_runtime_setup(pdev);
106 if (ret) {
107 pr_warn("%s: can't set up gpio\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300108 return ret;
Sangwook Leee209c5a2011-09-29 12:57:17 +0100109 }
110 }
111
Rhyland Klein7176ba22011-05-16 14:41:48 -0700112 rfkill->pdata = pdata;
113
114 len = strlen(pdata->name);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300115 rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
116 if (!rfkill->reset_name)
117 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700118
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300119 rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
120 if (!rfkill->shutdown_name)
121 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700122
123 snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
124 snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
125
126 if (pdata->power_clk_name) {
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300127 rfkill->pwr_clk = devm_clk_get(&pdev->dev,
128 pdata->power_clk_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700129 if (IS_ERR(rfkill->pwr_clk)) {
130 pr_warn("%s: can't find pwr_clk.\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300131 return PTR_ERR(rfkill->pwr_clk);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700132 }
133 }
134
135 if (gpio_is_valid(pdata->reset_gpio)) {
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300136 ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio,
137 rfkill->reset_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700138 if (ret) {
139 pr_warn("%s: failed to get reset gpio.\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300140 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700141 }
142 }
143
144 if (gpio_is_valid(pdata->shutdown_gpio)) {
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300145 ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio,
146 rfkill->shutdown_name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700147 if (ret) {
148 pr_warn("%s: failed to get shutdown gpio.\n", __func__);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300149 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700150 }
151 }
152
153 rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
Wei Yongjun06f95e62013-04-18 10:31:16 +0800154 &rfkill_gpio_ops, rfkill);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300155 if (!rfkill->rfkill_dev)
156 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700157
158 ret = rfkill_register(rfkill->rfkill_dev);
159 if (ret < 0)
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300160 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700161
162 platform_set_drvdata(pdev, rfkill);
163
164 dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
165
166 return 0;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700167}
168
169static int rfkill_gpio_remove(struct platform_device *pdev)
170{
171 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
Sangwook Leee209c5a2011-09-29 12:57:17 +0100172 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700173
Sangwook Leee209c5a2011-09-29 12:57:17 +0100174 if (pdata->gpio_runtime_close)
175 pdata->gpio_runtime_close(pdev);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700176 rfkill_unregister(rfkill->rfkill_dev);
177 rfkill_destroy(rfkill->rfkill_dev);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700178
179 return 0;
180}
181
182static struct platform_driver rfkill_gpio_driver = {
183 .probe = rfkill_gpio_probe,
Bill Pemberton9e2b29d2012-12-03 09:56:26 -0500184 .remove = rfkill_gpio_remove,
Rhyland Klein7176ba22011-05-16 14:41:48 -0700185 .driver = {
186 .name = "rfkill_gpio",
187 .owner = THIS_MODULE,
188 },
189};
190
Axel Lin98ef55f62011-11-28 17:15:04 +0800191module_platform_driver(rfkill_gpio_driver);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700192
193MODULE_DESCRIPTION("gpio rfkill");
194MODULE_AUTHOR("NVIDIA");
195MODULE_LICENSE("GPL");