pcf8563.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, interrupt 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-2007, 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 <linux/mutex.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.17 $"
  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 DEFINE_MUTEX(rtc_lock); /* Protect state etc */
  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. /* Cache VL bit value read at driver init since writing the RTC_SECOND
  46. * register clears the VL status.
  47. */
  48. static int voltage_low;
  49. static const struct file_operations pcf8563_fops = {
  50. .owner = THIS_MODULE,
  51. .ioctl = pcf8563_ioctl
  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. rtc_write(reg, val);
  90. }
  91. void
  92. get_rtc_time(struct rtc_time *tm)
  93. {
  94. tm->tm_sec = rtc_read(RTC_SECONDS);
  95. tm->tm_min = rtc_read(RTC_MINUTES);
  96. tm->tm_hour = rtc_read(RTC_HOURS);
  97. tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
  98. tm->tm_wday = rtc_read(RTC_WEEKDAY);
  99. tm->tm_mon = rtc_read(RTC_MONTH);
  100. tm->tm_year = rtc_read(RTC_YEAR);
  101. if (tm->tm_sec & 0x80) {
  102. printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
  103. "information is no longer guaranteed!\n", PCF8563_NAME);
  104. }
  105. tm->tm_year = BCD_TO_BIN(tm->tm_year) +
  106. ((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. static int res;
  124. static int first = 1;
  125. if (!first)
  126. return res;
  127. first = 0;
  128. /* Initiate the i2c protocol. */
  129. res = i2c_init();
  130. if (res < 0) {
  131. printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
  132. return res;
  133. }
  134. /*
  135. * First of all we need to reset the chip. This is done by
  136. * clearing control1, control2 and clk freq and resetting
  137. * all alarms.
  138. */
  139. if (rtc_write(RTC_CONTROL1, 0x00) < 0)
  140. goto err;
  141. if (rtc_write(RTC_CONTROL2, 0x00) < 0)
  142. goto err;
  143. if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
  144. goto err;
  145. if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
  146. goto err;
  147. /* Reset the alarms. */
  148. if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
  149. goto err;
  150. if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
  151. goto err;
  152. if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
  153. goto err;
  154. if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
  155. goto err;
  156. /* Check for low voltage, and warn about it. */
  157. if (rtc_read(RTC_SECONDS) & 0x80) {
  158. voltage_low = 1;
  159. printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
  160. "date/time information is no longer guaranteed!\n",
  161. PCF8563_NAME);
  162. }
  163. return res;
  164. err:
  165. printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
  166. res = -1;
  167. return res;
  168. }
  169. void __exit
  170. pcf8563_exit(void)
  171. {
  172. unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
  173. }
  174. /*
  175. * ioctl calls for this driver. Why return -ENOTTY upon error? Because
  176. * POSIX says so!
  177. */
  178. int pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  179. unsigned long arg)
  180. {
  181. /* Some sanity checks. */
  182. if (_IOC_TYPE(cmd) != RTC_MAGIC)
  183. return -ENOTTY;
  184. if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
  185. return -ENOTTY;
  186. switch (cmd) {
  187. case RTC_RD_TIME:
  188. {
  189. struct rtc_time tm;
  190. mutex_lock(&rtc_lock);
  191. memset(&tm, 0, sizeof tm);
  192. get_rtc_time(&tm);
  193. if (copy_to_user((struct rtc_time *) arg, &tm,
  194. sizeof tm)) {
  195. mutex_unlock(&rtc_lock);
  196. return -EFAULT;
  197. }
  198. mutex_unlock(&rtc_lock);
  199. return 0;
  200. }
  201. case RTC_SET_TIME:
  202. {
  203. int leap;
  204. int year;
  205. int century;
  206. struct rtc_time tm;
  207. memset(&tm, 0, sizeof tm);
  208. if (!capable(CAP_SYS_TIME))
  209. return -EPERM;
  210. if (copy_from_user(&tm, (struct rtc_time *) arg,
  211. sizeof tm))
  212. return -EFAULT;
  213. /* Convert from struct tm to struct rtc_time. */
  214. tm.tm_year += 1900;
  215. tm.tm_mon += 1;
  216. /*
  217. * Check if tm.tm_year is a leap year. A year is a leap
  218. * year if it is divisible by 4 but not 100, except
  219. * that years divisible by 400 _are_ leap years.
  220. */
  221. year = tm.tm_year;
  222. leap = (tm.tm_mon == 2) &&
  223. ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  224. /* Perform some sanity checks. */
  225. if ((tm.tm_year < 1970) ||
  226. (tm.tm_mon > 12) ||
  227. (tm.tm_mday == 0) ||
  228. (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
  229. (tm.tm_wday >= 7) ||
  230. (tm.tm_hour >= 24) ||
  231. (tm.tm_min >= 60) ||
  232. (tm.tm_sec >= 60))
  233. return -EINVAL;
  234. century = (tm.tm_year >= 2000) ? 0x80 : 0;
  235. tm.tm_year = tm.tm_year % 100;
  236. BIN_TO_BCD(tm.tm_year);
  237. BIN_TO_BCD(tm.tm_mon);
  238. BIN_TO_BCD(tm.tm_mday);
  239. BIN_TO_BCD(tm.tm_hour);
  240. BIN_TO_BCD(tm.tm_min);
  241. BIN_TO_BCD(tm.tm_sec);
  242. tm.tm_mon |= century;
  243. mutex_lock(&rtc_lock);
  244. rtc_write(RTC_YEAR, tm.tm_year);
  245. rtc_write(RTC_MONTH, tm.tm_mon);
  246. rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
  247. rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
  248. rtc_write(RTC_HOURS, tm.tm_hour);
  249. rtc_write(RTC_MINUTES, tm.tm_min);
  250. rtc_write(RTC_SECONDS, tm.tm_sec);
  251. mutex_unlock(&rtc_lock);
  252. return 0;
  253. }
  254. case RTC_VL_READ:
  255. if (voltage_low)
  256. printk(KERN_ERR "%s: RTC Voltage Low - "
  257. "reliable date/time information is no "
  258. "longer guaranteed!\n", PCF8563_NAME);
  259. if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
  260. return -EFAULT;
  261. return 0;
  262. case RTC_VL_CLR:
  263. {
  264. /* Clear the VL bit in the seconds register in case
  265. * the time has not been set already (which would
  266. * have cleared it). This does not really matter
  267. * because of the cached voltage_low value but do it
  268. * anyway for consistency. */
  269. int ret = rtc_read(RTC_SECONDS);
  270. rtc_write(RTC_SECONDS, (ret & 0x7F));
  271. /* Clear the cached value. */
  272. voltage_low = 0;
  273. return 0;
  274. }
  275. default:
  276. return -ENOTTY;
  277. }
  278. return 0;
  279. }
  280. static int __init pcf8563_register(void)
  281. {
  282. if (pcf8563_init() < 0) {
  283. printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
  284. "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
  285. return -1;
  286. }
  287. if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
  288. printk(KERN_INFO "%s: Unable to get major numer %d for RTC "
  289. "device.\n", PCF8563_NAME, PCF8563_MAJOR);
  290. return -1;
  291. }
  292. printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
  293. DRIVER_VERSION);
  294. /* Check for low voltage, and warn about it. */
  295. if (voltage_low) {
  296. printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
  297. "information is no longer guaranteed!\n", PCF8563_NAME);
  298. }
  299. return 0;
  300. }
  301. module_init(pcf8563_register);
  302. module_exit(pcf8563_exit);