tracing/kprobes: Support soft-mode disabling
[linux-2.6.git] / drivers / rtc / rtc-max8997.c
blob5693619614f48932f8676a648ba3790faded6419
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 #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/max8997-private.h>
22 #include <linux/irqdomain.h>
24 /* Module parameter for WTSR function control */
25 static int wtsr_en = 1;
26 module_param(wtsr_en, int, 0444);
27 MODULE_PARM_DESC(wtsr_en, "Wachdog Timeout & Sofware Reset (default=on)");
28 /* Module parameter for SMPL function control */
29 static int smpl_en = 1;
30 module_param(smpl_en, int, 0444);
31 MODULE_PARM_DESC(smpl_en, "Sudden Momentary Power Loss (default=on)");
33 /* RTC Control Register */
34 #define BCD_EN_SHIFT 0
35 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
36 #define MODEL24_SHIFT 1
37 #define MODEL24_MASK (1 << MODEL24_SHIFT)
38 /* RTC Update Register1 */
39 #define RTC_UDR_SHIFT 0
40 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
41 /* WTSR and SMPL Register */
42 #define WTSRT_SHIFT 0
43 #define SMPLT_SHIFT 2
44 #define WTSR_EN_SHIFT 6
45 #define SMPL_EN_SHIFT 7
46 #define WTSRT_MASK (3 << WTSRT_SHIFT)
47 #define SMPLT_MASK (3 << SMPLT_SHIFT)
48 #define WTSR_EN_MASK (1 << WTSR_EN_SHIFT)
49 #define SMPL_EN_MASK (1 << SMPL_EN_SHIFT)
50 /* RTC Hour register */
51 #define HOUR_PM_SHIFT 6
52 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
53 /* RTC Alarm Enable */
54 #define ALARM_ENABLE_SHIFT 7
55 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
57 enum {
58 RTC_SEC = 0,
59 RTC_MIN,
60 RTC_HOUR,
61 RTC_WEEKDAY,
62 RTC_MONTH,
63 RTC_YEAR,
64 RTC_DATE,
65 RTC_NR_TIME
68 struct max8997_rtc_info {
69 struct device *dev;
70 struct max8997_dev *max8997;
71 struct i2c_client *rtc;
72 struct rtc_device *rtc_dev;
73 struct mutex lock;
74 int virq;
75 int rtc_24hr_mode;
78 static void max8997_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
79 int rtc_24hr_mode)
81 tm->tm_sec = data[RTC_SEC] & 0x7f;
82 tm->tm_min = data[RTC_MIN] & 0x7f;
83 if (rtc_24hr_mode)
84 tm->tm_hour = data[RTC_HOUR] & 0x1f;
85 else {
86 tm->tm_hour = data[RTC_HOUR] & 0x0f;
87 if (data[RTC_HOUR] & HOUR_PM_MASK)
88 tm->tm_hour += 12;
91 tm->tm_wday = fls(data[RTC_WEEKDAY] & 0x7f) - 1;
92 tm->tm_mday = data[RTC_DATE] & 0x1f;
93 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
94 tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
95 tm->tm_yday = 0;
96 tm->tm_isdst = 0;
99 static int max8997_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
101 data[RTC_SEC] = tm->tm_sec;
102 data[RTC_MIN] = tm->tm_min;
103 data[RTC_HOUR] = tm->tm_hour;
104 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
105 data[RTC_DATE] = tm->tm_mday;
106 data[RTC_MONTH] = tm->tm_mon + 1;
107 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0 ;
109 if (tm->tm_year < 100) {
110 pr_warn("%s: MAX8997 RTC cannot handle the year %d."
111 "Assume it's 2000.\n", __func__, 1900 + tm->tm_year);
112 return -EINVAL;
114 return 0;
117 static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info *info)
119 int ret;
121 ret = max8997_write_reg(info->rtc, MAX8997_RTC_UPDATE1,
122 RTC_UDR_MASK);
123 if (ret < 0)
124 dev_err(info->dev, "%s: fail to write update reg(%d)\n",
125 __func__, ret);
126 else {
127 /* Minimum 16ms delay required before RTC update.
128 * Otherwise, we may read and update based on out-of-date
129 * value */
130 msleep(20);
133 return ret;
136 static int max8997_rtc_read_time(struct device *dev, struct rtc_time *tm)
138 struct max8997_rtc_info *info = dev_get_drvdata(dev);
139 u8 data[RTC_NR_TIME];
140 int ret;
142 mutex_lock(&info->lock);
143 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
144 mutex_unlock(&info->lock);
146 if (ret < 0) {
147 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
148 ret);
149 return ret;
152 max8997_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
154 return rtc_valid_tm(tm);
157 static int max8997_rtc_set_time(struct device *dev, struct rtc_time *tm)
159 struct max8997_rtc_info *info = dev_get_drvdata(dev);
160 u8 data[RTC_NR_TIME];
161 int ret;
163 ret = max8997_rtc_tm_to_data(tm, data);
164 if (ret < 0)
165 return ret;
167 mutex_lock(&info->lock);
169 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
170 if (ret < 0) {
171 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
172 ret);
173 goto out;
176 ret = max8997_rtc_set_update_reg(info);
177 out:
178 mutex_unlock(&info->lock);
179 return ret;
182 static int max8997_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
184 struct max8997_rtc_info *info = dev_get_drvdata(dev);
185 u8 data[RTC_NR_TIME];
186 u8 val;
187 int i, ret;
189 mutex_lock(&info->lock);
191 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
192 data);
193 if (ret < 0) {
194 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
195 __func__, __LINE__, ret);
196 goto out;
199 max8997_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
201 alrm->enabled = 0;
202 for (i = 0; i < RTC_NR_TIME; i++) {
203 if (data[i] & ALARM_ENABLE_MASK) {
204 alrm->enabled = 1;
205 break;
209 alrm->pending = 0;
210 ret = max8997_read_reg(info->max8997->i2c, MAX8997_REG_STATUS1, &val);
211 if (ret < 0) {
212 dev_err(info->dev, "%s:%d fail to read status1 reg(%d)\n",
213 __func__, __LINE__, ret);
214 goto out;
217 if (val & (1 << 4)) /* RTCA1 */
218 alrm->pending = 1;
220 out:
221 mutex_unlock(&info->lock);
222 return 0;
225 static int max8997_rtc_stop_alarm(struct max8997_rtc_info *info)
227 u8 data[RTC_NR_TIME];
228 int ret, i;
230 if (!mutex_is_locked(&info->lock))
231 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
233 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
234 data);
235 if (ret < 0) {
236 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
237 __func__, ret);
238 goto out;
241 for (i = 0; i < RTC_NR_TIME; i++)
242 data[i] &= ~ALARM_ENABLE_MASK;
244 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
245 data);
246 if (ret < 0) {
247 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
248 __func__, ret);
249 goto out;
252 ret = max8997_rtc_set_update_reg(info);
253 out:
254 return ret;
257 static int max8997_rtc_start_alarm(struct max8997_rtc_info *info)
259 u8 data[RTC_NR_TIME];
260 int ret;
262 if (!mutex_is_locked(&info->lock))
263 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
265 ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
266 data);
267 if (ret < 0) {
268 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
269 __func__, ret);
270 goto out;
273 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
274 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
275 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
276 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
277 if (data[RTC_MONTH] & 0xf)
278 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
279 if (data[RTC_YEAR] & 0x7f)
280 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
281 if (data[RTC_DATE] & 0x1f)
282 data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
284 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
285 data);
286 if (ret < 0) {
287 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
288 __func__, ret);
289 goto out;
292 ret = max8997_rtc_set_update_reg(info);
293 out:
294 return ret;
296 static int max8997_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
298 struct max8997_rtc_info *info = dev_get_drvdata(dev);
299 u8 data[RTC_NR_TIME];
300 int ret;
302 ret = max8997_rtc_tm_to_data(&alrm->time, data);
303 if (ret < 0)
304 return ret;
306 dev_info(info->dev, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__,
307 data[RTC_YEAR] + 2000, data[RTC_MONTH], data[RTC_DATE],
308 data[RTC_HOUR], data[RTC_MIN], data[RTC_SEC]);
310 mutex_lock(&info->lock);
312 ret = max8997_rtc_stop_alarm(info);
313 if (ret < 0)
314 goto out;
316 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
317 data);
318 if (ret < 0) {
319 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
320 __func__, ret);
321 goto out;
324 ret = max8997_rtc_set_update_reg(info);
325 if (ret < 0)
326 goto out;
328 if (alrm->enabled)
329 ret = max8997_rtc_start_alarm(info);
330 out:
331 mutex_unlock(&info->lock);
332 return ret;
335 static int max8997_rtc_alarm_irq_enable(struct device *dev,
336 unsigned int enabled)
338 struct max8997_rtc_info *info = dev_get_drvdata(dev);
339 int ret;
341 mutex_lock(&info->lock);
342 if (enabled)
343 ret = max8997_rtc_start_alarm(info);
344 else
345 ret = max8997_rtc_stop_alarm(info);
346 mutex_unlock(&info->lock);
348 return ret;
351 static irqreturn_t max8997_rtc_alarm_irq(int irq, void *data)
353 struct max8997_rtc_info *info = data;
355 dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
357 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
359 return IRQ_HANDLED;
362 static const struct rtc_class_ops max8997_rtc_ops = {
363 .read_time = max8997_rtc_read_time,
364 .set_time = max8997_rtc_set_time,
365 .read_alarm = max8997_rtc_read_alarm,
366 .set_alarm = max8997_rtc_set_alarm,
367 .alarm_irq_enable = max8997_rtc_alarm_irq_enable,
370 static void max8997_rtc_enable_wtsr(struct max8997_rtc_info *info, bool enable)
372 int ret;
373 u8 val, mask;
375 if (!wtsr_en)
376 return;
378 if (enable)
379 val = (1 << WTSR_EN_SHIFT) | (3 << WTSRT_SHIFT);
380 else
381 val = 0;
383 mask = WTSR_EN_MASK | WTSRT_MASK;
385 dev_info(info->dev, "%s: %s WTSR\n", __func__,
386 enable ? "enable" : "disable");
388 ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
389 if (ret < 0) {
390 dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
391 __func__, ret);
392 return;
395 max8997_rtc_set_update_reg(info);
398 static void max8997_rtc_enable_smpl(struct max8997_rtc_info *info, bool enable)
400 int ret;
401 u8 val, mask;
403 if (!smpl_en)
404 return;
406 if (enable)
407 val = (1 << SMPL_EN_SHIFT) | (0 << SMPLT_SHIFT);
408 else
409 val = 0;
411 mask = SMPL_EN_MASK | SMPLT_MASK;
413 dev_info(info->dev, "%s: %s SMPL\n", __func__,
414 enable ? "enable" : "disable");
416 ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
417 if (ret < 0) {
418 dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
419 __func__, ret);
420 return;
423 max8997_rtc_set_update_reg(info);
425 val = 0;
426 max8997_read_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, &val);
427 pr_info("%s: WTSR_SMPL(0x%02x)\n", __func__, val);
430 static int max8997_rtc_init_reg(struct max8997_rtc_info *info)
432 u8 data[2];
433 int ret;
435 /* Set RTC control register : Binary mode, 24hour mdoe */
436 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
437 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
439 info->rtc_24hr_mode = 1;
441 ret = max8997_bulk_write(info->rtc, MAX8997_RTC_CTRLMASK, 2, data);
442 if (ret < 0) {
443 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
444 __func__, ret);
445 return ret;
448 ret = max8997_rtc_set_update_reg(info);
449 return ret;
452 static int max8997_rtc_probe(struct platform_device *pdev)
454 struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
455 struct max8997_rtc_info *info;
456 int ret, virq;
458 info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_rtc_info),
459 GFP_KERNEL);
460 if (!info)
461 return -ENOMEM;
463 mutex_init(&info->lock);
464 info->dev = &pdev->dev;
465 info->max8997 = max8997;
466 info->rtc = max8997->rtc;
468 platform_set_drvdata(pdev, info);
470 ret = max8997_rtc_init_reg(info);
472 if (ret < 0) {
473 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
474 return ret;
477 max8997_rtc_enable_wtsr(info, true);
478 max8997_rtc_enable_smpl(info, true);
480 device_init_wakeup(&pdev->dev, 1);
482 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8997-rtc",
483 &max8997_rtc_ops, THIS_MODULE);
485 if (IS_ERR(info->rtc_dev)) {
486 ret = PTR_ERR(info->rtc_dev);
487 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
488 return ret;
491 virq = irq_create_mapping(max8997->irq_domain, MAX8997_PMICIRQ_RTCA1);
492 if (!virq) {
493 dev_err(&pdev->dev, "Failed to create mapping alarm IRQ\n");
494 ret = -ENXIO;
495 goto err_out;
497 info->virq = virq;
499 ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
500 max8997_rtc_alarm_irq, 0,
501 "rtc-alarm0", info);
502 if (ret < 0)
503 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
504 info->virq, ret);
506 err_out:
507 return ret;
510 static int max8997_rtc_remove(struct platform_device *pdev)
512 return 0;
515 static void max8997_rtc_shutdown(struct platform_device *pdev)
517 struct max8997_rtc_info *info = platform_get_drvdata(pdev);
519 max8997_rtc_enable_wtsr(info, false);
520 max8997_rtc_enable_smpl(info, false);
523 static const struct platform_device_id rtc_id[] = {
524 { "max8997-rtc", 0 },
528 static struct platform_driver max8997_rtc_driver = {
529 .driver = {
530 .name = "max8997-rtc",
531 .owner = THIS_MODULE,
533 .probe = max8997_rtc_probe,
534 .remove = max8997_rtc_remove,
535 .shutdown = max8997_rtc_shutdown,
536 .id_table = rtc_id,
539 module_platform_driver(max8997_rtc_driver);
541 MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
542 MODULE_AUTHOR("<ms925.kim@samsung.com>");
543 MODULE_LICENSE("GPL");