#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 "driver.h"




static int major = MAJOR_GPIO_I2C;
static int minor = 0;
static dev_t devno;
static struct cdev *gpio_i2c_cdev = NULL;
static int count = 1;
#define DEVNAME "gpio_i2c"

static int gpio_i2c_open(struct inode *inode, struct file *filep);
static int gpio_i2c_close(struct inode *inode, struct file *filep);
static ssize_t gpio_i2c_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
static ssize_t gpio__i2c_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
static int gpio_i2c_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
static int gpio_i2c_transfer(gpio_i2c_t *pArg);
static int gpio_i2c_receive(gpio_i2c_t *pArg);
static struct file_operations gpio_i2c_ops = 
{
	.owner = THIS_MODULE,
	.open = gpio_i2c_open,
	.release = gpio_i2c_close,
	.ioctl = gpio_i2c_ioctl,
	.read = gpio_i2c_read,
	.write = gpio_i2c_write,
	
};

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

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

static int gpio_i2c_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
{
	gpio_i2c_t gpio_i2c_arg;
	if ( copy_from_user(&gpio_i2c_arg, (void*)arg, sizeof(gpio_i2c_t)) )
		return -EFAULT;
	
	switch(cmd)
	{
		case GPIO_I2C_MASTER_TRANSFER:
			HAL_GPIO_Init(gpio_arg.GPIOx, &gpio_arg.GPIO_Init);
			break;
		case GPIO_I2C_MASTER_RECEIVE:
			HAL_GPIO_WritePin(gpio_arg.GPIOx, gpio_arg.GPIO_pin, gpio_arg.Data);
		
			gpio_arg.Data = HAL_GPIO_ReadPin(gpio_arg.GPIOx, gpio_arg.GPIO_pin);
			if ( copy_to_user((void*)arg, (void*)&gpio_arg, sizeof(gpio_t)))
				return -EFAULT;
			break;
	default:
		printk("---> Invalid action\n");
		return -EINVAL;
		break;		

	}
	return 0;
}

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

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

static int gpio_i2c_transfer(gpio_i2c_t *pArg)
{
	//TODO:
	return 0;
}

static int gpio_i2c_receive(gpio_i2c_t *pArg)
{
	//TODO:
	return 0;
}


static void gpio_i2c_hw_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	//config gpio
	GPIO_InitStruct.Mode	= GPIO_MODE_OUTPUT_OD;
	GPIO_InitStruct.Pull    = GPIO_PULLUP;
	GPIO_InitStruct.Speed   = GPIO_SPEED_FAST;
	//GPIO_I2C
	GPIO_InitStruct.Pin		= GPIO_PIN_6 | GPIO_PIN_7;	//SCL | SDA
	//HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

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

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

	gpio_i2c_hw_init();
	return 0;

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

static void __exit gpio_i2c_exit(void)
{
	unregister_chrdev_region(MKDEV(major,minor),count);
	cdev_del(gpio_i2c_cdev);
}

module_init(gpio_i2c_init);
module_exit(gpio_i2c_exit);

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



