rtc-s3c.c 21 KB

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