2 * rtc-rc5t583.c -- RICOH RC5T583 Real Time Clock
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/rtc.h>
25 #include <linux/bcd.h>
26 #include <linux/platform_device.h>
27 #include <linux/interrupt.h>
28 #include <linux/mfd/rc5t583.h>
31 struct rtc_device
*rtc
;
32 /* To store the list of enabled interrupts, during system suspend */
36 /* Total number of RTC registers needed to set time*/
37 #define NUM_TIME_REGS (RC5T583_RTC_YEAR - RC5T583_RTC_SEC + 1)
39 /* Total number of RTC registers needed to set Y-Alarm*/
40 #define NUM_YAL_REGS (RC5T583_RTC_AY_YEAR - RC5T583_RTC_AY_MIN + 1)
42 /* Set Y-Alarm interrupt */
43 #define SET_YAL BIT(5)
45 /* Get Y-Alarm interrupt status*/
46 #define GET_YAL_STATUS BIT(3)
48 static int rc5t583_rtc_alarm_irq_enable(struct device
*dev
, unsigned enabled
)
50 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
53 /* Set Y-Alarm, based on 'enabled' */
54 val
= enabled
? SET_YAL
: 0;
56 return regmap_update_bits(rc5t583
->regmap
, RC5T583_RTC_CTL1
, SET_YAL
,
61 * Gets current rc5t583 RTC time and date parameters.
63 * The RTC's time/alarm representation is not what gmtime(3) requires
66 * - Months are 1..12 vs Linux 0-11
67 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
69 static int rc5t583_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
71 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
72 u8 rtc_data
[NUM_TIME_REGS
];
75 ret
= regmap_bulk_read(rc5t583
->regmap
, RC5T583_RTC_SEC
, rtc_data
,
78 dev_err(dev
, "RTC read time failed with err:%d\n", ret
);
82 tm
->tm_sec
= bcd2bin(rtc_data
[0]);
83 tm
->tm_min
= bcd2bin(rtc_data
[1]);
84 tm
->tm_hour
= bcd2bin(rtc_data
[2]);
85 tm
->tm_wday
= bcd2bin(rtc_data
[3]);
86 tm
->tm_mday
= bcd2bin(rtc_data
[4]);
87 tm
->tm_mon
= bcd2bin(rtc_data
[5]) - 1;
88 tm
->tm_year
= bcd2bin(rtc_data
[6]) + 100;
93 static int rc5t583_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
95 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
96 unsigned char rtc_data
[NUM_TIME_REGS
];
99 rtc_data
[0] = bin2bcd(tm
->tm_sec
);
100 rtc_data
[1] = bin2bcd(tm
->tm_min
);
101 rtc_data
[2] = bin2bcd(tm
->tm_hour
);
102 rtc_data
[3] = bin2bcd(tm
->tm_wday
);
103 rtc_data
[4] = bin2bcd(tm
->tm_mday
);
104 rtc_data
[5] = bin2bcd(tm
->tm_mon
+ 1);
105 rtc_data
[6] = bin2bcd(tm
->tm_year
- 100);
107 ret
= regmap_bulk_write(rc5t583
->regmap
, RC5T583_RTC_SEC
, rtc_data
,
110 dev_err(dev
, "RTC set time failed with error %d\n", ret
);
117 static int rc5t583_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
119 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
120 unsigned char alarm_data
[NUM_YAL_REGS
];
121 u32 interrupt_enable
;
124 ret
= regmap_bulk_read(rc5t583
->regmap
, RC5T583_RTC_AY_MIN
, alarm_data
,
127 dev_err(dev
, "rtc_read_alarm error %d\n", ret
);
131 alm
->time
.tm_sec
= 0;
132 alm
->time
.tm_min
= bcd2bin(alarm_data
[0]);
133 alm
->time
.tm_hour
= bcd2bin(alarm_data
[1]);
134 alm
->time
.tm_mday
= bcd2bin(alarm_data
[2]);
135 alm
->time
.tm_mon
= bcd2bin(alarm_data
[3]) - 1;
136 alm
->time
.tm_year
= bcd2bin(alarm_data
[4]) + 100;
138 ret
= regmap_read(rc5t583
->regmap
, RC5T583_RTC_CTL1
, &interrupt_enable
);
142 /* check if YALE is set */
143 if (interrupt_enable
& SET_YAL
)
149 static int rc5t583_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
151 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
152 unsigned char alarm_data
[NUM_YAL_REGS
];
155 ret
= rc5t583_rtc_alarm_irq_enable(dev
, 0);
159 alarm_data
[0] = bin2bcd(alm
->time
.tm_min
);
160 alarm_data
[1] = bin2bcd(alm
->time
.tm_hour
);
161 alarm_data
[2] = bin2bcd(alm
->time
.tm_mday
);
162 alarm_data
[3] = bin2bcd(alm
->time
.tm_mon
+ 1);
163 alarm_data
[4] = bin2bcd(alm
->time
.tm_year
- 100);
165 ret
= regmap_bulk_write(rc5t583
->regmap
, RC5T583_RTC_AY_MIN
, alarm_data
,
168 dev_err(dev
, "rtc_set_alarm error %d\n", ret
);
173 ret
= rc5t583_rtc_alarm_irq_enable(dev
, 1);
178 static irqreturn_t
rc5t583_rtc_interrupt(int irq
, void *rtc
)
180 struct device
*dev
= rtc
;
181 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
182 struct rc5t583_rtc
*rc5t583_rtc
= dev_get_drvdata(dev
);
183 unsigned long events
= 0;
187 ret
= regmap_read(rc5t583
->regmap
, RC5T583_RTC_CTL2
, &rtc_reg
);
191 if (rtc_reg
& GET_YAL_STATUS
) {
192 events
= RTC_IRQF
| RTC_AF
;
193 /* clear pending Y-alarm interrupt bit */
194 rtc_reg
&= ~GET_YAL_STATUS
;
197 ret
= regmap_write(rc5t583
->regmap
, RC5T583_RTC_CTL2
, rtc_reg
);
201 /* Notify RTC core on event */
202 rtc_update_irq(rc5t583_rtc
->rtc
, 1, events
);
207 static const struct rtc_class_ops rc5t583_rtc_ops
= {
208 .read_time
= rc5t583_rtc_read_time
,
209 .set_time
= rc5t583_rtc_set_time
,
210 .read_alarm
= rc5t583_rtc_read_alarm
,
211 .set_alarm
= rc5t583_rtc_set_alarm
,
212 .alarm_irq_enable
= rc5t583_rtc_alarm_irq_enable
,
215 static int rc5t583_rtc_probe(struct platform_device
*pdev
)
217 struct rc5t583
*rc5t583
= dev_get_drvdata(pdev
->dev
.parent
);
218 struct rc5t583_rtc
*ricoh_rtc
;
219 struct rc5t583_platform_data
*pmic_plat_data
;
223 ricoh_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(struct rc5t583_rtc
),
228 platform_set_drvdata(pdev
, ricoh_rtc
);
230 /* Clear pending interrupts */
231 ret
= regmap_write(rc5t583
->regmap
, RC5T583_RTC_CTL2
, 0);
235 /* clear RTC Adjust register */
236 ret
= regmap_write(rc5t583
->regmap
, RC5T583_RTC_ADJ
, 0);
238 dev_err(&pdev
->dev
, "unable to program rtc_adjust reg\n");
242 pmic_plat_data
= dev_get_platdata(rc5t583
->dev
);
243 irq
= pmic_plat_data
->irq_base
;
245 dev_warn(&pdev
->dev
, "Wake up is not possible as irq = %d\n",
250 irq
+= RC5T583_IRQ_YALE
;
251 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
252 rc5t583_rtc_interrupt
, IRQF_TRIGGER_LOW
,
253 "rtc-rc5t583", &pdev
->dev
);
255 dev_err(&pdev
->dev
, "IRQ is not free.\n");
258 device_init_wakeup(&pdev
->dev
, 1);
260 ricoh_rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
261 &rc5t583_rtc_ops
, THIS_MODULE
);
262 if (IS_ERR(ricoh_rtc
->rtc
)) {
263 ret
= PTR_ERR(ricoh_rtc
->rtc
);
264 dev_err(&pdev
->dev
, "RTC device register: err %d\n", ret
);
272 * Disable rc5t583 RTC interrupts.
273 * Sets status flag to free.
275 static int rc5t583_rtc_remove(struct platform_device
*pdev
)
277 struct rc5t583_rtc
*rc5t583_rtc
= platform_get_drvdata(pdev
);
279 rc5t583_rtc_alarm_irq_enable(&rc5t583_rtc
->rtc
->dev
, 0);
283 #ifdef CONFIG_PM_SLEEP
284 static int rc5t583_rtc_suspend(struct device
*dev
)
286 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
287 struct rc5t583_rtc
*rc5t583_rtc
= dev_get_drvdata(dev
);
290 /* Store current list of enabled interrupts*/
291 ret
= regmap_read(rc5t583
->regmap
, RC5T583_RTC_CTL1
,
292 &rc5t583_rtc
->irqen
);
296 static int rc5t583_rtc_resume(struct device
*dev
)
298 struct rc5t583
*rc5t583
= dev_get_drvdata(dev
->parent
);
299 struct rc5t583_rtc
*rc5t583_rtc
= dev_get_drvdata(dev
);
301 /* Restore list of enabled interrupts before suspend */
302 return regmap_write(rc5t583
->regmap
, RC5T583_RTC_CTL1
,
307 static SIMPLE_DEV_PM_OPS(rc5t583_rtc_pm_ops
, rc5t583_rtc_suspend
,
310 static struct platform_driver rc5t583_rtc_driver
= {
311 .probe
= rc5t583_rtc_probe
,
312 .remove
= rc5t583_rtc_remove
,
314 .name
= "rtc-rc5t583",
315 .pm
= &rc5t583_rtc_pm_ops
,
319 module_platform_driver(rc5t583_rtc_driver
);
320 MODULE_ALIAS("platform:rtc-rc5t583");
321 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
322 MODULE_LICENSE("GPL v2");