rtc-ab3100.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2007-2009 ST-Ericsson AB
  3. * License terms: GNU General Public License (GPL) version 2
  4. * RTC clock driver for the AB3100 Analog Baseband Chip
  5. * Author: Linus Walleij <linus.walleij@stericsson.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/rtc.h>
  12. #include <linux/mfd/abx500.h>
  13. /* Clock rate in Hz */
  14. #define AB3100_RTC_CLOCK_RATE 32768
  15. /*
  16. * The AB3100 RTC registers. These are the same for
  17. * AB3000 and AB3100.
  18. * Control register:
  19. * Bit 0: RTC Monitor cleared=0, active=1, if you set it
  20. * to 1 it remains active until RTC power is lost.
  21. * Bit 1: 32 kHz Oscillator, 0 = on, 1 = bypass
  22. * Bit 2: Alarm on, 0 = off, 1 = on
  23. * Bit 3: 32 kHz buffer disabling, 0 = enabled, 1 = disabled
  24. */
  25. #define AB3100_RTC 0x53
  26. /* default setting, buffer disabled, alarm on */
  27. #define RTC_SETTING 0x30
  28. /* Alarm when AL0-AL3 == TI0-TI3 */
  29. #define AB3100_AL0 0x56
  30. #define AB3100_AL1 0x57
  31. #define AB3100_AL2 0x58
  32. #define AB3100_AL3 0x59
  33. /* This 48-bit register that counts up at 32768 Hz */
  34. #define AB3100_TI0 0x5a
  35. #define AB3100_TI1 0x5b
  36. #define AB3100_TI2 0x5c
  37. #define AB3100_TI3 0x5d
  38. #define AB3100_TI4 0x5e
  39. #define AB3100_TI5 0x5f
  40. /*
  41. * RTC clock functions and device struct declaration
  42. */
  43. static int ab3100_rtc_set_mmss(struct device *dev, unsigned long secs)
  44. {
  45. u8 regs[] = {AB3100_TI0, AB3100_TI1, AB3100_TI2,
  46. AB3100_TI3, AB3100_TI4, AB3100_TI5};
  47. unsigned char buf[6];
  48. u64 fat_time = (u64) secs * AB3100_RTC_CLOCK_RATE * 2;
  49. int err = 0;
  50. int i;
  51. buf[0] = (fat_time) & 0xFF;
  52. buf[1] = (fat_time >> 8) & 0xFF;
  53. buf[2] = (fat_time >> 16) & 0xFF;
  54. buf[3] = (fat_time >> 24) & 0xFF;
  55. buf[4] = (fat_time >> 32) & 0xFF;
  56. buf[5] = (fat_time >> 40) & 0xFF;
  57. for (i = 0; i < 6; i++) {
  58. err = abx500_set_register_interruptible(dev, 0,
  59. regs[i], buf[i]);
  60. if (err)
  61. return err;
  62. }
  63. /* Set the flag to mark that the clock is now set */
  64. return abx500_mask_and_set_register_interruptible(dev, 0,
  65. AB3100_RTC,
  66. 0x01, 0x01);
  67. }
  68. static int ab3100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  69. {
  70. unsigned long time;
  71. u8 rtcval;
  72. int err;
  73. err = abx500_get_register_interruptible(dev, 0,
  74. AB3100_RTC, &rtcval);
  75. if (err)
  76. return err;
  77. if (!(rtcval & 0x01)) {
  78. dev_info(dev, "clock not set (lost power)");
  79. return -EINVAL;
  80. } else {
  81. u64 fat_time;
  82. u8 buf[6];
  83. /* Read out time registers */
  84. err = abx500_get_register_page_interruptible(dev, 0,
  85. AB3100_TI0,
  86. buf, 6);
  87. if (err != 0)
  88. return err;
  89. fat_time = ((u64) buf[5] << 40) | ((u64) buf[4] << 32) |
  90. ((u64) buf[3] << 24) | ((u64) buf[2] << 16) |
  91. ((u64) buf[1] << 8) | (u64) buf[0];
  92. time = (unsigned long) (fat_time /
  93. (u64) (AB3100_RTC_CLOCK_RATE * 2));
  94. }
  95. rtc_time_to_tm(time, tm);
  96. return rtc_valid_tm(tm);
  97. }
  98. static int ab3100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  99. {
  100. unsigned long time;
  101. u64 fat_time;
  102. u8 buf[6];
  103. u8 rtcval;
  104. int err;
  105. /* Figure out if alarm is enabled or not */
  106. err = abx500_get_register_interruptible(dev, 0,
  107. AB3100_RTC, &rtcval);
  108. if (err)
  109. return err;
  110. if (rtcval & 0x04)
  111. alarm->enabled = 1;
  112. else
  113. alarm->enabled = 0;
  114. /* No idea how this could be represented */
  115. alarm->pending = 0;
  116. /* Read out alarm registers, only 4 bytes */
  117. err = abx500_get_register_page_interruptible(dev, 0,
  118. AB3100_AL0, buf, 4);
  119. if (err)
  120. return err;
  121. fat_time = ((u64) buf[3] << 40) | ((u64) buf[2] << 32) |
  122. ((u64) buf[1] << 24) | ((u64) buf[0] << 16);
  123. time = (unsigned long) (fat_time / (u64) (AB3100_RTC_CLOCK_RATE * 2));
  124. rtc_time_to_tm(time, &alarm->time);
  125. return rtc_valid_tm(&alarm->time);
  126. }
  127. static int ab3100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  128. {
  129. u8 regs[] = {AB3100_AL0, AB3100_AL1, AB3100_AL2, AB3100_AL3};
  130. unsigned char buf[4];
  131. unsigned long secs;
  132. u64 fat_time;
  133. int err;
  134. int i;
  135. rtc_tm_to_time(&alarm->time, &secs);
  136. fat_time = (u64) secs * AB3100_RTC_CLOCK_RATE * 2;
  137. buf[0] = (fat_time >> 16) & 0xFF;
  138. buf[1] = (fat_time >> 24) & 0xFF;
  139. buf[2] = (fat_time >> 32) & 0xFF;
  140. buf[3] = (fat_time >> 40) & 0xFF;
  141. /* Set the alarm */
  142. for (i = 0; i < 4; i++) {
  143. err = abx500_set_register_interruptible(dev, 0,
  144. regs[i], buf[i]);
  145. if (err)
  146. return err;
  147. }
  148. /* Then enable the alarm */
  149. return abx500_mask_and_set_register_interruptible(dev, 0,
  150. AB3100_RTC, (1 << 2),
  151. alarm->enabled << 2);
  152. }
  153. static int ab3100_rtc_irq_enable(struct device *dev, unsigned int enabled)
  154. {
  155. /*
  156. * It's not possible to enable/disable the alarm IRQ for this RTC.
  157. * It does not actually trigger any IRQ: instead its only function is
  158. * to power up the system, if it wasn't on. This will manifest as
  159. * a "power up cause" in the AB3100 power driver (battery charging etc)
  160. * and need to be handled there instead.
  161. */
  162. if (enabled)
  163. return abx500_mask_and_set_register_interruptible(dev, 0,
  164. AB3100_RTC, (1 << 2),
  165. 1 << 2);
  166. else
  167. return abx500_mask_and_set_register_interruptible(dev, 0,
  168. AB3100_RTC, (1 << 2),
  169. 0);
  170. }
  171. static const struct rtc_class_ops ab3100_rtc_ops = {
  172. .read_time = ab3100_rtc_read_time,
  173. .set_mmss = ab3100_rtc_set_mmss,
  174. .read_alarm = ab3100_rtc_read_alarm,
  175. .set_alarm = ab3100_rtc_set_alarm,
  176. .alarm_irq_enable = ab3100_rtc_irq_enable,
  177. };
  178. static int __init ab3100_rtc_probe(struct platform_device *pdev)
  179. {
  180. int err;
  181. u8 regval;
  182. struct rtc_device *rtc;
  183. /* The first RTC register needs special treatment */
  184. err = abx500_get_register_interruptible(&pdev->dev, 0,
  185. AB3100_RTC, &regval);
  186. if (err) {
  187. dev_err(&pdev->dev, "unable to read RTC register\n");
  188. return -ENODEV;
  189. }
  190. if ((regval & 0xFE) != RTC_SETTING) {
  191. dev_warn(&pdev->dev, "not default value in RTC reg 0x%x\n",
  192. regval);
  193. }
  194. if ((regval & 1) == 0) {
  195. /*
  196. * Set bit to detect power loss.
  197. * This bit remains until RTC power is lost.
  198. */
  199. regval = 1 | RTC_SETTING;
  200. err = abx500_set_register_interruptible(&pdev->dev, 0,
  201. AB3100_RTC, regval);
  202. /* Ignore any error on this write */
  203. }
  204. rtc = rtc_device_register("ab3100-rtc", &pdev->dev, &ab3100_rtc_ops,
  205. THIS_MODULE);
  206. if (IS_ERR(rtc)) {
  207. err = PTR_ERR(rtc);
  208. return err;
  209. }
  210. platform_set_drvdata(pdev, rtc);
  211. return 0;
  212. }
  213. static int __exit ab3100_rtc_remove(struct platform_device *pdev)
  214. {
  215. struct rtc_device *rtc = platform_get_drvdata(pdev);
  216. rtc_device_unregister(rtc);
  217. platform_set_drvdata(pdev, NULL);
  218. return 0;
  219. }
  220. static struct platform_driver ab3100_rtc_driver = {
  221. .driver = {
  222. .name = "ab3100-rtc",
  223. .owner = THIS_MODULE,
  224. },
  225. .remove = __exit_p(ab3100_rtc_remove),
  226. };
  227. static int __init ab3100_rtc_init(void)
  228. {
  229. return platform_driver_probe(&ab3100_rtc_driver,
  230. ab3100_rtc_probe);
  231. }
  232. static void __exit ab3100_rtc_exit(void)
  233. {
  234. platform_driver_unregister(&ab3100_rtc_driver);
  235. }
  236. module_init(ab3100_rtc_init);
  237. module_exit(ab3100_rtc_exit);
  238. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  239. MODULE_DESCRIPTION("AB3100 RTC Driver");
  240. MODULE_LICENSE("GPL");