interface.c 26 KB

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