interface.c 27 KB

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