rtc-pcap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * pcap rtc code for Motorola EZX phones
  3. *
  4. * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com>
  5. * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
  6. *
  7. * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola
  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. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/mfd/ezx-pcap.h>
  18. #include <linux/rtc.h>
  19. #include <linux/slab.h>
  20. #include <linux/platform_device.h>
  21. struct pcap_rtc {
  22. struct pcap_chip *pcap;
  23. struct rtc_device *rtc;
  24. };
  25. static irqreturn_t pcap_rtc_irq(int irq, void *_pcap_rtc)
  26. {
  27. struct pcap_rtc *pcap_rtc = _pcap_rtc;
  28. unsigned long rtc_events;
  29. if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ))
  30. rtc_events = RTC_IRQF | RTC_UF;
  31. else if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA))
  32. rtc_events = RTC_IRQF | RTC_AF;
  33. else
  34. rtc_events = 0;
  35. rtc_update_irq(pcap_rtc->rtc, 1, rtc_events);
  36. return IRQ_HANDLED;
  37. }
  38. static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  39. {
  40. struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
  41. struct rtc_time *tm = &alrm->time;
  42. unsigned long secs;
  43. u32 tod; /* time of day, seconds since midnight */
  44. u32 days; /* days since 1/1/1970 */
  45. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod);
  46. secs = tod & PCAP_RTC_TOD_MASK;
  47. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days);
  48. secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
  49. rtc_time_to_tm(secs, tm);
  50. return 0;
  51. }
  52. static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  53. {
  54. struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
  55. struct rtc_time *tm = &alrm->time;
  56. unsigned long secs;
  57. u32 tod, days;
  58. rtc_tm_to_time(tm, &secs);
  59. tod = secs % SEC_PER_DAY;
  60. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod);
  61. days = secs / SEC_PER_DAY;
  62. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days);
  63. return 0;
  64. }
  65. static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
  68. unsigned long secs;
  69. u32 tod, days;
  70. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod);
  71. secs = tod & PCAP_RTC_TOD_MASK;
  72. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days);
  73. secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
  74. rtc_time_to_tm(secs, tm);
  75. return 0;
  76. }
  77. static int pcap_rtc_set_mmss(struct device *dev, unsigned long secs)
  78. {
  79. struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
  80. u32 tod, days;
  81. tod = secs % SEC_PER_DAY;
  82. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod);
  83. days = secs / SEC_PER_DAY;
  84. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days);
  85. return 0;
  86. }
  87. static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en)
  88. {
  89. struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
  90. if (en)
  91. enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
  92. else
  93. disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
  94. return 0;
  95. }
  96. static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
  97. {
  98. return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en);
  99. }
  100. static const struct rtc_class_ops pcap_rtc_ops = {
  101. .read_time = pcap_rtc_read_time,
  102. .read_alarm = pcap_rtc_read_alarm,
  103. .set_alarm = pcap_rtc_set_alarm,
  104. .set_mmss = pcap_rtc_set_mmss,
  105. .alarm_irq_enable = pcap_rtc_alarm_irq_enable,
  106. };
  107. static int __init pcap_rtc_probe(struct platform_device *pdev)
  108. {
  109. struct pcap_rtc *pcap_rtc;
  110. int timer_irq, alarm_irq;
  111. int err = -ENOMEM;
  112. pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc),
  113. GFP_KERNEL);
  114. if (!pcap_rtc)
  115. return err;
  116. pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
  117. platform_set_drvdata(pdev, pcap_rtc);
  118. pcap_rtc->rtc = devm_rtc_device_register(&pdev->dev, "pcap",
  119. &pcap_rtc_ops, THIS_MODULE);
  120. if (IS_ERR(pcap_rtc->rtc))
  121. return PTR_ERR(pcap_rtc->rtc);
  122. timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
  123. alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
  124. err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0,
  125. "RTC Timer", pcap_rtc);
  126. if (err)
  127. return err;
  128. err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0,
  129. "RTC Alarm", pcap_rtc);
  130. if (err)
  131. return err;
  132. return 0;
  133. }
  134. static int __exit pcap_rtc_remove(struct platform_device *pdev)
  135. {
  136. return 0;
  137. }
  138. static struct platform_driver pcap_rtc_driver = {
  139. .remove = __exit_p(pcap_rtc_remove),
  140. .driver = {
  141. .name = "pcap-rtc",
  142. },
  143. };
  144. module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe);
  145. MODULE_DESCRIPTION("Motorola pcap rtc driver");
  146. MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
  147. MODULE_LICENSE("GPL");