#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <asm/current.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/err.h>
#include <asm/uaccess.h>
//#include <stdint.h>
#include "stm32f4xx.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32_hal_legacy.h"
#include "driver.h"
#include <linux/delay.h>




static int major = MAJOR_CRYPTO;
static int minor = 0;
static dev_t devno;
static struct cdev *crypto_cdev = NULL;
static int count = 1;
#define DEVNAME "crypto"

static int crypto_open(struct inode *inode, struct file *filep);
static int crypto_close(struct inode *inode, struct file *filep);
static ssize_t crypto_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
static ssize_t crypto_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
static int crypto_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
static struct file_operations crypto_ops = 
{
	.owner = THIS_MODULE,
	.open = crypto_open,
	.release = crypto_close,
	.ioctl = crypto_ioctl,
	.read = crypto_read,
	.write = crypto_write,
	
};

#define SCL_PORT	GPIOE
#define SCL_PIN 	GPIO_PIN_6
#define SDA_PORT 	GPIOE
#define SDA_PIN 	GPIO_PIN_5

#define SCLK_Set() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, 1);
#define SCLK_Clr() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, 0);
#define SDIN_Set() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, 1);
#define SDIN_Clr() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, 0);

void IIC_Start(void)
{
	SCLK_Set();
	SDIN_Set();
	udelay(1);
	SDIN_Clr();
	udelay(2);
	SDIN_Clr();
}

/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop(void)
{
	SCLK_Clr();
	SDIN_Clr();
	SCLK_Set();
	udelay(1);
	SDIN_Set();
}

void IIC_Ack(void)
{
	SCLK_Clr();
	SDIN_Clr();
	udelay(1); 
	SCLK_Set();
	udelay(1);
	SCLK_Clr();	
	SDIN_Set();	//release SDA
}

void IIC_NoAck(void)
{
	SCLK_Clr();
	SDIN_Set();
	udelay(1); 
	SCLK_Set();
	udelay(1);
	SCLK_Clr();
	SDIN_Set();	//release SDA
}

/*
	0: ACK ok
	1: NACK
*/
uint8_t IIC_Wait_Ack(void)
{
	uint32_t IDR[10];
	uint32_t idr_val = 0xffffffff;
	int i;
	uint8_t ack = 0;
	SCLK_Clr();
	SDIN_Set();	//release SDA
	udelay(2);
	SCLK_Set();
	udelay(1);
	if(HAL_GPIO_ReadPin(SDA_PORT, SDA_PIN) == GPIO_PIN_SET)
		ack = 1;
	else
		ack = 0;
	udelay(1);
	SCLK_Clr();
	return ack;
}

void Write_IIC_Byte(unsigned char IIC_Byte)
{
	unsigned char i;
	unsigned char m,da;
	da=IIC_Byte;
	SCLK_Clr();
	for(i=0;i<8;i++)		
	{
		m=da;
		m=m&0x80;
		if(m==0x80)
		{
			SDIN_Set();
		}
		else 
		{
			SDIN_Clr(); 
		}
		
		da=da<<1;
		udelay(1);
		SCLK_Set();
		udelay(1);
		SCLK_Clr();
	}
}

unsigned char Read_IIC_Byte(void)
{
	unsigned char i;
	unsigned int da = 0;

	SCLK_Clr();
	SDIN_Set();	//release SDA

	for(i=0;i<8;i++)	//8	
	{
		da=da<<1 ;
		udelay(1);
		SCLK_Set();
		if(HAL_GPIO_ReadPin(SDA_PORT, SDA_PIN) == 1)
		{da++;}
		udelay(1);
		SCLK_Clr();
	}

	return da;
}

static int crypto_open(struct inode *inode, struct file *filep)
{
	//__HAL_RCC_GPIOC_CLK_ENABLE();
	return 0;
}

static int crypto_close(struct inode *inode, struct file *filep)
{
	return 0;
}

