rtc-s3c.c 21 KB

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