rtc-s3c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. if (alm_en & S3C2410_RTCALM_MINEN)
  207. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  208. if (alm_en & S3C2410_RTCALM_HOUREN)
  209. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  210. if (alm_en & S3C2410_RTCALM_DAYEN)
  211. alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
  212. if (alm_en & S3C2410_RTCALM_MONEN) {
  213. alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
  214. alm_tm->tm_mon -= 1;
  215. }
  216. if (alm_en & S3C2410_RTCALM_YEAREN)
  217. alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
  218. return 0;
  219. }
  220. static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  221. {
  222. struct s3c_rtc *info = dev_get_drvdata(dev);
  223. struct rtc_time *tm = &alrm->time;
  224. unsigned int alrm_en;
  225. int year = tm->tm_year - 100;
  226. dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
  227. alrm->enabled,
  228. 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
  229. tm->tm_hour, tm->tm_min, tm->tm_sec);
  230. s3c_rtc_enable_clk(info);
  231. alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
  232. writeb(0x00, info->base + S3C2410_RTCALM);
  233. if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
  234. alrm_en |= S3C2410_RTCALM_SECEN;
  235. writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
  236. }
  237. if (tm->tm_min < 60 && tm->tm_min >= 0) {
  238. alrm_en |= S3C2410_RTCALM_MINEN;
  239. writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
  240. }
  241. if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
  242. alrm_en |= S3C2410_RTCALM_HOUREN;
  243. writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
  244. }
  245. if (year < 100 && year >= 0) {
  246. alrm_en |= S3C2410_RTCALM_YEAREN;
  247. writeb(bin2bcd(year), info->base + S3C2410_ALMYEAR);
  248. }
  249. if (tm->tm_mon < 12 && tm->tm_mon >= 0) {
  250. alrm_en |= S3C2410_RTCALM_MONEN;
  251. writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON);
  252. }
  253. if (tm->tm_mday <= 31 && tm->tm_mday >= 1) {
  254. alrm_en |= S3C2410_RTCALM_DAYEN;
  255. writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE);
  256. }
  257. dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
  258. writeb(alrm_en, info->base + S3C2410_RTCALM);
  259. s3c_rtc_disable_clk(info);
  260. s3c_rtc_setaie(dev, alrm->enabled);
  261. return 0;
  262. }
  263. static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
  264. {
  265. struct s3c_rtc *info = dev_get_drvdata(dev);
  266. s3c_rtc_enable_clk(info);
  267. if (info->data->enable_tick)
  268. info->data->enable_tick(info, seq);
  269. s3c_rtc_disable_clk(info);
  270. return 0;
  271. }
  272. static const struct rtc_class_ops s3c_rtcops = {
  273. .read_time = s3c_rtc_gettime,
  274. .set_time = s3c_rtc_settime,
  275. .read_alarm = s3c_rtc_getalarm,
  276. .set_alarm = s3c_rtc_setalarm,
  277. .proc = s3c_rtc_proc,
  278. .alarm_irq_enable = s3c_rtc_setaie,
  279. };
  280. static void s3c24xx_rtc_enable(struct s3c_rtc *info)
  281. {
  282. unsigned int con, tmp;
  283. con = readw(info->base + S3C2410_RTCCON);
  284. /* re-enable the device, and check it is ok */
  285. if ((con & S3C2410_RTCCON_RTCEN) == 0) {
  286. dev_info(info->dev, "rtc disabled, re-enabling\n");
  287. tmp = readw(info->base + S3C2410_RTCCON);
  288. writew(tmp | S3C2410_RTCCON_RTCEN,
  289. info->base + S3C2410_RTCCON);
  290. }
  291. if (con & S3C2410_RTCCON_CNTSEL) {
  292. dev_info(info->dev, "removing RTCCON_CNTSEL\n");
  293. tmp = readw(info->base + S3C2410_RTCCON);
  294. writew(tmp & ~S3C2410_RTCCON_CNTSEL,
  295. info->base + S3C2410_RTCCON);
  296. }
  297. if (con & S3C2410_RTCCON_CLKRST) {
  298. dev_info(info->dev, "removing RTCCON_CLKRST\n");
  299. tmp = readw(info->base + S3C2410_RTCCON);
  300. writew(tmp & ~S3C2410_RTCCON_CLKRST,
  301. info->base + S3C2410_RTCCON);
  302. }
  303. }
  304. static void s3c24xx_rtc_disable(struct s3c_rtc *info)
  305. {
  306. unsigned int con;
  307. con = readw(info->base + S3C2410_RTCCON);
  308. con &= ~S3C2410_RTCCON_RTCEN;
  309. writew(con, info->base + S3C2410_RTCCON);
  310. con = readb(info->base + S3C2410_TICNT);
  311. con &= ~S3C2410_TICNT_ENABLE;
  312. writeb(con, info->base + S3C2410_TICNT);
  313. }
  314. static void s3c6410_rtc_disable(struct s3c_rtc *info)
  315. {
  316. unsigned int con;
  317. con = readw(info->base + S3C2410_RTCCON);
  318. con &= ~S3C64XX_RTCCON_TICEN;
  319. con &= ~S3C2410_RTCCON_RTCEN;
  320. writew(con, info->base + S3C2410_RTCCON);
  321. }
  322. static int s3c_rtc_remove(struct platform_device *pdev)
  323. {
  324. struct s3c_rtc *info = platform_get_drvdata(pdev);
  325. s3c_rtc_setaie(info->dev, 0);
  326. if (info->data->needs_src_clk)
  327. clk_unprepare(info->rtc_src_clk);
  328. clk_unprepare(info->rtc_clk);
  329. return 0;
  330. }
  331. static const struct of_device_id s3c_rtc_dt_match[];
  332. static struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
  333. {
  334. const struct of_device_id *match;
  335. match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
  336. return (struct s3c_rtc_data *)match->data;
  337. }
  338. static int s3c_rtc_probe(struct platform_device *pdev)
  339. {
  340. struct s3c_rtc *info = NULL;
  341. struct rtc_time rtc_tm;
  342. struct resource *res;
  343. int ret;
  344. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  345. if (!info)
  346. return -ENOMEM;
  347. /* find the IRQs */
  348. info->irq_tick = platform_get_irq(pdev, 1);
  349. if (info->irq_tick < 0) {
  350. dev_err(&pdev->dev, "no irq for rtc tick\n");
  351. return info->irq_tick;
  352. }
  353. info->dev = &pdev->dev;
  354. info->data = s3c_rtc_get_data(pdev);
  355. if (!info->data) {
  356. dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
  357. return -EINVAL;
  358. }
  359. spin_lock_init(&info->pie_lock);
  360. spin_lock_init(&info->alarm_clk_lock);
  361. platform_set_drvdata(pdev, info);
  362. info->irq_alarm = platform_get_irq(pdev, 0);
  363. if (info->irq_alarm < 0) {
  364. dev_err(&pdev->dev, "no irq for alarm\n");
  365. return info->irq_alarm;
  366. }
  367. dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
  368. info->irq_tick, info->irq_alarm);
  369. /* get the memory region */
  370. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  371. info->base = devm_ioremap_resource(&pdev->dev, res);
  372. if (IS_ERR(info->base))
  373. return PTR_ERR(info->base);
  374. info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
  375. if (IS_ERR(info->rtc_clk)) {
  376. ret = PTR_ERR(info->rtc_clk);
  377. if (ret != -EPROBE_DEFER)
  378. dev_err(&pdev->dev, "failed to find rtc clock\n");
  379. else
  380. dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
  381. return ret;
  382. }
  383. clk_prepare_enable(info->rtc_clk);
  384. if (info->data->needs_src_clk) {
  385. info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
  386. if (IS_ERR(info->rtc_src_clk)) {
  387. ret = PTR_ERR(info->rtc_src_clk);
  388. if (ret != -EPROBE_DEFER)
  389. dev_err(&pdev->dev,
  390. "failed to find rtc source clock\n");
  391. else
  392. dev_dbg(&pdev->dev,
  393. "probe deferred due to missing rtc src clk\n");
  394. clk_disable_unprepare(info->rtc_clk);
  395. return ret;
  396. }
  397. clk_prepare_enable(info->rtc_src_clk);
  398. }
  399. /* check to see if everything is setup correctly */
  400. if (info->data->enable)
  401. info->data->enable(info);
  402. dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
  403. readw(info->base + S3C2410_RTCCON));
  404. device_init_wakeup(&pdev->dev, 1);
  405. /* Check RTC Time */
  406. if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
  407. rtc_tm.tm_year = 100;
  408. rtc_tm.tm_mon = 0;
  409. rtc_tm.tm_mday = 1;
  410. rtc_tm.tm_hour = 0;
  411. rtc_tm.tm_min = 0;
  412. rtc_tm.tm_sec = 0;
  413. s3c_rtc_settime(&pdev->dev, &rtc_tm);
  414. dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
  415. }
  416. /* register RTC and exit */
  417. info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
  418. THIS_MODULE);
  419. if (IS_ERR(info->rtc)) {
  420. dev_err(&pdev->dev, "cannot attach rtc\n");
  421. ret = PTR_ERR(info->rtc);
  422. goto err_nortc;
  423. }
  424. ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
  425. 0, "s3c2410-rtc alarm", info);
  426. if (ret) {
  427. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
  428. goto err_nortc;
  429. }
  430. ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
  431. 0, "s3c2410-rtc tick", info);
  432. if (ret) {
  433. dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
  434. goto err_nortc;
  435. }
  436. if (info->data->select_tick_clk)
  437. info->data->select_tick_clk(info);
  438. s3c_rtc_setfreq(info, 1);
  439. return 0;
  440. err_nortc:
  441. if (info->data->disable)
  442. info->data->disable(info);
  443. if (info->data->needs_src_clk)
  444. clk_disable_unprepare(info->rtc_src_clk);
  445. clk_disable_unprepare(info->rtc_clk);
  446. return ret;
  447. }
  448. #ifdef CONFIG_PM_SLEEP
  449. static int s3c_rtc_suspend(struct device *dev)
  450. {
  451. struct s3c_rtc *info = dev_get_drvdata(dev);
  452. s3c_rtc_enable_clk(info);
  453. /* save TICNT for anyone using periodic interrupts */
  454. if (info->data->save_tick_cnt)
  455. info->data->save_tick_cnt(info);
  456. if (info->data->disable)
  457. info->data->disable(info);
  458. if (device_may_wakeup(dev) && !info->wake_en) {
  459. if (enable_irq_wake(info->irq_alarm) == 0)
  460. info->wake_en = true;
  461. else
  462. dev_err(dev, "enable_irq_wake failed\n");
  463. }
  464. return 0;
  465. }
  466. static int s3c_rtc_resume(struct device *dev)
  467. {
  468. struct s3c_rtc *info = dev_get_drvdata(dev);
  469. if (info->data->enable)
  470. info->data->enable(info);
  471. if (info->data->restore_tick_cnt)
  472. info->data->restore_tick_cnt(info);
  473. s3c_rtc_disable_clk(info);
  474. if (device_may_wakeup(dev) && info->wake_en) {
  475. disable_irq_wake(info->irq_alarm);
  476. info->wake_en = false;
  477. }
  478. return 0;
  479. }
  480. #endif
  481. static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
  482. static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
  483. {
  484. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  485. }
  486. static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
  487. {
  488. rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
  489. writeb(mask, info->base + S3C2410_INTP);
  490. }
  491. static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
  492. {
  493. unsigned int tmp = 0;
  494. int val;
  495. tmp = readb(info->base + S3C2410_TICNT);
  496. tmp &= S3C2410_TICNT_ENABLE;
  497. val = (info->rtc->max_user_freq / freq) - 1;
  498. tmp |= val;
  499. writel(tmp, info->base + S3C2410_TICNT);
  500. }
  501. static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
  502. {
  503. unsigned int tmp = 0;
  504. int val;
  505. tmp = readb(info->base + S3C2410_TICNT);
  506. tmp &= S3C2410_TICNT_ENABLE;
  507. val = (info->rtc->max_user_freq / freq) - 1;
  508. tmp |= S3C2443_TICNT_PART(val);
  509. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  510. writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
  511. writel(tmp, info->base + S3C2410_TICNT);
  512. }
  513. static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
  514. {
  515. unsigned int tmp = 0;
  516. int val;
  517. tmp = readb(info->base + S3C2410_TICNT);
  518. tmp &= S3C2410_TICNT_ENABLE;
  519. val = (info->rtc->max_user_freq / freq) - 1;
  520. tmp |= S3C2443_TICNT_PART(val);
  521. writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
  522. writel(tmp, info->base + S3C2410_TICNT);
  523. }
  524. static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
  525. {
  526. int val;
  527. val = (info->rtc->max_user_freq / freq) - 1;
  528. writel(val, info->base + S3C2410_TICNT);
  529. }
  530. static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  531. {
  532. unsigned int ticnt;
  533. ticnt = readb(info->base + S3C2410_TICNT);
  534. ticnt &= S3C2410_TICNT_ENABLE;
  535. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  536. }
  537. static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
  538. {
  539. unsigned int con;
  540. con = readw(info->base + S3C2410_RTCCON);
  541. con |= S3C2443_RTCCON_TICSEL;
  542. writew(con, info->base + S3C2410_RTCCON);
  543. }
  544. static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
  545. {
  546. unsigned int ticnt;
  547. ticnt = readw(info->base + S3C2410_RTCCON);
  548. ticnt &= S3C64XX_RTCCON_TICEN;
  549. seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
  550. }
  551. static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
  552. {
  553. info->ticnt_save = readb(info->base + S3C2410_TICNT);
  554. }
  555. static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
  556. {
  557. writeb(info->ticnt_save, info->base + S3C2410_TICNT);
  558. }
  559. static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
  560. {
  561. info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
  562. info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
  563. info->ticnt_save = readl(info->base + S3C2410_TICNT);
  564. }
  565. static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
  566. {
  567. unsigned int con;
  568. writel(info->ticnt_save, info->base + S3C2410_TICNT);
  569. if (info->ticnt_en_save) {
  570. con = readw(info->base + S3C2410_RTCCON);
  571. writew(con | info->ticnt_en_save,
  572. info->base + S3C2410_RTCCON);
  573. }
  574. }
  575. static struct s3c_rtc_data const s3c2410_rtc_data = {
  576. .max_user_freq = 128,
  577. .irq_handler = s3c24xx_rtc_irq,
  578. .set_freq = s3c2410_rtc_setfreq,
  579. .enable_tick = s3c24xx_rtc_enable_tick,
  580. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  581. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  582. .enable = s3c24xx_rtc_enable,
  583. .disable = s3c24xx_rtc_disable,
  584. };
  585. static struct s3c_rtc_data const s3c2416_rtc_data = {
  586. .max_user_freq = 32768,
  587. .irq_handler = s3c24xx_rtc_irq,
  588. .set_freq = s3c2416_rtc_setfreq,
  589. .enable_tick = s3c24xx_rtc_enable_tick,
  590. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  591. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  592. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  593. .enable = s3c24xx_rtc_enable,
  594. .disable = s3c24xx_rtc_disable,
  595. };
  596. static struct s3c_rtc_data const s3c2443_rtc_data = {
  597. .max_user_freq = 32768,
  598. .irq_handler = s3c24xx_rtc_irq,
  599. .set_freq = s3c2443_rtc_setfreq,
  600. .enable_tick = s3c24xx_rtc_enable_tick,
  601. .select_tick_clk = s3c2416_rtc_select_tick_clk,
  602. .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
  603. .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
  604. .enable = s3c24xx_rtc_enable,
  605. .disable = s3c24xx_rtc_disable,
  606. };
  607. static struct s3c_rtc_data const s3c6410_rtc_data = {
  608. .max_user_freq = 32768,
  609. .needs_src_clk = true,
  610. .irq_handler = s3c6410_rtc_irq,
  611. .set_freq = s3c6410_rtc_setfreq,
  612. .enable_tick = s3c6410_rtc_enable_tick,
  613. .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
  614. .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
  615. .enable = s3c24xx_rtc_enable,
  616. .disable = s3c6410_rtc_disable,
  617. };
  618. static const struct of_device_id s3c_rtc_dt_match[] = {
  619. {
  620. .compatible = "samsung,s3c2410-rtc",
  621. .data = (void *)&s3c2410_rtc_data,
  622. }, {
  623. .compatible = "samsung,s3c2416-rtc",
  624. .data = (void *)&s3c2416_rtc_data,
  625. }, {
  626. .compatible = "samsung,s3c2443-rtc",
  627. .data = (void *)&s3c2443_rtc_data,
  628. }, {
  629. .compatible = "samsung,s3c6410-rtc",
  630. .data = (void *)&s3c6410_rtc_data,
  631. }, {
  632. .compatible = "samsung,exynos3250-rtc",
  633. .data = (void *)&s3c6410_rtc_data,
  634. },
  635. { /* sentinel */ },
  636. };
  637. MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
  638. static struct platform_driver s3c_rtc_driver = {
  639. .probe = s3c_rtc_probe,
  640. .remove = s3c_rtc_remove,
  641. .driver = {
  642. .name = "s3c-rtc",
  643. .pm = &s3c_rtc_pm_ops,
  644. .of_match_table = of_match_ptr(s3c_rtc_dt_match),
  645. },
  646. };
  647. module_platform_driver(s3c_rtc_driver);
  648. MODULE_DESCRIPTION("Samsung S3C RTC Driver");
  649. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  650. MODULE_LICENSE("GPL");
  651. MODULE_ALIAS("platform:s3c2410-rtc");