rtc-ds1685.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time
  3. * chips.
  4. *
  5. * Copyright (C) 2011-2014 Joshua Kinard <kumba@gentoo.org>.
  6. * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>.
  7. *
  8. * References:
  9. * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10.
  10. * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10.
  11. * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105.
  12. * Application Note 90, Using the Multiplex Bus RTC Extended Features.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/bcd.h>
  20. #include <linux/delay.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/rtc.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/rtc/ds1685.h>
  27. #ifdef CONFIG_PROC_FS
  28. #include <linux/proc_fs.h>
  29. #endif
  30. /* ----------------------------------------------------------------------- */
  31. /* Standard read/write functions if platform does not provide overrides */
  32. /**
  33. * ds1685_read - read a value from an rtc register.
  34. * @rtc: pointer to the ds1685 rtc structure.
  35. * @reg: the register address to read.
  36. */
  37. static u8
  38. ds1685_read(struct ds1685_priv *rtc, int reg)
  39. {
  40. return readb((u8 __iomem *)rtc->regs +
  41. (reg * rtc->regstep));
  42. }
  43. /**
  44. * ds1685_write - write a value to an rtc register.
  45. * @rtc: pointer to the ds1685 rtc structure.
  46. * @reg: the register address to write.
  47. * @value: value to write to the register.
  48. */
  49. static void
  50. ds1685_write(struct ds1685_priv *rtc, int reg, u8 value)
  51. {
  52. writeb(value, ((u8 __iomem *)rtc->regs +
  53. (reg * rtc->regstep)));
  54. }
  55. /* ----------------------------------------------------------------------- */
  56. /* ----------------------------------------------------------------------- */
  57. /* Inlined functions */
  58. /**
  59. * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD.
  60. * @rtc: pointer to the ds1685 rtc structure.
  61. * @val: u8 time value to consider converting.
  62. * @bcd_mask: u8 mask value if BCD mode is used.
  63. * @bin_mask: u8 mask value if BIN mode is used.
  64. *
  65. * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE.
  66. */
  67. static inline u8
  68. ds1685_rtc_bcd2bin(struct ds1685_priv *rtc, u8 val, u8 bcd_mask, u8 bin_mask)
  69. {
  70. if (rtc->bcd_mode)
  71. return (bcd2bin(val) & bcd_mask);
  72. return (val & bin_mask);
  73. }
  74. /**
  75. * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
  76. * @rtc: pointer to the ds1685 rtc structure.
  77. * @val: u8 time value to consider converting.
  78. * @bin_mask: u8 mask value if BIN mode is used.
  79. * @bcd_mask: u8 mask value if BCD mode is used.
  80. *
  81. * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE.
  82. */
  83. static inline u8
  84. ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask)
  85. {
  86. if (rtc->bcd_mode)
  87. return (bin2bcd(val) & bcd_mask);
  88. return (val & bin_mask);
  89. }
  90. /**
  91. * s1685_rtc_check_mday - check validity of the day of month.
  92. * @rtc: pointer to the ds1685 rtc structure.
  93. * @mday: day of month.
  94. *
  95. * Returns -EDOM if the day of month is not within 1..31 range.
  96. */
  97. static inline int
  98. ds1685_rtc_check_mday(struct ds1685_priv *rtc, u8 mday)
  99. {
  100. if (rtc->bcd_mode) {
  101. if (mday < 0x01 || mday > 0x31 || (mday & 0x0f) > 0x09)
  102. return -EDOM;
  103. } else {
  104. if (mday < 1 || mday > 31)
  105. return -EDOM;
  106. }
  107. return 0;
  108. }
  109. /**
  110. * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
  111. * @rtc: pointer to the ds1685 rtc structure.
  112. */
  113. static inline void
  114. ds1685_rtc_switch_to_bank0(struct ds1685_priv *rtc)
  115. {
  116. rtc->write(rtc, RTC_CTRL_A,
  117. (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0)));
  118. }
  119. /**
  120. * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1.
  121. * @rtc: pointer to the ds1685 rtc structure.
  122. */
  123. static inline void
  124. ds1685_rtc_switch_to_bank1(struct ds1685_priv *rtc)
  125. {
  126. rtc->write(rtc, RTC_CTRL_A,
  127. (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0));
  128. }
  129. /**
  130. * ds1685_rtc_begin_data_access - prepare the rtc for data access.
  131. * @rtc: pointer to the ds1685 rtc structure.
  132. *
  133. * This takes several steps to prepare the rtc for access to get/set time
  134. * and alarm values from the rtc registers:
  135. * - Sets the SET bit in Control Register B.
  136. * - Reads Ext Control Register 4A and checks the INCR bit.
  137. * - If INCR is active, a short delay is added before Ext Control Register 4A
  138. * is read again in a loop until INCR is inactive.
  139. * - Switches the rtc to bank 1. This allows access to all relevant
  140. * data for normal rtc operation, as bank 0 contains only the nvram.
  141. */
  142. static inline void
  143. ds1685_rtc_begin_data_access(struct ds1685_priv *rtc)
  144. {
  145. /* Set the SET bit in Ctrl B */
  146. rtc->write(rtc, RTC_CTRL_B,
  147. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  148. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  149. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  150. cpu_relax();
  151. /* Switch to Bank 1 */
  152. ds1685_rtc_switch_to_bank1(rtc);
  153. }
  154. /**
  155. * ds1685_rtc_end_data_access - end data access on the rtc.
  156. * @rtc: pointer to the ds1685 rtc structure.
  157. *
  158. * This ends what was started by ds1685_rtc_begin_data_access:
  159. * - Switches the rtc back to bank 0.
  160. * - Clears the SET bit in Control Register B.
  161. */
  162. static inline void
  163. ds1685_rtc_end_data_access(struct ds1685_priv *rtc)
  164. {
  165. /* Switch back to Bank 0 */
  166. ds1685_rtc_switch_to_bank1(rtc);
  167. /* Clear the SET bit in Ctrl B */
  168. rtc->write(rtc, RTC_CTRL_B,
  169. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  170. }
  171. /**
  172. * ds1685_rtc_begin_ctrl_access - prepare the rtc for ctrl access.
  173. * @rtc: pointer to the ds1685 rtc structure.
  174. * @flags: irq flags variable for spin_lock_irqsave.
  175. *
  176. * This takes several steps to prepare the rtc for access to read just the
  177. * control registers:
  178. * - Sets a spinlock on the rtc IRQ.
  179. * - Switches the rtc to bank 1. This allows access to the two extended
  180. * control registers.
  181. *
  182. * Only use this where you are certain another lock will not be held.
  183. */
  184. static inline void
  185. ds1685_rtc_begin_ctrl_access(struct ds1685_priv *rtc, unsigned long *flags)
  186. {
  187. spin_lock_irqsave(&rtc->lock, *flags);
  188. ds1685_rtc_switch_to_bank1(rtc);
  189. }
  190. /**
  191. * ds1685_rtc_end_ctrl_access - end ctrl access on the rtc.
  192. * @rtc: pointer to the ds1685 rtc structure.
  193. * @flags: irq flags variable for spin_unlock_irqrestore.
  194. *
  195. * This ends what was started by ds1685_rtc_begin_ctrl_access:
  196. * - Switches the rtc back to bank 0.
  197. * - Unsets the spinlock on the rtc IRQ.
  198. */
  199. static inline void
  200. ds1685_rtc_end_ctrl_access(struct ds1685_priv *rtc, unsigned long flags)
  201. {
  202. ds1685_rtc_switch_to_bank0(rtc);
  203. spin_unlock_irqrestore(&rtc->lock, flags);
  204. }
  205. /**
  206. * ds1685_rtc_get_ssn - retrieve the silicon serial number.
  207. * @rtc: pointer to the ds1685 rtc structure.
  208. * @ssn: u8 array to hold the bits of the silicon serial number.
  209. *
  210. * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
  211. * first byte is the model number, the next six bytes are the serial number
  212. * digits, and the final byte is a CRC check byte. Together, they form the
  213. * silicon serial number.
  214. *
  215. * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be
  216. * called first before calling this function, else data will be read out of
  217. * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done.
  218. */
  219. static inline void
  220. ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn)
  221. {
  222. ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL);
  223. ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1);
  224. ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2);
  225. ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3);
  226. ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4);
  227. ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5);
  228. ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6);
  229. ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC);
  230. }
  231. /* ----------------------------------------------------------------------- */
  232. /* ----------------------------------------------------------------------- */
  233. /* Read/Set Time & Alarm functions */
  234. /**
  235. * ds1685_rtc_read_time - reads the time registers.
  236. * @dev: pointer to device structure.
  237. * @tm: pointer to rtc_time structure.
  238. */
  239. static int
  240. ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm)
  241. {
  242. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  243. u8 ctrlb, century;
  244. u8 seconds, minutes, hours, wday, mday, month, years;
  245. /* Fetch the time info from the RTC registers. */
  246. ds1685_rtc_begin_data_access(rtc);
  247. seconds = rtc->read(rtc, RTC_SECS);
  248. minutes = rtc->read(rtc, RTC_MINS);
  249. hours = rtc->read(rtc, RTC_HRS);
  250. wday = rtc->read(rtc, RTC_WDAY);
  251. mday = rtc->read(rtc, RTC_MDAY);
  252. month = rtc->read(rtc, RTC_MONTH);
  253. years = rtc->read(rtc, RTC_YEAR);
  254. century = rtc->read(rtc, RTC_CENTURY);
  255. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  256. ds1685_rtc_end_data_access(rtc);
  257. /* bcd2bin if needed, perform fixups, and store to rtc_time. */
  258. years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
  259. RTC_YEAR_BIN_MASK);
  260. century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
  261. RTC_CENTURY_MASK);
  262. tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
  263. RTC_SECS_BIN_MASK);
  264. tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
  265. RTC_MINS_BIN_MASK);
  266. tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
  267. RTC_HRS_24_BIN_MASK);
  268. tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
  269. RTC_WDAY_MASK) - 1);
  270. tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  271. RTC_MDAY_BIN_MASK);
  272. tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
  273. RTC_MONTH_BIN_MASK) - 1);
  274. tm->tm_year = ((years + (century * 100)) - 1900);
  275. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  276. tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
  277. return 0;
  278. }
  279. /**
  280. * ds1685_rtc_set_time - sets the time registers.
  281. * @dev: pointer to device structure.
  282. * @tm: pointer to rtc_time structure.
  283. */
  284. static int
  285. ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
  286. {
  287. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  288. u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
  289. /* Fetch the time info from rtc_time. */
  290. seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
  291. RTC_SECS_BCD_MASK);
  292. minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
  293. RTC_MINS_BCD_MASK);
  294. hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
  295. RTC_HRS_24_BCD_MASK);
  296. wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
  297. RTC_WDAY_MASK);
  298. mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
  299. RTC_MDAY_BCD_MASK);
  300. month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
  301. RTC_MONTH_BCD_MASK);
  302. years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
  303. RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
  304. century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
  305. RTC_CENTURY_MASK, RTC_CENTURY_MASK);
  306. /*
  307. * Perform Sanity Checks:
  308. * - Months: !> 12, Month Day != 0.
  309. * - Month Day !> Max days in current month.
  310. * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
  311. */
  312. if ((tm->tm_mon > 11) || (mday == 0))
  313. return -EDOM;
  314. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
  315. return -EDOM;
  316. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
  317. (tm->tm_sec >= 60) || (wday > 7))
  318. return -EDOM;
  319. /*
  320. * Set the data mode to use and store the time values in the
  321. * RTC registers.
  322. */
  323. ds1685_rtc_begin_data_access(rtc);
  324. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  325. if (rtc->bcd_mode)
  326. ctrlb &= ~(RTC_CTRL_B_DM);
  327. else
  328. ctrlb |= RTC_CTRL_B_DM;
  329. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  330. rtc->write(rtc, RTC_SECS, seconds);
  331. rtc->write(rtc, RTC_MINS, minutes);
  332. rtc->write(rtc, RTC_HRS, hours);
  333. rtc->write(rtc, RTC_WDAY, wday);
  334. rtc->write(rtc, RTC_MDAY, mday);
  335. rtc->write(rtc, RTC_MONTH, month);
  336. rtc->write(rtc, RTC_YEAR, years);
  337. rtc->write(rtc, RTC_CENTURY, century);
  338. ds1685_rtc_end_data_access(rtc);
  339. return 0;
  340. }
  341. /**
  342. * ds1685_rtc_read_alarm - reads the alarm registers.
  343. * @dev: pointer to device structure.
  344. * @alrm: pointer to rtc_wkalrm structure.
  345. *
  346. * There are three primary alarm registers: seconds, minutes, and hours.
  347. * A fourth alarm register for the month date is also available in bank1 for
  348. * kickstart/wakeup features. The DS1685/DS1687 manual states that a
  349. * "don't care" value ranging from 0xc0 to 0xff may be written into one or
  350. * more of the three alarm bytes to act as a wildcard value. The fourth
  351. * byte doesn't support a "don't care" value.
  352. */
  353. static int
  354. ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  355. {
  356. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  357. u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
  358. int ret;
  359. /* Fetch the alarm info from the RTC alarm registers. */
  360. ds1685_rtc_begin_data_access(rtc);
  361. seconds = rtc->read(rtc, RTC_SECS_ALARM);
  362. minutes = rtc->read(rtc, RTC_MINS_ALARM);
  363. hours = rtc->read(rtc, RTC_HRS_ALARM);
  364. mday = rtc->read(rtc, RTC_MDAY_ALARM);
  365. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  366. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  367. ds1685_rtc_end_data_access(rtc);
  368. /* Check the month date for validity. */
  369. ret = ds1685_rtc_check_mday(rtc, mday);
  370. if (ret)
  371. return ret;
  372. /*
  373. * Check the three alarm bytes.
  374. *
  375. * The Linux RTC system doesn't support the "don't care" capability
  376. * of this RTC chip. We check for it anyways in case support is
  377. * added in the future and only assign when we care.
  378. */
  379. if (likely(seconds < 0xc0))
  380. alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
  381. RTC_SECS_BCD_MASK,
  382. RTC_SECS_BIN_MASK);
  383. if (likely(minutes < 0xc0))
  384. alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
  385. RTC_MINS_BCD_MASK,
  386. RTC_MINS_BIN_MASK);
  387. if (likely(hours < 0xc0))
  388. alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
  389. RTC_HRS_24_BCD_MASK,
  390. RTC_HRS_24_BIN_MASK);
  391. /* Write the data to rtc_wkalrm. */
  392. alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  393. RTC_MDAY_BIN_MASK);
  394. alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
  395. alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
  396. return 0;
  397. }
  398. /**
  399. * ds1685_rtc_set_alarm - sets the alarm in registers.
  400. * @dev: pointer to device structure.
  401. * @alrm: pointer to rtc_wkalrm structure.
  402. */
  403. static int
  404. ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  405. {
  406. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  407. u8 ctrlb, seconds, minutes, hours, mday;
  408. int ret;
  409. /* Fetch the alarm info and convert to BCD. */
  410. seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
  411. RTC_SECS_BIN_MASK,
  412. RTC_SECS_BCD_MASK);
  413. minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
  414. RTC_MINS_BIN_MASK,
  415. RTC_MINS_BCD_MASK);
  416. hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
  417. RTC_HRS_24_BIN_MASK,
  418. RTC_HRS_24_BCD_MASK);
  419. mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
  420. RTC_MDAY_BIN_MASK,
  421. RTC_MDAY_BCD_MASK);
  422. /* Check the month date for validity. */
  423. ret = ds1685_rtc_check_mday(rtc, mday);
  424. if (ret)
  425. return ret;
  426. /*
  427. * Check the three alarm bytes.
  428. *
  429. * The Linux RTC system doesn't support the "don't care" capability
  430. * of this RTC chip because rtc_valid_tm tries to validate every
  431. * field, and we only support four fields. We put the support
  432. * here anyways for the future.
  433. */
  434. if (unlikely(seconds >= 0xc0))
  435. seconds = 0xff;
  436. if (unlikely(minutes >= 0xc0))
  437. minutes = 0xff;
  438. if (unlikely(hours >= 0xc0))
  439. hours = 0xff;
  440. alrm->time.tm_mon = -1;
  441. alrm->time.tm_year = -1;
  442. alrm->time.tm_wday = -1;
  443. alrm->time.tm_yday = -1;
  444. alrm->time.tm_isdst = -1;
  445. /* Disable the alarm interrupt first. */
  446. ds1685_rtc_begin_data_access(rtc);
  447. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  448. rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE)));
  449. /* Read ctrlc to clear RTC_CTRL_C_AF. */
  450. rtc->read(rtc, RTC_CTRL_C);
  451. /*
  452. * Set the data mode to use and store the time values in the
  453. * RTC registers.
  454. */
  455. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  456. if (rtc->bcd_mode)
  457. ctrlb &= ~(RTC_CTRL_B_DM);
  458. else
  459. ctrlb |= RTC_CTRL_B_DM;
  460. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  461. rtc->write(rtc, RTC_SECS_ALARM, seconds);
  462. rtc->write(rtc, RTC_MINS_ALARM, minutes);
  463. rtc->write(rtc, RTC_HRS_ALARM, hours);
  464. rtc->write(rtc, RTC_MDAY_ALARM, mday);
  465. /* Re-enable the alarm if needed. */
  466. if (alrm->enabled) {
  467. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  468. ctrlb |= RTC_CTRL_B_AIE;
  469. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  470. }
  471. /* Done! */
  472. ds1685_rtc_end_data_access(rtc);
  473. return 0;
  474. }
  475. /* ----------------------------------------------------------------------- */
  476. /* ----------------------------------------------------------------------- */
  477. /* /dev/rtcX Interface functions */
  478. /**
  479. * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off.
  480. * @dev: pointer to device structure.
  481. * @enabled: flag indicating whether to enable or disable.
  482. */
  483. static int
  484. ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  485. {
  486. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  487. unsigned long flags = 0;
  488. /* Enable/disable the Alarm IRQ-Enable flag. */
  489. spin_lock_irqsave(&rtc->lock, flags);
  490. /* Flip the requisite interrupt-enable bit. */
  491. if (enabled)
  492. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
  493. RTC_CTRL_B_AIE));
  494. else
  495. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
  496. ~(RTC_CTRL_B_AIE)));
  497. /* Read Control C to clear all the flag bits. */
  498. rtc->read(rtc, RTC_CTRL_C);
  499. spin_unlock_irqrestore(&rtc->lock, flags);
  500. return 0;
  501. }
  502. /* ----------------------------------------------------------------------- */
  503. /* ----------------------------------------------------------------------- */
  504. /* IRQ handler & workqueue. */
  505. /**
  506. * ds1685_rtc_irq_handler - IRQ handler.
  507. * @irq: IRQ number.
  508. * @dev_id: platform device pointer.
  509. */
  510. static irqreturn_t
  511. ds1685_rtc_irq_handler(int irq, void *dev_id)
  512. {
  513. struct platform_device *pdev = dev_id;
  514. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  515. u8 ctrlb, ctrlc;
  516. unsigned long events = 0;
  517. u8 num_irqs = 0;
  518. /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
  519. if (unlikely(!rtc))
  520. return IRQ_HANDLED;
  521. /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
  522. spin_lock(&rtc->lock);
  523. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  524. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  525. /* Is the IRQF bit set? */
  526. if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
  527. /*
  528. * We need to determine if it was one of the standard
  529. * events: PF, AF, or UF. If so, we handle them and
  530. * update the RTC core.
  531. */
  532. if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
  533. events = RTC_IRQF;
  534. /* Check for a periodic interrupt. */
  535. if ((ctrlb & RTC_CTRL_B_PIE) &&
  536. (ctrlc & RTC_CTRL_C_PF)) {
  537. events |= RTC_PF;
  538. num_irqs++;
  539. }
  540. /* Check for an alarm interrupt. */
  541. if ((ctrlb & RTC_CTRL_B_AIE) &&
  542. (ctrlc & RTC_CTRL_C_AF)) {
  543. events |= RTC_AF;
  544. num_irqs++;
  545. }
  546. /* Check for an update interrupt. */
  547. if ((ctrlb & RTC_CTRL_B_UIE) &&
  548. (ctrlc & RTC_CTRL_C_UF)) {
  549. events |= RTC_UF;
  550. num_irqs++;
  551. }
  552. rtc_update_irq(rtc->dev, num_irqs, events);
  553. } else {
  554. /*
  555. * One of the "extended" interrupts was received that
  556. * is not recognized by the RTC core. These need to
  557. * be handled in task context as they can call other
  558. * functions and the time spent in irq context needs
  559. * to be minimized. Schedule them into a workqueue
  560. * and inform the RTC core that the IRQs were handled.
  561. */
  562. spin_unlock(&rtc->lock);
  563. schedule_work(&rtc->work);
  564. rtc_update_irq(rtc->dev, 0, 0);
  565. return IRQ_HANDLED;
  566. }
  567. }
  568. spin_unlock(&rtc->lock);
  569. return events ? IRQ_HANDLED : IRQ_NONE;
  570. }
  571. /**
  572. * ds1685_rtc_work_queue - work queue handler.
  573. * @work: work_struct containing data to work on in task context.
  574. */
  575. static void
  576. ds1685_rtc_work_queue(struct work_struct *work)
  577. {
  578. struct ds1685_priv *rtc = container_of(work,
  579. struct ds1685_priv, work);
  580. struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
  581. struct mutex *rtc_mutex = &rtc->dev->ops_lock;
  582. u8 ctrl4a, ctrl4b;
  583. mutex_lock(rtc_mutex);
  584. ds1685_rtc_switch_to_bank1(rtc);
  585. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  586. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  587. /*
  588. * Check for a kickstart interrupt. With Vcc applied, this
  589. * typically means that the power button was pressed, so we
  590. * begin the shutdown sequence.
  591. */
  592. if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
  593. /* Briefly disable kickstarts to debounce button presses. */
  594. rtc->write(rtc, RTC_EXT_CTRL_4B,
  595. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  596. ~(RTC_CTRL_4B_KSE)));
  597. /* Clear the kickstart flag. */
  598. rtc->write(rtc, RTC_EXT_CTRL_4A,
  599. (ctrl4a & ~(RTC_CTRL_4A_KF)));
  600. /*
  601. * Sleep 500ms before re-enabling kickstarts. This allows
  602. * adequate time to avoid reading signal jitter as additional
  603. * button presses.
  604. */
  605. msleep(500);
  606. rtc->write(rtc, RTC_EXT_CTRL_4B,
  607. (rtc->read(rtc, RTC_EXT_CTRL_4B) |
  608. RTC_CTRL_4B_KSE));
  609. /* Call the platform pre-poweroff function. Else, shutdown. */
  610. if (rtc->prepare_poweroff != NULL)
  611. rtc->prepare_poweroff();
  612. else
  613. ds1685_rtc_poweroff(pdev);
  614. }
  615. /*
  616. * Check for a wake-up interrupt. With Vcc applied, this is
  617. * essentially a second alarm interrupt, except it takes into
  618. * account the 'date' register in bank1 in addition to the
  619. * standard three alarm registers.
  620. */
  621. if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
  622. rtc->write(rtc, RTC_EXT_CTRL_4A,
  623. (ctrl4a & ~(RTC_CTRL_4A_WF)));
  624. /* Call the platform wake_alarm function if defined. */
  625. if (rtc->wake_alarm != NULL)
  626. rtc->wake_alarm();
  627. else
  628. dev_warn(&pdev->dev,
  629. "Wake Alarm IRQ just occurred!\n");
  630. }
  631. /*
  632. * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0
  633. * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting
  634. * each byte to a logic 1. This has no effect on any extended
  635. * NV-SRAM that might be present, nor on the time/calendar/alarm
  636. * registers. After a ram-clear is completed, there is a minimum
  637. * recovery time of ~150ms in which all reads/writes are locked out.
  638. * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot
  639. * catch this scenario.
  640. */
  641. if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
  642. rtc->write(rtc, RTC_EXT_CTRL_4A,
  643. (ctrl4a & ~(RTC_CTRL_4A_RF)));
  644. msleep(150);
  645. /* Call the platform post_ram_clear function if defined. */
  646. if (rtc->post_ram_clear != NULL)
  647. rtc->post_ram_clear();
  648. else
  649. dev_warn(&pdev->dev,
  650. "RAM-Clear IRQ just occurred!\n");
  651. }
  652. ds1685_rtc_switch_to_bank0(rtc);
  653. mutex_unlock(rtc_mutex);
  654. }
  655. /* ----------------------------------------------------------------------- */
  656. /* ----------------------------------------------------------------------- */
  657. /* ProcFS interface */
  658. #ifdef CONFIG_PROC_FS
  659. #define NUM_REGS 6 /* Num of control registers. */
  660. #define NUM_BITS 8 /* Num bits per register. */
  661. #define NUM_SPACES 4 /* Num spaces between each bit. */
  662. /*
  663. * Periodic Interrupt Rates.
  664. */
  665. static const char *ds1685_rtc_pirq_rate[16] = {
  666. "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
  667. "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
  668. "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
  669. };
  670. /*
  671. * Square-Wave Output Frequencies.
  672. */
  673. static const char *ds1685_rtc_sqw_freq[16] = {
  674. "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
  675. "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
  676. };
  677. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  678. /**
  679. * ds1685_rtc_print_regs - helper function to print register values.
  680. * @hex: hex byte to convert into binary bits.
  681. * @dest: destination char array.
  682. *
  683. * This is basically a hex->binary function, just with extra spacing between
  684. * the digits. It only works on 1-byte values (8 bits).
  685. */
  686. static char*
  687. ds1685_rtc_print_regs(u8 hex, char *dest)
  688. {
  689. u32 i, j;
  690. char *tmp = dest;
  691. for (i = 0; i < NUM_BITS; i++) {
  692. *tmp++ = ((hex & 0x80) != 0 ? '1' : '0');
  693. for (j = 0; j < NUM_SPACES; j++)
  694. *tmp++ = ' ';
  695. hex <<= 1;
  696. }
  697. *tmp++ = '\0';
  698. return dest;
  699. }
  700. #endif
  701. /**
  702. * ds1685_rtc_proc - procfs access function.
  703. * @dev: pointer to device structure.
  704. * @seq: pointer to seq_file structure.
  705. */
  706. static int
  707. ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
  708. {
  709. struct platform_device *pdev = to_platform_device(dev);
  710. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  711. u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8];
  712. char *model;
  713. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  714. char bits[NUM_REGS][(NUM_BITS * NUM_SPACES) + NUM_BITS + 1];
  715. #endif
  716. /* Read all the relevant data from the control registers. */
  717. ds1685_rtc_switch_to_bank1(rtc);
  718. ds1685_rtc_get_ssn(rtc, ssn);
  719. ctrla = rtc->read(rtc, RTC_CTRL_A);
  720. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  721. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  722. ctrld = rtc->read(rtc, RTC_CTRL_D);
  723. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  724. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  725. ds1685_rtc_switch_to_bank0(rtc);
  726. /* Determine the RTC model. */
  727. switch (ssn[0]) {
  728. case RTC_MODEL_DS1685:
  729. model = "DS1685/DS1687\0";
  730. break;
  731. case RTC_MODEL_DS1689:
  732. model = "DS1689/DS1693\0";
  733. break;
  734. case RTC_MODEL_DS17285:
  735. model = "DS17285/DS17287\0";
  736. break;
  737. case RTC_MODEL_DS17485:
  738. model = "DS17485/DS17487\0";
  739. break;
  740. case RTC_MODEL_DS17885:
  741. model = "DS17885/DS17887\0";
  742. break;
  743. default:
  744. model = "Unknown\0";
  745. break;
  746. }
  747. /* Print out the information. */
  748. seq_printf(seq,
  749. "Model\t\t: %s\n"
  750. "Oscillator\t: %s\n"
  751. "12/24hr\t\t: %s\n"
  752. "DST\t\t: %s\n"
  753. "Data mode\t: %s\n"
  754. "Battery\t\t: %s\n"
  755. "Aux batt\t: %s\n"
  756. "Update IRQ\t: %s\n"
  757. "Periodic IRQ\t: %s\n"
  758. "Periodic Rate\t: %s\n"
  759. "SQW Freq\t: %s\n"
  760. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  761. "Serial #\t: %8phC\n"
  762. "Register Status\t:\n"
  763. " Ctrl A\t: UIP DV2 DV1 DV0 RS3 RS2 RS1 RS0\n"
  764. "\t\t: %s\n"
  765. " Ctrl B\t: SET PIE AIE UIE SQWE DM 2412 DSE\n"
  766. "\t\t: %s\n"
  767. " Ctrl C\t: IRQF PF AF UF --- --- --- ---\n"
  768. "\t\t: %s\n"
  769. " Ctrl D\t: VRT --- --- --- --- --- --- ---\n"
  770. "\t\t: %s\n"
  771. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  772. " Ctrl 4A\t: VRT2 INCR BME --- PAB RF WF KF\n"
  773. #else
  774. " Ctrl 4A\t: VRT2 INCR --- --- PAB RF WF KF\n"
  775. #endif
  776. "\t\t: %s\n"
  777. " Ctrl 4B\t: ABE E32k CS RCE PRS RIE WIE KSE\n"
  778. "\t\t: %s\n",
  779. #else
  780. "Serial #\t: %8phC\n",
  781. #endif
  782. model,
  783. ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
  784. ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
  785. ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
  786. ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
  787. ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
  788. ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
  789. ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
  790. ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
  791. (!(ctrl4b & RTC_CTRL_4B_E32K) ?
  792. ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
  793. (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
  794. ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
  795. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  796. ssn,
  797. ds1685_rtc_print_regs(ctrla, bits[0]),
  798. ds1685_rtc_print_regs(ctrlb, bits[1]),
  799. ds1685_rtc_print_regs(ctrlc, bits[2]),
  800. ds1685_rtc_print_regs(ctrld, bits[3]),
  801. ds1685_rtc_print_regs(ctrl4a, bits[4]),
  802. ds1685_rtc_print_regs(ctrl4b, bits[5]));
  803. #else
  804. ssn);
  805. #endif
  806. return 0;
  807. }
  808. #else
  809. #define ds1685_rtc_proc NULL
  810. #endif /* CONFIG_PROC_FS */
  811. /* ----------------------------------------------------------------------- */
  812. /* ----------------------------------------------------------------------- */
  813. /* RTC Class operations */
  814. static const struct rtc_class_ops
  815. ds1685_rtc_ops = {
  816. .proc = ds1685_rtc_proc,
  817. .read_time = ds1685_rtc_read_time,
  818. .set_time = ds1685_rtc_set_time,
  819. .read_alarm = ds1685_rtc_read_alarm,
  820. .set_alarm = ds1685_rtc_set_alarm,
  821. .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
  822. };
  823. /* ----------------------------------------------------------------------- */
  824. /* ----------------------------------------------------------------------- */
  825. /* SysFS interface */
  826. #ifdef CONFIG_SYSFS
  827. /**
  828. * ds1685_rtc_sysfs_nvram_read - reads rtc nvram via sysfs.
  829. * @file: pointer to file structure.
  830. * @kobj: pointer to kobject structure.
  831. * @bin_attr: pointer to bin_attribute structure.
  832. * @buf: pointer to char array to hold the output.
  833. * @pos: current file position pointer.
  834. * @size: size of the data to read.
  835. */
  836. static ssize_t
  837. ds1685_rtc_sysfs_nvram_read(struct file *filp, struct kobject *kobj,
  838. struct bin_attribute *bin_attr, char *buf,
  839. loff_t pos, size_t size)
  840. {
  841. struct platform_device *pdev =
  842. to_platform_device(container_of(kobj, struct device, kobj));
  843. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  844. ssize_t count;
  845. unsigned long flags = 0;
  846. spin_lock_irqsave(&rtc->lock, flags);
  847. ds1685_rtc_switch_to_bank0(rtc);
  848. /* Read NVRAM in time and bank0 registers. */
  849. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  850. count++, size--) {
  851. if (count < NVRAM_SZ_TIME)
  852. *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
  853. else
  854. *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
  855. }
  856. #ifndef CONFIG_RTC_DRV_DS1689
  857. if (size > 0) {
  858. ds1685_rtc_switch_to_bank1(rtc);
  859. #ifndef CONFIG_RTC_DRV_DS1685
  860. /* Enable burst-mode on DS17x85/DS17x87 */
  861. rtc->write(rtc, RTC_EXT_CTRL_4A,
  862. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  863. RTC_CTRL_4A_BME));
  864. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  865. * reading with burst-mode */
  866. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  867. (pos - NVRAM_TOTAL_SZ_BANK0));
  868. #endif
  869. /* Read NVRAM in bank1 registers. */
  870. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  871. count++, size--) {
  872. #ifdef CONFIG_RTC_DRV_DS1685
  873. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  874. * before each read. */
  875. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  876. (pos - NVRAM_TOTAL_SZ_BANK0));
  877. #endif
  878. *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
  879. pos++;
  880. }
  881. #ifndef CONFIG_RTC_DRV_DS1685
  882. /* Disable burst-mode on DS17x85/DS17x87 */
  883. rtc->write(rtc, RTC_EXT_CTRL_4A,
  884. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  885. ~(RTC_CTRL_4A_BME)));
  886. #endif
  887. ds1685_rtc_switch_to_bank0(rtc);
  888. }
  889. #endif /* !CONFIG_RTC_DRV_DS1689 */
  890. spin_unlock_irqrestore(&rtc->lock, flags);
  891. /*
  892. * XXX: Bug? this appears to cause the function to get executed
  893. * several times in succession. But it's the only way to actually get
  894. * data written out to a file.
  895. */
  896. return count;
  897. }
  898. /**
  899. * ds1685_rtc_sysfs_nvram_write - writes rtc nvram via sysfs.
  900. * @file: pointer to file structure.
  901. * @kobj: pointer to kobject structure.
  902. * @bin_attr: pointer to bin_attribute structure.
  903. * @buf: pointer to char array to hold the input.
  904. * @pos: current file position pointer.
  905. * @size: size of the data to write.
  906. */
  907. static ssize_t
  908. ds1685_rtc_sysfs_nvram_write(struct file *filp, struct kobject *kobj,
  909. struct bin_attribute *bin_attr, char *buf,
  910. loff_t pos, size_t size)
  911. {
  912. struct platform_device *pdev =
  913. to_platform_device(container_of(kobj, struct device, kobj));
  914. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  915. ssize_t count;
  916. unsigned long flags = 0;
  917. spin_lock_irqsave(&rtc->lock, flags);
  918. ds1685_rtc_switch_to_bank0(rtc);
  919. /* Write NVRAM in time and bank0 registers. */
  920. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  921. count++, size--)
  922. if (count < NVRAM_SZ_TIME)
  923. rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
  924. *buf++);
  925. else
  926. rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
  927. #ifndef CONFIG_RTC_DRV_DS1689
  928. if (size > 0) {
  929. ds1685_rtc_switch_to_bank1(rtc);
  930. #ifndef CONFIG_RTC_DRV_DS1685
  931. /* Enable burst-mode on DS17x85/DS17x87 */
  932. rtc->write(rtc, RTC_EXT_CTRL_4A,
  933. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  934. RTC_CTRL_4A_BME));
  935. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  936. * writing with burst-mode */
  937. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  938. (pos - NVRAM_TOTAL_SZ_BANK0));
  939. #endif
  940. /* Write NVRAM in bank1 registers. */
  941. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  942. count++, size--) {
  943. #ifdef CONFIG_RTC_DRV_DS1685
  944. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  945. * before each read. */
  946. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  947. (pos - NVRAM_TOTAL_SZ_BANK0));
  948. #endif
  949. rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
  950. pos++;
  951. }
  952. #ifndef CONFIG_RTC_DRV_DS1685
  953. /* Disable burst-mode on DS17x85/DS17x87 */
  954. rtc->write(rtc, RTC_EXT_CTRL_4A,
  955. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  956. ~(RTC_CTRL_4A_BME)));
  957. #endif
  958. ds1685_rtc_switch_to_bank0(rtc);
  959. }
  960. #endif /* !CONFIG_RTC_DRV_DS1689 */
  961. spin_unlock_irqrestore(&rtc->lock, flags);
  962. return count;
  963. }
  964. /**
  965. * struct ds1685_rtc_sysfs_nvram_attr - sysfs attributes for rtc nvram.
  966. * @attr: nvram attributes.
  967. * @read: nvram read function.
  968. * @write: nvram write function.
  969. * @size: nvram total size (bank0 + extended).
  970. */
  971. static struct bin_attribute
  972. ds1685_rtc_sysfs_nvram_attr = {
  973. .attr = {
  974. .name = "nvram",
  975. .mode = S_IRUGO | S_IWUSR,
  976. },
  977. .read = ds1685_rtc_sysfs_nvram_read,
  978. .write = ds1685_rtc_sysfs_nvram_write,
  979. .size = NVRAM_TOTAL_SZ
  980. };
  981. /**
  982. * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
  983. * @dev: pointer to device structure.
  984. * @attr: pointer to device_attribute structure.
  985. * @buf: pointer to char array to hold the output.
  986. */
  987. static ssize_t
  988. ds1685_rtc_sysfs_battery_show(struct device *dev,
  989. struct device_attribute *attr, char *buf)
  990. {
  991. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  992. u8 ctrld;
  993. ctrld = rtc->read(rtc, RTC_CTRL_D);
  994. return sprintf(buf, "%s\n",
  995. (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
  996. }
  997. static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
  998. /**
  999. * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
  1000. * @dev: pointer to device structure.
  1001. * @attr: pointer to device_attribute structure.
  1002. * @buf: pointer to char array to hold the output.
  1003. */
  1004. static ssize_t
  1005. ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
  1006. struct device_attribute *attr, char *buf)
  1007. {
  1008. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1009. u8 ctrl4a;
  1010. ds1685_rtc_switch_to_bank1(rtc);
  1011. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1012. ds1685_rtc_switch_to_bank0(rtc);
  1013. return sprintf(buf, "%s\n",
  1014. (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
  1015. }
  1016. static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
  1017. /**
  1018. * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
  1019. * @dev: pointer to device structure.
  1020. * @attr: pointer to device_attribute structure.
  1021. * @buf: pointer to char array to hold the output.
  1022. */
  1023. static ssize_t
  1024. ds1685_rtc_sysfs_serial_show(struct device *dev,
  1025. struct device_attribute *attr, char *buf)
  1026. {
  1027. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1028. u8 ssn[8];
  1029. ds1685_rtc_switch_to_bank1(rtc);
  1030. ds1685_rtc_get_ssn(rtc, ssn);
  1031. ds1685_rtc_switch_to_bank0(rtc);
  1032. return sprintf(buf, "%8phC\n", ssn);
  1033. }
  1034. static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
  1035. /**
  1036. * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
  1037. */
  1038. static struct attribute*
  1039. ds1685_rtc_sysfs_misc_attrs[] = {
  1040. &dev_attr_battery.attr,
  1041. &dev_attr_auxbatt.attr,
  1042. &dev_attr_serial.attr,
  1043. NULL,
  1044. };
  1045. /**
  1046. * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
  1047. */
  1048. static const struct attribute_group
  1049. ds1685_rtc_sysfs_misc_grp = {
  1050. .name = "misc",
  1051. .attrs = ds1685_rtc_sysfs_misc_attrs,
  1052. };
  1053. /**
  1054. * ds1685_rtc_sysfs_register - register sysfs files.
  1055. * @dev: pointer to device structure.
  1056. */
  1057. static int
  1058. ds1685_rtc_sysfs_register(struct device *dev)
  1059. {
  1060. int ret = 0;
  1061. sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr);
  1062. ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1063. if (ret)
  1064. return ret;
  1065. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1066. if (ret)
  1067. return ret;
  1068. return 0;
  1069. }
  1070. /**
  1071. * ds1685_rtc_sysfs_unregister - unregister sysfs files.
  1072. * @dev: pointer to device structure.
  1073. */
  1074. static int
  1075. ds1685_rtc_sysfs_unregister(struct device *dev)
  1076. {
  1077. sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1078. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1079. return 0;
  1080. }
  1081. #endif /* CONFIG_SYSFS */
  1082. /* ----------------------------------------------------------------------- */
  1083. /* Driver Probe/Removal */
  1084. /**
  1085. * ds1685_rtc_probe - initializes rtc driver.
  1086. * @pdev: pointer to platform_device structure.
  1087. */
  1088. static int
  1089. ds1685_rtc_probe(struct platform_device *pdev)
  1090. {
  1091. struct rtc_device *rtc_dev;
  1092. struct resource *res;
  1093. struct ds1685_priv *rtc;
  1094. struct ds1685_rtc_platform_data *pdata;
  1095. u8 ctrla, ctrlb, hours;
  1096. unsigned char am_pm;
  1097. int ret = 0;
  1098. /* Get the platform data. */
  1099. pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
  1100. if (!pdata)
  1101. return -ENODEV;
  1102. /* Allocate memory for the rtc device. */
  1103. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  1104. if (!rtc)
  1105. return -ENOMEM;
  1106. /*
  1107. * Allocate/setup any IORESOURCE_MEM resources, if required. Not all
  1108. * platforms put the RTC in an easy-access place. Like the SGI Octane,
  1109. * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip
  1110. * that sits behind the IOC3 PCI metadevice.
  1111. */
  1112. if (pdata->alloc_io_resources) {
  1113. /* Get the platform resources. */
  1114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1115. if (!res)
  1116. return -ENXIO;
  1117. rtc->size = resource_size(res);
  1118. /* Request a memory region. */
  1119. /* XXX: mmio-only for now. */
  1120. if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
  1121. pdev->name))
  1122. return -EBUSY;
  1123. /*
  1124. * Set the base address for the rtc, and ioremap its
  1125. * registers.
  1126. */
  1127. rtc->baseaddr = res->start;
  1128. rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
  1129. if (!rtc->regs)
  1130. return -ENOMEM;
  1131. }
  1132. rtc->alloc_io_resources = pdata->alloc_io_resources;
  1133. /* Get the register step size. */
  1134. if (pdata->regstep > 0)
  1135. rtc->regstep = pdata->regstep;
  1136. else
  1137. rtc->regstep = 1;
  1138. /* Platform read function, else default if mmio setup */
  1139. if (pdata->plat_read)
  1140. rtc->read = pdata->plat_read;
  1141. else
  1142. if (pdata->alloc_io_resources)
  1143. rtc->read = ds1685_read;
  1144. else
  1145. return -ENXIO;
  1146. /* Platform write function, else default if mmio setup */
  1147. if (pdata->plat_write)
  1148. rtc->write = pdata->plat_write;
  1149. else
  1150. if (pdata->alloc_io_resources)
  1151. rtc->write = ds1685_write;
  1152. else
  1153. return -ENXIO;
  1154. /* Platform pre-shutdown function, if defined. */
  1155. if (pdata->plat_prepare_poweroff)
  1156. rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
  1157. /* Platform wake_alarm function, if defined. */
  1158. if (pdata->plat_wake_alarm)
  1159. rtc->wake_alarm = pdata->plat_wake_alarm;
  1160. /* Platform post_ram_clear function, if defined. */
  1161. if (pdata->plat_post_ram_clear)
  1162. rtc->post_ram_clear = pdata->plat_post_ram_clear;
  1163. /* Init the spinlock, workqueue, & set the driver data. */
  1164. spin_lock_init(&rtc->lock);
  1165. INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
  1166. platform_set_drvdata(pdev, rtc);
  1167. /* Turn the oscillator on if is not already on (DV1 = 1). */
  1168. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1169. if (!(ctrla & RTC_CTRL_A_DV1))
  1170. ctrla |= RTC_CTRL_A_DV1;
  1171. /* Enable the countdown chain (DV2 = 0) */
  1172. ctrla &= ~(RTC_CTRL_A_DV2);
  1173. /* Clear RS3-RS0 in Control A. */
  1174. ctrla &= ~(RTC_CTRL_A_RS_MASK);
  1175. /*
  1176. * All done with Control A. Switch to Bank 1 for the remainder of
  1177. * the RTC setup so we have access to the extended functions.
  1178. */
  1179. ctrla |= RTC_CTRL_A_DV0;
  1180. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1181. /* Default to 32768kHz output. */
  1182. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1183. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
  1184. /* Set the SET bit in Control B so we can do some housekeeping. */
  1185. rtc->write(rtc, RTC_CTRL_B,
  1186. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  1187. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  1188. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  1189. cpu_relax();
  1190. /*
  1191. * If the platform supports BCD mode, then set DM=0 in Control B.
  1192. * Otherwise, set DM=1 for BIN mode.
  1193. */
  1194. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  1195. if (pdata->bcd_mode)
  1196. ctrlb &= ~(RTC_CTRL_B_DM);
  1197. else
  1198. ctrlb |= RTC_CTRL_B_DM;
  1199. rtc->bcd_mode = pdata->bcd_mode;
  1200. /*
  1201. * Disable Daylight Savings Time (DSE = 0).
  1202. * The RTC has hardcoded timezone information that is rendered
  1203. * obselete. We'll let the OS deal with DST settings instead.
  1204. */
  1205. if (ctrlb & RTC_CTRL_B_DSE)
  1206. ctrlb &= ~(RTC_CTRL_B_DSE);
  1207. /* Force 24-hour mode (2412 = 1). */
  1208. if (!(ctrlb & RTC_CTRL_B_2412)) {
  1209. /* Reinitialize the time hours. */
  1210. hours = rtc->read(rtc, RTC_HRS);
  1211. am_pm = hours & RTC_HRS_AMPM_MASK;
  1212. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1213. RTC_HRS_12_BIN_MASK);
  1214. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1215. /* Enable 24-hour mode. */
  1216. ctrlb |= RTC_CTRL_B_2412;
  1217. /* Write back to Control B, including DM & DSE bits. */
  1218. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1219. /* Write the time hours back. */
  1220. rtc->write(rtc, RTC_HRS,
  1221. ds1685_rtc_bin2bcd(rtc, hours,
  1222. RTC_HRS_24_BIN_MASK,
  1223. RTC_HRS_24_BCD_MASK));
  1224. /* Reinitialize the alarm hours. */
  1225. hours = rtc->read(rtc, RTC_HRS_ALARM);
  1226. am_pm = hours & RTC_HRS_AMPM_MASK;
  1227. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1228. RTC_HRS_12_BIN_MASK);
  1229. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1230. /* Write the alarm hours back. */
  1231. rtc->write(rtc, RTC_HRS_ALARM,
  1232. ds1685_rtc_bin2bcd(rtc, hours,
  1233. RTC_HRS_24_BIN_MASK,
  1234. RTC_HRS_24_BCD_MASK));
  1235. } else {
  1236. /* 24-hour mode is already set, so write Control B back. */
  1237. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1238. }
  1239. /* Unset the SET bit in Control B so the RTC can update. */
  1240. rtc->write(rtc, RTC_CTRL_B,
  1241. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  1242. /* Check the main battery. */
  1243. if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
  1244. dev_warn(&pdev->dev,
  1245. "Main battery is exhausted! RTC may be invalid!\n");
  1246. /* Check the auxillary battery. It is optional. */
  1247. if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
  1248. dev_warn(&pdev->dev,
  1249. "Aux battery is exhausted or not available.\n");
  1250. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1251. rtc->write(rtc, RTC_CTRL_B,
  1252. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
  1253. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1254. rtc->read(rtc, RTC_CTRL_C);
  1255. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1256. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1257. (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
  1258. /* Clear RF/WF/KF in Ctrl 4A. */
  1259. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1260. (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
  1261. /*
  1262. * Re-enable KSE to handle power button events. We do not enable
  1263. * WIE or RIE by default.
  1264. */
  1265. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1266. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
  1267. rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  1268. if (IS_ERR(rtc_dev))
  1269. return PTR_ERR(rtc_dev);
  1270. rtc_dev->ops = &ds1685_rtc_ops;
  1271. /* Century bit is useless because leap year fails in 1900 and 2100 */
  1272. rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
  1273. rtc_dev->range_max = RTC_TIMESTAMP_END_2099;
  1274. /* Maximum periodic rate is 8192Hz (0.122070ms). */
  1275. rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
  1276. /* See if the platform doesn't support UIE. */
  1277. if (pdata->uie_unsupported)
  1278. rtc_dev->uie_unsupported = 1;
  1279. rtc->uie_unsupported = pdata->uie_unsupported;
  1280. rtc->dev = rtc_dev;
  1281. /*
  1282. * Fetch the IRQ and setup the interrupt handler.
  1283. *
  1284. * Not all platforms have the IRQF pin tied to something. If not, the
  1285. * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
  1286. * there won't be an automatic way of notifying the kernel about it,
  1287. * unless ctrlc is explicitly polled.
  1288. */
  1289. if (!pdata->no_irq) {
  1290. ret = platform_get_irq(pdev, 0);
  1291. if (ret > 0) {
  1292. rtc->irq_num = ret;
  1293. /* Request an IRQ. */
  1294. ret = devm_request_irq(&pdev->dev, rtc->irq_num,
  1295. ds1685_rtc_irq_handler,
  1296. IRQF_SHARED, pdev->name, pdev);
  1297. /* Check to see if something came back. */
  1298. if (unlikely(ret)) {
  1299. dev_warn(&pdev->dev,
  1300. "RTC interrupt not available\n");
  1301. rtc->irq_num = 0;
  1302. }
  1303. } else
  1304. return ret;
  1305. }
  1306. rtc->no_irq = pdata->no_irq;
  1307. /* Setup complete. */
  1308. ds1685_rtc_switch_to_bank0(rtc);
  1309. #ifdef CONFIG_SYSFS
  1310. ret = ds1685_rtc_sysfs_register(&pdev->dev);
  1311. if (ret)
  1312. return ret;
  1313. #endif
  1314. return rtc_register_device(rtc_dev);
  1315. }
  1316. /**
  1317. * ds1685_rtc_remove - removes rtc driver.
  1318. * @pdev: pointer to platform_device structure.
  1319. */
  1320. static int
  1321. ds1685_rtc_remove(struct platform_device *pdev)
  1322. {
  1323. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1324. #ifdef CONFIG_SYSFS
  1325. ds1685_rtc_sysfs_unregister(&pdev->dev);
  1326. #endif
  1327. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1328. rtc->write(rtc, RTC_CTRL_B,
  1329. (rtc->read(rtc, RTC_CTRL_B) &
  1330. ~(RTC_CTRL_B_PAU_MASK)));
  1331. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1332. rtc->read(rtc, RTC_CTRL_C);
  1333. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1334. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1335. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  1336. ~(RTC_CTRL_4B_RWK_MASK)));
  1337. /* Manually clear RF/WF/KF in Ctrl 4A. */
  1338. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1339. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  1340. ~(RTC_CTRL_4A_RWK_MASK)));
  1341. cancel_work_sync(&rtc->work);
  1342. return 0;
  1343. }
  1344. /**
  1345. * ds1685_rtc_driver - rtc driver properties.
  1346. */
  1347. static struct platform_driver ds1685_rtc_driver = {
  1348. .driver = {
  1349. .name = "rtc-ds1685",
  1350. },
  1351. .probe = ds1685_rtc_probe,
  1352. .remove = ds1685_rtc_remove,
  1353. };
  1354. module_platform_driver(ds1685_rtc_driver);
  1355. /* ----------------------------------------------------------------------- */
  1356. /* ----------------------------------------------------------------------- */
  1357. /* Poweroff function */
  1358. /**
  1359. * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
  1360. * @pdev: pointer to platform_device structure.
  1361. */
  1362. void __noreturn
  1363. ds1685_rtc_poweroff(struct platform_device *pdev)
  1364. {
  1365. u8 ctrla, ctrl4a, ctrl4b;
  1366. struct ds1685_priv *rtc;
  1367. /* Check for valid RTC data, else, spin forever. */
  1368. if (unlikely(!pdev)) {
  1369. pr_emerg("platform device data not available, spinning forever ...\n");
  1370. while(1);
  1371. unreachable();
  1372. } else {
  1373. /* Get the rtc data. */
  1374. rtc = platform_get_drvdata(pdev);
  1375. /*
  1376. * Disable our IRQ. We're powering down, so we're not
  1377. * going to worry about cleaning up. Most of that should
  1378. * have been taken care of by the shutdown scripts and this
  1379. * is the final function call.
  1380. */
  1381. if (!rtc->no_irq)
  1382. disable_irq_nosync(rtc->irq_num);
  1383. /* Oscillator must be on and the countdown chain enabled. */
  1384. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1385. ctrla |= RTC_CTRL_A_DV1;
  1386. ctrla &= ~(RTC_CTRL_A_DV2);
  1387. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1388. /*
  1389. * Read Control 4A and check the status of the auxillary
  1390. * battery. This must be present and working (VRT2 = 1)
  1391. * for wakeup and kickstart functionality to be useful.
  1392. */
  1393. ds1685_rtc_switch_to_bank1(rtc);
  1394. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1395. if (ctrl4a & RTC_CTRL_4A_VRT2) {
  1396. /* Clear all of the interrupt flags on Control 4A. */
  1397. ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
  1398. rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
  1399. /*
  1400. * The auxillary battery is present and working.
  1401. * Enable extended functions (ABE=1), enable
  1402. * wake-up (WIE=1), and enable kickstart (KSE=1)
  1403. * in Control 4B.
  1404. */
  1405. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  1406. ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
  1407. RTC_CTRL_4B_KSE);
  1408. rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
  1409. }
  1410. /* Set PAB to 1 in Control 4A to power the system down. */
  1411. dev_warn(&pdev->dev, "Powerdown.\n");
  1412. msleep(20);
  1413. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1414. (ctrl4a | RTC_CTRL_4A_PAB));
  1415. /* Spin ... we do not switch back to bank0. */
  1416. while(1);
  1417. unreachable();
  1418. }
  1419. }
  1420. EXPORT_SYMBOL(ds1685_rtc_poweroff);
  1421. /* ----------------------------------------------------------------------- */
  1422. MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
  1423. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
  1424. MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
  1425. MODULE_LICENSE("GPL");
  1426. MODULE_ALIAS("platform:rtc-ds1685");