Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 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 | |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 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; |
Heikki Krogerus | f02ae59 | 2013-10-16 13:53:40 +0300 | [diff] [blame^] | 35 | struct clk *clk; |
| 36 | bool clk_enabled; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 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); |
Heikki Krogerus | f02ae59 | 2013-10-16 13:53:40 +0300 | [diff] [blame^] | 48 | if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled) |
| 49 | clk_disable(rfkill->clk); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 50 | } else { |
Heikki Krogerus | f02ae59 | 2013-10-16 13:53:40 +0300 | [diff] [blame^] | 51 | if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled) |
| 52 | clk_enable(rfkill->clk); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 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 | |
Heikki Krogerus | f02ae59 | 2013-10-16 13:53:40 +0300 | [diff] [blame^] | 59 | rfkill->clk_enabled = blocked; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 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 | |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 88 | rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 89 | if (!rfkill) |
| 90 | return -ENOMEM; |
| 91 | |
Sangwook Lee | e209c5a | 2011-09-29 12:57:17 +0100 | [diff] [blame] | 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__); |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 96 | return ret; |
Sangwook Lee | e209c5a | 2011-09-29 12:57:17 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 100 | rfkill->pdata = pdata; |
| 101 | |
| 102 | len = strlen(pdata->name); |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 103 | rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL); |
| 104 | if (!rfkill->reset_name) |
| 105 | return -ENOMEM; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 106 | |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 107 | rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL); |
| 108 | if (!rfkill->shutdown_name) |
| 109 | return -ENOMEM; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 110 | |
| 111 | snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name); |
| 112 | snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name); |
| 113 | |
Heikki Krogerus | f02ae59 | 2013-10-16 13:53:40 +0300 | [diff] [blame^] | 114 | rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 115 | |
| 116 | if (gpio_is_valid(pdata->reset_gpio)) { |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 117 | ret = devm_gpio_request(&pdev->dev, pdata->reset_gpio, |
| 118 | rfkill->reset_name); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 119 | if (ret) { |
| 120 | pr_warn("%s: failed to get reset gpio.\n", __func__); |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 121 | return ret; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | if (gpio_is_valid(pdata->shutdown_gpio)) { |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 126 | ret = devm_gpio_request(&pdev->dev, pdata->shutdown_gpio, |
| 127 | rfkill->shutdown_name); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 128 | if (ret) { |
| 129 | pr_warn("%s: failed to get shutdown gpio.\n", __func__); |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 130 | return ret; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type, |
Wei Yongjun | 06f95e6 | 2013-04-18 10:31:16 +0800 | [diff] [blame] | 135 | &rfkill_gpio_ops, rfkill); |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 136 | if (!rfkill->rfkill_dev) |
| 137 | return -ENOMEM; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 138 | |
| 139 | ret = rfkill_register(rfkill->rfkill_dev); |
| 140 | if (ret < 0) |
Heikki Krogerus | 5e7ca39 | 2013-10-16 13:53:39 +0300 | [diff] [blame] | 141 | return ret; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 142 | |
| 143 | platform_set_drvdata(pdev, rfkill); |
| 144 | |
| 145 | dev_info(&pdev->dev, "%s device registered.\n", pdata->name); |
| 146 | |
| 147 | return 0; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int rfkill_gpio_remove(struct platform_device *pdev) |
| 151 | { |
| 152 | struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev); |
Sangwook Lee | e209c5a | 2011-09-29 12:57:17 +0100 | [diff] [blame] | 153 | struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data; |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 154 | |
Sangwook Lee | e209c5a | 2011-09-29 12:57:17 +0100 | [diff] [blame] | 155 | if (pdata->gpio_runtime_close) |
| 156 | pdata->gpio_runtime_close(pdev); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 157 | rfkill_unregister(rfkill->rfkill_dev); |
| 158 | rfkill_destroy(rfkill->rfkill_dev); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static struct platform_driver rfkill_gpio_driver = { |
| 164 | .probe = rfkill_gpio_probe, |
Bill Pemberton | 9e2b29d | 2012-12-03 09:56:26 -0500 | [diff] [blame] | 165 | .remove = rfkill_gpio_remove, |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 166 | .driver = { |
| 167 | .name = "rfkill_gpio", |
| 168 | .owner = THIS_MODULE, |
| 169 | }, |
| 170 | }; |
| 171 | |
Axel Lin | 98ef55f6 | 2011-11-28 17:15:04 +0800 | [diff] [blame] | 172 | module_platform_driver(rfkill_gpio_driver); |
Rhyland Klein | 7176ba2 | 2011-05-16 14:41:48 -0700 | [diff] [blame] | 173 | |
| 174 | MODULE_DESCRIPTION("gpio rfkill"); |
| 175 | MODULE_AUTHOR("NVIDIA"); |
| 176 | MODULE_LICENSE("GPL"); |