1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
250aae724SAnatolij Gustschin /*
350aae724SAnatolij Gustschin * Real-time clock driver for MPC5121
450aae724SAnatolij Gustschin *
550aae724SAnatolij Gustschin * Copyright 2007, Domen Puncer <domen.puncer@telargo.com>
650aae724SAnatolij Gustschin * Copyright 2008, Freescale Semiconductor, Inc. All rights reserved.
7955dbea3SDmitry Eremin-Solenikov * Copyright 2011, Dmitry Eremin-Solenikov
850aae724SAnatolij Gustschin */
950aae724SAnatolij Gustschin
1050aae724SAnatolij Gustschin #include <linux/init.h>
1150aae724SAnatolij Gustschin #include <linux/module.h>
1250aae724SAnatolij Gustschin #include <linux/rtc.h>
13c8a6046eSSachin Kamat #include <linux/of.h>
145af50730SRob Herring #include <linux/of_irq.h>
1548144c28SRob Herring #include <linux/platform_device.h>
1650aae724SAnatolij Gustschin #include <linux/io.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
1850aae724SAnatolij Gustschin
1950aae724SAnatolij Gustschin struct mpc5121_rtc_regs {
2050aae724SAnatolij Gustschin u8 set_time; /* RTC + 0x00 */
2150aae724SAnatolij Gustschin u8 hour_set; /* RTC + 0x01 */
2250aae724SAnatolij Gustschin u8 minute_set; /* RTC + 0x02 */
2350aae724SAnatolij Gustschin u8 second_set; /* RTC + 0x03 */
2450aae724SAnatolij Gustschin
2550aae724SAnatolij Gustschin u8 set_date; /* RTC + 0x04 */
2650aae724SAnatolij Gustschin u8 month_set; /* RTC + 0x05 */
2750aae724SAnatolij Gustschin u8 weekday_set; /* RTC + 0x06 */
2850aae724SAnatolij Gustschin u8 date_set; /* RTC + 0x07 */
2950aae724SAnatolij Gustschin
3050aae724SAnatolij Gustschin u8 write_sw; /* RTC + 0x08 */
3150aae724SAnatolij Gustschin u8 sw_set; /* RTC + 0x09 */
3250aae724SAnatolij Gustschin u16 year_set; /* RTC + 0x0a */
3350aae724SAnatolij Gustschin
3450aae724SAnatolij Gustschin u8 alm_enable; /* RTC + 0x0c */
3550aae724SAnatolij Gustschin u8 alm_hour_set; /* RTC + 0x0d */
3650aae724SAnatolij Gustschin u8 alm_min_set; /* RTC + 0x0e */
3750aae724SAnatolij Gustschin u8 int_enable; /* RTC + 0x0f */
3850aae724SAnatolij Gustschin
3950aae724SAnatolij Gustschin u8 reserved1;
4050aae724SAnatolij Gustschin u8 hour; /* RTC + 0x11 */
4150aae724SAnatolij Gustschin u8 minute; /* RTC + 0x12 */
4250aae724SAnatolij Gustschin u8 second; /* RTC + 0x13 */
4350aae724SAnatolij Gustschin
4450aae724SAnatolij Gustschin u8 month; /* RTC + 0x14 */
4550aae724SAnatolij Gustschin u8 wday_mday; /* RTC + 0x15 */
4650aae724SAnatolij Gustschin u16 year; /* RTC + 0x16 */
4750aae724SAnatolij Gustschin
4850aae724SAnatolij Gustschin u8 int_alm; /* RTC + 0x18 */
4950aae724SAnatolij Gustschin u8 int_sw; /* RTC + 0x19 */
5050aae724SAnatolij Gustschin u8 alm_status; /* RTC + 0x1a */
5150aae724SAnatolij Gustschin u8 sw_minute; /* RTC + 0x1b */
5250aae724SAnatolij Gustschin
5350aae724SAnatolij Gustschin u8 bus_error_1; /* RTC + 0x1c */
5450aae724SAnatolij Gustschin u8 int_day; /* RTC + 0x1d */
5550aae724SAnatolij Gustschin u8 int_min; /* RTC + 0x1e */
5650aae724SAnatolij Gustschin u8 int_sec; /* RTC + 0x1f */
5750aae724SAnatolij Gustschin
5850aae724SAnatolij Gustschin /*
5950aae724SAnatolij Gustschin * target_time:
6050aae724SAnatolij Gustschin * intended to be used for hibernation but hibernation
6150aae724SAnatolij Gustschin * does not work on silicon rev 1.5 so use it for non-volatile
6250aae724SAnatolij Gustschin * storage of offset between the actual_time register and linux
6350aae724SAnatolij Gustschin * time
6450aae724SAnatolij Gustschin */
6550aae724SAnatolij Gustschin u32 target_time; /* RTC + 0x20 */
6650aae724SAnatolij Gustschin /*
6750aae724SAnatolij Gustschin * actual_time:
6850aae724SAnatolij Gustschin * readonly time since VBAT_RTC was last connected
6950aae724SAnatolij Gustschin */
7050aae724SAnatolij Gustschin u32 actual_time; /* RTC + 0x24 */
7150aae724SAnatolij Gustschin u32 keep_alive; /* RTC + 0x28 */
7250aae724SAnatolij Gustschin };
7350aae724SAnatolij Gustschin
7450aae724SAnatolij Gustschin struct mpc5121_rtc_data {
7550aae724SAnatolij Gustschin unsigned irq;
7650aae724SAnatolij Gustschin unsigned irq_periodic;
7750aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs;
7850aae724SAnatolij Gustschin struct rtc_device *rtc;
7950aae724SAnatolij Gustschin struct rtc_wkalrm wkalarm;
8050aae724SAnatolij Gustschin };
8150aae724SAnatolij Gustschin
8250aae724SAnatolij Gustschin /*
8350aae724SAnatolij Gustschin * Update second/minute/hour registers.
8450aae724SAnatolij Gustschin *
8550aae724SAnatolij Gustschin * This is just so alarm will work.
8650aae724SAnatolij Gustschin */
mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem * regs,struct rtc_time * tm)8750aae724SAnatolij Gustschin static void mpc5121_rtc_update_smh(struct mpc5121_rtc_regs __iomem *regs,
8850aae724SAnatolij Gustschin struct rtc_time *tm)
8950aae724SAnatolij Gustschin {
9050aae724SAnatolij Gustschin out_8(®s->second_set, tm->tm_sec);
9150aae724SAnatolij Gustschin out_8(®s->minute_set, tm->tm_min);
9250aae724SAnatolij Gustschin out_8(®s->hour_set, tm->tm_hour);
9350aae724SAnatolij Gustschin
9450aae724SAnatolij Gustschin /* set time sequence */
9550aae724SAnatolij Gustschin out_8(®s->set_time, 0x1);
9650aae724SAnatolij Gustschin out_8(®s->set_time, 0x3);
9750aae724SAnatolij Gustschin out_8(®s->set_time, 0x1);
9850aae724SAnatolij Gustschin out_8(®s->set_time, 0x0);
9950aae724SAnatolij Gustschin }
10050aae724SAnatolij Gustschin
mpc5121_rtc_read_time(struct device * dev,struct rtc_time * tm)10150aae724SAnatolij Gustschin static int mpc5121_rtc_read_time(struct device *dev, struct rtc_time *tm)
10250aae724SAnatolij Gustschin {
10350aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
10450aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
10550aae724SAnatolij Gustschin unsigned long now;
10650aae724SAnatolij Gustschin
10750aae724SAnatolij Gustschin /*
10850aae724SAnatolij Gustschin * linux time is actual_time plus the offset saved in target_time
10950aae724SAnatolij Gustschin */
11050aae724SAnatolij Gustschin now = in_be32(®s->actual_time) + in_be32(®s->target_time);
11150aae724SAnatolij Gustschin
1123d5a4954SAlexandre Belloni rtc_time64_to_tm(now, tm);
11350aae724SAnatolij Gustschin
11450aae724SAnatolij Gustschin /*
11550aae724SAnatolij Gustschin * update second minute hour registers
11650aae724SAnatolij Gustschin * so alarms will work
11750aae724SAnatolij Gustschin */
11850aae724SAnatolij Gustschin mpc5121_rtc_update_smh(regs, tm);
11950aae724SAnatolij Gustschin
120ab62670eSAlexandre Belloni return 0;
12150aae724SAnatolij Gustschin }
12250aae724SAnatolij Gustschin
mpc5121_rtc_set_time(struct device * dev,struct rtc_time * tm)12350aae724SAnatolij Gustschin static int mpc5121_rtc_set_time(struct device *dev, struct rtc_time *tm)
12450aae724SAnatolij Gustschin {
12550aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
12650aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
12750aae724SAnatolij Gustschin unsigned long now;
12850aae724SAnatolij Gustschin
12950aae724SAnatolij Gustschin /*
13050aae724SAnatolij Gustschin * The actual_time register is read only so we write the offset
13150aae724SAnatolij Gustschin * between it and linux time to the target_time register.
13250aae724SAnatolij Gustschin */
1333d5a4954SAlexandre Belloni now = rtc_tm_to_time64(tm);
13450aae724SAnatolij Gustschin out_be32(®s->target_time, now - in_be32(®s->actual_time));
13550aae724SAnatolij Gustschin
13650aae724SAnatolij Gustschin /*
13750aae724SAnatolij Gustschin * update second minute hour registers
13850aae724SAnatolij Gustschin * so alarms will work
13950aae724SAnatolij Gustschin */
14050aae724SAnatolij Gustschin mpc5121_rtc_update_smh(regs, tm);
14150aae724SAnatolij Gustschin
14250aae724SAnatolij Gustschin return 0;
14350aae724SAnatolij Gustschin }
14450aae724SAnatolij Gustschin
mpc5200_rtc_read_time(struct device * dev,struct rtc_time * tm)145955dbea3SDmitry Eremin-Solenikov static int mpc5200_rtc_read_time(struct device *dev, struct rtc_time *tm)
146955dbea3SDmitry Eremin-Solenikov {
147955dbea3SDmitry Eremin-Solenikov struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
148955dbea3SDmitry Eremin-Solenikov struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
149955dbea3SDmitry Eremin-Solenikov int tmp;
150955dbea3SDmitry Eremin-Solenikov
151955dbea3SDmitry Eremin-Solenikov tm->tm_sec = in_8(®s->second);
152955dbea3SDmitry Eremin-Solenikov tm->tm_min = in_8(®s->minute);
153955dbea3SDmitry Eremin-Solenikov
154955dbea3SDmitry Eremin-Solenikov /* 12 hour format? */
155955dbea3SDmitry Eremin-Solenikov if (in_8(®s->hour) & 0x20)
156955dbea3SDmitry Eremin-Solenikov tm->tm_hour = (in_8(®s->hour) >> 1) +
157955dbea3SDmitry Eremin-Solenikov (in_8(®s->hour) & 1 ? 12 : 0);
158955dbea3SDmitry Eremin-Solenikov else
159955dbea3SDmitry Eremin-Solenikov tm->tm_hour = in_8(®s->hour);
160955dbea3SDmitry Eremin-Solenikov
161955dbea3SDmitry Eremin-Solenikov tmp = in_8(®s->wday_mday);
162955dbea3SDmitry Eremin-Solenikov tm->tm_mday = tmp & 0x1f;
163955dbea3SDmitry Eremin-Solenikov tm->tm_mon = in_8(®s->month) - 1;
164955dbea3SDmitry Eremin-Solenikov tm->tm_year = in_be16(®s->year) - 1900;
165955dbea3SDmitry Eremin-Solenikov tm->tm_wday = (tmp >> 5) % 7;
166955dbea3SDmitry Eremin-Solenikov tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
167955dbea3SDmitry Eremin-Solenikov tm->tm_isdst = 0;
168955dbea3SDmitry Eremin-Solenikov
169955dbea3SDmitry Eremin-Solenikov return 0;
170955dbea3SDmitry Eremin-Solenikov }
171955dbea3SDmitry Eremin-Solenikov
mpc5200_rtc_set_time(struct device * dev,struct rtc_time * tm)172955dbea3SDmitry Eremin-Solenikov static int mpc5200_rtc_set_time(struct device *dev, struct rtc_time *tm)
173955dbea3SDmitry Eremin-Solenikov {
174955dbea3SDmitry Eremin-Solenikov struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
175955dbea3SDmitry Eremin-Solenikov struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
176955dbea3SDmitry Eremin-Solenikov
177955dbea3SDmitry Eremin-Solenikov mpc5121_rtc_update_smh(regs, tm);
178955dbea3SDmitry Eremin-Solenikov
179955dbea3SDmitry Eremin-Solenikov /* date */
180955dbea3SDmitry Eremin-Solenikov out_8(®s->month_set, tm->tm_mon + 1);
181955dbea3SDmitry Eremin-Solenikov out_8(®s->weekday_set, tm->tm_wday ? tm->tm_wday : 7);
182955dbea3SDmitry Eremin-Solenikov out_8(®s->date_set, tm->tm_mday);
183955dbea3SDmitry Eremin-Solenikov out_be16(®s->year_set, tm->tm_year + 1900);
184955dbea3SDmitry Eremin-Solenikov
185955dbea3SDmitry Eremin-Solenikov /* set date sequence */
186955dbea3SDmitry Eremin-Solenikov out_8(®s->set_date, 0x1);
187955dbea3SDmitry Eremin-Solenikov out_8(®s->set_date, 0x3);
188955dbea3SDmitry Eremin-Solenikov out_8(®s->set_date, 0x1);
189955dbea3SDmitry Eremin-Solenikov out_8(®s->set_date, 0x0);
190955dbea3SDmitry Eremin-Solenikov
191955dbea3SDmitry Eremin-Solenikov return 0;
192955dbea3SDmitry Eremin-Solenikov }
193955dbea3SDmitry Eremin-Solenikov
mpc5121_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alarm)19450aae724SAnatolij Gustschin static int mpc5121_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
19550aae724SAnatolij Gustschin {
19650aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
19750aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
19850aae724SAnatolij Gustschin
19950aae724SAnatolij Gustschin *alarm = rtc->wkalarm;
20050aae724SAnatolij Gustschin
20150aae724SAnatolij Gustschin alarm->pending = in_8(®s->alm_status);
20250aae724SAnatolij Gustschin
20350aae724SAnatolij Gustschin return 0;
20450aae724SAnatolij Gustschin }
20550aae724SAnatolij Gustschin
mpc5121_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alarm)20650aae724SAnatolij Gustschin static int mpc5121_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
20750aae724SAnatolij Gustschin {
20850aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
20950aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
21050aae724SAnatolij Gustschin
21150aae724SAnatolij Gustschin alarm->time.tm_mday = -1;
21250aae724SAnatolij Gustschin alarm->time.tm_mon = -1;
21350aae724SAnatolij Gustschin alarm->time.tm_year = -1;
21450aae724SAnatolij Gustschin
21550aae724SAnatolij Gustschin out_8(®s->alm_min_set, alarm->time.tm_min);
21650aae724SAnatolij Gustschin out_8(®s->alm_hour_set, alarm->time.tm_hour);
21750aae724SAnatolij Gustschin
21850aae724SAnatolij Gustschin out_8(®s->alm_enable, alarm->enabled);
21950aae724SAnatolij Gustschin
22050aae724SAnatolij Gustschin rtc->wkalarm = *alarm;
22150aae724SAnatolij Gustschin return 0;
22250aae724SAnatolij Gustschin }
22350aae724SAnatolij Gustschin
mpc5121_rtc_handler(int irq,void * dev)22450aae724SAnatolij Gustschin static irqreturn_t mpc5121_rtc_handler(int irq, void *dev)
22550aae724SAnatolij Gustschin {
22650aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
22750aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
22850aae724SAnatolij Gustschin
22950aae724SAnatolij Gustschin if (in_8(®s->int_alm)) {
23050aae724SAnatolij Gustschin /* acknowledge and clear status */
23150aae724SAnatolij Gustschin out_8(®s->int_alm, 1);
23250aae724SAnatolij Gustschin out_8(®s->alm_status, 1);
23350aae724SAnatolij Gustschin
23450aae724SAnatolij Gustschin rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
23550aae724SAnatolij Gustschin return IRQ_HANDLED;
23650aae724SAnatolij Gustschin }
23750aae724SAnatolij Gustschin
23850aae724SAnatolij Gustschin return IRQ_NONE;
23950aae724SAnatolij Gustschin }
24050aae724SAnatolij Gustschin
mpc5121_rtc_handler_upd(int irq,void * dev)24150aae724SAnatolij Gustschin static irqreturn_t mpc5121_rtc_handler_upd(int irq, void *dev)
24250aae724SAnatolij Gustschin {
24350aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata((struct device *)dev);
24450aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
24550aae724SAnatolij Gustschin
24650aae724SAnatolij Gustschin if (in_8(®s->int_sec) && (in_8(®s->int_enable) & 0x1)) {
24750aae724SAnatolij Gustschin /* acknowledge */
24850aae724SAnatolij Gustschin out_8(®s->int_sec, 1);
24950aae724SAnatolij Gustschin
25050aae724SAnatolij Gustschin rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_UF);
25150aae724SAnatolij Gustschin return IRQ_HANDLED;
25250aae724SAnatolij Gustschin }
25350aae724SAnatolij Gustschin
25450aae724SAnatolij Gustschin return IRQ_NONE;
25550aae724SAnatolij Gustschin }
25650aae724SAnatolij Gustschin
mpc5121_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)25750aae724SAnatolij Gustschin static int mpc5121_rtc_alarm_irq_enable(struct device *dev,
25850aae724SAnatolij Gustschin unsigned int enabled)
25950aae724SAnatolij Gustschin {
26050aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc = dev_get_drvdata(dev);
26150aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
26250aae724SAnatolij Gustschin int val;
26350aae724SAnatolij Gustschin
26450aae724SAnatolij Gustschin if (enabled)
26550aae724SAnatolij Gustschin val = 1;
26650aae724SAnatolij Gustschin else
26750aae724SAnatolij Gustschin val = 0;
26850aae724SAnatolij Gustschin
26950aae724SAnatolij Gustschin out_8(®s->alm_enable, val);
27050aae724SAnatolij Gustschin rtc->wkalarm.enabled = val;
27150aae724SAnatolij Gustschin
27250aae724SAnatolij Gustschin return 0;
27350aae724SAnatolij Gustschin }
27450aae724SAnatolij Gustschin
27550aae724SAnatolij Gustschin static const struct rtc_class_ops mpc5121_rtc_ops = {
27650aae724SAnatolij Gustschin .read_time = mpc5121_rtc_read_time,
27750aae724SAnatolij Gustschin .set_time = mpc5121_rtc_set_time,
27850aae724SAnatolij Gustschin .read_alarm = mpc5121_rtc_read_alarm,
27950aae724SAnatolij Gustschin .set_alarm = mpc5121_rtc_set_alarm,
28050aae724SAnatolij Gustschin .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
28150aae724SAnatolij Gustschin };
28250aae724SAnatolij Gustschin
283955dbea3SDmitry Eremin-Solenikov static const struct rtc_class_ops mpc5200_rtc_ops = {
284955dbea3SDmitry Eremin-Solenikov .read_time = mpc5200_rtc_read_time,
285955dbea3SDmitry Eremin-Solenikov .set_time = mpc5200_rtc_set_time,
286955dbea3SDmitry Eremin-Solenikov .read_alarm = mpc5121_rtc_read_alarm,
287955dbea3SDmitry Eremin-Solenikov .set_alarm = mpc5121_rtc_set_alarm,
288955dbea3SDmitry Eremin-Solenikov .alarm_irq_enable = mpc5121_rtc_alarm_irq_enable,
289955dbea3SDmitry Eremin-Solenikov };
290955dbea3SDmitry Eremin-Solenikov
mpc5121_rtc_probe(struct platform_device * op)2915a167f45SGreg Kroah-Hartman static int mpc5121_rtc_probe(struct platform_device *op)
29250aae724SAnatolij Gustschin {
29350aae724SAnatolij Gustschin struct mpc5121_rtc_data *rtc;
29450aae724SAnatolij Gustschin int err = 0;
29550aae724SAnatolij Gustschin
296073bf424SJingoo Han rtc = devm_kzalloc(&op->dev, sizeof(*rtc), GFP_KERNEL);
29750aae724SAnatolij Gustschin if (!rtc)
29850aae724SAnatolij Gustschin return -ENOMEM;
29950aae724SAnatolij Gustschin
300d53d4ae9SAlexandre Belloni rtc->regs = devm_platform_ioremap_resource(op, 0);
301d53d4ae9SAlexandre Belloni if (IS_ERR(rtc->regs)) {
30250aae724SAnatolij Gustschin dev_err(&op->dev, "%s: couldn't map io space\n", __func__);
303710e4a82STiezhu Yang return PTR_ERR(rtc->regs);
30450aae724SAnatolij Gustschin }
30550aae724SAnatolij Gustschin
30650aae724SAnatolij Gustschin device_init_wakeup(&op->dev, 1);
30750aae724SAnatolij Gustschin
308ad87a766SJingoo Han platform_set_drvdata(op, rtc);
30950aae724SAnatolij Gustschin
310295bdd9cSGrant Likely rtc->irq = irq_of_parse_and_map(op->dev.of_node, 1);
311d53d4ae9SAlexandre Belloni err = devm_request_irq(&op->dev, rtc->irq, mpc5121_rtc_handler, 0,
31250aae724SAnatolij Gustschin "mpc5121-rtc", &op->dev);
31350aae724SAnatolij Gustschin if (err) {
31450aae724SAnatolij Gustschin dev_err(&op->dev, "%s: could not request irq: %i\n",
31550aae724SAnatolij Gustschin __func__, rtc->irq);
31650aae724SAnatolij Gustschin goto out_dispose;
31750aae724SAnatolij Gustschin }
31850aae724SAnatolij Gustschin
319295bdd9cSGrant Likely rtc->irq_periodic = irq_of_parse_and_map(op->dev.of_node, 0);
320d53d4ae9SAlexandre Belloni err = devm_request_irq(&op->dev, rtc->irq_periodic,
321d53d4ae9SAlexandre Belloni mpc5121_rtc_handler_upd, 0, "mpc5121-rtc_upd",
322d53d4ae9SAlexandre Belloni &op->dev);
32350aae724SAnatolij Gustschin if (err) {
32450aae724SAnatolij Gustschin dev_err(&op->dev, "%s: could not request irq: %i\n",
32550aae724SAnatolij Gustschin __func__, rtc->irq_periodic);
32650aae724SAnatolij Gustschin goto out_dispose2;
32750aae724SAnatolij Gustschin }
32850aae724SAnatolij Gustschin
329fffbe10eSAlexandre Belloni rtc->rtc = devm_rtc_allocate_device(&op->dev);
330fffbe10eSAlexandre Belloni if (IS_ERR(rtc->rtc)) {
331fffbe10eSAlexandre Belloni err = PTR_ERR(rtc->rtc);
332fffbe10eSAlexandre Belloni goto out_dispose2;
333fffbe10eSAlexandre Belloni }
334fffbe10eSAlexandre Belloni
335fffbe10eSAlexandre Belloni rtc->rtc->ops = &mpc5200_rtc_ops;
3369597f8ccSAlexandre Belloni set_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->rtc->features);
33714e6fc13SAlexandre Belloni clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->rtc->features);
3383905d1c0SAlexandre Belloni rtc->rtc->range_min = RTC_TIMESTAMP_BEGIN_0000;
3393905d1c0SAlexandre Belloni rtc->rtc->range_max = 65733206399ULL; /* 4052-12-31 23:59:59 */
340fffbe10eSAlexandre Belloni
341955dbea3SDmitry Eremin-Solenikov if (of_device_is_compatible(op->dev.of_node, "fsl,mpc5121-rtc")) {
342955dbea3SDmitry Eremin-Solenikov u32 ka;
34350aae724SAnatolij Gustschin ka = in_be32(&rtc->regs->keep_alive);
34450aae724SAnatolij Gustschin if (ka & 0x02) {
34550aae724SAnatolij Gustschin dev_warn(&op->dev,
34650aae724SAnatolij Gustschin "mpc5121-rtc: Battery or oscillator failure!\n");
34750aae724SAnatolij Gustschin out_be32(&rtc->regs->keep_alive, ka);
34850aae724SAnatolij Gustschin }
349fffbe10eSAlexandre Belloni rtc->rtc->ops = &mpc5121_rtc_ops;
3503905d1c0SAlexandre Belloni /*
3513905d1c0SAlexandre Belloni * This is a limitation of the driver that abuses the target
3523905d1c0SAlexandre Belloni * time register, the actual maximum year for the mpc5121 is
3533905d1c0SAlexandre Belloni * also 4052.
3543905d1c0SAlexandre Belloni */
3553905d1c0SAlexandre Belloni rtc->rtc->range_min = 0;
3563905d1c0SAlexandre Belloni rtc->rtc->range_max = U32_MAX;
357955dbea3SDmitry Eremin-Solenikov }
358955dbea3SDmitry Eremin-Solenikov
359fdcfd854SBartosz Golaszewski err = devm_rtc_register_device(rtc->rtc);
360fffbe10eSAlexandre Belloni if (err)
361d53d4ae9SAlexandre Belloni goto out_dispose2;
36250aae724SAnatolij Gustschin
36350aae724SAnatolij Gustschin return 0;
36450aae724SAnatolij Gustschin
36550aae724SAnatolij Gustschin out_dispose2:
36650aae724SAnatolij Gustschin irq_dispose_mapping(rtc->irq_periodic);
36750aae724SAnatolij Gustschin out_dispose:
36850aae724SAnatolij Gustschin irq_dispose_mapping(rtc->irq);
36950aae724SAnatolij Gustschin
37050aae724SAnatolij Gustschin return err;
37150aae724SAnatolij Gustschin }
37250aae724SAnatolij Gustschin
mpc5121_rtc_remove(struct platform_device * op)37322edbc10SUwe Kleine-König static void mpc5121_rtc_remove(struct platform_device *op)
37450aae724SAnatolij Gustschin {
375ad87a766SJingoo Han struct mpc5121_rtc_data *rtc = platform_get_drvdata(op);
37650aae724SAnatolij Gustschin struct mpc5121_rtc_regs __iomem *regs = rtc->regs;
37750aae724SAnatolij Gustschin
37850aae724SAnatolij Gustschin /* disable interrupt, so there are no nasty surprises */
37950aae724SAnatolij Gustschin out_8(®s->alm_enable, 0);
38050aae724SAnatolij Gustschin out_8(®s->int_enable, in_8(®s->int_enable) & ~0x1);
38150aae724SAnatolij Gustschin
38250aae724SAnatolij Gustschin irq_dispose_mapping(rtc->irq);
38350aae724SAnatolij Gustschin irq_dispose_mapping(rtc->irq_periodic);
38450aae724SAnatolij Gustschin }
38550aae724SAnatolij Gustschin
386c8a6046eSSachin Kamat #ifdef CONFIG_OF
387a28885bcSUwe Kleine-König static const struct of_device_id mpc5121_rtc_match[] = {
38850aae724SAnatolij Gustschin { .compatible = "fsl,mpc5121-rtc", },
389955dbea3SDmitry Eremin-Solenikov { .compatible = "fsl,mpc5200-rtc", },
39050aae724SAnatolij Gustschin {},
39150aae724SAnatolij Gustschin };
39273798d5cSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, mpc5121_rtc_match);
393c8a6046eSSachin Kamat #endif
39450aae724SAnatolij Gustschin
3951c48a5c9SGrant Likely static struct platform_driver mpc5121_rtc_driver = {
396295bdd9cSGrant Likely .driver = {
39750aae724SAnatolij Gustschin .name = "mpc5121-rtc",
398c8a6046eSSachin Kamat .of_match_table = of_match_ptr(mpc5121_rtc_match),
399295bdd9cSGrant Likely },
40050aae724SAnatolij Gustschin .probe = mpc5121_rtc_probe,
40122edbc10SUwe Kleine-König .remove_new = mpc5121_rtc_remove,
40250aae724SAnatolij Gustschin };
40350aae724SAnatolij Gustschin
4040c4eae66SAxel Lin module_platform_driver(mpc5121_rtc_driver);
40550aae724SAnatolij Gustschin
406*86e9b508SJeff Johnson MODULE_DESCRIPTION("Freescale MPC5121 built-in RTC driver");
40750aae724SAnatolij Gustschin MODULE_LICENSE("GPL");
40850aae724SAnatolij Gustschin MODULE_AUTHOR("John Rigby <jcrigby@gmail.com>");
409