rtc: pm8xxx: drop error messages
[platform/kernel/linux-starfive.git] / drivers / rtc / rtc-pm8xxx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
3  */
4 #include <linux/of.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/rtc.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm.h>
10 #include <linux/pm_wakeirq.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14
15 #include <asm/unaligned.h>
16
17 /* RTC_CTRL register bit fields */
18 #define PM8xxx_RTC_ENABLE               BIT(7)
19 #define PM8xxx_RTC_ALARM_CLEAR          BIT(0)
20 #define PM8xxx_RTC_ALARM_ENABLE         BIT(7)
21
22 #define NUM_8_BIT_RTC_REGS              0x4
23
24 /**
25  * struct pm8xxx_rtc_regs - describe RTC registers per PMIC versions
26  * @ctrl:               address of control register
27  * @write:              base address of write registers
28  * @read:               base address of read registers
29  * @alarm_ctrl:         address of alarm control register
30  * @alarm_ctrl2:        address of alarm control2 register
31  * @alarm_rw:           base address of alarm read-write registers
32  * @alarm_en:           alarm enable mask
33  */
34 struct pm8xxx_rtc_regs {
35         unsigned int ctrl;
36         unsigned int write;
37         unsigned int read;
38         unsigned int alarm_ctrl;
39         unsigned int alarm_ctrl2;
40         unsigned int alarm_rw;
41         unsigned int alarm_en;
42 };
43
44 /**
45  * struct pm8xxx_rtc -  RTC driver internal structure
46  * @rtc:                RTC device
47  * @regmap:             regmap used to access registers
48  * @allow_set_time:     whether the time can be set
49  * @alarm_irq:          alarm irq number
50  * @regs:               register description
51  * @dev:                device structure
52  */
53 struct pm8xxx_rtc {
54         struct rtc_device *rtc;
55         struct regmap *regmap;
56         bool allow_set_time;
57         int alarm_irq;
58         const struct pm8xxx_rtc_regs *regs;
59         struct device *dev;
60 };
61
62 static int pm8xxx_rtc_read_raw(struct pm8xxx_rtc *rtc_dd, u32 *secs)
63 {
64         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
65         u8 value[NUM_8_BIT_RTC_REGS];
66         unsigned int reg;
67         int rc;
68
69         rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value, sizeof(value));
70         if (rc)
71                 return rc;
72
73         /*
74          * Read the LSB again and check if there has been a carry over.
75          * If there has, redo the read operation.
76          */
77         rc = regmap_read(rtc_dd->regmap, regs->read, &reg);
78         if (rc < 0)
79                 return rc;
80
81         if (reg < value[0]) {
82                 rc = regmap_bulk_read(rtc_dd->regmap, regs->read, value,
83                                       sizeof(value));
84                 if (rc)
85                         return rc;
86         }
87
88         *secs = get_unaligned_le32(value);
89
90         return 0;
91 }
92
93 /*
94  * Steps to write the RTC registers.
95  * 1. Disable alarm if enabled.
96  * 2. Disable rtc if enabled.
97  * 3. Write 0x00 to LSB.
98  * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0].
99  * 5. Enable rtc if disabled in step 2.
100  * 6. Enable alarm if disabled in step 1.
101  */
102 static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
103 {
104         struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
105         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
106         u8 value[NUM_8_BIT_RTC_REGS];
107         bool alarm_enabled;
108         u32 secs;
109         int rc;
110
111         if (!rtc_dd->allow_set_time)
112                 return -ENODEV;
113
114         secs = rtc_tm_to_time64(tm);
115         put_unaligned_le32(secs, value);
116
117         dev_dbg(dev, "set time: %ptRd %ptRt (%u)\n", tm, tm, secs);
118
119         rc = regmap_update_bits_check(rtc_dd->regmap, regs->alarm_ctrl,
120                                       regs->alarm_en, 0, &alarm_enabled);
121         if (rc)
122                 return rc;
123
124         /* Disable RTC */
125         rc = regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE, 0);
126         if (rc)
127                 return rc;
128
129         /* Write 0 to Byte[0] */
130         rc = regmap_write(rtc_dd->regmap, regs->write, 0);
131         if (rc)
132                 return rc;
133
134         /* Write Byte[1], Byte[2], Byte[3] */
135         rc = regmap_bulk_write(rtc_dd->regmap, regs->write + 1,
136                                &value[1], sizeof(value) - 1);
137         if (rc)
138                 return rc;
139
140         /* Write Byte[0] */
141         rc = regmap_write(rtc_dd->regmap, regs->write, value[0]);
142         if (rc)
143                 return rc;
144
145         /* Enable RTC */
146         rc = regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE,
147                                 PM8xxx_RTC_ENABLE);
148         if (rc)
149                 return rc;
150
151         if (alarm_enabled) {
152                 rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
153                                         regs->alarm_en, regs->alarm_en);
154                 if (rc)
155                         return rc;
156         }
157
158         return 0;
159 }
160
161 static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
162 {
163         struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
164         u32 secs;
165         int rc;
166
167         rc = pm8xxx_rtc_read_raw(rtc_dd, &secs);
168         if (rc)
169                 return rc;
170
171         rtc_time64_to_tm(secs, tm);
172
173         dev_dbg(dev, "read time: %ptRd %ptRt (%u)\n", tm, tm, secs);
174
175         return 0;
176 }
177
178 static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
179 {
180         struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
181         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
182         u8 value[NUM_8_BIT_RTC_REGS];
183         u32 secs;
184         int rc;
185
186         secs = rtc_tm_to_time64(&alarm->time);
187         put_unaligned_le32(secs, value);
188
189         rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
190                                 regs->alarm_en, 0);
191         if (rc)
192                 return rc;
193
194         rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
195                                sizeof(value));
196         if (rc)
197                 return rc;
198
199         if (alarm->enabled) {
200                 rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
201                                         regs->alarm_en, regs->alarm_en);
202                 if (rc)
203                         return rc;
204         }
205
206         dev_dbg(dev, "set alarm: %ptRd %ptRt\n", &alarm->time, &alarm->time);
207
208         return 0;
209 }
210
211 static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
212 {
213         struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
214         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
215         u8 value[NUM_8_BIT_RTC_REGS];
216         unsigned int ctrl_reg;
217         u32 secs;
218         int rc;
219
220         rc = regmap_bulk_read(rtc_dd->regmap, regs->alarm_rw, value,
221                               sizeof(value));
222         if (rc)
223                 return rc;
224
225         secs = get_unaligned_le32(value);
226         rtc_time64_to_tm(secs, &alarm->time);
227
228         rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg);
229         if (rc)
230                 return rc;
231
232         alarm->enabled = !!(ctrl_reg & PM8xxx_RTC_ALARM_ENABLE);
233
234         dev_dbg(dev, "read alarm: %ptRd %ptRt\n", &alarm->time, &alarm->time);
235
236         return 0;
237 }
238
239 static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
240 {
241         struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
242         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
243         u8 value[NUM_8_BIT_RTC_REGS] = {0};
244         unsigned int val;
245         int rc;
246
247         if (enable)
248                 val = regs->alarm_en;
249         else
250                 val = 0;
251
252         rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
253                                 regs->alarm_en, val);
254         if (rc)
255                 return rc;
256
257         /* Clear alarm register */
258         if (!enable) {
259                 rc = regmap_bulk_write(rtc_dd->regmap, regs->alarm_rw, value,
260                                        sizeof(value));
261                 if (rc)
262                         return rc;
263         }
264
265         return 0;
266 }
267
268 static const struct rtc_class_ops pm8xxx_rtc_ops = {
269         .read_time      = pm8xxx_rtc_read_time,
270         .set_time       = pm8xxx_rtc_set_time,
271         .set_alarm      = pm8xxx_rtc_set_alarm,
272         .read_alarm     = pm8xxx_rtc_read_alarm,
273         .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
274 };
275
276 static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
277 {
278         struct pm8xxx_rtc *rtc_dd = dev_id;
279         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
280         int rc;
281
282         rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
283
284         /* Disable alarm */
285         rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl,
286                                 regs->alarm_en, 0);
287         if (rc)
288                 return IRQ_NONE;
289
290         /* Clear alarm status */
291         rc = regmap_update_bits(rtc_dd->regmap, regs->alarm_ctrl2,
292                                 PM8xxx_RTC_ALARM_CLEAR, 0);
293         if (rc)
294                 return IRQ_NONE;
295
296         return IRQ_HANDLED;
297 }
298
299 static int pm8xxx_rtc_enable(struct pm8xxx_rtc *rtc_dd)
300 {
301         const struct pm8xxx_rtc_regs *regs = rtc_dd->regs;
302
303         return regmap_update_bits(rtc_dd->regmap, regs->ctrl, PM8xxx_RTC_ENABLE,
304                                   PM8xxx_RTC_ENABLE);
305 }
306
307 static const struct pm8xxx_rtc_regs pm8921_regs = {
308         .ctrl           = 0x11d,
309         .write          = 0x11f,
310         .read           = 0x123,
311         .alarm_rw       = 0x127,
312         .alarm_ctrl     = 0x11d,
313         .alarm_ctrl2    = 0x11e,
314         .alarm_en       = BIT(1),
315 };
316
317 static const struct pm8xxx_rtc_regs pm8058_regs = {
318         .ctrl           = 0x1e8,
319         .write          = 0x1ea,
320         .read           = 0x1ee,
321         .alarm_rw       = 0x1f2,
322         .alarm_ctrl     = 0x1e8,
323         .alarm_ctrl2    = 0x1e9,
324         .alarm_en       = BIT(1),
325 };
326
327 static const struct pm8xxx_rtc_regs pm8941_regs = {
328         .ctrl           = 0x6046,
329         .write          = 0x6040,
330         .read           = 0x6048,
331         .alarm_rw       = 0x6140,
332         .alarm_ctrl     = 0x6146,
333         .alarm_ctrl2    = 0x6148,
334         .alarm_en       = BIT(7),
335 };
336
337 static const struct pm8xxx_rtc_regs pmk8350_regs = {
338         .ctrl           = 0x6146,
339         .write          = 0x6140,
340         .read           = 0x6148,
341         .alarm_rw       = 0x6240,
342         .alarm_ctrl     = 0x6246,
343         .alarm_ctrl2    = 0x6248,
344         .alarm_en       = BIT(7),
345 };
346
347 static const struct of_device_id pm8xxx_id_table[] = {
348         { .compatible = "qcom,pm8921-rtc", .data = &pm8921_regs },
349         { .compatible = "qcom,pm8058-rtc", .data = &pm8058_regs },
350         { .compatible = "qcom,pm8941-rtc", .data = &pm8941_regs },
351         { .compatible = "qcom,pmk8350-rtc", .data = &pmk8350_regs },
352         { },
353 };
354 MODULE_DEVICE_TABLE(of, pm8xxx_id_table);
355
356 static int pm8xxx_rtc_probe(struct platform_device *pdev)
357 {
358         const struct of_device_id *match;
359         struct pm8xxx_rtc *rtc_dd;
360         int rc;
361
362         match = of_match_node(pm8xxx_id_table, pdev->dev.of_node);
363         if (!match)
364                 return -ENXIO;
365
366         rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
367         if (rtc_dd == NULL)
368                 return -ENOMEM;
369
370         rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
371         if (!rtc_dd->regmap)
372                 return -ENXIO;
373
374         rtc_dd->alarm_irq = platform_get_irq(pdev, 0);
375         if (rtc_dd->alarm_irq < 0)
376                 return -ENXIO;
377
378         rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node,
379                                                       "allow-set-time");
380
381         rtc_dd->regs = match->data;
382         rtc_dd->dev = &pdev->dev;
383
384         rc = pm8xxx_rtc_enable(rtc_dd);
385         if (rc)
386                 return rc;
387
388         platform_set_drvdata(pdev, rtc_dd);
389
390         device_init_wakeup(&pdev->dev, 1);
391
392         rtc_dd->rtc = devm_rtc_allocate_device(&pdev->dev);
393         if (IS_ERR(rtc_dd->rtc))
394                 return PTR_ERR(rtc_dd->rtc);
395
396         rtc_dd->rtc->ops = &pm8xxx_rtc_ops;
397         rtc_dd->rtc->range_max = U32_MAX;
398
399         rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->alarm_irq,
400                                           pm8xxx_alarm_trigger,
401                                           IRQF_TRIGGER_RISING,
402                                           "pm8xxx_rtc_alarm", rtc_dd);
403         if (rc < 0)
404                 return rc;
405
406         rc = devm_rtc_register_device(rtc_dd->rtc);
407         if (rc)
408                 return rc;
409
410         rc = dev_pm_set_wake_irq(&pdev->dev, rtc_dd->alarm_irq);
411         if (rc)
412                 return rc;
413
414         return 0;
415 }
416
417 static int pm8xxx_remove(struct platform_device *pdev)
418 {
419         dev_pm_clear_wake_irq(&pdev->dev);
420         return 0;
421 }
422
423 static struct platform_driver pm8xxx_rtc_driver = {
424         .probe          = pm8xxx_rtc_probe,
425         .remove         = pm8xxx_remove,
426         .driver = {
427                 .name           = "rtc-pm8xxx",
428                 .of_match_table = pm8xxx_id_table,
429         },
430 };
431
432 module_platform_driver(pm8xxx_rtc_driver);
433
434 MODULE_ALIAS("platform:rtc-pm8xxx");
435 MODULE_DESCRIPTION("PMIC8xxx RTC driver");
436 MODULE_LICENSE("GPL v2");
437 MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");