rtc-pcap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 platform_device *pdev = to_platform_device(dev);
  41. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  42. struct rtc_time *tm = &alrm->time;
  43. unsigned long secs;
  44. u32 tod; /* time of day, seconds since midnight */
  45. u32 days; /* days since 1/1/1970 */
  46. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod);
  47. secs = tod & PCAP_RTC_TOD_MASK;
  48. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days);
  49. secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
  50. rtc_time_to_tm(secs, tm);
  51. return 0;
  52. }
  53. static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  54. {
  55. struct platform_device *pdev = to_platform_device(dev);
  56. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  57. struct rtc_time *tm = &alrm->time;
  58. unsigned long secs;
  59. u32 tod, days;
  60. rtc_tm_to_time(tm, &secs);
  61. tod = secs % SEC_PER_DAY;
  62. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod);
  63. days = secs / SEC_PER_DAY;
  64. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days);
  65. return 0;
  66. }
  67. static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm)
  68. {
  69. struct platform_device *pdev = to_platform_device(dev);
  70. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  71. unsigned long secs;
  72. u32 tod, days;
  73. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod);
  74. secs = tod & PCAP_RTC_TOD_MASK;
  75. ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days);
  76. secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
  77. rtc_time_to_tm(secs, tm);
  78. return rtc_valid_tm(tm);
  79. }
  80. static int pcap_rtc_set_mmss(struct device *dev, unsigned long secs)
  81. {
  82. struct platform_device *pdev = to_platform_device(dev);
  83. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  84. u32 tod, days;
  85. tod = secs % SEC_PER_DAY;
  86. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod);
  87. days = secs / SEC_PER_DAY;
  88. ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days);
  89. return 0;
  90. }
  91. static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en)
  92. {
  93. struct platform_device *pdev = to_platform_device(dev);
  94. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  95. if (en)
  96. enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
  97. else
  98. disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
  99. return 0;
  100. }
  101. static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
  102. {
  103. return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en);
  104. }
  105. static const struct rtc_class_ops pcap_rtc_ops = {
  106. .read_time = pcap_rtc_read_time,
  107. .read_alarm = pcap_rtc_read_alarm,
  108. .set_alarm = pcap_rtc_set_alarm,
  109. .set_mmss = pcap_rtc_set_mmss,
  110. .alarm_irq_enable = pcap_rtc_alarm_irq_enable,
  111. };
  112. static int __init pcap_rtc_probe(struct platform_device *pdev)
  113. {
  114. struct pcap_rtc *pcap_rtc;
  115. int timer_irq, alarm_irq;
  116. int err = -ENOMEM;
  117. pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc),
  118. GFP_KERNEL);
  119. if (!pcap_rtc)
  120. return err;
  121. pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
  122. platform_set_drvdata(pdev, pcap_rtc);
  123. pcap_rtc->rtc = devm_rtc_device_register(&pdev->dev, "pcap",
  124. &pcap_rtc_ops, THIS_MODULE);
  125. if (IS_ERR(pcap_rtc->rtc))
  126. return PTR_ERR(pcap_rtc->rtc);
  127. timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
  128. alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
  129. err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0,
  130. "RTC Timer", pcap_rtc);
  131. if (err)
  132. return err;
  133. err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0,
  134. "RTC Alarm", pcap_rtc);
  135. if (err)
  136. return err;
  137. return 0;
  138. }
  139. static int __exit pcap_rtc_remove(struct platform_device *pdev)
  140. {
  141. return 0;
  142. }
  143. static struct platform_driver pcap_rtc_driver = {
  144. .remove = __exit_p(pcap_rtc_remove),
  145. .driver = {
  146. .name = "pcap-rtc",
  147. },
  148. };
  149. module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe);
  150. MODULE_DESCRIPTION("Motorola pcap rtc driver");
  151. MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
  152. MODULE_LICENSE("GPL");