rtc-s3c.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 <linux/uaccess.h>
  29. #include <linux/io.h>
  30. #include <asm/irq.h>
  31. #include "rtc-s3c.h"
  32. struct s3c_rtc {
  33. struct device *dev;
  34. struct rtc_device *rtc;
  35. void __iomem *base;
  36. struct clk *rtc_clk;
  37. struct clk *rtc_src_clk;
  38. struct s3c_rtc_data *data;
  39. int irq_alarm;
  40. int irq_tick;
  41. spinlock_t pie_lock;
  42. spinlock_t alarm_clk_lock;
  43. int ticnt_save, ticnt_en_save;
  44. bool wake_en;
  45. };
  46. struct s3c_rtc_data {
  47. int max_user_freq;
  48. bool needs_src_clk;
  49. void (*irq_handler) (struct s3c_rtc *info, int mask);
  50. void (*set_freq) (struct s3c_rtc *info, int freq);
  51. void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
  52. void (*select_tick_clk) (struct s3c_rtc *info);
  53. void (*save_tick_cnt) (struct s3c_rtc *info);
  54. void (*restore_tick_cnt) (struct s3c_rtc *info);
  55. void (*enable) (struct s3c_rtc *info);
  56. void (*disable) (struct s3c_rtc *info);
  57. };
  58. static void s3c_rtc_enable_clk(struct s3c_rtc *info)
  59. {
  60. unsigned long irq_flags;
  61. spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
  62. clk_enable(info->rtc_clk);
  63. if (info->data->needs_src_clk)
  64. clk_enable(info->rtc_src_clk);
  65. spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
  66. }
  67. static void s3c_rtc_disable_clk(struct s3c_rtc *info)
  68. {
  69. unsigned long irq_flags;
  70. spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
  71. if (info->data->needs_src_clk)
  72. clk_disable(info->rtc_src_clk);
  73. clk_disable(info->rtc_clk);
  74. spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
  75. }
  76. /* IRQ Handlers */
  77. static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
  78. {
  79. struct s3c_rtc *info = (struct s3c_rtc *)id;
  80. if (info->data->irq_handler)
  81. info->data->irq_handler(info, S3C2410_INTP_TIC);
  82. return IRQ_HANDLED;
  83. }
  84. static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
  85. {
  86. struct s3c_rtc *info = (struct s3c_rtc *)id;
  87. if (info->data->irq_handler)
  88. info->data->irq_handler(info, S3C2410_INTP_ALM);
  89. return IRQ_HANDLED;
  90. }
  91. /* Update control registers */
  92. static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
  93. {
  94. struct s3c_rtc *info = dev_get_drvdata(dev);
  95. unsigned int tmp;
  96. dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
  97. s3c_rtc_enable_clk(info);
  98. tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
  99. if (enabled)
  100. tmp |= S3C2410_RTCALM_ALMEN;
  101. writeb(tmp, info->base + S3C2410_RTCALM);
  102. s3c_rtc_disable_clk(info);
  103. return 0;
  104. }
  105. /* Set RTC frequency */
  106. static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
  107. {
  108. if (!is_power_of_2(freq))
  109. return -EINVAL;
  110. spin_lock_irq(&info->pie_lock);
  111. if (info->data->set_freq)
  112. info->data->set_freq(info, freq);
  113. spin_unlock_irq(&info->pie_lock);
  114. return 0;
  115. }
  116. /* Time read/write */
  117. static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  118. {
  119. struct s3c_rtc *info = dev_get_drvdata(dev);
  120. unsigned int have_retried = 0;
  121. s3c_rtc_enable_clk(info);
  122. retry_get_time:
  123. rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
  124. rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
  125. rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
  126. rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
  127. rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
  128. rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
  129. /* the only way to work out whether the system was mid-update
  130. * when we read it is to check the second counter, and if it
  131. * is zero, then we re-try the entire read
  132. */
  133. if (rtc_tm->tm_sec == 0 && !have_retried) {
  134. have_retried = 1;
  135. goto retry_get_time;
  136. }
  137. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  138. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  139. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  140. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  141. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
  142. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  143. s3c_rtc_disable_clk(info);
  144. rtc_tm->tm_year += 100;
  145. dev_dbg(dev, "read time %04d.%02d.%02d %02d:%02d:%02d\n",
  146. 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  147. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  148. rtc_tm->tm_mon -= 1;
  149. return rtc_valid_tm(rtc_tm);
  150. }
  151. static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
  152. {
  153. struct s3c_rtc *info = dev_get_drvdata(dev);
  154. int year = tm->tm_year - 100;
  155. dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
  156. 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
  157. tm->tm_hour, tm->tm_min, tm->tm_sec);
  158. /* we get around y2k by simply not supporting it */
  159. if (year < 0 || year >= 100) {
  160. dev_err(dev, "rtc only supports 100 years\n");
  161. return -EINVAL;
  162. }
  163. s3c_rtc_enable_clk(info);
  164. writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
  165. writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
  166. writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
  167. writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
  168. writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
  169. writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
  170. s3c_rtc_disable_clk(info);
  171. return 0;
  172. }
  173. static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  174. {
  175. struct s3c_rtc *info = dev_get_drvdata(dev);
  176. struct rtc_time *alm_tm = &alrm->time;
  177. unsigned int alm_en;
  178. s3c_rtc_enable_clk(info);
  179. alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
  180. alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
  181. alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
  182. alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
  183. alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
  184. alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
  185. alm_en = readb(info->base + S3C2410_RTCALM);
  186. s3c_rtc_disable_clk(info);
  187. alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
  188. dev_dbg(dev, "read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
  189. alm_en,
  190. 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  191. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  192. /* decode the alarm enable field */
  193. if (alm_en & S3C2410_RTCALM_SECEN)
  194. alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
  195. else
  196. alm_tm->tm_sec = -1;
  197. if (alm_en & S3C2410_RTCALM_MINEN)
  198. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  199. else
  200. alm_tm->tm_min = -1;
  201. if (alm_en & S3C2410_RTCALM_HOUREN)
  202. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  203. else
  204. alm_tm->tm_hour = -1;
  205. if (alm_en & S3C2410_RTCALM_DAYEN)
  206. alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
  207. else
  208. alm_tm->tm_mday = -1;
  209. if (alm_en & S3C2410_RTCALM_MONEN) {
  210. alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
  211. alm_tm->tm_mon -= 1;
  212. } else {
  213. alm_tm->tm_mon = -1;
  214. }
  215. if (alm_en & S3C2410_RTCALM_YEAREN)
  216. alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
  217. else
  218. alm_tm->tm_year = -1;
  219. return 0;
  220. }
  221. static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  222. {
  223. struct s3c_rtc *info = dev_get_drvdata(dev);
  224. struct rtc_time *tm = &alrm->time;
  225. unsigned int alrm_en;
  226. dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
  227. alrm->enabled,
  228. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  229. tm->tm_hour, tm->tm_min, tm->tm_sec);
  230. s3c_rtc_enable_clk(info);
  231. alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  232. writeb(0x00, info->base + S3C2410_RTCALM);
  233. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  234. alrm_en |= S3C2410_RTCALM_SECEN;
  235. writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
  236. }
  237. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  238. alrm_en |= S3C2410_RTCALM_MINEN;
  239. writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
  240. }
  241. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  242. alrm_en |= S3C2410_RTCALM_HOUREN;
  243. writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
  244. }
  245. dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
  246. writeb(alrm_en, info->base + S3C2410_RTCALM);
  247. s3c_rtc_disable_clk(info);
  248. s3c_rtc_setaie(dev, alrm->enabled);
  249. return 0;
  250. }
  251. static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
  252. {
  253. struct s3c_rtc *info = dev_get_drvdata(dev);
  254. s3c_rtc_enable_clk(info);
  255. if (info->data->enable_tick)
  256. info->data->enable_tick(info, seq);
  257. s3c_rtc_disable_clk(info);
  258. return 0;
  259. }
  260. static const struct rtc_class_ops s3c_rtcops = {
  261. .read_time = s3c_rtc_gettime,
  262. .set_time = s3c_rtc_settime,
  263. .read_alarm = s3c_rtc_getalarm,
  264. .set_alarm = s3c_rtc_setalarm,
  265. .proc = s3c_rtc_proc,
  266. .alarm_irq_enable = s3c_rtc_setaie,
  267. };
  268. static void s3c24xx_rtc_enable(struct s3c_rtc *info)
  269. {
  270. unsigned int con, tmp;
  271. con = readw(info->base + S3C2410_RTCCON);
  272. /* re-enable the device, and check it is ok */
  273. if ((con & S3C2410_RTCCON_RTCEN) == 0) {
  274. dev_info(info->dev, "rtc disabled, re-enabling\n");
  275. tmp = readw(info->base + S3C2410_RTCCON);
  276. writew(tmp | S3C2410_RTCCON_RTCEN,
  277. info->base + S3C2410_RTCCON);
  278. }
  279. if (con & S3C2410_RTCCON_CNTSEL) {
  280. dev_info(info->dev, "removing RTCCON_CNTSEL\n");
  281. tmp = readw(info->base + S3C2410_RTCCON);
  282. writew(tmp & ~S3C2410_RTCCON_CNTSEL,
  283. info->base + S3C2410_RTCCON);
  284. }
  285. if (con & S3C2410_RTCCON_CLKRST) {
  286. dev_info(info->dev, "removing RTCCON_CLKRST\n");
  287. tmp = readw(info->base + S3C2410_RTCCON);
  288. writew(tmp & ~S3C2410_RTCCON_CLKRST,
  289. info->base + S3C2410_RTCCON);
  290. }
  291. }
  292. static void s3c24xx_rtc_disable(struct s3c_rtc *info)
  293. {
  294. unsigned int con;
  295. con = readw(info->base + S3C2410_RTCCON);
  296. con &= ~S3C2410_RTCCON_RTCEN;
  297. writew(con, info->base + S3C2410_RTCCON);
  298. con = readb(info->base + S3C2410_TICNT);
  299. con &= ~S3C2410_TICNT_ENABLE;
  300. writeb(con, info->base + S3C2410_TICNT);
  301. }
  302. static void s3c6410_rtc_disable(struct s3c_rtc *info)
  303. {
  304. unsigned int con;
  305. con = readw(info->base + S3C2410_RTCCON);
  306. con &= ~S3C64XX_RTCCON_TICEN;
  307. con &= ~S3C2410_RTCCON_RTCEN;
  308. writew(con, info->base + S3C2410_RTCCON);
  309. }
  310. static int s3c_rtc_remove(struct platform_device *pdev)
  311. {
  312. struct s3c_rtc *info = platform_get_drvdata(pdev);
  313. s3c_rtc_setaie(info->dev, 0);
  314. clk_unprepare(info->rtc_clk);
  315. info->rtc_clk = NULL;
  316. return 0;
  317. }
  318. static const struct of_device_id s3c_rtc_dt_match[];
  319. static struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
  320. {
  321. const struct of_device_id *match;
  322. match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
  323. return (struct s3c_rtc_data *)match->data;
  324. }
  325. static int s3c_rtc_probe(struct platform_device *pdev)
  326. {
  327. struct s3c_rtc *info = NULL;
  328. struct rtc_time rtc_tm;
  329. struct resource *res;
  330. int ret;
  331. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  332. if (!info)
  333. return -ENOMEM;
  334. /* find the IRQs */
  335. info->irq_tick = platform_get_irq(pdev, 1);
  336. if (info->irq_tick < 0) {
  337. dev_err(&pdev->dev, "no irq for rtc tick\n");
  338. return info->irq_tick;
  339. }
  340. info->dev = &pdev->dev;
  341. info->data = s3c_rtc_get_data(pdev);
  342. if (!info->data) {
  343. dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
  344. return -EINVAL;
  345. }
  346. spin_lock_init(&info->pie_lock);
  347. spin_lock_init(&info->alarm_clk_lock);
  348. platform_set_drvdata(pdev, info);
  349. info->irq_alarm = platform_get_irq(pdev, 0);
  350. if (info->irq_alarm < 0) {
  351. dev_err(&pdev->dev, "no irq for alarm\n");
  352. return info->irq_alarm;
  353. }
  354. dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
  355. info->irq_tick, info->irq_alarm);
  356. /* get the memory region */
  357. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  358. info->base = devm_ioremap_resource(&pdev->dev, res);
  359. if (IS_ERR(info->base))
  360. return PTR_ERR(info->base);
  361. info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
  362. if (IS_ERR(info->rtc_clk)) {
  363. dev_err(&pdev->dev, "failed to find rtc clock\n");
  364. return PTR_ERR(info->rtc_clk);
  365. }
  366. clk_prepare_enable(info->rtc_clk);
  367. if (info->data->needs_src_clk) {
  368. info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
  369. if (IS_ERR(info->rtc_src_clk)) {
  370. dev_err(&pdev->dev,
  371. "failed to find rtc source clock\n");
  372. return PTR_ERR(info->rtc_src_clk);
  373. }
  374. clk_prepare_enable(info->rtc_src_clk);
  375. }
  376. /* check to see if everything is setup correctly */
  377. if (info->data->enable)
  378. info->data->enable(info);
  379. dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
  380. readw(info->base + S3C2410_RTCCON));
  381. device_init_wakeup(&pdev->dev, 1);
  382. /* Check RTC Time */
  383. if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
  384. rtc_tm.tm_year = 100;
  385. rtc_tm.tm_mon = 0;
  386. rtc_tm.tm_mday = 1;
  387. rtc_tm.tm_hour = 0;
  388. rtc_tm.tm_min = 0;
  389. rtc_tm.tm_sec = 0;
  390. s3c_rtc_settime(&pdev->dev, &rtc_tm);
  391. dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
  392. }
  393. /* register RTC and exit */
  394. info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
  395. THIS_MODULE);
  396. if (IS_ERR(info->rtc)) {
  397. dev_err(&pdev->dev, "cannot attach rtc\n");
  398. ret = PTR_ERR(info->rtc);
  399. goto err_nortc;
  400. }
  401. ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
  402. 0, "s3c2410-rtc alarm", info);
  403. if (ret) {
  404. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
  405. goto err_nortc;
  406. }
  407. ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
  408. 0, "s3c2410-rtc tick", info);
  409. if (ret) {
  410. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
  411. goto err_nortc;
  412. }
  413. if (info->data->select_tick_clk)
  414. info->data->select_tick_clk(info);
  415. s3c_rtc_setfreq(info, 1);
  416. s3c_rtc_disable_clk(info);
  417. return 0;
  418. err_nortc:
  419. if (info->data->disable)
  420. info->data->disable(info);
  421. if (info->data->needs_src_clk)
  422. clk_disable_unprepare(info->rtc_src_clk);
  423. clk_disable_unprepare(info->rtc_clk);
  424. return ret;
  425. }
  426. #ifdef CONFIG_PM_SLEEP
  427. static int s3c_rtc_suspend(struct device *dev)
  428. {
  429. struct s3c_rtc *info = dev_get_drvdata(dev);
  430. s3c_rtc_enable_clk(info);
  431. /* save TICNT for anyone using periodic interrupts */
  432. if (info->data->save_tick_cnt)
  433. info->data->save_tick_cnt(info);
  434. if (info->data->disable)
  435. info->data->disable(info);
  436. if (device_may_wakeup(dev) && !info->wake_en) {
  437. if (enable_irq_wake(info->irq_alarm) == 0)
  438. info->wake_en = true;
  439. else
  440. dev_err(dev, "enable_irq_wake failed\n");
  441. }
  442. return 0;
  443. }
  444. static int s3c_rtc_resume(struct device *dev)
  445. {
  446. struct s3c_rtc *info = dev_get_drvdata(dev);
  447. if (info->data->enable)
  448. info->data->enable(info);
  449. if (info->data->restore_tick_cnt)
  450. info->data->restore_tick_cnt(info);
  451. s3c_rtc_disable_clk(info);
  452. if (device_may_wakeup(dev) && info->wake_en) {
  453. disable_irq_wake(info->irq_alarm);
  454. info->wake_en = false;
  455. }
  456. return 0;
  457. }
  458. #endif
  459. static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
  460. static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
  461. {
  462. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  463. }
  464. static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
  465. {
  466. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  467. writeb(mask, info->base + S3C2410_INTP);
  468. }
  469. static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
  470. {
  471. unsigned int tmp = 0;
  472. int val;
  473. tmp = readb(info->base + S3C2410_TICNT);
  474. tmp &= S3C2410_TICNT_ENABLE;
  475. val = (info->rtc->max_user_freq / freq) - 1;
  476. tmp |= val;
  477. writel(tmp, info->base + S3C2410_TICNT);
  478. }
  479. static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
  480. {
  481. unsigned int tmp = 0;
  482. int val;
  483. tmp = readb(info->base + S3C2410_TICNT);
  484. tmp &= S3C2410_TICNT_ENABLE;
  485. val = (info->rtc->max_user_freq / freq) - 1;
  486. tmp |= S3C2443_TICNT_PART(val);
  487. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  488. writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
  489. writel(tmp, info->base + S3C2410_TICNT);
  490. }
  491. static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
  492. {
  493. unsigned int tmp = 0;
  494. int val;
  495. tmp = readb(info->base + S3C2410_TICNT);
  496. tmp &= S3C2410_TICNT_ENABLE;
  497. val = (info->rtc->max_user_freq / freq) - 1;
  498. tmp |= S3C2443_TICNT_PART(val);
  499. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  500. writel(tmp, info->base + S3C2410_TICNT);
  501. }
  502. static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
  503. {
  504. int val;
  505. val = (info->rtc->max_user_freq / freq) - 1;
  506. writel(val, info->base + S3C2410_TICNT);
  507. }
  508. static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  509. {
  510. unsigned int ticnt;
  511. ticnt = readb(info->base + S3C2410_TICNT);
  512. ticnt &= S3C2410_TICNT_ENABLE;
  513. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  514. }
  515. static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
  516. {
  517. unsigned int con;
  518. con = readw(info->base + S3C2410_RTCCON);
  519. con |= S3C2443_RTCCON_TICSEL;
  520. writew(con, info->base + S3C2410_RTCCON);
  521. }
  522. static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  523. {
  524. unsigned int ticnt;
  525. ticnt = readw(info->base + S3C2410_RTCCON);
  526. ticnt &= S3C64XX_RTCCON_TICEN;
  527. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  528. }
  529. static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
  530. {
  531. info->ticnt_save = readb(info->base + S3C2410_TICNT);
  532. }
  533. static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
  534. {
  535. writeb(info->ticnt_save, info->base + S3C2410_TICNT);
  536. }
  537. static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
  538. {
  539. info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
  540. info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
  541. info->ticnt_save = readl(info->base + S3C2410_TICNT);
  542. }
  543. static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
  544. {
  545. unsigned int con;
  546. writel(info->ticnt_save, info->base + S3C2410_TICNT);
  547. if (info->ticnt_en_save) {
  548. con = readw(info->base + S3C2410_RTCCON);
  549. writew(con | info->ticnt_en_save,
  550. info->base + S3C2410_RTCCON);
  551. }
  552. }
  553. static struct s3c_rtc_data const s3c2410_rtc_data = {
  554. .max_user_freq = 128,
  555. .irq_handler = s3c24xx_rtc_irq,
  556. .set_freq = s3c2410_rtc_setfreq,
  557. .enable_tick = s3c24xx_rtc_enable_tick,
  558. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  559. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  560. .enable = s3c24xx_rtc_enable,
  561. .disable = s3c24xx_rtc_disable,
  562. };
  563. static struct s3c_rtc_data const s3c2416_rtc_data = {
  564. .max_user_freq = 32768,
  565. .irq_handler = s3c24xx_rtc_irq,
  566. .set_freq = s3c2416_rtc_setfreq,
  567. .enable_tick = s3c24xx_rtc_enable_tick,
  568. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  569. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  570. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  571. .enable = s3c24xx_rtc_enable,
  572. .disable = s3c24xx_rtc_disable,
  573. };
  574. static struct s3c_rtc_data const s3c2443_rtc_data = {
  575. .max_user_freq = 32768,
  576. .irq_handler = s3c24xx_rtc_irq,
  577. .set_freq = s3c2443_rtc_setfreq,
  578. .enable_tick = s3c24xx_rtc_enable_tick,
  579. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  580. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  581. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  582. .enable = s3c24xx_rtc_enable,
  583. .disable = s3c24xx_rtc_disable,
  584. };
  585. static struct s3c_rtc_data const s3c6410_rtc_data = {
  586. .max_user_freq = 32768,
  587. .needs_src_clk = true,
  588. .irq_handler = s3c6410_rtc_irq,
  589. .set_freq = s3c6410_rtc_setfreq,
  590. .enable_tick = s3c6410_rtc_enable_tick,
  591. .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
  592. .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
  593. .enable = s3c24xx_rtc_enable,
  594. .disable = s3c6410_rtc_disable,
  595. };
  596. static const struct of_device_id s3c_rtc_dt_match[] = {
  597. {
  598. .compatible = "samsung,s3c2410-rtc",
  599. .data = (void *)&s3c2410_rtc_data,
  600. }, {
  601. .compatible = "samsung,s3c2416-rtc",
  602. .data = (void *)&s3c2416_rtc_data,
  603. }, {
  604. .compatible = "samsung,s3c2443-rtc",
  605. .data = (void *)&s3c2443_rtc_data,
  606. }, {
  607. .compatible = "samsung,s3c6410-rtc",
  608. .data = (void *)&s3c6410_rtc_data,
  609. }, {
  610. .compatible = "samsung,exynos3250-rtc",
  611. .data = (void *)&s3c6410_rtc_data,
  612. },
  613. { /* sentinel */ },
  614. };
  615. MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
  616. static struct platform_driver s3c_rtc_driver = {
  617. .probe = s3c_rtc_probe,
  618. .remove = s3c_rtc_remove,
  619. .driver = {
  620. .name = "s3c-rtc",
  621. .pm = &s3c_rtc_pm_ops,
  622. .of_match_table = of_match_ptr(s3c_rtc_dt_match),
  623. },
  624. };
  625. module_platform_driver(s3c_rtc_driver);
  626. MODULE_DESCRIPTION("Samsung S3C RTC Driver");
  627. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  628. MODULE_LICENSE("GPL");
  629. MODULE_ALIAS("platform:s3c2410-rtc");