/*
 * Return:
 *	0:	power on successfully
 *	others: power on failed
 */
int platform_wifi_power_on(void)
{
	int ret = 0;

#ifdef CONFIG_MMC
	{
		script_item_u val;
		script_item_value_type_e type;

#ifdef CONFIG_PLATFORM_ARM_SUN8I_W5P1
		unsigned int mod_sel = get_rf_mod_type();
#else
		unsigned int mod_sel = wifi_pm_get_mod_type();
#endif

		type = script_get_item("wifi_para", "wifi_sdc_id", &val);
		if (SCIRPT_ITEM_VALUE_TYPE_INT != type) {
			RTW_INFO("get wifi_sdc_id failed\n");
			ret = -1;
		} else {
			sdc_id = val.val;
			RTW_INFO("----- %s sdc_id: %d, mod_sel: %d\n", __FUNCTION__, sdc_id, mod_sel);

#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I)
			sw_mci_rescan_card(sdc_id, 1);
#elif defined(CONFIG_PLATFORM_ARM_SUN8I)
			sunxi_mci_rescan_card(sdc_id, 1);
#endif
			mdelay(100);
			wifi_pm_power(1);

			RTW_INFO("%s: power up, rescan card.\n", __FUNCTION__);
		}

#ifdef CONFIG_GPIO_WAKEUP
#ifdef CONFIG_PLATFORM_ARM_SUN8I_W5P1
		type = script_get_item("wifi_para", "wl_host_wake", &val);
#else
#ifdef CONFIG_RTL8723B
		type = script_get_item("wifi_para", "rtl8723bs_wl_host_wake", &val);
#endif
#ifdef CONFIG_RTL8188E
		type = script_get_item("wifi_para", "rtl8189es_host_wake", &val);
#endif
#endif /* CONFIG_PLATFORM_ARM_SUN8I_W5P1 */
		if (SCIRPT_ITEM_VALUE_TYPE_PIO != type) {
			RTW_INFO("No definition of wake up host PIN\n");
			ret = -1;
		} else {
			gpio_eint_wlan = val.gpio.gpio;
#ifdef CONFIG_PLATFORM_ARM_SUN8I
			oob_irq = gpio_to_irq(gpio_eint_wlan);
#endif
		}
#endif /* CONFIG_GPIO_WAKEUP */
	}
#endif /* CONFIG_MMC */

	return ret;
}
void platform_wifi_power_off(void)
{
#ifdef CONFIG_MMC
	wifi_pm_power(0);
#if defined(CONFIG_PLATFORM_ARM_SUN6I) ||defined(CONFIG_PLATFORM_ARM_SUN7I)
	sw_mci_rescan_card(sdc_id, 0);
#elif defined(CONFIG_PLATFORM_ARM_SUN8I)
	sunxi_mci_rescan_card(sdc_id, 0);
#endif
	DBG_871X("%s: remove card, power off.\n", __FUNCTION__);
#endif // CONFIG_MMC
}
/*
 * Return:
 *	0:	power on successfully
 *	others: power on failed
 */
int platform_wifi_power_on(void)
{
	int ret = 0;

#ifdef CONFIG_MMC
{
	script_item_u val;
	script_item_value_type_e type;

	type = script_get_item("wifi_para", "wifi_used", &val);
	if ((SCIRPT_ITEM_VALUE_TYPE_INT != type) || (0 == val.val)) {
		DBG_871X("wifi is not used\n");
		return -1;
	}

	type = script_get_item("wifi_para", "wifi_sdc_id", &val);
	if (SCIRPT_ITEM_VALUE_TYPE_INT!=type) {
		DBG_871X("get wifi_sdc_id failed\n");
		ret = -1;
	} else {
		sdc_id = val.val;
		DBG_871X("----- %s sdc_id: %d\n", __FUNCTION__, sdc_id);

#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I)
		sw_mci_rescan_card(sdc_id, 1);
#elif defined(CONFIG_PLATFORM_ARM_SUN8I)
		sunxi_mci_rescan_card(sdc_id, 1);
#endif
		mdelay(100);
		wifi_pm_power(1);

		DBG_871X("%s: power up, rescan card.\n", __FUNCTION__);
	}

#ifdef CONFIG_GPIO_WAKEUP
	type = script_get_item("wifi_para", "wl_host_wake", &val);
	if (SCIRPT_ITEM_VALUE_TYPE_PIO != type) {
		DBG_871X("No definition of wake up host PIN\n");
		ret = -1;
	} else {
		gpio_eint_wlan = val.gpio.gpio;
#ifdef CONFIG_PLATFORM_ARM_SUN8I
		oob_irq = gpio_to_irq(gpio_eint_wlan);
#endif
	}
#endif // CONFIG_GPIO_WAKEUP
}
#endif // CONFIG_MMC

	return ret;
}
void platform_wifi_power_off(void)
{
#ifdef CONFIG_MMC
#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I)
	sw_mci_rescan_card(sdc_id, 0);
#elif defined(CONFIG_PLATFORM_ARM_SUN8I)
	sunxi_mci_rescan_card(sdc_id, 0);
#endif
	mdelay(100);
	wifi_pm_power(0);

	RTW_INFO("%s: remove card, power off.\n", __FUNCTION__);
#endif /* CONFIG_MMC */
}
/*
 * Return:
 *	0:	power on successfully
 *	others: power on failed
 */
