840-rtc7301.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. --- a/drivers/rtc/Kconfig
  2. +++ b/drivers/rtc/Kconfig
  3. @@ -979,6 +979,15 @@ config RTC_DRV_NUC900
  4. If you say yes here you get support for the RTC subsystem of the
  5. NUC910/NUC920 used in embedded systems.
  6. +config RTC_DRV_RTC7301
  7. + tristate "Epson RTC-7301 SF/DG"
  8. + help
  9. + If you say Y here you will get support for the
  10. + Epson RTC-7301 SF/DG RTC chips.
  11. +
  12. + This driver can also be built as a module. If so, the module
  13. + will be called rtc-7301.
  14. +
  15. comment "on-CPU RTC drivers"
  16. config RTC_DRV_DAVINCI
  17. --- a/drivers/rtc/Makefile
  18. +++ b/drivers/rtc/Makefile
  19. @@ -115,6 +115,7 @@ obj-$(CONFIG_RTC_DRV_RP5C01) += rtc-rp5c
  20. obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5c313.o
  21. obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o
  22. obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
  23. +obj-$(CONFIG_RTC_DRV_RTC7301) += rtc-rtc7301.o
  24. obj-$(CONFIG_RTC_DRV_RV3029C2) += rtc-rv3029c2.o
  25. obj-$(CONFIG_RTC_DRV_RX4581) += rtc-rx4581.o
  26. obj-$(CONFIG_RTC_DRV_RX8025) += rtc-rx8025.o
  27. --- /dev/null
  28. +++ b/drivers/rtc/rtc-rtc7301.c
  29. @@ -0,0 +1,219 @@
  30. +/*
  31. + * Driver for Epson RTC-7301SF/DG
  32. + *
  33. + * Copyright (C) 2009 Jose Vasconcellos
  34. + *
  35. + * This program is free software; you can redistribute it and/or modify
  36. + * it under the terms of the GNU General Public License version 2 as
  37. + * published by the Free Software Foundation.
  38. + */
  39. +
  40. +#include <linux/module.h>
  41. +#include <linux/rtc.h>
  42. +#include <linux/platform_device.h>
  43. +#include <linux/io.h>
  44. +#include <linux/delay.h>
  45. +#include <linux/bcd.h>
  46. +
  47. +#define RTC_NAME "rtc7301"
  48. +#define RTC_VERSION "0.1"
  49. +
  50. +/* Epson RTC-7301 register addresses */
  51. +#define RTC7301_SEC 0x00
  52. +#define RTC7301_SEC10 0x01
  53. +#define RTC7301_MIN 0x02
  54. +#define RTC7301_MIN10 0x03
  55. +#define RTC7301_HOUR 0x04
  56. +#define RTC7301_HOUR10 0x05
  57. +#define RTC7301_WEEKDAY 0x06
  58. +#define RTC7301_DAY 0x07
  59. +#define RTC7301_DAY10 0x08
  60. +#define RTC7301_MON 0x09
  61. +#define RTC7301_MON10 0x0A
  62. +#define RTC7301_YEAR 0x0B
  63. +#define RTC7301_YEAR10 0x0C
  64. +#define RTC7301_YEAR100 0x0D
  65. +#define RTC7301_YEAR1000 0x0E
  66. +#define RTC7301_CTRLREG 0x0F
  67. +
  68. +static uint8_t __iomem *rtc7301_base;
  69. +
  70. +#define read_reg(offset) (readb(rtc7301_base + offset) & 0xf)
  71. +#define write_reg(offset, data) writeb(data, rtc7301_base + (offset))
  72. +
  73. +#define rtc7301_isbusy() (read_reg(RTC7301_CTRLREG) & 1)
  74. +
  75. +static void rtc7301_init_settings(void)
  76. +{
  77. + int i;
  78. +
  79. + write_reg(RTC7301_CTRLREG, 2);
  80. + write_reg(RTC7301_YEAR1000, 2);
  81. + udelay(122);
  82. +
  83. + /* bank 1 */
  84. + write_reg(RTC7301_CTRLREG, 6);
  85. + for (i=0; i<15; i++)
  86. + write_reg(i, 0);
  87. +
  88. + /* bank 2 */
  89. + write_reg(RTC7301_CTRLREG, 14);
  90. + for (i=0; i<15; i++)
  91. + write_reg(i, 0);
  92. + write_reg(RTC7301_CTRLREG, 0);
  93. +}
  94. +
  95. +static int rtc7301_get_datetime(struct device *dev, struct rtc_time *dt)
  96. +{
  97. + int cnt;
  98. + uint8_t buf[16];
  99. +
  100. + cnt = 0;
  101. + while (rtc7301_isbusy()) {
  102. + udelay(244);
  103. + if (cnt++ > 100) {
  104. + dev_err(dev, "%s: timeout error %x\n", __func__, rtc7301_base[RTC7301_CTRLREG]);
  105. + return -EIO;
  106. + }
  107. + }
  108. +
  109. + for (cnt=0; cnt<16; cnt++)
  110. + buf[cnt] = read_reg(cnt);
  111. +
  112. + if (buf[RTC7301_SEC10] & 8) {
  113. + dev_err(dev, "%s: RTC not set\n", __func__);
  114. + return -EINVAL;
  115. + }
  116. +
  117. + memset(dt, 0, sizeof(*dt));
  118. +
  119. + dt->tm_sec = buf[RTC7301_SEC] + buf[RTC7301_SEC10]*10;
  120. + dt->tm_min = buf[RTC7301_MIN] + buf[RTC7301_MIN10]*10;
  121. + dt->tm_hour = buf[RTC7301_HOUR] + buf[RTC7301_HOUR10]*10;
  122. +
  123. + dt->tm_mday = buf[RTC7301_DAY] + buf[RTC7301_DAY10]*10;
  124. + dt->tm_mon = buf[RTC7301_MON] + buf[RTC7301_MON10]*10 - 1;
  125. + dt->tm_year = buf[RTC7301_YEAR] + buf[RTC7301_YEAR10]*10 +
  126. + buf[RTC7301_YEAR100]*100 +
  127. + ((buf[RTC7301_YEAR1000] & 3)*1000) - 1900;
  128. +
  129. + /* the rtc device may contain illegal values on power up
  130. + * according to the data sheet. make sure they are valid.
  131. + */
  132. +
  133. + return rtc_valid_tm(dt);
  134. +}
  135. +
  136. +static int rtc7301_set_datetime(struct device *dev, struct rtc_time *dt)
  137. +{
  138. + int data;
  139. +
  140. + data = dt->tm_year + 1900;
  141. + if (data >= 2100 || data < 1900)
  142. + return -EINVAL;
  143. +
  144. + write_reg(RTC7301_CTRLREG, 2);
  145. + udelay(122);
  146. +
  147. + data = bin2bcd(dt->tm_sec);
  148. + write_reg(RTC7301_SEC, data);
  149. + write_reg(RTC7301_SEC10, (data >> 4));
  150. +
  151. + data = bin2bcd(dt->tm_min);
  152. + write_reg(RTC7301_MIN, data );
  153. + write_reg(RTC7301_MIN10, (data >> 4));
  154. +
  155. + data = bin2bcd(dt->tm_hour);
  156. + write_reg(RTC7301_HOUR, data);
  157. + write_reg(RTC7301_HOUR10, (data >> 4));
  158. +
  159. + data = bin2bcd(dt->tm_mday);
  160. + write_reg(RTC7301_DAY, data);
  161. + write_reg(RTC7301_DAY10, (data>> 4));
  162. +
  163. + data = bin2bcd(dt->tm_mon + 1);
  164. + write_reg(RTC7301_MON, data);
  165. + write_reg(RTC7301_MON10, (data >> 4));
  166. +
  167. + data = bin2bcd(dt->tm_year % 100);
  168. + write_reg(RTC7301_YEAR, data);
  169. + write_reg(RTC7301_YEAR10, (data >> 4));
  170. + data = bin2bcd((1900 + dt->tm_year) / 100);
  171. + write_reg(RTC7301_YEAR100, data);
  172. +
  173. + data = bin2bcd(dt->tm_wday);
  174. + write_reg(RTC7301_WEEKDAY, data);
  175. +
  176. + write_reg(RTC7301_CTRLREG, 0);
  177. +
  178. + return 0;
  179. +}
  180. +
  181. +static const struct rtc_class_ops rtc7301_rtc_ops = {
  182. + .read_time = rtc7301_get_datetime,
  183. + .set_time = rtc7301_set_datetime,
  184. +};
  185. +
  186. +static int rtc7301_probe(struct platform_device *pdev)
  187. +{
  188. + struct rtc_device *rtc;
  189. + struct resource *res;
  190. +
  191. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. + if (!res)
  193. + return -ENOENT;
  194. +
  195. + rtc7301_base = ioremap_nocache(res->start, 0x1000 /*res->end - res->start + 1*/);
  196. + if (!rtc7301_base)
  197. + return -EINVAL;
  198. +
  199. + rtc = rtc_device_register(RTC_NAME, &pdev->dev,
  200. + &rtc7301_rtc_ops, THIS_MODULE);
  201. + if (IS_ERR(rtc)) {
  202. + iounmap(rtc7301_base);
  203. + return PTR_ERR(rtc);
  204. + }
  205. +
  206. + platform_set_drvdata(pdev, rtc);
  207. +
  208. + rtc7301_init_settings();
  209. + return 0;
  210. +}
  211. +
  212. +static int rtc7301_remove(struct platform_device *pdev)
  213. +{
  214. + struct rtc_device *rtc = platform_get_drvdata(pdev);
  215. +
  216. + if (rtc)
  217. + rtc_device_unregister(rtc);
  218. + if (rtc7301_base)
  219. + iounmap(rtc7301_base);
  220. + return 0;
  221. +}
  222. +
  223. +static struct platform_driver rtc7301_driver = {
  224. + .driver = {
  225. + .name = RTC_NAME,
  226. + .owner = THIS_MODULE,
  227. + },
  228. + .probe = rtc7301_probe,
  229. + .remove = rtc7301_remove,
  230. +};
  231. +
  232. +static __init int rtc7301_init(void)
  233. +{
  234. + return platform_driver_register(&rtc7301_driver);
  235. +}
  236. +module_init(rtc7301_init);
  237. +
  238. +static __exit void rtc7301_exit(void)
  239. +{
  240. + platform_driver_unregister(&rtc7301_driver);
  241. +}
  242. +module_exit(rtc7301_exit);
  243. +
  244. +MODULE_DESCRIPTION("Epson 7301 RTC driver");
  245. +MODULE_AUTHOR("Jose Vasconcellos <jvasco@verizon.net>");
  246. +MODULE_LICENSE("GPL");
  247. +MODULE_ALIAS("platform:" RTC_NAME);
  248. +MODULE_VERSION(RTC_VERSION);