rtc-omap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. enum rtc_pin_config_param {
  473. PIN_CONFIG_ACTIVE_HIGH = PIN_CONFIG_END + 1,
  474. };
  475. static const struct pinconf_generic_params rtc_params[] = {
  476. {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
  477. };
  478. #ifdef CONFIG_DEBUG_FS
  479. static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
  480. PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
  481. };
  482. #endif
  483. static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
  484. unsigned int pin, unsigned long *config)
  485. {
  486. struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
  487. unsigned int param = pinconf_to_config_param(*config);
  488. u32 val;
  489. u16 arg = 0;
  490. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  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. u32 param_val;
  514. int i;
  515. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  516. /* active low by default */
  517. val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  518. for (i = 0; i < num_configs; i++) {
  519. param = pinconf_to_config_param(configs[i]);
  520. param_val = pinconf_to_config_argument(configs[i]);
  521. switch (param) {
  522. case PIN_CONFIG_INPUT_ENABLE:
  523. if (param_val)
  524. val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  525. else
  526. val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  527. break;
  528. case PIN_CONFIG_ACTIVE_HIGH:
  529. val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  530. break;
  531. default:
  532. dev_err(&rtc->rtc->dev, "Property %u not supported\n",
  533. param);
  534. return -ENOTSUPP;
  535. }
  536. }
  537. rtc->type->unlock(rtc);
  538. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
  539. rtc->type->lock(rtc);
  540. return 0;
  541. }
  542. static const struct pinconf_ops rtc_pinconf_ops = {
  543. .is_generic = true,
  544. .pin_config_get = rtc_pinconf_get,
  545. .pin_config_set = rtc_pinconf_set,
  546. };
  547. static struct pinctrl_desc rtc_pinctrl_desc = {
  548. .pins = rtc_pins_desc,
  549. .npins = ARRAY_SIZE(rtc_pins_desc),
  550. .pctlops = &rtc_pinctrl_ops,
  551. .confops = &rtc_pinconf_ops,
  552. .custom_params = rtc_params,
  553. .num_custom_params = ARRAY_SIZE(rtc_params),
  554. #ifdef CONFIG_DEBUG_FS
  555. .custom_conf_items = rtc_conf_items,
  556. #endif
  557. .owner = THIS_MODULE,
  558. };
  559. static int omap_rtc_scratch_read(void *priv, unsigned int offset, void *_val,
  560. size_t bytes)
  561. {
  562. struct omap_rtc *rtc = priv;
  563. u32 *val = _val;
  564. int i;
  565. for (i = 0; i < bytes / 4; i++)
  566. val[i] = rtc_readl(rtc,
  567. OMAP_RTC_SCRATCH0_REG + offset + (i * 4));
  568. return 0;
  569. }
  570. static int omap_rtc_scratch_write(void *priv, unsigned int offset, void *_val,
  571. size_t bytes)
  572. {
  573. struct omap_rtc *rtc = priv;
  574. u32 *val = _val;
  575. int i;
  576. rtc->type->unlock(rtc);
  577. for (i = 0; i < bytes / 4; i++)
  578. rtc_writel(rtc,
  579. OMAP_RTC_SCRATCH0_REG + offset + (i * 4), val[i]);
  580. rtc->type->lock(rtc);
  581. return 0;
  582. }
  583. static struct nvmem_config omap_rtc_nvmem_config = {
  584. .name = "omap_rtc_scratch",
  585. .word_size = 4,
  586. .stride = 4,
  587. .size = OMAP_RTC_KICK0_REG - OMAP_RTC_SCRATCH0_REG,
  588. .reg_read = omap_rtc_scratch_read,
  589. .reg_write = omap_rtc_scratch_write,
  590. };
  591. static int omap_rtc_probe(struct platform_device *pdev)
  592. {
  593. struct omap_rtc *rtc;
  594. struct resource *res;
  595. u8 reg, mask, new_ctrl;
  596. const struct platform_device_id *id_entry;
  597. const struct of_device_id *of_id;
  598. int ret;
  599. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  600. if (!rtc)
  601. return -ENOMEM;
  602. of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
  603. if (of_id) {
  604. rtc->type = of_id->data;
  605. rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
  606. of_property_read_bool(pdev->dev.of_node,
  607. "system-power-controller");
  608. } else {
  609. id_entry = platform_get_device_id(pdev);
  610. rtc->type = (void *)id_entry->driver_data;
  611. }
  612. rtc->irq_timer = platform_get_irq(pdev, 0);
  613. if (rtc->irq_timer <= 0)
  614. return -ENOENT;
  615. rtc->irq_alarm = platform_get_irq(pdev, 1);
  616. if (rtc->irq_alarm <= 0)
  617. return -ENOENT;
  618. rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
  619. if (!IS_ERR(rtc->clk))
  620. rtc->has_ext_clk = true;
  621. else
  622. rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
  623. if (!IS_ERR(rtc->clk))
  624. clk_prepare_enable(rtc->clk);
  625. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  626. rtc->base = devm_ioremap_resource(&pdev->dev, res);
  627. if (IS_ERR(rtc->base)) {
  628. clk_disable_unprepare(rtc->clk);
  629. return PTR_ERR(rtc->base);
  630. }
  631. platform_set_drvdata(pdev, rtc);
  632. /* Enable the clock/module so that we can access the registers */
  633. pm_runtime_enable(&pdev->dev);
  634. pm_runtime_get_sync(&pdev->dev);
  635. rtc->type->unlock(rtc);
  636. /*
  637. * disable interrupts
  638. *
  639. * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
  640. */
  641. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  642. /* enable RTC functional clock */
  643. if (rtc->type->has_32kclk_en) {
  644. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  645. rtc_writel(rtc, OMAP_RTC_OSC_REG,
  646. reg | OMAP_RTC_OSC_32KCLK_EN);
  647. }
  648. /* clear old status */
  649. reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  650. mask = OMAP_RTC_STATUS_ALARM;
  651. if (rtc->type->has_pmic_mode)
  652. mask |= OMAP_RTC_STATUS_ALARM2;
  653. if (rtc->type->has_power_up_reset) {
  654. mask |= OMAP_RTC_STATUS_POWER_UP;
  655. if (reg & OMAP_RTC_STATUS_POWER_UP)
  656. dev_info(&pdev->dev, "RTC power up reset detected\n");
  657. }
  658. if (reg & mask)
  659. rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
  660. /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
  661. reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
  662. if (reg & OMAP_RTC_CTRL_STOP)
  663. dev_info(&pdev->dev, "already running\n");
  664. /* force to 24 hour mode */
  665. new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
  666. new_ctrl |= OMAP_RTC_CTRL_STOP;
  667. /*
  668. * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
  669. *
  670. * - Device wake-up capability setting should come through chip
  671. * init logic. OMAP1 boards should initialize the "wakeup capable"
  672. * flag in the platform device if the board is wired right for
  673. * being woken up by RTC alarm. For OMAP-L138, this capability
  674. * is built into the SoC by the "Deep Sleep" capability.
  675. *
  676. * - Boards wired so RTC_ON_nOFF is used as the reset signal,
  677. * rather than nPWRON_RESET, should forcibly enable split
  678. * power mode. (Some chip errata report that RTC_CTRL_SPLIT
  679. * is write-only, and always reads as zero...)
  680. */
  681. if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
  682. dev_info(&pdev->dev, "split power mode\n");
  683. if (reg != new_ctrl)
  684. rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
  685. /*
  686. * If we have the external clock then switch to it so we can keep
  687. * ticking across suspend.
  688. */
  689. if (rtc->has_ext_clk) {
  690. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  691. reg &= ~OMAP_RTC_OSC_OSC32K_GZ_DISABLE;
  692. reg |= OMAP_RTC_OSC_32KCLK_EN | OMAP_RTC_OSC_SEL_32KCLK_SRC;
  693. rtc_writel(rtc, OMAP_RTC_OSC_REG, reg);
  694. }
  695. rtc->type->lock(rtc);
  696. device_init_wakeup(&pdev->dev, true);
  697. rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  698. if (IS_ERR(rtc->rtc)) {
  699. ret = PTR_ERR(rtc->rtc);
  700. goto err;
  701. }
  702. rtc->rtc->ops = &omap_rtc_ops;
  703. omap_rtc_nvmem_config.priv = rtc;
  704. /* handle periodic and alarm irqs */
  705. ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
  706. dev_name(&rtc->rtc->dev), rtc);
  707. if (ret)
  708. goto err;
  709. if (rtc->irq_timer != rtc->irq_alarm) {
  710. ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
  711. dev_name(&rtc->rtc->dev), rtc);
  712. if (ret)
  713. goto err;
  714. }
  715. /* Support ext_wakeup pinconf */
  716. rtc_pinctrl_desc.name = dev_name(&pdev->dev);
  717. rtc->pctldev = pinctrl_register(&rtc_pinctrl_desc, &pdev->dev, rtc);
  718. if (IS_ERR(rtc->pctldev)) {
  719. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  720. ret = PTR_ERR(rtc->pctldev);
  721. goto err;
  722. }
  723. ret = rtc_register_device(rtc->rtc);
  724. if (ret)
  725. goto err_deregister_pinctrl;
  726. rtc_nvmem_register(rtc->rtc, &omap_rtc_nvmem_config);
  727. if (rtc->is_pmic_controller) {
  728. if (!pm_power_off) {
  729. omap_rtc_power_off_rtc = rtc;
  730. pm_power_off = omap_rtc_power_off;
  731. }
  732. }
  733. return 0;
  734. err_deregister_pinctrl:
  735. pinctrl_unregister(rtc->pctldev);
  736. err:
  737. clk_disable_unprepare(rtc->clk);
  738. device_init_wakeup(&pdev->dev, false);
  739. rtc->type->lock(rtc);
  740. pm_runtime_put_sync(&pdev->dev);
  741. pm_runtime_disable(&pdev->dev);
  742. return ret;
  743. }
  744. static int omap_rtc_remove(struct platform_device *pdev)
  745. {
  746. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  747. u8 reg;
  748. if (pm_power_off == omap_rtc_power_off &&
  749. omap_rtc_power_off_rtc == rtc) {
  750. pm_power_off = NULL;
  751. omap_rtc_power_off_rtc = NULL;
  752. }
  753. device_init_wakeup(&pdev->dev, 0);
  754. if (!IS_ERR(rtc->clk))
  755. clk_disable_unprepare(rtc->clk);
  756. rtc->type->unlock(rtc);
  757. /* leave rtc running, but disable irqs */
  758. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  759. if (rtc->has_ext_clk) {
  760. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  761. reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
  762. rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
  763. }
  764. rtc->type->lock(rtc);
  765. /* Disable the clock/module */
  766. pm_runtime_put_sync(&pdev->dev);
  767. pm_runtime_disable(&pdev->dev);
  768. /* Remove ext_wakeup pinconf */
  769. pinctrl_unregister(rtc->pctldev);
  770. return 0;
  771. }
  772. static int __maybe_unused omap_rtc_suspend(struct device *dev)
  773. {
  774. struct omap_rtc *rtc = dev_get_drvdata(dev);
  775. rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  776. rtc->type->unlock(rtc);
  777. /*
  778. * FIXME: the RTC alarm is not currently acting as a wakeup event
  779. * source on some platforms, and in fact this enable() call is just
  780. * saving a flag that's never used...
  781. */
  782. if (device_may_wakeup(dev))
  783. enable_irq_wake(rtc->irq_alarm);
  784. else
  785. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  786. rtc->type->lock(rtc);
  787. rtc->is_suspending = true;
  788. return 0;
  789. }
  790. static int __maybe_unused omap_rtc_resume(struct device *dev)
  791. {
  792. struct omap_rtc *rtc = dev_get_drvdata(dev);
  793. rtc->type->unlock(rtc);
  794. if (device_may_wakeup(dev))
  795. disable_irq_wake(rtc->irq_alarm);
  796. else
  797. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
  798. rtc->type->lock(rtc);
  799. rtc->is_suspending = false;
  800. return 0;
  801. }
  802. static int __maybe_unused omap_rtc_runtime_suspend(struct device *dev)
  803. {
  804. struct omap_rtc *rtc = dev_get_drvdata(dev);
  805. if (rtc->is_suspending && !rtc->has_ext_clk)
  806. return -EBUSY;
  807. return 0;
  808. }
  809. static const struct dev_pm_ops omap_rtc_pm_ops = {
  810. SET_SYSTEM_SLEEP_PM_OPS(omap_rtc_suspend, omap_rtc_resume)
  811. SET_RUNTIME_PM_OPS(omap_rtc_runtime_suspend, NULL, NULL)
  812. };
  813. static void omap_rtc_shutdown(struct platform_device *pdev)
  814. {
  815. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  816. u8 mask;
  817. /*
  818. * Keep the ALARM interrupt enabled to allow the system to power up on
  819. * alarm events.
  820. */
  821. rtc->type->unlock(rtc);
  822. mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  823. mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
  824. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
  825. rtc->type->lock(rtc);
  826. }
  827. static struct platform_driver omap_rtc_driver = {
  828. .probe = omap_rtc_probe,
  829. .remove = omap_rtc_remove,
  830. .shutdown = omap_rtc_shutdown,
  831. .driver = {
  832. .name = "omap_rtc",
  833. .pm = &omap_rtc_pm_ops,
  834. .of_match_table = omap_rtc_of_match,
  835. },
  836. .id_table = omap_rtc_id_table,
  837. };
  838. module_platform_driver(omap_rtc_driver);
  839. MODULE_ALIAS("platform:omap_rtc");
  840. MODULE_AUTHOR("George G. Davis (and others)");
  841. MODULE_LICENSE("GPL");