rtc-davinci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * DaVinci Power Management and Real Time Clock Driver for TI platforms
  3. *
  4. * Copyright (C) 2009 Texas Instruments, Inc
  5. *
  6. * Author: Miguel Aguilar <miguel.aguilar@ridgerun.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/ioport.h>
  26. #include <linux/delay.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/rtc.h>
  29. #include <linux/bcd.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/io.h>
  32. #include <linux/slab.h>
  33. /*
  34. * The DaVinci RTC is a simple RTC with the following
  35. * Sec: 0 - 59 : BCD count
  36. * Min: 0 - 59 : BCD count
  37. * Hour: 0 - 23 : BCD count
  38. * Day: 0 - 0x7FFF(32767) : Binary count ( Over 89 years )
  39. */
  40. /* PRTC interface registers */
  41. #define DAVINCI_PRTCIF_PID 0x00
  42. #define PRTCIF_CTLR 0x04
  43. #define PRTCIF_LDATA 0x08
  44. #define PRTCIF_UDATA 0x0C
  45. #define PRTCIF_INTEN 0x10
  46. #define PRTCIF_INTFLG 0x14
  47. /* PRTCIF_CTLR bit fields */
  48. #define PRTCIF_CTLR_BUSY BIT(31)
  49. #define PRTCIF_CTLR_SIZE BIT(25)
  50. #define PRTCIF_CTLR_DIR BIT(24)
  51. #define PRTCIF_CTLR_BENU_MSB BIT(23)
  52. #define PRTCIF_CTLR_BENU_3RD_BYTE BIT(22)
  53. #define PRTCIF_CTLR_BENU_2ND_BYTE BIT(21)
  54. #define PRTCIF_CTLR_BENU_LSB BIT(20)
  55. #define PRTCIF_CTLR_BENU_MASK (0x00F00000)
  56. #define PRTCIF_CTLR_BENL_MSB BIT(19)
  57. #define PRTCIF_CTLR_BENL_3RD_BYTE BIT(18)
  58. #define PRTCIF_CTLR_BENL_2ND_BYTE BIT(17)
  59. #define PRTCIF_CTLR_BENL_LSB BIT(16)
  60. #define PRTCIF_CTLR_BENL_MASK (0x000F0000)
  61. /* PRTCIF_INTEN bit fields */
  62. #define PRTCIF_INTEN_RTCSS BIT(1)
  63. #define PRTCIF_INTEN_RTCIF BIT(0)
  64. #define PRTCIF_INTEN_MASK (PRTCIF_INTEN_RTCSS \
  65. | PRTCIF_INTEN_RTCIF)
  66. /* PRTCIF_INTFLG bit fields */
  67. #define PRTCIF_INTFLG_RTCSS BIT(1)
  68. #define PRTCIF_INTFLG_RTCIF BIT(0)
  69. #define PRTCIF_INTFLG_MASK (PRTCIF_INTFLG_RTCSS \
  70. | PRTCIF_INTFLG_RTCIF)
  71. /* PRTC subsystem registers */
  72. #define PRTCSS_RTC_INTC_EXTENA1 (0x0C)
  73. #define PRTCSS_RTC_CTRL (0x10)
  74. #define PRTCSS_RTC_WDT (0x11)
  75. #define PRTCSS_RTC_TMR0 (0x12)
  76. #define PRTCSS_RTC_TMR1 (0x13)
  77. #define PRTCSS_RTC_CCTRL (0x14)
  78. #define PRTCSS_RTC_SEC (0x15)
  79. #define PRTCSS_RTC_MIN (0x16)
  80. #define PRTCSS_RTC_HOUR (0x17)
  81. #define PRTCSS_RTC_DAY0 (0x18)
  82. #define PRTCSS_RTC_DAY1 (0x19)
  83. #define PRTCSS_RTC_AMIN (0x1A)
  84. #define PRTCSS_RTC_AHOUR (0x1B)
  85. #define PRTCSS_RTC_ADAY0 (0x1C)
  86. #define PRTCSS_RTC_ADAY1 (0x1D)
  87. #define PRTCSS_RTC_CLKC_CNT (0x20)
  88. /* PRTCSS_RTC_INTC_EXTENA1 */
  89. #define PRTCSS_RTC_INTC_EXTENA1_MASK (0x07)
  90. /* PRTCSS_RTC_CTRL bit fields */
  91. #define PRTCSS_RTC_CTRL_WDTBUS BIT(7)
  92. #define PRTCSS_RTC_CTRL_WEN BIT(6)
  93. #define PRTCSS_RTC_CTRL_WDRT BIT(5)
  94. #define PRTCSS_RTC_CTRL_WDTFLG BIT(4)
  95. #define PRTCSS_RTC_CTRL_TE BIT(3)
  96. #define PRTCSS_RTC_CTRL_TIEN BIT(2)
  97. #define PRTCSS_RTC_CTRL_TMRFLG BIT(1)
  98. #define PRTCSS_RTC_CTRL_TMMD BIT(0)
  99. /* PRTCSS_RTC_CCTRL bit fields */
  100. #define PRTCSS_RTC_CCTRL_CALBUSY BIT(7)
  101. #define PRTCSS_RTC_CCTRL_DAEN BIT(5)
  102. #define PRTCSS_RTC_CCTRL_HAEN BIT(4)
  103. #define PRTCSS_RTC_CCTRL_MAEN BIT(3)
  104. #define PRTCSS_RTC_CCTRL_ALMFLG BIT(2)
  105. #define PRTCSS_RTC_CCTRL_AIEN BIT(1)
  106. #define PRTCSS_RTC_CCTRL_CAEN BIT(0)
  107. static DEFINE_SPINLOCK(davinci_rtc_lock);
  108. struct davinci_rtc {
  109. struct rtc_device *rtc;
  110. void __iomem *base;
  111. resource_size_t pbase;
  112. size_t base_size;
  113. int irq;
  114. };
  115. static inline void rtcif_write(struct davinci_rtc *davinci_rtc,
  116. u32 val, u32 addr)
  117. {
  118. writel(val, davinci_rtc->base + addr);
  119. }
  120. static inline u32 rtcif_read(struct davinci_rtc *davinci_rtc, u32 addr)
  121. {
  122. return readl(davinci_rtc->base + addr);
  123. }
  124. static inline void rtcif_wait(struct davinci_rtc *davinci_rtc)
  125. {
  126. while (rtcif_read(davinci_rtc, PRTCIF_CTLR) & PRTCIF_CTLR_BUSY)
  127. cpu_relax();
  128. }
  129. static inline void rtcss_write(struct davinci_rtc *davinci_rtc,
  130. unsigned long val, u8 addr)
  131. {
  132. rtcif_wait(davinci_rtc);
  133. rtcif_write(davinci_rtc, PRTCIF_CTLR_BENL_LSB | addr, PRTCIF_CTLR);
  134. rtcif_write(davinci_rtc, val, PRTCIF_LDATA);
  135. rtcif_wait(davinci_rtc);
  136. }
  137. static inline u8 rtcss_read(struct davinci_rtc *davinci_rtc, u8 addr)
  138. {
  139. rtcif_wait(davinci_rtc);
  140. rtcif_write(davinci_rtc, PRTCIF_CTLR_DIR | PRTCIF_CTLR_BENL_LSB | addr,
  141. PRTCIF_CTLR);
  142. rtcif_wait(davinci_rtc);
  143. return rtcif_read(davinci_rtc, PRTCIF_LDATA);
  144. }
  145. static inline void davinci_rtcss_calendar_wait(struct davinci_rtc *davinci_rtc)
  146. {
  147. while (rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL) &
  148. PRTCSS_RTC_CCTRL_CALBUSY)
  149. cpu_relax();
  150. }
  151. static irqreturn_t davinci_rtc_interrupt(int irq, void *class_dev)
  152. {
  153. struct davinci_rtc *davinci_rtc = class_dev;
  154. unsigned long events = 0;
  155. u32 irq_flg;
  156. u8 alm_irq, tmr_irq;
  157. u8 rtc_ctrl, rtc_cctrl;
  158. int ret = IRQ_NONE;
  159. irq_flg = rtcif_read(davinci_rtc, PRTCIF_INTFLG) &
  160. PRTCIF_INTFLG_RTCSS;
  161. alm_irq = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL) &
  162. PRTCSS_RTC_CCTRL_ALMFLG;
  163. tmr_irq = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL) &
  164. PRTCSS_RTC_CTRL_TMRFLG;
  165. if (irq_flg) {
  166. if (alm_irq) {
  167. events |= RTC_IRQF | RTC_AF;
  168. rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
  169. rtc_cctrl |= PRTCSS_RTC_CCTRL_ALMFLG;
  170. rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
  171. } else if (tmr_irq) {
  172. events |= RTC_IRQF | RTC_PF;
  173. rtc_ctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL);
  174. rtc_ctrl |= PRTCSS_RTC_CTRL_TMRFLG;
  175. rtcss_write(davinci_rtc, rtc_ctrl, PRTCSS_RTC_CTRL);
  176. }
  177. rtcif_write(davinci_rtc, PRTCIF_INTFLG_RTCSS,
  178. PRTCIF_INTFLG);
  179. rtc_update_irq(davinci_rtc->rtc, 1, events);
  180. ret = IRQ_HANDLED;
  181. }
  182. return ret;
  183. }
  184. static int
  185. davinci_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  186. {
  187. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  188. u8 rtc_ctrl;
  189. unsigned long flags;
  190. int ret = 0;
  191. spin_lock_irqsave(&davinci_rtc_lock, flags);
  192. rtc_ctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CTRL);
  193. switch (cmd) {
  194. case RTC_WIE_ON:
  195. rtc_ctrl |= PRTCSS_RTC_CTRL_WEN | PRTCSS_RTC_CTRL_WDTFLG;
  196. break;
  197. case RTC_WIE_OFF:
  198. rtc_ctrl &= ~PRTCSS_RTC_CTRL_WEN;
  199. break;
  200. default:
  201. ret = -ENOIOCTLCMD;
  202. }
  203. rtcss_write(davinci_rtc, rtc_ctrl, PRTCSS_RTC_CTRL);
  204. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  205. return ret;
  206. }
  207. static int convertfromdays(u16 days, struct rtc_time *tm)
  208. {
  209. int tmp_days, year, mon;
  210. for (year = 2000;; year++) {
  211. tmp_days = rtc_year_days(1, 12, year);
  212. if (days >= tmp_days)
  213. days -= tmp_days;
  214. else {
  215. for (mon = 0;; mon++) {
  216. tmp_days = rtc_month_days(mon, year);
  217. if (days >= tmp_days) {
  218. days -= tmp_days;
  219. } else {
  220. tm->tm_year = year - 1900;
  221. tm->tm_mon = mon;
  222. tm->tm_mday = days + 1;
  223. break;
  224. }
  225. }
  226. break;
  227. }
  228. }
  229. return 0;
  230. }
  231. static int convert2days(u16 *days, struct rtc_time *tm)
  232. {
  233. int i;
  234. *days = 0;
  235. /* epoch == 1900 */
  236. if (tm->tm_year < 100 || tm->tm_year > 199)
  237. return -EINVAL;
  238. for (i = 2000; i < 1900 + tm->tm_year; i++)
  239. *days += rtc_year_days(1, 12, i);
  240. *days += rtc_year_days(tm->tm_mday, tm->tm_mon, 1900 + tm->tm_year);
  241. return 0;
  242. }
  243. static int davinci_rtc_read_time(struct device *dev, struct rtc_time *tm)
  244. {
  245. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  246. u16 days = 0;
  247. u8 day0, day1;
  248. unsigned long flags;
  249. spin_lock_irqsave(&davinci_rtc_lock, flags);
  250. davinci_rtcss_calendar_wait(davinci_rtc);
  251. tm->tm_sec = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_SEC));
  252. davinci_rtcss_calendar_wait(davinci_rtc);
  253. tm->tm_min = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_MIN));
  254. davinci_rtcss_calendar_wait(davinci_rtc);
  255. tm->tm_hour = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_HOUR));
  256. davinci_rtcss_calendar_wait(davinci_rtc);
  257. day0 = rtcss_read(davinci_rtc, PRTCSS_RTC_DAY0);
  258. davinci_rtcss_calendar_wait(davinci_rtc);
  259. day1 = rtcss_read(davinci_rtc, PRTCSS_RTC_DAY1);
  260. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  261. days |= day1;
  262. days <<= 8;
  263. days |= day0;
  264. if (convertfromdays(days, tm) < 0)
  265. return -EINVAL;
  266. return 0;
  267. }
  268. static int davinci_rtc_set_time(struct device *dev, struct rtc_time *tm)
  269. {
  270. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  271. u16 days;
  272. u8 rtc_cctrl;
  273. unsigned long flags;
  274. if (convert2days(&days, tm) < 0)
  275. return -EINVAL;
  276. spin_lock_irqsave(&davinci_rtc_lock, flags);
  277. davinci_rtcss_calendar_wait(davinci_rtc);
  278. rtcss_write(davinci_rtc, bin2bcd(tm->tm_sec), PRTCSS_RTC_SEC);
  279. davinci_rtcss_calendar_wait(davinci_rtc);
  280. rtcss_write(davinci_rtc, bin2bcd(tm->tm_min), PRTCSS_RTC_MIN);
  281. davinci_rtcss_calendar_wait(davinci_rtc);
  282. rtcss_write(davinci_rtc, bin2bcd(tm->tm_hour), PRTCSS_RTC_HOUR);
  283. davinci_rtcss_calendar_wait(davinci_rtc);
  284. rtcss_write(davinci_rtc, days & 0xFF, PRTCSS_RTC_DAY0);
  285. davinci_rtcss_calendar_wait(davinci_rtc);
  286. rtcss_write(davinci_rtc, (days & 0xFF00) >> 8, PRTCSS_RTC_DAY1);
  287. rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
  288. rtc_cctrl |= PRTCSS_RTC_CCTRL_CAEN;
  289. rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
  290. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  291. return 0;
  292. }
  293. static int davinci_rtc_alarm_irq_enable(struct device *dev,
  294. unsigned int enabled)
  295. {
  296. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  297. unsigned long flags;
  298. u8 rtc_cctrl = rtcss_read(davinci_rtc, PRTCSS_RTC_CCTRL);
  299. spin_lock_irqsave(&davinci_rtc_lock, flags);
  300. if (enabled)
  301. rtc_cctrl |= PRTCSS_RTC_CCTRL_DAEN |
  302. PRTCSS_RTC_CCTRL_HAEN |
  303. PRTCSS_RTC_CCTRL_MAEN |
  304. PRTCSS_RTC_CCTRL_ALMFLG |
  305. PRTCSS_RTC_CCTRL_AIEN;
  306. else
  307. rtc_cctrl &= ~PRTCSS_RTC_CCTRL_AIEN;
  308. davinci_rtcss_calendar_wait(davinci_rtc);
  309. rtcss_write(davinci_rtc, rtc_cctrl, PRTCSS_RTC_CCTRL);
  310. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  311. return 0;
  312. }
  313. static int davinci_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  314. {
  315. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  316. u16 days = 0;
  317. u8 day0, day1;
  318. unsigned long flags;
  319. spin_lock_irqsave(&davinci_rtc_lock, flags);
  320. davinci_rtcss_calendar_wait(davinci_rtc);
  321. alm->time.tm_min = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_AMIN));
  322. davinci_rtcss_calendar_wait(davinci_rtc);
  323. alm->time.tm_hour = bcd2bin(rtcss_read(davinci_rtc, PRTCSS_RTC_AHOUR));
  324. davinci_rtcss_calendar_wait(davinci_rtc);
  325. day0 = rtcss_read(davinci_rtc, PRTCSS_RTC_ADAY0);
  326. davinci_rtcss_calendar_wait(davinci_rtc);
  327. day1 = rtcss_read(davinci_rtc, PRTCSS_RTC_ADAY1);
  328. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  329. days |= day1;
  330. days <<= 8;
  331. days |= day0;
  332. if (convertfromdays(days, &alm->time) < 0)
  333. return -EINVAL;
  334. alm->pending = !!(rtcss_read(davinci_rtc,
  335. PRTCSS_RTC_CCTRL) &
  336. PRTCSS_RTC_CCTRL_AIEN);
  337. alm->enabled = alm->pending && device_may_wakeup(dev);
  338. return 0;
  339. }
  340. static int davinci_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  341. {
  342. struct davinci_rtc *davinci_rtc = dev_get_drvdata(dev);
  343. unsigned long flags;
  344. u16 days;
  345. if (alm->time.tm_mday <= 0 && alm->time.tm_mon < 0
  346. && alm->time.tm_year < 0) {
  347. struct rtc_time tm;
  348. unsigned long now, then;
  349. davinci_rtc_read_time(dev, &tm);
  350. rtc_tm_to_time(&tm, &now);
  351. alm->time.tm_mday = tm.tm_mday;
  352. alm->time.tm_mon = tm.tm_mon;
  353. alm->time.tm_year = tm.tm_year;
  354. rtc_tm_to_time(&alm->time, &then);
  355. if (then < now) {
  356. rtc_time_to_tm(now + 24 * 60 * 60, &tm);
  357. alm->time.tm_mday = tm.tm_mday;
  358. alm->time.tm_mon = tm.tm_mon;
  359. alm->time.tm_year = tm.tm_year;
  360. }
  361. }
  362. if (convert2days(&days, &alm->time) < 0)
  363. return -EINVAL;
  364. spin_lock_irqsave(&davinci_rtc_lock, flags);
  365. davinci_rtcss_calendar_wait(davinci_rtc);
  366. rtcss_write(davinci_rtc, bin2bcd(alm->time.tm_min), PRTCSS_RTC_AMIN);
  367. davinci_rtcss_calendar_wait(davinci_rtc);
  368. rtcss_write(davinci_rtc, bin2bcd(alm->time.tm_hour), PRTCSS_RTC_AHOUR);
  369. davinci_rtcss_calendar_wait(davinci_rtc);
  370. rtcss_write(davinci_rtc, days & 0xFF, PRTCSS_RTC_ADAY0);
  371. davinci_rtcss_calendar_wait(davinci_rtc);
  372. rtcss_write(davinci_rtc, (days & 0xFF00) >> 8, PRTCSS_RTC_ADAY1);
  373. spin_unlock_irqrestore(&davinci_rtc_lock, flags);
  374. return 0;
  375. }
  376. static struct rtc_class_ops davinci_rtc_ops = {
  377. .ioctl = davinci_rtc_ioctl,
  378. .read_time = davinci_rtc_read_time,
  379. .set_time = davinci_rtc_set_time,
  380. .alarm_irq_enable = davinci_rtc_alarm_irq_enable,
  381. .read_alarm = davinci_rtc_read_alarm,
  382. .set_alarm = davinci_rtc_set_alarm,
  383. };
  384. static int __init davinci_rtc_probe(struct platform_device *pdev)
  385. {
  386. struct device *dev = &pdev->dev;
  387. struct davinci_rtc *davinci_rtc;
  388. struct resource *res, *mem;
  389. int ret = 0;
  390. davinci_rtc = kzalloc(sizeof(struct davinci_rtc), GFP_KERNEL);
  391. if (!davinci_rtc) {
  392. dev_dbg(dev, "could not allocate memory for private data\n");
  393. return -ENOMEM;
  394. }
  395. davinci_rtc->irq = platform_get_irq(pdev, 0);
  396. if (davinci_rtc->irq < 0) {
  397. dev_err(dev, "no RTC irq\n");
  398. ret = davinci_rtc->irq;
  399. goto fail1;
  400. }
  401. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  402. if (!res) {
  403. dev_err(dev, "no mem resource\n");
  404. ret = -EINVAL;
  405. goto fail1;
  406. }
  407. davinci_rtc->pbase = res->start;
  408. davinci_rtc->base_size = resource_size(res);
  409. mem = request_mem_region(davinci_rtc->pbase, davinci_rtc->base_size,
  410. pdev->name);
  411. if (!mem) {
  412. dev_err(dev, "RTC registers at %08x are not free\n",
  413. davinci_rtc->pbase);
  414. ret = -EBUSY;
  415. goto fail1;
  416. }
  417. davinci_rtc->base = ioremap(davinci_rtc->pbase, davinci_rtc->base_size);
  418. if (!davinci_rtc->base) {
  419. dev_err(dev, "unable to ioremap MEM resource\n");
  420. ret = -ENOMEM;
  421. goto fail2;
  422. }
  423. platform_set_drvdata(pdev, davinci_rtc);
  424. davinci_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
  425. &davinci_rtc_ops, THIS_MODULE);
  426. if (IS_ERR(davinci_rtc->rtc)) {
  427. dev_err(dev, "unable to register RTC device, err %ld\n",
  428. PTR_ERR(davinci_rtc->rtc));
  429. goto fail3;
  430. }
  431. rtcif_write(davinci_rtc, PRTCIF_INTFLG_RTCSS, PRTCIF_INTFLG);
  432. rtcif_write(davinci_rtc, 0, PRTCIF_INTEN);
  433. rtcss_write(davinci_rtc, 0, PRTCSS_RTC_INTC_EXTENA1);
  434. rtcss_write(davinci_rtc, 0, PRTCSS_RTC_CTRL);
  435. rtcss_write(davinci_rtc, 0, PRTCSS_RTC_CCTRL);
  436. ret = request_irq(davinci_rtc->irq, davinci_rtc_interrupt,
  437. 0, "davinci_rtc", davinci_rtc);
  438. if (ret < 0) {
  439. dev_err(dev, "unable to register davinci RTC interrupt\n");
  440. goto fail4;
  441. }
  442. /* Enable interrupts */
  443. rtcif_write(davinci_rtc, PRTCIF_INTEN_RTCSS, PRTCIF_INTEN);
  444. rtcss_write(davinci_rtc, PRTCSS_RTC_INTC_EXTENA1_MASK,
  445. PRTCSS_RTC_INTC_EXTENA1);
  446. rtcss_write(davinci_rtc, PRTCSS_RTC_CCTRL_CAEN, PRTCSS_RTC_CCTRL);
  447. device_init_wakeup(&pdev->dev, 0);
  448. return 0;
  449. fail4:
  450. rtc_device_unregister(davinci_rtc->rtc);
  451. fail3:
  452. platform_set_drvdata(pdev, NULL);
  453. iounmap(davinci_rtc->base);
  454. fail2:
  455. release_mem_region(davinci_rtc->pbase, davinci_rtc->base_size);
  456. fail1:
  457. kfree(davinci_rtc);
  458. return ret;
  459. }
  460. static int __devexit davinci_rtc_remove(struct platform_device *pdev)
  461. {
  462. struct davinci_rtc *davinci_rtc = platform_get_drvdata(pdev);
  463. device_init_wakeup(&pdev->dev, 0);
  464. rtcif_write(davinci_rtc, 0, PRTCIF_INTEN);
  465. free_irq(davinci_rtc->irq, davinci_rtc);
  466. rtc_device_unregister(davinci_rtc->rtc);
  467. iounmap(davinci_rtc->base);
  468. release_mem_region(davinci_rtc->pbase, davinci_rtc->base_size);
  469. platform_set_drvdata(pdev, NULL);
  470. kfree(davinci_rtc);
  471. return 0;
  472. }
  473. static struct platform_driver davinci_rtc_driver = {
  474. .probe = davinci_rtc_probe,
  475. .remove = __devexit_p(davinci_rtc_remove),
  476. .driver = {
  477. .name = "rtc_davinci",
  478. .owner = THIS_MODULE,
  479. },
  480. };
  481. static int __init rtc_init(void)
  482. {
  483. return platform_driver_probe(&davinci_rtc_driver, davinci_rtc_probe);
  484. }
  485. module_init(rtc_init);
  486. static void __exit rtc_exit(void)
  487. {
  488. platform_driver_unregister(&davinci_rtc_driver);
  489. }
  490. module_exit(rtc_exit);
  491. MODULE_AUTHOR("Miguel Aguilar <miguel.aguilar@ridgerun.com>");
  492. MODULE_DESCRIPTION("Texas Instruments DaVinci PRTC Driver");
  493. MODULE_LICENSE("GPL");