rtc-ds1685.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time
  4. * chips.
  5. *
  6. * Copyright (C) 2011-2014 Joshua Kinard <kumba@gentoo.org>.
  7. * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>.
  8. *
  9. * References:
  10. * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10.
  11. * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10.
  12. * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105.
  13. * Application Note 90, Using the Multiplex Bus RTC Extended Features.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/bcd.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rtc.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/rtc/ds1685.h>
  24. #ifdef CONFIG_PROC_FS
  25. #include <linux/proc_fs.h>
  26. #endif
  27. /* ----------------------------------------------------------------------- */
  28. /*
  29. * Standard read/write
  30. * all registers are mapped in CPU address space
  31. */
  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. * Indirect read/write functions
  58. * access happens via address and data register mapped in CPU address space
  59. */
  60. /**
  61. * ds1685_indirect_read - read a value from an rtc register.
  62. * @rtc: pointer to the ds1685 rtc structure.
  63. * @reg: the register address to read.
  64. */
  65. static u8
  66. ds1685_indirect_read(struct ds1685_priv *rtc, int reg)
  67. {
  68. writeb(reg, rtc->regs);
  69. return readb(rtc->data);
  70. }
  71. /**
  72. * ds1685_indirect_write - write a value to an rtc register.
  73. * @rtc: pointer to the ds1685 rtc structure.
  74. * @reg: the register address to write.
  75. * @value: value to write to the register.
  76. */
  77. static void
  78. ds1685_indirect_write(struct ds1685_priv *rtc, int reg, u8 value)
  79. {
  80. writeb(reg, rtc->regs);
  81. writeb(value, rtc->data);
  82. }
  83. /* ----------------------------------------------------------------------- */
  84. /* Inlined functions */
  85. /**
  86. * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD.
  87. * @rtc: pointer to the ds1685 rtc structure.
  88. * @val: u8 time value to consider converting.
  89. * @bcd_mask: u8 mask value if BCD mode is used.
  90. * @bin_mask: u8 mask value if BIN mode is used.
  91. *
  92. * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE.
  93. */
  94. static inline u8
  95. ds1685_rtc_bcd2bin(struct ds1685_priv *rtc, u8 val, u8 bcd_mask, u8 bin_mask)
  96. {
  97. if (rtc->bcd_mode)
  98. return (bcd2bin(val) & bcd_mask);
  99. return (val & bin_mask);
  100. }
  101. /**
  102. * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
  103. * @rtc: pointer to the ds1685 rtc structure.
  104. * @val: u8 time value to consider converting.
  105. * @bin_mask: u8 mask value if BIN mode is used.
  106. * @bcd_mask: u8 mask value if BCD mode is used.
  107. *
  108. * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE.
  109. */
  110. static inline u8
  111. ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask)
  112. {
  113. if (rtc->bcd_mode)
  114. return (bin2bcd(val) & bcd_mask);
  115. return (val & bin_mask);
  116. }
  117. /**
  118. * s1685_rtc_check_mday - check validity of the day of month.
  119. * @rtc: pointer to the ds1685 rtc structure.
  120. * @mday: day of month.
  121. *
  122. * Returns -EDOM if the day of month is not within 1..31 range.
  123. */
  124. static inline int
  125. ds1685_rtc_check_mday(struct ds1685_priv *rtc, u8 mday)
  126. {
  127. if (rtc->bcd_mode) {
  128. if (mday < 0x01 || mday > 0x31 || (mday & 0x0f) > 0x09)
  129. return -EDOM;
  130. } else {
  131. if (mday < 1 || mday > 31)
  132. return -EDOM;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
  138. * @rtc: pointer to the ds1685 rtc structure.
  139. */
  140. static inline void
  141. ds1685_rtc_switch_to_bank0(struct ds1685_priv *rtc)
  142. {
  143. rtc->write(rtc, RTC_CTRL_A,
  144. (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0)));
  145. }
  146. /**
  147. * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1.
  148. * @rtc: pointer to the ds1685 rtc structure.
  149. */
  150. static inline void
  151. ds1685_rtc_switch_to_bank1(struct ds1685_priv *rtc)
  152. {
  153. rtc->write(rtc, RTC_CTRL_A,
  154. (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0));
  155. }
  156. /**
  157. * ds1685_rtc_begin_data_access - prepare the rtc for data access.
  158. * @rtc: pointer to the ds1685 rtc structure.
  159. *
  160. * This takes several steps to prepare the rtc for access to get/set time
  161. * and alarm values from the rtc registers:
  162. * - Sets the SET bit in Control Register B.
  163. * - Reads Ext Control Register 4A and checks the INCR bit.
  164. * - If INCR is active, a short delay is added before Ext Control Register 4A
  165. * is read again in a loop until INCR is inactive.
  166. * - Switches the rtc to bank 1. This allows access to all relevant
  167. * data for normal rtc operation, as bank 0 contains only the nvram.
  168. */
  169. static inline void
  170. ds1685_rtc_begin_data_access(struct ds1685_priv *rtc)
  171. {
  172. /* Set the SET bit in Ctrl B */
  173. rtc->write(rtc, RTC_CTRL_B,
  174. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  175. /* Switch to Bank 1 */
  176. ds1685_rtc_switch_to_bank1(rtc);
  177. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  178. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  179. cpu_relax();
  180. }
  181. /**
  182. * ds1685_rtc_end_data_access - end data access on the rtc.
  183. * @rtc: pointer to the ds1685 rtc structure.
  184. *
  185. * This ends what was started by ds1685_rtc_begin_data_access:
  186. * - Switches the rtc back to bank 0.
  187. * - Clears the SET bit in Control Register B.
  188. */
  189. static inline void
  190. ds1685_rtc_end_data_access(struct ds1685_priv *rtc)
  191. {
  192. /* Switch back to Bank 0 */
  193. ds1685_rtc_switch_to_bank0(rtc);
  194. /* Clear the SET bit in Ctrl B */
  195. rtc->write(rtc, RTC_CTRL_B,
  196. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  197. }
  198. /**
  199. * ds1685_rtc_get_ssn - retrieve the silicon serial number.
  200. * @rtc: pointer to the ds1685 rtc structure.
  201. * @ssn: u8 array to hold the bits of the silicon serial number.
  202. *
  203. * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
  204. * first byte is the model number, the next six bytes are the serial number
  205. * digits, and the final byte is a CRC check byte. Together, they form the
  206. * silicon serial number.
  207. *
  208. * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be
  209. * called first before calling this function, else data will be read out of
  210. * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done.
  211. */
  212. static inline void
  213. ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn)
  214. {
  215. ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL);
  216. ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1);
  217. ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2);
  218. ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3);
  219. ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4);
  220. ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5);
  221. ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6);
  222. ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC);
  223. }
  224. /* ----------------------------------------------------------------------- */
  225. /* ----------------------------------------------------------------------- */
  226. /* Read/Set Time & Alarm functions */
  227. /**
  228. * ds1685_rtc_read_time - reads the time registers.
  229. * @dev: pointer to device structure.
  230. * @tm: pointer to rtc_time structure.
  231. */
  232. static int
  233. ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm)
  234. {
  235. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  236. u8 century;
  237. u8 seconds, minutes, hours, wday, mday, month, years;
  238. /* Fetch the time info from the RTC registers. */
  239. ds1685_rtc_begin_data_access(rtc);
  240. seconds = rtc->read(rtc, RTC_SECS);
  241. minutes = rtc->read(rtc, RTC_MINS);
  242. hours = rtc->read(rtc, RTC_HRS);
  243. wday = rtc->read(rtc, RTC_WDAY);
  244. mday = rtc->read(rtc, RTC_MDAY);
  245. month = rtc->read(rtc, RTC_MONTH);
  246. years = rtc->read(rtc, RTC_YEAR);
  247. century = rtc->read(rtc, RTC_CENTURY);
  248. ds1685_rtc_end_data_access(rtc);
  249. /* bcd2bin if needed, perform fixups, and store to rtc_time. */
  250. years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
  251. RTC_YEAR_BIN_MASK);
  252. century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
  253. RTC_CENTURY_MASK);
  254. tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
  255. RTC_SECS_BIN_MASK);
  256. tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
  257. RTC_MINS_BIN_MASK);
  258. tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
  259. RTC_HRS_24_BIN_MASK);
  260. tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
  261. RTC_WDAY_MASK) - 1);
  262. tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  263. RTC_MDAY_BIN_MASK);
  264. tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
  265. RTC_MONTH_BIN_MASK) - 1);
  266. tm->tm_year = ((years + (century * 100)) - 1900);
  267. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  268. tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
  269. return 0;
  270. }
  271. /**
  272. * ds1685_rtc_set_time - sets the time registers.
  273. * @dev: pointer to device structure.
  274. * @tm: pointer to rtc_time structure.
  275. */
  276. static int
  277. ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
  278. {
  279. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  280. u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
  281. /* Fetch the time info from rtc_time. */
  282. seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
  283. RTC_SECS_BCD_MASK);
  284. minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
  285. RTC_MINS_BCD_MASK);
  286. hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
  287. RTC_HRS_24_BCD_MASK);
  288. wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
  289. RTC_WDAY_MASK);
  290. mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
  291. RTC_MDAY_BCD_MASK);
  292. month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
  293. RTC_MONTH_BCD_MASK);
  294. years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
  295. RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
  296. century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
  297. RTC_CENTURY_MASK, RTC_CENTURY_MASK);
  298. /*
  299. * Perform Sanity Checks:
  300. * - Months: !> 12, Month Day != 0.
  301. * - Month Day !> Max days in current month.
  302. * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
  303. */
  304. if ((tm->tm_mon > 11) || (mday == 0))
  305. return -EDOM;
  306. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
  307. return -EDOM;
  308. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
  309. (tm->tm_sec >= 60) || (wday > 7))
  310. return -EDOM;
  311. /*
  312. * Set the data mode to use and store the time values in the
  313. * RTC registers.
  314. */
  315. ds1685_rtc_begin_data_access(rtc);
  316. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  317. if (rtc->bcd_mode)
  318. ctrlb &= ~(RTC_CTRL_B_DM);
  319. else
  320. ctrlb |= RTC_CTRL_B_DM;
  321. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  322. rtc->write(rtc, RTC_SECS, seconds);
  323. rtc->write(rtc, RTC_MINS, minutes);
  324. rtc->write(rtc, RTC_HRS, hours);
  325. rtc->write(rtc, RTC_WDAY, wday);
  326. rtc->write(rtc, RTC_MDAY, mday);
  327. rtc->write(rtc, RTC_MONTH, month);
  328. rtc->write(rtc, RTC_YEAR, years);
  329. rtc->write(rtc, RTC_CENTURY, century);
  330. ds1685_rtc_end_data_access(rtc);
  331. return 0;
  332. }
  333. /**
  334. * ds1685_rtc_read_alarm - reads the alarm registers.
  335. * @dev: pointer to device structure.
  336. * @alrm: pointer to rtc_wkalrm structure.
  337. *
  338. * There are three primary alarm registers: seconds, minutes, and hours.
  339. * A fourth alarm register for the month date is also available in bank1 for
  340. * kickstart/wakeup features. The DS1685/DS1687 manual states that a
  341. * "don't care" value ranging from 0xc0 to 0xff may be written into one or
  342. * more of the three alarm bytes to act as a wildcard value. The fourth
  343. * byte doesn't support a "don't care" value.
  344. */
  345. static int
  346. ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  347. {
  348. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  349. u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
  350. int ret;
  351. /* Fetch the alarm info from the RTC alarm registers. */
  352. ds1685_rtc_begin_data_access(rtc);
  353. seconds = rtc->read(rtc, RTC_SECS_ALARM);
  354. minutes = rtc->read(rtc, RTC_MINS_ALARM);
  355. hours = rtc->read(rtc, RTC_HRS_ALARM);
  356. mday = rtc->read(rtc, RTC_MDAY_ALARM);
  357. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  358. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  359. ds1685_rtc_end_data_access(rtc);
  360. /* Check the month date for validity. */
  361. ret = ds1685_rtc_check_mday(rtc, mday);
  362. if (ret)
  363. return ret;
  364. /*
  365. * Check the three alarm bytes.
  366. *
  367. * The Linux RTC system doesn't support the "don't care" capability
  368. * of this RTC chip. We check for it anyways in case support is
  369. * added in the future and only assign when we care.
  370. */
  371. if (likely(seconds < 0xc0))
  372. alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
  373. RTC_SECS_BCD_MASK,
  374. RTC_SECS_BIN_MASK);
  375. if (likely(minutes < 0xc0))
  376. alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
  377. RTC_MINS_BCD_MASK,
  378. RTC_MINS_BIN_MASK);
  379. if (likely(hours < 0xc0))
  380. alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
  381. RTC_HRS_24_BCD_MASK,
  382. RTC_HRS_24_BIN_MASK);
  383. /* Write the data to rtc_wkalrm. */
  384. alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  385. RTC_MDAY_BIN_MASK);
  386. alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
  387. alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
  388. return 0;
  389. }
  390. /**
  391. * ds1685_rtc_set_alarm - sets the alarm in registers.
  392. * @dev: pointer to device structure.
  393. * @alrm: pointer to rtc_wkalrm structure.
  394. */
  395. static int
  396. ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  397. {
  398. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  399. u8 ctrlb, seconds, minutes, hours, mday;
  400. int ret;
  401. /* Fetch the alarm info and convert to BCD. */
  402. seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
  403. RTC_SECS_BIN_MASK,
  404. RTC_SECS_BCD_MASK);
  405. minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
  406. RTC_MINS_BIN_MASK,
  407. RTC_MINS_BCD_MASK);
  408. hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
  409. RTC_HRS_24_BIN_MASK,
  410. RTC_HRS_24_BCD_MASK);
  411. mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
  412. RTC_MDAY_BIN_MASK,
  413. RTC_MDAY_BCD_MASK);
  414. /* Check the month date for validity. */
  415. ret = ds1685_rtc_check_mday(rtc, mday);
  416. if (ret)
  417. return ret;
  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. /* Flip the requisite interrupt-enable bit. */
  480. if (enabled)
  481. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
  482. RTC_CTRL_B_AIE));
  483. else
  484. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
  485. ~(RTC_CTRL_B_AIE)));
  486. /* Read Control C to clear all the flag bits. */
  487. rtc->read(rtc, RTC_CTRL_C);
  488. return 0;
  489. }
  490. /* ----------------------------------------------------------------------- */
  491. /* ----------------------------------------------------------------------- */
  492. /* IRQ handler */
  493. /**
  494. * ds1685_rtc_extended_irq - take care of extended interrupts
  495. * @rtc: pointer to the ds1685 rtc structure.
  496. * @pdev: platform device pointer.
  497. */
  498. static void
  499. ds1685_rtc_extended_irq(struct ds1685_priv *rtc, struct platform_device *pdev)
  500. {
  501. u8 ctrl4a, ctrl4b;
  502. ds1685_rtc_switch_to_bank1(rtc);
  503. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  504. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  505. /*
  506. * Check for a kickstart interrupt. With Vcc applied, this
  507. * typically means that the power button was pressed, so we
  508. * begin the shutdown sequence.
  509. */
  510. if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
  511. /* Briefly disable kickstarts to debounce button presses. */
  512. rtc->write(rtc, RTC_EXT_CTRL_4B,
  513. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  514. ~(RTC_CTRL_4B_KSE)));
  515. /* Clear the kickstart flag. */
  516. rtc->write(rtc, RTC_EXT_CTRL_4A,
  517. (ctrl4a & ~(RTC_CTRL_4A_KF)));
  518. /*
  519. * Sleep 500ms before re-enabling kickstarts. This allows
  520. * adequate time to avoid reading signal jitter as additional
  521. * button presses.
  522. */
  523. msleep(500);
  524. rtc->write(rtc, RTC_EXT_CTRL_4B,
  525. (rtc->read(rtc, RTC_EXT_CTRL_4B) |
  526. RTC_CTRL_4B_KSE));
  527. /* Call the platform pre-poweroff function. Else, shutdown. */
  528. if (rtc->prepare_poweroff != NULL)
  529. rtc->prepare_poweroff();
  530. else
  531. ds1685_rtc_poweroff(pdev);
  532. }
  533. /*
  534. * Check for a wake-up interrupt. With Vcc applied, this is
  535. * essentially a second alarm interrupt, except it takes into
  536. * account the 'date' register in bank1 in addition to the
  537. * standard three alarm registers.
  538. */
  539. if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
  540. rtc->write(rtc, RTC_EXT_CTRL_4A,
  541. (ctrl4a & ~(RTC_CTRL_4A_WF)));
  542. /* Call the platform wake_alarm function if defined. */
  543. if (rtc->wake_alarm != NULL)
  544. rtc->wake_alarm();
  545. else
  546. dev_warn(&pdev->dev,
  547. "Wake Alarm IRQ just occurred!\n");
  548. }
  549. /*
  550. * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0
  551. * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting
  552. * each byte to a logic 1. This has no effect on any extended
  553. * NV-SRAM that might be present, nor on the time/calendar/alarm
  554. * registers. After a ram-clear is completed, there is a minimum
  555. * recovery time of ~150ms in which all reads/writes are locked out.
  556. * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot
  557. * catch this scenario.
  558. */
  559. if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
  560. rtc->write(rtc, RTC_EXT_CTRL_4A,
  561. (ctrl4a & ~(RTC_CTRL_4A_RF)));
  562. msleep(150);
  563. /* Call the platform post_ram_clear function if defined. */
  564. if (rtc->post_ram_clear != NULL)
  565. rtc->post_ram_clear();
  566. else
  567. dev_warn(&pdev->dev,
  568. "RAM-Clear IRQ just occurred!\n");
  569. }
  570. ds1685_rtc_switch_to_bank0(rtc);
  571. }
  572. /**
  573. * ds1685_rtc_irq_handler - IRQ handler.
  574. * @irq: IRQ number.
  575. * @dev_id: platform device pointer.
  576. */
  577. static irqreturn_t
  578. ds1685_rtc_irq_handler(int irq, void *dev_id)
  579. {
  580. struct platform_device *pdev = dev_id;
  581. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  582. struct mutex *rtc_mutex;
  583. u8 ctrlb, ctrlc;
  584. unsigned long events = 0;
  585. u8 num_irqs = 0;
  586. /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
  587. if (unlikely(!rtc))
  588. return IRQ_HANDLED;
  589. rtc_mutex = &rtc->dev->ops_lock;
  590. mutex_lock(rtc_mutex);
  591. /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
  592. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  593. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  594. /* Is the IRQF bit set? */
  595. if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
  596. /*
  597. * We need to determine if it was one of the standard
  598. * events: PF, AF, or UF. If so, we handle them and
  599. * update the RTC core.
  600. */
  601. if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
  602. events = RTC_IRQF;
  603. /* Check for a periodic interrupt. */
  604. if ((ctrlb & RTC_CTRL_B_PIE) &&
  605. (ctrlc & RTC_CTRL_C_PF)) {
  606. events |= RTC_PF;
  607. num_irqs++;
  608. }
  609. /* Check for an alarm interrupt. */
  610. if ((ctrlb & RTC_CTRL_B_AIE) &&
  611. (ctrlc & RTC_CTRL_C_AF)) {
  612. events |= RTC_AF;
  613. num_irqs++;
  614. }
  615. /* Check for an update interrupt. */
  616. if ((ctrlb & RTC_CTRL_B_UIE) &&
  617. (ctrlc & RTC_CTRL_C_UF)) {
  618. events |= RTC_UF;
  619. num_irqs++;
  620. }
  621. } else {
  622. /*
  623. * One of the "extended" interrupts was received that
  624. * is not recognized by the RTC core.
  625. */
  626. ds1685_rtc_extended_irq(rtc, pdev);
  627. }
  628. }
  629. rtc_update_irq(rtc->dev, num_irqs, events);
  630. mutex_unlock(rtc_mutex);
  631. return events ? IRQ_HANDLED : IRQ_NONE;
  632. }
  633. /* ----------------------------------------------------------------------- */
  634. /* ----------------------------------------------------------------------- */
  635. /* ProcFS interface */
  636. #ifdef CONFIG_PROC_FS
  637. #define NUM_REGS 6 /* Num of control registers. */
  638. #define NUM_BITS 8 /* Num bits per register. */
  639. #define NUM_SPACES 4 /* Num spaces between each bit. */
  640. /*
  641. * Periodic Interrupt Rates.
  642. */
  643. static const char *ds1685_rtc_pirq_rate[16] = {
  644. "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
  645. "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
  646. "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
  647. };
  648. /*
  649. * Square-Wave Output Frequencies.
  650. */
  651. static const char *ds1685_rtc_sqw_freq[16] = {
  652. "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
  653. "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
  654. };
  655. /**
  656. * ds1685_rtc_proc - procfs access function.
  657. * @dev: pointer to device structure.
  658. * @seq: pointer to seq_file structure.
  659. */
  660. static int
  661. ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
  662. {
  663. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  664. u8 ctrla, ctrlb, ctrld, ctrl4a, ctrl4b, ssn[8];
  665. char *model;
  666. /* Read all the relevant data from the control registers. */
  667. ds1685_rtc_switch_to_bank1(rtc);
  668. ds1685_rtc_get_ssn(rtc, ssn);
  669. ctrla = rtc->read(rtc, RTC_CTRL_A);
  670. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  671. ctrld = rtc->read(rtc, RTC_CTRL_D);
  672. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  673. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  674. ds1685_rtc_switch_to_bank0(rtc);
  675. /* Determine the RTC model. */
  676. switch (ssn[0]) {
  677. case RTC_MODEL_DS1685:
  678. model = "DS1685/DS1687\0";
  679. break;
  680. case RTC_MODEL_DS1689:
  681. model = "DS1689/DS1693\0";
  682. break;
  683. case RTC_MODEL_DS17285:
  684. model = "DS17285/DS17287\0";
  685. break;
  686. case RTC_MODEL_DS17485:
  687. model = "DS17485/DS17487\0";
  688. break;
  689. case RTC_MODEL_DS17885:
  690. model = "DS17885/DS17887\0";
  691. break;
  692. default:
  693. model = "Unknown\0";
  694. break;
  695. }
  696. /* Print out the information. */
  697. seq_printf(seq,
  698. "Model\t\t: %s\n"
  699. "Oscillator\t: %s\n"
  700. "12/24hr\t\t: %s\n"
  701. "DST\t\t: %s\n"
  702. "Data mode\t: %s\n"
  703. "Battery\t\t: %s\n"
  704. "Aux batt\t: %s\n"
  705. "Update IRQ\t: %s\n"
  706. "Periodic IRQ\t: %s\n"
  707. "Periodic Rate\t: %s\n"
  708. "SQW Freq\t: %s\n"
  709. "Serial #\t: %8phC\n",
  710. model,
  711. ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
  712. ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
  713. ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
  714. ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
  715. ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
  716. ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
  717. ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
  718. ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
  719. (!(ctrl4b & RTC_CTRL_4B_E32K) ?
  720. ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
  721. (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
  722. ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
  723. ssn);
  724. return 0;
  725. }
  726. #else
  727. #define ds1685_rtc_proc NULL
  728. #endif /* CONFIG_PROC_FS */
  729. /* ----------------------------------------------------------------------- */
  730. /* ----------------------------------------------------------------------- */
  731. /* RTC Class operations */
  732. static const struct rtc_class_ops
  733. ds1685_rtc_ops = {
  734. .proc = ds1685_rtc_proc,
  735. .read_time = ds1685_rtc_read_time,
  736. .set_time = ds1685_rtc_set_time,
  737. .read_alarm = ds1685_rtc_read_alarm,
  738. .set_alarm = ds1685_rtc_set_alarm,
  739. .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
  740. };
  741. /* ----------------------------------------------------------------------- */
  742. static int ds1685_nvram_read(void *priv, unsigned int pos, void *val,
  743. size_t size)
  744. {
  745. struct ds1685_priv *rtc = priv;
  746. struct mutex *rtc_mutex = &rtc->dev->ops_lock;
  747. ssize_t count;
  748. u8 *buf = val;
  749. int err;
  750. err = mutex_lock_interruptible(rtc_mutex);
  751. if (err)
  752. return err;
  753. ds1685_rtc_switch_to_bank0(rtc);
  754. /* Read NVRAM in time and bank0 registers. */
  755. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  756. count++, size--) {
  757. if (count < NVRAM_SZ_TIME)
  758. *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
  759. else
  760. *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
  761. }
  762. #ifndef CONFIG_RTC_DRV_DS1689
  763. if (size > 0) {
  764. ds1685_rtc_switch_to_bank1(rtc);
  765. #ifndef CONFIG_RTC_DRV_DS1685
  766. /* Enable burst-mode on DS17x85/DS17x87 */
  767. rtc->write(rtc, RTC_EXT_CTRL_4A,
  768. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  769. RTC_CTRL_4A_BME));
  770. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  771. * reading with burst-mode */
  772. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  773. (pos - NVRAM_TOTAL_SZ_BANK0));
  774. #endif
  775. /* Read NVRAM in bank1 registers. */
  776. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  777. count++, size--) {
  778. #ifdef CONFIG_RTC_DRV_DS1685
  779. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  780. * before each read. */
  781. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  782. (pos - NVRAM_TOTAL_SZ_BANK0));
  783. #endif
  784. *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
  785. pos++;
  786. }
  787. #ifndef CONFIG_RTC_DRV_DS1685
  788. /* Disable burst-mode on DS17x85/DS17x87 */
  789. rtc->write(rtc, RTC_EXT_CTRL_4A,
  790. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  791. ~(RTC_CTRL_4A_BME)));
  792. #endif
  793. ds1685_rtc_switch_to_bank0(rtc);
  794. }
  795. #endif /* !CONFIG_RTC_DRV_DS1689 */
  796. mutex_unlock(rtc_mutex);
  797. return 0;
  798. }
  799. static int ds1685_nvram_write(void *priv, unsigned int pos, void *val,
  800. size_t size)
  801. {
  802. struct ds1685_priv *rtc = priv;
  803. struct mutex *rtc_mutex = &rtc->dev->ops_lock;
  804. ssize_t count;
  805. u8 *buf = val;
  806. int err;
  807. err = mutex_lock_interruptible(rtc_mutex);
  808. if (err)
  809. return err;
  810. ds1685_rtc_switch_to_bank0(rtc);
  811. /* Write NVRAM in time and bank0 registers. */
  812. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  813. count++, size--)
  814. if (count < NVRAM_SZ_TIME)
  815. rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
  816. *buf++);
  817. else
  818. rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
  819. #ifndef CONFIG_RTC_DRV_DS1689
  820. if (size > 0) {
  821. ds1685_rtc_switch_to_bank1(rtc);
  822. #ifndef CONFIG_RTC_DRV_DS1685
  823. /* Enable burst-mode on DS17x85/DS17x87 */
  824. rtc->write(rtc, RTC_EXT_CTRL_4A,
  825. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  826. RTC_CTRL_4A_BME));
  827. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  828. * writing with burst-mode */
  829. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  830. (pos - NVRAM_TOTAL_SZ_BANK0));
  831. #endif
  832. /* Write NVRAM in bank1 registers. */
  833. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  834. count++, size--) {
  835. #ifdef CONFIG_RTC_DRV_DS1685
  836. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  837. * before each read. */
  838. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  839. (pos - NVRAM_TOTAL_SZ_BANK0));
  840. #endif
  841. rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
  842. pos++;
  843. }
  844. #ifndef CONFIG_RTC_DRV_DS1685
  845. /* Disable burst-mode on DS17x85/DS17x87 */
  846. rtc->write(rtc, RTC_EXT_CTRL_4A,
  847. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  848. ~(RTC_CTRL_4A_BME)));
  849. #endif
  850. ds1685_rtc_switch_to_bank0(rtc);
  851. }
  852. #endif /* !CONFIG_RTC_DRV_DS1689 */
  853. mutex_unlock(rtc_mutex);
  854. return 0;
  855. }
  856. /* ----------------------------------------------------------------------- */
  857. /* SysFS interface */
  858. /**
  859. * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
  860. * @dev: pointer to device structure.
  861. * @attr: pointer to device_attribute structure.
  862. * @buf: pointer to char array to hold the output.
  863. */
  864. static ssize_t
  865. ds1685_rtc_sysfs_battery_show(struct device *dev,
  866. struct device_attribute *attr, char *buf)
  867. {
  868. struct ds1685_priv *rtc = dev_get_drvdata(dev->parent);
  869. u8 ctrld;
  870. ctrld = rtc->read(rtc, RTC_CTRL_D);
  871. return sprintf(buf, "%s\n",
  872. (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
  873. }
  874. static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
  875. /**
  876. * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
  877. * @dev: pointer to device structure.
  878. * @attr: pointer to device_attribute structure.
  879. * @buf: pointer to char array to hold the output.
  880. */
  881. static ssize_t
  882. ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
  883. struct device_attribute *attr, char *buf)
  884. {
  885. struct ds1685_priv *rtc = dev_get_drvdata(dev->parent);
  886. u8 ctrl4a;
  887. ds1685_rtc_switch_to_bank1(rtc);
  888. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  889. ds1685_rtc_switch_to_bank0(rtc);
  890. return sprintf(buf, "%s\n",
  891. (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
  892. }
  893. static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
  894. /**
  895. * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
  896. * @dev: pointer to device structure.
  897. * @attr: pointer to device_attribute structure.
  898. * @buf: pointer to char array to hold the output.
  899. */
  900. static ssize_t
  901. ds1685_rtc_sysfs_serial_show(struct device *dev,
  902. struct device_attribute *attr, char *buf)
  903. {
  904. struct ds1685_priv *rtc = dev_get_drvdata(dev->parent);
  905. u8 ssn[8];
  906. ds1685_rtc_switch_to_bank1(rtc);
  907. ds1685_rtc_get_ssn(rtc, ssn);
  908. ds1685_rtc_switch_to_bank0(rtc);
  909. return sprintf(buf, "%8phC\n", ssn);
  910. }
  911. static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
  912. /*
  913. * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
  914. */
  915. static struct attribute*
  916. ds1685_rtc_sysfs_misc_attrs[] = {
  917. &dev_attr_battery.attr,
  918. &dev_attr_auxbatt.attr,
  919. &dev_attr_serial.attr,
  920. NULL,
  921. };
  922. /*
  923. * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
  924. */
  925. static const struct attribute_group
  926. ds1685_rtc_sysfs_misc_grp = {
  927. .name = "misc",
  928. .attrs = ds1685_rtc_sysfs_misc_attrs,
  929. };
  930. /* ----------------------------------------------------------------------- */
  931. /* Driver Probe/Removal */
  932. /**
  933. * ds1685_rtc_probe - initializes rtc driver.
  934. * @pdev: pointer to platform_device structure.
  935. */
  936. static int
  937. ds1685_rtc_probe(struct platform_device *pdev)
  938. {
  939. struct rtc_device *rtc_dev;
  940. struct ds1685_priv *rtc;
  941. struct ds1685_rtc_platform_data *pdata;
  942. u8 ctrla, ctrlb, hours;
  943. unsigned char am_pm;
  944. int ret = 0;
  945. struct nvmem_config nvmem_cfg = {
  946. .name = "ds1685_nvram",
  947. .size = NVRAM_TOTAL_SZ,
  948. .reg_read = ds1685_nvram_read,
  949. .reg_write = ds1685_nvram_write,
  950. };
  951. /* Get the platform data. */
  952. pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
  953. if (!pdata)
  954. return -ENODEV;
  955. /* Allocate memory for the rtc device. */
  956. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  957. if (!rtc)
  958. return -ENOMEM;
  959. /* Setup resources and access functions */
  960. switch (pdata->access_type) {
  961. case ds1685_reg_direct:
  962. rtc->regs = devm_platform_ioremap_resource(pdev, 0);
  963. if (IS_ERR(rtc->regs))
  964. return PTR_ERR(rtc->regs);
  965. rtc->read = ds1685_read;
  966. rtc->write = ds1685_write;
  967. break;
  968. case ds1685_reg_indirect:
  969. rtc->regs = devm_platform_ioremap_resource(pdev, 0);
  970. if (IS_ERR(rtc->regs))
  971. return PTR_ERR(rtc->regs);
  972. rtc->data = devm_platform_ioremap_resource(pdev, 1);
  973. if (IS_ERR(rtc->data))
  974. return PTR_ERR(rtc->data);
  975. rtc->read = ds1685_indirect_read;
  976. rtc->write = ds1685_indirect_write;
  977. break;
  978. }
  979. if (!rtc->read || !rtc->write)
  980. return -ENXIO;
  981. /* Get the register step size. */
  982. if (pdata->regstep > 0)
  983. rtc->regstep = pdata->regstep;
  984. else
  985. rtc->regstep = 1;
  986. /* Platform pre-shutdown function, if defined. */
  987. if (pdata->plat_prepare_poweroff)
  988. rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
  989. /* Platform wake_alarm function, if defined. */
  990. if (pdata->plat_wake_alarm)
  991. rtc->wake_alarm = pdata->plat_wake_alarm;
  992. /* Platform post_ram_clear function, if defined. */
  993. if (pdata->plat_post_ram_clear)
  994. rtc->post_ram_clear = pdata->plat_post_ram_clear;
  995. /* set the driver data. */
  996. platform_set_drvdata(pdev, rtc);
  997. /* Turn the oscillator on if is not already on (DV1 = 1). */
  998. ctrla = rtc->read(rtc, RTC_CTRL_A);
  999. if (!(ctrla & RTC_CTRL_A_DV1))
  1000. ctrla |= RTC_CTRL_A_DV1;
  1001. /* Enable the countdown chain (DV2 = 0) */
  1002. ctrla &= ~(RTC_CTRL_A_DV2);
  1003. /* Clear RS3-RS0 in Control A. */
  1004. ctrla &= ~(RTC_CTRL_A_RS_MASK);
  1005. /*
  1006. * All done with Control A. Switch to Bank 1 for the remainder of
  1007. * the RTC setup so we have access to the extended functions.
  1008. */
  1009. ctrla |= RTC_CTRL_A_DV0;
  1010. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1011. /* Default to 32768kHz output. */
  1012. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1013. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
  1014. /* Set the SET bit in Control B so we can do some housekeeping. */
  1015. rtc->write(rtc, RTC_CTRL_B,
  1016. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  1017. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  1018. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  1019. cpu_relax();
  1020. /*
  1021. * If the platform supports BCD mode, then set DM=0 in Control B.
  1022. * Otherwise, set DM=1 for BIN mode.
  1023. */
  1024. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  1025. if (pdata->bcd_mode)
  1026. ctrlb &= ~(RTC_CTRL_B_DM);
  1027. else
  1028. ctrlb |= RTC_CTRL_B_DM;
  1029. rtc->bcd_mode = pdata->bcd_mode;
  1030. /*
  1031. * Disable Daylight Savings Time (DSE = 0).
  1032. * The RTC has hardcoded timezone information that is rendered
  1033. * obselete. We'll let the OS deal with DST settings instead.
  1034. */
  1035. if (ctrlb & RTC_CTRL_B_DSE)
  1036. ctrlb &= ~(RTC_CTRL_B_DSE);
  1037. /* Force 24-hour mode (2412 = 1). */
  1038. if (!(ctrlb & RTC_CTRL_B_2412)) {
  1039. /* Reinitialize the time hours. */
  1040. hours = rtc->read(rtc, RTC_HRS);
  1041. am_pm = hours & RTC_HRS_AMPM_MASK;
  1042. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1043. RTC_HRS_12_BIN_MASK);
  1044. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1045. /* Enable 24-hour mode. */
  1046. ctrlb |= RTC_CTRL_B_2412;
  1047. /* Write back to Control B, including DM & DSE bits. */
  1048. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1049. /* Write the time hours back. */
  1050. rtc->write(rtc, RTC_HRS,
  1051. ds1685_rtc_bin2bcd(rtc, hours,
  1052. RTC_HRS_24_BIN_MASK,
  1053. RTC_HRS_24_BCD_MASK));
  1054. /* Reinitialize the alarm hours. */
  1055. hours = rtc->read(rtc, RTC_HRS_ALARM);
  1056. am_pm = hours & RTC_HRS_AMPM_MASK;
  1057. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1058. RTC_HRS_12_BIN_MASK);
  1059. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1060. /* Write the alarm hours back. */
  1061. rtc->write(rtc, RTC_HRS_ALARM,
  1062. ds1685_rtc_bin2bcd(rtc, hours,
  1063. RTC_HRS_24_BIN_MASK,
  1064. RTC_HRS_24_BCD_MASK));
  1065. } else {
  1066. /* 24-hour mode is already set, so write Control B back. */
  1067. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1068. }
  1069. /* Unset the SET bit in Control B so the RTC can update. */
  1070. rtc->write(rtc, RTC_CTRL_B,
  1071. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  1072. /* Check the main battery. */
  1073. if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
  1074. dev_warn(&pdev->dev,
  1075. "Main battery is exhausted! RTC may be invalid!\n");
  1076. /* Check the auxillary battery. It is optional. */
  1077. if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
  1078. dev_warn(&pdev->dev,
  1079. "Aux battery is exhausted or not available.\n");
  1080. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1081. rtc->write(rtc, RTC_CTRL_B,
  1082. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
  1083. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1084. rtc->read(rtc, RTC_CTRL_C);
  1085. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1086. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1087. (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
  1088. /* Clear RF/WF/KF in Ctrl 4A. */
  1089. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1090. (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
  1091. /*
  1092. * Re-enable KSE to handle power button events. We do not enable
  1093. * WIE or RIE by default.
  1094. */
  1095. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1096. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
  1097. rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  1098. if (IS_ERR(rtc_dev))
  1099. return PTR_ERR(rtc_dev);
  1100. rtc_dev->ops = &ds1685_rtc_ops;
  1101. /* Century bit is useless because leap year fails in 1900 and 2100 */
  1102. rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
  1103. rtc_dev->range_max = RTC_TIMESTAMP_END_2099;
  1104. /* Maximum periodic rate is 8192Hz (0.122070ms). */
  1105. rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
  1106. /* See if the platform doesn't support UIE. */
  1107. if (pdata->uie_unsupported)
  1108. rtc_dev->uie_unsupported = 1;
  1109. rtc->dev = rtc_dev;
  1110. /*
  1111. * Fetch the IRQ and setup the interrupt handler.
  1112. *
  1113. * Not all platforms have the IRQF pin tied to something. If not, the
  1114. * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
  1115. * there won't be an automatic way of notifying the kernel about it,
  1116. * unless ctrlc is explicitly polled.
  1117. */
  1118. if (!pdata->no_irq) {
  1119. ret = platform_get_irq(pdev, 0);
  1120. if (ret <= 0)
  1121. return ret;
  1122. rtc->irq_num = ret;
  1123. /* Request an IRQ. */
  1124. ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_num,
  1125. NULL, ds1685_rtc_irq_handler,
  1126. IRQF_SHARED | IRQF_ONESHOT,
  1127. pdev->name, pdev);
  1128. /* Check to see if something came back. */
  1129. if (unlikely(ret)) {
  1130. dev_warn(&pdev->dev,
  1131. "RTC interrupt not available\n");
  1132. rtc->irq_num = 0;
  1133. }
  1134. }
  1135. rtc->no_irq = pdata->no_irq;
  1136. /* Setup complete. */
  1137. ds1685_rtc_switch_to_bank0(rtc);
  1138. ret = rtc_add_group(rtc_dev, &ds1685_rtc_sysfs_misc_grp);
  1139. if (ret)
  1140. return ret;
  1141. rtc_dev->nvram_old_abi = true;
  1142. nvmem_cfg.priv = rtc;
  1143. ret = rtc_nvmem_register(rtc_dev, &nvmem_cfg);
  1144. if (ret)
  1145. return ret;
  1146. return rtc_register_device(rtc_dev);
  1147. }
  1148. /**
  1149. * ds1685_rtc_remove - removes rtc driver.
  1150. * @pdev: pointer to platform_device structure.
  1151. */
  1152. static int
  1153. ds1685_rtc_remove(struct platform_device *pdev)
  1154. {
  1155. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1156. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1157. rtc->write(rtc, RTC_CTRL_B,
  1158. (rtc->read(rtc, RTC_CTRL_B) &
  1159. ~(RTC_CTRL_B_PAU_MASK)));
  1160. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1161. rtc->read(rtc, RTC_CTRL_C);
  1162. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1163. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1164. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  1165. ~(RTC_CTRL_4B_RWK_MASK)));
  1166. /* Manually clear RF/WF/KF in Ctrl 4A. */
  1167. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1168. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  1169. ~(RTC_CTRL_4A_RWK_MASK)));
  1170. return 0;
  1171. }
  1172. /*
  1173. * ds1685_rtc_driver - rtc driver properties.
  1174. */
  1175. static struct platform_driver ds1685_rtc_driver = {
  1176. .driver = {
  1177. .name = "rtc-ds1685",
  1178. },
  1179. .probe = ds1685_rtc_probe,
  1180. .remove = ds1685_rtc_remove,
  1181. };
  1182. module_platform_driver(ds1685_rtc_driver);
  1183. /* ----------------------------------------------------------------------- */
  1184. /* ----------------------------------------------------------------------- */
  1185. /* Poweroff function */
  1186. /**
  1187. * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
  1188. * @pdev: pointer to platform_device structure.
  1189. */
  1190. void __noreturn
  1191. ds1685_rtc_poweroff(struct platform_device *pdev)
  1192. {
  1193. u8 ctrla, ctrl4a, ctrl4b;
  1194. struct ds1685_priv *rtc;
  1195. /* Check for valid RTC data, else, spin forever. */
  1196. if (unlikely(!pdev)) {
  1197. pr_emerg("platform device data not available, spinning forever ...\n");
  1198. while(1);
  1199. unreachable();
  1200. } else {
  1201. /* Get the rtc data. */
  1202. rtc = platform_get_drvdata(pdev);
  1203. /*
  1204. * Disable our IRQ. We're powering down, so we're not
  1205. * going to worry about cleaning up. Most of that should
  1206. * have been taken care of by the shutdown scripts and this
  1207. * is the final function call.
  1208. */
  1209. if (!rtc->no_irq)
  1210. disable_irq_nosync(rtc->irq_num);
  1211. /* Oscillator must be on and the countdown chain enabled. */
  1212. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1213. ctrla |= RTC_CTRL_A_DV1;
  1214. ctrla &= ~(RTC_CTRL_A_DV2);
  1215. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1216. /*
  1217. * Read Control 4A and check the status of the auxillary
  1218. * battery. This must be present and working (VRT2 = 1)
  1219. * for wakeup and kickstart functionality to be useful.
  1220. */
  1221. ds1685_rtc_switch_to_bank1(rtc);
  1222. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1223. if (ctrl4a & RTC_CTRL_4A_VRT2) {
  1224. /* Clear all of the interrupt flags on Control 4A. */
  1225. ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
  1226. rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
  1227. /*
  1228. * The auxillary battery is present and working.
  1229. * Enable extended functions (ABE=1), enable
  1230. * wake-up (WIE=1), and enable kickstart (KSE=1)
  1231. * in Control 4B.
  1232. */
  1233. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  1234. ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
  1235. RTC_CTRL_4B_KSE);
  1236. rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
  1237. }
  1238. /* Set PAB to 1 in Control 4A to power the system down. */
  1239. dev_warn(&pdev->dev, "Powerdown.\n");
  1240. msleep(20);
  1241. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1242. (ctrl4a | RTC_CTRL_4A_PAB));
  1243. /* Spin ... we do not switch back to bank0. */
  1244. while(1);
  1245. unreachable();
  1246. }
  1247. }
  1248. EXPORT_SYMBOL(ds1685_rtc_poweroff);
  1249. /* ----------------------------------------------------------------------- */
  1250. MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
  1251. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
  1252. MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
  1253. MODULE_LICENSE("GPL");
  1254. MODULE_ALIAS("platform:rtc-ds1685");