rtc-test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * An RTC test device/driver
  3. * Copyright (C) 2005 Tower Technologies
  4. * Author: Alessandro Zummo <a.zummo@towertech.it>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/rtc.h>
  13. #include <linux/platform_device.h>
  14. static struct platform_device *test0 = NULL, *test1 = NULL;
  15. static int test_rtc_read_alarm(struct device *dev,
  16. struct rtc_wkalrm *alrm)
  17. {
  18. return 0;
  19. }
  20. static int test_rtc_set_alarm(struct device *dev,
  21. struct rtc_wkalrm *alrm)
  22. {
  23. return 0;
  24. }
  25. static int test_rtc_read_time(struct device *dev,
  26. struct rtc_time *tm)
  27. {
  28. rtc_time_to_tm(get_seconds(), tm);
  29. return 0;
  30. }
  31. static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
  32. {
  33. dev_info(dev, "%s, secs = %lu\n", __func__, secs);
  34. return 0;
  35. }
  36. static int test_rtc_proc(struct device *dev, struct seq_file *seq)
  37. {
  38. struct platform_device *plat_dev = to_platform_device(dev);
  39. seq_printf(seq, "test\t\t: yes\n");
  40. seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
  41. return 0;
  42. }
  43. static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  44. {
  45. return 0;
  46. }
  47. static const struct rtc_class_ops test_rtc_ops = {
  48. .proc = test_rtc_proc,
  49. .read_time = test_rtc_read_time,
  50. .read_alarm = test_rtc_read_alarm,
  51. .set_alarm = test_rtc_set_alarm,
  52. .set_mmss = test_rtc_set_mmss,
  53. .alarm_irq_enable = test_rtc_alarm_irq_enable,
  54. };
  55. static ssize_t test_irq_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. return sprintf(buf, "%d\n", 42);
  59. }
  60. static ssize_t test_irq_store(struct device *dev,
  61. struct device_attribute *attr,
  62. const char *buf, size_t count)
  63. {
  64. int retval;
  65. struct platform_device *plat_dev = to_platform_device(dev);
  66. struct rtc_device *rtc = platform_get_drvdata(plat_dev);
  67. retval = count;
  68. if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
  69. rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
  70. else if (strncmp(buf, "alarm", 5) == 0) {
  71. struct rtc_wkalrm alrm;
  72. int err = rtc_read_alarm(rtc, &alrm);
  73. if (!err && alrm.enabled)
  74. rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
  75. } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
  76. rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
  77. else
  78. retval = -EINVAL;
  79. return retval;
  80. }
  81. static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
  82. static int test_probe(struct platform_device *plat_dev)
  83. {
  84. int err;
  85. struct rtc_device *rtc = rtc_device_register("test", &plat_dev->dev,
  86. &test_rtc_ops, THIS_MODULE);
  87. if (IS_ERR(rtc)) {
  88. err = PTR_ERR(rtc);
  89. return err;
  90. }
  91. err = device_create_file(&plat_dev->dev, &dev_attr_irq);
  92. if (err)
  93. goto err;
  94. platform_set_drvdata(plat_dev, rtc);
  95. return 0;
  96. err:
  97. rtc_device_unregister(rtc);
  98. return err;
  99. }
  100. static int __devexit test_remove(struct platform_device *plat_dev)
  101. {
  102. struct rtc_device *rtc = platform_get_drvdata(plat_dev);
  103. rtc_device_unregister(rtc);
  104. device_remove_file(&plat_dev->dev, &dev_attr_irq);
  105. return 0;
  106. }
  107. static struct platform_driver test_driver = {
  108. .probe = test_probe,
  109. .remove = __devexit_p(test_remove),
  110. .driver = {
  111. .name = "rtc-test",
  112. .owner = THIS_MODULE,
  113. },
  114. };
  115. static int __init test_init(void)
  116. {
  117. int err;
  118. if ((err = platform_driver_register(&test_driver)))
  119. return err;
  120. if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
  121. err = -ENOMEM;
  122. goto exit_driver_unregister;
  123. }
  124. if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
  125. err = -ENOMEM;
  126. goto exit_free_test0;
  127. }
  128. if ((err = platform_device_add(test0)))
  129. goto exit_free_test1;
  130. if ((err = platform_device_add(test1)))
  131. goto exit_device_unregister;
  132. return 0;
  133. exit_device_unregister:
  134. platform_device_unregister(test0);
  135. exit_free_test1:
  136. platform_device_put(test1);
  137. exit_free_test0:
  138. platform_device_put(test0);
  139. exit_driver_unregister:
  140. platform_driver_unregister(&test_driver);
  141. return err;
  142. }
  143. static void __exit test_exit(void)
  144. {
  145. platform_device_unregister(test0);
  146. platform_device_unregister(test1);
  147. platform_driver_unregister(&test_driver);
  148. }
  149. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  150. MODULE_DESCRIPTION("RTC test driver/device");
  151. MODULE_LICENSE("GPL");
  152. module_init(test_init);
  153. module_exit(test_exit);