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.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
14 #include <linux/platform_device.h>
17 #define RTC_TIME_REG_OFFS 0
18 #define RTC_SECONDS_OFFS 0
19 #define RTC_MINUTES_OFFS 8
20 #define RTC_HOURS_OFFS 16
21 #define RTC_WDAY_OFFS 24
22 #define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
24 #define RTC_DATE_REG_OFFS 4
25 #define RTC_MDAY_OFFS 0
26 #define RTC_MONTH_OFFS 8
27 #define RTC_YEAR_OFFS 16
30 struct rtc_plat_data
{
31 struct rtc_device
*rtc
;
35 static int mv_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
37 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
38 void __iomem
*ioaddr
= pdata
->ioaddr
;
41 rtc_reg
= (bin2bcd(tm
->tm_sec
) << RTC_SECONDS_OFFS
) |
42 (bin2bcd(tm
->tm_min
) << RTC_MINUTES_OFFS
) |
43 (bin2bcd(tm
->tm_hour
) << RTC_HOURS_OFFS
) |
44 (bin2bcd(tm
->tm_wday
) << RTC_WDAY_OFFS
);
45 writel(rtc_reg
, ioaddr
+ RTC_TIME_REG_OFFS
);
47 rtc_reg
= (bin2bcd(tm
->tm_mday
) << RTC_MDAY_OFFS
) |
48 (bin2bcd(tm
->tm_mon
+ 1) << RTC_MONTH_OFFS
) |
49 (bin2bcd(tm
->tm_year
% 100) << RTC_YEAR_OFFS
);
50 writel(rtc_reg
, ioaddr
+ RTC_DATE_REG_OFFS
);
55 static int mv_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
57 struct rtc_plat_data
*pdata
= dev_get_drvdata(dev
);
58 void __iomem
*ioaddr
= pdata
->ioaddr
;
59 u32 rtc_time
, rtc_date
;
60 unsigned int year
, month
, day
, hour
, minute
, second
, wday
;
62 rtc_time
= readl(ioaddr
+ RTC_TIME_REG_OFFS
);
63 rtc_date
= readl(ioaddr
+ RTC_DATE_REG_OFFS
);
65 second
= rtc_time
& 0x7f;
66 minute
= (rtc_time
>> RTC_MINUTES_OFFS
) & 0x7f;
67 hour
= (rtc_time
>> RTC_HOURS_OFFS
) & 0x3f; /* assume 24 hours mode */
68 wday
= (rtc_time
>> RTC_WDAY_OFFS
) & 0x7;
70 day
= rtc_date
& 0x3f;
71 month
= (rtc_date
>> RTC_MONTH_OFFS
) & 0x3f;
72 year
= (rtc_date
>> RTC_YEAR_OFFS
) & 0xff;
74 tm
->tm_sec
= bcd2bin(second
);
75 tm
->tm_min
= bcd2bin(minute
);
76 tm
->tm_hour
= bcd2bin(hour
);
77 tm
->tm_mday
= bcd2bin(day
);
78 tm
->tm_wday
= bcd2bin(wday
);
79 tm
->tm_mon
= bcd2bin(month
) - 1;
80 /* hw counts from year 2000, but tm_year is relative to 1900 */
81 tm
->tm_year
= bcd2bin(year
) + 100;
83 return rtc_valid_tm(tm
);
86 static const struct rtc_class_ops mv_rtc_ops
= {
87 .read_time
= mv_rtc_read_time
,
88 .set_time
= mv_rtc_set_time
,
91 static int __init
mv_rtc_probe(struct platform_device
*pdev
)
94 struct rtc_plat_data
*pdata
;
98 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
102 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
106 size
= resource_size(res
);
107 if (!devm_request_mem_region(&pdev
->dev
, res
->start
, size
,
111 pdata
->ioaddr
= devm_ioremap(&pdev
->dev
, res
->start
, size
);
115 /* make sure the 24 hours mode is enabled */
116 rtc_time
= readl(pdata
->ioaddr
+ RTC_TIME_REG_OFFS
);
117 if (rtc_time
& RTC_HOURS_12H_MODE
) {
118 dev_err(&pdev
->dev
, "24 Hours mode not supported.\n");
122 platform_set_drvdata(pdev
, pdata
);
123 pdata
->rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
124 &mv_rtc_ops
, THIS_MODULE
);
125 if (IS_ERR(pdata
->rtc
))
126 return PTR_ERR(pdata
->rtc
);
131 static int __exit
mv_rtc_remove(struct platform_device
*pdev
)
133 struct rtc_plat_data
*pdata
= platform_get_drvdata(pdev
);
135 rtc_device_unregister(pdata
->rtc
);
139 static struct platform_driver mv_rtc_driver
= {
140 .remove
= __exit_p(mv_rtc_remove
),
143 .owner
= THIS_MODULE
,
147 static __init
int mv_init(void)
149 return platform_driver_probe(&mv_rtc_driver
, mv_rtc_probe
);
152 static __exit
void mv_exit(void)
154 platform_driver_unregister(&mv_rtc_driver
);
157 module_init(mv_init
);
158 module_exit(mv_exit
);
160 MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
161 MODULE_DESCRIPTION("Marvell RTC driver");
162 MODULE_LICENSE("GPL");
163 MODULE_ALIAS("platform:rtc-mv");