rtc-omap.c 25 KB

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