rtc-ep93xx.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <linux/gfp.h>
  16. #define EP93XX_RTC_DATA 0x000
  17. #define EP93XX_RTC_MATCH 0x004
  18. #define EP93XX_RTC_STATUS 0x008
  19. #define EP93XX_RTC_STATUS_INTR (1<<0)
  20. #define EP93XX_RTC_LOAD 0x00C
  21. #define EP93XX_RTC_CONTROL 0x010
  22. #define EP93XX_RTC_CONTROL_MIE (1<<0)
  23. #define EP93XX_RTC_SWCOMP 0x108
  24. #define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000
  25. #define EP93XX_RTC_SWCOMP_DEL_SHIFT 16
  26. #define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff
  27. #define EP93XX_RTC_SWCOMP_INT_SHIFT 0
  28. #define DRV_VERSION "0.3"
  29. /*
  30. * struct device dev.platform_data is used to store our private data
  31. * because struct rtc_device does not have a variable to hold it.
  32. */
  33. struct ep93xx_rtc {
  34. void __iomem *mmio_base;
  35. struct rtc_device *rtc;
  36. };
  37. static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload,
  38. unsigned short *delete)
  39. {
  40. struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
  41. unsigned long comp;
  42. comp = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP);
  43. if (preload)
  44. *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK)
  45. >> EP93XX_RTC_SWCOMP_INT_SHIFT;
  46. if (delete)
  47. *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK)
  48. >> EP93XX_RTC_SWCOMP_DEL_SHIFT;
  49. return 0;
  50. }
  51. static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  52. {
  53. struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
  54. unsigned long time;
  55. time = readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA);
  56. rtc_time_to_tm(time, tm);
  57. return 0;
  58. }
  59. static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs)
  60. {
  61. struct ep93xx_rtc *ep93xx_rtc = dev_get_platdata(dev);
  62. writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD);
  63. return 0;
  64. }
  65. static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq)
  66. {
  67. unsigned short preload, delete;
  68. ep93xx_rtc_get_swcomp(dev, &preload, &delete);
  69. seq_printf(seq, "preload\t\t: %d\n", preload);
  70. seq_printf(seq, "delete\t\t: %d\n", delete);
  71. return 0;
  72. }
  73. static const struct rtc_class_ops ep93xx_rtc_ops = {
  74. .read_time = ep93xx_rtc_read_time,
  75. .set_mmss = ep93xx_rtc_set_mmss,
  76. .proc = ep93xx_rtc_proc,
  77. };
  78. static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. unsigned short preload;
  82. ep93xx_rtc_get_swcomp(dev, &preload, NULL);
  83. return sprintf(buf, "%d\n", preload);
  84. }
  85. static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL);
  86. static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. unsigned short delete;
  90. ep93xx_rtc_get_swcomp(dev, NULL, &delete);
  91. return sprintf(buf, "%d\n", delete);
  92. }
  93. static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL);
  94. static struct attribute *ep93xx_rtc_attrs[] = {
  95. &dev_attr_comp_preload.attr,
  96. &dev_attr_comp_delete.attr,
  97. NULL
  98. };
  99. static const struct attribute_group ep93xx_rtc_sysfs_files = {
  100. .attrs = ep93xx_rtc_attrs,
  101. };
  102. static int ep93xx_rtc_probe(struct platform_device *pdev)
  103. {
  104. struct ep93xx_rtc *ep93xx_rtc;
  105. struct resource *res;
  106. int err;
  107. ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL);
  108. if (!ep93xx_rtc)
  109. return -ENOMEM;
  110. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. ep93xx_rtc->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  112. if (IS_ERR(ep93xx_rtc->mmio_base))
  113. return PTR_ERR(ep93xx_rtc->mmio_base);
  114. pdev->dev.platform_data = ep93xx_rtc;
  115. platform_set_drvdata(pdev, ep93xx_rtc);
  116. ep93xx_rtc->rtc = devm_rtc_device_register(&pdev->dev,
  117. pdev->name, &ep93xx_rtc_ops, THIS_MODULE);
  118. if (IS_ERR(ep93xx_rtc->rtc)) {
  119. err = PTR_ERR(ep93xx_rtc->rtc);
  120. goto exit;
  121. }
  122. err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
  123. if (err)
  124. goto exit;
  125. return 0;
  126. exit:
  127. pdev->dev.platform_data = NULL;
  128. return err;
  129. }
  130. static int ep93xx_rtc_remove(struct platform_device *pdev)
  131. {
  132. sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
  133. pdev->dev.platform_data = NULL;
  134. return 0;
  135. }
  136. static struct platform_driver ep93xx_rtc_driver = {
  137. .driver = {
  138. .name = "ep93xx-rtc",
  139. },
  140. .probe = ep93xx_rtc_probe,
  141. .remove = ep93xx_rtc_remove,
  142. };
  143. module_platform_driver(ep93xx_rtc_driver);
  144. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  145. MODULE_DESCRIPTION("EP93XX RTC driver");
  146. MODULE_LICENSE("GPL");
  147. MODULE_VERSION(DRV_VERSION);
  148. MODULE_ALIAS("platform:ep93xx-rtc");