blob: 9c04fd2bc209b033ee209b3f2ab3e5f689ccbfdf [file] [log] [blame]
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -07001/*
2 * Dallas DS1216 RTC driver
3 *
4 * Copyright (c) 2007 Thomas Bogendoerfer
5 *
6 */
7
8#include <linux/module.h>
9#include <linux/rtc.h>
10#include <linux/platform_device.h>
11#include <linux/bcd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070013
Alessandro Zummo97a1f952009-01-06 14:42:23 -080014#define DRV_VERSION "0.2"
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070015
16struct ds1216_regs {
17 u8 tsec;
18 u8 sec;
19 u8 min;
20 u8 hour;
21 u8 wday;
22 u8 mday;
23 u8 month;
24 u8 year;
25};
26
27#define DS1216_HOUR_1224 (1 << 7)
28#define DS1216_HOUR_AMPM (1 << 5)
29
30struct ds1216_priv {
31 struct rtc_device *rtc;
32 void __iomem *ioaddr;
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070033};
34
35static const u8 magic[] = {
36 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
37};
38
39/*
40 * Read the 64 bit we'd like to have - It a series
41 * of 64 bits showing up in the LSB of the base register.
42 *
43 */
44static void ds1216_read(u8 __iomem *ioaddr, u8 *buf)
45{
46 unsigned char c;
47 int i, j;
48
49 for (i = 0; i < 8; i++) {
50 c = 0;
51 for (j = 0; j < 8; j++)
52 c |= (readb(ioaddr) & 0x1) << j;
53 buf[i] = c;
54 }
55}
56
57static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf)
58{
59 unsigned char c;
60 int i, j;
61
62 for (i = 0; i < 8; i++) {
63 c = buf[i];
64 for (j = 0; j < 8; j++) {
65 writeb(c, ioaddr);
66 c = c >> 1;
67 }
68 }
69}
70
71static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr)
72{
73 /* Reset magic pointer */
74 readb(ioaddr);
75 /* Write 64 bit magic to DS1216 */
76 ds1216_write(ioaddr, magic);
77}
78
79static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
80{
81 struct platform_device *pdev = to_platform_device(dev);
82 struct ds1216_priv *priv = platform_get_drvdata(pdev);
83 struct ds1216_regs regs;
84
85 ds1216_switch_ds_to_clock(priv->ioaddr);
86 ds1216_read(priv->ioaddr, (u8 *)&regs);
87
Adrian Bunkfe20ba72008-10-18 20:28:41 -070088 tm->tm_sec = bcd2bin(regs.sec);
89 tm->tm_min = bcd2bin(regs.min);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070090 if (regs.hour & DS1216_HOUR_1224) {
91 /* AM/PM mode */
Adrian Bunkfe20ba72008-10-18 20:28:41 -070092 tm->tm_hour = bcd2bin(regs.hour & 0x1f);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070093 if (regs.hour & DS1216_HOUR_AMPM)
94 tm->tm_hour += 12;
95 } else
Adrian Bunkfe20ba72008-10-18 20:28:41 -070096 tm->tm_hour = bcd2bin(regs.hour & 0x3f);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -070097 tm->tm_wday = (regs.wday & 7) - 1;
Adrian Bunkfe20ba72008-10-18 20:28:41 -070098 tm->tm_mday = bcd2bin(regs.mday & 0x3f);
99 tm->tm_mon = bcd2bin(regs.month & 0x1f);
100 tm->tm_year = bcd2bin(regs.year);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700101 if (tm->tm_year < 70)
102 tm->tm_year += 100;
Alessandro Zummo97a1f952009-01-06 14:42:23 -0800103
104 return rtc_valid_tm(tm);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700105}
106
107static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
108{
109 struct platform_device *pdev = to_platform_device(dev);
110 struct ds1216_priv *priv = platform_get_drvdata(pdev);
111 struct ds1216_regs regs;
112
113 ds1216_switch_ds_to_clock(priv->ioaddr);
114 ds1216_read(priv->ioaddr, (u8 *)&regs);
115
116 regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700117 regs.sec = bin2bcd(tm->tm_sec);
118 regs.min = bin2bcd(tm->tm_min);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700119 regs.hour &= DS1216_HOUR_1224;
120 if (regs.hour && tm->tm_hour > 12) {
121 regs.hour |= DS1216_HOUR_AMPM;
122 tm->tm_hour -= 12;
123 }
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700124 regs.hour |= bin2bcd(tm->tm_hour);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700125 regs.wday &= ~7;
126 regs.wday |= tm->tm_wday;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700127 regs.mday = bin2bcd(tm->tm_mday);
128 regs.month = bin2bcd(tm->tm_mon);
129 regs.year = bin2bcd(tm->tm_year % 100);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700130
131 ds1216_switch_ds_to_clock(priv->ioaddr);
132 ds1216_write(priv->ioaddr, (u8 *)&regs);
133 return 0;
134}
135
136static const struct rtc_class_ops ds1216_rtc_ops = {
137 .read_time = ds1216_rtc_read_time,
138 .set_time = ds1216_rtc_set_time,
139};
140
Alessandro Zummo97a1f952009-01-06 14:42:23 -0800141static int __init ds1216_rtc_probe(struct platform_device *pdev)
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700142{
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700143 struct resource *res;
144 struct ds1216_priv *priv;
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700145 u8 dummy[8];
146
147 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148 if (!res)
149 return -ENODEV;
Jingoo Han15c15672013-04-29 16:20:37 -0700150 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700151 if (!priv)
152 return -ENOMEM;
Alessandro Zummo97a1f952009-01-06 14:42:23 -0800153
154 platform_set_drvdata(pdev, priv);
155
Jingoo Han15c15672013-04-29 16:20:37 -0700156 priv->ioaddr = devm_ioremap_resource(&pdev->dev, res);
157 if (IS_ERR(priv->ioaddr))
158 return PTR_ERR(priv->ioaddr);
159
160 priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216",
161 &ds1216_rtc_ops, THIS_MODULE);
162 if (IS_ERR(priv->rtc))
163 return PTR_ERR(priv->rtc);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700164
165 /* dummy read to get clock into a known state */
166 ds1216_read(priv->ioaddr, dummy);
167 return 0;
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700168}
169
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700170static struct platform_driver ds1216_rtc_platform_driver = {
171 .driver = {
172 .name = "rtc-ds1216",
173 .owner = THIS_MODULE,
174 },
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700175};
176
Fabio Porcedda061d2a32013-07-03 15:08:03 -0700177module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe);
Thomas Bogendoerfer537739d2007-07-17 04:05:06 -0700178
179MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
180MODULE_DESCRIPTION("DS1216 RTC driver");
181MODULE_LICENSE("GPL");
182MODULE_VERSION(DRV_VERSION);
Kay Sieversad28a072008-04-10 21:29:25 -0700183MODULE_ALIAS("platform:rtc-ds1216");