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 #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/max77686-private.h>
22 #include <linux/irqdomain.h>
23 #include <linux/regmap.h>
25 /* RTC Control Register */
26 #define BCD_EN_SHIFT 0
27 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
28 #define MODEL24_SHIFT 1
29 #define MODEL24_MASK (1 << MODEL24_SHIFT)
30 /* RTC Update Register1 */
31 #define RTC_UDR_SHIFT 0
32 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
33 #define RTC_RBUDR_SHIFT 4
34 #define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
35 /* RTC Hour register */
36 #define HOUR_PM_SHIFT 6
37 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
38 /* RTC Alarm Enable */
39 #define ALARM_ENABLE_SHIFT 7
40 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
42 #define MAX77686_RTC_UPDATE_DELAY 16
55 struct max77686_rtc_info
{
57 struct max77686_dev
*max77686
;
58 struct i2c_client
*rtc
;
59 struct rtc_device
*rtc_dev
;
62 struct regmap
*regmap
;
68 enum MAX77686_RTC_OP
{
73 static void max77686_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
76 tm
->tm_sec
= data
[RTC_SEC
] & 0x7f;
77 tm
->tm_min
= data
[RTC_MIN
] & 0x7f;
79 tm
->tm_hour
= data
[RTC_HOUR
] & 0x1f;
81 tm
->tm_hour
= data
[RTC_HOUR
] & 0x0f;
82 if (data
[RTC_HOUR
] & HOUR_PM_MASK
)
86 /* Only a single bit is set in data[], so fls() would be equivalent */
87 tm
->tm_wday
= ffs(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 max77686_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("%s: MAX77686 RTC cannot handle the year %d."
107 "Assume it's 2000.\n", __func__
, 1900 + tm
->tm_year
);
113 static int max77686_rtc_update(struct max77686_rtc_info
*info
,
114 enum MAX77686_RTC_OP op
)
119 if (op
== MAX77686_RTC_WRITE
)
120 data
= 1 << RTC_UDR_SHIFT
;
122 data
= 1 << RTC_RBUDR_SHIFT
;
124 ret
= regmap_update_bits(info
->max77686
->rtc_regmap
,
125 MAX77686_RTC_UPDATE0
, data
, data
);
127 dev_err(info
->dev
, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
128 __func__
, ret
, data
);
130 /* Minimum 16ms delay required before RTC update. */
131 msleep(MAX77686_RTC_UPDATE_DELAY
);
137 static int max77686_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
139 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
140 u8 data
[RTC_NR_TIME
];
143 mutex_lock(&info
->lock
);
145 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
149 ret
= regmap_bulk_read(info
->max77686
->rtc_regmap
,
150 MAX77686_RTC_SEC
, data
, RTC_NR_TIME
);
152 dev_err(info
->dev
, "%s: fail to read time reg(%d)\n", __func__
, ret
);
156 max77686_rtc_data_to_tm(data
, tm
, info
->rtc_24hr_mode
);
158 ret
= rtc_valid_tm(tm
);
161 mutex_unlock(&info
->lock
);
165 static int max77686_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
167 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
168 u8 data
[RTC_NR_TIME
];
171 ret
= max77686_rtc_tm_to_data(tm
, data
);
175 mutex_lock(&info
->lock
);
177 ret
= regmap_bulk_write(info
->max77686
->rtc_regmap
,
178 MAX77686_RTC_SEC
, data
, RTC_NR_TIME
);
180 dev_err(info
->dev
, "%s: fail to write time reg(%d)\n", __func__
,
185 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
188 mutex_unlock(&info
->lock
);
192 static int max77686_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
194 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
195 u8 data
[RTC_NR_TIME
];
199 mutex_lock(&info
->lock
);
201 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
205 ret
= regmap_bulk_read(info
->max77686
->rtc_regmap
,
206 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
208 dev_err(info
->dev
, "%s:%d fail to read alarm reg(%d)\n",
209 __func__
, __LINE__
, ret
);
213 max77686_rtc_data_to_tm(data
, &alrm
->time
, info
->rtc_24hr_mode
);
216 for (i
= 0; i
< RTC_NR_TIME
; i
++) {
217 if (data
[i
] & ALARM_ENABLE_MASK
) {
224 ret
= regmap_read(info
->max77686
->regmap
, MAX77686_REG_STATUS2
, &val
);
226 dev_err(info
->dev
, "%s:%d fail to read status2 reg(%d)\n",
227 __func__
, __LINE__
, ret
);
231 if (val
& (1 << 4)) /* RTCA1 */
235 mutex_unlock(&info
->lock
);
239 static int max77686_rtc_stop_alarm(struct max77686_rtc_info
*info
)
241 u8 data
[RTC_NR_TIME
];
245 if (!mutex_is_locked(&info
->lock
))
246 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
248 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
252 ret
= regmap_bulk_read(info
->max77686
->rtc_regmap
,
253 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
255 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
260 max77686_rtc_data_to_tm(data
, &tm
, info
->rtc_24hr_mode
);
262 for (i
= 0; i
< RTC_NR_TIME
; i
++)
263 data
[i
] &= ~ALARM_ENABLE_MASK
;
265 ret
= regmap_bulk_write(info
->max77686
->rtc_regmap
,
266 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
268 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
273 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
278 static int max77686_rtc_start_alarm(struct max77686_rtc_info
*info
)
280 u8 data
[RTC_NR_TIME
];
284 if (!mutex_is_locked(&info
->lock
))
285 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
287 ret
= max77686_rtc_update(info
, MAX77686_RTC_READ
);
291 ret
= regmap_bulk_read(info
->max77686
->rtc_regmap
,
292 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
294 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
299 max77686_rtc_data_to_tm(data
, &tm
, info
->rtc_24hr_mode
);
301 data
[RTC_SEC
] |= (1 << ALARM_ENABLE_SHIFT
);
302 data
[RTC_MIN
] |= (1 << ALARM_ENABLE_SHIFT
);
303 data
[RTC_HOUR
] |= (1 << ALARM_ENABLE_SHIFT
);
304 data
[RTC_WEEKDAY
] &= ~ALARM_ENABLE_MASK
;
305 if (data
[RTC_MONTH
] & 0xf)
306 data
[RTC_MONTH
] |= (1 << ALARM_ENABLE_SHIFT
);
307 if (data
[RTC_YEAR
] & 0x7f)
308 data
[RTC_YEAR
] |= (1 << ALARM_ENABLE_SHIFT
);
309 if (data
[RTC_DATE
] & 0x1f)
310 data
[RTC_DATE
] |= (1 << ALARM_ENABLE_SHIFT
);
312 ret
= regmap_bulk_write(info
->max77686
->rtc_regmap
,
313 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
315 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
320 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
325 static int max77686_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
327 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
328 u8 data
[RTC_NR_TIME
];
331 ret
= max77686_rtc_tm_to_data(&alrm
->time
, data
);
335 mutex_lock(&info
->lock
);
337 ret
= max77686_rtc_stop_alarm(info
);
341 ret
= regmap_bulk_write(info
->max77686
->rtc_regmap
,
342 MAX77686_ALARM1_SEC
, data
, RTC_NR_TIME
);
345 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
350 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
355 ret
= max77686_rtc_start_alarm(info
);
357 mutex_unlock(&info
->lock
);
361 static int max77686_rtc_alarm_irq_enable(struct device
*dev
,
362 unsigned int enabled
)
364 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
367 mutex_lock(&info
->lock
);
369 ret
= max77686_rtc_start_alarm(info
);
371 ret
= max77686_rtc_stop_alarm(info
);
372 mutex_unlock(&info
->lock
);
377 static irqreturn_t
max77686_rtc_alarm_irq(int irq
, void *data
)
379 struct max77686_rtc_info
*info
= data
;
381 dev_info(info
->dev
, "%s:irq(%d)\n", __func__
, irq
);
383 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
388 static const struct rtc_class_ops max77686_rtc_ops
= {
389 .read_time
= max77686_rtc_read_time
,
390 .set_time
= max77686_rtc_set_time
,
391 .read_alarm
= max77686_rtc_read_alarm
,
392 .set_alarm
= max77686_rtc_set_alarm
,
393 .alarm_irq_enable
= max77686_rtc_alarm_irq_enable
,
396 static int max77686_rtc_init_reg(struct max77686_rtc_info
*info
)
401 /* Set RTC control register : Binary mode, 24hour mdoe */
402 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
403 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
405 info
->rtc_24hr_mode
= 1;
407 ret
= regmap_bulk_write(info
->max77686
->rtc_regmap
, MAX77686_RTC_CONTROLM
, data
, 2);
409 dev_err(info
->dev
, "%s: fail to write controlm reg(%d)\n",
414 ret
= max77686_rtc_update(info
, MAX77686_RTC_WRITE
);
418 static int max77686_rtc_probe(struct platform_device
*pdev
)
420 struct max77686_dev
*max77686
= dev_get_drvdata(pdev
->dev
.parent
);
421 struct max77686_rtc_info
*info
;
424 dev_info(&pdev
->dev
, "%s\n", __func__
);
426 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77686_rtc_info
),
431 mutex_init(&info
->lock
);
432 info
->dev
= &pdev
->dev
;
433 info
->max77686
= max77686
;
434 info
->rtc
= max77686
->rtc
;
436 platform_set_drvdata(pdev
, info
);
438 ret
= max77686_rtc_init_reg(info
);
441 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
445 device_init_wakeup(&pdev
->dev
, 1);
447 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max77686-rtc",
448 &max77686_rtc_ops
, THIS_MODULE
);
450 if (IS_ERR(info
->rtc_dev
)) {
451 ret
= PTR_ERR(info
->rtc_dev
);
452 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
458 if (!max77686
->rtc_irq_data
) {
460 dev_err(&pdev
->dev
, "%s: no RTC regmap IRQ chip\n", __func__
);
464 info
->virq
= regmap_irq_get_virq(max77686
->rtc_irq_data
,
465 MAX77686_RTCIRQ_RTCA1
);
471 ret
= devm_request_threaded_irq(&pdev
->dev
, info
->virq
, NULL
,
472 max77686_rtc_alarm_irq
, 0, "rtc-alarm1", info
);
474 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
481 #ifdef CONFIG_PM_SLEEP
482 static int max77686_rtc_suspend(struct device
*dev
)
484 if (device_may_wakeup(dev
)) {
485 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
487 return enable_irq_wake(info
->virq
);
493 static int max77686_rtc_resume(struct device
*dev
)
495 if (device_may_wakeup(dev
)) {
496 struct max77686_rtc_info
*info
= dev_get_drvdata(dev
);
498 return disable_irq_wake(info
->virq
);
505 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops
,
506 max77686_rtc_suspend
, max77686_rtc_resume
);
508 static const struct platform_device_id rtc_id
[] = {
509 { "max77686-rtc", 0 },
513 static struct platform_driver max77686_rtc_driver
= {
515 .name
= "max77686-rtc",
516 .owner
= THIS_MODULE
,
517 .pm
= &max77686_rtc_pm_ops
,
519 .probe
= max77686_rtc_probe
,
523 module_platform_driver(max77686_rtc_driver
);
525 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
526 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
527 MODULE_LICENSE("GPL");