1 // SPDX-License-Identifier: GPL-2.0+
3 // RTC driver for Maxim MAX8998
5 // Copyright (C) 2010 Samsung Electronics Co.Ltd
6 // Author: Minkyu Kang <mk7.kang@samsung.com>
7 // Author: Joonyoung Shim <jy0922.shim@samsung.com>
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/bcd.h>
13 #include <linux/irqdomain.h>
14 #include <linux/rtc.h>
15 #include <linux/platform_device.h>
16 #include <linux/mfd/max8998.h>
17 #include <linux/mfd/max8998-private.h>
18 #include <linux/delay.h>
20 #define MAX8998_RTC_SEC 0x00
21 #define MAX8998_RTC_MIN 0x01
22 #define MAX8998_RTC_HOUR 0x02
23 #define MAX8998_RTC_WEEKDAY 0x03
24 #define MAX8998_RTC_DATE 0x04
25 #define MAX8998_RTC_MONTH 0x05
26 #define MAX8998_RTC_YEAR1 0x06
27 #define MAX8998_RTC_YEAR2 0x07
28 #define MAX8998_ALARM0_SEC 0x08
29 #define MAX8998_ALARM0_MIN 0x09
30 #define MAX8998_ALARM0_HOUR 0x0a
31 #define MAX8998_ALARM0_WEEKDAY 0x0b
32 #define MAX8998_ALARM0_DATE 0x0c
33 #define MAX8998_ALARM0_MONTH 0x0d
34 #define MAX8998_ALARM0_YEAR1 0x0e
35 #define MAX8998_ALARM0_YEAR2 0x0f
36 #define MAX8998_ALARM1_SEC 0x10
37 #define MAX8998_ALARM1_MIN 0x11
38 #define MAX8998_ALARM1_HOUR 0x12
39 #define MAX8998_ALARM1_WEEKDAY 0x13
40 #define MAX8998_ALARM1_DATE 0x14
41 #define MAX8998_ALARM1_MONTH 0x15
42 #define MAX8998_ALARM1_YEAR1 0x16
43 #define MAX8998_ALARM1_YEAR2 0x17
44 #define MAX8998_ALARM0_CONF 0x18
45 #define MAX8998_ALARM1_CONF 0x19
46 #define MAX8998_RTC_STATUS 0x1a
47 #define MAX8998_WTSR_SMPL_CNTL 0x1b
48 #define MAX8998_TEST 0x1f
50 #define HOUR_12 (1 << 7)
51 #define HOUR_PM (1 << 5)
52 #define ALARM0_STATUS (1 << 1)
53 #define ALARM1_STATUS (1 << 2)
66 struct max8998_rtc_info
{
68 struct max8998_dev
*max8998
;
69 struct i2c_client
*rtc
;
70 struct rtc_device
*rtc_dev
;
72 bool lp3974_bug_workaround
;
75 static void max8998_data_to_tm(u8
*data
, struct rtc_time
*tm
)
77 tm
->tm_sec
= bcd2bin(data
[RTC_SEC
]);
78 tm
->tm_min
= bcd2bin(data
[RTC_MIN
]);
79 if (data
[RTC_HOUR
] & HOUR_12
) {
80 tm
->tm_hour
= bcd2bin(data
[RTC_HOUR
] & 0x1f);
81 if (data
[RTC_HOUR
] & HOUR_PM
)
84 tm
->tm_hour
= bcd2bin(data
[RTC_HOUR
] & 0x3f);
86 tm
->tm_wday
= data
[RTC_WEEKDAY
] & 0x07;
87 tm
->tm_mday
= bcd2bin(data
[RTC_DATE
]);
88 tm
->tm_mon
= bcd2bin(data
[RTC_MONTH
]);
89 tm
->tm_year
= bcd2bin(data
[RTC_YEAR1
]) + bcd2bin(data
[RTC_YEAR2
]) * 100;
93 static void max8998_tm_to_data(struct rtc_time
*tm
, u8
*data
)
95 data
[RTC_SEC
] = bin2bcd(tm
->tm_sec
);
96 data
[RTC_MIN
] = bin2bcd(tm
->tm_min
);
97 data
[RTC_HOUR
] = bin2bcd(tm
->tm_hour
);
98 data
[RTC_WEEKDAY
] = tm
->tm_wday
;
99 data
[RTC_DATE
] = bin2bcd(tm
->tm_mday
);
100 data
[RTC_MONTH
] = bin2bcd(tm
->tm_mon
);
101 data
[RTC_YEAR1
] = bin2bcd(tm
->tm_year
% 100);
102 data
[RTC_YEAR2
] = bin2bcd((tm
->tm_year
+ 1900) / 100);
105 static int max8998_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
107 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
111 ret
= max8998_bulk_read(info
->rtc
, MAX8998_RTC_SEC
, 8, data
);
115 max8998_data_to_tm(data
, tm
);
120 static int max8998_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
122 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
126 max8998_tm_to_data(tm
, data
);
128 ret
= max8998_bulk_write(info
->rtc
, MAX8998_RTC_SEC
, 8, data
);
130 if (info
->lp3974_bug_workaround
)
136 static int max8998_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
138 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
143 ret
= max8998_bulk_read(info
->rtc
, MAX8998_ALARM0_SEC
, 8, data
);
147 max8998_data_to_tm(data
, &alrm
->time
);
149 ret
= max8998_read_reg(info
->rtc
, MAX8998_ALARM0_CONF
, &val
);
153 alrm
->enabled
= !!val
;
155 ret
= max8998_read_reg(info
->rtc
, MAX8998_RTC_STATUS
, &val
);
159 if (val
& ALARM0_STATUS
)
167 static int max8998_rtc_stop_alarm(struct max8998_rtc_info
*info
)
169 int ret
= max8998_write_reg(info
->rtc
, MAX8998_ALARM0_CONF
, 0);
171 if (info
->lp3974_bug_workaround
)
177 static int max8998_rtc_start_alarm(struct max8998_rtc_info
*info
)
180 u8 alarm0_conf
= 0x77;
182 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
183 if (info
->lp3974_bug_workaround
)
186 ret
= max8998_write_reg(info
->rtc
, MAX8998_ALARM0_CONF
, alarm0_conf
);
188 if (info
->lp3974_bug_workaround
)
194 static int max8998_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
196 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
200 max8998_tm_to_data(&alrm
->time
, data
);
202 ret
= max8998_rtc_stop_alarm(info
);
206 ret
= max8998_bulk_write(info
->rtc
, MAX8998_ALARM0_SEC
, 8, data
);
210 if (info
->lp3974_bug_workaround
)
214 ret
= max8998_rtc_start_alarm(info
);
219 static int max8998_rtc_alarm_irq_enable(struct device
*dev
,
220 unsigned int enabled
)
222 struct max8998_rtc_info
*info
= dev_get_drvdata(dev
);
225 return max8998_rtc_start_alarm(info
);
227 return max8998_rtc_stop_alarm(info
);
230 static irqreturn_t
max8998_rtc_alarm_irq(int irq
, void *data
)
232 struct max8998_rtc_info
*info
= data
;
234 rtc_update_irq(info
->rtc_dev
, 1, RTC_IRQF
| RTC_AF
);
239 static const struct rtc_class_ops max8998_rtc_ops
= {
240 .read_time
= max8998_rtc_read_time
,
241 .set_time
= max8998_rtc_set_time
,
242 .read_alarm
= max8998_rtc_read_alarm
,
243 .set_alarm
= max8998_rtc_set_alarm
,
244 .alarm_irq_enable
= max8998_rtc_alarm_irq_enable
,
247 static int max8998_rtc_probe(struct platform_device
*pdev
)
249 struct max8998_dev
*max8998
= dev_get_drvdata(pdev
->dev
.parent
);
250 struct max8998_platform_data
*pdata
= max8998
->pdata
;
251 struct max8998_rtc_info
*info
;
254 info
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8998_rtc_info
),
259 info
->dev
= &pdev
->dev
;
260 info
->max8998
= max8998
;
261 info
->rtc
= max8998
->rtc
;
263 platform_set_drvdata(pdev
, info
);
265 info
->rtc_dev
= devm_rtc_device_register(&pdev
->dev
, "max8998-rtc",
266 &max8998_rtc_ops
, THIS_MODULE
);
268 if (IS_ERR(info
->rtc_dev
)) {
269 ret
= PTR_ERR(info
->rtc_dev
);
270 dev_err(&pdev
->dev
, "Failed to register RTC device: %d\n", ret
);
274 if (!max8998
->irq_domain
)
277 info
->irq
= irq_create_mapping(max8998
->irq_domain
, MAX8998_IRQ_ALARM0
);
279 dev_warn(&pdev
->dev
, "Failed to map alarm IRQ\n");
283 ret
= devm_request_threaded_irq(&pdev
->dev
, info
->irq
, NULL
,
284 max8998_rtc_alarm_irq
, 0, "rtc-alarm0", info
);
287 dev_err(&pdev
->dev
, "Failed to request alarm IRQ: %d: %d\n",
291 dev_info(&pdev
->dev
, "RTC CHIP NAME: %s\n", pdev
->id_entry
->name
);
292 if (pdata
&& pdata
->rtc_delay
) {
293 info
->lp3974_bug_workaround
= true;
294 dev_warn(&pdev
->dev
, "LP3974 with RTC REGERR option."
295 " RTC updates will be extremely slow.\n");
301 static const struct platform_device_id max8998_rtc_id
[] = {
302 { "max8998-rtc", TYPE_MAX8998
},
303 { "lp3974-rtc", TYPE_LP3974
},
306 MODULE_DEVICE_TABLE(platform
, max8998_rtc_id
);
308 static struct platform_driver max8998_rtc_driver
= {
310 .name
= "max8998-rtc",
312 .probe
= max8998_rtc_probe
,
313 .id_table
= max8998_rtc_id
,
316 module_platform_driver(max8998_rtc_driver
);
318 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
319 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
320 MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
321 MODULE_LICENSE("GPL");