rtc-at91rm9200.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Real Time Clock interface for Linux on Atmel AT91RM9200
  4. *
  5. * Copyright (C) 2002 Rick Bronson
  6. *
  7. * Converted to RTC class model by Andrew Victor
  8. *
  9. * Ported to Linux 2.6 by Steven Scholz
  10. * Based on s3c2410-rtc.c Simtec Electronics
  11. *
  12. * Based on sa1100-rtc.c by Nils Faerber
  13. * Based on rtc.c by Paul Gortmaker
  14. */
  15. #include <linux/bcd.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/clk.h>
  18. #include <linux/completion.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/rtc.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/suspend.h>
  29. #include <linux/time.h>
  30. #include <linux/uaccess.h>
  31. #define AT91_RTC_CR 0x00 /* Control Register */
  32. #define AT91_RTC_UPDTIM BIT(0) /* Update Request Time Register */
  33. #define AT91_RTC_UPDCAL BIT(1) /* Update Request Calendar Register */
  34. #define AT91_RTC_MR 0x04 /* Mode Register */
  35. #define AT91_RTC_HRMOD BIT(0) /* 12/24 hour mode */
  36. #define AT91_RTC_NEGPPM BIT(4) /* Negative PPM correction */
  37. #define AT91_RTC_CORRECTION GENMASK(14, 8) /* Slow clock correction */
  38. #define AT91_RTC_HIGHPPM BIT(15) /* High PPM correction */
  39. #define AT91_RTC_TIMR 0x08 /* Time Register */
  40. #define AT91_RTC_SEC GENMASK(6, 0) /* Current Second */
  41. #define AT91_RTC_MIN GENMASK(14, 8) /* Current Minute */
  42. #define AT91_RTC_HOUR GENMASK(21, 16) /* Current Hour */
  43. #define AT91_RTC_AMPM BIT(22) /* Ante Meridiem Post Meridiem Indicator */
  44. #define AT91_RTC_CALR 0x0c /* Calendar Register */
  45. #define AT91_RTC_CENT GENMASK(6, 0) /* Current Century */
  46. #define AT91_RTC_YEAR GENMASK(15, 8) /* Current Year */
  47. #define AT91_RTC_MONTH GENMASK(20, 16) /* Current Month */
  48. #define AT91_RTC_DAY GENMASK(23, 21) /* Current Day */
  49. #define AT91_RTC_DATE GENMASK(29, 24) /* Current Date */
  50. #define AT91_RTC_TIMALR 0x10 /* Time Alarm Register */
  51. #define AT91_RTC_SECEN BIT(7) /* Second Alarm Enable */
  52. #define AT91_RTC_MINEN BIT(15) /* Minute Alarm Enable */
  53. #define AT91_RTC_HOUREN BIT(23) /* Hour Alarm Enable */
  54. #define AT91_RTC_CALALR 0x14 /* Calendar Alarm Register */
  55. #define AT91_RTC_MTHEN BIT(23) /* Month Alarm Enable */
  56. #define AT91_RTC_DATEEN BIT(31) /* Date Alarm Enable */
  57. #define AT91_RTC_SR 0x18 /* Status Register */
  58. #define AT91_RTC_ACKUPD BIT(0) /* Acknowledge for Update */
  59. #define AT91_RTC_ALARM BIT(1) /* Alarm Flag */
  60. #define AT91_RTC_SECEV BIT(2) /* Second Event */
  61. #define AT91_RTC_TIMEV BIT(3) /* Time Event */
  62. #define AT91_RTC_CALEV BIT(4) /* Calendar Event */
  63. #define AT91_RTC_SCCR 0x1c /* Status Clear Command Register */
  64. #define AT91_RTC_IER 0x20 /* Interrupt Enable Register */
  65. #define AT91_RTC_IDR 0x24 /* Interrupt Disable Register */
  66. #define AT91_RTC_IMR 0x28 /* Interrupt Mask Register */
  67. #define AT91_RTC_VER 0x2c /* Valid Entry Register */
  68. #define AT91_RTC_NVTIM BIT(0) /* Non valid Time */
  69. #define AT91_RTC_NVCAL BIT(1) /* Non valid Calendar */
  70. #define AT91_RTC_NVTIMALR BIT(2) /* Non valid Time Alarm */
  71. #define AT91_RTC_NVCALALR BIT(3) /* Non valid Calendar Alarm */
  72. #define AT91_RTC_CORR_DIVIDEND 3906000
  73. #define AT91_RTC_CORR_LOW_RATIO 20
  74. #define at91_rtc_read(field) \
  75. readl_relaxed(at91_rtc_regs + field)
  76. #define at91_rtc_write(field, val) \
  77. writel_relaxed((val), at91_rtc_regs + field)
  78. struct at91_rtc_config {
  79. bool use_shadow_imr;
  80. bool has_correction;
  81. };
  82. static const struct at91_rtc_config *at91_rtc_config;
  83. static DECLARE_COMPLETION(at91_rtc_updated);
  84. static DECLARE_COMPLETION(at91_rtc_upd_rdy);
  85. static void __iomem *at91_rtc_regs;
  86. static int irq;
  87. static DEFINE_SPINLOCK(at91_rtc_lock);
  88. static u32 at91_rtc_shadow_imr;
  89. static bool suspended;
  90. static DEFINE_SPINLOCK(suspended_lock);
  91. static unsigned long cached_events;
  92. static u32 at91_rtc_imr;
  93. static struct clk *sclk;
  94. static void at91_rtc_write_ier(u32 mask)
  95. {
  96. unsigned long flags;
  97. spin_lock_irqsave(&at91_rtc_lock, flags);
  98. at91_rtc_shadow_imr |= mask;
  99. at91_rtc_write(AT91_RTC_IER, mask);
  100. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  101. }
  102. static void at91_rtc_write_idr(u32 mask)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&at91_rtc_lock, flags);
  106. at91_rtc_write(AT91_RTC_IDR, mask);
  107. /*
  108. * Register read back (of any RTC-register) needed to make sure
  109. * IDR-register write has reached the peripheral before updating
  110. * shadow mask.
  111. *
  112. * Note that there is still a possibility that the mask is updated
  113. * before interrupts have actually been disabled in hardware. The only
  114. * way to be certain would be to poll the IMR-register, which is
  115. * the very register we are trying to emulate. The register read back
  116. * is a reasonable heuristic.
  117. */
  118. at91_rtc_read(AT91_RTC_SR);
  119. at91_rtc_shadow_imr &= ~mask;
  120. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  121. }
  122. static u32 at91_rtc_read_imr(void)
  123. {
  124. unsigned long flags;
  125. u32 mask;
  126. if (at91_rtc_config->use_shadow_imr) {
  127. spin_lock_irqsave(&at91_rtc_lock, flags);
  128. mask = at91_rtc_shadow_imr;
  129. spin_unlock_irqrestore(&at91_rtc_lock, flags);
  130. } else {
  131. mask = at91_rtc_read(AT91_RTC_IMR);
  132. }
  133. return mask;
  134. }
  135. /*
  136. * Decode time/date into rtc_time structure
  137. */
  138. static void at91_rtc_decodetime(unsigned int timereg, unsigned int calreg,
  139. struct rtc_time *tm)
  140. {
  141. unsigned int time, date;
  142. /* must read twice in case it changes */
  143. do {
  144. time = at91_rtc_read(timereg);
  145. date = at91_rtc_read(calreg);
  146. } while ((time != at91_rtc_read(timereg)) ||
  147. (date != at91_rtc_read(calreg)));
  148. tm->tm_sec = bcd2bin(FIELD_GET(AT91_RTC_SEC, time));
  149. tm->tm_min = bcd2bin(FIELD_GET(AT91_RTC_MIN, time));
  150. tm->tm_hour = bcd2bin(FIELD_GET(AT91_RTC_HOUR, time));
  151. /*
  152. * The Calendar Alarm register does not have a field for
  153. * the year - so these will return an invalid value.
  154. */
  155. tm->tm_year = bcd2bin(date & AT91_RTC_CENT) * 100; /* century */
  156. tm->tm_year += bcd2bin(FIELD_GET(AT91_RTC_YEAR, date)); /* year */
  157. tm->tm_wday = bcd2bin(FIELD_GET(AT91_RTC_DAY, date)) - 1; /* day of the week [0-6], Sunday=0 */
  158. tm->tm_mon = bcd2bin(FIELD_GET(AT91_RTC_MONTH, date)) - 1;
  159. tm->tm_mday = bcd2bin(FIELD_GET(AT91_RTC_DATE, date));
  160. }
  161. /*
  162. * Read current time and date in RTC
  163. */
  164. static int at91_rtc_readtime(struct device *dev, struct rtc_time *tm)
  165. {
  166. at91_rtc_decodetime(AT91_RTC_TIMR, AT91_RTC_CALR, tm);
  167. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  168. tm->tm_year = tm->tm_year - 1900;
  169. dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
  170. return 0;
  171. }
  172. /*
  173. * Set current time and date in RTC
  174. */
  175. static int at91_rtc_settime(struct device *dev, struct rtc_time *tm)
  176. {
  177. unsigned long cr;
  178. dev_dbg(dev, "%s(): %ptR\n", __func__, tm);
  179. wait_for_completion(&at91_rtc_upd_rdy);
  180. /* Stop Time/Calendar from counting */
  181. cr = at91_rtc_read(AT91_RTC_CR);
  182. at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM);
  183. at91_rtc_write_ier(AT91_RTC_ACKUPD);
  184. wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */
  185. at91_rtc_write_idr(AT91_RTC_ACKUPD);
  186. at91_rtc_write(AT91_RTC_TIMR,
  187. FIELD_PREP(AT91_RTC_SEC, bin2bcd(tm->tm_sec))
  188. | FIELD_PREP(AT91_RTC_MIN, bin2bcd(tm->tm_min))
  189. | FIELD_PREP(AT91_RTC_HOUR, bin2bcd(tm->tm_hour)));
  190. at91_rtc_write(AT91_RTC_CALR,
  191. FIELD_PREP(AT91_RTC_CENT,
  192. bin2bcd((tm->tm_year + 1900) / 100))
  193. | FIELD_PREP(AT91_RTC_YEAR, bin2bcd(tm->tm_year % 100))
  194. | FIELD_PREP(AT91_RTC_MONTH, bin2bcd(tm->tm_mon + 1))
  195. | FIELD_PREP(AT91_RTC_DAY, bin2bcd(tm->tm_wday + 1))
  196. | FIELD_PREP(AT91_RTC_DATE, bin2bcd(tm->tm_mday)));
  197. /* Restart Time/Calendar */
  198. cr = at91_rtc_read(AT91_RTC_CR);
  199. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_SECEV);
  200. at91_rtc_write(AT91_RTC_CR, cr & ~(AT91_RTC_UPDCAL | AT91_RTC_UPDTIM));
  201. at91_rtc_write_ier(AT91_RTC_SECEV);
  202. return 0;
  203. }
  204. /*
  205. * Read alarm time and date in RTC
  206. */
  207. static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
  208. {
  209. struct rtc_time *tm = &alrm->time;
  210. at91_rtc_decodetime(AT91_RTC_TIMALR, AT91_RTC_CALALR, tm);
  211. tm->tm_year = -1;
  212. alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM)
  213. ? 1 : 0;
  214. dev_dbg(dev, "%s(): %ptR %sabled\n", __func__, tm,
  215. alrm->enabled ? "en" : "dis");
  216. return 0;
  217. }
  218. /*
  219. * Set alarm time and date in RTC
  220. */
  221. static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  222. {
  223. struct rtc_time tm = alrm->time;
  224. at91_rtc_write_idr(AT91_RTC_ALARM);
  225. at91_rtc_write(AT91_RTC_TIMALR,
  226. FIELD_PREP(AT91_RTC_SEC, bin2bcd(alrm->time.tm_sec))
  227. | FIELD_PREP(AT91_RTC_MIN, bin2bcd(alrm->time.tm_min))
  228. | FIELD_PREP(AT91_RTC_HOUR, bin2bcd(alrm->time.tm_hour))
  229. | AT91_RTC_HOUREN | AT91_RTC_MINEN | AT91_RTC_SECEN);
  230. at91_rtc_write(AT91_RTC_CALALR,
  231. FIELD_PREP(AT91_RTC_MONTH, bin2bcd(alrm->time.tm_mon + 1))
  232. | FIELD_PREP(AT91_RTC_DATE, bin2bcd(alrm->time.tm_mday))
  233. | AT91_RTC_DATEEN | AT91_RTC_MTHEN);
  234. if (alrm->enabled) {
  235. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  236. at91_rtc_write_ier(AT91_RTC_ALARM);
  237. }
  238. dev_dbg(dev, "%s(): %ptR\n", __func__, &tm);
  239. return 0;
  240. }
  241. static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  242. {
  243. dev_dbg(dev, "%s(): cmd=%08x\n", __func__, enabled);
  244. if (enabled) {
  245. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  246. at91_rtc_write_ier(AT91_RTC_ALARM);
  247. } else
  248. at91_rtc_write_idr(AT91_RTC_ALARM);
  249. return 0;
  250. }
  251. static int at91_rtc_readoffset(struct device *dev, long *offset)
  252. {
  253. u32 mr = at91_rtc_read(AT91_RTC_MR);
  254. long val = FIELD_GET(AT91_RTC_CORRECTION, mr);
  255. if (!val) {
  256. *offset = 0;
  257. return 0;
  258. }
  259. val++;
  260. if (!(mr & AT91_RTC_NEGPPM))
  261. val = -val;
  262. if (!(mr & AT91_RTC_HIGHPPM))
  263. val *= AT91_RTC_CORR_LOW_RATIO;
  264. *offset = DIV_ROUND_CLOSEST(AT91_RTC_CORR_DIVIDEND, val);
  265. return 0;
  266. }
  267. static int at91_rtc_setoffset(struct device *dev, long offset)
  268. {
  269. long corr;
  270. u32 mr;
  271. if (offset > AT91_RTC_CORR_DIVIDEND / 2)
  272. return -ERANGE;
  273. if (offset < -AT91_RTC_CORR_DIVIDEND / 2)
  274. return -ERANGE;
  275. mr = at91_rtc_read(AT91_RTC_MR);
  276. mr &= ~(AT91_RTC_NEGPPM | AT91_RTC_CORRECTION | AT91_RTC_HIGHPPM);
  277. if (offset > 0)
  278. mr |= AT91_RTC_NEGPPM;
  279. else
  280. offset = -offset;
  281. /* offset less than 764 ppb, disable correction*/
  282. if (offset < 764) {
  283. at91_rtc_write(AT91_RTC_MR, mr & ~AT91_RTC_NEGPPM);
  284. return 0;
  285. }
  286. /*
  287. * 29208 ppb is the perfect cutoff between low range and high range
  288. * low range values are never better than high range value after that.
  289. */
  290. if (offset < 29208) {
  291. corr = DIV_ROUND_CLOSEST(AT91_RTC_CORR_DIVIDEND, offset * AT91_RTC_CORR_LOW_RATIO);
  292. } else {
  293. corr = DIV_ROUND_CLOSEST(AT91_RTC_CORR_DIVIDEND, offset);
  294. mr |= AT91_RTC_HIGHPPM;
  295. }
  296. if (corr > 128)
  297. corr = 128;
  298. mr |= FIELD_PREP(AT91_RTC_CORRECTION, corr - 1);
  299. at91_rtc_write(AT91_RTC_MR, mr);
  300. return 0;
  301. }
  302. /*
  303. * IRQ handler for the RTC
  304. */
  305. static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id)
  306. {
  307. struct platform_device *pdev = dev_id;
  308. struct rtc_device *rtc = platform_get_drvdata(pdev);
  309. unsigned int rtsr;
  310. unsigned long events = 0;
  311. int ret = IRQ_NONE;
  312. spin_lock(&suspended_lock);
  313. rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr();
  314. if (rtsr) { /* this interrupt is shared! Is it ours? */
  315. if (rtsr & AT91_RTC_ALARM)
  316. events |= (RTC_AF | RTC_IRQF);
  317. if (rtsr & AT91_RTC_SECEV) {
  318. complete(&at91_rtc_upd_rdy);
  319. at91_rtc_write_idr(AT91_RTC_SECEV);
  320. }
  321. if (rtsr & AT91_RTC_ACKUPD)
  322. complete(&at91_rtc_updated);
  323. at91_rtc_write(AT91_RTC_SCCR, rtsr); /* clear status reg */
  324. if (!suspended) {
  325. rtc_update_irq(rtc, 1, events);
  326. dev_dbg(&pdev->dev, "%s(): num=%ld, events=0x%02lx\n",
  327. __func__, events >> 8, events & 0x000000FF);
  328. } else {
  329. cached_events |= events;
  330. at91_rtc_write_idr(at91_rtc_imr);
  331. pm_system_wakeup();
  332. }
  333. ret = IRQ_HANDLED;
  334. }
  335. spin_unlock(&suspended_lock);
  336. return ret;
  337. }
  338. static const struct at91_rtc_config at91rm9200_config = {
  339. };
  340. static const struct at91_rtc_config at91sam9x5_config = {
  341. .use_shadow_imr = true,
  342. };
  343. static const struct at91_rtc_config sama5d4_config = {
  344. .has_correction = true,
  345. };
  346. static const struct of_device_id at91_rtc_dt_ids[] = {
  347. {
  348. .compatible = "atmel,at91rm9200-rtc",
  349. .data = &at91rm9200_config,
  350. }, {
  351. .compatible = "atmel,at91sam9x5-rtc",
  352. .data = &at91sam9x5_config,
  353. }, {
  354. .compatible = "atmel,sama5d4-rtc",
  355. .data = &sama5d4_config,
  356. }, {
  357. .compatible = "atmel,sama5d2-rtc",
  358. .data = &sama5d4_config,
  359. }, {
  360. .compatible = "microchip,sam9x60-rtc",
  361. .data = &sama5d4_config,
  362. }, {
  363. /* sentinel */
  364. }
  365. };
  366. MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids);
  367. static const struct rtc_class_ops at91_rtc_ops = {
  368. .read_time = at91_rtc_readtime,
  369. .set_time = at91_rtc_settime,
  370. .read_alarm = at91_rtc_readalarm,
  371. .set_alarm = at91_rtc_setalarm,
  372. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  373. };
  374. static const struct rtc_class_ops sama5d4_rtc_ops = {
  375. .read_time = at91_rtc_readtime,
  376. .set_time = at91_rtc_settime,
  377. .read_alarm = at91_rtc_readalarm,
  378. .set_alarm = at91_rtc_setalarm,
  379. .alarm_irq_enable = at91_rtc_alarm_irq_enable,
  380. .set_offset = at91_rtc_setoffset,
  381. .read_offset = at91_rtc_readoffset,
  382. };
  383. /*
  384. * Initialize and install RTC driver
  385. */
  386. static int __init at91_rtc_probe(struct platform_device *pdev)
  387. {
  388. struct rtc_device *rtc;
  389. struct resource *regs;
  390. int ret = 0;
  391. at91_rtc_config = of_device_get_match_data(&pdev->dev);
  392. if (!at91_rtc_config)
  393. return -ENODEV;
  394. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  395. if (!regs) {
  396. dev_err(&pdev->dev, "no mmio resource defined\n");
  397. return -ENXIO;
  398. }
  399. irq = platform_get_irq(pdev, 0);
  400. if (irq < 0)
  401. return -ENXIO;
  402. at91_rtc_regs = devm_ioremap(&pdev->dev, regs->start,
  403. resource_size(regs));
  404. if (!at91_rtc_regs) {
  405. dev_err(&pdev->dev, "failed to map registers, aborting.\n");
  406. return -ENOMEM;
  407. }
  408. rtc = devm_rtc_allocate_device(&pdev->dev);
  409. if (IS_ERR(rtc))
  410. return PTR_ERR(rtc);
  411. platform_set_drvdata(pdev, rtc);
  412. sclk = devm_clk_get(&pdev->dev, NULL);
  413. if (IS_ERR(sclk))
  414. return PTR_ERR(sclk);
  415. ret = clk_prepare_enable(sclk);
  416. if (ret) {
  417. dev_err(&pdev->dev, "Could not enable slow clock\n");
  418. return ret;
  419. }
  420. at91_rtc_write(AT91_RTC_CR, 0);
  421. at91_rtc_write(AT91_RTC_MR, at91_rtc_read(AT91_RTC_MR) & ~AT91_RTC_HRMOD);
  422. /* Disable all interrupts */
  423. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  424. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  425. AT91_RTC_CALEV);
  426. ret = devm_request_irq(&pdev->dev, irq, at91_rtc_interrupt,
  427. IRQF_SHARED | IRQF_COND_SUSPEND,
  428. "at91_rtc", pdev);
  429. if (ret) {
  430. dev_err(&pdev->dev, "IRQ %d already in use.\n", irq);
  431. goto err_clk;
  432. }
  433. /* cpu init code should really have flagged this device as
  434. * being wake-capable; if it didn't, do that here.
  435. */
  436. if (!device_can_wakeup(&pdev->dev))
  437. device_init_wakeup(&pdev->dev, true);
  438. if (at91_rtc_config->has_correction)
  439. rtc->ops = &sama5d4_rtc_ops;
  440. else
  441. rtc->ops = &at91_rtc_ops;
  442. rtc->range_min = RTC_TIMESTAMP_BEGIN_1900;
  443. rtc->range_max = RTC_TIMESTAMP_END_2099;
  444. ret = devm_rtc_register_device(rtc);
  445. if (ret)
  446. goto err_clk;
  447. /* enable SECEV interrupt in order to initialize at91_rtc_upd_rdy
  448. * completion.
  449. */
  450. at91_rtc_write_ier(AT91_RTC_SECEV);
  451. dev_info(&pdev->dev, "AT91 Real Time Clock driver.\n");
  452. return 0;
  453. err_clk:
  454. clk_disable_unprepare(sclk);
  455. return ret;
  456. }
  457. /*
  458. * Disable and remove the RTC driver
  459. */
  460. static void __exit at91_rtc_remove(struct platform_device *pdev)
  461. {
  462. /* Disable all interrupts */
  463. at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  464. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  465. AT91_RTC_CALEV);
  466. clk_disable_unprepare(sclk);
  467. }
  468. static void at91_rtc_shutdown(struct platform_device *pdev)
  469. {
  470. /* Disable all interrupts */
  471. at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM |
  472. AT91_RTC_SECEV | AT91_RTC_TIMEV |
  473. AT91_RTC_CALEV);
  474. }
  475. #ifdef CONFIG_PM_SLEEP
  476. /* AT91RM9200 RTC Power management control */
  477. static int at91_rtc_suspend(struct device *dev)
  478. {
  479. /* this IRQ is shared with DBGU and other hardware which isn't
  480. * necessarily doing PM like we are...
  481. */
  482. at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM);
  483. at91_rtc_imr = at91_rtc_read_imr()
  484. & (AT91_RTC_ALARM|AT91_RTC_SECEV);
  485. if (at91_rtc_imr) {
  486. if (device_may_wakeup(dev)) {
  487. unsigned long flags;
  488. enable_irq_wake(irq);
  489. spin_lock_irqsave(&suspended_lock, flags);
  490. suspended = true;
  491. spin_unlock_irqrestore(&suspended_lock, flags);
  492. } else {
  493. at91_rtc_write_idr(at91_rtc_imr);
  494. }
  495. }
  496. return 0;
  497. }
  498. static int at91_rtc_resume(struct device *dev)
  499. {
  500. struct rtc_device *rtc = dev_get_drvdata(dev);
  501. if (at91_rtc_imr) {
  502. if (device_may_wakeup(dev)) {
  503. unsigned long flags;
  504. spin_lock_irqsave(&suspended_lock, flags);
  505. if (cached_events) {
  506. rtc_update_irq(rtc, 1, cached_events);
  507. cached_events = 0;
  508. }
  509. suspended = false;
  510. spin_unlock_irqrestore(&suspended_lock, flags);
  511. disable_irq_wake(irq);
  512. }
  513. at91_rtc_write_ier(at91_rtc_imr);
  514. }
  515. return 0;
  516. }
  517. #endif
  518. static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume);
  519. /*
  520. * at91_rtc_remove() lives in .exit.text. For drivers registered via
  521. * module_platform_driver_probe() this is ok because they cannot get unbound at
  522. * runtime. So mark the driver struct with __refdata to prevent modpost
  523. * triggering a section mismatch warning.
  524. */
  525. static struct platform_driver at91_rtc_driver __refdata = {
  526. .remove = __exit_p(at91_rtc_remove),
  527. .shutdown = at91_rtc_shutdown,
  528. .driver = {
  529. .name = "at91_rtc",
  530. .pm = &at91_rtc_pm_ops,
  531. .of_match_table = at91_rtc_dt_ids,
  532. },
  533. };
  534. module_platform_driver_probe(at91_rtc_driver, at91_rtc_probe);
  535. MODULE_AUTHOR("Rick Bronson");
  536. MODULE_DESCRIPTION("RTC driver for Atmel AT91RM9200");
  537. MODULE_LICENSE("GPL");