rtc-omap.c 21 KB

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