rtc-s3c.c 21 KB

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