rtc-cmos.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/log2.h>
  38. #include <linux/pm.h>
  39. #include <linux/of.h>
  40. #include <linux/of_platform.h>
  41. /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
  42. #include <linux/mc146818rtc.h>
  43. struct cmos_rtc {
  44. struct rtc_device *rtc;
  45. struct device *dev;
  46. int irq;
  47. struct resource *iomem;
  48. time64_t alarm_expires;
  49. void (*wake_on)(struct device *);
  50. void (*wake_off)(struct device *);
  51. u8 enabled_wake;
  52. u8 suspend_ctrl;
  53. /* newer hardware extends the original register set */
  54. u8 day_alrm;
  55. u8 mon_alrm;
  56. u8 century;
  57. struct rtc_wkalrm saved_wkalrm;
  58. };
  59. /* both platform and pnp busses use negative numbers for invalid irqs */
  60. #define is_valid_irq(n) ((n) > 0)
  61. static const char driver_name[] = "rtc_cmos";
  62. /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
  63. * always mask it against the irq enable bits in RTC_CONTROL. Bit values
  64. * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
  65. */
  66. #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
  67. static inline int is_intr(u8 rtc_intr)
  68. {
  69. if (!(rtc_intr & RTC_IRQF))
  70. return 0;
  71. return rtc_intr & RTC_IRQMASK;
  72. }
  73. /*----------------------------------------------------------------*/
  74. /* Much modern x86 hardware has HPETs (10+ MHz timers) which, because
  75. * many BIOS programmers don't set up "sane mode" IRQ routing, are mostly
  76. * used in a broken "legacy replacement" mode. The breakage includes
  77. * HPET #1 hijacking the IRQ for this RTC, and being unavailable for
  78. * other (better) use.
  79. *
  80. * When that broken mode is in use, platform glue provides a partial
  81. * emulation of hardware RTC IRQ facilities using HPET #1. We don't
  82. * want to use HPET for anything except those IRQs though...
  83. */
  84. #ifdef CONFIG_HPET_EMULATE_RTC
  85. #include <asm/hpet.h>
  86. #else
  87. static inline int is_hpet_enabled(void)
  88. {
  89. return 0;
  90. }
  91. static inline int hpet_mask_rtc_irq_bit(unsigned long mask)
  92. {
  93. return 0;
  94. }
  95. static inline int hpet_set_rtc_irq_bit(unsigned long mask)
  96. {
  97. return 0;
  98. }
  99. static inline int
  100. hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  101. {
  102. return 0;
  103. }
  104. static inline int hpet_set_periodic_freq(unsigned long freq)
  105. {
  106. return 0;
  107. }
  108. static inline int hpet_rtc_dropped_irq(void)
  109. {
  110. return 0;
  111. }
  112. static inline int hpet_rtc_timer_init(void)
  113. {
  114. return 0;
  115. }
  116. extern irq_handler_t hpet_rtc_interrupt;
  117. static inline int hpet_register_irq_handler(irq_handler_t handler)
  118. {
  119. return 0;
  120. }
  121. static inline int hpet_unregister_irq_handler(irq_handler_t handler)
  122. {
  123. return 0;
  124. }
  125. #endif
  126. /*----------------------------------------------------------------*/
  127. #ifdef RTC_PORT
  128. /* Most newer x86 systems have two register banks, the first used
  129. * for RTC and NVRAM and the second only for NVRAM. Caller must
  130. * own rtc_lock ... and we won't worry about access during NMI.
  131. */
  132. #define can_bank2 true
  133. static inline unsigned char cmos_read_bank2(unsigned char addr)
  134. {
  135. outb(addr, RTC_PORT(2));
  136. return inb(RTC_PORT(3));
  137. }
  138. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  139. {
  140. outb(addr, RTC_PORT(2));
  141. outb(val, RTC_PORT(3));
  142. }
  143. #else
  144. #define can_bank2 false
  145. static inline unsigned char cmos_read_bank2(unsigned char addr)
  146. {
  147. return 0;
  148. }
  149. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  150. {
  151. }
  152. #endif
  153. /*----------------------------------------------------------------*/
  154. static int cmos_read_time(struct device *dev, struct rtc_time *t)
  155. {
  156. /* REVISIT: if the clock has a "century" register, use
  157. * that instead of the heuristic in mc146818_get_time().
  158. * That'll make Y3K compatility (year > 2070) easy!
  159. */
  160. mc146818_get_time(t);
  161. return 0;
  162. }
  163. static int cmos_set_time(struct device *dev, struct rtc_time *t)
  164. {
  165. /* REVISIT: set the "century" register if available
  166. *
  167. * NOTE: this ignores the issue whereby updating the seconds
  168. * takes effect exactly 500ms after we write the register.
  169. * (Also queueing and other delays before we get this far.)
  170. */
  171. return mc146818_set_time(t);
  172. }
  173. static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  174. {
  175. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  176. unsigned char rtc_control;
  177. if (!is_valid_irq(cmos->irq))
  178. return -EIO;
  179. /* Basic alarms only support hour, minute, and seconds fields.
  180. * Some also support day and month, for alarms up to a year in
  181. * the future.
  182. */
  183. spin_lock_irq(&rtc_lock);
  184. t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  185. t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  186. t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  187. if (cmos->day_alrm) {
  188. /* ignore upper bits on readback per ACPI spec */
  189. t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f;
  190. if (!t->time.tm_mday)
  191. t->time.tm_mday = -1;
  192. if (cmos->mon_alrm) {
  193. t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
  194. if (!t->time.tm_mon)
  195. t->time.tm_mon = -1;
  196. }
  197. }
  198. rtc_control = CMOS_READ(RTC_CONTROL);
  199. spin_unlock_irq(&rtc_lock);
  200. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  201. if (((unsigned)t->time.tm_sec) < 0x60)
  202. t->time.tm_sec = bcd2bin(t->time.tm_sec);
  203. else
  204. t->time.tm_sec = -1;
  205. if (((unsigned)t->time.tm_min) < 0x60)
  206. t->time.tm_min = bcd2bin(t->time.tm_min);
  207. else
  208. t->time.tm_min = -1;
  209. if (((unsigned)t->time.tm_hour) < 0x24)
  210. t->time.tm_hour = bcd2bin(t->time.tm_hour);
  211. else
  212. t->time.tm_hour = -1;
  213. if (cmos->day_alrm) {
  214. if (((unsigned)t->time.tm_mday) <= 0x31)
  215. t->time.tm_mday = bcd2bin(t->time.tm_mday);
  216. else
  217. t->time.tm_mday = -1;
  218. if (cmos->mon_alrm) {
  219. if (((unsigned)t->time.tm_mon) <= 0x12)
  220. t->time.tm_mon = bcd2bin(t->time.tm_mon)-1;
  221. else
  222. t->time.tm_mon = -1;
  223. }
  224. }
  225. }
  226. t->enabled = !!(rtc_control & RTC_AIE);
  227. t->pending = 0;
  228. return 0;
  229. }
  230. static void cmos_checkintr(struct cmos_rtc *cmos, unsigned char rtc_control)
  231. {
  232. unsigned char rtc_intr;
  233. /* NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  234. * allegedly some older rtcs need that to handle irqs properly
  235. */
  236. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  237. if (is_hpet_enabled())
  238. return;
  239. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  240. if (is_intr(rtc_intr))
  241. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  242. }
  243. static void cmos_irq_enable(struct cmos_rtc *cmos, unsigned char mask)
  244. {
  245. unsigned char rtc_control;
  246. /* flush any pending IRQ status, notably for update irqs,
  247. * before we enable new IRQs
  248. */
  249. rtc_control = CMOS_READ(RTC_CONTROL);
  250. cmos_checkintr(cmos, rtc_control);
  251. rtc_control |= mask;
  252. CMOS_WRITE(rtc_control, RTC_CONTROL);
  253. hpet_set_rtc_irq_bit(mask);
  254. cmos_checkintr(cmos, rtc_control);
  255. }
  256. static void cmos_irq_disable(struct cmos_rtc *cmos, unsigned char mask)
  257. {
  258. unsigned char rtc_control;
  259. rtc_control = CMOS_READ(RTC_CONTROL);
  260. rtc_control &= ~mask;
  261. CMOS_WRITE(rtc_control, RTC_CONTROL);
  262. hpet_mask_rtc_irq_bit(mask);
  263. cmos_checkintr(cmos, rtc_control);
  264. }
  265. static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  266. {
  267. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  268. unsigned char mon, mday, hrs, min, sec, rtc_control;
  269. if (!is_valid_irq(cmos->irq))
  270. return -EIO;
  271. mon = t->time.tm_mon + 1;
  272. mday = t->time.tm_mday;
  273. hrs = t->time.tm_hour;
  274. min = t->time.tm_min;
  275. sec = t->time.tm_sec;
  276. rtc_control = CMOS_READ(RTC_CONTROL);
  277. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  278. /* Writing 0xff means "don't care" or "match all". */
  279. mon = (mon <= 12) ? bin2bcd(mon) : 0xff;
  280. mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
  281. hrs = (hrs < 24) ? bin2bcd(hrs) : 0xff;
  282. min = (min < 60) ? bin2bcd(min) : 0xff;
  283. sec = (sec < 60) ? bin2bcd(sec) : 0xff;
  284. }
  285. spin_lock_irq(&rtc_lock);
  286. /* next rtc irq must not be from previous alarm setting */
  287. cmos_irq_disable(cmos, RTC_AIE);
  288. /* update alarm */
  289. CMOS_WRITE(hrs, RTC_HOURS_ALARM);
  290. CMOS_WRITE(min, RTC_MINUTES_ALARM);
  291. CMOS_WRITE(sec, RTC_SECONDS_ALARM);
  292. /* the system may support an "enhanced" alarm */
  293. if (cmos->day_alrm) {
  294. CMOS_WRITE(mday, cmos->day_alrm);
  295. if (cmos->mon_alrm)
  296. CMOS_WRITE(mon, cmos->mon_alrm);
  297. }
  298. /* FIXME the HPET alarm glue currently ignores day_alrm
  299. * and mon_alrm ...
  300. */
  301. hpet_set_alarm_time(t->time.tm_hour, t->time.tm_min, t->time.tm_sec);
  302. if (t->enabled)
  303. cmos_irq_enable(cmos, RTC_AIE);
  304. spin_unlock_irq(&rtc_lock);
  305. cmos->alarm_expires = rtc_tm_to_time64(&t->time);
  306. return 0;
  307. }
  308. static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
  309. {
  310. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  311. unsigned long flags;
  312. if (!is_valid_irq(cmos->irq))
  313. return -EINVAL;
  314. spin_lock_irqsave(&rtc_lock, flags);
  315. if (enabled)
  316. cmos_irq_enable(cmos, RTC_AIE);
  317. else
  318. cmos_irq_disable(cmos, RTC_AIE);
  319. spin_unlock_irqrestore(&rtc_lock, flags);
  320. return 0;
  321. }
  322. #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
  323. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  324. {
  325. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  326. unsigned char rtc_control, valid;
  327. spin_lock_irq(&rtc_lock);
  328. rtc_control = CMOS_READ(RTC_CONTROL);
  329. valid = CMOS_READ(RTC_VALID);
  330. spin_unlock_irq(&rtc_lock);
  331. /* NOTE: at least ICH6 reports battery status using a different
  332. * (non-RTC) bit; and SQWE is ignored on many current systems.
  333. */
  334. seq_printf(seq,
  335. "periodic_IRQ\t: %s\n"
  336. "update_IRQ\t: %s\n"
  337. "HPET_emulated\t: %s\n"
  338. // "square_wave\t: %s\n"
  339. "BCD\t\t: %s\n"
  340. "DST_enable\t: %s\n"
  341. "periodic_freq\t: %d\n"
  342. "batt_status\t: %s\n",
  343. (rtc_control & RTC_PIE) ? "yes" : "no",
  344. (rtc_control & RTC_UIE) ? "yes" : "no",
  345. is_hpet_enabled() ? "yes" : "no",
  346. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  347. (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  348. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  349. cmos->rtc->irq_freq,
  350. (valid & RTC_VRT) ? "okay" : "dead");
  351. return 0;
  352. }
  353. #else
  354. #define cmos_procfs NULL
  355. #endif
  356. static const struct rtc_class_ops cmos_rtc_ops = {
  357. .read_time = cmos_read_time,
  358. .set_time = cmos_set_time,
  359. .read_alarm = cmos_read_alarm,
  360. .set_alarm = cmos_set_alarm,
  361. .proc = cmos_procfs,
  362. .alarm_irq_enable = cmos_alarm_irq_enable,
  363. };
  364. /*----------------------------------------------------------------*/
  365. /*
  366. * All these chips have at least 64 bytes of address space, shared by
  367. * RTC registers and NVRAM. Most of those bytes of NVRAM are used
  368. * by boot firmware. Modern chips have 128 or 256 bytes.
  369. */
  370. #define NVRAM_OFFSET (RTC_REG_D + 1)
  371. static ssize_t
  372. cmos_nvram_read(struct file *filp, struct kobject *kobj,
  373. struct bin_attribute *attr,
  374. char *buf, loff_t off, size_t count)
  375. {
  376. int retval;
  377. off += NVRAM_OFFSET;
  378. spin_lock_irq(&rtc_lock);
  379. for (retval = 0; count; count--, off++, retval++) {
  380. if (off < 128)
  381. *buf++ = CMOS_READ(off);
  382. else if (can_bank2)
  383. *buf++ = cmos_read_bank2(off);
  384. else
  385. break;
  386. }
  387. spin_unlock_irq(&rtc_lock);
  388. return retval;
  389. }
  390. static ssize_t
  391. cmos_nvram_write(struct file *filp, struct kobject *kobj,
  392. struct bin_attribute *attr,
  393. char *buf, loff_t off, size_t count)
  394. {
  395. struct cmos_rtc *cmos;
  396. int retval;
  397. cmos = dev_get_drvdata(container_of(kobj, struct device, kobj));
  398. /* NOTE: on at least PCs and Ataris, the boot firmware uses a
  399. * checksum on part of the NVRAM data. That's currently ignored
  400. * here. If userspace is smart enough to know what fields of
  401. * NVRAM to update, updating checksums is also part of its job.
  402. */
  403. off += NVRAM_OFFSET;
  404. spin_lock_irq(&rtc_lock);
  405. for (retval = 0; count; count--, off++, retval++) {
  406. /* don't trash RTC registers */
  407. if (off == cmos->day_alrm
  408. || off == cmos->mon_alrm
  409. || off == cmos->century)
  410. buf++;
  411. else if (off < 128)
  412. CMOS_WRITE(*buf++, off);
  413. else if (can_bank2)
  414. cmos_write_bank2(*buf++, off);
  415. else
  416. break;
  417. }
  418. spin_unlock_irq(&rtc_lock);
  419. return retval;
  420. }
  421. static struct bin_attribute nvram = {
  422. .attr = {
  423. .name = "nvram",
  424. .mode = S_IRUGO | S_IWUSR,
  425. },
  426. .read = cmos_nvram_read,
  427. .write = cmos_nvram_write,
  428. /* size gets set up later */
  429. };
  430. /*----------------------------------------------------------------*/
  431. static struct cmos_rtc cmos_rtc;
  432. static irqreturn_t cmos_interrupt(int irq, void *p)
  433. {
  434. u8 irqstat;
  435. u8 rtc_control;
  436. spin_lock(&rtc_lock);
  437. /* When the HPET interrupt handler calls us, the interrupt
  438. * status is passed as arg1 instead of the irq number. But
  439. * always clear irq status, even when HPET is in the way.
  440. *
  441. * Note that HPET and RTC are almost certainly out of phase,
  442. * giving different IRQ status ...
  443. */
  444. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  445. rtc_control = CMOS_READ(RTC_CONTROL);
  446. if (is_hpet_enabled())
  447. irqstat = (unsigned long)irq & 0xF0;
  448. /* If we were suspended, RTC_CONTROL may not be accurate since the
  449. * bios may have cleared it.
  450. */
  451. if (!cmos_rtc.suspend_ctrl)
  452. irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  453. else
  454. irqstat &= (cmos_rtc.suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
  455. /* All Linux RTC alarms should be treated as if they were oneshot.
  456. * Similar code may be needed in system wakeup paths, in case the
  457. * alarm woke the system.
  458. */
  459. if (irqstat & RTC_AIE) {
  460. cmos_rtc.suspend_ctrl &= ~RTC_AIE;
  461. rtc_control &= ~RTC_AIE;
  462. CMOS_WRITE(rtc_control, RTC_CONTROL);
  463. hpet_mask_rtc_irq_bit(RTC_AIE);
  464. CMOS_READ(RTC_INTR_FLAGS);
  465. }
  466. spin_unlock(&rtc_lock);
  467. if (is_intr(irqstat)) {
  468. rtc_update_irq(p, 1, irqstat);
  469. return IRQ_HANDLED;
  470. } else
  471. return IRQ_NONE;
  472. }
  473. #ifdef CONFIG_PNP
  474. #define INITSECTION
  475. #else
  476. #define INITSECTION __init
  477. #endif
  478. static int INITSECTION
  479. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  480. {
  481. struct cmos_rtc_board_info *info = dev_get_platdata(dev);
  482. int retval = 0;
  483. unsigned char rtc_control;
  484. unsigned address_space;
  485. u32 flags = 0;
  486. /* there can be only one ... */
  487. if (cmos_rtc.dev)
  488. return -EBUSY;
  489. if (!ports)
  490. return -ENODEV;
  491. /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
  492. *
  493. * REVISIT non-x86 systems may instead use memory space resources
  494. * (needing ioremap etc), not i/o space resources like this ...
  495. */
  496. if (RTC_IOMAPPED)
  497. ports = request_region(ports->start, resource_size(ports),
  498. driver_name);
  499. else
  500. ports = request_mem_region(ports->start, resource_size(ports),
  501. driver_name);
  502. if (!ports) {
  503. dev_dbg(dev, "i/o registers already in use\n");
  504. return -EBUSY;
  505. }
  506. cmos_rtc.irq = rtc_irq;
  507. cmos_rtc.iomem = ports;
  508. /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
  509. * driver did, but don't reject unknown configs. Old hardware
  510. * won't address 128 bytes. Newer chips have multiple banks,
  511. * though they may not be listed in one I/O resource.
  512. */
  513. #if defined(CONFIG_ATARI)
  514. address_space = 64;
  515. #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
  516. || defined(__sparc__) || defined(__mips__) \
  517. || defined(__powerpc__) || defined(CONFIG_MN10300)
  518. address_space = 128;
  519. #else
  520. #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
  521. address_space = 128;
  522. #endif
  523. if (can_bank2 && ports->end > (ports->start + 1))
  524. address_space = 256;
  525. /* For ACPI systems extension info comes from the FADT. On others,
  526. * board specific setup provides it as appropriate. Systems where
  527. * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
  528. * some almost-clones) can provide hooks to make that behave.
  529. *
  530. * Note that ACPI doesn't preclude putting these registers into
  531. * "extended" areas of the chip, including some that we won't yet
  532. * expect CMOS_READ and friends to handle.
  533. */
  534. if (info) {
  535. if (info->flags)
  536. flags = info->flags;
  537. if (info->address_space)
  538. address_space = info->address_space;
  539. if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
  540. cmos_rtc.day_alrm = info->rtc_day_alarm;
  541. if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
  542. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  543. if (info->rtc_century && info->rtc_century < 128)
  544. cmos_rtc.century = info->rtc_century;
  545. if (info->wake_on && info->wake_off) {
  546. cmos_rtc.wake_on = info->wake_on;
  547. cmos_rtc.wake_off = info->wake_off;
  548. }
  549. }
  550. cmos_rtc.dev = dev;
  551. dev_set_drvdata(dev, &cmos_rtc);
  552. cmos_rtc.rtc = rtc_device_register(driver_name, dev,
  553. &cmos_rtc_ops, THIS_MODULE);
  554. if (IS_ERR(cmos_rtc.rtc)) {
  555. retval = PTR_ERR(cmos_rtc.rtc);
  556. goto cleanup0;
  557. }
  558. rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
  559. spin_lock_irq(&rtc_lock);
  560. if (!(flags & CMOS_RTC_FLAGS_NOFREQ)) {
  561. /* force periodic irq to CMOS reset default of 1024Hz;
  562. *
  563. * REVISIT it's been reported that at least one x86_64 ALI
  564. * mobo doesn't use 32KHz here ... for portability we might
  565. * need to do something about other clock frequencies.
  566. */
  567. cmos_rtc.rtc->irq_freq = 1024;
  568. hpet_set_periodic_freq(cmos_rtc.rtc->irq_freq);
  569. CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
  570. }
  571. /* disable irqs */
  572. if (is_valid_irq(rtc_irq))
  573. cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
  574. rtc_control = CMOS_READ(RTC_CONTROL);
  575. spin_unlock_irq(&rtc_lock);
  576. /* FIXME:
  577. * <asm-generic/rtc.h> doesn't know 12-hour mode either.
  578. */
  579. if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) {
  580. dev_warn(dev, "only 24-hr supported\n");
  581. retval = -ENXIO;
  582. goto cleanup1;
  583. }
  584. hpet_rtc_timer_init();
  585. if (is_valid_irq(rtc_irq)) {
  586. irq_handler_t rtc_cmos_int_handler;
  587. if (is_hpet_enabled()) {
  588. rtc_cmos_int_handler = hpet_rtc_interrupt;
  589. retval = hpet_register_irq_handler(cmos_interrupt);
  590. if (retval) {
  591. hpet_mask_rtc_irq_bit(RTC_IRQMASK);
  592. dev_warn(dev, "hpet_register_irq_handler "
  593. " failed in rtc_init().");
  594. goto cleanup1;
  595. }
  596. } else
  597. rtc_cmos_int_handler = cmos_interrupt;
  598. retval = request_irq(rtc_irq, rtc_cmos_int_handler,
  599. IRQF_SHARED, dev_name(&cmos_rtc.rtc->dev),
  600. cmos_rtc.rtc);
  601. if (retval < 0) {
  602. dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
  603. goto cleanup1;
  604. }
  605. }
  606. /* export at least the first block of NVRAM */
  607. nvram.size = address_space - NVRAM_OFFSET;
  608. retval = sysfs_create_bin_file(&dev->kobj, &nvram);
  609. if (retval < 0) {
  610. dev_dbg(dev, "can't create nvram file? %d\n", retval);
  611. goto cleanup2;
  612. }
  613. dev_info(dev, "%s%s, %zd bytes nvram%s\n",
  614. !is_valid_irq(rtc_irq) ? "no alarms" :
  615. cmos_rtc.mon_alrm ? "alarms up to one year" :
  616. cmos_rtc.day_alrm ? "alarms up to one month" :
  617. "alarms up to one day",
  618. cmos_rtc.century ? ", y3k" : "",
  619. nvram.size,
  620. is_hpet_enabled() ? ", hpet irqs" : "");
  621. return 0;
  622. cleanup2:
  623. if (is_valid_irq(rtc_irq))
  624. free_irq(rtc_irq, cmos_rtc.rtc);
  625. cleanup1:
  626. cmos_rtc.dev = NULL;
  627. rtc_device_unregister(cmos_rtc.rtc);
  628. cleanup0:
  629. if (RTC_IOMAPPED)
  630. release_region(ports->start, resource_size(ports));
  631. else
  632. release_mem_region(ports->start, resource_size(ports));
  633. return retval;
  634. }
  635. static void cmos_do_shutdown(int rtc_irq)
  636. {
  637. spin_lock_irq(&rtc_lock);
  638. if (is_valid_irq(rtc_irq))
  639. cmos_irq_disable(&cmos_rtc, RTC_IRQMASK);
  640. spin_unlock_irq(&rtc_lock);
  641. }
  642. static void cmos_do_remove(struct device *dev)
  643. {
  644. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  645. struct resource *ports;
  646. cmos_do_shutdown(cmos->irq);
  647. sysfs_remove_bin_file(&dev->kobj, &nvram);
  648. if (is_valid_irq(cmos->irq)) {
  649. free_irq(cmos->irq, cmos->rtc);
  650. hpet_unregister_irq_handler(cmos_interrupt);
  651. }
  652. rtc_device_unregister(cmos->rtc);
  653. cmos->rtc = NULL;
  654. ports = cmos->iomem;
  655. if (RTC_IOMAPPED)
  656. release_region(ports->start, resource_size(ports));
  657. else
  658. release_mem_region(ports->start, resource_size(ports));
  659. cmos->iomem = NULL;
  660. cmos->dev = NULL;
  661. }
  662. static int cmos_aie_poweroff(struct device *dev)
  663. {
  664. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  665. struct rtc_time now;
  666. time64_t t_now;
  667. int retval = 0;
  668. unsigned char rtc_control;
  669. if (!cmos->alarm_expires)
  670. return -EINVAL;
  671. spin_lock_irq(&rtc_lock);
  672. rtc_control = CMOS_READ(RTC_CONTROL);
  673. spin_unlock_irq(&rtc_lock);
  674. /* We only care about the situation where AIE is disabled. */
  675. if (rtc_control & RTC_AIE)
  676. return -EBUSY;
  677. cmos_read_time(dev, &now);
  678. t_now = rtc_tm_to_time64(&now);
  679. /*
  680. * When enabling "RTC wake-up" in BIOS setup, the machine reboots
  681. * automatically right after shutdown on some buggy boxes.
  682. * This automatic rebooting issue won't happen when the alarm
  683. * time is larger than now+1 seconds.
  684. *
  685. * If the alarm time is equal to now+1 seconds, the issue can be
  686. * prevented by cancelling the alarm.
  687. */
  688. if (cmos->alarm_expires == t_now + 1) {
  689. struct rtc_wkalrm alarm;
  690. /* Cancel the AIE timer by configuring the past time. */
  691. rtc_time64_to_tm(t_now - 1, &alarm.time);
  692. alarm.enabled = 0;
  693. retval = cmos_set_alarm(dev, &alarm);
  694. } else if (cmos->alarm_expires > t_now + 1) {
  695. retval = -EBUSY;
  696. }
  697. return retval;
  698. }
  699. static int cmos_suspend(struct device *dev)
  700. {
  701. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  702. unsigned char tmp;
  703. /* only the alarm might be a wakeup event source */
  704. spin_lock_irq(&rtc_lock);
  705. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  706. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  707. unsigned char mask;
  708. if (device_may_wakeup(dev))
  709. mask = RTC_IRQMASK & ~RTC_AIE;
  710. else
  711. mask = RTC_IRQMASK;
  712. tmp &= ~mask;
  713. CMOS_WRITE(tmp, RTC_CONTROL);
  714. hpet_mask_rtc_irq_bit(mask);
  715. cmos_checkintr(cmos, tmp);
  716. }
  717. spin_unlock_irq(&rtc_lock);
  718. if (tmp & RTC_AIE) {
  719. cmos->enabled_wake = 1;
  720. if (cmos->wake_on)
  721. cmos->wake_on(dev);
  722. else
  723. enable_irq_wake(cmos->irq);
  724. }
  725. cmos_read_alarm(dev, &cmos->saved_wkalrm);
  726. dev_dbg(dev, "suspend%s, ctrl %02x\n",
  727. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  728. tmp);
  729. return 0;
  730. }
  731. /* We want RTC alarms to wake us from e.g. ACPI G2/S5 "soft off", even
  732. * after a detour through G3 "mechanical off", although the ACPI spec
  733. * says wakeup should only work from G1/S4 "hibernate". To most users,
  734. * distinctions between S4 and S5 are pointless. So when the hardware
  735. * allows, don't draw that distinction.
  736. */
  737. static inline int cmos_poweroff(struct device *dev)
  738. {
  739. if (!IS_ENABLED(CONFIG_PM))
  740. return -ENOSYS;
  741. return cmos_suspend(dev);
  742. }
  743. static void cmos_check_wkalrm(struct device *dev)
  744. {
  745. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  746. struct rtc_wkalrm current_alarm;
  747. time64_t t_current_expires;
  748. time64_t t_saved_expires;
  749. cmos_read_alarm(dev, &current_alarm);
  750. t_current_expires = rtc_tm_to_time64(&current_alarm.time);
  751. t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time);
  752. if (t_current_expires != t_saved_expires ||
  753. cmos->saved_wkalrm.enabled != current_alarm.enabled) {
  754. cmos_set_alarm(dev, &cmos->saved_wkalrm);
  755. }
  756. }
  757. static void cmos_check_acpi_rtc_status(struct device *dev,
  758. unsigned char *rtc_control);
  759. static int __maybe_unused cmos_resume(struct device *dev)
  760. {
  761. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  762. unsigned char tmp;
  763. if (cmos->enabled_wake) {
  764. if (cmos->wake_off)
  765. cmos->wake_off(dev);
  766. else
  767. disable_irq_wake(cmos->irq);
  768. cmos->enabled_wake = 0;
  769. }
  770. /* The BIOS might have changed the alarm, restore it */
  771. cmos_check_wkalrm(dev);
  772. spin_lock_irq(&rtc_lock);
  773. tmp = cmos->suspend_ctrl;
  774. cmos->suspend_ctrl = 0;
  775. /* re-enable any irqs previously active */
  776. if (tmp & RTC_IRQMASK) {
  777. unsigned char mask;
  778. if (device_may_wakeup(dev))
  779. hpet_rtc_timer_init();
  780. do {
  781. CMOS_WRITE(tmp, RTC_CONTROL);
  782. hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
  783. mask = CMOS_READ(RTC_INTR_FLAGS);
  784. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  785. if (!is_hpet_enabled() || !is_intr(mask))
  786. break;
  787. /* force one-shot behavior if HPET blocked
  788. * the wake alarm's irq
  789. */
  790. rtc_update_irq(cmos->rtc, 1, mask);
  791. tmp &= ~RTC_AIE;
  792. hpet_mask_rtc_irq_bit(RTC_AIE);
  793. } while (mask & RTC_AIE);
  794. if (tmp & RTC_AIE)
  795. cmos_check_acpi_rtc_status(dev, &tmp);
  796. }
  797. spin_unlock_irq(&rtc_lock);
  798. dev_dbg(dev, "resume, ctrl %02x\n", tmp);
  799. return 0;
  800. }
  801. static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
  802. /*----------------------------------------------------------------*/
  803. /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
  804. * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
  805. * probably list them in similar PNPBIOS tables; so PNP is more common.
  806. *
  807. * We don't use legacy "poke at the hardware" probing. Ancient PCs that
  808. * predate even PNPBIOS should set up platform_bus devices.
  809. */
  810. #ifdef CONFIG_ACPI
  811. #include <linux/acpi.h>
  812. static u32 rtc_handler(void *context)
  813. {
  814. struct device *dev = context;
  815. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  816. unsigned char rtc_control = 0;
  817. unsigned char rtc_intr;
  818. unsigned long flags;
  819. spin_lock_irqsave(&rtc_lock, flags);
  820. if (cmos_rtc.suspend_ctrl)
  821. rtc_control = CMOS_READ(RTC_CONTROL);
  822. if (rtc_control & RTC_AIE) {
  823. cmos_rtc.suspend_ctrl &= ~RTC_AIE;
  824. CMOS_WRITE(rtc_control, RTC_CONTROL);
  825. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  826. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  827. }
  828. spin_unlock_irqrestore(&rtc_lock, flags);
  829. pm_wakeup_event(dev, 0);
  830. acpi_clear_event(ACPI_EVENT_RTC);
  831. acpi_disable_event(ACPI_EVENT_RTC, 0);
  832. return ACPI_INTERRUPT_HANDLED;
  833. }
  834. static inline void rtc_wake_setup(struct device *dev)
  835. {
  836. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev);
  837. /*
  838. * After the RTC handler is installed, the Fixed_RTC event should
  839. * be disabled. Only when the RTC alarm is set will it be enabled.
  840. */
  841. acpi_clear_event(ACPI_EVENT_RTC);
  842. acpi_disable_event(ACPI_EVENT_RTC, 0);
  843. }
  844. static void rtc_wake_on(struct device *dev)
  845. {
  846. acpi_clear_event(ACPI_EVENT_RTC);
  847. acpi_enable_event(ACPI_EVENT_RTC, 0);
  848. }
  849. static void rtc_wake_off(struct device *dev)
  850. {
  851. acpi_disable_event(ACPI_EVENT_RTC, 0);
  852. }
  853. /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
  854. * its device node and pass extra config data. This helps its driver use
  855. * capabilities that the now-obsolete mc146818 didn't have, and informs it
  856. * that this board's RTC is wakeup-capable (per ACPI spec).
  857. */
  858. static struct cmos_rtc_board_info acpi_rtc_info;
  859. static void cmos_wake_setup(struct device *dev)
  860. {
  861. if (acpi_disabled)
  862. return;
  863. rtc_wake_setup(dev);
  864. acpi_rtc_info.wake_on = rtc_wake_on;
  865. acpi_rtc_info.wake_off = rtc_wake_off;
  866. /* workaround bug in some ACPI tables */
  867. if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
  868. dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
  869. acpi_gbl_FADT.month_alarm);
  870. acpi_gbl_FADT.month_alarm = 0;
  871. }
  872. acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
  873. acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
  874. acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
  875. /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
  876. if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
  877. dev_info(dev, "RTC can wake from S4\n");
  878. dev->platform_data = &acpi_rtc_info;
  879. /* RTC always wakes from S1/S2/S3, and often S4/STD */
  880. device_init_wakeup(dev, 1);
  881. }
  882. static void cmos_check_acpi_rtc_status(struct device *dev,
  883. unsigned char *rtc_control)
  884. {
  885. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  886. acpi_event_status rtc_status;
  887. acpi_status status;
  888. if (acpi_gbl_FADT.flags & ACPI_FADT_FIXED_RTC)
  889. return;
  890. status = acpi_get_event_status(ACPI_EVENT_RTC, &rtc_status);
  891. if (ACPI_FAILURE(status)) {
  892. dev_err(dev, "Could not get RTC status\n");
  893. } else if (rtc_status & ACPI_EVENT_FLAG_SET) {
  894. unsigned char mask;
  895. *rtc_control &= ~RTC_AIE;
  896. CMOS_WRITE(*rtc_control, RTC_CONTROL);
  897. mask = CMOS_READ(RTC_INTR_FLAGS);
  898. rtc_update_irq(cmos->rtc, 1, mask);
  899. }
  900. }
  901. #else
  902. static void cmos_wake_setup(struct device *dev)
  903. {
  904. }
  905. static void cmos_check_acpi_rtc_status(struct device *dev,
  906. unsigned char *rtc_control)
  907. {
  908. }
  909. #endif
  910. #ifdef CONFIG_PNP
  911. #include <linux/pnp.h>
  912. static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  913. {
  914. cmos_wake_setup(&pnp->dev);
  915. if (pnp_port_start(pnp, 0) == 0x70 && !pnp_irq_valid(pnp, 0))
  916. /* Some machines contain a PNP entry for the RTC, but
  917. * don't define the IRQ. It should always be safe to
  918. * hardcode it in these cases
  919. */
  920. return cmos_do_probe(&pnp->dev,
  921. pnp_get_resource(pnp, IORESOURCE_IO, 0), 8);
  922. else
  923. return cmos_do_probe(&pnp->dev,
  924. pnp_get_resource(pnp, IORESOURCE_IO, 0),
  925. pnp_irq(pnp, 0));
  926. }
  927. static void cmos_pnp_remove(struct pnp_dev *pnp)
  928. {
  929. cmos_do_remove(&pnp->dev);
  930. }
  931. static void cmos_pnp_shutdown(struct pnp_dev *pnp)
  932. {
  933. struct device *dev = &pnp->dev;
  934. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  935. if (system_state == SYSTEM_POWER_OFF) {
  936. int retval = cmos_poweroff(dev);
  937. if (cmos_aie_poweroff(dev) < 0 && !retval)
  938. return;
  939. }
  940. cmos_do_shutdown(cmos->irq);
  941. }
  942. static const struct pnp_device_id rtc_ids[] = {
  943. { .id = "PNP0b00", },
  944. { .id = "PNP0b01", },
  945. { .id = "PNP0b02", },
  946. { },
  947. };
  948. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  949. static struct pnp_driver cmos_pnp_driver = {
  950. .name = (char *) driver_name,
  951. .id_table = rtc_ids,
  952. .probe = cmos_pnp_probe,
  953. .remove = cmos_pnp_remove,
  954. .shutdown = cmos_pnp_shutdown,
  955. /* flag ensures resume() gets called, and stops syslog spam */
  956. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  957. .driver = {
  958. .pm = &cmos_pm_ops,
  959. },
  960. };
  961. #endif /* CONFIG_PNP */
  962. #ifdef CONFIG_OF
  963. static const struct of_device_id of_cmos_match[] = {
  964. {
  965. .compatible = "motorola,mc146818",
  966. },
  967. { },
  968. };
  969. MODULE_DEVICE_TABLE(of, of_cmos_match);
  970. static __init void cmos_of_init(struct platform_device *pdev)
  971. {
  972. struct device_node *node = pdev->dev.of_node;
  973. struct rtc_time time;
  974. int ret;
  975. const __be32 *val;
  976. if (!node)
  977. return;
  978. val = of_get_property(node, "ctrl-reg", NULL);
  979. if (val)
  980. CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL);
  981. val = of_get_property(node, "freq-reg", NULL);
  982. if (val)
  983. CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
  984. cmos_read_time(&pdev->dev, &time);
  985. ret = rtc_valid_tm(&time);
  986. if (ret) {
  987. struct rtc_time def_time = {
  988. .tm_year = 1,
  989. .tm_mday = 1,
  990. };
  991. cmos_set_time(&pdev->dev, &def_time);
  992. }
  993. }
  994. #else
  995. static inline void cmos_of_init(struct platform_device *pdev) {}
  996. #endif
  997. /*----------------------------------------------------------------*/
  998. /* Platform setup should have set up an RTC device, when PNP is
  999. * unavailable ... this could happen even on (older) PCs.
  1000. */
  1001. static int __init cmos_platform_probe(struct platform_device *pdev)
  1002. {
  1003. struct resource *resource;
  1004. int irq;
  1005. cmos_of_init(pdev);
  1006. cmos_wake_setup(&pdev->dev);
  1007. if (RTC_IOMAPPED)
  1008. resource = platform_get_resource(pdev, IORESOURCE_IO, 0);
  1009. else
  1010. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1011. irq = platform_get_irq(pdev, 0);
  1012. if (irq < 0)
  1013. irq = -1;
  1014. return cmos_do_probe(&pdev->dev, resource, irq);
  1015. }
  1016. static int cmos_platform_remove(struct platform_device *pdev)
  1017. {
  1018. cmos_do_remove(&pdev->dev);
  1019. return 0;
  1020. }
  1021. static void cmos_platform_shutdown(struct platform_device *pdev)
  1022. {
  1023. struct device *dev = &pdev->dev;
  1024. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1025. if (system_state == SYSTEM_POWER_OFF) {
  1026. int retval = cmos_poweroff(dev);
  1027. if (cmos_aie_poweroff(dev) < 0 && !retval)
  1028. return;
  1029. }
  1030. cmos_do_shutdown(cmos->irq);
  1031. }
  1032. /* work with hotplug and coldplug */
  1033. MODULE_ALIAS("platform:rtc_cmos");
  1034. static struct platform_driver cmos_platform_driver = {
  1035. .remove = cmos_platform_remove,
  1036. .shutdown = cmos_platform_shutdown,
  1037. .driver = {
  1038. .name = driver_name,
  1039. .pm = &cmos_pm_ops,
  1040. .of_match_table = of_match_ptr(of_cmos_match),
  1041. }
  1042. };
  1043. #ifdef CONFIG_PNP
  1044. static bool pnp_driver_registered;
  1045. #endif
  1046. static bool platform_driver_registered;
  1047. static int __init cmos_init(void)
  1048. {
  1049. int retval = 0;
  1050. #ifdef CONFIG_PNP
  1051. retval = pnp_register_driver(&cmos_pnp_driver);
  1052. if (retval == 0)
  1053. pnp_driver_registered = true;
  1054. #endif
  1055. if (!cmos_rtc.dev) {
  1056. retval = platform_driver_probe(&cmos_platform_driver,
  1057. cmos_platform_probe);
  1058. if (retval == 0)
  1059. platform_driver_registered = true;
  1060. }
  1061. if (retval == 0)
  1062. return 0;
  1063. #ifdef CONFIG_PNP
  1064. if (pnp_driver_registered)
  1065. pnp_unregister_driver(&cmos_pnp_driver);
  1066. #endif
  1067. return retval;
  1068. }
  1069. module_init(cmos_init);
  1070. static void __exit cmos_exit(void)
  1071. {
  1072. #ifdef CONFIG_PNP
  1073. if (pnp_driver_registered)
  1074. pnp_unregister_driver(&cmos_pnp_driver);
  1075. #endif
  1076. if (platform_driver_registered)
  1077. platform_driver_unregister(&cmos_platform_driver);
  1078. }
  1079. module_exit(cmos_exit);
  1080. MODULE_AUTHOR("David Brownell");
  1081. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  1082. MODULE_LICENSE("GPL");