interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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/log2.h>
  16. #include <linux/workqueue.h>
  17. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
  18. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
  19. static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  20. {
  21. int err;
  22. if (!rtc->ops)
  23. err = -ENODEV;
  24. else if (!rtc->ops->read_time)
  25. err = -EINVAL;
  26. else {
  27. memset(tm, 0, sizeof(struct rtc_time));
  28. err = rtc->ops->read_time(rtc->dev.parent, tm);
  29. }
  30. return err;
  31. }
  32. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  33. {
  34. int err;
  35. err = mutex_lock_interruptible(&rtc->ops_lock);
  36. if (err)
  37. return err;
  38. err = __rtc_read_time(rtc, tm);
  39. mutex_unlock(&rtc->ops_lock);
  40. return err;
  41. }
  42. EXPORT_SYMBOL_GPL(rtc_read_time);
  43. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  44. {
  45. int err;
  46. err = rtc_valid_tm(tm);
  47. if (err != 0)
  48. return err;
  49. err = mutex_lock_interruptible(&rtc->ops_lock);
  50. if (err)
  51. return err;
  52. if (!rtc->ops)
  53. err = -ENODEV;
  54. else if (rtc->ops->set_time)
  55. err = rtc->ops->set_time(rtc->dev.parent, tm);
  56. else if (rtc->ops->set_mmss) {
  57. unsigned long secs;
  58. err = rtc_tm_to_time(tm, &secs);
  59. if (err == 0)
  60. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  61. } else
  62. err = -EINVAL;
  63. mutex_unlock(&rtc->ops_lock);
  64. return err;
  65. }
  66. EXPORT_SYMBOL_GPL(rtc_set_time);
  67. int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
  68. {
  69. int err;
  70. err = mutex_lock_interruptible(&rtc->ops_lock);
  71. if (err)
  72. return err;
  73. if (!rtc->ops)
  74. err = -ENODEV;
  75. else if (rtc->ops->set_mmss)
  76. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  77. else if (rtc->ops->read_time && rtc->ops->set_time) {
  78. struct rtc_time new, old;
  79. err = rtc->ops->read_time(rtc->dev.parent, &old);
  80. if (err == 0) {
  81. rtc_time_to_tm(secs, &new);
  82. /*
  83. * avoid writing when we're going to change the day of
  84. * the month. We will retry in the next minute. This
  85. * basically means that if the RTC must not drift
  86. * by more than 1 minute in 11 minutes.
  87. */
  88. if (!((old.tm_hour == 23 && old.tm_min == 59) ||
  89. (new.tm_hour == 23 && new.tm_min == 59)))
  90. err = rtc->ops->set_time(rtc->dev.parent,
  91. &new);
  92. }
  93. }
  94. else
  95. err = -EINVAL;
  96. mutex_unlock(&rtc->ops_lock);
  97. return err;
  98. }
  99. EXPORT_SYMBOL_GPL(rtc_set_mmss);
  100. static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  101. {
  102. int err;
  103. err = mutex_lock_interruptible(&rtc->ops_lock);
  104. if (err)
  105. return err;
  106. if (rtc->ops == NULL)
  107. err = -ENODEV;
  108. else if (!rtc->ops->read_alarm)
  109. err = -EINVAL;
  110. else {
  111. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  112. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  113. }
  114. mutex_unlock(&rtc->ops_lock);
  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. unsigned long 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. /* with luck, no rollover is needed */
  214. rtc_tm_to_time(&now, &t_now);
  215. rtc_tm_to_time(&alarm->time, &t_alm);
  216. if (t_now < t_alm)
  217. goto done;
  218. switch (missing) {
  219. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  220. * that will trigger at 5am will do so at 5am Tuesday, which
  221. * could also be in the next month or year. This is a common
  222. * case, especially for PCs.
  223. */
  224. case day:
  225. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  226. t_alm += 24 * 60 * 60;
  227. rtc_time_to_tm(t_alm, &alarm->time);
  228. break;
  229. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  230. * be next month. An alarm matching on the 30th, 29th, or 28th
  231. * may end up in the month after that! Many newer PCs support
  232. * this type of alarm.
  233. */
  234. case month:
  235. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  236. do {
  237. if (alarm->time.tm_mon < 11)
  238. alarm->time.tm_mon++;
  239. else {
  240. alarm->time.tm_mon = 0;
  241. alarm->time.tm_year++;
  242. }
  243. days = rtc_month_days(alarm->time.tm_mon,
  244. alarm->time.tm_year);
  245. } while (days < alarm->time.tm_mday);
  246. break;
  247. /* Year rollover ... easy except for leap years! */
  248. case year:
  249. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  250. do {
  251. alarm->time.tm_year++;
  252. } while (rtc_valid_tm(&alarm->time) != 0);
  253. break;
  254. default:
  255. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  256. }
  257. done:
  258. return 0;
  259. }
  260. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  261. {
  262. int err;
  263. err = mutex_lock_interruptible(&rtc->ops_lock);
  264. if (err)
  265. return err;
  266. if (rtc->ops == NULL)
  267. err = -ENODEV;
  268. else if (!rtc->ops->read_alarm)
  269. err = -EINVAL;
  270. else {
  271. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  272. alarm->enabled = rtc->aie_timer.enabled;
  273. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  274. }
  275. mutex_unlock(&rtc->ops_lock);
  276. return err;
  277. }
  278. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  279. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  280. {
  281. struct rtc_time tm;
  282. long now, scheduled;
  283. int err;
  284. err = rtc_valid_tm(&alarm->time);
  285. if (err)
  286. return err;
  287. rtc_tm_to_time(&alarm->time, &scheduled);
  288. /* Make sure we're not setting alarms in the past */
  289. err = __rtc_read_time(rtc, &tm);
  290. rtc_tm_to_time(&tm, &now);
  291. if (scheduled <= now)
  292. return -ETIME;
  293. /*
  294. * XXX - We just checked to make sure the alarm time is not
  295. * in the past, but there is still a race window where if
  296. * the is alarm set for the next second and the second ticks
  297. * over right here, before we set the alarm.
  298. */
  299. if (!rtc->ops)
  300. err = -ENODEV;
  301. else if (!rtc->ops->set_alarm)
  302. err = -EINVAL;
  303. else
  304. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  305. return err;
  306. }
  307. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  308. {
  309. int err;
  310. err = rtc_valid_tm(&alarm->time);
  311. if (err != 0)
  312. return err;
  313. err = mutex_lock_interruptible(&rtc->ops_lock);
  314. if (err)
  315. return err;
  316. if (rtc->aie_timer.enabled) {
  317. rtc_timer_remove(rtc, &rtc->aie_timer);
  318. }
  319. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  320. rtc->aie_timer.period = ktime_set(0, 0);
  321. if (alarm->enabled) {
  322. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  323. }
  324. mutex_unlock(&rtc->ops_lock);
  325. return err;
  326. }
  327. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  328. /* Called once per device from rtc_device_register */
  329. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  330. {
  331. int err;
  332. err = rtc_valid_tm(&alarm->time);
  333. if (err != 0)
  334. return err;
  335. err = mutex_lock_interruptible(&rtc->ops_lock);
  336. if (err)
  337. return err;
  338. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  339. rtc->aie_timer.period = ktime_set(0, 0);
  340. if (alarm->enabled) {
  341. rtc->aie_timer.enabled = 1;
  342. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  343. }
  344. mutex_unlock(&rtc->ops_lock);
  345. return err;
  346. }
  347. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  348. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  349. {
  350. int err = mutex_lock_interruptible(&rtc->ops_lock);
  351. if (err)
  352. return err;
  353. if (rtc->aie_timer.enabled != enabled) {
  354. if (enabled)
  355. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  356. else
  357. rtc_timer_remove(rtc, &rtc->aie_timer);
  358. }
  359. if (err)
  360. /* nothing */;
  361. else if (!rtc->ops)
  362. err = -ENODEV;
  363. else if (!rtc->ops->alarm_irq_enable)
  364. err = -EINVAL;
  365. else
  366. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  367. mutex_unlock(&rtc->ops_lock);
  368. return err;
  369. }
  370. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  371. int rtc_update_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. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  377. if (enabled == 0 && rtc->uie_irq_active) {
  378. mutex_unlock(&rtc->ops_lock);
  379. return rtc_dev_update_irq_enable_emul(rtc, 0);
  380. }
  381. #endif
  382. /* make sure we're changing state */
  383. if (rtc->uie_rtctimer.enabled == enabled)
  384. goto out;
  385. if (enabled) {
  386. struct rtc_time tm;
  387. ktime_t now, onesec;
  388. __rtc_read_time(rtc, &tm);
  389. onesec = ktime_set(1, 0);
  390. now = rtc_tm_to_ktime(tm);
  391. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  392. rtc->uie_rtctimer.period = ktime_set(1, 0);
  393. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  394. } else
  395. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  396. out:
  397. mutex_unlock(&rtc->ops_lock);
  398. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  399. /*
  400. * Enable emulation if the driver did not provide
  401. * the update_irq_enable function pointer or if returned
  402. * -EINVAL to signal that it has been configured without
  403. * interrupts or that are not available at the moment.
  404. */
  405. if (err == -EINVAL)
  406. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  407. #endif
  408. return err;
  409. }
  410. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  411. /**
  412. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  413. * @rtc: pointer to the rtc device
  414. *
  415. * This function is called when an AIE, UIE or PIE mode interrupt
  416. * has occurred (or been emulated).
  417. *
  418. * Triggers the registered irq_task function callback.
  419. */
  420. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  421. {
  422. unsigned long flags;
  423. /* mark one irq of the appropriate mode */
  424. spin_lock_irqsave(&rtc->irq_lock, flags);
  425. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  426. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  427. /* call the task func */
  428. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  429. if (rtc->irq_task)
  430. rtc->irq_task->func(rtc->irq_task->private_data);
  431. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  432. wake_up_interruptible(&rtc->irq_queue);
  433. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  434. }
  435. /**
  436. * rtc_aie_update_irq - AIE mode rtctimer hook
  437. * @private: pointer to the rtc_device
  438. *
  439. * This functions is called when the aie_timer expires.
  440. */
  441. void rtc_aie_update_irq(void *private)
  442. {
  443. struct rtc_device *rtc = (struct rtc_device *)private;
  444. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  445. }
  446. /**
  447. * rtc_uie_update_irq - UIE mode rtctimer hook
  448. * @private: pointer to the rtc_device
  449. *
  450. * This functions is called when the uie_timer expires.
  451. */
  452. void rtc_uie_update_irq(void *private)
  453. {
  454. struct rtc_device *rtc = (struct rtc_device *)private;
  455. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  456. }
  457. /**
  458. * rtc_pie_update_irq - PIE mode hrtimer hook
  459. * @timer: pointer to the pie mode hrtimer
  460. *
  461. * This function is used to emulate PIE mode interrupts
  462. * using an hrtimer. This function is called when the periodic
  463. * hrtimer expires.
  464. */
  465. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  466. {
  467. struct rtc_device *rtc;
  468. ktime_t period;
  469. int count;
  470. rtc = container_of(timer, struct rtc_device, pie_timer);
  471. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  472. count = hrtimer_forward_now(timer, period);
  473. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  474. return HRTIMER_RESTART;
  475. }
  476. /**
  477. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  478. * @rtc: the rtc device
  479. * @num: how many irqs are being reported (usually one)
  480. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  481. * Context: any
  482. */
  483. void rtc_update_irq(struct rtc_device *rtc,
  484. unsigned long num, unsigned long events)
  485. {
  486. schedule_work(&rtc->irqwork);
  487. }
  488. EXPORT_SYMBOL_GPL(rtc_update_irq);
  489. static int __rtc_match(struct device *dev, void *data)
  490. {
  491. char *name = (char *)data;
  492. if (strcmp(dev_name(dev), name) == 0)
  493. return 1;
  494. return 0;
  495. }
  496. struct rtc_device *rtc_class_open(char *name)
  497. {
  498. struct device *dev;
  499. struct rtc_device *rtc = NULL;
  500. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  501. if (dev)
  502. rtc = to_rtc_device(dev);
  503. if (rtc) {
  504. if (!try_module_get(rtc->owner)) {
  505. put_device(dev);
  506. rtc = NULL;
  507. }
  508. }
  509. return rtc;
  510. }
  511. EXPORT_SYMBOL_GPL(rtc_class_open);
  512. void rtc_class_close(struct rtc_device *rtc)
  513. {
  514. module_put(rtc->owner);
  515. put_device(&rtc->dev);
  516. }
  517. EXPORT_SYMBOL_GPL(rtc_class_close);
  518. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  519. {
  520. int retval = -EBUSY;
  521. if (task == NULL || task->func == NULL)
  522. return -EINVAL;
  523. /* Cannot register while the char dev is in use */
  524. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  525. return -EBUSY;
  526. spin_lock_irq(&rtc->irq_task_lock);
  527. if (rtc->irq_task == NULL) {
  528. rtc->irq_task = task;
  529. retval = 0;
  530. }
  531. spin_unlock_irq(&rtc->irq_task_lock);
  532. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  533. return retval;
  534. }
  535. EXPORT_SYMBOL_GPL(rtc_irq_register);
  536. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  537. {
  538. spin_lock_irq(&rtc->irq_task_lock);
  539. if (rtc->irq_task == task)
  540. rtc->irq_task = NULL;
  541. spin_unlock_irq(&rtc->irq_task_lock);
  542. }
  543. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  544. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  545. {
  546. /*
  547. * We unconditionally cancel the timer here, because otherwise
  548. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  549. * when we manage to start the timer before the callback
  550. * returns HRTIMER_RESTART.
  551. *
  552. * We cannot use hrtimer_cancel() here as a running callback
  553. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  554. * would spin forever.
  555. */
  556. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  557. return -1;
  558. if (enabled) {
  559. ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
  560. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  561. }
  562. return 0;
  563. }
  564. /**
  565. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  566. * @rtc: the rtc device
  567. * @task: currently registered with rtc_irq_register()
  568. * @enabled: true to enable periodic IRQs
  569. * Context: any
  570. *
  571. * Note that rtc_irq_set_freq() should previously have been used to
  572. * specify the desired frequency of periodic IRQ task->func() callbacks.
  573. */
  574. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  575. {
  576. int err = 0;
  577. unsigned long flags;
  578. retry:
  579. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  580. if (rtc->irq_task != NULL && task == NULL)
  581. err = -EBUSY;
  582. if (rtc->irq_task != task)
  583. err = -EACCES;
  584. if (!err) {
  585. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  586. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  587. cpu_relax();
  588. goto retry;
  589. }
  590. rtc->pie_enabled = enabled;
  591. }
  592. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  593. return err;
  594. }
  595. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  596. /**
  597. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  598. * @rtc: the rtc device
  599. * @task: currently registered with rtc_irq_register()
  600. * @freq: positive frequency with which task->func() will be called
  601. * Context: any
  602. *
  603. * Note that rtc_irq_set_state() is used to enable or disable the
  604. * periodic IRQs.
  605. */
  606. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  607. {
  608. int err = 0;
  609. unsigned long flags;
  610. if (freq <= 0 || freq > RTC_MAX_FREQ)
  611. return -EINVAL;
  612. retry:
  613. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  614. if (rtc->irq_task != NULL && task == NULL)
  615. err = -EBUSY;
  616. if (rtc->irq_task != task)
  617. err = -EACCES;
  618. if (!err) {
  619. rtc->irq_freq = freq;
  620. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  621. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  622. cpu_relax();
  623. goto retry;
  624. }
  625. }
  626. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  627. return err;
  628. }
  629. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  630. /**
  631. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  632. * @rtc rtc device
  633. * @timer timer being added.
  634. *
  635. * Enqueues a timer onto the rtc devices timerqueue and sets
  636. * the next alarm event appropriately.
  637. *
  638. * Sets the enabled bit on the added timer.
  639. *
  640. * Must hold ops_lock for proper serialization of timerqueue
  641. */
  642. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  643. {
  644. timer->enabled = 1;
  645. timerqueue_add(&rtc->timerqueue, &timer->node);
  646. if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
  647. struct rtc_wkalrm alarm;
  648. int err;
  649. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  650. alarm.enabled = 1;
  651. err = __rtc_set_alarm(rtc, &alarm);
  652. if (err == -ETIME)
  653. schedule_work(&rtc->irqwork);
  654. else if (err) {
  655. timerqueue_del(&rtc->timerqueue, &timer->node);
  656. timer->enabled = 0;
  657. return err;
  658. }
  659. }
  660. return 0;
  661. }
  662. static void rtc_alarm_disable(struct rtc_device *rtc)
  663. {
  664. if (!rtc->ops || !rtc->ops->alarm_irq_enable)
  665. return;
  666. rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
  667. }
  668. /**
  669. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  670. * @rtc rtc device
  671. * @timer timer being removed.
  672. *
  673. * Removes a timer onto the rtc devices timerqueue and sets
  674. * the next alarm event appropriately.
  675. *
  676. * Clears the enabled bit on the removed timer.
  677. *
  678. * Must hold ops_lock for proper serialization of timerqueue
  679. */
  680. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  681. {
  682. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  683. timerqueue_del(&rtc->timerqueue, &timer->node);
  684. timer->enabled = 0;
  685. if (next == &timer->node) {
  686. struct rtc_wkalrm alarm;
  687. int err;
  688. next = timerqueue_getnext(&rtc->timerqueue);
  689. if (!next) {
  690. rtc_alarm_disable(rtc);
  691. return;
  692. }
  693. alarm.time = rtc_ktime_to_tm(next->expires);
  694. alarm.enabled = 1;
  695. err = __rtc_set_alarm(rtc, &alarm);
  696. if (err == -ETIME)
  697. schedule_work(&rtc->irqwork);
  698. }
  699. }
  700. /**
  701. * rtc_timer_do_work - Expires rtc timers
  702. * @rtc rtc device
  703. * @timer timer being removed.
  704. *
  705. * Expires rtc timers. Reprograms next alarm event if needed.
  706. * Called via worktask.
  707. *
  708. * Serializes access to timerqueue via ops_lock mutex
  709. */
  710. void rtc_timer_do_work(struct work_struct *work)
  711. {
  712. struct rtc_timer *timer;
  713. struct timerqueue_node *next;
  714. ktime_t now;
  715. struct rtc_time tm;
  716. struct rtc_device *rtc =
  717. container_of(work, struct rtc_device, irqwork);
  718. mutex_lock(&rtc->ops_lock);
  719. again:
  720. __rtc_read_time(rtc, &tm);
  721. now = rtc_tm_to_ktime(tm);
  722. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  723. if (next->expires.tv64 > now.tv64)
  724. break;
  725. /* expire timer */
  726. timer = container_of(next, struct rtc_timer, node);
  727. timerqueue_del(&rtc->timerqueue, &timer->node);
  728. timer->enabled = 0;
  729. if (timer->task.func)
  730. timer->task.func(timer->task.private_data);
  731. /* Re-add/fwd periodic timers */
  732. if (ktime_to_ns(timer->period)) {
  733. timer->node.expires = ktime_add(timer->node.expires,
  734. timer->period);
  735. timer->enabled = 1;
  736. timerqueue_add(&rtc->timerqueue, &timer->node);
  737. }
  738. }
  739. /* Set next alarm */
  740. if (next) {
  741. struct rtc_wkalrm alarm;
  742. int err;
  743. alarm.time = rtc_ktime_to_tm(next->expires);
  744. alarm.enabled = 1;
  745. err = __rtc_set_alarm(rtc, &alarm);
  746. if (err == -ETIME)
  747. goto again;
  748. } else
  749. rtc_alarm_disable(rtc);
  750. mutex_unlock(&rtc->ops_lock);
  751. }
  752. /* rtc_timer_init - Initializes an rtc_timer
  753. * @timer: timer to be intiialized
  754. * @f: function pointer to be called when timer fires
  755. * @data: private data passed to function pointer
  756. *
  757. * Kernel interface to initializing an rtc_timer.
  758. */
  759. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
  760. {
  761. timerqueue_init(&timer->node);
  762. timer->enabled = 0;
  763. timer->task.func = f;
  764. timer->task.private_data = data;
  765. }
  766. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  767. * @ rtc: rtc device to be used
  768. * @ timer: timer being set
  769. * @ expires: time at which to expire the timer
  770. * @ period: period that the timer will recur
  771. *
  772. * Kernel interface to set an rtc_timer
  773. */
  774. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  775. ktime_t expires, ktime_t period)
  776. {
  777. int ret = 0;
  778. mutex_lock(&rtc->ops_lock);
  779. if (timer->enabled)
  780. rtc_timer_remove(rtc, timer);
  781. timer->node.expires = expires;
  782. timer->period = period;
  783. ret = rtc_timer_enqueue(rtc, timer);
  784. mutex_unlock(&rtc->ops_lock);
  785. return ret;
  786. }
  787. /* rtc_timer_cancel - Stops an rtc_timer
  788. * @ rtc: rtc device to be used
  789. * @ timer: timer being set
  790. *
  791. * Kernel interface to cancel an rtc_timer
  792. */
  793. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
  794. {
  795. int ret = 0;
  796. mutex_lock(&rtc->ops_lock);
  797. if (timer->enabled)
  798. rtc_timer_remove(rtc, timer);
  799. mutex_unlock(&rtc->ops_lock);
  800. return ret;
  801. }