/*
* Description : open uart collision control
* Input : 
* Output : 
*/
static int snfc_uart_control_open(struct inode *inode, struct file *fp)
{
	int rc = 0;

	if(isopen_snfcuartcontrol == 1)
	{
		SNFC_DEBUG_MSG_LOW("[snfc_uart_control] snfc_uart_control_open - already open \n");
		return 0;
	}
    	SNFC_DEBUG_MSG_LOW("[snfc_uart_control] snfc_uart_control_open - start \n");
	isopen_snfcuartcontrol = 1;

	if(gpio_init ==0)
	{	
		rc = gpio_request(GPIO_SNFC_HSEL, "snfc_hsel");
		if (rc)
		{
			SNFC_DEBUG_MSG("[snfc_intu_poll] gpio_request snfc_intu fail\n");
		}
		rc = gpio_request(GPIO_SNFC_PON, "snfc_pon");
		if (rc)
		{
			SNFC_DEBUG_MSG("[snfc_intu_poll] gpio_request snfc_intu fail\n");
		}

		//snfc_gpio_open(GPIO_SNFC_HSEL,GPIO_DIRECTION_OUT,GPIO_LOW_VALUE);
		//snfc_gpio_open(GPIO_SNFC_PON,GPIO_DIRECTION_OUT,GPIO_LOW_VALUE);
		SNFC_DEBUG_MSG_LOW("[snfc_uart_control] GPIO_SNFC_PON = %d, GPIO_SNFC_HSEL = %d\n",
			snfc_gpio_read(GPIO_SNFC_PON),snfc_gpio_read(GPIO_SNFC_HSEL) );	
		gpio_init = 1;
	}
	SNFC_DEBUG_MSG_LOW("[snfc_uart_control] snfc_uart_control_open - end \n");

	return rc;
}
int snfc_uart_control_probe(struct device_node *np)
{
    int rc;

    snfc_gpios.gpio_hsel = of_get_named_gpio_flags(np, "sony,hsel-gpio", 0, NULL);
    snfc_gpios.gpio_pon = of_get_named_gpio_flags(np, "sony,pon-gpio", 0, NULL);
    snfc_gpios.gpio_hvdd = of_get_named_gpio_flags(np, "sony,hvdd-gpio", 0, NULL);
    snfc_gpios.gpio_uicc_con = of_get_named_gpio_flags(np, "sony,uicc_con", 0, NULL);
    //SNFC_DEBUG_MSG("[snfc_driver] of_get_named_gpio_flags gpio_hsel %d gpio_pon %d gpio_hvdd %d\n",
    //    snfc_gpios.gpio_hsel,snfc_gpios.gpio_pon,snfc_gpios.gpio_hvdd );     

    rc = gpio_request(snfc_gpios.gpio_hsel, "snfc_hsel");
    if (rc)
    {
        SNFC_DEBUG_MSG("[snfc_driver] gpio_request snfc_hsel fail\n");
    }
    rc = gpio_request(snfc_gpios.gpio_pon, "snfc_pon");
    if (rc)
    {
        SNFC_DEBUG_MSG("[snfc_driver] gpio_request snfc_pon fail\n");
    }

    SNFC_DEBUG_MSG_LOW("[snfc_driver] GPIO_SNFC_PON = %d, GPIO_SNFC_HSEL = %d\n",
        snfc_gpio_read(snfc_gpios.gpio_pon),snfc_gpio_read(snfc_gpios.gpio_hsel) ); 

    /* register the device file */
    rc = misc_register(&snfc_uart_control_device);
    if (rc)
    {
        SNFC_DEBUG_MSG("[snfc_driver] FAIL!! can not register snfc_uart_control \n");
        return rc;
    }
    return rc;
}
/*
 * Description:
 * Input:
 * Output:
 */
static int __snfc_avail_poll_get_rfs_status(void)
{
    int return_val;

    return_val = snfc_gpio_read(gpio_rfs);              //Rev.B

    return return_val;
}
/*
 * Description: Notify a change in the device status. A blocking function
 * Input: 
 * Output: Intu changed form low to high - 1, Intu changed from high to low - 0
 */
