interface.c 24 KB

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