Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pcap rtc code for Motorola EZX phones |
| 3 | * |
| 4 | * Copyright (c) 2008 guiming zhuo <gmzhuo@gmail.com> |
| 5 | * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com> |
| 6 | * |
| 7 | * Based on Motorola's rtc.c Copyright (c) 2003-2005 Motorola |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/mfd/ezx-pcap.h> |
| 19 | #include <linux/rtc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | |
| 23 | struct pcap_rtc { |
| 24 | struct pcap_chip *pcap; |
| 25 | struct rtc_device *rtc; |
| 26 | }; |
| 27 | |
| 28 | static irqreturn_t pcap_rtc_irq(int irq, void *_pcap_rtc) |
| 29 | { |
| 30 | struct pcap_rtc *pcap_rtc = _pcap_rtc; |
| 31 | unsigned long rtc_events; |
| 32 | |
| 33 | if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ)) |
| 34 | rtc_events = RTC_IRQF | RTC_UF; |
| 35 | else if (irq == pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA)) |
| 36 | rtc_events = RTC_IRQF | RTC_AF; |
| 37 | else |
| 38 | rtc_events = 0; |
| 39 | |
| 40 | rtc_update_irq(pcap_rtc->rtc, 1, rtc_events); |
| 41 | return IRQ_HANDLED; |
| 42 | } |
| 43 | |
| 44 | static int pcap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 45 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 46 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 47 | struct rtc_time *tm = &alrm->time; |
| 48 | unsigned long secs; |
| 49 | u32 tod; /* time of day, seconds since midnight */ |
| 50 | u32 days; /* days since 1/1/1970 */ |
| 51 | |
| 52 | ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TODA, &tod); |
| 53 | secs = tod & PCAP_RTC_TOD_MASK; |
| 54 | |
| 55 | ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, &days); |
| 56 | secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY; |
| 57 | |
| 58 | rtc_time_to_tm(secs, tm); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int pcap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 64 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 65 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 66 | struct rtc_time *tm = &alrm->time; |
| 67 | unsigned long secs; |
| 68 | u32 tod, days; |
| 69 | |
| 70 | rtc_tm_to_time(tm, &secs); |
| 71 | |
| 72 | tod = secs % SEC_PER_DAY; |
| 73 | ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TODA, tod); |
| 74 | |
| 75 | days = secs / SEC_PER_DAY; |
| 76 | ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAYA, days); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int pcap_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 82 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 83 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 84 | unsigned long secs; |
| 85 | u32 tod, days; |
| 86 | |
| 87 | ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_TOD, &tod); |
| 88 | secs = tod & PCAP_RTC_TOD_MASK; |
| 89 | |
| 90 | ezx_pcap_read(pcap_rtc->pcap, PCAP_REG_RTC_DAY, &days); |
| 91 | secs += (days & PCAP_RTC_DAY_MASK) * SEC_PER_DAY; |
| 92 | |
| 93 | rtc_time_to_tm(secs, tm); |
| 94 | |
Alexandre Belloni | ab62670 | 2018-02-19 16:23:55 +0100 | [diff] [blame] | 95 | return 0; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int pcap_rtc_set_mmss(struct device *dev, unsigned long secs) |
| 99 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 100 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 101 | u32 tod, days; |
| 102 | |
| 103 | tod = secs % SEC_PER_DAY; |
| 104 | ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_TOD, tod); |
| 105 | |
| 106 | days = secs / SEC_PER_DAY; |
| 107 | ezx_pcap_write(pcap_rtc->pcap, PCAP_REG_RTC_DAY, days); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int pcap_rtc_irq_enable(struct device *dev, int pirq, unsigned int en) |
| 113 | { |
Wolfram Sang | 85368bb | 2018-04-19 16:06:14 +0200 | [diff] [blame] | 114 | struct pcap_rtc *pcap_rtc = dev_get_drvdata(dev); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 115 | |
| 116 | if (en) |
| 117 | enable_irq(pcap_to_irq(pcap_rtc->pcap, pirq)); |
| 118 | else |
| 119 | disable_irq(pcap_to_irq(pcap_rtc->pcap, pirq)); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int pcap_rtc_alarm_irq_enable(struct device *dev, unsigned int en) |
| 125 | { |
| 126 | return pcap_rtc_irq_enable(dev, PCAP_IRQ_TODA, en); |
| 127 | } |
| 128 | |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 129 | static const struct rtc_class_ops pcap_rtc_ops = { |
| 130 | .read_time = pcap_rtc_read_time, |
| 131 | .read_alarm = pcap_rtc_read_alarm, |
| 132 | .set_alarm = pcap_rtc_set_alarm, |
| 133 | .set_mmss = pcap_rtc_set_mmss, |
| 134 | .alarm_irq_enable = pcap_rtc_alarm_irq_enable, |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 137 | static int __init pcap_rtc_probe(struct platform_device *pdev) |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 138 | { |
| 139 | struct pcap_rtc *pcap_rtc; |
| 140 | int timer_irq, alarm_irq; |
| 141 | int err = -ENOMEM; |
| 142 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 143 | pcap_rtc = devm_kzalloc(&pdev->dev, sizeof(struct pcap_rtc), |
| 144 | GFP_KERNEL); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 145 | if (!pcap_rtc) |
| 146 | return err; |
| 147 | |
| 148 | pcap_rtc->pcap = dev_get_drvdata(pdev->dev.parent); |
| 149 | |
John Stultz | 4b3687f | 2011-05-06 17:30:57 -0700 | [diff] [blame] | 150 | platform_set_drvdata(pdev, pcap_rtc); |
| 151 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 152 | pcap_rtc->rtc = devm_rtc_device_register(&pdev->dev, "pcap", |
| 153 | &pcap_rtc_ops, THIS_MODULE); |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 154 | if (IS_ERR(pcap_rtc->rtc)) |
| 155 | return PTR_ERR(pcap_rtc->rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 156 | |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 157 | timer_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_1HZ); |
| 158 | alarm_irq = pcap_to_irq(pcap_rtc->pcap, PCAP_IRQ_TODA); |
| 159 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 160 | err = devm_request_irq(&pdev->dev, timer_irq, pcap_rtc_irq, 0, |
| 161 | "RTC Timer", pcap_rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 162 | if (err) |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 163 | return err; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 164 | |
Jingoo Han | 6b5f486 | 2013-04-29 16:20:47 -0700 | [diff] [blame] | 165 | err = devm_request_irq(&pdev->dev, alarm_irq, pcap_rtc_irq, 0, |
| 166 | "RTC Alarm", pcap_rtc); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 167 | if (err) |
Jingoo Han | a1c805a | 2013-07-03 15:06:32 -0700 | [diff] [blame] | 168 | return err; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 169 | |
| 170 | return 0; |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 173 | static int __exit pcap_rtc_remove(struct platform_device *pdev) |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 174 | { |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static struct platform_driver pcap_rtc_driver = { |
Jingoo Han | 5cc2b9c | 2013-04-29 16:18:26 -0700 | [diff] [blame] | 179 | .remove = __exit_p(pcap_rtc_remove), |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 180 | .driver = { |
| 181 | .name = "pcap-rtc", |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 182 | }, |
| 183 | }; |
| 184 | |
Jingoo Han | 0de4c7c | 2013-04-29 16:18:47 -0700 | [diff] [blame] | 185 | module_platform_driver_probe(pcap_rtc_driver, pcap_rtc_probe); |
Daniel Ribeiro | d3c7a3f | 2009-09-22 16:46:27 -0700 | [diff] [blame] | 186 | |
| 187 | MODULE_DESCRIPTION("Motorola pcap rtc driver"); |
| 188 | MODULE_AUTHOR("guiming zhuo <gmzhuo@gmail.com>"); |
| 189 | MODULE_LICENSE("GPL"); |