rtc-pcap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 __devinit 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 = kmalloc(sizeof(struct pcap_rtc), GFP_KERNEL);
  118. if (!pcap_rtc)
  119. return err;
  120. pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
  121. platform_set_drvdata(pdev, pcap_rtc);
  122. pcap_rtc->rtc = rtc_device_register("pcap", &pdev->dev,
  123. &pcap_rtc_ops, THIS_MODULE);
  124. if (IS_ERR(pcap_rtc->rtc)) {
  125. err = PTR_ERR(pcap_rtc->rtc);
  126. goto fail_rtc;
  127. }
  128. timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
  129. alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
  130. err = request_irq(timer_irq, pcap_rtc_irq, 0, "RTC Timer", pcap_rtc);
  131. if (err)
  132. goto fail_timer;
  133. err = request_irq(alarm_irq, pcap_rtc_irq, 0, "RTC Alarm", pcap_rtc);
  134. if (err)
  135. goto fail_alarm;
  136. return 0;
  137. fail_alarm:
  138. free_irq(timer_irq, pcap_rtc);
  139. fail_timer:
  140. rtc_device_unregister(pcap_rtc->rtc);
  141. fail_rtc:
  142. platform_set_drvdata(pdev, NULL);
  143. kfree(pcap_rtc);
  144. return err;
  145. }
  146. static int __devexit pcap_rtc_remove(struct platform_device *pdev)
  147. {
  148. struct pcap_rtc *pcap_rtc = platform_get_drvdata(pdev);
  149. free_irq(pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ), pcap_rtc);
  150. free_irq(pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA), pcap_rtc);
  151. rtc_device_unregister(pcap_rtc->rtc);
  152. kfree(pcap_rtc);
  153. return 0;
  154. }
  155. static struct platform_driver pcap_rtc_driver = {
  156. .remove = __devexit_p(pcap_rtc_remove),
  157. .driver = {
  158. .name = "pcap-rtc",
  159. .owner = THIS_MODULE,
  160. },
  161. };
  162. static int __init rtc_pcap_init(void)
  163. {
  164. return platform_driver_probe(&pcap_rtc_driver, pcap_rtc_probe);
  165. }
  166. static void __exit rtc_pcap_exit(void)
  167. {
  168. platform_driver_unregister(&pcap_rtc_driver);
  169. }
  170. module_init(rtc_pcap_init);
  171. module_exit(rtc_pcap_exit);
  172. MODULE_DESCRIPTION("Motorola pcap rtc driver");
  173. MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
  174. MODULE_LICENSE("GPL");