rtc-ep93xx.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 struct attribute *ep93xx_rtc_attrs[] = {
  93. &dev_attr_comp_preload.attr,
  94. &dev_attr_comp_delete.attr,
  95. NULL
  96. };
  97. static const struct attribute_group ep93xx_rtc_sysfs_files = {
  98. .attrs = ep93xx_rtc_attrs,
  99. };
  100. static int __init ep93xx_rtc_probe(struct platform_device *pdev)
  101. {
  102. struct ep93xx_rtc *ep93xx_rtc;
  103. struct resource *res;
  104. struct rtc_device *rtc;
  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. if (!res)
  111. return -ENXIO;
  112. if (!devm_request_mem_region(&pdev->dev, res->start,
  113. resource_size(res), pdev->name))
  114. return -EBUSY;
  115. ep93xx_rtc->mmio_base = devm_ioremap(&pdev->dev, res->start,
  116. resource_size(res));
  117. if (!ep93xx_rtc->mmio_base)
  118. return -ENXIO;
  119. pdev->dev.platform_data = ep93xx_rtc;
  120. rtc = rtc_device_register(pdev->name,
  121. &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE);
  122. if (IS_ERR(rtc)) {
  123. err = PTR_ERR(rtc);
  124. goto exit;
  125. }
  126. platform_set_drvdata(pdev, rtc);
  127. err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
  128. if (err)
  129. goto fail;
  130. return 0;
  131. fail:
  132. platform_set_drvdata(pdev, NULL);
  133. rtc_device_unregister(rtc);
  134. exit:
  135. pdev->dev.platform_data = NULL;
  136. return err;
  137. }
  138. static int __exit ep93xx_rtc_remove(struct platform_device *pdev)
  139. {
  140. struct rtc_device *rtc = platform_get_drvdata(pdev);
  141. sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files);
  142. platform_set_drvdata(pdev, NULL);
  143. rtc_device_unregister(rtc);
  144. pdev->dev.platform_data = NULL;
  145. return 0;
  146. }
  147. /* work with hotplug and coldplug */
  148. MODULE_ALIAS("platform:ep93xx-rtc");
  149. static struct platform_driver ep93xx_rtc_driver = {
  150. .driver = {
  151. .name = "ep93xx-rtc",
  152. .owner = THIS_MODULE,
  153. },
  154. .remove = __exit_p(ep93xx_rtc_remove),
  155. };
  156. static int __init ep93xx_rtc_init(void)
  157. {
  158. return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe);
  159. }
  160. static void __exit ep93xx_rtc_exit(void)
  161. {
  162. platform_driver_unregister(&ep93xx_rtc_driver);
  163. }
  164. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  165. MODULE_DESCRIPTION("EP93XX RTC driver");
  166. MODULE_LICENSE("GPL");
  167. MODULE_VERSION(DRV_VERSION);
  168. module_init(ep93xx_rtc_init);
  169. module_exit(ep93xx_rtc_exit);