interface.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. if (!rtc->ops)
  317. return -ENODEV;
  318. else if (!rtc->ops->set_alarm)
  319. return -EINVAL;
  320. err = rtc_valid_tm(&alarm->time);
  321. if (err != 0)
  322. return err;
  323. err = mutex_lock_interruptible(&rtc->ops_lock);
  324. if (err)
  325. return err;
  326. if (rtc->aie_timer.enabled)
  327. rtc_timer_remove(rtc, &rtc->aie_timer);
  328. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  329. rtc->aie_timer.period = ktime_set(0, 0);
  330. if (alarm->enabled)
  331. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  332. mutex_unlock(&rtc->ops_lock);
  333. return err;
  334. }
  335. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  336. /* Called once per device from rtc_device_register */
  337. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  338. {
  339. int err;
  340. struct rtc_time now;
  341. err = rtc_valid_tm(&alarm->time);
  342. if (err != 0)
  343. return err;
  344. err = rtc_read_time(rtc, &now);
  345. if (err)
  346. return err;
  347. err = mutex_lock_interruptible(&rtc->ops_lock);
  348. if (err)
  349. return err;
  350. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  351. rtc->aie_timer.period = ktime_set(0, 0);
  352. /* Alarm has to be enabled & in the future for us to enqueue it */
  353. if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
  354. rtc->aie_timer.node.expires.tv64)) {
  355. rtc->aie_timer.enabled = 1;
  356. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  357. }
  358. mutex_unlock(&rtc->ops_lock);
  359. return err;
  360. }
  361. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  362. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  363. {
  364. int err = mutex_lock_interruptible(&rtc->ops_lock);
  365. if (err)
  366. return err;
  367. if (rtc->aie_timer.enabled != enabled) {
  368. if (enabled)
  369. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  370. else
  371. rtc_timer_remove(rtc, &rtc->aie_timer);
  372. }
  373. if (err)
  374. /* nothing */;
  375. else if (!rtc->ops)
  376. err = -ENODEV;
  377. else if (!rtc->ops->alarm_irq_enable)
  378. err = -EINVAL;
  379. else
  380. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  381. mutex_unlock(&rtc->ops_lock);
  382. return err;
  383. }
  384. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  385. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  386. {
  387. int err = mutex_lock_interruptible(&rtc->ops_lock);
  388. if (err)
  389. return err;
  390. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  391. if (enabled == 0 && rtc->uie_irq_active) {
  392. mutex_unlock(&rtc->ops_lock);
  393. return rtc_dev_update_irq_enable_emul(rtc, 0);
  394. }
  395. #endif
  396. /* make sure we're changing state */
  397. if (rtc->uie_rtctimer.enabled == enabled)
  398. goto out;
  399. if (rtc->uie_unsupported) {
  400. err = -EINVAL;
  401. goto out;
  402. }
  403. if (enabled) {
  404. struct rtc_time tm;
  405. ktime_t now, onesec;
  406. __rtc_read_time(rtc, &tm);
  407. onesec = ktime_set(1, 0);
  408. now = rtc_tm_to_ktime(tm);
  409. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  410. rtc->uie_rtctimer.period = ktime_set(1, 0);
  411. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  412. } else
  413. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  414. out:
  415. mutex_unlock(&rtc->ops_lock);
  416. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  417. /*
  418. * Enable emulation if the driver did not provide
  419. * the update_irq_enable function pointer or if returned
  420. * -EINVAL to signal that it has been configured without
  421. * interrupts or that are not available at the moment.
  422. */
  423. if (err == -EINVAL)
  424. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  425. #endif
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  429. /**
  430. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  431. * @rtc: pointer to the rtc device
  432. *
  433. * This function is called when an AIE, UIE or PIE mode interrupt
  434. * has occurred (or been emulated).
  435. *
  436. * Triggers the registered irq_task function callback.
  437. */
  438. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  439. {
  440. unsigned long flags;
  441. /* mark one irq of the appropriate mode */
  442. spin_lock_irqsave(&rtc->irq_lock, flags);
  443. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  444. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  445. /* call the task func */
  446. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  447. if (rtc->irq_task)
  448. rtc->irq_task->func(rtc->irq_task->private_data);
  449. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  450. wake_up_interruptible(&rtc->irq_queue);
  451. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  452. }
  453. /**
  454. * rtc_aie_update_irq - AIE mode rtctimer hook
  455. * @private: pointer to the rtc_device
  456. *
  457. * This functions is called when the aie_timer expires.
  458. */
  459. void rtc_aie_update_irq(void *private)
  460. {
  461. struct rtc_device *rtc = (struct rtc_device *)private;
  462. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  463. }
  464. /**
  465. * rtc_uie_update_irq - UIE mode rtctimer hook
  466. * @private: pointer to the rtc_device
  467. *
  468. * This functions is called when the uie_timer expires.
  469. */
  470. void rtc_uie_update_irq(void *private)
  471. {
  472. struct rtc_device *rtc = (struct rtc_device *)private;
  473. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  474. }
  475. /**
  476. * rtc_pie_update_irq - PIE mode hrtimer hook
  477. * @timer: pointer to the pie mode hrtimer
  478. *
  479. * This function is used to emulate PIE mode interrupts
  480. * using an hrtimer. This function is called when the periodic
  481. * hrtimer expires.
  482. */
  483. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  484. {
  485. struct rtc_device *rtc;
  486. ktime_t period;
  487. int count;
  488. rtc = container_of(timer, struct rtc_device, pie_timer);
  489. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  490. count = hrtimer_forward_now(timer, period);
  491. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  492. return HRTIMER_RESTART;
  493. }
  494. /**
  495. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  496. * @rtc: the rtc device
  497. * @num: how many irqs are being reported (usually one)
  498. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  499. * Context: any
  500. */
  501. void rtc_update_irq(struct rtc_device *rtc,
  502. unsigned long num, unsigned long events)
  503. {
  504. if (IS_ERR_OR_NULL(rtc))
  505. return;
  506. pm_stay_awake(rtc->dev.parent);
  507. schedule_work(&rtc->irqwork);
  508. }
  509. EXPORT_SYMBOL_GPL(rtc_update_irq);
  510. static int __rtc_match(struct device *dev, const void *data)
  511. {
  512. const char *name = data;
  513. if (strcmp(dev_name(dev), name) == 0)
  514. return 1;
  515. return 0;
  516. }
  517. struct rtc_device *rtc_class_open(const char *name)
  518. {
  519. struct device *dev;
  520. struct rtc_device *rtc = NULL;
  521. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  522. if (dev)
  523. rtc = to_rtc_device(dev);
  524. if (rtc) {
  525. if (!try_module_get(rtc->owner)) {
  526. put_device(dev);
  527. rtc = NULL;
  528. }
  529. }
  530. return rtc;
  531. }
  532. EXPORT_SYMBOL_GPL(rtc_class_open);
  533. void rtc_class_close(struct rtc_device *rtc)
  534. {
  535. module_put(rtc->owner);
  536. put_device(&rtc->dev);
  537. }
  538. EXPORT_SYMBOL_GPL(rtc_class_close);
  539. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  540. {
  541. int retval = -EBUSY;
  542. if (task == NULL || task->func == NULL)
  543. return -EINVAL;
  544. /* Cannot register while the char dev is in use */
  545. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  546. return -EBUSY;
  547. spin_lock_irq(&rtc->irq_task_lock);
  548. if (rtc->irq_task == NULL) {
  549. rtc->irq_task = task;
  550. retval = 0;
  551. }
  552. spin_unlock_irq(&rtc->irq_task_lock);
  553. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  554. return retval;
  555. }
  556. EXPORT_SYMBOL_GPL(rtc_irq_register);
  557. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  558. {
  559. spin_lock_irq(&rtc->irq_task_lock);
  560. if (rtc->irq_task == task)
  561. rtc->irq_task = NULL;
  562. spin_unlock_irq(&rtc->irq_task_lock);
  563. }
  564. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  565. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  566. {
  567. /*
  568. * We always cancel the timer here first, because otherwise
  569. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  570. * when we manage to start the timer before the callback
  571. * returns HRTIMER_RESTART.
  572. *
  573. * We cannot use hrtimer_cancel() here as a running callback
  574. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  575. * would spin forever.
  576. */
  577. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  578. return -1;
  579. if (enabled) {
  580. ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
  581. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  582. }
  583. return 0;
  584. }
  585. /**
  586. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  587. * @rtc: the rtc device
  588. * @task: currently registered with rtc_irq_register()
  589. * @enabled: true to enable periodic IRQs
  590. * Context: any
  591. *
  592. * Note that rtc_irq_set_freq() should previously have been used to
  593. * specify the desired frequency of periodic IRQ task->func() callbacks.
  594. */
  595. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  596. {
  597. int err = 0;
  598. unsigned long flags;
  599. retry:
  600. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  601. if (rtc->irq_task != NULL && task == NULL)
  602. err = -EBUSY;
  603. else if (rtc->irq_task != task)
  604. err = -EACCES;
  605. else {
  606. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  607. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  608. cpu_relax();
  609. goto retry;
  610. }
  611. rtc->pie_enabled = enabled;
  612. }
  613. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  614. return err;
  615. }
  616. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  617. /**
  618. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  619. * @rtc: the rtc device
  620. * @task: currently registered with rtc_irq_register()
  621. * @freq: positive frequency with which task->func() will be called
  622. * Context: any
  623. *
  624. * Note that rtc_irq_set_state() is used to enable or disable the
  625. * periodic IRQs.
  626. */
  627. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  628. {
  629. int err = 0;
  630. unsigned long flags;
  631. if (freq <= 0 || freq > RTC_MAX_FREQ)
  632. return -EINVAL;
  633. retry:
  634. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  635. if (rtc->irq_task != NULL && task == NULL)
  636. err = -EBUSY;
  637. else if (rtc->irq_task != task)
  638. err = -EACCES;
  639. else {
  640. rtc->irq_freq = freq;
  641. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  642. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  643. cpu_relax();
  644. goto retry;
  645. }
  646. }
  647. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  648. return err;
  649. }
  650. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  651. /**
  652. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  653. * @rtc rtc device
  654. * @timer timer being added.
  655. *
  656. * Enqueues a timer onto the rtc devices timerqueue and sets
  657. * the next alarm event appropriately.
  658. *
  659. * Sets the enabled bit on the added timer.
  660. *
  661. * Must hold ops_lock for proper serialization of timerqueue
  662. */
  663. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  664. {
  665. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  666. struct rtc_time tm;
  667. ktime_t now;
  668. timer->enabled = 1;
  669. __rtc_read_time(rtc, &tm);
  670. now = rtc_tm_to_ktime(tm);
  671. /* Skip over expired timers */
  672. while (next) {
  673. if (next->expires.tv64 >= now.tv64)
  674. break;
  675. next = timerqueue_iterate_next(next);
  676. }
  677. timerqueue_add(&rtc->timerqueue, &timer->node);
  678. if (!next || ktime_before(timer->node.expires, next->expires)) {
  679. struct rtc_wkalrm alarm;
  680. int err;
  681. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  682. alarm.enabled = 1;
  683. err = __rtc_set_alarm(rtc, &alarm);
  684. if (err == -ETIME) {
  685. pm_stay_awake(rtc->dev.parent);
  686. schedule_work(&rtc->irqwork);
  687. } else if (err) {
  688. timerqueue_del(&rtc->timerqueue, &timer->node);
  689. timer->enabled = 0;
  690. return err;
  691. }
  692. }
  693. return 0;
  694. }
  695. static void rtc_alarm_disable(struct rtc_device *rtc)
  696. {
  697. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  698. return;
  699. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  700. }
  701. /**
  702. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  703. * @rtc rtc device
  704. * @timer timer being removed.
  705. *
  706. * Removes a timer onto the rtc devices timerqueue and sets
  707. * the next alarm event appropriately.
  708. *
  709. * Clears the enabled bit on the removed timer.
  710. *
  711. * Must hold ops_lock for proper serialization of timerqueue
  712. */
  713. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  714. {
  715. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  716. timerqueue_del(&rtc->timerqueue, &timer->node);
  717. timer->enabled = 0;
  718. if (next == &timer->node) {
  719. struct rtc_wkalrm alarm;
  720. int err;
  721. next = timerqueue_getnext(&rtc->timerqueue);
  722. if (!next) {
  723. rtc_alarm_disable(rtc);
  724. return;
  725. }
  726. alarm.time = rtc_ktime_to_tm(next->expires);
  727. alarm.enabled = 1;
  728. err = __rtc_set_alarm(rtc, &alarm);
  729. if (err == -ETIME) {
  730. pm_stay_awake(rtc->dev.parent);
  731. schedule_work(&rtc->irqwork);
  732. }
  733. }
  734. }
  735. /**
  736. * rtc_timer_do_work - Expires rtc timers
  737. * @rtc rtc device
  738. * @timer timer being removed.
  739. *
  740. * Expires rtc timers. Reprograms next alarm event if needed.
  741. * Called via worktask.
  742. *
  743. * Serializes access to timerqueue via ops_lock mutex
  744. */
  745. void rtc_timer_do_work(struct work_struct *work)
  746. {
  747. struct rtc_timer *timer;
  748. struct timerqueue_node *next;
  749. ktime_t now;
  750. struct rtc_time tm;
  751. struct rtc_device *rtc =
  752. container_of(work, struct rtc_device, irqwork);
  753. mutex_lock(&rtc->ops_lock);
  754. again:
  755. __rtc_read_time(rtc, &tm);
  756. now = rtc_tm_to_ktime(tm);
  757. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  758. if (next->expires.tv64 > now.tv64)
  759. break;
  760. /* expire timer */
  761. timer = container_of(next, struct rtc_timer, node);
  762. timerqueue_del(&rtc->timerqueue, &timer->node);
  763. timer->enabled = 0;
  764. if (timer->task.func)
  765. timer->task.func(timer->task.private_data);
  766. /* Re-add/fwd periodic timers */
  767. if (ktime_to_ns(timer->period)) {
  768. timer->node.expires = ktime_add(timer->node.expires,
  769. timer->period);
  770. timer->enabled = 1;
  771. timerqueue_add(&rtc->timerqueue, &timer->node);
  772. }
  773. }
  774. /* Set next alarm */
  775. if (next) {
  776. struct rtc_wkalrm alarm;
  777. int err;
  778. int retry = 3;
  779. alarm.time = rtc_ktime_to_tm(next->expires);
  780. alarm.enabled = 1;
  781. reprogram:
  782. err = __rtc_set_alarm(rtc, &alarm);
  783. if (err == -ETIME)
  784. goto again;
  785. else if (err) {
  786. if (retry-- > 0)
  787. goto reprogram;
  788. timer = container_of(next, struct rtc_timer, node);
  789. timerqueue_del(&rtc->timerqueue, &timer->node);
  790. timer->enabled = 0;
  791. dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
  792. goto again;
  793. }
  794. } else
  795. rtc_alarm_disable(rtc);
  796. pm_relax(rtc->dev.parent);
  797. mutex_unlock(&rtc->ops_lock);
  798. }
  799. /* rtc_timer_init - Initializes an rtc_timer
  800. * @timer: timer to be intiialized
  801. * @f: function pointer to be called when timer fires
  802. * @data: private data passed to function pointer
  803. *
  804. * Kernel interface to initializing an rtc_timer.
  805. */
  806. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
  807. {
  808. timerqueue_init(&timer->node);
  809. timer->enabled = 0;
  810. timer->task.func = f;
  811. timer->task.private_data = data;
  812. }
  813. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  814. * @ rtc: rtc device to be used
  815. * @ timer: timer being set
  816. * @ expires: time at which to expire the timer
  817. * @ period: period that the timer will recur
  818. *
  819. * Kernel interface to set an rtc_timer
  820. */
  821. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
  822. ktime_t expires, ktime_t period)
  823. {
  824. int ret = 0;
  825. mutex_lock(&rtc->ops_lock);
  826. if (timer->enabled)
  827. rtc_timer_remove(rtc, timer);
  828. timer->node.expires = expires;
  829. timer->period = period;
  830. ret = rtc_timer_enqueue(rtc, timer);
  831. mutex_unlock(&rtc->ops_lock);
  832. return ret;
  833. }
  834. /* rtc_timer_cancel - Stops an rtc_timer
  835. * @ rtc: rtc device to be used
  836. * @ timer: timer being set
  837. *
  838. * Kernel interface to cancel an rtc_timer
  839. */
  840. void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
  841. {
  842. mutex_lock(&rtc->ops_lock);
  843. if (timer->enabled)
  844. rtc_timer_remove(rtc, timer);
  845. mutex_unlock(&rtc->ops_lock);
  846. }
  847. /**
  848. * rtc_read_offset - Read the amount of rtc offset in parts per billion
  849. * @ rtc: rtc device to be used
  850. * @ offset: the offset in parts per billion
  851. *
  852. * see below for details.
  853. *
  854. * Kernel interface to read rtc clock offset
  855. * Returns 0 on success, or a negative number on error.
  856. * If read_offset() is not implemented for the rtc, return -EINVAL
  857. */
  858. int rtc_read_offset(struct rtc_device *rtc, long *offset)
  859. {
  860. int ret;
  861. if (!rtc->ops)
  862. return -ENODEV;
  863. if (!rtc->ops->read_offset)
  864. return -EINVAL;
  865. mutex_lock(&rtc->ops_lock);
  866. ret = rtc->ops->read_offset(rtc->dev.parent, offset);
  867. mutex_unlock(&rtc->ops_lock);
  868. return ret;
  869. }
  870. /**
  871. * rtc_set_offset - Adjusts the duration of the average second
  872. * @ rtc: rtc device to be used
  873. * @ offset: the offset in parts per billion
  874. *
  875. * Some rtc's allow an adjustment to the average duration of a second
  876. * to compensate for differences in the actual clock rate due to temperature,
  877. * the crystal, capacitor, etc.
  878. *
  879. * Kernel interface to adjust an rtc clock offset.
  880. * Return 0 on success, or a negative number on error.
  881. * If the rtc offset is not setable (or not implemented), return -EINVAL
  882. */
  883. int rtc_set_offset(struct rtc_device *rtc, long offset)
  884. {
  885. int ret;
  886. if (!rtc->ops)
  887. return -ENODEV;
  888. if (!rtc->ops->set_offset)
  889. return -EINVAL;
  890. mutex_lock(&rtc->ops_lock);
  891. ret = rtc->ops->set_offset(rtc->dev.parent, offset);
  892. mutex_unlock(&rtc->ops_lock);
  893. return ret;
  894. }