static ssize_t snfc_intu_read(struct file *pf, char *pbuf, size_t size, loff_t *pos)
{
	int rc = 0;
	int current_intu_status = GPIO_LOW_VALUE;
	//int new_intu_status = GPIO_LOW_VALUE;
	int return_val;

	#ifdef FEATURE_DEBUG_LOW 
	SNFC_DEBUG_MSG("[snfc_intu_poll] snfc_intu_read - start \n");
	#endif

	/* Parameters check*/
	if(pf == NULL || pbuf == NULL || size == !1 /*|| pos == NULL*/) //need to know meaning of pos, size is fixed to 1
	{
		SNFC_DEBUG_MSG("[snfc_intu_poll] ERROR pf = %p , buf = %p, size = %d, pos = %d\n",pf,pbuf,(int)size,(int)pos);
		return -1;    
	}

	/* Get intu status */
	current_intu_status = snfc_gpio_read(GPIO_SNFC_INTU);
	SNFC_DEBUG_MSG("[snfc_intu_poll] current intu value is %d",current_intu_status);

	intu_sig=0;
	enable_irq_wake(gpio_to_irq(GPIO_SNFC_INTU));
	enable_irq(gpio_to_irq(GPIO_SNFC_INTU));
	SNFC_DEBUG_MSG("enable_irq intu irq");
	
	rc = wait_event_interruptible(intuwq,intu_sig);	

	if(intu_sig==0)
	{
		disable_irq_nosync(gpio_to_irq(GPIO_SNFC_INTU));
		disable_irq_wake(gpio_to_irq(GPIO_SNFC_INTU));		
		return_val = 1;
	}
	else
		return_val = 0;

	intu_sig=0;
	
	SNFC_DEBUG_MSG("snfc_intu_poll] wait_event_interruptible(),rc =%d !!!\n",rc);

	//current_intu_status = snfc_gpio_read(GPIO_SNFC_INTU);

	rc = copy_to_user((void*)pbuf, (void*)&return_val, size);
	if(rc)
	{
		SNFC_DEBUG_MSG("[snfc_intu_poll] ERROR -  copy_to_user \n");
		return rc;
	}

	#ifdef FEATURE_DEBUG_LOW 
	SNFC_DEBUG_MSG("[snfc_intu_poll] snfc_intu_read - end \n");
	#endif

	return size;
}
/*
 * Description:
 * Input: 
 * Output: RFS low : 1 RFS high : 0
 */
