blob: 739ef55694f4d30c6d7370ae66c5ad42e996430f [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>
Marek Vasutdd8d20a2012-06-26 16:29:27 +080028#include <linux/of_device.h>
dmitry pervushindf17f632009-09-22 16:46:26 -070029
Wolfram Sang46b21212011-05-25 12:56:50 +020030#include <mach/common.h>
Wolfram Sang47eac332011-05-25 12:56:49 +020031
32#define STMP3XXX_RTC_CTRL 0x0
Wolfram Sangb5167152011-05-25 12:56:52 +020033#define STMP3XXX_RTC_CTRL_SET 0x4
34#define STMP3XXX_RTC_CTRL_CLR 0x8
Wolfram Sang47eac332011-05-25 12:56:49 +020035#define STMP3XXX_RTC_CTRL_ALARM_IRQ_EN 0x00000001
36#define STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN 0x00000002
37#define STMP3XXX_RTC_CTRL_ALARM_IRQ 0x00000004
Wolfram Sang47eac332011-05-25 12:56:49 +020038
39#define STMP3XXX_RTC_STAT 0x10
40#define STMP3XXX_RTC_STAT_STALE_SHIFT 16
41#define STMP3XXX_RTC_STAT_RTC_PRESENT 0x80000000
42
43#define STMP3XXX_RTC_SECONDS 0x30
44
45#define STMP3XXX_RTC_ALARM 0x40
46
47#define STMP3XXX_RTC_PERSISTENT0 0x60
Wolfram Sangb5167152011-05-25 12:56:52 +020048#define STMP3XXX_RTC_PERSISTENT0_SET 0x64
49#define STMP3XXX_RTC_PERSISTENT0_CLR 0x68
Wolfram Sang47eac332011-05-25 12:56:49 +020050#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN 0x00000002
51#define STMP3XXX_RTC_PERSISTENT0_ALARM_EN 0x00000004
52#define STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE 0x00000080
dmitry pervushindf17f632009-09-22 16:46:26 -070053
54struct stmp3xxx_rtc_data {
55 struct rtc_device *rtc;
dmitry pervushindf17f632009-09-22 16:46:26 -070056 void __iomem *io;
Wolfram Sang7e794cb2011-05-25 12:56:53 +020057 int irq_alarm;
dmitry pervushindf17f632009-09-22 16:46:26 -070058};
59
60static void stmp3xxx_wait_time(struct stmp3xxx_rtc_data *rtc_data)
61{
62 /*
63 * The datasheet doesn't say which way round the
64 * NEW_REGS/STALE_REGS bitfields go. In fact it's 0x1=P0,
65 * 0x2=P1, .., 0x20=P5, 0x40=ALARM, 0x80=SECONDS
66 */
Wolfram Sangb5167152011-05-25 12:56:52 +020067 while (readl(rtc_data->io + STMP3XXX_RTC_STAT) &
Wolfram Sang47eac332011-05-25 12:56:49 +020068 (0x80 << STMP3XXX_RTC_STAT_STALE_SHIFT))
dmitry pervushindf17f632009-09-22 16:46:26 -070069 cpu_relax();
70}
71
72/* Time read/write */
73static int stmp3xxx_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
74{
75 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
76
77 stmp3xxx_wait_time(rtc_data);
Wolfram Sangb5167152011-05-25 12:56:52 +020078 rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_SECONDS), rtc_tm);
dmitry pervushindf17f632009-09-22 16:46:26 -070079 return 0;
80}
81
82static int stmp3xxx_rtc_set_mmss(struct device *dev, unsigned long t)
83{
84 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
85
Wolfram Sangb5167152011-05-25 12:56:52 +020086 writel(t, rtc_data->io + STMP3XXX_RTC_SECONDS);
dmitry pervushindf17f632009-09-22 16:46:26 -070087 stmp3xxx_wait_time(rtc_data);
88 return 0;
89}
90
91/* interrupt(s) handler */
92static irqreturn_t stmp3xxx_rtc_interrupt(int irq, void *dev_id)
93{
94 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev_id);
Wolfram Sang7e794cb2011-05-25 12:56:53 +020095 u32 status = readl(rtc_data->io + STMP3XXX_RTC_CTRL);
dmitry pervushindf17f632009-09-22 16:46:26 -070096
Wolfram Sang47eac332011-05-25 12:56:49 +020097 if (status & STMP3XXX_RTC_CTRL_ALARM_IRQ) {
Wolfram Sangb5167152011-05-25 12:56:52 +020098 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ,
99 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200100 rtc_update_irq(rtc_data->rtc, 1, RTC_AF | RTC_IRQF);
101 return IRQ_HANDLED;
dmitry pervushindf17f632009-09-22 16:46:26 -0700102 }
103
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200104 return IRQ_NONE;
dmitry pervushindf17f632009-09-22 16:46:26 -0700105}
106
107static int stmp3xxx_alarm_irq_enable(struct device *dev, unsigned int enabled)
108{
109 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700110
111 if (enabled) {
Wolfram Sangb5167152011-05-25 12:56:52 +0200112 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
113 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
114 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_SET);
115 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
116 rtc_data->io + STMP3XXX_RTC_CTRL_SET);
dmitry pervushindf17f632009-09-22 16:46:26 -0700117 } else {
Wolfram Sangb5167152011-05-25 12:56:52 +0200118 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
119 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN,
120 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
121 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
122 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700123 }
124 return 0;
125}
126
dmitry pervushindf17f632009-09-22 16:46:26 -0700127static int stmp3xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
128{
129 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
130
Wolfram Sangb5167152011-05-25 12:56:52 +0200131 rtc_time_to_tm(readl(rtc_data->io + STMP3XXX_RTC_ALARM), &alm->time);
dmitry pervushindf17f632009-09-22 16:46:26 -0700132 return 0;
133}
134
135static int stmp3xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
136{
137 unsigned long t;
138 struct stmp3xxx_rtc_data *rtc_data = dev_get_drvdata(dev);
139
140 rtc_tm_to_time(&alm->time, &t);
Wolfram Sangb5167152011-05-25 12:56:52 +0200141 writel(t, rtc_data->io + STMP3XXX_RTC_ALARM);
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200142
143 stmp3xxx_alarm_irq_enable(dev, alm->enabled);
144
dmitry pervushindf17f632009-09-22 16:46:26 -0700145 return 0;
146}
147
148static struct rtc_class_ops stmp3xxx_rtc_ops = {
149 .alarm_irq_enable =
150 stmp3xxx_alarm_irq_enable,
dmitry pervushindf17f632009-09-22 16:46:26 -0700151 .read_time = stmp3xxx_rtc_gettime,
152 .set_mmss = stmp3xxx_rtc_set_mmss,
153 .read_alarm = stmp3xxx_rtc_read_alarm,
154 .set_alarm = stmp3xxx_rtc_set_alarm,
155};
156
157static int stmp3xxx_rtc_remove(struct platform_device *pdev)
158{
159 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(pdev);
160
161 if (!rtc_data)
162 return 0;
163
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200164 writel(STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
Wolfram Sangb5167152011-05-25 12:56:52 +0200165 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700166 free_irq(rtc_data->irq_alarm, &pdev->dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700167 rtc_device_unregister(rtc_data->rtc);
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200168 platform_set_drvdata(pdev, NULL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700169 iounmap(rtc_data->io);
170 kfree(rtc_data);
171
172 return 0;
173}
174
175static int stmp3xxx_rtc_probe(struct platform_device *pdev)
176{
177 struct stmp3xxx_rtc_data *rtc_data;
178 struct resource *r;
179 int err;
180
181 rtc_data = kzalloc(sizeof *rtc_data, GFP_KERNEL);
182 if (!rtc_data)
183 return -ENOMEM;
184
185 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
186 if (!r) {
187 dev_err(&pdev->dev, "failed to get resource\n");
188 err = -ENXIO;
189 goto out_free;
190 }
191
192 rtc_data->io = ioremap(r->start, resource_size(r));
193 if (!rtc_data->io) {
194 dev_err(&pdev->dev, "ioremap failed\n");
195 err = -EIO;
196 goto out_free;
197 }
198
199 rtc_data->irq_alarm = platform_get_irq(pdev, 0);
dmitry pervushindf17f632009-09-22 16:46:26 -0700200
Wolfram Sangb5167152011-05-25 12:56:52 +0200201 if (!(readl(STMP3XXX_RTC_STAT + rtc_data->io) &
Wolfram Sang47eac332011-05-25 12:56:49 +0200202 STMP3XXX_RTC_STAT_RTC_PRESENT)) {
dmitry pervushindf17f632009-09-22 16:46:26 -0700203 dev_err(&pdev->dev, "no device onboard\n");
204 err = -ENODEV;
205 goto out_remap;
206 }
207
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200208 platform_set_drvdata(pdev, rtc_data);
209
Wolfram Sang46b21212011-05-25 12:56:50 +0200210 mxs_reset_block(rtc_data->io);
Wolfram Sangb5167152011-05-25 12:56:52 +0200211 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200212 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
213 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
Wolfram Sangb5167152011-05-25 12:56:52 +0200214 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200215
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200216 writel(STMP3XXX_RTC_CTRL_ONEMSEC_IRQ_EN |
217 STMP3XXX_RTC_CTRL_ALARM_IRQ_EN,
218 rtc_data->io + STMP3XXX_RTC_CTRL_CLR);
219
dmitry pervushindf17f632009-09-22 16:46:26 -0700220 rtc_data->rtc = rtc_device_register(pdev->name, &pdev->dev,
221 &stmp3xxx_rtc_ops, THIS_MODULE);
222 if (IS_ERR(rtc_data->rtc)) {
223 err = PTR_ERR(rtc_data->rtc);
224 goto out_remap;
225 }
226
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200227 err = request_irq(rtc_data->irq_alarm, stmp3xxx_rtc_interrupt, 0,
228 "RTC alarm", &pdev->dev);
dmitry pervushindf17f632009-09-22 16:46:26 -0700229 if (err) {
230 dev_err(&pdev->dev, "Cannot claim IRQ%d\n",
231 rtc_data->irq_alarm);
232 goto out_irq_alarm;
233 }
dmitry pervushindf17f632009-09-22 16:46:26 -0700234
dmitry pervushindf17f632009-09-22 16:46:26 -0700235 return 0;
236
dmitry pervushindf17f632009-09-22 16:46:26 -0700237out_irq_alarm:
dmitry pervushindf17f632009-09-22 16:46:26 -0700238 rtc_device_unregister(rtc_data->rtc);
239out_remap:
Wolfram Sanga91d2ba2011-05-25 12:56:51 +0200240 platform_set_drvdata(pdev, NULL);
dmitry pervushindf17f632009-09-22 16:46:26 -0700241 iounmap(rtc_data->io);
242out_free:
243 kfree(rtc_data);
244 return err;
245}
246
247#ifdef CONFIG_PM
248static int stmp3xxx_rtc_suspend(struct platform_device *dev, pm_message_t state)
249{
250 return 0;
251}
252
253static int stmp3xxx_rtc_resume(struct platform_device *dev)
254{
255 struct stmp3xxx_rtc_data *rtc_data = platform_get_drvdata(dev);
256
Wolfram Sang46b21212011-05-25 12:56:50 +0200257 mxs_reset_block(rtc_data->io);
Wolfram Sangb5167152011-05-25 12:56:52 +0200258 writel(STMP3XXX_RTC_PERSISTENT0_ALARM_EN |
Wolfram Sang47eac332011-05-25 12:56:49 +0200259 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE_EN |
260 STMP3XXX_RTC_PERSISTENT0_ALARM_WAKE,
Wolfram Sangb5167152011-05-25 12:56:52 +0200261 rtc_data->io + STMP3XXX_RTC_PERSISTENT0_CLR);
dmitry pervushindf17f632009-09-22 16:46:26 -0700262 return 0;
263}
264#else
265#define stmp3xxx_rtc_suspend NULL
266#define stmp3xxx_rtc_resume NULL
267#endif
268
Marek Vasutdd8d20a2012-06-26 16:29:27 +0800269static const struct of_device_id rtc_dt_ids[] = {
270 { .compatible = "fsl,stmp3xxx-rtc", },
271 { /* sentinel */ }
272};
273MODULE_DEVICE_TABLE(of, rtc_dt_ids);
274
dmitry pervushindf17f632009-09-22 16:46:26 -0700275static struct platform_driver stmp3xxx_rtcdrv = {
276 .probe = stmp3xxx_rtc_probe,
277 .remove = stmp3xxx_rtc_remove,
278 .suspend = stmp3xxx_rtc_suspend,
279 .resume = stmp3xxx_rtc_resume,
280 .driver = {
281 .name = "stmp3xxx-rtc",
282 .owner = THIS_MODULE,
Marek Vasutdd8d20a2012-06-26 16:29:27 +0800283 .of_match_table = rtc_dt_ids,
dmitry pervushindf17f632009-09-22 16:46:26 -0700284 },
285};
286
Axel Lin0c4eae62012-01-10 15:10:48 -0800287module_platform_driver(stmp3xxx_rtcdrv);
dmitry pervushindf17f632009-09-22 16:46:26 -0700288
289MODULE_DESCRIPTION("STMP3xxx RTC Driver");
Wolfram Sang7e794cb2011-05-25 12:56:53 +0200290MODULE_AUTHOR("dmitry pervushin <dpervushin@embeddedalley.com> and "
291 "Wolfram Sang <w.sang@pengutronix.de>");
dmitry pervushindf17f632009-09-22 16:46:26 -0700292MODULE_LICENSE("GPL");