1 // SPDX-License-Identifier: GPL-2.0+
3 // RTC driver for Maxim MAX8997
5 // Copyright (C) 2013 Samsung Electronics Co.Ltd
7 // based on rtc-max8998.c
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/slab.h>
12 #include <linux/rtc.h>
13 #include <linux/delay.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/mfd/max8997-private.h>
18 #include <linux/irqdomain.h>
20 /* Module parameter for WTSR function control */
21 static int wtsr_en
= 1;
22 module_param(wtsr_en
, int, 0444);
23 MODULE_PARM_DESC(wtsr_en
, "Watchdog Timeout & Software Reset (default=on)");
24 /* Module parameter for SMPL function control */
25 static int smpl_en
= 1;
26 module_param(smpl_en
, int, 0444);
27 MODULE_PARM_DESC(smpl_en
, "Sudden Momentary Power Loss (default=on)");
29 /* RTC Control Register */
30 #define BCD_EN_SHIFT 0
31 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
32 #define MODEL24_SHIFT 1
33 #define MODEL24_MASK (1 << MODEL24_SHIFT)
34 /* RTC Update Register1 */
35 #define RTC_UDR_SHIFT 0
36 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
37 /* WTSR and SMPL Register */
40 #define WTSR_EN_SHIFT 6
41 #define SMPL_EN_SHIFT 7
42 #define WTSRT_MASK (3 << WTSRT_SHIFT)
43 #define SMPLT_MASK (3 << SMPLT_SHIFT)
44 #define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
45 #define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
46 /* RTC Hour register */
47 #define HOUR_PM_SHIFT 6
48 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
49 /* RTC Alarm Enable */
50 #define ALARM_ENABLE_SHIFT 7
51 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
64 struct max8997_rtc_info
{
66 struct max8997_dev
*max8997
;
67 struct i2c_client
*rtc
;
68 struct rtc_device
*rtc_dev
;
74 static void max8997_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
77 tm
->tm_sec
= data
[RTC_SEC
] & 0x7f;
78 tm
->tm_min
= data
[RTC_MIN
] & 0x7f;
80 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
82 tm
->tm_hour
= data
[RTC_HOUR
] & 0x0f;
83 if (data
[RTC_HOUR
] & HOUR_PM_MASK
)
87 tm
->tm_wday
= fls(data
[RTC_WEEKDAY
] & 0x7f) - 1;
88 tm
->tm_mday
= data
[RTC_DATE
] & 0x1f;
89 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
90 tm
->tm_year
= (data
[RTC_YEAR
] & 0x7f) + 100;
95 static int max8997_rtc_tm_to_data(struct rtc_time
*tm
, u8
*data
)
97 data
[RTC_SEC
] = tm
->tm_sec
;
98 data
[RTC_MIN
] = tm
->tm_min
;
99 data
[RTC_HOUR
] = tm
->tm_hour
;
100 data
[RTC_WEEKDAY
] = 1 << tm
->tm_wday
;
101 data
[RTC_DATE
] = tm
->tm_mday
;
102 data
[RTC_MONTH
] = tm
->tm_mon
+ 1;
103 data
[RTC_YEAR
] = tm
->tm_year
> 100 ? (tm
->tm_year
- 100) : 0;
105 if (tm
->tm_year
< 100) {
106 pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
113 static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info
*info
)
117 ret
= max8997_write_reg(info
->rtc
, MAX8997_RTC_UPDATE1
,
120 dev_err(info
->dev
, "%s: fail to write update reg(%d)\n",
123 /* Minimum 16ms delay required before RTC update.
124 * Otherwise, we may read and update based on out-of-date
132 static int max8997_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
134 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
135 u8 data
[RTC_NR_TIME
];
138 mutex_lock(&info
->lock
);
139 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
140 mutex_unlock(&info
->lock
);
143 dev_err(info
->dev
, "%s: fail to read time reg(%d)\n", __func__
,
148 max8997_rtc_data_to_tm(data
, tm
, info
->rtc_24hr_mode
);
153 static int max8997_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
155 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
156 u8 data
[RTC_NR_TIME
];
159 ret
= max8997_rtc_tm_to_data(tm
, data
);
163 mutex_lock(&info
->lock
);
165 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
167 dev_err(info
->dev
, "%s: fail to write time reg(%d)\n", __func__
,
172 ret
= max8997_rtc_set_update_reg(info
);
174 mutex_unlock(&info
->lock
);
178 static int max8997_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
180 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
181 u8 data
[RTC_NR_TIME
];
185 mutex_lock(&info
->lock
);
187 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
190 dev_err(info
->dev
, "%s:%d fail to read alarm reg(%d)\n",
191 __func__
, __LINE__
, ret
);
195 max8997_rtc_data_to_tm(data
, &alrm
->time
, info
->rtc_24hr_mode
);
198 for (i
= 0; i
< RTC_NR_TIME
; i
++) {
199 if (data
[i
] & ALARM_ENABLE_MASK
) {
206 ret
= max8997_read_reg(info
->max8997
->i2c
, MAX8997_REG_STATUS1
, &val
);
208 dev_err(info
->dev
, "%s:%d fail to read status1 reg(%d)\n",
209 __func__
, __LINE__
, ret
);
213 if (val
& (1 << 4)) /* RTCA1 */
217 mutex_unlock(&info
->lock
);
221 static int max8997_rtc_stop_alarm(struct max8997_rtc_info
*info
)
223 u8 data
[RTC_NR_TIME
];
226 if (!mutex_is_locked(&info
->lock
))
227 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
229 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
232 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
237 for (i
= 0; i
< RTC_NR_TIME
; i
++)
238 data
[i
] &= ~ALARM_ENABLE_MASK
;
240 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
243 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
248 ret
= max8997_rtc_set_update_reg(info
);
253 static int max8997_rtc_start_alarm(struct max8997_rtc_info
*info
)
255 u8 data
[RTC_NR_TIME
];
258 if (!mutex_is_locked(&info
->lock
))
259 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
261 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
264 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
269 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
270 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
271 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
272 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
273 if (data
[RTC_MONTH
] & 0xf)
274 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
275 if (data
[RTC_YEAR
] & 0x7f)
276 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
277 if (data
[RTC_DATE
] & 0x1f)
278 data
[RTC_DATE
] |= (1 << ALARM_ENABLE_SHIFT
);
280 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
283 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
288 ret
= max8997_rtc_set_update_reg(info
);
292 static int max8997_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
294 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
295 u8 data
[RTC_NR_TIME
];
298 ret
= max8997_rtc_tm_to_data(&alrm
->time
, data
);
302 dev_info(info
->dev
, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__
,
303 data
[RTC_YEAR
] + 2000, data
[RTC_MONTH
], data
[RTC_DATE
],
304 data
[RTC_HOUR
], data
[RTC_MIN
], data
[RTC_SEC
]);
306 mutex_lock(&info
->lock
);
308 ret
= max8997_rtc_stop_alarm(info
);
312 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
315 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
320 ret
= max8997_rtc_set_update_reg(info
);
325 ret
= max8997_rtc_start_alarm(info
);
327 mutex_unlock(&info
->lock
);
331 static int max8997_rtc_alarm_irq_enable(struct device
*dev
,
332 unsigned int enabled
)
334 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
337 mutex_lock(&info
->lock
);
339 ret
= max8997_rtc_start_alarm(info
);
341 ret
= max8997_rtc_stop_alarm(info
);
342 mutex_unlock(&info
->lock
);
347 static irqreturn_t
max8997_rtc_alarm_irq(int irq
, void *data
)
349 struct max8997_rtc_info
*info
= data
;
351 dev_info(info
->dev
, "%s:irq(%d)\n", __func__
, irq
);
353 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
358 static const struct rtc_class_ops max8997_rtc_ops
= {
359 .read_time
= max8997_rtc_read_time
,
360 .set_time
= max8997_rtc_set_time
,
361 .read_alarm
= max8997_rtc_read_alarm
,
362 .set_alarm
= max8997_rtc_set_alarm
,
363 .alarm_irq_enable
= max8997_rtc_alarm_irq_enable
,
366 static void max8997_rtc_enable_wtsr(struct max8997_rtc_info
*info
, bool enable
)
375 val
= (1 << WTSR_EN_SHIFT
) | (3 << WTSRT_SHIFT
);
379 mask
= WTSR_EN_MASK
| WTSRT_MASK
;
381 dev_info(info
->dev
, "%s: %s WTSR\n", __func__
,
382 enable
? "enable" : "disable");
384 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
386 dev_err(info
->dev
, "%s: fail to update WTSR reg(%d)\n",
391 max8997_rtc_set_update_reg(info
);
394 static void max8997_rtc_enable_smpl(struct max8997_rtc_info
*info
, bool enable
)
403 val
= (1 << SMPL_EN_SHIFT
) | (0 << SMPLT_SHIFT
);
407 mask
= SMPL_EN_MASK
| SMPLT_MASK
;
409 dev_info(info
->dev
, "%s: %s SMPL\n", __func__
,
410 enable
? "enable" : "disable");
412 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
414 dev_err(info
->dev
, "%s: fail to update SMPL reg(%d)\n",
419 max8997_rtc_set_update_reg(info
);
422 max8997_read_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, &val
);
423 pr_info("WTSR_SMPL(0x%02x)\n", val
);
426 static int max8997_rtc_init_reg(struct max8997_rtc_info
*info
)
431 /* Set RTC control register : Binary mode, 24hour mdoe */
432 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
433 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
435 info
->rtc_24hr_mode
= 1;
437 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_CTRLMASK
, 2, data
);
439 dev_err(info
->dev
, "%s: fail to write controlm reg(%d)\n",
444 ret
= max8997_rtc_set_update_reg(info
);
448 static int max8997_rtc_probe(struct platform_device
*pdev
)
450 struct max8997_dev
*max8997
= dev_get_drvdata(pdev
->dev
.parent
);
451 struct max8997_rtc_info
*info
;
454 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_rtc_info
),
459 mutex_init(&info
->lock
);
460 info
->dev
= &pdev
->dev
;
461 info
->max8997
= max8997
;
462 info
->rtc
= max8997
->rtc
;
464 platform_set_drvdata(pdev
, info
);
466 ret
= max8997_rtc_init_reg(info
);
469 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
473 max8997_rtc_enable_wtsr(info
, true);
474 max8997_rtc_enable_smpl(info
, true);
476 device_init_wakeup(&pdev
->dev
, 1);
478 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max8997-rtc",
479 &max8997_rtc_ops
, THIS_MODULE
);
481 if (IS_ERR(info
->rtc_dev
)) {
482 ret
= PTR_ERR(info
->rtc_dev
);
483 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
487 virq
= irq_create_mapping(max8997
->irq_domain
, MAX8997_PMICIRQ_RTCA1
);
489 dev_err(&pdev
->dev
, "Failed to create mapping alarm IRQ\n");
495 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
496 max8997_rtc_alarm_irq
, 0,
499 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
506 static void max8997_rtc_shutdown(struct platform_device
*pdev
)
508 struct max8997_rtc_info
*info
= platform_get_drvdata(pdev
);
510 max8997_rtc_enable_wtsr(info
, false);
511 max8997_rtc_enable_smpl(info
, false);
514 static const struct platform_device_id rtc_id
[] = {
515 { "max8997-rtc", 0 },
518 MODULE_DEVICE_TABLE(platform
, rtc_id
);
520 static struct platform_driver max8997_rtc_driver
= {
522 .name
= "max8997-rtc",
524 .probe
= max8997_rtc_probe
,
525 .shutdown
= max8997_rtc_shutdown
,
529 module_platform_driver(max8997_rtc_driver
);
531 MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
532 MODULE_AUTHOR("<ms925.kim@samsung.com>");
533 MODULE_LICENSE("GPL");