interface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. int rtc_read_alarm(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. alarm->enabled = rtc->aie_timer.enabled;
  113. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  114. }
  115. mutex_unlock(&rtc->ops_lock);
  116. return err;
  117. }
  118. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  119. int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  120. {
  121. struct rtc_time tm;
  122. long now, scheduled;
  123. int err;
  124. err = rtc_valid_tm(&alarm->time);
  125. if (err)
  126. return err;
  127. rtc_tm_to_time(&alarm->time, &scheduled);
  128. /* Make sure we're not setting alarms in the past */
  129. err = __rtc_read_time(rtc, &tm);
  130. rtc_tm_to_time(&tm, &now);
  131. if (scheduled <= now)
  132. return -ETIME;
  133. /*
  134. * XXX - We just checked to make sure the alarm time is not
  135. * in the past, but there is still a race window where if
  136. * the is alarm set for the next second and the second ticks
  137. * over right here, before we set the alarm.
  138. */
  139. if (!rtc->ops)
  140. err = -ENODEV;
  141. else if (!rtc->ops->set_alarm)
  142. err = -EINVAL;
  143. else
  144. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  145. return err;
  146. }
  147. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  148. {
  149. int err;
  150. err = rtc_valid_tm(&alarm->time);
  151. if (err != 0)
  152. return err;
  153. err = mutex_lock_interruptible(&rtc->ops_lock);
  154. if (err)
  155. return err;
  156. if (rtc->aie_timer.enabled) {
  157. rtc_timer_remove(rtc, &rtc->aie_timer);
  158. }
  159. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  160. rtc->aie_timer.period = ktime_set(0, 0);
  161. if (alarm->enabled) {
  162. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  163. }
  164. mutex_unlock(&rtc->ops_lock);
  165. return err;
  166. }
  167. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  168. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  169. {
  170. int err = mutex_lock_interruptible(&rtc->ops_lock);
  171. if (err)
  172. return err;
  173. if (rtc->aie_timer.enabled != enabled) {
  174. if (enabled)
  175. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  176. else
  177. rtc_timer_remove(rtc, &rtc->aie_timer);
  178. }
  179. if (err)
  180. /* nothing */;
  181. else if (!rtc->ops)
  182. err = -ENODEV;
  183. else if (!rtc->ops->alarm_irq_enable)
  184. err = -EINVAL;
  185. else
  186. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  187. mutex_unlock(&rtc->ops_lock);
  188. return err;
  189. }
  190. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  191. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  192. {
  193. int err = mutex_lock_interruptible(&rtc->ops_lock);
  194. if (err)
  195. return err;
  196. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  197. if (enabled == 0 && rtc->uie_irq_active) {
  198. mutex_unlock(&rtc->ops_lock);
  199. return rtc_dev_update_irq_enable_emul(rtc, 0);
  200. }
  201. #endif
  202. /* make sure we're changing state */
  203. if (rtc->uie_rtctimer.enabled == enabled)
  204. goto out;
  205. if (enabled) {
  206. struct rtc_time tm;
  207. ktime_t now, onesec;
  208. __rtc_read_time(rtc, &tm);
  209. onesec = ktime_set(1, 0);
  210. now = rtc_tm_to_ktime(tm);
  211. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  212. rtc->uie_rtctimer.period = ktime_set(1, 0);
  213. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  214. } else
  215. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  216. out:
  217. mutex_unlock(&rtc->ops_lock);
  218. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  219. /*
  220. * Enable emulation if the driver did not provide
  221. * the update_irq_enable function pointer or if returned
  222. * -EINVAL to signal that it has been configured without
  223. * interrupts or that are not available at the moment.
  224. */
  225. if (err == -EINVAL)
  226. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  227. #endif
  228. return err;
  229. }
  230. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  231. /**
  232. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  233. * @rtc: pointer to the rtc device
  234. *
  235. * This function is called when an AIE, UIE or PIE mode interrupt
  236. * has occured (or been emulated).
  237. *
  238. * Triggers the registered irq_task function callback.
  239. */
  240. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  241. {
  242. unsigned long flags;
  243. /* mark one irq of the appropriate mode */
  244. spin_lock_irqsave(&rtc->irq_lock, flags);
  245. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  246. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  247. /* call the task func */
  248. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  249. if (rtc->irq_task)
  250. rtc->irq_task->func(rtc->irq_task->private_data);
  251. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  252. wake_up_interruptible(&rtc->irq_queue);
  253. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  254. }
  255. /**
  256. * rtc_aie_update_irq - AIE mode rtctimer hook
  257. * @private: pointer to the rtc_device
  258. *
  259. * This functions is called when the aie_timer expires.
  260. */
  261. void rtc_aie_update_irq(void *private)
  262. {
  263. struct rtc_device *rtc = (struct rtc_device *)private;
  264. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  265. }
  266. /**
  267. * rtc_uie_update_irq - UIE mode rtctimer hook
  268. * @private: pointer to the rtc_device
  269. *
  270. * This functions is called when the uie_timer expires.
  271. */
  272. void rtc_uie_update_irq(void *private)
  273. {
  274. struct rtc_device *rtc = (struct rtc_device *)private;
  275. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  276. }
  277. /**
  278. * rtc_pie_update_irq - PIE mode hrtimer hook
  279. * @timer: pointer to the pie mode hrtimer
  280. *
  281. * This function is used to emulate PIE mode interrupts
  282. * using an hrtimer. This function is called when the periodic
  283. * hrtimer expires.
  284. */
  285. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  286. {
  287. struct rtc_device *rtc;
  288. ktime_t period;
  289. int count;
  290. rtc = container_of(timer, struct rtc_device, pie_timer);
  291. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  292. count = hrtimer_forward_now(timer, period);
  293. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  294. return HRTIMER_RESTART;
  295. }
  296. /**
  297. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  298. * @rtc: the rtc device
  299. * @num: how many irqs are being reported (usually one)
  300. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  301. * Context: any
  302. */
  303. void rtc_update_irq(struct rtc_device *rtc,
  304. unsigned long num, unsigned long events)
  305. {
  306. schedule_work(&rtc->irqwork);
  307. }
  308. EXPORT_SYMBOL_GPL(rtc_update_irq);
  309. static int __rtc_match(struct device *dev, void *data)
  310. {
  311. char *name = (char *)data;
  312. if (strcmp(dev_name(dev), name) == 0)
  313. return 1;
  314. return 0;
  315. }
  316. struct rtc_device *rtc_class_open(char *name)
  317. {
  318. struct device *dev;
  319. struct rtc_device *rtc = NULL;
  320. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  321. if (dev)
  322. rtc = to_rtc_device(dev);
  323. if (rtc) {
  324. if (!try_module_get(rtc->owner)) {
  325. put_device(dev);
  326. rtc = NULL;
  327. }
  328. }
  329. return rtc;
  330. }
  331. EXPORT_SYMBOL_GPL(rtc_class_open);
  332. void rtc_class_close(struct rtc_device *rtc)
  333. {
  334. module_put(rtc->owner);
  335. put_device(&rtc->dev);
  336. }
  337. EXPORT_SYMBOL_GPL(rtc_class_close);
  338. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  339. {
  340. int retval = -EBUSY;
  341. if (task == NULL || task->func == NULL)
  342. return -EINVAL;
  343. /* Cannot register while the char dev is in use */
  344. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  345. return -EBUSY;
  346. spin_lock_irq(&rtc->irq_task_lock);
  347. if (rtc->irq_task == NULL) {
  348. rtc->irq_task = task;
  349. retval = 0;
  350. }
  351. spin_unlock_irq(&rtc->irq_task_lock);
  352. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  353. return retval;
  354. }
  355. EXPORT_SYMBOL_GPL(rtc_irq_register);
  356. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  357. {
  358. spin_lock_irq(&rtc->irq_task_lock);
  359. if (rtc->irq_task == task)
  360. rtc->irq_task = NULL;
  361. spin_unlock_irq(&rtc->irq_task_lock);
  362. }
  363. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  364. /**
  365. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  366. * @rtc: the rtc device
  367. * @task: currently registered with rtc_irq_register()
  368. * @enabled: true to enable periodic IRQs
  369. * Context: any
  370. *
  371. * Note that rtc_irq_set_freq() should previously have been used to
  372. * specify the desired frequency of periodic IRQ task->func() callbacks.
  373. */
  374. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  375. {
  376. int err = 0;
  377. unsigned long flags;
  378. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  379. if (rtc->irq_task != NULL && task == NULL)
  380. err = -EBUSY;
  381. if (rtc->irq_task != task)
  382. err = -EACCES;
  383. if (enabled) {
  384. ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  385. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  386. } else {
  387. hrtimer_cancel(&rtc->pie_timer);
  388. }
  389. rtc->pie_enabled = enabled;
  390. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  391. return err;
  392. }
  393. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  394. /**
  395. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  396. * @rtc: the rtc device
  397. * @task: currently registered with rtc_irq_register()
  398. * @freq: positive frequency with which task->func() will be called
  399. * Context: any
  400. *
  401. * Note that rtc_irq_set_state() is used to enable or disable the
  402. * periodic IRQs.
  403. */
  404. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  405. {
  406. int err = 0;
  407. unsigned long flags;
  408. if (freq <= 0)
  409. return -EINVAL;
  410. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  411. if (rtc->irq_task != NULL && task == NULL)
  412. err = -EBUSY;
  413. if (rtc->irq_task != task)
  414. err = -EACCES;
  415. if (err == 0) {
  416. rtc->irq_freq = freq;
  417. if (rtc->pie_enabled) {
  418. ktime_t period;
  419. hrtimer_cancel(&rtc->pie_timer);
  420. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  421. hrtimer_start(&rtc->pie_timer, period,
  422. HRTIMER_MODE_REL);
  423. }
  424. }
  425. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  429. /**
  430. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  431. * @rtc rtc device
  432. * @timer timer being added.
  433. *
  434. * Enqueues a timer onto the rtc devices timerqueue and sets
  435. * the next alarm event appropriately.
  436. *
  437. * Sets the enabled bit on the added timer.
  438. *
  439. * Must hold ops_lock for proper serialization of timerqueue
  440. */
  441. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  442. {
  443. timer->enabled = 1;
  444. timerqueue_add(&rtc->timerqueue, &timer->node);
  445. if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
  446. struct rtc_wkalrm alarm;
  447. int err;
  448. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  449. alarm.enabled = 1;
  450. err = __rtc_set_alarm(rtc, &alarm);
  451. if (err == -ETIME)
  452. schedule_work(&rtc->irqwork);
  453. else if (err) {
  454. timerqueue_del(&rtc->timerqueue, &timer->node);
  455. timer->enabled = 0;
  456. return err;
  457. }
  458. }
  459. return 0;
  460. }
  461. /**
  462. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  463. * @rtc rtc device
  464. * @timer timer being removed.
  465. *
  466. * Removes a timer onto the rtc devices timerqueue and sets
  467. * the next alarm event appropriately.
  468. *
  469. * Clears the enabled bit on the removed timer.
  470. *
  471. * Must hold ops_lock for proper serialization of timerqueue
  472. */
  473. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  474. {
  475. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  476. timerqueue_del(&rtc->timerqueue, &timer->node);
  477. timer->enabled = 0;
  478. if (next == &timer->node) {
  479. struct rtc_wkalrm alarm;
  480. int err;
  481. next = timerqueue_getnext(&rtc->timerqueue);
  482. if (!next)
  483. return;
  484. alarm.time = rtc_ktime_to_tm(next->expires);
  485. alarm.enabled = 1;
  486. err = __rtc_set_alarm(rtc, &alarm);
  487. if (err == -ETIME)
  488. schedule_work(&rtc->irqwork);
  489. }
  490. }
  491. /**
  492. * rtc_timer_do_work - Expires rtc timers
  493. * @rtc rtc device
  494. * @timer timer being removed.
  495. *
  496. * Expires rtc timers. Reprograms next alarm event if needed.
  497. * Called via worktask.
  498. *
  499. * Serializes access to timerqueue via ops_lock mutex
  500. */
  501. void rtc_timer_do_work(struct work_struct *work)
  502. {
  503. struct rtc_timer *timer;
  504. struct timerqueue_node *next;
  505. ktime_t now;
  506. struct rtc_time tm;
  507. struct rtc_device *rtc =
  508. container_of(work, struct rtc_device, irqwork);
  509. mutex_lock(&rtc->ops_lock);
  510. again:
  511. __rtc_read_time(rtc, &tm);
  512. now = rtc_tm_to_ktime(tm);
  513. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  514. if (next->expires.tv64 > now.tv64)
  515. break;
  516. /* expire timer */
  517. timer = container_of(next, struct rtc_timer, node);
  518. timerqueue_del(&rtc->timerqueue, &timer->node);
  519. timer->enabled = 0;
  520. if (timer->task.func)
  521. timer->task.func(timer->task.private_data);
  522. /* Re-add/fwd periodic timers */
  523. if (ktime_to_ns(timer->period)) {
  524. timer->node.expires = ktime_add(timer->node.expires,
  525. timer->period);
  526. timer->enabled = 1;
  527. timerqueue_add(&rtc->timerqueue, &timer->node);
  528. }
  529. }
  530. /* Set next alarm */
  531. if (next) {
  532. struct rtc_wkalrm alarm;
  533. int err;
  534. alarm.time = rtc_ktime_to_tm(next->expires);
  535. alarm.enabled = 1;
  536. err = __rtc_set_alarm(rtc, &alarm);
  537. if (err == -ETIME)
  538. goto again;
  539. }
  540. mutex_unlock(&rtc->ops_lock);
  541. }
  542. /* rtc_timer_init - Initializes an rtc_timer
  543. * @timer: timer to be intiialized
  544. * @f: function pointer to be called when timer fires
  545. * @data: private data passed to function pointer
  546. *
  547. * Kernel interface to initializing an rtc_timer.
  548. */
  549. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
  550. {
  551. timerqueue_init(&timer->node);
  552. timer->enabled = 0;
  553. timer->task.func = f;
  554. timer->task.private_data = data;
  555. }
  556. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  557. * @ rtc: rtc device to be used
  558. * @ timer: timer being set
  559. * @ expires: time at which to expire the timer
  560. * @ period: period that the timer will recur
  561. *
  562. * Kernel interface to set an rtc_timer
  563. */
  564. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  565. ktime_t expires, ktime_t period)
  566. {
  567. int ret = 0;
  568. mutex_lock(&rtc->ops_lock);
  569. if (timer->enabled)
  570. rtc_timer_remove(rtc, timer);
  571. timer->node.expires = expires;
  572. timer->period = period;
  573. ret = rtc_timer_enqueue(rtc, timer);
  574. mutex_unlock(&rtc->ops_lock);
  575. return ret;
  576. }
  577. /* rtc_timer_cancel - Stops an rtc_timer
  578. * @ rtc: rtc device to be used
  579. * @ timer: timer being set
  580. *
  581. * Kernel interface to cancel an rtc_timer
  582. */
  583. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
  584. {
  585. int ret = 0;
  586. mutex_lock(&rtc->ops_lock);
  587. if (timer->enabled)
  588. rtc_timer_remove(rtc, timer);
  589. mutex_unlock(&rtc->ops_lock);
  590. return ret;
  591. }