rtc-cmos.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * RTC class driver for "CMOS RTC": PCs, ACPI, etc
  3. *
  4. * Copyright (C) 1996 Paul Gortmaker (drivers/char/rtc.c)
  5. * Copyright (C) 2006 David Brownell (convert to new framework)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. /*
  13. * The original "cmos clock" chip was an MC146818 chip, now obsolete.
  14. * That defined the register interface now provided by all PCs, some
  15. * non-PC systems, and incorporated into ACPI. Modern PC chipsets
  16. * integrate an MC146818 clone in their southbridge, and boards use
  17. * that instead of discrete clones like the DS12887 or M48T86. There
  18. * are also clones that connect using the LPC bus.
  19. *
  20. * That register API is also used directly by various other drivers
  21. * (notably for integrated NVRAM), infrastructure (x86 has code to
  22. * bypass the RTC framework, directly reading the RTC during boot
  23. * and updating minutes/seconds for systems using NTP synch) and
  24. * utilities (like userspace 'hwclock', if no /dev node exists).
  25. *
  26. * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with
  27. * interrupts disabled, holding the global rtc_lock, to exclude those
  28. * other drivers and utilities on correctly configured systems.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/mod_devicetable.h>
  37. /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
  38. #include <asm-generic/rtc.h>
  39. struct cmos_rtc {
  40. struct rtc_device *rtc;
  41. struct device *dev;
  42. int irq;
  43. struct resource *iomem;
  44. u8 suspend_ctrl;
  45. /* newer hardware extends the original register set */
  46. u8 day_alrm;
  47. u8 mon_alrm;
  48. u8 century;
  49. };
  50. /* both platform and pnp busses use negative numbers for invalid irqs */
  51. #define is_valid_irq(n) ((n) >= 0)
  52. static const char driver_name[] = "rtc_cmos";
  53. /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
  54. * always mask it against the irq enable bits in RTC_CONTROL. Bit values
  55. * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
  56. */
  57. #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
  58. static inline int is_intr(u8 rtc_intr)
  59. {
  60. if (!(rtc_intr & RTC_IRQF))
  61. return 0;
  62. return rtc_intr & RTC_IRQMASK;
  63. }
  64. /*----------------------------------------------------------------*/
  65. static int cmos_read_time(struct device *dev, struct rtc_time *t)
  66. {
  67. /* REVISIT: if the clock has a "century" register, use
  68. * that instead of the heuristic in get_rtc_time().
  69. * That'll make Y3K compatility (year > 2070) easy!
  70. */
  71. get_rtc_time(t);
  72. return 0;
  73. }
  74. static int cmos_set_time(struct device *dev, struct rtc_time *t)
  75. {
  76. /* REVISIT: set the "century" register if available
  77. *
  78. * NOTE: this ignores the issue whereby updating the seconds
  79. * takes effect exactly 500ms after we write the register.
  80. * (Also queueing and other delays before we get this far.)
  81. */
  82. return set_rtc_time(t);
  83. }
  84. static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  85. {
  86. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  87. unsigned char rtc_control;
  88. if (!is_valid_irq(cmos->irq))
  89. return -EIO;
  90. /* Basic alarms only support hour, minute, and seconds fields.
  91. * Some also support day and month, for alarms up to a year in
  92. * the future.
  93. */
  94. t->time.tm_mday = -1;
  95. t->time.tm_mon = -1;
  96. spin_lock_irq(&rtc_lock);
  97. t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  98. t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  99. t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  100. if (cmos->day_alrm) {
  101. t->time.tm_mday = CMOS_READ(cmos->day_alrm);
  102. if (!t->time.tm_mday)
  103. t->time.tm_mday = -1;
  104. if (cmos->mon_alrm) {
  105. t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
  106. if (!t->time.tm_mon)
  107. t->time.tm_mon = -1;
  108. }
  109. }
  110. rtc_control = CMOS_READ(RTC_CONTROL);
  111. spin_unlock_irq(&rtc_lock);
  112. /* REVISIT this assumes PC style usage: always BCD */
  113. if (((unsigned)t->time.tm_sec) < 0x60)
  114. t->time.tm_sec = BCD2BIN(t->time.tm_sec);
  115. else
  116. t->time.tm_sec = -1;
  117. if (((unsigned)t->time.tm_min) < 0x60)
  118. t->time.tm_min = BCD2BIN(t->time.tm_min);
  119. else
  120. t->time.tm_min = -1;
  121. if (((unsigned)t->time.tm_hour) < 0x24)
  122. t->time.tm_hour = BCD2BIN(t->time.tm_hour);
  123. else
  124. t->time.tm_hour = -1;
  125. if (cmos->day_alrm) {
  126. if (((unsigned)t->time.tm_mday) <= 0x31)
  127. t->time.tm_mday = BCD2BIN(t->time.tm_mday);
  128. else
  129. t->time.tm_mday = -1;
  130. if (cmos->mon_alrm) {
  131. if (((unsigned)t->time.tm_mon) <= 0x12)
  132. t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1;
  133. else
  134. t->time.tm_mon = -1;
  135. }
  136. }
  137. t->time.tm_year = -1;
  138. t->enabled = !!(rtc_control & RTC_AIE);
  139. t->pending = 0;
  140. return 0;
  141. }
  142. static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  143. {
  144. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  145. unsigned char mon, mday, hrs, min, sec;
  146. unsigned char rtc_control, rtc_intr;
  147. if (!is_valid_irq(cmos->irq))
  148. return -EIO;
  149. /* REVISIT this assumes PC style usage: always BCD */
  150. /* Writing 0xff means "don't care" or "match all". */
  151. mon = t->time.tm_mon;
  152. mon = (mon < 12) ? BIN2BCD(mon) : 0xff;
  153. mon++;
  154. mday = t->time.tm_mday;
  155. mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
  156. hrs = t->time.tm_hour;
  157. hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff;
  158. min = t->time.tm_min;
  159. min = (min < 60) ? BIN2BCD(min) : 0xff;
  160. sec = t->time.tm_sec;
  161. sec = (sec < 60) ? BIN2BCD(sec) : 0xff;
  162. spin_lock_irq(&rtc_lock);
  163. /* next rtc irq must not be from previous alarm setting */
  164. rtc_control = CMOS_READ(RTC_CONTROL);
  165. rtc_control &= ~RTC_AIE;
  166. CMOS_WRITE(rtc_control, RTC_CONTROL);
  167. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  168. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  169. if (is_intr(rtc_intr))
  170. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  171. /* update alarm */
  172. CMOS_WRITE(hrs, RTC_HOURS_ALARM);
  173. CMOS_WRITE(min, RTC_MINUTES_ALARM);
  174. CMOS_WRITE(sec, RTC_SECONDS_ALARM);
  175. /* the system may support an "enhanced" alarm */
  176. if (cmos->day_alrm) {
  177. CMOS_WRITE(mday, cmos->day_alrm);
  178. if (cmos->mon_alrm)
  179. CMOS_WRITE(mon, cmos->mon_alrm);
  180. }
  181. if (t->enabled) {
  182. rtc_control |= RTC_AIE;
  183. CMOS_WRITE(rtc_control, RTC_CONTROL);
  184. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  185. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  186. if (is_intr(rtc_intr))
  187. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  188. }
  189. spin_unlock_irq(&rtc_lock);
  190. return 0;
  191. }
  192. static int cmos_set_freq(struct device *dev, int freq)
  193. {
  194. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  195. int f;
  196. unsigned long flags;
  197. if (!is_valid_irq(cmos->irq))
  198. return -ENXIO;
  199. /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
  200. f = ffs(freq);
  201. if (f != 0) {
  202. if (f-- > 16 || freq != (1 << f))
  203. return -EINVAL;
  204. f = 16 - f;
  205. }
  206. spin_lock_irqsave(&rtc_lock, flags);
  207. CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
  208. spin_unlock_irqrestore(&rtc_lock, flags);
  209. return 0;
  210. }
  211. #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
  212. static int
  213. cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  214. {
  215. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  216. unsigned char rtc_control, rtc_intr;
  217. unsigned long flags;
  218. switch (cmd) {
  219. case RTC_AIE_OFF:
  220. case RTC_AIE_ON:
  221. case RTC_UIE_OFF:
  222. case RTC_UIE_ON:
  223. case RTC_PIE_OFF:
  224. case RTC_PIE_ON:
  225. if (!is_valid_irq(cmos->irq))
  226. return -EINVAL;
  227. break;
  228. default:
  229. return -ENOIOCTLCMD;
  230. }
  231. spin_lock_irqsave(&rtc_lock, flags);
  232. rtc_control = CMOS_READ(RTC_CONTROL);
  233. switch (cmd) {
  234. case RTC_AIE_OFF: /* alarm off */
  235. rtc_control &= ~RTC_AIE;
  236. break;
  237. case RTC_AIE_ON: /* alarm on */
  238. rtc_control |= RTC_AIE;
  239. break;
  240. case RTC_UIE_OFF: /* update off */
  241. rtc_control &= ~RTC_UIE;
  242. break;
  243. case RTC_UIE_ON: /* update on */
  244. rtc_control |= RTC_UIE;
  245. break;
  246. case RTC_PIE_OFF: /* periodic off */
  247. rtc_control &= ~RTC_PIE;
  248. break;
  249. case RTC_PIE_ON: /* periodic on */
  250. rtc_control |= RTC_PIE;
  251. break;
  252. }
  253. CMOS_WRITE(rtc_control, RTC_CONTROL);
  254. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  255. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  256. if (is_intr(rtc_intr))
  257. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  258. spin_unlock_irqrestore(&rtc_lock, flags);
  259. return 0;
  260. }
  261. #else
  262. #define cmos_rtc_ioctl NULL
  263. #endif
  264. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  265. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  266. {
  267. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  268. unsigned char rtc_control, valid;
  269. spin_lock_irq(&rtc_lock);
  270. rtc_control = CMOS_READ(RTC_CONTROL);
  271. valid = CMOS_READ(RTC_VALID);
  272. spin_unlock_irq(&rtc_lock);
  273. /* NOTE: at least ICH6 reports battery status using a different
  274. * (non-RTC) bit; and SQWE is ignored on many current systems.
  275. */
  276. return seq_printf(seq,
  277. "periodic_IRQ\t: %s\n"
  278. "update_IRQ\t: %s\n"
  279. // "square_wave\t: %s\n"
  280. // "BCD\t\t: %s\n"
  281. "DST_enable\t: %s\n"
  282. "periodic_freq\t: %d\n"
  283. "batt_status\t: %s\n",
  284. (rtc_control & RTC_PIE) ? "yes" : "no",
  285. (rtc_control & RTC_UIE) ? "yes" : "no",
  286. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  287. // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  288. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  289. cmos->rtc->irq_freq,
  290. (valid & RTC_VRT) ? "okay" : "dead");
  291. }
  292. #else
  293. #define cmos_procfs NULL
  294. #endif
  295. static const struct rtc_class_ops cmos_rtc_ops = {
  296. .ioctl = cmos_rtc_ioctl,
  297. .read_time = cmos_read_time,
  298. .set_time = cmos_set_time,
  299. .read_alarm = cmos_read_alarm,
  300. .set_alarm = cmos_set_alarm,
  301. .proc = cmos_procfs,
  302. .irq_set_freq = cmos_set_freq,
  303. };
  304. /*----------------------------------------------------------------*/
  305. static struct cmos_rtc cmos_rtc;
  306. static irqreturn_t cmos_interrupt(int irq, void *p)
  307. {
  308. u8 irqstat;
  309. spin_lock(&rtc_lock);
  310. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  311. irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
  312. spin_unlock(&rtc_lock);
  313. if (is_intr(irqstat)) {
  314. rtc_update_irq(p, 1, irqstat);
  315. return IRQ_HANDLED;
  316. } else
  317. return IRQ_NONE;
  318. }
  319. #ifdef CONFIG_PNPACPI
  320. #define is_pnpacpi() 1
  321. #define INITSECTION
  322. #else
  323. #define is_pnpacpi() 0
  324. #define INITSECTION __init
  325. #endif
  326. static int INITSECTION
  327. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  328. {
  329. struct cmos_rtc_board_info *info = dev->platform_data;
  330. int retval = 0;
  331. unsigned char rtc_control;
  332. /* there can be only one ... */
  333. if (cmos_rtc.dev)
  334. return -EBUSY;
  335. if (!ports)
  336. return -ENODEV;
  337. cmos_rtc.irq = rtc_irq;
  338. cmos_rtc.iomem = ports;
  339. /* For ACPI systems the info comes from the FADT. On others,
  340. * board specific setup provides it as appropriate.
  341. */
  342. if (info) {
  343. cmos_rtc.day_alrm = info->rtc_day_alarm;
  344. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  345. cmos_rtc.century = info->rtc_century;
  346. }
  347. cmos_rtc.rtc = rtc_device_register(driver_name, dev,
  348. &cmos_rtc_ops, THIS_MODULE);
  349. if (IS_ERR(cmos_rtc.rtc))
  350. return PTR_ERR(cmos_rtc.rtc);
  351. cmos_rtc.dev = dev;
  352. dev_set_drvdata(dev, &cmos_rtc);
  353. /* platform and pnp busses handle resources incompatibly.
  354. *
  355. * REVISIT for non-x86 systems we may need to handle io memory
  356. * resources: ioremap them, and request_mem_region().
  357. */
  358. if (is_pnpacpi()) {
  359. retval = request_resource(&ioport_resource, ports);
  360. if (retval < 0) {
  361. dev_dbg(dev, "i/o registers already in use\n");
  362. goto cleanup0;
  363. }
  364. }
  365. rename_region(ports, cmos_rtc.rtc->dev.bus_id);
  366. spin_lock_irq(&rtc_lock);
  367. /* force periodic irq to CMOS reset default of 1024Hz;
  368. *
  369. * REVISIT it's been reported that at least one x86_64 ALI mobo
  370. * doesn't use 32KHz here ... for portability we might need to
  371. * do something about other clock frequencies.
  372. */
  373. CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
  374. cmos_rtc.rtc->irq_freq = 1024;
  375. /* disable irqs.
  376. *
  377. * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  378. * allegedly some older rtcs need that to handle irqs properly
  379. */
  380. rtc_control = CMOS_READ(RTC_CONTROL);
  381. rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
  382. CMOS_WRITE(rtc_control, RTC_CONTROL);
  383. CMOS_READ(RTC_INTR_FLAGS);
  384. spin_unlock_irq(&rtc_lock);
  385. /* FIXME teach the alarm code how to handle binary mode;
  386. * <asm-generic/rtc.h> doesn't know 12-hour mode either.
  387. */
  388. if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
  389. dev_dbg(dev, "only 24-hr BCD mode supported\n");
  390. retval = -ENXIO;
  391. goto cleanup1;
  392. }
  393. if (is_valid_irq(rtc_irq))
  394. retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
  395. cmos_rtc.rtc->dev.bus_id,
  396. cmos_rtc.rtc);
  397. if (retval < 0) {
  398. dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
  399. goto cleanup1;
  400. }
  401. /* REVISIT optionally make 50 or 114 bytes NVRAM available,
  402. * like rtc-ds1553, rtc-ds1742 ... this will often include
  403. * registers for century, and day/month alarm.
  404. */
  405. pr_info("%s: alarms up to one %s%s\n",
  406. cmos_rtc.rtc->dev.bus_id,
  407. is_valid_irq(rtc_irq)
  408. ? (cmos_rtc.mon_alrm
  409. ? "year"
  410. : (cmos_rtc.day_alrm
  411. ? "month" : "day"))
  412. : "no",
  413. cmos_rtc.century ? ", y3k" : ""
  414. );
  415. return 0;
  416. cleanup1:
  417. rename_region(ports, NULL);
  418. cleanup0:
  419. rtc_device_unregister(cmos_rtc.rtc);
  420. return retval;
  421. }
  422. static void cmos_do_shutdown(void)
  423. {
  424. unsigned char rtc_control;
  425. spin_lock_irq(&rtc_lock);
  426. rtc_control = CMOS_READ(RTC_CONTROL);
  427. rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  428. CMOS_WRITE(rtc_control, RTC_CONTROL);
  429. CMOS_READ(RTC_INTR_FLAGS);
  430. spin_unlock_irq(&rtc_lock);
  431. }
  432. static void __exit cmos_do_remove(struct device *dev)
  433. {
  434. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  435. cmos_do_shutdown();
  436. if (is_pnpacpi())
  437. release_resource(cmos->iomem);
  438. rename_region(cmos->iomem, NULL);
  439. if (is_valid_irq(cmos->irq))
  440. free_irq(cmos->irq, cmos_rtc.rtc);
  441. rtc_device_unregister(cmos_rtc.rtc);
  442. cmos_rtc.dev = NULL;
  443. dev_set_drvdata(dev, NULL);
  444. }
  445. #ifdef CONFIG_PM
  446. static int cmos_suspend(struct device *dev, pm_message_t mesg)
  447. {
  448. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  449. int do_wake = device_may_wakeup(dev);
  450. unsigned char tmp;
  451. /* only the alarm might be a wakeup event source */
  452. spin_lock_irq(&rtc_lock);
  453. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  454. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  455. unsigned char irqstat;
  456. if (do_wake)
  457. tmp &= ~(RTC_PIE|RTC_UIE);
  458. else
  459. tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  460. CMOS_WRITE(tmp, RTC_CONTROL);
  461. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  462. irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  463. if (is_intr(irqstat))
  464. rtc_update_irq(cmos->rtc, 1, irqstat);
  465. }
  466. spin_unlock_irq(&rtc_lock);
  467. /* ACPI HOOK: enable ACPI_EVENT_RTC when (tmp & RTC_AIE)
  468. * ... it'd be best if we could do that under rtc_lock.
  469. */
  470. pr_debug("%s: suspend%s, ctrl %02x\n",
  471. cmos_rtc.rtc->dev.bus_id,
  472. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  473. tmp);
  474. return 0;
  475. }
  476. static int cmos_resume(struct device *dev)
  477. {
  478. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  479. unsigned char tmp = cmos->suspend_ctrl;
  480. /* REVISIT: a mechanism to resync the system clock (jiffies)
  481. * on resume should be portable between platforms ...
  482. */
  483. /* re-enable any irqs previously active */
  484. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  485. /* ACPI HOOK: disable ACPI_EVENT_RTC when (tmp & RTC_AIE) */
  486. spin_lock_irq(&rtc_lock);
  487. CMOS_WRITE(tmp, RTC_CONTROL);
  488. tmp = CMOS_READ(RTC_INTR_FLAGS);
  489. tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
  490. if (is_intr(tmp))
  491. rtc_update_irq(cmos->rtc, 1, tmp);
  492. spin_unlock_irq(&rtc_lock);
  493. }
  494. pr_debug("%s: resume, ctrl %02x\n",
  495. cmos_rtc.rtc->dev.bus_id,
  496. cmos->suspend_ctrl);
  497. return 0;
  498. }
  499. #else
  500. #define cmos_suspend NULL
  501. #define cmos_resume NULL
  502. #endif
  503. /*----------------------------------------------------------------*/
  504. /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
  505. * the device node will always be created as a PNPACPI device.
  506. */
  507. #ifdef CONFIG_PNPACPI
  508. #include <linux/pnp.h>
  509. static int __devinit
  510. cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  511. {
  512. /* REVISIT paranoia argues for a shutdown notifier, since PNP
  513. * drivers can't provide shutdown() methods to disable IRQs.
  514. * Or better yet, fix PNP to allow those methods...
  515. */
  516. return cmos_do_probe(&pnp->dev,
  517. &pnp->res.port_resource[0],
  518. pnp->res.irq_resource[0].start);
  519. }
  520. static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
  521. {
  522. cmos_do_remove(&pnp->dev);
  523. }
  524. #ifdef CONFIG_PM
  525. static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
  526. {
  527. return cmos_suspend(&pnp->dev, mesg);
  528. }
  529. static int cmos_pnp_resume(struct pnp_dev *pnp)
  530. {
  531. return cmos_resume(&pnp->dev);
  532. }
  533. #else
  534. #define cmos_pnp_suspend NULL
  535. #define cmos_pnp_resume NULL
  536. #endif
  537. static const struct pnp_device_id rtc_ids[] = {
  538. { .id = "PNP0b00", },
  539. { .id = "PNP0b01", },
  540. { .id = "PNP0b02", },
  541. { },
  542. };
  543. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  544. static struct pnp_driver cmos_pnp_driver = {
  545. .name = (char *) driver_name,
  546. .id_table = rtc_ids,
  547. .probe = cmos_pnp_probe,
  548. .remove = __exit_p(cmos_pnp_remove),
  549. /* flag ensures resume() gets called, and stops syslog spam */
  550. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  551. .suspend = cmos_pnp_suspend,
  552. .resume = cmos_pnp_resume,
  553. };
  554. static int __init cmos_init(void)
  555. {
  556. return pnp_register_driver(&cmos_pnp_driver);
  557. }
  558. module_init(cmos_init);
  559. static void __exit cmos_exit(void)
  560. {
  561. pnp_unregister_driver(&cmos_pnp_driver);
  562. }
  563. module_exit(cmos_exit);
  564. #else /* no PNPACPI */
  565. /*----------------------------------------------------------------*/
  566. /* Platform setup should have set up an RTC device, when PNPACPI is
  567. * unavailable ... this could happen even on (older) PCs.
  568. */
  569. static int __init cmos_platform_probe(struct platform_device *pdev)
  570. {
  571. return cmos_do_probe(&pdev->dev,
  572. platform_get_resource(pdev, IORESOURCE_IO, 0),
  573. platform_get_irq(pdev, 0));
  574. }
  575. static int __exit cmos_platform_remove(struct platform_device *pdev)
  576. {
  577. cmos_do_remove(&pdev->dev);
  578. return 0;
  579. }
  580. static void cmos_platform_shutdown(struct platform_device *pdev)
  581. {
  582. cmos_do_shutdown();
  583. }
  584. static struct platform_driver cmos_platform_driver = {
  585. .remove = __exit_p(cmos_platform_remove),
  586. .shutdown = cmos_platform_shutdown,
  587. .driver = {
  588. .name = (char *) driver_name,
  589. .suspend = cmos_suspend,
  590. .resume = cmos_resume,
  591. }
  592. };
  593. static int __init cmos_init(void)
  594. {
  595. return platform_driver_probe(&cmos_platform_driver,
  596. cmos_platform_probe);
  597. }
  598. module_init(cmos_init);
  599. static void __exit cmos_exit(void)
  600. {
  601. platform_driver_unregister(&cmos_platform_driver);
  602. }
  603. module_exit(cmos_exit);
  604. #endif /* !PNPACPI */
  605. MODULE_AUTHOR("David Brownell");
  606. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  607. MODULE_LICENSE("GPL");