rtc-msm7x00a.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* drivers/rtc/rtc-msm7x00a.c
  2. *
  3. * Copyright (C) 2008 Google, Inc.
  4. * Author: San Mehat <san@google.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/version.h>
  18. #include <linux/kernel.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/init.h>
  21. #include <linux/types.h>
  22. #include <linux/rtc.h>
  23. #include <linux/msm_rpcrouter.h>
  24. #include <mach/msm_rpcrouter.h>
  25. #define RTC_DEBUG 0
  26. extern void msm_pm_set_max_sleep_time(int64_t sleep_time_ns);
  27. #if CONFIG_MSM_AMSS_VERSION >= 6350 || defined(CONFIG_ARCH_QSD8X50)
  28. #define APP_TIMEREMOTE_PDEV_NAME "rs30000048:00010000"
  29. #else
  30. #define APP_TIMEREMOTE_PDEV_NAME "rs30000048:0da5b528"
  31. #endif
  32. #define TIMEREMOTE_PROCEEDURE_SET_JULIAN 6
  33. #define TIMEREMOTE_PROCEEDURE_GET_JULIAN 7
  34. struct rpc_time_julian {
  35. uint32_t year;
  36. uint32_t month;
  37. uint32_t day;
  38. uint32_t hour;
  39. uint32_t minute;
  40. uint32_t second;
  41. uint32_t day_of_week;
  42. };
  43. static struct msm_rpc_endpoint *ep;
  44. static struct rtc_device *rtc;
  45. static unsigned long rtcalarm_time;
  46. static int
  47. msmrtc_timeremote_set_time(struct device *dev, struct rtc_time *tm)
  48. {
  49. int rc;
  50. struct timeremote_set_julian_req {
  51. struct rpc_request_hdr hdr;
  52. uint32_t opt_arg;
  53. struct rpc_time_julian time;
  54. } req;
  55. struct timeremote_set_julian_rep {
  56. struct rpc_reply_hdr hdr;
  57. } rep;
  58. if (tm->tm_year < 1900)
  59. tm->tm_year += 1900;
  60. if (tm->tm_year < 1970)
  61. return -EINVAL;
  62. #if RTC_DEBUG
  63. printk(KERN_DEBUG "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n",
  64. __func__, tm->tm_mon, tm->tm_mday, tm->tm_year,
  65. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
  66. #endif
  67. req.opt_arg = cpu_to_be32(1);
  68. req.time.year = cpu_to_be32(tm->tm_year);
  69. req.time.month = cpu_to_be32(tm->tm_mon + 1);
  70. req.time.day = cpu_to_be32(tm->tm_mday);
  71. req.time.hour = cpu_to_be32(tm->tm_hour);
  72. req.time.minute = cpu_to_be32(tm->tm_min);
  73. req.time.second = cpu_to_be32(tm->tm_sec);
  74. req.time.day_of_week = cpu_to_be32(tm->tm_wday);
  75. rc = msm_rpc_call_reply(ep, TIMEREMOTE_PROCEEDURE_SET_JULIAN,
  76. &req, sizeof(req),
  77. &rep, sizeof(rep),
  78. 5 * HZ);
  79. return rc;
  80. }
  81. static int
  82. msmrtc_timeremote_read_time(struct device *dev, struct rtc_time *tm)
  83. {
  84. int rc;
  85. struct timeremote_get_julian_req {
  86. struct rpc_request_hdr hdr;
  87. uint32_t julian_time_not_null;
  88. } req;
  89. struct timeremote_get_julian_rep {
  90. struct rpc_reply_hdr hdr;
  91. uint32_t opt_arg;
  92. struct rpc_time_julian time;
  93. } rep;
  94. req.julian_time_not_null = cpu_to_be32(1);
  95. rc = msm_rpc_call_reply(ep, TIMEREMOTE_PROCEEDURE_GET_JULIAN,
  96. &req, sizeof(req),
  97. &rep, sizeof(rep),
  98. 5 * HZ);
  99. if (rc < 0)
  100. return rc;
  101. if (!be32_to_cpu(rep.opt_arg)) {
  102. printk(KERN_ERR "%s: No data from RTC\n", __func__);
  103. return -ENODATA;
  104. }
  105. tm->tm_year = be32_to_cpu(rep.time.year);
  106. tm->tm_mon = be32_to_cpu(rep.time.month);
  107. tm->tm_mday = be32_to_cpu(rep.time.day);
  108. tm->tm_hour = be32_to_cpu(rep.time.hour);
  109. tm->tm_min = be32_to_cpu(rep.time.minute);
  110. tm->tm_sec = be32_to_cpu(rep.time.second);
  111. tm->tm_wday = be32_to_cpu(rep.time.day_of_week);
  112. #if RTC_DEBUG
  113. printk(KERN_DEBUG "%s: %.2u/%.2u/%.4u %.2u:%.2u:%.2u (%.2u)\n",
  114. __func__, tm->tm_mon, tm->tm_mday, tm->tm_year,
  115. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
  116. #endif
  117. tm->tm_year -= 1900; /* RTC layer expects years to start at 1900 */
  118. tm->tm_mon--; /* RTC layer expects mons to be 0 based */
  119. if (rtc_valid_tm(tm) < 0) {
  120. dev_err(dev, "retrieved date/time is not valid.\n");
  121. rtc_time_to_tm(0, tm);
  122. }
  123. return 0;
  124. }
  125. static int
  126. msmrtc_virtual_alarm_set(struct device *dev, struct rtc_wkalrm *a)
  127. {
  128. unsigned long now = get_seconds();
  129. if (!a->enabled) {
  130. rtcalarm_time = 0;
  131. return 0;
  132. } else
  133. rtc_tm_to_time(&a->time, &rtcalarm_time);
  134. if (now > rtcalarm_time) {
  135. printk(KERN_ERR "%s: Attempt to set alarm in the past\n",
  136. __func__);
  137. rtcalarm_time = 0;
  138. return -EINVAL;
  139. }
  140. return 0;
  141. }
  142. static struct rtc_class_ops msm_rtc_ops = {
  143. .read_time = msmrtc_timeremote_read_time,
  144. .set_time = msmrtc_timeremote_set_time,
  145. .set_alarm = msmrtc_virtual_alarm_set,
  146. };
  147. static void
  148. msmrtc_alarmtimer_expired(unsigned long _data)
  149. {
  150. #if RTC_DEBUG
  151. printk(KERN_DEBUG "%s: Generating alarm event (src %lu)\n",
  152. rtc->name, _data);
  153. #endif
  154. rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
  155. rtcalarm_time = 0;
  156. }
  157. static int
  158. msmrtc_probe(struct platform_device *pdev)
  159. {
  160. struct rpcsvr_platform_device *rdev =
  161. container_of(pdev, struct rpcsvr_platform_device, base);
  162. ep = msm_rpc_connect(rdev->prog, rdev->vers, 0);
  163. if (IS_ERR(ep)) {
  164. printk(KERN_ERR "%s: init rpc failed! rc = %ld\n",
  165. __func__, PTR_ERR(ep));
  166. return PTR_ERR(ep);
  167. }
  168. rtc = rtc_device_register("msm_rtc",
  169. &pdev->dev,
  170. &msm_rtc_ops,
  171. THIS_MODULE);
  172. if (IS_ERR(rtc)) {
  173. printk(KERN_ERR "%s: Can't register RTC device (%ld)\n",
  174. pdev->name, PTR_ERR(rtc));
  175. return PTR_ERR(rtc);
  176. }
  177. return 0;
  178. }
  179. static unsigned long msmrtc_get_seconds(void)
  180. {
  181. struct rtc_time tm;
  182. unsigned long now;
  183. msmrtc_timeremote_read_time(NULL, &tm);
  184. rtc_tm_to_time(&tm, &now);
  185. return now;
  186. }
  187. static int
  188. msmrtc_suspend(struct platform_device *dev, pm_message_t state)
  189. {
  190. if (rtcalarm_time) {
  191. unsigned long now = msmrtc_get_seconds();
  192. int diff = rtcalarm_time - now;
  193. if (diff <= 0) {
  194. msmrtc_alarmtimer_expired(1);
  195. msm_pm_set_max_sleep_time(0);
  196. return 0;
  197. }
  198. msm_pm_set_max_sleep_time((int64_t) ((int64_t) diff * NSEC_PER_SEC));
  199. } else
  200. msm_pm_set_max_sleep_time(0);
  201. return 0;
  202. }
  203. static int
  204. msmrtc_resume(struct platform_device *dev)
  205. {
  206. if (rtcalarm_time) {
  207. unsigned long now = msmrtc_get_seconds();
  208. int diff = rtcalarm_time - now;
  209. if (diff <= 0)
  210. msmrtc_alarmtimer_expired(2);
  211. }
  212. return 0;
  213. }
  214. static struct platform_driver msmrtc_driver = {
  215. .probe = msmrtc_probe,
  216. .suspend = msmrtc_suspend,
  217. .resume = msmrtc_resume,
  218. .driver = {
  219. .name = APP_TIMEREMOTE_PDEV_NAME,
  220. .owner = THIS_MODULE,
  221. },
  222. };
  223. static int __init msmrtc_init(void)
  224. {
  225. rtcalarm_time = 0;
  226. return platform_driver_register(&msmrtc_driver);
  227. }
  228. module_init(msmrtc_init);
  229. MODULE_DESCRIPTION("RTC driver for Qualcomm MSM7x00a chipsets");
  230. MODULE_AUTHOR("San Mehat <san@android.com>");
  231. MODULE_LICENSE("GPL");