treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / rtc / rtc-rc5t583.c
blob18684a7026c44ca10738be349756c9a28341047e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * rtc-rc5t583.c -- RICOH RC5T583 Real Time Clock
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
7 */
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/rtc.h>
15 #include <linux/bcd.h>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/mfd/rc5t583.h>
20 struct rc5t583_rtc {
21 struct rtc_device *rtc;
22 /* To store the list of enabled interrupts, during system suspend */
23 u32 irqen;
26 /* Total number of RTC registers needed to set time*/
27 #define NUM_TIME_REGS (RC5T583_RTC_YEAR - RC5T583_RTC_SEC + 1)
29 /* Total number of RTC registers needed to set Y-Alarm*/
30 #define NUM_YAL_REGS (RC5T583_RTC_AY_YEAR - RC5T583_RTC_AY_MIN + 1)
32 /* Set Y-Alarm interrupt */
33 #define SET_YAL BIT(5)
35 /* Get Y-Alarm interrupt status*/
36 #define GET_YAL_STATUS BIT(3)
38 static int rc5t583_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
40 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
41 u8 val;
43 /* Set Y-Alarm, based on 'enabled' */
44 val = enabled ? SET_YAL : 0;
46 return regmap_update_bits(rc5t583->regmap, RC5T583_RTC_CTL1, SET_YAL,
47 val);
51 * Gets current rc5t583 RTC time and date parameters.
53 * The RTC's time/alarm representation is not what gmtime(3) requires
54 * Linux to use:
56 * - Months are 1..12 vs Linux 0-11
57 * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
59 static int rc5t583_rtc_read_time(struct device *dev, struct rtc_time *tm)
61 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
62 u8 rtc_data[NUM_TIME_REGS];
63 int ret;
65 ret = regmap_bulk_read(rc5t583->regmap, RC5T583_RTC_SEC, rtc_data,
66 NUM_TIME_REGS);
67 if (ret < 0) {
68 dev_err(dev, "RTC read time failed with err:%d\n", ret);
69 return ret;
72 tm->tm_sec = bcd2bin(rtc_data[0]);
73 tm->tm_min = bcd2bin(rtc_data[1]);
74 tm->tm_hour = bcd2bin(rtc_data[2]);
75 tm->tm_wday = bcd2bin(rtc_data[3]);
76 tm->tm_mday = bcd2bin(rtc_data[4]);
77 tm->tm_mon = bcd2bin(rtc_data[5]) - 1;
78 tm->tm_year = bcd2bin(rtc_data[6]) + 100;
80 return ret;
83 static int rc5t583_rtc_set_time(struct device *dev, struct rtc_time *tm)
85 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
86 unsigned char rtc_data[NUM_TIME_REGS];
87 int ret;
89 rtc_data[0] = bin2bcd(tm->tm_sec);
90 rtc_data[1] = bin2bcd(tm->tm_min);
91 rtc_data[2] = bin2bcd(tm->tm_hour);
92 rtc_data[3] = bin2bcd(tm->tm_wday);
93 rtc_data[4] = bin2bcd(tm->tm_mday);
94 rtc_data[5] = bin2bcd(tm->tm_mon + 1);
95 rtc_data[6] = bin2bcd(tm->tm_year - 100);
97 ret = regmap_bulk_write(rc5t583->regmap, RC5T583_RTC_SEC, rtc_data,
98 NUM_TIME_REGS);
99 if (ret < 0) {
100 dev_err(dev, "RTC set time failed with error %d\n", ret);
101 return ret;
104 return ret;
107 static int rc5t583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
109 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
110 unsigned char alarm_data[NUM_YAL_REGS];
111 u32 interrupt_enable;
112 int ret;
114 ret = regmap_bulk_read(rc5t583->regmap, RC5T583_RTC_AY_MIN, alarm_data,
115 NUM_YAL_REGS);
116 if (ret < 0) {
117 dev_err(dev, "rtc_read_alarm error %d\n", ret);
118 return ret;
121 alm->time.tm_sec = 0;
122 alm->time.tm_min = bcd2bin(alarm_data[0]);
123 alm->time.tm_hour = bcd2bin(alarm_data[1]);
124 alm->time.tm_mday = bcd2bin(alarm_data[2]);
125 alm->time.tm_mon = bcd2bin(alarm_data[3]) - 1;
126 alm->time.tm_year = bcd2bin(alarm_data[4]) + 100;
128 ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL1, &interrupt_enable);
129 if (ret < 0)
130 return ret;
132 /* check if YALE is set */
133 if (interrupt_enable & SET_YAL)
134 alm->enabled = 1;
136 return ret;
139 static int rc5t583_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
141 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
142 unsigned char alarm_data[NUM_YAL_REGS];
143 int ret;
145 ret = rc5t583_rtc_alarm_irq_enable(dev, 0);
146 if (ret)
147 return ret;
149 alarm_data[0] = bin2bcd(alm->time.tm_min);
150 alarm_data[1] = bin2bcd(alm->time.tm_hour);
151 alarm_data[2] = bin2bcd(alm->time.tm_mday);
152 alarm_data[3] = bin2bcd(alm->time.tm_mon + 1);
153 alarm_data[4] = bin2bcd(alm->time.tm_year - 100);
155 ret = regmap_bulk_write(rc5t583->regmap, RC5T583_RTC_AY_MIN, alarm_data,
156 NUM_YAL_REGS);
157 if (ret) {
158 dev_err(dev, "rtc_set_alarm error %d\n", ret);
159 return ret;
162 if (alm->enabled)
163 ret = rc5t583_rtc_alarm_irq_enable(dev, 1);
165 return ret;
168 static irqreturn_t rc5t583_rtc_interrupt(int irq, void *rtc)
170 struct device *dev = rtc;
171 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
172 struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
173 unsigned long events = 0;
174 int ret;
175 u32 rtc_reg;
177 ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL2, &rtc_reg);
178 if (ret < 0)
179 return IRQ_NONE;
181 if (rtc_reg & GET_YAL_STATUS) {
182 events = RTC_IRQF | RTC_AF;
183 /* clear pending Y-alarm interrupt bit */
184 rtc_reg &= ~GET_YAL_STATUS;
187 ret = regmap_write(rc5t583->regmap, RC5T583_RTC_CTL2, rtc_reg);
188 if (ret)
189 return IRQ_NONE;
191 /* Notify RTC core on event */
192 rtc_update_irq(rc5t583_rtc->rtc, 1, events);
194 return IRQ_HANDLED;
197 static const struct rtc_class_ops rc5t583_rtc_ops = {
198 .read_time = rc5t583_rtc_read_time,
199 .set_time = rc5t583_rtc_set_time,
200 .read_alarm = rc5t583_rtc_read_alarm,
201 .set_alarm = rc5t583_rtc_set_alarm,
202 .alarm_irq_enable = rc5t583_rtc_alarm_irq_enable,
205 static int rc5t583_rtc_probe(struct platform_device *pdev)
207 struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
208 struct rc5t583_rtc *ricoh_rtc;
209 struct rc5t583_platform_data *pmic_plat_data;
210 int ret;
211 int irq;
213 ricoh_rtc = devm_kzalloc(&pdev->dev, sizeof(struct rc5t583_rtc),
214 GFP_KERNEL);
215 if (!ricoh_rtc)
216 return -ENOMEM;
218 platform_set_drvdata(pdev, ricoh_rtc);
220 /* Clear pending interrupts */
221 ret = regmap_write(rc5t583->regmap, RC5T583_RTC_CTL2, 0);
222 if (ret < 0)
223 return ret;
225 /* clear RTC Adjust register */
226 ret = regmap_write(rc5t583->regmap, RC5T583_RTC_ADJ, 0);
227 if (ret < 0) {
228 dev_err(&pdev->dev, "unable to program rtc_adjust reg\n");
229 return -EBUSY;
232 pmic_plat_data = dev_get_platdata(rc5t583->dev);
233 irq = pmic_plat_data->irq_base;
234 if (irq <= 0) {
235 dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
236 irq);
237 return ret;
240 irq += RC5T583_IRQ_YALE;
241 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
242 rc5t583_rtc_interrupt, IRQF_TRIGGER_LOW,
243 "rtc-rc5t583", &pdev->dev);
244 if (ret < 0) {
245 dev_err(&pdev->dev, "IRQ is not free.\n");
246 return ret;
248 device_init_wakeup(&pdev->dev, 1);
250 ricoh_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
251 &rc5t583_rtc_ops, THIS_MODULE);
252 if (IS_ERR(ricoh_rtc->rtc)) {
253 ret = PTR_ERR(ricoh_rtc->rtc);
254 dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
255 return ret;
258 return 0;
262 * Disable rc5t583 RTC interrupts.
263 * Sets status flag to free.
265 static int rc5t583_rtc_remove(struct platform_device *pdev)
267 struct rc5t583_rtc *rc5t583_rtc = platform_get_drvdata(pdev);
269 rc5t583_rtc_alarm_irq_enable(&rc5t583_rtc->rtc->dev, 0);
270 return 0;
273 #ifdef CONFIG_PM_SLEEP
274 static int rc5t583_rtc_suspend(struct device *dev)
276 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
277 struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
278 int ret;
280 /* Store current list of enabled interrupts*/
281 ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL1,
282 &rc5t583_rtc->irqen);
283 return ret;
286 static int rc5t583_rtc_resume(struct device *dev)
288 struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
289 struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
291 /* Restore list of enabled interrupts before suspend */
292 return regmap_write(rc5t583->regmap, RC5T583_RTC_CTL1,
293 rc5t583_rtc->irqen);
295 #endif
297 static SIMPLE_DEV_PM_OPS(rc5t583_rtc_pm_ops, rc5t583_rtc_suspend,
298 rc5t583_rtc_resume);
300 static struct platform_driver rc5t583_rtc_driver = {
301 .probe = rc5t583_rtc_probe,
302 .remove = rc5t583_rtc_remove,
303 .driver = {
304 .name = "rtc-rc5t583",
305 .pm = &rc5t583_rtc_pm_ops,
309 module_platform_driver(rc5t583_rtc_driver);
310 MODULE_ALIAS("platform:rtc-rc5t583");
311 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
312 MODULE_LICENSE("GPL v2");