pcf8563.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 byte.
  12. *
  13. * Copyright (c) 2002-2003, Axis Communications AB
  14. * All rights reserved.
  15. *
  16. * Author: Tobias Anderberg <tobiasa@axis.com>.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/sched.h>
  23. #include <linux/init.h>
  24. #include <linux/fs.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/delay.h>
  27. #include <linux/bcd.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/system.h>
  30. #include <asm/io.h>
  31. #include <asm/rtc.h>
  32. #include "i2c.h"
  33. #define PCF8563_MAJOR 121 /* Local major number. */
  34. #define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */
  35. #define PCF8563_NAME "PCF8563"
  36. #define DRIVER_VERSION "$Revision: 1.1 $"
  37. /* Two simple wrapper macros, saves a few keystrokes. */
  38. #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
  39. #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
  40. static const unsigned char days_in_month[] =
  41. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  42. int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  43. int pcf8563_open(struct inode *, struct file *);
  44. int pcf8563_release(struct inode *, struct file *);
  45. static struct file_operations pcf8563_fops = {
  46. owner: THIS_MODULE,
  47. ioctl: pcf8563_ioctl,
  48. open: pcf8563_open,
  49. release: pcf8563_release,
  50. };
  51. unsigned char
  52. pcf8563_readreg(int reg)
  53. {
  54. unsigned char res = rtc_read(reg);
  55. /* The PCF8563 does not return 0 for unimplemented bits */
  56. switch (reg) {
  57. case RTC_SECONDS:
  58. case RTC_MINUTES:
  59. res &= 0x7F;
  60. break;
  61. case RTC_HOURS:
  62. case RTC_DAY_OF_MONTH:
  63. res &= 0x3F;
  64. break;
  65. case RTC_WEEKDAY:
  66. res &= 0x07;
  67. break;
  68. case RTC_MONTH:
  69. res &= 0x1F;
  70. break;
  71. case RTC_CONTROL1:
  72. res &= 0xA8;
  73. break;
  74. case RTC_CONTROL2:
  75. res &= 0x1F;
  76. break;
  77. case RTC_CLOCKOUT_FREQ:
  78. case RTC_TIMER_CONTROL:
  79. res &= 0x83;
  80. break;
  81. }
  82. return res;
  83. }
  84. void
  85. pcf8563_writereg(int reg, unsigned char val)
  86. {
  87. #ifdef CONFIG_ETRAX_RTC_READONLY
  88. if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
  89. return;
  90. #endif
  91. rtc_write(reg, val);
  92. }
  93. void
  94. get_rtc_time(struct rtc_time *tm)
  95. {
  96. tm->tm_sec = rtc_read(RTC_SECONDS);
  97. tm->tm_min = rtc_read(RTC_MINUTES);
  98. tm->tm_hour = rtc_read(RTC_HOURS);
  99. tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
  100. tm->tm_wday = rtc_read(RTC_WEEKDAY);
  101. tm->tm_mon = rtc_read(RTC_MONTH);
  102. tm->tm_year = rtc_read(RTC_YEAR);
  103. if (tm->tm_sec & 0x80)
  104. printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
  105. "information is no longer guaranteed!\n", PCF8563_NAME);
  106. tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
  107. tm->tm_sec &= 0x7F;
  108. tm->tm_min &= 0x7F;
  109. tm->tm_hour &= 0x3F;
  110. tm->tm_mday &= 0x3F;
  111. tm->tm_wday &= 0x07; /* Not coded in BCD. */
  112. tm->tm_mon &= 0x1F;
  113. BCD_TO_BIN(tm->tm_sec);
  114. BCD_TO_BIN(tm->tm_min);
  115. BCD_TO_BIN(tm->tm_hour);
  116. BCD_TO_BIN(tm->tm_mday);
  117. BCD_TO_BIN(tm->tm_mon);
  118. tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
  119. }
  120. int __init
  121. pcf8563_init(void)
  122. {
  123. /* Initiate the i2c protocol. */
  124. i2c_init();
  125. /*
  126. * First of all we need to reset the chip. This is done by
  127. * clearing control1, control2 and clk freq and resetting
  128. * all alarms.
  129. */
  130. if (rtc_write(RTC_CONTROL1, 0x00) < 0)
  131. goto err;
  132. if (rtc_write(RTC_CONTROL2, 0x00) < 0)
  133. goto err;
  134. if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
  135. goto err;
  136. if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
  137. goto err;
  138. /* Reset the alarms. */
  139. if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
  140. goto err;
  141. if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
  142. goto err;
  143. if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
  144. goto err;
  145. if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
  146. goto err;
  147. if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
  148. printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.\n",
  149. PCF8563_NAME, PCF8563_MAJOR);
  150. return -1;
  151. }
  152. printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
  153. /* Check for low voltage, and warn about it.. */
  154. if (rtc_read(RTC_SECONDS) & 0x80)
  155. printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
  156. "information is no longer guaranteed!\n", PCF8563_NAME);
  157. return 0;
  158. err:
  159. printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
  160. return -1;
  161. }
  162. void __exit
  163. pcf8563_exit(void)
  164. {
  165. if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
  166. printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
  167. }
  168. }
  169. /*
  170. * ioctl calls for this driver. Why return -ENOTTY upon error? Because
  171. * POSIX says so!
  172. */
  173. int
  174. pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  175. {
  176. /* Some sanity checks. */
  177. if (_IOC_TYPE(cmd) != RTC_MAGIC)
  178. return -ENOTTY;
  179. if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
  180. return -ENOTTY;
  181. switch (cmd) {
  182. case RTC_RD_TIME:
  183. {
  184. struct rtc_time tm;
  185. memset(&tm, 0, sizeof (struct rtc_time));
  186. get_rtc_time(&tm);
  187. if (copy_to_user((struct rtc_time *) arg, &tm, sizeof tm)) {
  188. return -EFAULT;
  189. }
  190. return 0;
  191. }
  192. case RTC_SET_TIME:
  193. {
  194. #ifdef CONFIG_ETRAX_RTC_READONLY
  195. return -EPERM;
  196. #else
  197. int leap;
  198. int year;
  199. int century;
  200. struct rtc_time tm;
  201. if (!capable(CAP_SYS_TIME))
  202. return -EPERM;
  203. if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
  204. return -EFAULT;
  205. /* Convert from struct tm to struct rtc_time. */
  206. tm.tm_year += 1900;
  207. tm.tm_mon += 1;
  208. /*
  209. * Check if tm.tm_year is a leap year. A year is a leap
  210. * year if it is divisible by 4 but not 100, except
  211. * that years divisible by 400 _are_ leap years.
  212. */
  213. year = tm.tm_year;
  214. leap = (tm.tm_mon == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  215. /* Perform some sanity checks. */
  216. if ((tm.tm_year < 1970) ||
  217. (tm.tm_mon > 12) ||
  218. (tm.tm_mday == 0) ||
  219. (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
  220. (tm.tm_wday >= 7) ||
  221. (tm.tm_hour >= 24) ||
  222. (tm.tm_min >= 60) ||
  223. (tm.tm_sec >= 60))
  224. return -EINVAL;
  225. century = (tm.tm_year >= 2000) ? 0x80 : 0;
  226. tm.tm_year = tm.tm_year % 100;
  227. BIN_TO_BCD(tm.tm_year);
  228. BIN_TO_BCD(tm.tm_mday);
  229. BIN_TO_BCD(tm.tm_hour);
  230. BIN_TO_BCD(tm.tm_min);
  231. BIN_TO_BCD(tm.tm_sec);
  232. tm.tm_mon |= century;
  233. rtc_write(RTC_YEAR, tm.tm_year);
  234. rtc_write(RTC_MONTH, tm.tm_mon);
  235. rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
  236. rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
  237. rtc_write(RTC_HOURS, tm.tm_hour);
  238. rtc_write(RTC_MINUTES, tm.tm_min);
  239. rtc_write(RTC_SECONDS, tm.tm_sec);
  240. return 0;
  241. #endif /* !CONFIG_ETRAX_RTC_READONLY */
  242. }
  243. case RTC_VLOW_RD:
  244. {
  245. int vl_bit = 0;
  246. if (rtc_read(RTC_SECONDS) & 0x80) {
  247. vl_bit = 1;
  248. printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
  249. "date/time information is no longer guaranteed!\n",
  250. PCF8563_NAME);
  251. }
  252. if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
  253. return -EFAULT;
  254. return 0;
  255. }
  256. case RTC_VLOW_SET:
  257. {
  258. /* Clear the VL bit in the seconds register */
  259. int ret = rtc_read(RTC_SECONDS);
  260. rtc_write(RTC_SECONDS, (ret & 0x7F));
  261. return 0;
  262. }
  263. default:
  264. return -ENOTTY;
  265. }
  266. return 0;
  267. }
  268. int
  269. pcf8563_open(struct inode *inode, struct file *filp)
  270. {
  271. return 0;
  272. }
  273. int
  274. pcf8563_release(struct inode *inode, struct file *filp)
  275. {
  276. return 0;
  277. }
  278. module_init(pcf8563_init);
  279. module_exit(pcf8563_exit);