interface.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. void rtc_update_irq(struct class_device *class_dev,
  123. unsigned long num, unsigned long events)
  124. {
  125. struct rtc_device *rtc = to_rtc_device(class_dev);
  126. spin_lock(&rtc->irq_lock);
  127. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  128. spin_unlock(&rtc->irq_lock);
  129. spin_lock(&rtc->irq_task_lock);
  130. if (rtc->irq_task)
  131. rtc->irq_task->func(rtc->irq_task->private_data);
  132. spin_unlock(&rtc->irq_task_lock);
  133. wake_up_interruptible(&rtc->irq_queue);
  134. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  135. }
  136. EXPORT_SYMBOL_GPL(rtc_update_irq);
  137. struct class_device *rtc_class_open(char *name)
  138. {
  139. struct class_device *class_dev = NULL,
  140. *class_dev_tmp;
  141. down(&rtc_class->sem);
  142. list_for_each_entry(class_dev_tmp, &rtc_class->children, node) {
  143. if (strncmp(class_dev_tmp->class_id, name, BUS_ID_SIZE) == 0) {
  144. class_dev = class_dev_tmp;
  145. break;
  146. }
  147. }
  148. if (class_dev) {
  149. if (!try_module_get(to_rtc_device(class_dev)->owner))
  150. class_dev = NULL;
  151. }
  152. up(&rtc_class->sem);
  153. return class_dev;
  154. }
  155. EXPORT_SYMBOL_GPL(rtc_class_open);
  156. void rtc_class_close(struct class_device *class_dev)
  157. {
  158. module_put(to_rtc_device(class_dev)->owner);
  159. }
  160. EXPORT_SYMBOL_GPL(rtc_class_close);
  161. int rtc_irq_register(struct class_device *class_dev, struct rtc_task *task)
  162. {
  163. int retval = -EBUSY;
  164. struct rtc_device *rtc = to_rtc_device(class_dev);
  165. if (task == NULL || task->func == NULL)
  166. return -EINVAL;
  167. spin_lock(&rtc->irq_task_lock);
  168. if (rtc->irq_task == NULL) {
  169. rtc->irq_task = task;
  170. retval = 0;
  171. }
  172. spin_unlock(&rtc->irq_task_lock);
  173. return retval;
  174. }
  175. EXPORT_SYMBOL_GPL(rtc_irq_register);
  176. void rtc_irq_unregister(struct class_device *class_dev, struct rtc_task *task)
  177. {
  178. struct rtc_device *rtc = to_rtc_device(class_dev);
  179. spin_lock(&rtc->irq_task_lock);
  180. if (rtc->irq_task == task)
  181. rtc->irq_task = NULL;
  182. spin_unlock(&rtc->irq_task_lock);
  183. }
  184. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  185. int rtc_irq_set_state(struct class_device *class_dev, struct rtc_task *task, int enabled)
  186. {
  187. int err = 0;
  188. unsigned long flags;
  189. struct rtc_device *rtc = to_rtc_device(class_dev);
  190. if (rtc->ops->irq_set_state == NULL)
  191. return -ENXIO;
  192. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  193. if (rtc->irq_task != task)
  194. err = -ENXIO;
  195. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  196. if (err == 0)
  197. err = rtc->ops->irq_set_state(class_dev->dev, enabled);
  198. return err;
  199. }
  200. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  201. int rtc_irq_set_freq(struct class_device *class_dev, struct rtc_task *task, int freq)
  202. {
  203. int err = 0;
  204. unsigned long flags;
  205. struct rtc_device *rtc = to_rtc_device(class_dev);
  206. if (rtc->ops->irq_set_freq == NULL)
  207. return -ENXIO;
  208. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  209. if (rtc->irq_task != task)
  210. err = -ENXIO;
  211. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  212. if (err == 0) {
  213. err = rtc->ops->irq_set_freq(class_dev->dev, freq);
  214. if (err == 0)
  215. rtc->irq_freq = freq;
  216. }
  217. return err;
  218. }