static int crypto_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
{
	crypto_t crypto_arg;
	int i,j;
	if((void*)arg != NULL)
	{
		if ( copy_from_user(&crypto_arg, (void*)arg, sizeof(crypto_t)) )
			return -EFAULT;
	}
	
	switch(cmd)
	{
		case CRYPTO_WAIT_CLOCK:
			SDIN_Clr();
			for(i=0;i<crypto_arg.loop;i++)
			{
				IIC_Start();
				for(j=0;j<15;j++)
				{
					SCLK_Set();
					udelay(1);
					SCLK_Clr();
					udelay(1);
				}
				IIC_Stop();
			}
			break;
		case CRYPTO_SEND_COMMAND:
			i = 100;
			while(i>0)
			{
				IIC_Start();
				Write_IIC_Byte(crypto_arg.data[0]);
				if(IIC_Wait_Ack() != 0)
				{
					if(--i == 0) 
					{
						printk("CRYPTO_SEND_COMMAND error!\n");
						return -1;
					}			
					udelay(10);		
				}
				else
				{
					break;
				}
			}

			for(i=1;i<crypto_arg.size;i++)
			{
				udelay(1);
				Write_IIC_Byte(crypto_arg.data[i]);
				if(IIC_Wait_Ack() != 0)
				{
					printk("---> CRYPTO_SEND_COMMAND ack error! i = %d\n", i);
					return -1;
				}
			}
			break;
		case CRYPTO_RECEIVE_DATA:
			if(crypto_arg.size > 0)
			{
				for(i=0;i<crypto_arg.size-1;i++)
				{
					crypto_arg.data[i] = Read_IIC_Byte();
					IIC_Ack();
				}
				crypto_arg.data[i] = Read_IIC_Byte();
				IIC_NoAck();
			}
			IIC_Stop();

			if ( copy_to_user((void*)arg, (void*)&crypto_arg, sizeof(crypto_t)))
				return -EFAULT;
			break;
		case CRYPTO_SEND_DATA:
			for(i=0;i<crypto_arg.size;i++)
			{
				udelay(1);
				Write_IIC_Byte(crypto_arg.data[i]);
				if(IIC_Wait_Ack() != 0)
				{
					return -1;
				}
			}
			IIC_Stop();
			break;
		case CRYPTO_POWER_ON:
		    SCLK_Clr();
		    SDIN_Set();
		    for(i=0;i<crypto_arg.loop;i++)	//CM_PWRON_CLKS
		    {
		    	SCLK_Clr()
		    	udelay(1);
		    	SCLK_Set();
		    	udelay(1);
		    }

		    SDIN_Set();
		    SCLK_Set();	//Release bus
			break;
		case CRYPTO_POWER_OFF:
			// udelay(1);
			// //SCLK_Clr();
			// udelay(6);
			IIC_Stop();
			SDIN_Set();
		    SCLK_Set();	//Release bus
			break;
		case CRYPTO_SEND_CMD_BYTE:
			i = 100;
			while(i)
			{
				IIC_Start();
				Write_IIC_Byte(crypto_arg.data[0]);
				if(IIC_Wait_Ack() != 0)
				{
					if(--i == 0) 
					{
						printk("CRYPTO_SEND_CMD_BYTE error!\n");
						return -1;
					}			
					udelay(10);		
				}
				else
				{
					break;
				}
			}
			break;
	default:
		printk("---> Invalid action\n");
		return -EINVAL;
		break;		

	}
	return 0;
}

static ssize_t crypto_read(struct file *filep, char __user *buf, size_t size, loff_t *offset)
{
	return 0;
}

static ssize_t crypto_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset)
{
	return 0;
}

static void crypto_hw_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	//config gpio
	GPIO_InitStruct.Mode	= GPIO_MODE_OUTPUT_OD;
	GPIO_InitStruct.Pull    = GPIO_NOPULL;	//GPIO_PULLDOWN;	//GPIO_PULLUP;
	GPIO_InitStruct.Speed   = GPIO_SPEED_FAST;
	
	//SCL
	GPIO_InitStruct.Pin		= SCL_PIN;
	HAL_GPIO_Init(SCL_PORT, &GPIO_InitStruct);
	//SDA
	GPIO_InitStruct.Pin		= SDA_PIN;
	HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);

	HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET);
	HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_SET);

}

static int __init crypto_init(void)
{
	int ret;
	crypto_cdev = cdev_alloc();
	if(crypto_cdev == NULL){
		return -ENOMEM;
	}

	cdev_init(crypto_cdev,&crypto_ops);
	devno = MKDEV(major,minor);
	ret = register_chrdev_region(devno, count, DEVNAME);
	if(ret){
		goto ERR_STEP;
	}
	
	ret = cdev_add(crypto_cdev, devno, count);
	if(ret){
		goto ERR_STEP1;
	}

	crypto_hw_init();

	printk("Crypto module start...\n");
	return 0;

ERR_STEP1:
	unregister_chrdev_region(devno,count);
ERR_STEP:
	cdev_del(crypto_cdev);
	return ret;
}

static void __exit crypto_exit(void)
{
	unregister_chrdev_region(MKDEV(major,minor),count);
	cdev_del(crypto_cdev);
}

module_init(crypto_init);
module_exit(crypto_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jimbo");
MODULE_DESCRIPTION("this is crypto AT88SC0104C module");



