rtc-cmos.c 18 KB

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