2 * RTC driver for Maxim MAX77802
4 * Copyright (C) 2013 Google, Inc
6 * Copyright (C) 2012 Samsung Electronics Co.Ltd
8 * based on rtc-max8997.c
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
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 /* For the RTCAE1 register, we write this value to enable the alarm */
45 #define ALARM_ENABLE_VALUE 0x77
47 #define MAX77802_RTC_UPDATE_DELAY_US 200
60 struct max77802_rtc_info
{
62 struct max77686_dev
*max77802
;
63 struct i2c_client
*rtc
;
64 struct rtc_device
*rtc_dev
;
67 struct regmap
*regmap
;
73 enum MAX77802_RTC_OP
{
78 static void max77802_rtc_data_to_tm(u8
*data
, struct rtc_time
*tm
,
81 tm
->tm_sec
= data
[RTC_SEC
] & 0xff;
82 tm
->tm_min
= data
[RTC_MIN
] & 0xff;
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 /* Only a single bit is set in data[], so fls() would be equivalent */
92 tm
->tm_wday
= ffs(data
[RTC_WEEKDAY
] & 0xff) - 1;
93 tm
->tm_mday
= data
[RTC_DATE
] & 0x1f;
94 tm
->tm_mon
= (data
[RTC_MONTH
] & 0x0f) - 1;
96 tm
->tm_year
= data
[RTC_YEAR
] & 0xff;
101 static int max77802_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
;
114 static int max77802_rtc_update(struct max77802_rtc_info
*info
,
115 enum MAX77802_RTC_OP op
)
120 if (op
== MAX77802_RTC_WRITE
)
121 data
= 1 << RTC_UDR_SHIFT
;
123 data
= 1 << RTC_RBUDR_SHIFT
;
125 ret
= regmap_update_bits(info
->max77802
->regmap
,
126 MAX77802_RTC_UPDATE0
, data
, data
);
128 dev_err(info
->dev
, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
129 __func__
, ret
, data
);
131 /* Minimum delay required before RTC update. */
132 usleep_range(MAX77802_RTC_UPDATE_DELAY_US
,
133 MAX77802_RTC_UPDATE_DELAY_US
* 2);
139 static int max77802_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
141 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
142 u8 data
[RTC_NR_TIME
];
145 mutex_lock(&info
->lock
);
147 ret
= max77802_rtc_update(info
, MAX77802_RTC_READ
);
151 ret
= regmap_bulk_read(info
->max77802
->regmap
,
152 MAX77802_RTC_SEC
, data
, RTC_NR_TIME
);
154 dev_err(info
->dev
, "%s: fail to read time reg(%d)\n", __func__
,
159 max77802_rtc_data_to_tm(data
, tm
, info
->rtc_24hr_mode
);
161 ret
= rtc_valid_tm(tm
);
164 mutex_unlock(&info
->lock
);
168 static int max77802_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
170 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
171 u8 data
[RTC_NR_TIME
];
174 ret
= max77802_rtc_tm_to_data(tm
, data
);
178 mutex_lock(&info
->lock
);
180 ret
= regmap_bulk_write(info
->max77802
->regmap
,
181 MAX77802_RTC_SEC
, data
, RTC_NR_TIME
);
183 dev_err(info
->dev
, "%s: fail to write time reg(%d)\n", __func__
,
188 ret
= max77802_rtc_update(info
, MAX77802_RTC_WRITE
);
191 mutex_unlock(&info
->lock
);
195 static int max77802_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
197 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
198 u8 data
[RTC_NR_TIME
];
202 mutex_lock(&info
->lock
);
204 ret
= max77802_rtc_update(info
, MAX77802_RTC_READ
);
208 ret
= regmap_bulk_read(info
->max77802
->regmap
,
209 MAX77802_ALARM1_SEC
, data
, RTC_NR_TIME
);
211 dev_err(info
->dev
, "%s:%d fail to read alarm reg(%d)\n",
212 __func__
, __LINE__
, ret
);
216 max77802_rtc_data_to_tm(data
, &alrm
->time
, info
->rtc_24hr_mode
);
219 ret
= regmap_read(info
->max77802
->regmap
,
220 MAX77802_RTC_AE1
, &val
);
222 dev_err(info
->dev
, "%s:%d fail to read alarm enable(%d)\n",
223 __func__
, __LINE__
, ret
);
230 ret
= regmap_read(info
->max77802
->regmap
, MAX77802_REG_STATUS2
, &val
);
232 dev_err(info
->dev
, "%s:%d fail to read status2 reg(%d)\n",
233 __func__
, __LINE__
, ret
);
237 if (val
& (1 << 2)) /* RTCA1 */
241 mutex_unlock(&info
->lock
);
245 static int max77802_rtc_stop_alarm(struct max77802_rtc_info
*info
)
249 if (!mutex_is_locked(&info
->lock
))
250 dev_warn(info
->dev
, "%s: should have mutex locked\n", __func__
);
252 ret
= max77802_rtc_update(info
, MAX77802_RTC_READ
);
256 ret
= regmap_write(info
->max77802
->regmap
,
257 MAX77802_RTC_AE1
, 0);
259 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
264 ret
= max77802_rtc_update(info
, MAX77802_RTC_WRITE
);
269 static int max77802_rtc_start_alarm(struct max77802_rtc_info
*info
)
273 if (!mutex_is_locked(&info
->lock
))
274 dev_warn(info
->dev
, "%s: should have mutex locked\n",
277 ret
= max77802_rtc_update(info
, MAX77802_RTC_READ
);
281 ret
= regmap_write(info
->max77802
->regmap
,
286 dev_err(info
->dev
, "%s: fail to read alarm reg(%d)\n",
291 ret
= max77802_rtc_update(info
, MAX77802_RTC_WRITE
);
296 static int max77802_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
298 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
299 u8 data
[RTC_NR_TIME
];
302 ret
= max77802_rtc_tm_to_data(&alrm
->time
, data
);
306 mutex_lock(&info
->lock
);
308 ret
= max77802_rtc_stop_alarm(info
);
312 ret
= regmap_bulk_write(info
->max77802
->regmap
,
313 MAX77802_ALARM1_SEC
, data
, RTC_NR_TIME
);
316 dev_err(info
->dev
, "%s: fail to write alarm reg(%d)\n",
321 ret
= max77802_rtc_update(info
, MAX77802_RTC_WRITE
);
326 ret
= max77802_rtc_start_alarm(info
);
328 mutex_unlock(&info
->lock
);
332 static int max77802_rtc_alarm_irq_enable(struct device
*dev
,
333 unsigned int enabled
)
335 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
338 mutex_lock(&info
->lock
);
340 ret
= max77802_rtc_start_alarm(info
);
342 ret
= max77802_rtc_stop_alarm(info
);
343 mutex_unlock(&info
->lock
);
348 static irqreturn_t
max77802_rtc_alarm_irq(int irq
, void *data
)
350 struct max77802_rtc_info
*info
= data
;
352 dev_dbg(info
->dev
, "%s:irq(%d)\n", __func__
, irq
);
354 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
359 static const struct rtc_class_ops max77802_rtc_ops
= {
360 .read_time
= max77802_rtc_read_time
,
361 .set_time
= max77802_rtc_set_time
,
362 .read_alarm
= max77802_rtc_read_alarm
,
363 .set_alarm
= max77802_rtc_set_alarm
,
364 .alarm_irq_enable
= max77802_rtc_alarm_irq_enable
,
367 static int max77802_rtc_init_reg(struct max77802_rtc_info
*info
)
372 max77802_rtc_update(info
, MAX77802_RTC_READ
);
374 /* Set RTC control register : Binary mode, 24hour mdoe */
375 data
[0] = (1 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
376 data
[1] = (0 << BCD_EN_SHIFT
) | (1 << MODEL24_SHIFT
);
378 info
->rtc_24hr_mode
= 1;
380 ret
= regmap_bulk_write(info
->max77802
->regmap
,
381 MAX77802_RTC_CONTROLM
, data
, ARRAY_SIZE(data
));
383 dev_err(info
->dev
, "%s: fail to write controlm reg(%d)\n",
388 ret
= max77802_rtc_update(info
, MAX77802_RTC_WRITE
);
392 static int max77802_rtc_probe(struct platform_device
*pdev
)
394 struct max77686_dev
*max77802
= dev_get_drvdata(pdev
->dev
.parent
);
395 struct max77802_rtc_info
*info
;
398 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
400 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max77802_rtc_info
),
405 mutex_init(&info
->lock
);
406 info
->dev
= &pdev
->dev
;
407 info
->max77802
= max77802
;
408 info
->rtc
= max77802
->i2c
;
410 platform_set_drvdata(pdev
, info
);
412 ret
= max77802_rtc_init_reg(info
);
415 dev_err(&pdev
->dev
, "Failed to initialize RTC reg:%d\n", ret
);
419 device_init_wakeup(&pdev
->dev
, 1);
421 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max77802-rtc",
422 &max77802_rtc_ops
, THIS_MODULE
);
424 if (IS_ERR(info
->rtc_dev
)) {
425 ret
= PTR_ERR(info
->rtc_dev
);
426 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
432 if (!max77802
->rtc_irq_data
) {
433 dev_err(&pdev
->dev
, "No RTC regmap IRQ chip\n");
437 info
->virq
= regmap_irq_get_virq(max77802
->rtc_irq_data
,
438 MAX77686_RTCIRQ_RTCA1
);
440 if (info
->virq
<= 0) {
441 dev_err(&pdev
->dev
, "Failed to get virtual IRQ %d\n",
442 MAX77686_RTCIRQ_RTCA1
);
446 ret
= devm_request_threaded_irq(&pdev
->dev
, info
->virq
, NULL
,
447 max77802_rtc_alarm_irq
, 0, "rtc-alarm1",
450 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
456 #ifdef CONFIG_PM_SLEEP
457 static int max77802_rtc_suspend(struct device
*dev
)
459 if (device_may_wakeup(dev
)) {
460 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
462 return enable_irq_wake(info
->virq
);
468 static int max77802_rtc_resume(struct device
*dev
)
470 if (device_may_wakeup(dev
)) {
471 struct max77802_rtc_info
*info
= dev_get_drvdata(dev
);
473 return disable_irq_wake(info
->virq
);
480 static SIMPLE_DEV_PM_OPS(max77802_rtc_pm_ops
,
481 max77802_rtc_suspend
, max77802_rtc_resume
);
483 static const struct platform_device_id rtc_id
[] = {
484 { "max77802-rtc", 0 },
487 MODULE_DEVICE_TABLE(platform
, rtc_id
);
489 static struct platform_driver max77802_rtc_driver
= {
491 .name
= "max77802-rtc",
492 .pm
= &max77802_rtc_pm_ops
,
494 .probe
= max77802_rtc_probe
,
498 module_platform_driver(max77802_rtc_driver
);
500 MODULE_DESCRIPTION("Maxim MAX77802 RTC driver");
501 MODULE_AUTHOR("Simon Glass <sjg@chromium.org>");
502 MODULE_LICENSE("GPL");