interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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) {
  200. alarm->time.tm_mday = now.tm_mday;
  201. missing = day;
  202. }
  203. if (alarm->time.tm_mon == -1) {
  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. 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. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  329. {
  330. int err = mutex_lock_interruptible(&rtc->ops_lock);
  331. if (err)
  332. return err;
  333. if (rtc->aie_timer.enabled != enabled) {
  334. if (enabled)
  335. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  336. else
  337. rtc_timer_remove(rtc, &rtc->aie_timer);
  338. }
  339. if (err)
  340. /* nothing */;
  341. else if (!rtc->ops)
  342. err = -ENODEV;
  343. else if (!rtc->ops->alarm_irq_enable)
  344. err = -EINVAL;
  345. else
  346. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  347. mutex_unlock(&rtc->ops_lock);
  348. return err;
  349. }
  350. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  351. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  352. {
  353. int err = mutex_lock_interruptible(&rtc->ops_lock);
  354. if (err)
  355. return err;
  356. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  357. if (enabled == 0 && rtc->uie_irq_active) {
  358. mutex_unlock(&rtc->ops_lock);
  359. return rtc_dev_update_irq_enable_emul(rtc, 0);
  360. }
  361. #endif
  362. /* make sure we're changing state */
  363. if (rtc->uie_rtctimer.enabled == enabled)
  364. goto out;
  365. if (enabled) {
  366. struct rtc_time tm;
  367. ktime_t now, onesec;
  368. __rtc_read_time(rtc, &tm);
  369. onesec = ktime_set(1, 0);
  370. now = rtc_tm_to_ktime(tm);
  371. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  372. rtc->uie_rtctimer.period = ktime_set(1, 0);
  373. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  374. } else
  375. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  376. out:
  377. mutex_unlock(&rtc->ops_lock);
  378. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  379. /*
  380. * Enable emulation if the driver did not provide
  381. * the update_irq_enable function pointer or if returned
  382. * -EINVAL to signal that it has been configured without
  383. * interrupts or that are not available at the moment.
  384. */
  385. if (err == -EINVAL)
  386. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  387. #endif
  388. return err;
  389. }
  390. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  391. /**
  392. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  393. * @rtc: pointer to the rtc device
  394. *
  395. * This function is called when an AIE, UIE or PIE mode interrupt
  396. * has occured (or been emulated).
  397. *
  398. * Triggers the registered irq_task function callback.
  399. */
  400. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  401. {
  402. unsigned long flags;
  403. /* mark one irq of the appropriate mode */
  404. spin_lock_irqsave(&rtc->irq_lock, flags);
  405. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  406. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  407. /* call the task func */
  408. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  409. if (rtc->irq_task)
  410. rtc->irq_task->func(rtc->irq_task->private_data);
  411. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  412. wake_up_interruptible(&rtc->irq_queue);
  413. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  414. }
  415. /**
  416. * rtc_aie_update_irq - AIE mode rtctimer hook
  417. * @private: pointer to the rtc_device
  418. *
  419. * This functions is called when the aie_timer expires.
  420. */
  421. void rtc_aie_update_irq(void *private)
  422. {
  423. struct rtc_device *rtc = (struct rtc_device *)private;
  424. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  425. }
  426. /**
  427. * rtc_uie_update_irq - UIE mode rtctimer hook
  428. * @private: pointer to the rtc_device
  429. *
  430. * This functions is called when the uie_timer expires.
  431. */
  432. void rtc_uie_update_irq(void *private)
  433. {
  434. struct rtc_device *rtc = (struct rtc_device *)private;
  435. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  436. }
  437. /**
  438. * rtc_pie_update_irq - PIE mode hrtimer hook
  439. * @timer: pointer to the pie mode hrtimer
  440. *
  441. * This function is used to emulate PIE mode interrupts
  442. * using an hrtimer. This function is called when the periodic
  443. * hrtimer expires.
  444. */
  445. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  446. {
  447. struct rtc_device *rtc;
  448. ktime_t period;
  449. int count;
  450. rtc = container_of(timer, struct rtc_device, pie_timer);
  451. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  452. count = hrtimer_forward_now(timer, period);
  453. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  454. return HRTIMER_RESTART;
  455. }
  456. /**
  457. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  458. * @rtc: the rtc device
  459. * @num: how many irqs are being reported (usually one)
  460. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  461. * Context: any
  462. */
  463. void rtc_update_irq(struct rtc_device *rtc,
  464. unsigned long num, unsigned long events)
  465. {
  466. schedule_work(&rtc->irqwork);
  467. }
  468. EXPORT_SYMBOL_GPL(rtc_update_irq);
  469. static int __rtc_match(struct device *dev, void *data)
  470. {
  471. char *name = (char *)data;
  472. if (strcmp(dev_name(dev), name) == 0)
  473. return 1;
  474. return 0;
  475. }
  476. struct rtc_device *rtc_class_open(char *name)
  477. {
  478. struct device *dev;
  479. struct rtc_device *rtc = NULL;
  480. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  481. if (dev)
  482. rtc = to_rtc_device(dev);
  483. if (rtc) {
  484. if (!try_module_get(rtc->owner)) {
  485. put_device(dev);
  486. rtc = NULL;
  487. }
  488. }
  489. return rtc;
  490. }
  491. EXPORT_SYMBOL_GPL(rtc_class_open);
  492. void rtc_class_close(struct rtc_device *rtc)
  493. {
  494. module_put(rtc->owner);
  495. put_device(&rtc->dev);
  496. }
  497. EXPORT_SYMBOL_GPL(rtc_class_close);
  498. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  499. {
  500. int retval = -EBUSY;
  501. if (task == NULL || task->func == NULL)
  502. return -EINVAL;
  503. /* Cannot register while the char dev is in use */
  504. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  505. return -EBUSY;
  506. spin_lock_irq(&rtc->irq_task_lock);
  507. if (rtc->irq_task == NULL) {
  508. rtc->irq_task = task;
  509. retval = 0;
  510. }
  511. spin_unlock_irq(&rtc->irq_task_lock);
  512. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  513. return retval;
  514. }
  515. EXPORT_SYMBOL_GPL(rtc_irq_register);
  516. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  517. {
  518. spin_lock_irq(&rtc->irq_task_lock);
  519. if (rtc->irq_task == task)
  520. rtc->irq_task = NULL;
  521. spin_unlock_irq(&rtc->irq_task_lock);
  522. }
  523. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  524. /**
  525. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  526. * @rtc: the rtc device
  527. * @task: currently registered with rtc_irq_register()
  528. * @enabled: true to enable periodic IRQs
  529. * Context: any
  530. *
  531. * Note that rtc_irq_set_freq() should previously have been used to
  532. * specify the desired frequency of periodic IRQ task->func() callbacks.
  533. */
  534. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  535. {
  536. int err = 0;
  537. unsigned long flags;
  538. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  539. if (rtc->irq_task != NULL && task == NULL)
  540. err = -EBUSY;
  541. if (rtc->irq_task != task)
  542. err = -EACCES;
  543. if (enabled) {
  544. ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  545. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  546. } else {
  547. hrtimer_cancel(&rtc->pie_timer);
  548. }
  549. rtc->pie_enabled = enabled;
  550. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  551. return err;
  552. }
  553. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  554. /**
  555. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  556. * @rtc: the rtc device
  557. * @task: currently registered with rtc_irq_register()
  558. * @freq: positive frequency with which task->func() will be called
  559. * Context: any
  560. *
  561. * Note that rtc_irq_set_state() is used to enable or disable the
  562. * periodic IRQs.
  563. */
  564. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  565. {
  566. int err = 0;
  567. unsigned long flags;
  568. if (freq <= 0)
  569. return -EINVAL;
  570. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  571. if (rtc->irq_task != NULL && task == NULL)
  572. err = -EBUSY;
  573. if (rtc->irq_task != task)
  574. err = -EACCES;
  575. if (err == 0) {
  576. rtc->irq_freq = freq;
  577. if (rtc->pie_enabled) {
  578. ktime_t period;
  579. hrtimer_cancel(&rtc->pie_timer);
  580. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  581. hrtimer_start(&rtc->pie_timer, period,
  582. HRTIMER_MODE_REL);
  583. }
  584. }
  585. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  586. return err;
  587. }
  588. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  589. /**
  590. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  591. * @rtc rtc device
  592. * @timer timer being added.
  593. *
  594. * Enqueues a timer onto the rtc devices timerqueue and sets
  595. * the next alarm event appropriately.
  596. *
  597. * Sets the enabled bit on the added timer.
  598. *
  599. * Must hold ops_lock for proper serialization of timerqueue
  600. */
  601. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  602. {
  603. timer->enabled = 1;
  604. timerqueue_add(&rtc->timerqueue, &timer->node);
  605. if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
  606. struct rtc_wkalrm alarm;
  607. int err;
  608. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  609. alarm.enabled = 1;
  610. err = __rtc_set_alarm(rtc, &alarm);
  611. if (err == -ETIME)
  612. schedule_work(&rtc->irqwork);
  613. else if (err) {
  614. timerqueue_del(&rtc->timerqueue, &timer->node);
  615. timer->enabled = 0;
  616. return err;
  617. }
  618. }
  619. return 0;
  620. }
  621. /**
  622. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  623. * @rtc rtc device
  624. * @timer timer being removed.
  625. *
  626. * Removes a timer onto the rtc devices timerqueue and sets
  627. * the next alarm event appropriately.
  628. *
  629. * Clears the enabled bit on the removed timer.
  630. *
  631. * Must hold ops_lock for proper serialization of timerqueue
  632. */
  633. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  634. {
  635. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  636. timerqueue_del(&rtc->timerqueue, &timer->node);
  637. timer->enabled = 0;
  638. if (next == &timer->node) {
  639. struct rtc_wkalrm alarm;
  640. int err;
  641. next = timerqueue_getnext(&rtc->timerqueue);
  642. if (!next)
  643. return;
  644. alarm.time = rtc_ktime_to_tm(next->expires);
  645. alarm.enabled = 1;
  646. err = __rtc_set_alarm(rtc, &alarm);
  647. if (err == -ETIME)
  648. schedule_work(&rtc->irqwork);
  649. }
  650. }
  651. /**
  652. * rtc_timer_do_work - Expires rtc timers
  653. * @rtc rtc device
  654. * @timer timer being removed.
  655. *
  656. * Expires rtc timers. Reprograms next alarm event if needed.
  657. * Called via worktask.
  658. *
  659. * Serializes access to timerqueue via ops_lock mutex
  660. */
  661. void rtc_timer_do_work(struct work_struct *work)
  662. {
  663. struct rtc_timer *timer;
  664. struct timerqueue_node *next;
  665. ktime_t now;
  666. struct rtc_time tm;
  667. struct rtc_device *rtc =
  668. container_of(work, struct rtc_device, irqwork);
  669. mutex_lock(&rtc->ops_lock);
  670. again:
  671. __rtc_read_time(rtc, &tm);
  672. now = rtc_tm_to_ktime(tm);
  673. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  674. if (next->expires.tv64 > now.tv64)
  675. break;
  676. /* expire timer */
  677. timer = container_of(next, struct rtc_timer, node);
  678. timerqueue_del(&rtc->timerqueue, &timer->node);
  679. timer->enabled = 0;
  680. if (timer->task.func)
  681. timer->task.func(timer->task.private_data);
  682. /* Re-add/fwd periodic timers */
  683. if (ktime_to_ns(timer->period)) {
  684. timer->node.expires = ktime_add(timer->node.expires,
  685. timer->period);
  686. timer->enabled = 1;
  687. timerqueue_add(&rtc->timerqueue, &timer->node);
  688. }
  689. }
  690. /* Set next alarm */
  691. if (next) {
  692. struct rtc_wkalrm alarm;
  693. int err;
  694. alarm.time = rtc_ktime_to_tm(next->expires);
  695. alarm.enabled = 1;
  696. err = __rtc_set_alarm(rtc, &alarm);
  697. if (err == -ETIME)
  698. goto again;
  699. }
  700. mutex_unlock(&rtc->ops_lock);
  701. }
  702. /* rtc_timer_init - Initializes an rtc_timer
  703. * @timer: timer to be intiialized
  704. * @f: function pointer to be called when timer fires
  705. * @data: private data passed to function pointer
  706. *
  707. * Kernel interface to initializing an rtc_timer.
  708. */
  709. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
  710. {
  711. timerqueue_init(&timer->node);
  712. timer->enabled = 0;
  713. timer->task.func = f;
  714. timer->task.private_data = data;
  715. }
  716. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  717. * @ rtc: rtc device to be used
  718. * @ timer: timer being set
  719. * @ expires: time at which to expire the timer
  720. * @ period: period that the timer will recur
  721. *
  722. * Kernel interface to set an rtc_timer
  723. */
  724. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  725. ktime_t expires, ktime_t period)
  726. {
  727. int ret = 0;
  728. mutex_lock(&rtc->ops_lock);
  729. if (timer->enabled)
  730. rtc_timer_remove(rtc, timer);
  731. timer->node.expires = expires;
  732. timer->period = period;
  733. ret = rtc_timer_enqueue(rtc, timer);
  734. mutex_unlock(&rtc->ops_lock);
  735. return ret;
  736. }
  737. /* rtc_timer_cancel - Stops an rtc_timer
  738. * @ rtc: rtc device to be used
  739. * @ timer: timer being set
  740. *
  741. * Kernel interface to cancel an rtc_timer
  742. */
  743. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
  744. {
  745. int ret = 0;
  746. mutex_lock(&rtc->ops_lock);
  747. if (timer->enabled)
  748. rtc_timer_remove(rtc, timer);
  749. mutex_unlock(&rtc->ops_lock);
  750. return ret;
  751. }