interface.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  15. {
  16. int err;
  17. err = mutex_lock_interruptible(&rtc->ops_lock);
  18. if (err)
  19. return -EBUSY;
  20. if (!rtc->ops)
  21. err = -ENODEV;
  22. else if (!rtc->ops->read_time)
  23. err = -EINVAL;
  24. else {
  25. memset(tm, 0, sizeof(struct rtc_time));
  26. err = rtc->ops->read_time(rtc->dev.parent, tm);
  27. }
  28. mutex_unlock(&rtc->ops_lock);
  29. return err;
  30. }
  31. EXPORT_SYMBOL_GPL(rtc_read_time);
  32. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  33. {
  34. int err;
  35. err = rtc_valid_tm(tm);
  36. if (err != 0)
  37. return err;
  38. err = mutex_lock_interruptible(&rtc->ops_lock);
  39. if (err)
  40. return -EBUSY;
  41. if (!rtc->ops)
  42. err = -ENODEV;
  43. else if (!rtc->ops->set_time)
  44. err = -EINVAL;
  45. else
  46. err = rtc->ops->set_time(rtc->dev.parent, tm);
  47. mutex_unlock(&rtc->ops_lock);
  48. return err;
  49. }
  50. EXPORT_SYMBOL_GPL(rtc_set_time);
  51. int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
  52. {
  53. int err;
  54. err = mutex_lock_interruptible(&rtc->ops_lock);
  55. if (err)
  56. return -EBUSY;
  57. if (!rtc->ops)
  58. err = -ENODEV;
  59. else if (rtc->ops->set_mmss)
  60. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  61. else if (rtc->ops->read_time && rtc->ops->set_time) {
  62. struct rtc_time new, old;
  63. err = rtc->ops->read_time(rtc->dev.parent, &old);
  64. if (err == 0) {
  65. rtc_time_to_tm(secs, &new);
  66. /*
  67. * avoid writing when we're going to change the day of
  68. * the month. We will retry in the next minute. This
  69. * basically means that if the RTC must not drift
  70. * by more than 1 minute in 11 minutes.
  71. */
  72. if (!((old.tm_hour == 23 && old.tm_min == 59) ||
  73. (new.tm_hour == 23 && new.tm_min == 59)))
  74. err = rtc->ops->set_time(rtc->dev.parent,
  75. &new);
  76. }
  77. }
  78. else
  79. err = -EINVAL;
  80. mutex_unlock(&rtc->ops_lock);
  81. return err;
  82. }
  83. EXPORT_SYMBOL_GPL(rtc_set_mmss);
  84. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  85. {
  86. int err;
  87. err = mutex_lock_interruptible(&rtc->ops_lock);
  88. if (err)
  89. return -EBUSY;
  90. if (rtc->ops == NULL)
  91. err = -ENODEV;
  92. else if (!rtc->ops->read_alarm)
  93. err = -EINVAL;
  94. else {
  95. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  96. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  97. }
  98. mutex_unlock(&rtc->ops_lock);
  99. return err;
  100. }
  101. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  102. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  103. {
  104. int err;
  105. err = rtc_valid_tm(&alarm->time);
  106. if (err != 0)
  107. return err;
  108. err = mutex_lock_interruptible(&rtc->ops_lock);
  109. if (err)
  110. return -EBUSY;
  111. if (!rtc->ops)
  112. err = -ENODEV;
  113. else if (!rtc->ops->set_alarm)
  114. err = -EINVAL;
  115. else
  116. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  117. mutex_unlock(&rtc->ops_lock);
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  121. /**
  122. * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
  123. * @rtc: the rtc device
  124. * @num: how many irqs are being reported (usually one)
  125. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  126. * Context: in_interrupt(), irqs blocked
  127. */
  128. void rtc_update_irq(struct rtc_device *rtc,
  129. unsigned long num, unsigned long events)
  130. {
  131. spin_lock(&rtc->irq_lock);
  132. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  133. spin_unlock(&rtc->irq_lock);
  134. spin_lock(&rtc->irq_task_lock);
  135. if (rtc->irq_task)
  136. rtc->irq_task->func(rtc->irq_task->private_data);
  137. spin_unlock(&rtc->irq_task_lock);
  138. wake_up_interruptible(&rtc->irq_queue);
  139. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  140. }
  141. EXPORT_SYMBOL_GPL(rtc_update_irq);
  142. struct rtc_device *rtc_class_open(char *name)
  143. {
  144. struct device *dev;
  145. struct rtc_device *rtc = NULL;
  146. down(&rtc_class->sem);
  147. list_for_each_entry(dev, &rtc_class->devices, node) {
  148. if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0) {
  149. dev = get_device(dev);
  150. if (dev)
  151. rtc = to_rtc_device(dev);
  152. break;
  153. }
  154. }
  155. if (rtc) {
  156. if (!try_module_get(rtc->owner)) {
  157. put_device(dev);
  158. rtc = NULL;
  159. }
  160. }
  161. up(&rtc_class->sem);
  162. return rtc;
  163. }
  164. EXPORT_SYMBOL_GPL(rtc_class_open);
  165. void rtc_class_close(struct rtc_device *rtc)
  166. {
  167. module_put(rtc->owner);
  168. put_device(&rtc->dev);
  169. }
  170. EXPORT_SYMBOL_GPL(rtc_class_close);
  171. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  172. {
  173. int retval = -EBUSY;
  174. if (task == NULL || task->func == NULL)
  175. return -EINVAL;
  176. spin_lock_irq(&rtc->irq_task_lock);
  177. if (rtc->irq_task == NULL) {
  178. rtc->irq_task = task;
  179. retval = 0;
  180. }
  181. spin_unlock_irq(&rtc->irq_task_lock);
  182. return retval;
  183. }
  184. EXPORT_SYMBOL_GPL(rtc_irq_register);
  185. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  186. {
  187. spin_lock_irq(&rtc->irq_task_lock);
  188. if (rtc->irq_task == task)
  189. rtc->irq_task = NULL;
  190. spin_unlock_irq(&rtc->irq_task_lock);
  191. }
  192. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  193. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  194. {
  195. int err = 0;
  196. unsigned long flags;
  197. if (rtc->ops->irq_set_state == NULL)
  198. return -ENXIO;
  199. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  200. if (rtc->irq_task != task)
  201. err = -ENXIO;
  202. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  203. if (err == 0)
  204. err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
  205. return err;
  206. }
  207. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  208. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  209. {
  210. int err = 0;
  211. unsigned long flags;
  212. if (rtc->ops->irq_set_freq == NULL)
  213. return -ENXIO;
  214. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  215. if (rtc->irq_task != task)
  216. err = -ENXIO;
  217. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  218. if (err == 0) {
  219. err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
  220. if (err == 0)
  221. rtc->irq_freq = freq;
  222. }
  223. return err;
  224. }
  225. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);