1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a39069f6SHaojian Zhuang /*
3a39069f6SHaojian Zhuang * RTC driver for Maxim MAX8925
4a39069f6SHaojian Zhuang *
5a39069f6SHaojian Zhuang * Copyright (C) 2009-2010 Marvell International Ltd.
6a39069f6SHaojian Zhuang * Haojian Zhuang <haojian.zhuang@marvell.com>
7a39069f6SHaojian Zhuang */
8a39069f6SHaojian Zhuang
9a39069f6SHaojian Zhuang #include <linux/module.h>
10a39069f6SHaojian Zhuang #include <linux/i2c.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12a39069f6SHaojian Zhuang #include <linux/rtc.h>
13a39069f6SHaojian Zhuang #include <linux/platform_device.h>
14a39069f6SHaojian Zhuang #include <linux/mfd/max8925.h>
15a39069f6SHaojian Zhuang
16a39069f6SHaojian Zhuang enum {
17a39069f6SHaojian Zhuang RTC_SEC = 0,
18a39069f6SHaojian Zhuang RTC_MIN,
19a39069f6SHaojian Zhuang RTC_HOUR,
20a39069f6SHaojian Zhuang RTC_WEEKDAY,
21a39069f6SHaojian Zhuang RTC_DATE,
22a39069f6SHaojian Zhuang RTC_MONTH,
23a39069f6SHaojian Zhuang RTC_YEAR1,
24a39069f6SHaojian Zhuang RTC_YEAR2,
25a39069f6SHaojian Zhuang };
26a39069f6SHaojian Zhuang
27a39069f6SHaojian Zhuang #define MAX8925_RTC_SEC 0x00
28a39069f6SHaojian Zhuang #define MAX8925_RTC_MIN 0x01
29a39069f6SHaojian Zhuang #define MAX8925_RTC_HOUR 0x02
30a39069f6SHaojian Zhuang #define MAX8925_RTC_WEEKDAY 0x03
31a39069f6SHaojian Zhuang #define MAX8925_RTC_DATE 0x04
32a39069f6SHaojian Zhuang #define MAX8925_RTC_MONTH 0x05
33a39069f6SHaojian Zhuang #define MAX8925_RTC_YEAR1 0x06
34a39069f6SHaojian Zhuang #define MAX8925_RTC_YEAR2 0x07
35a39069f6SHaojian Zhuang #define MAX8925_ALARM0_SEC 0x08
36a39069f6SHaojian Zhuang #define MAX8925_ALARM0_MIN 0x09
37a39069f6SHaojian Zhuang #define MAX8925_ALARM0_HOUR 0x0a
38a39069f6SHaojian Zhuang #define MAX8925_ALARM0_WEEKDAY 0x0b
39a39069f6SHaojian Zhuang #define MAX8925_ALARM0_DATE 0x0c
40a39069f6SHaojian Zhuang #define MAX8925_ALARM0_MON 0x0d
41a39069f6SHaojian Zhuang #define MAX8925_ALARM0_YEAR1 0x0e
42a39069f6SHaojian Zhuang #define MAX8925_ALARM0_YEAR2 0x0f
43a39069f6SHaojian Zhuang #define MAX8925_ALARM1_SEC 0x10
44a39069f6SHaojian Zhuang #define MAX8925_ALARM1_MIN 0x11
45a39069f6SHaojian Zhuang #define MAX8925_ALARM1_HOUR 0x12
46a39069f6SHaojian Zhuang #define MAX8925_ALARM1_WEEKDAY 0x13
47a39069f6SHaojian Zhuang #define MAX8925_ALARM1_DATE 0x14
48a39069f6SHaojian Zhuang #define MAX8925_ALARM1_MON 0x15
49a39069f6SHaojian Zhuang #define MAX8925_ALARM1_YEAR1 0x16
50a39069f6SHaojian Zhuang #define MAX8925_ALARM1_YEAR2 0x17
51a39069f6SHaojian Zhuang #define MAX8925_RTC_CNTL 0x1b
52a39069f6SHaojian Zhuang #define MAX8925_RTC_STATUS 0x20
53a39069f6SHaojian Zhuang
54a39069f6SHaojian Zhuang #define TIME_NUM 8
55a39069f6SHaojian Zhuang #define ALARM_1SEC (1 << 7)
56a39069f6SHaojian Zhuang #define HOUR_12 (1 << 7)
57a39069f6SHaojian Zhuang #define HOUR_AM_PM (1 << 5)
58a39069f6SHaojian Zhuang #define ALARM0_IRQ (1 << 3)
59a39069f6SHaojian Zhuang #define ALARM1_IRQ (1 << 2)
60a39069f6SHaojian Zhuang #define ALARM0_STATUS (1 << 2)
61a39069f6SHaojian Zhuang #define ALARM1_STATUS (1 << 1)
62a39069f6SHaojian Zhuang
63a39069f6SHaojian Zhuang
64a39069f6SHaojian Zhuang struct max8925_rtc_info {
65a39069f6SHaojian Zhuang struct rtc_device *rtc_dev;
66a39069f6SHaojian Zhuang struct max8925_chip *chip;
67a39069f6SHaojian Zhuang struct i2c_client *rtc;
68a39069f6SHaojian Zhuang struct device *dev;
69c1a2f31dSHaojian Zhuang int irq;
70a39069f6SHaojian Zhuang };
71a39069f6SHaojian Zhuang
rtc_update_handler(int irq,void * data)72a39069f6SHaojian Zhuang static irqreturn_t rtc_update_handler(int irq, void *data)
73a39069f6SHaojian Zhuang {
74a39069f6SHaojian Zhuang struct max8925_rtc_info *info = (struct max8925_rtc_info *)data;
75a39069f6SHaojian Zhuang
76a39069f6SHaojian Zhuang /* disable ALARM0 except for 1SEC alarm */
77a39069f6SHaojian Zhuang max8925_set_bits(info->rtc, MAX8925_ALARM0_CNTL, 0x7f, 0);
78a39069f6SHaojian Zhuang rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
79a39069f6SHaojian Zhuang return IRQ_HANDLED;
80a39069f6SHaojian Zhuang }
81a39069f6SHaojian Zhuang
tm_calc(struct rtc_time * tm,unsigned char * buf,int len)82a39069f6SHaojian Zhuang static int tm_calc(struct rtc_time *tm, unsigned char *buf, int len)
83a39069f6SHaojian Zhuang {
84a39069f6SHaojian Zhuang if (len < TIME_NUM)
85a39069f6SHaojian Zhuang return -EINVAL;
86a39069f6SHaojian Zhuang tm->tm_year = (buf[RTC_YEAR2] >> 4) * 1000
87a39069f6SHaojian Zhuang + (buf[RTC_YEAR2] & 0xf) * 100
88a39069f6SHaojian Zhuang + (buf[RTC_YEAR1] >> 4) * 10
89a39069f6SHaojian Zhuang + (buf[RTC_YEAR1] & 0xf);
90a39069f6SHaojian Zhuang tm->tm_year -= 1900;
91a39069f6SHaojian Zhuang tm->tm_mon = ((buf[RTC_MONTH] >> 4) & 0x01) * 10
92a39069f6SHaojian Zhuang + (buf[RTC_MONTH] & 0x0f);
93a39069f6SHaojian Zhuang tm->tm_mday = ((buf[RTC_DATE] >> 4) & 0x03) * 10
94a39069f6SHaojian Zhuang + (buf[RTC_DATE] & 0x0f);
95a39069f6SHaojian Zhuang tm->tm_wday = buf[RTC_WEEKDAY] & 0x07;
96a39069f6SHaojian Zhuang if (buf[RTC_HOUR] & HOUR_12) {
97a39069f6SHaojian Zhuang tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x1) * 10
98a39069f6SHaojian Zhuang + (buf[RTC_HOUR] & 0x0f);
99a39069f6SHaojian Zhuang if (buf[RTC_HOUR] & HOUR_AM_PM)
100a39069f6SHaojian Zhuang tm->tm_hour += 12;
101a39069f6SHaojian Zhuang } else
102a39069f6SHaojian Zhuang tm->tm_hour = ((buf[RTC_HOUR] >> 4) & 0x03) * 10
103a39069f6SHaojian Zhuang + (buf[RTC_HOUR] & 0x0f);
104a39069f6SHaojian Zhuang tm->tm_min = ((buf[RTC_MIN] >> 4) & 0x7) * 10
105a39069f6SHaojian Zhuang + (buf[RTC_MIN] & 0x0f);
106a39069f6SHaojian Zhuang tm->tm_sec = ((buf[RTC_SEC] >> 4) & 0x7) * 10
107a39069f6SHaojian Zhuang + (buf[RTC_SEC] & 0x0f);
108a39069f6SHaojian Zhuang return 0;
109a39069f6SHaojian Zhuang }
110a39069f6SHaojian Zhuang
data_calc(unsigned char * buf,struct rtc_time * tm,int len)111a39069f6SHaojian Zhuang static int data_calc(unsigned char *buf, struct rtc_time *tm, int len)
112a39069f6SHaojian Zhuang {
113a39069f6SHaojian Zhuang unsigned char high, low;
114a39069f6SHaojian Zhuang
115a39069f6SHaojian Zhuang if (len < TIME_NUM)
116a39069f6SHaojian Zhuang return -EINVAL;
117a39069f6SHaojian Zhuang
118a39069f6SHaojian Zhuang high = (tm->tm_year + 1900) / 1000;
119a39069f6SHaojian Zhuang low = (tm->tm_year + 1900) / 100;
120a39069f6SHaojian Zhuang low = low - high * 10;
121a39069f6SHaojian Zhuang buf[RTC_YEAR2] = (high << 4) + low;
122a39069f6SHaojian Zhuang high = (tm->tm_year + 1900) / 10;
123a39069f6SHaojian Zhuang low = tm->tm_year + 1900;
124a39069f6SHaojian Zhuang low = low - high * 10;
125a39069f6SHaojian Zhuang high = high - (high / 10) * 10;
126a39069f6SHaojian Zhuang buf[RTC_YEAR1] = (high << 4) + low;
127a39069f6SHaojian Zhuang high = tm->tm_mon / 10;
128a39069f6SHaojian Zhuang low = tm->tm_mon;
129a39069f6SHaojian Zhuang low = low - high * 10;
130a39069f6SHaojian Zhuang buf[RTC_MONTH] = (high << 4) + low;
131a39069f6SHaojian Zhuang high = tm->tm_mday / 10;
132a39069f6SHaojian Zhuang low = tm->tm_mday;
133a39069f6SHaojian Zhuang low = low - high * 10;
134a39069f6SHaojian Zhuang buf[RTC_DATE] = (high << 4) + low;
135a39069f6SHaojian Zhuang buf[RTC_WEEKDAY] = tm->tm_wday;
136a39069f6SHaojian Zhuang high = tm->tm_hour / 10;
137a39069f6SHaojian Zhuang low = tm->tm_hour;
138a39069f6SHaojian Zhuang low = low - high * 10;
139a39069f6SHaojian Zhuang buf[RTC_HOUR] = (high << 4) + low;
140a39069f6SHaojian Zhuang high = tm->tm_min / 10;
141a39069f6SHaojian Zhuang low = tm->tm_min;
142a39069f6SHaojian Zhuang low = low - high * 10;
143a39069f6SHaojian Zhuang buf[RTC_MIN] = (high << 4) + low;
144a39069f6SHaojian Zhuang high = tm->tm_sec / 10;
145a39069f6SHaojian Zhuang low = tm->tm_sec;
146a39069f6SHaojian Zhuang low = low - high * 10;
147a39069f6SHaojian Zhuang buf[RTC_SEC] = (high << 4) + low;
148a39069f6SHaojian Zhuang return 0;
149a39069f6SHaojian Zhuang }
150a39069f6SHaojian Zhuang
max8925_rtc_read_time(struct device * dev,struct rtc_time * tm)151a39069f6SHaojian Zhuang static int max8925_rtc_read_time(struct device *dev, struct rtc_time *tm)
152a39069f6SHaojian Zhuang {
153a39069f6SHaojian Zhuang struct max8925_rtc_info *info = dev_get_drvdata(dev);
154a39069f6SHaojian Zhuang unsigned char buf[TIME_NUM];
155a39069f6SHaojian Zhuang int ret;
156a39069f6SHaojian Zhuang
157a39069f6SHaojian Zhuang ret = max8925_bulk_read(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
158a39069f6SHaojian Zhuang if (ret < 0)
159a39069f6SHaojian Zhuang goto out;
160a39069f6SHaojian Zhuang ret = tm_calc(tm, buf, TIME_NUM);
161a39069f6SHaojian Zhuang out:
162a39069f6SHaojian Zhuang return ret;
163a39069f6SHaojian Zhuang }
164a39069f6SHaojian Zhuang
max8925_rtc_set_time(struct device * dev,struct rtc_time * tm)165a39069f6SHaojian Zhuang static int max8925_rtc_set_time(struct device *dev, struct rtc_time *tm)
166a39069f6SHaojian Zhuang {
167a39069f6SHaojian Zhuang struct max8925_rtc_info *info = dev_get_drvdata(dev);
168a39069f6SHaojian Zhuang unsigned char buf[TIME_NUM];
169a39069f6SHaojian Zhuang int ret;
170a39069f6SHaojian Zhuang
171a39069f6SHaojian Zhuang ret = data_calc(buf, tm, TIME_NUM);
172a39069f6SHaojian Zhuang if (ret < 0)
173a39069f6SHaojian Zhuang goto out;
174a39069f6SHaojian Zhuang ret = max8925_bulk_write(info->rtc, MAX8925_RTC_SEC, TIME_NUM, buf);
175a39069f6SHaojian Zhuang out:
176a39069f6SHaojian Zhuang return ret;
177a39069f6SHaojian Zhuang }
178a39069f6SHaojian Zhuang
max8925_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)179a39069f6SHaojian Zhuang static int max8925_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
180a39069f6SHaojian Zhuang {
181a39069f6SHaojian Zhuang struct max8925_rtc_info *info = dev_get_drvdata(dev);
182a39069f6SHaojian Zhuang unsigned char buf[TIME_NUM];
183a39069f6SHaojian Zhuang int ret;
184a39069f6SHaojian Zhuang
185a39069f6SHaojian Zhuang ret = max8925_bulk_read(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
186a39069f6SHaojian Zhuang if (ret < 0)
187a39069f6SHaojian Zhuang goto out;
188a39069f6SHaojian Zhuang ret = tm_calc(&alrm->time, buf, TIME_NUM);
189a39069f6SHaojian Zhuang if (ret < 0)
190a39069f6SHaojian Zhuang goto out;
191a39069f6SHaojian Zhuang ret = max8925_reg_read(info->rtc, MAX8925_RTC_IRQ_MASK);
192a39069f6SHaojian Zhuang if (ret < 0)
193a39069f6SHaojian Zhuang goto out;
194fad0738dSKevin Liu if (ret & ALARM0_IRQ) {
195a39069f6SHaojian Zhuang alrm->enabled = 0;
196fad0738dSKevin Liu } else {
197fad0738dSKevin Liu ret = max8925_reg_read(info->rtc, MAX8925_ALARM0_CNTL);
198fad0738dSKevin Liu if (ret < 0)
199fad0738dSKevin Liu goto out;
200fad0738dSKevin Liu if (!ret)
201fad0738dSKevin Liu alrm->enabled = 0;
202fad0738dSKevin Liu else
203fad0738dSKevin Liu alrm->enabled = 1;
204fad0738dSKevin Liu }
205a39069f6SHaojian Zhuang ret = max8925_reg_read(info->rtc, MAX8925_RTC_STATUS);
206a39069f6SHaojian Zhuang if (ret < 0)
207a39069f6SHaojian Zhuang goto out;
208a39069f6SHaojian Zhuang if (ret & ALARM0_STATUS)
209a39069f6SHaojian Zhuang alrm->pending = 1;
210a39069f6SHaojian Zhuang else
211a39069f6SHaojian Zhuang alrm->pending = 0;
2120cf30bddSKevin Liu return 0;
213a39069f6SHaojian Zhuang out:
214a39069f6SHaojian Zhuang return ret;
215a39069f6SHaojian Zhuang }
216a39069f6SHaojian Zhuang
max8925_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)217a39069f6SHaojian Zhuang static int max8925_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
218a39069f6SHaojian Zhuang {
219a39069f6SHaojian Zhuang struct max8925_rtc_info *info = dev_get_drvdata(dev);
220a39069f6SHaojian Zhuang unsigned char buf[TIME_NUM];
221a39069f6SHaojian Zhuang int ret;
222a39069f6SHaojian Zhuang
223a39069f6SHaojian Zhuang ret = data_calc(buf, &alrm->time, TIME_NUM);
224a39069f6SHaojian Zhuang if (ret < 0)
225a39069f6SHaojian Zhuang goto out;
226a39069f6SHaojian Zhuang ret = max8925_bulk_write(info->rtc, MAX8925_ALARM0_SEC, TIME_NUM, buf);
227a39069f6SHaojian Zhuang if (ret < 0)
228a39069f6SHaojian Zhuang goto out;
229fad0738dSKevin Liu if (alrm->enabled)
230a39069f6SHaojian Zhuang /* only enable alarm on year/month/day/hour/min/sec */
231a39069f6SHaojian Zhuang ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x77);
232fad0738dSKevin Liu else
233fad0738dSKevin Liu ret = max8925_reg_write(info->rtc, MAX8925_ALARM0_CNTL, 0x0);
234a39069f6SHaojian Zhuang out:
235a39069f6SHaojian Zhuang return ret;
236a39069f6SHaojian Zhuang }
237a39069f6SHaojian Zhuang
238a39069f6SHaojian Zhuang static const struct rtc_class_ops max8925_rtc_ops = {
239a39069f6SHaojian Zhuang .read_time = max8925_rtc_read_time,
240a39069f6SHaojian Zhuang .set_time = max8925_rtc_set_time,
241a39069f6SHaojian Zhuang .read_alarm = max8925_rtc_read_alarm,
242a39069f6SHaojian Zhuang .set_alarm = max8925_rtc_set_alarm,
243a39069f6SHaojian Zhuang };
244a39069f6SHaojian Zhuang
max8925_rtc_probe(struct platform_device * pdev)2455a167f45SGreg Kroah-Hartman static int max8925_rtc_probe(struct platform_device *pdev)
246a39069f6SHaojian Zhuang {
247a39069f6SHaojian Zhuang struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
248a39069f6SHaojian Zhuang struct max8925_rtc_info *info;
249c1a2f31dSHaojian Zhuang int ret;
250a39069f6SHaojian Zhuang
251e035be6aSSachin Kamat info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_rtc_info),
252e035be6aSSachin Kamat GFP_KERNEL);
253a39069f6SHaojian Zhuang if (!info)
254a39069f6SHaojian Zhuang return -ENOMEM;
255a39069f6SHaojian Zhuang info->chip = chip;
256a39069f6SHaojian Zhuang info->rtc = chip->rtc;
257a39069f6SHaojian Zhuang info->dev = &pdev->dev;
258c1a2f31dSHaojian Zhuang info->irq = platform_get_irq(pdev, 0);
259a39069f6SHaojian Zhuang
260e035be6aSSachin Kamat ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
261e035be6aSSachin Kamat rtc_update_handler, IRQF_ONESHOT,
262e035be6aSSachin Kamat "rtc-alarm0", info);
263a39069f6SHaojian Zhuang if (ret < 0) {
264a39069f6SHaojian Zhuang dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
265c1a2f31dSHaojian Zhuang info->irq, ret);
26628e1de09SJingoo Han return ret;
267a39069f6SHaojian Zhuang }
268a39069f6SHaojian Zhuang
269f945a3d9SZhangfei Gao dev_set_drvdata(&pdev->dev, info);
2702f5c4fe8SJohn Stultz /* XXX - isn't this redundant? */
2712f5c4fe8SJohn Stultz platform_set_drvdata(pdev, info);
272f945a3d9SZhangfei Gao
27374d836c4SKevin Liu device_init_wakeup(&pdev->dev, 1);
27474d836c4SKevin Liu
275e035be6aSSachin Kamat info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8925-rtc",
276a39069f6SHaojian Zhuang &max8925_rtc_ops, THIS_MODULE);
277a39069f6SHaojian Zhuang ret = PTR_ERR(info->rtc_dev);
278a39069f6SHaojian Zhuang if (IS_ERR(info->rtc_dev)) {
279a39069f6SHaojian Zhuang dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
28028e1de09SJingoo Han return ret;
281a39069f6SHaojian Zhuang }
282a39069f6SHaojian Zhuang
283a39069f6SHaojian Zhuang return 0;
284a39069f6SHaojian Zhuang }
285a39069f6SHaojian Zhuang
28674d836c4SKevin Liu #ifdef CONFIG_PM_SLEEP
max8925_rtc_suspend(struct device * dev)28774d836c4SKevin Liu static int max8925_rtc_suspend(struct device *dev)
28874d836c4SKevin Liu {
28974d836c4SKevin Liu struct platform_device *pdev = to_platform_device(dev);
29074d836c4SKevin Liu struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
29174d836c4SKevin Liu
29274d836c4SKevin Liu if (device_may_wakeup(dev))
29374d836c4SKevin Liu chip->wakeup_flag |= 1 << MAX8925_IRQ_RTC_ALARM0;
29474d836c4SKevin Liu return 0;
29574d836c4SKevin Liu }
max8925_rtc_resume(struct device * dev)29674d836c4SKevin Liu static int max8925_rtc_resume(struct device *dev)
29774d836c4SKevin Liu {
29874d836c4SKevin Liu struct platform_device *pdev = to_platform_device(dev);
29974d836c4SKevin Liu struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
30074d836c4SKevin Liu
30174d836c4SKevin Liu if (device_may_wakeup(dev))
30274d836c4SKevin Liu chip->wakeup_flag &= ~(1 << MAX8925_IRQ_RTC_ALARM0);
30374d836c4SKevin Liu return 0;
30474d836c4SKevin Liu }
30574d836c4SKevin Liu #endif
30674d836c4SKevin Liu
30774d836c4SKevin Liu static SIMPLE_DEV_PM_OPS(max8925_rtc_pm_ops, max8925_rtc_suspend, max8925_rtc_resume);
30874d836c4SKevin Liu
309a39069f6SHaojian Zhuang static struct platform_driver max8925_rtc_driver = {
310a39069f6SHaojian Zhuang .driver = {
311a39069f6SHaojian Zhuang .name = "max8925-rtc",
31274d836c4SKevin Liu .pm = &max8925_rtc_pm_ops,
313a39069f6SHaojian Zhuang },
314a39069f6SHaojian Zhuang .probe = max8925_rtc_probe,
315a39069f6SHaojian Zhuang };
316a39069f6SHaojian Zhuang
3170c4eae66SAxel Lin module_platform_driver(max8925_rtc_driver);
318a39069f6SHaojian Zhuang
319a39069f6SHaojian Zhuang MODULE_DESCRIPTION("Maxim MAX8925 RTC driver");
320a39069f6SHaojian Zhuang MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
321a39069f6SHaojian Zhuang MODULE_LICENSE("GPL");
322a39069f6SHaojian Zhuang
323