rtc-ds1685.c 65 KB

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