interface.c 15 KB

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