rtc-ep93xx.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. err = -ENXIO;
  104. goto fail_free;
  105. }
  106. res = request_mem_region(res->start, resource_size(res), pdev->name);
  107. if (res == NULL) {
  108. err = -EBUSY;
  109. goto fail_free;
  110. }
  111. ep93xx_rtc->mmio_base = ioremap(res->start, resource_size(res));
  112. if (ep93xx_rtc->mmio_base == NULL) {
  113. err = -ENXIO;
  114. goto fail;
  115. }
  116. pdev->dev.platform_data = ep93xx_rtc;
  117. rtc = rtc_device_register(pdev->name,
  118. &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
  119. if (IS_ERR(rtc)) {
  120. err = PTR_ERR(rtc);
  121. goto fail;
  122. }
  123. platform_set_drvdata(pdev, rtc);
  124. err = device_create_file(&pdev->dev, &dev_attr_comp_preload);
  125. if (err)
  126. goto fail;
  127. err = device_create_file(&pdev->dev, &dev_attr_comp_delete);
  128. if (err) {
  129. device_remove_file(&pdev->dev, &dev_attr_comp_preload);
  130. goto fail;
  131. }
  132. return 0;
  133. fail:
  134. if (ep93xx_rtc->mmio_base) {
  135. iounmap(ep93xx_rtc->mmio_base);
  136. pdev->dev.platform_data = NULL;
  137. }
  138. release_mem_region(res->start, resource_size(res));
  139. fail_free:
  140. kfree(ep93xx_rtc);
  141. return err;
  142. }
  143. static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
  144. {
  145. struct rtc_device *rtc = platform_get_drvdata(pdev);
  146. struct ep93xx_rtc *ep93xx_rtc = pdev->dev.platform_data;
  147. struct resource *res;
  148. /* cleanup sysfs */
  149. device_remove_file(&pdev->dev, &dev_attr_comp_delete);
  150. device_remove_file(&pdev->dev, &dev_attr_comp_preload);
  151. rtc_device_unregister(rtc);
  152. iounmap(ep93xx_rtc->mmio_base);
  153. pdev->dev.platform_data = NULL;
  154. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  155. release_mem_region(res->start, resource_size(res));
  156. platform_set_drvdata(pdev, NULL);
  157. return 0;
  158. }
  159. /* work with hotplug and coldplug */
  160. MODULE_ALIAS("platform:ep93xx-rtc");
  161. static struct platform_driver ep93xx_rtc_driver = {
  162. .driver = {
  163. .name = "ep93xx-rtc",
  164. .owner = THIS_MODULE,
  165. },
  166. .remove = __exit_p(ep93xx_rtc_remove),
  167. };
  168. static int __init ep93xx_rtc_init(void)
  169. {
  170. return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
  171. }
  172. static void __exit ep93xx_rtc_exit(void)
  173. {
  174. platform_driver_unregister(&ep93xx_rtc_driver);
  175. }
  176. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  177. MODULE_DESCRIPTION("EP93XX RTC driver");
  178. MODULE_LICENSE("GPL");
  179. MODULE_VERSION(DRV_VERSION);
  180. module_init(ep93xx_rtc_init);
  181. module_exit(ep93xx_rtc_exit);