rtc-omap.c 28 KB

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