rtc-ds1685.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 ds1685_priv *rtc = dev_get_drvdata(dev);
  243. u8 ctrlb, century;
  244. u8 seconds, minutes, hours, wday, mday, month, years;
  245. /* Fetch the time info from the RTC registers. */
  246. ds1685_rtc_begin_data_access(rtc);
  247. seconds = rtc->read(rtc, RTC_SECS);
  248. minutes = rtc->read(rtc, RTC_MINS);
  249. hours = rtc->read(rtc, RTC_HRS);
  250. wday = rtc->read(rtc, RTC_WDAY);
  251. mday = rtc->read(rtc, RTC_MDAY);
  252. month = rtc->read(rtc, RTC_MONTH);
  253. years = rtc->read(rtc, RTC_YEAR);
  254. century = rtc->read(rtc, RTC_CENTURY);
  255. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  256. ds1685_rtc_end_data_access(rtc);
  257. /* bcd2bin if needed, perform fixups, and store to rtc_time. */
  258. years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK,
  259. RTC_YEAR_BIN_MASK);
  260. century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK,
  261. RTC_CENTURY_MASK);
  262. tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK,
  263. RTC_SECS_BIN_MASK);
  264. tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK,
  265. RTC_MINS_BIN_MASK);
  266. tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK,
  267. RTC_HRS_24_BIN_MASK);
  268. tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK,
  269. RTC_WDAY_MASK) - 1);
  270. tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  271. RTC_MDAY_BIN_MASK);
  272. tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK,
  273. RTC_MONTH_BIN_MASK) - 1);
  274. tm->tm_year = ((years + (century * 100)) - 1900);
  275. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  276. tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */
  277. return 0;
  278. }
  279. /**
  280. * ds1685_rtc_set_time - sets the time registers.
  281. * @dev: pointer to device structure.
  282. * @tm: pointer to rtc_time structure.
  283. */
  284. static int
  285. ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm)
  286. {
  287. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  288. u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century;
  289. /* Fetch the time info from rtc_time. */
  290. seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK,
  291. RTC_SECS_BCD_MASK);
  292. minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK,
  293. RTC_MINS_BCD_MASK);
  294. hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK,
  295. RTC_HRS_24_BCD_MASK);
  296. wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK,
  297. RTC_WDAY_MASK);
  298. mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK,
  299. RTC_MDAY_BCD_MASK);
  300. month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK,
  301. RTC_MONTH_BCD_MASK);
  302. years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100),
  303. RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK);
  304. century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100),
  305. RTC_CENTURY_MASK, RTC_CENTURY_MASK);
  306. /*
  307. * Perform Sanity Checks:
  308. * - Months: !> 12, Month Day != 0.
  309. * - Month Day !> Max days in current month.
  310. * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7.
  311. */
  312. if ((tm->tm_mon > 11) || (mday == 0))
  313. return -EDOM;
  314. if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year))
  315. return -EDOM;
  316. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) ||
  317. (tm->tm_sec >= 60) || (wday > 7))
  318. return -EDOM;
  319. /*
  320. * Set the data mode to use and store the time values in the
  321. * RTC registers.
  322. */
  323. ds1685_rtc_begin_data_access(rtc);
  324. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  325. if (rtc->bcd_mode)
  326. ctrlb &= ~(RTC_CTRL_B_DM);
  327. else
  328. ctrlb |= RTC_CTRL_B_DM;
  329. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  330. rtc->write(rtc, RTC_SECS, seconds);
  331. rtc->write(rtc, RTC_MINS, minutes);
  332. rtc->write(rtc, RTC_HRS, hours);
  333. rtc->write(rtc, RTC_WDAY, wday);
  334. rtc->write(rtc, RTC_MDAY, mday);
  335. rtc->write(rtc, RTC_MONTH, month);
  336. rtc->write(rtc, RTC_YEAR, years);
  337. rtc->write(rtc, RTC_CENTURY, century);
  338. ds1685_rtc_end_data_access(rtc);
  339. return 0;
  340. }
  341. /**
  342. * ds1685_rtc_read_alarm - reads the alarm registers.
  343. * @dev: pointer to device structure.
  344. * @alrm: pointer to rtc_wkalrm structure.
  345. *
  346. * There are three primary alarm registers: seconds, minutes, and hours.
  347. * A fourth alarm register for the month date is also available in bank1 for
  348. * kickstart/wakeup features. The DS1685/DS1687 manual states that a
  349. * "don't care" value ranging from 0xc0 to 0xff may be written into one or
  350. * more of the three alarm bytes to act as a wildcard value. The fourth
  351. * byte doesn't support a "don't care" value.
  352. */
  353. static int
  354. ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  355. {
  356. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  357. u8 seconds, minutes, hours, mday, ctrlb, ctrlc;
  358. int ret;
  359. /* Fetch the alarm info from the RTC alarm registers. */
  360. ds1685_rtc_begin_data_access(rtc);
  361. seconds = rtc->read(rtc, RTC_SECS_ALARM);
  362. minutes = rtc->read(rtc, RTC_MINS_ALARM);
  363. hours = rtc->read(rtc, RTC_HRS_ALARM);
  364. mday = rtc->read(rtc, RTC_MDAY_ALARM);
  365. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  366. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  367. ds1685_rtc_end_data_access(rtc);
  368. /* Check the month date for validity. */
  369. ret = ds1685_rtc_check_mday(rtc, mday);
  370. if (ret)
  371. return ret;
  372. /*
  373. * Check the three alarm bytes.
  374. *
  375. * The Linux RTC system doesn't support the "don't care" capability
  376. * of this RTC chip. We check for it anyways in case support is
  377. * added in the future and only assign when we care.
  378. */
  379. if (likely(seconds < 0xc0))
  380. alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
  381. RTC_SECS_BCD_MASK,
  382. RTC_SECS_BIN_MASK);
  383. if (likely(minutes < 0xc0))
  384. alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
  385. RTC_MINS_BCD_MASK,
  386. RTC_MINS_BIN_MASK);
  387. if (likely(hours < 0xc0))
  388. alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
  389. RTC_HRS_24_BCD_MASK,
  390. RTC_HRS_24_BIN_MASK);
  391. /* Write the data to rtc_wkalrm. */
  392. alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
  393. RTC_MDAY_BIN_MASK);
  394. alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
  395. alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
  396. return 0;
  397. }
  398. /**
  399. * ds1685_rtc_set_alarm - sets the alarm in registers.
  400. * @dev: pointer to device structure.
  401. * @alrm: pointer to rtc_wkalrm structure.
  402. */
  403. static int
  404. ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  405. {
  406. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  407. u8 ctrlb, seconds, minutes, hours, mday;
  408. int ret;
  409. /* Fetch the alarm info and convert to BCD. */
  410. seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
  411. RTC_SECS_BIN_MASK,
  412. RTC_SECS_BCD_MASK);
  413. minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
  414. RTC_MINS_BIN_MASK,
  415. RTC_MINS_BCD_MASK);
  416. hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
  417. RTC_HRS_24_BIN_MASK,
  418. RTC_HRS_24_BCD_MASK);
  419. mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
  420. RTC_MDAY_BIN_MASK,
  421. RTC_MDAY_BCD_MASK);
  422. /* Check the month date for validity. */
  423. ret = ds1685_rtc_check_mday(rtc, mday);
  424. if (ret)
  425. return ret;
  426. /*
  427. * Check the three alarm bytes.
  428. *
  429. * The Linux RTC system doesn't support the "don't care" capability
  430. * of this RTC chip because rtc_valid_tm tries to validate every
  431. * field, and we only support four fields. We put the support
  432. * here anyways for the future.
  433. */
  434. if (unlikely(seconds >= 0xc0))
  435. seconds = 0xff;
  436. if (unlikely(minutes >= 0xc0))
  437. minutes = 0xff;
  438. if (unlikely(hours >= 0xc0))
  439. hours = 0xff;
  440. alrm->time.tm_mon = -1;
  441. alrm->time.tm_year = -1;
  442. alrm->time.tm_wday = -1;
  443. alrm->time.tm_yday = -1;
  444. alrm->time.tm_isdst = -1;
  445. /* Disable the alarm interrupt first. */
  446. ds1685_rtc_begin_data_access(rtc);
  447. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  448. rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE)));
  449. /* Read ctrlc to clear RTC_CTRL_C_AF. */
  450. rtc->read(rtc, RTC_CTRL_C);
  451. /*
  452. * Set the data mode to use and store the time values in the
  453. * RTC registers.
  454. */
  455. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  456. if (rtc->bcd_mode)
  457. ctrlb &= ~(RTC_CTRL_B_DM);
  458. else
  459. ctrlb |= RTC_CTRL_B_DM;
  460. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  461. rtc->write(rtc, RTC_SECS_ALARM, seconds);
  462. rtc->write(rtc, RTC_MINS_ALARM, minutes);
  463. rtc->write(rtc, RTC_HRS_ALARM, hours);
  464. rtc->write(rtc, RTC_MDAY_ALARM, mday);
  465. /* Re-enable the alarm if needed. */
  466. if (alrm->enabled) {
  467. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  468. ctrlb |= RTC_CTRL_B_AIE;
  469. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  470. }
  471. /* Done! */
  472. ds1685_rtc_end_data_access(rtc);
  473. return 0;
  474. }
  475. /* ----------------------------------------------------------------------- */
  476. /* ----------------------------------------------------------------------- */
  477. /* /dev/rtcX Interface functions */
  478. /**
  479. * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off.
  480. * @dev: pointer to device structure.
  481. * @enabled: flag indicating whether to enable or disable.
  482. */
  483. static int
  484. ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  485. {
  486. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  487. unsigned long flags = 0;
  488. /* Enable/disable the Alarm IRQ-Enable flag. */
  489. spin_lock_irqsave(&rtc->lock, flags);
  490. /* Flip the requisite interrupt-enable bit. */
  491. if (enabled)
  492. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
  493. RTC_CTRL_B_AIE));
  494. else
  495. rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
  496. ~(RTC_CTRL_B_AIE)));
  497. /* Read Control C to clear all the flag bits. */
  498. rtc->read(rtc, RTC_CTRL_C);
  499. spin_unlock_irqrestore(&rtc->lock, flags);
  500. return 0;
  501. }
  502. /* ----------------------------------------------------------------------- */
  503. /* ----------------------------------------------------------------------- */
  504. /* IRQ handler & workqueue. */
  505. /**
  506. * ds1685_rtc_irq_handler - IRQ handler.
  507. * @irq: IRQ number.
  508. * @dev_id: platform device pointer.
  509. */
  510. static irqreturn_t
  511. ds1685_rtc_irq_handler(int irq, void *dev_id)
  512. {
  513. struct platform_device *pdev = dev_id;
  514. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  515. u8 ctrlb, ctrlc;
  516. unsigned long events = 0;
  517. u8 num_irqs = 0;
  518. /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
  519. if (unlikely(!rtc))
  520. return IRQ_HANDLED;
  521. /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
  522. spin_lock(&rtc->lock);
  523. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  524. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  525. /* Is the IRQF bit set? */
  526. if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
  527. /*
  528. * We need to determine if it was one of the standard
  529. * events: PF, AF, or UF. If so, we handle them and
  530. * update the RTC core.
  531. */
  532. if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
  533. events = RTC_IRQF;
  534. /* Check for a periodic interrupt. */
  535. if ((ctrlb & RTC_CTRL_B_PIE) &&
  536. (ctrlc & RTC_CTRL_C_PF)) {
  537. events |= RTC_PF;
  538. num_irqs++;
  539. }
  540. /* Check for an alarm interrupt. */
  541. if ((ctrlb & RTC_CTRL_B_AIE) &&
  542. (ctrlc & RTC_CTRL_C_AF)) {
  543. events |= RTC_AF;
  544. num_irqs++;
  545. }
  546. /* Check for an update interrupt. */
  547. if ((ctrlb & RTC_CTRL_B_UIE) &&
  548. (ctrlc & RTC_CTRL_C_UF)) {
  549. events |= RTC_UF;
  550. num_irqs++;
  551. }
  552. rtc_update_irq(rtc->dev, num_irqs, events);
  553. } else {
  554. /*
  555. * One of the "extended" interrupts was received that
  556. * is not recognized by the RTC core. These need to
  557. * be handled in task context as they can call other
  558. * functions and the time spent in irq context needs
  559. * to be minimized. Schedule them into a workqueue
  560. * and inform the RTC core that the IRQs were handled.
  561. */
  562. spin_unlock(&rtc->lock);
  563. schedule_work(&rtc->work);
  564. rtc_update_irq(rtc->dev, 0, 0);
  565. return IRQ_HANDLED;
  566. }
  567. }
  568. spin_unlock(&rtc->lock);
  569. return events ? IRQ_HANDLED : IRQ_NONE;
  570. }
  571. /**
  572. * ds1685_rtc_work_queue - work queue handler.
  573. * @work: work_struct containing data to work on in task context.
  574. */
  575. static void
  576. ds1685_rtc_work_queue(struct work_struct *work)
  577. {
  578. struct ds1685_priv *rtc = container_of(work,
  579. struct ds1685_priv, work);
  580. struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
  581. struct mutex *rtc_mutex = &rtc->dev->ops_lock;
  582. u8 ctrl4a, ctrl4b;
  583. mutex_lock(rtc_mutex);
  584. ds1685_rtc_switch_to_bank1(rtc);
  585. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  586. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  587. /*
  588. * Check for a kickstart interrupt. With Vcc applied, this
  589. * typically means that the power button was pressed, so we
  590. * begin the shutdown sequence.
  591. */
  592. if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
  593. /* Briefly disable kickstarts to debounce button presses. */
  594. rtc->write(rtc, RTC_EXT_CTRL_4B,
  595. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  596. ~(RTC_CTRL_4B_KSE)));
  597. /* Clear the kickstart flag. */
  598. rtc->write(rtc, RTC_EXT_CTRL_4A,
  599. (ctrl4a & ~(RTC_CTRL_4A_KF)));
  600. /*
  601. * Sleep 500ms before re-enabling kickstarts. This allows
  602. * adequate time to avoid reading signal jitter as additional
  603. * button presses.
  604. */
  605. msleep(500);
  606. rtc->write(rtc, RTC_EXT_CTRL_4B,
  607. (rtc->read(rtc, RTC_EXT_CTRL_4B) |
  608. RTC_CTRL_4B_KSE));
  609. /* Call the platform pre-poweroff function. Else, shutdown. */
  610. if (rtc->prepare_poweroff != NULL)
  611. rtc->prepare_poweroff();
  612. else
  613. ds1685_rtc_poweroff(pdev);
  614. }
  615. /*
  616. * Check for a wake-up interrupt. With Vcc applied, this is
  617. * essentially a second alarm interrupt, except it takes into
  618. * account the 'date' register in bank1 in addition to the
  619. * standard three alarm registers.
  620. */
  621. if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
  622. rtc->write(rtc, RTC_EXT_CTRL_4A,
  623. (ctrl4a & ~(RTC_CTRL_4A_WF)));
  624. /* Call the platform wake_alarm function if defined. */
  625. if (rtc->wake_alarm != NULL)
  626. rtc->wake_alarm();
  627. else
  628. dev_warn(&pdev->dev,
  629. "Wake Alarm IRQ just occurred!\n");
  630. }
  631. /*
  632. * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0
  633. * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting
  634. * each byte to a logic 1. This has no effect on any extended
  635. * NV-SRAM that might be present, nor on the time/calendar/alarm
  636. * registers. After a ram-clear is completed, there is a minimum
  637. * recovery time of ~150ms in which all reads/writes are locked out.
  638. * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot
  639. * catch this scenario.
  640. */
  641. if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
  642. rtc->write(rtc, RTC_EXT_CTRL_4A,
  643. (ctrl4a & ~(RTC_CTRL_4A_RF)));
  644. msleep(150);
  645. /* Call the platform post_ram_clear function if defined. */
  646. if (rtc->post_ram_clear != NULL)
  647. rtc->post_ram_clear();
  648. else
  649. dev_warn(&pdev->dev,
  650. "RAM-Clear IRQ just occurred!\n");
  651. }
  652. ds1685_rtc_switch_to_bank0(rtc);
  653. mutex_unlock(rtc_mutex);
  654. }
  655. /* ----------------------------------------------------------------------- */
  656. /* ----------------------------------------------------------------------- */
  657. /* ProcFS interface */
  658. #ifdef CONFIG_PROC_FS
  659. #define NUM_REGS 6 /* Num of control registers. */
  660. #define NUM_BITS 8 /* Num bits per register. */
  661. #define NUM_SPACES 4 /* Num spaces between each bit. */
  662. /*
  663. * Periodic Interrupt Rates.
  664. */
  665. static const char *ds1685_rtc_pirq_rate[16] = {
  666. "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
  667. "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
  668. "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
  669. };
  670. /*
  671. * Square-Wave Output Frequencies.
  672. */
  673. static const char *ds1685_rtc_sqw_freq[16] = {
  674. "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
  675. "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
  676. };
  677. /**
  678. * ds1685_rtc_proc - procfs access function.
  679. * @dev: pointer to device structure.
  680. * @seq: pointer to seq_file structure.
  681. */
  682. static int
  683. ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
  684. {
  685. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  686. u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8];
  687. char *model;
  688. /* Read all the relevant data from the control registers. */
  689. ds1685_rtc_switch_to_bank1(rtc);
  690. ds1685_rtc_get_ssn(rtc, ssn);
  691. ctrla = rtc->read(rtc, RTC_CTRL_A);
  692. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  693. ctrlc = rtc->read(rtc, RTC_CTRL_C);
  694. ctrld = rtc->read(rtc, RTC_CTRL_D);
  695. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  696. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  697. ds1685_rtc_switch_to_bank0(rtc);
  698. /* Determine the RTC model. */
  699. switch (ssn[0]) {
  700. case RTC_MODEL_DS1685:
  701. model = "DS1685/DS1687\0";
  702. break;
  703. case RTC_MODEL_DS1689:
  704. model = "DS1689/DS1693\0";
  705. break;
  706. case RTC_MODEL_DS17285:
  707. model = "DS17285/DS17287\0";
  708. break;
  709. case RTC_MODEL_DS17485:
  710. model = "DS17485/DS17487\0";
  711. break;
  712. case RTC_MODEL_DS17885:
  713. model = "DS17885/DS17887\0";
  714. break;
  715. default:
  716. model = "Unknown\0";
  717. break;
  718. }
  719. /* Print out the information. */
  720. seq_printf(seq,
  721. "Model\t\t: %s\n"
  722. "Oscillator\t: %s\n"
  723. "12/24hr\t\t: %s\n"
  724. "DST\t\t: %s\n"
  725. "Data mode\t: %s\n"
  726. "Battery\t\t: %s\n"
  727. "Aux batt\t: %s\n"
  728. "Update IRQ\t: %s\n"
  729. "Periodic IRQ\t: %s\n"
  730. "Periodic Rate\t: %s\n"
  731. "SQW Freq\t: %s\n"
  732. "Serial #\t: %8phC\n",
  733. model,
  734. ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
  735. ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
  736. ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
  737. ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
  738. ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
  739. ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
  740. ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
  741. ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
  742. (!(ctrl4b & RTC_CTRL_4B_E32K) ?
  743. ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
  744. (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
  745. ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
  746. ssn);
  747. return 0;
  748. }
  749. #else
  750. #define ds1685_rtc_proc NULL
  751. #endif /* CONFIG_PROC_FS */
  752. /* ----------------------------------------------------------------------- */
  753. /* ----------------------------------------------------------------------- */
  754. /* RTC Class operations */
  755. static const struct rtc_class_ops
  756. ds1685_rtc_ops = {
  757. .proc = ds1685_rtc_proc,
  758. .read_time = ds1685_rtc_read_time,
  759. .set_time = ds1685_rtc_set_time,
  760. .read_alarm = ds1685_rtc_read_alarm,
  761. .set_alarm = ds1685_rtc_set_alarm,
  762. .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
  763. };
  764. /* ----------------------------------------------------------------------- */
  765. static int ds1685_nvram_read(void *priv, unsigned int pos, void *val,
  766. size_t size)
  767. {
  768. struct ds1685_priv *rtc = priv;
  769. ssize_t count;
  770. unsigned long flags = 0;
  771. u8 *buf = val;
  772. spin_lock_irqsave(&rtc->lock, flags);
  773. ds1685_rtc_switch_to_bank0(rtc);
  774. /* Read NVRAM in time and bank0 registers. */
  775. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  776. count++, size--) {
  777. if (count < NVRAM_SZ_TIME)
  778. *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
  779. else
  780. *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
  781. }
  782. #ifndef CONFIG_RTC_DRV_DS1689
  783. if (size > 0) {
  784. ds1685_rtc_switch_to_bank1(rtc);
  785. #ifndef CONFIG_RTC_DRV_DS1685
  786. /* Enable burst-mode on DS17x85/DS17x87 */
  787. rtc->write(rtc, RTC_EXT_CTRL_4A,
  788. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  789. RTC_CTRL_4A_BME));
  790. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  791. * reading with burst-mode */
  792. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  793. (pos - NVRAM_TOTAL_SZ_BANK0));
  794. #endif
  795. /* Read NVRAM in bank1 registers. */
  796. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  797. count++, size--) {
  798. #ifdef CONFIG_RTC_DRV_DS1685
  799. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  800. * before each read. */
  801. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  802. (pos - NVRAM_TOTAL_SZ_BANK0));
  803. #endif
  804. *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
  805. pos++;
  806. }
  807. #ifndef CONFIG_RTC_DRV_DS1685
  808. /* Disable burst-mode on DS17x85/DS17x87 */
  809. rtc->write(rtc, RTC_EXT_CTRL_4A,
  810. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  811. ~(RTC_CTRL_4A_BME)));
  812. #endif
  813. ds1685_rtc_switch_to_bank0(rtc);
  814. }
  815. #endif /* !CONFIG_RTC_DRV_DS1689 */
  816. spin_unlock_irqrestore(&rtc->lock, flags);
  817. return 0;
  818. }
  819. static int ds1685_nvram_write(void *priv, unsigned int pos, void *val,
  820. size_t size)
  821. {
  822. struct ds1685_priv *rtc = priv;
  823. ssize_t count;
  824. unsigned long flags = 0;
  825. u8 *buf = val;
  826. spin_lock_irqsave(&rtc->lock, flags);
  827. ds1685_rtc_switch_to_bank0(rtc);
  828. /* Write NVRAM in time and bank0 registers. */
  829. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
  830. count++, size--)
  831. if (count < NVRAM_SZ_TIME)
  832. rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
  833. *buf++);
  834. else
  835. rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
  836. #ifndef CONFIG_RTC_DRV_DS1689
  837. if (size > 0) {
  838. ds1685_rtc_switch_to_bank1(rtc);
  839. #ifndef CONFIG_RTC_DRV_DS1685
  840. /* Enable burst-mode on DS17x85/DS17x87 */
  841. rtc->write(rtc, RTC_EXT_CTRL_4A,
  842. (rtc->read(rtc, RTC_EXT_CTRL_4A) |
  843. RTC_CTRL_4A_BME));
  844. /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start
  845. * writing with burst-mode */
  846. rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
  847. (pos - NVRAM_TOTAL_SZ_BANK0));
  848. #endif
  849. /* Write NVRAM in bank1 registers. */
  850. for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
  851. count++, size--) {
  852. #ifdef CONFIG_RTC_DRV_DS1685
  853. /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR
  854. * before each read. */
  855. rtc->write(rtc, RTC_BANK1_RAM_ADDR,
  856. (pos - NVRAM_TOTAL_SZ_BANK0));
  857. #endif
  858. rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
  859. pos++;
  860. }
  861. #ifndef CONFIG_RTC_DRV_DS1685
  862. /* Disable burst-mode on DS17x85/DS17x87 */
  863. rtc->write(rtc, RTC_EXT_CTRL_4A,
  864. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  865. ~(RTC_CTRL_4A_BME)));
  866. #endif
  867. ds1685_rtc_switch_to_bank0(rtc);
  868. }
  869. #endif /* !CONFIG_RTC_DRV_DS1689 */
  870. spin_unlock_irqrestore(&rtc->lock, flags);
  871. return 0;
  872. }
  873. /* ----------------------------------------------------------------------- */
  874. /* SysFS interface */
  875. /**
  876. * ds1685_rtc_sysfs_battery_show - sysfs file for main 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_battery_show(struct device *dev,
  883. struct device_attribute *attr, char *buf)
  884. {
  885. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  886. u8 ctrld;
  887. ctrld = rtc->read(rtc, RTC_CTRL_D);
  888. return sprintf(buf, "%s\n",
  889. (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
  890. }
  891. static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
  892. /**
  893. * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
  894. * @dev: pointer to device structure.
  895. * @attr: pointer to device_attribute structure.
  896. * @buf: pointer to char array to hold the output.
  897. */
  898. static ssize_t
  899. ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
  900. struct device_attribute *attr, char *buf)
  901. {
  902. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  903. u8 ctrl4a;
  904. ds1685_rtc_switch_to_bank1(rtc);
  905. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  906. ds1685_rtc_switch_to_bank0(rtc);
  907. return sprintf(buf, "%s\n",
  908. (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
  909. }
  910. static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
  911. /**
  912. * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
  913. * @dev: pointer to device structure.
  914. * @attr: pointer to device_attribute structure.
  915. * @buf: pointer to char array to hold the output.
  916. */
  917. static ssize_t
  918. ds1685_rtc_sysfs_serial_show(struct device *dev,
  919. struct device_attribute *attr, char *buf)
  920. {
  921. struct ds1685_priv *rtc = dev_get_drvdata(dev);
  922. u8 ssn[8];
  923. ds1685_rtc_switch_to_bank1(rtc);
  924. ds1685_rtc_get_ssn(rtc, ssn);
  925. ds1685_rtc_switch_to_bank0(rtc);
  926. return sprintf(buf, "%8phC\n", ssn);
  927. }
  928. static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
  929. /**
  930. * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
  931. */
  932. static struct attribute*
  933. ds1685_rtc_sysfs_misc_attrs[] = {
  934. &dev_attr_battery.attr,
  935. &dev_attr_auxbatt.attr,
  936. &dev_attr_serial.attr,
  937. NULL,
  938. };
  939. /**
  940. * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
  941. */
  942. static const struct attribute_group
  943. ds1685_rtc_sysfs_misc_grp = {
  944. .name = "misc",
  945. .attrs = ds1685_rtc_sysfs_misc_attrs,
  946. };
  947. /* ----------------------------------------------------------------------- */
  948. /* Driver Probe/Removal */
  949. /**
  950. * ds1685_rtc_probe - initializes rtc driver.
  951. * @pdev: pointer to platform_device structure.
  952. */
  953. static int
  954. ds1685_rtc_probe(struct platform_device *pdev)
  955. {
  956. struct rtc_device *rtc_dev;
  957. struct resource *res;
  958. struct ds1685_priv *rtc;
  959. struct ds1685_rtc_platform_data *pdata;
  960. u8 ctrla, ctrlb, hours;
  961. unsigned char am_pm;
  962. int ret = 0;
  963. struct nvmem_config nvmem_cfg = {
  964. .name = "ds1685_nvram",
  965. .size = NVRAM_TOTAL_SZ,
  966. .reg_read = ds1685_nvram_read,
  967. .reg_write = ds1685_nvram_write,
  968. };
  969. /* Get the platform data. */
  970. pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
  971. if (!pdata)
  972. return -ENODEV;
  973. /* Allocate memory for the rtc device. */
  974. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  975. if (!rtc)
  976. return -ENOMEM;
  977. /*
  978. * Allocate/setup any IORESOURCE_MEM resources, if required. Not all
  979. * platforms put the RTC in an easy-access place. Like the SGI Octane,
  980. * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip
  981. * that sits behind the IOC3 PCI metadevice.
  982. */
  983. if (pdata->alloc_io_resources) {
  984. /* Get the platform resources. */
  985. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  986. if (!res)
  987. return -ENXIO;
  988. rtc->size = resource_size(res);
  989. /* Request a memory region. */
  990. /* XXX: mmio-only for now. */
  991. if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
  992. pdev->name))
  993. return -EBUSY;
  994. /*
  995. * Set the base address for the rtc, and ioremap its
  996. * registers.
  997. */
  998. rtc->baseaddr = res->start;
  999. rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
  1000. if (!rtc->regs)
  1001. return -ENOMEM;
  1002. }
  1003. rtc->alloc_io_resources = pdata->alloc_io_resources;
  1004. /* Get the register step size. */
  1005. if (pdata->regstep > 0)
  1006. rtc->regstep = pdata->regstep;
  1007. else
  1008. rtc->regstep = 1;
  1009. /* Platform read function, else default if mmio setup */
  1010. if (pdata->plat_read)
  1011. rtc->read = pdata->plat_read;
  1012. else
  1013. if (pdata->alloc_io_resources)
  1014. rtc->read = ds1685_read;
  1015. else
  1016. return -ENXIO;
  1017. /* Platform write function, else default if mmio setup */
  1018. if (pdata->plat_write)
  1019. rtc->write = pdata->plat_write;
  1020. else
  1021. if (pdata->alloc_io_resources)
  1022. rtc->write = ds1685_write;
  1023. else
  1024. return -ENXIO;
  1025. /* Platform pre-shutdown function, if defined. */
  1026. if (pdata->plat_prepare_poweroff)
  1027. rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
  1028. /* Platform wake_alarm function, if defined. */
  1029. if (pdata->plat_wake_alarm)
  1030. rtc->wake_alarm = pdata->plat_wake_alarm;
  1031. /* Platform post_ram_clear function, if defined. */
  1032. if (pdata->plat_post_ram_clear)
  1033. rtc->post_ram_clear = pdata->plat_post_ram_clear;
  1034. /* Init the spinlock, workqueue, & set the driver data. */
  1035. spin_lock_init(&rtc->lock);
  1036. INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
  1037. platform_set_drvdata(pdev, rtc);
  1038. /* Turn the oscillator on if is not already on (DV1 = 1). */
  1039. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1040. if (!(ctrla & RTC_CTRL_A_DV1))
  1041. ctrla |= RTC_CTRL_A_DV1;
  1042. /* Enable the countdown chain (DV2 = 0) */
  1043. ctrla &= ~(RTC_CTRL_A_DV2);
  1044. /* Clear RS3-RS0 in Control A. */
  1045. ctrla &= ~(RTC_CTRL_A_RS_MASK);
  1046. /*
  1047. * All done with Control A. Switch to Bank 1 for the remainder of
  1048. * the RTC setup so we have access to the extended functions.
  1049. */
  1050. ctrla |= RTC_CTRL_A_DV0;
  1051. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1052. /* Default to 32768kHz output. */
  1053. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1054. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
  1055. /* Set the SET bit in Control B so we can do some housekeeping. */
  1056. rtc->write(rtc, RTC_CTRL_B,
  1057. (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
  1058. /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */
  1059. while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
  1060. cpu_relax();
  1061. /*
  1062. * If the platform supports BCD mode, then set DM=0 in Control B.
  1063. * Otherwise, set DM=1 for BIN mode.
  1064. */
  1065. ctrlb = rtc->read(rtc, RTC_CTRL_B);
  1066. if (pdata->bcd_mode)
  1067. ctrlb &= ~(RTC_CTRL_B_DM);
  1068. else
  1069. ctrlb |= RTC_CTRL_B_DM;
  1070. rtc->bcd_mode = pdata->bcd_mode;
  1071. /*
  1072. * Disable Daylight Savings Time (DSE = 0).
  1073. * The RTC has hardcoded timezone information that is rendered
  1074. * obselete. We'll let the OS deal with DST settings instead.
  1075. */
  1076. if (ctrlb & RTC_CTRL_B_DSE)
  1077. ctrlb &= ~(RTC_CTRL_B_DSE);
  1078. /* Force 24-hour mode (2412 = 1). */
  1079. if (!(ctrlb & RTC_CTRL_B_2412)) {
  1080. /* Reinitialize the time hours. */
  1081. hours = rtc->read(rtc, RTC_HRS);
  1082. am_pm = hours & RTC_HRS_AMPM_MASK;
  1083. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1084. RTC_HRS_12_BIN_MASK);
  1085. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1086. /* Enable 24-hour mode. */
  1087. ctrlb |= RTC_CTRL_B_2412;
  1088. /* Write back to Control B, including DM & DSE bits. */
  1089. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1090. /* Write the time hours back. */
  1091. rtc->write(rtc, RTC_HRS,
  1092. ds1685_rtc_bin2bcd(rtc, hours,
  1093. RTC_HRS_24_BIN_MASK,
  1094. RTC_HRS_24_BCD_MASK));
  1095. /* Reinitialize the alarm hours. */
  1096. hours = rtc->read(rtc, RTC_HRS_ALARM);
  1097. am_pm = hours & RTC_HRS_AMPM_MASK;
  1098. hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
  1099. RTC_HRS_12_BIN_MASK);
  1100. hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
  1101. /* Write the alarm hours back. */
  1102. rtc->write(rtc, RTC_HRS_ALARM,
  1103. ds1685_rtc_bin2bcd(rtc, hours,
  1104. RTC_HRS_24_BIN_MASK,
  1105. RTC_HRS_24_BCD_MASK));
  1106. } else {
  1107. /* 24-hour mode is already set, so write Control B back. */
  1108. rtc->write(rtc, RTC_CTRL_B, ctrlb);
  1109. }
  1110. /* Unset the SET bit in Control B so the RTC can update. */
  1111. rtc->write(rtc, RTC_CTRL_B,
  1112. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
  1113. /* Check the main battery. */
  1114. if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
  1115. dev_warn(&pdev->dev,
  1116. "Main battery is exhausted! RTC may be invalid!\n");
  1117. /* Check the auxillary battery. It is optional. */
  1118. if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
  1119. dev_warn(&pdev->dev,
  1120. "Aux battery is exhausted or not available.\n");
  1121. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1122. rtc->write(rtc, RTC_CTRL_B,
  1123. (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
  1124. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1125. rtc->read(rtc, RTC_CTRL_C);
  1126. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1127. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1128. (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
  1129. /* Clear RF/WF/KF in Ctrl 4A. */
  1130. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1131. (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
  1132. /*
  1133. * Re-enable KSE to handle power button events. We do not enable
  1134. * WIE or RIE by default.
  1135. */
  1136. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1137. (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
  1138. rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  1139. if (IS_ERR(rtc_dev))
  1140. return PTR_ERR(rtc_dev);
  1141. rtc_dev->ops = &ds1685_rtc_ops;
  1142. /* Century bit is useless because leap year fails in 1900 and 2100 */
  1143. rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000;
  1144. rtc_dev->range_max = RTC_TIMESTAMP_END_2099;
  1145. /* Maximum periodic rate is 8192Hz (0.122070ms). */
  1146. rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
  1147. /* See if the platform doesn't support UIE. */
  1148. if (pdata->uie_unsupported)
  1149. rtc_dev->uie_unsupported = 1;
  1150. rtc->uie_unsupported = pdata->uie_unsupported;
  1151. rtc->dev = rtc_dev;
  1152. /*
  1153. * Fetch the IRQ and setup the interrupt handler.
  1154. *
  1155. * Not all platforms have the IRQF pin tied to something. If not, the
  1156. * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but
  1157. * there won't be an automatic way of notifying the kernel about it,
  1158. * unless ctrlc is explicitly polled.
  1159. */
  1160. if (!pdata->no_irq) {
  1161. ret = platform_get_irq(pdev, 0);
  1162. if (ret > 0) {
  1163. rtc->irq_num = ret;
  1164. /* Request an IRQ. */
  1165. ret = devm_request_irq(&pdev->dev, rtc->irq_num,
  1166. ds1685_rtc_irq_handler,
  1167. IRQF_SHARED, pdev->name, pdev);
  1168. /* Check to see if something came back. */
  1169. if (unlikely(ret)) {
  1170. dev_warn(&pdev->dev,
  1171. "RTC interrupt not available\n");
  1172. rtc->irq_num = 0;
  1173. }
  1174. } else
  1175. return ret;
  1176. }
  1177. rtc->no_irq = pdata->no_irq;
  1178. /* Setup complete. */
  1179. ds1685_rtc_switch_to_bank0(rtc);
  1180. ret = rtc_add_group(rtc_dev, &ds1685_rtc_sysfs_misc_grp);
  1181. if (ret)
  1182. return ret;
  1183. rtc_dev->nvram_old_abi = true;
  1184. nvmem_cfg.priv = rtc;
  1185. ret = rtc_nvmem_register(rtc_dev, &nvmem_cfg);
  1186. if (ret)
  1187. return ret;
  1188. return rtc_register_device(rtc_dev);
  1189. }
  1190. /**
  1191. * ds1685_rtc_remove - removes rtc driver.
  1192. * @pdev: pointer to platform_device structure.
  1193. */
  1194. static int
  1195. ds1685_rtc_remove(struct platform_device *pdev)
  1196. {
  1197. struct ds1685_priv *rtc = platform_get_drvdata(pdev);
  1198. /* Read Ctrl B and clear PIE/AIE/UIE. */
  1199. rtc->write(rtc, RTC_CTRL_B,
  1200. (rtc->read(rtc, RTC_CTRL_B) &
  1201. ~(RTC_CTRL_B_PAU_MASK)));
  1202. /* Reading Ctrl C auto-clears PF/AF/UF. */
  1203. rtc->read(rtc, RTC_CTRL_C);
  1204. /* Read Ctrl 4B and clear RIE/WIE/KSE. */
  1205. rtc->write(rtc, RTC_EXT_CTRL_4B,
  1206. (rtc->read(rtc, RTC_EXT_CTRL_4B) &
  1207. ~(RTC_CTRL_4B_RWK_MASK)));
  1208. /* Manually clear RF/WF/KF in Ctrl 4A. */
  1209. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1210. (rtc->read(rtc, RTC_EXT_CTRL_4A) &
  1211. ~(RTC_CTRL_4A_RWK_MASK)));
  1212. cancel_work_sync(&rtc->work);
  1213. return 0;
  1214. }
  1215. /**
  1216. * ds1685_rtc_driver - rtc driver properties.
  1217. */
  1218. static struct platform_driver ds1685_rtc_driver = {
  1219. .driver = {
  1220. .name = "rtc-ds1685",
  1221. },
  1222. .probe = ds1685_rtc_probe,
  1223. .remove = ds1685_rtc_remove,
  1224. };
  1225. module_platform_driver(ds1685_rtc_driver);
  1226. /* ----------------------------------------------------------------------- */
  1227. /* ----------------------------------------------------------------------- */
  1228. /* Poweroff function */
  1229. /**
  1230. * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
  1231. * @pdev: pointer to platform_device structure.
  1232. */
  1233. void __noreturn
  1234. ds1685_rtc_poweroff(struct platform_device *pdev)
  1235. {
  1236. u8 ctrla, ctrl4a, ctrl4b;
  1237. struct ds1685_priv *rtc;
  1238. /* Check for valid RTC data, else, spin forever. */
  1239. if (unlikely(!pdev)) {
  1240. pr_emerg("platform device data not available, spinning forever ...\n");
  1241. while(1);
  1242. unreachable();
  1243. } else {
  1244. /* Get the rtc data. */
  1245. rtc = platform_get_drvdata(pdev);
  1246. /*
  1247. * Disable our IRQ. We're powering down, so we're not
  1248. * going to worry about cleaning up. Most of that should
  1249. * have been taken care of by the shutdown scripts and this
  1250. * is the final function call.
  1251. */
  1252. if (!rtc->no_irq)
  1253. disable_irq_nosync(rtc->irq_num);
  1254. /* Oscillator must be on and the countdown chain enabled. */
  1255. ctrla = rtc->read(rtc, RTC_CTRL_A);
  1256. ctrla |= RTC_CTRL_A_DV1;
  1257. ctrla &= ~(RTC_CTRL_A_DV2);
  1258. rtc->write(rtc, RTC_CTRL_A, ctrla);
  1259. /*
  1260. * Read Control 4A and check the status of the auxillary
  1261. * battery. This must be present and working (VRT2 = 1)
  1262. * for wakeup and kickstart functionality to be useful.
  1263. */
  1264. ds1685_rtc_switch_to_bank1(rtc);
  1265. ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
  1266. if (ctrl4a & RTC_CTRL_4A_VRT2) {
  1267. /* Clear all of the interrupt flags on Control 4A. */
  1268. ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
  1269. rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
  1270. /*
  1271. * The auxillary battery is present and working.
  1272. * Enable extended functions (ABE=1), enable
  1273. * wake-up (WIE=1), and enable kickstart (KSE=1)
  1274. * in Control 4B.
  1275. */
  1276. ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
  1277. ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
  1278. RTC_CTRL_4B_KSE);
  1279. rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
  1280. }
  1281. /* Set PAB to 1 in Control 4A to power the system down. */
  1282. dev_warn(&pdev->dev, "Powerdown.\n");
  1283. msleep(20);
  1284. rtc->write(rtc, RTC_EXT_CTRL_4A,
  1285. (ctrl4a | RTC_CTRL_4A_PAB));
  1286. /* Spin ... we do not switch back to bank0. */
  1287. while(1);
  1288. unreachable();
  1289. }
  1290. }
  1291. EXPORT_SYMBOL(ds1685_rtc_poweroff);
  1292. /* ----------------------------------------------------------------------- */
  1293. MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
  1294. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
  1295. MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
  1296. MODULE_LICENSE("GPL");
  1297. MODULE_ALIAS("platform:rtc-ds1685");