interface.c 24 KB

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