ext2: support statx syscall
[linux/fpc-iii.git] / drivers / rtc / rtc-mxc.c
blob878c6ee829019d3a709a50924301e3dc39c12fb8
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright 2004-2008 Freescale Semiconductor, Inc. All Rights Reserved.
5 #include <linux/io.h>
6 #include <linux/rtc.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
15 #define RTC_INPUT_CLK_32768HZ (0x00 << 5)
16 #define RTC_INPUT_CLK_32000HZ (0x01 << 5)
17 #define RTC_INPUT_CLK_38400HZ (0x02 << 5)
19 #define RTC_SW_BIT (1 << 0)
20 #define RTC_ALM_BIT (1 << 2)
21 #define RTC_1HZ_BIT (1 << 4)
22 #define RTC_2HZ_BIT (1 << 7)
23 #define RTC_SAM0_BIT (1 << 8)
24 #define RTC_SAM1_BIT (1 << 9)
25 #define RTC_SAM2_BIT (1 << 10)
26 #define RTC_SAM3_BIT (1 << 11)
27 #define RTC_SAM4_BIT (1 << 12)
28 #define RTC_SAM5_BIT (1 << 13)
29 #define RTC_SAM6_BIT (1 << 14)
30 #define RTC_SAM7_BIT (1 << 15)
31 #define PIT_ALL_ON (RTC_2HZ_BIT | RTC_SAM0_BIT | RTC_SAM1_BIT | \
32 RTC_SAM2_BIT | RTC_SAM3_BIT | RTC_SAM4_BIT | \
33 RTC_SAM5_BIT | RTC_SAM6_BIT | RTC_SAM7_BIT)
35 #define RTC_ENABLE_BIT (1 << 7)
37 #define MAX_PIE_NUM 9
38 #define MAX_PIE_FREQ 512
40 #define MXC_RTC_TIME 0
41 #define MXC_RTC_ALARM 1
43 #define RTC_HOURMIN 0x00 /* 32bit rtc hour/min counter reg */
44 #define RTC_SECOND 0x04 /* 32bit rtc seconds counter reg */
45 #define RTC_ALRM_HM 0x08 /* 32bit rtc alarm hour/min reg */
46 #define RTC_ALRM_SEC 0x0C /* 32bit rtc alarm seconds reg */
47 #define RTC_RTCCTL 0x10 /* 32bit rtc control reg */
48 #define RTC_RTCISR 0x14 /* 32bit rtc interrupt status reg */
49 #define RTC_RTCIENR 0x18 /* 32bit rtc interrupt enable reg */
50 #define RTC_STPWCH 0x1C /* 32bit rtc stopwatch min reg */
51 #define RTC_DAYR 0x20 /* 32bit rtc days counter reg */
52 #define RTC_DAYALARM 0x24 /* 32bit rtc day alarm reg */
53 #define RTC_TEST1 0x28 /* 32bit rtc test reg 1 */
54 #define RTC_TEST2 0x2C /* 32bit rtc test reg 2 */
55 #define RTC_TEST3 0x30 /* 32bit rtc test reg 3 */
57 enum imx_rtc_type {
58 IMX1_RTC,
59 IMX21_RTC,
62 struct rtc_plat_data {
63 struct rtc_device *rtc;
64 void __iomem *ioaddr;
65 int irq;
66 struct clk *clk_ref;
67 struct clk *clk_ipg;
68 struct rtc_time g_rtc_alarm;
69 enum imx_rtc_type devtype;
72 static const struct platform_device_id imx_rtc_devtype[] = {
74 .name = "imx1-rtc",
75 .driver_data = IMX1_RTC,
76 }, {
77 .name = "imx21-rtc",
78 .driver_data = IMX21_RTC,
79 }, {
80 /* sentinel */
83 MODULE_DEVICE_TABLE(platform, imx_rtc_devtype);
85 #ifdef CONFIG_OF
86 static const struct of_device_id imx_rtc_dt_ids[] = {
87 { .compatible = "fsl,imx1-rtc", .data = (const void *)IMX1_RTC },
88 { .compatible = "fsl,imx21-rtc", .data = (const void *)IMX21_RTC },
91 MODULE_DEVICE_TABLE(of, imx_rtc_dt_ids);
92 #endif
94 static inline int is_imx1_rtc(struct rtc_plat_data *data)
96 return data->devtype == IMX1_RTC;
100 * This function is used to obtain the RTC time or the alarm value in
101 * second.
103 static time64_t get_alarm_or_time(struct device *dev, int time_alarm)
105 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
106 void __iomem *ioaddr = pdata->ioaddr;
107 u32 day = 0, hr = 0, min = 0, sec = 0, hr_min = 0;
109 switch (time_alarm) {
110 case MXC_RTC_TIME:
111 day = readw(ioaddr + RTC_DAYR);
112 hr_min = readw(ioaddr + RTC_HOURMIN);
113 sec = readw(ioaddr + RTC_SECOND);
114 break;
115 case MXC_RTC_ALARM:
116 day = readw(ioaddr + RTC_DAYALARM);
117 hr_min = readw(ioaddr + RTC_ALRM_HM) & 0xffff;
118 sec = readw(ioaddr + RTC_ALRM_SEC);
119 break;
122 hr = hr_min >> 8;
123 min = hr_min & 0xff;
125 return ((((time64_t)day * 24 + hr) * 60) + min) * 60 + sec;
129 * This function sets the RTC alarm value or the time value.
131 static void set_alarm_or_time(struct device *dev, int time_alarm, time64_t time)
133 u32 tod, day, hr, min, sec, temp;
134 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
135 void __iomem *ioaddr = pdata->ioaddr;
137 day = div_s64_rem(time, 86400, &tod);
139 /* time is within a day now */
140 hr = tod / 3600;
141 tod -= hr * 3600;
143 /* time is within an hour now */
144 min = tod / 60;
145 sec = tod - min * 60;
147 temp = (hr << 8) + min;
149 switch (time_alarm) {
150 case MXC_RTC_TIME:
151 writew(day, ioaddr + RTC_DAYR);
152 writew(sec, ioaddr + RTC_SECOND);
153 writew(temp, ioaddr + RTC_HOURMIN);
154 break;
155 case MXC_RTC_ALARM:
156 writew(day, ioaddr + RTC_DAYALARM);
157 writew(sec, ioaddr + RTC_ALRM_SEC);
158 writew(temp, ioaddr + RTC_ALRM_HM);
159 break;
164 * This function updates the RTC alarm registers and then clears all the
165 * interrupt status bits.
167 static void rtc_update_alarm(struct device *dev, struct rtc_time *alrm)
169 time64_t time;
170 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
171 void __iomem *ioaddr = pdata->ioaddr;
173 time = rtc_tm_to_time64(alrm);
175 /* clear all the interrupt status bits */
176 writew(readw(ioaddr + RTC_RTCISR), ioaddr + RTC_RTCISR);
177 set_alarm_or_time(dev, MXC_RTC_ALARM, time);
180 static void mxc_rtc_irq_enable(struct device *dev, unsigned int bit,
181 unsigned int enabled)
183 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
184 void __iomem *ioaddr = pdata->ioaddr;
185 u32 reg;
187 spin_lock_irq(&pdata->rtc->irq_lock);
188 reg = readw(ioaddr + RTC_RTCIENR);
190 if (enabled)
191 reg |= bit;
192 else
193 reg &= ~bit;
195 writew(reg, ioaddr + RTC_RTCIENR);
196 spin_unlock_irq(&pdata->rtc->irq_lock);
199 /* This function is the RTC interrupt service routine. */
200 static irqreturn_t mxc_rtc_interrupt(int irq, void *dev_id)
202 struct platform_device *pdev = dev_id;
203 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
204 void __iomem *ioaddr = pdata->ioaddr;
205 unsigned long flags;
206 u32 status;
207 u32 events = 0;
209 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
210 status = readw(ioaddr + RTC_RTCISR) & readw(ioaddr + RTC_RTCIENR);
211 /* clear interrupt sources */
212 writew(status, ioaddr + RTC_RTCISR);
214 /* update irq data & counter */
215 if (status & RTC_ALM_BIT) {
216 events |= (RTC_AF | RTC_IRQF);
217 /* RTC alarm should be one-shot */
218 mxc_rtc_irq_enable(&pdev->dev, RTC_ALM_BIT, 0);
221 if (status & PIT_ALL_ON)
222 events |= (RTC_PF | RTC_IRQF);
224 rtc_update_irq(pdata->rtc, 1, events);
225 spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
227 return IRQ_HANDLED;
230 static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
232 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, enabled);
233 return 0;
237 * This function reads the current RTC time into tm in Gregorian date.
239 static int mxc_rtc_read_time(struct device *dev, struct rtc_time *tm)
241 time64_t val;
243 /* Avoid roll-over from reading the different registers */
244 do {
245 val = get_alarm_or_time(dev, MXC_RTC_TIME);
246 } while (val != get_alarm_or_time(dev, MXC_RTC_TIME));
248 rtc_time64_to_tm(val, tm);
250 return 0;
254 * This function sets the internal RTC time based on tm in Gregorian date.
256 static int mxc_rtc_set_mmss(struct device *dev, time64_t time)
258 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
261 * TTC_DAYR register is 9-bit in MX1 SoC, save time and day of year only
263 if (is_imx1_rtc(pdata)) {
264 struct rtc_time tm;
266 rtc_time64_to_tm(time, &tm);
267 tm.tm_year = 70;
268 time = rtc_tm_to_time64(&tm);
271 /* Avoid roll-over from reading the different registers */
272 do {
273 set_alarm_or_time(dev, MXC_RTC_TIME, time);
274 } while (time != get_alarm_or_time(dev, MXC_RTC_TIME));
276 return 0;
280 * This function reads the current alarm value into the passed in 'alrm'
281 * argument. It updates the alrm's pending field value based on the whether
282 * an alarm interrupt occurs or not.
284 static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
286 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
287 void __iomem *ioaddr = pdata->ioaddr;
289 rtc_time64_to_tm(get_alarm_or_time(dev, MXC_RTC_ALARM), &alrm->time);
290 alrm->pending = ((readw(ioaddr + RTC_RTCISR) & RTC_ALM_BIT)) ? 1 : 0;
292 return 0;
296 * This function sets the RTC alarm based on passed in alrm.
298 static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
300 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
302 rtc_update_alarm(dev, &alrm->time);
304 memcpy(&pdata->g_rtc_alarm, &alrm->time, sizeof(struct rtc_time));
305 mxc_rtc_irq_enable(dev, RTC_ALM_BIT, alrm->enabled);
307 return 0;
310 /* RTC layer */
311 static const struct rtc_class_ops mxc_rtc_ops = {
312 .read_time = mxc_rtc_read_time,
313 .set_mmss64 = mxc_rtc_set_mmss,
314 .read_alarm = mxc_rtc_read_alarm,
315 .set_alarm = mxc_rtc_set_alarm,
316 .alarm_irq_enable = mxc_rtc_alarm_irq_enable,
319 static int mxc_rtc_probe(struct platform_device *pdev)
321 struct resource *res;
322 struct rtc_device *rtc;
323 struct rtc_plat_data *pdata = NULL;
324 u32 reg;
325 unsigned long rate;
326 int ret;
327 const struct of_device_id *of_id;
329 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
330 if (!pdata)
331 return -ENOMEM;
333 of_id = of_match_device(imx_rtc_dt_ids, &pdev->dev);
334 if (of_id)
335 pdata->devtype = (enum imx_rtc_type)of_id->data;
336 else
337 pdata->devtype = pdev->id_entry->driver_data;
339 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
340 pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
341 if (IS_ERR(pdata->ioaddr))
342 return PTR_ERR(pdata->ioaddr);
344 pdata->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
345 if (IS_ERR(pdata->clk_ipg)) {
346 dev_err(&pdev->dev, "unable to get ipg clock!\n");
347 return PTR_ERR(pdata->clk_ipg);
350 ret = clk_prepare_enable(pdata->clk_ipg);
351 if (ret)
352 return ret;
354 pdata->clk_ref = devm_clk_get(&pdev->dev, "ref");
355 if (IS_ERR(pdata->clk_ref)) {
356 dev_err(&pdev->dev, "unable to get ref clock!\n");
357 ret = PTR_ERR(pdata->clk_ref);
358 goto exit_put_clk_ipg;
361 ret = clk_prepare_enable(pdata->clk_ref);
362 if (ret)
363 goto exit_put_clk_ipg;
365 rate = clk_get_rate(pdata->clk_ref);
367 if (rate == 32768)
368 reg = RTC_INPUT_CLK_32768HZ;
369 else if (rate == 32000)
370 reg = RTC_INPUT_CLK_32000HZ;
371 else if (rate == 38400)
372 reg = RTC_INPUT_CLK_38400HZ;
373 else {
374 dev_err(&pdev->dev, "rtc clock is not valid (%lu)\n", rate);
375 ret = -EINVAL;
376 goto exit_put_clk_ref;
379 reg |= RTC_ENABLE_BIT;
380 writew(reg, (pdata->ioaddr + RTC_RTCCTL));
381 if (((readw(pdata->ioaddr + RTC_RTCCTL)) & RTC_ENABLE_BIT) == 0) {
382 dev_err(&pdev->dev, "hardware module can't be enabled!\n");
383 ret = -EIO;
384 goto exit_put_clk_ref;
387 platform_set_drvdata(pdev, pdata);
389 /* Configure and enable the RTC */
390 pdata->irq = platform_get_irq(pdev, 0);
392 if (pdata->irq >= 0 &&
393 devm_request_irq(&pdev->dev, pdata->irq, mxc_rtc_interrupt,
394 IRQF_SHARED, pdev->name, pdev) < 0) {
395 dev_warn(&pdev->dev, "interrupt not available.\n");
396 pdata->irq = -1;
399 if (pdata->irq >= 0)
400 device_init_wakeup(&pdev->dev, 1);
402 rtc = devm_rtc_device_register(&pdev->dev, pdev->name, &mxc_rtc_ops,
403 THIS_MODULE);
404 if (IS_ERR(rtc)) {
405 ret = PTR_ERR(rtc);
406 goto exit_put_clk_ref;
409 pdata->rtc = rtc;
411 return 0;
413 exit_put_clk_ref:
414 clk_disable_unprepare(pdata->clk_ref);
415 exit_put_clk_ipg:
416 clk_disable_unprepare(pdata->clk_ipg);
418 return ret;
421 static int mxc_rtc_remove(struct platform_device *pdev)
423 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
425 clk_disable_unprepare(pdata->clk_ref);
426 clk_disable_unprepare(pdata->clk_ipg);
428 return 0;
431 #ifdef CONFIG_PM_SLEEP
432 static int mxc_rtc_suspend(struct device *dev)
434 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
436 if (device_may_wakeup(dev))
437 enable_irq_wake(pdata->irq);
439 return 0;
442 static int mxc_rtc_resume(struct device *dev)
444 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
446 if (device_may_wakeup(dev))
447 disable_irq_wake(pdata->irq);
449 return 0;
451 #endif
453 static SIMPLE_DEV_PM_OPS(mxc_rtc_pm_ops, mxc_rtc_suspend, mxc_rtc_resume);
455 static struct platform_driver mxc_rtc_driver = {
456 .driver = {
457 .name = "mxc_rtc",
458 .of_match_table = of_match_ptr(imx_rtc_dt_ids),
459 .pm = &mxc_rtc_pm_ops,
461 .id_table = imx_rtc_devtype,
462 .probe = mxc_rtc_probe,
463 .remove = mxc_rtc_remove,
466 module_platform_driver(mxc_rtc_driver)
468 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
469 MODULE_DESCRIPTION("RTC driver for Freescale MXC");
470 MODULE_LICENSE("GPL");