rtc-ds1685.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. /*
  2. * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time
  3. * chips.
  4. *
  5. * Copyright (C) 2011-2014 Joshua Kinard <kumba@gentoo.org>.
  6. * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>.
  7. *
  8. * References:
  9. * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10.
  10. * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10.
  11. * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105.
  12. * Application Note 90, Using the Multiplex Bus RTC Extended Features.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/bcd.h>
  19. #include <linux/delay.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/rtc.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/rtc/ds1685.h>
  26. #ifdef CONFIG_PROC_FS
  27. #include <linux/proc_fs.h>
  28. #endif
  29. #define DRV_VERSION "0.42.0"
  30. /* ----------------------------------------------------------------------- */
  31. /* Standard read/write functions if platform does not provide overrides */
  32. /**
  33. * ds1685_read - read a value from an rtc register.
  34. * @rtc: pointer to the ds1685 rtc structure.
  35. * @reg: the register address to read.
  36. */
  37. static u8
  38. ds1685_read(struct ds1685_priv *rtc, int reg)
  39. {
  40. return readb((u8 __iomem *)rtc->regs +
  41. (reg * rtc->regstep));
  42. }
  43. /**
  44. * ds1685_write - write a value to an rtc register.
  45. * @rtc: pointer to the ds1685 rtc structure.
  46. * @reg: the register address to write.
  47. * @value: value to write to the register.
  48. */
  49. static void
  50. ds1685_write(struct ds1685_priv *rtc, int reg, u8 value)
  51. {
  52. writeb(value, ((u8 __iomem *)rtc->regs +
  53. (reg * rtc->regstep)));
  54. }
  55. /* ----------------------------------------------------------------------- */
  56. /* ----------------------------------------------------------------------- */
  57. /* Inlined functions */
  58. /**
  59. * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD.
  60. * @rtc: pointer to the ds1685 rtc structure.
  61. * @val: u8 time value to consider converting.
  62. * @bcd_mask: u8 mask value if BCD mode is used.
  63. * @bin_mask: u8 mask value if BIN mode is used.
  64. *
  65. * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE.
  66. */
  67. static inline u8
  68. ds1685_rtc_bcd2bin(struct ds1685_priv *rtc, u8 val, u8 bcd_mask, u8 bin_mask)
  69. {
  70. if (rtc->bcd_mode)
  71. return (bcd2bin(val) & bcd_mask);
  72. return (val & bin_mask);
  73. }
  74. /**
  75. * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
  76. * @rtc: pointer to the ds1685 rtc structure.
  77. * @val: u8 time value to consider converting.
  78. * @bin_mask: u8 mask value if BIN mode is used.
  79. * @bcd_mask: u8 mask value if BCD mode is used.
  80. *
  81. * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE.
  82. */
  83. static inline u8
  84. ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask)
  85. {
  86. if (rtc->bcd_mode)
  87. return (bin2bcd(val) & bcd_mask);
  88. return (val & bin_mask);
  89. }
  90. /**
  91. * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
  92. * @rtc: pointer to the ds1685 rtc structure.
  93. */
  94. static inline void
  95. ds1685_rtc_switch_to_bank0(struct ds1685_priv *rtc)
  96. {
  97. rtc->write(rtc, RTC_CTRL_A,
  98. (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0)));
  99. }
  100. /**
  101. * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1.
  102. * @rtc: pointer to the ds1685 rtc structure.
  103. */
  104. static inline void
  105. ds1685_rtc_switch_to_bank1(struct ds1685_priv *rtc)
  106. {
  107. rtc->write(rtc, RTC_CTRL_A,
  108. (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0));
  109. }
  110. /**
  111. * ds1685_rtc_begin_data_access - prepare the rtc for data access.
  112. * @rtc: pointer to the ds1685 rtc structure.
  113. *
  114. * This takes several steps to prepare the rtc for access to get/set time
  115. * and alarm values from the rtc registers:
  116. * - Sets the SET bit in Control Register B.
  117. * - Reads Ext Control Register 4A and checks the INCR bit.
  118. * - If INCR is active, a short delay is added before Ext Control Register 4A
  119. * is read again in a loop until INCR is inactive.
  120. * - Switches the rtc to bank 1. This allows access to all relevant
  121. * data for normal rtc operation, as bank 0 contains only the nvram.
  122. */
  123. static inline void
  124. ds1685_rtc_begin_data_access(struct ds1685_priv *rtc)
  125. {
  126. /* Set the SET bit in Ctrl B */
  127. rtc->write(rtc, RTC_CTRL_B,
  128. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  129. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  130. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  131. cpu_relax();
  132. /* Switch to Bank 1 */
  133. ds1685_rtc_switch_to_bank1(rtc);
  134. }
  135. /**
  136. * ds1685_rtc_end_data_access - end data access on the rtc.
  137. * @rtc: pointer to the ds1685 rtc structure.
  138. *
  139. * This ends what was started by ds1685_rtc_begin_data_access:
  140. * - Switches the rtc back to bank 0.
  141. * - Clears the SET bit in Control Register B.
  142. */
  143. static inline void
  144. ds1685_rtc_end_data_access(struct ds1685_priv *rtc)
  145. {
  146. /* Switch back to Bank 0 */
  147. ds1685_rtc_switch_to_bank1(rtc);
  148. /* Clear the SET bit in Ctrl B */
  149. rtc->write(rtc, RTC_CTRL_B,
  150. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  151. }
  152. /**
  153. * ds1685_rtc_begin_ctrl_access - prepare the rtc for ctrl access.
  154. * @rtc: pointer to the ds1685 rtc structure.
  155. * @flags: irq flags variable for spin_lock_irqsave.
  156. *
  157. * This takes several steps to prepare the rtc for access to read just the
  158. * control registers:
  159. * - Sets a spinlock on the rtc IRQ.
  160. * - Switches the rtc to bank 1. This allows access to the two extended
  161. * control registers.
  162. *
  163. * Only use this where you are certain another lock will not be held.
  164. */
  165. static inline void
  166. ds1685_rtc_begin_ctrl_access(struct ds1685_priv *rtc, unsigned long flags)
  167. {
  168. spin_lock_irqsave(&rtc->lock, flags);
  169. ds1685_rtc_switch_to_bank1(rtc);
  170. }
  171. /**
  172. * ds1685_rtc_end_ctrl_access - end ctrl access on the rtc.
  173. * @rtc: pointer to the ds1685 rtc structure.
  174. * @flags: irq flags variable for spin_unlock_irqrestore.
  175. *
  176. * This ends what was started by ds1685_rtc_begin_ctrl_access:
  177. * - Switches the rtc back to bank 0.
  178. * - Unsets the spinlock on the rtc IRQ.
  179. */
  180. static inline void
  181. ds1685_rtc_end_ctrl_access(struct ds1685_priv *rtc, unsigned long flags)
  182. {
  183. ds1685_rtc_switch_to_bank0(rtc);
  184. spin_unlock_irqrestore(&rtc->lock, flags);
  185. }
  186. /**
  187. * ds1685_rtc_get_ssn - retrieve the silicon serial number.
  188. * @rtc: pointer to the ds1685 rtc structure.
  189. * @ssn: u8 array to hold the bits of the silicon serial number.
  190. *
  191. * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
  192. * first byte is the model number, the next six bytes are the serial number
  193. * digits, and the final byte is a CRC check byte. Together, they form the
  194. * silicon serial number.
  195. *
  196. * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be
  197. * called first before calling this function, else data will be read out of
  198. * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done.
  199. */
  200. static inline void
  201. ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn)
  202. {
  203. ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL);
  204. ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1);
  205. ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2);
  206. ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3);
  207. ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4);
  208. ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5);
  209. ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6);
  210. ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC);
  211. }
  212. /* ----------------------------------------------------------------------- */
  213. /* ----------------------------------------------------------------------- */
  214. /* Read/Set Time & Alarm functions */
  215. /**
  216. * ds1685_rtc_read_time - reads the time registers.
  217. * @dev: pointer to device structure.
  218. * @tm: pointer to rtc_time structure.
  219. */
  220. static int
  221. ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm)
  222. {
  223. struct platform_device *pdev = to_platform_device(dev);
  224. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  225. u8 ctrlb, century;
  226. u8 seconds, minutes, hours, wday, mday, month, years;
  227. /* Fetch the time info from the RTC registers. */
  228. ds1685_rtc_begin_data_access(rtc);
  229. seconds = rtc->read(rtc, RTC_SECS);
  230. minutes = rtc->read(rtc, RTC_MINS);
  231. hours = rtc->read(rtc, RTC_HRS);
  232. wday = rtc->read(rtc, RTC_WDAY);
  233. mday = rtc->read(rtc, RTC_MDAY);
  234. month = rtc->read(rtc, RTC_MONTH);
  235. years = rtc->read(rtc, RTC_YEAR);
  236. century = rtc->read(rtc, RTC_CENTURY);
  237. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  238. ds1685_rtc_end_data_access(rtc);
  239. /* bcd2bin if needed, perform fixups, and store to rtc_time. */
  240. years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
  241. RTC_YEAR_BIN_MASK);
  242. century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
  243. RTC_CENTURY_MASK);
  244. tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
  245. RTC_SECS_BIN_MASK);
  246. tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
  247. RTC_MINS_BIN_MASK);
  248. tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
  249. RTC_HRS_24_BIN_MASK);
  250. tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
  251. RTC_WDAY_MASK) - 1);
  252. tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  253. RTC_MDAY_BIN_MASK);
  254. tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
  255. RTC_MONTH_BIN_MASK) - 1);
  256. tm->tm_year = ((years + (century * 100)) - 1900);
  257. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  258. tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
  259. return rtc_valid_tm(tm);
  260. }
  261. /**
  262. * ds1685_rtc_set_time - sets the time registers.
  263. * @dev: pointer to device structure.
  264. * @tm: pointer to rtc_time structure.
  265. */
  266. static int
  267. ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
  268. {
  269. struct platform_device *pdev = to_platform_device(dev);
  270. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  271. u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
  272. /* Fetch the time info from rtc_time. */
  273. seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
  274. RTC_SECS_BCD_MASK);
  275. minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
  276. RTC_MINS_BCD_MASK);
  277. hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
  278. RTC_HRS_24_BCD_MASK);
  279. wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
  280. RTC_WDAY_MASK);
  281. mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
  282. RTC_MDAY_BCD_MASK);
  283. month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
  284. RTC_MONTH_BCD_MASK);
  285. years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
  286. RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
  287. century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
  288. RTC_CENTURY_MASK, RTC_CENTURY_MASK);
  289. /*
  290. * Perform Sanity Checks:
  291. * - Months: !> 12, Month Day != 0.
  292. * - Month Day !> Max days in current month.
  293. * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
  294. */
  295. if ((tm->tm_mon > 11) || (mday == 0))
  296. return -EDOM;
  297. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
  298. return -EDOM;
  299. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
  300. (tm->tm_sec >= 60) || (wday > 7))
  301. return -EDOM;
  302. /*
  303. * Set the data mode to use and store the time values in the
  304. * RTC registers.
  305. */
  306. ds1685_rtc_begin_data_access(rtc);
  307. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  308. if (rtc->bcd_mode)
  309. ctrlb &= ~(RTC_CTRL_B_DM);
  310. else
  311. ctrlb |= RTC_CTRL_B_DM;
  312. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  313. rtc->write(rtc, RTC_SECS, seconds);
  314. rtc->write(rtc, RTC_MINS, minutes);
  315. rtc->write(rtc, RTC_HRS, hours);
  316. rtc->write(rtc, RTC_WDAY, wday);
  317. rtc->write(rtc, RTC_MDAY, mday);
  318. rtc->write(rtc, RTC_MONTH, month);
  319. rtc->write(rtc, RTC_YEAR, years);
  320. rtc->write(rtc, RTC_CENTURY, century);
  321. ds1685_rtc_end_data_access(rtc);
  322. return 0;
  323. }
  324. /**
  325. * ds1685_rtc_read_alarm - reads the alarm registers.
  326. * @dev: pointer to device structure.
  327. * @alrm: pointer to rtc_wkalrm structure.
  328. *
  329. * There are three primary alarm registers: seconds, minutes, and hours.
  330. * A fourth alarm register for the month date is also available in bank1 for
  331. * kickstart/wakeup features. The DS1685/DS1687 manual states that a
  332. * "don't care" value ranging from 0xc0 to 0xff may be written into one or
  333. * more of the three alarm bytes to act as a wildcard value. The fourth
  334. * byte doesn't support a "don't care" value.
  335. */
  336. static int
  337. ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  338. {
  339. struct platform_device *pdev = to_platform_device(dev);
  340. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  341. u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
  342. /* Fetch the alarm info from the RTC alarm registers. */
  343. ds1685_rtc_begin_data_access(rtc);
  344. seconds = rtc->read(rtc, RTC_SECS_ALARM);
  345. minutes = rtc->read(rtc, RTC_MINS_ALARM);
  346. hours = rtc->read(rtc, RTC_HRS_ALARM);
  347. mday = rtc->read(rtc, RTC_MDAY_ALARM);
  348. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  349. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  350. ds1685_rtc_end_data_access(rtc);
  351. /* Check month date. */
  352. if (!(mday >= 1) && (mday <= 31))
  353. return -EDOM;
  354. /*
  355. * Check the three alarm bytes.
  356. *
  357. * The Linux RTC system doesn't support the "don't care" capability
  358. * of this RTC chip. We check for it anyways in case support is
  359. * added in the future.
  360. */
  361. if (unlikely(seconds >= 0xc0))
  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 = '\0';
  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: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\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: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\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[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5], ssn[6], ssn[7],
  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[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5], ssn[6], ssn[7]);
  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 snprintf(buf, 13, "%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 snprintf(buf, 13, "%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 snprintf(buf, 24, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  1028. ssn[0], ssn[1], ssn[2], ssn[3], ssn[4], ssn[5],
  1029. ssn[6], ssn[7]);
  1030. return 0;
  1031. }
  1032. static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
  1033. /**
  1034. * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
  1035. */
  1036. static struct attribute*
  1037. ds1685_rtc_sysfs_misc_attrs[] = {
  1038. &dev_attr_battery.attr,
  1039. &dev_attr_auxbatt.attr,
  1040. &dev_attr_serial.attr,
  1041. NULL,
  1042. };
  1043. /**
  1044. * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
  1045. */
  1046. static const struct attribute_group
  1047. ds1685_rtc_sysfs_misc_grp = {
  1048. .name = "misc",
  1049. .attrs = ds1685_rtc_sysfs_misc_attrs,
  1050. };
  1051. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1052. /**
  1053. * struct ds1685_rtc_ctrl_regs.
  1054. * @name: char pointer for the bit name.
  1055. * @reg: control register the bit is in.
  1056. * @bit: the bit's offset in the register.
  1057. */
  1058. struct ds1685_rtc_ctrl_regs {
  1059. const char *name;
  1060. const u8 reg;
  1061. const u8 bit;
  1062. };
  1063. /*
  1064. * Ctrl register bit lookup table.
  1065. */
  1066. static const struct ds1685_rtc_ctrl_regs
  1067. ds1685_ctrl_regs_table[] = {
  1068. { "uip", RTC_CTRL_A, RTC_CTRL_A_UIP },
  1069. { "dv2", RTC_CTRL_A, RTC_CTRL_A_DV2 },
  1070. { "dv1", RTC_CTRL_A, RTC_CTRL_A_DV1 },
  1071. { "dv0", RTC_CTRL_A, RTC_CTRL_A_DV0 },
  1072. { "rs3", RTC_CTRL_A, RTC_CTRL_A_RS3 },
  1073. { "rs2", RTC_CTRL_A, RTC_CTRL_A_RS2 },
  1074. { "rs1", RTC_CTRL_A, RTC_CTRL_A_RS1 },
  1075. { "rs0", RTC_CTRL_A, RTC_CTRL_A_RS0 },
  1076. { "set", RTC_CTRL_B, RTC_CTRL_B_SET },
  1077. { "pie", RTC_CTRL_B, RTC_CTRL_B_PIE },
  1078. { "aie", RTC_CTRL_B, RTC_CTRL_B_AIE },
  1079. { "uie", RTC_CTRL_B, RTC_CTRL_B_UIE },
  1080. { "sqwe", RTC_CTRL_B, RTC_CTRL_B_SQWE },
  1081. { "dm", RTC_CTRL_B, RTC_CTRL_B_DM },
  1082. { "2412", RTC_CTRL_B, RTC_CTRL_B_2412 },
  1083. { "dse", RTC_CTRL_B, RTC_CTRL_B_DSE },
  1084. { "irqf", RTC_CTRL_C, RTC_CTRL_C_IRQF },
  1085. { "pf", RTC_CTRL_C, RTC_CTRL_C_PF },
  1086. { "af", RTC_CTRL_C, RTC_CTRL_C_AF },
  1087. { "uf", RTC_CTRL_C, RTC_CTRL_C_UF },
  1088. { "vrt", RTC_CTRL_D, RTC_CTRL_D_VRT },
  1089. { "vrt2", RTC_EXT_CTRL_4A, RTC_CTRL_4A_VRT2 },
  1090. { "incr", RTC_EXT_CTRL_4A, RTC_CTRL_4A_INCR },
  1091. { "pab", RTC_EXT_CTRL_4A, RTC_CTRL_4A_PAB },
  1092. { "rf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_RF },
  1093. { "wf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_WF },
  1094. { "kf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_KF },
  1095. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1096. { "bme", RTC_EXT_CTRL_4A, RTC_CTRL_4A_BME },
  1097. #endif
  1098. { "abe", RTC_EXT_CTRL_4B, RTC_CTRL_4B_ABE },
  1099. { "e32k", RTC_EXT_CTRL_4B, RTC_CTRL_4B_E32K },
  1100. { "cs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_CS },
  1101. { "rce", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RCE },
  1102. { "prs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_PRS },
  1103. { "rie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RIE },
  1104. { "wie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_WIE },
  1105. { "kse", RTC_EXT_CTRL_4B, RTC_CTRL_4B_KSE },
  1106. { NULL, 0, 0 },
  1107. };
  1108. /**
  1109. * ds1685_rtc_sysfs_ctrl_regs_lookup - ctrl register bit lookup function.
  1110. * @name: ctrl register bit to look up in ds1685_ctrl_regs_table.
  1111. */
  1112. static const struct ds1685_rtc_ctrl_regs*
  1113. ds1685_rtc_sysfs_ctrl_regs_lookup(const char *name)
  1114. {
  1115. const struct ds1685_rtc_ctrl_regs *p = ds1685_ctrl_regs_table;
  1116. for (; p->name != NULL; ++p)
  1117. if (strcmp(p->name, name) == 0)
  1118. return p;
  1119. return NULL;
  1120. }
  1121. /**
  1122. * ds1685_rtc_sysfs_ctrl_regs_show - reads a ctrl register bit via sysfs.
  1123. * @dev: pointer to device structure.
  1124. * @attr: pointer to device_attribute structure.
  1125. * @buf: pointer to char array to hold the output.
  1126. */
  1127. static ssize_t
  1128. ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev,
  1129. struct device_attribute *attr, char *buf)
  1130. {
  1131. u8 tmp;
  1132. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1133. const struct ds1685_rtc_ctrl_regs *reg_info =
  1134. ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
  1135. /* Make sure we actually matched something. */
  1136. if (!reg_info)
  1137. return -EINVAL;
  1138. /* No spinlock during a read -- mutex is already held. */
  1139. ds1685_rtc_switch_to_bank1(rtc);
  1140. tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit;
  1141. ds1685_rtc_switch_to_bank0(rtc);
  1142. return snprintf(buf, 2, "%d\n", (tmp ? 1 : 0));
  1143. }
  1144. /**
  1145. * ds1685_rtc_sysfs_ctrl_regs_store - writes a ctrl register bit via sysfs.
  1146. * @dev: pointer to device structure.
  1147. * @attr: pointer to device_attribute structure.
  1148. * @buf: pointer to char array to hold the output.
  1149. * @count: number of bytes written.
  1150. */
  1151. static ssize_t
  1152. ds1685_rtc_sysfs_ctrl_regs_store(struct device *dev,
  1153. struct device_attribute *attr,
  1154. const char *buf, size_t count)
  1155. {
  1156. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1157. u8 reg = 0, bit = 0, tmp;
  1158. unsigned long flags = 0;
  1159. long int val = 0;
  1160. const struct ds1685_rtc_ctrl_regs *reg_info =
  1161. ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
  1162. /* We only accept numbers. */
  1163. if (kstrtol(buf, 10, &val) < 0)
  1164. return -EINVAL;
  1165. /* bits are binary, 0 or 1 only. */
  1166. if ((val != 0) && (val != 1))
  1167. return -ERANGE;
  1168. /* Make sure we actually matched something. */
  1169. if (!reg_info)
  1170. return -EINVAL;
  1171. reg = reg_info->reg;
  1172. bit = reg_info->bit;
  1173. /* Safe to spinlock during a write. */
  1174. ds1685_rtc_begin_ctrl_access(rtc, flags);
  1175. tmp = rtc->read(rtc, reg);
  1176. rtc->write(rtc, reg, (val ? (tmp | bit) : (tmp & ~(bit))));
  1177. ds1685_rtc_end_ctrl_access(rtc, flags);
  1178. return count;
  1179. }
  1180. /**
  1181. * DS1685_RTC_SYSFS_CTRL_REG_RO - device_attribute for read-only register bit.
  1182. * @bit: bit to read.
  1183. */
  1184. #define DS1685_RTC_SYSFS_CTRL_REG_RO(bit) \
  1185. static DEVICE_ATTR(bit, S_IRUGO, \
  1186. ds1685_rtc_sysfs_ctrl_regs_show, NULL)
  1187. /**
  1188. * DS1685_RTC_SYSFS_CTRL_REG_RW - device_attribute for read-write register bit.
  1189. * @bit: bit to read or write.
  1190. */
  1191. #define DS1685_RTC_SYSFS_CTRL_REG_RW(bit) \
  1192. static DEVICE_ATTR(bit, S_IRUGO | S_IWUSR, \
  1193. ds1685_rtc_sysfs_ctrl_regs_show, \
  1194. ds1685_rtc_sysfs_ctrl_regs_store)
  1195. /*
  1196. * Control Register A bits.
  1197. */
  1198. DS1685_RTC_SYSFS_CTRL_REG_RO(uip);
  1199. DS1685_RTC_SYSFS_CTRL_REG_RW(dv2);
  1200. DS1685_RTC_SYSFS_CTRL_REG_RW(dv1);
  1201. DS1685_RTC_SYSFS_CTRL_REG_RO(dv0);
  1202. DS1685_RTC_SYSFS_CTRL_REG_RW(rs3);
  1203. DS1685_RTC_SYSFS_CTRL_REG_RW(rs2);
  1204. DS1685_RTC_SYSFS_CTRL_REG_RW(rs1);
  1205. DS1685_RTC_SYSFS_CTRL_REG_RW(rs0);
  1206. static struct attribute*
  1207. ds1685_rtc_sysfs_ctrla_attrs[] = {
  1208. &dev_attr_uip.attr,
  1209. &dev_attr_dv2.attr,
  1210. &dev_attr_dv1.attr,
  1211. &dev_attr_dv0.attr,
  1212. &dev_attr_rs3.attr,
  1213. &dev_attr_rs2.attr,
  1214. &dev_attr_rs1.attr,
  1215. &dev_attr_rs0.attr,
  1216. NULL,
  1217. };
  1218. static const struct attribute_group
  1219. ds1685_rtc_sysfs_ctrla_grp = {
  1220. .name = "ctrla",
  1221. .attrs = ds1685_rtc_sysfs_ctrla_attrs,
  1222. };
  1223. /*
  1224. * Control Register B bits.
  1225. */
  1226. DS1685_RTC_SYSFS_CTRL_REG_RO(set);
  1227. DS1685_RTC_SYSFS_CTRL_REG_RW(pie);
  1228. DS1685_RTC_SYSFS_CTRL_REG_RW(aie);
  1229. DS1685_RTC_SYSFS_CTRL_REG_RW(uie);
  1230. DS1685_RTC_SYSFS_CTRL_REG_RW(sqwe);
  1231. DS1685_RTC_SYSFS_CTRL_REG_RO(dm);
  1232. DS1685_RTC_SYSFS_CTRL_REG_RO(2412);
  1233. DS1685_RTC_SYSFS_CTRL_REG_RO(dse);
  1234. static struct attribute*
  1235. ds1685_rtc_sysfs_ctrlb_attrs[] = {
  1236. &dev_attr_set.attr,
  1237. &dev_attr_pie.attr,
  1238. &dev_attr_aie.attr,
  1239. &dev_attr_uie.attr,
  1240. &dev_attr_sqwe.attr,
  1241. &dev_attr_dm.attr,
  1242. &dev_attr_2412.attr,
  1243. &dev_attr_dse.attr,
  1244. NULL,
  1245. };
  1246. static const struct attribute_group
  1247. ds1685_rtc_sysfs_ctrlb_grp = {
  1248. .name = "ctrlb",
  1249. .attrs = ds1685_rtc_sysfs_ctrlb_attrs,
  1250. };
  1251. /*
  1252. * Control Register C bits.
  1253. *
  1254. * Reading Control C clears these bits! Reading them individually can
  1255. * possibly cause an interrupt to be missed. Use the /proc interface
  1256. * to see all the bits in this register simultaneously.
  1257. */
  1258. DS1685_RTC_SYSFS_CTRL_REG_RO(irqf);
  1259. DS1685_RTC_SYSFS_CTRL_REG_RO(pf);
  1260. DS1685_RTC_SYSFS_CTRL_REG_RO(af);
  1261. DS1685_RTC_SYSFS_CTRL_REG_RO(uf);
  1262. static struct attribute*
  1263. ds1685_rtc_sysfs_ctrlc_attrs[] = {
  1264. &dev_attr_irqf.attr,
  1265. &dev_attr_pf.attr,
  1266. &dev_attr_af.attr,
  1267. &dev_attr_uf.attr,
  1268. NULL,
  1269. };
  1270. static const struct attribute_group
  1271. ds1685_rtc_sysfs_ctrlc_grp = {
  1272. .name = "ctrlc",
  1273. .attrs = ds1685_rtc_sysfs_ctrlc_attrs,
  1274. };
  1275. /*
  1276. * Control Register D bits.
  1277. */
  1278. DS1685_RTC_SYSFS_CTRL_REG_RO(vrt);
  1279. static struct attribute*
  1280. ds1685_rtc_sysfs_ctrld_attrs[] = {
  1281. &dev_attr_vrt.attr,
  1282. NULL,
  1283. };
  1284. static const struct attribute_group
  1285. ds1685_rtc_sysfs_ctrld_grp = {
  1286. .name = "ctrld",
  1287. .attrs = ds1685_rtc_sysfs_ctrld_attrs,
  1288. };
  1289. /*
  1290. * Control Register 4A bits.
  1291. */
  1292. DS1685_RTC_SYSFS_CTRL_REG_RO(vrt2);
  1293. DS1685_RTC_SYSFS_CTRL_REG_RO(incr);
  1294. DS1685_RTC_SYSFS_CTRL_REG_RW(pab);
  1295. DS1685_RTC_SYSFS_CTRL_REG_RW(rf);
  1296. DS1685_RTC_SYSFS_CTRL_REG_RW(wf);
  1297. DS1685_RTC_SYSFS_CTRL_REG_RW(kf);
  1298. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1299. DS1685_RTC_SYSFS_CTRL_REG_RO(bme);
  1300. #endif
  1301. static struct attribute*
  1302. ds1685_rtc_sysfs_ctrl4a_attrs[] = {
  1303. &dev_attr_vrt2.attr,
  1304. &dev_attr_incr.attr,
  1305. &dev_attr_pab.attr,
  1306. &dev_attr_rf.attr,
  1307. &dev_attr_wf.attr,
  1308. &dev_attr_kf.attr,
  1309. #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
  1310. &dev_attr_bme.attr,
  1311. #endif
  1312. NULL,
  1313. };
  1314. static const struct attribute_group
  1315. ds1685_rtc_sysfs_ctrl4a_grp = {
  1316. .name = "ctrl4a",
  1317. .attrs = ds1685_rtc_sysfs_ctrl4a_attrs,
  1318. };
  1319. /*
  1320. * Control Register 4B bits.
  1321. */
  1322. DS1685_RTC_SYSFS_CTRL_REG_RW(abe);
  1323. DS1685_RTC_SYSFS_CTRL_REG_RW(e32k);
  1324. DS1685_RTC_SYSFS_CTRL_REG_RO(cs);
  1325. DS1685_RTC_SYSFS_CTRL_REG_RW(rce);
  1326. DS1685_RTC_SYSFS_CTRL_REG_RW(prs);
  1327. DS1685_RTC_SYSFS_CTRL_REG_RW(rie);
  1328. DS1685_RTC_SYSFS_CTRL_REG_RW(wie);
  1329. DS1685_RTC_SYSFS_CTRL_REG_RW(kse);
  1330. static struct attribute*
  1331. ds1685_rtc_sysfs_ctrl4b_attrs[] = {
  1332. &dev_attr_abe.attr,
  1333. &dev_attr_e32k.attr,
  1334. &dev_attr_cs.attr,
  1335. &dev_attr_rce.attr,
  1336. &dev_attr_prs.attr,
  1337. &dev_attr_rie.attr,
  1338. &dev_attr_wie.attr,
  1339. &dev_attr_kse.attr,
  1340. NULL,
  1341. };
  1342. static const struct attribute_group
  1343. ds1685_rtc_sysfs_ctrl4b_grp = {
  1344. .name = "ctrl4b",
  1345. .attrs = ds1685_rtc_sysfs_ctrl4b_attrs,
  1346. };
  1347. /**
  1348. * struct ds1685_rtc_ctrl_regs.
  1349. * @name: char pointer for the bit name.
  1350. * @reg: control register the bit is in.
  1351. * @bit: the bit's offset in the register.
  1352. */
  1353. struct ds1685_rtc_time_regs {
  1354. const char *name;
  1355. const u8 reg;
  1356. const u8 mask;
  1357. const u8 min;
  1358. const u8 max;
  1359. };
  1360. /*
  1361. * Time/Date register lookup tables.
  1362. */
  1363. static const struct ds1685_rtc_time_regs
  1364. ds1685_time_regs_bcd_table[] = {
  1365. { "seconds", RTC_SECS, RTC_SECS_BCD_MASK, 0, 59 },
  1366. { "minutes", RTC_MINS, RTC_MINS_BCD_MASK, 0, 59 },
  1367. { "hours", RTC_HRS, RTC_HRS_24_BCD_MASK, 0, 23 },
  1368. { "wday", RTC_WDAY, RTC_WDAY_MASK, 1, 7 },
  1369. { "mday", RTC_MDAY, RTC_MDAY_BCD_MASK, 1, 31 },
  1370. { "month", RTC_MONTH, RTC_MONTH_BCD_MASK, 1, 12 },
  1371. { "year", RTC_YEAR, RTC_YEAR_BCD_MASK, 0, 99 },
  1372. { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0, 99 },
  1373. { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BCD_MASK, 0, 59 },
  1374. { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BCD_MASK, 0, 59 },
  1375. { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BCD_MASK, 0, 23 },
  1376. { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 1, 31 },
  1377. { NULL, 0, 0, 0, 0 },
  1378. };
  1379. static const struct ds1685_rtc_time_regs
  1380. ds1685_time_regs_bin_table[] = {
  1381. { "seconds", RTC_SECS, RTC_SECS_BIN_MASK, 0x00, 0x3b },
  1382. { "minutes", RTC_MINS, RTC_MINS_BIN_MASK, 0x00, 0x3b },
  1383. { "hours", RTC_HRS, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
  1384. { "wday", RTC_WDAY, RTC_WDAY_MASK, 0x01, 0x07 },
  1385. { "mday", RTC_MDAY, RTC_MDAY_BIN_MASK, 0x01, 0x1f },
  1386. { "month", RTC_MONTH, RTC_MONTH_BIN_MASK, 0x01, 0x0c },
  1387. { "year", RTC_YEAR, RTC_YEAR_BIN_MASK, 0x00, 0x63 },
  1388. { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0x00, 0x63 },
  1389. { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BIN_MASK, 0x00, 0x3b },
  1390. { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BIN_MASK, 0x00, 0x3b },
  1391. { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
  1392. { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 0x01, 0x1f },
  1393. { NULL, 0, 0, 0x00, 0x00 },
  1394. };
  1395. /**
  1396. * ds1685_rtc_sysfs_time_regs_bcd_lookup - time/date reg bit lookup function.
  1397. * @name: register bit to look up in ds1685_time_regs_bcd_table.
  1398. */
  1399. static const struct ds1685_rtc_time_regs*
  1400. ds1685_rtc_sysfs_time_regs_lookup(const char *name, bool bcd_mode)
  1401. {
  1402. const struct ds1685_rtc_time_regs *p;
  1403. if (bcd_mode)
  1404. p = ds1685_time_regs_bcd_table;
  1405. else
  1406. p = ds1685_time_regs_bin_table;
  1407. for (; p->name != NULL; ++p)
  1408. if (strcmp(p->name, name) == 0)
  1409. return p;
  1410. return NULL;
  1411. }
  1412. /**
  1413. * ds1685_rtc_sysfs_time_regs_show - reads a time/date register via sysfs.
  1414. * @dev: pointer to device structure.
  1415. * @attr: pointer to device_attribute structure.
  1416. * @buf: pointer to char array to hold the output.
  1417. */
  1418. static ssize_t
  1419. ds1685_rtc_sysfs_time_regs_show(struct device *dev,
  1420. struct device_attribute *attr, char *buf)
  1421. {
  1422. u8 tmp;
  1423. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1424. const struct ds1685_rtc_time_regs *bcd_reg_info =
  1425. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
  1426. const struct ds1685_rtc_time_regs *bin_reg_info =
  1427. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
  1428. /* Make sure we actually matched something. */
  1429. if (!bcd_reg_info || !bin_reg_info)
  1430. return -EINVAL;
  1431. /* bcd_reg_info->reg == bin_reg_info->reg. */
  1432. ds1685_rtc_begin_data_access(rtc);
  1433. tmp = rtc->read(rtc, bcd_reg_info->reg);
  1434. ds1685_rtc_end_data_access(rtc);
  1435. tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask,
  1436. bin_reg_info->mask);
  1437. return snprintf(buf, 4, "%d\n", tmp);
  1438. }
  1439. /**
  1440. * ds1685_rtc_sysfs_time_regs_store - writes a time/date register via sysfs.
  1441. * @dev: pointer to device structure.
  1442. * @attr: pointer to device_attribute structure.
  1443. * @buf: pointer to char array to hold the output.
  1444. * @count: number of bytes written.
  1445. */
  1446. static ssize_t
  1447. ds1685_rtc_sysfs_time_regs_store(struct device *dev,
  1448. struct device_attribute *attr,
  1449. const char *buf, size_t count)
  1450. {
  1451. long int val = 0;
  1452. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  1453. const struct ds1685_rtc_time_regs *bcd_reg_info =
  1454. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
  1455. const struct ds1685_rtc_time_regs *bin_reg_info =
  1456. ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
  1457. /* We only accept numbers. */
  1458. if (kstrtol(buf, 10, &val) < 0)
  1459. return -EINVAL;
  1460. /* Make sure we actually matched something. */
  1461. if (!bcd_reg_info || !bin_reg_info)
  1462. return -EINVAL;
  1463. /* Check for a valid range. */
  1464. if (rtc->bcd_mode) {
  1465. if ((val < bcd_reg_info->min) || (val > bcd_reg_info->max))
  1466. return -ERANGE;
  1467. } else {
  1468. if ((val < bin_reg_info->min) || (val > bin_reg_info->max))
  1469. return -ERANGE;
  1470. }
  1471. val = ds1685_rtc_bin2bcd(rtc, val, bin_reg_info->mask,
  1472. bcd_reg_info->mask);
  1473. /* bcd_reg_info->reg == bin_reg_info->reg. */
  1474. ds1685_rtc_begin_data_access(rtc);
  1475. rtc->write(rtc, bcd_reg_info->reg, val);
  1476. ds1685_rtc_end_data_access(rtc);
  1477. return count;
  1478. }
  1479. /**
  1480. * DS1685_RTC_SYSFS_REG_RW - device_attribute for a read-write time register.
  1481. * @reg: time/date register to read or write.
  1482. */
  1483. #define DS1685_RTC_SYSFS_TIME_REG_RW(reg) \
  1484. static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, \
  1485. ds1685_rtc_sysfs_time_regs_show, \
  1486. ds1685_rtc_sysfs_time_regs_store)
  1487. /*
  1488. * Time/Date Register bits.
  1489. */
  1490. DS1685_RTC_SYSFS_TIME_REG_RW(seconds);
  1491. DS1685_RTC_SYSFS_TIME_REG_RW(minutes);
  1492. DS1685_RTC_SYSFS_TIME_REG_RW(hours);
  1493. DS1685_RTC_SYSFS_TIME_REG_RW(wday);
  1494. DS1685_RTC_SYSFS_TIME_REG_RW(mday);
  1495. DS1685_RTC_SYSFS_TIME_REG_RW(month);
  1496. DS1685_RTC_SYSFS_TIME_REG_RW(year);
  1497. DS1685_RTC_SYSFS_TIME_REG_RW(century);
  1498. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_seconds);
  1499. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_minutes);
  1500. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_hours);
  1501. DS1685_RTC_SYSFS_TIME_REG_RW(alarm_mday);
  1502. static struct attribute*
  1503. ds1685_rtc_sysfs_time_attrs[] = {
  1504. &dev_attr_seconds.attr,
  1505. &dev_attr_minutes.attr,
  1506. &dev_attr_hours.attr,
  1507. &dev_attr_wday.attr,
  1508. &dev_attr_mday.attr,
  1509. &dev_attr_month.attr,
  1510. &dev_attr_year.attr,
  1511. &dev_attr_century.attr,
  1512. NULL,
  1513. };
  1514. static const struct attribute_group
  1515. ds1685_rtc_sysfs_time_grp = {
  1516. .name = "datetime",
  1517. .attrs = ds1685_rtc_sysfs_time_attrs,
  1518. };
  1519. static struct attribute*
  1520. ds1685_rtc_sysfs_alarm_attrs[] = {
  1521. &dev_attr_alarm_seconds.attr,
  1522. &dev_attr_alarm_minutes.attr,
  1523. &dev_attr_alarm_hours.attr,
  1524. &dev_attr_alarm_mday.attr,
  1525. NULL,
  1526. };
  1527. static const struct attribute_group
  1528. ds1685_rtc_sysfs_alarm_grp = {
  1529. .name = "alarm",
  1530. .attrs = ds1685_rtc_sysfs_alarm_attrs,
  1531. };
  1532. #endif /* CONFIG_RTC_DS1685_SYSFS_REGS */
  1533. /**
  1534. * ds1685_rtc_sysfs_register - register sysfs files.
  1535. * @dev: pointer to device structure.
  1536. */
  1537. static int
  1538. ds1685_rtc_sysfs_register(struct device *dev)
  1539. {
  1540. int ret = 0;
  1541. sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr);
  1542. ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1543. if (ret)
  1544. return ret;
  1545. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1546. if (ret)
  1547. return ret;
  1548. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1549. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
  1550. if (ret)
  1551. return ret;
  1552. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
  1553. if (ret)
  1554. return ret;
  1555. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
  1556. if (ret)
  1557. return ret;
  1558. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
  1559. if (ret)
  1560. return ret;
  1561. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
  1562. if (ret)
  1563. return ret;
  1564. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
  1565. if (ret)
  1566. return ret;
  1567. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
  1568. if (ret)
  1569. return ret;
  1570. ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
  1571. if (ret)
  1572. return ret;
  1573. #endif
  1574. return 0;
  1575. }
  1576. /**
  1577. * ds1685_rtc_sysfs_unregister - unregister sysfs files.
  1578. * @dev: pointer to device structure.
  1579. */
  1580. static int
  1581. ds1685_rtc_sysfs_unregister(struct device *dev)
  1582. {
  1583. sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
  1584. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
  1585. #ifdef CONFIG_RTC_DS1685_SYSFS_REGS
  1586. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
  1587. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
  1588. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
  1589. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
  1590. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
  1591. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
  1592. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
  1593. sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
  1594. #endif
  1595. return 0;
  1596. }
  1597. #endif /* CONFIG_SYSFS */
  1598. /* ----------------------------------------------------------------------- */
  1599. /* Driver Probe/Removal */
  1600. /**
  1601. * ds1685_rtc_probe - initializes rtc driver.
  1602. * @pdev: pointer to platform_device structure.
  1603. */
  1604. static int
  1605. ds1685_rtc_probe(struct platform_device *pdev)
  1606. {
  1607. struct rtc_device *rtc_dev;
  1608. struct resource *res;
  1609. struct ds1685_priv *rtc;
  1610. struct ds1685_rtc_platform_data *pdata;
  1611. u8 ctrla, ctrlb, hours;
  1612. unsigned char am_pm;
  1613. int ret = 0;
  1614. /* Get the platform data. */
  1615. pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
  1616. if (!pdata)
  1617. return -ENODEV;
  1618. /* Allocate memory for the rtc device. */
  1619. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  1620. if (!rtc)
  1621. return -ENOMEM;
  1622. /*
  1623. * Allocate/setup any IORESOURCE_MEM resources, if required. Not all
  1624. * platforms put the RTC in an easy-access place. Like the SGI Octane,
  1625. * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip
  1626. * that sits behind the IOC3 PCI metadevice.
  1627. */
  1628. if (pdata->alloc_io_resources) {
  1629. /* Get the platform resources. */
  1630. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1631. if (!res)
  1632. return -ENXIO;
  1633. rtc->size = resource_size(res);
  1634. /* Request a memory region. */
  1635. /* XXX: mmio-only for now. */
  1636. if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
  1637. pdev->name))
  1638. return -EBUSY;
  1639. /*
  1640. * Set the base address for the rtc, and ioremap its
  1641. * registers.
  1642. */
  1643. rtc->baseaddr = res->start;
  1644. rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
  1645. if (!rtc->regs)
  1646. return -ENOMEM;
  1647. }
  1648. rtc->alloc_io_resources = pdata->alloc_io_resources;
  1649. /* Get the register step size. */
  1650. if (pdata->regstep > 0)
  1651. rtc->regstep = pdata->regstep;
  1652. else
  1653. rtc->regstep = 1;
  1654. /* Platform read function, else default if mmio setup */
  1655. if (pdata->plat_read)
  1656. rtc->read = pdata->plat_read;
  1657. else
  1658. if (pdata->alloc_io_resources)
  1659. rtc->read = ds1685_read;
  1660. else
  1661. return -ENXIO;
  1662. /* Platform write function, else default if mmio setup */
  1663. if (pdata->plat_write)
  1664. rtc->write = pdata->plat_write;
  1665. else
  1666. if (pdata->alloc_io_resources)
  1667. rtc->write = ds1685_write;
  1668. else
  1669. return -ENXIO;
  1670. /* Platform pre-shutdown function, if defined. */
  1671. if (pdata->plat_prepare_poweroff)
  1672. rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
  1673. /* Platform wake_alarm function, if defined. */
  1674. if (pdata->plat_wake_alarm)
  1675. rtc->wake_alarm = pdata->plat_wake_alarm;
  1676. /* Platform post_ram_clear function, if defined. */
  1677. if (pdata->plat_post_ram_clear)
  1678. rtc->post_ram_clear = pdata->plat_post_ram_clear;
  1679. /* Init the spinlock, workqueue, & set the driver data. */
  1680. spin_lock_init(&rtc->lock);
  1681. INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
  1682. platform_set_drvdata(pdev, rtc);
  1683. /* Turn the oscillator on if is not already on (DV1 = 1). */
  1684. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1685. if (!(ctrla & RTC_CTRL_A_DV1))
  1686. ctrla |= RTC_CTRL_A_DV1;
  1687. /* Enable the countdown chain (DV2 = 0) */
  1688. ctrla &= ~(RTC_CTRL_A_DV2);
  1689. /* Clear RS3-RS0 in Control A. */
  1690. ctrla &= ~(RTC_CTRL_A_RS_MASK);
  1691. /*
  1692. * All done with Control A. Switch to Bank 1 for the remainder of
  1693. * the RTC setup so we have access to the extended functions.
  1694. */
  1695. ctrla |= RTC_CTRL_A_DV0;
  1696. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1697. /* Default to 32768kHz output. */
  1698. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1699. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
  1700. /* Set the SET bit in Control B so we can do some housekeeping. */
  1701. rtc->write(rtc, RTC_CTRL_B,
  1702. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  1703. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  1704. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  1705. cpu_relax();
  1706. /*
  1707. * If the platform supports BCD mode, then set DM=0 in Control B.
  1708. * Otherwise, set DM=1 for BIN mode.
  1709. */
  1710. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  1711. if (pdata->bcd_mode)
  1712. ctrlb &= ~(RTC_CTRL_B_DM);
  1713. else
  1714. ctrlb |= RTC_CTRL_B_DM;
  1715. rtc->bcd_mode = pdata->bcd_mode;
  1716. /*
  1717. * Disable Daylight Savings Time (DSE = 0).
  1718. * The RTC has hardcoded timezone information that is rendered
  1719. * obselete. We'll let the OS deal with DST settings instead.
  1720. */
  1721. if (ctrlb & RTC_CTRL_B_DSE)
  1722. ctrlb &= ~(RTC_CTRL_B_DSE);
  1723. /* Force 24-hour mode (2412 = 1). */
  1724. if (!(ctrlb & RTC_CTRL_B_2412)) {
  1725. /* Reinitialize the time hours. */
  1726. hours = rtc->read(rtc, RTC_HRS);
  1727. am_pm = hours & RTC_HRS_AMPM_MASK;
  1728. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1729. RTC_HRS_12_BIN_MASK);
  1730. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1731. /* Enable 24-hour mode. */
  1732. ctrlb |= RTC_CTRL_B_2412;
  1733. /* Write back to Control B, including DM & DSE bits. */
  1734. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1735. /* Write the time hours back. */
  1736. rtc->write(rtc, RTC_HRS,
  1737. ds1685_rtc_bin2bcd(rtc, hours,
  1738. RTC_HRS_24_BIN_MASK,
  1739. RTC_HRS_24_BCD_MASK));
  1740. /* Reinitialize the alarm hours. */
  1741. hours = rtc->read(rtc, RTC_HRS_ALARM);
  1742. am_pm = hours & RTC_HRS_AMPM_MASK;
  1743. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1744. RTC_HRS_12_BIN_MASK);
  1745. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1746. /* Write the alarm hours back. */
  1747. rtc->write(rtc, RTC_HRS_ALARM,
  1748. ds1685_rtc_bin2bcd(rtc, hours,
  1749. RTC_HRS_24_BIN_MASK,
  1750. RTC_HRS_24_BCD_MASK));
  1751. } else {
  1752. /* 24-hour mode is already set, so write Control B back. */
  1753. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1754. }
  1755. /* Unset the SET bit in Control B so the RTC can update. */
  1756. rtc->write(rtc, RTC_CTRL_B,
  1757. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  1758. /* Check the main battery. */
  1759. if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
  1760. dev_warn(&pdev->dev,
  1761. "Main battery is exhausted! RTC may be invalid!\n");
  1762. /* Check the auxillary battery. It is optional. */
  1763. if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
  1764. dev_warn(&pdev->dev,
  1765. "Aux battery is exhausted or not available.\n");
  1766. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1767. rtc->write(rtc, RTC_CTRL_B,
  1768. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
  1769. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1770. rtc->read(rtc, RTC_CTRL_C);
  1771. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1772. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1773. (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
  1774. /* Clear RF/WF/KF in Ctrl 4A. */
  1775. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1776. (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
  1777. /*
  1778. * Re-enable KSE to handle power button events. We do not enable
  1779. * WIE or RIE by default.
  1780. */
  1781. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1782. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
  1783. /*
  1784. * Fetch the IRQ and setup the interrupt handler.
  1785. *
  1786. * Not all platforms have the IRQF pin tied to something. If not, the
  1787. * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
  1788. * there won't be an automatic way of notifying the kernel about it,
  1789. * unless ctrlc is explicitly polled.
  1790. */
  1791. if (!pdata->no_irq) {
  1792. ret = platform_get_irq(pdev, 0);
  1793. if (ret > 0) {
  1794. rtc->irq_num = ret;
  1795. /* Request an IRQ. */
  1796. ret = devm_request_irq(&pdev->dev, rtc->irq_num,
  1797. ds1685_rtc_irq_handler,
  1798. IRQF_SHARED, pdev->name, pdev);
  1799. /* Check to see if something came back. */
  1800. if (unlikely(ret)) {
  1801. dev_warn(&pdev->dev,
  1802. "RTC interrupt not available\n");
  1803. rtc->irq_num = 0;
  1804. }
  1805. } else
  1806. return ret;
  1807. }
  1808. rtc->no_irq = pdata->no_irq;
  1809. /* Setup complete. */
  1810. ds1685_rtc_switch_to_bank0(rtc);
  1811. /* Register the device as an RTC. */
  1812. rtc_dev = rtc_device_register(pdev->name, &pdev->dev,
  1813. &ds1685_rtc_ops, THIS_MODULE);
  1814. /* Success? */
  1815. if (IS_ERR(rtc_dev))
  1816. return PTR_ERR(rtc_dev);
  1817. /* Maximum periodic rate is 8192Hz (0.122070ms). */
  1818. rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
  1819. /* See if the platform doesn't support UIE. */
  1820. if (pdata->uie_unsupported)
  1821. rtc_dev->uie_unsupported = 1;
  1822. rtc->uie_unsupported = pdata->uie_unsupported;
  1823. rtc->dev = rtc_dev;
  1824. #ifdef CONFIG_SYSFS
  1825. ret = ds1685_rtc_sysfs_register(&pdev->dev);
  1826. if (ret)
  1827. rtc_device_unregister(rtc->dev);
  1828. #endif
  1829. /* Done! */
  1830. return ret;
  1831. }
  1832. /**
  1833. * ds1685_rtc_remove - removes rtc driver.
  1834. * @pdev: pointer to platform_device structure.
  1835. */
  1836. static int
  1837. ds1685_rtc_remove(struct platform_device *pdev)
  1838. {
  1839. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1840. #ifdef CONFIG_SYSFS
  1841. ds1685_rtc_sysfs_unregister(&pdev->dev);
  1842. #endif
  1843. rtc_device_unregister(rtc->dev);
  1844. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1845. rtc->write(rtc, RTC_CTRL_B,
  1846. (rtc->read(rtc, RTC_CTRL_B) &
  1847. ~(RTC_CTRL_B_PAU_MASK)));
  1848. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1849. rtc->read(rtc, RTC_CTRL_C);
  1850. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1851. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1852. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  1853. ~(RTC_CTRL_4B_RWK_MASK)));
  1854. /* Manually clear RF/WF/KF in Ctrl 4A. */
  1855. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1856. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  1857. ~(RTC_CTRL_4A_RWK_MASK)));
  1858. cancel_work_sync(&rtc->work);
  1859. return 0;
  1860. }
  1861. /**
  1862. * ds1685_rtc_driver - rtc driver properties.
  1863. */
  1864. static struct platform_driver ds1685_rtc_driver = {
  1865. .driver = {
  1866. .name = "rtc-ds1685",
  1867. .owner = THIS_MODULE,
  1868. },
  1869. .probe = ds1685_rtc_probe,
  1870. .remove = ds1685_rtc_remove,
  1871. };
  1872. /**
  1873. * ds1685_rtc_init - rtc module init.
  1874. */
  1875. static int __init
  1876. ds1685_rtc_init(void)
  1877. {
  1878. return platform_driver_register(&ds1685_rtc_driver);
  1879. }
  1880. /**
  1881. * ds1685_rtc_exit - rtc module exit.
  1882. */
  1883. static void __exit
  1884. ds1685_rtc_exit(void)
  1885. {
  1886. platform_driver_unregister(&ds1685_rtc_driver);
  1887. }
  1888. module_init(ds1685_rtc_init);
  1889. module_exit(ds1685_rtc_exit);
  1890. /* ----------------------------------------------------------------------- */
  1891. /* ----------------------------------------------------------------------- */
  1892. /* Poweroff function */
  1893. /**
  1894. * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
  1895. * @pdev: pointer to platform_device structure.
  1896. */
  1897. extern void __noreturn
  1898. ds1685_rtc_poweroff(struct platform_device *pdev)
  1899. {
  1900. u8 ctrla, ctrl4a, ctrl4b;
  1901. struct ds1685_priv *rtc;
  1902. /* Check for valid RTC data, else, spin forever. */
  1903. if (unlikely(!pdev)) {
  1904. pr_emerg("rtc-ds1685: platform device data not available, spinning forever ...\n");
  1905. unreachable();
  1906. } else {
  1907. /* Get the rtc data. */
  1908. rtc = platform_get_drvdata(pdev);
  1909. /*
  1910. * Disable our IRQ. We're powering down, so we're not
  1911. * going to worry about cleaning up. Most of that should
  1912. * have been taken care of by the shutdown scripts and this
  1913. * is the final function call.
  1914. */
  1915. if (!rtc->no_irq)
  1916. disable_irq_nosync(rtc->irq_num);
  1917. /* Oscillator must be on and the countdown chain enabled. */
  1918. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1919. ctrla |= RTC_CTRL_A_DV1;
  1920. ctrla &= ~(RTC_CTRL_A_DV2);
  1921. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1922. /*
  1923. * Read Control 4A and check the status of the auxillary
  1924. * battery. This must be present and working (VRT2 = 1)
  1925. * for wakeup and kickstart functionality to be useful.
  1926. */
  1927. ds1685_rtc_switch_to_bank1(rtc);
  1928. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1929. if (ctrl4a & RTC_CTRL_4A_VRT2) {
  1930. /* Clear all of the interrupt flags on Control 4A. */
  1931. ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
  1932. rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
  1933. /*
  1934. * The auxillary battery is present and working.
  1935. * Enable extended functions (ABE=1), enable
  1936. * wake-up (WIE=1), and enable kickstart (KSE=1)
  1937. * in Control 4B.
  1938. */
  1939. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  1940. ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
  1941. RTC_CTRL_4B_KSE);
  1942. rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
  1943. }
  1944. /* Set PAB to 1 in Control 4A to power the system down. */
  1945. dev_warn(&pdev->dev, "Powerdown.\n");
  1946. msleep(20);
  1947. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1948. (ctrl4a | RTC_CTRL_4A_PAB));
  1949. /* Spin ... we do not switch back to bank0. */
  1950. unreachable();
  1951. }
  1952. }
  1953. EXPORT_SYMBOL(ds1685_rtc_poweroff);
  1954. /* ----------------------------------------------------------------------- */
  1955. MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
  1956. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
  1957. MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
  1958. MODULE_LICENSE("GPL");
  1959. MODULE_VERSION(DRV_VERSION);
  1960. MODULE_ALIAS("platform:rtc-ds1685");