* 'for-linus' of git://www.jni.nu/cris: Correct auto-restart of syscalls via restartblock CRISv10: Fix return before mutex_unlock in pcf8563 Drop the CRISv32 version of pcf8563
···11-/*22- * PCF8563 RTC33- *44- * From Phillips' datasheet:55- *66- * The PCF8563 is a CMOS real-time clock/calendar optimized for low power77- * consumption. A programmable clock output, interrupt output and voltage88- * low detector are also provided. All address and data are transferred99- * serially via two-line bidirectional I2C-bus. Maximum bus speed is1010- * 400 kbits/s. The built-in word address register is incremented1111- * automatically after each written or read byte.1212- *1313- * Copyright (c) 2002-2007, Axis Communications AB1414- * All rights reserved.1515- *1616- * Author: Tobias Anderberg <tobiasa@axis.com>.1717- *1818- */1919-2020-#include <linux/module.h>2121-#include <linux/kernel.h>2222-#include <linux/types.h>2323-#include <linux/sched.h>2424-#include <linux/init.h>2525-#include <linux/fs.h>2626-#include <linux/ioctl.h>2727-#include <linux/delay.h>2828-#include <linux/bcd.h>2929-#include <linux/mutex.h>3030-3131-#include <asm/uaccess.h>3232-#include <asm/system.h>3333-#include <asm/io.h>3434-#include <asm/rtc.h>3535-3636-#include "i2c.h"3737-3838-#define PCF8563_MAJOR 121 /* Local major number. */3939-#define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */4040-#define PCF8563_NAME "PCF8563"4141-#define DRIVER_VERSION "$Revision: 1.17 $"4242-4343-/* Two simple wrapper macros, saves a few keystrokes. */4444-#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)4545-#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)4646-4747-static DEFINE_MUTEX(pcf8563_mutex);4848-static DEFINE_MUTEX(rtc_lock); /* Protect state etc */4949-5050-static const unsigned char days_in_month[] =5151- { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };5252-5353-static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);5454-5555-/* Cache VL bit value read at driver init since writing the RTC_SECOND5656- * register clears the VL status.5757- */5858-static int voltage_low;5959-6060-static const struct file_operations pcf8563_fops = {6161- .owner = THIS_MODULE,6262- .unlocked_ioctl = pcf8563_unlocked_ioctl,6363- .llseek = noop_llseek,6464-};6565-6666-unsigned char6767-pcf8563_readreg(int reg)6868-{6969- unsigned char res = rtc_read(reg);7070-7171- /* The PCF8563 does not return 0 for unimplemented bits. */7272- switch (reg) {7373- case RTC_SECONDS:7474- case RTC_MINUTES:7575- res &= 0x7F;7676- break;7777- case RTC_HOURS:7878- case RTC_DAY_OF_MONTH:7979- res &= 0x3F;8080- break;8181- case RTC_WEEKDAY:8282- res &= 0x07;8383- break;8484- case RTC_MONTH:8585- res &= 0x1F;8686- break;8787- case RTC_CONTROL1:8888- res &= 0xA8;8989- break;9090- case RTC_CONTROL2:9191- res &= 0x1F;9292- break;9393- case RTC_CLOCKOUT_FREQ:9494- case RTC_TIMER_CONTROL:9595- res &= 0x83;9696- break;9797- }9898- return res;9999-}100100-101101-void102102-pcf8563_writereg(int reg, unsigned char val)103103-{104104- rtc_write(reg, val);105105-}106106-107107-void108108-get_rtc_time(struct rtc_time *tm)109109-{110110- tm->tm_sec = rtc_read(RTC_SECONDS);111111- tm->tm_min = rtc_read(RTC_MINUTES);112112- tm->tm_hour = rtc_read(RTC_HOURS);113113- tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);114114- tm->tm_wday = rtc_read(RTC_WEEKDAY);115115- tm->tm_mon = rtc_read(RTC_MONTH);116116- tm->tm_year = rtc_read(RTC_YEAR);117117-118118- if (tm->tm_sec & 0x80) {119119- printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "120120- "information is no longer guaranteed!\n", PCF8563_NAME);121121- }122122-123123- tm->tm_year = bcd2bin(tm->tm_year) +124124- ((tm->tm_mon & 0x80) ? 100 : 0);125125- tm->tm_sec &= 0x7F;126126- tm->tm_min &= 0x7F;127127- tm->tm_hour &= 0x3F;128128- tm->tm_mday &= 0x3F;129129- tm->tm_wday &= 0x07; /* Not coded in BCD. */130130- tm->tm_mon &= 0x1F;131131-132132- tm->tm_sec = bcd2bin(tm->tm_sec);133133- tm->tm_min = bcd2bin(tm->tm_min);134134- tm->tm_hour = bcd2bin(tm->tm_hour);135135- tm->tm_mday = bcd2bin(tm->tm_mday);136136- tm->tm_mon = bcd2bin(tm->tm_mon);137137- tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */138138-}139139-140140-int __init141141-pcf8563_init(void)142142-{143143- static int res;144144- static int first = 1;145145-146146- if (!first)147147- return res;148148- first = 0;149149-150150- /* Initiate the i2c protocol. */151151- res = i2c_init();152152- if (res < 0) {153153- printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");154154- return res;155155- }156156-157157- /*158158- * First of all we need to reset the chip. This is done by159159- * clearing control1, control2 and clk freq and resetting160160- * all alarms.161161- */162162- if (rtc_write(RTC_CONTROL1, 0x00) < 0)163163- goto err;164164-165165- if (rtc_write(RTC_CONTROL2, 0x00) < 0)166166- goto err;167167-168168- if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)169169- goto err;170170-171171- if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)172172- goto err;173173-174174- /* Reset the alarms. */175175- if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)176176- goto err;177177-178178- if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)179179- goto err;180180-181181- if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)182182- goto err;183183-184184- if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)185185- goto err;186186-187187- /* Check for low voltage, and warn about it. */188188- if (rtc_read(RTC_SECONDS) & 0x80) {189189- voltage_low = 1;190190- printk(KERN_WARNING "%s: RTC Voltage Low - reliable "191191- "date/time information is no longer guaranteed!\n",192192- PCF8563_NAME);193193- }194194-195195- return res;196196-197197-err:198198- printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);199199- res = -1;200200- return res;201201-}202202-203203-void __exit204204-pcf8563_exit(void)205205-{206206- unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);207207-}208208-209209-/*210210- * ioctl calls for this driver. Why return -ENOTTY upon error? Because211211- * POSIX says so!212212- */213213-static int pcf8563_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)214214-{215215- /* Some sanity checks. */216216- if (_IOC_TYPE(cmd) != RTC_MAGIC)217217- return -ENOTTY;218218-219219- if (_IOC_NR(cmd) > RTC_MAX_IOCTL)220220- return -ENOTTY;221221-222222- switch (cmd) {223223- case RTC_RD_TIME:224224- {225225- struct rtc_time tm;226226-227227- mutex_lock(&rtc_lock);228228- memset(&tm, 0, sizeof tm);229229- get_rtc_time(&tm);230230-231231- if (copy_to_user((struct rtc_time *) arg, &tm,232232- sizeof tm)) {233233- mutex_unlock(&rtc_lock);234234- return -EFAULT;235235- }236236-237237- mutex_unlock(&rtc_lock);238238-239239- return 0;240240- }241241- case RTC_SET_TIME:242242- {243243- int leap;244244- int year;245245- int century;246246- struct rtc_time tm;247247-248248- memset(&tm, 0, sizeof tm);249249- if (!capable(CAP_SYS_TIME))250250- return -EPERM;251251-252252- if (copy_from_user(&tm, (struct rtc_time *) arg,253253- sizeof tm))254254- return -EFAULT;255255-256256- /* Convert from struct tm to struct rtc_time. */257257- tm.tm_year += 1900;258258- tm.tm_mon += 1;259259-260260- /*261261- * Check if tm.tm_year is a leap year. A year is a leap262262- * year if it is divisible by 4 but not 100, except263263- * that years divisible by 400 _are_ leap years.264264- */265265- year = tm.tm_year;266266- leap = (tm.tm_mon == 2) &&267267- ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);268268-269269- /* Perform some sanity checks. */270270- if ((tm.tm_year < 1970) ||271271- (tm.tm_mon > 12) ||272272- (tm.tm_mday == 0) ||273273- (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||274274- (tm.tm_wday >= 7) ||275275- (tm.tm_hour >= 24) ||276276- (tm.tm_min >= 60) ||277277- (tm.tm_sec >= 60))278278- return -EINVAL;279279-280280- century = (tm.tm_year >= 2000) ? 0x80 : 0;281281- tm.tm_year = tm.tm_year % 100;282282-283283- tm.tm_year = bin2bcd(tm.tm_year);284284- tm.tm_mon = bin2bcd(tm.tm_mon);285285- tm.tm_mday = bin2bcd(tm.tm_mday);286286- tm.tm_hour = bin2bcd(tm.tm_hour);287287- tm.tm_min = bin2bcd(tm.tm_min);288288- tm.tm_sec = bin2bcd(tm.tm_sec);289289- tm.tm_mon |= century;290290-291291- mutex_lock(&rtc_lock);292292-293293- rtc_write(RTC_YEAR, tm.tm_year);294294- rtc_write(RTC_MONTH, tm.tm_mon);295295- rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */296296- rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);297297- rtc_write(RTC_HOURS, tm.tm_hour);298298- rtc_write(RTC_MINUTES, tm.tm_min);299299- rtc_write(RTC_SECONDS, tm.tm_sec);300300-301301- mutex_unlock(&rtc_lock);302302-303303- return 0;304304- }305305- case RTC_VL_READ:306306- if (voltage_low)307307- printk(KERN_ERR "%s: RTC Voltage Low - "308308- "reliable date/time information is no "309309- "longer guaranteed!\n", PCF8563_NAME);310310-311311- if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))312312- return -EFAULT;313313- return 0;314314-315315- case RTC_VL_CLR:316316- {317317- /* Clear the VL bit in the seconds register in case318318- * the time has not been set already (which would319319- * have cleared it). This does not really matter320320- * because of the cached voltage_low value but do it321321- * anyway for consistency. */322322-323323- int ret = rtc_read(RTC_SECONDS);324324-325325- rtc_write(RTC_SECONDS, (ret & 0x7F));326326-327327- /* Clear the cached value. */328328- voltage_low = 0;329329-330330- return 0;331331- }332332- default:333333- return -ENOTTY;334334- }335335-336336- return 0;337337-}338338-339339-static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)340340-{341341- int ret;342342-343343- mutex_lock(&pcf8563_mutex);344344- return pcf8563_ioctl(filp, cmd, arg);345345- mutex_unlock(&pcf8563_mutex);346346-347347- return ret;348348-}349349-350350-static int __init pcf8563_register(void)351351-{352352- if (pcf8563_init() < 0) {353353- printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "354354- "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);355355- return -1;356356- }357357-358358- if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {359359- printk(KERN_INFO "%s: Unable to get major numer %d for RTC "360360- "device.\n", PCF8563_NAME, PCF8563_MAJOR);361361- return -1;362362- }363363-364364- printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,365365- DRIVER_VERSION);366366-367367- /* Check for low voltage, and warn about it. */368368- if (voltage_low) {369369- printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "370370- "information is no longer guaranteed!\n", PCF8563_NAME);371371- }372372-373373- return 0;374374-}375375-376376-module_init(pcf8563_register);377377-module_exit(pcf8563_exit);