blob: 8c7a98a5452c6a2020d326499696fb4f84c9a97e [file] [log] [blame]
Alexandre Bellonif5fae392019-04-30 11:33:02 +02001// SPDX-License-Identifier: GPL-2.0
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -07002/*
3 * pcap rtc code for Motorola EZX phones
4 *
5 * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com>
6 * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
7 *
8 * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -07009 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/mfd/ezx-pcap.h>
15#include <linux/rtc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070017#include <linux/platform_device.h>
18
19struct pcap_rtc {
20 struct pcap_chip *pcap;
21 struct rtc_device *rtc;
22};
23
24static irqreturn_t pcap_rtc_irq(int irq, void *_pcap_rtc)
25{
26 struct pcap_rtc *pcap_rtc = _pcap_rtc;
27 unsigned long rtc_events;
28
29 if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ))
30 rtc_events = RTC_IRQF | RTC_UF;
31 else if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA))
32 rtc_events = RTC_IRQF | RTC_AF;
33 else
34 rtc_events = 0;
35
36 rtc_update_irq(pcap_rtc->rtc, 1, rtc_events);
37 return IRQ_HANDLED;
38}
39
40static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
41{
Wolfram Sang85368bb2018-04-19 16:06:14 +020042 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070043 struct rtc_time *tm = &alrm->time;
44 unsigned long secs;
45 u32 tod; /* time of day, seconds since midnight */
46 u32 days; /* days since 1/1/1970 */
47
48 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod);
49 secs = tod & PCAP_RTC_TOD_MASK;
50
51 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days);
52 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
53
Alexandre Belloni03745d12019-04-30 11:33:00 +020054 rtc_time64_to_tm(secs, tm);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070055
56 return 0;
57}
58
59static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
60{
Wolfram Sang85368bb2018-04-19 16:06:14 +020061 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
Alexandre Belloni03745d12019-04-30 11:33:00 +020062 unsigned long secs = rtc_tm_to_time64(&alrm->time);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070063 u32 tod, days;
64
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070065 tod = secs % SEC_PER_DAY;
66 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod);
67
68 days = secs / SEC_PER_DAY;
69 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days);
70
71 return 0;
72}
73
74static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm)
75{
Wolfram Sang85368bb2018-04-19 16:06:14 +020076 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070077 unsigned long secs;
78 u32 tod, days;
79
80 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod);
81 secs = tod & PCAP_RTC_TOD_MASK;
82
83 ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days);
84 secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY;
85
Alexandre Belloni03745d12019-04-30 11:33:00 +020086 rtc_time64_to_tm(secs, tm);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070087
Alexandre Belloniab626702018-02-19 16:23:55 +010088 return 0;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070089}
90
Alexandre Bellonic8b599e2019-04-30 11:33:01 +020091static int pcap_rtc_set_time(struct device *dev, struct rtc_time *tm)
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070092{
Wolfram Sang85368bb2018-04-19 16:06:14 +020093 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
Alexandre Bellonic8b599e2019-04-30 11:33:01 +020094 unsigned long secs = rtc_tm_to_time64(tm);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -070095 u32 tod, days;
96
97 tod = secs % SEC_PER_DAY;
98 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod);
99
100 days = secs / SEC_PER_DAY;
101 ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days);
102
103 return 0;
104}
105
106static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en)
107{
Wolfram Sang85368bb2018-04-19 16:06:14 +0200108 struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700109
110 if (en)
111 enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
112 else
113 disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq));
114
115 return 0;
116}
117
118static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
119{
120 return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en);
121}
122
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700123static const struct rtc_class_ops pcap_rtc_ops = {
124 .read_time = pcap_rtc_read_time,
Alexandre Bellonic8b599e2019-04-30 11:33:01 +0200125 .set_time = pcap_rtc_set_time,
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700126 .read_alarm = pcap_rtc_read_alarm,
127 .set_alarm = pcap_rtc_set_alarm,
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700128 .alarm_irq_enable = pcap_rtc_alarm_irq_enable,
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700129};
130
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700131static int __init pcap_rtc_probe(struct platform_device *pdev)
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700132{
133 struct pcap_rtc *pcap_rtc;
134 int timer_irq, alarm_irq;
135 int err = -ENOMEM;
136
Jingoo Han6b5f4862013-04-29 16:20:47 -0700137 pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc),
138 GFP_KERNEL);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700139 if (!pcap_rtc)
140 return err;
141
142 pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent);
143
John Stultz4b3687f2011-05-06 17:30:57 -0700144 platform_set_drvdata(pdev, pcap_rtc);
145
Alexandre Bellonid1403c42019-04-30 11:32:59 +0200146 pcap_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
Jingoo Hana1c805a2013-07-03 15:06:32 -0700147 if (IS_ERR(pcap_rtc->rtc))
148 return PTR_ERR(pcap_rtc->rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700149
Alexandre Bellonid1403c42019-04-30 11:32:59 +0200150 pcap_rtc->rtc->ops = &pcap_rtc_ops;
151 pcap_rtc->rtc->range_max = (1 << 14) * 86400ULL - 1;
152
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700153 timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ);
154 alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA);
155
Jingoo Han6b5f4862013-04-29 16:20:47 -0700156 err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0,
157 "RTC Timer", pcap_rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700158 if (err)
Jingoo Hana1c805a2013-07-03 15:06:32 -0700159 return err;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700160
Jingoo Han6b5f4862013-04-29 16:20:47 -0700161 err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0,
162 "RTC Alarm", pcap_rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700163 if (err)
Jingoo Hana1c805a2013-07-03 15:06:32 -0700164 return err;
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700165
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100166 return devm_rtc_register_device(pcap_rtc->rtc);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700167}
168
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700169static int __exit pcap_rtc_remove(struct platform_device *pdev)
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700170{
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700171 return 0;
172}
173
174static struct platform_driver pcap_rtc_driver = {
Jingoo Han5cc2b9c2013-04-29 16:18:26 -0700175 .remove = __exit_p(pcap_rtc_remove),
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700176 .driver = {
177 .name = "pcap-rtc",
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700178 },
179};
180
Jingoo Han0de4c7c2013-04-29 16:18:47 -0700181module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe);
Daniel Ribeirod3c7a3f2009-09-22 16:46:27 -0700182
183MODULE_DESCRIPTION("Motorola pcap rtc driver");
184MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>");
185MODULE_LICENSE("GPL");