rtc-s3c.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. if (info->data->needs_src_clk) {
  421. info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
  422. if (IS_ERR(info->rtc_src_clk)) {
  423. dev_err(&pdev->dev,
  424. "failed to find rtc source clock\n");
  425. return PTR_ERR(info->rtc_src_clk);
  426. }
  427. clk_prepare_enable(info->rtc_src_clk);
  428. }
  429. /* check to see if everything is setup correctly */
  430. if (info->data->enable)
  431. info->data->enable(info);
  432. dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
  433. readw(info->base + S3C2410_RTCCON));
  434. device_init_wakeup(&pdev->dev, 1);
  435. /* register RTC and exit */
  436. info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
  437. THIS_MODULE);
  438. if (IS_ERR(info->rtc)) {
  439. dev_err(&pdev->dev, "cannot attach rtc\n");
  440. ret = PTR_ERR(info->rtc);
  441. goto err_nortc;
  442. }
  443. ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
  444. 0, "s3c2410-rtc alarm", info);
  445. if (ret) {
  446. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
  447. goto err_nortc;
  448. }
  449. ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
  450. 0, "s3c2410-rtc tick", info);
  451. if (ret) {
  452. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
  453. goto err_nortc;
  454. }
  455. /* Check RTC Time */
  456. s3c_rtc_gettime(&pdev->dev, &rtc_tm);
  457. if (rtc_valid_tm(&rtc_tm)) {
  458. rtc_tm.tm_year = 100;
  459. rtc_tm.tm_mon = 0;
  460. rtc_tm.tm_mday = 1;
  461. rtc_tm.tm_hour = 0;
  462. rtc_tm.tm_min = 0;
  463. rtc_tm.tm_sec = 0;
  464. s3c_rtc_settime(&pdev->dev, &rtc_tm);
  465. dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
  466. }
  467. if (info->data->select_tick_clk)
  468. info->data->select_tick_clk(info);
  469. s3c_rtc_setfreq(info, 1);
  470. if (info->data->needs_src_clk)
  471. clk_disable(info->rtc_src_clk);
  472. clk_disable(info->rtc_clk);
  473. return 0;
  474. err_nortc:
  475. if (info->data->disable)
  476. info->data->disable(info);
  477. clk_disable_unprepare(info->rtc_clk);
  478. return ret;
  479. }
  480. #ifdef CONFIG_PM_SLEEP
  481. static int s3c_rtc_suspend(struct device *dev)
  482. {
  483. struct s3c_rtc *info = dev_get_drvdata(dev);
  484. clk_enable(info->rtc_clk);
  485. if (info->data->needs_src_clk)
  486. clk_enable(info->rtc_src_clk);
  487. /* save TICNT for anyone using periodic interrupts */
  488. if (info->data->save_tick_cnt)
  489. info->data->save_tick_cnt(info);
  490. if (info->data->disable)
  491. info->data->disable(info);
  492. if (device_may_wakeup(dev) && !info->wake_en) {
  493. if (enable_irq_wake(info->irq_alarm) == 0)
  494. info->wake_en = true;
  495. else
  496. dev_err(dev, "enable_irq_wake failed\n");
  497. }
  498. if (info->data->needs_src_clk)
  499. clk_disable(info->rtc_src_clk);
  500. clk_disable(info->rtc_clk);
  501. return 0;
  502. }
  503. static int s3c_rtc_resume(struct device *dev)
  504. {
  505. struct s3c_rtc *info = dev_get_drvdata(dev);
  506. clk_enable(info->rtc_clk);
  507. if (info->data->needs_src_clk)
  508. clk_enable(info->rtc_src_clk);
  509. if (info->data->enable)
  510. info->data->enable(info);
  511. if (info->data->restore_tick_cnt)
  512. info->data->restore_tick_cnt(info);
  513. if (device_may_wakeup(dev) && info->wake_en) {
  514. disable_irq_wake(info->irq_alarm);
  515. info->wake_en = false;
  516. }
  517. if (info->data->needs_src_clk)
  518. clk_disable(info->rtc_src_clk);
  519. clk_disable(info->rtc_clk);
  520. return 0;
  521. }
  522. #endif
  523. static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
  524. static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
  525. {
  526. clk_enable(info->rtc_clk);
  527. if (info->data->needs_src_clk)
  528. clk_enable(info->rtc_src_clk);
  529. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  530. if (info->data->needs_src_clk)
  531. clk_disable(info->rtc_src_clk);
  532. clk_disable(info->rtc_clk);
  533. s3c_rtc_alarm_clk_enable(info, false);
  534. }
  535. static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
  536. {
  537. clk_enable(info->rtc_clk);
  538. if (info->data->needs_src_clk)
  539. clk_enable(info->rtc_src_clk);
  540. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  541. writeb(mask, info->base + S3C2410_INTP);
  542. if (info->data->needs_src_clk)
  543. clk_disable(info->rtc_src_clk);
  544. clk_disable(info->rtc_clk);
  545. s3c_rtc_alarm_clk_enable(info, false);
  546. }
  547. static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
  548. {
  549. unsigned int tmp = 0;
  550. int val;
  551. tmp = readb(info->base + S3C2410_TICNT);
  552. tmp &= S3C2410_TICNT_ENABLE;
  553. val = (info->rtc->max_user_freq / freq) - 1;
  554. tmp |= val;
  555. writel(tmp, info->base + S3C2410_TICNT);
  556. }
  557. static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
  558. {
  559. unsigned int tmp = 0;
  560. int val;
  561. tmp = readb(info->base + S3C2410_TICNT);
  562. tmp &= S3C2410_TICNT_ENABLE;
  563. val = (info->rtc->max_user_freq / freq) - 1;
  564. tmp |= S3C2443_TICNT_PART(val);
  565. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  566. writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
  567. writel(tmp, info->base + S3C2410_TICNT);
  568. }
  569. static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
  570. {
  571. unsigned int tmp = 0;
  572. int val;
  573. tmp = readb(info->base + S3C2410_TICNT);
  574. tmp &= S3C2410_TICNT_ENABLE;
  575. val = (info->rtc->max_user_freq / freq) - 1;
  576. tmp |= S3C2443_TICNT_PART(val);
  577. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  578. writel(tmp, info->base + S3C2410_TICNT);
  579. }
  580. static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
  581. {
  582. int val;
  583. val = (info->rtc->max_user_freq / freq) - 1;
  584. writel(val, info->base + S3C2410_TICNT);
  585. }
  586. static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  587. {
  588. unsigned int ticnt;
  589. ticnt = readb(info->base + S3C2410_TICNT);
  590. ticnt &= S3C2410_TICNT_ENABLE;
  591. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  592. }
  593. static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
  594. {
  595. unsigned int con;
  596. con = readw(info->base + S3C2410_RTCCON);
  597. con |= S3C2443_RTCCON_TICSEL;
  598. writew(con, info->base + S3C2410_RTCCON);
  599. }
  600. static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  601. {
  602. unsigned int ticnt;
  603. ticnt = readw(info->base + S3C2410_RTCCON);
  604. ticnt &= S3C64XX_RTCCON_TICEN;
  605. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  606. }
  607. static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
  608. {
  609. info->ticnt_save = readb(info->base + S3C2410_TICNT);
  610. }
  611. static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
  612. {
  613. writeb(info->ticnt_save, info->base + S3C2410_TICNT);
  614. }
  615. static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
  616. {
  617. info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
  618. info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
  619. info->ticnt_save = readl(info->base + S3C2410_TICNT);
  620. }
  621. static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
  622. {
  623. unsigned int con;
  624. writel(info->ticnt_save, info->base + S3C2410_TICNT);
  625. if (info->ticnt_en_save) {
  626. con = readw(info->base + S3C2410_RTCCON);
  627. writew(con | info->ticnt_en_save,
  628. info->base + S3C2410_RTCCON);
  629. }
  630. }
  631. static struct s3c_rtc_data const s3c2410_rtc_data = {
  632. .max_user_freq = 128,
  633. .irq_handler = s3c24xx_rtc_irq,
  634. .set_freq = s3c2410_rtc_setfreq,
  635. .enable_tick = s3c24xx_rtc_enable_tick,
  636. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  637. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  638. .enable = s3c24xx_rtc_enable,
  639. .disable = s3c24xx_rtc_disable,
  640. };
  641. static struct s3c_rtc_data const s3c2416_rtc_data = {
  642. .max_user_freq = 32768,
  643. .irq_handler = s3c24xx_rtc_irq,
  644. .set_freq = s3c2416_rtc_setfreq,
  645. .enable_tick = s3c24xx_rtc_enable_tick,
  646. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  647. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  648. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  649. .enable = s3c24xx_rtc_enable,
  650. .disable = s3c24xx_rtc_disable,
  651. };
  652. static struct s3c_rtc_data const s3c2443_rtc_data = {
  653. .max_user_freq = 32768,
  654. .irq_handler = s3c24xx_rtc_irq,
  655. .set_freq = s3c2443_rtc_setfreq,
  656. .enable_tick = s3c24xx_rtc_enable_tick,
  657. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  658. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  659. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  660. .enable = s3c24xx_rtc_enable,
  661. .disable = s3c24xx_rtc_disable,
  662. };
  663. static struct s3c_rtc_data const s3c6410_rtc_data = {
  664. .max_user_freq = 32768,
  665. .irq_handler = s3c6410_rtc_irq,
  666. .set_freq = s3c6410_rtc_setfreq,
  667. .enable_tick = s3c6410_rtc_enable_tick,
  668. .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
  669. .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
  670. .enable = s3c24xx_rtc_enable,
  671. .disable = s3c6410_rtc_disable,
  672. };
  673. static struct s3c_rtc_data const exynos3250_rtc_data = {
  674. .max_user_freq = 32768,
  675. .needs_src_clk = true,
  676. .irq_handler = s3c6410_rtc_irq,
  677. .set_freq = s3c6410_rtc_setfreq,
  678. .enable_tick = s3c6410_rtc_enable_tick,
  679. .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
  680. .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
  681. .enable = s3c24xx_rtc_enable,
  682. .disable = s3c6410_rtc_disable,
  683. };
  684. static const struct of_device_id s3c_rtc_dt_match[] = {
  685. {
  686. .compatible = "samsung,s3c2410-rtc",
  687. .data = (void *)&s3c2410_rtc_data,
  688. }, {
  689. .compatible = "samsung,s3c2416-rtc",
  690. .data = (void *)&s3c2416_rtc_data,
  691. }, {
  692. .compatible = "samsung,s3c2443-rtc",
  693. .data = (void *)&s3c2443_rtc_data,
  694. }, {
  695. .compatible = "samsung,s3c6410-rtc",
  696. .data = (void *)&s3c6410_rtc_data,
  697. }, {
  698. .compatible = "samsung,exynos3250-rtc",
  699. .data = (void *)&exynos3250_rtc_data,
  700. },
  701. { /* sentinel */ },
  702. };
  703. MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
  704. static struct platform_driver s3c_rtc_driver = {
  705. .probe = s3c_rtc_probe,
  706. .remove = s3c_rtc_remove,
  707. .driver = {
  708. .name = "s3c-rtc",
  709. .owner = THIS_MODULE,
  710. .pm = &s3c_rtc_pm_ops,
  711. .of_match_table = of_match_ptr(s3c_rtc_dt_match),
  712. },
  713. };
  714. module_platform_driver(s3c_rtc_driver);
  715. MODULE_DESCRIPTION("Samsung S3C RTC Driver");
  716. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  717. MODULE_LICENSE("GPL");
  718. MODULE_ALIAS("platform:s3c2410-rtc");