code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.32.60-moxart.git] / drivers / rtc / rtc-mv.c
blobe0263d2005eeba6beb78c73809bae192f9c40bfb
1 /*
2 * Driver for the RTC in Marvell SoCs.
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
7 */
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/delay.h>
18 #define RTC_TIME_REG_OFFS 0
19 #define RTC_SECONDS_OFFS 0
20 #define RTC_MINUTES_OFFS 8
21 #define RTC_HOURS_OFFS 16
22 #define RTC_WDAY_OFFS 24
23 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
25 #define RTC_DATE_REG_OFFS 4
26 #define RTC_MDAY_OFFS 0
27 #define RTC_MONTH_OFFS 8
28 #define RTC_YEAR_OFFS 16
31 struct rtc_plat_data {
32 struct rtc_device *rtc;
33 void __iomem *ioaddr;
36 static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
38 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
39 void __iomem *ioaddr = pdata->ioaddr;
40 u32 rtc_reg;
42 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
43 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
44 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
45 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
46 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
48 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
49 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
50 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
51 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
53 return 0;
56 static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
58 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
59 void __iomem *ioaddr = pdata->ioaddr;
60 u32 rtc_time, rtc_date;
61 unsigned int year, month, day, hour, minute, second, wday;
63 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
64 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
66 second = rtc_time & 0x7f;
67 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
68 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
69 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
71 day = rtc_date & 0x3f;
72 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
73 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
75 tm->tm_sec = bcd2bin(second);
76 tm->tm_min = bcd2bin(minute);
77 tm->tm_hour = bcd2bin(hour);
78 tm->tm_mday = bcd2bin(day);
79 tm->tm_wday = bcd2bin(wday);
80 tm->tm_mon = bcd2bin(month) - 1;
81 /* hw counts from year 2000, but tm_year is relative to 1900 */
82 tm->tm_year = bcd2bin(year) + 100;
84 return rtc_valid_tm(tm);
87 static const struct rtc_class_ops mv_rtc_ops = {
88 .read_time = mv_rtc_read_time,
89 .set_time = mv_rtc_set_time,
92 static int __init mv_rtc_probe(struct platform_device *pdev)
94 struct resource *res;
95 struct rtc_plat_data *pdata;
96 resource_size_t size;
97 u32 rtc_time;
99 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
100 if (!res)
101 return -ENODEV;
103 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
104 if (!pdata)
105 return -ENOMEM;
107 size = resource_size(res);
108 if (!devm_request_mem_region(&pdev->dev, res->start, size,
109 pdev->name))
110 return -EBUSY;
112 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
113 if (!pdata->ioaddr)
114 return -ENOMEM;
116 /* make sure the 24 hours mode is enabled */
117 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
118 if (rtc_time & RTC_HOURS_12H_MODE) {
119 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
120 return -EINVAL;
123 /* make sure it is actually functional */
124 if (rtc_time == 0x01000000) {
125 ssleep(1);
126 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
127 if (rtc_time == 0x01000000) {
128 dev_err(&pdev->dev, "internal RTC not ticking\n");
129 return -ENODEV;
133 platform_set_drvdata(pdev, pdata);
134 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
135 &mv_rtc_ops, THIS_MODULE);
136 if (IS_ERR(pdata->rtc))
137 return PTR_ERR(pdata->rtc);
139 return 0;
142 static int __exit mv_rtc_remove(struct platform_device *pdev)
144 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
146 rtc_device_unregister(pdata->rtc);
147 return 0;
150 static struct platform_driver mv_rtc_driver = {
151 .remove = __exit_p(mv_rtc_remove),
152 .driver = {
153 .name = "rtc-mv",
154 .owner = THIS_MODULE,
158 static __init int mv_init(void)
160 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
163 static __exit void mv_exit(void)
165 platform_driver_unregister(&mv_rtc_driver);
168 module_init(mv_init);
169 module_exit(mv_exit);
171 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
172 MODULE_DESCRIPTION("Marvell RTC driver");
173 MODULE_LICENSE("GPL");
174 MODULE_ALIAS("platform:rtc-mv");