rtc-s3c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /* drivers/rtc/rtc-s3c.c
  2. *
  3. * Copyright (c) 2010 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. *
  6. * Copyright (c) 2004,2006 Simtec Electronics
  7. * Ben Dooks, <ben@simtec.co.uk>
  8. * http://armlinux.simtec.co.uk/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * S3C2410/S3C2440/S3C24XX Internal RTC Driver
  15. */
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/rtc.h>
  23. #include <linux/bcd.h>
  24. #include <linux/clk.h>
  25. #include <linux/log2.h>
  26. #include <linux/slab.h>
  27. #include <linux/of.h>
  28. #include <mach/hardware.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <plat/regs-rtc.h>
  33. enum s3c_cpu_type {
  34. TYPE_S3C2410,
  35. TYPE_S3C64XX,
  36. };
  37. /* I have yet to find an S3C implementation with more than one
  38. * of these rtc blocks in */
  39. static struct resource *s3c_rtc_mem;
  40. static struct clk *rtc_clk;
  41. static void __iomem *s3c_rtc_base;
  42. static int s3c_rtc_alarmno = NO_IRQ;
  43. static int s3c_rtc_tickno = NO_IRQ;
  44. static bool wake_en;
  45. static enum s3c_cpu_type s3c_rtc_cpu_type;
  46. static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
  47. static void s3c_rtc_alarm_clk_enable(bool enable)
  48. {
  49. static DEFINE_SPINLOCK(s3c_rtc_alarm_clk_lock);
  50. static bool alarm_clk_enabled;
  51. unsigned long irq_flags;
  52. spin_lock_irqsave(&s3c_rtc_alarm_clk_lock, irq_flags);
  53. if (enable) {
  54. if (!alarm_clk_enabled) {
  55. clk_enable(rtc_clk);
  56. alarm_clk_enabled = true;
  57. }
  58. } else {
  59. if (alarm_clk_enabled) {
  60. clk_disable(rtc_clk);
  61. alarm_clk_enabled = false;
  62. }
  63. }
  64. spin_unlock_irqrestore(&s3c_rtc_alarm_clk_lock, irq_flags);
  65. }
  66. /* IRQ Handlers */
  67. static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
  68. {
  69. struct rtc_device *rdev = id;
  70. clk_enable(rtc_clk);
  71. rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
  72. if (s3c_rtc_cpu_type == TYPE_S3C64XX)
  73. writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
  74. clk_disable(rtc_clk);
  75. s3c_rtc_alarm_clk_enable(false);
  76. return IRQ_HANDLED;
  77. }
  78. static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
  79. {
  80. struct rtc_device *rdev = id;
  81. clk_enable(rtc_clk);
  82. rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
  83. if (s3c_rtc_cpu_type == TYPE_S3C64XX)
  84. writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
  85. clk_disable(rtc_clk);
  86. return IRQ_HANDLED;
  87. }
  88. /* Update control registers */
  89. static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
  90. {
  91. unsigned int tmp;
  92. pr_debug("%s: aie=%d\n", __func__, enabled);
  93. clk_enable(rtc_clk);
  94. tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
  95. if (enabled)
  96. tmp |= S3C2410_RTCALM_ALMEN;
  97. writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
  98. clk_disable(rtc_clk);
  99. s3c_rtc_alarm_clk_enable(enabled);
  100. return 0;
  101. }
  102. static int s3c_rtc_setfreq(struct device *dev, int freq)
  103. {
  104. struct platform_device *pdev = to_platform_device(dev);
  105. struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
  106. unsigned int tmp = 0;
  107. if (!is_power_of_2(freq))
  108. return -EINVAL;
  109. clk_enable(rtc_clk);
  110. spin_lock_irq(&s3c_rtc_pie_lock);
  111. if (s3c_rtc_cpu_type == TYPE_S3C2410) {
  112. tmp = readb(s3c_rtc_base + S3C2410_TICNT);
  113. tmp &= S3C2410_TICNT_ENABLE;
  114. }
  115. tmp |= (rtc_dev->max_user_freq / freq)-1;
  116. writel(tmp, s3c_rtc_base + S3C2410_TICNT);
  117. spin_unlock_irq(&s3c_rtc_pie_lock);
  118. clk_disable(rtc_clk);
  119. return 0;
  120. }
  121. /* Time read/write */
  122. static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  123. {
  124. unsigned int have_retried = 0;
  125. void __iomem *base = s3c_rtc_base;
  126. clk_enable(rtc_clk);
  127. retry_get_time:
  128. rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
  129. rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
  130. rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
  131. rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
  132. rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
  133. rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
  134. /* the only way to work out wether the system was mid-update
  135. * when we read it is to check the second counter, and if it
  136. * is zero, then we re-try the entire read
  137. */
  138. if (rtc_tm->tm_sec == 0 && !have_retried) {
  139. have_retried = 1;
  140. goto retry_get_time;
  141. }
  142. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  143. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  144. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  145. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  146. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  147. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  148. rtc_tm->tm_year += 100;
  149. pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
  150. 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  151. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  152. rtc_tm->tm_mon -= 1;
  153. clk_disable(rtc_clk);
  154. return rtc_valid_tm(rtc_tm);
  155. }
  156. static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
  157. {
  158. void __iomem *base = s3c_rtc_base;
  159. int year = tm->tm_year - 100;
  160. pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
  161. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  162. tm->tm_hour, tm->tm_min, tm->tm_sec);
  163. /* we get around y2k by simply not supporting it */
  164. if (year < 0 || year >= 100) {
  165. dev_err(dev, "rtc only supports 100 years\n");
  166. return -EINVAL;
  167. }
  168. clk_enable(rtc_clk);
  169. writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
  170. writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
  171. writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
  172. writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
  173. writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
  174. writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
  175. clk_disable(rtc_clk);
  176. return 0;
  177. }
  178. static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  179. {
  180. struct rtc_time *alm_tm = &alrm->time;
  181. void __iomem *base = s3c_rtc_base;
  182. unsigned int alm_en;
  183. clk_enable(rtc_clk);
  184. alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
  185. alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
  186. alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
  187. alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
  188. alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
  189. alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
  190. alm_en = readb(base + S3C2410_RTCALM);
  191. alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
  192. pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
  193. alm_en,
  194. 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  195. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  196. /* decode the alarm enable field */
  197. if (alm_en & S3C2410_RTCALM_SECEN)
  198. alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
  199. else
  200. alm_tm->tm_sec = -1;
  201. if (alm_en & S3C2410_RTCALM_MINEN)
  202. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  203. else
  204. alm_tm->tm_min = -1;
  205. if (alm_en & S3C2410_RTCALM_HOUREN)
  206. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  207. else
  208. alm_tm->tm_hour = -1;
  209. if (alm_en & S3C2410_RTCALM_DAYEN)
  210. alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
  211. else
  212. alm_tm->tm_mday = -1;
  213. if (alm_en & S3C2410_RTCALM_MONEN) {
  214. alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
  215. alm_tm->tm_mon -= 1;
  216. } else {
  217. alm_tm->tm_mon = -1;
  218. }
  219. if (alm_en & S3C2410_RTCALM_YEAREN)
  220. alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
  221. else
  222. alm_tm->tm_year = -1;
  223. clk_disable(rtc_clk);
  224. return 0;
  225. }
  226. static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  227. {
  228. struct rtc_time *tm = &alrm->time;
  229. void __iomem *base = s3c_rtc_base;
  230. unsigned int alrm_en;
  231. clk_enable(rtc_clk);
  232. pr_debug("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
  233. alrm->enabled,
  234. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  235. tm->tm_hour, tm->tm_min, tm->tm_sec);
  236. alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  237. writeb(0x00, base + S3C2410_RTCALM);
  238. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  239. alrm_en |= S3C2410_RTCALM_SECEN;
  240. writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
  241. }
  242. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  243. alrm_en |= S3C2410_RTCALM_MINEN;
  244. writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
  245. }
  246. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  247. alrm_en |= S3C2410_RTCALM_HOUREN;
  248. writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
  249. }
  250. pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
  251. writeb(alrm_en, base + S3C2410_RTCALM);
  252. s3c_rtc_setaie(dev, alrm->enabled);
  253. clk_disable(rtc_clk);
  254. return 0;
  255. }
  256. static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
  257. {
  258. unsigned int ticnt;
  259. clk_enable(rtc_clk);
  260. if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
  261. ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
  262. ticnt &= S3C64XX_RTCCON_TICEN;
  263. } else {
  264. ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
  265. ticnt &= S3C2410_TICNT_ENABLE;
  266. }
  267. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  268. clk_disable(rtc_clk);
  269. return 0;
  270. }
  271. static const struct rtc_class_ops s3c_rtcops = {
  272. .read_time = s3c_rtc_gettime,
  273. .set_time = s3c_rtc_settime,
  274. .read_alarm = s3c_rtc_getalarm,
  275. .set_alarm = s3c_rtc_setalarm,
  276. .proc = s3c_rtc_proc,
  277. .alarm_irq_enable = s3c_rtc_setaie,
  278. };
  279. static void s3c_rtc_enable(struct platform_device *pdev, int en)
  280. {
  281. void __iomem *base = s3c_rtc_base;
  282. unsigned int tmp;
  283. if (s3c_rtc_base == NULL)
  284. return;
  285. clk_enable(rtc_clk);
  286. if (!en) {
  287. tmp = readw(base + S3C2410_RTCCON);
  288. if (s3c_rtc_cpu_type == TYPE_S3C64XX)
  289. tmp &= ~S3C64XX_RTCCON_TICEN;
  290. tmp &= ~S3C2410_RTCCON_RTCEN;
  291. writew(tmp, base + S3C2410_RTCCON);
  292. if (s3c_rtc_cpu_type == TYPE_S3C2410) {
  293. tmp = readb(base + S3C2410_TICNT);
  294. tmp &= ~S3C2410_TICNT_ENABLE;
  295. writeb(tmp, base + S3C2410_TICNT);
  296. }
  297. } else {
  298. /* re-enable the device, and check it is ok */
  299. if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
  300. dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
  301. tmp = readw(base + S3C2410_RTCCON);
  302. writew(tmp | S3C2410_RTCCON_RTCEN,
  303. base + S3C2410_RTCCON);
  304. }
  305. if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
  306. dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
  307. tmp = readw(base + S3C2410_RTCCON);
  308. writew(tmp & ~S3C2410_RTCCON_CNTSEL,
  309. base + S3C2410_RTCCON);
  310. }
  311. if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
  312. dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
  313. tmp = readw(base + S3C2410_RTCCON);
  314. writew(tmp & ~S3C2410_RTCCON_CLKRST,
  315. base + S3C2410_RTCCON);
  316. }
  317. }
  318. clk_disable(rtc_clk);
  319. }
  320. static int __devexit s3c_rtc_remove(struct platform_device *dev)
  321. {
  322. struct rtc_device *rtc = platform_get_drvdata(dev);
  323. free_irq(s3c_rtc_alarmno, rtc);
  324. free_irq(s3c_rtc_tickno, rtc);
  325. platform_set_drvdata(dev, NULL);
  326. rtc_device_unregister(rtc);
  327. s3c_rtc_setaie(&dev->dev, 0);
  328. clk_put(rtc_clk);
  329. rtc_clk = NULL;
  330. iounmap(s3c_rtc_base);
  331. release_resource(s3c_rtc_mem);
  332. kfree(s3c_rtc_mem);
  333. return 0;
  334. }
  335. static int __devinit s3c_rtc_probe(struct platform_device *pdev)
  336. {
  337. struct rtc_device *rtc;
  338. struct rtc_time rtc_tm;
  339. struct resource *res;
  340. int ret;
  341. pr_debug("%s: probe=%p\n", __func__, pdev);
  342. /* find the IRQs */
  343. s3c_rtc_tickno = platform_get_irq(pdev, 1);
  344. if (s3c_rtc_tickno < 0) {
  345. dev_err(&pdev->dev, "no irq for rtc tick\n");
  346. return -ENOENT;
  347. }
  348. s3c_rtc_alarmno = platform_get_irq(pdev, 0);
  349. if (s3c_rtc_alarmno < 0) {
  350. dev_err(&pdev->dev, "no irq for alarm\n");
  351. return -ENOENT;
  352. }
  353. pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
  354. s3c_rtc_tickno, s3c_rtc_alarmno);
  355. /* get the memory region */
  356. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  357. if (res == NULL) {
  358. dev_err(&pdev->dev, "failed to get memory region resource\n");
  359. return -ENOENT;
  360. }
  361. s3c_rtc_mem = request_mem_region(res->start, resource_size(res),
  362. pdev->name);
  363. if (s3c_rtc_mem == NULL) {
  364. dev_err(&pdev->dev, "failed to reserve memory region\n");
  365. ret = -ENOENT;
  366. goto err_nores;
  367. }
  368. s3c_rtc_base = ioremap(res->start, resource_size(res));
  369. if (s3c_rtc_base == NULL) {
  370. dev_err(&pdev->dev, "failed ioremap()\n");
  371. ret = -EINVAL;
  372. goto err_nomap;
  373. }
  374. rtc_clk = clk_get(&pdev->dev, "rtc");
  375. if (IS_ERR(rtc_clk)) {
  376. dev_err(&pdev->dev, "failed to find rtc clock source\n");
  377. ret = PTR_ERR(rtc_clk);
  378. rtc_clk = NULL;
  379. goto err_clk;
  380. }
  381. clk_enable(rtc_clk);
  382. /* check to see if everything is setup correctly */
  383. s3c_rtc_enable(pdev, 1);
  384. pr_debug("s3c2410_rtc: RTCCON=%02x\n",
  385. readw(s3c_rtc_base + S3C2410_RTCCON));
  386. device_init_wakeup(&pdev->dev, 1);
  387. /* register RTC and exit */
  388. rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
  389. THIS_MODULE);
  390. if (IS_ERR(rtc)) {
  391. dev_err(&pdev->dev, "cannot attach rtc\n");
  392. ret = PTR_ERR(rtc);
  393. goto err_nortc;
  394. }
  395. #ifdef CONFIG_OF
  396. if (pdev->dev.of_node)
  397. s3c_rtc_cpu_type = of_device_is_compatible(pdev->dev.of_node,
  398. "samsung,s3c6410-rtc") ? TYPE_S3C64XX : TYPE_S3C2410;
  399. else
  400. #endif
  401. s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
  402. /* Check RTC Time */
  403. s3c_rtc_gettime(NULL, &rtc_tm);
  404. if (rtc_valid_tm(&rtc_tm)) {
  405. rtc_tm.tm_year = 100;
  406. rtc_tm.tm_mon = 0;
  407. rtc_tm.tm_mday = 1;
  408. rtc_tm.tm_hour = 0;
  409. rtc_tm.tm_min = 0;
  410. rtc_tm.tm_sec = 0;
  411. s3c_rtc_settime(NULL, &rtc_tm);
  412. dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
  413. }
  414. if (s3c_rtc_cpu_type == TYPE_S3C64XX)
  415. rtc->max_user_freq = 32768;
  416. else
  417. rtc->max_user_freq = 128;
  418. platform_set_drvdata(pdev, rtc);
  419. s3c_rtc_setfreq(&pdev->dev, 1);
  420. ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
  421. 0, "s3c2410-rtc alarm", rtc);
  422. if (ret) {
  423. dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
  424. goto err_alarm_irq;
  425. }
  426. ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
  427. 0, "s3c2410-rtc tick", rtc);
  428. if (ret) {
  429. dev_err(&pdev->dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
  430. free_irq(s3c_rtc_alarmno, rtc);
  431. goto err_tick_irq;
  432. }
  433. clk_disable(rtc_clk);
  434. return 0;
  435. err_tick_irq:
  436. free_irq(s3c_rtc_alarmno, rtc);
  437. err_alarm_irq:
  438. platform_set_drvdata(pdev, NULL);
  439. rtc_device_unregister(rtc);
  440. err_nortc:
  441. s3c_rtc_enable(pdev, 0);
  442. clk_disable(rtc_clk);
  443. clk_put(rtc_clk);
  444. err_clk:
  445. iounmap(s3c_rtc_base);
  446. err_nomap:
  447. release_resource(s3c_rtc_mem);
  448. err_nores:
  449. return ret;
  450. }
  451. #ifdef CONFIG_PM
  452. /* RTC Power management control */
  453. static int ticnt_save, ticnt_en_save;
  454. static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
  455. {
  456. clk_enable(rtc_clk);
  457. /* save TICNT for anyone using periodic interrupts */
  458. ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
  459. if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
  460. ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
  461. ticnt_en_save &= S3C64XX_RTCCON_TICEN;
  462. }
  463. s3c_rtc_enable(pdev, 0);
  464. if (device_may_wakeup(&pdev->dev) && !wake_en) {
  465. if (enable_irq_wake(s3c_rtc_alarmno) == 0)
  466. wake_en = true;
  467. else
  468. dev_err(&pdev->dev, "enable_irq_wake failed\n");
  469. }
  470. clk_disable(rtc_clk);
  471. return 0;
  472. }
  473. static int s3c_rtc_resume(struct platform_device *pdev)
  474. {
  475. unsigned int tmp;
  476. clk_enable(rtc_clk);
  477. s3c_rtc_enable(pdev, 1);
  478. writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
  479. if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
  480. tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
  481. writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
  482. }
  483. if (device_may_wakeup(&pdev->dev) && wake_en) {
  484. disable_irq_wake(s3c_rtc_alarmno);
  485. wake_en = false;
  486. }
  487. clk_disable(rtc_clk);
  488. return 0;
  489. }
  490. #else
  491. #define s3c_rtc_suspend NULL
  492. #define s3c_rtc_resume NULL
  493. #endif
  494. #ifdef CONFIG_OF
  495. static const struct of_device_id s3c_rtc_dt_match[] = {
  496. { .compatible = "samsung,s3c2410-rtc" },
  497. { .compatible = "samsung,s3c6410-rtc" },
  498. {},
  499. };
  500. MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
  501. #else
  502. #define s3c_rtc_dt_match NULL
  503. #endif
  504. static struct platform_device_id s3c_rtc_driver_ids[] = {
  505. {
  506. .name = "s3c2410-rtc",
  507. .driver_data = TYPE_S3C2410,
  508. }, {
  509. .name = "s3c64xx-rtc",
  510. .driver_data = TYPE_S3C64XX,
  511. },
  512. { }
  513. };
  514. MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
  515. static struct platform_driver s3c_rtc_driver = {
  516. .probe = s3c_rtc_probe,
  517. .remove = __devexit_p(s3c_rtc_remove),
  518. .suspend = s3c_rtc_suspend,
  519. .resume = s3c_rtc_resume,
  520. .id_table = s3c_rtc_driver_ids,
  521. .driver = {
  522. .name = "s3c-rtc",
  523. .owner = THIS_MODULE,
  524. .of_match_table = s3c_rtc_dt_match,
  525. },
  526. };
  527. module_platform_driver(s3c_rtc_driver);
  528. MODULE_DESCRIPTION("Samsung S3C RTC Driver");
  529. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  530. MODULE_LICENSE("GPL");
  531. MODULE_ALIAS("platform:s3c2410-rtc");