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 #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/max8997-private.h>
24 #include <linux/irqdomain.h>
26 /* Module parameter for WTSR function control */
27 static int wtsr_en
= 1;
28 module_param(wtsr_en
, int, 0444);
29 MODULE_PARM_DESC(wtsr_en
, "Watchdog Timeout & Software Reset (default=on)");
30 /* Module parameter for SMPL function control */
31 static int smpl_en
= 1;
32 module_param(smpl_en
, int, 0444);
33 MODULE_PARM_DESC(smpl_en
, "Sudden Momentary Power Loss (default=on)");
35 /* RTC Control Register */
36 #define BCD_EN_SHIFT 0
37 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
38 #define MODEL24_SHIFT 1
39 #define MODEL24_MASK (1 << MODEL24_SHIFT)
40 /* RTC Update Register1 */
41 #define RTC_UDR_SHIFT 0
42 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
43 /* WTSR and SMPL Register */
46 #define WTSR_EN_SHIFT 6
47 #define SMPL_EN_SHIFT 7
48 #define WTSRT_MASK (3 << WTSRT_SHIFT)
49 #define SMPLT_MASK (3 << SMPLT_SHIFT)
50 #define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
51 #define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
52 /* RTC Hour register */
53 #define HOUR_PM_SHIFT 6
54 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
55 /* RTC Alarm Enable */
56 #define ALARM_ENABLE_SHIFT 7
57 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
70 struct max8997_rtc_info
{
72 struct max8997_dev
*max8997
;
73 struct i2c_client
*rtc
;
74 struct rtc_device
*rtc_dev
;
80 static void max8997_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
83 tm
->tm_sec
= data
[RTC_SEC
] & 0x7f;
84 tm
->tm_min
= data
[RTC_MIN
] & 0x7f;
86 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
88 tm
->tm_hour
= data
[RTC_HOUR
] & 0x0f;
89 if (data
[RTC_HOUR
] & HOUR_PM_MASK
)
93 tm
->tm_wday
= fls(data
[RTC_WEEKDAY
] & 0x7f) - 1;
94 tm
->tm_mday
= data
[RTC_DATE
] & 0x1f;
95 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
96 tm
->tm_year
= (data
[RTC_YEAR
] & 0x7f) + 100;
101 static int max8997_rtc_tm_to_data(struct rtc_time
*tm
, u8
*data
)
103 data
[RTC_SEC
] = tm
->tm_sec
;
104 data
[RTC_MIN
] = tm
->tm_min
;
105 data
[RTC_HOUR
] = tm
->tm_hour
;
106 data
[RTC_WEEKDAY
] = 1 << tm
->tm_wday
;
107 data
[RTC_DATE
] = tm
->tm_mday
;
108 data
[RTC_MONTH
] = tm
->tm_mon
+ 1;
109 data
[RTC_YEAR
] = tm
->tm_year
> 100 ? (tm
->tm_year
- 100) : 0;
111 if (tm
->tm_year
< 100) {
112 pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
119 static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info
*info
)
123 ret
= max8997_write_reg(info
->rtc
, MAX8997_RTC_UPDATE1
,
126 dev_err(info
->dev
, "%s: fail to write update reg(%d)\n",
129 /* Minimum 16ms delay required before RTC update.
130 * Otherwise, we may read and update based on out-of-date
138 static int max8997_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
140 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
141 u8 data
[RTC_NR_TIME
];
144 mutex_lock(&info
->lock
);
145 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
146 mutex_unlock(&info
->lock
);
149 dev_err(info
->dev
, "%s: fail to read time reg(%d)\n", __func__
,
154 max8997_rtc_data_to_tm(data
, tm
, info
->rtc_24hr_mode
);
159 static int max8997_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
161 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
162 u8 data
[RTC_NR_TIME
];
165 ret
= max8997_rtc_tm_to_data(tm
, data
);
169 mutex_lock(&info
->lock
);
171 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_SEC
, RTC_NR_TIME
, data
);
173 dev_err(info
->dev
, "%s: fail to write time reg(%d)\n", __func__
,
178 ret
= max8997_rtc_set_update_reg(info
);
180 mutex_unlock(&info
->lock
);
184 static int max8997_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
186 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
187 u8 data
[RTC_NR_TIME
];
191 mutex_lock(&info
->lock
);
193 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
196 dev_err(info
->dev
, "%s:%d fail to read alarm reg(%d)\n",
197 __func__
, __LINE__
, ret
);
201 max8997_rtc_data_to_tm(data
, &alrm
->time
, info
->rtc_24hr_mode
);
204 for (i
= 0; i
< RTC_NR_TIME
; i
++) {
205 if (data
[i
] & ALARM_ENABLE_MASK
) {
212 ret
= max8997_read_reg(info
->max8997
->i2c
, MAX8997_REG_STATUS1
, &val
);
214 dev_err(info
->dev
, "%s:%d fail to read status1 reg(%d)\n",
215 __func__
, __LINE__
, ret
);
219 if (val
& (1 << 4)) /* RTCA1 */
223 mutex_unlock(&info
->lock
);
227 static int max8997_rtc_stop_alarm(struct max8997_rtc_info
*info
)
229 u8 data
[RTC_NR_TIME
];
232 if (!mutex_is_locked(&info
->lock
))
233 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
235 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
238 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
243 for (i
= 0; i
< RTC_NR_TIME
; i
++)
244 data
[i
] &= ~ALARM_ENABLE_MASK
;
246 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
249 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
254 ret
= max8997_rtc_set_update_reg(info
);
259 static int max8997_rtc_start_alarm(struct max8997_rtc_info
*info
)
261 u8 data
[RTC_NR_TIME
];
264 if (!mutex_is_locked(&info
->lock
))
265 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
267 ret
= max8997_bulk_read(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
270 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
275 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
276 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
277 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
278 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
279 if (data
[RTC_MONTH
] & 0xf)
280 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
281 if (data
[RTC_YEAR
] & 0x7f)
282 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
283 if (data
[RTC_DATE
] & 0x1f)
284 data
[RTC_DATE
] |= (1 << ALARM_ENABLE_SHIFT
);
286 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
289 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
294 ret
= max8997_rtc_set_update_reg(info
);
298 static int max8997_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
300 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
301 u8 data
[RTC_NR_TIME
];
304 ret
= max8997_rtc_tm_to_data(&alrm
->time
, data
);
308 dev_info(info
->dev
, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__
,
309 data
[RTC_YEAR
] + 2000, data
[RTC_MONTH
], data
[RTC_DATE
],
310 data
[RTC_HOUR
], data
[RTC_MIN
], data
[RTC_SEC
]);
312 mutex_lock(&info
->lock
);
314 ret
= max8997_rtc_stop_alarm(info
);
318 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_ALARM1_SEC
, RTC_NR_TIME
,
321 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
326 ret
= max8997_rtc_set_update_reg(info
);
331 ret
= max8997_rtc_start_alarm(info
);
333 mutex_unlock(&info
->lock
);
337 static int max8997_rtc_alarm_irq_enable(struct device
*dev
,
338 unsigned int enabled
)
340 struct max8997_rtc_info
*info
= dev_get_drvdata(dev
);
343 mutex_lock(&info
->lock
);
345 ret
= max8997_rtc_start_alarm(info
);
347 ret
= max8997_rtc_stop_alarm(info
);
348 mutex_unlock(&info
->lock
);
353 static irqreturn_t
max8997_rtc_alarm_irq(int irq
, void *data
)
355 struct max8997_rtc_info
*info
= data
;
357 dev_info(info
->dev
, "%s:irq(%d)\n", __func__
, irq
);
359 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
364 static const struct rtc_class_ops max8997_rtc_ops
= {
365 .read_time
= max8997_rtc_read_time
,
366 .set_time
= max8997_rtc_set_time
,
367 .read_alarm
= max8997_rtc_read_alarm
,
368 .set_alarm
= max8997_rtc_set_alarm
,
369 .alarm_irq_enable
= max8997_rtc_alarm_irq_enable
,
372 static void max8997_rtc_enable_wtsr(struct max8997_rtc_info
*info
, bool enable
)
381 val
= (1 << WTSR_EN_SHIFT
) | (3 << WTSRT_SHIFT
);
385 mask
= WTSR_EN_MASK
| WTSRT_MASK
;
387 dev_info(info
->dev
, "%s: %s WTSR\n", __func__
,
388 enable
? "enable" : "disable");
390 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
392 dev_err(info
->dev
, "%s: fail to update WTSR reg(%d)\n",
397 max8997_rtc_set_update_reg(info
);
400 static void max8997_rtc_enable_smpl(struct max8997_rtc_info
*info
, bool enable
)
409 val
= (1 << SMPL_EN_SHIFT
) | (0 << SMPLT_SHIFT
);
413 mask
= SMPL_EN_MASK
| SMPLT_MASK
;
415 dev_info(info
->dev
, "%s: %s SMPL\n", __func__
,
416 enable
? "enable" : "disable");
418 ret
= max8997_update_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, val
, mask
);
420 dev_err(info
->dev
, "%s: fail to update SMPL reg(%d)\n",
425 max8997_rtc_set_update_reg(info
);
428 max8997_read_reg(info
->rtc
, MAX8997_RTC_WTSR_SMPL
, &val
);
429 pr_info("WTSR_SMPL(0x%02x)\n", val
);
432 static int max8997_rtc_init_reg(struct max8997_rtc_info
*info
)
437 /* Set RTC control register : Binary mode, 24hour mdoe */
438 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
439 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
441 info
->rtc_24hr_mode
= 1;
443 ret
= max8997_bulk_write(info
->rtc
, MAX8997_RTC_CTRLMASK
, 2, data
);
445 dev_err(info
->dev
, "%s: fail to write controlm reg(%d)\n",
450 ret
= max8997_rtc_set_update_reg(info
);
454 static int max8997_rtc_probe(struct platform_device
*pdev
)
456 struct max8997_dev
*max8997
= dev_get_drvdata(pdev
->dev
.parent
);
457 struct max8997_rtc_info
*info
;
460 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8997_rtc_info
),
465 mutex_init(&info
->lock
);
466 info
->dev
= &pdev
->dev
;
467 info
->max8997
= max8997
;
468 info
->rtc
= max8997
->rtc
;
470 platform_set_drvdata(pdev
, info
);
472 ret
= max8997_rtc_init_reg(info
);
475 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
479 max8997_rtc_enable_wtsr(info
, true);
480 max8997_rtc_enable_smpl(info
, true);
482 device_init_wakeup(&pdev
->dev
, 1);
484 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max8997-rtc",
485 &max8997_rtc_ops
, THIS_MODULE
);
487 if (IS_ERR(info
->rtc_dev
)) {
488 ret
= PTR_ERR(info
->rtc_dev
);
489 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
493 virq
= irq_create_mapping(max8997
->irq_domain
, MAX8997_PMICIRQ_RTCA1
);
495 dev_err(&pdev
->dev
, "Failed to create mapping alarm IRQ\n");
501 ret
= devm_request_threaded_irq(&pdev
->dev
, virq
, NULL
,
502 max8997_rtc_alarm_irq
, 0,
505 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
512 static void max8997_rtc_shutdown(struct platform_device
*pdev
)
514 struct max8997_rtc_info
*info
= platform_get_drvdata(pdev
);
516 max8997_rtc_enable_wtsr(info
, false);
517 max8997_rtc_enable_smpl(info
, false);
520 static const struct platform_device_id rtc_id
[] = {
521 { "max8997-rtc", 0 },
524 MODULE_DEVICE_TABLE(platform
, rtc_id
);
526 static struct platform_driver max8997_rtc_driver
= {
528 .name
= "max8997-rtc",
530 .probe
= max8997_rtc_probe
,
531 .shutdown
= max8997_rtc_shutdown
,
535 module_platform_driver(max8997_rtc_driver
);
537 MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
538 MODULE_AUTHOR("<ms925.kim@samsung.com>");
539 MODULE_LICENSE("GPL");