⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds12885.c

📁 Ep9315 arm ds12885 ep9315 ds12885时间芯片驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * linux/drivers/char/ds12885.c -- RTC dirver for linux-2.6 * *  This file is subject to the terms and conditions of the GNU General Public *  License. See the file COPYING in the main directory of this archive for *  more details. */ #include <linux/module.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/tty.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/reboot.h>#include <linux/config.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/ds12885.h>#include <linux/poll.h>#include <linux/fs.h>#include <linux/devfs_fs_kernel.h>#include <linux/proc_fs.h>#include <linux/sysctl.h>#include <linux/wait.h>#include <asm/current.h>#include <asm/uaccess.h>#include <asm/system.h>#include <linux/cdev.h>#include <linux/ioport.h>#include <asm/mach/time.h>#include <linux/timex.h>#include <linux/timer.h>#include <linux/time.h>#define DEBUG#define devicename		"ds12885"#define DS12885_MAJOR	10#define DS12885_MINOR	135#define ds12885_irq				IRQ_GPIO#define GPIO_PA_0				0x00000001#define DS12885_IRQ_POART		GPIO_PA_0struct ds12885_dev {		struct semaphore sem;     /* mutual exclusion semaphore     */		struct cdev cdev;	  /* Char device structure		*/};struct ds12885_dev rtc12885_dev;//extern unsigned long wall_jiffies;//#ifdef ds12885_irq//static int rtc_has_irq = 1;//#endif//static struct fasync_struct *rtc_async_queue;//static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data);static ssize_t rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos);static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);static void get_rtc_alm_time (struct rtc_time *alm_tm);static void rtc_get_rtc_time(struct rtc_time *rtc_tm);//static struct timer_list rtc_irq_timer;//static unsigned int rtc_poll(struct file *file, poll_table *wait);//static void rtc_dropped_irq(unsigned long data);static void set_rtc_irq_bit(unsigned char bit);static void mask_rtc_irq_bit(unsigned char bit);/* *	Bits in rtc_status. (6 bits of room for future expansion) */#define RTC_IS_OPEN		0x01	/* means /dev/rtc is in use	*/#define RTC_TIMER_ON		0x02	/* missed irq timer active	*//* * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is * protected by the big kernel lock. However, ioctl can still disable the timer * in rtc_status and then with del_timer after the interrupt has read * rtc_status but before mod_timer is called, which would then reenable the * timer (but you would need to have an awful timing before you'd trip on it) */static unsigned long rtc_status = 0;	/* bitmapped status byte.	*/static unsigned long rtc_freq = 0;	/* Current periodic IRQ rate	*/static unsigned long rtc_irq_data = 0;	/* our output to the world	*/static unsigned long sync_interval = 600; static struct ctl_table_header *sysctl_header;/* *	If this driver ever becomes modularised, it will be really nice *	to make the epoch retain its value across module reload... */static unsigned long epoch = 1900;	/* year corresponding to 0x00	*/static const unsigned char days_in_mo[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/* * sysctl-tuning infrastructure. */static ctl_table rtc_table[] = {	{		.ctl_name	= 1,		.procname	= "sync_interval",		.data		= &sync_interval,		.maxlen		= sizeof(int),		.mode		= 0644,		.proc_handler	= &proc_dointvec,	},	{ .ctl_name = 0 }};static ctl_table rtc_root[] = {	{		.ctl_name	= 1,		.procname	= "rtc",		.maxlen		= 0,		.mode		= 0555,		.child		= rtc_table,	},	{ .ctl_name = 0 }};static ctl_table dev_root[] = {	{		.ctl_name	= CTL_DEV,		.procname	= "dev",		.maxlen		= 0,		.mode		= 0555,		.child		= rtc_root,	},	{ .ctl_name = 0 }};static void __exit cleanup_sysctl(void){    unregister_sysctl_table(sysctl_header);}/* * Returns true if a clock update is in progress */static inline unsigned char rtc_is_updating(void){	unsigned char uip;	spin_lock_irq(&rtc_lock);	uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);	spin_unlock_irq(&rtc_lock);	return uip;}int ds12885_is_interrupt(void){	if(inl(GPIO_RAWINTSTSTISA) & DS12885_IRQ_POART)		{		return 1;		}	return 0;}irqreturn_t ds12885_interrupt(int irq, void *dev_id, struct pt_regs *regs){	u8 int_flag;	unsigned int year, mon, day, hour, min, sec;	struct timespec new_xtime;	if(!ds12885_is_interrupt())		{		return IRQ_NONE;		}	else		{		outb(DS12885_IRQ_POART, GPIO_AEOI);//clear GPIO interrupt		}		spin_lock (&rtc_lock);	int_flag=CMOS_READ(RTC_INTR_FLAGS);	if (int_flag&0x80){		if (int_flag&RTC_PF){			//periodic int 		}		if (int_flag&RTC_AF){			//alarm int		}		if (int_flag&RTC_UF){			//update int			rtc_irq_data ++;			if (rtc_irq_data>sync_interval){#ifdef DEBUG				printk(KERN_NOTICE "RTC synchron to xtime!\n");#endif				if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP){#ifdef DEBUG					printk(KERN_ALERT "RTC in updating... can not synchron!\n");#endif					return IRQ_HANDLED;				}				sec = CMOS_READ(RTC_SECONDS);				min = CMOS_READ(RTC_MINUTES);				hour = CMOS_READ(RTC_HOURS);				day = CMOS_READ(RTC_DAY_OF_MONTH);				mon = CMOS_READ(RTC_MONTH);				year = CMOS_READ(RTC_YEAR);								if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)) {	  				BCD_TO_BIN(sec);	    				BCD_TO_BIN(min);	    				BCD_TO_BIN(hour);	    				BCD_TO_BIN(day);	    				BCD_TO_BIN(mon);	    				BCD_TO_BIN(year);	  			}				if ((year += 1900) < 1970)					year += 100;				new_xtime.tv_sec=mktime(year, mon, day, hour, min, sec);				new_xtime.tv_nsec=0;//				wall_jiffies=jiffies;			//reset ms  need protection				do_settimeofday(&new_xtime);				rtc_irq_data = 0;							}		}	}	spin_unlock (&rtc_lock);	return IRQ_HANDLED;}/* *	Now all the various file operations that we export. */static ssize_t rtc_read(struct file *file, char __user *buf,size_t count, loff_t *ppos){	return -EIO;}static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel){	struct rtc_time wtime; 	switch (cmd) {	case RTC_AIE_OFF:	/* Mask alarm int. enab. bit	*/	{		mask_rtc_irq_bit(RTC_AIE);		return 0;	}	case RTC_AIE_ON:	/* Allow alarm interrupts.	*/	{		set_rtc_irq_bit(RTC_AIE);		return 0;	}	case RTC_PIE_OFF:	/* Mask periodic int. enab. bit	*/	{		mask_rtc_irq_bit(RTC_PIE);		if (rtc_status & RTC_TIMER_ON) {			//spin_lock_irq (&rtc_lock);			rtc_status &= ~RTC_TIMER_ON;			//del_timer(&rtc_irq_timer);			//spin_unlock_irq (&rtc_lock);		}		return 0;	}	case RTC_PIE_ON:	/* Allow periodic ints		*/	{		if (!(rtc_status & RTC_TIMER_ON)) {			//spin_lock_irq (&rtc_lock);			//rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;			//add_timer(&rtc_irq_timer);			rtc_status |= RTC_TIMER_ON;			//spin_unlock_irq (&rtc_lock);		}		set_rtc_irq_bit(RTC_PIE);		return 0;	}	case RTC_UIE_OFF:	/* Mask ints from RTC updates.	*/	{		mask_rtc_irq_bit(RTC_UIE);		return 0;	}	case RTC_UIE_ON:	/* Allow ints for RTC updates.	*/	{		set_rtc_irq_bit(RTC_UIE);		return 0;	}	case RTC_ALM_READ:	/* Read the present alarm time */	{		/*		 * This returns a struct rtc_time. Reading >= 0xc0		 * means "don't care" or "match all". Only the tm_hour,		 * tm_min, and tm_sec values are filled in.		 */		memset(&wtime, 0, sizeof(struct rtc_time));		get_rtc_alm_time(&wtime);		break; 	}	case RTC_ALM_SET:	/* Store a time into the alarm */	{		/*		 * This expects a struct rtc_time. Writing 0xff means		 * "don't care" or "match all". Only the tm_hour,		 * tm_min and tm_sec are used.		 */		unsigned char hrs, min, sec;		struct rtc_time alm_tm;		if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,				   sizeof(struct rtc_time)))			return -EFAULT;		hrs = alm_tm.tm_hour;		min = alm_tm.tm_min;		sec = alm_tm.tm_sec;		spin_lock_irq(&rtc_lock);		if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||		    RTC_ALWAYS_BCD)		{			if (sec < 60) BIN_TO_BCD(sec);			else sec = 0xff;			if (min < 60) BIN_TO_BCD(min);			else min = 0xff;			if (hrs < 24) BIN_TO_BCD(hrs);			else hrs = 0xff;		}		CMOS_WRITE(hrs, RTC_HOURS_ALARM);		CMOS_WRITE(min, RTC_MINUTES_ALARM);		CMOS_WRITE(sec, RTC_SECONDS_ALARM);		spin_unlock_irq(&rtc_lock);		return 0;	}	case RTC_RD_TIME:	/* Read the time/date from RTC	*/	{		memset(&wtime, 0, sizeof(struct rtc_time));		rtc_get_rtc_time(&wtime);		break;	}	case RTC_SET_TIME:	/* Set the RTC */	{		struct rtc_time rtc_tm;		unsigned char mon, day, hrs, min, sec, leap_yr;		unsigned char save_control, save_freq_select;		unsigned int yrs;		if (!capable(CAP_SYS_TIME))			return -EACCES;		if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,				   sizeof(struct rtc_time)))			return -EFAULT;		yrs = rtc_tm.tm_year + 1900;		mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */		day = rtc_tm.tm_mday;		hrs = rtc_tm.tm_hour;		min = rtc_tm.tm_min;		sec = rtc_tm.tm_sec;		if (yrs < 1970)			return -EINVAL;		leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));		if ((mon > 12) || (day == 0))			return -EINVAL;		if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))			return -EINVAL;					if ((hrs >= 24) || (min >= 60) || (sec >= 60))			return -EINVAL;		if ((yrs -= epoch) > 255)    /* They are unsigned */			return -EINVAL;		spin_lock_irq(&rtc_lock);		/* These limits and adjustments are independent of		 * whether the chip is in binary mode or not.		 */		if (yrs > 169) {			spin_unlock_irq(&rtc_lock);			return -EINVAL;		}		if (yrs >= 100)			yrs -= 100;		if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)		    || RTC_ALWAYS_BCD) {			BIN_TO_BCD(sec);			BIN_TO_BCD(min);			BIN_TO_BCD(hrs);			BIN_TO_BCD(day);			BIN_TO_BCD(mon);			BIN_TO_BCD(yrs);		}		save_control = CMOS_READ(RTC_CONTROL);		CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);		save_freq_select = CMOS_READ(RTC_FREQ_SELECT);		CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);		CMOS_WRITE(yrs, RTC_YEAR);		CMOS_WRITE(mon, RTC_MONTH);		CMOS_WRITE(day, RTC_DAY_OF_MONTH);		CMOS_WRITE(hrs, RTC_HOURS);		CMOS_WRITE(min, RTC_MINUTES);		CMOS_WRITE(sec, RTC_SECONDS);		CMOS_WRITE(save_control, RTC_CONTROL);		CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);		spin_unlock_irq(&rtc_lock);		return 0;	}	case RTC_IRQP_READ:	/* Read the periodic IRQ rate.	*/	{		return put_user(rtc_freq, (unsigned long __user *)arg);	}	case RTC_IRQP_SET:	/* Set periodic IRQ rate.	*/	{		int tmp = 0;		unsigned char val;		/* 		 * The max we can do is 8192Hz.		 */		if ((arg < 2) || (arg > 8192))			return -EINVAL;		while (arg > (1<<tmp))			tmp++;		/*		 * Check that the input was really a power of 2.		 */		if (arg != (1<<tmp))			return -EINVAL;		spin_lock_irq(&rtc_lock);		rtc_freq = arg;		val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;		val |= (16 - tmp);		CMOS_WRITE(val, RTC_FREQ_SELECT);		spin_unlock_irq(&rtc_lock);		return 0;	}	case RTC_EPOCH_READ:	/* Read the epoch.	*/	{		return put_user (epoch, (unsigned long __user *)arg);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -