net: rfkill: gpio: clean up clock handling
[cascardo/linux.git] / net / rfkill / rfkill-gpio.c
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
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
30 struct rfkill_gpio_data {
31         struct rfkill_gpio_platform_data        *pdata;
32         struct rfkill                           *rfkill_dev;
33         char                                    *reset_name;
34         char                                    *shutdown_name;
35         struct clk                              *clk;
36         bool                                    clk_enabled;
37 };
38
39 static 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);
48                 if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
49                         clk_disable(rfkill->clk);
50         } else {
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);
57         }
58
59         rfkill->clk_enabled = blocked;
60
61         return 0;
62 }
63
64 static const struct rfkill_ops rfkill_gpio_ops = {
65         .set_block = rfkill_gpio_set_power,
66 };
67
68 static 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
88         rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
89         if (!rfkill)
90                 return -ENOMEM;
91
92         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__);
96                         return ret;
97                 }
98         }
99
100         rfkill->pdata = pdata;
101
102         len = strlen(pdata->name);
103         rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
104         if (!rfkill->reset_name)
105                 return -ENOMEM;
106
107         rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
108         if (!rfkill->shutdown_name)
109                 return -ENOMEM;
110
111         snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
112         snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
113
114         rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name);
115
116         if (gpio_is_valid(pdata->reset_gpio)) {
117                 ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio,
118                                         rfkill->reset_name);
119                 if (ret) {
120                         pr_warn("%s: failed to get reset gpio.\n", __func__);
121                         return ret;
122                 }
123         }
124
125         if (gpio_is_valid(pdata->shutdown_gpio)) {
126                 ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio,
127                                         rfkill->shutdown_name);
128                 if (ret) {
129                         pr_warn("%s: failed to get shutdown gpio.\n", __func__);
130                         return ret;
131                 }
132         }
133
134         rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
135                                           &rfkill_gpio_ops, rfkill);
136         if (!rfkill->rfkill_dev)
137                 return -ENOMEM;
138
139         ret = rfkill_register(rfkill->rfkill_dev);
140         if (ret < 0)
141                 return ret;
142
143         platform_set_drvdata(pdev, rfkill);
144
145         dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
146
147         return 0;
148 }
149
150 static int rfkill_gpio_remove(struct platform_device *pdev)
151 {
152         struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
153         struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
154
155         if (pdata->gpio_runtime_close)
156                 pdata->gpio_runtime_close(pdev);
157         rfkill_unregister(rfkill->rfkill_dev);
158         rfkill_destroy(rfkill->rfkill_dev);
159
160         return 0;
161 }
162
163 static struct platform_driver rfkill_gpio_driver = {
164         .probe = rfkill_gpio_probe,
165         .remove = rfkill_gpio_remove,
166         .driver = {
167                    .name = "rfkill_gpio",
168                    .owner = THIS_MODULE,
169         },
170 };
171
172 module_platform_driver(rfkill_gpio_driver);
173
174 MODULE_DESCRIPTION("gpio rfkill");
175 MODULE_AUTHOR("NVIDIA");
176 MODULE_LICENSE("GPL");