rtc-ep93xx.c 4.6 KB

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