static ssize_t snfc_rfs_read(struct file *pf, char *pbuf, size_t size, loff_t *pos)
{
	int rc = 0;
	int getvalue = GPIO_LOW_VALUE;
	int rfonoff;

	#ifdef FEATURE_DEBUG_LOW 
	SNFC_DEBUG_MSG("[snfc_rfs] snfc_rfs_read - start \n");
	#endif

	/* Check Parameters */
	if(pf == NULL || pbuf == NULL /*|| size == NULL*/ /*||pos == NULL*/)
	{
		SNFC_DEBUG_MSG("[snfc_rfs] parameters ERROR pf = %p, pbuf = %p, size = %d, pos = %p\n",pf,pbuf,(int)size,pos);
		return -1;    
	}

	/* Get GPIO value */
	getvalue = snfc_gpio_read(GPIO_SNFC_RFS);

	if((getvalue != GPIO_LOW_VALUE)&&(getvalue != GPIO_HIGH_VALUE))
	{
		SNFC_DEBUG_MSG("[snfc_rfs] ERROR - getvalue is out of range \n");
		return -2;    
	}

	/* Copy value to user memory */
	//getvalue = getvalue ? GPIO_LOW_VALUE: GPIO_HIGH_VALUE;
	SNFC_DEBUG_MSG("[snfc_rfs] RFS pin status : %d \n", getvalue);

	if(getvalue)
		rfonoff = 0;
	else
		rfonoff = 1;
	
	SNFC_DEBUG_MSG("[snfc_rfs] rf status : %d \n", rfonoff);

	rc = copy_to_user((void*)pbuf, (void*)&rfonoff, size);
	if(rc)
	{
		SNFC_DEBUG_MSG("[snfc_rfs] ERROR -  copy_to_user \n");
		return rc;
	}

	#ifdef FEATURE_DEBUG_LOW 
	SNFC_DEBUG_MSG("[snfc_rfs] snfc_rfs_read - end \n");
	#endif

	return size;
}
static irqreturn_t snfc_int_low_isr(int irq, void *dev_id)
{
	SNFC_DEBUG_MSG_LOW("[snfc_intu_poll] snfc_int_low_isr - start \n");
	disable_irq_nosync(gpio_to_irq(snfc_gpios.gpio_intu));
	disable_irq_wake(gpio_to_irq(snfc_gpios.gpio_intu));  

	/* Wake up waiting readers */
	if(snfc_gpio_read(snfc_gpios.gpio_intu) != 1)
	{
		wake_up(&intuwq);
		intu_sig = 1;
	}
	SNFC_DEBUG_MSG_LOW("[snfc_intu_poll] snfc_int_low_isr - end \n");

	return IRQ_HANDLED;
}
int snfc_hvdd_wait_rfs_low(void)
{
    int rc = 0;

    do{
        snfcpollavail_sig = 0;
        if( snfc_gpio_read(snfc_gpios.gpio_rfs) == GPIO_HIGH_VALUE ){
            snfc_gpio_write(snfc_gpios.gpio_uicc_con ,GPIO_HIGH_VALUE );
        } else {
            snfc_gpio_write(snfc_gpios.gpio_uicc_con ,GPIO_LOW_VALUE );
            break;
        }
        rc = wait_event_interruptible( snfcpollavailwq, snfcpollavail_sig);
    }while(rc != -ERESTARTSYS);

    return rc;
}
static ssize_t snfc_rfs_read(struct file *pf, char *pbuf, size_t size, loff_t *pos)
{
	int rc = 0;
	int getvalue = GPIO_LOW_VALUE;
	int rfonoff;

	SNFC_DEBUG_MSG_LOW("[snfc_rfs] snfc_rfs_read - start \n");

	/*                  */
	if(pf == NULL || pbuf == NULL /*               */ /*             */)
	{
		SNFC_DEBUG_MSG("[snfc_rfs] parameters ERROR pf = %p, pbuf = %p, size = %d, pos = %p\n",pf,pbuf,(int)size,pos);
		return -1;    
	}

	/*                */
	getvalue = snfc_gpio_read(snfc_gpios.gpio_rfs);	

	if((getvalue != GPIO_LOW_VALUE)&&(getvalue != GPIO_HIGH_VALUE))
	{
		SNFC_DEBUG_MSG("[snfc_rfs] ERROR - getvalue is out of range \n");
		return -2;    
	}

	/*                           */
	//                                                      
	SNFC_DEBUG_MSG_LOW("[snfc_rfs] RFS pin status : %d \n", getvalue);

	if(getvalue)
		rfonoff = 0;
	else
		rfonoff = 1;
	
	SNFC_DEBUG_MSG_LOW("[snfc_rfs] rf status : %d \n", rfonoff);

	rc = copy_to_user((void*)pbuf, (void*)&rfonoff, size);
	if(rc)
	{
		SNFC_DEBUG_MSG("[snfc_rfs] ERROR -  copy_to_user \n");
		return rc;
	}

	SNFC_DEBUG_MSG_LOW("[snfc_rfs] snfc_rfs_read - end \n");

	return size;
}
/*
 * Description: 
 * Input: 
 * Output: 
 */
