interface.c 24 KB

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