pcf8563.c 7.4 KB

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