interface.c 25 KB

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