1
2
3
4
5
6
7
8
9
10
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#include <linux/slab.h>
23
24#ifndef NO_IRQ
25#define NO_IRQ (-1)
26#endif
27
28#define M48T59_READ(reg) (pdata->read_byte(dev, pdata->offset + reg))
29#define M48T59_WRITE(val, reg) \
30 (pdata->write_byte(dev, pdata->offset + reg, val))
31
32#define M48T59_SET_BITS(mask, reg) \
33 M48T59_WRITE((M48T59_READ(reg) | (mask)), (reg))
34#define M48T59_CLEAR_BITS(mask, reg) \
35 M48T59_WRITE((M48T59_READ(reg) & ~(mask)), (reg))
36
37struct m48t59_private {
38 void __iomem *ioaddr;
39 int irq;
40 struct rtc_device *rtc;
41 spinlock_t lock;
42};
43
44
45
46
47static void
48m48t59_mem_writeb(struct device *dev, u32 ofs, u8 val)
49{
50 struct platform_device *pdev = to_platform_device(dev);
51 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
52
53 writeb(val, m48t59->ioaddr+ofs);
54}
55
56static u8
57m48t59_mem_readb(struct device *dev, u32 ofs)
58{
59 struct platform_device *pdev = to_platform_device(dev);
60 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
61
62 return readb(m48t59->ioaddr+ofs);
63}
64
65
66
67
68static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
69{
70 struct platform_device *pdev = to_platform_device(dev);
71 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
72 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
73 unsigned long flags;
74 u8 val;
75
76 spin_lock_irqsave(&m48t59->lock, flags);
77
78 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
79
80 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
81
82 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
83 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_MDAY));
84
85 val = M48T59_READ(M48T59_WDAY);
86 if ((pdata->type == M48T59RTC_TYPE_M48T59) &&
87 (val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB)) {
88 dev_dbg(dev, "Century bit is enabled\n");
89 tm->tm_year += 100;
90 }
91#ifdef CONFIG_SPARC
92
93 tm->tm_year += 68;
94#endif
95
96 tm->tm_wday = bcd2bin(val & 0x07);
97 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
98 tm->tm_min = bcd2bin(M48T59_READ(M48T59_MIN) & 0x7F);
99 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_SEC) & 0x7F);
100
101
102 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
103 spin_unlock_irqrestore(&m48t59->lock, flags);
104
105 dev_dbg(dev, "RTC read time %04d-%02d-%02d %02d/%02d/%02d\n",
106 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
107 tm->tm_hour, tm->tm_min, tm->tm_sec);
108 return rtc_valid_tm(tm);
109}
110
111static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
112{
113 struct platform_device *pdev = to_platform_device(dev);
114 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
115 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
116 unsigned long flags;
117 u8 val = 0;
118 int year = tm->tm_year;
119
120#ifdef CONFIG_SPARC
121
122 year -= 68;
123#endif
124
125 dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
126 year + 1900, tm->tm_mon, tm->tm_mday,
127 tm->tm_hour, tm->tm_min, tm->tm_sec);
128
129 if (year < 0)
130 return -EINVAL;
131
132 spin_lock_irqsave(&m48t59->lock, flags);
133
134 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
135
136 M48T59_WRITE((bin2bcd(tm->tm_sec) & 0x7F), M48T59_SEC);
137 M48T59_WRITE((bin2bcd(tm->tm_min) & 0x7F), M48T59_MIN);
138 M48T59_WRITE((bin2bcd(tm->tm_hour) & 0x3F), M48T59_HOUR);
139 M48T59_WRITE((bin2bcd(tm->tm_mday) & 0x3F), M48T59_MDAY);
140
141 M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
142 M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
143
144 if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
145 val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
146 val |= (bin2bcd(tm->tm_wday) & 0x07);
147 M48T59_WRITE(val, M48T59_WDAY);
148
149
150 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
151 spin_unlock_irqrestore(&m48t59->lock, flags);
152 return 0;
153}
154
155
156
157
158static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
159{
160 struct platform_device *pdev = to_platform_device(dev);
161 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
162 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
163 struct rtc_time *tm = &alrm->time;
164 unsigned long flags;
165 u8 val;
166
167
168 if (m48t59->irq == NO_IRQ)
169 return -EIO;
170
171 spin_lock_irqsave(&m48t59->lock, flags);
172
173 M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
174
175 tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
176#ifdef CONFIG_SPARC
177
178 tm->tm_year += 68;
179#endif
180
181 tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
182
183 val = M48T59_READ(M48T59_WDAY);
184 if ((val & M48T59_WDAY_CEB) && (val & M48T59_WDAY_CB))
185 tm->tm_year += 100;
186
187 tm->tm_mday = bcd2bin(M48T59_READ(M48T59_ALARM_DATE));
188 tm->tm_hour = bcd2bin(M48T59_READ(M48T59_ALARM_HOUR));
189 tm->tm_min = bcd2bin(M48T59_READ(M48T59_ALARM_MIN));
190 tm->tm_sec = bcd2bin(M48T59_READ(M48T59_ALARM_SEC));
191
192
193 M48T59_CLEAR_BITS(M48T59_CNTL_READ, M48T59_CNTL);
194 spin_unlock_irqrestore(&m48t59->lock, flags);
195
196 dev_dbg(dev, "RTC read alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
197 tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
198 tm->tm_hour, tm->tm_min, tm->tm_sec);
199 return rtc_valid_tm(tm);
200}
201
202
203
204
205static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
206{
207 struct platform_device *pdev = to_platform_device(dev);
208 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
209 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
210 struct rtc_time *tm = &alrm->time;
211 u8 mday, hour, min, sec;
212 unsigned long flags;
213 int year = tm->tm_year;
214
215#ifdef CONFIG_SPARC
216
217 year -= 68;
218#endif
219
220
221 if (m48t59->irq == NO_IRQ)
222 return -EIO;
223
224 if (year < 0)
225 return -EINVAL;
226
227
228
229
230 mday = tm->tm_mday;
231 mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
232 if (mday == 0xff)
233 mday = M48T59_READ(M48T59_MDAY);
234
235 hour = tm->tm_hour;
236 hour = (hour < 24) ? bin2bcd(hour) : 0x00;
237
238 min = tm->tm_min;
239 min = (min < 60) ? bin2bcd(min) : 0x00;
240
241 sec = tm->tm_sec;
242 sec = (sec < 60) ? bin2bcd(sec) : 0x00;
243
244 spin_lock_irqsave(&m48t59->lock, flags);
245
246 M48T59_SET_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
247
248 M48T59_WRITE(mday, M48T59_ALARM_DATE);
249 M48T59_WRITE(hour, M48T59_ALARM_HOUR);
250 M48T59_WRITE(min, M48T59_ALARM_MIN);
251 M48T59_WRITE(sec, M48T59_ALARM_SEC);
252
253
254 M48T59_CLEAR_BITS(M48T59_CNTL_WRITE, M48T59_CNTL);
255 spin_unlock_irqrestore(&m48t59->lock, flags);
256
257 dev_dbg(dev, "RTC set alarm time %04d-%02d-%02d %02d/%02d/%02d\n",
258 year + 1900, tm->tm_mon, tm->tm_mday,
259 tm->tm_hour, tm->tm_min, tm->tm_sec);
260 return 0;
261}
262
263
264
265
266static int m48t59_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
267{
268 struct platform_device *pdev = to_platform_device(dev);
269 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
270 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
271 unsigned long flags;
272
273 spin_lock_irqsave(&m48t59->lock, flags);
274 if (enabled)
275 M48T59_WRITE(M48T59_INTR_AFE, M48T59_INTR);
276 else
277 M48T59_WRITE(0x00, M48T59_INTR);
278 spin_unlock_irqrestore(&m48t59->lock, flags);
279
280 return 0;
281}
282
283static int m48t59_rtc_proc(struct device *dev, struct seq_file *seq)
284{
285 struct platform_device *pdev = to_platform_device(dev);
286 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
287 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
288 unsigned long flags;
289 u8 val;
290
291 spin_lock_irqsave(&m48t59->lock, flags);
292 val = M48T59_READ(M48T59_FLAGS);
293 spin_unlock_irqrestore(&m48t59->lock, flags);
294
295 seq_printf(seq, "battery\t\t: %s\n",
296 (val & M48T59_FLAGS_BF) ? "low" : "normal");
297 return 0;
298}
299
300
301
302
303static irqreturn_t m48t59_rtc_interrupt(int irq, void *dev_id)
304{
305 struct device *dev = (struct device *)dev_id;
306 struct platform_device *pdev = to_platform_device(dev);
307 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
308 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
309 u8 event;
310
311 spin_lock(&m48t59->lock);
312 event = M48T59_READ(M48T59_FLAGS);
313 spin_unlock(&m48t59->lock);
314
315 if (event & M48T59_FLAGS_AF) {
316 rtc_update_irq(m48t59->rtc, 1, (RTC_AF | RTC_IRQF));
317 return IRQ_HANDLED;
318 }
319
320 return IRQ_NONE;
321}
322
323static const struct rtc_class_ops m48t59_rtc_ops = {
324 .read_time = m48t59_rtc_read_time,
325 .set_time = m48t59_rtc_set_time,
326 .read_alarm = m48t59_rtc_readalarm,
327 .set_alarm = m48t59_rtc_setalarm,
328 .proc = m48t59_rtc_proc,
329 .alarm_irq_enable = m48t59_rtc_alarm_irq_enable,
330};
331
332static const struct rtc_class_ops m48t02_rtc_ops = {
333 .read_time = m48t59_rtc_read_time,
334 .set_time = m48t59_rtc_set_time,
335};
336
337static ssize_t m48t59_nvram_read(struct file *filp, struct kobject *kobj,
338 struct bin_attribute *bin_attr,
339 char *buf, loff_t pos, size_t size)
340{
341 struct device *dev = container_of(kobj, struct device, kobj);
342 struct platform_device *pdev = to_platform_device(dev);
343 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
344 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
345 ssize_t cnt = 0;
346 unsigned long flags;
347
348 for (; size > 0 && pos < pdata->offset; cnt++, size--) {
349 spin_lock_irqsave(&m48t59->lock, flags);
350 *buf++ = M48T59_READ(cnt);
351 spin_unlock_irqrestore(&m48t59->lock, flags);
352 }
353
354 return cnt;
355}
356
357static ssize_t m48t59_nvram_write(struct file *filp, struct kobject *kobj,
358 struct bin_attribute *bin_attr,
359 char *buf, loff_t pos, size_t size)
360{
361 struct device *dev = container_of(kobj, struct device, kobj);
362 struct platform_device *pdev = to_platform_device(dev);
363 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
364 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
365 ssize_t cnt = 0;
366 unsigned long flags;
367
368 for (; size > 0 && pos < pdata->offset; cnt++, size--) {
369 spin_lock_irqsave(&m48t59->lock, flags);
370 M48T59_WRITE(*buf++, cnt);
371 spin_unlock_irqrestore(&m48t59->lock, flags);
372 }
373
374 return cnt;
375}
376
377static struct bin_attribute m48t59_nvram_attr = {
378 .attr = {
379 .name = "nvram",
380 .mode = S_IRUGO | S_IWUSR,
381 },
382 .read = m48t59_nvram_read,
383 .write = m48t59_nvram_write,
384};
385
386static int m48t59_rtc_probe(struct platform_device *pdev)
387{
388 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
389 struct m48t59_private *m48t59 = NULL;
390 struct resource *res;
391 int ret = -ENOMEM;
392 char *name;
393 const struct rtc_class_ops *ops;
394
395
396 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
397 if (!res) {
398 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
399 if (!res)
400 return -EINVAL;
401 }
402
403 if (res->flags & IORESOURCE_IO) {
404
405
406
407 if (!pdata || !pdata->write_byte || !pdata->read_byte)
408 return -EINVAL;
409 } else if (res->flags & IORESOURCE_MEM) {
410
411 if (!pdata) {
412 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
413 if (!pdata)
414 return -ENOMEM;
415
416 pdev->dev.platform_data = pdata;
417 }
418 if (!pdata->type)
419 pdata->type = M48T59RTC_TYPE_M48T59;
420
421
422 if (!pdata->write_byte)
423 pdata->write_byte = m48t59_mem_writeb;
424 if (!pdata->read_byte)
425 pdata->read_byte = m48t59_mem_readb;
426 }
427
428 m48t59 = kzalloc(sizeof(*m48t59), GFP_KERNEL);
429 if (!m48t59)
430 return -ENOMEM;
431
432 m48t59->ioaddr = pdata->ioaddr;
433
434 if (!m48t59->ioaddr) {
435
436 m48t59->ioaddr = ioremap(res->start, resource_size(res));
437 if (!m48t59->ioaddr)
438 goto out;
439 }
440
441
442
443
444 m48t59->irq = platform_get_irq(pdev, 0);
445 if (m48t59->irq <= 0)
446 m48t59->irq = NO_IRQ;
447
448 if (m48t59->irq != NO_IRQ) {
449 ret = request_irq(m48t59->irq, m48t59_rtc_interrupt,
450 IRQF_SHARED, "rtc-m48t59", &pdev->dev);
451 if (ret)
452 goto out;
453 }
454 switch (pdata->type) {
455 case M48T59RTC_TYPE_M48T59:
456 name = "m48t59";
457 ops = &m48t59_rtc_ops;
458 pdata->offset = 0x1ff0;
459 break;
460 case M48T59RTC_TYPE_M48T02:
461 name = "m48t02";
462 ops = &m48t02_rtc_ops;
463 pdata->offset = 0x7f0;
464 break;
465 case M48T59RTC_TYPE_M48T08:
466 name = "m48t08";
467 ops = &m48t02_rtc_ops;
468 pdata->offset = 0x1ff0;
469 break;
470 default:
471 dev_err(&pdev->dev, "Unknown RTC type\n");
472 ret = -ENODEV;
473 goto out;
474 }
475
476 spin_lock_init(&m48t59->lock);
477 platform_set_drvdata(pdev, m48t59);
478
479 m48t59->rtc = rtc_device_register(name, &pdev->dev, ops, THIS_MODULE);
480 if (IS_ERR(m48t59->rtc)) {
481 ret = PTR_ERR(m48t59->rtc);
482 goto out;
483 }
484
485 m48t59_nvram_attr.size = pdata->offset;
486
487 ret = sysfs_create_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
488 if (ret) {
489 rtc_device_unregister(m48t59->rtc);
490 goto out;
491 }
492
493 return 0;
494
495out:
496 if (m48t59->irq != NO_IRQ)
497 free_irq(m48t59->irq, &pdev->dev);
498 if (m48t59->ioaddr)
499 iounmap(m48t59->ioaddr);
500 kfree(m48t59);
501 return ret;
502}
503
504static int m48t59_rtc_remove(struct platform_device *pdev)
505{
506 struct m48t59_private *m48t59 = platform_get_drvdata(pdev);
507 struct m48t59_plat_data *pdata = pdev->dev.platform_data;
508
509 sysfs_remove_bin_file(&pdev->dev.kobj, &m48t59_nvram_attr);
510 if (!IS_ERR(m48t59->rtc))
511 rtc_device_unregister(m48t59->rtc);
512 if (m48t59->ioaddr && !pdata->ioaddr)
513 iounmap(m48t59->ioaddr);
514 if (m48t59->irq != NO_IRQ)
515 free_irq(m48t59->irq, &pdev->dev);
516 platform_set_drvdata(pdev, NULL);
517 kfree(m48t59);
518 return 0;
519}
520
521
522MODULE_ALIAS("platform:rtc-m48t59");
523
524static struct platform_driver m48t59_rtc_driver = {
525 .driver = {
526 .name = "rtc-m48t59",
527 .owner = THIS_MODULE,
528 },
529 .probe = m48t59_rtc_probe,
530 .remove = m48t59_rtc_remove,
531};
532
533module_platform_driver(m48t59_rtc_driver);
534
535MODULE_AUTHOR("Mark Zhan <rongkai.zhan@windriver.com>");
536MODULE_DESCRIPTION("M48T59/M48T02/M48T08 RTC driver");
537MODULE_LICENSE("GPL");
538