static int __snfc_avail_poll_get_rfs_status(void)
{
	return snfc_gpio_read(GPIO_SNFC_RFS);
}
/*
* Description : 
* Input : 
* Output : 
*/
static long snfc_uart_control_ioctl(struct file *flip, unsigned int cmd, unsigned long arg)
{
	//ioctl_buf *k_buf;
	//int i,err;
	int size;
	_e_snfc_uart_status current_status;
	int autopoll_wait_cnt;
	int break_cnt;
	unsigned char write_buf = 0x00/*, read_buf = 0x00*/;
	int rc =0;
	
	size = _IOC_SIZE(cmd);
	SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] snfc_uart_control_ioctl - start,cmd =%d\n", cmd);
		
	current_status = __snfc_uart_control_get_uart_status();
	if( current_status == UART_STATUS_FOR_FELICA )
	{
		SNFC_DEBUG_MSG("[snfc_uart_control] snfc_uart_control_ioctl, UART is used to FeliCa\n");
		return -1;
	}

	__snfc_uart_control_set_uart_status(UART_STATUS_FOR_NFC);	

	switch(cmd)
	{
		case IOCTL_SNFC_START_SETTING :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_SETTING - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);			
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			mdelay(10); 
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_SETTING - end\n");
			break;
			
		case IOCTL_SNFC_START_AUTOPOLL :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_AUTOPOLL - start\n");
			autopoll_wait_cnt = 0;
			break_cnt = 0;
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);	
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);

			#ifndef CONFIG_CXD2235AGG_GJ_KDDI	
				//SNFC_DEBUG_MSG("[snfc_uart_control] !!!! RFS disable start !!!!\n");
				disable_irq(gpio_to_irq(gpio_rfs));
			#endif
			
			mdelay(10);
			autopoll_status = 1;		
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_AUTOPOLL - end\n");
			break;
			
		case IOCTL_SNFC_START_RW :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_RW - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);				
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			mdelay(10);
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_RW - end\n");
			break;
			
		case IOCTL_SNFC_START_TARGET :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_TARGET - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;	
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);				
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			if(GPIO_HIGH_VALUE == snfc_gpio_read(gpio_rfs))
				mdelay(10);
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_TARGET - end\n");
			break;
			
		case IOCTL_SNFC_START_INTU :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_INTU - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);				
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			//mdelay(10);
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_INTU - end\n");
			break;
			
		case IOCTL_SNFC_START_WAITSIMBOOT:
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_WAITSIMBOOT - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
				break;
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);	
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			mdelay(10);
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_WAITSIMBOOT - end\n");		
			break;
			
		case IOCTL_SNFC_HSEL_UP:
			forced_hsel_up_flag = 1;
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hsel_up\n");	
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_HIGH_VALUE);	
			break;
			
		case IOCTL_SNFC_HSEL_DOWN:	
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hsel_down\n");	
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_LOW_VALUE);
			forced_hsel_up_flag = 0;
			if(forced_pon_up_flag == 0 && forced_hsel_up_flag == 0)
				__snfc_uart_control_set_uart_status(UART_STATUS_READY);
			break;
			
		case IOCTL_SNFC_PON_UP:
			forced_pon_up_flag = 1;
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_pon_up\n");	
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_HIGH_VALUE);
			mdelay(10);
			break;
			
		case IOCTL_SNFC_PON_DOWN:	
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_pon_down\n");	
			forced_pon_up_flag = 0;
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_LOW_VALUE);	
			if(forced_pon_up_flag == 0 && forced_hsel_up_flag == 0)
				__snfc_uart_control_set_uart_status(UART_STATUS_READY);
			break;

		case IOCTL_SNFC_BOOT_CEN_HI:		//Kernel init only
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_boot_hi\n");
			SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] CEN = High (UNLOCK) \n");
			write_buf = 0x81; // set unlock  
			//mutex_lock(&nfc_cen_mutex);	
			rc = snfc_i2c_write(0x02, &write_buf, 1);
			//mutex_unlock(&nfc_cen_mutex);   
			break;
			
		case IOCTL_SNFC_BOOT_CEN_LO:	//Kernel init only
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_boot_low\n");
			SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] CEN = Low (LOCK) \n");
			write_buf = 0x80; // set lock
			//mutex_lock(&nfc_cen_mutex);
			rc = snfc_i2c_write(0x02, &write_buf, 1);
			//mutex_unlock(&nfc_cen_mutex);  			
			break;

		case IOCTL_SNFC_HVDD_DOWN_SET:
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hvdd_down\n");
			while(koto_abnormal == 10)
			{
				if(snfc_poweroff_flag == 1)
				{
					break;
				}
				usleep(100);
			}		
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hvdd_down HVDD %d\n",snfc_gpio_read(86));
			break;
			
		case IOCTL_SNFC_END :
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_END - start\n");
			if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
			{
				SNFC_DEBUG_MSG("[snfc_uart_control] pon & hsel forced up!! pon and/or sel will keep high\n");
				break;
			}
			if(current_status != UART_STATUS_FOR_NFC)
			{
				SNFC_DEBUG_MSG("[snfc_uart_control] IOCTL_SNFC_END, UART is not used to NFC\n");
				//return -2;
			}				
			snfc_gpio_write(GPIO_SNFC_HSEL, GPIO_LOW_VALUE);
			snfc_gpio_write(GPIO_SNFC_PON, GPIO_LOW_VALUE);	
			__snfc_uart_control_set_uart_status(UART_STATUS_READY);
			
			#ifndef CONFIG_CXD2235AGG_GJ_KDDI	
				if(autopoll_status == 1) 
				{
					//SNFC_DEBUG_MSG("[snfc_uart_control] !!!! RFS disable end !!!!\n");
					enable_irq(gpio_to_irq(gpio_rfs));
				}
			#endif
			
			autopoll_status = 0;
			SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_END - end (hsel low)(pon low)\n");
			break;			
			
	}
	SNFC_DEBUG_MSG_LOW("[snfc_uart_control] snfc_uart_control_ioctl - end\n");

	return 0;
}
/*
* Description :
* Input :
* Output :
*/
static long snfc_uart_control_ioctl(struct file *flip, unsigned int cmd, unsigned long arg)
{
    //ioctl_buf *k_buf;
    //int i,err;
    int size;
    _e_snfc_uart_status current_status;
       int break_cnt;
    int autopoll_wait_cnt;
    unsigned char write_buf = 0x00/*, read_buf = 0x00*/;
    int rc =0;
    int snfcbootmode;

    size = _IOC_SIZE(cmd);
    SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] snfc_uart_control_ioctl - start,cmd =%d\n", cmd);

    if(cmd == IOCTL_SNFC_READ_BOOTMODE){
            snfcbootmode = lge_get_boot_mode();
            SNFC_DEBUG_MSG("[snfc_uart_control] read boot mode %d \n",snfcbootmode);
             // 0 : NORMAL, 1 : CHARGER, 2 : CHARGERLOGO, 3 : FACTORY, 4 : FACTORY2, 5 : PIFBOOT, 6 : PIFBOOT2, 7 : MINIOS
            return snfcbootmode;
    }
    current_status = __snfc_uart_control_get_uart_status();
    if( current_status == UART_STATUS_FOR_FELICA )
    {
        SNFC_DEBUG_MSG("[snfc_uart_control] snfc_uart_control_ioctl, UART is used to FeliCa\n");
        return -1;
    }

    __snfc_uart_control_set_uart_status(UART_STATUS_FOR_NFC);

    switch(cmd)
    {
        case IOCTL_SNFC_START_SETTING :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_SETTING - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            mdelay(10);
                  SNFC_DEBUG_MSG_LOW("[snfc_uart_control] hsel %d, pon %d\n",
                    snfc_gpios.gpio_hsel,snfc_gpios.gpio_pon);
                  SNFC_DEBUG_MSG_LOW("[snfc_driver] GPIO_SNFC_PON = %d, GPIO_SNFC_HSEL = %d, GPIO_SNFC_HBDD = %d\n",
                    snfc_gpio_read(snfc_gpios.gpio_pon),snfc_gpio_read(snfc_gpios.gpio_hsel),snfc_gpio_read(snfc_gpios.gpio_hvdd)  );
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_SETTING - end\n");
            break;

        case IOCTL_SNFC_START_AUTOPOLL :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_AUTOPOLL - start\n");
            autopoll_wait_cnt = 0;
            break_cnt = 0;
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            mdelay(10);
            autopoll_status = 1;
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_AUTOPOLL - end\n");
            break;

        case IOCTL_SNFC_START_RW :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_RW - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            mdelay(10);
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_RW - end\n");
            break;

        case IOCTL_SNFC_START_TARGET :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_TARGET - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            if(GPIO_HIGH_VALUE == snfc_gpio_read(snfc_gpios.gpio_rfs))
                mdelay(10);
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_TARGET - end\n");
            break;

        case IOCTL_SNFC_START_INTU :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_INTU - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            //mdelay(10);
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_INTU - end\n");
            break;

        case IOCTL_SNFC_START_WAITSIMBOOT:
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_WAITSIMBOOT - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
                break;
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            mdelay(10);
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_START_WAITSIMBOOT - end\n");
            break;

        case IOCTL_SNFC_HSEL_UP:
            forced_hsel_up_flag = 1;
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hsel_up\n");
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_HIGH_VALUE);
            break;

        case IOCTL_SNFC_HSEL_DOWN:
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_hsel_down\n");
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_LOW_VALUE);
            forced_hsel_up_flag = 0;
            if(forced_pon_up_flag == 0 && forced_hsel_up_flag == 0)
                __snfc_uart_control_set_uart_status(UART_STATUS_READY);
            break;

        case IOCTL_SNFC_PON_UP:
            forced_pon_up_flag = 1;
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_pon_up\n");
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_HIGH_VALUE);
            mdelay(10);
            break;

        case IOCTL_SNFC_PON_DOWN:
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_pon_down\n");
            forced_pon_up_flag = 0;
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_LOW_VALUE);
            if(forced_pon_up_flag == 0 && forced_hsel_up_flag == 0)
                __snfc_uart_control_set_uart_status(UART_STATUS_READY);
            break;

        case IOCTL_SNFC_BOOT_CEN_HI:        //Kernel init only
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_boot_hi\n");
            SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] CEN = High (UNLOCK) \n");
            write_buf = 0x81; // set unlock
            //mutex_lock(&nfc_cen_mutex);
            rc = snfc_i2c_write(0x02, &write_buf, 1, snfc_i2c_dev.client);
            //mutex_unlock(&nfc_cen_mutex);
            break;

        case IOCTL_SNFC_BOOT_CEN_LO:    //Kernel init only
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] ioctl_snfc_boot_low\n");
            SNFC_DEBUG_MSG_MIDDLE("[snfc_uart_control] CEN = Low (LOCK) \n");
            write_buf = 0x80; // set lock
            //mutex_lock(&nfc_cen_mutex);
            rc = snfc_i2c_write(0x02, &write_buf, 1, snfc_i2c_dev.client);
            //mutex_unlock(&nfc_cen_mutex);
            break;

           case IOCTL_SNFC_HVDD_DOWN_SET:
                 SNFC_DEBUG_MSG("snfc_uart_control] before HVDD Down\n");
                 snfc_hvdd_wait_rfs_low();
                 break;

        case IOCTL_SNFC_END :
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_END - start\n");
            if(forced_pon_up_flag == 1 || forced_hsel_up_flag == 1)
            {
                SNFC_DEBUG_MSG("[snfc_uart_control] pon & hsel forced up!! pon and/or sel will keep high\n");
                break;
            }
            if(current_status != UART_STATUS_FOR_NFC)
            {
                SNFC_DEBUG_MSG("[snfc_uart_control] IOCTL_SNFC_END, UART is not used to NFC\n");
                //return -2;
            }
            snfc_gpio_write(snfc_gpios.gpio_hsel, GPIO_LOW_VALUE);
            snfc_gpio_write(snfc_gpios.gpio_pon, GPIO_LOW_VALUE);
            __snfc_uart_control_set_uart_status(UART_STATUS_READY);

            autopoll_status = 0;
            SNFC_DEBUG_MSG_LOW("[snfc_uart_control] IOCTL_SNFC_END - end (hsel low)(pon low)\n");
            break;

    }
    SNFC_DEBUG_MSG_LOW("[snfc_uart_control] snfc_uart_control_ioctl - end\n");

    return 0;
}