rtc-ep93xx.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * A driver for the RTC embedded in the Cirrus Logic EP93XX processors
  3. * Copyright (c) 2006 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/rtc.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #define EP93XX_RTC_DATA 0x000
  16. #define EP93XX_RTC_MATCH 0x004
  17. #define EP93XX_RTC_STATUS 0x008
  18. #define EP93XX_RTC_STATUS_INTR (1<<0)
  19. #define EP93XX_RTC_LOAD 0x00C
  20. #define EP93XX_RTC_CONTROL 0x010
  21. #define EP93XX_RTC_CONTROL_MIE (1<<0)
  22. #define EP93XX_RTC_SWCOMP 0x108
  23. #define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
  24. #define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
  25. #define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
  26. #define EP93XX_RTC_SWCOMP_INT_SHIFT 0
  27. #define DRV_VERSION "0.3"
  28. /*
  29. * struct device dev.platform_data is used to store our private data
  30. * because struct rtc_device does not have a variable to hold it.
  31. */
  32. struct ep93xx_rtc {
  33. void __iomem *mmio_base;
  34. };
  35. static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
  36. unsigned short *delete)
  37. {
  38. struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
  39. unsigned long comp;
  40. comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
  41. if (preload)
  42. *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
  43. >> EP93XX_RTC_SWCOMP_INT_SHIFT;
  44. if (delete)
  45. *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
  46. >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
  47. return 0;
  48. }
  49. static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  50. {
  51. struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
  52. unsigned long time;
  53. time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
  54. rtc_time_to_tm(time, tm);
  55. return 0;
  56. }
  57. static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
  58. {
  59. struct ep93xx_rtc *ep93xx_rtc = dev->platform_data;
  60. __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
  61. return 0;
  62. }
  63. static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
  64. {
  65. unsigned short preload, delete;
  66. ep93xx_rtc_get_swcomp(dev, &preload, &delete);
  67. seq_printf(seq, "preload\t\t: %d\n", preload);
  68. seq_printf(seq, "delete\t\t: %d\n", delete);
  69. return 0;
  70. }
  71. static const struct rtc_class_ops ep93xx_rtc_ops = {
  72. .read_time = ep93xx_rtc_read_time,
  73. .set_mmss = ep93xx_rtc_set_mmss,
  74. .proc = ep93xx_rtc_proc,
  75. };
  76. static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. unsigned short preload;
  80. ep93xx_rtc_get_swcomp(dev, &preload, NULL);
  81. return sprintf(buf, "%d\n", preload);
  82. }
  83. static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
  84. static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. unsigned short delete;
  88. ep93xx_rtc_get_swcomp(dev, NULL, &delete);
  89. return sprintf(buf, "%d\n", delete);
  90. }
  91. static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
  92. static int __init ep93xx_rtc_probe(struct platform_device *pdev)
  93. {
  94. struct ep93xx_rtc *ep93xx_rtc;
  95. struct resource *res;
  96. struct rtc_device *rtc;
  97. int err;
  98. ep93xx_rtc = kzalloc(sizeof(struct ep93xx_rtc), GFP_KERNEL);
  99. if (ep93xx_rtc == NULL)
  100. return -ENOMEM;
  101. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  102. if (res == NULL)
  103. return -ENXIO;
  104. res = request_mem_region(res->start, resource_size(res), pdev->name);
  105. if (res == NULL)
  106. return -EBUSY;
  107. ep93xx_rtc->mmio_base = ioremap(res->start, resource_size(res));
  108. if (ep93xx_rtc->mmio_base == NULL) {
  109. err = -ENXIO;
  110. goto fail;
  111. }
  112. pdev->dev.platform_data = ep93xx_rtc;
  113. rtc = rtc_device_register(pdev->name,
  114. &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
  115. if (IS_ERR(rtc)) {
  116. err = PTR_ERR(rtc);
  117. goto fail;
  118. }
  119. platform_set_drvdata(pdev, rtc);
  120. err = device_create_file(&pdev->dev, &dev_attr_comp_preload);
  121. if (err)
  122. goto fail;
  123. err = device_create_file(&pdev->dev, &dev_attr_comp_delete);
  124. if (err) {
  125. device_remove_file(&pdev->dev, &dev_attr_comp_preload);
  126. goto fail;
  127. }
  128. return 0;
  129. fail:
  130. if (ep93xx_rtc->mmio_base) {
  131. iounmap(ep93xx_rtc->mmio_base);
  132. pdev->dev.platform_data = NULL;
  133. }
  134. release_mem_region(res->start, resource_size(res));
  135. return err;
  136. }
  137. static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
  138. {
  139. struct rtc_device *rtc = platform_get_drvdata(pdev);
  140. struct ep93xx_rtc *ep93xx_rtc = pdev->dev.platform_data;
  141. struct resource *res;
  142. /* cleanup sysfs */
  143. device_remove_file(&pdev->dev, &dev_attr_comp_delete);
  144. device_remove_file(&pdev->dev, &dev_attr_comp_preload);
  145. rtc_device_unregister(rtc);
  146. iounmap(ep93xx_rtc->mmio_base);
  147. pdev->dev.platform_data = NULL;
  148. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  149. release_mem_region(res->start, resource_size(res));
  150. platform_set_drvdata(pdev, NULL);
  151. return 0;
  152. }
  153. /* work with hotplug and coldplug */
  154. MODULE_ALIAS("platform:ep93xx-rtc");
  155. static struct platform_driver ep93xx_rtc_driver = {
  156. .driver = {
  157. .name = "ep93xx-rtc",
  158. .owner = THIS_MODULE,
  159. },
  160. .remove = __exit_p(ep93xx_rtc_remove),
  161. };
  162. static int __init ep93xx_rtc_init(void)
  163. {
  164. return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
  165. }
  166. static void __exit ep93xx_rtc_exit(void)
  167. {
  168. platform_driver_unregister(&ep93xx_rtc_driver);
  169. }
  170. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  171. MODULE_DESCRIPTION("EP93XX RTC driver");
  172. MODULE_LICENSE("GPL");
  173. MODULE_VERSION(DRV_VERSION);
  174. module_init(ep93xx_rtc_init);
  175. module_exit(ep93xx_rtc_exit);