rtc-ds1685.c 64 KB

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