interface.c 24 KB

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