rtc-ds1685.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  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 platform_device *pdev = to_platform_device(dev);
  243. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  244. u8 ctrlb, century;
  245. u8 seconds, minutes, hours, wday, mday, month, years;
  246. /* Fetch the time info from the RTC registers. */
  247. ds1685_rtc_begin_data_access(rtc);
  248. seconds = rtc->read(rtc, RTC_SECS);
  249. minutes = rtc->read(rtc, RTC_MINS);
  250. hours = rtc->read(rtc, RTC_HRS);
  251. wday = rtc->read(rtc, RTC_WDAY);
  252. mday = rtc->read(rtc, RTC_MDAY);
  253. month = rtc->read(rtc, RTC_MONTH);
  254. years = rtc->read(rtc, RTC_YEAR);
  255. century = rtc->read(rtc, RTC_CENTURY);
  256. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  257. ds1685_rtc_end_data_access(rtc);
  258. /* bcd2bin if needed, perform fixups, and store to rtc_time. */
  259. years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
  260. RTC_YEAR_BIN_MASK);
  261. century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
  262. RTC_CENTURY_MASK);
  263. tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
  264. RTC_SECS_BIN_MASK);
  265. tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
  266. RTC_MINS_BIN_MASK);
  267. tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
  268. RTC_HRS_24_BIN_MASK);
  269. tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
  270. RTC_WDAY_MASK) - 1);
  271. tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  272. RTC_MDAY_BIN_MASK);
  273. tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
  274. RTC_MONTH_BIN_MASK) - 1);
  275. tm->tm_year = ((years + (century * 100)) - 1900);
  276. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  277. tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
  278. return rtc_valid_tm(tm);
  279. }
  280. /**
  281. * ds1685_rtc_set_time - sets the time registers.
  282. * @dev: pointer to device structure.
  283. * @tm: pointer to rtc_time structure.
  284. */
  285. static int
  286. ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
  287. {
  288. struct platform_device *pdev = to_platform_device(dev);
  289. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  290. u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
  291. /* Fetch the time info from rtc_time. */
  292. seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
  293. RTC_SECS_BCD_MASK);
  294. minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
  295. RTC_MINS_BCD_MASK);
  296. hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
  297. RTC_HRS_24_BCD_MASK);
  298. wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
  299. RTC_WDAY_MASK);
  300. mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
  301. RTC_MDAY_BCD_MASK);
  302. month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
  303. RTC_MONTH_BCD_MASK);
  304. years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
  305. RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
  306. century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
  307. RTC_CENTURY_MASK, RTC_CENTURY_MASK);
  308. /*
  309. * Perform Sanity Checks:
  310. * - Months: !> 12, Month Day != 0.
  311. * - Month Day !> Max days in current month.
  312. * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
  313. */
  314. if ((tm->tm_mon > 11) || (mday == 0))
  315. return -EDOM;
  316. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
  317. return -EDOM;
  318. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
  319. (tm->tm_sec >= 60) || (wday > 7))
  320. return -EDOM;
  321. /*
  322. * Set the data mode to use and store the time values in the
  323. * RTC registers.
  324. */
  325. ds1685_rtc_begin_data_access(rtc);
  326. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  327. if (rtc->bcd_mode)
  328. ctrlb &= ~(RTC_CTRL_B_DM);
  329. else
  330. ctrlb |= RTC_CTRL_B_DM;
  331. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  332. rtc->write(rtc, RTC_SECS, seconds);
  333. rtc->write(rtc, RTC_MINS, minutes);
  334. rtc->write(rtc, RTC_HRS, hours);
  335. rtc->write(rtc, RTC_WDAY, wday);
  336. rtc->write(rtc, RTC_MDAY, mday);
  337. rtc->write(rtc, RTC_MONTH, month);
  338. rtc->write(rtc, RTC_YEAR, years);
  339. rtc->write(rtc, RTC_CENTURY, century);
  340. ds1685_rtc_end_data_access(rtc);
  341. return 0;
  342. }
  343. /**
  344. * ds1685_rtc_read_alarm - reads the alarm registers.
  345. * @dev: pointer to device structure.
  346. * @alrm: pointer to rtc_wkalrm structure.
  347. *
  348. * There are three primary alarm registers: seconds, minutes, and hours.
  349. * A fourth alarm register for the month date is also available in bank1 for
  350. * kickstart/wakeup features. The DS1685/DS1687 manual states that a
  351. * "don't care" value ranging from 0xc0 to 0xff may be written into one or
  352. * more of the three alarm bytes to act as a wildcard value. The fourth
  353. * byte doesn't support a "don't care" value.
  354. */
  355. static int
  356. ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  357. {
  358. struct platform_device *pdev = to_platform_device(dev);
  359. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  360. u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
  361. int ret;
  362. /* Fetch the alarm info from the RTC alarm registers. */
  363. ds1685_rtc_begin_data_access(rtc);
  364. seconds = rtc->read(rtc, RTC_SECS_ALARM);
  365. minutes = rtc->read(rtc, RTC_MINS_ALARM);
  366. hours = rtc->read(rtc, RTC_HRS_ALARM);
  367. mday = rtc->read(rtc, RTC_MDAY_ALARM);
  368. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  369. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  370. ds1685_rtc_end_data_access(rtc);
  371. /* Check the month date for validity. */
  372. ret = ds1685_rtc_check_mday(rtc, mday);
  373. if (ret)
  374. return ret;
  375. /*
  376. * Check the three alarm bytes.
  377. *
  378. * The Linux RTC system doesn't support the "don't care" capability
  379. * of this RTC chip. We check for it anyways in case support is
  380. * added in the future and only assign when we care.
  381. */
  382. if (likely(seconds < 0xc0))
  383. alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
  384. RTC_SECS_BCD_MASK,
  385. RTC_SECS_BIN_MASK);
  386. if (likely(minutes < 0xc0))
  387. alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
  388. RTC_MINS_BCD_MASK,
  389. RTC_MINS_BIN_MASK);
  390. if (likely(hours < 0xc0))
  391. alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
  392. RTC_HRS_24_BCD_MASK,
  393. RTC_HRS_24_BIN_MASK);
  394. /* Write the data to rtc_wkalrm. */
  395. alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  396. RTC_MDAY_BIN_MASK);
  397. alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
  398. alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
  399. return 0;
  400. }
  401. /**
  402. * ds1685_rtc_set_alarm - sets the alarm in registers.
  403. * @dev: pointer to device structure.
  404. * @alrm: pointer to rtc_wkalrm structure.
  405. */
  406. static int
  407. ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  408. {
  409. struct platform_device *pdev = to_platform_device(dev);
  410. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  411. u8 ctrlb, seconds, minutes, hours, mday;
  412. int ret;
  413. /* Fetch the alarm info and convert to BCD. */
  414. seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
  415. RTC_SECS_BIN_MASK,
  416. RTC_SECS_BCD_MASK);
  417. minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
  418. RTC_MINS_BIN_MASK,
  419. RTC_MINS_BCD_MASK);
  420. hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
  421. RTC_HRS_24_BIN_MASK,
  422. RTC_HRS_24_BCD_MASK);
  423. mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
  424. RTC_MDAY_BIN_MASK,
  425. RTC_MDAY_BCD_MASK);
  426. /* Check the month date for validity. */
  427. ret = ds1685_rtc_check_mday(rtc, mday);
  428. if (ret)
  429. return ret;
  430. /*
  431. * Check the three alarm bytes.
  432. *
  433. * The Linux RTC system doesn't support the "don't care" capability
  434. * of this RTC chip because rtc_valid_tm tries to validate every
  435. * field, and we only support four fields. We put the support
  436. * here anyways for the future.
  437. */
  438. if (unlikely(seconds >= 0xc0))
  439. seconds = 0xff;
  440. if (unlikely(minutes >= 0xc0))
  441. minutes = 0xff;
  442. if (unlikely(hours >= 0xc0))
  443. hours = 0xff;
  444. alrm->time.tm_mon = -1;
  445. alrm->time.tm_year = -1;
  446. alrm->time.tm_wday = -1;
  447. alrm->time.tm_yday = -1;
  448. alrm->time.tm_isdst = -1;
  449. /* Disable the alarm interrupt first. */
  450. ds1685_rtc_begin_data_access(rtc);
  451. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  452. rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE)));
  453. /* Read ctrlc to clear RTC_CTRL_C_AF. */
  454. rtc->read(rtc, RTC_CTRL_C);
  455. /*
  456. * Set the data mode to use and store the time values in the
  457. * RTC registers.
  458. */
  459. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  460. if (rtc->bcd_mode)
  461. ctrlb &= ~(RTC_CTRL_B_DM);
  462. else
  463. ctrlb |= RTC_CTRL_B_DM;
  464. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  465. rtc->write(rtc, RTC_SECS_ALARM, seconds);
  466. rtc->write(rtc, RTC_MINS_ALARM, minutes);
  467. rtc->write(rtc, RTC_HRS_ALARM, hours);
  468. rtc->write(rtc, RTC_MDAY_ALARM, mday);
  469. /* Re-enable the alarm if needed. */
  470. if (alrm->enabled) {
  471. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  472. ctrlb |= RTC_CTRL_B_AIE;
  473. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  474. }
  475. /* Done! */
  476. ds1685_rtc_end_data_access(rtc);
  477. return 0;
  478. }
  479. /* ----------------------------------------------------------------------- */
  480. /* ----------------------------------------------------------------------- */
  481. /* /dev/rtcX Interface functions */
  482. /**
  483. * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off.
  484. * @dev: pointer to device structure.
  485. * @enabled: flag indicating whether to enable or disable.
  486. */
  487. static int
  488. ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  489. {
  490. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  491. unsigned long flags = 0;
  492. /* Enable/disable the Alarm IRQ-Enable flag. */
  493. spin_lock_irqsave(&rtc->lock, flags);
  494. /* Flip the requisite interrupt-enable bit. */
  495. if (enabled)
  496. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
  497. RTC_CTRL_B_AIE));
  498. else
  499. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
  500. ~(RTC_CTRL_B_AIE)));
  501. /* Read Control C to clear all the flag bits. */
  502. rtc->read(rtc, RTC_CTRL_C);
  503. spin_unlock_irqrestore(&rtc->lock, flags);
  504. return 0;
  505. }
  506. /* ----------------------------------------------------------------------- */
  507. /* ----------------------------------------------------------------------- */
  508. /* IRQ handler & workqueue. */
  509. /**
  510. * ds1685_rtc_irq_handler - IRQ handler.
  511. * @irq: IRQ number.
  512. * @dev_id: platform device pointer.
  513. */
  514. static irqreturn_t
  515. ds1685_rtc_irq_handler(int irq, void *dev_id)
  516. {
  517. struct platform_device *pdev = dev_id;
  518. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  519. u8 ctrlb, ctrlc;
  520. unsigned long events = 0;
  521. u8 num_irqs = 0;
  522. /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
  523. if (unlikely(!rtc))
  524. return IRQ_HANDLED;
  525. /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
  526. spin_lock(&rtc->lock);
  527. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  528. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  529. /* Is the IRQF bit set? */
  530. if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
  531. /*
  532. * We need to determine if it was one of the standard
  533. * events: PF, AF, or UF. If so, we handle them and
  534. * update the RTC core.
  535. */
  536. if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
  537. events = RTC_IRQF;
  538. /* Check for a periodic interrupt. */
  539. if ((ctrlb & RTC_CTRL_B_PIE) &&
  540. (ctrlc & RTC_CTRL_C_PF)) {
  541. events |= RTC_PF;
  542. num_irqs++;
  543. }
  544. /* Check for an alarm interrupt. */
  545. if ((ctrlb & RTC_CTRL_B_AIE) &&
  546. (ctrlc & RTC_CTRL_C_AF)) {
  547. events |= RTC_AF;
  548. num_irqs++;
  549. }
  550. /* Check for an update interrupt. */
  551. if ((ctrlb & RTC_CTRL_B_UIE) &&
  552. (ctrlc & RTC_CTRL_C_UF)) {
  553. events |= RTC_UF;
  554. num_irqs++;
  555. }
  556. rtc_update_irq(rtc->dev, num_irqs, events);
  557. } else {
  558. /*
  559. * One of the "extended" interrupts was received that
  560. * is not recognized by the RTC core. These need to
  561. * be handled in task context as they can call other
  562. * functions and the time spent in irq context needs
  563. * to be minimized. Schedule them into a workqueue
  564. * and inform the RTC core that the IRQs were handled.
  565. */
  566. spin_unlock(&rtc->lock);
  567. schedule_work(&rtc->work);
  568. rtc_update_irq(rtc->dev, 0, 0);
  569. return IRQ_HANDLED;
  570. }
  571. }
  572. spin_unlock(&rtc->lock);
  573. return events ? IRQ_HANDLED : IRQ_NONE;
  574. }
  575. /**
  576. * ds1685_rtc_work_queue - work queue handler.
  577. * @work: work_struct containing data to work on in task context.
  578. */
  579. static void
  580. ds1685_rtc_work_queue(struct work_struct *work)
  581. {
  582. struct ds1685_priv *rtc = container_of(work,
  583. struct ds1685_priv, work);
  584. struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
  585. struct mutex *rtc_mutex = &rtc->dev->ops_lock;
  586. u8 ctrl4a, ctrl4b;
  587. mutex_lock(rtc_mutex);
  588. ds1685_rtc_switch_to_bank1(rtc);
  589. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  590. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  591. /*
  592. * Check for a kickstart interrupt. With Vcc applied, this
  593. * typically means that the power button was pressed, so we
  594. * begin the shutdown sequence.
  595. */
  596. if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
  597. /* Briefly disable kickstarts to debounce button presses. */
  598. rtc->write(rtc, RTC_EXT_CTRL_4B,
  599. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  600. ~(RTC_CTRL_4B_KSE)));
  601. /* Clear the kickstart flag. */
  602. rtc->write(rtc, RTC_EXT_CTRL_4A,
  603. (ctrl4a & ~(RTC_CTRL_4A_KF)));
  604. /*
  605. * Sleep 500ms before re-enabling kickstarts. This allows
  606. * adequate time to avoid reading signal jitter as additional
  607. * button presses.
  608. */
  609. msleep(500);
  610. rtc->write(rtc, RTC_EXT_CTRL_4B,
  611. (rtc->read(rtc, RTC_EXT_CTRL_4B) |
  612. RTC_CTRL_4B_KSE));
  613. /* Call the platform pre-poweroff function. Else, shutdown. */
  614. if (rtc->prepare_poweroff != NULL)
  615. rtc->prepare_poweroff();
  616. else
  617. ds1685_rtc_poweroff(pdev);
  618. }
  619. /*
  620. * Check for a wake-up interrupt. With Vcc applied, this is
  621. * essentially a second alarm interrupt, except it takes into
  622. * account the 'date' register in bank1 in addition to the
  623. * standard three alarm registers.
  624. */
  625. if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
  626. rtc->write(rtc, RTC_EXT_CTRL_4A,
  627. (ctrl4a & ~(RTC_CTRL_4A_WF)));
  628. /* Call the platform wake_alarm function if defined. */
  629. if (rtc->wake_alarm != NULL)
  630. rtc->wake_alarm();
  631. else
  632. dev_warn(&pdev->dev,
  633. "Wake Alarm IRQ just occurred!\n");
  634. }
  635. /*
  636. * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0
  637. * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting
  638. * each byte to a logic 1. This has no effect on any extended
  639. * NV-SRAM that might be present, nor on the time/calendar/alarm
  640. * registers. After a ram-clear is completed, there is a minimum
  641. * recovery time of ~150ms in which all reads/writes are locked out.
  642. * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot
  643. * catch this scenario.
  644. */
  645. if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
  646. rtc->write(rtc, RTC_EXT_CTRL_4A,
  647. (ctrl4a & ~(RTC_CTRL_4A_RF)));
  648. msleep(150);
  649. /* Call the platform post_ram_clear function if defined. */
  650. if (rtc->post_ram_clear != NULL)
  651. rtc->post_ram_clear();
  652. else
  653. dev_warn(&pdev->dev,
  654. "RAM-Clear IRQ just occurred!\n");
  655. }
  656. ds1685_rtc_switch_to_bank0(rtc);
  657. mutex_unlock(rtc_mutex);
  658. }
  659. /* ----------------------------------------------------------------------- */
  660. /* ----------------------------------------------------------------------- */
  661. /* ProcFS interface */
  662. #ifdef CONFIG_PROC_FS
  663. #define NUM_REGS 6 /* Num of control registers. */
  664. #define NUM_BITS 8 /* Num bits per register. */
  665. #define NUM_SPACES 4 /* Num spaces between each bit. */
  666. /*
  667. * Periodic Interrupt Rates.
  668. */
  669. static const char *ds1685_rtc_pirq_rate[16] = {
  670. "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
  671. "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
  672. "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
  673. };
  674. /*
  675. * Square-Wave Output Frequencies.
  676. */
  677. static const char *ds1685_rtc_sqw_freq[16] = {
  678. "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
  679. "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
  680. };
  681. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  682. /**
  683. * ds1685_rtc_print_regs - helper function to print register values.
  684. * @hex: hex byte to convert into binary bits.
  685. * @dest: destination char array.
  686. *
  687. * This is basically a hex->binary function, just with extra spacing between
  688. * the digits. It only works on 1-byte values (8 bits).
  689. */
  690. static char*
  691. ds1685_rtc_print_regs(u8 hex, char *dest)
  692. {
  693. u32 i, j;
  694. char *tmp = dest;
  695. for (i = 0; i < NUM_BITS; i++) {
  696. *tmp++ = ((hex & 0x80) != 0 ? '1' : '0');
  697. for (j = 0; j < NUM_SPACES; j++)
  698. *tmp++ = ' ';
  699. hex <<= 1;
  700. }
  701. *tmp++ = '\0';
  702. return dest;
  703. }
  704. #endif
  705. /**
  706. * ds1685_rtc_proc - procfs access function.
  707. * @dev: pointer to device structure.
  708. * @seq: pointer to seq_file structure.
  709. */
  710. static int
  711. ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
  712. {
  713. struct platform_device *pdev = to_platform_device(dev);
  714. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  715. u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8];
  716. char *model;
  717. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  718. char bits[NUM_REGS][(NUM_BITS * NUM_SPACES) + NUM_BITS + 1];
  719. #endif
  720. /* Read all the relevant data from the control registers. */
  721. ds1685_rtc_switch_to_bank1(rtc);
  722. ds1685_rtc_get_ssn(rtc, ssn);
  723. ctrla = rtc->read(rtc, RTC_CTRL_A);
  724. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  725. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  726. ctrld = rtc->read(rtc, RTC_CTRL_D);
  727. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  728. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  729. ds1685_rtc_switch_to_bank0(rtc);
  730. /* Determine the RTC model. */
  731. switch (ssn[0]) {
  732. case RTC_MODEL_DS1685:
  733. model = "DS1685/DS1687\0";
  734. break;
  735. case RTC_MODEL_DS1689:
  736. model = "DS1689/DS1693\0";
  737. break;
  738. case RTC_MODEL_DS17285:
  739. model = "DS17285/DS17287\0";
  740. break;
  741. case RTC_MODEL_DS17485:
  742. model = "DS17485/DS17487\0";
  743. break;
  744. case RTC_MODEL_DS17885:
  745. model = "DS17885/DS17887\0";
  746. break;
  747. default:
  748. model = "Unknown\0";
  749. break;
  750. }
  751. /* Print out the information. */
  752. seq_printf(seq,
  753. "Model\t\t: %s\n"
  754. "Oscillator\t: %s\n"
  755. "12/24hr\t\t: %s\n"
  756. "DST\t\t: %s\n"
  757. "Data mode\t: %s\n"
  758. "Battery\t\t: %s\n"
  759. "Aux batt\t: %s\n"
  760. "Update IRQ\t: %s\n"
  761. "Periodic IRQ\t: %s\n"
  762. "Periodic Rate\t: %s\n"
  763. "SQW Freq\t: %s\n"
  764. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  765. "Serial #\t: %8phC\n"
  766. "Register Status\t:\n"
  767. " Ctrl A\t: UIP DV2 DV1 DV0 RS3 RS2 RS1 RS0\n"
  768. "\t\t: %s\n"
  769. " Ctrl B\t: SET PIE AIE UIE SQWE DM 2412 DSE\n"
  770. "\t\t: %s\n"
  771. " Ctrl C\t: IRQF PF AF UF --- --- --- ---\n"
  772. "\t\t: %s\n"
  773. " Ctrl D\t: VRT --- --- --- --- --- --- ---\n"
  774. "\t\t: %s\n"
  775. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  776. " Ctrl 4A\t: VRT2 INCR BME --- PAB RF WF KF\n"
  777. #else
  778. " Ctrl 4A\t: VRT2 INCR --- --- PAB RF WF KF\n"
  779. #endif
  780. "\t\t: %s\n"
  781. " Ctrl 4B\t: ABE E32k CS RCE PRS RIE WIE KSE\n"
  782. "\t\t: %s\n",
  783. #else
  784. "Serial #\t: %8phC\n",
  785. #endif
  786. model,
  787. ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
  788. ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
  789. ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
  790. ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
  791. ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
  792. ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
  793. ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
  794. ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
  795. (!(ctrl4b & RTC_CTRL_4B_E32K) ?
  796. ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
  797. (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
  798. ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
  799. #ifdef CONFIG_RTC_DS1685_PROC_REGS
  800. ssn,
  801. ds1685_rtc_print_regs(ctrla, bits[0]),
  802. ds1685_rtc_print_regs(ctrlb, bits[1]),
  803. ds1685_rtc_print_regs(ctrlc, bits[2]),
  804. ds1685_rtc_print_regs(ctrld, bits[3]),
  805. ds1685_rtc_print_regs(ctrl4a, bits[4]),
  806. ds1685_rtc_print_regs(ctrl4b, bits[5]));
  807. #else
  808. ssn);
  809. #endif
  810. return 0;
  811. }
  812. #else
  813. #define ds1685_rtc_proc NULL
  814. #endif /* CONFIG_PROC_FS */
  815. /* ----------------------------------------------------------------------- */
  816. /* ----------------------------------------------------------------------- */
  817. /* RTC Class operations */
  818. static const struct rtc_class_ops
  819. ds1685_rtc_ops = {
  820. .proc = ds1685_rtc_proc,
  821. .read_time = ds1685_rtc_read_time,
  822. .set_time = ds1685_rtc_set_time,
  823. .read_alarm = ds1685_rtc_read_alarm,
  824. .set_alarm = ds1685_rtc_set_alarm,
  825. .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
  826. };
  827. /* ----------------------------------------------------------------------- */
  828. /* ----------------------------------------------------------------------- */
  829. /* SysFS interface */
  830. #ifdef CONFIG_SYSFS
  831. /**
  832. * ds1685_rtc_sysfs_nvram_read - reads rtc nvram via sysfs.
  833. * @file: pointer to file structure.
  834. * @kobj: pointer to kobject structure.
  835. * @bin_attr: pointer to bin_attribute structure.
  836. * @buf: pointer to char array to hold the output.
  837. * @pos: current file position pointer.
  838. * @size: size of the data to read.
  839. */
  840. static ssize_t
  841. ds1685_rtc_sysfs_nvram_read(struct file *filp, struct kobject *kobj,
  842. struct bin_attribute *bin_attr, char *buf,
  843. loff_t pos, size_t size)
  844. {
  845. struct platform_device *pdev =
  846. to_platform_device(container_of(kobj, struct device, kobj));
  847. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  848. ssize_t count;
  849. unsigned long flags = 0;
  850. spin_lock_irqsave(&rtc->lock, flags);
  851. ds1685_rtc_switch_to_bank0(rtc);
  852. /* Read NVRAM in time and bank0 registers. */
  853. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  854. count++, size--) {
  855. if (count < NVRAM_SZ_TIME)
  856. *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
  857. else
  858. *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
  859. }
  860. #ifndef CONFIG_RTC_DRV_DS1689
  861. if (size > 0) {
  862. ds1685_rtc_switch_to_bank1(rtc);
  863. #ifndef CONFIG_RTC_DRV_DS1685
  864. /* Enable burst-mode on DS17x85/DS17x87 */
  865. rtc->write(rtc, RTC_EXT_CTRL_4A,
  866. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  867. RTC_CTRL_4A_BME));
  868. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  869. * reading with burst-mode */
  870. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  871. (pos - NVRAM_TOTAL_SZ_BANK0));
  872. #endif
  873. /* Read NVRAM in bank1 registers. */
  874. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  875. count++, size--) {
  876. #ifdef CONFIG_RTC_DRV_DS1685
  877. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  878. * before each read. */
  879. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  880. (pos - NVRAM_TOTAL_SZ_BANK0));
  881. #endif
  882. *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
  883. pos++;
  884. }
  885. #ifndef CONFIG_RTC_DRV_DS1685
  886. /* Disable burst-mode on DS17x85/DS17x87 */
  887. rtc->write(rtc, RTC_EXT_CTRL_4A,
  888. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  889. ~(RTC_CTRL_4A_BME)));
  890. #endif
  891. ds1685_rtc_switch_to_bank0(rtc);
  892. }
  893. #endif /* !CONFIG_RTC_DRV_DS1689 */
  894. spin_unlock_irqrestore(&rtc->lock, flags);
  895. /*
  896. * XXX: Bug? this appears to cause the function to get executed
  897. * several times in succession. But it's the only way to actually get
  898. * data written out to a file.
  899. */
  900. return count;
  901. }
  902. /**
  903. * ds1685_rtc_sysfs_nvram_write - writes rtc nvram via sysfs.
  904. * @file: pointer to file structure.
  905. * @kobj: pointer to kobject structure.
  906. * @bin_attr: pointer to bin_attribute structure.
  907. * @buf: pointer to char array to hold the input.
  908. * @pos: current file position pointer.
  909. * @size: size of the data to write.
  910. */
  911. static ssize_t
  912. ds1685_rtc_sysfs_nvram_write(struct file *filp, struct kobject *kobj,
  913. struct bin_attribute *bin_attr, char *buf,
  914. loff_t pos, size_t size)
  915. {
  916. struct platform_device *pdev =
  917. to_platform_device(container_of(kobj, struct device, kobj));
  918. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  919. ssize_t count;
  920. unsigned long flags = 0;
  921. spin_lock_irqsave(&rtc->lock, flags);
  922. ds1685_rtc_switch_to_bank0(rtc);
  923. /* Write NVRAM in time and bank0 registers. */
  924. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  925. count++, size--)
  926. if (count < NVRAM_SZ_TIME)
  927. rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
  928. *buf++);
  929. else
  930. rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
  931. #ifndef CONFIG_RTC_DRV_DS1689
  932. if (size > 0) {
  933. ds1685_rtc_switch_to_bank1(rtc);
  934. #ifndef CONFIG_RTC_DRV_DS1685
  935. /* Enable burst-mode on DS17x85/DS17x87 */
  936. rtc->write(rtc, RTC_EXT_CTRL_4A,
  937. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  938. RTC_CTRL_4A_BME));
  939. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  940. * writing with burst-mode */
  941. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  942. (pos - NVRAM_TOTAL_SZ_BANK0));
  943. #endif
  944. /* Write NVRAM in bank1 registers. */
  945. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  946. count++, size--) {
  947. #ifdef CONFIG_RTC_DRV_DS1685
  948. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  949. * before each read. */
  950. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  951. (pos - NVRAM_TOTAL_SZ_BANK0));
  952. #endif
  953. rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
  954. pos++;
  955. }
  956. #ifndef CONFIG_RTC_DRV_DS1685
  957. /* Disable burst-mode on DS17x85/DS17x87 */
  958. rtc->write(rtc, RTC_EXT_CTRL_4A,
  959. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  960. ~(RTC_CTRL_4A_BME)));
  961. #endif
  962. ds1685_rtc_switch_to_bank0(rtc);
  963. }
  964. #endif /* !CONFIG_RTC_DRV_DS1689 */
  965. spin_unlock_irqrestore(&rtc->lock, flags);
  966. return count;
  967. }
  968. /**
  969. * struct ds1685_rtc_sysfs_nvram_attr - sysfs attributes for rtc nvram.
  970. * @attr: nvram attributes.
  971. * @read: nvram read function.
  972. * @write: nvram write function.
  973. * @size: nvram total size (bank0 + extended).
  974. */
  975. static struct bin_attribute
  976. ds1685_rtc_sysfs_nvram_attr = {
  977. .attr = {
  978. .name = "nvram",
  979. .mode = S_IRUGO | S_IWUSR,
  980. },
  981. .read = ds1685_rtc_sysfs_nvram_read,
  982. .write = ds1685_rtc_sysfs_nvram_write,
  983. .size = NVRAM_TOTAL_SZ
  984. };
  985. /**
  986. * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
  987. * @dev: pointer to device structure.
  988. * @attr: pointer to device_attribute structure.
  989. * @buf: pointer to char array to hold the output.
  990. */
  991. static ssize_t
  992. ds1685_rtc_sysfs_battery_show(struct device *dev,
  993. struct device_attribute *attr, char *buf)
  994. {
  995. struct platform_device *pdev = to_platform_device(dev);
  996. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  997. u8 ctrld;
  998. ctrld = rtc->read(rtc, RTC_CTRL_D);
  999. return sprintf(buf, "%s\n",
  1000. (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
  1001. }
  1002. static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
  1003. /**
  1004. * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
  1005. * @dev: pointer to device structure.
  1006. * @attr: pointer to device_attribute structure.
  1007. * @buf: pointer to char array to hold the output.
  1008. */
  1009. static ssize_t
  1010. ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
  1011. struct device_attribute *attr, char *buf)
  1012. {
  1013. struct platform_device *pdev = to_platform_device(dev);
  1014. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1015. u8 ctrl4a;
  1016. ds1685_rtc_switch_to_bank1(rtc);
  1017. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1018. ds1685_rtc_switch_to_bank0(rtc);
  1019. return sprintf(buf, "%s\n",
  1020. (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
  1021. }
  1022. static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
  1023. /**
  1024. * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
  1025. * @dev: pointer to device structure.
  1026. * @attr: pointer to device_attribute structure.
  1027. * @buf: pointer to char array to hold the output.
  1028. */
  1029. static ssize_t
  1030. ds1685_rtc_sysfs_serial_show(struct device *dev,
  1031. struct device_attribute *attr, char *buf)
  1032. {
  1033. struct platform_device *pdev = to_platform_device(dev);
  1034. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1035. u8 ssn[8];
  1036. ds1685_rtc_switch_to_bank1(rtc);
  1037. ds1685_rtc_get_ssn(rtc, ssn);
  1038. ds1685_rtc_switch_to_bank0(rtc);
  1039. return sprintf(buf, "%8phC\n", ssn);
  1040. }
  1041. static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
  1042. /**
  1043. * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
  1044. */
  1045. static struct attribute*
  1046. ds1685_rtc_sysfs_misc_attrs[] = {
  1047. &dev_attr_battery.attr,
  1048. &dev_attr_auxbatt.attr,
  1049. &dev_attr_serial.attr,
  1050. NULL,
  1051. };
  1052. /**
  1053. * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
  1054. */
  1055. static const struct attribute_group
  1056. ds1685_rtc_sysfs_misc_grp = {
  1057. .name = "misc",
  1058. .attrs = ds1685_rtc_sysfs_misc_attrs,
  1059. };
  1060. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1061. /**
  1062. * struct ds1685_rtc_ctrl_regs.
  1063. * @name: char pointer for the bit name.
  1064. * @reg: control register the bit is in.
  1065. * @bit: the bit's offset in the register.
  1066. */
  1067. struct ds1685_rtc_ctrl_regs {
  1068. const char *name;
  1069. const u8 reg;
  1070. const u8 bit;
  1071. };
  1072. /*
  1073. * Ctrl register bit lookup table.
  1074. */
  1075. static const struct ds1685_rtc_ctrl_regs
  1076. ds1685_ctrl_regs_table[] = {
  1077. { "uip", RTC_CTRL_A, RTC_CTRL_A_UIP },
  1078. { "dv2", RTC_CTRL_A, RTC_CTRL_A_DV2 },
  1079. { "dv1", RTC_CTRL_A, RTC_CTRL_A_DV1 },
  1080. { "dv0", RTC_CTRL_A, RTC_CTRL_A_DV0 },
  1081. { "rs3", RTC_CTRL_A, RTC_CTRL_A_RS3 },
  1082. { "rs2", RTC_CTRL_A, RTC_CTRL_A_RS2 },
  1083. { "rs1", RTC_CTRL_A, RTC_CTRL_A_RS1 },
  1084. { "rs0", RTC_CTRL_A, RTC_CTRL_A_RS0 },
  1085. { "set", RTC_CTRL_B, RTC_CTRL_B_SET },
  1086. { "pie", RTC_CTRL_B, RTC_CTRL_B_PIE },
  1087. { "aie", RTC_CTRL_B, RTC_CTRL_B_AIE },
  1088. { "uie", RTC_CTRL_B, RTC_CTRL_B_UIE },
  1089. { "sqwe", RTC_CTRL_B, RTC_CTRL_B_SQWE },
  1090. { "dm", RTC_CTRL_B, RTC_CTRL_B_DM },
  1091. { "2412", RTC_CTRL_B, RTC_CTRL_B_2412 },
  1092. { "dse", RTC_CTRL_B, RTC_CTRL_B_DSE },
  1093. { "irqf", RTC_CTRL_C, RTC_CTRL_C_IRQF },
  1094. { "pf", RTC_CTRL_C, RTC_CTRL_C_PF },
  1095. { "af", RTC_CTRL_C, RTC_CTRL_C_AF },
  1096. { "uf", RTC_CTRL_C, RTC_CTRL_C_UF },
  1097. { "vrt", RTC_CTRL_D, RTC_CTRL_D_VRT },
  1098. { "vrt2", RTC_EXT_CTRL_4A, RTC_CTRL_4A_VRT2 },
  1099. { "incr", RTC_EXT_CTRL_4A, RTC_CTRL_4A_INCR },
  1100. { "pab", RTC_EXT_CTRL_4A, RTC_CTRL_4A_PAB },
  1101. { "rf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_RF },
  1102. { "wf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_WF },
  1103. { "kf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_KF },
  1104. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1105. { "bme", RTC_EXT_CTRL_4A, RTC_CTRL_4A_BME },
  1106. #endif
  1107. { "abe", RTC_EXT_CTRL_4B, RTC_CTRL_4B_ABE },
  1108. { "e32k", RTC_EXT_CTRL_4B, RTC_CTRL_4B_E32K },
  1109. { "cs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_CS },
  1110. { "rce", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RCE },
  1111. { "prs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_PRS },
  1112. { "rie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RIE },
  1113. { "wie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_WIE },
  1114. { "kse", RTC_EXT_CTRL_4B, RTC_CTRL_4B_KSE },
  1115. { NULL, 0, 0 },
  1116. };
  1117. /**
  1118. * ds1685_rtc_sysfs_ctrl_regs_lookup - ctrl register bit lookup function.
  1119. * @name: ctrl register bit to look up in ds1685_ctrl_regs_table.
  1120. */
  1121. static const struct ds1685_rtc_ctrl_regs*
  1122. ds1685_rtc_sysfs_ctrl_regs_lookup(const char *name)
  1123. {
  1124. const struct ds1685_rtc_ctrl_regs *p = ds1685_ctrl_regs_table;
  1125. for (; p->name != NULL; ++p)
  1126. if (strcmp(p->name, name) == 0)
  1127. return p;
  1128. return NULL;
  1129. }
  1130. /**
  1131. * ds1685_rtc_sysfs_ctrl_regs_show - reads a ctrl register bit via sysfs.
  1132. * @dev: pointer to device structure.
  1133. * @attr: pointer to device_attribute structure.
  1134. * @buf: pointer to char array to hold the output.
  1135. */
  1136. static ssize_t
  1137. ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev,
  1138. struct device_attribute *attr, char *buf)
  1139. {
  1140. u8 tmp;
  1141. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1142. const struct ds1685_rtc_ctrl_regs *reg_info =
  1143. ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
  1144. /* Make sure we actually matched something. */
  1145. if (!reg_info)
  1146. return -EINVAL;
  1147. /* No spinlock during a read -- mutex is already held. */
  1148. ds1685_rtc_switch_to_bank1(rtc);
  1149. tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit;
  1150. ds1685_rtc_switch_to_bank0(rtc);
  1151. return sprintf(buf, "%d\n", (tmp ? 1 : 0));
  1152. }
  1153. /**
  1154. * ds1685_rtc_sysfs_ctrl_regs_store - writes a ctrl register bit via sysfs.
  1155. * @dev: pointer to device structure.
  1156. * @attr: pointer to device_attribute structure.
  1157. * @buf: pointer to char array to hold the output.
  1158. * @count: number of bytes written.
  1159. */
  1160. static ssize_t
  1161. ds1685_rtc_sysfs_ctrl_regs_store(struct device *dev,
  1162. struct device_attribute *attr,
  1163. const char *buf, size_t count)
  1164. {
  1165. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1166. u8 reg = 0, bit = 0, tmp;
  1167. unsigned long flags;
  1168. long int val = 0;
  1169. const struct ds1685_rtc_ctrl_regs *reg_info =
  1170. ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
  1171. /* We only accept numbers. */
  1172. if (kstrtol(buf, 10, &val) < 0)
  1173. return -EINVAL;
  1174. /* bits are binary, 0 or 1 only. */
  1175. if ((val != 0) && (val != 1))
  1176. return -ERANGE;
  1177. /* Make sure we actually matched something. */
  1178. if (!reg_info)
  1179. return -EINVAL;
  1180. reg = reg_info->reg;
  1181. bit = reg_info->bit;
  1182. /* Safe to spinlock during a write. */
  1183. ds1685_rtc_begin_ctrl_access(rtc, &flags);
  1184. tmp = rtc->read(rtc, reg);
  1185. rtc->write(rtc, reg, (val ? (tmp | bit) : (tmp & ~(bit))));
  1186. ds1685_rtc_end_ctrl_access(rtc, flags);
  1187. return count;
  1188. }
  1189. /**
  1190. * DS1685_RTC_SYSFS_CTRL_REG_RO - device_attribute for read-only register bit.
  1191. * @bit: bit to read.
  1192. */
  1193. #define DS1685_RTC_SYSFS_CTRL_REG_RO(bit) \
  1194. static DEVICE_ATTR(bit, S_IRUGO, \
  1195. ds1685_rtc_sysfs_ctrl_regs_show, NULL)
  1196. /**
  1197. * DS1685_RTC_SYSFS_CTRL_REG_RW - device_attribute for read-write register bit.
  1198. * @bit: bit to read or write.
  1199. */
  1200. #define DS1685_RTC_SYSFS_CTRL_REG_RW(bit) \
  1201. static DEVICE_ATTR(bit, S_IRUGO | S_IWUSR, \
  1202. ds1685_rtc_sysfs_ctrl_regs_show, \
  1203. ds1685_rtc_sysfs_ctrl_regs_store)
  1204. /*
  1205. * Control Register A bits.
  1206. */
  1207. DS1685_RTC_SYSFS_CTRL_REG_RO(uip);
  1208. DS1685_RTC_SYSFS_CTRL_REG_RW(dv2);
  1209. DS1685_RTC_SYSFS_CTRL_REG_RW(dv1);
  1210. DS1685_RTC_SYSFS_CTRL_REG_RO(dv0);
  1211. DS1685_RTC_SYSFS_CTRL_REG_RW(rs3);
  1212. DS1685_RTC_SYSFS_CTRL_REG_RW(rs2);
  1213. DS1685_RTC_SYSFS_CTRL_REG_RW(rs1);
  1214. DS1685_RTC_SYSFS_CTRL_REG_RW(rs0);
  1215. static struct attribute*
  1216. ds1685_rtc_sysfs_ctrla_attrs[] = {
  1217. &dev_attr_uip.attr,
  1218. &dev_attr_dv2.attr,
  1219. &dev_attr_dv1.attr,
  1220. &dev_attr_dv0.attr,
  1221. &dev_attr_rs3.attr,
  1222. &dev_attr_rs2.attr,
  1223. &dev_attr_rs1.attr,
  1224. &dev_attr_rs0.attr,
  1225. NULL,
  1226. };
  1227. static const struct attribute_group
  1228. ds1685_rtc_sysfs_ctrla_grp = {
  1229. .name = "ctrla",
  1230. .attrs = ds1685_rtc_sysfs_ctrla_attrs,
  1231. };
  1232. /*
  1233. * Control Register B bits.
  1234. */
  1235. DS1685_RTC_SYSFS_CTRL_REG_RO(set);
  1236. DS1685_RTC_SYSFS_CTRL_REG_RW(pie);
  1237. DS1685_RTC_SYSFS_CTRL_REG_RW(aie);
  1238. DS1685_RTC_SYSFS_CTRL_REG_RW(uie);
  1239. DS1685_RTC_SYSFS_CTRL_REG_RW(sqwe);
  1240. DS1685_RTC_SYSFS_CTRL_REG_RO(dm);
  1241. DS1685_RTC_SYSFS_CTRL_REG_RO(2412);
  1242. DS1685_RTC_SYSFS_CTRL_REG_RO(dse);
  1243. static struct attribute*
  1244. ds1685_rtc_sysfs_ctrlb_attrs[] = {
  1245. &dev_attr_set.attr,
  1246. &dev_attr_pie.attr,
  1247. &dev_attr_aie.attr,
  1248. &dev_attr_uie.attr,
  1249. &dev_attr_sqwe.attr,
  1250. &dev_attr_dm.attr,
  1251. &dev_attr_2412.attr,
  1252. &dev_attr_dse.attr,
  1253. NULL,
  1254. };
  1255. static const struct attribute_group
  1256. ds1685_rtc_sysfs_ctrlb_grp = {
  1257. .name = "ctrlb",
  1258. .attrs = ds1685_rtc_sysfs_ctrlb_attrs,
  1259. };
  1260. /*
  1261. * Control Register C bits.
  1262. *
  1263. * Reading Control C clears these bits! Reading them individually can
  1264. * possibly cause an interrupt to be missed. Use the /proc interface
  1265. * to see all the bits in this register simultaneously.
  1266. */
  1267. DS1685_RTC_SYSFS_CTRL_REG_RO(irqf);
  1268. DS1685_RTC_SYSFS_CTRL_REG_RO(pf);
  1269. DS1685_RTC_SYSFS_CTRL_REG_RO(af);
  1270. DS1685_RTC_SYSFS_CTRL_REG_RO(uf);
  1271. static struct attribute*
  1272. ds1685_rtc_sysfs_ctrlc_attrs[] = {
  1273. &dev_attr_irqf.attr,
  1274. &dev_attr_pf.attr,
  1275. &dev_attr_af.attr,
  1276. &dev_attr_uf.attr,
  1277. NULL,
  1278. };
  1279. static const struct attribute_group
  1280. ds1685_rtc_sysfs_ctrlc_grp = {
  1281. .name = "ctrlc",
  1282. .attrs = ds1685_rtc_sysfs_ctrlc_attrs,
  1283. };
  1284. /*
  1285. * Control Register D bits.
  1286. */
  1287. DS1685_RTC_SYSFS_CTRL_REG_RO(vrt);
  1288. static struct attribute*
  1289. ds1685_rtc_sysfs_ctrld_attrs[] = {
  1290. &dev_attr_vrt.attr,
  1291. NULL,
  1292. };
  1293. static const struct attribute_group
  1294. ds1685_rtc_sysfs_ctrld_grp = {
  1295. .name = "ctrld",
  1296. .attrs = ds1685_rtc_sysfs_ctrld_attrs,
  1297. };
  1298. /*
  1299. * Control Register 4A bits.
  1300. */
  1301. DS1685_RTC_SYSFS_CTRL_REG_RO(vrt2);
  1302. DS1685_RTC_SYSFS_CTRL_REG_RO(incr);
  1303. DS1685_RTC_SYSFS_CTRL_REG_RW(pab);
  1304. DS1685_RTC_SYSFS_CTRL_REG_RW(rf);
  1305. DS1685_RTC_SYSFS_CTRL_REG_RW(wf);
  1306. DS1685_RTC_SYSFS_CTRL_REG_RW(kf);
  1307. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1308. DS1685_RTC_SYSFS_CTRL_REG_RO(bme);
  1309. #endif
  1310. static struct attribute*
  1311. ds1685_rtc_sysfs_ctrl4a_attrs[] = {
  1312. &dev_attr_vrt2.attr,
  1313. &dev_attr_incr.attr,
  1314. &dev_attr_pab.attr,
  1315. &dev_attr_rf.attr,
  1316. &dev_attr_wf.attr,
  1317. &dev_attr_kf.attr,
  1318. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1319. &dev_attr_bme.attr,
  1320. #endif
  1321. NULL,
  1322. };
  1323. static const struct attribute_group
  1324. ds1685_rtc_sysfs_ctrl4a_grp = {
  1325. .name = "ctrl4a",
  1326. .attrs = ds1685_rtc_sysfs_ctrl4a_attrs,
  1327. };
  1328. /*
  1329. * Control Register 4B bits.
  1330. */
  1331. DS1685_RTC_SYSFS_CTRL_REG_RW(abe);
  1332. DS1685_RTC_SYSFS_CTRL_REG_RW(e32k);
  1333. DS1685_RTC_SYSFS_CTRL_REG_RO(cs);
  1334. DS1685_RTC_SYSFS_CTRL_REG_RW(rce);
  1335. DS1685_RTC_SYSFS_CTRL_REG_RW(prs);
  1336. DS1685_RTC_SYSFS_CTRL_REG_RW(rie);
  1337. DS1685_RTC_SYSFS_CTRL_REG_RW(wie);
  1338. DS1685_RTC_SYSFS_CTRL_REG_RW(kse);
  1339. static struct attribute*
  1340. ds1685_rtc_sysfs_ctrl4b_attrs[] = {
  1341. &dev_attr_abe.attr,
  1342. &dev_attr_e32k.attr,
  1343. &dev_attr_cs.attr,
  1344. &dev_attr_rce.attr,
  1345. &dev_attr_prs.attr,
  1346. &dev_attr_rie.attr,
  1347. &dev_attr_wie.attr,
  1348. &dev_attr_kse.attr,
  1349. NULL,
  1350. };
  1351. static const struct attribute_group
  1352. ds1685_rtc_sysfs_ctrl4b_grp = {
  1353. .name = "ctrl4b",
  1354. .attrs = ds1685_rtc_sysfs_ctrl4b_attrs,
  1355. };
  1356. /**
  1357. * struct ds1685_rtc_ctrl_regs.
  1358. * @name: char pointer for the bit name.
  1359. * @reg: control register the bit is in.
  1360. * @bit: the bit's offset in the register.
  1361. */
  1362. struct ds1685_rtc_time_regs {
  1363. const char *name;
  1364. const u8 reg;
  1365. const u8 mask;
  1366. const u8 min;
  1367. const u8 max;
  1368. };
  1369. /*
  1370. * Time/Date register lookup tables.
  1371. */
  1372. static const struct ds1685_rtc_time_regs
  1373. ds1685_time_regs_bcd_table[] = {
  1374. { "seconds", RTC_SECS, RTC_SECS_BCD_MASK, 0, 59 },
  1375. { "minutes", RTC_MINS, RTC_MINS_BCD_MASK, 0, 59 },
  1376. { "hours", RTC_HRS, RTC_HRS_24_BCD_MASK, 0, 23 },
  1377. { "wday", RTC_WDAY, RTC_WDAY_MASK, 1, 7 },
  1378. { "mday", RTC_MDAY, RTC_MDAY_BCD_MASK, 1, 31 },
  1379. { "month", RTC_MONTH, RTC_MONTH_BCD_MASK, 1, 12 },
  1380. { "year", RTC_YEAR, RTC_YEAR_BCD_MASK, 0, 99 },
  1381. { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0, 99 },
  1382. { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BCD_MASK, 0, 59 },
  1383. { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BCD_MASK, 0, 59 },
  1384. { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BCD_MASK, 0, 23 },
  1385. { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 1, 31 },
  1386. { NULL, 0, 0, 0, 0 },
  1387. };
  1388. static const struct ds1685_rtc_time_regs
  1389. ds1685_time_regs_bin_table[] = {
  1390. { "seconds", RTC_SECS, RTC_SECS_BIN_MASK, 0x00, 0x3b },
  1391. { "minutes", RTC_MINS, RTC_MINS_BIN_MASK, 0x00, 0x3b },
  1392. { "hours", RTC_HRS, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
  1393. { "wday", RTC_WDAY, RTC_WDAY_MASK, 0x01, 0x07 },
  1394. { "mday", RTC_MDAY, RTC_MDAY_BIN_MASK, 0x01, 0x1f },
  1395. { "month", RTC_MONTH, RTC_MONTH_BIN_MASK, 0x01, 0x0c },
  1396. { "year", RTC_YEAR, RTC_YEAR_BIN_MASK, 0x00, 0x63 },
  1397. { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0x00, 0x63 },
  1398. { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BIN_MASK, 0x00, 0x3b },
  1399. { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BIN_MASK, 0x00, 0x3b },
  1400. { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
  1401. { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 0x01, 0x1f },
  1402. { NULL, 0, 0, 0x00, 0x00 },
  1403. };
  1404. /**
  1405. * ds1685_rtc_sysfs_time_regs_bcd_lookup - time/date reg bit lookup function.
  1406. * @name: register bit to look up in ds1685_time_regs_bcd_table.
  1407. */
  1408. static const struct ds1685_rtc_time_regs*
  1409. ds1685_rtc_sysfs_time_regs_lookup(const char *name, bool bcd_mode)
  1410. {
  1411. const struct ds1685_rtc_time_regs *p;
  1412. if (bcd_mode)
  1413. p = ds1685_time_regs_bcd_table;
  1414. else
  1415. p = ds1685_time_regs_bin_table;
  1416. for (; p->name != NULL; ++p)
  1417. if (strcmp(p->name, name) == 0)
  1418. return p;
  1419. return NULL;
  1420. }
  1421. /**
  1422. * ds1685_rtc_sysfs_time_regs_show - reads a time/date register via sysfs.
  1423. * @dev: pointer to device structure.
  1424. * @attr: pointer to device_attribute structure.
  1425. * @buf: pointer to char array to hold the output.
  1426. */
  1427. static ssize_t
  1428. ds1685_rtc_sysfs_time_regs_show(struct device *dev,
  1429. struct device_attribute *attr, char *buf)
  1430. {
  1431. u8 tmp;
  1432. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1433. const struct ds1685_rtc_time_regs *bcd_reg_info =
  1434. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
  1435. const struct ds1685_rtc_time_regs *bin_reg_info =
  1436. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
  1437. /* Make sure we actually matched something. */
  1438. if (!bcd_reg_info || !bin_reg_info)
  1439. return -EINVAL;
  1440. /* bcd_reg_info->reg == bin_reg_info->reg. */
  1441. ds1685_rtc_begin_data_access(rtc);
  1442. tmp = rtc->read(rtc, bcd_reg_info->reg);
  1443. ds1685_rtc_end_data_access(rtc);
  1444. tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask,
  1445. bin_reg_info->mask);
  1446. return sprintf(buf, "%d\n", tmp);
  1447. }
  1448. /**
  1449. * ds1685_rtc_sysfs_time_regs_store - writes a time/date register via sysfs.
  1450. * @dev: pointer to device structure.
  1451. * @attr: pointer to device_attribute structure.
  1452. * @buf: pointer to char array to hold the output.
  1453. * @count: number of bytes written.
  1454. */
  1455. static ssize_t
  1456. ds1685_rtc_sysfs_time_regs_store(struct device *dev,
  1457. struct device_attribute *attr,
  1458. const char *buf, size_t count)
  1459. {
  1460. long int val = 0;
  1461. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1462. const struct ds1685_rtc_time_regs *bcd_reg_info =
  1463. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
  1464. const struct ds1685_rtc_time_regs *bin_reg_info =
  1465. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
  1466. /* We only accept numbers. */
  1467. if (kstrtol(buf, 10, &val) < 0)
  1468. return -EINVAL;
  1469. /* Make sure we actually matched something. */
  1470. if (!bcd_reg_info || !bin_reg_info)
  1471. return -EINVAL;
  1472. /* Check for a valid range. */
  1473. if (rtc->bcd_mode) {
  1474. if ((val < bcd_reg_info->min) || (val > bcd_reg_info->max))
  1475. return -ERANGE;
  1476. } else {
  1477. if ((val < bin_reg_info->min) || (val > bin_reg_info->max))
  1478. return -ERANGE;
  1479. }
  1480. val = ds1685_rtc_bin2bcd(rtc, val, bin_reg_info->mask,
  1481. bcd_reg_info->mask);
  1482. /* bcd_reg_info->reg == bin_reg_info->reg. */
  1483. ds1685_rtc_begin_data_access(rtc);
  1484. rtc->write(rtc, bcd_reg_info->reg, val);
  1485. ds1685_rtc_end_data_access(rtc);
  1486. return count;
  1487. }
  1488. /**
  1489. * DS1685_RTC_SYSFS_REG_RW - device_attribute for a read-write time register.
  1490. * @reg: time/date register to read or write.
  1491. */
  1492. #define DS1685_RTC_SYSFS_TIME_REG_RW(reg) \
  1493. static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, \
  1494. ds1685_rtc_sysfs_time_regs_show, \
  1495. ds1685_rtc_sysfs_time_regs_store)
  1496. /*
  1497. * Time/Date Register bits.
  1498. */
  1499. DS1685_RTC_SYSFS_TIME_REG_RW(seconds);
  1500. DS1685_RTC_SYSFS_TIME_REG_RW(minutes);
  1501. DS1685_RTC_SYSFS_TIME_REG_RW(hours);
  1502. DS1685_RTC_SYSFS_TIME_REG_RW(wday);
  1503. DS1685_RTC_SYSFS_TIME_REG_RW(mday);
  1504. DS1685_RTC_SYSFS_TIME_REG_RW(month);
  1505. DS1685_RTC_SYSFS_TIME_REG_RW(year);
  1506. DS1685_RTC_SYSFS_TIME_REG_RW(century);
  1507. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_seconds);
  1508. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_minutes);
  1509. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_hours);
  1510. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_mday);
  1511. static struct attribute*
  1512. ds1685_rtc_sysfs_time_attrs[] = {
  1513. &dev_attr_seconds.attr,
  1514. &dev_attr_minutes.attr,
  1515. &dev_attr_hours.attr,
  1516. &dev_attr_wday.attr,
  1517. &dev_attr_mday.attr,
  1518. &dev_attr_month.attr,
  1519. &dev_attr_year.attr,
  1520. &dev_attr_century.attr,
  1521. NULL,
  1522. };
  1523. static const struct attribute_group
  1524. ds1685_rtc_sysfs_time_grp = {
  1525. .name = "datetime",
  1526. .attrs = ds1685_rtc_sysfs_time_attrs,
  1527. };
  1528. static struct attribute*
  1529. ds1685_rtc_sysfs_alarm_attrs[] = {
  1530. &dev_attr_alarm_seconds.attr,
  1531. &dev_attr_alarm_minutes.attr,
  1532. &dev_attr_alarm_hours.attr,
  1533. &dev_attr_alarm_mday.attr,
  1534. NULL,
  1535. };
  1536. static const struct attribute_group
  1537. ds1685_rtc_sysfs_alarm_grp = {
  1538. .name = "alarm",
  1539. .attrs = ds1685_rtc_sysfs_alarm_attrs,
  1540. };
  1541. #endif /* CONFIG_RTC_DS1685_SYSFS_REGS */
  1542. /**
  1543. * ds1685_rtc_sysfs_register - register sysfs files.
  1544. * @dev: pointer to device structure.
  1545. */
  1546. static int
  1547. ds1685_rtc_sysfs_register(struct device *dev)
  1548. {
  1549. int ret = 0;
  1550. sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr);
  1551. ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1552. if (ret)
  1553. return ret;
  1554. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1555. if (ret)
  1556. return ret;
  1557. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1558. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
  1559. if (ret)
  1560. return ret;
  1561. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
  1562. if (ret)
  1563. return ret;
  1564. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
  1565. if (ret)
  1566. return ret;
  1567. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
  1568. if (ret)
  1569. return ret;
  1570. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
  1571. if (ret)
  1572. return ret;
  1573. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
  1574. if (ret)
  1575. return ret;
  1576. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
  1577. if (ret)
  1578. return ret;
  1579. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
  1580. if (ret)
  1581. return ret;
  1582. #endif
  1583. return 0;
  1584. }
  1585. /**
  1586. * ds1685_rtc_sysfs_unregister - unregister sysfs files.
  1587. * @dev: pointer to device structure.
  1588. */
  1589. static int
  1590. ds1685_rtc_sysfs_unregister(struct device *dev)
  1591. {
  1592. sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1593. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1594. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1595. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
  1596. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
  1597. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
  1598. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
  1599. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
  1600. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
  1601. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
  1602. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
  1603. #endif
  1604. return 0;
  1605. }
  1606. #endif /* CONFIG_SYSFS */
  1607. /* ----------------------------------------------------------------------- */
  1608. /* Driver Probe/Removal */
  1609. /**
  1610. * ds1685_rtc_probe - initializes rtc driver.
  1611. * @pdev: pointer to platform_device structure.
  1612. */
  1613. static int
  1614. ds1685_rtc_probe(struct platform_device *pdev)
  1615. {
  1616. struct rtc_device *rtc_dev;
  1617. struct resource *res;
  1618. struct ds1685_priv *rtc;
  1619. struct ds1685_rtc_platform_data *pdata;
  1620. u8 ctrla, ctrlb, hours;
  1621. unsigned char am_pm;
  1622. int ret = 0;
  1623. /* Get the platform data. */
  1624. pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
  1625. if (!pdata)
  1626. return -ENODEV;
  1627. /* Allocate memory for the rtc device. */
  1628. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  1629. if (!rtc)
  1630. return -ENOMEM;
  1631. /*
  1632. * Allocate/setup any IORESOURCE_MEM resources, if required. Not all
  1633. * platforms put the RTC in an easy-access place. Like the SGI Octane,
  1634. * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip
  1635. * that sits behind the IOC3 PCI metadevice.
  1636. */
  1637. if (pdata->alloc_io_resources) {
  1638. /* Get the platform resources. */
  1639. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1640. if (!res)
  1641. return -ENXIO;
  1642. rtc->size = resource_size(res);
  1643. /* Request a memory region. */
  1644. /* XXX: mmio-only for now. */
  1645. if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
  1646. pdev->name))
  1647. return -EBUSY;
  1648. /*
  1649. * Set the base address for the rtc, and ioremap its
  1650. * registers.
  1651. */
  1652. rtc->baseaddr = res->start;
  1653. rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
  1654. if (!rtc->regs)
  1655. return -ENOMEM;
  1656. }
  1657. rtc->alloc_io_resources = pdata->alloc_io_resources;
  1658. /* Get the register step size. */
  1659. if (pdata->regstep > 0)
  1660. rtc->regstep = pdata->regstep;
  1661. else
  1662. rtc->regstep = 1;
  1663. /* Platform read function, else default if mmio setup */
  1664. if (pdata->plat_read)
  1665. rtc->read = pdata->plat_read;
  1666. else
  1667. if (pdata->alloc_io_resources)
  1668. rtc->read = ds1685_read;
  1669. else
  1670. return -ENXIO;
  1671. /* Platform write function, else default if mmio setup */
  1672. if (pdata->plat_write)
  1673. rtc->write = pdata->plat_write;
  1674. else
  1675. if (pdata->alloc_io_resources)
  1676. rtc->write = ds1685_write;
  1677. else
  1678. return -ENXIO;
  1679. /* Platform pre-shutdown function, if defined. */
  1680. if (pdata->plat_prepare_poweroff)
  1681. rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
  1682. /* Platform wake_alarm function, if defined. */
  1683. if (pdata->plat_wake_alarm)
  1684. rtc->wake_alarm = pdata->plat_wake_alarm;
  1685. /* Platform post_ram_clear function, if defined. */
  1686. if (pdata->plat_post_ram_clear)
  1687. rtc->post_ram_clear = pdata->plat_post_ram_clear;
  1688. /* Init the spinlock, workqueue, & set the driver data. */
  1689. spin_lock_init(&rtc->lock);
  1690. INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
  1691. platform_set_drvdata(pdev, rtc);
  1692. /* Turn the oscillator on if is not already on (DV1 = 1). */
  1693. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1694. if (!(ctrla & RTC_CTRL_A_DV1))
  1695. ctrla |= RTC_CTRL_A_DV1;
  1696. /* Enable the countdown chain (DV2 = 0) */
  1697. ctrla &= ~(RTC_CTRL_A_DV2);
  1698. /* Clear RS3-RS0 in Control A. */
  1699. ctrla &= ~(RTC_CTRL_A_RS_MASK);
  1700. /*
  1701. * All done with Control A. Switch to Bank 1 for the remainder of
  1702. * the RTC setup so we have access to the extended functions.
  1703. */
  1704. ctrla |= RTC_CTRL_A_DV0;
  1705. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1706. /* Default to 32768kHz output. */
  1707. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1708. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
  1709. /* Set the SET bit in Control B so we can do some housekeeping. */
  1710. rtc->write(rtc, RTC_CTRL_B,
  1711. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  1712. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  1713. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  1714. cpu_relax();
  1715. /*
  1716. * If the platform supports BCD mode, then set DM=0 in Control B.
  1717. * Otherwise, set DM=1 for BIN mode.
  1718. */
  1719. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  1720. if (pdata->bcd_mode)
  1721. ctrlb &= ~(RTC_CTRL_B_DM);
  1722. else
  1723. ctrlb |= RTC_CTRL_B_DM;
  1724. rtc->bcd_mode = pdata->bcd_mode;
  1725. /*
  1726. * Disable Daylight Savings Time (DSE = 0).
  1727. * The RTC has hardcoded timezone information that is rendered
  1728. * obselete. We'll let the OS deal with DST settings instead.
  1729. */
  1730. if (ctrlb & RTC_CTRL_B_DSE)
  1731. ctrlb &= ~(RTC_CTRL_B_DSE);
  1732. /* Force 24-hour mode (2412 = 1). */
  1733. if (!(ctrlb & RTC_CTRL_B_2412)) {
  1734. /* Reinitialize the time hours. */
  1735. hours = rtc->read(rtc, RTC_HRS);
  1736. am_pm = hours & RTC_HRS_AMPM_MASK;
  1737. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1738. RTC_HRS_12_BIN_MASK);
  1739. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1740. /* Enable 24-hour mode. */
  1741. ctrlb |= RTC_CTRL_B_2412;
  1742. /* Write back to Control B, including DM & DSE bits. */
  1743. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1744. /* Write the time hours back. */
  1745. rtc->write(rtc, RTC_HRS,
  1746. ds1685_rtc_bin2bcd(rtc, hours,
  1747. RTC_HRS_24_BIN_MASK,
  1748. RTC_HRS_24_BCD_MASK));
  1749. /* Reinitialize the alarm hours. */
  1750. hours = rtc->read(rtc, RTC_HRS_ALARM);
  1751. am_pm = hours & RTC_HRS_AMPM_MASK;
  1752. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1753. RTC_HRS_12_BIN_MASK);
  1754. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1755. /* Write the alarm hours back. */
  1756. rtc->write(rtc, RTC_HRS_ALARM,
  1757. ds1685_rtc_bin2bcd(rtc, hours,
  1758. RTC_HRS_24_BIN_MASK,
  1759. RTC_HRS_24_BCD_MASK));
  1760. } else {
  1761. /* 24-hour mode is already set, so write Control B back. */
  1762. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1763. }
  1764. /* Unset the SET bit in Control B so the RTC can update. */
  1765. rtc->write(rtc, RTC_CTRL_B,
  1766. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  1767. /* Check the main battery. */
  1768. if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
  1769. dev_warn(&pdev->dev,
  1770. "Main battery is exhausted! RTC may be invalid!\n");
  1771. /* Check the auxillary battery. It is optional. */
  1772. if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
  1773. dev_warn(&pdev->dev,
  1774. "Aux battery is exhausted or not available.\n");
  1775. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1776. rtc->write(rtc, RTC_CTRL_B,
  1777. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
  1778. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1779. rtc->read(rtc, RTC_CTRL_C);
  1780. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1781. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1782. (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
  1783. /* Clear RF/WF/KF in Ctrl 4A. */
  1784. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1785. (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
  1786. /*
  1787. * Re-enable KSE to handle power button events. We do not enable
  1788. * WIE or RIE by default.
  1789. */
  1790. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1791. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
  1792. /*
  1793. * Fetch the IRQ and setup the interrupt handler.
  1794. *
  1795. * Not all platforms have the IRQF pin tied to something. If not, the
  1796. * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
  1797. * there won't be an automatic way of notifying the kernel about it,
  1798. * unless ctrlc is explicitly polled.
  1799. */
  1800. if (!pdata->no_irq) {
  1801. ret = platform_get_irq(pdev, 0);
  1802. if (ret > 0) {
  1803. rtc->irq_num = ret;
  1804. /* Request an IRQ. */
  1805. ret = devm_request_irq(&pdev->dev, rtc->irq_num,
  1806. ds1685_rtc_irq_handler,
  1807. IRQF_SHARED, pdev->name, pdev);
  1808. /* Check to see if something came back. */
  1809. if (unlikely(ret)) {
  1810. dev_warn(&pdev->dev,
  1811. "RTC interrupt not available\n");
  1812. rtc->irq_num = 0;
  1813. }
  1814. } else
  1815. return ret;
  1816. }
  1817. rtc->no_irq = pdata->no_irq;
  1818. /* Setup complete. */
  1819. ds1685_rtc_switch_to_bank0(rtc);
  1820. /* Register the device as an RTC. */
  1821. rtc_dev = rtc_device_register(pdev->name, &pdev->dev,
  1822. &ds1685_rtc_ops, THIS_MODULE);
  1823. /* Success? */
  1824. if (IS_ERR(rtc_dev))
  1825. return PTR_ERR(rtc_dev);
  1826. /* Maximum periodic rate is 8192Hz (0.122070ms). */
  1827. rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
  1828. /* See if the platform doesn't support UIE. */
  1829. if (pdata->uie_unsupported)
  1830. rtc_dev->uie_unsupported = 1;
  1831. rtc->uie_unsupported = pdata->uie_unsupported;
  1832. rtc->dev = rtc_dev;
  1833. #ifdef CONFIG_SYSFS
  1834. ret = ds1685_rtc_sysfs_register(&pdev->dev);
  1835. if (ret)
  1836. rtc_device_unregister(rtc->dev);
  1837. #endif
  1838. /* Done! */
  1839. return ret;
  1840. }
  1841. /**
  1842. * ds1685_rtc_remove - removes rtc driver.
  1843. * @pdev: pointer to platform_device structure.
  1844. */
  1845. static int
  1846. ds1685_rtc_remove(struct platform_device *pdev)
  1847. {
  1848. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1849. #ifdef CONFIG_SYSFS
  1850. ds1685_rtc_sysfs_unregister(&pdev->dev);
  1851. #endif
  1852. rtc_device_unregister(rtc->dev);
  1853. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1854. rtc->write(rtc, RTC_CTRL_B,
  1855. (rtc->read(rtc, RTC_CTRL_B) &
  1856. ~(RTC_CTRL_B_PAU_MASK)));
  1857. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1858. rtc->read(rtc, RTC_CTRL_C);
  1859. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1860. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1861. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  1862. ~(RTC_CTRL_4B_RWK_MASK)));
  1863. /* Manually clear RF/WF/KF in Ctrl 4A. */
  1864. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1865. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  1866. ~(RTC_CTRL_4A_RWK_MASK)));
  1867. cancel_work_sync(&rtc->work);
  1868. return 0;
  1869. }
  1870. /**
  1871. * ds1685_rtc_driver - rtc driver properties.
  1872. */
  1873. static struct platform_driver ds1685_rtc_driver = {
  1874. .driver = {
  1875. .name = "rtc-ds1685",
  1876. },
  1877. .probe = ds1685_rtc_probe,
  1878. .remove = ds1685_rtc_remove,
  1879. };
  1880. module_platform_driver(ds1685_rtc_driver);
  1881. /* ----------------------------------------------------------------------- */
  1882. /* ----------------------------------------------------------------------- */
  1883. /* Poweroff function */
  1884. /**
  1885. * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
  1886. * @pdev: pointer to platform_device structure.
  1887. */
  1888. void __noreturn
  1889. ds1685_rtc_poweroff(struct platform_device *pdev)
  1890. {
  1891. u8 ctrla, ctrl4a, ctrl4b;
  1892. struct ds1685_priv *rtc;
  1893. /* Check for valid RTC data, else, spin forever. */
  1894. if (unlikely(!pdev)) {
  1895. pr_emerg("platform device data not available, spinning forever ...\n");
  1896. while(1);
  1897. unreachable();
  1898. } else {
  1899. /* Get the rtc data. */
  1900. rtc = platform_get_drvdata(pdev);
  1901. /*
  1902. * Disable our IRQ. We're powering down, so we're not
  1903. * going to worry about cleaning up. Most of that should
  1904. * have been taken care of by the shutdown scripts and this
  1905. * is the final function call.
  1906. */
  1907. if (!rtc->no_irq)
  1908. disable_irq_nosync(rtc->irq_num);
  1909. /* Oscillator must be on and the countdown chain enabled. */
  1910. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1911. ctrla |= RTC_CTRL_A_DV1;
  1912. ctrla &= ~(RTC_CTRL_A_DV2);
  1913. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1914. /*
  1915. * Read Control 4A and check the status of the auxillary
  1916. * battery. This must be present and working (VRT2 = 1)
  1917. * for wakeup and kickstart functionality to be useful.
  1918. */
  1919. ds1685_rtc_switch_to_bank1(rtc);
  1920. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1921. if (ctrl4a & RTC_CTRL_4A_VRT2) {
  1922. /* Clear all of the interrupt flags on Control 4A. */
  1923. ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
  1924. rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
  1925. /*
  1926. * The auxillary battery is present and working.
  1927. * Enable extended functions (ABE=1), enable
  1928. * wake-up (WIE=1), and enable kickstart (KSE=1)
  1929. * in Control 4B.
  1930. */
  1931. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  1932. ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
  1933. RTC_CTRL_4B_KSE);
  1934. rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
  1935. }
  1936. /* Set PAB to 1 in Control 4A to power the system down. */
  1937. dev_warn(&pdev->dev, "Powerdown.\n");
  1938. msleep(20);
  1939. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1940. (ctrl4a | RTC_CTRL_4A_PAB));
  1941. /* Spin ... we do not switch back to bank0. */
  1942. while(1);
  1943. unreachable();
  1944. }
  1945. }
  1946. EXPORT_SYMBOL(ds1685_rtc_poweroff);
  1947. /* ----------------------------------------------------------------------- */
  1948. MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
  1949. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
  1950. MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
  1951. MODULE_LICENSE("GPL");
  1952. MODULE_ALIAS("platform:rtc-ds1685");