微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM-Linux驱动--RTC(实时时钟)驱动分析

ARM-Linux驱动--RTC(实时时钟)驱动分析

时间:11-20 来源:互联网 点击:

  1. writeb(bin2bcd(tm->writeb(bin2bcd(tm->tm_sec),base+S3C2410_RTCSEC);
  2. writeb(bin2bcd(tm->tm_min),base+S3C2410_RTCMIN);
  3. writeb(bin2bcd(tm->tm_hour),base+S3C2410_RTCHOUR);
  4. writeb(bin2bcd(tm->tm_mday),base+S3C2410_RTCDATE);
  5. writeb(bin2bcd(tm->tm_mon+1),base+S3C2410_RTCMON);
  6. writeb(bin2bcd(year),base+S3C2410_RTCYEAR);
  7. return0;
  8. }
  9. staticints3c_rtc_open(structdevice*dev)
  10. {
  11. structplatform_device*pdev=to_platform_device(dev);
  12. structrtc_device*rtc_dev=platform_get_drvdata(pdev);
  13. intret;
  14. ret=request_irq(s3c_rtc_tickno,s3c_rtc_tickirq,
  15. IRQF_DISABLED,"s3c2410-rtctick",rtc_dev);
  16. if(ret){
  17. dev_err(dev,"IRQ%derror%d\n",s3c_rtc_tickno,ret);
  18. gototick_err;
  19. }
  20. tick_err:
  21. returnret;
  22. }
  23. staticvoids3c_rtc_release(structdevice*dev)
  24. {
  25. structplatform_device*pdev=to_platform_device(dev);
  26. structrtc_device*rtc_dev=platform_get_drvdata(pdev);
  27. /*donotclearAIEhere,itmaybeneededforwake*/
  28. s3c_rtc_setpie(dev,0);
  29. free_irq(s3c_rtc_tickno,rtc_dev);
  30. }
  31. staticconststructrtc_class_opss3c_rtcops={
  32. .open=s3c_rtc_open,
  33. .release=s3c_rtc_release,
  34. .read_time=s3c_rtc_gettime,
  35. .set_time=s3c_rtc_settime,
  36. .irq_set_freq=s3c_rtc_setfreq,
  37. .irq_set_state=s3c_rtc_setpie,
  38. };
  39. staticvoids3c_rtc_enable(structplatform_device*pdev,inten)
  40. {
  41. void__iomem*base=s3c_rtc_base;
  42. unsignedinttmp;
  43. if(s3c_rtc_base==NULL)
  44. return;
  45. if(!en){
  46. tmp=readb(base+S3C2410_RTCCON);
  47. writeb(tmp&~S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);
  48. tmp=readb(base+S3C2410_TICNT);
  49. writeb(tmp&~S3C2410_TICNT_ENABLE,base+S3C2410_TICNT);
  50. }else{
  51. /*re-enablethedevice,andcheckitisok*/
  52. if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_RTCEN)==0){
  53. dev_info(&pdev->dev,"rtcdisabled,re-enabling\n");
  54. tmp=readb(base+S3C2410_RTCCON);
  55. writeb(tmp|S3C2410_RTCCON_RTCEN,base+S3C2410_RTCCON);
  56. }
  57. if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CNTSEL)){
  58. dev_info(&pdev->dev,"removingRTCCON_CNTSEL\n");
  59. tmp=readb(base+S3C2410_RTCCON);
  60. writeb(tmp&~S3C2410_RTCCON_CNTSEL,base+S3C2410_RTCCON);
  61. }
  62. if((readb(base+S3C2410_RTCCON)&S3C2410_RTCCON_CLKRST)){
  63. dev_info(&pdev->dev,"removingRTCCON_CLKRST\n");
  64. tmp=readb(base+S3C2410_RTCCON);
  65. writeb(tmp&~S3C2410_RTCCON_CLKRST,base+S3C2410_RTCCON);
  66. }
  67. }
  68. }
  69. staticint__devexits3c_rtc_remove(structplatform_device*dev)
  70. {
  71. structrtc_device*rtc=platform_get_drvdata(dev);
  72. platform_set_drvdata(dev,NULL);
  73. rtc_device_unregister(rtc);
  74. s3c_rtc_setpie(&dev->dev,0);
  75. s3c_rtc_setaie(0);
  76. iounmap(s3c_rtc_base);
  77. release_resource(s3c_rtc_mem);
  78. kfree(s3c_rtc_mem);
  79. return0;
  80. }
  81. staticint__devinits3c_rtc_probe(structplatform_device*pdev)
  82. {
  83. structrtc_device*rtc;
  84. structresource*res;
  85. intret;
  86. pr_debug("%s:probe=%p\n",__func__,pdev);
  87. /*findtheIRQs*/
  88. s3c_rtc_tickno=platform_get_irq(pdev,1);
  89. if(s3c_rtc_tickno<0){
  90. dev_err(&pdev->dev,"noirqforrtctick\n");
  91. return-ENOENT;
  92. }
  93. /*getthememoryregion*/
  94. res=platform_get_resource(pdev,IORESOURCE_MEM,0);
  95. if(res==NULL){
  96. dev_err(&pdev->dev,"failedtogetmemoryregionresource\n");
  97. return-ENOENT;
  98. }
  99. s3c_rtc_mem=request_mem_region(res->start,
  100. res->end-res->start+1,
  101. pdev->name);
  102. if(s3c_rtc_mem==NULL){
  103. dev_err(&pdev->dev,"failedtoreservememoryregion\n");
  104. ret=-ENOENT;
  105. gotoerr_nores;
  106. }
  107. s3c_rtc_base=ioremap(res->start,res->end-res->start+1);
  108. if(s3c_rtc_base==NULL){
  109. dev_err(&pdev->dev,"failedioremap()\n");
  110. ret=-EINVAL;
  111. gotoerr_nomap;
  112. }
  113. /*checktoseeifeverythingissetupcorrectly*/
  114. s3c_rtc_enable(pdev,1);
  115. pr_debug("s3c2410_rtc:RTCCON=%02x\n",
  116. readb(s3c_rtc_base+S3C2410_RTCCON));
  117. s3c_rtc_setfreq(&pdev->dev,1);
  118. /*registerRTCandexit*/
  119. rtc=rtc_device_register("s3c",&pdev->dev,&s3c_rtcops,
  120. THIS_MODULE);
  121. if(IS_ERR(rtc)){
  122. dev_err(&pdev->dev,"cannotattachrtc\n");
  123. ret=PTR_ERR(rtc);
  124. gotoerr_nortc;
  125. }
  126. rtc->max_user_freq=128;
  127. platform_set_drvdata(pdev,rtc);
  128. return0;
  129. err_nortc:
  130. s3c_rtc_enable(pdev,0);
  131. iounmap(s3c_rtc_base);
  132. err_nomap:
  133. release_resource(s3c_rtc_mem);
  134. err_nores:
  135. returnret;
  136. }
  137. staticstructplatform_drivers3c2410_rtc

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top