pcf8563.c 7.7 KB

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