interface.c 27 KB

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