rtc: snvs: Switch to SPDX identifier
[linux-block.git] / drivers / rtc / rtc-mxc.c
CommitLineData
d00ed3cf
DM
1/*
2 * Copyright 2004-2008 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
7 *
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
10 */
11
12#include <linux/io.h>
13#include <linux/rtc.h>
14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
d00ed3cf
DM
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/clk.h>
cec13c26
PR
19#include <linux/of.h>
20#include <linux/of_device.h>
d00ed3cf 21
d00ed3cf
DM
22#define RTC_INPUT_CLK_32768HZ (0x00 << 5)
23#define RTC_INPUT_CLK_32000HZ (0x01 << 5)
24#define RTC_INPUT_CLK_38400HZ (0x02 << 5)
25
26#define RTC_SW_BIT (1 << 0)
27#define RTC_ALM_BIT (1 << 2)
28#define RTC_1HZ_BIT (1 << 4)
29#define RTC_2HZ_BIT (1 << 7)
30#define RTC_SAM0_BIT (1 << 8)
31#define RTC_SAM1_BIT (1 << 9)
32#define RTC_SAM2_BIT (1 << 10)
33#define RTC_SAM3_BIT (1 << 11)
34#define RTC_SAM4_BIT (1 << 12)
35#define RTC_SAM5_BIT (1 << 13)
36#define RTC_SAM6_BIT (1 << 14)
37#define RTC_SAM7_BIT (1 << 15)
38#define PIT_ALL_ON (RTC_2HZ_BIT | RTC_SAM0_BIT | RTC_SAM1_BIT | \
39 RTC_SAM2_BIT | RTC_SAM3_BIT | RTC_SAM4_BIT | \
40 RTC_SAM5_BIT | RTC_SAM6_BIT | RTC_SAM7_BIT)
41
42#define RTC_ENABLE_BIT (1 << 7)
43
44#define MAX_PIE_NUM 9
45#define MAX_PIE_FREQ 512
d00ed3cf 46
d00ed3cf
DM
47#define MXC_RTC_TIME 0
48#define MXC_RTC_ALARM 1
49
50#define RTC_HOURMIN 0x00 /* 32bit rtc hour/min counter reg */
51#define RTC_SECOND 0x04 /* 32bit rtc seconds counter reg */
52#define RTC_ALRM_HM 0x08 /* 32bit rtc alarm hour/min reg */
53#define RTC_ALRM_SEC 0x0C /* 32bit rtc alarm seconds reg */
54#define RTC_RTCCTL 0x10 /* 32bit rtc control reg */
55#define RTC_RTCISR 0x14 /* 32bit rtc interrupt status reg */
56#define RTC_RTCIENR 0x18 /* 32bit rtc interrupt enable reg */
57#define RTC_STPWCH 0x1C /* 32bit rtc stopwatch min reg */
58#define RTC_DAYR 0x20 /* 32bit rtc days counter reg */
59#define RTC_DAYALARM 0x24 /* 32bit rtc day alarm reg */
60#define RTC_TEST1 0x28 /* 32bit rtc test reg 1 */
61#define RTC_TEST2 0x2C /* 32bit rtc test reg 2 */
62#define RTC_TEST3 0x30 /* 32bit rtc test reg 3 */
63
bb1d34a2
SG
64enum imx_rtc_type {
65 IMX1_RTC,
66 IMX21_RTC,
67};
68
d00ed3cf
DM
69struct rtc_plat_data {
70 struct rtc_device *rtc;
71 void __iomem *ioaddr;
72 int irq;
8f5fe778
PR
73 struct clk *clk_ref;
74 struct clk *clk_ipg;
d00ed3cf 75 struct rtc_time g_rtc_alarm;
bb1d34a2 76 enum imx_rtc_type devtype;
d00ed3cf
DM
77};
78
cd6ba00a 79static const struct platform_device_id imx_rtc_devtype[] = {
bb1d34a2
SG
80 {
81 .name = "imx1-rtc",
82 .driver_data = IMX1_RTC,
83 }, {
84 .name = "imx21-rtc",
85 .driver_data = IMX21_RTC,
86 }, {
87 /* sentinel */
88 }
89};
90MODULE_DEVICE_TABLE(platform, imx_rtc_devtype);
91
cec13c26
PR
92#ifdef CONFIG_OF
93static const struct of_device_id imx_rtc_dt_ids[] = {
94 { .compatible = "fsl,imx1-rtc", .data = (const void *)IMX1_RTC },
95 { .compatible = "fsl,imx21-rtc", .data = (const void *)IMX21_RTC },
96 {}
97};
98MODULE_DEVICE_TABLE(of, imx_rtc_dt_ids);
99#endif
100
bb1d34a2
SG
101static inline int is_imx1_rtc(struct rtc_plat_data *data)
102{
103 return data->devtype == IMX1_RTC;
104}
105
d00ed3cf
DM
106/*
107 * This function is used to obtain the RTC time or the alarm value in
108 * second.
109 */
a015b8aa 110static time64_t get_alarm_or_time(struct device *dev, int time_alarm)
d00ed3cf 111{
85368bb9 112 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
d00ed3cf
DM
113 void __iomem *ioaddr = pdata->ioaddr;
114 u32 day = 0, hr = 0, min = 0, sec = 0, hr_min = 0;
115
116 switch (time_alarm) {
117 case MXC_RTC_TIME:
118 day = readw(ioaddr + RTC_DAYR);
119 hr_min = readw(ioaddr + RTC_HOURMIN);
120 sec = readw(ioaddr + RTC_SECOND);
121 break;
122 case MXC_RTC_ALARM:
123 day = readw(ioaddr + RTC_DAYALARM);
124 hr_min = readw(ioaddr + RTC_ALRM_HM) & 0xffff;
125 sec = readw(ioaddr + RTC_ALRM_SEC);
126 break;
127 }
128
129 hr = hr_min >> 8;
130 min = hr_min & 0xff;
131
a015b8aa 132 return ((((time64_t)day * 24 + hr) * 60) + min) * 60 + sec;
d00ed3cf
DM
133}
134
135/*
136 * This function sets the RTC alarm value or the time value.
137 */
a015b8aa 138static void set_alarm_or_time(struct device *dev, int time_alarm, time64_t time)
d00ed3cf 139{
a015b8aa 140 u32 tod, day, hr, min, sec, temp;
85368bb9 141 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
d00ed3cf
DM
142 void __iomem *ioaddr = pdata->ioaddr;
143
a015b8aa 144 day = div_s64_rem(time, 86400, &tod);
d00ed3cf
DM
145
146 /* time is within a day now */
a015b8aa
XP
147 hr = tod / 3600;
148 tod -= hr * 3600;
d00ed3cf
DM
149
150 /* time is within an hour now */
a015b8aa
XP
151 min = tod / 60;
152 sec = tod - min * 60;
d00ed3cf
DM
153
154 temp = (hr << 8) + min;
155
156 switch (time_alarm) {
157 case MXC_RTC_TIME:
158 writew(day, ioaddr + RTC_DAYR);
159 writew(sec, ioaddr + RTC_SECOND);
160 writew(temp, ioaddr + RTC_HOURMIN);
161 break;
162 case MXC_RTC_ALARM:
163 writew(day, ioaddr + RTC_DAYALARM);
164 writew(sec, ioaddr + RTC_ALRM_SEC);
165 writew(temp, ioaddr + RTC_ALRM_HM);
166 break;
167 }
168}
169
170/*
171 * This function updates the RTC alarm registers and then clears all the
172 * interrupt status bits.
173 */
482494a8 174static void rtc_update_alarm(struct device *dev, struct rtc_time *alrm)
d00ed3cf 175{
a015b8aa 176 time64_t time;
85368bb9 177 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
d00ed3cf
DM
178 void __iomem *ioaddr = pdata->ioaddr;
179
a015b8aa 180 time = rtc_tm_to_time64(alrm);
d00ed3cf 181
d00ed3cf
DM
182 /* clear all the interrupt status bits */
183 writew(readw(ioaddr + RTC_RTCISR), ioaddr + RTC_RTCISR);
184 set_alarm_or_time(dev, MXC_RTC_ALARM, time);
c92182ee
YK
185}
186
187static void mxc_rtc_irq_enable(struct device *dev, unsigned int bit,
188 unsigned int enabled)
189{
85368bb9 190 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
c92182ee
YK
191 void __iomem *ioaddr = pdata->ioaddr;
192 u32 reg;
193
194 spin_lock_irq(&pdata->rtc->irq_lock);
195 reg = readw(ioaddr + RTC_RTCIENR);
196
197 if (enabled)
198 reg |= bit;
199 else
200 reg &= ~bit;
201
202 writew(reg, ioaddr + RTC_RTCIENR);
203 spin_unlock_irq(&pdata->rtc->irq_lock);
d00ed3cf
DM
204}
205
206/* This function is the RTC interrupt service routine. */
207static irqreturn_t mxc_rtc_interrupt(int irq, void *dev_id)
208{
209 struct platform_device *pdev = dev_id;
210 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
211 void __iomem *ioaddr = pdata->ioaddr;
b59f6d1f 212 unsigned long flags;
d00ed3cf
DM
213 u32 status;
214 u32 events = 0;
215
b59f6d1f 216 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
d00ed3cf
DM
217 status = readw(ioaddr + RTC_RTCISR) & readw(ioaddr + RTC_RTCIENR);
218 /* clear interrupt sources */
219 writew(status, ioaddr + RTC_RTCISR);
220
d00ed3cf 221 /* update irq data & counter */
c92182ee 222 if (status & RTC_ALM_BIT) {
d00ed3cf 223 events |= (RTC_AF | RTC_IRQF);
c92182ee
YK
224 /* RTC alarm should be one-shot */
225 mxc_rtc_irq_enable(&pdev->dev, RTC_ALM_BIT, 0);
226 }
d00ed3cf 227
d00ed3cf
DM
228 if (status & PIT_ALL_ON)
229 events |= (RTC_PF | RTC_IRQF);
230
d00ed3cf 231 rtc_update_irq(pdata->rtc, 1, events);
b59f6d1f 232 spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
d00ed3cf
DM
233
234 return IRQ_HANDLED;
235}
236
d00ed3cf
DM
237static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
238{
239 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, enabled);
240 return 0;
241}
242
d00ed3cf
DM
243/*
244 * This function reads the current RTC time into tm in Gregorian date.
245 */
246static int mxc_rtc_read_time(struct device *dev, struct rtc_time *tm)
247{
a015b8aa 248 time64_t val;
d00ed3cf
DM
249
250 /* Avoid roll-over from reading the different registers */
251 do {
252 val = get_alarm_or_time(dev, MXC_RTC_TIME);
253 } while (val != get_alarm_or_time(dev, MXC_RTC_TIME));
254
a015b8aa 255 rtc_time64_to_tm(val, tm);
d00ed3cf
DM
256
257 return 0;
258}
259
260/*
261 * This function sets the internal RTC time based on tm in Gregorian date.
262 */
933623c3 263static int mxc_rtc_set_mmss(struct device *dev, time64_t time)
d00ed3cf 264{
85368bb9 265 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
bb1d34a2 266
7287be1d
YK
267 /*
268 * TTC_DAYR register is 9-bit in MX1 SoC, save time and day of year only
269 */
bb1d34a2 270 if (is_imx1_rtc(pdata)) {
7287be1d
YK
271 struct rtc_time tm;
272
933623c3 273 rtc_time64_to_tm(time, &tm);
7287be1d 274 tm.tm_year = 70;
933623c3 275 time = rtc_tm_to_time64(&tm);
7287be1d
YK
276 }
277
d00ed3cf
DM
278 /* Avoid roll-over from reading the different registers */
279 do {
280 set_alarm_or_time(dev, MXC_RTC_TIME, time);
281 } while (time != get_alarm_or_time(dev, MXC_RTC_TIME));
282
283 return 0;
284}
285
286/*
287 * This function reads the current alarm value into the passed in 'alrm'
288 * argument. It updates the alrm's pending field value based on the whether
289 * an alarm interrupt occurs or not.
290 */
291static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
292{
85368bb9 293 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
d00ed3cf
DM
294 void __iomem *ioaddr = pdata->ioaddr;
295
a015b8aa 296 rtc_time64_to_tm(get_alarm_or_time(dev, MXC_RTC_ALARM), &alrm->time);
d00ed3cf
DM
297 alrm->pending = ((readw(ioaddr + RTC_RTCISR) & RTC_ALM_BIT)) ? 1 : 0;
298
299 return 0;
300}
301
302/*
303 * This function sets the RTC alarm based on passed in alrm.
304 */
305static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
306{
85368bb9 307 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
d00ed3cf 308
482494a8 309 rtc_update_alarm(dev, &alrm->time);
d00ed3cf
DM
310
311 memcpy(&pdata->g_rtc_alarm, &alrm->time, sizeof(struct rtc_time));
312 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, alrm->enabled);
313
314 return 0;
315}
316
317/* RTC layer */
8bc57e7f 318static const struct rtc_class_ops mxc_rtc_ops = {
d00ed3cf 319 .read_time = mxc_rtc_read_time,
933623c3 320 .set_mmss64 = mxc_rtc_set_mmss,
d00ed3cf
DM
321 .read_alarm = mxc_rtc_read_alarm,
322 .set_alarm = mxc_rtc_set_alarm,
323 .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
d00ed3cf
DM
324};
325
5a167f45 326static int mxc_rtc_probe(struct platform_device *pdev)
d00ed3cf 327{
d00ed3cf
DM
328 struct resource *res;
329 struct rtc_device *rtc;
330 struct rtc_plat_data *pdata = NULL;
331 u32 reg;
c783a29e
VZ
332 unsigned long rate;
333 int ret;
cec13c26 334 const struct of_device_id *of_id;
d00ed3cf 335
c783a29e 336 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
d00ed3cf
DM
337 if (!pdata)
338 return -ENOMEM;
339
cec13c26
PR
340 of_id = of_match_device(imx_rtc_dt_ids, &pdev->dev);
341 if (of_id)
342 pdata->devtype = (enum imx_rtc_type)of_id->data;
343 else
344 pdata->devtype = pdev->id_entry->driver_data;
bb1d34a2 345
7c1d69ee
JL
346 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347 pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
348 if (IS_ERR(pdata->ioaddr))
349 return PTR_ERR(pdata->ioaddr);
d00ed3cf 350
8f5fe778
PR
351 pdata->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
352 if (IS_ERR(pdata->clk_ipg)) {
353 dev_err(&pdev->dev, "unable to get ipg clock!\n");
354 return PTR_ERR(pdata->clk_ipg);
49908e73 355 }
d00ed3cf 356
8f5fe778 357 ret = clk_prepare_enable(pdata->clk_ipg);
1b3d2243
FE
358 if (ret)
359 return ret;
360
8f5fe778
PR
361 pdata->clk_ref = devm_clk_get(&pdev->dev, "ref");
362 if (IS_ERR(pdata->clk_ref)) {
363 dev_err(&pdev->dev, "unable to get ref clock!\n");
364 ret = PTR_ERR(pdata->clk_ref);
365 goto exit_put_clk_ipg;
366 }
367
368 ret = clk_prepare_enable(pdata->clk_ref);
369 if (ret)
370 goto exit_put_clk_ipg;
371
372 rate = clk_get_rate(pdata->clk_ref);
d00ed3cf
DM
373
374 if (rate == 32768)
375 reg = RTC_INPUT_CLK_32768HZ;
376 else if (rate == 32000)
377 reg = RTC_INPUT_CLK_32000HZ;
378 else if (rate == 38400)
379 reg = RTC_INPUT_CLK_38400HZ;
380 else {
c783a29e 381 dev_err(&pdev->dev, "rtc clock is not valid (%lu)\n", rate);
d00ed3cf 382 ret = -EINVAL;
8f5fe778 383 goto exit_put_clk_ref;
d00ed3cf
DM
384 }
385
386 reg |= RTC_ENABLE_BIT;
387 writew(reg, (pdata->ioaddr + RTC_RTCCTL));
388 if (((readw(pdata->ioaddr + RTC_RTCCTL)) & RTC_ENABLE_BIT) == 0) {
389 dev_err(&pdev->dev, "hardware module can't be enabled!\n");
390 ret = -EIO;
8f5fe778 391 goto exit_put_clk_ref;
d00ed3cf
DM
392 }
393
d00ed3cf
DM
394 platform_set_drvdata(pdev, pdata);
395
396 /* Configure and enable the RTC */
397 pdata->irq = platform_get_irq(pdev, 0);
398
399 if (pdata->irq >= 0 &&
c783a29e
VZ
400 devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt,
401 IRQF_SHARED, pdev->name, pdev) < 0) {
d00ed3cf
DM
402 dev_warn(&pdev->dev, "interrupt not available.\n");
403 pdata->irq = -1;
404 }
405
4a8282d0 406 if (pdata->irq >= 0)
c92182ee
YK
407 device_init_wakeup(&pdev->dev, 1);
408
033ca3ad 409 rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
5f54c8a0
WS
410 THIS_MODULE);
411 if (IS_ERR(rtc)) {
412 ret = PTR_ERR(rtc);
8f5fe778 413 goto exit_put_clk_ref;
5f54c8a0
WS
414 }
415
416 pdata->rtc = rtc;
417
d00ed3cf
DM
418 return 0;
419
8f5fe778
PR
420exit_put_clk_ref:
421 clk_disable_unprepare(pdata->clk_ref);
422exit_put_clk_ipg:
423 clk_disable_unprepare(pdata->clk_ipg);
d00ed3cf 424
d00ed3cf
DM
425 return ret;
426}
427
5a167f45 428static int mxc_rtc_remove(struct platform_device *pdev)
d00ed3cf
DM
429{
430 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
431
8f5fe778
PR
432 clk_disable_unprepare(pdata->clk_ref);
433 clk_disable_unprepare(pdata->clk_ipg);
d00ed3cf
DM
434
435 return 0;
436}
437
75634cc4 438#ifdef CONFIG_PM_SLEEP
c92182ee
YK
439static int mxc_rtc_suspend(struct device *dev)
440{
441 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
442
443 if (device_may_wakeup(dev))
444 enable_irq_wake(pdata->irq);
445
446 return 0;
447}
448
449static int mxc_rtc_resume(struct device *dev)
450{
451 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
452
453 if (device_may_wakeup(dev))
454 disable_irq_wake(pdata->irq);
455
456 return 0;
457}
c92182ee
YK
458#endif
459
75634cc4
JH
460static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
461
d00ed3cf
DM
462static struct platform_driver mxc_rtc_driver = {
463 .driver = {
464 .name = "mxc_rtc",
cec13c26 465 .of_match_table = of_match_ptr(imx_rtc_dt_ids),
c92182ee 466 .pm = &mxc_rtc_pm_ops,
d00ed3cf 467 },
bb1d34a2 468 .id_table = imx_rtc_devtype,
be8b6d51 469 .probe = mxc_rtc_probe,
5a167f45 470 .remove = mxc_rtc_remove,
d00ed3cf
DM
471};
472
be8b6d51 473module_platform_driver(mxc_rtc_driver)
d00ed3cf
DM
474
475MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
476MODULE_DESCRIPTION("RTC driver for Freescale MXC");
477MODULE_LICENSE("GPL");
478