xref: /linux/drivers/rtc/rtc-max8998.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // RTC driver for Maxim MAX8998
4 //
5 // Copyright (C) 2010 Samsung Electronics Co.Ltd
6 // Author: Minkyu Kang <mk7.kang@samsung.com>
7 // Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/bcd.h>
13 #include <linux/irqdomain.h>
14 #include <linux/rtc.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/max8998.h>
17 #include <linux/mfd/max8998-private.h>
18 #include <linux/delay.h>
19 
20 #define MAX8998_RTC_SEC			0x00
21 #define MAX8998_RTC_MIN			0x01
22 #define MAX8998_RTC_HOUR		0x02
23 #define MAX8998_RTC_WEEKDAY		0x03
24 #define MAX8998_RTC_DATE		0x04
25 #define MAX8998_RTC_MONTH		0x05
26 #define MAX8998_RTC_YEAR1		0x06
27 #define MAX8998_RTC_YEAR2		0x07
28 #define MAX8998_ALARM0_SEC		0x08
29 #define MAX8998_ALARM0_MIN		0x09
30 #define MAX8998_ALARM0_HOUR		0x0a
31 #define MAX8998_ALARM0_WEEKDAY		0x0b
32 #define MAX8998_ALARM0_DATE		0x0c
33 #define MAX8998_ALARM0_MONTH		0x0d
34 #define MAX8998_ALARM0_YEAR1		0x0e
35 #define MAX8998_ALARM0_YEAR2		0x0f
36 #define MAX8998_ALARM1_SEC		0x10
37 #define MAX8998_ALARM1_MIN		0x11
38 #define MAX8998_ALARM1_HOUR		0x12
39 #define MAX8998_ALARM1_WEEKDAY		0x13
40 #define MAX8998_ALARM1_DATE		0x14
41 #define MAX8998_ALARM1_MONTH		0x15
42 #define MAX8998_ALARM1_YEAR1		0x16
43 #define MAX8998_ALARM1_YEAR2		0x17
44 #define MAX8998_ALARM0_CONF		0x18
45 #define MAX8998_ALARM1_CONF		0x19
46 #define MAX8998_RTC_STATUS		0x1a
47 #define MAX8998_WTSR_SMPL_CNTL		0x1b
48 #define MAX8998_TEST			0x1f
49 
50 #define HOUR_12				(1 << 7)
51 #define HOUR_PM				(1 << 5)
52 #define ALARM0_STATUS			(1 << 1)
53 #define ALARM1_STATUS			(1 << 2)
54 
55 enum {
56 	RTC_SEC = 0,
57 	RTC_MIN,
58 	RTC_HOUR,
59 	RTC_WEEKDAY,
60 	RTC_DATE,
61 	RTC_MONTH,
62 	RTC_YEAR1,
63 	RTC_YEAR2,
64 };
65 
66 struct max8998_rtc_info {
67 	struct device		*dev;
68 	struct max8998_dev	*max8998;
69 	struct i2c_client	*rtc;
70 	struct rtc_device	*rtc_dev;
71 	int irq;
72 	bool lp3974_bug_workaround;
73 };
74 
75 static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
76 {
77 	tm->tm_sec = bcd2bin(data[RTC_SEC]);
78 	tm->tm_min = bcd2bin(data[RTC_MIN]);
79 	if (data[RTC_HOUR] & HOUR_12) {
80 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
81 		if (data[RTC_HOUR] & HOUR_PM)
82 			tm->tm_hour += 12;
83 	} else
84 		tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
85 
86 	tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
87 	tm->tm_mday = bcd2bin(data[RTC_DATE]);
88 	tm->tm_mon = bcd2bin(data[RTC_MONTH]);
89 	tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
90 	tm->tm_year -= 1900;
91 }
92 
93 static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
94 {
95 	data[RTC_SEC] = bin2bcd(tm->tm_sec);
96 	data[RTC_MIN] = bin2bcd(tm->tm_min);
97 	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
98 	data[RTC_WEEKDAY] = tm->tm_wday;
99 	data[RTC_DATE] = bin2bcd(tm->tm_mday);
100 	data[RTC_MONTH] = bin2bcd(tm->tm_mon);
101 	data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
102 	data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
103 }
104 
105 static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
106 {
107 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
108 	u8 data[8];
109 	int ret;
110 
111 	ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
112 	if (ret < 0)
113 		return ret;
114 
115 	max8998_data_to_tm(data, tm);
116 
117 	return 0;
118 }
119 
120 static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
121 {
122 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
123 	u8 data[8];
124 	int ret;
125 
126 	max8998_tm_to_data(tm, data);
127 
128 	ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
129 
130 	if (info->lp3974_bug_workaround)
131 		msleep(2000);
132 
133 	return ret;
134 }
135 
136 static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
137 {
138 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
139 	u8 data[8];
140 	u8 val;
141 	int ret;
142 
143 	ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
144 	if (ret < 0)
145 		return ret;
146 
147 	max8998_data_to_tm(data, &alrm->time);
148 
149 	ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
150 	if (ret < 0)
151 		return ret;
152 
153 	alrm->enabled = !!val;
154 
155 	ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
156 	if (ret < 0)
157 		return ret;
158 
159 	if (val & ALARM0_STATUS)
160 		alrm->pending = 1;
161 	else
162 		alrm->pending = 0;
163 
164 	return 0;
165 }
166 
167 static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
168 {
169 	int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
170 
171 	if (info->lp3974_bug_workaround)
172 		msleep(2000);
173 
174 	return ret;
175 }
176 
177 static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
178 {
179 	int ret;
180 	u8 alarm0_conf = 0x77;
181 
182 	/* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
183 	if (info->lp3974_bug_workaround)
184 		alarm0_conf = 0x57;
185 
186 	ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
187 
188 	if (info->lp3974_bug_workaround)
189 		msleep(2000);
190 
191 	return ret;
192 }
193 
194 static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195 {
196 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
197 	u8 data[8];
198 	int ret;
199 
200 	max8998_tm_to_data(&alrm->time, data);
201 
202 	ret = max8998_rtc_stop_alarm(info);
203 	if (ret < 0)
204 		return ret;
205 
206 	ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
207 	if (ret < 0)
208 		return ret;
209 
210 	if (info->lp3974_bug_workaround)
211 		msleep(2000);
212 
213 	if (alrm->enabled)
214 		ret = max8998_rtc_start_alarm(info);
215 
216 	return ret;
217 }
218 
219 static int max8998_rtc_alarm_irq_enable(struct device *dev,
220 					unsigned int enabled)
221 {
222 	struct max8998_rtc_info *info = dev_get_drvdata(dev);
223 
224 	if (enabled)
225 		return max8998_rtc_start_alarm(info);
226 	else
227 		return max8998_rtc_stop_alarm(info);
228 }
229 
230 static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
231 {
232 	struct max8998_rtc_info *info = data;
233 
234 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
235 
236 	return IRQ_HANDLED;
237 }
238 
239 static const struct rtc_class_ops max8998_rtc_ops = {
240 	.read_time = max8998_rtc_read_time,
241 	.set_time = max8998_rtc_set_time,
242 	.read_alarm = max8998_rtc_read_alarm,
243 	.set_alarm = max8998_rtc_set_alarm,
244 	.alarm_irq_enable = max8998_rtc_alarm_irq_enable,
245 };
246 
247 static int max8998_rtc_probe(struct platform_device *pdev)
248 {
249 	struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
250 	struct max8998_platform_data *pdata = max8998->pdata;
251 	struct max8998_rtc_info *info;
252 	int ret;
253 
254 	info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
255 			GFP_KERNEL);
256 	if (!info)
257 		return -ENOMEM;
258 
259 	info->dev = &pdev->dev;
260 	info->max8998 = max8998;
261 	info->rtc = max8998->rtc;
262 
263 	platform_set_drvdata(pdev, info);
264 
265 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc",
266 			&max8998_rtc_ops, THIS_MODULE);
267 
268 	if (IS_ERR(info->rtc_dev)) {
269 		ret = PTR_ERR(info->rtc_dev);
270 		dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
271 		return ret;
272 	}
273 
274 	if (!max8998->irq_domain)
275 		goto no_irq;
276 
277 	info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0);
278 	if (!info->irq) {
279 		dev_warn(&pdev->dev, "Failed to map alarm IRQ\n");
280 		goto no_irq;
281 	}
282 
283 	ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
284 				max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
285 
286 	if (ret < 0)
287 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
288 			info->irq, ret);
289 
290 no_irq:
291 	dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
292 	if (pdata && pdata->rtc_delay) {
293 		info->lp3974_bug_workaround = true;
294 		dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
295 				" RTC updates will be extremely slow.\n");
296 	}
297 
298 	return 0;
299 }
300 
301 static const struct platform_device_id max8998_rtc_id[] = {
302 	{ "max8998-rtc", TYPE_MAX8998 },
303 	{ "lp3974-rtc", TYPE_LP3974 },
304 	{ }
305 };
306 MODULE_DEVICE_TABLE(platform, max8998_rtc_id);
307 
308 static struct platform_driver max8998_rtc_driver = {
309 	.driver		= {
310 		.name	= "max8998-rtc",
311 	},
312 	.probe		= max8998_rtc_probe,
313 	.id_table	= max8998_rtc_id,
314 };
315 
316 module_platform_driver(max8998_rtc_driver);
317 
318 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
319 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
320 MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
321 MODULE_LICENSE("GPL");
322