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