blob: 10287865e33012a154100b6b674f541273d132d3 [file] [log] [blame]
dmitry pervushindf17f632009-09-22 16:46:26 -07001/*
2 * Freescale STMP37XX/STMP378X Real Time Clock driver
3 *
4 * Copyright (c) 2007 Sigmatel, Inc.
5 * Peter Hartley, <peter.hartley@sigmatel.com>
6 *
7 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
8 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
Wolfram Sang7e794cb2011-05-25 12:56:53 +02009 * Copyright 2011 Wolfram Sang, Pengutronix e.K.
dmitry pervushindf17f632009-09-22 16:46:26 -070010 */
11
12/*
13 * The code contained herein is licensed under the GNU General Public
14 * License. You may obtain a copy of the GNU General Public License
15 * Version 2 or later at the following locations:
16 *
17 * http://www.opensource.org/licenses/gpl-license.html
18 * http://www.gnu.org/copyleft/gpl.html
19 */
20#include <linux/kernel.h>
21#include <linux/module.h>
Wolfram Sangb5167152011-05-25 12:56:52 +020022#include <linux/io.h>
dmitry pervushindf17f632009-09-22 16:46:26 -070023#include <linux/init.h>
24#include <linux/platform_device.h>
25#include <linux/interrupt.h>
26#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
dmitry pervushindf17f632009-09-22 16:46:26 -070028
Wolfram Sang46b21212011-05-25 12:56:50 +020029#include <mach/common.h>
Wolfram Sang47eac332011-05-25 12:56:49 +020030
31#define STMP3XXX_RTC_CTRL 0x0
Wolfram Sangb5167152011-05-25 12:56:52 +020032#define STMP3XXX_RTC_CTRL_SET 0x4
33#define STMP3XXX_RTC_CTRL_CLR 0x8
Wolfram Sang47eac332011-05-25 12:56:49 +020034#define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
35#define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
36#define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
Wolfram Sang47eac332011-05-25 12:56:49 +020037
38#define STMP3XXX_RTC_STAT 0x10
39#define STMP3XXX_RTC_STAT_STALE_SHIFT 16
40#define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
41
42#define STMP3XXX_RTC_SECONDS 0x30
43
44#define STMP3XXX_RTC_ALARM 0x40
45
46#define STMP3XXX_RTC_PERSISTENT0 0x60
Wolfram Sangb5167152011-05-25 12:56:52 +020047#define STMP3XXX_RTC_PERSISTENT0_SET 0x64
48#define STMP3XXX_RTC_PERSISTENT0_CLR 0x68
Wolfram Sang47eac332011-05-25 12:56:49 +020049#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN 0x00000002
50#define STMP3XXX_RTC_PERSISTENT0_ALARM_EN 0x00000004
51#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE 0x00000080
dmitry pervushindf17f632009-09-22 16:46:26 -070052
53struct stmp3xxx_rtc_data {
54 struct rtc_device *rtc;
dmitry pervushindf17f632009-09-22 16:46:26 -070055 void __iomem *io;
Wolfram Sang7e794cb2011-05-25 12:56:53 +020056 int irq_alarm;
dmitry pervushindf17f632009-09-22 16:46:26 -070057};
58
59static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
60{
61 /*
62 * The datasheet doesn't say which way round the
63 * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0,
64 * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS
65 */
Wolfram Sangb5167152011-05-25 12:56:52 +020066 while (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
Wolfram Sang47eac332011-05-25 12:56:49 +020067 (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT))
dmitry pervushindf17f632009-09-22 16:46:26 -070068 cpu_relax();
69}
70
71/* Time read/write */
72static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
73{
74 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
75
76 stmp3xxx_wait_time(rtc_data);
Wolfram Sangb5167152011-05-25 12:56:52 +020077 rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
dmitry pervushindf17f632009-09-22 16:46:26 -070078 return 0;
79}
80
81static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
82{
83 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
84
Wolfram Sangb5167152011-05-25 12:56:52 +020085 writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
dmitry pervushindf17f632009-09-22 16:46:26 -070086 stmp3xxx_wait_time(rtc_data);
87 return 0;
88}
89
90/* interrupt(s) handler */
91static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
92{
93 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
Wolfram Sang7e794cb2011-05-25 12:56:53 +020094 u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -070095
Wolfram Sang47eac332011-05-25 12:56:49 +020096 if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
Wolfram Sangb5167152011-05-25 12:56:52 +020097 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
98 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
Wolfram Sang7e794cb2011-05-25 12:56:53 +020099 rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
100 return IRQ_HANDLED;
dmitry pervushindf17f632009-09-22 16:46:26 -0700101 }
102
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200103 return IRQ_NONE;
dmitry pervushindf17f632009-09-22 16:46:26 -0700104}
105
106static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
107{
108 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700109
110 if (enabled) {
Wolfram Sangb5167152011-05-25 12:56:52 +0200111 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
112 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
113 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_SET);
114 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
115 rtc_data->io + STMP3XXX_RTC_CTRL_SET);
dmitry pervushindf17f632009-09-22 16:46:26 -0700116 } else {
Wolfram Sangb5167152011-05-25 12:56:52 +0200117 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
118 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
119 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
120 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
121 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700122 }
123 return 0;
124}
125
dmitry pervushindf17f632009-09-22 16:46:26 -0700126static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
127{
128 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
129
Wolfram Sangb5167152011-05-25 12:56:52 +0200130 rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
dmitry pervushindf17f632009-09-22 16:46:26 -0700131 return 0;
132}
133
134static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
135{
136 unsigned long t;
137 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
138
139 rtc_tm_to_time(&alm->time, &t);
Wolfram Sangb5167152011-05-25 12:56:52 +0200140 writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200141
142 stmp3xxx_alarm_irq_enable(dev, alm->enabled);
143
dmitry pervushindf17f632009-09-22 16:46:26 -0700144 return 0;
145}
146
147static struct rtc_class_ops stmp3xxx_rtc_ops = {
148 .alarm_irq_enable =
149 stmp3xxx_alarm_irq_enable,
dmitry pervushindf17f632009-09-22 16:46:26 -0700150 .read_time = stmp3xxx_rtc_gettime,
151 .set_mmss = stmp3xxx_rtc_set_mmss,
152 .read_alarm = stmp3xxx_rtc_read_alarm,
153 .set_alarm = stmp3xxx_rtc_set_alarm,
154};
155
156static int stmp3xxx_rtc_remove(struct platform_device *pdev)
157{
158 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
159
160 if (!rtc_data)
161 return 0;
162
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200163 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
Wolfram Sangb5167152011-05-25 12:56:52 +0200164 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700165 free_irq(rtc_data->irq_alarm, &pdev->dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700166 rtc_device_unregister(rtc_data->rtc);
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200167 platform_set_drvdata(pdev, NULL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700168 iounmap(rtc_data->io);
169 kfree(rtc_data);
170
171 return 0;
172}
173
174static int stmp3xxx_rtc_probe(struct platform_device *pdev)
175{
176 struct stmp3xxx_rtc_data *rtc_data;
177 struct resource *r;
178 int err;
179
180 rtc_data = kzalloc(sizeof *rtc_data, GFP_KERNEL);
181 if (!rtc_data)
182 return -ENOMEM;
183
184 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
185 if (!r) {
186 dev_err(&pdev->dev, "failed to get resource\n");
187 err = -ENXIO;
188 goto out_free;
189 }
190
191 rtc_data->io = ioremap(r->start, resource_size(r));
192 if (!rtc_data->io) {
193 dev_err(&pdev->dev, "ioremap failed\n");
194 err = -EIO;
195 goto out_free;
196 }
197
198 rtc_data->irq_alarm = platform_get_irq(pdev, 0);
dmitry pervushindf17f632009-09-22 16:46:26 -0700199
Wolfram Sangb5167152011-05-25 12:56:52 +0200200 if (!(readl(STMP3XXX_RTC_STAT + rtc_data->io) &
Wolfram Sang47eac332011-05-25 12:56:49 +0200201 STMP3XXX_RTC_STAT_RTC_PRESENT)) {
dmitry pervushindf17f632009-09-22 16:46:26 -0700202 dev_err(&pdev->dev, "no device onboard\n");
203 err = -ENODEV;
204 goto out_remap;
205 }
206
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200207 platform_set_drvdata(pdev, rtc_data);
208
Wolfram Sang46b21212011-05-25 12:56:50 +0200209 mxs_reset_block(rtc_data->io);
Wolfram Sangb5167152011-05-25 12:56:52 +0200210 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200211 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
212 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
Wolfram Sangb5167152011-05-25 12:56:52 +0200213 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200214
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200215 writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
216 STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
217 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
218
dmitry pervushindf17f632009-09-22 16:46:26 -0700219 rtc_data->rtc = rtc_device_register(pdev->name, &pdev->dev,
220 &stmp3xxx_rtc_ops, THIS_MODULE);
221 if (IS_ERR(rtc_data->rtc)) {
222 err = PTR_ERR(rtc_data->rtc);
223 goto out_remap;
224 }
225
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200226 err = request_irq(rtc_data->irq_alarm, stmp3xxx_rtc_interrupt, 0,
227 "RTC alarm", &pdev->dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700228 if (err) {
229 dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
230 rtc_data->irq_alarm);
231 goto out_irq_alarm;
232 }
dmitry pervushindf17f632009-09-22 16:46:26 -0700233
dmitry pervushindf17f632009-09-22 16:46:26 -0700234 return 0;
235
dmitry pervushindf17f632009-09-22 16:46:26 -0700236out_irq_alarm:
dmitry pervushindf17f632009-09-22 16:46:26 -0700237 rtc_device_unregister(rtc_data->rtc);
238out_remap:
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200239 platform_set_drvdata(pdev, NULL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700240 iounmap(rtc_data->io);
241out_free:
242 kfree(rtc_data);
243 return err;
244}
245
246#ifdef CONFIG_PM
247static int stmp3xxx_rtc_suspend(struct platform_device *dev, pm_message_t state)
248{
249 return 0;
250}
251
252static int stmp3xxx_rtc_resume(struct platform_device *dev)
253{
254 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(dev);
255
Wolfram Sang46b21212011-05-25 12:56:50 +0200256 mxs_reset_block(rtc_data->io);
Wolfram Sangb5167152011-05-25 12:56:52 +0200257 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200258 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
259 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
Wolfram Sangb5167152011-05-25 12:56:52 +0200260 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700261 return 0;
262}
263#else
264#define stmp3xxx_rtc_suspend NULL
265#define stmp3xxx_rtc_resume NULL
266#endif
267
268static struct platform_driver stmp3xxx_rtcdrv = {
269 .probe = stmp3xxx_rtc_probe,
270 .remove = stmp3xxx_rtc_remove,
271 .suspend = stmp3xxx_rtc_suspend,
272 .resume = stmp3xxx_rtc_resume,
273 .driver = {
274 .name = "stmp3xxx-rtc",
275 .owner = THIS_MODULE,
276 },
277};
278
Axel Lin0c4eae62012-01-10 15:10:48 -0800279module_platform_driver(stmp3xxx_rtcdrv);
dmitry pervushindf17f632009-09-22 16:46:26 -0700280
281MODULE_DESCRIPTION("STMP3xxx RTC Driver");
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200282MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
283 "Wolfram Sang <w.sang@pengutronix.de>");
dmitry pervushindf17f632009-09-22 16:46:26 -0700284MODULE_LICENSE("GPL");