Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/rtc.h> |
| 14 | #include <linux/platform_device.h> |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 15 | #include <linux/io.h> |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 16 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 17 | #define EP93XX_RTC_DATA 0x000 |
| 18 | #define EP93XX_RTC_MATCH 0x004 |
| 19 | #define EP93XX_RTC_STATUS 0x008 |
| 20 | #define EP93XX_RTC_STATUS_INTR (1<<0) |
| 21 | #define EP93XX_RTC_LOAD 0x00C |
| 22 | #define EP93XX_RTC_CONTROL 0x010 |
| 23 | #define EP93XX_RTC_CONTROL_MIE (1<<0) |
| 24 | #define EP93XX_RTC_SWCOMP 0x108 |
| 25 | #define EP93XX_RTC_SWCOMP_DEL_MASK 0x001f0000 |
| 26 | #define EP93XX_RTC_SWCOMP_DEL_SHIFT 16 |
| 27 | #define EP93XX_RTC_SWCOMP_INT_MASK 0x0000ffff |
| 28 | #define EP93XX_RTC_SWCOMP_INT_SHIFT 0 |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 29 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 30 | #define DRV_VERSION "0.3" |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 31 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 32 | /* |
| 33 | * struct device dev.platform_data is used to store our private data |
| 34 | * because struct rtc_device does not have a variable to hold it. |
| 35 | */ |
| 36 | struct ep93xx_rtc { |
| 37 | void __iomem *mmio_base; |
| 38 | }; |
| 39 | |
| 40 | static int ep93xx_rtc_get_swcomp(struct device *dev, unsigned short *preload, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 41 | unsigned short *delete) |
| 42 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 43 | struct ep93xx_rtc *ep93xx_rtc = dev->platform_data; |
| 44 | unsigned long comp; |
| 45 | |
| 46 | comp = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_SWCOMP); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 47 | |
| 48 | if (preload) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 49 | *preload = (comp & EP93XX_RTC_SWCOMP_INT_MASK) |
| 50 | >> EP93XX_RTC_SWCOMP_INT_SHIFT; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 51 | |
| 52 | if (delete) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 53 | *delete = (comp & EP93XX_RTC_SWCOMP_DEL_MASK) |
| 54 | >> EP93XX_RTC_SWCOMP_DEL_SHIFT; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int ep93xx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 60 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 61 | struct ep93xx_rtc *ep93xx_rtc = dev->platform_data; |
| 62 | unsigned long time; |
| 63 | |
| 64 | time = __raw_readl(ep93xx_rtc->mmio_base + EP93XX_RTC_DATA); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 65 | |
| 66 | rtc_time_to_tm(time, tm); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int ep93xx_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 71 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 72 | struct ep93xx_rtc *ep93xx_rtc = dev->platform_data; |
| 73 | |
| 74 | __raw_writel(secs + 1, ep93xx_rtc->mmio_base + EP93XX_RTC_LOAD); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 78 | static int ep93xx_rtc_proc(struct device *dev, struct seq_file *seq) |
| 79 | { |
| 80 | unsigned short preload, delete; |
| 81 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 82 | ep93xx_rtc_get_swcomp(dev, &preload, &delete); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 83 | |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 84 | seq_printf(seq, "preload\t\t: %d\n", preload); |
| 85 | seq_printf(seq, "delete\t\t: %d\n", delete); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 90 | static const struct rtc_class_ops ep93xx_rtc_ops = { |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 91 | .read_time = ep93xx_rtc_read_time, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 92 | .set_mmss = ep93xx_rtc_set_mmss, |
| 93 | .proc = ep93xx_rtc_proc, |
| 94 | }; |
| 95 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 96 | static ssize_t ep93xx_rtc_show_comp_preload(struct device *dev, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 97 | struct device_attribute *attr, char *buf) |
| 98 | { |
| 99 | unsigned short preload; |
| 100 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 101 | ep93xx_rtc_get_swcomp(dev, &preload, NULL); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 102 | |
| 103 | return sprintf(buf, "%d\n", preload); |
| 104 | } |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 105 | static DEVICE_ATTR(comp_preload, S_IRUGO, ep93xx_rtc_show_comp_preload, NULL); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 106 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 107 | static ssize_t ep93xx_rtc_show_comp_delete(struct device *dev, |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 108 | struct device_attribute *attr, char *buf) |
| 109 | { |
| 110 | unsigned short delete; |
| 111 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 112 | ep93xx_rtc_get_swcomp(dev, NULL, &delete); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 113 | |
| 114 | return sprintf(buf, "%d\n", delete); |
| 115 | } |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 116 | static DEVICE_ATTR(comp_delete, S_IRUGO, ep93xx_rtc_show_comp_delete, NULL); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 117 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 118 | static struct attribute *ep93xx_rtc_attrs[] = { |
| 119 | &dev_attr_comp_preload.attr, |
| 120 | &dev_attr_comp_delete.attr, |
| 121 | NULL |
| 122 | }; |
| 123 | |
| 124 | static const struct attribute_group ep93xx_rtc_sysfs_files = { |
| 125 | .attrs = ep93xx_rtc_attrs, |
| 126 | }; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 127 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 128 | static int __init ep93xx_rtc_probe(struct platform_device *pdev) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 129 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 130 | struct ep93xx_rtc *ep93xx_rtc; |
| 131 | struct resource *res; |
| 132 | struct rtc_device *rtc; |
| 133 | int err; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 134 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 135 | ep93xx_rtc = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_rtc), GFP_KERNEL); |
| 136 | if (!ep93xx_rtc) |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 137 | return -ENOMEM; |
| 138 | |
| 139 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 140 | if (!res) |
| 141 | return -ENXIO; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 142 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 143 | if (!devm_request_mem_region(&pdev->dev, res->start, |
| 144 | resource_size(res), pdev->name)) |
| 145 | return -EBUSY; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 146 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 147 | ep93xx_rtc->mmio_base = devm_ioremap(&pdev->dev, res->start, |
| 148 | resource_size(res)); |
| 149 | if (!ep93xx_rtc->mmio_base) |
| 150 | return -ENXIO; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 151 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 152 | pdev->dev.platform_data = ep93xx_rtc; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 153 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 154 | rtc = rtc_device_register(pdev->name, |
| 155 | &pdev->dev, &ep93xx_rtc_ops, THIS_MODULE); |
| 156 | if (IS_ERR(rtc)) { |
| 157 | err = PTR_ERR(rtc); |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 158 | goto exit; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | platform_set_drvdata(pdev, rtc); |
| 162 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 163 | err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 164 | if (err) |
| 165 | goto fail; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 166 | |
| 167 | return 0; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 168 | |
| 169 | fail: |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 170 | platform_set_drvdata(pdev, NULL); |
| 171 | rtc_device_unregister(rtc); |
| 172 | exit: |
| 173 | pdev->dev.platform_data = NULL; |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 174 | return err; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 177 | static int __exit ep93xx_rtc_remove(struct platform_device *pdev) |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 178 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 179 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 180 | |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 181 | sysfs_remove_group(&pdev->dev.kobj, &ep93xx_rtc_sysfs_files); |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 182 | platform_set_drvdata(pdev, NULL); |
H Hartley Sweeten | b4877d2 | 2010-03-05 13:44:20 -0800 | [diff] [blame^] | 183 | rtc_device_unregister(rtc); |
| 184 | pdev->dev.platform_data = NULL; |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 189 | /* work with hotplug and coldplug */ |
| 190 | MODULE_ALIAS("platform:ep93xx-rtc"); |
| 191 | |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 192 | static struct platform_driver ep93xx_rtc_driver = { |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 193 | .driver = { |
| 194 | .name = "ep93xx-rtc", |
| 195 | .owner = THIS_MODULE, |
| 196 | }, |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 197 | .remove = __exit_p(ep93xx_rtc_remove), |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static int __init ep93xx_rtc_init(void) |
| 201 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 202 | return platform_driver_probe(&ep93xx_rtc_driver, ep93xx_rtc_probe); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void __exit ep93xx_rtc_exit(void) |
| 206 | { |
Hartley Sweeten | 38f7b00 | 2009-04-15 23:18:26 +0100 | [diff] [blame] | 207 | platform_driver_unregister(&ep93xx_rtc_driver); |
Alessandro Zummo | fd507e2 | 2006-03-27 01:16:45 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 211 | MODULE_DESCRIPTION("EP93XX RTC driver"); |
| 212 | MODULE_LICENSE("GPL"); |
| 213 | MODULE_VERSION(DRV_VERSION); |
| 214 | |
| 215 | module_init(ep93xx_rtc_init); |
| 216 | module_exit(ep93xx_rtc_exit); |