pcf8563.c 8.5 KB

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