rtc-s3c.c 22 KB

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