rtc-omap.c 21 KB

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