interface.c 24 KB

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