blob: 013e6c103b9c465f1cc66eabe1d3011e697b5704 [file] [log] [blame]
Mark Zhan2e774c72007-07-17 04:05:05 -07001/*
2 * ST M48T59 RTC driver
3 *
4 * Copyright (c) 2007 Wind River Systems, Inc.
5 *
6 * Author: Mark Zhan <rongkai.zhan@windriver.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/device.h>
18#include <linux/platform_device.h>
19#include <linux/rtc.h>
20#include <linux/rtc/m48t59.h>
21#include <linux/bcd.h>
22
23#ifndef NO_IRQ
24#define NO_IRQ (-1)
25#endif
26
27#define M48T59_READ(reg) pdata->read_byte(dev, reg)
28#define M48T59_WRITE(val, reg) pdata->write_byte(dev, reg, val)
29
30#define M48T59_SET_BITS(mask, reg) \
31 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
32#define M48T59_CLEAR_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
34
35struct m48t59_private {
36 void __iomem *ioaddr;
37 unsigned int size; /* iomem size */
Mark Zhan04392082007-07-31 00:39:47 -070038 int irq;
Mark Zhan2e774c72007-07-17 04:05:05 -070039 struct rtc_device *rtc;
40 spinlock_t lock; /* serialize the NVRAM and RTC access */
41};
42
43/*
44 * This is the generic access method when the chip is memory-mapped
45 */
46static void
47m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
48{
49 struct platform_device *pdev = to_platform_device(dev);
50 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
51
52 writeb(val, m48t59->ioaddr+ofs);
53}
54
55static u8
56m48t59_mem_readb(struct device *dev, u32 ofs)
57{
58 struct platform_device *pdev = to_platform_device(dev);
59 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
60
61 return readb(m48t59->ioaddr+ofs);
62}
63
64/*
65 * NOTE: M48T59 only uses BCD mode
66 */
67static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
68{
69 struct platform_device *pdev = to_platform_device(dev);
70 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
71 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
72 unsigned long flags;
73 u8 val;
74
75 spin_lock_irqsave(&m48t59->lock, flags);
76 /* Issue the READ command */
77 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
78
79 tm->tm_year = BCD2BIN(M48T59_READ(M48T59_YEAR));
80 /* tm_mon is 0-11 */
81 tm->tm_mon = BCD2BIN(M48T59_READ(M48T59_MONTH)) - 1;
82 tm->tm_mday = BCD2BIN(M48T59_READ(M48T59_MDAY));
83
84 val = M48T59_READ(M48T59_WDAY);
85 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
86 dev_dbg(dev, "Century bit is enabled\n");
87 tm->tm_year += 100; /* one century */
88 }
89
90 tm->tm_wday = BCD2BIN(val & 0x07);
91 tm->tm_hour = BCD2BIN(M48T59_READ(M48T59_HOUR) & 0x3F);
92 tm->tm_min = BCD2BIN(M48T59_READ(M48T59_MIN) & 0x7F);
93 tm->tm_sec = BCD2BIN(M48T59_READ(M48T59_SEC) & 0x7F);
94
95 /* Clear the READ bit */
96 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
97 spin_unlock_irqrestore(&m48t59->lock, flags);
98
99 dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
100 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
101 tm->tm_hour, tm->tm_min, tm->tm_sec);
102 return 0;
103}
104
105static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
106{
107 struct platform_device *pdev = to_platform_device(dev);
108 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
109 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
110 unsigned long flags;
111 u8 val = 0;
112
113 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
114 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
115 tm->tm_hour, tm->tm_min, tm->tm_sec);
116
117 spin_lock_irqsave(&m48t59->lock, flags);
118 /* Issue the WRITE command */
119 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
120
121 M48T59_WRITE((BIN2BCD(tm->tm_sec) & 0x7F), M48T59_SEC);
122 M48T59_WRITE((BIN2BCD(tm->tm_min) & 0x7F), M48T59_MIN);
123 M48T59_WRITE((BIN2BCD(tm->tm_hour) & 0x3F), M48T59_HOUR);
124 M48T59_WRITE((BIN2BCD(tm->tm_mday) & 0x3F), M48T59_MDAY);
125 /* tm_mon is 0-11 */
126 M48T59_WRITE((BIN2BCD(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
127 M48T59_WRITE(BIN2BCD(tm->tm_year % 100), M48T59_YEAR);
128
129 if (tm->tm_year/100)
130 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
131 val |= (BIN2BCD(tm->tm_wday) & 0x07);
132 M48T59_WRITE(val, M48T59_WDAY);
133
134 /* Clear the WRITE bit */
135 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
136 spin_unlock_irqrestore(&m48t59->lock, flags);
137 return 0;
138}
139
140/*
141 * Read alarm time and date in RTC
142 */
143static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
144{
145 struct platform_device *pdev = to_platform_device(dev);
146 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
147 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
148 struct rtc_time *tm = &alrm->time;
149 unsigned long flags;
150 u8 val;
151
152 /* If no irq, we don't support ALARM */
153 if (m48t59->irq == NO_IRQ)
154 return -EIO;
155
156 spin_lock_irqsave(&m48t59->lock, flags);
157 /* Issue the READ command */
158 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
159
160 tm->tm_year = BCD2BIN(M48T59_READ(M48T59_YEAR));
161 /* tm_mon is 0-11 */
162 tm->tm_mon = BCD2BIN(M48T59_READ(M48T59_MONTH)) - 1;
163
164 val = M48T59_READ(M48T59_WDAY);
165 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
166 tm->tm_year += 100; /* one century */
167
168 tm->tm_mday = BCD2BIN(M48T59_READ(M48T59_ALARM_DATE));
169 tm->tm_hour = BCD2BIN(M48T59_READ(M48T59_ALARM_HOUR));
170 tm->tm_min = BCD2BIN(M48T59_READ(M48T59_ALARM_MIN));
171 tm->tm_sec = BCD2BIN(M48T59_READ(M48T59_ALARM_SEC));
172
173 /* Clear the READ bit */
174 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
175 spin_unlock_irqrestore(&m48t59->lock, flags);
176
177 dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
178 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
179 tm->tm_hour, tm->tm_min, tm->tm_sec);
180 return 0;
181}
182
183/*
184 * Set alarm time and date in RTC
185 */
186static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
187{
188 struct platform_device *pdev = to_platform_device(dev);
189 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
190 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
191 struct rtc_time *tm = &alrm->time;
192 u8 mday, hour, min, sec;
193 unsigned long flags;
194
195 /* If no irq, we don't support ALARM */
196 if (m48t59->irq == NO_IRQ)
197 return -EIO;
198
199 /*
200 * 0xff means "always match"
201 */
202 mday = tm->tm_mday;
203 mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
204 if (mday == 0xff)
205 mday = M48T59_READ(M48T59_MDAY);
206
207 hour = tm->tm_hour;
208 hour = (hour < 24) ? BIN2BCD(hour) : 0x00;
209
210 min = tm->tm_min;
211 min = (min < 60) ? BIN2BCD(min) : 0x00;
212
213 sec = tm->tm_sec;
214 sec = (sec < 60) ? BIN2BCD(sec) : 0x00;
215
216 spin_lock_irqsave(&m48t59->lock, flags);
217 /* Issue the WRITE command */
218 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
219
220 M48T59_WRITE(mday, M48T59_ALARM_DATE);
221 M48T59_WRITE(hour, M48T59_ALARM_HOUR);
222 M48T59_WRITE(min, M48T59_ALARM_MIN);
223 M48T59_WRITE(sec, M48T59_ALARM_SEC);
224
225 /* Clear the WRITE bit */
226 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
227 spin_unlock_irqrestore(&m48t59->lock, flags);
228
229 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
230 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
231 tm->tm_hour, tm->tm_min, tm->tm_sec);
232 return 0;
233}
234
235/*
236 * Handle commands from user-space
237 */
238static int m48t59_rtc_ioctl(struct device *dev, unsigned int cmd,
239 unsigned long arg)
240{
241 struct platform_device *pdev = to_platform_device(dev);
242 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
243 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
244 unsigned long flags;
245 int ret = 0;
246
247 spin_lock_irqsave(&m48t59->lock, flags);
248 switch (cmd) {
249 case RTC_AIE_OFF: /* alarm interrupt off */
250 M48T59_WRITE(0x00, M48T59_INTR);
251 break;
252 case RTC_AIE_ON: /* alarm interrupt on */
253 M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
254 break;
255 default:
256 ret = -ENOIOCTLCMD;
257 break;
258 }
259 spin_unlock_irqrestore(&m48t59->lock, flags);
260
261 return ret;
262}
263
264static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
265{
266 struct platform_device *pdev = to_platform_device(dev);
267 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
268 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
269 unsigned long flags;
270 u8 val;
271
272 spin_lock_irqsave(&m48t59->lock, flags);
273 val = M48T59_READ(M48T59_FLAGS);
274 spin_unlock_irqrestore(&m48t59->lock, flags);
275
276 seq_printf(seq, "battery\t\t: %s\n",
277 (val & M48T59_FLAGS_BF) ? "low" : "normal");
278 return 0;
279}
280
281/*
282 * IRQ handler for the RTC
283 */
284static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
285{
286 struct device *dev = (struct device *)dev_id;
287 struct platform_device *pdev = to_platform_device(dev);
288 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
289 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
290 u8 event;
291
292 spin_lock(&m48t59->lock);
293 event = M48T59_READ(M48T59_FLAGS);
294 spin_unlock(&m48t59->lock);
295
296 if (event & M48T59_FLAGS_AF) {
297 rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
298 return IRQ_HANDLED;
299 }
300
301 return IRQ_NONE;
302}
303
304static const struct rtc_class_ops m48t59_rtc_ops = {
305 .ioctl = m48t59_rtc_ioctl,
306 .read_time = m48t59_rtc_read_time,
307 .set_time = m48t59_rtc_set_time,
308 .read_alarm = m48t59_rtc_readalarm,
309 .set_alarm = m48t59_rtc_setalarm,
310 .proc = m48t59_rtc_proc,
311};
312
313static ssize_t m48t59_nvram_read(struct kobject *kobj,
314 struct bin_attribute *bin_attr,
315 char *buf, loff_t pos, size_t size)
316{
317 struct device *dev = container_of(kobj, struct device, kobj);
318 struct platform_device *pdev = to_platform_device(dev);
319 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
320 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
321 ssize_t cnt = 0;
322 unsigned long flags;
323
324 for (; size > 0 && pos < M48T59_NVRAM_SIZE; cnt++, size--) {
325 spin_lock_irqsave(&m48t59->lock, flags);
326 *buf++ = M48T59_READ(cnt);
327 spin_unlock_irqrestore(&m48t59->lock, flags);
328 }
329
330 return cnt;
331}
332
333static ssize_t m48t59_nvram_write(struct kobject *kobj,
334 struct bin_attribute *bin_attr,
335 char *buf, loff_t pos, size_t size)
336{
337 struct device *dev = container_of(kobj, struct device, kobj);
338 struct platform_device *pdev = to_platform_device(dev);
339 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
340 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
341 ssize_t cnt = 0;
342 unsigned long flags;
343
344 for (; size > 0 && pos < M48T59_NVRAM_SIZE; cnt++, size--) {
345 spin_lock_irqsave(&m48t59->lock, flags);
346 M48T59_WRITE(*buf++, cnt);
347 spin_unlock_irqrestore(&m48t59->lock, flags);
348 }
349
350 return cnt;
351}
352
353static struct bin_attribute m48t59_nvram_attr = {
354 .attr = {
355 .name = "nvram",
David Brownella4b1d502007-11-14 16:58:30 -0800356 .mode = S_IRUGO | S_IWUSR,
Mark Zhan2e774c72007-07-17 04:05:05 -0700357 .owner = THIS_MODULE,
358 },
359 .read = m48t59_nvram_read,
360 .write = m48t59_nvram_write,
David Brownella4b1d502007-11-14 16:58:30 -0800361 .size = M48T59_NVRAM_SIZE,
Mark Zhan2e774c72007-07-17 04:05:05 -0700362};
363
364static int __devinit m48t59_rtc_probe(struct platform_device *pdev)
365{
366 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
367 struct m48t59_private *m48t59 = NULL;
368 struct resource *res;
369 int ret = -ENOMEM;
370
371 /* This chip could be memory-mapped or I/O-mapped */
372 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373 if (!res) {
374 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
375 if (!res)
376 return -EINVAL;
377 }
378
379 if (res->flags & IORESOURCE_IO) {
380 /* If we are I/O-mapped, the platform should provide
381 * the operations accessing chip registers.
382 */
383 if (!pdata || !pdata->write_byte || !pdata->read_byte)
384 return -EINVAL;
385 } else if (res->flags & IORESOURCE_MEM) {
386 /* we are memory-mapped */
387 if (!pdata) {
388 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
389 if (!pdata)
390 return -ENOMEM;
391 /* Ensure we only kmalloc platform data once */
392 pdev->dev.platform_data = pdata;
393 }
394
395 /* Try to use the generic memory read/write ops */
396 if (!pdata->write_byte)
397 pdata->write_byte = m48t59_mem_writeb;
398 if (!pdata->read_byte)
399 pdata->read_byte = m48t59_mem_readb;
400 }
401
402 m48t59 = kzalloc(sizeof(*m48t59), GFP_KERNEL);
403 if (!m48t59)
404 return -ENOMEM;
405
406 m48t59->size = res->end - res->start + 1;
407 m48t59->ioaddr = ioremap(res->start, m48t59->size);
408 if (!m48t59->ioaddr)
409 goto out;
410
411 /* Try to get irq number. We also can work in
412 * the mode without IRQ.
413 */
414 m48t59->irq = platform_get_irq(pdev, 0);
415 if (m48t59->irq < 0)
416 m48t59->irq = NO_IRQ;
417
418 if (m48t59->irq != NO_IRQ) {
419 ret = request_irq(m48t59->irq, m48t59_rtc_interrupt,
420 IRQF_SHARED, "rtc-m48t59", &pdev->dev);
421 if (ret)
422 goto out;
423 }
424
425 m48t59->rtc = rtc_device_register("m48t59", &pdev->dev,
426 &m48t59_rtc_ops, THIS_MODULE);
427 if (IS_ERR(m48t59->rtc)) {
428 ret = PTR_ERR(m48t59->rtc);
429 goto out;
430 }
431
432 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
433 if (ret)
434 goto out;
435
436 spin_lock_init(&m48t59->lock);
437 platform_set_drvdata(pdev, m48t59);
438 return 0;
439
440out:
441 if (!IS_ERR(m48t59->rtc))
442 rtc_device_unregister(m48t59->rtc);
443 if (m48t59->irq != NO_IRQ)
444 free_irq(m48t59->irq, &pdev->dev);
445 if (m48t59->ioaddr)
446 iounmap(m48t59->ioaddr);
447 if (m48t59)
448 kfree(m48t59);
449 return ret;
450}
451
452static int __devexit m48t59_rtc_remove(struct platform_device *pdev)
453{
454 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
455
456 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
457 if (!IS_ERR(m48t59->rtc))
458 rtc_device_unregister(m48t59->rtc);
459 if (m48t59->ioaddr)
460 iounmap(m48t59->ioaddr);
461 if (m48t59->irq != NO_IRQ)
462 free_irq(m48t59->irq, &pdev->dev);
463 platform_set_drvdata(pdev, NULL);
464 kfree(m48t59);
465 return 0;
466}
467
Kay Sieversad28a072008-04-10 21:29:25 -0700468/* work with hotplug and coldplug */
469MODULE_ALIAS("platform:rtc-m48t59");
470
Randy Dunlap12730922007-11-05 14:51:00 -0800471static struct platform_driver m48t59_rtc_driver = {
Mark Zhan2e774c72007-07-17 04:05:05 -0700472 .driver = {
473 .name = "rtc-m48t59",
474 .owner = THIS_MODULE,
475 },
476 .probe = m48t59_rtc_probe,
477 .remove = __devexit_p(m48t59_rtc_remove),
478};
479
480static int __init m48t59_rtc_init(void)
481{
Randy Dunlap12730922007-11-05 14:51:00 -0800482 return platform_driver_register(&m48t59_rtc_driver);
Mark Zhan2e774c72007-07-17 04:05:05 -0700483}
484
485static void __exit m48t59_rtc_exit(void)
486{
Randy Dunlap12730922007-11-05 14:51:00 -0800487 platform_driver_unregister(&m48t59_rtc_driver);
Mark Zhan2e774c72007-07-17 04:05:05 -0700488}
489
490module_init(m48t59_rtc_init);
491module_exit(m48t59_rtc_exit);
492
493MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
494MODULE_DESCRIPTION("M48T59 RTC driver");
495MODULE_LICENSE("GPL");