2011-05-17 05:41:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011, NVIDIA Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/rfkill.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#include <linux/rfkill-gpio.h>
|
|
|
|
|
|
|
|
struct rfkill_gpio_data {
|
|
|
|
struct rfkill_gpio_platform_data *pdata;
|
|
|
|
struct rfkill *rfkill_dev;
|
|
|
|
char *reset_name;
|
|
|
|
char *shutdown_name;
|
2013-10-16 18:53:40 +08:00
|
|
|
struct clk *clk;
|
|
|
|
bool clk_enabled;
|
2011-05-17 05:41:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int rfkill_gpio_set_power(void *data, bool blocked)
|
|
|
|
{
|
|
|
|
struct rfkill_gpio_data *rfkill = data;
|
|
|
|
|
|
|
|
if (blocked) {
|
|
|
|
if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
|
2013-10-16 18:53:41 +08:00
|
|
|
gpio_set_value(rfkill->pdata->shutdown_gpio, 0);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (gpio_is_valid(rfkill->pdata->reset_gpio))
|
2013-10-16 18:53:41 +08:00
|
|
|
gpio_set_value(rfkill->pdata->reset_gpio, 0);
|
2013-10-16 18:53:40 +08:00
|
|
|
if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
|
|
|
|
clk_disable(rfkill->clk);
|
2011-05-17 05:41:48 +08:00
|
|
|
} else {
|
2013-10-16 18:53:40 +08:00
|
|
|
if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
|
|
|
|
clk_enable(rfkill->clk);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (gpio_is_valid(rfkill->pdata->reset_gpio))
|
2013-10-16 18:53:41 +08:00
|
|
|
gpio_set_value(rfkill->pdata->reset_gpio, 1);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
|
2013-10-16 18:53:41 +08:00
|
|
|
gpio_set_value(rfkill->pdata->shutdown_gpio, 1);
|
2011-05-17 05:41:48 +08:00
|
|
|
}
|
|
|
|
|
2013-10-16 18:53:40 +08:00
|
|
|
rfkill->clk_enabled = blocked;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rfkill_ops rfkill_gpio_ops = {
|
|
|
|
.set_block = rfkill_gpio_set_power,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rfkill_gpio_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct rfkill_gpio_data *rfkill;
|
|
|
|
struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
|
|
|
|
int ret = 0;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (!pdata) {
|
|
|
|
pr_warn("%s: No platform data specified\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure at-least one of the GPIO is defined and that
|
|
|
|
* a name is specified for this instance */
|
|
|
|
if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) &&
|
|
|
|
!gpio_is_valid(pdata->shutdown_gpio))) {
|
|
|
|
pr_warn("%s: invalid platform data\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-10-16 18:53:39 +08:00
|
|
|
rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (!rfkill)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-09-29 19:57:17 +08:00
|
|
|
if (pdata->gpio_runtime_setup) {
|
|
|
|
ret = pdata->gpio_runtime_setup(pdev);
|
|
|
|
if (ret) {
|
|
|
|
pr_warn("%s: can't set up gpio\n", __func__);
|
2013-10-16 18:53:39 +08:00
|
|
|
return ret;
|
2011-09-29 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-17 05:41:48 +08:00
|
|
|
rfkill->pdata = pdata;
|
|
|
|
|
|
|
|
len = strlen(pdata->name);
|
2013-10-16 18:53:39 +08:00
|
|
|
rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
|
|
|
|
if (!rfkill->reset_name)
|
|
|
|
return -ENOMEM;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
2013-10-16 18:53:39 +08:00
|
|
|
rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
|
|
|
|
if (!rfkill->shutdown_name)
|
|
|
|
return -ENOMEM;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name);
|
|
|
|
snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name);
|
|
|
|
|
2013-10-16 18:53:40 +08:00
|
|
|
rfkill->clk = devm_clk_get(&pdev->dev, pdata->power_clk_name);
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
if (gpio_is_valid(pdata->reset_gpio)) {
|
2013-10-16 18:53:41 +08:00
|
|
|
ret = devm_gpio_request_one(&pdev->dev, pdata->reset_gpio,
|
|
|
|
0, rfkill->reset_name);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (ret) {
|
|
|
|
pr_warn("%s: failed to get reset gpio.\n", __func__);
|
2013-10-16 18:53:39 +08:00
|
|
|
return ret;
|
2011-05-17 05:41:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gpio_is_valid(pdata->shutdown_gpio)) {
|
2013-10-16 18:53:41 +08:00
|
|
|
ret = devm_gpio_request_one(&pdev->dev, pdata->shutdown_gpio,
|
|
|
|
0, rfkill->shutdown_name);
|
2011-05-17 05:41:48 +08:00
|
|
|
if (ret) {
|
|
|
|
pr_warn("%s: failed to get shutdown gpio.\n", __func__);
|
2013-10-16 18:53:39 +08:00
|
|
|
return ret;
|
2011-05-17 05:41:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type,
|
2013-04-18 10:31:16 +08:00
|
|
|
&rfkill_gpio_ops, rfkill);
|
2013-10-16 18:53:39 +08:00
|
|
|
if (!rfkill->rfkill_dev)
|
|
|
|
return -ENOMEM;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
ret = rfkill_register(rfkill->rfkill_dev);
|
|
|
|
if (ret < 0)
|
2013-10-16 18:53:39 +08:00
|
|
|
return ret;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, rfkill);
|
|
|
|
|
|
|
|
dev_info(&pdev->dev, "%s device registered.\n", pdata->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rfkill_gpio_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
|
2011-09-29 19:57:17 +08:00
|
|
|
struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
|
2011-05-17 05:41:48 +08:00
|
|
|
|
2011-09-29 19:57:17 +08:00
|
|
|
if (pdata->gpio_runtime_close)
|
|
|
|
pdata->gpio_runtime_close(pdev);
|
2011-05-17 05:41:48 +08:00
|
|
|
rfkill_unregister(rfkill->rfkill_dev);
|
|
|
|
rfkill_destroy(rfkill->rfkill_dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver rfkill_gpio_driver = {
|
|
|
|
.probe = rfkill_gpio_probe,
|
2012-12-03 22:56:26 +08:00
|
|
|
.remove = rfkill_gpio_remove,
|
2011-05-17 05:41:48 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "rfkill_gpio",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-11-28 17:15:04 +08:00
|
|
|
module_platform_driver(rfkill_gpio_driver);
|
2011-05-17 05:41:48 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("gpio rfkill");
|
|
|
|
MODULE_AUTHOR("NVIDIA");
|
|
|
|
MODULE_LICENSE("GPL");
|