2 * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd
3 * http://www.samsung.com
5 * Copyright (C) 2013 Google, Inc
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/i2c.h>
22 #include <linux/bcd.h>
23 #include <linux/regmap.h>
24 #include <linux/rtc.h>
25 #include <linux/platform_device.h>
26 #include <linux/mfd/samsung/core.h>
27 #include <linux/mfd/samsung/irq.h>
28 #include <linux/mfd/samsung/rtc.h>
29 #include <linux/mfd/samsung/s2mps14.h>
32 * Maximum number of retries for checking changes in UDR field
33 * of S5M_RTC_UDR_CON register (to limit possible endless loop).
35 * After writing to RTC registers (setting time or alarm) read the UDR field
36 * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have
39 #define UDR_READ_RETRY_CNT 5
41 /* Registers used by the driver which are different between chipsets. */
42 struct s5m_rtc_reg_config {
43 /* Number of registers used for setting time/alarm0/alarm1 */
44 unsigned int regs_count;
45 /* First register for time, seconds */
47 /* RTC control register */
49 /* First register for alarm 0, seconds */
51 /* First register for alarm 1, seconds */
54 * Register for update flag (UDR). Typically setting UDR field to 1
55 * will enable update of time or alarm register. Then it will be
56 * auto-cleared after successful update.
58 unsigned int rtc_udr_update;
59 /* Mask for UDR field in 'rtc_udr_update' register */
60 unsigned int rtc_udr_mask;
63 /* Register map for S5M8763 and S5M8767 */
64 static const struct s5m_rtc_reg_config s5m_rtc_regs = {
67 .ctrl = S5M_ALARM1_CONF,
68 .alarm0 = S5M_ALARM0_SEC,
69 .alarm1 = S5M_ALARM1_SEC,
70 .rtc_udr_update = S5M_RTC_UDR_CON,
71 .rtc_udr_mask = S5M_RTC_UDR_MASK,
75 * Register map for S2MPS14.
76 * It may be also suitable for S2MPS11 but this was not tested.
78 static const struct s5m_rtc_reg_config s2mps_rtc_regs = {
80 .time = S2MPS_RTC_SEC,
81 .ctrl = S2MPS_RTC_CTRL,
82 .alarm0 = S2MPS_ALARM0_SEC,
83 .alarm1 = S2MPS_ALARM1_SEC,
84 .rtc_udr_update = S2MPS_RTC_UDR_CON,
85 .rtc_udr_mask = S2MPS_RTC_WUDR_MASK,
90 struct i2c_client *i2c;
91 struct sec_pmic_dev *s5m87xx;
92 struct regmap *regmap;
93 struct rtc_device *rtc_dev;
95 enum sec_device_type device_type;
97 const struct s5m_rtc_reg_config *regs;
100 static const struct regmap_config s5m_rtc_regmap_config = {
104 .max_register = S5M_RTC_REG_MAX,
107 static const struct regmap_config s2mps14_rtc_regmap_config = {
111 .max_register = S2MPS_RTC_REG_MAX,
114 static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
117 tm->tm_sec = data[RTC_SEC] & 0x7f;
118 tm->tm_min = data[RTC_MIN] & 0x7f;
120 tm->tm_hour = data[RTC_HOUR] & 0x1f;
122 tm->tm_hour = data[RTC_HOUR] & 0x0f;
123 if (data[RTC_HOUR] & HOUR_PM_MASK)
127 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
128 tm->tm_mday = data[RTC_DATE] & 0x1f;
129 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
130 tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
135 static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
137 data[RTC_SEC] = tm->tm_sec;
138 data[RTC_MIN] = tm->tm_min;
140 if (tm->tm_hour >= 12)
141 data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
143 data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
145 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
146 data[RTC_DATE] = tm->tm_mday;
147 data[RTC_MONTH] = tm->tm_mon + 1;
148 data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
150 if (tm->tm_year < 100) {
151 pr_err("RTC cannot handle the year %d\n",
160 * Read RTC_UDR_CON register and wait till UDR field is cleared.
161 * This indicates that time/alarm update ended.
163 static inline int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
165 int ret, retry = UDR_READ_RETRY_CNT;
169 ret = regmap_read(info->regmap, info->regs->rtc_udr_update,
171 } while (--retry && (data & info->regs->rtc_udr_mask) && !ret);
174 dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
179 static inline int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
180 struct rtc_wkalrm *alarm)
185 switch (info->device_type) {
188 ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
189 val &= S5M_ALARM0_STATUS;
193 ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2,
195 val &= S2MPS_ALARM0_STATUS;
211 static inline int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
216 ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
218 dev_err(info->dev, "failed to read update reg(%d)\n", ret);
222 data |= info->regs->rtc_udr_mask;
223 if (info->device_type == S5M8763X || info->device_type == S5M8767X)
224 data |= S5M_RTC_TIME_EN_MASK;
226 ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
228 dev_err(info->dev, "failed to write update reg(%d)\n", ret);
232 ret = s5m8767_wait_for_udr_update(info);
237 static inline int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
242 ret = regmap_read(info->regmap, info->regs->rtc_udr_update, &data);
244 dev_err(info->dev, "%s: fail to read update reg(%d)\n",
249 data |= info->regs->rtc_udr_mask;
250 switch (info->device_type) {
253 data &= ~S5M_RTC_TIME_EN_MASK;
256 data |= S2MPS_RTC_RUDR_MASK;
259 data |= S2MPS13_RTC_AUDR_MASK;
265 ret = regmap_write(info->regmap, info->regs->rtc_udr_update, data);
267 dev_err(info->dev, "%s: fail to write update reg(%d)\n",
272 ret = s5m8767_wait_for_udr_update(info);
274 /* On S2MPS13 the AUDR is not auto-cleared */
275 if (info->device_type == S2MPS13X)
276 regmap_update_bits(info->regmap, info->regs->rtc_udr_update,
277 S2MPS13_RTC_AUDR_MASK, 0);
282 static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
284 tm->tm_sec = bcd2bin(data[RTC_SEC]);
285 tm->tm_min = bcd2bin(data[RTC_MIN]);
287 if (data[RTC_HOUR] & HOUR_12) {
288 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
289 if (data[RTC_HOUR] & HOUR_PM)
292 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
295 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
296 tm->tm_mday = bcd2bin(data[RTC_DATE]);
297 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
298 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
302 static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
304 data[RTC_SEC] = bin2bcd(tm->tm_sec);
305 data[RTC_MIN] = bin2bcd(tm->tm_min);
306 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
307 data[RTC_WEEKDAY] = tm->tm_wday;
308 data[RTC_DATE] = bin2bcd(tm->tm_mday);
309 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
310 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
311 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
314 static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
316 struct s5m_rtc_info *info = dev_get_drvdata(dev);
317 u8 data[info->regs->regs_count];
320 if (info->device_type == S2MPS14X || info->device_type == S2MPS13X) {
321 ret = regmap_update_bits(info->regmap,
322 info->regs->rtc_udr_update,
323 S2MPS_RTC_RUDR_MASK, S2MPS_RTC_RUDR_MASK);
326 "Failed to prepare registers for time reading: %d\n",
331 ret = regmap_bulk_read(info->regmap, info->regs->time, data,
332 info->regs->regs_count);
336 switch (info->device_type) {
338 s5m8763_data_to_tm(data, tm);
344 s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
351 dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
352 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
353 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
355 return rtc_valid_tm(tm);
358 static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
360 struct s5m_rtc_info *info = dev_get_drvdata(dev);
361 u8 data[info->regs->regs_count];
364 switch (info->device_type) {
366 s5m8763_tm_to_data(tm, data);
371 ret = s5m8767_tm_to_data(tm, data);
380 dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
381 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
382 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
384 ret = regmap_raw_write(info->regmap, info->regs->time, data,
385 info->regs->regs_count);
389 ret = s5m8767_rtc_set_time_reg(info);
394 static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
396 struct s5m_rtc_info *info = dev_get_drvdata(dev);
397 u8 data[info->regs->regs_count];
401 ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
402 info->regs->regs_count);
406 switch (info->device_type) {
408 s5m8763_data_to_tm(data, &alrm->time);
409 ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val);
413 alrm->enabled = !!val;
419 s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
421 for (i = 0; i < info->regs->regs_count; i++) {
422 if (data[i] & ALARM_ENABLE_MASK) {
433 dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
434 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
435 alrm->time.tm_mday, alrm->time.tm_hour,
436 alrm->time.tm_min, alrm->time.tm_sec,
439 ret = s5m_check_peding_alarm_interrupt(info, alrm);
444 static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
446 u8 data[info->regs->regs_count];
450 ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
451 info->regs->regs_count);
455 s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
456 dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
457 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
458 tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
460 switch (info->device_type) {
462 ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0);
468 for (i = 0; i < info->regs->regs_count; i++)
469 data[i] &= ~ALARM_ENABLE_MASK;
471 ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
472 info->regs->regs_count);
476 ret = s5m8767_rtc_set_alarm_reg(info);
487 static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
490 u8 data[info->regs->regs_count];
494 ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
495 info->regs->regs_count);
499 s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
500 dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
501 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
502 tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
504 switch (info->device_type) {
507 ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf);
513 data[RTC_SEC] |= ALARM_ENABLE_MASK;
514 data[RTC_MIN] |= ALARM_ENABLE_MASK;
515 data[RTC_HOUR] |= ALARM_ENABLE_MASK;
516 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
517 if (data[RTC_DATE] & 0x1f)
518 data[RTC_DATE] |= ALARM_ENABLE_MASK;
519 if (data[RTC_MONTH] & 0xf)
520 data[RTC_MONTH] |= ALARM_ENABLE_MASK;
521 if (data[RTC_YEAR1] & 0x7f)
522 data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
524 ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
525 info->regs->regs_count);
528 ret = s5m8767_rtc_set_alarm_reg(info);
539 static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
541 struct s5m_rtc_info *info = dev_get_drvdata(dev);
542 u8 data[info->regs->regs_count];
545 switch (info->device_type) {
547 s5m8763_tm_to_data(&alrm->time, data);
553 s5m8767_tm_to_data(&alrm->time, data);
560 dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
561 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
562 alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
563 alrm->time.tm_sec, alrm->time.tm_wday);
565 ret = s5m_rtc_stop_alarm(info);
569 ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
570 info->regs->regs_count);
574 ret = s5m8767_rtc_set_alarm_reg(info);
579 ret = s5m_rtc_start_alarm(info);
584 static int s5m_rtc_alarm_irq_enable(struct device *dev,
585 unsigned int enabled)
587 struct s5m_rtc_info *info = dev_get_drvdata(dev);
590 return s5m_rtc_start_alarm(info);
592 return s5m_rtc_stop_alarm(info);
595 static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
597 struct s5m_rtc_info *info = data;
599 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
604 static const struct rtc_class_ops s5m_rtc_ops = {
605 .read_time = s5m_rtc_read_time,
606 .set_time = s5m_rtc_set_time,
607 .read_alarm = s5m_rtc_read_alarm,
608 .set_alarm = s5m_rtc_set_alarm,
609 .alarm_irq_enable = s5m_rtc_alarm_irq_enable,
612 static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
617 switch (info->device_type) {
620 /* UDR update time. Default of 7.32 ms is too long. */
621 ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
622 S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
624 dev_err(info->dev, "%s: fail to change UDR time: %d\n",
627 /* Set RTC control register : Binary mode, 24hour mode */
628 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
629 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
631 ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
636 data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
637 ret = regmap_write(info->regmap, info->regs->ctrl, data[0]);
642 * Should set WUDR & (RUDR or AUDR) bits to high after writing
643 * RTC_CTRL register like writing Alarm registers. We can't find
644 * the description from datasheet but vendor code does that
647 ret = s5m8767_rtc_set_alarm_reg(info);
654 info->rtc_24hr_mode = 1;
656 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
664 static int s5m_rtc_probe(struct platform_device *pdev)
666 struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
667 struct sec_platform_data *pdata = s5m87xx->pdata;
668 struct s5m_rtc_info *info;
669 const struct regmap_config *regmap_cfg;
673 dev_err(pdev->dev.parent, "Platform data not supplied\n");
677 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
681 switch (platform_get_device_id(pdev)->driver_data) {
684 regmap_cfg = &s2mps14_rtc_regmap_config;
685 info->regs = &s2mps_rtc_regs;
686 alarm_irq = S2MPS14_IRQ_RTCA0;
689 regmap_cfg = &s5m_rtc_regmap_config;
690 info->regs = &s5m_rtc_regs;
691 alarm_irq = S5M8763_IRQ_ALARM0;
694 regmap_cfg = &s5m_rtc_regmap_config;
695 info->regs = &s5m_rtc_regs;
696 alarm_irq = S5M8767_IRQ_RTCA1;
700 "Device type %lu is not supported by RTC driver\n",
701 platform_get_device_id(pdev)->driver_data);
705 info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
707 dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
711 info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
712 if (IS_ERR(info->regmap)) {
713 ret = PTR_ERR(info->regmap);
714 dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
719 info->dev = &pdev->dev;
720 info->s5m87xx = s5m87xx;
721 info->device_type = platform_get_device_id(pdev)->driver_data;
723 if (s5m87xx->irq_data) {
724 info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq);
725 if (info->irq <= 0) {
727 dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
733 platform_set_drvdata(pdev, info);
735 ret = s5m8767_rtc_init_reg(info);
737 device_init_wakeup(&pdev->dev, 1);
739 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
740 &s5m_rtc_ops, THIS_MODULE);
742 if (IS_ERR(info->rtc_dev)) {
743 ret = PTR_ERR(info->rtc_dev);
748 dev_info(&pdev->dev, "Alarm IRQ not available\n");
752 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
753 s5m_rtc_alarm_irq, 0, "rtc-alarm0",
756 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
764 i2c_unregister_device(info->i2c);
769 static int s5m_rtc_remove(struct platform_device *pdev)
771 struct s5m_rtc_info *info = platform_get_drvdata(pdev);
773 i2c_unregister_device(info->i2c);
778 #ifdef CONFIG_PM_SLEEP
779 static int s5m_rtc_resume(struct device *dev)
781 struct s5m_rtc_info *info = dev_get_drvdata(dev);
784 if (info->irq && device_may_wakeup(dev))
785 ret = disable_irq_wake(info->irq);
790 static int s5m_rtc_suspend(struct device *dev)
792 struct s5m_rtc_info *info = dev_get_drvdata(dev);
795 if (info->irq && device_may_wakeup(dev))
796 ret = enable_irq_wake(info->irq);
800 #endif /* CONFIG_PM_SLEEP */
802 static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
804 static const struct platform_device_id s5m_rtc_id[] = {
805 { "s5m-rtc", S5M8767X },
806 { "s2mps13-rtc", S2MPS13X },
807 { "s2mps14-rtc", S2MPS14X },
810 MODULE_DEVICE_TABLE(platform, s5m_rtc_id);
812 static struct platform_driver s5m_rtc_driver = {
815 .pm = &s5m_rtc_pm_ops,
817 .probe = s5m_rtc_probe,
818 .remove = s5m_rtc_remove,
819 .id_table = s5m_rtc_id,
822 module_platform_driver(s5m_rtc_driver);
824 /* Module information */
825 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
826 MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver");
827 MODULE_LICENSE("GPL");
828 MODULE_ALIAS("platform:s5m-rtc");