1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
294c01ab6SStephen Warren /*
394c01ab6SStephen Warren * RTC driver for Maxim MAX8907
494c01ab6SStephen Warren *
594c01ab6SStephen Warren * Copyright (c) 2011-2012, NVIDIA Corporation.
694c01ab6SStephen Warren *
794c01ab6SStephen Warren * Based on drivers/rtc/rtc-max8925.c,
894c01ab6SStephen Warren * Copyright (C) 2009-2010 Marvell International Ltd.
994c01ab6SStephen Warren */
1094c01ab6SStephen Warren
1194c01ab6SStephen Warren #include <linux/bcd.h>
1294c01ab6SStephen Warren #include <linux/mfd/max8907.h>
1394c01ab6SStephen Warren #include <linux/module.h>
1494c01ab6SStephen Warren #include <linux/platform_device.h>
1594c01ab6SStephen Warren #include <linux/regmap.h>
1694c01ab6SStephen Warren #include <linux/rtc.h>
1794c01ab6SStephen Warren #include <linux/slab.h>
1894c01ab6SStephen Warren
1994c01ab6SStephen Warren enum {
2094c01ab6SStephen Warren RTC_SEC = 0,
2194c01ab6SStephen Warren RTC_MIN,
2294c01ab6SStephen Warren RTC_HOUR,
2394c01ab6SStephen Warren RTC_WEEKDAY,
2494c01ab6SStephen Warren RTC_DATE,
2594c01ab6SStephen Warren RTC_MONTH,
2694c01ab6SStephen Warren RTC_YEAR1,
2794c01ab6SStephen Warren RTC_YEAR2,
2894c01ab6SStephen Warren };
2994c01ab6SStephen Warren
3094c01ab6SStephen Warren #define TIME_NUM 8
3194c01ab6SStephen Warren #define ALARM_1SEC (1 << 7)
3294c01ab6SStephen Warren #define HOUR_12 (1 << 7)
3394c01ab6SStephen Warren #define HOUR_AM_PM (1 << 5)
3494c01ab6SStephen Warren #define ALARM0_IRQ (1 << 3)
3594c01ab6SStephen Warren #define ALARM1_IRQ (1 << 2)
3694c01ab6SStephen Warren #define ALARM0_STATUS (1 << 2)
3794c01ab6SStephen Warren #define ALARM1_STATUS (1 << 1)
3894c01ab6SStephen Warren
3994c01ab6SStephen Warren struct max8907_rtc {
4094c01ab6SStephen Warren struct max8907 *max8907;
4194c01ab6SStephen Warren struct regmap *regmap;
4294c01ab6SStephen Warren struct rtc_device *rtc_dev;
4394c01ab6SStephen Warren int irq;
4494c01ab6SStephen Warren };
4594c01ab6SStephen Warren
max8907_irq_handler(int irq,void * data)4694c01ab6SStephen Warren static irqreturn_t max8907_irq_handler(int irq, void *data)
4794c01ab6SStephen Warren {
4894c01ab6SStephen Warren struct max8907_rtc *rtc = data;
4994c01ab6SStephen Warren
5075ea799dSStephen Warren regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0);
5194c01ab6SStephen Warren
5294c01ab6SStephen Warren rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
5394c01ab6SStephen Warren
5494c01ab6SStephen Warren return IRQ_HANDLED;
5594c01ab6SStephen Warren }
5694c01ab6SStephen Warren
regs_to_tm(u8 * regs,struct rtc_time * tm)5794c01ab6SStephen Warren static void regs_to_tm(u8 *regs, struct rtc_time *tm)
5894c01ab6SStephen Warren {
5994c01ab6SStephen Warren tm->tm_year = bcd2bin(regs[RTC_YEAR2]) * 100 +
6094c01ab6SStephen Warren bcd2bin(regs[RTC_YEAR1]) - 1900;
6194c01ab6SStephen Warren tm->tm_mon = bcd2bin(regs[RTC_MONTH] & 0x1f) - 1;
6294c01ab6SStephen Warren tm->tm_mday = bcd2bin(regs[RTC_DATE] & 0x3f);
6375ea799dSStephen Warren tm->tm_wday = (regs[RTC_WEEKDAY] & 0x07);
6494c01ab6SStephen Warren if (regs[RTC_HOUR] & HOUR_12) {
6594c01ab6SStephen Warren tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x01f);
6694c01ab6SStephen Warren if (tm->tm_hour == 12)
6794c01ab6SStephen Warren tm->tm_hour = 0;
6894c01ab6SStephen Warren if (regs[RTC_HOUR] & HOUR_AM_PM)
6994c01ab6SStephen Warren tm->tm_hour += 12;
7094c01ab6SStephen Warren } else {
7194c01ab6SStephen Warren tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x03f);
7294c01ab6SStephen Warren }
7394c01ab6SStephen Warren tm->tm_min = bcd2bin(regs[RTC_MIN] & 0x7f);
7494c01ab6SStephen Warren tm->tm_sec = bcd2bin(regs[RTC_SEC] & 0x7f);
7594c01ab6SStephen Warren }
7694c01ab6SStephen Warren
tm_to_regs(struct rtc_time * tm,u8 * regs)7794c01ab6SStephen Warren static void tm_to_regs(struct rtc_time *tm, u8 *regs)
7894c01ab6SStephen Warren {
7994c01ab6SStephen Warren u8 high, low;
8094c01ab6SStephen Warren
8194c01ab6SStephen Warren high = (tm->tm_year + 1900) / 100;
8294c01ab6SStephen Warren low = tm->tm_year % 100;
8394c01ab6SStephen Warren regs[RTC_YEAR2] = bin2bcd(high);
8494c01ab6SStephen Warren regs[RTC_YEAR1] = bin2bcd(low);
8594c01ab6SStephen Warren regs[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
8694c01ab6SStephen Warren regs[RTC_DATE] = bin2bcd(tm->tm_mday);
8775ea799dSStephen Warren regs[RTC_WEEKDAY] = tm->tm_wday;
8894c01ab6SStephen Warren regs[RTC_HOUR] = bin2bcd(tm->tm_hour);
8994c01ab6SStephen Warren regs[RTC_MIN] = bin2bcd(tm->tm_min);
9094c01ab6SStephen Warren regs[RTC_SEC] = bin2bcd(tm->tm_sec);
9194c01ab6SStephen Warren }
9294c01ab6SStephen Warren
max8907_rtc_read_time(struct device * dev,struct rtc_time * tm)9394c01ab6SStephen Warren static int max8907_rtc_read_time(struct device *dev, struct rtc_time *tm)
9494c01ab6SStephen Warren {
9594c01ab6SStephen Warren struct max8907_rtc *rtc = dev_get_drvdata(dev);
9694c01ab6SStephen Warren u8 regs[TIME_NUM];
9794c01ab6SStephen Warren int ret;
9894c01ab6SStephen Warren
9994c01ab6SStephen Warren ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_RTC_SEC, regs,
10094c01ab6SStephen Warren TIME_NUM);
10194c01ab6SStephen Warren if (ret < 0)
10294c01ab6SStephen Warren return ret;
10394c01ab6SStephen Warren
10494c01ab6SStephen Warren regs_to_tm(regs, tm);
10594c01ab6SStephen Warren
10694c01ab6SStephen Warren return 0;
10794c01ab6SStephen Warren }
10894c01ab6SStephen Warren
max8907_rtc_set_time(struct device * dev,struct rtc_time * tm)10994c01ab6SStephen Warren static int max8907_rtc_set_time(struct device *dev, struct rtc_time *tm)
11094c01ab6SStephen Warren {
11194c01ab6SStephen Warren struct max8907_rtc *rtc = dev_get_drvdata(dev);
11294c01ab6SStephen Warren u8 regs[TIME_NUM];
11394c01ab6SStephen Warren
11494c01ab6SStephen Warren tm_to_regs(tm, regs);
11594c01ab6SStephen Warren
11694c01ab6SStephen Warren return regmap_bulk_write(rtc->regmap, MAX8907_REG_RTC_SEC, regs,
11794c01ab6SStephen Warren TIME_NUM);
11894c01ab6SStephen Warren }
11994c01ab6SStephen Warren
max8907_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)12094c01ab6SStephen Warren static int max8907_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
12194c01ab6SStephen Warren {
12294c01ab6SStephen Warren struct max8907_rtc *rtc = dev_get_drvdata(dev);
12394c01ab6SStephen Warren u8 regs[TIME_NUM];
12494c01ab6SStephen Warren unsigned int val;
12594c01ab6SStephen Warren int ret;
12694c01ab6SStephen Warren
12794c01ab6SStephen Warren ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs,
12894c01ab6SStephen Warren TIME_NUM);
12994c01ab6SStephen Warren if (ret < 0)
13094c01ab6SStephen Warren return ret;
13194c01ab6SStephen Warren
13294c01ab6SStephen Warren regs_to_tm(regs, &alrm->time);
13394c01ab6SStephen Warren
13494c01ab6SStephen Warren ret = regmap_read(rtc->regmap, MAX8907_REG_ALARM0_CNTL, &val);
13594c01ab6SStephen Warren if (ret < 0)
13694c01ab6SStephen Warren return ret;
13794c01ab6SStephen Warren
13894c01ab6SStephen Warren alrm->enabled = !!(val & 0x7f);
13994c01ab6SStephen Warren
14094c01ab6SStephen Warren return 0;
14194c01ab6SStephen Warren }
14294c01ab6SStephen Warren
max8907_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)14394c01ab6SStephen Warren static int max8907_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
14494c01ab6SStephen Warren {
14594c01ab6SStephen Warren struct max8907_rtc *rtc = dev_get_drvdata(dev);
14694c01ab6SStephen Warren u8 regs[TIME_NUM];
14794c01ab6SStephen Warren int ret;
14894c01ab6SStephen Warren
14994c01ab6SStephen Warren tm_to_regs(&alrm->time, regs);
15094c01ab6SStephen Warren
15194c01ab6SStephen Warren /* Disable alarm while we update the target time */
15275ea799dSStephen Warren ret = regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0);
15394c01ab6SStephen Warren if (ret < 0)
15494c01ab6SStephen Warren return ret;
15594c01ab6SStephen Warren
15694c01ab6SStephen Warren ret = regmap_bulk_write(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs,
15794c01ab6SStephen Warren TIME_NUM);
15894c01ab6SStephen Warren if (ret < 0)
15994c01ab6SStephen Warren return ret;
16094c01ab6SStephen Warren
16194c01ab6SStephen Warren if (alrm->enabled)
16275ea799dSStephen Warren ret = regmap_write(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x77);
16394c01ab6SStephen Warren
16494c01ab6SStephen Warren return ret;
16594c01ab6SStephen Warren }
16694c01ab6SStephen Warren
16794c01ab6SStephen Warren static const struct rtc_class_ops max8907_rtc_ops = {
16894c01ab6SStephen Warren .read_time = max8907_rtc_read_time,
16994c01ab6SStephen Warren .set_time = max8907_rtc_set_time,
17094c01ab6SStephen Warren .read_alarm = max8907_rtc_read_alarm,
17194c01ab6SStephen Warren .set_alarm = max8907_rtc_set_alarm,
17294c01ab6SStephen Warren };
17394c01ab6SStephen Warren
max8907_rtc_probe(struct platform_device * pdev)1745a167f45SGreg Kroah-Hartman static int max8907_rtc_probe(struct platform_device *pdev)
17594c01ab6SStephen Warren {
17694c01ab6SStephen Warren struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent);
17794c01ab6SStephen Warren struct max8907_rtc *rtc;
17894c01ab6SStephen Warren int ret;
17994c01ab6SStephen Warren
18094c01ab6SStephen Warren rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
18194c01ab6SStephen Warren if (!rtc)
18294c01ab6SStephen Warren return -ENOMEM;
18394c01ab6SStephen Warren platform_set_drvdata(pdev, rtc);
18494c01ab6SStephen Warren
18594c01ab6SStephen Warren rtc->max8907 = max8907;
18694c01ab6SStephen Warren rtc->regmap = max8907->regmap_rtc;
18794c01ab6SStephen Warren
188ac4e7d49SJingoo Han rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8907-rtc",
18994c01ab6SStephen Warren &max8907_rtc_ops, THIS_MODULE);
19094c01ab6SStephen Warren if (IS_ERR(rtc->rtc_dev)) {
19194c01ab6SStephen Warren ret = PTR_ERR(rtc->rtc_dev);
19294c01ab6SStephen Warren dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
19394c01ab6SStephen Warren return ret;
19494c01ab6SStephen Warren }
19594c01ab6SStephen Warren
19694c01ab6SStephen Warren rtc->irq = regmap_irq_get_virq(max8907->irqc_rtc,
19794c01ab6SStephen Warren MAX8907_IRQ_RTC_ALARM0);
1982fa76062SSachin Kamat if (rtc->irq < 0)
1992fa76062SSachin Kamat return rtc->irq;
20094c01ab6SStephen Warren
20183a72c87SJingoo Han ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
20283a72c87SJingoo Han max8907_irq_handler,
20394c01ab6SStephen Warren IRQF_ONESHOT, "max8907-alarm0", rtc);
2042fa76062SSachin Kamat if (ret < 0)
20594c01ab6SStephen Warren dev_err(&pdev->dev, "Failed to request IRQ%d: %d\n",
20694c01ab6SStephen Warren rtc->irq, ret);
20794c01ab6SStephen Warren
20894c01ab6SStephen Warren return ret;
20994c01ab6SStephen Warren }
21094c01ab6SStephen Warren
21194c01ab6SStephen Warren static struct platform_driver max8907_rtc_driver = {
21294c01ab6SStephen Warren .driver = {
21394c01ab6SStephen Warren .name = "max8907-rtc",
21494c01ab6SStephen Warren },
21594c01ab6SStephen Warren .probe = max8907_rtc_probe,
21694c01ab6SStephen Warren };
21794c01ab6SStephen Warren module_platform_driver(max8907_rtc_driver);
21894c01ab6SStephen Warren
21994c01ab6SStephen Warren MODULE_DESCRIPTION("Maxim MAX8907 RTC driver");
22094c01ab6SStephen Warren MODULE_LICENSE("GPL v2");
221