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