pcf8563.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.24 $"
  38. /* I2C bus slave registers. */
  39. #define RTC_I2C_READ 0xa3
  40. #define RTC_I2C_WRITE 0xa2
  41. /* Two simple wrapper macros, saves a few keystrokes. */
  42. #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
  43. #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
  44. static DEFINE_MUTEX(pcf8563_mutex);
  45. static DEFINE_MUTEX(rtc_lock); /* Protect state etc */
  46. static const unsigned char days_in_month[] =
  47. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  48. static long pcf8563_unlocked_ioctl(struct file *, unsigned int, unsigned long);
  49. /* Cache VL bit value read at driver init since writing the RTC_SECOND
  50. * register clears the VL status.
  51. */
  52. static int voltage_low;
  53. static const struct file_operations pcf8563_fops = {
  54. .owner = THIS_MODULE,
  55. .unlocked_ioctl = pcf8563_unlocked_ioctl,
  56. .llseek = noop_llseek,
  57. };
  58. unsigned char
  59. pcf8563_readreg(int reg)
  60. {
  61. unsigned char res = rtc_read(reg);
  62. /* The PCF8563 does not return 0 for unimplemented bits. */
  63. switch (reg) {
  64. case RTC_SECONDS:
  65. case RTC_MINUTES:
  66. res &= 0x7F;
  67. break;
  68. case RTC_HOURS:
  69. case RTC_DAY_OF_MONTH:
  70. res &= 0x3F;
  71. break;
  72. case RTC_WEEKDAY:
  73. res &= 0x07;
  74. break;
  75. case RTC_MONTH:
  76. res &= 0x1F;
  77. break;
  78. case RTC_CONTROL1:
  79. res &= 0xA8;
  80. break;
  81. case RTC_CONTROL2:
  82. res &= 0x1F;
  83. break;
  84. case RTC_CLOCKOUT_FREQ:
  85. case RTC_TIMER_CONTROL:
  86. res &= 0x83;
  87. break;
  88. }
  89. return res;
  90. }
  91. void
  92. pcf8563_writereg(int reg, unsigned char val)
  93. {
  94. rtc_write(reg, val);
  95. }
  96. void
  97. get_rtc_time(struct rtc_time *tm)
  98. {
  99. tm->tm_sec = rtc_read(RTC_SECONDS);
  100. tm->tm_min = rtc_read(RTC_MINUTES);
  101. tm->tm_hour = rtc_read(RTC_HOURS);
  102. tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
  103. tm->tm_wday = rtc_read(RTC_WEEKDAY);
  104. tm->tm_mon = rtc_read(RTC_MONTH);
  105. tm->tm_year = rtc_read(RTC_YEAR);
  106. if (tm->tm_sec & 0x80) {
  107. printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
  108. "information is no longer guaranteed!\n", PCF8563_NAME);
  109. }
  110. tm->tm_year = bcd2bin(tm->tm_year) +
  111. ((tm->tm_mon & 0x80) ? 100 : 0);
  112. tm->tm_sec &= 0x7F;
  113. tm->tm_min &= 0x7F;
  114. tm->tm_hour &= 0x3F;
  115. tm->tm_mday &= 0x3F;
  116. tm->tm_wday &= 0x07; /* Not coded in BCD. */
  117. tm->tm_mon &= 0x1F;
  118. tm->tm_sec = bcd2bin(tm->tm_sec);
  119. tm->tm_min = bcd2bin(tm->tm_min);
  120. tm->tm_hour = bcd2bin(tm->tm_hour);
  121. tm->tm_mday = bcd2bin(tm->tm_mday);
  122. tm->tm_mon = bcd2bin(tm->tm_mon);
  123. tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
  124. }
  125. int __init
  126. pcf8563_init(void)
  127. {
  128. static int res;
  129. static int first = 1;
  130. if (!first)
  131. return res;
  132. first = 0;
  133. /* Initiate the i2c protocol. */
  134. res = i2c_init();
  135. if (res < 0) {
  136. printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
  137. return res;
  138. }
  139. /*
  140. * First of all we need to reset the chip. This is done by
  141. * clearing control1, control2 and clk freq and resetting
  142. * all alarms.
  143. */
  144. if (rtc_write(RTC_CONTROL1, 0x00) < 0)
  145. goto err;
  146. if (rtc_write(RTC_CONTROL2, 0x00) < 0)
  147. goto err;
  148. if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
  149. goto err;
  150. if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
  151. goto err;
  152. /* Reset the alarms. */
  153. if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
  154. goto err;
  155. if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
  156. goto err;
  157. if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
  158. goto err;
  159. if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
  160. goto err;
  161. /* Check for low voltage, and warn about it. */
  162. if (rtc_read(RTC_SECONDS) & 0x80) {
  163. voltage_low = 1;
  164. printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
  165. "date/time information is no longer guaranteed!\n",
  166. PCF8563_NAME);
  167. }
  168. return res;
  169. err:
  170. printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
  171. res = -1;
  172. return res;
  173. }
  174. void __exit
  175. pcf8563_exit(void)
  176. {
  177. unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
  178. }
  179. /*
  180. * ioctl calls for this driver. Why return -ENOTTY upon error? Because
  181. * POSIX says so!
  182. */
  183. static int pcf8563_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  184. {
  185. /* Some sanity checks. */
  186. if (_IOC_TYPE(cmd) != RTC_MAGIC)
  187. return -ENOTTY;
  188. if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
  189. return -ENOTTY;
  190. switch (cmd) {
  191. case RTC_RD_TIME:
  192. {
  193. struct rtc_time tm;
  194. mutex_lock(&rtc_lock);
  195. memset(&tm, 0, sizeof tm);
  196. get_rtc_time(&tm);
  197. if (copy_to_user((struct rtc_time *) arg, &tm,
  198. sizeof tm)) {
  199. mutex_unlock(&rtc_lock);
  200. return -EFAULT;
  201. }
  202. mutex_unlock(&rtc_lock);
  203. return 0;
  204. }
  205. case RTC_SET_TIME:
  206. {
  207. int leap;
  208. int year;
  209. int century;
  210. struct rtc_time tm;
  211. memset(&tm, 0, sizeof tm);
  212. if (!capable(CAP_SYS_TIME))
  213. return -EPERM;
  214. if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
  215. return -EFAULT;
  216. /* Convert from struct tm to struct rtc_time. */
  217. tm.tm_year += 1900;
  218. tm.tm_mon += 1;
  219. /*
  220. * Check if tm.tm_year is a leap year. A year is a leap
  221. * year if it is divisible by 4 but not 100, except
  222. * that years divisible by 400 _are_ leap years.
  223. */
  224. year = tm.tm_year;
  225. leap = (tm.tm_mon == 2) &&
  226. ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  227. /* Perform some sanity checks. */
  228. if ((tm.tm_year < 1970) ||
  229. (tm.tm_mon > 12) ||
  230. (tm.tm_mday == 0) ||
  231. (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
  232. (tm.tm_wday >= 7) ||
  233. (tm.tm_hour >= 24) ||
  234. (tm.tm_min >= 60) ||
  235. (tm.tm_sec >= 60))
  236. return -EINVAL;
  237. century = (tm.tm_year >= 2000) ? 0x80 : 0;
  238. tm.tm_year = tm.tm_year % 100;
  239. tm.tm_year = bin2bcd(tm.tm_year);
  240. tm.tm_mon = bin2bcd(tm.tm_mon);
  241. tm.tm_mday = bin2bcd(tm.tm_mday);
  242. tm.tm_hour = bin2bcd(tm.tm_hour);
  243. tm.tm_min = bin2bcd(tm.tm_min);
  244. tm.tm_sec = bin2bcd(tm.tm_sec);
  245. tm.tm_mon |= century;
  246. mutex_lock(&rtc_lock);
  247. rtc_write(RTC_YEAR, tm.tm_year);
  248. rtc_write(RTC_MONTH, tm.tm_mon);
  249. rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
  250. rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
  251. rtc_write(RTC_HOURS, tm.tm_hour);
  252. rtc_write(RTC_MINUTES, tm.tm_min);
  253. rtc_write(RTC_SECONDS, tm.tm_sec);
  254. mutex_unlock(&rtc_lock);
  255. return 0;
  256. }
  257. case RTC_VL_READ:
  258. if (voltage_low) {
  259. printk(KERN_ERR "%s: RTC Voltage Low - "
  260. "reliable date/time information is no "
  261. "longer guaranteed!\n", PCF8563_NAME);
  262. }
  263. if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
  264. return -EFAULT;
  265. return 0;
  266. case RTC_VL_CLR:
  267. {
  268. /* Clear the VL bit in the seconds register in case
  269. * the time has not been set already (which would
  270. * have cleared it). This does not really matter
  271. * because of the cached voltage_low value but do it
  272. * anyway for consistency. */
  273. int ret = rtc_read(RTC_SECONDS);
  274. rtc_write(RTC_SECONDS, (ret & 0x7F));
  275. /* Clear the cached value. */
  276. voltage_low = 0;
  277. return 0;
  278. }
  279. default:
  280. return -ENOTTY;
  281. }
  282. return 0;
  283. }
  284. static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  285. {
  286. int ret;
  287. mutex_lock(&pcf8563_mutex);
  288. ret = pcf8563_ioctl(filp, cmd, arg);
  289. mutex_unlock(&pcf8563_mutex);
  290. return ret;
  291. }
  292. static int __init pcf8563_register(void)
  293. {
  294. if (pcf8563_init() < 0) {
  295. printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
  296. "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
  297. return -1;
  298. }
  299. if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
  300. printk(KERN_INFO "%s: Unable to get major number %d for RTC device.\n",
  301. PCF8563_NAME, PCF8563_MAJOR);
  302. return -1;
  303. }
  304. printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
  305. DRIVER_VERSION);
  306. /* Check for low voltage, and warn about it. */
  307. if (voltage_low) {
  308. printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
  309. "information is no longer guaranteed!\n", PCF8563_NAME);
  310. }
  311. return 0;
  312. }
  313. module_init(pcf8563_register);
  314. module_exit(pcf8563_exit);