davinci: make it possible to include clock.h and psc.h in assembly code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-tx4939.c
blob9ee81d8aa7c02ecd88c637776b82d14978e063ef
1 /*
2 * TX4939 internal RTC driver
3 * Based on RBTX49xx patch from CELF patch archive.
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
7 * for more details.
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <asm/txx9/tx4939.h>
17 struct tx4939rtc_plat_data {
18 struct rtc_device *rtc;
19 struct tx4939_rtc_reg __iomem *rtcreg;
20 spinlock_t lock;
23 static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *dev)
25 return platform_get_drvdata(to_platform_device(dev));
28 static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
30 int i = 0;
32 __raw_writel(cmd, &rtcreg->ctl);
33 /* This might take 30us (next 32.768KHz clock) */
34 while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
35 /* timeout on approx. 100us (@ GBUS200MHz) */
36 if (i++ > 200 * 100)
37 return -EBUSY;
38 cpu_relax();
40 return 0;
43 static int tx4939_rtc_set_mmss(struct device *dev, unsigned long secs)
45 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
46 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
47 int i, ret;
48 unsigned char buf[6];
50 buf[0] = 0;
51 buf[1] = 0;
52 buf[2] = secs;
53 buf[3] = secs >> 8;
54 buf[4] = secs >> 16;
55 buf[5] = secs >> 24;
56 spin_lock_irq(&pdata->lock);
57 __raw_writel(0, &rtcreg->adr);
58 for (i = 0; i < 6; i++)
59 __raw_writel(buf[i], &rtcreg->dat);
60 ret = tx4939_rtc_cmd(rtcreg,
61 TX4939_RTCCTL_COMMAND_SETTIME |
62 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
63 spin_unlock_irq(&pdata->lock);
64 return ret;
67 static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
69 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
70 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
71 int i, ret;
72 unsigned long sec;
73 unsigned char buf[6];
75 spin_lock_irq(&pdata->lock);
76 ret = tx4939_rtc_cmd(rtcreg,
77 TX4939_RTCCTL_COMMAND_GETTIME |
78 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
79 if (ret) {
80 spin_unlock_irq(&pdata->lock);
81 return ret;
83 __raw_writel(2, &rtcreg->adr);
84 for (i = 2; i < 6; i++)
85 buf[i] = __raw_readl(&rtcreg->dat);
86 spin_unlock_irq(&pdata->lock);
87 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
88 rtc_time_to_tm(sec, tm);
89 return rtc_valid_tm(tm);
92 static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
94 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
95 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
96 int i, ret;
97 unsigned long sec;
98 unsigned char buf[6];
100 if (alrm->time.tm_sec < 0 ||
101 alrm->time.tm_min < 0 ||
102 alrm->time.tm_hour < 0 ||
103 alrm->time.tm_mday < 0 ||
104 alrm->time.tm_mon < 0 ||
105 alrm->time.tm_year < 0)
106 return -EINVAL;
107 rtc_tm_to_time(&alrm->time, &sec);
108 buf[0] = 0;
109 buf[1] = 0;
110 buf[2] = sec;
111 buf[3] = sec >> 8;
112 buf[4] = sec >> 16;
113 buf[5] = sec >> 24;
114 spin_lock_irq(&pdata->lock);
115 __raw_writel(0, &rtcreg->adr);
116 for (i = 0; i < 6; i++)
117 __raw_writel(buf[i], &rtcreg->dat);
118 ret = tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
119 (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
120 spin_unlock_irq(&pdata->lock);
121 return ret;
124 static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
126 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
127 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
128 int i, ret;
129 unsigned long sec;
130 unsigned char buf[6];
131 u32 ctl;
133 spin_lock_irq(&pdata->lock);
134 ret = tx4939_rtc_cmd(rtcreg,
135 TX4939_RTCCTL_COMMAND_GETALARM |
136 (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
137 if (ret) {
138 spin_unlock_irq(&pdata->lock);
139 return ret;
141 __raw_writel(2, &rtcreg->adr);
142 for (i = 2; i < 6; i++)
143 buf[i] = __raw_readl(&rtcreg->dat);
144 ctl = __raw_readl(&rtcreg->ctl);
145 alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
146 alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
147 spin_unlock_irq(&pdata->lock);
148 sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
149 rtc_time_to_tm(sec, &alrm->time);
150 return rtc_valid_tm(&alrm->time);
153 static int tx4939_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
155 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
157 spin_lock_irq(&pdata->lock);
158 tx4939_rtc_cmd(pdata->rtcreg,
159 TX4939_RTCCTL_COMMAND_NOP |
160 (enabled ? TX4939_RTCCTL_ALME : 0));
161 spin_unlock_irq(&pdata->lock);
162 return 0;
165 static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
167 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev_id);
168 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
169 unsigned long events = RTC_IRQF;
171 spin_lock(&pdata->lock);
172 if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
173 events |= RTC_AF;
174 tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
176 spin_unlock(&pdata->lock);
177 if (likely(pdata->rtc))
178 rtc_update_irq(pdata->rtc, 1, events);
179 return IRQ_HANDLED;
182 static const struct rtc_class_ops tx4939_rtc_ops = {
183 .read_time = tx4939_rtc_read_time,
184 .read_alarm = tx4939_rtc_read_alarm,
185 .set_alarm = tx4939_rtc_set_alarm,
186 .set_mmss = tx4939_rtc_set_mmss,
187 .alarm_irq_enable = tx4939_rtc_alarm_irq_enable,
190 static ssize_t tx4939_rtc_nvram_read(struct kobject *kobj,
191 struct bin_attribute *bin_attr,
192 char *buf, loff_t pos, size_t size)
194 struct device *dev = container_of(kobj, struct device, kobj);
195 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
196 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
197 ssize_t count;
199 spin_lock_irq(&pdata->lock);
200 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
201 count++, size--) {
202 __raw_writel(pos++, &rtcreg->adr);
203 *buf++ = __raw_readl(&rtcreg->dat);
205 spin_unlock_irq(&pdata->lock);
206 return count;
209 static ssize_t tx4939_rtc_nvram_write(struct kobject *kobj,
210 struct bin_attribute *bin_attr,
211 char *buf, loff_t pos, size_t size)
213 struct device *dev = container_of(kobj, struct device, kobj);
214 struct tx4939rtc_plat_data *pdata = get_tx4939rtc_plat_data(dev);
215 struct tx4939_rtc_reg __iomem *rtcreg = pdata->rtcreg;
216 ssize_t count;
218 spin_lock_irq(&pdata->lock);
219 for (count = 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
220 count++, size--) {
221 __raw_writel(pos++, &rtcreg->adr);
222 __raw_writel(*buf++, &rtcreg->dat);
224 spin_unlock_irq(&pdata->lock);
225 return count;
228 static struct bin_attribute tx4939_rtc_nvram_attr = {
229 .attr = {
230 .name = "nvram",
231 .mode = S_IRUGO | S_IWUSR,
233 .size = TX4939_RTC_REG_RAMSIZE,
234 .read = tx4939_rtc_nvram_read,
235 .write = tx4939_rtc_nvram_write,
238 static int __init tx4939_rtc_probe(struct platform_device *pdev)
240 struct rtc_device *rtc;
241 struct tx4939rtc_plat_data *pdata;
242 struct resource *res;
243 int irq, ret;
245 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
246 if (!res)
247 return -ENODEV;
248 irq = platform_get_irq(pdev, 0);
249 if (irq < 0)
250 return -ENODEV;
251 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
252 if (!pdata)
253 return -ENOMEM;
254 platform_set_drvdata(pdev, pdata);
256 if (!devm_request_mem_region(&pdev->dev, res->start,
257 resource_size(res), pdev->name))
258 return -EBUSY;
259 pdata->rtcreg = devm_ioremap(&pdev->dev, res->start,
260 resource_size(res));
261 if (!pdata->rtcreg)
262 return -EBUSY;
264 spin_lock_init(&pdata->lock);
265 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
266 if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
267 IRQF_DISABLED, pdev->name, &pdev->dev) < 0)
268 return -EBUSY;
269 rtc = rtc_device_register(pdev->name, &pdev->dev,
270 &tx4939_rtc_ops, THIS_MODULE);
271 if (IS_ERR(rtc))
272 return PTR_ERR(rtc);
273 pdata->rtc = rtc;
274 ret = sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
275 if (ret)
276 rtc_device_unregister(rtc);
277 return ret;
280 static int __exit tx4939_rtc_remove(struct platform_device *pdev)
282 struct tx4939rtc_plat_data *pdata = platform_get_drvdata(pdev);
284 sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
285 rtc_device_unregister(pdata->rtc);
286 spin_lock_irq(&pdata->lock);
287 tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
288 spin_unlock_irq(&pdata->lock);
289 return 0;
292 static struct platform_driver tx4939_rtc_driver = {
293 .remove = __exit_p(tx4939_rtc_remove),
294 .driver = {
295 .name = "tx4939rtc",
296 .owner = THIS_MODULE,
300 static int __init tx4939rtc_init(void)
302 return platform_driver_probe(&tx4939_rtc_driver, tx4939_rtc_probe);
305 static void __exit tx4939rtc_exit(void)
307 platform_driver_unregister(&tx4939_rtc_driver);
310 module_init(tx4939rtc_init);
311 module_exit(tx4939rtc_exit);
313 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
314 MODULE_DESCRIPTION("TX4939 internal RTC driver");
315 MODULE_LICENSE("GPL");
316 MODULE_ALIAS("platform:tx4939rtc");