pcf8563.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 bute.
  12. *
  13. * Copyright (c) 2002, Axis Communications AB
  14. * All rights reserved.
  15. *
  16. * Author: Tobias Anderberg <tobiasa@axis.com>.
  17. *
  18. * $Id: pcf8563.c,v 1.11 2005/03/07 13:13:07 starvik Exp $
  19. */
  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 <linux/capability.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/system.h>
  32. #include <asm/io.h>
  33. #include <asm/arch/svinto.h>
  34. #include <asm/rtc.h>
  35. #include "i2c.h"
  36. #define PCF8563_MAJOR 121 /* Local major number. */
  37. #define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */
  38. #define PCF8563_NAME "PCF8563"
  39. #define DRIVER_VERSION "$Revision: 1.11 $"
  40. /* I2C bus slave registers. */
  41. #define RTC_I2C_READ 0xa3
  42. #define RTC_I2C_WRITE 0xa2
  43. /* Two simple wrapper macros, saves a few keystrokes. */
  44. #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
  45. #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
  46. static DEFINE_SPINLOCK(rtc_lock); /* Protect state etc */
  47. static const unsigned char days_in_month[] =
  48. { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  49. int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
  50. static struct file_operations pcf8563_fops = {
  51. .owner = THIS_MODULE,
  52. .ioctl = pcf8563_ioctl,
  53. };
  54. unsigned char
  55. pcf8563_readreg(int reg)
  56. {
  57. unsigned char res = i2c_readreg(RTC_I2C_READ, reg);
  58. /* The PCF8563 does not return 0 for unimplemented bits */
  59. switch(reg)
  60. {
  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_MONTH:
  70. res = (res & 0x1f) - 1; /* PCF8563 returns month in range 1-12 */
  71. break;
  72. }
  73. return res;
  74. }
  75. void
  76. pcf8563_writereg(int reg, unsigned char val)
  77. {
  78. #ifdef CONFIG_ETRAX_RTC_READONLY
  79. if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
  80. return;
  81. #endif
  82. rtc_write(reg, val);
  83. }
  84. void
  85. get_rtc_time(struct rtc_time *tm)
  86. {
  87. tm->tm_sec = rtc_read(RTC_SECONDS);
  88. tm->tm_min = rtc_read(RTC_MINUTES);
  89. tm->tm_hour = rtc_read(RTC_HOURS);
  90. tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
  91. tm->tm_mon = rtc_read(RTC_MONTH);
  92. tm->tm_year = rtc_read(RTC_YEAR);
  93. if (tm->tm_sec & 0x80)
  94. printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
  95. tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
  96. tm->tm_sec &= 0x7f;
  97. tm->tm_min &= 0x7f;
  98. tm->tm_hour &= 0x3f;
  99. tm->tm_mday &= 0x3f;
  100. tm->tm_mon &= 0x1f;
  101. BCD_TO_BIN(tm->tm_sec);
  102. BCD_TO_BIN(tm->tm_min);
  103. BCD_TO_BIN(tm->tm_hour);
  104. BCD_TO_BIN(tm->tm_mday);
  105. BCD_TO_BIN(tm->tm_mon);
  106. tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
  107. }
  108. int __init
  109. pcf8563_init(void)
  110. {
  111. int ret;
  112. if ((ret = i2c_init())) {
  113. printk(KERN_CRIT "pcf8563_init: failed to init i2c\n");
  114. return ret;
  115. }
  116. /*
  117. * First of all we need to reset the chip. This is done by
  118. * clearing control1, control2 and clk freq, clear the
  119. * Voltage Low bit, and resetting all alarms.
  120. */
  121. if (rtc_write(RTC_CONTROL1, 0x00) < 0)
  122. goto err;
  123. if (rtc_write(RTC_CONTROL2, 0x00) < 0)
  124. goto err;
  125. if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
  126. goto err;
  127. /* Clear the VL bit in the seconds register. */
  128. ret = rtc_read(RTC_SECONDS);
  129. if (rtc_write(RTC_SECONDS, (ret & 0x7f)) < 0)
  130. goto err;
  131. /* Reset the alarms. */
  132. if (rtc_write(RTC_MINUTE_ALARM, 0x00) < 0)
  133. goto err;
  134. if (rtc_write(RTC_HOUR_ALARM, 0x00) < 0)
  135. goto err;
  136. if (rtc_write(RTC_DAY_ALARM, 0x00) < 0)
  137. goto err;
  138. if (rtc_write(RTC_WEEKDAY_ALARM, 0x00) < 0)
  139. goto err;
  140. /* Check for low voltage, and warn about it.. */
  141. if (rtc_read(RTC_SECONDS) & 0x80)
  142. printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
  143. return 0;
  144. err:
  145. printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
  146. return -1;
  147. }
  148. void __exit
  149. pcf8563_exit(void)
  150. {
  151. if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
  152. printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
  153. }
  154. }
  155. /*
  156. * ioctl calls for this driver. Why return -ENOTTY upon error? Because
  157. * POSIX says so!
  158. */
  159. int
  160. pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  161. {
  162. /* Some sanity checks. */
  163. if (_IOC_TYPE(cmd) != RTC_MAGIC)
  164. return -ENOTTY;
  165. if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
  166. return -ENOTTY;
  167. switch (cmd) {
  168. case RTC_RD_TIME:
  169. {
  170. struct rtc_time tm;
  171. spin_lock(&rtc_lock);
  172. get_rtc_time(&tm);
  173. if (copy_to_user((struct rtc_time *) arg, &tm, sizeof(struct rtc_time))) {
  174. spin_unlock(&rtc_lock);
  175. return -EFAULT;
  176. }
  177. spin_unlock(&rtc_lock);
  178. return 0;
  179. }
  180. break;
  181. case RTC_SET_TIME:
  182. {
  183. #ifdef CONFIG_ETRAX_RTC_READONLY
  184. return -EPERM;
  185. #else
  186. int leap;
  187. int century;
  188. struct rtc_time tm;
  189. memset(&tm, 0, sizeof (struct rtc_time));
  190. if (!capable(CAP_SYS_TIME))
  191. return -EPERM;
  192. if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time)))
  193. return -EFAULT;
  194. /* Convert from struct tm to struct rtc_time. */
  195. tm.tm_year += 1900;
  196. tm.tm_mon += 1;
  197. leap = ((tm.tm_mon == 2) && ((tm.tm_year % 4) == 0)) ? 1 : 0;
  198. /* Perform some sanity checks. */
  199. if ((tm.tm_year < 1970) ||
  200. (tm.tm_mon > 12) ||
  201. (tm.tm_mday == 0) ||
  202. (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
  203. (tm.tm_hour >= 24) ||
  204. (tm.tm_min >= 60) ||
  205. (tm.tm_sec >= 60))
  206. return -EINVAL;
  207. century = (tm.tm_year >= 2000) ? 0x80 : 0;
  208. tm.tm_year = tm.tm_year % 100;
  209. BIN_TO_BCD(tm.tm_year);
  210. BIN_TO_BCD(tm.tm_mday);
  211. BIN_TO_BCD(tm.tm_hour);
  212. BIN_TO_BCD(tm.tm_min);
  213. BIN_TO_BCD(tm.tm_sec);
  214. tm.tm_mon |= century;
  215. spin_lock(&rtc_lock);
  216. rtc_write(RTC_YEAR, tm.tm_year);
  217. rtc_write(RTC_MONTH, tm.tm_mon);
  218. rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
  219. rtc_write(RTC_HOURS, tm.tm_hour);
  220. rtc_write(RTC_MINUTES, tm.tm_min);
  221. rtc_write(RTC_SECONDS, tm.tm_sec);
  222. spin_unlock(&rtc_lock);
  223. return 0;
  224. #endif /* !CONFIG_ETRAX_RTC_READONLY */
  225. }
  226. case RTC_VLOW_RD:
  227. {
  228. int vl_bit = 0;
  229. if (rtc_read(RTC_SECONDS) & 0x80) {
  230. vl_bit = 1;
  231. printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
  232. "date/time information is no longer guaranteed!\n",
  233. PCF8563_NAME);
  234. }
  235. if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
  236. return -EFAULT;
  237. return 0;
  238. }
  239. case RTC_VLOW_SET:
  240. {
  241. /* Clear the VL bit in the seconds register */
  242. int ret = rtc_read(RTC_SECONDS);
  243. rtc_write(RTC_SECONDS, (ret & 0x7F));
  244. return 0;
  245. }
  246. default:
  247. return -ENOTTY;
  248. }
  249. return 0;
  250. }
  251. static int __init
  252. pcf8563_register(void)
  253. {
  254. pcf8563_init();
  255. if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
  256. printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.\n",
  257. PCF8563_NAME, PCF8563_MAJOR);
  258. return -1;
  259. }
  260. printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
  261. return 0;
  262. }
  263. module_init(pcf8563_register);
  264. module_exit(pcf8563_exit);