interface.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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->class_dev.dev, 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->class_dev.dev, 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->class_dev.dev, 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->class_dev.dev, &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->class_dev.dev,
  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->class_dev.dev, 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 = mutex_lock_interruptible(&rtc->ops_lock);
  106. if (err)
  107. return -EBUSY;
  108. if (!rtc->ops)
  109. err = -ENODEV;
  110. else if (!rtc->ops->set_alarm)
  111. err = -EINVAL;
  112. else
  113. err = rtc->ops->set_alarm(rtc->class_dev.dev, alarm);
  114. mutex_unlock(&rtc->ops_lock);
  115. return err;
  116. }
  117. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  118. /**
  119. * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
  120. * @rtc: the rtc device
  121. * @num: how many irqs are being reported (usually one)
  122. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  123. * Context: in_interrupt(), irqs blocked
  124. */
  125. void rtc_update_irq(struct rtc_device *rtc,
  126. unsigned long num, unsigned long events)
  127. {
  128. spin_lock(&rtc->irq_lock);
  129. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  130. spin_unlock(&rtc->irq_lock);
  131. spin_lock(&rtc->irq_task_lock);
  132. if (rtc->irq_task)
  133. rtc->irq_task->func(rtc->irq_task->private_data);
  134. spin_unlock(&rtc->irq_task_lock);
  135. wake_up_interruptible(&rtc->irq_queue);
  136. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  137. }
  138. EXPORT_SYMBOL_GPL(rtc_update_irq);
  139. struct rtc_device *rtc_class_open(char *name)
  140. {
  141. struct class_device *class_dev_tmp;
  142. struct rtc_device *rtc = NULL;
  143. down(&rtc_class->sem);
  144. list_for_each_entry(class_dev_tmp, &rtc_class->children, node) {
  145. if (strncmp(class_dev_tmp->class_id, name, BUS_ID_SIZE) == 0) {
  146. class_dev_tmp = class_device_get(class_dev_tmp);
  147. if (class_dev_tmp)
  148. rtc = to_rtc_device(class_dev_tmp);
  149. break;
  150. }
  151. }
  152. if (rtc) {
  153. if (!try_module_get(rtc->owner)) {
  154. class_device_put(class_dev_tmp);
  155. rtc = NULL;
  156. }
  157. }
  158. up(&rtc_class->sem);
  159. return rtc;
  160. }
  161. EXPORT_SYMBOL_GPL(rtc_class_open);
  162. void rtc_class_close(struct rtc_device *rtc)
  163. {
  164. module_put(rtc->owner);
  165. class_device_put(&rtc->class_dev);
  166. }
  167. EXPORT_SYMBOL_GPL(rtc_class_close);
  168. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  169. {
  170. int retval = -EBUSY;
  171. if (task == NULL || task->func == NULL)
  172. return -EINVAL;
  173. spin_lock_irq(&rtc->irq_task_lock);
  174. if (rtc->irq_task == NULL) {
  175. rtc->irq_task = task;
  176. retval = 0;
  177. }
  178. spin_unlock_irq(&rtc->irq_task_lock);
  179. return retval;
  180. }
  181. EXPORT_SYMBOL_GPL(rtc_irq_register);
  182. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  183. {
  184. spin_lock_irq(&rtc->irq_task_lock);
  185. if (rtc->irq_task == task)
  186. rtc->irq_task = NULL;
  187. spin_unlock_irq(&rtc->irq_task_lock);
  188. }
  189. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  190. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  191. {
  192. int err = 0;
  193. unsigned long flags;
  194. if (rtc->ops->irq_set_state == NULL)
  195. return -ENXIO;
  196. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  197. if (rtc->irq_task != task)
  198. err = -ENXIO;
  199. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  200. if (err == 0)
  201. err = rtc->ops->irq_set_state(rtc->class_dev.dev, enabled);
  202. return err;
  203. }
  204. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  205. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  206. {
  207. int err = 0;
  208. unsigned long flags;
  209. if (rtc->ops->irq_set_freq == NULL)
  210. return -ENXIO;
  211. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  212. if (rtc->irq_task != task)
  213. err = -ENXIO;
  214. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  215. if (err == 0) {
  216. err = rtc->ops->irq_set_freq(rtc->class_dev.dev, freq);
  217. if (err == 0)
  218. rtc->irq_freq = freq;
  219. }
  220. return err;
  221. }
  222. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);