pcf8563.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * PCF8563 RTC
  3.  *
  4.  * From Phillips' datasheet:
  5.  *
  6.  * The PCF8563 is a CMOS real-time clock/calendar optimized for low power
  7.  * consumption. A programmable clock output, interupt output and voltage
  8.  * low detector are also provided. All address and data are transferred
  9.  * serially via two-line bidirectional I2C-bus. Maximum bus speed is
  10.  * 400 kbits/s. The built-in word address register is incremented
  11.  * automatically after each written or read bute.
  12.  *
  13.  * Copyright (c) 2002, Axis Communications AB
  14.  * All rights reserved.
  15.  *
  16.  * Author: Tobias Anderberg <tobiasa@axis.com>.
  17.  *
  18.  * $Id: pcf8563.c,v 1.1 2002/08/12 13:46:02 starvik Exp $
  19.  */
  20. #include <linux/config.h>
  21. #include <linux/version.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/sched.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/delay.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/system.h>
  32. #include <asm/io.h>
  33. #include <asm/svinto.h>
  34. #include <asm/rtc.h>
  35. #include "i2c.h"
  36. #define PCF8563_MAJOR 121 /* Local major number. */
  37. #define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */
  38. #define PCF8563_NAME "PCF8563"
  39. #define DRIVER_VERSION "$Rev$"
  40. /* Two simple wrapper macros, saves a few keystrokes. */
  41. #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
  42. #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
  43. static const unsigned char days_in_month[] =
  44. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  45. int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  46. int pcf8563_open(struct inode *, struct file *);
  47. int pcf8563_release(struct inode *, struct file *);
  48. static struct file_operations pcf8563_fops = {
  49. owner: THIS_MODULE,
  50. ioctl: pcf8563_ioctl,
  51. open: pcf8563_open,
  52. release: pcf8563_release,
  53. };
  54. void
  55. get_rtc_time(struct rtc_time *tm)
  56. {
  57. unsigned long flags;
  58. tm->tm_sec = rtc_read(RTC_SECONDS);
  59. tm->tm_min = rtc_read(RTC_MINUTES);
  60. tm->tm_hour = rtc_read(RTC_HOURS);
  61. tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
  62. tm->tm_mon = rtc_read(RTC_MONTH);
  63. tm->tm_year = rtc_read(RTC_YEAR);
  64. if (tm->tm_sec & 0x80)
  65. printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!n", PCF8563_NAME);
  66.         tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
  67. tm->tm_sec &= 0x7f;
  68. tm->tm_min &= 0x7f;
  69. tm->tm_hour &= 0x3f;
  70. tm->tm_mday &= 0x3f;
  71. tm->tm_mon &= 0x1f;
  72. BCD_TO_BIN(tm->tm_sec);
  73. BCD_TO_BIN(tm->tm_min);
  74. BCD_TO_BIN(tm->tm_hour);
  75. BCD_TO_BIN(tm->tm_mday);
  76. BCD_TO_BIN(tm->tm_mon);
  77. tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
  78. }
  79. int __init
  80. pcf8563_init(void)
  81. {
  82. unsigned char ret;
  83.         struct rtc_time tm;
  84.         
  85. /*
  86.  * First of all we need to reset the chip. This is done by
  87.  * clearing control1, control2 and clk freq, clear the 
  88.  * Voltage Low bit, and resetting all alarms.
  89.  */
  90. if (rtc_write(RTC_CONTROL1, 0x00) < 0)
  91. goto err;
  92. if (rtc_write(RTC_CONTROL2, 0x00) < 0)
  93. goto err;
  94. if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
  95. goto err;
  96. /* Clear the VL bit in the seconds register. */
  97. ret = rtc_read(RTC_SECONDS);
  98. if (rtc_write(RTC_SECONDS, (ret & 0x7f)) < 0)
  99. goto err;
  100. /* Reset the alarms. */
  101. if (rtc_write(RTC_MINUTE_ALARM, 0x00) < 0)
  102. goto err;
  103. if (rtc_write(RTC_HOUR_ALARM, 0x00) < 0)
  104. goto err;
  105. if (rtc_write(RTC_DAY_ALARM, 0x00) < 0)
  106. goto err;
  107. if (rtc_write(RTC_WEEKDAY_ALARM, 0x00) < 0)
  108. goto err;
  109. if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
  110. printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.n", 
  111.        PCF8563_NAME, PCF8563_MAJOR);
  112. return -1;
  113. }
  114. printk(KERN_INFO "%s Real-Time Driver, %sn", PCF8563_NAME, DRIVER_VERSION);
  115.         
  116. /* Check for low voltage, and warn about it.. */
  117. if (rtc_read(RTC_SECONDS) & 0x80)
  118. printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!n", PCF8563_NAME);
  119. return 0;
  120. err:
  121. printk(KERN_INFO "%s: Error initializing chip.n", PCF8563_NAME);
  122. return -1;
  123. }
  124. void __exit
  125. pcf8563_exit(void)
  126. {
  127. if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
  128. printk(KERN_INFO "%s: Unable to unregister device.n", PCF8563_NAME);
  129. }
  130. }
  131. /*
  132.  * ioctl calls for this driver. Why return -ENOTTY upon error? Because
  133.  * POSIX says so!
  134.  */
  135. int
  136. pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  137. {
  138. /* Some sanity checks. */
  139. if (_IOC_TYPE(cmd) != PCF8563_MAGIC)
  140. return -ENOTTY;
  141. if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
  142. return -ENOTTY;
  143. switch (cmd) {
  144. case RTC_RD_TIME:
  145. {
  146. struct rtc_time tm;
  147. get_rtc_time(&tm);
  148. if (copy_to_user((struct rtc_time *) arg, &tm, sizeof(struct rtc_time))) {
  149. return -EFAULT;
  150. }
  151. return 0;
  152. }
  153. break;
  154. case RTC_SET_TIME:
  155. {
  156. int leap;
  157. int century;
  158. unsigned long flags;
  159. struct rtc_time tm;
  160. if (!capable(CAP_SYS_TIME))
  161. return -EPERM;
  162. if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time)))
  163. return -EFAULT;
  164. /* Convert from struct tm to struct rtc_time. */
  165. tm.tm_year += 1900;
  166. tm.tm_mon += 1;
  167. leap = ((tm.tm_mon == 2) && ((tm.tm_year % 4) == 0)) ? 1 : 0;
  168. /* Perform some sanity checks. */
  169. if ((tm.tm_year < 1970) ||
  170.     (tm.tm_mon > 12) ||
  171.     (tm.tm_mday == 0) ||
  172.     (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
  173.     (tm.tm_hour >= 24) ||
  174.     (tm.tm_min >= 60) ||
  175.     (tm.tm_sec >= 60))
  176. return -EINVAL;
  177. century = (tm.tm_year >= 2000) ? 0x80 : 0;
  178. tm.tm_year = tm.tm_year % 100;
  179. BIN_TO_BCD(tm.tm_year);
  180. BIN_TO_BCD(tm.tm_mday);
  181. BIN_TO_BCD(tm.tm_hour);
  182. BIN_TO_BCD(tm.tm_min);
  183. BIN_TO_BCD(tm.tm_sec);
  184. tm.tm_mon |= century;
  185. rtc_write(RTC_YEAR, tm.tm_year);
  186. rtc_write(RTC_MONTH, tm.tm_mon);
  187. rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
  188. rtc_write(RTC_HOURS, tm.tm_hour);
  189. rtc_write(RTC_MINUTES, tm.tm_min);
  190. rtc_write(RTC_SECONDS, tm.tm_sec);
  191. return 0;
  192. }
  193. break;
  194. default:
  195. return -ENOTTY;
  196. }
  197. return 0;
  198. }
  199. int 
  200. pcf8563_open(struct inode *inode, struct file *filp)
  201. {
  202. MOD_INC_USE_COUNT;
  203. return 0;
  204. }
  205. int
  206. pcf8563_release(struct inode *inode, struct file *filp)
  207. {
  208. MOD_DEC_USE_COUNT;
  209. return 0;
  210. }
  211. EXPORT_NO_SYMBOLS;
  212. module_init(pcf8563_init);
  213. module_exit(pcf8563_exit);