rtc-omap.c 25 KB

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