rtc-omap.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * TI OMAP Real Time Clock interface for Linux
  4. *
  5. * Copyright (C) 2003 MontaVista Software, Inc.
  6. * Author: George G. Davis <[email protected]> or <[email protected]>
  7. *
  8. * Copyright (C) 2006 David Brownell (new RTC framework)
  9. * Copyright (C) 2014 Johan Hovold <[email protected]>
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinconf-generic.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/rtc.h>
  27. #include <linux/rtc/rtc-omap.h>
  28. /*
  29. * The OMAP RTC is a year/month/day/hours/minutes/seconds BCD clock
  30. * with century-range alarm matching, driven by the 32kHz clock.
  31. *
  32. * The main user-visible ways it differs from PC RTCs are by omitting
  33. * "don't care" alarm fields and sub-second periodic IRQs, and having
  34. * an autoadjust mechanism to calibrate to the true oscillator rate.
  35. *
  36. * Board-specific wiring options include using split power mode with
  37. * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
  38. * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
  39. * low power modes) for OMAP1 boards (OMAP-L138 has this built into
  40. * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
  41. */
  42. /* RTC registers */
  43. #define OMAP_RTC_SECONDS_REG 0x00
  44. #define OMAP_RTC_MINUTES_REG 0x04
  45. #define OMAP_RTC_HOURS_REG 0x08
  46. #define OMAP_RTC_DAYS_REG 0x0C
  47. #define OMAP_RTC_MONTHS_REG 0x10
  48. #define OMAP_RTC_YEARS_REG 0x14
  49. #define OMAP_RTC_WEEKS_REG 0x18
  50. #define OMAP_RTC_ALARM_SECONDS_REG 0x20
  51. #define OMAP_RTC_ALARM_MINUTES_REG 0x24
  52. #define OMAP_RTC_ALARM_HOURS_REG 0x28
  53. #define OMAP_RTC_ALARM_DAYS_REG 0x2c
  54. #define OMAP_RTC_ALARM_MONTHS_REG 0x30
  55. #define OMAP_RTC_ALARM_YEARS_REG 0x34
  56. #define OMAP_RTC_CTRL_REG 0x40
  57. #define OMAP_RTC_STATUS_REG 0x44
  58. #define OMAP_RTC_INTERRUPTS_REG 0x48
  59. #define OMAP_RTC_COMP_LSB_REG 0x4c
  60. #define OMAP_RTC_COMP_MSB_REG 0x50
  61. #define OMAP_RTC_OSC_REG 0x54
  62. #define OMAP_RTC_SCRATCH0_REG 0x60
  63. #define OMAP_RTC_SCRATCH1_REG 0x64
  64. #define OMAP_RTC_SCRATCH2_REG 0x68
  65. #define OMAP_RTC_KICK0_REG 0x6c
  66. #define OMAP_RTC_KICK1_REG 0x70
  67. #define OMAP_RTC_IRQWAKEEN 0x7c
  68. #define OMAP_RTC_ALARM2_SECONDS_REG 0x80
  69. #define OMAP_RTC_ALARM2_MINUTES_REG 0x84
  70. #define OMAP_RTC_ALARM2_HOURS_REG 0x88
  71. #define OMAP_RTC_ALARM2_DAYS_REG 0x8c
  72. #define OMAP_RTC_ALARM2_MONTHS_REG 0x90
  73. #define OMAP_RTC_ALARM2_YEARS_REG 0x94
  74. #define OMAP_RTC_PMIC_REG 0x98
  75. /* OMAP_RTC_CTRL_REG bit fields: */
  76. #define OMAP_RTC_CTRL_SPLIT BIT(7)
  77. #define OMAP_RTC_CTRL_DISABLE BIT(6)
  78. #define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
  79. #define OMAP_RTC_CTRL_TEST BIT(4)
  80. #define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
  81. #define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
  82. #define OMAP_RTC_CTRL_ROUND_30S BIT(1)
  83. #define OMAP_RTC_CTRL_STOP BIT(0)
  84. /* OMAP_RTC_STATUS_REG bit fields: */
  85. #define OMAP_RTC_STATUS_POWER_UP BIT(7)
  86. #define OMAP_RTC_STATUS_ALARM2 BIT(7)
  87. #define OMAP_RTC_STATUS_ALARM BIT(6)
  88. #define OMAP_RTC_STATUS_1D_EVENT BIT(5)
  89. #define OMAP_RTC_STATUS_1H_EVENT BIT(4)
  90. #define OMAP_RTC_STATUS_1M_EVENT BIT(3)
  91. #define OMAP_RTC_STATUS_1S_EVENT BIT(2)
  92. #define OMAP_RTC_STATUS_RUN BIT(1)
  93. #define OMAP_RTC_STATUS_BUSY BIT(0)
  94. /* OMAP_RTC_INTERRUPTS_REG bit fields: */
  95. #define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
  96. #define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
  97. #define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
  98. /* OMAP_RTC_OSC_REG bit fields: */
  99. #define OMAP_RTC_OSC_32KCLK_EN BIT(6)
  100. #define OMAP_RTC_OSC_SEL_32KCLK_SRC BIT(3)
  101. #define OMAP_RTC_OSC_OSC32K_GZ_DISABLE BIT(4)
  102. /* OMAP_RTC_IRQWAKEEN bit fields: */
  103. #define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
  104. /* OMAP_RTC_PMIC bit fields: */
  105. #define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
  106. #define OMAP_RTC_PMIC_EXT_WKUP_EN(x) BIT(x)
  107. #define OMAP_RTC_PMIC_EXT_WKUP_POL(x) BIT(4 + x)
  108. /* OMAP_RTC_KICKER values */
  109. #define KICK0_VALUE 0x83e70b13
  110. #define KICK1_VALUE 0x95a4f1e0
  111. struct omap_rtc;
  112. struct omap_rtc_device_type {
  113. bool has_32kclk_en;
  114. bool has_irqwakeen;
  115. bool has_pmic_mode;
  116. bool has_power_up_reset;
  117. void (*lock)(struct omap_rtc *rtc);
  118. void (*unlock)(struct omap_rtc *rtc);
  119. };
  120. struct omap_rtc {
  121. struct rtc_device *rtc;
  122. void __iomem *base;
  123. struct clk *clk;
  124. int irq_alarm;
  125. int irq_timer;
  126. u8 interrupts_reg;
  127. bool is_pmic_controller;
  128. bool has_ext_clk;
  129. bool is_suspending;
  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 void tm2bcd(struct rtc_time *tm)
  229. {
  230. tm->tm_sec = bin2bcd(tm->tm_sec);
  231. tm->tm_min = bin2bcd(tm->tm_min);
  232. tm->tm_hour = bin2bcd(tm->tm_hour);
  233. tm->tm_mday = bin2bcd(tm->tm_mday);
  234. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  235. tm->tm_year = bin2bcd(tm->tm_year - 100);
  236. }
  237. static void bcd2tm(struct rtc_time *tm)
  238. {
  239. tm->tm_sec = bcd2bin(tm->tm_sec);
  240. tm->tm_min = bcd2bin(tm->tm_min);
  241. tm->tm_hour = bcd2bin(tm->tm_hour);
  242. tm->tm_mday = bcd2bin(tm->tm_mday);
  243. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  244. /* epoch == 1900 */
  245. tm->tm_year = bcd2bin(tm->tm_year) + 100;
  246. }
  247. static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
  248. {
  249. tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
  250. tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
  251. tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
  252. tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
  253. tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
  254. tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
  255. }
  256. static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
  257. {
  258. struct omap_rtc *rtc = dev_get_drvdata(dev);
  259. /* we don't report wday/yday/isdst ... */
  260. local_irq_disable();
  261. rtc_wait_not_busy(rtc);
  262. omap_rtc_read_time_raw(rtc, tm);
  263. local_irq_enable();
  264. bcd2tm(tm);
  265. return 0;
  266. }
  267. static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
  268. {
  269. struct omap_rtc *rtc = dev_get_drvdata(dev);
  270. tm2bcd(tm);
  271. local_irq_disable();
  272. rtc_wait_not_busy(rtc);
  273. rtc->type->unlock(rtc);
  274. rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
  275. rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
  276. rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
  277. rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
  278. rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
  279. rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
  280. rtc->type->lock(rtc);
  281. local_irq_enable();
  282. return 0;
  283. }
  284. static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  285. {
  286. struct omap_rtc *rtc = dev_get_drvdata(dev);
  287. u8 interrupts;
  288. local_irq_disable();
  289. rtc_wait_not_busy(rtc);
  290. alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
  291. alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
  292. alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
  293. alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
  294. alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
  295. alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
  296. local_irq_enable();
  297. bcd2tm(&alm->time);
  298. interrupts = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  299. alm->enabled = !!(interrupts & OMAP_RTC_INTERRUPTS_IT_ALARM);
  300. return 0;
  301. }
  302. static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  303. {
  304. struct omap_rtc *rtc = dev_get_drvdata(dev);
  305. u8 reg, irqwake_reg = 0;
  306. tm2bcd(&alm->time);
  307. local_irq_disable();
  308. rtc_wait_not_busy(rtc);
  309. rtc->type->unlock(rtc);
  310. rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
  311. rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
  312. rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
  313. rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
  314. rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
  315. rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
  316. reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  317. if (rtc->type->has_irqwakeen)
  318. irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
  319. if (alm->enabled) {
  320. reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
  321. irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  322. } else {
  323. reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
  324. irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
  325. }
  326. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
  327. if (rtc->type->has_irqwakeen)
  328. rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
  329. rtc->type->lock(rtc);
  330. local_irq_enable();
  331. return 0;
  332. }
  333. static struct omap_rtc *omap_rtc_power_off_rtc;
  334. /**
  335. * omap_rtc_power_off_program: Set the pmic power off sequence. The RTC
  336. * generates pmic_pwr_enable control, which can be used to control an external
  337. * PMIC.
  338. */
  339. int omap_rtc_power_off_program(struct device *dev)
  340. {
  341. struct omap_rtc *rtc = omap_rtc_power_off_rtc;
  342. struct rtc_time tm;
  343. unsigned long now;
  344. int seconds;
  345. u32 val;
  346. rtc->type->unlock(rtc);
  347. /* enable pmic_power_en control */
  348. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  349. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
  350. again:
  351. /* Clear any existing ALARM2 event */
  352. rtc_writel(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM2);
  353. /* set alarm one second from now */
  354. omap_rtc_read_time_raw(rtc, &tm);
  355. seconds = tm.tm_sec;
  356. bcd2tm(&tm);
  357. now = rtc_tm_to_time64(&tm);
  358. rtc_time64_to_tm(now + 1, &tm);
  359. tm2bcd(&tm);
  360. rtc_wait_not_busy(rtc);
  361. rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
  362. rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
  363. rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
  364. rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
  365. rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
  366. rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
  367. /*
  368. * enable ALARM2 interrupt
  369. *
  370. * NOTE: this fails on AM3352 if rtc_write (writeb) is used
  371. */
  372. val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  373. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
  374. val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
  375. /* Retry in case roll over happened before alarm was armed. */
  376. if (rtc_read(rtc, OMAP_RTC_SECONDS_REG) != seconds) {
  377. val = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  378. if (!(val & OMAP_RTC_STATUS_ALARM2))
  379. goto again;
  380. }
  381. rtc->type->lock(rtc);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(omap_rtc_power_off_program);
  385. /*
  386. * omap_rtc_poweroff: RTC-controlled power off
  387. *
  388. * The RTC can be used to control an external PMIC via the pmic_power_en pin,
  389. * which can be configured to transition to OFF on ALARM2 events.
  390. *
  391. * Notes:
  392. * The one-second alarm offset is the shortest offset possible as the alarm
  393. * registers must be set before the next timer update and the offset
  394. * calculation is too heavy for everything to be done within a single access
  395. * period (~15 us).
  396. *
  397. * Called with local interrupts disabled.
  398. */
  399. static void omap_rtc_power_off(void)
  400. {
  401. struct rtc_device *rtc = omap_rtc_power_off_rtc->rtc;
  402. u32 val;
  403. omap_rtc_power_off_program(rtc->dev.parent);
  404. /* Set PMIC power enable and EXT_WAKEUP in case PB power on is used */
  405. omap_rtc_power_off_rtc->type->unlock(omap_rtc_power_off_rtc);
  406. val = rtc_readl(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG);
  407. val |= OMAP_RTC_PMIC_POWER_EN_EN | OMAP_RTC_PMIC_EXT_WKUP_POL(0) |
  408. OMAP_RTC_PMIC_EXT_WKUP_EN(0);
  409. rtc_writel(omap_rtc_power_off_rtc, OMAP_RTC_PMIC_REG, val);
  410. omap_rtc_power_off_rtc->type->lock(omap_rtc_power_off_rtc);
  411. /*
  412. * Wait for alarm to trigger (within one second) and external PMIC to
  413. * power off the system. Add a 500 ms margin for external latencies
  414. * (e.g. debounce circuits).
  415. */
  416. mdelay(1500);
  417. }
  418. static const struct rtc_class_ops omap_rtc_ops = {
  419. .read_time = omap_rtc_read_time,
  420. .set_time = omap_rtc_set_time,
  421. .read_alarm = omap_rtc_read_alarm,
  422. .set_alarm = omap_rtc_set_alarm,
  423. .alarm_irq_enable = omap_rtc_alarm_irq_enable,
  424. };
  425. static const struct omap_rtc_device_type omap_rtc_default_type = {
  426. .has_power_up_reset = true,
  427. .lock = default_rtc_lock,
  428. .unlock = default_rtc_unlock,
  429. };
  430. static const struct omap_rtc_device_type omap_rtc_am3352_type = {
  431. .has_32kclk_en = true,
  432. .has_irqwakeen = true,
  433. .has_pmic_mode = true,
  434. .lock = am3352_rtc_lock,
  435. .unlock = am3352_rtc_unlock,
  436. };
  437. static const struct omap_rtc_device_type omap_rtc_da830_type = {
  438. .lock = am3352_rtc_lock,
  439. .unlock = am3352_rtc_unlock,
  440. };
  441. static const struct platform_device_id omap_rtc_id_table[] = {
  442. {
  443. .name = "omap_rtc",
  444. .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
  445. }, {
  446. .name = "am3352-rtc",
  447. .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
  448. }, {
  449. .name = "da830-rtc",
  450. .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
  451. }, {
  452. /* sentinel */
  453. }
  454. };
  455. MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
  456. static const struct of_device_id omap_rtc_of_match[] = {
  457. {
  458. .compatible = "ti,am3352-rtc",
  459. .data = &omap_rtc_am3352_type,
  460. }, {
  461. .compatible = "ti,da830-rtc",
  462. .data = &omap_rtc_da830_type,
  463. }, {
  464. /* sentinel */
  465. }
  466. };
  467. MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
  468. static const struct pinctrl_pin_desc rtc_pins_desc[] = {
  469. PINCTRL_PIN(0, "ext_wakeup0"),
  470. PINCTRL_PIN(1, "ext_wakeup1"),
  471. PINCTRL_PIN(2, "ext_wakeup2"),
  472. PINCTRL_PIN(3, "ext_wakeup3"),
  473. };
  474. static int rtc_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  475. {
  476. return 0;
  477. }
  478. static const char *rtc_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  479. unsigned int group)
  480. {
  481. return NULL;
  482. }
  483. static const struct pinctrl_ops rtc_pinctrl_ops = {
  484. .get_groups_count = rtc_pinctrl_get_groups_count,
  485. .get_group_name = rtc_pinctrl_get_group_name,
  486. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  487. .dt_free_map = pinconf_generic_dt_free_map,
  488. };
  489. #define PIN_CONFIG_ACTIVE_HIGH (PIN_CONFIG_END + 1)
  490. static const struct pinconf_generic_params rtc_params[] = {
  491. {"ti,active-high", PIN_CONFIG_ACTIVE_HIGH, 0},
  492. };
  493. #ifdef CONFIG_DEBUG_FS
  494. static const struct pin_config_item rtc_conf_items[ARRAY_SIZE(rtc_params)] = {
  495. PCONFDUMP(PIN_CONFIG_ACTIVE_HIGH, "input active high", NULL, false),
  496. };
  497. #endif
  498. static int rtc_pinconf_get(struct pinctrl_dev *pctldev,
  499. unsigned int pin, unsigned long *config)
  500. {
  501. struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
  502. unsigned int param = pinconf_to_config_param(*config);
  503. u32 val;
  504. u16 arg = 0;
  505. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  506. switch (param) {
  507. case PIN_CONFIG_INPUT_ENABLE:
  508. if (!(val & OMAP_RTC_PMIC_EXT_WKUP_EN(pin)))
  509. return -EINVAL;
  510. break;
  511. case PIN_CONFIG_ACTIVE_HIGH:
  512. if (val & OMAP_RTC_PMIC_EXT_WKUP_POL(pin))
  513. return -EINVAL;
  514. break;
  515. default:
  516. return -ENOTSUPP;
  517. }
  518. *config = pinconf_to_config_packed(param, arg);
  519. return 0;
  520. }
  521. static int rtc_pinconf_set(struct pinctrl_dev *pctldev,
  522. unsigned int pin, unsigned long *configs,
  523. unsigned int num_configs)
  524. {
  525. struct omap_rtc *rtc = pinctrl_dev_get_drvdata(pctldev);
  526. u32 val;
  527. unsigned int param;
  528. u32 param_val;
  529. int i;
  530. val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
  531. /* active low by default */
  532. val |= OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  533. for (i = 0; i < num_configs; i++) {
  534. param = pinconf_to_config_param(configs[i]);
  535. param_val = pinconf_to_config_argument(configs[i]);
  536. switch (param) {
  537. case PIN_CONFIG_INPUT_ENABLE:
  538. if (param_val)
  539. val |= OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  540. else
  541. val &= ~OMAP_RTC_PMIC_EXT_WKUP_EN(pin);
  542. break;
  543. case PIN_CONFIG_ACTIVE_HIGH:
  544. val &= ~OMAP_RTC_PMIC_EXT_WKUP_POL(pin);
  545. break;
  546. default:
  547. dev_err(&rtc->rtc->dev, "Property %u not supported\n",
  548. param);
  549. return -ENOTSUPP;
  550. }
  551. }
  552. rtc->type->unlock(rtc);
  553. rtc_writel(rtc, OMAP_RTC_PMIC_REG, val);
  554. rtc->type->lock(rtc);
  555. return 0;
  556. }
  557. static const struct pinconf_ops rtc_pinconf_ops = {
  558. .is_generic = true,
  559. .pin_config_get = rtc_pinconf_get,
  560. .pin_config_set = rtc_pinconf_set,
  561. };
  562. static struct pinctrl_desc rtc_pinctrl_desc = {
  563. .pins = rtc_pins_desc,
  564. .npins = ARRAY_SIZE(rtc_pins_desc),
  565. .pctlops = &rtc_pinctrl_ops,
  566. .confops = &rtc_pinconf_ops,
  567. .custom_params = rtc_params,
  568. .num_custom_params = ARRAY_SIZE(rtc_params),
  569. #ifdef CONFIG_DEBUG_FS
  570. .custom_conf_items = rtc_conf_items,
  571. #endif
  572. .owner = THIS_MODULE,
  573. };
  574. static int omap_rtc_scratch_read(void *priv, unsigned int offset, void *_val,
  575. size_t bytes)
  576. {
  577. struct omap_rtc *rtc = priv;
  578. u32 *val = _val;
  579. int i;
  580. for (i = 0; i < bytes / 4; i++)
  581. val[i] = rtc_readl(rtc,
  582. OMAP_RTC_SCRATCH0_REG + offset + (i * 4));
  583. return 0;
  584. }
  585. static int omap_rtc_scratch_write(void *priv, unsigned int offset, void *_val,
  586. size_t bytes)
  587. {
  588. struct omap_rtc *rtc = priv;
  589. u32 *val = _val;
  590. int i;
  591. rtc->type->unlock(rtc);
  592. for (i = 0; i < bytes / 4; i++)
  593. rtc_writel(rtc,
  594. OMAP_RTC_SCRATCH0_REG + offset + (i * 4), val[i]);
  595. rtc->type->lock(rtc);
  596. return 0;
  597. }
  598. static struct nvmem_config omap_rtc_nvmem_config = {
  599. .name = "omap_rtc_scratch",
  600. .word_size = 4,
  601. .stride = 4,
  602. .size = OMAP_RTC_KICK0_REG - OMAP_RTC_SCRATCH0_REG,
  603. .reg_read = omap_rtc_scratch_read,
  604. .reg_write = omap_rtc_scratch_write,
  605. };
  606. static int omap_rtc_probe(struct platform_device *pdev)
  607. {
  608. struct omap_rtc *rtc;
  609. u8 reg, mask, new_ctrl;
  610. const struct platform_device_id *id_entry;
  611. const struct of_device_id *of_id;
  612. int ret;
  613. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  614. if (!rtc)
  615. return -ENOMEM;
  616. of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
  617. if (of_id) {
  618. rtc->type = of_id->data;
  619. rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
  620. of_device_is_system_power_controller(pdev->dev.of_node);
  621. } else {
  622. id_entry = platform_get_device_id(pdev);
  623. rtc->type = (void *)id_entry->driver_data;
  624. }
  625. rtc->irq_timer = platform_get_irq(pdev, 0);
  626. if (rtc->irq_timer <= 0)
  627. return -ENOENT;
  628. rtc->irq_alarm = platform_get_irq(pdev, 1);
  629. if (rtc->irq_alarm <= 0)
  630. return -ENOENT;
  631. rtc->clk = devm_clk_get(&pdev->dev, "ext-clk");
  632. if (!IS_ERR(rtc->clk))
  633. rtc->has_ext_clk = true;
  634. else
  635. rtc->clk = devm_clk_get(&pdev->dev, "int-clk");
  636. if (!IS_ERR(rtc->clk))
  637. clk_prepare_enable(rtc->clk);
  638. rtc->base = devm_platform_ioremap_resource(pdev, 0);
  639. if (IS_ERR(rtc->base)) {
  640. clk_disable_unprepare(rtc->clk);
  641. return PTR_ERR(rtc->base);
  642. }
  643. platform_set_drvdata(pdev, rtc);
  644. /* Enable the clock/module so that we can access the registers */
  645. pm_runtime_enable(&pdev->dev);
  646. pm_runtime_get_sync(&pdev->dev);
  647. rtc->type->unlock(rtc);
  648. /*
  649. * disable interrupts
  650. *
  651. * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
  652. */
  653. rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  654. /* enable RTC functional clock */
  655. if (rtc->type->has_32kclk_en) {
  656. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  657. rtc_write(rtc, OMAP_RTC_OSC_REG, reg | OMAP_RTC_OSC_32KCLK_EN);
  658. }
  659. /* clear old status */
  660. reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
  661. mask = OMAP_RTC_STATUS_ALARM;
  662. if (rtc->type->has_pmic_mode)
  663. mask |= OMAP_RTC_STATUS_ALARM2;
  664. if (rtc->type->has_power_up_reset) {
  665. mask |= OMAP_RTC_STATUS_POWER_UP;
  666. if (reg & OMAP_RTC_STATUS_POWER_UP)
  667. dev_info(&pdev->dev, "RTC power up reset detected\n");
  668. }
  669. if (reg & mask)
  670. rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
  671. /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
  672. reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
  673. if (reg & OMAP_RTC_CTRL_STOP)
  674. dev_info(&pdev->dev, "already running\n");
  675. /* force to 24 hour mode */
  676. new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT | OMAP_RTC_CTRL_AUTO_COMP);
  677. new_ctrl |= OMAP_RTC_CTRL_STOP;
  678. /*
  679. * BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
  680. *
  681. * - Device wake-up capability setting should come through chip
  682. * init logic. OMAP1 boards should initialize the "wakeup capable"
  683. * flag in the platform device if the board is wired right for
  684. * being woken up by RTC alarm. For OMAP-L138, this capability
  685. * is built into the SoC by the "Deep Sleep" capability.
  686. *
  687. * - Boards wired so RTC_ON_nOFF is used as the reset signal,
  688. * rather than nPWRON_RESET, should forcibly enable split
  689. * power mode. (Some chip errata report that RTC_CTRL_SPLIT
  690. * is write-only, and always reads as zero...)
  691. */
  692. if (new_ctrl & OMAP_RTC_CTRL_SPLIT)
  693. dev_info(&pdev->dev, "split power mode\n");
  694. if (reg != new_ctrl)
  695. rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
  696. /*
  697. * If we have the external clock then switch to it so we can keep
  698. * ticking across suspend.
  699. */
  700. if (rtc->has_ext_clk) {
  701. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  702. reg &= ~OMAP_RTC_OSC_OSC32K_GZ_DISABLE;
  703. reg |= OMAP_RTC_OSC_32KCLK_EN | OMAP_RTC_OSC_SEL_32KCLK_SRC;
  704. rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
  705. }
  706. rtc->type->lock(rtc);
  707. device_init_wakeup(&pdev->dev, true);
  708. rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  709. if (IS_ERR(rtc->rtc)) {
  710. ret = PTR_ERR(rtc->rtc);
  711. goto err;
  712. }
  713. rtc->rtc->ops = &omap_rtc_ops;
  714. rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  715. rtc->rtc->range_max = RTC_TIMESTAMP_END_2099;
  716. omap_rtc_nvmem_config.priv = rtc;
  717. /* handle periodic and alarm irqs */
  718. ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
  719. dev_name(&rtc->rtc->dev), rtc);
  720. if (ret)
  721. goto err;
  722. if (rtc->irq_timer != rtc->irq_alarm) {
  723. ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
  724. dev_name(&rtc->rtc->dev), rtc);
  725. if (ret)
  726. goto err;
  727. }
  728. /* Support ext_wakeup pinconf */
  729. rtc_pinctrl_desc.name = dev_name(&pdev->dev);
  730. rtc->pctldev = devm_pinctrl_register(&pdev->dev, &rtc_pinctrl_desc, rtc);
  731. if (IS_ERR(rtc->pctldev)) {
  732. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  733. ret = PTR_ERR(rtc->pctldev);
  734. goto err;
  735. }
  736. ret = devm_rtc_register_device(rtc->rtc);
  737. if (ret)
  738. goto err;
  739. devm_rtc_nvmem_register(rtc->rtc, &omap_rtc_nvmem_config);
  740. if (rtc->is_pmic_controller) {
  741. if (!pm_power_off) {
  742. omap_rtc_power_off_rtc = rtc;
  743. pm_power_off = omap_rtc_power_off;
  744. }
  745. }
  746. return 0;
  747. err:
  748. clk_disable_unprepare(rtc->clk);
  749. device_init_wakeup(&pdev->dev, false);
  750. rtc->type->lock(rtc);
  751. pm_runtime_put_sync(&pdev->dev);
  752. pm_runtime_disable(&pdev->dev);
  753. return ret;
  754. }
  755. static int omap_rtc_remove(struct platform_device *pdev)
  756. {
  757. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  758. u8 reg;
  759. if (pm_power_off == omap_rtc_power_off &&
  760. omap_rtc_power_off_rtc == rtc) {
  761. pm_power_off = NULL;
  762. omap_rtc_power_off_rtc = NULL;
  763. }
  764. device_init_wakeup(&pdev->dev, 0);
  765. if (!IS_ERR(rtc->clk))
  766. clk_disable_unprepare(rtc->clk);
  767. rtc->type->unlock(rtc);
  768. /* leave rtc running, but disable irqs */
  769. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  770. if (rtc->has_ext_clk) {
  771. reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
  772. reg &= ~OMAP_RTC_OSC_SEL_32KCLK_SRC;
  773. rtc_write(rtc, OMAP_RTC_OSC_REG, reg);
  774. }
  775. rtc->type->lock(rtc);
  776. /* Disable the clock/module */
  777. pm_runtime_put_sync(&pdev->dev);
  778. pm_runtime_disable(&pdev->dev);
  779. return 0;
  780. }
  781. static int __maybe_unused omap_rtc_suspend(struct device *dev)
  782. {
  783. struct omap_rtc *rtc = dev_get_drvdata(dev);
  784. rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  785. rtc->type->unlock(rtc);
  786. /*
  787. * FIXME: the RTC alarm is not currently acting as a wakeup event
  788. * source on some platforms, and in fact this enable() call is just
  789. * saving a flag that's never used...
  790. */
  791. if (device_may_wakeup(dev))
  792. enable_irq_wake(rtc->irq_alarm);
  793. else
  794. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
  795. rtc->type->lock(rtc);
  796. rtc->is_suspending = true;
  797. return 0;
  798. }
  799. static int __maybe_unused omap_rtc_resume(struct device *dev)
  800. {
  801. struct omap_rtc *rtc = dev_get_drvdata(dev);
  802. rtc->type->unlock(rtc);
  803. if (device_may_wakeup(dev))
  804. disable_irq_wake(rtc->irq_alarm);
  805. else
  806. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
  807. rtc->type->lock(rtc);
  808. rtc->is_suspending = false;
  809. return 0;
  810. }
  811. static int __maybe_unused omap_rtc_runtime_suspend(struct device *dev)
  812. {
  813. struct omap_rtc *rtc = dev_get_drvdata(dev);
  814. if (rtc->is_suspending && !rtc->has_ext_clk)
  815. return -EBUSY;
  816. return 0;
  817. }
  818. static const struct dev_pm_ops omap_rtc_pm_ops = {
  819. SET_SYSTEM_SLEEP_PM_OPS(omap_rtc_suspend, omap_rtc_resume)
  820. SET_RUNTIME_PM_OPS(omap_rtc_runtime_suspend, NULL, NULL)
  821. };
  822. static void omap_rtc_shutdown(struct platform_device *pdev)
  823. {
  824. struct omap_rtc *rtc = platform_get_drvdata(pdev);
  825. u8 mask;
  826. /*
  827. * Keep the ALARM interrupt enabled to allow the system to power up on
  828. * alarm events.
  829. */
  830. rtc->type->unlock(rtc);
  831. mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
  832. mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
  833. rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
  834. rtc->type->lock(rtc);
  835. }
  836. static struct platform_driver omap_rtc_driver = {
  837. .probe = omap_rtc_probe,
  838. .remove = omap_rtc_remove,
  839. .shutdown = omap_rtc_shutdown,
  840. .driver = {
  841. .name = "omap_rtc",
  842. .pm = &omap_rtc_pm_ops,
  843. .of_match_table = omap_rtc_of_match,
  844. },
  845. .id_table = omap_rtc_id_table,
  846. };
  847. module_platform_driver(omap_rtc_driver);
  848. MODULE_AUTHOR("George G. Davis (and others)");
  849. MODULE_LICENSE("GPL");