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