Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7edf7440 authored by Jesper Nilsson's avatar Jesper Nilsson
Browse files

CRIS v32: Update driver for RTC chip pcf8563.

- Moved all calls to register_chrdev to a function called by module_init.
- Added mutex locking.
- Added better error handling at start up.
- Added BIN_TO_BCD of the month value before it is saved to the RTC.
- Corrected the month value returned by pcf8563_readreg.
- Cache the voltage low value at driver init so the battery status
  information does not get 'accidentally' cleared when setting the RTC time.
- Removed obsolete CONFIG_ETRAX_RTC_READONLY
- Voltage low ioctl():s RTC_VLOW_RD -> RTC_VL_READ, RTC_VLOW_SET -> RTC_VL_CLR
parent d8ac17a0
Loading
Loading
Loading
Loading
+163 −133
Original line number Original line Diff line number Diff line
@@ -10,7 +10,7 @@
 * 400 kbits/s. The built-in word address register is incremented
 * 400 kbits/s. The built-in word address register is incremented
 * automatically after each written or read byte.
 * automatically after each written or read byte.
 *
 *
 * Copyright (c) 2002-2003, Axis Communications AB
 * Copyright (c) 2002-2007, Axis Communications AB
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Author: Tobias Anderberg <tobiasa@axis.com>.
 * Author: Tobias Anderberg <tobiasa@axis.com>.
@@ -26,6 +26,7 @@
#include <linux/ioctl.h>
#include <linux/ioctl.h>
#include <linux/delay.h>
#include <linux/delay.h>
#include <linux/bcd.h>
#include <linux/bcd.h>
#include <linux/mutex.h>


#include <asm/uaccess.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/system.h>
@@ -37,24 +38,27 @@
#define PCF8563_MAJOR	121	/* Local major number. */
#define PCF8563_MAJOR	121	/* Local major number. */
#define DEVICE_NAME	"rtc"	/* Name which is registered in /proc/devices. */
#define DEVICE_NAME	"rtc"	/* Name which is registered in /proc/devices. */
#define PCF8563_NAME	"PCF8563"
#define PCF8563_NAME	"PCF8563"
#define DRIVER_VERSION	"$Revision: 1.1 $"
#define DRIVER_VERSION	"$Revision: 1.17 $"


/* Two simple wrapper macros, saves a few keystrokes. */
/* Two simple wrapper macros, saves a few keystrokes. */
#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)


static DEFINE_MUTEX(rtc_lock); /* Protect state etc */

