treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / rtc / rtc-as3722.c
blob0f21af27f4cfecdc9d1656f6d3045c38dba55e58
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * rtc-as3722.c - Real Time Clock driver for ams AS3722 PMICs
5 * Copyright (C) 2013 ams AG
6 * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
8 * Author: Florian Lobmaier <florian.lobmaier@ams.com>
9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
12 #include <linux/bcd.h>
13 #include <linux/completion.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioctl.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mfd/as3722.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtc.h>
22 #include <linux/time.h>
24 #define AS3722_RTC_START_YEAR 2000
25 struct as3722_rtc {
26 struct rtc_device *rtc;
27 struct device *dev;
28 struct as3722 *as3722;
29 int alarm_irq;
30 bool irq_enable;
33 static void as3722_time_to_reg(u8 *rbuff, struct rtc_time *tm)
35 rbuff[0] = bin2bcd(tm->tm_sec);
36 rbuff[1] = bin2bcd(tm->tm_min);
37 rbuff[2] = bin2bcd(tm->tm_hour);
38 rbuff[3] = bin2bcd(tm->tm_mday);
39 rbuff[4] = bin2bcd(tm->tm_mon + 1);
40 rbuff[5] = bin2bcd(tm->tm_year - (AS3722_RTC_START_YEAR - 1900));
43 static void as3722_reg_to_time(u8 *rbuff, struct rtc_time *tm)
45 tm->tm_sec = bcd2bin(rbuff[0] & 0x7F);
46 tm->tm_min = bcd2bin(rbuff[1] & 0x7F);
47 tm->tm_hour = bcd2bin(rbuff[2] & 0x3F);
48 tm->tm_mday = bcd2bin(rbuff[3] & 0x3F);
49 tm->tm_mon = bcd2bin(rbuff[4] & 0x1F) - 1;
50 tm->tm_year = (AS3722_RTC_START_YEAR - 1900) + bcd2bin(rbuff[5] & 0x7F);
51 return;
54 static int as3722_rtc_read_time(struct device *dev, struct rtc_time *tm)
56 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
57 struct as3722 *as3722 = as3722_rtc->as3722;
58 u8 as_time_array[6];
59 int ret;
61 ret = as3722_block_read(as3722, AS3722_RTC_SECOND_REG,
62 6, as_time_array);
63 if (ret < 0) {
64 dev_err(dev, "RTC_SECOND reg block read failed %d\n", ret);
65 return ret;
67 as3722_reg_to_time(as_time_array, tm);
68 return 0;
71 static int as3722_rtc_set_time(struct device *dev, struct rtc_time *tm)
73 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
74 struct as3722 *as3722 = as3722_rtc->as3722;
75 u8 as_time_array[6];
76 int ret;
78 if (tm->tm_year < (AS3722_RTC_START_YEAR - 1900))
79 return -EINVAL;
81 as3722_time_to_reg(as_time_array, tm);
82 ret = as3722_block_write(as3722, AS3722_RTC_SECOND_REG, 6,
83 as_time_array);
84 if (ret < 0)
85 dev_err(dev, "RTC_SECOND reg block write failed %d\n", ret);
86 return ret;
89 static int as3722_rtc_alarm_irq_enable(struct device *dev,
90 unsigned int enabled)
92 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
94 if (enabled && !as3722_rtc->irq_enable) {
95 enable_irq(as3722_rtc->alarm_irq);
96 as3722_rtc->irq_enable = true;
97 } else if (!enabled && as3722_rtc->irq_enable) {
98 disable_irq(as3722_rtc->alarm_irq);
99 as3722_rtc->irq_enable = false;
101 return 0;
104 static int as3722_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
106 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
107 struct as3722 *as3722 = as3722_rtc->as3722;
108 u8 as_time_array[6];
109 int ret;
111 ret = as3722_block_read(as3722, AS3722_RTC_ALARM_SECOND_REG, 6,
112 as_time_array);
113 if (ret < 0) {
114 dev_err(dev, "RTC_ALARM_SECOND block read failed %d\n", ret);
115 return ret;
118 as3722_reg_to_time(as_time_array, &alrm->time);
119 return 0;
122 static int as3722_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
124 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
125 struct as3722 *as3722 = as3722_rtc->as3722;
126 u8 as_time_array[6];
127 int ret;
129 if (alrm->time.tm_year < (AS3722_RTC_START_YEAR - 1900))
130 return -EINVAL;
132 ret = as3722_rtc_alarm_irq_enable(dev, 0);
133 if (ret < 0) {
134 dev_err(dev, "Disable RTC alarm failed\n");
135 return ret;
138 as3722_time_to_reg(as_time_array, &alrm->time);
139 ret = as3722_block_write(as3722, AS3722_RTC_ALARM_SECOND_REG, 6,
140 as_time_array);
141 if (ret < 0) {
142 dev_err(dev, "RTC_ALARM_SECOND block write failed %d\n", ret);
143 return ret;
146 if (alrm->enabled)
147 ret = as3722_rtc_alarm_irq_enable(dev, alrm->enabled);
148 return ret;
151 static irqreturn_t as3722_alarm_irq(int irq, void *data)
153 struct as3722_rtc *as3722_rtc = data;
155 rtc_update_irq(as3722_rtc->rtc, 1, RTC_IRQF | RTC_AF);
156 return IRQ_HANDLED;
159 static const struct rtc_class_ops as3722_rtc_ops = {
160 .read_time = as3722_rtc_read_time,
161 .set_time = as3722_rtc_set_time,
162 .read_alarm = as3722_rtc_read_alarm,
163 .set_alarm = as3722_rtc_set_alarm,
164 .alarm_irq_enable = as3722_rtc_alarm_irq_enable,
167 static int as3722_rtc_probe(struct platform_device *pdev)
169 struct as3722 *as3722 = dev_get_drvdata(pdev->dev.parent);
170 struct as3722_rtc *as3722_rtc;
171 int ret;
173 as3722_rtc = devm_kzalloc(&pdev->dev, sizeof(*as3722_rtc), GFP_KERNEL);
174 if (!as3722_rtc)
175 return -ENOMEM;
177 as3722_rtc->as3722 = as3722;
178 as3722_rtc->dev = &pdev->dev;
179 platform_set_drvdata(pdev, as3722_rtc);
181 /* Enable the RTC to make sure it is running. */
182 ret = as3722_update_bits(as3722, AS3722_RTC_CONTROL_REG,
183 AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN,
184 AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN);
185 if (ret < 0) {
186 dev_err(&pdev->dev, "RTC_CONTROL reg write failed: %d\n", ret);
187 return ret;
190 device_init_wakeup(&pdev->dev, 1);
192 as3722_rtc->rtc = devm_rtc_device_register(&pdev->dev, "as3722-rtc",
193 &as3722_rtc_ops, THIS_MODULE);
194 if (IS_ERR(as3722_rtc->rtc)) {
195 ret = PTR_ERR(as3722_rtc->rtc);
196 dev_err(&pdev->dev, "RTC register failed: %d\n", ret);
197 return ret;
200 as3722_rtc->alarm_irq = platform_get_irq(pdev, 0);
201 dev_info(&pdev->dev, "RTC interrupt %d\n", as3722_rtc->alarm_irq);
203 ret = devm_request_threaded_irq(&pdev->dev, as3722_rtc->alarm_irq, NULL,
204 as3722_alarm_irq, IRQF_ONESHOT,
205 "rtc-alarm", as3722_rtc);
206 if (ret < 0) {
207 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
208 as3722_rtc->alarm_irq, ret);
209 return ret;
211 disable_irq(as3722_rtc->alarm_irq);
212 return 0;
215 #ifdef CONFIG_PM_SLEEP
216 static int as3722_rtc_suspend(struct device *dev)
218 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
220 if (device_may_wakeup(dev))
221 enable_irq_wake(as3722_rtc->alarm_irq);
223 return 0;
226 static int as3722_rtc_resume(struct device *dev)
228 struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
230 if (device_may_wakeup(dev))
231 disable_irq_wake(as3722_rtc->alarm_irq);
232 return 0;
234 #endif
236 static SIMPLE_DEV_PM_OPS(as3722_rtc_pm_ops, as3722_rtc_suspend,
237 as3722_rtc_resume);
239 static struct platform_driver as3722_rtc_driver = {
240 .probe = as3722_rtc_probe,
241 .driver = {
242 .name = "as3722-rtc",
243 .pm = &as3722_rtc_pm_ops,
246 module_platform_driver(as3722_rtc_driver);
248 MODULE_DESCRIPTION("RTC driver for AS3722 PMICs");
249 MODULE_ALIAS("platform:as3722-rtc");
250 MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
251 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
252 MODULE_LICENSE("GPL");