rtc-omap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * TI OMAP Real Time Clock interface for Linux
  3. *
  4. * Copyright (C) 2003 MontaVista Software, Inc.
  5. * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
  6. *
  7. * Copyright (C) 2006 David Brownell (new RTC framework)
  8. * Copyright (C) 2014 Johan Hovold <johan@kernel.org>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/ioport.h>
  19. #include <linux/delay.h>
  20. #include <linux/rtc.h>
  21. #include <linux/bcd.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/io.h>
  27. /*
  28. * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
  29. * with century-range alarm matching, driven by the 32kHz clock.
  30. *
  31. * The main user-visible ways it differs from PC RTCs are by omitting
  32. * "don't care" alarm fields and sub-second periodic IRQs, and having
  33. * an autoadjust mechanism to calibrate to the true oscillator rate.
  34. *
  35. * Board-specific wiring options include using split power mode with
  36. * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
  37. * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
  38. * low power modes) for OMAP1 boards (OMAP-L138 has this built into
  39. * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
  40. */
  41. /* RTC registers */
  42. #define OMAP_RTC_SECONDS_REG 0x00
  43. #define OMAP_RTC_MINUTES_REG 0x04
  44. #define OMAP_RTC_HOURS_REG 0x08
  45. #define OMAP_RTC_DAYS_REG 0x0C
  46. #define OMAP_RTC_MONTHS_REG 0x10
  47. #define OMAP_RTC_YEARS_REG 0x14
  48. #define OMAP_RTC_WEEKS_REG 0x18
  49. #define OMAP_RTC_ALARM_SECONDS_REG 0x20
  50. #define OMAP_RTC_ALARM_MINUTES_REG 0x24
  51. #define OMAP_RTC_ALARM_HOURS_REG 0x28
  52. #define OMAP_RTC_ALARM_DAYS_REG 0x2c
  53. #define OMAP_RTC_ALARM_MONTHS_REG 0x30
  54. #define OMAP_RTC_ALARM_YEARS_REG 0x34
  55. #define OMAP_RTC_CTRL_REG 0x40
  56. #define OMAP_RTC_STATUS_REG 0x44
  57. #define OMAP_RTC_INTERRUPTS_REG 0x48
  58. #define OMAP_RTC_COMP_LSB_REG 0x4c
  59. #define OMAP_RTC_COMP_MSB_REG 0x50
  60. #define OMAP_RTC_OSC_REG 0x54
  61. #define OMAP_RTC_KICK0_REG 0x6c
  62. #define OMAP_RTC_KICK1_REG 0x70
  63. #define OMAP_RTC_IRQWAKEEN 0x7c
  64. #define OMAP_RTC_ALARM2_SECONDS_REG 0x80
  65. #define OMAP_RTC_ALARM2_MINUTES_REG 0x84
  66. #define OMAP_RTC_ALARM2_HOURS_REG 0x88
  67. #define OMAP_RTC_ALARM2_DAYS_REG 0x8c
  68. #define OMAP_RTC_ALARM2_MONTHS_REG 0x90
  69. #define OMAP_RTC_ALARM2_YEARS_REG 0x94
  70. #define OMAP_RTC_PMIC_REG 0x98
  71. /* OMAP_RTC_CTRL_REG bit fields: */
  72. #define OMAP_RTC_CTRL_SPLIT BIT(7)
  73. #define OMAP_RTC_CTRL_DISABLE BIT(6)
  74. #define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
  75. #define OMAP_RTC_CTRL_TEST BIT(4)
  76. #define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
  77. #define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
  78. #define OMAP_RTC_CTRL_ROUND_30S BIT(1)
  79. #define OMAP_RTC_CTRL_STOP BIT(0)
  80. /* OMAP_RTC_STATUS_REG bit fields: */
  81. #define OMAP_RTC_STATUS_POWER_UP BIT(7)
  82. #define OMAP_RTC_STATUS_ALARM2 BIT(7)
  83. #define OMAP_RTC_STATUS_ALARM BIT(6)
  84. #define OMAP_RTC_STATUS_1D_EVENT BIT(5)
  85. #define OMAP_RTC_STATUS_1H_EVENT BIT(4)
  86. #define OMAP_RTC_STATUS_1M_EVENT BIT(3)
  87. #define OMAP_RTC_STATUS_1S_EVENT BIT(2)
  88. #define OMAP_RTC_STATUS_RUN BIT(1)
  89. #define OMAP_RTC_STATUS_BUSY BIT(0)
  90. /* OMAP_RTC_INTERRUPTS_REG bit fields: */
  91. #define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
  92. #define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
  93. #define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
  94. /* OMAP_RTC_OSC_REG bit fields: */
  95. #define OMAP_RTC_OSC_32KCLK_EN BIT(6)
  96. /* OMAP_RTC_IRQWAKEEN bit fields: */
  97. #define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
  98. /* OMAP_RTC_PMIC bit fields: */
  99. #define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
  100. /* OMAP_RTC_KICKER values */
  101. #define KICK0_VALUE 0x83e70b13
  102. #define KICK1_VALUE 0x95a4f1e0
  103. struct omap_rtc_device_type {
  104. bool has_32kclk_en;
  105. bool has_kicker;
  106. bool has_irqwakeen;
  107. bool has_pmic_mode;
  108. bool has_power_up_reset;
  109. };
  110. struct omap_rtc {
  111. struct rtc_device *rtc;
  112. void __iomem *base;
  113. int irq_alarm;
  114. int irq_timer;
  115. u8 interrupts_reg;
  116. bool is_pmic_controller;
  117. const struct omap_rtc_device_type *type;
  118. };
  119. static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
  120. {
  121. return readb(rtc->base + reg);
  122. }
  123. static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
  124. {
  125. return readl(rtc->base + reg);
  126. }
  127. static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
  128. {
  129. writeb(val, rtc->base + reg);
  130. }
  131. static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
  132. {
  133. writel(val, rtc->base + reg);
  134. }
  135. /*
  136. * We rely on the rtc framework to handle locking (rtc->ops_lock),
  137. * so the only other requirement is that register accesses which
  138. * require BUSY to be clear are made with IRQs locally disabled
  139. */
  140. static void rtc_wait_not_busy(struct omap_rtc *rtc)
  141. {
  142. int count;
  143. u8 status;
  144. /* BUSY may stay active for 1/32768 second (~30 usec) */
  145. for (count = 0; count < 50; count++) {
  146. status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  147. if (!(status & OMAP_RTC_STATUS_BUSY))
  148. break;
  149. udelay(1);
  150. }
  151. /* now we have ~15 usec to read/write various registers */
  152. }
  153. static irqreturn_t rtc_irq(int irq, void *dev_id)
  154. {
  155. struct omap_rtc *rtc = dev_id;
  156. unsigned long events = 0;
  157. u8 irq_data;
  158. irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  159. /* alarm irq? */
  160. if (irq_data & OMAP_RTC_STATUS_ALARM) {
  161. rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
  162. events |= RTC_IRQF | RTC_AF;
  163. }
  164. /* 1/sec periodic/update irq? */
  165. if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
  166. events |= RTC_IRQF | RTC_UF;
  167. rtc_update_irq(rtc->rtc, 1, events);
  168. return IRQ_HANDLED;
  169. }
  170. static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  171. {
  172. struct omap_rtc *rtc = dev_get_drvdata(dev);
  173. u8 reg, irqwake_reg = 0;
  174. local_irq_disable();
  175. rtc_wait_not_busy(rtc);
  176. reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  177. if (rtc->type->has_irqwakeen)
  178. irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
  179. if (enabled) {
  180. reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
  181. irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  182. } else {
  183. reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
  184. irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  185. }
  186. rtc_wait_not_busy(rtc);
  187. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
  188. if (rtc->type->has_irqwakeen)
  189. rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
  190. local_irq_enable();
  191. return 0;
  192. }
  193. /* this hardware doesn't support "don't care" alarm fields */
  194. static int tm2bcd(struct rtc_time *tm)
  195. {
  196. if (rtc_valid_tm(tm) != 0)
  197. return -EINVAL;
  198. tm->tm_sec = bin2bcd(tm->tm_sec);
  199. tm->tm_min = bin2bcd(tm->tm_min);
  200. tm->tm_hour = bin2bcd(tm->tm_hour);
  201. tm->tm_mday = bin2bcd(tm->tm_mday);
  202. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  203. /* epoch == 1900 */
  204. if (tm->tm_year < 100 || tm->tm_year > 199)
  205. return -EINVAL;
  206. tm->tm_year = bin2bcd(tm->tm_year - 100);
  207. return 0;
  208. }
  209. static void bcd2tm(struct rtc_time *tm)
  210. {
  211. tm->tm_sec = bcd2bin(tm->tm_sec);
  212. tm->tm_min = bcd2bin(tm->tm_min);
  213. tm->tm_hour = bcd2bin(tm->tm_hour);
  214. tm->tm_mday = bcd2bin(tm->tm_mday);
  215. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  216. /* epoch == 1900 */
  217. tm->tm_year = bcd2bin(tm->tm_year) + 100;
  218. }
  219. static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
  220. {
  221. tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
  222. tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
  223. tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
  224. tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
  225. tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
  226. tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
  227. }
  228. static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
  229. {
  230. struct omap_rtc *rtc = dev_get_drvdata(dev);
  231. /* we don't report wday/yday/isdst ... */
  232. local_irq_disable();
  233. rtc_wait_not_busy(rtc);
  234. omap_rtc_read_time_raw(rtc, tm);
  235. local_irq_enable();
  236. bcd2tm(tm);
  237. return 0;
  238. }
  239. static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
  240. {
  241. struct omap_rtc *rtc = dev_get_drvdata(dev);
  242. if (tm2bcd(tm) < 0)
  243. return -EINVAL;
  244. local_irq_disable();
  245. rtc_wait_not_busy(rtc);
  246. rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
  247. rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
  248. rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
  249. rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
  250. rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
  251. rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
  252. local_irq_enable();
  253. return 0;
  254. }
  255. static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  256. {
  257. struct omap_rtc *rtc = dev_get_drvdata(dev);
  258. u8 interrupts;
  259. local_irq_disable();
  260. rtc_wait_not_busy(rtc);
  261. alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
  262. alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
  263. alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
  264. alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
  265. alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
  266. alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
  267. local_irq_enable();
  268. bcd2tm(&alm->time);
  269. interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  270. alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
  271. return 0;
  272. }
  273. static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  274. {
  275. struct omap_rtc *rtc = dev_get_drvdata(dev);
  276. u8 reg, irqwake_reg = 0;
  277. if (tm2bcd(&alm->time) < 0)
  278. return -EINVAL;
  279. local_irq_disable();
  280. rtc_wait_not_busy(rtc);
  281. rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
  282. rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
  283. rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
  284. rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
  285. rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
  286. rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
  287. reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  288. if (rtc->type->has_irqwakeen)
  289. irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
  290. if (alm->enabled) {
  291. reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
  292. irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  293. } else {
  294. reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
  295. irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  296. }
  297. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
  298. if (rtc->type->has_irqwakeen)
  299. rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
  300. local_irq_enable();
  301. return 0;
  302. }
  303. static struct omap_rtc *omap_rtc_power_off_rtc;
  304. /*
  305. * omap_rtc_poweroff: RTC-controlled power off
  306. *
  307. * The RTC can be used to control an external PMIC via the pmic_power_en pin,
  308. * which can be configured to transition to OFF on ALARM2 events.
  309. *
  310. * Notes:
  311. * The two-second alarm offset is the shortest offset possible as the alarm
  312. * registers must be set before the next timer update and the offset
  313. * calculation is too heavy for everything to be done within a single access
  314. * period (~15 us).
  315. *
  316. * Called with local interrupts disabled.
  317. */
  318. static void omap_rtc_power_off(void)
  319. {
  320. struct omap_rtc *rtc = omap_rtc_power_off_rtc;
  321. struct rtc_time tm;
  322. unsigned long now;
  323. u32 val;
  324. /* enable pmic_power_en control */
  325. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  326. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
  327. /* set alarm two seconds from now */
  328. omap_rtc_read_time_raw(rtc, &tm);
  329. bcd2tm(&tm);
  330. rtc_tm_to_time(&tm, &now);
  331. rtc_time_to_tm(now + 2, &tm);
  332. if (tm2bcd(&tm) < 0) {
  333. dev_err(&rtc->rtc->dev, "power off failed\n");
  334. return;
  335. }
  336. rtc_wait_not_busy(rtc);
  337. rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
  338. rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
  339. rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
  340. rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
  341. rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
  342. rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
  343. /*
  344. * enable ALARM2 interrupt
  345. *
  346. * NOTE: this fails on AM3352 if rtc_write (writeb) is used
  347. */
  348. val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  349. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
  350. val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
  351. /*
  352. * Wait for alarm to trigger (within two seconds) and external PMIC to
  353. * power off the system. Add a 500 ms margin for external latencies
  354. * (e.g. debounce circuits).
  355. */
  356. mdelay(2500);
  357. }
  358. static struct rtc_class_ops omap_rtc_ops = {
  359. .read_time = omap_rtc_read_time,
  360. .set_time = omap_rtc_set_time,
  361. .read_alarm = omap_rtc_read_alarm,
  362. .set_alarm = omap_rtc_set_alarm,
  363. .alarm_irq_enable = omap_rtc_alarm_irq_enable,
  364. };
  365. static const struct omap_rtc_device_type omap_rtc_default_type = {
  366. .has_power_up_reset = true,
  367. };
  368. static const struct omap_rtc_device_type omap_rtc_am3352_type = {
  369. .has_32kclk_en = true,
  370. .has_kicker = true,
  371. .has_irqwakeen = true,
  372. .has_pmic_mode = true,
  373. };
  374. static const struct omap_rtc_device_type omap_rtc_da830_type = {
  375. .has_kicker = true,
  376. };
  377. static const struct platform_device_id omap_rtc_id_table[] = {
  378. {
  379. .name = "omap_rtc",
  380. .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
  381. }, {
  382. .name = "am3352-rtc",
  383. .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
  384. }, {
  385. .name = "da830-rtc",
  386. .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
  387. }, {
  388. /* sentinel */
  389. }
  390. };
  391. MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
  392. static const struct of_device_id omap_rtc_of_match[] = {
  393. {
  394. .compatible = "ti,am3352-rtc",
  395. .data = &omap_rtc_am3352_type,
  396. }, {
  397. .compatible = "ti,da830-rtc",
  398. .data = &omap_rtc_da830_type,
  399. }, {
  400. /* sentinel */
  401. }
  402. };
  403. MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
  404. static int __init omap_rtc_probe(struct platform_device *pdev)
  405. {
  406. struct omap_rtc *rtc;
  407. struct resource *res;
  408. u8 reg, mask, new_ctrl;
  409. const struct platform_device_id *id_entry;
  410. const struct of_device_id *of_id;
  411. int ret;
  412. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  413. if (!rtc)
  414. return -ENOMEM;
  415. of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
  416. if (of_id) {
  417. rtc->type = of_id->data;
  418. rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
  419. of_property_read_bool(pdev->dev.of_node,
  420. "system-power-controller");
  421. } else {
  422. id_entry = platform_get_device_id(pdev);
  423. rtc->type = (void *)id_entry->driver_data;
  424. }
  425. rtc->irq_timer = platform_get_irq(pdev, 0);
  426. if (rtc->irq_timer <= 0)
  427. return -ENOENT;
  428. rtc->irq_alarm = platform_get_irq(pdev, 1);
  429. if (rtc->irq_alarm <= 0)
  430. return -ENOENT;
  431. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  432. rtc->base = devm_ioremap_resource(&pdev->dev, res);
  433. if (IS_ERR(rtc->base))
  434. return PTR_ERR(rtc->base);
  435. platform_set_drvdata(pdev, rtc);
  436. /* Enable the clock/module so that we can access the registers */
  437. pm_runtime_enable(&pdev->dev);
  438. pm_runtime_get_sync(&pdev->dev);
  439. if (rtc->type->has_kicker) {
  440. rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
  441. rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
  442. }
  443. /*
  444. * disable interrupts
  445. *
  446. * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
  447. */
  448. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  449. /* enable RTC functional clock */
  450. if (rtc->type->has_32kclk_en) {
  451. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  452. rtc_writel(rtc, OMAP_RTC_OSC_REG,
  453. reg | OMAP_RTC_OSC_32KCLK_EN);
  454. }
  455. /* clear old status */
  456. reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  457. mask = OMAP_RTC_STATUS_ALARM;
  458. if (rtc->type->has_pmic_mode)
  459. mask |= OMAP_RTC_STATUS_ALARM2;
  460. if (rtc->type->has_power_up_reset) {
  461. mask |= OMAP_RTC_STATUS_POWER_UP;
  462. if (reg & OMAP_RTC_STATUS_POWER_UP)
  463. dev_info(&pdev->dev, "RTC power up reset detected\n");
  464. }
  465. if (reg & mask)
  466. rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
  467. /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
  468. reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
  469. if (reg & OMAP_RTC_CTRL_STOP)
  470. dev_info(&pdev->dev, "already running\n");
  471. /* force to 24 hour mode */
  472. new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
  473. new_ctrl |= OMAP_RTC_CTRL_STOP;
  474. /*
  475. * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
  476. *
  477. * - Device wake-up capability setting should come through chip
  478. * init logic. OMAP1 boards should initialize the "wakeup capable"
  479. * flag in the platform device if the board is wired right for
  480. * being woken up by RTC alarm. For OMAP-L138, this capability
  481. * is built into the SoC by the "Deep Sleep" capability.
  482. *
  483. * - Boards wired so RTC_ON_nOFF is used as the reset signal,
  484. * rather than nPWRON_RESET, should forcibly enable split
  485. * power mode. (Some chip errata report that RTC_CTRL_SPLIT
  486. * is write-only, and always reads as zero...)
  487. */
  488. if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
  489. dev_info(&pdev->dev, "split power mode\n");
  490. if (reg != new_ctrl)
  491. rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
  492. device_init_wakeup(&pdev->dev, true);
  493. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  494. &omap_rtc_ops, THIS_MODULE);
  495. if (IS_ERR(rtc->rtc)) {
  496. ret = PTR_ERR(rtc->rtc);
  497. goto err;
  498. }
  499. /* handle periodic and alarm irqs */
  500. ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
  501. dev_name(&rtc->rtc->dev), rtc);
  502. if (ret)
  503. goto err;
  504. if (rtc->irq_timer != rtc->irq_alarm) {
  505. ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
  506. dev_name(&rtc->rtc->dev), rtc);
  507. if (ret)
  508. goto err;
  509. }
  510. if (rtc->is_pmic_controller) {
  511. if (!pm_power_off) {
  512. omap_rtc_power_off_rtc = rtc;
  513. pm_power_off = omap_rtc_power_off;
  514. }
  515. }
  516. return 0;
  517. err:
  518. device_init_wakeup(&pdev->dev, false);
  519. if (rtc->type->has_kicker)
  520. rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
  521. pm_runtime_put_sync(&pdev->dev);
  522. pm_runtime_disable(&pdev->dev);
  523. return ret;
  524. }
  525. static int __exit omap_rtc_remove(struct platform_device *pdev)
  526. {
  527. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  528. if (pm_power_off == omap_rtc_power_off &&
  529. omap_rtc_power_off_rtc == rtc) {
  530. pm_power_off = NULL;
  531. omap_rtc_power_off_rtc = NULL;
  532. }
  533. device_init_wakeup(&pdev->dev, 0);
  534. /* leave rtc running, but disable irqs */
  535. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  536. if (rtc->type->has_kicker)
  537. rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
  538. /* Disable the clock/module */
  539. pm_runtime_put_sync(&pdev->dev);
  540. pm_runtime_disable(&pdev->dev);
  541. return 0;
  542. }
  543. #ifdef CONFIG_PM_SLEEP
  544. static int omap_rtc_suspend(struct device *dev)
  545. {
  546. struct omap_rtc *rtc = dev_get_drvdata(dev);
  547. rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  548. /*
  549. * FIXME: the RTC alarm is not currently acting as a wakeup event
  550. * source on some platforms, and in fact this enable() call is just
  551. * saving a flag that's never used...
  552. */
  553. if (device_may_wakeup(dev))
  554. enable_irq_wake(rtc->irq_alarm);
  555. else
  556. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  557. /* Disable the clock/module */
  558. pm_runtime_put_sync(dev);
  559. return 0;
  560. }
  561. static int omap_rtc_resume(struct device *dev)
  562. {
  563. struct omap_rtc *rtc = dev_get_drvdata(dev);
  564. /* Enable the clock/module so that we can access the registers */
  565. pm_runtime_get_sync(dev);
  566. if (device_may_wakeup(dev))
  567. disable_irq_wake(rtc->irq_alarm);
  568. else
  569. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
  570. return 0;
  571. }
  572. #endif
  573. static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
  574. static void omap_rtc_shutdown(struct platform_device *pdev)
  575. {
  576. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  577. u8 mask;
  578. /*
  579. * Keep the ALARM interrupt enabled to allow the system to power up on
  580. * alarm events.
  581. */
  582. mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  583. mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
  584. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
  585. }
  586. static struct platform_driver omap_rtc_driver = {
  587. .remove = __exit_p(omap_rtc_remove),
  588. .shutdown = omap_rtc_shutdown,
  589. .driver = {
  590. .name = "omap_rtc",
  591. .pm = &omap_rtc_pm_ops,
  592. .of_match_table = omap_rtc_of_match,
  593. },
  594. .id_table = omap_rtc_id_table,
  595. };
  596. module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
  597. MODULE_ALIAS("platform:omap_rtc");
  598. MODULE_AUTHOR("George G. Davis (and others)");
  599. MODULE_LICENSE("GPL");