rtc-test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #define MAX_RTC_TEST 3
  15. struct rtc_test_data {
  16. struct rtc_device *rtc;
  17. time64_t offset;
  18. struct timer_list alarm;
  19. bool alarm_en;
  20. };
  21. static struct platform_device *pdev[MAX_RTC_TEST];
  22. static int test_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  23. {
  24. struct rtc_test_data *rtd = dev_get_drvdata(dev);
  25. time64_t alarm;
  26. alarm = (rtd->alarm.expires - jiffies) / HZ;
  27. alarm += ktime_get_real_seconds() + rtd->offset;
  28. rtc_time64_to_tm(alarm, &alrm->time);
  29. alrm->enabled = rtd->alarm_en;
  30. return 0;
  31. }
  32. static int test_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  33. {
  34. struct rtc_test_data *rtd = dev_get_drvdata(dev);
  35. ktime_t timeout;
  36. u64 expires;
  37. timeout = rtc_tm_to_time64(&alrm->time) - ktime_get_real_seconds();
  38. timeout -= rtd->offset;
  39. del_timer(&rtd->alarm);
  40. expires = jiffies + timeout * HZ;
  41. if (expires > U32_MAX)
  42. expires = U32_MAX;
  43. pr_err("ABE: %s +%d %s\n", __FILE__, __LINE__, __func__);
  44. rtd->alarm.expires = expires;
  45. if (alrm->enabled)
  46. add_timer(&rtd->alarm);
  47. rtd->alarm_en = alrm->enabled;
  48. return 0;
  49. }
  50. static int test_rtc_read_time(struct device *dev, struct rtc_time *tm)
  51. {
  52. struct rtc_test_data *rtd = dev_get_drvdata(dev);
  53. rtc_time64_to_tm(ktime_get_real_seconds() + rtd->offset, tm);
  54. return 0;
  55. }
  56. static int test_rtc_set_mmss64(struct device *dev, time64_t secs)
  57. {
  58. struct rtc_test_data *rtd = dev_get_drvdata(dev);
  59. rtd->offset = secs - ktime_get_real_seconds();
  60. return 0;
  61. }
  62. static int test_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  63. {
  64. struct rtc_test_data *rtd = dev_get_drvdata(dev);
  65. rtd->alarm_en = enable;
  66. if (enable)
  67. add_timer(&rtd->alarm);
  68. else
  69. del_timer(&rtd->alarm);
  70. return 0;
  71. }
  72. static const struct rtc_class_ops test_rtc_ops_noalm = {
  73. .read_time = test_rtc_read_time,
  74. .set_mmss64 = test_rtc_set_mmss64,
  75. .alarm_irq_enable = test_rtc_alarm_irq_enable,
  76. };
  77. static const struct rtc_class_ops test_rtc_ops = {
  78. .read_time = test_rtc_read_time,
  79. .read_alarm = test_rtc_read_alarm,
  80. .set_alarm = test_rtc_set_alarm,
  81. .set_mmss64 = test_rtc_set_mmss64,
  82. .alarm_irq_enable = test_rtc_alarm_irq_enable,
  83. };
  84. static void test_rtc_alarm_handler(struct timer_list *t)
  85. {
  86. struct rtc_test_data *rtd = from_timer(rtd, t, alarm);
  87. rtc_update_irq(rtd->rtc, 1, RTC_AF | RTC_IRQF);
  88. }
  89. static int test_probe(struct platform_device *plat_dev)
  90. {
  91. struct rtc_test_data *rtd;
  92. rtd = devm_kzalloc(&plat_dev->dev, sizeof(*rtd), GFP_KERNEL);
  93. if (!rtd)
  94. return -ENOMEM;
  95. platform_set_drvdata(plat_dev, rtd);
  96. rtd->rtc = devm_rtc_allocate_device(&plat_dev->dev);
  97. if (IS_ERR(rtd->rtc))
  98. return PTR_ERR(rtd->rtc);
  99. switch (plat_dev->id) {
  100. case 0:
  101. rtd->rtc->ops = &test_rtc_ops_noalm;
  102. break;
  103. default:
  104. rtd->rtc->ops = &test_rtc_ops;
  105. }
  106. timer_setup(&rtd->alarm, test_rtc_alarm_handler, 0);
  107. rtd->alarm.expires = 0;
  108. return rtc_register_device(rtd->rtc);
  109. }
  110. static struct platform_driver test_driver = {
  111. .probe = test_probe,
  112. .driver = {
  113. .name = "rtc-test",
  114. },
  115. };
  116. static int __init test_init(void)
  117. {
  118. int i, err;
  119. if ((err = platform_driver_register(&test_driver)))
  120. return err;
  121. err = -ENOMEM;
  122. for (i = 0; i < MAX_RTC_TEST; i++) {
  123. pdev[i] = platform_device_alloc("rtc-test", i);
  124. if (!pdev[i])
  125. goto exit_free_mem;
  126. }
  127. for (i = 0; i < MAX_RTC_TEST; i++) {
  128. err = platform_device_add(pdev[i]);
  129. if (err)
  130. goto exit_device_del;
  131. }
  132. return 0;
  133. exit_device_del:
  134. for (; i > 0; i--)
  135. platform_device_del(pdev[i - 1]);
  136. exit_free_mem:
  137. for (i = 0; i < MAX_RTC_TEST; i++)
  138. platform_device_put(pdev[i]);
  139. platform_driver_unregister(&test_driver);
  140. return err;
  141. }
  142. static void __exit test_exit(void)
  143. {
  144. int i;
  145. for (i = 0; i < MAX_RTC_TEST; i++)
  146. platform_device_unregister(pdev[i]);
  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);