Alexandre Belloni | f5fae39 | 2019-04-30 11:33:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 2 | /* |
| 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 Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 9 | */ |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | |
| 19 | struct pcap_rtc { |
| 20 | struct pcap_chip *pcap; |
| 21 | struct rtc_device *rtc; |
| 22 | }; |
| 23 | |
| 24 | static 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 | |
| 40 | static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 41 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 42 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 43 | 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 Belloni | 03745d1 | 2019-04-30 11:33:00 +0200 | [diff] [blame] | 54 | rtc_time64_to_tm(secs, tm); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 60 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 61 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Alexandre Belloni | 03745d1 | 2019-04-30 11:33:00 +0200 | [diff] [blame] | 62 | unsigned long secs = rtc_tm_to_time64(&alrm->time); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 63 | u32 tod, days; |
| 64 | |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 65 | 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 | |
| 74 | static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 75 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 76 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 77 | 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 Belloni | 03745d1 | 2019-04-30 11:33:00 +0200 | [diff] [blame] | 86 | rtc_time64_to_tm(secs, tm); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 87 | |
Alexandre Belloni | ab62670 | 2018-02-19 16:23:55 +0100 | [diff] [blame] | 88 | return 0; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alexandre Belloni | c8b599e | 2019-04-30 11:33:01 +0200 | [diff] [blame] | 91 | static int pcap_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 92 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 93 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Alexandre Belloni | c8b599e | 2019-04-30 11:33:01 +0200 | [diff] [blame] | 94 | unsigned long secs = rtc_tm_to_time64(tm); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 95 | 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 | |
| 106 | static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en) |
| 107 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 108 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 109 | |
| 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 | |
| 118 | static 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 Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 123 | static const struct rtc_class_ops pcap_rtc_ops = { |
| 124 | .read_time = pcap_rtc_read_time, |
Alexandre Belloni | c8b599e | 2019-04-30 11:33:01 +0200 | [diff] [blame] | 125 | .set_time = pcap_rtc_set_time, |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 126 | .read_alarm = pcap_rtc_read_alarm, |
| 127 | .set_alarm = pcap_rtc_set_alarm, |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 128 | .alarm_irq_enable = pcap_rtc_alarm_irq_enable, |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 131 | static int __init pcap_rtc_probe(struct platform_device *pdev) |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 132 | { |
| 133 | struct pcap_rtc *pcap_rtc; |
| 134 | int timer_irq, alarm_irq; |
| 135 | int err = -ENOMEM; |
| 136 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 137 | pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc), |
| 138 | GFP_KERNEL); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 139 | if (!pcap_rtc) |
| 140 | return err; |
| 141 | |
| 142 | pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent); |
| 143 | |
John Stultz | 4b3687f | 2011-05-06 17:30:57 -0700 | [diff] [blame] | 144 | platform_set_drvdata(pdev, pcap_rtc); |
| 145 | |
Alexandre Belloni | d1403c4 | 2019-04-30 11:32:59 +0200 | [diff] [blame] | 146 | pcap_rtc->rtc = devm_rtc_allocate_device(&pdev->dev); |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 147 | if (IS_ERR(pcap_rtc->rtc)) |
| 148 | return PTR_ERR(pcap_rtc->rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 149 | |
Alexandre Belloni | d1403c4 | 2019-04-30 11:32:59 +0200 | [diff] [blame] | 150 | pcap_rtc->rtc->ops = &pcap_rtc_ops; |
| 151 | pcap_rtc->rtc->range_max = (1 << 14) * 86400ULL - 1; |
| 152 | |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 153 | 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 Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 156 | err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0, |
| 157 | "RTC Timer", pcap_rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 158 | if (err) |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 159 | return err; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 160 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 161 | err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0, |
| 162 | "RTC Alarm", pcap_rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 163 | if (err) |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 164 | return err; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 165 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 166 | return devm_rtc_register_device(pcap_rtc->rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 169 | static int __exit pcap_rtc_remove(struct platform_device *pdev) |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 170 | { |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static struct platform_driver pcap_rtc_driver = { |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 175 | .remove = __exit_p(pcap_rtc_remove), |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 176 | .driver = { |
| 177 | .name = "pcap-rtc", |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 178 | }, |
| 179 | }; |
| 180 | |
Jingoo Han | 0de4c7c | 2013-04-29 16:18:47 -0700 | [diff] [blame] | 181 | module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 182 | |
| 183 | MODULE_DESCRIPTION("Motorola pcap rtc driver"); |
| 184 | MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>"); |
| 185 | MODULE_LICENSE("GPL"); |