int platform_wifi_power_on(void)
{
	int ret = 0;

#ifdef CONFIG_MMC
{
	script_item_u val;
	script_item_value_type_e type;

	unsigned int mod_sel = wifi_pm_get_mod_type();

	type = script_get_item("wifi_para", "wifi_sdc_id", &val);
	if (SCIRPT_ITEM_VALUE_TYPE_INT!=type) {
		DBG_871X("get wifi_sdc_id failed\n");
		ret = -1;
	} else {
		sdc_id = val.val;
		DBG_871X("----- %s sdc_id: %d, mod_sel: %d\n", __FUNCTION__, sdc_id, mod_sel);
		wifi_pm_power(1);
		mdelay(10);
#if defined(CONFIG_PLATFORM_ARM_SUN6I) || defined(CONFIG_PLATFORM_ARM_SUN7I)
		sw_mci_rescan_card(sdc_id, 1);
#elif defined(CONFIG_PLATFORM_ARM_SUN8I)
		sunxi_mci_rescan_card(sdc_id, 1);
#endif
		DBG_871X("%s: power up, rescan card.\n", __FUNCTION__);
	}

#ifdef CONFIG_GPIO_WAKEUP
	type = script_get_item("wifi_para", "rtl8723bs_wl_host_wake", &val);
	if (SCIRPT_ITEM_VALUE_TYPE_PIO != type) {
		DBG_871X("has no rtl8723bs_wl_wake_host\n");
		ret = -1;
	} else {
		gpio_eint_wlan = val.gpio.gpio;
#ifdef CONFIG_PLATFORM_ARM_SUN8I
		oob_irq = gpio_to_irq(gpio_eint_wlan);
#endif
	}
#endif // CONFIG_GPIO_WAKEUP
}
#endif // CONFIG_MMC

	return ret;
}
/* Customer function to control hw specific wlan gpios */
void
dhd_customer_gpio_wlan_ctrl(int onoff)
{
    static int first = 1;
    static int sdc_id = 1;

    script_item_value_type_e type;
    script_item_u val;

    if (first == 1) {
        type = script_get_item("wifi_para", "wifi_sdc_id", &val);
        if (SCIRPT_ITEM_VALUE_TYPE_INT != type) {
            WL_ERROR(("failed to fetch sdio card's sdcid\n"));
            return -1;
        }
        sdc_id = val.val;
        first = 0;
    }

    switch (onoff) {
    case WLAN_RESET_OFF:
        WL_TRACE(("%s: call customer specific GPIO to insert WLAN RESET\n",
                  __FUNCTION__));
#ifdef CUSTOMER_HW
        wifi_pm_power(0);
#endif /* CUSTOMER_HW */
#if defined(CUSTOMER_HW2)
        wifi_set_power(0, 0);
#endif
        mdelay(100);
        WL_ERROR(("=========== WLAN placed in RESET ========\n"));
        break;

    case WLAN_RESET_ON:
        WL_TRACE(("%s: callc customer specific GPIO to remove WLAN RESET\n",
                  __FUNCTION__));
#ifdef CUSTOMER_HW
        wifi_pm_power(1);
#endif /* CUSTOMER_HW */
#if defined(CUSTOMER_HW2)
        wifi_set_power(1, 0);
#endif
        mdelay(100);
        WL_ERROR(("=========== WLAN going back to live  ========\n"));
        break;

    case WLAN_POWER_OFF:
        WL_TRACE(("%s: call customer specific GPIO to turn off WL_REG_ON\n",
                  __FUNCTION__));
#ifdef CUSTOMER_HW
        wifi_pm_power(0);
        sw_mci_rescan_card(sdc_id, 0);
#endif /* CUSTOMER_HW */
        WL_ERROR(("=========== WLAN placed in POWER OFF ========\n"));
        break;

    case WLAN_POWER_ON:
        WL_TRACE(("%s: call customer specific GPIO to turn on WL_REG_ON\n",
                  __FUNCTION__));
#ifdef CUSTOMER_HW
        wifi_pm_power(1);
        sw_mci_rescan_card(sdc_id, 1);
        /* Lets customer power to get stable */
#endif /* CUSTOMER_HW */
        mdelay(100);
        WL_ERROR(("=========== WLAN placed in POWER ON ========\n"));
        break;
    }
}