of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / rtc / rtc-max8997.c
blobdb984d4bf9526bbc78e501ff8da6a7036801872b
1 /*
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 */
44 #define WTSRT_SHIFT 0
45 #define SMPLT_SHIFT 2
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)
59 enum {
60 RTC_SEC = 0,
61 RTC_MIN,
62 RTC_HOUR,
63 RTC_WEEKDAY,
64 RTC_MONTH,
65 RTC_YEAR,
66 RTC_DATE,
67 RTC_NR_TIME
70 struct max8997_rtc_info {
71 struct device *dev;
72 struct max8997_dev *max8997;
73 struct i2c_client *rtc;
74 struct rtc_device *rtc_dev;
75 struct mutex lock;
76 int virq;
77 int rtc_24hr_mode;
80 static void max8997_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
81 int rtc_24hr_mode)
83 tm->tm_sec = data[RTC_SEC] & 0x7f;
84 tm->tm_min = data[RTC_MIN] & 0x7f;
85 if (rtc_24hr_mode)
86 tm->tm_hour = data[RTC_HOUR] & 0x1f;
87 else {
88 tm->tm_hour = data[RTC_HOUR] & 0x0f;
89 if (data[RTC_HOUR] & HOUR_PM_MASK)
90 tm->tm_hour += 12;
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;
97 tm->tm_yday = 0;
98 tm->tm_isdst = 0;
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",
113 1900 + tm->tm_year);
114 return -EINVAL;
116 return 0;
119 static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info *info)
121 int ret;
123 ret = max8997_write_reg(info->rtc, MAX8997_RTC_UPDATE1,
124 RTC_UDR_MASK);
125 if (ret < 0)
126 dev_err(info->dev, "%s: fail to write update reg(%d)\n",
127 __func__, ret);
128 else {
129 /* Minimum 16ms delay required before RTC update.
130 * Otherwise, we may read and update based on out-of-date
131 * value */
132 msleep(20);
135 return ret;
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];
142 int ret;
144 mutex_lock(&info->lock);
145 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
146 mutex_unlock(&info->lock);
148 if (ret < 0) {
149 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
150 ret);
151 return ret;
154 max8997_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
156 return rtc_valid_tm(tm);
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];
163 int ret;
165 ret = max8997_rtc_tm_to_data(tm, data);
166 if (ret < 0)
167 return ret;
169 mutex_lock(&info->lock);
171 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
172 if (ret < 0) {
173 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
174 ret);
175 goto out;
178 ret = max8997_rtc_set_update_reg(info);
179 out:
180 mutex_unlock(&info->lock);
181 return ret;
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];
188 u8 val;
189 int i, ret;
191 mutex_lock(&info->lock);
193 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
194 data);
195 if (ret < 0) {
196 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
197 __func__, __LINE__, ret);
198 goto out;
201 max8997_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
203 alrm->enabled = 0;
204 for (i = 0; i < RTC_NR_TIME; i++) {
205 if (data[i] & ALARM_ENABLE_MASK) {
206 alrm->enabled = 1;
207 break;
211 alrm->pending = 0;
212 ret = max8997_read_reg(info->max8997->i2c, MAX8997_REG_STATUS1, &val);
213 if (ret < 0) {
214 dev_err(info->dev, "%s:%d fail to read status1 reg(%d)\n",
215 __func__, __LINE__, ret);
216 goto out;
219 if (val & (1 << 4)) /* RTCA1 */
220 alrm->pending = 1;
222 out:
223 mutex_unlock(&info->lock);
224 return 0;
227 static int max8997_rtc_stop_alarm(struct max8997_rtc_info *info)
229 u8 data[RTC_NR_TIME];
230 int ret, i;
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,
236 data);
237 if (ret < 0) {
238 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
239 __func__, ret);
240 goto out;
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,
247 data);
248 if (ret < 0) {
249 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
250 __func__, ret);
251 goto out;
254 ret = max8997_rtc_set_update_reg(info);
255 out:
256 return ret;
259 static int max8997_rtc_start_alarm(struct max8997_rtc_info *info)
261 u8 data[RTC_NR_TIME];
262 int ret;
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,
268 data);
269 if (ret < 0) {
270 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
271 __func__, ret);
272 goto out;
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,
287 data);
288 if (ret < 0) {
289 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
290 __func__, ret);
291 goto out;
294 ret = max8997_rtc_set_update_reg(info);
295 out:
296 return ret;
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];
302 int ret;
304 ret = max8997_rtc_tm_to_data(&alrm->time, data);
305 if (ret < 0)
306 return ret;
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);
315 if (ret < 0)
316 goto out;
318 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
319 data);
320 if (ret < 0) {
321 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
322 __func__, ret);
323 goto out;
326 ret = max8997_rtc_set_update_reg(info);
327 if (ret < 0)
328 goto out;
330 if (alrm->enabled)
331 ret = max8997_rtc_start_alarm(info);
332 out:
333 mutex_unlock(&info->lock);
334 return ret;
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);
341 int ret;
343 mutex_lock(&info->lock);
344 if (enabled)
345 ret = max8997_rtc_start_alarm(info);
346 else
347 ret = max8997_rtc_stop_alarm(info);
348 mutex_unlock(&info->lock);
350 return ret;
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);
361 return IRQ_HANDLED;
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)
374 int ret;
375 u8 val, mask;
377 if (!wtsr_en)
378 return;
380 if (enable)
381 val = (1 << WTSR_EN_SHIFT) | (3 << WTSRT_SHIFT);
382 else
383 val = 0;
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);
391 if (ret < 0) {
392 dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
393 __func__, ret);
394 return;
397 max8997_rtc_set_update_reg(info);
400 static void max8997_rtc_enable_smpl(struct max8997_rtc_info *info, bool enable)
402 int ret;
403 u8 val, mask;
405 if (!smpl_en)
406 return;
408 if (enable)
409 val = (1 << SMPL_EN_SHIFT) | (0 << SMPLT_SHIFT);
410 else
411 val = 0;
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);
419 if (ret < 0) {
420 dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
421 __func__, ret);
422 return;
425 max8997_rtc_set_update_reg(info);
427 val = 0;
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)
434 u8 data[2];
435 int ret;
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);
444 if (ret < 0) {
445 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
446 __func__, ret);
447 return ret;
450 ret = max8997_rtc_set_update_reg(info);
451 return ret;
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;
458 int ret, virq;
460 info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_rtc_info),
461 GFP_KERNEL);
462 if (!info)
463 return -ENOMEM;
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);
474 if (ret < 0) {
475 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
476 return 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);
490 return ret;
493 virq = irq_create_mapping(max8997->irq_domain, MAX8997_PMICIRQ_RTCA1);
494 if (!virq) {
495 dev_err(&pdev->dev, "Failed to create mapping alarm IRQ\n");
496 ret = -ENXIO;
497 goto err_out;
499 info->virq = virq;
501 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
502 max8997_rtc_alarm_irq, 0,
503 "rtc-alarm0", info);
504 if (ret < 0)
505 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
506 info->virq, ret);
508 err_out:
509 return ret;
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 = {
527 .driver = {
528 .name = "max8997-rtc",
530 .probe = max8997_rtc_probe,
531 .shutdown = max8997_rtc_shutdown,
532 .id_table = rtc_id,
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");