rtc-ds1685.c 64 KB

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