2 * RTC driver for Maxim MAX8997
4 * Copyright (C) 2013 Samsung Electronics Co.Ltd
6 * based on rtc-max8998.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 #include <linux/slab.h>
16 #include <linux/rtc.h>
17 #include <linux/delay.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/max8997-private.h>
22 #include <linux/irqdomain.h>
24 /* Module parameter for WTSR function control */
25 static int wtsr_en
= 1;
26 module_param(wtsr_en
, int, 0444);
27 MODULE_PARM_DESC(wtsr_en
, "Watchdog Timeout & Software Reset (default=on)");
28 /* Module parameter for SMPL function control */
29 static int smpl_en
= 1;
30 module_param(smpl_en
, int, 0444);
31 MODULE_PARM_DESC(smpl_en
, "Sudden Momentary Power Loss (default=on)");
33 /* RTC Control Register */
34 #define BCD_EN_SHIFT 0
35 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
36 #define MODEL24_SHIFT 1
37 #define MODEL24_MASK (1 << MODEL24_SHIFT)
38 /* RTC Update Register1 */
39 #define RTC_UDR_SHIFT 0
40 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
41 /* WTSR and SMPL Register */
44 #define WTSR_EN_SHIFT 6
45 #define SMPL_EN_SHIFT 7
46 #define WTSRT_MASK (3 << WTSRT_SHIFT)
47 #define SMPLT_MASK (3 << SMPLT_SHIFT)
48 #define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
49 #define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
50 /* RTC Hour register */
51 #define HOUR_PM_SHIFT 6
52 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
53 /* RTC Alarm Enable */
54 #define ALARM_ENABLE_SHIFT 7
55 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
68 struct max8997_rtc_info
{
70 struct max8997_dev
*max8997
;
71 struct i2c_client
*rtc
;
72 struct rtc_device
*rtc_dev
;
78 static void max8997_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
81 tm
->tm_sec
= data
[RTC_SEC
] & 0x7f;
82 tm
->tm_min
= data
[RTC_MIN
] & 0x7f;
84 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
86 tm
->tm_hour
= data
[RTC_HOUR
] & 0x0f;
87 if (data
[RTC_HOUR
] & HOUR_PM_MASK
)
91 tm
->tm_wday
= fls(data
[RTC_WEEKDAY
] & 0x7f) - 1;
92 tm
->tm_mday
= data
[RTC_DATE
] & 0x1f;
93 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
94 tm
->tm_year
= (data
[RTC_YEAR
] & 0x7f) + 100;
99 static int max8997_rtc_tm_to_data(struct rtc_time
*tm
, u8
*data
)
101 data
[RTC_SEC
] = tm
->tm_sec
;
102 data
[RTC_MIN
] = tm
->tm_min
;
103 data
[RTC_HOUR
] = tm
->tm_hour
;
104 data
[RTC_WEEKDAY
] = 1 << tm
->tm_wday
;
105 data
[RTC_DATE
] = tm
->tm_mday
;
106 data
[RTC_MONTH
] = tm
->tm_mon
+ 1;
107 data
[RTC_YEAR
] = tm
->tm_year
> 100 ? (tm
->tm_year
- 100) : 0;
109 if (tm
->tm_year
< 100) {
110 pr_warn("%s: MAX8997 RTC cannot handle the year %d."
111 "Assume it's 2000.\n", __func__
, 1900 + tm
->tm_year
);
117 static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info
*info
)
121 ret
= max8997_write_reg(info
->rtc
, MAX8997_RTC_UPDATE1
,
124 dev_err(info
->dev
, "%s: fail to write update reg(%d)\n",
127 /* Minimum 16ms delay required before RTC update.
128 * Otherwise, we may read and update based on out-of-date
136 static int max8997_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
138 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
139 u8 data
[RTC_NR_TIME
];
142 mutex_lock(&info
->lock
);
143 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
144 mutex_unlock(&info
->lock
);
147 dev_err(info
->dev
, "%s: fail to read time reg(%d)\n", __func__
,
152 max8997_rtc_data_to_tm(data
, tm
, info
->rtc_24hr_mode
);
154 return rtc_valid_tm(tm
);
157 static int max8997_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
159 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
160 u8 data
[RTC_NR_TIME
];
163 ret
= max8997_rtc_tm_to_data(tm
, data
);
167 mutex_lock(&info
->lock
);
169 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
171 dev_err(info
->dev
, "%s: fail to write time reg(%d)\n", __func__
,
176 ret
= max8997_rtc_set_update_reg(info
);
178 mutex_unlock(&info
->lock
);
182 static int max8997_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
184 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
185 u8 data
[RTC_NR_TIME
];
189 mutex_lock(&info
->lock
);
191 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
194 dev_err(info
->dev
, "%s:%d fail to read alarm reg(%d)\n",
195 __func__
, __LINE__
, ret
);
199 max8997_rtc_data_to_tm(data
, &alrm
->time
, info
->rtc_24hr_mode
);
202 for (i
= 0; i
< RTC_NR_TIME
; i
++) {
203 if (data
[i
] & ALARM_ENABLE_MASK
) {
210 ret
= max8997_read_reg(info
->max8997
->i2c
, MAX8997_REG_STATUS1
, &val
);
212 dev_err(info
->dev
, "%s:%d fail to read status1 reg(%d)\n",
213 __func__
, __LINE__
, ret
);
217 if (val
& (1 << 4)) /* RTCA1 */
221 mutex_unlock(&info
->lock
);
225 static int max8997_rtc_stop_alarm(struct max8997_rtc_info
*info
)
227 u8 data
[RTC_NR_TIME
];
230 if (!mutex_is_locked(&info
->lock
))
231 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
233 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
236 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
241 for (i
= 0; i
< RTC_NR_TIME
; i
++)
242 data
[i
] &= ~ALARM_ENABLE_MASK
;
244 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
247 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
252 ret
= max8997_rtc_set_update_reg(info
);
257 static int max8997_rtc_start_alarm(struct max8997_rtc_info
*info
)
259 u8 data
[RTC_NR_TIME
];
262 if (!mutex_is_locked(&info
->lock
))
263 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
265 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
268 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
273 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
274 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
275 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
276 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
277 if (data
[RTC_MONTH
] & 0xf)
278 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
279 if (data
[RTC_YEAR
] & 0x7f)
280 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
281 if (data
[RTC_DATE
] & 0x1f)
282 data
[RTC_DATE
] |= (1 << ALARM_ENABLE_SHIFT
);
284 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
287 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
292 ret
= max8997_rtc_set_update_reg(info
);
296 static int max8997_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
298 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
299 u8 data
[RTC_NR_TIME
];
302 ret
= max8997_rtc_tm_to_data(&alrm
->time
, data
);
306 dev_info(info
->dev
, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__
,
307 data
[RTC_YEAR
] + 2000, data
[RTC_MONTH
], data
[RTC_DATE
],
308 data
[RTC_HOUR
], data
[RTC_MIN
], data
[RTC_SEC
]);
310 mutex_lock(&info
->lock
);
312 ret
= max8997_rtc_stop_alarm(info
);
316 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
319 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
324 ret
= max8997_rtc_set_update_reg(info
);
329 ret
= max8997_rtc_start_alarm(info
);
331 mutex_unlock(&info
->lock
);
335 static int max8997_rtc_alarm_irq_enable(struct device
*dev
,
336 unsigned int enabled
)
338 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
341 mutex_lock(&info
->lock
);
343 ret
= max8997_rtc_start_alarm(info
);
345 ret
= max8997_rtc_stop_alarm(info
);
346 mutex_unlock(&info
->lock
);
351 static irqreturn_t
max8997_rtc_alarm_irq(int irq
, void *data
)
353 struct max8997_rtc_info
*info
= data
;
355 dev_info(info
->dev
, "%s:irq(%d)\n", __func__
, irq
);
357 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
362 static const struct rtc_class_ops max8997_rtc_ops
= {
363 .read_time
= max8997_rtc_read_time
,
364 .set_time
= max8997_rtc_set_time
,
365 .read_alarm
= max8997_rtc_read_alarm
,
366 .set_alarm
= max8997_rtc_set_alarm
,
367 .alarm_irq_enable
= max8997_rtc_alarm_irq_enable
,
370 static void max8997_rtc_enable_wtsr(struct max8997_rtc_info
*info
, bool enable
)
379 val
= (1 << WTSR_EN_SHIFT
) | (3 << WTSRT_SHIFT
);
383 mask
= WTSR_EN_MASK
| WTSRT_MASK
;
385 dev_info(info
->dev
, "%s: %s WTSR\n", __func__
,
386 enable
? "enable" : "disable");
388 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
390 dev_err(info
->dev
, "%s: fail to update WTSR reg(%d)\n",
395 max8997_rtc_set_update_reg(info
);
398 static void max8997_rtc_enable_smpl(struct max8997_rtc_info
*info
, bool enable
)
407 val
= (1 << SMPL_EN_SHIFT
) | (0 << SMPLT_SHIFT
);
411 mask
= SMPL_EN_MASK
| SMPLT_MASK
;
413 dev_info(info
->dev
, "%s: %s SMPL\n", __func__
,
414 enable
? "enable" : "disable");
416 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
418 dev_err(info
->dev
, "%s: fail to update SMPL reg(%d)\n",
423 max8997_rtc_set_update_reg(info
);
426 max8997_read_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, &val
);
427 pr_info("%s: WTSR_SMPL(0x%02x)\n", __func__
, val
);
430 static int max8997_rtc_init_reg(struct max8997_rtc_info
*info
)
435 /* Set RTC control register : Binary mode, 24hour mdoe */
436 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
437 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
439 info
->rtc_24hr_mode
= 1;
441 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_CTRLMASK
, 2, data
);
443 dev_err(info
->dev
, "%s: fail to write controlm reg(%d)\n",
448 ret
= max8997_rtc_set_update_reg(info
);
452 static int max8997_rtc_probe(struct platform_device
*pdev
)
454 struct max8997_dev
*max8997
= dev_get_drvdata(pdev
->dev
.parent
);
455 struct max8997_rtc_info
*info
;
458 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_rtc_info
),
463 mutex_init(&info
->lock
);
464 info
->dev
= &pdev
->dev
;
465 info
->max8997
= max8997
;
466 info
->rtc
= max8997
->rtc
;
468 platform_set_drvdata(pdev
, info
);
470 ret
= max8997_rtc_init_reg(info
);
473 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
477 max8997_rtc_enable_wtsr(info
, true);
478 max8997_rtc_enable_smpl(info
, true);
480 device_init_wakeup(&pdev
->dev
, 1);
482 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max8997-rtc",
483 &max8997_rtc_ops
, THIS_MODULE
);
485 if (IS_ERR(info
->rtc_dev
)) {
486 ret
= PTR_ERR(info
->rtc_dev
);
487 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
491 virq
= irq_create_mapping(max8997
->irq_domain
, MAX8997_PMICIRQ_RTCA1
);
493 dev_err(&pdev
->dev
, "Failed to create mapping alarm IRQ\n");
499 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
500 max8997_rtc_alarm_irq
, 0,
503 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
510 static void max8997_rtc_shutdown(struct platform_device
*pdev
)
512 struct max8997_rtc_info
*info
= platform_get_drvdata(pdev
);
514 max8997_rtc_enable_wtsr(info
, false);
515 max8997_rtc_enable_smpl(info
, false);
518 static const struct platform_device_id rtc_id
[] = {
519 { "max8997-rtc", 0 },
523 static struct platform_driver max8997_rtc_driver
= {
525 .name
= "max8997-rtc",
526 .owner
= THIS_MODULE
,
528 .probe
= max8997_rtc_probe
,
529 .shutdown
= max8997_rtc_shutdown
,
533 module_platform_driver(max8997_rtc_driver
);
535 MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
536 MODULE_AUTHOR("<ms925.kim@samsung.com>");
537 MODULE_LICENSE("GPL");