rtc-omap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. * Called with local interrupts disabled.
  351. */
  352. static void omap_rtc_power_off(void)
  353. {
  354. struct omap_rtc *rtc = omap_rtc_power_off_rtc;
  355. struct rtc_time tm;
  356. unsigned long now;
  357. int seconds;
  358. u32 val;
  359. rtc->type->unlock(rtc);
  360. /* enable pmic_power_en control */
  361. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  362. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
  363. again:
  364. /* set alarm one second from now */
  365. omap_rtc_read_time_raw(rtc, &tm);
  366. seconds = tm.tm_sec;
  367. bcd2tm(&tm);
  368. rtc_tm_to_time(&tm, &now);
  369. rtc_time_to_tm(now + 1, &tm);
  370. if (tm2bcd(&tm) < 0) {
  371. dev_err(&rtc->rtc->dev, "power off failed\n");
  372. rtc->type->lock(rtc);
  373. return;
  374. }
  375. rtc_wait_not_busy(rtc);
  376. rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
  377. rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
  378. rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
  379. rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
  380. rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
  381. rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
  382. /*
  383. * enable ALARM2 interrupt
  384. *
  385. * NOTE: this fails on AM3352 if rtc_write (writeb) is used
  386. */
  387. val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  388. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
  389. val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
  390. /* Retry in case roll over happened before alarm was armed. */
  391. if (rtc_read(rtc, OMAP_RTC_SECONDS_REG) != seconds) {
  392. val = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  393. if (!(val & OMAP_RTC_STATUS_ALARM2))
  394. goto again;
  395. }
  396. rtc->type->lock(rtc);
  397. /*
  398. * Wait for alarm to trigger (within one second) and external PMIC to
  399. * power off the system. Add a 500 ms margin for external latencies
  400. * (e.g. debounce circuits).
  401. */
  402. mdelay(1500);
  403. }
  404. static const struct rtc_class_ops omap_rtc_ops = {
  405. .read_time = omap_rtc_read_time,
  406. .set_time = omap_rtc_set_time,
  407. .read_alarm = omap_rtc_read_alarm,
  408. .set_alarm = omap_rtc_set_alarm,
  409. .alarm_irq_enable = omap_rtc_alarm_irq_enable,
  410. };
  411. static const struct omap_rtc_device_type omap_rtc_default_type = {
  412. .has_power_up_reset = true,
  413. .lock = default_rtc_lock,
  414. .unlock = default_rtc_unlock,
  415. };
  416. static const struct omap_rtc_device_type omap_rtc_am3352_type = {
  417. .has_32kclk_en = true,
  418. .has_irqwakeen = true,
  419. .has_pmic_mode = true,
  420. .lock = am3352_rtc_lock,
  421. .unlock = am3352_rtc_unlock,
  422. };
  423. static const struct omap_rtc_device_type omap_rtc_da830_type = {
  424. .lock = am3352_rtc_lock,
  425. .unlock = am3352_rtc_unlock,
  426. };
  427. static const struct platform_device_id omap_rtc_id_table[] = {
  428. {
  429. .name = "omap_rtc",
  430. .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
  431. }, {
  432. .name = "am3352-rtc",
  433. .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
  434. }, {
  435. .name = "da830-rtc",
  436. .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
  437. }, {
  438. /* sentinel */
  439. }
  440. };
  441. MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
  442. static const struct of_device_id omap_rtc_of_match[] = {
  443. {
  444. .compatible = "ti,am3352-rtc",
  445. .data = &omap_rtc_am3352_type,
  446. }, {
  447. .compatible = "ti,da830-rtc",
  448. .data = &omap_rtc_da830_type,
  449. }, {
  450. /* sentinel */
  451. }
  452. };
  453. MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
  454. static const struct pinctrl_pin_desc rtc_pins_desc[] = {
  455. PINCTRL_PIN(0, "ext_wakeup0"),
  456. PINCTRL_PIN(1, "ext_wakeup1"),
  457. PINCTRL_PIN(2, "ext_wakeup2"),
  458. PINCTRL_PIN(3, "ext_wakeup3"),
  459. };
  460. static int rtc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  461. {
  462. return 0;
  463. }
  464. static const char *rtc_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  465. unsigned int group)
  466. {
  467. return NULL;
  468. }
  469. static const struct pinctrl_ops rtc_pinctrl_ops = {
  470. .get_groups_count = rtc_pinctrl_get_groups_count,
  471. .get_group_name = rtc_pinctrl_get_group_name,
  472. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  473. .dt_free_map = pinconf_generic_dt_free_map,
  474. };
  475. enum rtc_pin_config_param {
  476. PIN_CONFIG_ACTIVE_HIGH = PIN_CONFIG_END + 1,
  477. };
  478. static const struct pinconf_generic_params rtc_params[] = {
  479. {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
  480. };
  481. #ifdef CONFIG_DEBUG_FS
  482. static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
  483. PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
  484. };
  485. #endif
  486. static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
  487. unsigned int pin, unsigned long *config)
  488. {
  489. struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
  490. unsigned int param = pinconf_to_config_param(*config);
  491. u32 val;
  492. u16 arg = 0;
  493. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  494. switch (param) {
  495. case PIN_CONFIG_INPUT_ENABLE:
  496. if (!(val & OMAP_RTC_PMIC_EXT_WKUP_EN(pin)))
  497. return -EINVAL;
  498. break;
  499. case PIN_CONFIG_ACTIVE_HIGH:
  500. if (val & OMAP_RTC_PMIC_EXT_WKUP_POL(pin))
  501. return -EINVAL;
  502. break;
  503. default:
  504. return -ENOTSUPP;
  505. };
  506. *config = pinconf_to_config_packed(param, arg);
  507. return 0;
  508. }
  509. static int rtc_pinconf_set(struct pinctrl_dev *pctldev,
  510. unsigned int pin, unsigned long *configs,
  511. unsigned int num_configs)
  512. {
  513. struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
  514. u32 val;
  515. unsigned int param;
  516. u32 param_val;
  517. int i;
  518. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  519. /* active low by default */
  520. val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  521. for (i = 0; i < num_configs; i++) {
  522. param = pinconf_to_config_param(configs[i]);
  523. param_val = pinconf_to_config_argument(configs[i]);
  524. switch (param) {
  525. case PIN_CONFIG_INPUT_ENABLE:
  526. if (param_val)
  527. val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  528. else
  529. val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  530. break;
  531. case PIN_CONFIG_ACTIVE_HIGH:
  532. val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  533. break;
  534. default:
  535. dev_err(&rtc->rtc->dev, "Property %u not supported\n",
  536. param);
  537. return -ENOTSUPP;
  538. }
  539. }
  540. rtc->type->unlock(rtc);
  541. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
  542. rtc->type->lock(rtc);
  543. return 0;
  544. }
  545. static const struct pinconf_ops rtc_pinconf_ops = {
  546. .is_generic = true,
  547. .pin_config_get = rtc_pinconf_get,
  548. .pin_config_set = rtc_pinconf_set,
  549. };
  550. static struct pinctrl_desc rtc_pinctrl_desc = {
  551. .pins = rtc_pins_desc,
  552. .npins = ARRAY_SIZE(rtc_pins_desc),
  553. .pctlops = &rtc_pinctrl_ops,
  554. .confops = &rtc_pinconf_ops,
  555. .custom_params = rtc_params,
  556. .num_custom_params = ARRAY_SIZE(rtc_params),
  557. #ifdef CONFIG_DEBUG_FS
  558. .custom_conf_items = rtc_conf_items,
  559. #endif
  560. .owner = THIS_MODULE,
  561. };
  562. static int omap_rtc_scratch_read(void *priv, unsigned int offset, void *_val,
  563. size_t bytes)
  564. {
  565. struct omap_rtc *rtc = priv;
  566. u32 *val = _val;
  567. int i;
  568. for (i = 0; i < bytes / 4; i++)
  569. val[i] = rtc_readl(rtc,
  570. OMAP_RTC_SCRATCH0_REG + offset + (i * 4));
  571. return 0;
  572. }
  573. static int omap_rtc_scratch_write(void *priv, unsigned int offset, void *_val,
  574. size_t bytes)
  575. {
  576. struct omap_rtc *rtc = priv;
  577. u32 *val = _val;
  578. int i;
  579. rtc->type->unlock(rtc);
  580. for (i = 0; i < bytes / 4; i++)
  581. rtc_writel(rtc,
  582. OMAP_RTC_SCRATCH0_REG + offset + (i * 4), val[i]);
  583. rtc->type->lock(rtc);
  584. return 0;
  585. }
  586. static struct nvmem_config omap_rtc_nvmem_config = {
  587. .name = "omap_rtc_scratch",
  588. .word_size = 4,
  589. .stride = 4,
  590. .size = OMAP_RTC_KICK0_REG - OMAP_RTC_SCRATCH0_REG,
  591. .reg_read = omap_rtc_scratch_read,
  592. .reg_write = omap_rtc_scratch_write,
  593. };
  594. static int omap_rtc_probe(struct platform_device *pdev)
  595. {
  596. struct omap_rtc *rtc;
  597. struct resource *res;
  598. u8 reg, mask, new_ctrl;
  599. const struct platform_device_id *id_entry;
  600. const struct of_device_id *of_id;
  601. int ret;
  602. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  603. if (!rtc)
  604. return -ENOMEM;
  605. of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
  606. if (of_id) {
  607. rtc->type = of_id->data;
  608. rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
  609. of_device_is_system_power_controller(pdev->dev.of_node);
  610. } else {
  611. id_entry = platform_get_device_id(pdev);
  612. rtc->type = (void *)id_entry->driver_data;
  613. }
  614. rtc->irq_timer = platform_get_irq(pdev, 0);
  615. if (rtc->irq_timer <= 0)
  616. return -ENOENT;
  617. rtc->irq_alarm = platform_get_irq(pdev, 1);
  618. if (rtc->irq_alarm <= 0)
  619. return -ENOENT;
  620. rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
  621. if (!IS_ERR(rtc->clk))
  622. rtc->has_ext_clk = true;
  623. else
  624. rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
  625. if (!IS_ERR(rtc->clk))
  626. clk_prepare_enable(rtc->clk);
  627. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. rtc->base = devm_ioremap_resource(&pdev->dev, res);
  629. if (IS_ERR(rtc->base)) {
  630. clk_disable_unprepare(rtc->clk);
  631. return PTR_ERR(rtc->base);
  632. }
  633. platform_set_drvdata(pdev, rtc);
  634. /* Enable the clock/module so that we can access the registers */
  635. pm_runtime_enable(&pdev->dev);
  636. pm_runtime_get_sync(&pdev->dev);
  637. rtc->type->unlock(rtc);
  638. /*
  639. * disable interrupts
  640. *
  641. * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
  642. */
  643. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  644. /* enable RTC functional clock */
  645. if (rtc->type->has_32kclk_en) {
  646. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  647. rtc_writel(rtc, OMAP_RTC_OSC_REG,
  648. reg | OMAP_RTC_OSC_32KCLK_EN);
  649. }
  650. /* clear old status */
  651. reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  652. mask = OMAP_RTC_STATUS_ALARM;
  653. if (rtc->type->has_pmic_mode)
  654. mask |= OMAP_RTC_STATUS_ALARM2;
  655. if (rtc->type->has_power_up_reset) {
  656. mask |= OMAP_RTC_STATUS_POWER_UP;
  657. if (reg & OMAP_RTC_STATUS_POWER_UP)
  658. dev_info(&pdev->dev, "RTC power up reset detected\n");
  659. }
  660. if (reg & mask)
  661. rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
  662. /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
  663. reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
  664. if (reg & OMAP_RTC_CTRL_STOP)
  665. dev_info(&pdev->dev, "already running\n");
  666. /* force to 24 hour mode */
  667. new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
  668. new_ctrl |= OMAP_RTC_CTRL_STOP;
  669. /*
  670. * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
  671. *
  672. * - Device wake-up capability setting should come through chip
  673. * init logic. OMAP1 boards should initialize the "wakeup capable"
  674. * flag in the platform device if the board is wired right for
  675. * being woken up by RTC alarm. For OMAP-L138, this capability
  676. * is built into the SoC by the "Deep Sleep" capability.
  677. *
  678. * - Boards wired so RTC_ON_nOFF is used as the reset signal,
  679. * rather than nPWRON_RESET, should forcibly enable split
  680. * power mode. (Some chip errata report that RTC_CTRL_SPLIT
  681. * is write-only, and always reads as zero...)
  682. */
  683. if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
  684. dev_info(&pdev->dev, "split power mode\n");
  685. if (reg != new_ctrl)
  686. rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
  687. /*
  688. * If we have the external clock then switch to it so we can keep
  689. * ticking across suspend.
  690. */
  691. if (rtc->has_ext_clk) {
  692. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  693. reg &= ~OMAP_RTC_OSC_OSC32K_GZ_DISABLE;
  694. reg |= OMAP_RTC_OSC_32KCLK_EN | OMAP_RTC_OSC_SEL_32KCLK_SRC;
  695. rtc_writel(rtc, OMAP_RTC_OSC_REG, reg);
  696. }
  697. rtc->type->lock(rtc);
  698. device_init_wakeup(&pdev->dev, true);
  699. rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  700. if (IS_ERR(rtc->rtc)) {
  701. ret = PTR_ERR(rtc->rtc);
  702. goto err;
  703. }
  704. rtc->rtc->ops = &omap_rtc_ops;
  705. omap_rtc_nvmem_config.priv = rtc;
  706. /* handle periodic and alarm irqs */
  707. ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
  708. dev_name(&rtc->rtc->dev), rtc);
  709. if (ret)
  710. goto err;
  711. if (rtc->irq_timer != rtc->irq_alarm) {
  712. ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
  713. dev_name(&rtc->rtc->dev), rtc);
  714. if (ret)
  715. goto err;
  716. }
  717. /* Support ext_wakeup pinconf */
  718. rtc_pinctrl_desc.name = dev_name(&pdev->dev);
  719. rtc->pctldev = pinctrl_register(&rtc_pinctrl_desc, &pdev->dev, rtc);
  720. if (IS_ERR(rtc->pctldev)) {
  721. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  722. ret = PTR_ERR(rtc->pctldev);
  723. goto err;
  724. }
  725. ret = rtc_register_device(rtc->rtc);
  726. if (ret)
  727. goto err_deregister_pinctrl;
  728. rtc_nvmem_register(rtc->rtc, &omap_rtc_nvmem_config);
  729. if (rtc->is_pmic_controller) {
  730. if (!pm_power_off) {
  731. omap_rtc_power_off_rtc = rtc;
  732. pm_power_off = omap_rtc_power_off;
  733. }
  734. }
  735. return 0;
  736. err_deregister_pinctrl:
  737. pinctrl_unregister(rtc->pctldev);
  738. err:
  739. clk_disable_unprepare(rtc->clk);
  740. device_init_wakeup(&pdev->dev, false);
  741. rtc->type->lock(rtc);
  742. pm_runtime_put_sync(&pdev->dev);
  743. pm_runtime_disable(&pdev->dev);
  744. return ret;
  745. }
  746. static int omap_rtc_remove(struct platform_device *pdev)
  747. {
  748. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  749. u8 reg;
  750. if (pm_power_off == omap_rtc_power_off &&
  751. omap_rtc_power_off_rtc == rtc) {
  752. pm_power_off = NULL;
  753. omap_rtc_power_off_rtc = NULL;
  754. }
  755. device_init_wakeup(&pdev->dev, 0);
  756. if (!IS_ERR(rtc->clk))
  757. clk_disable_unprepare(rtc->clk);
  758. rtc->type->unlock(rtc);
  759. /* leave rtc running, but disable irqs */
  760. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  761. if (rtc->has_ext_clk) {
  762. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  763. reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
  764. rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
  765. }
  766. rtc->type->lock(rtc);
  767. /* Disable the clock/module */
  768. pm_runtime_put_sync(&pdev->dev);
  769. pm_runtime_disable(&pdev->dev);
  770. /* Remove ext_wakeup pinconf */
  771. pinctrl_unregister(rtc->pctldev);
  772. return 0;
  773. }
  774. static int __maybe_unused omap_rtc_suspend(struct device *dev)
  775. {
  776. struct omap_rtc *rtc = dev_get_drvdata(dev);
  777. rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  778. rtc->type->unlock(rtc);
  779. /*
  780. * FIXME: the RTC alarm is not currently acting as a wakeup event
  781. * source on some platforms, and in fact this enable() call is just
  782. * saving a flag that's never used...
  783. */
  784. if (device_may_wakeup(dev))
  785. enable_irq_wake(rtc->irq_alarm);
  786. else
  787. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  788. rtc->type->lock(rtc);
  789. rtc->is_suspending = true;
  790. return 0;
  791. }
  792. static int __maybe_unused omap_rtc_resume(struct device *dev)
  793. {
  794. struct omap_rtc *rtc = dev_get_drvdata(dev);
  795. rtc->type->unlock(rtc);
  796. if (device_may_wakeup(dev))
  797. disable_irq_wake(rtc->irq_alarm);
  798. else
  799. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
  800. rtc->type->lock(rtc);
  801. rtc->is_suspending = false;
  802. return 0;
  803. }
  804. static int __maybe_unused omap_rtc_runtime_suspend(struct device *dev)
  805. {
  806. struct omap_rtc *rtc = dev_get_drvdata(dev);
  807. if (rtc->is_suspending && !rtc->has_ext_clk)
  808. return -EBUSY;
  809. return 0;
  810. }
  811. static const struct dev_pm_ops omap_rtc_pm_ops = {
  812. SET_SYSTEM_SLEEP_PM_OPS(omap_rtc_suspend, omap_rtc_resume)
  813. SET_RUNTIME_PM_OPS(omap_rtc_runtime_suspend, NULL, NULL)
  814. };
  815. static void omap_rtc_shutdown(struct platform_device *pdev)
  816. {
  817. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  818. u8 mask;
  819. /*
  820. * Keep the ALARM interrupt enabled to allow the system to power up on
  821. * alarm events.
  822. */
  823. rtc->type->unlock(rtc);
  824. mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  825. mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
  826. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
  827. rtc->type->lock(rtc);
  828. }
  829. static struct platform_driver omap_rtc_driver = {
  830. .probe = omap_rtc_probe,
  831. .remove = omap_rtc_remove,
  832. .shutdown = omap_rtc_shutdown,
  833. .driver = {
  834. .name = "omap_rtc",
  835. .pm = &omap_rtc_pm_ops,
  836. .of_match_table = omap_rtc_of_match,
  837. },
  838. .id_table = omap_rtc_id_table,
  839. };
  840. module_platform_driver(omap_rtc_driver);
  841. MODULE_ALIAS("platform:omap_rtc");
  842. MODULE_AUTHOR("George G. Davis (and others)");
  843. MODULE_LICENSE("GPL");