interface.c 24 KB

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