rtc-omap.c 26 KB

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