rtc-ds1685.c 64 KB

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