pcf8563.c 7.6 KB

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