rtc-omap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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;
  104. struct omap_rtc_device_type {
  105. bool has_32kclk_en;
  106. bool has_irqwakeen;
  107. bool has_pmic_mode;
  108. bool has_power_up_reset;
  109. void (*lock)(struct omap_rtc *rtc);
  110. void (*unlock)(struct omap_rtc *rtc);
  111. };
  112. struct omap_rtc {
  113. struct rtc_device *rtc;
  114. void __iomem *base;
  115. int irq_alarm;
  116. int irq_timer;
  117. u8 interrupts_reg;
  118. bool is_pmic_controller;
  119. const struct omap_rtc_device_type *type;
  120. };
  121. static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
  122. {
  123. return readb(rtc->base + reg);
  124. }
  125. static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
  126. {
  127. return readl(rtc->base + reg);
  128. }
  129. static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
  130. {
  131. writeb(val, rtc->base + reg);
  132. }
  133. static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
  134. {
  135. writel(val, rtc->base + reg);
  136. }
  137. static void am3352_rtc_unlock(struct omap_rtc *rtc)
  138. {
  139. rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
  140. rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
  141. }
  142. static void am3352_rtc_lock(struct omap_rtc *rtc)
  143. {
  144. rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
  145. rtc_writel(rtc, OMAP_RTC_KICK1_REG, 0);
  146. }
  147. static void default_rtc_unlock(struct omap_rtc *rtc)
  148. {
  149. }
  150. static void default_rtc_lock(struct omap_rtc *rtc)
  151. {
  152. }
  153. /*
  154. * We rely on the rtc framework to handle locking (rtc->ops_lock),
  155. * so the only other requirement is that register accesses which
  156. * require BUSY to be clear are made with IRQs locally disabled
  157. */
  158. static void rtc_wait_not_busy(struct omap_rtc *rtc)
  159. {
  160. int count;
  161. u8 status;
  162. /* BUSY may stay active for 1/32768 second (~30 usec) */
  163. for (count = 0; count < 50; count++) {
  164. status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  165. if (!(status & OMAP_RTC_STATUS_BUSY))
  166. break;
  167. udelay(1);
  168. }
  169. /* now we have ~15 usec to read/write various registers */
  170. }
  171. static irqreturn_t rtc_irq(int irq, void *dev_id)
  172. {
  173. struct omap_rtc *rtc = dev_id;
  174. unsigned long events = 0;
  175. u8 irq_data;
  176. irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  177. /* alarm irq? */
  178. if (irq_data & OMAP_RTC_STATUS_ALARM) {
  179. rtc->type->unlock(rtc);
  180. rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
  181. rtc->type->lock(rtc);
  182. events |= RTC_IRQF | RTC_AF;
  183. }
  184. /* 1/sec periodic/update irq? */
  185. if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
  186. events |= RTC_IRQF | RTC_UF;
  187. rtc_update_irq(rtc->rtc, 1, events);
  188. return IRQ_HANDLED;
  189. }
  190. static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  191. {
  192. struct omap_rtc *rtc = dev_get_drvdata(dev);
  193. u8 reg, irqwake_reg = 0;
  194. local_irq_disable();
  195. rtc_wait_not_busy(rtc);
  196. reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  197. if (rtc->type->has_irqwakeen)
  198. irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
  199. if (enabled) {
  200. reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
  201. irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  202. } else {
  203. reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
  204. irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  205. }
  206. rtc_wait_not_busy(rtc);
  207. rtc->type->unlock(rtc);
  208. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
  209. if (rtc->type->has_irqwakeen)
  210. rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
  211. rtc->type->lock(rtc);
  212. local_irq_enable();
  213. return 0;
  214. }
  215. /* this hardware doesn't support "don't care" alarm fields */
  216. static int tm2bcd(struct rtc_time *tm)
  217. {
  218. if (rtc_valid_tm(tm) != 0)
  219. return -EINVAL;
  220. tm->tm_sec = bin2bcd(tm->tm_sec);
  221. tm->tm_min = bin2bcd(tm->tm_min);
  222. tm->tm_hour = bin2bcd(tm->tm_hour);
  223. tm->tm_mday = bin2bcd(tm->tm_mday);
  224. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  225. /* epoch == 1900 */
  226. if (tm->tm_year < 100 || tm->tm_year > 199)
  227. return -EINVAL;
  228. tm->tm_year = bin2bcd(tm->tm_year - 100);
  229. return 0;
  230. }
  231. static void bcd2tm(struct rtc_time *tm)
  232. {
  233. tm->tm_sec = bcd2bin(tm->tm_sec);
  234. tm->tm_min = bcd2bin(tm->tm_min);
  235. tm->tm_hour = bcd2bin(tm->tm_hour);
  236. tm->tm_mday = bcd2bin(tm->tm_mday);
  237. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  238. /* epoch == 1900 */
  239. tm->tm_year = bcd2bin(tm->tm_year) + 100;
  240. }
  241. static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
  242. {
  243. tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
  244. tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
  245. tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
  246. tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
  247. tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
  248. tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
  249. }
  250. static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
  251. {
  252. struct omap_rtc *rtc = dev_get_drvdata(dev);
  253. /* we don't report wday/yday/isdst ... */
  254. local_irq_disable();
  255. rtc_wait_not_busy(rtc);
  256. omap_rtc_read_time_raw(rtc, tm);
  257. local_irq_enable();
  258. bcd2tm(tm);
  259. return 0;
  260. }
  261. static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
  262. {
  263. struct omap_rtc *rtc = dev_get_drvdata(dev);
  264. if (tm2bcd(tm) < 0)
  265. return -EINVAL;
  266. local_irq_disable();
  267. rtc_wait_not_busy(rtc);
  268. rtc->type->unlock(rtc);
  269. rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
  270. rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
  271. rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
  272. rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
  273. rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
  274. rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
  275. rtc->type->lock(rtc);
  276. local_irq_enable();
  277. return 0;
  278. }
  279. static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  280. {
  281. struct omap_rtc *rtc = dev_get_drvdata(dev);
  282. u8 interrupts;
  283. local_irq_disable();
  284. rtc_wait_not_busy(rtc);
  285. alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
  286. alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
  287. alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
  288. alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
  289. alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
  290. alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
  291. local_irq_enable();
  292. bcd2tm(&alm->time);
  293. interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  294. alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
  295. return 0;
  296. }
  297. static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  298. {
  299. struct omap_rtc *rtc = dev_get_drvdata(dev);
  300. u8 reg, irqwake_reg = 0;
  301. if (tm2bcd(&alm->time) < 0)
  302. return -EINVAL;
  303. local_irq_disable();
  304. rtc_wait_not_busy(rtc);
  305. rtc->type->unlock(rtc);
  306. rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
  307. rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
  308. rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
  309. rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
  310. rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
  311. rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
  312. reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  313. if (rtc->type->has_irqwakeen)
  314. irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
  315. if (alm->enabled) {
  316. reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
  317. irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  318. } else {
  319. reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
  320. irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  321. }
  322. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
  323. if (rtc->type->has_irqwakeen)
  324. rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
  325. rtc->type->lock(rtc);
  326. local_irq_enable();
  327. return 0;
  328. }
  329. static struct omap_rtc *omap_rtc_power_off_rtc;
  330. /*
  331. * omap_rtc_poweroff: RTC-controlled power off
  332. *
  333. * The RTC can be used to control an external PMIC via the pmic_power_en pin,
  334. * which can be configured to transition to OFF on ALARM2 events.
  335. *
  336. * Notes:
  337. * The two-second alarm offset is the shortest offset possible as the alarm
  338. * registers must be set before the next timer update and the offset
  339. * calculation is too heavy for everything to be done within a single access
  340. * period (~15 us).
  341. *
  342. * Called with local interrupts disabled.
  343. */
  344. static void omap_rtc_power_off(void)
  345. {
  346. struct omap_rtc *rtc = omap_rtc_power_off_rtc;
  347. struct rtc_time tm;
  348. unsigned long now;
  349. u32 val;
  350. rtc->type->unlock(rtc);
  351. /* enable pmic_power_en control */
  352. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  353. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
  354. /* set alarm two seconds from now */
  355. omap_rtc_read_time_raw(rtc, &tm);
  356. bcd2tm(&tm);
  357. rtc_tm_to_time(&tm, &now);
  358. rtc_time_to_tm(now + 2, &tm);
  359. if (tm2bcd(&tm) < 0) {
  360. dev_err(&rtc->rtc->dev, "power off failed\n");
  361. return;
  362. }
  363. rtc_wait_not_busy(rtc);
  364. rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
  365. rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
  366. rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
  367. rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
  368. rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
  369. rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
  370. /*
  371. * enable ALARM2 interrupt
  372. *
  373. * NOTE: this fails on AM3352 if rtc_write (writeb) is used
  374. */
  375. val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  376. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
  377. val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
  378. rtc->type->lock(rtc);
  379. /*
  380. * Wait for alarm to trigger (within two seconds) and external PMIC to
  381. * power off the system. Add a 500 ms margin for external latencies
  382. * (e.g. debounce circuits).
  383. */
  384. mdelay(2500);
  385. }
  386. static struct rtc_class_ops omap_rtc_ops = {
  387. .read_time = omap_rtc_read_time,
  388. .set_time = omap_rtc_set_time,
  389. .read_alarm = omap_rtc_read_alarm,
  390. .set_alarm = omap_rtc_set_alarm,
  391. .alarm_irq_enable = omap_rtc_alarm_irq_enable,
  392. };
  393. static const struct omap_rtc_device_type omap_rtc_default_type = {
  394. .has_power_up_reset = true,
  395. .lock = default_rtc_lock,
  396. .unlock = default_rtc_unlock,
  397. };
  398. static const struct omap_rtc_device_type omap_rtc_am3352_type = {
  399. .has_32kclk_en = true,
  400. .has_irqwakeen = true,
  401. .has_pmic_mode = true,
  402. .lock = am3352_rtc_lock,
  403. .unlock = am3352_rtc_unlock,
  404. };
  405. static const struct omap_rtc_device_type omap_rtc_da830_type = {
  406. .lock = am3352_rtc_lock,
  407. .unlock = am3352_rtc_unlock,
  408. };
  409. static const struct platform_device_id omap_rtc_id_table[] = {
  410. {
  411. .name = "omap_rtc",
  412. .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
  413. }, {
  414. .name = "am3352-rtc",
  415. .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
  416. }, {
  417. .name = "da830-rtc",
  418. .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
  419. }, {
  420. /* sentinel */
  421. }
  422. };
  423. MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
  424. static const struct of_device_id omap_rtc_of_match[] = {
  425. {
  426. .compatible = "ti,am3352-rtc",
  427. .data = &omap_rtc_am3352_type,
  428. }, {
  429. .compatible = "ti,da830-rtc",
  430. .data = &omap_rtc_da830_type,
  431. }, {
  432. /* sentinel */
  433. }
  434. };
  435. MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
  436. static int omap_rtc_probe(struct platform_device *pdev)
  437. {
  438. struct omap_rtc *rtc;
  439. struct resource *res;
  440. u8 reg, mask, new_ctrl;
  441. const struct platform_device_id *id_entry;
  442. const struct of_device_id *of_id;
  443. int ret;
  444. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  445. if (!rtc)
  446. return -ENOMEM;
  447. of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
  448. if (of_id) {
  449. rtc->type = of_id->data;
  450. rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
  451. of_property_read_bool(pdev->dev.of_node,
  452. "system-power-controller");
  453. } else {
  454. id_entry = platform_get_device_id(pdev);
  455. rtc->type = (void *)id_entry->driver_data;
  456. }
  457. rtc->irq_timer = platform_get_irq(pdev, 0);
  458. if (rtc->irq_timer <= 0)
  459. return -ENOENT;
  460. rtc->irq_alarm = platform_get_irq(pdev, 1);
  461. if (rtc->irq_alarm <= 0)
  462. return -ENOENT;
  463. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  464. rtc->base = devm_ioremap_resource(&pdev->dev, res);
  465. if (IS_ERR(rtc->base))
  466. return PTR_ERR(rtc->base);
  467. platform_set_drvdata(pdev, rtc);
  468. /* Enable the clock/module so that we can access the registers */
  469. pm_runtime_enable(&pdev->dev);
  470. pm_runtime_get_sync(&pdev->dev);
  471. rtc->type->unlock(rtc);
  472. /*
  473. * disable interrupts
  474. *
  475. * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
  476. */
  477. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  478. /* enable RTC functional clock */
  479. if (rtc->type->has_32kclk_en) {
  480. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  481. rtc_writel(rtc, OMAP_RTC_OSC_REG,
  482. reg | OMAP_RTC_OSC_32KCLK_EN);
  483. }
  484. /* clear old status */
  485. reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  486. mask = OMAP_RTC_STATUS_ALARM;
  487. if (rtc->type->has_pmic_mode)
  488. mask |= OMAP_RTC_STATUS_ALARM2;
  489. if (rtc->type->has_power_up_reset) {
  490. mask |= OMAP_RTC_STATUS_POWER_UP;
  491. if (reg & OMAP_RTC_STATUS_POWER_UP)
  492. dev_info(&pdev->dev, "RTC power up reset detected\n");
  493. }
  494. if (reg & mask)
  495. rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
  496. /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
  497. reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
  498. if (reg & OMAP_RTC_CTRL_STOP)
  499. dev_info(&pdev->dev, "already running\n");
  500. /* force to 24 hour mode */
  501. new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
  502. new_ctrl |= OMAP_RTC_CTRL_STOP;
  503. /*
  504. * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
  505. *
  506. * - Device wake-up capability setting should come through chip
  507. * init logic. OMAP1 boards should initialize the "wakeup capable"
  508. * flag in the platform device if the board is wired right for
  509. * being woken up by RTC alarm. For OMAP-L138, this capability
  510. * is built into the SoC by the "Deep Sleep" capability.
  511. *
  512. * - Boards wired so RTC_ON_nOFF is used as the reset signal,
  513. * rather than nPWRON_RESET, should forcibly enable split
  514. * power mode. (Some chip errata report that RTC_CTRL_SPLIT
  515. * is write-only, and always reads as zero...)
  516. */
  517. if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
  518. dev_info(&pdev->dev, "split power mode\n");
  519. if (reg != new_ctrl)
  520. rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
  521. rtc->type->lock(rtc);
  522. device_init_wakeup(&pdev->dev, true);
  523. rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  524. &omap_rtc_ops, THIS_MODULE);
  525. if (IS_ERR(rtc->rtc)) {
  526. ret = PTR_ERR(rtc->rtc);
  527. goto err;
  528. }
  529. /* handle periodic and alarm irqs */
  530. ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
  531. dev_name(&rtc->rtc->dev), rtc);
  532. if (ret)
  533. goto err;
  534. if (rtc->irq_timer != rtc->irq_alarm) {
  535. ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
  536. dev_name(&rtc->rtc->dev), rtc);
  537. if (ret)
  538. goto err;
  539. }
  540. if (rtc->is_pmic_controller) {
  541. if (!pm_power_off) {
  542. omap_rtc_power_off_rtc = rtc;
  543. pm_power_off = omap_rtc_power_off;
  544. }
  545. }
  546. return 0;
  547. err:
  548. device_init_wakeup(&pdev->dev, false);
  549. rtc->type->lock(rtc);
  550. pm_runtime_put_sync(&pdev->dev);
  551. pm_runtime_disable(&pdev->dev);
  552. return ret;
  553. }
  554. static int __exit omap_rtc_remove(struct platform_device *pdev)
  555. {
  556. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  557. if (pm_power_off == omap_rtc_power_off &&
  558. omap_rtc_power_off_rtc == rtc) {
  559. pm_power_off = NULL;
  560. omap_rtc_power_off_rtc = NULL;
  561. }
  562. device_init_wakeup(&pdev->dev, 0);
  563. rtc->type->unlock(rtc);
  564. /* leave rtc running, but disable irqs */
  565. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  566. rtc->type->lock(rtc);
  567. /* Disable the clock/module */
  568. pm_runtime_put_sync(&pdev->dev);
  569. pm_runtime_disable(&pdev->dev);
  570. return 0;
  571. }
  572. #ifdef CONFIG_PM_SLEEP
  573. static int omap_rtc_suspend(struct device *dev)
  574. {
  575. struct omap_rtc *rtc = dev_get_drvdata(dev);
  576. rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  577. rtc->type->unlock(rtc);
  578. /*
  579. * FIXME: the RTC alarm is not currently acting as a wakeup event
  580. * source on some platforms, and in fact this enable() call is just
  581. * saving a flag that's never used...
  582. */
  583. if (device_may_wakeup(dev))
  584. enable_irq_wake(rtc->irq_alarm);
  585. else
  586. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  587. rtc->type->lock(rtc);
  588. /* Disable the clock/module */
  589. pm_runtime_put_sync(dev);
  590. return 0;
  591. }
  592. static int omap_rtc_resume(struct device *dev)
  593. {
  594. struct omap_rtc *rtc = dev_get_drvdata(dev);
  595. /* Enable the clock/module so that we can access the registers */
  596. pm_runtime_get_sync(dev);
  597. rtc->type->unlock(rtc);
  598. if (device_may_wakeup(dev))
  599. disable_irq_wake(rtc->irq_alarm);
  600. else
  601. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
  602. rtc->type->lock(rtc);
  603. return 0;
  604. }
  605. #endif
  606. static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
  607. static void omap_rtc_shutdown(struct platform_device *pdev)
  608. {
  609. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  610. u8 mask;
  611. /*
  612. * Keep the ALARM interrupt enabled to allow the system to power up on
  613. * alarm events.
  614. */
  615. rtc->type->unlock(rtc);
  616. mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  617. mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
  618. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
  619. rtc->type->lock(rtc);
  620. }
  621. static struct platform_driver omap_rtc_driver = {
  622. .probe = omap_rtc_probe,
  623. .remove = __exit_p(omap_rtc_remove),
  624. .shutdown = omap_rtc_shutdown,
  625. .driver = {
  626. .name = "omap_rtc",
  627. .pm = &omap_rtc_pm_ops,
  628. .of_match_table = omap_rtc_of_match,
  629. },
  630. .id_table = omap_rtc_id_table,
  631. };
  632. module_platform_driver(omap_rtc_driver);
  633. MODULE_ALIAS("platform:omap_rtc");
  634. MODULE_AUTHOR("George G. Davis (and others)");
  635. MODULE_LICENSE("GPL");