2 * RTC driver for Maxim MAX8998
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Minkyu Kang <mk7.kang@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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/module.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/bcd.h>
19 #include <linux/rtc.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/max8998.h>
22 #include <linux/mfd/max8998-private.h>
23 #include <linux/delay.h>
25 #define MAX8998_RTC_SEC 0x00
26 #define MAX8998_RTC_MIN 0x01
27 #define MAX8998_RTC_HOUR 0x02
28 #define MAX8998_RTC_WEEKDAY 0x03
29 #define MAX8998_RTC_DATE 0x04
30 #define MAX8998_RTC_MONTH 0x05
31 #define MAX8998_RTC_YEAR1 0x06
32 #define MAX8998_RTC_YEAR2 0x07
33 #define MAX8998_ALARM0_SEC 0x08
34 #define MAX8998_ALARM0_MIN 0x09
35 #define MAX8998_ALARM0_HOUR 0x0a
36 #define MAX8998_ALARM0_WEEKDAY 0x0b
37 #define MAX8998_ALARM0_DATE 0x0c
38 #define MAX8998_ALARM0_MONTH 0x0d
39 #define MAX8998_ALARM0_YEAR1 0x0e
40 #define MAX8998_ALARM0_YEAR2 0x0f
41 #define MAX8998_ALARM1_SEC 0x10
42 #define MAX8998_ALARM1_MIN 0x11
43 #define MAX8998_ALARM1_HOUR 0x12
44 #define MAX8998_ALARM1_WEEKDAY 0x13
45 #define MAX8998_ALARM1_DATE 0x14
46 #define MAX8998_ALARM1_MONTH 0x15
47 #define MAX8998_ALARM1_YEAR1 0x16
48 #define MAX8998_ALARM1_YEAR2 0x17
49 #define MAX8998_ALARM0_CONF 0x18
50 #define MAX8998_ALARM1_CONF 0x19
51 #define MAX8998_RTC_STATUS 0x1a
52 #define MAX8998_WTSR_SMPL_CNTL 0x1b
53 #define MAX8998_TEST 0x1f
55 #define HOUR_12 (1 << 7)
56 #define HOUR_PM (1 << 5)
57 #define ALARM0_STATUS (1 << 1)
58 #define ALARM1_STATUS (1 << 2)
71 struct max8998_rtc_info
{
73 struct max8998_dev
*max8998
;
74 struct i2c_client
*rtc
;
75 struct rtc_device
*rtc_dev
;
77 bool lp3974_bug_workaround
;
80 static void max8998_data_to_tm(u8
*data
, struct rtc_time
*tm
)
82 tm
->tm_sec
= bcd2bin(data
[RTC_SEC
]);
83 tm
->tm_min
= bcd2bin(data
[RTC_MIN
]);
84 if (data
[RTC_HOUR
] & HOUR_12
) {
85 tm
->tm_hour
= bcd2bin(data
[RTC_HOUR
] & 0x1f);
86 if (data
[RTC_HOUR
] & HOUR_PM
)
89 tm
->tm_hour
= bcd2bin(data
[RTC_HOUR
] & 0x3f);
91 tm
->tm_wday
= data
[RTC_WEEKDAY
] & 0x07;
92 tm
->tm_mday
= bcd2bin(data
[RTC_DATE
]);
93 tm
->tm_mon
= bcd2bin(data
[RTC_MONTH
]);
94 tm
->tm_year
= bcd2bin(data
[RTC_YEAR1
]) + bcd2bin(data
[RTC_YEAR2
]) * 100;
98 static void max8998_tm_to_data(struct rtc_time
*tm
, u8
*data
)
100 data
[RTC_SEC
] = bin2bcd(tm
->tm_sec
);
101 data
[RTC_MIN
] = bin2bcd(tm
->tm_min
);
102 data
[RTC_HOUR
] = bin2bcd(tm
->tm_hour
);
103 data
[RTC_WEEKDAY
] = tm
->tm_wday
;
104 data
[RTC_DATE
] = bin2bcd(tm
->tm_mday
);
105 data
[RTC_MONTH
] = bin2bcd(tm
->tm_mon
);
106 data
[RTC_YEAR1
] = bin2bcd(tm
->tm_year
% 100);
107 data
[RTC_YEAR2
] = bin2bcd((tm
->tm_year
+ 1900) / 100);
110 static int max8998_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
112 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
116 ret
= max8998_bulk_read(info
->rtc
, MAX8998_RTC_SEC
, 8, data
);
120 max8998_data_to_tm(data
, tm
);
122 return rtc_valid_tm(tm
);
125 static int max8998_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
127 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
131 max8998_tm_to_data(tm
, data
);
133 ret
= max8998_bulk_write(info
->rtc
, MAX8998_RTC_SEC
, 8, data
);
135 if (info
->lp3974_bug_workaround
)
141 static int max8998_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
143 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
148 ret
= max8998_bulk_read(info
->rtc
, MAX8998_ALARM0_SEC
, 8, data
);
152 max8998_data_to_tm(data
, &alrm
->time
);
154 ret
= max8998_read_reg(info
->rtc
, MAX8998_ALARM0_CONF
, &val
);
158 alrm
->enabled
= !!val
;
160 ret
= max8998_read_reg(info
->rtc
, MAX8998_RTC_STATUS
, &val
);
164 if (val
& ALARM0_STATUS
)
172 static int max8998_rtc_stop_alarm(struct max8998_rtc_info
*info
)
174 int ret
= max8998_write_reg(info
->rtc
, MAX8998_ALARM0_CONF
, 0);
176 if (info
->lp3974_bug_workaround
)
182 static int max8998_rtc_start_alarm(struct max8998_rtc_info
*info
)
185 u8 alarm0_conf
= 0x77;
187 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
188 if (info
->lp3974_bug_workaround
)
191 ret
= max8998_write_reg(info
->rtc
, MAX8998_ALARM0_CONF
, alarm0_conf
);
193 if (info
->lp3974_bug_workaround
)
199 static int max8998_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
201 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
205 max8998_tm_to_data(&alrm
->time
, data
);
207 ret
= max8998_rtc_stop_alarm(info
);
211 ret
= max8998_bulk_write(info
->rtc
, MAX8998_ALARM0_SEC
, 8, data
);
215 if (info
->lp3974_bug_workaround
)
219 ret
= max8998_rtc_start_alarm(info
);
224 static int max8998_rtc_alarm_irq_enable(struct device
*dev
,
225 unsigned int enabled
)
227 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
230 return max8998_rtc_start_alarm(info
);
232 return max8998_rtc_stop_alarm(info
);
235 static irqreturn_t
max8998_rtc_alarm_irq(int irq
, void *data
)
237 struct max8998_rtc_info
*info
= data
;
239 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
244 static const struct rtc_class_ops max8998_rtc_ops
= {
245 .read_time
= max8998_rtc_read_time
,
246 .set_time
= max8998_rtc_set_time
,
247 .read_alarm
= max8998_rtc_read_alarm
,
248 .set_alarm
= max8998_rtc_set_alarm
,
249 .alarm_irq_enable
= max8998_rtc_alarm_irq_enable
,
252 static int __devinit
max8998_rtc_probe(struct platform_device
*pdev
)
254 struct max8998_dev
*max8998
= dev_get_drvdata(pdev
->dev
.parent
);
255 struct max8998_platform_data
*pdata
= dev_get_platdata(max8998
->dev
);
256 struct max8998_rtc_info
*info
;
259 info
= kzalloc(sizeof(struct max8998_rtc_info
), GFP_KERNEL
);
263 info
->dev
= &pdev
->dev
;
264 info
->max8998
= max8998
;
265 info
->rtc
= max8998
->rtc
;
266 info
->irq
= max8998
->irq_base
+ MAX8998_IRQ_ALARM0
;
268 platform_set_drvdata(pdev
, info
);
270 info
->rtc_dev
= rtc_device_register("max8998-rtc", &pdev
->dev
,
271 &max8998_rtc_ops
, THIS_MODULE
);
273 if (IS_ERR(info
->rtc_dev
)) {
274 ret
= PTR_ERR(info
->rtc_dev
);
275 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
279 ret
= request_threaded_irq(info
->irq
, NULL
, max8998_rtc_alarm_irq
, 0,
283 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
286 dev_info(&pdev
->dev
, "RTC CHIP NAME: %s\n", pdev
->id_entry
->name
);
287 if (pdata
->rtc_delay
) {
288 info
->lp3974_bug_workaround
= true;
289 dev_warn(&pdev
->dev
, "LP3974 with RTC REGERR option."
290 " RTC updates will be extremely slow.\n");
296 platform_set_drvdata(pdev
, NULL
);
301 static int __devexit
max8998_rtc_remove(struct platform_device
*pdev
)
303 struct max8998_rtc_info
*info
= platform_get_drvdata(pdev
);
306 free_irq(info
->irq
, info
);
307 rtc_device_unregister(info
->rtc_dev
);
314 static const struct platform_device_id max8998_rtc_id
[] = {
315 { "max8998-rtc", TYPE_MAX8998
},
316 { "lp3974-rtc", TYPE_LP3974
},
320 static struct platform_driver max8998_rtc_driver
= {
322 .name
= "max8998-rtc",
323 .owner
= THIS_MODULE
,
325 .probe
= max8998_rtc_probe
,
326 .remove
= __devexit_p(max8998_rtc_remove
),
327 .id_table
= max8998_rtc_id
,
330 static int __init
max8998_rtc_init(void)
332 return platform_driver_register(&max8998_rtc_driver
);
334 module_init(max8998_rtc_init
);
336 static void __exit
max8998_rtc_exit(void)
338 platform_driver_unregister(&max8998_rtc_driver
);
340 module_exit(max8998_rtc_exit
);
342 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
343 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
344 MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
345 MODULE_LICENSE("GPL");