rtc-ab3100.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/ab3100.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. struct ab3100 *ab3100_data = dev_get_drvdata(dev);
  46. u8 regs[] = {AB3100_TI0, AB3100_TI1, AB3100_TI2,
  47. AB3100_TI3, AB3100_TI4, AB3100_TI5};
  48. unsigned char buf[6];
  49. u64 fat_time = (u64) secs * AB3100_RTC_CLOCK_RATE * 2;
  50. int err = 0;
  51. int i;
  52. buf[0] = (fat_time) & 0xFF;
  53. buf[1] = (fat_time >> 8) & 0xFF;
  54. buf[2] = (fat_time >> 16) & 0xFF;
  55. buf[3] = (fat_time >> 24) & 0xFF;
  56. buf[4] = (fat_time >> 32) & 0xFF;
  57. buf[5] = (fat_time >> 40) & 0xFF;
  58. for (i = 0; i < 6; i++) {
  59. err = ab3100_set_register_interruptible(ab3100_data,
  60. regs[i], buf[i]);
  61. if (err)
  62. return err;
  63. }
  64. /* Set the flag to mark that the clock is now set */
  65. return ab3100_mask_and_set_register_interruptible(ab3100_data,
  66. AB3100_RTC,
  67. 0xFE, 0x01);
  68. }
  69. static int ab3100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. struct ab3100 *ab3100_data = dev_get_drvdata(dev);
  72. unsigned long time;
  73. u8 rtcval;
  74. int err;
  75. err = ab3100_get_register_interruptible(ab3100_data,
  76. AB3100_RTC, &rtcval);
  77. if (err)
  78. return err;
  79. if (!(rtcval & 0x01)) {
  80. dev_info(dev, "clock not set (lost power)");
  81. return -EINVAL;
  82. } else {
  83. u64 fat_time;
  84. u8 buf[6];
  85. /* Read out time registers */
  86. err = ab3100_get_register_page_interruptible(ab3100_data,
  87. AB3100_TI0,
  88. buf, 6);
  89. if (err != 0)
  90. return err;
  91. fat_time = ((u64) buf[5] << 40) | ((u64) buf[4] << 32) |
  92. ((u64) buf[3] << 24) | ((u64) buf[2] << 16) |
  93. ((u64) buf[1] << 8) | (u64) buf[0];
  94. time = (unsigned long) (fat_time /
  95. (u64) (AB3100_RTC_CLOCK_RATE * 2));
  96. }
  97. rtc_time_to_tm(time, tm);
  98. return rtc_valid_tm(tm);
  99. }
  100. static int ab3100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  101. {
  102. struct ab3100 *ab3100_data = dev_get_drvdata(dev);
  103. unsigned long time;
  104. u64 fat_time;
  105. u8 buf[6];
  106. u8 rtcval;
  107. int err;
  108. /* Figure out if alarm is enabled or not */
  109. err = ab3100_get_register_interruptible(ab3100_data,
  110. AB3100_RTC, &rtcval);
  111. if (err)
  112. return err;
  113. if (rtcval & 0x04)
  114. alarm->enabled = 1;
  115. else
  116. alarm->enabled = 0;
  117. /* No idea how this could be represented */
  118. alarm->pending = 0;
  119. /* Read out alarm registers, only 4 bytes */
  120. err = ab3100_get_register_page_interruptible(ab3100_data,
  121. AB3100_AL0, buf, 4);
  122. if (err)
  123. return err;
  124. fat_time = ((u64) buf[3] << 40) | ((u64) buf[2] << 32) |
  125. ((u64) buf[1] << 24) | ((u64) buf[0] << 16);
  126. time = (unsigned long) (fat_time / (u64) (AB3100_RTC_CLOCK_RATE * 2));
  127. rtc_time_to_tm(time, &alarm->time);
  128. return rtc_valid_tm(&alarm->time);
  129. }
  130. static int ab3100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  131. {
  132. struct ab3100 *ab3100_data = dev_get_drvdata(dev);
  133. u8 regs[] = {AB3100_AL0, AB3100_AL1, AB3100_AL2, AB3100_AL3};
  134. unsigned char buf[4];
  135. unsigned long secs;
  136. u64 fat_time;
  137. int err;
  138. int i;
  139. rtc_tm_to_time(&alarm->time, &secs);
  140. fat_time = (u64) secs * AB3100_RTC_CLOCK_RATE * 2;
  141. buf[0] = (fat_time >> 16) & 0xFF;
  142. buf[1] = (fat_time >> 24) & 0xFF;
  143. buf[2] = (fat_time >> 32) & 0xFF;
  144. buf[3] = (fat_time >> 40) & 0xFF;
  145. /* Set the alarm */
  146. for (i = 0; i < 4; i++) {
  147. err = ab3100_set_register_interruptible(ab3100_data,
  148. regs[i], buf[i]);
  149. if (err)
  150. return err;
  151. }
  152. /* Then enable the alarm */
  153. return ab3100_mask_and_set_register_interruptible(ab3100_data,
  154. AB3100_RTC, ~(1 << 2),
  155. alarm->enabled << 2);
  156. }
  157. static int ab3100_rtc_irq_enable(struct device *dev, unsigned int enabled)
  158. {
  159. struct ab3100 *ab3100_data = dev_get_drvdata(dev);
  160. /*
  161. * It's not possible to enable/disable the alarm IRQ for this RTC.
  162. * It does not actually trigger any IRQ: instead its only function is
  163. * to power up the system, if it wasn't on. This will manifest as
  164. * a "power up cause" in the AB3100 power driver (battery charging etc)
  165. * and need to be handled there instead.
  166. */
  167. if (enabled)
  168. return ab3100_mask_and_set_register_interruptible(ab3100_data,
  169. AB3100_RTC, ~(1 << 2),
  170. 1 << 2);
  171. else
  172. return ab3100_mask_and_set_register_interruptible(ab3100_data,
  173. AB3100_RTC, ~(1 << 2),
  174. 0);
  175. }
  176. static const struct rtc_class_ops ab3100_rtc_ops = {
  177. .read_time = ab3100_rtc_read_time,
  178. .set_mmss = ab3100_rtc_set_mmss,
  179. .read_alarm = ab3100_rtc_read_alarm,
  180. .set_alarm = ab3100_rtc_set_alarm,
  181. .alarm_irq_enable = ab3100_rtc_irq_enable,
  182. };
  183. static int __init ab3100_rtc_probe(struct platform_device *pdev)
  184. {
  185. int err;
  186. u8 regval;
  187. struct rtc_device *rtc;
  188. struct ab3100 *ab3100_data = platform_get_drvdata(pdev);
  189. /* The first RTC register needs special treatment */
  190. err = ab3100_get_register_interruptible(ab3100_data,
  191. AB3100_RTC, &regval);
  192. if (err) {
  193. dev_err(&pdev->dev, "unable to read RTC register\n");
  194. return -ENODEV;
  195. }
  196. if ((regval & 0xFE) != RTC_SETTING) {
  197. dev_warn(&pdev->dev, "not default value in RTC reg 0x%x\n",
  198. regval);
  199. }
  200. if ((regval & 1) == 0) {
  201. /*
  202. * Set bit to detect power loss.
  203. * This bit remains until RTC power is lost.
  204. */
  205. regval = 1 | RTC_SETTING;
  206. err = ab3100_set_register_interruptible(ab3100_data,
  207. AB3100_RTC, regval);
  208. /* Ignore any error on this write */
  209. }
  210. rtc = rtc_device_register("ab3100-rtc", &pdev->dev, &ab3100_rtc_ops,
  211. THIS_MODULE);
  212. if (IS_ERR(rtc)) {
  213. err = PTR_ERR(rtc);
  214. return err;
  215. }
  216. return 0;
  217. }
  218. static int __exit ab3100_rtc_remove(struct platform_device *pdev)
  219. {
  220. struct rtc_device *rtc = platform_get_drvdata(pdev);
  221. rtc_device_unregister(rtc);
  222. return 0;
  223. }
  224. static struct platform_driver ab3100_rtc_driver = {
  225. .driver = {
  226. .name = "ab3100-rtc",
  227. .owner = THIS_MODULE,
  228. },
  229. .remove = __exit_p(ab3100_rtc_remove),
  230. };
  231. static int __init ab3100_rtc_init(void)
  232. {
  233. return platform_driver_probe(&ab3100_rtc_driver,
  234. ab3100_rtc_probe);
  235. }
  236. static void __exit ab3100_rtc_exit(void)
  237. {
  238. platform_driver_unregister(&ab3100_rtc_driver);
  239. }
  240. module_init(ab3100_rtc_init);
  241. module_exit(ab3100_rtc_exit);
  242. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  243. MODULE_DESCRIPTION("AB3100 RTC Driver");
  244. MODULE_LICENSE("GPL");