rtc-ep93xx.c 5.3 KB

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