/*
 * icx1087_usb_epson.c -- ICX1087 USB helper
 *
 * Copyright 2006 Sony 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */


#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/interrupt.h>

#include <linux/kernel.h>       /* printk() */
#include <linux/errno.h>        /* error codes */
#include <linux/types.h>        /* size_t */
#include <linux/fcntl.h>        /* O_ACCMODE */
#include <linux/seq_file.h>
#include <linux/cdev.h>
#include <linux/device.h>

#include <asm/arch/smu.h>
#include <asm/arch/pmu.h>
#include <asm/arch/pwc.h>
#include <asm/arch/gpiocore.h>
#include <asm/arch/icx1087_power.h>

#if 1
#define DEBUG_MSG(format, arg...) \
    printk(KERN_DEBUG "*** " format , ## arg)
#else
#define DEBUG_MSG(format, arg...)
#endif


#define PWC_IO04_MASK 0x10
#define DETUSB_MASK   0x20
#define USBINT_MASK   0x80


/* pin control */
#define EXTUSB_SMU_PCTL_CAM        0x00000006 /* GPIO16,GPIO17 */
#define EXTUSB_SMU_PCTL_CAM_MASK   0x0000001E
#define EXTUSB_SMU_PCTL_ASYNC      0x0000000C /* CSZ2,GPIO25,GPIO24,DMA(None) */
#define EXTUSB_SMU_PCTL_ASYNC_MASK 0x0000002F

/* gpio control */
#define EXTUSB_GPIO_LOW_EO      0x00000000 /* CPU GPIO 17 = out */
#define EXTUSB_GPIO_LOW_EI      0x00030000 /* CPU GPIO 16 = in */
#define EXTUSB_GPIO_LOW_EX_MASK 0x00030000
#define EXTUSB_PWC_IOSEL1       0x00       /* PWC GPIO 4 = in */
#define EXTUSB_PWC_IOSEL1_MASK  0x10
#define EXTUSB_PWC_IOSEL2       0x00       /* PWC GPIO 8 = in */
#define EXTUSB_PWC_IOSEL2_MASK  0x01

/* pull control */
#define EXTUSB_SMU_PULLCTL_CAM        0x00000000 /* GPIO16 = none, GPIO17 = none */
#define EXTUSB_SMU_PULLCTL_CAM_MASK   0x00001800
#define EXTUSB_SMU_PULLCTL_ASYNC      0x00000004 /* A24=down */
#define EXTUSB_SMU_PULLCTL_ASYNC_MASK 0x0040004C
#define EXTUSB_PWC_GPPUPD2            0x00       /* PWC GPIO 4 = none */
#define EXTUSB_PWC_GPPUPD2_MASK       0x03
#define EXTUSB_PWC_GPPUPD3            0x00       /* PWC GPIO 8 = none */
#define EXTUSB_PWC_GPPUPD3_MASK       0x03

/* clock control */
#define EXTUSB_SMU_GCLKCTRL2_ENA      0x00000200
#define EXTUSB_SMU_GCLKCTRL2_ENA_MASK 0x00000200
#define EXTUSB_SMU_GCLKCTRL2          0x00000200
#define EXTUSB_SMU_GCLKCTRL2_MASK     0x00000200
#define EXTUSB_SMU_AHBCLKCTRL1        0x00000000
#define EXTUSB_SMU_AHBCLKCTRL1_MASK   0x00000002

/* read/write control */
#define EXTUSB_GPIO_LOW_XRST    0x00020000
#define EXTUSB_GPIO_LOW_XRST_EN 0x00000000
#define EXTUSB_GPIO_LOW_XRST_DI 0x00020000
#define EXTUSB_GPIO_LOW_INT2    0x00010000
#define EXTUSB_IOIN1_INT1       0x10
#define EXTUSB_IOIN2_OK         0x01



int icx1087_usb_epson_enable_wakeup(int);
extern unsigned char s1r72xxx_get_status(void);


static unsigned int SmuPctlCam;
static unsigned int SmuPctlAsync;
static unsigned int GpioLowEo;
static unsigned int GpioLowEi;
static unsigned int PwcIosel1;
static unsigned int PwcIosel2;
static unsigned int SmuPullctlCam;
static unsigned int SmuPullctlAsync;
static unsigned int PwcGppupd2;
static unsigned int PwcGppupd3;
static unsigned int SmuAhbclkctrl1;

static int clk_ref_counter = 0;
static int vbus_high = 0;
static spinlock_t platform_lock;
static int _nSetAddress = 0;
static struct timer_list _dpdm_check_timer;
static int _nHandlerEnabled = 1;
static int _nUSB_AC_Adaptor = 0;

#define DPDM_CHECK_INTERVAL 300   /* 300ms */
static int _timer_delay = (DPDM_CHECK_INTERVAL * HZ) / 1000;




/**
 *
 */
void icx1087_usb_epson_enable_vbus_handler(int nEnable)
{
    printk("%s() nEnable=%d\n", __FUNCTION__, nEnable);
    _nHandlerEnabled = nEnable;
}


/**
 *
 */
void icx1087_usb_epson_set_vbus_irq_type(void)
{
    pwc_write(PWC_BMASK3, 0x00, USBINT_MASK);
    set_irq_type(INT_PWC_DETUSBINT, IRQT_RISING);
    pwc_write(PWC_CLRFACT3, USBINT_MASK, USBINT_MASK);
    pwc_write(PWC_BMASK3, USBINT_MASK, USBINT_MASK);
}


/**
 *
 */
int icx1087_usb_epson_start_timer(void)
{
    /* start timer to check Line State for USB_AC Adaptor */
    _dpdm_check_timer.expires = jiffies + _timer_delay;
    add_timer(&_dpdm_check_timer);
    return 0;
}



/**
 *
 */
static void _timeout_handler(unsigned long data)
{
    /* read DETUSB/DETADP etc */
    uint nRet = icx1087_get_power_status();
    
    /* Check SDETUSB */
    if (nRet & DETUSB_MASK) {
        unsigned char nLS = s1r72xxx_get_status();
        DEBUG_MSG("*** %s() nLS=0x%02x ***\n", __FUNCTION__, nLS);

        if (0x03 == (nLS & 0x03)) {
            DEBUG_MSG("*** USB_AC_ADAPTOR detected ***\n");
            _nUSB_AC_Adaptor = 1;
            icx1087_set_vbus_limit(500, 1);
        }
    }

}


/**
 *
 */
static int _update_det_mod(void)
{

    /* read DETUSB/DETADP etc */
    uint nRet = icx1087_get_power_status();

    /* Check SDETUSB */
    if (nRet & DETUSB_MASK) {
        /* VBUS = H */
        set_irq_type(INT_PWC_DETUSBINT, IRQT_FALLING);
    } else {
        /* VBUS = L */
        _nUSB_AC_Adaptor = 0;
        icx1087_set_vbus_limit(100, 0);
        set_irq_type(INT_PWC_DETUSBINT, IRQT_RISING);
    }

    return 0;
}



/*
 *
 */
static irqreturn_t _icx1087_usb_vbus_interrupt(int irq,
                                          void *dev_id,
                                          struct pt_regs *regs)
{
    DEBUG_MSG("*** %s() ***\n", __FUNCTION__);

    _nSetAddress = 0;

    if (0 == _nHandlerEnabled) {
        return IRQ_HANDLED;
    }
    
    _update_det_mod();
    icx1087_vbus_changed();
    return IRQ_HANDLED;
}


/*
 *
 */
static irqreturn_t _icx1087_usb_wakeup_interrupt(int irq,
                                                 void *dev_id,
                                                 struct pt_regs *regs)
{
    //DEBUG_MSG("*** %s() ***\n", __FUNCTION__);
    return IRQ_HANDLED;
}




/**
 *
 */
int icx1087_usb_epson_register_driver(void)
{
    /* setup timer */
    init_timer(&_dpdm_check_timer);
    _dpdm_check_timer.function = _timeout_handler;
    _dpdm_check_timer.data = 0;

    request_irq(INT_PWC_DETUSBINT,
                _icx1087_usb_vbus_interrupt,
                0,
                "icx1087_vbus",
                0);


    set_irq_type(INT_PWC_IO04, IRQT_RISING);

    request_irq(INT_PWC_IO04,
                _icx1087_usb_wakeup_interrupt,
                0,
                "icx1087_usb_wakeup",
                0);

    /* disable wakeup */
    icx1087_usb_epson_enable_wakeup(0);
    _update_det_mod();
    
    return 0;
}


/**
 *
 */
int icx1087_usb_epson_unregister_driver(void)
{
    return 0;
}



/**
 *
 */
int icx1087_usb_epson_enable_wakeup(int nEnable)
{
    if (0 == nEnable) {
        /* disable PWC_IO04 */
        pwc_write(PWC_FMASK1, 0x00, PWC_IO04_MASK);
        pwc_write(PWC_CLRFACT1, PWC_IO04_MASK, PWC_IO04_MASK);
        pwc_write(PWC_BMASK1, 0x00, PWC_IO04_MASK);
    } else {
        pwc_write(PWC_FMASK1, PWC_IO04_MASK, PWC_IO04_MASK);
        pwc_write(PWC_CLRFACT1, PWC_IO04_MASK, PWC_IO04_MASK);
        pwc_write(PWC_BMASK1, PWC_IO04_MASK, PWC_IO04_MASK);
    }

    return 0;
}

/**
 *
 */
int icx1087_usb_epson_check_vbus(void)
{

    if (ICX1087_POWER_STATUS_DETUSB &icx1087_get_power_status()) {
        return 1;
    } else {
        return 0;
    }
    
}


/**
 *
 */
int icx1087_usb_epson_notify_reset(void)
{
    _nSetAddress = 0;
    return 0;
}


/**
 *
 */
int icx1087_usb_epson_notify_set_address(void)
{
    _nSetAddress = 1;
    return 0;
}



/**
 * 1: Can enter DeepSleep
 * 0: Do not enter DeepSleep
 */
int icx1087_usb_epson_check_deep_sleep(void)
{
    if (0 == _nSetAddress) {
        return 0;
    }
    
    /* SetAddress received, then check VBUS */
    
    if (icx1087_usb_epson_check_vbus()) {
        return 1;
    }

    return 0;
    
}



/**
 *
 */
int icx1087_usb_epson_set_platform(void)
{
	uint64_t u64;

	/* init spin lock */
	spin_lock_init(&platform_lock);

	/* set pin control */
	mp200_pmu_read_reg(SMU_PCTL_CAM,&SmuPctlCam);
	mp200_pmu_write_reg(SMU_PCTL_CAM,EXTUSB_SMU_PCTL_CAM,EXTUSB_SMU_PCTL_CAM_MASK);
	mp200_pmu_read_reg(SMU_PCTL_ASYNC,&SmuPctlAsync);
	mp200_pmu_write_reg(SMU_PCTL_ASYNC,EXTUSB_SMU_PCTL_ASYNC,EXTUSB_SMU_PCTL_ASYNC_MASK);

	/* set gpio control */
	u64=mp200_gio_get_mode();
	GpioLowEo=(unsigned int)u64;
	GpioLowEi=~(unsigned int)u64;
	mp200_gio_set_mode_output_low32(EXTUSB_GPIO_LOW_EO);
	mp200_gio_set_mode_input_low32(EXTUSB_GPIO_LOW_EI);
	pwc_read(PWC_IOSEL1,&PwcIosel1);
	pwc_write(PWC_IOSEL1,EXTUSB_PWC_IOSEL1,EXTUSB_PWC_IOSEL1_MASK);
	pwc_read(PWC_IOSEL2,&PwcIosel2);
	pwc_write(PWC_IOSEL2,EXTUSB_PWC_IOSEL2,EXTUSB_PWC_IOSEL2_MASK);

	/* set pull control */
	mp200_pmu_read_reg(SMU_PULLCTL_CAM,&SmuPullctlCam);
	mp200_pmu_write_reg(SMU_PULLCTL_CAM,EXTUSB_SMU_PULLCTL_CAM,EXTUSB_SMU_PULLCTL_CAM_MASK);
	mp200_pmu_read_reg(SMU_PULLCTL_ASYNC,&SmuPullctlAsync);
	mp200_pmu_write_reg(SMU_PULLCTL_ASYNC,EXTUSB_SMU_PULLCTL_ASYNC,EXTUSB_SMU_PULLCTL_ASYNC_MASK);
	pwc_read(PWC_GPPUPD2,&PwcGppupd2);
	pwc_write(PWC_GPPUPD2,EXTUSB_PWC_GPPUPD2,EXTUSB_PWC_GPPUPD2_MASK);
	pwc_read(PWC_GPPUPD3,&PwcGppupd3);
	pwc_write(PWC_GPPUPD3,EXTUSB_PWC_GPPUPD3,EXTUSB_PWC_GPPUPD3_MASK);

	/* disable auto control */
	mp200_pmu_read_reg(SMU_AHBCLKCTRL1,&SmuAhbclkctrl1);
	mp200_pmu_write_reg(SMU_AHBCLKCTRL1,EXTUSB_SMU_AHBCLKCTRL1,EXTUSB_SMU_AHBCLKCTRL1_MASK);

#if 0
	printk("SMU_PCTL_CAM:      %08X\n",SmuPctlCam);
	printk("SMU_PCTL_ASYNC:    %08X\n",SmuPctlAsync);
	printk("GPIO_LOW_EO:       %08X\n",GpioLowEo);
	printk("GPIO_LOW_EI:       %08X\n",GpioLowEi);
	printk("PWC_IOSEL1:        %02X\n",PwcIosel1);
	printk("PWC_IOSEL2:        %02X\n",PwcIosel2);
	printk("SMU_PULLCTL_CAM:   %08X\n",SmuPullctlCam);
	printk("SMU_PULLCTRL_ASYNC:%08X\n",SmuPullctlAsync);
	printk("PWC_GPPUPD2:       %02X\n",PwcGppupd2);
	printk("PWC_GPPUPD3:       %02X\n",PwcGppupd3);
	printk("SMU_AHBCLKCTRL1:   %08X\n",SmuAhbclkctrl1);
#endif

	return(0);
}


/**
 *
 */
int icx1087_usb_epson_reset_platform(void)
{
	/* restore auto control */
	mp200_pmu_write_reg(SMU_AHBCLKCTRL1,SmuAhbclkctrl1,EXTUSB_SMU_AHBCLKCTRL1_MASK);

	/* restore pull control */
	mp200_pmu_write_reg(SMU_PULLCTL_CAM,SmuPullctlCam,EXTUSB_SMU_PULLCTL_CAM_MASK);
	mp200_pmu_write_reg(SMU_PULLCTL_ASYNC,SmuPullctlAsync,EXTUSB_SMU_PULLCTL_ASYNC_MASK);
	pwc_write(PWC_GPPUPD2,PwcGppupd2,EXTUSB_PWC_GPPUPD2_MASK);
	pwc_write(PWC_GPPUPD3,PwcGppupd3,EXTUSB_PWC_GPPUPD3_MASK);

	/* restore gpio control */
	mp200_gio_set_mode_output_low32(GpioLowEo&EXTUSB_GPIO_LOW_EX_MASK);
	mp200_gio_set_mode_input_low32(GpioLowEi&EXTUSB_GPIO_LOW_EX_MASK);
	pwc_write(PWC_IOSEL1,PwcIosel1,EXTUSB_PWC_IOSEL1_MASK);
	pwc_write(PWC_IOSEL2,PwcIosel2,EXTUSB_PWC_IOSEL2_MASK);

	/* restore pin control */
	mp200_pmu_write_reg(SMU_PCTL_CAM,SmuPctlCam,EXTUSB_SMU_PCTL_CAM_MASK);
	mp200_pmu_write_reg(SMU_PCTL_ASYNC,SmuPctlAsync,EXTUSB_SMU_PCTL_ASYNC_MASK);

	return(0);
}



/**
 *
 */
int icx1087_usb_epson_enable_flash_clock(void)
{
	unsigned long ibk;
	unsigned int val;

	spin_lock_irqsave(&platform_lock,ibk);

	if(clk_ref_counter<0)
		clk_ref_counter=0;

	clk_ref_counter++;

	/* get current */
	mp200_pmu_read_reg(SMU_GCLKCTRL2,&val);

	if((val&EXTUSB_SMU_GCLKCTRL2)==0){

		/* set clock enable */
		mp200_pmu_write_reg(
			SMU_GCLKCTRL2_ENA,
			EXTUSB_SMU_GCLKCTRL2_ENA,
			EXTUSB_SMU_GCLKCTRL2_ENA_MASK
		);

		/* enable clock */
		mp200_pmu_write_reg(
			SMU_GCLKCTRL2,
			EXTUSB_SMU_GCLKCTRL2,
			EXTUSB_SMU_GCLKCTRL2_MASK
		);

		/* reset clock enable */
		mp200_pmu_write_reg(
			SMU_GCLKCTRL2_ENA,
			~EXTUSB_SMU_GCLKCTRL2_ENA,
			EXTUSB_SMU_GCLKCTRL2_ENA_MASK
		);
	}

	spin_unlock_irqrestore(&platform_lock,ibk);

	return(0);
}


/**
 *
 */
int icx1087_usb_epson_disable_flash_clock(void)
{
	unsigned long ibk;
	unsigned int val;

	spin_lock_irqsave(&platform_lock,ibk);

	clk_ref_counter--;

	if(clk_ref_counter<0)
		clk_ref_counter=0;

	if(clk_ref_counter==0 && !vbus_high){

		/* get current */
		mp200_pmu_read_reg(SMU_GCLKCTRL2,&val);

		if(val&EXTUSB_SMU_GCLKCTRL2){

			/* set clock enable */
			mp200_pmu_write_reg(
				SMU_GCLKCTRL2_ENA,
				EXTUSB_SMU_GCLKCTRL2_ENA,
				EXTUSB_SMU_GCLKCTRL2_ENA_MASK
			);

			/* disable clock */
			mp200_pmu_write_reg(
				SMU_GCLKCTRL2,
				~EXTUSB_SMU_GCLKCTRL2,
				EXTUSB_SMU_GCLKCTRL2_MASK
			);

			/* reset clock enable */
			mp200_pmu_write_reg(
				SMU_GCLKCTRL2_ENA,
				~EXTUSB_SMU_GCLKCTRL2_ENA,
				EXTUSB_SMU_GCLKCTRL2_ENA_MASK
			);
		}
	}

	spin_unlock_irqrestore(&platform_lock,ibk);

	return(0);
}

void icx1087_usb_epson_set_vbus_stat(int high)
{
	vbus_high=high;
}



#if 0

/**
 *
 */
int icx1087_usb_epson_reset_usb_chip(void)
{
	/* set reset pin to low */
	mp200_gio_write_outputs_low32(EXTUSB_GPIO_LOW_XRST,EXTUSB_GPIO_LOW_XRST_EN);

	udelay(1);

	/* set reset pin to high */
	mp200_gio_write_outputs_low32(EXTUSB_GPIO_LOW_XRST,EXTUSB_GPIO_LOW_XRST_DI);

	return(0);
}
#endif

EXPORT_SYMBOL(icx1087_usb_epson_check_deep_sleep);


