xref: /linux/drivers/rtc/rtc-max77686.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * RTC driver for Maxim MAX77686
3  *
4  * Copyright (C) 2012 Samsung Electronics Co.Ltd
5  *
6  *  based on rtc-max8997.c
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
19 #include <linux/delay.h>
20 #include <linux/mutex.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/max77686-private.h>
24 #include <linux/irqdomain.h>
25 #include <linux/regmap.h>
26 
27 /* RTC Control Register */
28 #define BCD_EN_SHIFT			0
29 #define BCD_EN_MASK			(1 << BCD_EN_SHIFT)
30 #define MODEL24_SHIFT			1
31 #define MODEL24_MASK			(1 << MODEL24_SHIFT)
32 /* RTC Update Register1 */
33 #define RTC_UDR_SHIFT			0
34 #define RTC_UDR_MASK			(1 << RTC_UDR_SHIFT)
35 #define RTC_RBUDR_SHIFT			4
36 #define RTC_RBUDR_MASK			(1 << RTC_RBUDR_SHIFT)
37 /* RTC Hour register */
38 #define HOUR_PM_SHIFT			6
39 #define HOUR_PM_MASK			(1 << HOUR_PM_SHIFT)
40 /* RTC Alarm Enable */
41 #define ALARM_ENABLE_SHIFT		7
42 #define ALARM_ENABLE_MASK		(1 << ALARM_ENABLE_SHIFT)
43 
44 #define MAX77686_RTC_UPDATE_DELAY	16
45 
46 enum {
47 	RTC_SEC = 0,
48 	RTC_MIN,
49 	RTC_HOUR,
50 	RTC_WEEKDAY,
51 	RTC_MONTH,
52 	RTC_YEAR,
53 	RTC_DATE,
54 	RTC_NR_TIME
55 };
56 
57 struct max77686_rtc_info {
58 	struct device		*dev;
59 	struct max77686_dev	*max77686;
60 	struct i2c_client	*rtc;
61 	struct rtc_device	*rtc_dev;
62 	struct mutex		lock;
63 
64 	struct regmap		*regmap;
65 
66 	int virq;
67 	int rtc_24hr_mode;
68 };
69 
70 enum MAX77686_RTC_OP {
71 	MAX77686_RTC_WRITE,
72 	MAX77686_RTC_READ,
73 };
74 
75 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
76 				   int rtc_24hr_mode)
77 {
78 	tm->tm_sec = data[RTC_SEC] & 0x7f;
79 	tm->tm_min = data[RTC_MIN] & 0x7f;
80 	if (rtc_24hr_mode)
81 		tm->tm_hour = data[RTC_HOUR] & 0x1f;
82 	else {
83 		tm->tm_hour = data[RTC_HOUR] & 0x0f;
84 		if (data[RTC_HOUR] & HOUR_PM_MASK)
85 			tm->tm_hour += 12;
86 	}
87 
88 	/* Only a single bit is set in data[], so fls() would be equivalent */
89 	tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
90 	tm->tm_mday = data[RTC_DATE] & 0x1f;
91 	tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
92 	tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
93 	tm->tm_yday = 0;
94 	tm->tm_isdst = 0;
95 }
96 
97 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
98 {
99 	data[RTC_SEC] = tm->tm_sec;
100 	data[RTC_MIN] = tm->tm_min;
101 	data[RTC_HOUR] = tm->tm_hour;
102 	data[RTC_WEEKDAY] = 1 << tm->tm_wday;
103 	data[RTC_DATE] = tm->tm_mday;
104 	data[RTC_MONTH] = tm->tm_mon + 1;
105 	data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
106 
107 	if (tm->tm_year < 100) {
108 		pr_warn("RTC cannot handle the year %d.  Assume it's 2000.\n",
109 			1900 + tm->tm_year);
110 		return -EINVAL;
111 	}
112 	return 0;
113 }
114 
115 static int max77686_rtc_update(struct max77686_rtc_info *info,
116 	enum MAX77686_RTC_OP op)
117 {
118 	int ret;
119 	unsigned int data;
120 
121 	if (op == MAX77686_RTC_WRITE)
122 		data = 1 << RTC_UDR_SHIFT;
123 	else
124 		data = 1 << RTC_RBUDR_SHIFT;
125 
126 	ret = regmap_update_bits(info->max77686->rtc_regmap,
127 				 MAX77686_RTC_UPDATE0, data, data);
128 	if (ret < 0)
129 		dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
130 				__func__, ret, data);
131 	else {
132 		/* Minimum 16ms delay required before RTC update. */
133 		msleep(MAX77686_RTC_UPDATE_DELAY);
134 	}
135 
136 	return ret;
137 }
138 
139 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
140 {
141 	struct max77686_rtc_info *info = dev_get_drvdata(dev);
142 	u8 data[RTC_NR_TIME];
143 	int ret;
144 
145 	mutex_lock(&info->lock);
146 
147 	ret = max77686_rtc_update(info, MAX77686_RTC_READ);
148 	if (ret < 0)
149 		goto out;
150 
151 	ret = regmap_bulk_read(info->max77686->rtc_regmap,
152 				MAX77686_RTC_SEC, data, RTC_NR_TIME);
153 	if (ret < 0) {
154 		dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,	ret);
155 		goto out;
156 	}
157 
158 	max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
159 
160 	ret = rtc_valid_tm(tm);
161 
162 out:
163 	mutex_unlock(&info->lock);
164 	return ret;
165 }
166 
167 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
168 {
169 	struct max77686_rtc_info *info = dev_get_drvdata(dev);
170 	u8 data[RTC_NR_TIME];
171 	int ret;
172 
173 	ret = max77686_rtc_tm_to_data(tm, data);
174 	if (ret < 0)
175 		return ret;
176 
177 	mutex_lock(&info->lock);
178 
179 	ret = regmap_bulk_write(info->max77686->rtc_regmap,
180 				 MAX77686_RTC_SEC, data, RTC_NR_TIME);
181 	if (ret < 0) {
182 		dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
183 				ret);
184 		goto out;
185 	}
186 
187 	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
188 
189 out:
190 	mutex_unlock(&info->lock);
191 	return ret;
192 }
193 
194 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195 {
196 	struct max77686_rtc_info *info = dev_get_drvdata(dev);
197 	u8 data[RTC_NR_TIME];
198 	unsigned int val;
199 	int i, ret;
200 
201 	mutex_lock(&info->lock);
202 
203 	ret = max77686_rtc_update(info, MAX77686_RTC_READ);
204 	if (ret < 0)
205 		goto out;
206 
207 	ret = regmap_bulk_read(info->max77686->rtc_regmap,
208 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
209 	if (ret < 0) {
210 		dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
211 				__func__, __LINE__, ret);
212 		goto out;
213 	}
214 
215 	max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
216 
217 	alrm->enabled = 0;
218 	for (i = 0; i < RTC_NR_TIME; i++) {
219 		if (data[i] & ALARM_ENABLE_MASK) {
220 			alrm->enabled = 1;
221 			break;
222 		}
223 	}
224 
225 	alrm->pending = 0;
226 	ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
227 	if (ret < 0) {
228 		dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
229 				__func__, __LINE__, ret);
230 		goto out;
231 	}
232 
233 	if (val & (1 << 4)) /* RTCA1 */
234 		alrm->pending = 1;
235 
236 out:
237 	mutex_unlock(&info->lock);
238 	return 0;
239 }
240 
241 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
242 {
243 	u8 data[RTC_NR_TIME];
244 	int ret, i;
245 	struct rtc_time tm;
246 
247 	if (!mutex_is_locked(&info->lock))
248 		dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
249 
250 	ret = max77686_rtc_update(info, MAX77686_RTC_READ);
251 	if (ret < 0)
252 		goto out;
253 
254 	ret = regmap_bulk_read(info->max77686->rtc_regmap,
255 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
256 	if (ret < 0) {
257 		dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
258 				__func__, ret);
259 		goto out;
260 	}
261 
262 	max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
263 
264 	for (i = 0; i < RTC_NR_TIME; i++)
265 		data[i] &= ~ALARM_ENABLE_MASK;
266 
267 	ret = regmap_bulk_write(info->max77686->rtc_regmap,
268 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
269 	if (ret < 0) {
270 		dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
271 				__func__, ret);
272 		goto out;
273 	}
274 
275 	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
276 out:
277 	return ret;
278 }
279 
280 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
281 {
282 	u8 data[RTC_NR_TIME];
283 	int ret;
284 	struct rtc_time tm;
285 
286 	if (!mutex_is_locked(&info->lock))
287 		dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
288 
289 	ret = max77686_rtc_update(info, MAX77686_RTC_READ);
290 	if (ret < 0)
291 		goto out;
292 
293 	ret = regmap_bulk_read(info->max77686->rtc_regmap,
294 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
295 	if (ret < 0) {
296 		dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
297 				__func__, ret);
298 		goto out;
299 	}
300 
301 	max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
302 
303 	data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
304 	data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
305 	data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
306 	data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
307 	if (data[RTC_MONTH] & 0xf)
308 		data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
309 	if (data[RTC_YEAR] & 0x7f)
310 		data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
311 	if (data[RTC_DATE] & 0x1f)
312 		data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
313 
314 	ret = regmap_bulk_write(info->max77686->rtc_regmap,
315 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
316 	if (ret < 0) {
317 		dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
318 				__func__, ret);
319 		goto out;
320 	}
321 
322 	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
323 out:
324 	return ret;
325 }
326 
327 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
328 {
329 	struct max77686_rtc_info *info = dev_get_drvdata(dev);
330 	u8 data[RTC_NR_TIME];
331 	int ret;
332 
333 	ret = max77686_rtc_tm_to_data(&alrm->time, data);
334 	if (ret < 0)
335 		return ret;
336 
337 	mutex_lock(&info->lock);
338 
339 	ret = max77686_rtc_stop_alarm(info);
340 	if (ret < 0)
341 		goto out;
342 
343 	ret = regmap_bulk_write(info->max77686->rtc_regmap,
344 				 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
345 
346 	if (ret < 0) {
347 		dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
348 				__func__, ret);
349 		goto out;
350 	}
351 
352 	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
353 	if (ret < 0)
354 		goto out;
355 
356 	if (alrm->enabled)
357 		ret = max77686_rtc_start_alarm(info);
358 out:
359 	mutex_unlock(&info->lock);
360 	return ret;
361 }
362 
363 static int max77686_rtc_alarm_irq_enable(struct device *dev,
364 					unsigned int enabled)
365 {
366 	struct max77686_rtc_info *info = dev_get_drvdata(dev);
367 	int ret;
368 
369 	mutex_lock(&info->lock);
370 	if (enabled)
371 		ret = max77686_rtc_start_alarm(info);
372 	else
373 		ret = max77686_rtc_stop_alarm(info);
374 	mutex_unlock(&info->lock);
375 
376 	return ret;
377 }
378 
379 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
380 {
381 	struct max77686_rtc_info *info = data;
382 
383 	dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
384 
385 	rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
386 
387 	return IRQ_HANDLED;
388 }
389 
390 static const struct rtc_class_ops max77686_rtc_ops = {
391 	.read_time = max77686_rtc_read_time,
392 	.set_time = max77686_rtc_set_time,
393 	.read_alarm = max77686_rtc_read_alarm,
394 	.set_alarm = max77686_rtc_set_alarm,
395 	.alarm_irq_enable = max77686_rtc_alarm_irq_enable,
396 };
397 
398 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
399 {
400 	u8 data[2];
401 	int ret;
402 
403 	/* Set RTC control register : Binary mode, 24hour mdoe */
404 	data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
405 	data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
406 
407 	info->rtc_24hr_mode = 1;
408 
409 	ret = regmap_bulk_write(info->max77686->rtc_regmap, MAX77686_RTC_CONTROLM, data, 2);
410 	if (ret < 0) {
411 		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
412 				__func__, ret);
413 		return ret;
414 	}
415 
416 	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
417 	return ret;
418 }
419 
420 static int max77686_rtc_probe(struct platform_device *pdev)
421 {
422 	struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
423 	struct max77686_rtc_info *info;
424 	int ret;
425 
426 	dev_info(&pdev->dev, "%s\n", __func__);
427 
428 	info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
429 				GFP_KERNEL);
430 	if (!info)
431 		return -ENOMEM;
432 
433 	mutex_init(&info->lock);
434 	info->dev = &pdev->dev;
435 	info->max77686 = max77686;
436 	info->rtc = max77686->rtc;
437 
438 	platform_set_drvdata(pdev, info);
439 
440 	ret = max77686_rtc_init_reg(info);
441 
442 	if (ret < 0) {
443 		dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
444 		goto err_rtc;
445 	}
446 
447 	device_init_wakeup(&pdev->dev, 1);
448 
449 	info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
450 					&max77686_rtc_ops, THIS_MODULE);
451 
452 	if (IS_ERR(info->rtc_dev)) {
453 		ret = PTR_ERR(info->rtc_dev);
454 		dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
455 		if (ret == 0)
456 			ret = -EINVAL;
457 		goto err_rtc;
458 	}
459 
460 	if (!max77686->rtc_irq_data) {
461 		ret = -EINVAL;
462 		dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
463 		goto err_rtc;
464 	}
465 
466 	info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
467 					 MAX77686_RTCIRQ_RTCA1);
468 	if (!info->virq) {
469 		ret = -ENXIO;
470 		goto err_rtc;
471 	}
472 
473 	ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
474 				max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
475 	if (ret < 0)
476 		dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
477 			info->virq, ret);
478 
479 err_rtc:
480 	return ret;
481 }
482 
483 #ifdef CONFIG_PM_SLEEP
484 static int max77686_rtc_suspend(struct device *dev)
485 {
486 	if (device_may_wakeup(dev)) {
487 		struct max77686_rtc_info *info = dev_get_drvdata(dev);
488 
489 		return enable_irq_wake(info->virq);
490 	}
491 
492 	return 0;
493 }
494 
495 static int max77686_rtc_resume(struct device *dev)
496 {
497 	if (device_may_wakeup(dev)) {
498 		struct max77686_rtc_info *info = dev_get_drvdata(dev);
499 
500 		return disable_irq_wake(info->virq);
501 	}
502 
503 	return 0;
504 }
505 #endif
506 
507 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
508 			 max77686_rtc_suspend, max77686_rtc_resume);
509 
510 static const struct platform_device_id rtc_id[] = {
511 	{ "max77686-rtc", 0 },
512 	{},
513 };
514 MODULE_DEVICE_TABLE(platform, rtc_id);
515 
516 static struct platform_driver max77686_rtc_driver = {
517 	.driver		= {
518 		.name	= "max77686-rtc",
519 		.pm	= &max77686_rtc_pm_ops,
520 	},
521 	.probe		= max77686_rtc_probe,
522 	.id_table	= rtc_id,
523 };
524 
525 module_platform_driver(max77686_rtc_driver);
526 
527 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
528 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
529 MODULE_LICENSE("GPL");
530