interface.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * RTC subsystem, interface functions
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * based on arch/arm/common/rtctime.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/rtc.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <linux/log2.h>
  17. #include <linux/workqueue.h>
  18. #define CREATE_TRACE_POINTS
  19. #include <trace/events/rtc.h>
  20. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
  21. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
  22. static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
  23. {
  24. time64_t secs;
  25. if (!rtc->offset_secs)
  26. return;
  27. secs = rtc_tm_to_time64(tm);
  28. /*
  29. * Since the reading time values from RTC device are always in the RTC
  30. * original valid range, but we need to skip the overlapped region
  31. * between expanded range and original range, which is no need to add
  32. * the offset.
  33. */
  34. if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
  35. (rtc->start_secs < rtc->range_min &&
  36. secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
  37. return;
  38. rtc_time64_to_tm(secs + rtc->offset_secs, tm);
  39. }
  40. static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
  41. {
  42. time64_t secs;
  43. if (!rtc->offset_secs)
  44. return;
  45. secs = rtc_tm_to_time64(tm);
  46. /*
  47. * If the setting time values are in the valid range of RTC hardware
  48. * device, then no need to subtract the offset when setting time to RTC
  49. * device. Otherwise we need to subtract the offset to make the time
  50. * values are valid for RTC hardware device.
  51. */
  52. if (secs >= rtc->range_min && secs <= rtc->range_max)
  53. return;
  54. rtc_time64_to_tm(secs - rtc->offset_secs, tm);
  55. }
  56. static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
  57. {
  58. if (rtc->range_min != rtc->range_max) {
  59. time64_t time = rtc_tm_to_time64(tm);
  60. time64_t range_min = rtc->set_start_time ? rtc->start_secs :
  61. rtc->range_min;
  62. time64_t range_max = rtc->set_start_time ?
  63. (rtc->start_secs + rtc->range_max - rtc->range_min) :
  64. rtc->range_max;
  65. if (time < range_min || time > range_max)
  66. return -ERANGE;
  67. }
  68. return 0;
  69. }
  70. static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  71. {
  72. int err;
  73. if (!rtc->ops)
  74. err = -ENODEV;
  75. else if (!rtc->ops->read_time)
  76. err = -EINVAL;
  77. else {
  78. memset(tm, 0, sizeof(struct rtc_time));
  79. err = rtc->ops->read_time(rtc->dev.parent, tm);
  80. if (err < 0) {
  81. dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
  82. err);
  83. return err;
  84. }
  85. rtc_add_offset(rtc, tm);
  86. err = rtc_valid_tm(tm);
  87. if (err < 0)
  88. dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
  89. }
  90. return err;
  91. }
  92. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  93. {
  94. int err;
  95. err = mutex_lock_interruptible(&rtc->ops_lock);
  96. if (err)
  97. return err;
  98. err = __rtc_read_time(rtc, tm);
  99. mutex_unlock(&rtc->ops_lock);
  100. trace_rtc_read_time(rtc_tm_to_time64(tm), err);
  101. return err;
  102. }
  103. EXPORT_SYMBOL_GPL(rtc_read_time);
  104. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  105. {
  106. int err;
  107. err = rtc_valid_tm(tm);
  108. if (err != 0)
  109. return err;
  110. err = rtc_valid_range(rtc, tm);
  111. if (err)
  112. return err;
  113. rtc_subtract_offset(rtc, tm);
  114. err = mutex_lock_interruptible(&rtc->ops_lock);
  115. if (err)
  116. return err;
  117. if (!rtc->ops)
  118. err = -ENODEV;
  119. else if (rtc->ops->set_time)
  120. err = rtc->ops->set_time(rtc->dev.parent, tm);
  121. else if (rtc->ops->set_mmss64) {
  122. time64_t secs64 = rtc_tm_to_time64(tm);
  123. err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
  124. } else if (rtc->ops->set_mmss) {
  125. time64_t secs64 = rtc_tm_to_time64(tm);
  126. err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
  127. } else
  128. err = -EINVAL;
  129. pm_stay_awake(rtc->dev.parent);
  130. mutex_unlock(&rtc->ops_lock);
  131. /* A timer might have just expired */
  132. schedule_work(&rtc->irqwork);
  133. trace_rtc_set_time(rtc_tm_to_time64(tm), err);
  134. return err;
  135. }
  136. EXPORT_SYMBOL_GPL(rtc_set_time);
  137. static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  138. {
  139. int err;
  140. err = mutex_lock_interruptible(&rtc->ops_lock);
  141. if (err)
  142. return err;
  143. if (rtc->ops == NULL)
  144. err = -ENODEV;
  145. else if (!rtc->ops->read_alarm)
  146. err = -EINVAL;
  147. else {
  148. alarm->enabled = 0;
  149. alarm->pending = 0;
  150. alarm->time.tm_sec = -1;
  151. alarm->time.tm_min = -1;
  152. alarm->time.tm_hour = -1;
  153. alarm->time.tm_mday = -1;
  154. alarm->time.tm_mon = -1;
  155. alarm->time.tm_year = -1;
  156. alarm->time.tm_wday = -1;
  157. alarm->time.tm_yday = -1;
  158. alarm->time.tm_isdst = -1;
  159. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  160. }
  161. mutex_unlock(&rtc->ops_lock);
  162. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  163. return err;
  164. }
  165. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  166. {
  167. int err;
  168. struct rtc_time before, now;
  169. int first_time = 1;
  170. time64_t t_now, t_alm;
  171. enum { none, day, month, year } missing = none;
  172. unsigned days;
  173. /* The lower level RTC driver may return -1 in some fields,
  174. * creating invalid alarm->time values, for reasons like:
  175. *
  176. * - The hardware may not be capable of filling them in;
  177. * many alarms match only on time-of-day fields, not
  178. * day/month/year calendar data.
  179. *
  180. * - Some hardware uses illegal values as "wildcard" match
  181. * values, which non-Linux firmware (like a BIOS) may try
  182. * to set up as e.g. "alarm 15 minutes after each hour".
  183. * Linux uses only oneshot alarms.
  184. *
  185. * When we see that here, we deal with it by using values from
  186. * a current RTC timestamp for any missing (-1) values. The
  187. * RTC driver prevents "periodic alarm" modes.
  188. *
  189. * But this can be racey, because some fields of the RTC timestamp
  190. * may have wrapped in the interval since we read the RTC alarm,
  191. * which would lead to us inserting inconsistent values in place
  192. * of the -1 fields.
  193. *
  194. * Reading the alarm and timestamp in the reverse sequence
  195. * would have the same race condition, and not solve the issue.
  196. *
  197. * So, we must first read the RTC timestamp,
  198. * then read the RTC alarm value,
  199. * and then read a second RTC timestamp.
  200. *
  201. * If any fields of the second timestamp have changed
  202. * when compared with the first timestamp, then we know
  203. * our timestamp may be inconsistent with that used by
  204. * the low-level rtc_read_alarm_internal() function.
  205. *
  206. * So, when the two timestamps disagree, we just loop and do
  207. * the process again to get a fully consistent set of values.
  208. *
  209. * This could all instead be done in the lower level driver,
  210. * but since more than one lower level RTC implementation needs it,
  211. * then it's probably best best to do it here instead of there..
  212. */
  213. /* Get the "before" timestamp */
  214. err = rtc_read_time(rtc, &before);
  215. if (err < 0)
  216. return err;
  217. do {
  218. if (!first_time)
  219. memcpy(&before, &now, sizeof(struct rtc_time));
  220. first_time = 0;
  221. /* get the RTC alarm values, which may be incomplete */
  222. err = rtc_read_alarm_internal(rtc, alarm);
  223. if (err)
  224. return err;
  225. /* full-function RTCs won't have such missing fields */
  226. if (rtc_valid_tm(&alarm->time) == 0)
  227. return 0;
  228. /* get the "after" timestamp, to detect wrapped fields */
  229. err = rtc_read_time(rtc, &now);
  230. if (err < 0)
  231. return err;
  232. /* note that tm_sec is a "don't care" value here: */
  233. } while ( before.tm_min != now.tm_min
  234. || before.tm_hour != now.tm_hour
  235. || before.tm_mon != now.tm_mon
  236. || before.tm_year != now.tm_year);
  237. /* Fill in the missing alarm fields using the timestamp; we
  238. * know there's at least one since alarm->time is invalid.
  239. */
  240. if (alarm->time.tm_sec == -1)
  241. alarm->time.tm_sec = now.tm_sec;
  242. if (alarm->time.tm_min == -1)
  243. alarm->time.tm_min = now.tm_min;
  244. if (alarm->time.tm_hour == -1)
  245. alarm->time.tm_hour = now.tm_hour;
  246. /* For simplicity, only support date rollover for now */
  247. if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
  248. alarm->time.tm_mday = now.tm_mday;
  249. missing = day;
  250. }
  251. if ((unsigned)alarm->time.tm_mon >= 12) {
  252. alarm->time.tm_mon = now.tm_mon;
  253. if (missing == none)
  254. missing = month;
  255. }
  256. if (alarm->time.tm_year == -1) {
  257. alarm->time.tm_year = now.tm_year;
  258. if (missing == none)
  259. missing = year;
  260. }
  261. /* Can't proceed if alarm is still invalid after replacing
  262. * missing fields.
  263. */
  264. err = rtc_valid_tm(&alarm->time);
  265. if (err)
  266. goto done;
  267. /* with luck, no rollover is needed */
  268. t_now = rtc_tm_to_time64(&now);
  269. t_alm = rtc_tm_to_time64(&alarm->time);
  270. if (t_now < t_alm)
  271. goto done;
  272. switch (missing) {
  273. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  274. * that will trigger at 5am will do so at 5am Tuesday, which
  275. * could also be in the next month or year. This is a common
  276. * case, especially for PCs.
  277. */
  278. case day:
  279. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  280. t_alm += 24 * 60 * 60;
  281. rtc_time64_to_tm(t_alm, &alarm->time);
  282. break;
  283. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  284. * be next month. An alarm matching on the 30th, 29th, or 28th
  285. * may end up in the month after that! Many newer PCs support
  286. * this type of alarm.
  287. */
  288. case month:
  289. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  290. do {
  291. if (alarm->time.tm_mon < 11)
  292. alarm->time.tm_mon++;
  293. else {
  294. alarm->time.tm_mon = 0;
  295. alarm->time.tm_year++;
  296. }
  297. days = rtc_month_days(alarm->time.tm_mon,
  298. alarm->time.tm_year);
  299. } while (days < alarm->time.tm_mday);
  300. break;
  301. /* Year rollover ... easy except for leap years! */
  302. case year:
  303. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  304. do {
  305. alarm->time.tm_year++;
  306. } while (!is_leap_year(alarm->time.tm_year + 1900)
  307. && rtc_valid_tm(&alarm->time) != 0);
  308. break;
  309. default:
  310. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  311. }
  312. err = rtc_valid_tm(&alarm->time);
  313. done:
  314. if (err) {
  315. dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
  316. alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
  317. alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
  318. alarm->time.tm_sec);
  319. }
  320. return err;
  321. }
  322. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  323. {
  324. int err;
  325. err = mutex_lock_interruptible(&rtc->ops_lock);
  326. if (err)
  327. return err;
  328. if (rtc->ops == NULL)
  329. err = -ENODEV;
  330. else if (!rtc->ops->read_alarm)
  331. err = -EINVAL;
  332. else {
  333. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  334. alarm->enabled = rtc->aie_timer.enabled;
  335. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  336. }
  337. mutex_unlock(&rtc->ops_lock);
  338. trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
  339. return err;
  340. }
  341. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  342. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  343. {
  344. struct rtc_time tm;
  345. time64_t now, scheduled;
  346. int err;
  347. err = rtc_valid_tm(&alarm->time);
  348. if (err)
  349. return err;
  350. rtc_subtract_offset(rtc, &alarm->time);
  351. scheduled = rtc_tm_to_time64(&alarm->time);
  352. /* Make sure we're not setting alarms in the past */
  353. err = __rtc_read_time(rtc, &tm);
  354. if (err)
  355. return err;
  356. now = rtc_tm_to_time64(&tm);
  357. if (scheduled <= now)
  358. return -ETIME;
  359. /*
  360. * XXX - We just checked to make sure the alarm time is not
  361. * in the past, but there is still a race window where if
  362. * the is alarm set for the next second and the second ticks
  363. * over right here, before we set the alarm.
  364. */
  365. if (!rtc->ops)
  366. err = -ENODEV;
  367. else if (!rtc->ops->set_alarm)
  368. err = -EINVAL;
  369. else
  370. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  371. trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
  372. return err;
  373. }
  374. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  375. {
  376. int err;
  377. err = rtc_valid_tm(&alarm->time);
  378. if (err != 0)
  379. return err;
  380. err = rtc_valid_range(rtc, &alarm->time);
  381. if (err)
  382. return err;
  383. err = mutex_lock_interruptible(&rtc->ops_lock);
  384. if (err)
  385. return err;
  386. if (rtc->aie_timer.enabled)
  387. rtc_timer_remove(rtc, &rtc->aie_timer);
  388. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  389. rtc->aie_timer.period = 0;
  390. if (alarm->enabled)
  391. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  392. mutex_unlock(&rtc->ops_lock);
  393. rtc_add_offset(rtc, &alarm->time);
  394. return err;
  395. }
  396. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  397. /* Called once per device from rtc_device_register */
  398. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  399. {
  400. int err;
  401. struct rtc_time now;
  402. err = rtc_valid_tm(&alarm->time);
  403. if (err != 0)
  404. return err;
  405. err = rtc_read_time(rtc, &now);
  406. if (err)
  407. return err;
  408. err = mutex_lock_interruptible(&rtc->ops_lock);
  409. if (err)
  410. return err;
  411. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  412. rtc->aie_timer.period = 0;
  413. /* Alarm has to be enabled & in the future for us to enqueue it */
  414. if (alarm->enabled && (rtc_tm_to_ktime(now) <
  415. rtc->aie_timer.node.expires)) {
  416. rtc->aie_timer.enabled = 1;
  417. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  418. trace_rtc_timer_enqueue(&rtc->aie_timer);
  419. }
  420. mutex_unlock(&rtc->ops_lock);
  421. return err;
  422. }
  423. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  424. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  425. {
  426. int err = mutex_lock_interruptible(&rtc->ops_lock);
  427. if (err)
  428. return err;
  429. if (rtc->aie_timer.enabled != enabled) {
  430. if (enabled)
  431. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  432. else
  433. rtc_timer_remove(rtc, &rtc->aie_timer);
  434. }
  435. if (err)
  436. /* nothing */;
  437. else if (!rtc->ops)
  438. err = -ENODEV;
  439. else if (!rtc->ops->alarm_irq_enable)
  440. err = -EINVAL;
  441. else
  442. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  443. mutex_unlock(&rtc->ops_lock);
  444. trace_rtc_alarm_irq_enable(enabled, err);
  445. return err;
  446. }
  447. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  448. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  449. {
  450. int err = mutex_lock_interruptible(&rtc->ops_lock);
  451. if (err)
  452. return err;
  453. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  454. if (enabled == 0 && rtc->uie_irq_active) {
  455. mutex_unlock(&rtc->ops_lock);
  456. return rtc_dev_update_irq_enable_emul(rtc, 0);
  457. }
  458. #endif
  459. /* make sure we're changing state */
  460. if (rtc->uie_rtctimer.enabled == enabled)
  461. goto out;
  462. if (rtc->uie_unsupported) {
  463. err = -EINVAL;
  464. goto out;
  465. }
  466. if (enabled) {
  467. struct rtc_time tm;
  468. ktime_t now, onesec;
  469. __rtc_read_time(rtc, &tm);
  470. onesec = ktime_set(1, 0);
  471. now = rtc_tm_to_ktime(tm);
  472. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  473. rtc->uie_rtctimer.period = ktime_set(1, 0);
  474. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  475. } else
  476. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  477. out:
  478. mutex_unlock(&rtc->ops_lock);
  479. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  480. /*
  481. * Enable emulation if the driver did not provide
  482. * the update_irq_enable function pointer or if returned
  483. * -EINVAL to signal that it has been configured without
  484. * interrupts or that are not available at the moment.
  485. */
  486. if (err == -EINVAL)
  487. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  488. #endif
  489. return err;
  490. }
  491. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  492. /**
  493. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  494. * @rtc: pointer to the rtc device
  495. *
  496. * This function is called when an AIE, UIE or PIE mode interrupt
  497. * has occurred (or been emulated).
  498. *
  499. * Triggers the registered irq_task function callback.
  500. */
  501. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  502. {
  503. unsigned long flags;
  504. /* mark one irq of the appropriate mode */
  505. spin_lock_irqsave(&rtc->irq_lock, flags);
  506. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  507. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  508. /* call the task func */
  509. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  510. if (rtc->irq_task)
  511. rtc->irq_task->func(rtc->irq_task->private_data);
  512. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  513. wake_up_interruptible(&rtc->irq_queue);
  514. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  515. }
  516. /**
  517. * rtc_aie_update_irq - AIE mode rtctimer hook
  518. * @private: pointer to the rtc_device
  519. *
  520. * This functions is called when the aie_timer expires.
  521. */
  522. void rtc_aie_update_irq(void *private)
  523. {
  524. struct rtc_device *rtc = (struct rtc_device *)private;
  525. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  526. }
  527. /**
  528. * rtc_uie_update_irq - UIE mode rtctimer hook
  529. * @private: pointer to the rtc_device
  530. *
  531. * This functions is called when the uie_timer expires.
  532. */
  533. void rtc_uie_update_irq(void *private)
  534. {
  535. struct rtc_device *rtc = (struct rtc_device *)private;
  536. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  537. }
  538. /**
  539. * rtc_pie_update_irq - PIE mode hrtimer hook
  540. * @timer: pointer to the pie mode hrtimer
  541. *
  542. * This function is used to emulate PIE mode interrupts
  543. * using an hrtimer. This function is called when the periodic
  544. * hrtimer expires.
  545. */
  546. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  547. {
  548. struct rtc_device *rtc;
  549. ktime_t period;
  550. int count;
  551. rtc = container_of(timer, struct rtc_device, pie_timer);
  552. period = NSEC_PER_SEC / rtc->irq_freq;
  553. count = hrtimer_forward_now(timer, period);
  554. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  555. return HRTIMER_RESTART;
  556. }
  557. /**
  558. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  559. * @rtc: the rtc device
  560. * @num: how many irqs are being reported (usually one)
  561. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  562. * Context: any
  563. */
  564. void rtc_update_irq(struct rtc_device *rtc,
  565. unsigned long num, unsigned long events)
  566. {
  567. if (IS_ERR_OR_NULL(rtc))
  568. return;
  569. pm_stay_awake(rtc->dev.parent);
  570. schedule_work(&rtc->irqwork);
  571. }
  572. EXPORT_SYMBOL_GPL(rtc_update_irq);
  573. static int __rtc_match(struct device *dev, const void *data)
  574. {
  575. const char *name = data;
  576. if (strcmp(dev_name(dev), name) == 0)
  577. return 1;
  578. return 0;
  579. }
  580. struct rtc_device *rtc_class_open(const char *name)
  581. {
  582. struct device *dev;
  583. struct rtc_device *rtc = NULL;
  584. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  585. if (dev)
  586. rtc = to_rtc_device(dev);
  587. if (rtc) {
  588. if (!try_module_get(rtc->owner)) {
  589. put_device(dev);
  590. rtc = NULL;
  591. }
  592. }
  593. return rtc;
  594. }
  595. EXPORT_SYMBOL_GPL(rtc_class_open);
  596. void rtc_class_close(struct rtc_device *rtc)
  597. {
  598. module_put(rtc->owner);
  599. put_device(&rtc->dev);
  600. }
  601. EXPORT_SYMBOL_GPL(rtc_class_close);
  602. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  603. {
  604. int retval = -EBUSY;
  605. if (task == NULL || task->func == NULL)
  606. return -EINVAL;
  607. /* Cannot register while the char dev is in use */
  608. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  609. return -EBUSY;
  610. spin_lock_irq(&rtc->irq_task_lock);
  611. if (rtc->irq_task == NULL) {
  612. rtc->irq_task = task;
  613. retval = 0;
  614. }
  615. spin_unlock_irq(&rtc->irq_task_lock);
  616. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  617. return retval;
  618. }
  619. EXPORT_SYMBOL_GPL(rtc_irq_register);
  620. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  621. {
  622. spin_lock_irq(&rtc->irq_task_lock);
  623. if (rtc->irq_task == task)
  624. rtc->irq_task = NULL;
  625. spin_unlock_irq(&rtc->irq_task_lock);
  626. }
  627. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  628. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  629. {
  630. /*
  631. * We always cancel the timer here first, because otherwise
  632. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  633. * when we manage to start the timer before the callback
  634. * returns HRTIMER_RESTART.
  635. *
  636. * We cannot use hrtimer_cancel() here as a running callback
  637. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  638. * would spin forever.
  639. */
  640. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  641. return -1;
  642. if (enabled) {
  643. ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
  644. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  645. }
  646. return 0;
  647. }
  648. /**
  649. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  650. * @rtc: the rtc device
  651. * @task: currently registered with rtc_irq_register()
  652. * @enabled: true to enable periodic IRQs
  653. * Context: any
  654. *
  655. * Note that rtc_irq_set_freq() should previously have been used to
  656. * specify the desired frequency of periodic IRQ task->func() callbacks.
  657. */
  658. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  659. {
  660. int err = 0;
  661. unsigned long flags;
  662. retry:
  663. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  664. if (rtc->irq_task != NULL && task == NULL)
  665. err = -EBUSY;
  666. else if (rtc->irq_task != task)
  667. err = -EACCES;
  668. else {
  669. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  670. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  671. cpu_relax();
  672. goto retry;
  673. }
  674. rtc->pie_enabled = enabled;
  675. }
  676. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  677. trace_rtc_irq_set_state(enabled, err);
  678. return err;
  679. }
  680. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  681. /**
  682. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  683. * @rtc: the rtc device
  684. * @task: currently registered with rtc_irq_register()
  685. * @freq: positive frequency with which task->func() will be called
  686. * Context: any
  687. *
  688. * Note that rtc_irq_set_state() is used to enable or disable the
  689. * periodic IRQs.
  690. */
  691. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  692. {
  693. int err = 0;
  694. unsigned long flags;
  695. if (freq <= 0 || freq > RTC_MAX_FREQ)
  696. return -EINVAL;
  697. retry:
  698. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  699. if (rtc->irq_task != NULL && task == NULL)
  700. err = -EBUSY;
  701. else if (rtc->irq_task != task)
  702. err = -EACCES;
  703. else {
  704. rtc->irq_freq = freq;
  705. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  706. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  707. cpu_relax();
  708. goto retry;
  709. }
  710. }
  711. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  712. trace_rtc_irq_set_freq(freq, err);
  713. return err;
  714. }
  715. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  716. /**
  717. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  718. * @rtc rtc device
  719. * @timer timer being added.
  720. *
  721. * Enqueues a timer onto the rtc devices timerqueue and sets
  722. * the next alarm event appropriately.
  723. *
  724. * Sets the enabled bit on the added timer.
  725. *
  726. * Must hold ops_lock for proper serialization of timerqueue
  727. */
  728. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  729. {
  730. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  731. struct rtc_time tm;
  732. ktime_t now;
  733. timer->enabled = 1;
  734. __rtc_read_time(rtc, &tm);
  735. now = rtc_tm_to_ktime(tm);
  736. /* Skip over expired timers */
  737. while (next) {
  738. if (next->expires >= now)
  739. break;
  740. next = timerqueue_iterate_next(next);
  741. }
  742. timerqueue_add(&rtc->timerqueue, &timer->node);
  743. trace_rtc_timer_enqueue(timer);
  744. if (!next || ktime_before(timer->node.expires, next->expires)) {
  745. struct rtc_wkalrm alarm;
  746. int err;
  747. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  748. alarm.enabled = 1;
  749. err = __rtc_set_alarm(rtc, &alarm);
  750. if (err == -ETIME) {
  751. pm_stay_awake(rtc->dev.parent);
  752. schedule_work(&rtc->irqwork);
  753. } else if (err) {
  754. timerqueue_del(&rtc->timerqueue, &timer->node);
  755. trace_rtc_timer_dequeue(timer);
  756. timer->enabled = 0;
  757. return err;
  758. }
  759. }
  760. return 0;
  761. }
  762. static void rtc_alarm_disable(struct rtc_device *rtc)
  763. {
  764. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  765. return;
  766. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  767. trace_rtc_alarm_irq_enable(0, 0);
  768. }
  769. /**
  770. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  771. * @rtc rtc device
  772. * @timer timer being removed.
  773. *
  774. * Removes a timer onto the rtc devices timerqueue and sets
  775. * the next alarm event appropriately.
  776. *
  777. * Clears the enabled bit on the removed timer.
  778. *
  779. * Must hold ops_lock for proper serialization of timerqueue
  780. */
  781. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  782. {
  783. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  784. timerqueue_del(&rtc->timerqueue, &timer->node);
  785. trace_rtc_timer_dequeue(timer);
  786. timer->enabled = 0;
  787. if (next == &timer->node) {
  788. struct rtc_wkalrm alarm;
  789. int err;
  790. next = timerqueue_getnext(&rtc->timerqueue);
  791. if (!next) {
  792. rtc_alarm_disable(rtc);
  793. return;
  794. }
  795. alarm.time = rtc_ktime_to_tm(next->expires);
  796. alarm.enabled = 1;
  797. err = __rtc_set_alarm(rtc, &alarm);
  798. if (err == -ETIME) {
  799. pm_stay_awake(rtc->dev.parent);
  800. schedule_work(&rtc->irqwork);
  801. }
  802. }
  803. }
  804. /**
  805. * rtc_timer_do_work - Expires rtc timers
  806. * @rtc rtc device
  807. * @timer timer being removed.
  808. *
  809. * Expires rtc timers. Reprograms next alarm event if needed.
  810. * Called via worktask.
  811. *
  812. * Serializes access to timerqueue via ops_lock mutex
  813. */
  814. void rtc_timer_do_work(struct work_struct *work)
  815. {
  816. struct rtc_timer *timer;
  817. struct timerqueue_node *next;
  818. ktime_t now;
  819. struct rtc_time tm;
  820. struct rtc_device *rtc =
  821. container_of(work, struct rtc_device, irqwork);
  822. mutex_lock(&rtc->ops_lock);
  823. again:
  824. __rtc_read_time(rtc, &tm);
  825. now = rtc_tm_to_ktime(tm);
  826. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  827. if (next->expires > now)
  828. break;
  829. /* expire timer */
  830. timer = container_of(next, struct rtc_timer, node);
  831. timerqueue_del(&rtc->timerqueue, &timer->node);
  832. trace_rtc_timer_dequeue(timer);
  833. timer->enabled = 0;
  834. if (timer->task.func)
  835. timer->task.func(timer->task.private_data);
  836. trace_rtc_timer_fired(timer);
  837. /* Re-add/fwd periodic timers */
  838. if (ktime_to_ns(timer->period)) {
  839. timer->node.expires = ktime_add(timer->node.expires,
  840. timer->period);
  841. timer->enabled = 1;
  842. timerqueue_add(&rtc->timerqueue, &timer->node);
  843. trace_rtc_timer_enqueue(timer);
  844. }
  845. }
  846. /* Set next alarm */
  847. if (next) {
  848. struct rtc_wkalrm alarm;
  849. int err;
  850. int retry = 3;
  851. alarm.time = rtc_ktime_to_tm(next->expires);
  852. alarm.enabled = 1;
  853. reprogram:
  854. err = __rtc_set_alarm(rtc, &alarm);
  855. if (err == -ETIME)
  856. goto again;
  857. else if (err) {
  858. if (retry-- > 0)
  859. goto reprogram;
  860. timer = container_of(next, struct rtc_timer, node);
  861. timerqueue_del(&rtc->timerqueue, &timer->node);
  862. trace_rtc_timer_dequeue(timer);
  863. timer->enabled = 0;
  864. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  865. goto again;
  866. }
  867. } else
  868. rtc_alarm_disable(rtc);
  869. pm_relax(rtc->dev.parent);
  870. mutex_unlock(&rtc->ops_lock);
  871. }
  872. /* rtc_timer_init - Initializes an rtc_timer
  873. * @timer: timer to be intiialized
  874. * @f: function pointer to be called when timer fires
  875. * @data: private data passed to function pointer
  876. *
  877. * Kernel interface to initializing an rtc_timer.
  878. */
  879. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
  880. {
  881. timerqueue_init(&timer->node);
  882. timer->enabled = 0;
  883. timer->task.func = f;
  884. timer->task.private_data = data;
  885. }
  886. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  887. * @ rtc: rtc device to be used
  888. * @ timer: timer being set
  889. * @ expires: time at which to expire the timer
  890. * @ period: period that the timer will recur
  891. *
  892. * Kernel interface to set an rtc_timer
  893. */
  894. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  895. ktime_t expires, ktime_t period)
  896. {
  897. int ret = 0;
  898. mutex_lock(&rtc->ops_lock);
  899. if (timer->enabled)
  900. rtc_timer_remove(rtc, timer);
  901. timer->node.expires = expires;
  902. timer->period = period;
  903. ret = rtc_timer_enqueue(rtc, timer);
  904. mutex_unlock(&rtc->ops_lock);
  905. return ret;
  906. }
  907. /* rtc_timer_cancel - Stops an rtc_timer
  908. * @ rtc: rtc device to be used
  909. * @ timer: timer being set
  910. *
  911. * Kernel interface to cancel an rtc_timer
  912. */
  913. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  914. {
  915. mutex_lock(&rtc->ops_lock);
  916. if (timer->enabled)
  917. rtc_timer_remove(rtc, timer);
  918. mutex_unlock(&rtc->ops_lock);
  919. }
  920. /**
  921. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  922. * @ rtc: rtc device to be used
  923. * @ offset: the offset in parts per billion
  924. *
  925. * see below for details.
  926. *
  927. * Kernel interface to read rtc clock offset
  928. * Returns 0 on success, or a negative number on error.
  929. * If read_offset() is not implemented for the rtc, return -EINVAL
  930. */
  931. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  932. {
  933. int ret;
  934. if (!rtc->ops)
  935. return -ENODEV;
  936. if (!rtc->ops->read_offset)
  937. return -EINVAL;
  938. mutex_lock(&rtc->ops_lock);
  939. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  940. mutex_unlock(&rtc->ops_lock);
  941. trace_rtc_read_offset(*offset, ret);
  942. return ret;
  943. }
  944. /**
  945. * rtc_set_offset - Adjusts the duration of the average second
  946. * @ rtc: rtc device to be used
  947. * @ offset: the offset in parts per billion
  948. *
  949. * Some rtc's allow an adjustment to the average duration of a second
  950. * to compensate for differences in the actual clock rate due to temperature,
  951. * the crystal, capacitor, etc.
  952. *
  953. * The adjustment applied is as follows:
  954. * t = t0 * (1 + offset * 1e-9)
  955. * where t0 is the measured length of 1 RTC second with offset = 0
  956. *
  957. * Kernel interface to adjust an rtc clock offset.
  958. * Return 0 on success, or a negative number on error.
  959. * If the rtc offset is not setable (or not implemented), return -EINVAL
  960. */
  961. int rtc_set_offset(struct rtc_device *rtc, long offset)
  962. {
  963. int ret;
  964. if (!rtc->ops)
  965. return -ENODEV;
  966. if (!rtc->ops->set_offset)
  967. return -EINVAL;
  968. mutex_lock(&rtc->ops_lock);
  969. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  970. mutex_unlock(&rtc->ops_lock);
  971. trace_rtc_set_offset(offset, ret);
  972. return ret;
  973. }