2 * RTC driver for Maxim MAX77686
4 * Copyright (C) 2012 Samsung Electronics Co.Ltd
6 * based on rtc-max8997.c
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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)
44 #define MAX77686_RTC_UPDATE_DELAY 16
57 struct max77686_rtc_info {
59 struct max77686_dev *max77686;
60 struct i2c_client *rtc;
61 struct rtc_device *rtc_dev;
64 struct regmap *regmap;
70 enum MAX77686_RTC_OP {
75 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
78 tm->tm_sec = data[RTC_SEC] & 0x7f;
79 tm->tm_min = data[RTC_MIN] & 0x7f;
81 tm->tm_hour = data[RTC_HOUR] & 0x1f;
83 tm->tm_hour = data[RTC_HOUR] & 0x0f;
84 if (data[RTC_HOUR] & HOUR_PM_MASK)
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;
97 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
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;
107 if (tm->tm_year < 100) {
108 pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
115 static int max77686_rtc_update(struct max77686_rtc_info *info,
116 enum MAX77686_RTC_OP op)
121 if (op == MAX77686_RTC_WRITE)
122 data = 1 << RTC_UDR_SHIFT;
124 data = 1 << RTC_RBUDR_SHIFT;
126 ret = regmap_update_bits(info->max77686->rtc_regmap,
127 MAX77686_RTC_UPDATE0, data, data);
129 dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
130 __func__, ret, data);
132 /* Minimum 16ms delay required before RTC update. */
133 msleep(MAX77686_RTC_UPDATE_DELAY);
139 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
141 struct max77686_rtc_info *info = dev_get_drvdata(dev);
142 u8 data[RTC_NR_TIME];
145 mutex_lock(&info->lock);
147 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
151 ret = regmap_bulk_read(info->max77686->rtc_regmap,
152 MAX77686_RTC_SEC, data, RTC_NR_TIME);
154 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__, ret);
158 max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
160 ret = rtc_valid_tm(tm);
163 mutex_unlock(&info->lock);
167 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
169 struct max77686_rtc_info *info = dev_get_drvdata(dev);
170 u8 data[RTC_NR_TIME];
173 ret = max77686_rtc_tm_to_data(tm, data);
177 mutex_lock(&info->lock);
179 ret = regmap_bulk_write(info->max77686->rtc_regmap,
180 MAX77686_RTC_SEC, data, RTC_NR_TIME);
182 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
187 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
190 mutex_unlock(&info->lock);
194 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196 struct max77686_rtc_info *info = dev_get_drvdata(dev);
197 u8 data[RTC_NR_TIME];
201 mutex_lock(&info->lock);
203 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
207 ret = regmap_bulk_read(info->max77686->rtc_regmap,
208 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
210 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
211 __func__, __LINE__, ret);
215 max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
218 for (i = 0; i < RTC_NR_TIME; i++) {
219 if (data[i] & ALARM_ENABLE_MASK) {
226 ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
228 dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
229 __func__, __LINE__, ret);
233 if (val & (1 << 4)) /* RTCA1 */
237 mutex_unlock(&info->lock);
241 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
243 u8 data[RTC_NR_TIME];
247 if (!mutex_is_locked(&info->lock))
248 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
250 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
254 ret = regmap_bulk_read(info->max77686->rtc_regmap,
255 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
257 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
262 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
264 for (i = 0; i < RTC_NR_TIME; i++)
265 data[i] &= ~ALARM_ENABLE_MASK;
267 ret = regmap_bulk_write(info->max77686->rtc_regmap,
268 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
270 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
275 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
280 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
282 u8 data[RTC_NR_TIME];
286 if (!mutex_is_locked(&info->lock))
287 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
289 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
293 ret = regmap_bulk_read(info->max77686->rtc_regmap,
294 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
296 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
301 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
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);
314 ret = regmap_bulk_write(info->max77686->rtc_regmap,
315 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
317 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
322 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
327 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
329 struct max77686_rtc_info *info = dev_get_drvdata(dev);
330 u8 data[RTC_NR_TIME];
333 ret = max77686_rtc_tm_to_data(&alrm->time, data);
337 mutex_lock(&info->lock);
339 ret = max77686_rtc_stop_alarm(info);
343 ret = regmap_bulk_write(info->max77686->rtc_regmap,
344 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
347 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
352 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
357 ret = max77686_rtc_start_alarm(info);
359 mutex_unlock(&info->lock);
363 static int max77686_rtc_alarm_irq_enable(struct device *dev,
364 unsigned int enabled)
366 struct max77686_rtc_info *info = dev_get_drvdata(dev);
369 mutex_lock(&info->lock);
371 ret = max77686_rtc_start_alarm(info);
373 ret = max77686_rtc_stop_alarm(info);
374 mutex_unlock(&info->lock);
379 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
381 struct max77686_rtc_info *info = data;
383 dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
385 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
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,
398 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
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);
407 info->rtc_24hr_mode = 1;
409 ret = regmap_bulk_write(info->max77686->rtc_regmap, MAX77686_RTC_CONTROLM, data, 2);
411 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
416 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
420 static int max77686_rtc_probe(struct platform_device *pdev)
422 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
423 struct max77686_rtc_info *info;
426 dev_info(&pdev->dev, "%s\n", __func__);
428 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
433 mutex_init(&info->lock);
434 info->dev = &pdev->dev;
435 info->max77686 = max77686;
436 info->rtc = max77686->rtc;
438 platform_set_drvdata(pdev, info);
440 ret = max77686_rtc_init_reg(info);
443 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
447 device_init_wakeup(&pdev->dev, 1);
449 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
450 &max77686_rtc_ops, THIS_MODULE);
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);
460 if (!max77686->rtc_irq_data) {
462 dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
466 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
467 MAX77686_RTCIRQ_RTCA1);
468 if (info->virq <= 0) {
473 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
474 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
476 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
483 #ifdef CONFIG_PM_SLEEP
484 static int max77686_rtc_suspend(struct device *dev)
486 if (device_may_wakeup(dev)) {
487 struct max77686_rtc_info *info = dev_get_drvdata(dev);
489 return enable_irq_wake(info->virq);
495 static int max77686_rtc_resume(struct device *dev)
497 if (device_may_wakeup(dev)) {
498 struct max77686_rtc_info *info = dev_get_drvdata(dev);
500 return disable_irq_wake(info->virq);
507 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
508 max77686_rtc_suspend, max77686_rtc_resume);
510 static const struct platform_device_id rtc_id[] = {
511 { "max77686-rtc", 0 },
514 MODULE_DEVICE_TABLE(platform, rtc_id);
516 static struct platform_driver max77686_rtc_driver = {
518 .name = "max77686-rtc",
519 .pm = &max77686_rtc_pm_ops,
521 .probe = max77686_rtc_probe,
525 module_platform_driver(max77686_rtc_driver);
527 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
528 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
529 MODULE_LICENSE("GPL");