rtc-cmos.c 18 KB

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