rtc-test.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 int test_mmss64;
  15. module_param(test_mmss64, int, 0644);
  16. MODULE_PARM_DESC(test_mmss64, "Test struct rtc_class_ops.set_mmss64().");
  17. static struct platform_device *test0 = NULL, *test1 = NULL;
  18. static int test_rtc_read_alarm(struct device *dev,
  19. struct rtc_wkalrm *alrm)
  20. {
  21. return 0;
  22. }
  23. static int test_rtc_set_alarm(struct device *dev,
  24. struct rtc_wkalrm *alrm)
  25. {
  26. return 0;
  27. }
  28. static int test_rtc_read_time(struct device *dev,
  29. struct rtc_time *tm)
  30. {
  31. rtc_time64_to_tm(ktime_get_real_seconds(), tm);
  32. return 0;
  33. }
  34. static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
  35. {
  36. dev_info(dev, "%s, secs = %lld\n", __func__, (long long)secs);
  37. return 0;
  38. }
  39. static int test_rtc_set_mmss(struct device *dev, unsigned long secs)
  40. {
  41. dev_info(dev, "%s, secs = %lu\n", __func__, secs);
  42. return 0;
  43. }
  44. static int test_rtc_proc(struct device *dev, struct seq_file *seq)
  45. {
  46. struct platform_device *plat_dev = to_platform_device(dev);
  47. seq_printf(seq, "test\t\t: yes\n");
  48. seq_printf(seq, "id\t\t: %d\n", plat_dev->id);
  49. return 0;
  50. }
  51. static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  52. {
  53. return 0;
  54. }
  55. static struct rtc_class_ops test_rtc_ops = {
  56. .proc = test_rtc_proc,
  57. .read_time = test_rtc_read_time,
  58. .read_alarm = test_rtc_read_alarm,
  59. .set_alarm = test_rtc_set_alarm,
  60. .set_mmss = test_rtc_set_mmss,
  61. .alarm_irq_enable = test_rtc_alarm_irq_enable,
  62. };
  63. static ssize_t test_irq_show(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. return sprintf(buf, "%d\n", 42);
  67. }
  68. static ssize_t test_irq_store(struct device *dev,
  69. struct device_attribute *attr,
  70. const char *buf, size_t count)
  71. {
  72. int retval;
  73. struct platform_device *plat_dev = to_platform_device(dev);
  74. struct rtc_device *rtc = platform_get_drvdata(plat_dev);
  75. retval = count;
  76. if (strncmp(buf, "tick", 4) == 0 && rtc->pie_enabled)
  77. rtc_update_irq(rtc, 1, RTC_PF | RTC_IRQF);
  78. else if (strncmp(buf, "alarm", 5) == 0) {
  79. struct rtc_wkalrm alrm;
  80. int err = rtc_read_alarm(rtc, &alrm);
  81. if (!err && alrm.enabled)
  82. rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
  83. } else if (strncmp(buf, "update", 6) == 0 && rtc->uie_rtctimer.enabled)
  84. rtc_update_irq(rtc, 1, RTC_UF | RTC_IRQF);
  85. else
  86. retval = -EINVAL;
  87. return retval;
  88. }
  89. static DEVICE_ATTR(irq, S_IRUGO | S_IWUSR, test_irq_show, test_irq_store);
  90. static int test_probe(struct platform_device *plat_dev)
  91. {
  92. int err;
  93. struct rtc_device *rtc;
  94. if (test_mmss64) {
  95. test_rtc_ops.set_mmss64 = test_rtc_set_mmss64;
  96. test_rtc_ops.set_mmss = NULL;
  97. }
  98. rtc = devm_rtc_device_register(&plat_dev->dev, "test",
  99. &test_rtc_ops, THIS_MODULE);
  100. if (IS_ERR(rtc)) {
  101. return PTR_ERR(rtc);
  102. }
  103. err = device_create_file(&plat_dev->dev, &dev_attr_irq);
  104. if (err)
  105. dev_err(&plat_dev->dev, "Unable to create sysfs entry: %s\n",
  106. dev_attr_irq.attr.name);
  107. platform_set_drvdata(plat_dev, rtc);
  108. return 0;
  109. }
  110. static int test_remove(struct platform_device *plat_dev)
  111. {
  112. device_remove_file(&plat_dev->dev, &dev_attr_irq);
  113. return 0;
  114. }
  115. static struct platform_driver test_driver = {
  116. .probe = test_probe,
  117. .remove = test_remove,
  118. .driver = {
  119. .name = "rtc-test",
  120. },
  121. };
  122. static int __init test_init(void)
  123. {
  124. int err;
  125. if ((err = platform_driver_register(&test_driver)))
  126. return err;
  127. if ((test0 = platform_device_alloc("rtc-test", 0)) == NULL) {
  128. err = -ENOMEM;
  129. goto exit_driver_unregister;
  130. }
  131. if ((test1 = platform_device_alloc("rtc-test", 1)) == NULL) {
  132. err = -ENOMEM;
  133. goto exit_put_test0;
  134. }
  135. if ((err = platform_device_add(test0)))
  136. goto exit_put_test1;
  137. if ((err = platform_device_add(test1)))
  138. goto exit_del_test0;
  139. return 0;
  140. exit_del_test0:
  141. platform_device_del(test0);
  142. exit_put_test1:
  143. platform_device_put(test1);
  144. exit_put_test0:
  145. platform_device_put(test0);
  146. exit_driver_unregister:
  147. platform_driver_unregister(&test_driver);
  148. return err;
  149. }
  150. static void __exit test_exit(void)
  151. {
  152. platform_device_unregister(test0);
  153. platform_device_unregister(test1);
  154. platform_driver_unregister(&test_driver);
  155. }
  156. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  157. MODULE_DESCRIPTION("RTC test driver/device");
  158. MODULE_LICENSE("GPL");
  159. module_init(test_init);
  160. module_exit(test_exit);