interface.c 6.4 KB

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