static const unsigned char days_in_month[] =
static const unsigned char days_in_month[] =
	{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
int pcf8563_open(struct inode *, struct file *);

int pcf8563_release(struct inode *, struct file *);
/* Cache VL bit value read at driver init since writing the RTC_SECOND
 * register clears the VL status.
 */
static int voltage_low;


static const struct file_operations pcf8563_fops = {
static const struct file_operations pcf8563_fops = {
	.owner =	THIS_MODULE,
	.owner =	THIS_MODULE,
	.ioctl =	pcf8563_ioctl,
	.ioctl =	pcf8563_ioctl
	.open =		pcf8563_open,
	.release =	pcf8563_release,
};
};


unsigned char
unsigned char
@@ -62,7 +66,7 @@ pcf8563_readreg(int reg)
{
{
	unsigned char res = rtc_read(reg);
	unsigned char res = rtc_read(reg);


	/* The PCF8563 does not return 0 for unimplemented bits */
	/* The PCF8563 does not return 0 for unimplemented bits. */
	switch (reg) {
	switch (reg) {
		case RTC_SECONDS:
		case RTC_SECONDS:
		case RTC_MINUTES:
		case RTC_MINUTES:
@@ -95,11 +99,6 @@ pcf8563_readreg(int reg)
void
void
pcf8563_writereg(int reg, unsigned char val)
pcf8563_writereg(int reg, unsigned char val)
{
{
#ifdef CONFIG_ETRAX_RTC_READONLY
	if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
		return;
#endif

	rtc_write(reg, val);
	rtc_write(reg, val);
}
}


@@ -114,11 +113,13 @@ get_rtc_time(struct rtc_time *tm)
	tm->tm_mon  = rtc_read(RTC_MONTH);
	tm->tm_mon  = rtc_read(RTC_MONTH);
	tm->tm_year = rtc_read(RTC_YEAR);
	tm->tm_year = rtc_read(RTC_YEAR);


	if (tm->tm_sec & 0x80)
	if (tm->tm_sec & 0x80) {
		printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
		printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
		       "information is no longer guaranteed!\n", PCF8563_NAME);
		       "information is no longer guaranteed!\n", PCF8563_NAME);
	}


	tm->tm_year  = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
	tm->tm_year  = BCD_TO_BIN(tm->tm_year) +
		       ((tm->tm_mon & 0x80) ? 100 : 0);
	tm->tm_sec  &= 0x7F;
	tm->tm_sec  &= 0x7F;
	tm->tm_min  &= 0x7F;
	tm->tm_min  &= 0x7F;
	tm->tm_hour &= 0x3F;
	tm->tm_hour &= 0x3F;
@@ -137,8 +138,19 @@ get_rtc_time(struct rtc_time *tm)
int __init
int __init
pcf8563_init(void)
pcf8563_init(void)
{
{
	static int res;
	static int first = 1;

	if (!first)
		return res;
	first = 0;

	/* Initiate the i2c protocol. */
	/* Initiate the i2c protocol. */
	i2c_init();
	res = i2c_init();
	if (res < 0) {
		printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
		return res;
	}


	/*
	/*
	 * First of all we need to reset the chip. This is done by
	 * First of all we need to reset the chip. This is done by
@@ -170,24 +182,20 @@ pcf8563_init(void)
	if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
	if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
		goto err;
		goto err;


	if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
	/* Check for low voltage, and warn about it. */
		printk(KERN_INFO "%s: Unable to get major number %d for RTC device.\n",
	if (rtc_read(RTC_SECONDS) & 0x80) {
		       PCF8563_NAME, PCF8563_MAJOR);
		voltage_low = 1;
		return -1;
		printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
		       "date/time information is no longer guaranteed!\n",
		       PCF8563_NAME);
	}
	}


	printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
	return res;

	/* Check for low voltage, and warn about it.. */
	if (rtc_read(RTC_SECONDS) & 0x80)
		printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
		       "information is no longer guaranteed!\n", PCF8563_NAME);

	return 0;


err:
err:
	printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
	printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
	return -1;
	res = -1;
	return res;
}
}


void __exit
void __exit
@@ -200,8 +208,8 @@ pcf8563_exit(void)
 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
 * POSIX says so!
 * POSIX says so!
 */
 */
