cevt-ds1287.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DS1287 clockevent driver
  4. *
  5. * Copyright (C) 2008 Yoichi Yuasa <[email protected]>
  6. */
  7. #include <linux/clockchips.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mc146818rtc.h>
  11. #include <linux/irq.h>
  12. #include <asm/time.h>
  13. int ds1287_timer_state(void)
  14. {
  15. return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
  16. }
  17. int ds1287_set_base_clock(unsigned int hz)
  18. {
  19. u8 rate;
  20. switch (hz) {
  21. case 128:
  22. rate = 0x9;
  23. break;
  24. case 256:
  25. rate = 0x8;
  26. break;
  27. case 1024:
  28. rate = 0x6;
  29. break;
  30. default:
  31. return -EINVAL;
  32. }
  33. CMOS_WRITE(RTC_REF_CLCK_32KHZ | rate, RTC_REG_A);
  34. return 0;
  35. }
  36. static int ds1287_set_next_event(unsigned long delta,
  37. struct clock_event_device *evt)
  38. {
  39. return -EINVAL;
  40. }
  41. static int ds1287_shutdown(struct clock_event_device *evt)
  42. {
  43. u8 val;
  44. spin_lock(&rtc_lock);
  45. val = CMOS_READ(RTC_REG_B);
  46. val &= ~RTC_PIE;
  47. CMOS_WRITE(val, RTC_REG_B);
  48. spin_unlock(&rtc_lock);
  49. return 0;
  50. }
  51. static int ds1287_set_periodic(struct clock_event_device *evt)
  52. {
  53. u8 val;
  54. spin_lock(&rtc_lock);
  55. val = CMOS_READ(RTC_REG_B);
  56. val |= RTC_PIE;
  57. CMOS_WRITE(val, RTC_REG_B);
  58. spin_unlock(&rtc_lock);
  59. return 0;
  60. }
  61. static void ds1287_event_handler(struct clock_event_device *dev)
  62. {
  63. }
  64. static struct clock_event_device ds1287_clockevent = {
  65. .name = "ds1287",
  66. .features = CLOCK_EVT_FEAT_PERIODIC,
  67. .set_next_event = ds1287_set_next_event,
  68. .set_state_shutdown = ds1287_shutdown,
  69. .set_state_periodic = ds1287_set_periodic,
  70. .tick_resume = ds1287_shutdown,
  71. .event_handler = ds1287_event_handler,
  72. };
  73. static irqreturn_t ds1287_interrupt(int irq, void *dev_id)
  74. {
  75. struct clock_event_device *cd = &ds1287_clockevent;
  76. /* Ack the RTC interrupt. */
  77. CMOS_READ(RTC_REG_C);
  78. cd->event_handler(cd);
  79. return IRQ_HANDLED;
  80. }
  81. int __init ds1287_clockevent_init(int irq)
  82. {
  83. unsigned long flags = IRQF_PERCPU | IRQF_TIMER;
  84. struct clock_event_device *cd;
  85. cd = &ds1287_clockevent;
  86. cd->rating = 100;
  87. cd->irq = irq;
  88. clockevent_set_clock(cd, 32768);
  89. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  90. cd->max_delta_ticks = 0x7fffffff;
  91. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  92. cd->min_delta_ticks = 0x300;
  93. cd->cpumask = cpumask_of(0);
  94. clockevents_register_device(&ds1287_clockevent);
  95. return request_irq(irq, ds1287_interrupt, flags, "ds1287", NULL);
  96. }