blob: 4b7a1d8e764e3278cc9fc8273768452502a7fe48 [file] [log] [blame]
xiaolin340e5b72019-09-02 11:50:48 +08001/* Copyright (c) 2012-2015, 2017-2019,The Linux Foundation. All rights reserved.
David Collins8885f792017-01-26 14:36:34 -08002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/regmap.h>
15#include <linux/init.h>
16#include <linux/rtc.h>
17#include <linux/pm.h>
18#include <linux/slab.h>
19#include <linux/idr.h>
20#include <linux/of_device.h>
21#include <linux/of_irq.h>
22#include <linux/spmi.h>
23#include <linux/platform_device.h>
24#include <linux/spinlock.h>
25#include <linux/alarmtimer.h>
26
27/* RTC/ALARM Register offsets */
28#define REG_OFFSET_ALARM_RW 0x40
29#define REG_OFFSET_ALARM_CTRL1 0x46
30#define REG_OFFSET_ALARM_CTRL2 0x48
31#define REG_OFFSET_RTC_WRITE 0x40
32#define REG_OFFSET_RTC_CTRL 0x46
33#define REG_OFFSET_RTC_READ 0x48
34#define REG_OFFSET_PERP_SUBTYPE 0x05
35
36/* RTC_CTRL register bit fields */
37#define BIT_RTC_ENABLE BIT(7)
38#define BIT_RTC_ALARM_ENABLE BIT(7)
39#define BIT_RTC_ABORT_ENABLE BIT(0)
40#define BIT_RTC_ALARM_CLEAR BIT(0)
41
42/* RTC/ALARM peripheral subtype values */
43#define RTC_PERPH_SUBTYPE 0x1
44#define ALARM_PERPH_SUBTYPE 0x3
45
46#define NUM_8_BIT_RTC_REGS 0x4
47
48#define TO_SECS(arr) (arr[0] | (arr[1] << 8) | (arr[2] << 16) | \
49 (arr[3] << 24))
50
51/* Module parameter to control power-on-alarm */
52bool poweron_alarm;
53EXPORT_SYMBOL(poweron_alarm);
54module_param(poweron_alarm, bool, 0644);
55MODULE_PARM_DESC(poweron_alarm, "Enable/Disable power-on alarm");
56
57/* rtc driver internal structure */
58struct qpnp_rtc {
59 u8 rtc_ctrl_reg;
60 u8 alarm_ctrl_reg1;
61 u16 rtc_base;
62 u16 alarm_base;
63 u32 rtc_write_enable;
64 u32 rtc_alarm_powerup;
65 int rtc_alarm_irq;
66 struct device *rtc_dev;
67 struct rtc_device *rtc;
68 struct platform_device *pdev;
69 struct regmap *regmap;
70 spinlock_t alarm_ctrl_lock;
71};
72
73static int qpnp_read_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val,
74 u16 base, int count)
75{
76 int rc;
77
78 rc = regmap_bulk_read(rtc_dd->regmap, base, rtc_val, count);
79 if (rc) {
80 dev_err(rtc_dd->rtc_dev, "SPMI read failed\n");
81 return rc;
82 }
83 return 0;
84}
85
86static int qpnp_write_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val,
87 u16 base, int count)
88{
89 int rc;
90
91 rc = regmap_bulk_write(rtc_dd->regmap, base, rtc_val, count);
92 if (rc) {
93 dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
94 return rc;
95 }
96
97 return 0;
98}
99
100static int
101qpnp_rtc_set_time(struct device *dev, struct rtc_time *tm)
102{
103 int rc;
104 unsigned long secs, irq_flags;
105 u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg;
106 u8 rtc_disabled = 0, rtc_ctrl_reg;
107 struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
108
109 rtc_tm_to_time(tm, &secs);
110
111 value[0] = secs & 0xFF;
112 value[1] = (secs >> 8) & 0xFF;
113 value[2] = (secs >> 16) & 0xFF;
114 value[3] = (secs >> 24) & 0xFF;
115
116 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
117
118 spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
119 ctrl_reg = rtc_dd->alarm_ctrl_reg1;
120
121 if (ctrl_reg & BIT_RTC_ALARM_ENABLE) {
122 alarm_enabled = 1;
123 ctrl_reg &= ~BIT_RTC_ALARM_ENABLE;
124 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
125 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
126 if (rc) {
127 dev_err(dev, "Write to ALARM ctrl reg failed\n");
128 goto rtc_rw_fail;
129 }
130 } else
131 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
132
133 /*
134 * 32 bit seconds value is coverted to four 8 bit values
135 * |<------ 32 bit time value in seconds ------>|
136 * <- 8 bit ->|<- 8 bit ->|<- 8 bit ->|<- 8 bit ->|
137 * ----------------------------------------------
138 * | BYTE[3] | BYTE[2] | BYTE[1] | BYTE[0] |
139 * ----------------------------------------------
140 *
141 * RTC has four 8 bit registers for writing time in seconds:
142 * WDATA[3], WDATA[2], WDATA[1], WDATA[0]
143 *
144 * Write to the RTC registers should be done in following order
145 * Clear WDATA[0] register
146 *
147 * Write BYTE[1], BYTE[2] and BYTE[3] of time to
148 * RTC WDATA[3], WDATA[2], WDATA[1] registers
149 *
150 * Write BYTE[0] of time to RTC WDATA[0] register
151 *
152 * Clearing BYTE[0] and writing in the end will prevent any
153 * unintentional overflow from WDATA[0] to higher bytes during the
154 * write operation
155 */
156
157 /* Disable RTC H/w before writing on RTC register*/
158 rtc_ctrl_reg = rtc_dd->rtc_ctrl_reg;
159 if (rtc_ctrl_reg & BIT_RTC_ENABLE) {
160 rtc_disabled = 1;
161 rtc_ctrl_reg &= ~BIT_RTC_ENABLE;
162 rc = qpnp_write_wrapper(rtc_dd, &rtc_ctrl_reg,
163 rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
164 if (rc) {
165 dev_err(dev, "Disabling of RTC control reg failed with error:%d\n",
166 rc);
167 goto rtc_rw_fail;
168 }
169 rtc_dd->rtc_ctrl_reg = rtc_ctrl_reg;
170 }
171
172 /* Clear WDATA[0] */
173 reg = 0x0;
174 rc = qpnp_write_wrapper(rtc_dd, &reg,
175 rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1);
176 if (rc) {
177 dev_err(dev, "Write to RTC reg failed\n");
178 goto rtc_rw_fail;
179 }
180
181 /* Write to WDATA[3], WDATA[2] and WDATA[1] */
182 rc = qpnp_write_wrapper(rtc_dd, &value[1],
183 rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE + 1, 3);
184 if (rc) {
185 dev_err(dev, "Write to RTC reg failed\n");
186 goto rtc_rw_fail;
187 }
188
189 /* Write to WDATA[0] */
190 rc = qpnp_write_wrapper(rtc_dd, value,
191 rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1);
192 if (rc) {
193 dev_err(dev, "Write to RTC reg failed\n");
194 goto rtc_rw_fail;
195 }
196
197 /* Enable RTC H/w after writing on RTC register*/
198 if (rtc_disabled) {
199 rtc_ctrl_reg |= BIT_RTC_ENABLE;
200 rc = qpnp_write_wrapper(rtc_dd, &rtc_ctrl_reg,
201 rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
202 if (rc) {
203 dev_err(dev, "Enabling of RTC control reg failed with error:%d\n",
204 rc);
205 goto rtc_rw_fail;
206 }
207 rtc_dd->rtc_ctrl_reg = rtc_ctrl_reg;
208 }
209
210 if (alarm_enabled) {
211 ctrl_reg |= BIT_RTC_ALARM_ENABLE;
212 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
213 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
214 if (rc) {
215 dev_err(dev, "Write to ALARM ctrl reg failed\n");
216 goto rtc_rw_fail;
217 }
218 }
219
220 rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
221
222rtc_rw_fail:
223 if (alarm_enabled)
224 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
225
226 return rc;
227}
228
229static int
230qpnp_rtc_read_time(struct device *dev, struct rtc_time *tm)
231{
232 int rc;
233 u8 value[4], reg;
234 unsigned long secs;
235 struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
236
237 rc = qpnp_read_wrapper(rtc_dd, value,
238 rtc_dd->rtc_base + REG_OFFSET_RTC_READ,
239 NUM_8_BIT_RTC_REGS);
240 if (rc) {
241 dev_err(dev, "Read from RTC reg failed\n");
242 return rc;
243 }
244
245 /*
246 * Read the LSB again and check if there has been a carry over
247 * If there is, redo the read operation
248 */
249 rc = qpnp_read_wrapper(rtc_dd, &reg,
250 rtc_dd->rtc_base + REG_OFFSET_RTC_READ, 1);
251 if (rc) {
252 dev_err(dev, "Read from RTC reg failed\n");
253 return rc;
254 }
255
256 if (reg < value[0]) {
257 rc = qpnp_read_wrapper(rtc_dd, value,
258 rtc_dd->rtc_base + REG_OFFSET_RTC_READ,
259 NUM_8_BIT_RTC_REGS);
260 if (rc) {
261 dev_err(dev, "Read from RTC reg failed\n");
262 return rc;
263 }
264 }
265
266 secs = TO_SECS(value);
267
268 rtc_time_to_tm(secs, tm);
269
270 rc = rtc_valid_tm(tm);
271 if (rc) {
272 dev_err(dev, "Invalid time read from RTC\n");
273 return rc;
274 }
275
276 dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
277 secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
278 tm->tm_mday, tm->tm_mon, tm->tm_year);
279
280 return 0;
281}
282
283static int
284qpnp_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
285{
286 int rc;
287 u8 value[4], ctrl_reg;
288 unsigned long secs, secs_rtc, irq_flags;
289 struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
290 struct rtc_time rtc_tm;
291
292 rtc_tm_to_time(&alarm->time, &secs);
293
294 /*
295 * Read the current RTC time and verify if the alarm time is in the
296 * past. If yes, return invalid
297 */
298 rc = qpnp_rtc_read_time(dev, &rtc_tm);
299 if (rc) {
300 dev_err(dev, "Unable to read RTC time\n");
301 return -EINVAL;
302 }
303
304 rtc_tm_to_time(&rtc_tm, &secs_rtc);
305 if (secs < secs_rtc) {
306 dev_err(dev, "Trying to set alarm in the past\n");
307 return -EINVAL;
308 }
309
310 value[0] = secs & 0xFF;
311 value[1] = (secs >> 8) & 0xFF;
312 value[2] = (secs >> 16) & 0xFF;
313 value[3] = (secs >> 24) & 0xFF;
314
315 spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
316
317 rc = qpnp_write_wrapper(rtc_dd, value,
318 rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
319 NUM_8_BIT_RTC_REGS);
320 if (rc) {
321 dev_err(dev, "Write to ALARM reg failed\n");
322 goto rtc_rw_fail;
323 }
324
325 ctrl_reg = (alarm->enabled) ?
326 (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE) :
327 (rtc_dd->alarm_ctrl_reg1 & ~BIT_RTC_ALARM_ENABLE);
328
329 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
330 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
331 if (rc) {
332 dev_err(dev, "Write to ALARM cntrol reg failed\n");
333 goto rtc_rw_fail;
334 }
335
336 rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
337
338 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
339 alarm->time.tm_hour, alarm->time.tm_min,
340 alarm->time.tm_sec, alarm->time.tm_mday,
341 alarm->time.tm_mon, alarm->time.tm_year);
342rtc_rw_fail:
343 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
344 return rc;
345}
346
347static int
348qpnp_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
349{
350 int rc;
351 u8 value[4];
352 unsigned long secs;
353 struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
354
355 rc = qpnp_read_wrapper(rtc_dd, value,
356 rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
357 NUM_8_BIT_RTC_REGS);
358 if (rc) {
359 dev_err(dev, "Read from ALARM reg failed\n");
360 return rc;
361 }
362
363 secs = TO_SECS(value);
364 rtc_time_to_tm(secs, &alarm->time);
365
366 rc = rtc_valid_tm(&alarm->time);
367 if (rc) {
368 dev_err(dev, "Invalid time read from RTC\n");
369 return rc;
370 }
371
372 dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
373 alarm->time.tm_hour, alarm->time.tm_min,
374 alarm->time.tm_sec, alarm->time.tm_mday,
375 alarm->time.tm_mon, alarm->time.tm_year);
376
Mao Jinlong0b0987b2017-08-02 16:14:19 +0800377 rc = qpnp_read_wrapper(rtc_dd, value,
378 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
379 if (rc) {
380 dev_err(dev, "Read from ALARM CTRL1 failed\n");
381 return rc;
382 }
383
384 alarm->enabled = !!(value[0] & BIT_RTC_ALARM_ENABLE);
385
David Collins8885f792017-01-26 14:36:34 -0800386 return 0;
387}
388
389
390static int
391qpnp_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
392{
393 int rc;
394 unsigned long irq_flags;
395 struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
396 u8 ctrl_reg;
397 u8 value[4] = {0};
398
399 spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
400 ctrl_reg = rtc_dd->alarm_ctrl_reg1;
401 ctrl_reg = enabled ? (ctrl_reg | BIT_RTC_ALARM_ENABLE) :
402 (ctrl_reg & ~BIT_RTC_ALARM_ENABLE);
403
404 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
405 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
406 if (rc) {
407 dev_err(dev, "Write to ALARM control reg failed\n");
408 goto rtc_rw_fail;
409 }
410
411 rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
412
413 /* Clear Alarm register */
414 if (!enabled) {
415 rc = qpnp_write_wrapper(rtc_dd, value,
416 rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
417 NUM_8_BIT_RTC_REGS);
418 if (rc)
419 dev_err(dev, "Clear ALARM value reg failed\n");
420 }
421
422rtc_rw_fail:
423 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
424 return rc;
425}
426
427static const struct rtc_class_ops qpnp_rtc_ro_ops = {
428 .read_time = qpnp_rtc_read_time,
429 .set_alarm = qpnp_rtc_set_alarm,
430 .read_alarm = qpnp_rtc_read_alarm,
431 .alarm_irq_enable = qpnp_rtc_alarm_irq_enable,
432};
433
434static const struct rtc_class_ops qpnp_rtc_rw_ops = {
435 .read_time = qpnp_rtc_read_time,
436 .set_alarm = qpnp_rtc_set_alarm,
437 .read_alarm = qpnp_rtc_read_alarm,
438 .alarm_irq_enable = qpnp_rtc_alarm_irq_enable,
439 .set_time = qpnp_rtc_set_time,
440};
441
442static irqreturn_t qpnp_alarm_trigger(int irq, void *dev_id)
443{
444 struct qpnp_rtc *rtc_dd = dev_id;
445 u8 ctrl_reg;
446 int rc;
447 unsigned long irq_flags;
448
449 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
450
451 spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
452
453 /* Clear the alarm enable bit */
454 ctrl_reg = rtc_dd->alarm_ctrl_reg1;
455 ctrl_reg &= ~BIT_RTC_ALARM_ENABLE;
456
457 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
458 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
459 if (rc) {
460 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
461 dev_err(rtc_dd->rtc_dev,
462 "Write to ALARM control reg failed\n");
463 goto rtc_alarm_handled;
464 }
465
466 rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
467 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
468
469 /* Set ALARM_CLR bit */
470 ctrl_reg = 0x1;
471 rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
472 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL2, 1);
473 if (rc)
474 dev_err(rtc_dd->rtc_dev,
475 "Write to ALARM control reg failed\n");
476
477rtc_alarm_handled:
478 return IRQ_HANDLED;
479}
480
481static int qpnp_rtc_probe(struct platform_device *pdev)
482{
483 const struct rtc_class_ops *rtc_ops = &qpnp_rtc_ro_ops;
484 int rc;
485 u8 subtype;
486 struct qpnp_rtc *rtc_dd;
487 unsigned int base;
488 struct device_node *child;
489
490 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
491 if (rtc_dd == NULL)
492 return -ENOMEM;
493
494 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
495 if (!rtc_dd->regmap) {
496 dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
497 return -EINVAL;
498 }
499
500 /* Get the rtc write property */
501 rc = of_property_read_u32(pdev->dev.of_node, "qcom,qpnp-rtc-write",
502 &rtc_dd->rtc_write_enable);
503 if (rc && rc != -EINVAL) {
504 dev_err(&pdev->dev,
505 "Error reading rtc_write_enable property %d\n", rc);
506 return rc;
507 }
508
509 rc = of_property_read_u32(pdev->dev.of_node,
510 "qcom,qpnp-rtc-alarm-pwrup",
511 &rtc_dd->rtc_alarm_powerup);
512 if (rc && rc != -EINVAL) {
513 dev_err(&pdev->dev,
514 "Error reading rtc_alarm_powerup property %d\n", rc);
515 return rc;
516 }
517
518 /* Initialise spinlock to protect RTC control register */
519 spin_lock_init(&rtc_dd->alarm_ctrl_lock);
520
521 rtc_dd->rtc_dev = &(pdev->dev);
522 rtc_dd->pdev = pdev;
523
524
525 if (of_get_available_child_count(pdev->dev.of_node) == 0) {
526 pr_err("no child nodes\n");
527 rc = -ENXIO;
528 goto fail_rtc_enable;
529 }
530
531 /* Get RTC/ALARM resources */
532 for_each_available_child_of_node(pdev->dev.of_node, child) {
533 rc = of_property_read_u32(child, "reg", &base);
534 if (rc < 0) {
535 dev_err(&pdev->dev,
536 "Couldn't find reg in node = %s rc = %d\n",
537 child->full_name, rc);
538 goto fail_rtc_enable;
539 }
540
541 rc = qpnp_read_wrapper(rtc_dd, &subtype,
542 base + REG_OFFSET_PERP_SUBTYPE, 1);
543 if (rc) {
544 dev_err(&pdev->dev,
545 "Peripheral subtype read failed\n");
546 goto fail_rtc_enable;
547 }
548
549 switch (subtype) {
550 case RTC_PERPH_SUBTYPE:
551 rtc_dd->rtc_base = base;
552 break;
553 case ALARM_PERPH_SUBTYPE:
554 rtc_dd->alarm_base = base;
555 rtc_dd->rtc_alarm_irq = of_irq_get(child, 0);
556 if (rtc_dd->rtc_alarm_irq < 0) {
557 dev_err(&pdev->dev, "ALARM IRQ absent\n");
558 rc = -ENXIO;
559 goto fail_rtc_enable;
560 }
561 break;
562 default:
563 dev_err(&pdev->dev, "Invalid peripheral subtype\n");
564 rc = -EINVAL;
565 goto fail_rtc_enable;
566 }
567 }
568
569 rc = qpnp_read_wrapper(rtc_dd, &rtc_dd->rtc_ctrl_reg,
570 rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
571 if (rc) {
572 dev_err(&pdev->dev, "Read from RTC control reg failed\n");
573 goto fail_rtc_enable;
574 }
575
576 if (!(rtc_dd->rtc_ctrl_reg & BIT_RTC_ENABLE)) {
577 dev_err(&pdev->dev, "RTC h/w disabled, rtc not registered\n");
578 goto fail_rtc_enable;
579 }
580
581 rc = qpnp_read_wrapper(rtc_dd, &rtc_dd->alarm_ctrl_reg1,
582 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
583 if (rc) {
584 dev_err(&pdev->dev, "Read from Alarm control reg failed\n");
585 goto fail_rtc_enable;
586 }
587 /* Enable abort enable feature */
588 rtc_dd->alarm_ctrl_reg1 |= BIT_RTC_ABORT_ENABLE;
589 rc = qpnp_write_wrapper(rtc_dd, &rtc_dd->alarm_ctrl_reg1,
590 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
591 if (rc) {
592 dev_err(&pdev->dev, "SPMI write failed!\n");
593 goto fail_rtc_enable;
594 }
595
596 if (rtc_dd->rtc_write_enable == true)
597 rtc_ops = &qpnp_rtc_rw_ops;
598
599 dev_set_drvdata(&pdev->dev, rtc_dd);
xiaolin340e5b72019-09-02 11:50:48 +0800600 device_init_wakeup(&pdev->dev, 1);
David Collins8885f792017-01-26 14:36:34 -0800601 /* Register the RTC device */
602 rtc_dd->rtc = rtc_device_register("qpnp_rtc", &pdev->dev,
603 rtc_ops, THIS_MODULE);
604 if (IS_ERR(rtc_dd->rtc)) {
605 dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
606 __func__, PTR_ERR(rtc_dd->rtc));
607 rc = PTR_ERR(rtc_dd->rtc);
608 goto fail_rtc_enable;
609 }
610
David Collins8885f792017-01-26 14:36:34 -0800611 /* Request the alarm IRQ */
612 rc = request_any_context_irq(rtc_dd->rtc_alarm_irq,
613 qpnp_alarm_trigger, IRQF_TRIGGER_RISING,
614 "qpnp_rtc_alarm", rtc_dd);
615 if (rc) {
616 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
617 goto fail_req_irq;
618 }
619
620 device_init_wakeup(&pdev->dev, 1);
621 enable_irq_wake(rtc_dd->rtc_alarm_irq);
622
623 dev_dbg(&pdev->dev, "Probe success !!\n");
624
625 return 0;
626
627fail_req_irq:
628 rtc_device_unregister(rtc_dd->rtc);
629fail_rtc_enable:
xiaolin340e5b72019-09-02 11:50:48 +0800630 device_init_wakeup(&pdev->dev, 0);
David Collins8885f792017-01-26 14:36:34 -0800631 dev_set_drvdata(&pdev->dev, NULL);
632
633 return rc;
634}
635
636static int qpnp_rtc_remove(struct platform_device *pdev)
637{
638 struct qpnp_rtc *rtc_dd = dev_get_drvdata(&pdev->dev);
639
640 device_init_wakeup(&pdev->dev, 0);
641 free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
642 rtc_device_unregister(rtc_dd->rtc);
643 dev_set_drvdata(&pdev->dev, NULL);
644
645 return 0;
646}
647
648static void qpnp_rtc_shutdown(struct platform_device *pdev)
649{
650 u8 value[4] = {0};
651 u8 reg;
652 int rc;
653 unsigned long irq_flags;
654 struct qpnp_rtc *rtc_dd;
655 bool rtc_alarm_powerup;
656
657 if (!pdev) {
658 pr_err("qpnp-rtc: spmi device not found\n");
659 return;
660 }
661 rtc_dd = dev_get_drvdata(&pdev->dev);
662 if (!rtc_dd) {
663 pr_err("qpnp-rtc: rtc driver data not found\n");
664 return;
665 }
666 rtc_alarm_powerup = rtc_dd->rtc_alarm_powerup;
667 if (!rtc_alarm_powerup && !poweron_alarm) {
668 spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
669 dev_dbg(&pdev->dev, "Disabling alarm interrupts\n");
670
671 /* Disable RTC alarms */
672 reg = rtc_dd->alarm_ctrl_reg1;
673 reg &= ~BIT_RTC_ALARM_ENABLE;
674 rc = qpnp_write_wrapper(rtc_dd, &reg,
675 rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
676 if (rc) {
677 dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
678 goto fail_alarm_disable;
679 }
680
681 /* Clear Alarm register */
682 rc = qpnp_write_wrapper(rtc_dd, value,
683 rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
684 NUM_8_BIT_RTC_REGS);
685 if (rc)
686 dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
687
688fail_alarm_disable:
689 spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
690 }
691}
692
693static const struct of_device_id spmi_match_table[] = {
694 {
695 .compatible = "qcom,qpnp-rtc",
696 },
697 {}
698};
699
700static struct platform_driver qpnp_rtc_driver = {
701 .probe = qpnp_rtc_probe,
702 .remove = qpnp_rtc_remove,
703 .shutdown = qpnp_rtc_shutdown,
704 .driver = {
705 .name = "qcom,qpnp-rtc",
706 .owner = THIS_MODULE,
707 .of_match_table = spmi_match_table,
708 },
709};
710
711static int __init qpnp_rtc_init(void)
712{
713 return platform_driver_register(&qpnp_rtc_driver);
714}
715module_init(qpnp_rtc_init);
716
717static void __exit qpnp_rtc_exit(void)
718{
719 platform_driver_unregister(&qpnp_rtc_driver);
720}
721module_exit(qpnp_rtc_exit);
722
723MODULE_DESCRIPTION("SMPI PMIC RTC driver");
724MODULE_LICENSE("GPL v2");