int
int pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
	unsigned long arg)
{
{
	/* Some sanity checks. */
	/* Some sanity checks. */
	if (_IOC_TYPE(cmd) != RTC_MAGIC)
	if (_IOC_TYPE(cmd) != RTC_MAGIC)
@@ -215,30 +223,33 @@ pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned
	{
	{
		struct rtc_time tm;
		struct rtc_time tm;


			memset(&tm, 0, sizeof (struct rtc_time));
		mutex_lock(&rtc_lock);
		memset(&tm, 0, sizeof tm);
		get_rtc_time(&tm);
		get_rtc_time(&tm);


			if (copy_to_user((struct rtc_time *) arg, &tm, sizeof tm)) {
		if (copy_to_user((struct rtc_time *) arg, &tm,
				 sizeof tm)) {
			spin_unlock(&rtc_lock);
			return -EFAULT;
			return -EFAULT;
		}
		}


		mutex_unlock(&rtc_lock);

		return 0;
		return 0;
	}
	}

	case RTC_SET_TIME:
	case RTC_SET_TIME:
	{
	{
#ifdef CONFIG_ETRAX_RTC_READONLY
			return -EPERM;
#else
		int leap;
		int leap;
		int year;
		int year;
		int century;
		int century;
		struct rtc_time tm;
		struct rtc_time tm;


		memset(&tm, 0, sizeof tm);
		if (!capable(CAP_SYS_TIME))
		if (!capable(CAP_SYS_TIME))
			return -EPERM;
			return -EPERM;


			if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
		if (copy_from_user(&tm, (struct rtc_time *) arg,
				   sizeof tm))
			return -EFAULT;
			return -EFAULT;


		/* Convert from struct tm to struct rtc_time. */
		/* Convert from struct tm to struct rtc_time. */
@@ -251,7 +262,8 @@ pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned
		 * that years divisible by 400 _are_ leap years.
		 * that years divisible by 400 _are_ leap years.
		 */
		 */
		year = tm.tm_year;
		year = tm.tm_year;
			leap = (tm.tm_mon == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
		leap = (tm.tm_mon == 2) &&
			((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);


		/* Perform some sanity checks. */
		/* Perform some sanity checks. */
		if ((tm.tm_year < 1970) ||
		if ((tm.tm_year < 1970) ||
@@ -268,12 +280,15 @@ pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned
		tm.tm_year = tm.tm_year % 100;
		tm.tm_year = tm.tm_year % 100;


		BIN_TO_BCD(tm.tm_year);
		BIN_TO_BCD(tm.tm_year);
		BIN_TO_BCD(tm.tm_mon);
		BIN_TO_BCD(tm.tm_mday);
		BIN_TO_BCD(tm.tm_mday);
		BIN_TO_BCD(tm.tm_hour);
		BIN_TO_BCD(tm.tm_hour);
		BIN_TO_BCD(tm.tm_min);
		BIN_TO_BCD(tm.tm_min);
		BIN_TO_BCD(tm.tm_sec);
		BIN_TO_BCD(tm.tm_sec);
		tm.tm_mon |= century;
		tm.tm_mon |= century;


		mutex_lock(&rtc_lock);

		rtc_write(RTC_YEAR, tm.tm_year);
		rtc_write(RTC_YEAR, tm.tm_year);
		rtc_write(RTC_MONTH, tm.tm_mon);
		rtc_write(RTC_MONTH, tm.tm_mon);
		rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
		rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
@@ -282,36 +297,37 @@ pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned
		rtc_write(RTC_MINUTES, tm.tm_min);
		rtc_write(RTC_MINUTES, tm.tm_min);
		rtc_write(RTC_SECONDS, tm.tm_sec);
		rtc_write(RTC_SECONDS, tm.tm_sec);


		mutex_unlock(&rtc_lock);

		return 0;
		return 0;
#endif /* !CONFIG_ETRAX_RTC_READONLY */
	}
	}
	case RTC_VL_READ:
		if (voltage_low)
			printk(KERN_ERR "%s: RTC Voltage Low - "
			       "reliable date/time information is no "
			       "longer guaranteed!\n", PCF8563_NAME);


		case RTC_VLOW_RD:
		if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
		{
			int vl_bit = 0;

			if (rtc_read(RTC_SECONDS) & 0x80) {
				vl_bit = 1;
				printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
				       "date/time information is no longer guaranteed!\n",
				       PCF8563_NAME);
			}
			if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
			return -EFAULT;
			return -EFAULT;

		return 0;
		return 0;
		}


		case RTC_VLOW_SET:
	case RTC_VL_CLR:
	{
	{
			/* Clear the VL bit in the seconds register */
		/* Clear the VL bit in the seconds register in case
		 * the time has not been set already (which would
		 * have cleared it). This does not really matter
		 * because of the cached voltage_low value but do it
		 * anyway for consistency. */

		int ret = rtc_read(RTC_SECONDS);
		int ret = rtc_read(RTC_SECONDS);


		rtc_write(RTC_SECONDS, (ret & 0x7F));
		rtc_write(RTC_SECONDS, (ret & 0x7F));


		/* Clear the cached value. */
		voltage_low = 0;

		return 0;
		return 0;
	}
	}

	default:
	default:
		return -ENOTTY;
		return -ENOTTY;
	}
	}
@@ -319,17 +335,31 @@ pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned
	return 0;
	return 0;
}
}


int
static int __init pcf8563_register(void)
pcf8563_open(struct inode *inode, struct file *filp)
{
{
	return 0;
	if (pcf8563_init() < 0) {
		printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
		       "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
		return -1;
	}

	if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
		printk(KERN_INFO "%s: Unable to get major numer %d for RTC "
		       "device.\n", PCF8563_NAME, PCF8563_MAJOR);
		return -1;
	}

	printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
	       DRIVER_VERSION);

	/* Check for low voltage, and warn about it. */
	if (voltage_low) {
		printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
		       "information is no longer guaranteed!\n", PCF8563_NAME);
	}
	}


int
pcf8563_release(struct inode *inode, struct file *filp)
{
	return 0;
	return 0;
}
}


module_init(pcf8563_init);
module_init(pcf8563_register);
module_exit(pcf8563_exit);
module_exit(pcf8563_exit);