blob: ed5c36f0e82a0a2b3c33f105861c781e28db1cda [file] [log] [blame]
/*
* rfkill power contorl for wlan/bt on ASR platform
*
* Copyright (C) 2018 ASR, Inc.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/mmc/host.h>
#include <linux/platform_data/asr_sdhci.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/suspend.h>
#include <linux/clk.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/edge_wakeup_mmp.h>
#include <linux/regulator/consumer.h>
#include <linux/asr_rfkill.h>
#include "../mmc/host/sdhci.h"
#define ASR_DEV_NAME "asr-rfkill"
/*
* Define optimum max current to 200mA while min current to 0.
* They will be handled in regulator_set_optimum_mode and
* enable or disable the regulator sleep mode accordingly.
*/
#define OPTIMUM_MAX_CURRENT 200000
#define OPTIMUM_MIN_CURRENT 0
#define ASR_MAX_SDIO_HOST 2
static int sdio_rfkill_id;
struct asr_rfkill_platform_data *rfkill_plat[ASR_MAX_SDIO_HOST];
static DEFINE_MUTEX(asr_pwr_mutex);
static void wlan_edge_wakeup(int irq, void *p_rsv)
{
pr_debug("wlan_edge_wakeup event +++++\n");
}
static void asr_rfkill_platform_data_init(
struct asr_rfkill_platform_data *pdata)
{
/* all intems are invalid just after alloc */
pdata->gpio_power_down = -1;
pdata->gpio_reset = -1;
pdata->gpio_edge_wakeup = -1;
pdata->gpio_3v3_en = -1;
pdata->gpio_1v8_en = -1;
pdata->gpio_wakeup_device = -1;
pdata->wib_3v3 = NULL;
pdata->wib_1v8 = NULL;
pdata->wib_sdio_1v8 = NULL;
/*for issue mmc card_detection interrupt */
pdata->mmc = NULL;
/* power status, unknown status at first */
pdata->is_on = -1;
}
static struct asr_rfkill_platform_data
*asr_rfkill_platform_data_alloc(struct platform_device *pdev)
{
struct asr_rfkill_platform_data *pdata;
/* create a new one and init it */
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (pdata) {
asr_rfkill_platform_data_init(pdata);
return pdata;
}
return NULL;
}
/*
* For asr_wlan device, there are two power: supply 1v8 and 3v3
*
* If 18v and 3v3 can't be both disabled during power off stage,
* it is recommand to keep them both on and disable pd-gpio only
* TODO: add flags to check gpio active level
*/
static int asr_1v8_3v3_control(struct asr_rfkill_platform_data *pdata, int on)
{
int gpio_3v3_en = pdata->gpio_3v3_en;
int gpio_1v8_en = pdata->gpio_1v8_en;
if (gpio_3v3_en >= 0) {
if (gpio_request(gpio_3v3_en, "asr_wlan 3v3 on")) {
pr_info("gpio %d request failed\n", gpio_3v3_en);
return -1;
}
}
if (gpio_1v8_en >= 0) {
if (gpio_request(gpio_1v8_en, "asr_wlan 1v8 on")) {
pr_info("gpio %d request failed\n", gpio_1v8_en);
if (gpio_3v3_en >= 0)
gpio_free(gpio_3v3_en);
return -1;
}
}
if (on) {
if (gpio_1v8_en >= 0)
gpio_direction_output(gpio_1v8_en, 1);
if (gpio_3v3_en >= 0)
gpio_direction_output(gpio_3v3_en, 1);
if (pdata->wib_3v3) {
if (regulator_set_voltage(pdata->wib_3v3, 3300000, 3300000))
pr_err("fail to set regulator wib_3v3 to 3.3v\n");
if (!regulator_is_enabled(pdata->wib_3v3) &&
regulator_enable(pdata->wib_3v3))
pr_err("fail to enable regulator wib_3v3\n");
}
if (pdata->wib_1v8) {
if (regulator_set_voltage(pdata->wib_1v8, 1800000, 1800000))
pr_err("fail to set regulator wib_1v8 to 1.8v\n");
if (!regulator_is_enabled(pdata->wib_1v8) &&
regulator_enable(pdata->wib_1v8))
pr_err("fail to enable regulator wib_1v8\n");
}
if (pdata->wib_sdio_1v8) {
if (regulator_set_voltage(pdata->wib_sdio_1v8, 1800000, 1800000))
pr_err("fail to set regulator wib_sdio_1v8 to 1.8v\n");
if (!regulator_is_enabled(pdata->wib_sdio_1v8) &&
regulator_enable(pdata->wib_sdio_1v8))
pr_err("fail to enable regulator wib_sdio_1v8\n");
}
} else {
if (gpio_1v8_en >= 0)
gpio_direction_output(gpio_1v8_en, 0);
if (gpio_3v3_en >= 0)
gpio_direction_output(gpio_3v3_en, 0);
if (pdata->wib_3v3) {
if (regulator_is_enabled(pdata->wib_3v3) &&
regulator_disable(pdata->wib_3v3))
pr_err("fail to disable regulator wib_3v3\n");
}
if (pdata->wib_1v8) {
if (regulator_is_enabled(pdata->wib_1v8) &&
regulator_disable(pdata->wib_1v8))
pr_err("fail to disable regulator wib_1v8\n");
}
if (pdata->wib_sdio_1v8) {
if (regulator_is_enabled(pdata->wib_sdio_1v8) &&
regulator_disable(pdata->wib_sdio_1v8))
pr_err("fail to disable regulator wib_sdio_1v8\n");
}
}
if (gpio_3v3_en >= 0)
gpio_free(gpio_3v3_en);
if (gpio_1v8_en >= 0)
gpio_free(gpio_1v8_en);
return 0;
}
/*
* Power on/off asr_wlan by control GPIO or regulator
* TODO: fine tune the function if need, like adding regulator
* support
*
*/
static int asr_pwr_ctrl(struct asr_rfkill_platform_data *pdata, int on)
{
int gpio_power_down = pdata->gpio_power_down;
int gpio_reset = pdata->gpio_reset;
int host_wakeup_wlan_gpio = pdata->gpio_wakeup_device;
int ret = 0;
pr_info("%s(%s): on=%d\n", __func__, mmc_hostname(pdata->mmc), on);
if (gpio_power_down >= 0) {
if (gpio_request(gpio_power_down, "asr_wlan power down")) {
pr_info("gpio %d request failed\n", gpio_power_down);
return -1;
}
}
if (gpio_reset >= 0) {
if (gpio_request(gpio_reset, "asr_wlan reset")) {
pr_info("gpio %d request failed\n", gpio_reset);
ret = -1;
goto err_reset;
}
}
if (host_wakeup_wlan_gpio >= 0) {
if (gpio_request(host_wakeup_wlan_gpio, "asr_wlan host wakeup")) {
pr_info("gpio %d request failed\n", host_wakeup_wlan_gpio);
ret = -1;
goto err_wakeup;
}
}
if (on) {
if (host_wakeup_wlan_gpio >= 0) {
msleep(1);
gpio_direction_output(host_wakeup_wlan_gpio, 0);
msleep(1);
}
asr_1v8_3v3_control(pdata, 1);
if (gpio_reset >= 0) {
gpio_direction_output(gpio_reset, 0);
msleep(1);
gpio_direction_output(gpio_reset, 1);
}
if (gpio_power_down >= 0) {
msleep(1);
gpio_direction_output(gpio_power_down, 1);
}
msleep(10);
} else {
if (gpio_power_down >= 0)
gpio_direction_output(gpio_power_down, 0);
if (gpio_reset >= 0)
gpio_direction_output(gpio_reset, 0);
asr_1v8_3v3_control(pdata, 0);
}
if (host_wakeup_wlan_gpio >= 0)
gpio_free(host_wakeup_wlan_gpio);
err_wakeup:
if (gpio_reset >= 0)
gpio_free(gpio_reset);
err_reset:
if (gpio_power_down >= 0)
gpio_free(gpio_power_down);
return 0;
}
static int asr_trigger_mmc_detect(struct mmc_host *host)
{
unsigned long timeout = msecs_to_jiffies(10 * 1000); //HoopoeL need more than 5seconds cost by rf calibration
unsigned long ret = 0;
unsigned long flags;
int retry = 2;
BUG_ON(!host);
while (retry--) {
spin_lock_irqsave(&host->lock, flags);
host->rescan_entered = 0;
spin_unlock_irqrestore(&host->lock, flags);
ret = mmc_detect_change_sync(host, msecs_to_jiffies(10),
timeout);
if (!ret)
pr_warn("mmc detect has taken %u ms,something wrong\n",
jiffies_to_msecs(timeout));
if (host->card)
return 0;
pr_info("Retry mmc detection\n");
}
return -EBUSY;
}
static int asr_pwr_on(struct asr_rfkill_platform_data *pdata)
{
int ret = 0;
struct platform_device *pdev;
struct asr_sdhci_platdata *sdhci_pdata;
struct sdhci_host *host;
if (pdata->is_on)
return 0;
if (!pdata->mmc)
return -EINVAL;
pr_info("asr_wlan(%s) set_power on start\n", mmc_hostname(pdata->mmc));
pdev = to_platform_device(mmc_dev(pdata->mmc));
sdhci_pdata = pdev->dev.platform_data;
host = mmc_priv(pdata->mmc);
if (pdata->set_power)
pdata->set_power(1);
/*
* As we known, if we want to support Card Interrupt well, SDIO Host's
* Main Source CLK should be free running.
*
* But if PM Runtime is enabled, the host's RPM callback may dynamic
* on/off the SDIO Host's SRC CLK
*
* Here we call clk_prepare_enable to make sure Source Clock will not
* be disabled untill is called again
*
*/
/* First: power up asr_wlan device */
asr_pwr_ctrl(pdata, 1);
/* Second: check whether sdio device can be detected */
if (pdata->mmc) {
ret = asr_trigger_mmc_detect(pdata->mmc);
if (ret)
pr_err("%s: fail to detect SDIO device\n",
mmc_hostname(pdata->mmc));
else
pr_info("%s: succeed to detect SDIO device\n",
mmc_hostname(pdata->mmc));
}
/* Last: save power on status after detection */
if (!ret) {
pdata->is_on = 1;
if (pdata->gpio_edge_wakeup >= 0) {
request_mfp_edge_wakeup(pdata->gpio_edge_wakeup,
wlan_edge_wakeup, NULL, pdata->mmc->parent);
}
if (pdata->pinctrl && pdata->pin_on)
pinctrl_select_state(pdata->pinctrl, pdata->pin_on);
} else {
pdata->is_on = 0;
/* roll back if fail */
asr_pwr_ctrl(pdata, 0);
if (pdata->set_power)
pdata->set_power(0);
if (pdata->pinctrl && pdata->pin_off)
pinctrl_select_state(pdata->pinctrl, pdata->pin_off);
}
pr_info("asr_wlan(%s) set_power on end\n", mmc_hostname(pdata->mmc));
return ret;
}
static int asr_pwr_off(struct asr_rfkill_platform_data *pdata)
{
int ret = 0;
struct sdhci_host *host;
struct platform_device *pdev;
struct asr_sdhci_platdata *sdhci_pdata;
if (!pdata->is_on)
return 0;
if (!pdata->mmc)
return -EINVAL;
pr_info("asr_wlan(%s) set_power off start\n", mmc_hostname(pdata->mmc));
host = mmc_priv(pdata->mmc);
pdev = to_platform_device(mmc_dev(pdata->mmc));
sdhci_pdata = pdev->dev.platform_data;
/* First: tell SDIO bus the device will be power off */
if (host->mmc && host->mmc->card)
mmc_disable_sdio(host->mmc);
/* Second: power off asr_wlan device */
asr_pwr_ctrl(pdata, 0);
if (pdata->pinctrl && pdata->pin_off)
pinctrl_select_state(pdata->pinctrl, pdata->pin_off);
/* TODO: check sdio device can't be detected indeed */
/*
* Last: update the status
*/
if (pdata->gpio_edge_wakeup >= 0)
remove_mfp_edge_wakeup(pdata->gpio_edge_wakeup);
if (pdata->set_power)
pdata->set_power(0);
pdata->is_on = 0;
pr_info("asr_wlan(%s) set_power off end\n", mmc_hostname(pdata->mmc));
return ret;
}
int asr_sdio_card_enable(int pwr_ctrl)
{
struct asr_rfkill_platform_data *pdata;
int i;
mutex_lock(&asr_pwr_mutex);
for (i = 0; i < ASR_MAX_SDIO_HOST; i++) {
pdata = rfkill_plat[i];
if (pdata) {
if (pwr_ctrl)
asr_pwr_on(pdata);
else
asr_pwr_off(pdata);
}
}
mutex_unlock(&asr_pwr_mutex);
return 0;
}
EXPORT_SYMBOL(asr_sdio_card_enable);
static ssize_t asr_pwr_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int len = 0;
struct asr_rfkill_platform_data *pdata = dev->platform_data;
mutex_lock(&asr_pwr_mutex);
len = sprintf(buf, "asr_wlan(%s) Device is power %s\n",
mmc_hostname(pdata->mmc), pdata->is_on ? "on" : "off");
mutex_unlock(&asr_pwr_mutex);
return (ssize_t)len;
}
static ssize_t asr_pwr_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
int pwr_ctrl;
int valid_ctrl = 0;
struct asr_rfkill_platform_data *pdata = dev->platform_data;
mutex_lock(&asr_pwr_mutex);
if (sscanf(buf, "%d", &pwr_ctrl) == 1) {
if ((pwr_ctrl == 1) || (pwr_ctrl == 0))
valid_ctrl = 1;
}
if (valid_ctrl != 1) {
pr_err("Please input valid ctrl: 0: Close, 1: Open\n");
mutex_unlock(&asr_pwr_mutex);
return size;
}
if (pwr_ctrl)
asr_pwr_on(pdata);
else
asr_pwr_off(pdata);
pr_info("Now asr_wlan(%s) Device is power %s\n",
mmc_hostname(pdata->mmc), pdata->is_on ? "on" : "off");
mutex_unlock(&asr_pwr_mutex);
return size;
}
static DEVICE_ATTR(pwr_ctrl, S_IRUGO | S_IWUSR, asr_pwr_show, asr_pwr_store);
#ifdef CONFIG_OF
static const struct of_device_id asr_rfkill_of_match[] = {
{
.compatible = "asr,asr-rfkill",
},
{},
};
MODULE_DEVICE_TABLE(of, asr_rfkill_of_match);
static int asr_rfkill_probe_dt(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node, *sdh_np;
struct asr_rfkill_platform_data *pdata = pdev->dev.platform_data;
struct platform_device *sdh_pdev;
struct sdhci_host *host;
int sdh_phandle, gpio;
struct regulator *wib_3v3, *wib_1v8, *wib_sdio_1v8;
/* Get PD/RST pins status */
pdata->pinctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR(pdata->pinctrl)) {
pdata->pinctrl = NULL;
dev_warn(&pdev->dev, "could not get PD/RST pinctrl.\n");
} else {
pdata->pin_off = pinctrl_lookup_state(pdata->pinctrl, "off");
if (IS_ERR(pdata->pin_off)) {
pdata->pin_off = NULL;
dev_err(&pdev->dev, "could not get off pinstate.\n");
}
pdata->pin_on = pinctrl_lookup_state(pdata->pinctrl, "on");
if (IS_ERR(pdata->pin_on)) {
pdata->pin_on = NULL;
dev_err(&pdev->dev, "could not get on pinstate.\n");
}
}
if (pdata->pinctrl && pdata->pin_off)
pinctrl_select_state(pdata->pinctrl, pdata->pin_off);
if (of_property_read_u32(np, "sd-host", &sdh_phandle)) {
dev_err(&pdev->dev, "failed to find sd-host in dt\n");
return -1;
}
/* we've got the phandle for sdh */
sdh_np = of_find_node_by_phandle(sdh_phandle);
if (unlikely(IS_ERR(sdh_np))) {
dev_err(&pdev->dev, "failed to find device_node for sdh\n");
return -1;
}
sdh_pdev = of_find_device_by_node(sdh_np);
if (unlikely(IS_ERR(sdh_pdev))) {
dev_err(&pdev->dev, "failed to find platform_device for sdh\n");
return -1;
}
/* sdh_pdev->dev->driver_data was set as sdhci_host in sdhci driver */
host = platform_get_drvdata(sdh_pdev);
/*
* If we cannot find host, it's because sdh device is not registered
* yet. Probe again later.
*/
if (!host)
return -EPROBE_DEFER;
pdata->mmc = host->mmc;
if (sdio_rfkill_id >= ASR_MAX_SDIO_HOST)
dev_warn(&pdev->dev, "exceed max sdio host number\n");
else
rfkill_plat[sdio_rfkill_id++] = pdata;
/* get gpios from dt */
gpio = of_get_named_gpio(np, "edge-wakeup-gpio", 0);
if (unlikely(gpio < 0)) {
dev_warn(&pdev->dev, "edge-wakeup-gpio undefined\n");
pdata->gpio_edge_wakeup = -1;
} else {
pdata->gpio_edge_wakeup = gpio;
}
gpio = of_get_named_gpio(np, "pd-gpio", 0);
if (unlikely(gpio < 0)) {
dev_err(&pdev->dev, "pd-gpio undefined\n");
pdata->gpio_power_down = -1;
} else {
pdata->gpio_power_down = gpio;
}
gpio = of_get_named_gpio(np, "rst-gpio", 0);
if (unlikely(gpio < 0)) {
printk(KERN_DEBUG "rst-gpio undefined\n");
pdata->gpio_reset = -1;
} else {
pdata->gpio_reset = gpio;
}
gpio = of_get_named_gpio(np, "3v3-ldo-gpio", 0);
if (unlikely(gpio < 0)) {
printk(KERN_DEBUG "3v3-ldo-gpio undefined\n");
pdata->gpio_3v3_en = -1;
} else {
pdata->gpio_3v3_en = gpio;
}
gpio = of_get_named_gpio(np, "1v8-ldo-gpio", 0);
if (unlikely(gpio < 0)) {
printk(KERN_DEBUG "1v8-ldo-gpio undefined\n");
pdata->gpio_1v8_en = -1;
} else {
pdata->gpio_1v8_en = gpio;
}
wib_3v3 = regulator_get_optional(&pdev->dev, "wib_3v3");
if (IS_ERR_OR_NULL(wib_3v3))
printk(KERN_DEBUG "%s: regulator wib_3v3 not found\n", __func__);
else
pdata->wib_3v3 = wib_3v3;
wib_1v8 = regulator_get_optional(&pdev->dev, "wib_1v8");
if (IS_ERR_OR_NULL(wib_1v8))
printk(KERN_DEBUG "%s: regulator wib_1v8 not found\n", __func__);
else
pdata->wib_1v8 = wib_1v8;
wib_sdio_1v8 = regulator_get_optional(&pdev->dev, "wib_sdio_1v8");
if (IS_ERR_OR_NULL(wib_sdio_1v8))
printk(KERN_DEBUG "%s: regulator wib_sdio_1v8 not found\n", __func__);
else
pdata->wib_sdio_1v8 = wib_sdio_1v8;
gpio = of_get_named_gpio(np, "host-wakeup-wlan-gpio", 0);
if (unlikely(gpio < 0)) {
pdata->gpio_wakeup_device = -1;
dev_err(&pdev->dev, "host-wakeup-wlan-gpio undefined\n");
} else {
pdata->gpio_wakeup_device = gpio;
printk(KERN_DEBUG "%s: host_wakeup_wlan_gpio %d\n", __func__, gpio);
}
return 0;
}
#else
static int asr_rfkill_probe_dt(struct platform_device *pdev)
{
return 0;
}
#endif
static int asr_rfkill_probe(struct platform_device *pdev)
{
struct asr_rfkill_platform_data *pdata = NULL;
struct device *dev = &pdev->dev;
/* flag: whether pdata is passed from platfrom_data */
int pdata_passed = 1;
const struct of_device_id *match = NULL;
int ret = -1;
/* make sure asr_rfkill_platform_data is valid */
pdata = pdev->dev.platform_data;
if (!pdata) {
/* if platfrom data do not pass the struct to us */
pdata_passed = 0;
pdata = asr_rfkill_platform_data_alloc(pdev);
if (!pdata) {
pr_err("can't get asr_rfkill_platform_data struct during probe\n");
goto err_pdata;
}
pdev->dev.platform_data = pdata;
}
pdata->clk_26m_out = devm_clk_get(dev, "wifi_26m");
if (!IS_ERR(pdata->clk_26m_out)) {
dev_info(dev, "enable 26M output for wifi\n");
clk_prepare_enable(pdata->clk_26m_out);
}
/* set value to asr_rfkill_platform_data if DT pass them to us */
#ifdef CONFIG_OF
match = of_match_device(of_match_ptr(asr_rfkill_of_match),
&pdev->dev);
#endif
if (match) {
ret = asr_rfkill_probe_dt(pdev);
if (ret)
goto err_dt;
}
pdata->is_on = 0;
asr_pwr_ctrl(pdata, 0);
/*
* Create a proc interface, allowing user space to control
* asr_wlan devices' power
*/
device_create_file(&pdev->dev, &dev_attr_pwr_ctrl);
return 0;
err_dt:
if (!pdata_passed)
pdev->dev.platform_data = NULL;
err_pdata:
return ret;
}
static int asr_rfkill_remove(struct platform_device *pdev)
{
struct asr_rfkill_platform_data *pdata;
pdata = pdev->dev.platform_data;
if (pdata->wib_3v3)
regulator_put(pdata->wib_3v3);
if (pdata->wib_1v8)
regulator_put(pdata->wib_1v8);
if (pdata->wib_sdio_1v8)
regulator_put(pdata->wib_sdio_1v8);
if (!IS_ERR(pdata->clk_26m_out))
clk_disable_unprepare(pdata->clk_26m_out);
return 0;
}
static int asr_rfkill_suspend(struct platform_device *pdev,
pm_message_t pm_state)
{
return 0;
}
static int asr_rfkill_resume(struct platform_device *pdev)
{
return 0;
}
static struct platform_driver asr_rfkill_platform_driver = {
.probe = asr_rfkill_probe,
.remove = asr_rfkill_remove,
.driver = {
.name = ASR_DEV_NAME,
.owner = THIS_MODULE,
#ifdef CONFIG_OF
.of_match_table = asr_rfkill_of_match,
#endif
},
.suspend = asr_rfkill_suspend,
.resume = asr_rfkill_resume,
};
static int __init asr_rfkill_init(void)
{
return platform_driver_register(&asr_rfkill_platform_driver);
}
static void __exit asr_rfkill_exit(void)
{
platform_driver_unregister(&asr_rfkill_platform_driver);
}
module_init(asr_rfkill_init);
module_exit(asr_rfkill_exit);
MODULE_ALIAS("platform:asr_rfkill");
MODULE_DESCRIPTION("asr_rfkill");
MODULE_AUTHOR("ASR");
MODULE_LICENSE("GPL");