xref: /openbmc/linux/drivers/rtc/rtc-ds1216.c (revision d623f60d)
1 /*
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>
12 #include <linux/slab.h>
13 
14 struct ds1216_regs {
15 	u8 tsec;
16 	u8 sec;
17 	u8 min;
18 	u8 hour;
19 	u8 wday;
20 	u8 mday;
21 	u8 month;
22 	u8 year;
23 };
24 
25 #define DS1216_HOUR_1224	(1 << 7)
26 #define DS1216_HOUR_AMPM	(1 << 5)
27 
28 struct ds1216_priv {
29 	struct rtc_device *rtc;
30 	void __iomem *ioaddr;
31 };
32 
33 static const u8 magic[] = {
34 	0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
35 };
36 
37 /*
38  * Read the 64 bit we'd like to have - It a series
39  * of 64 bits showing up in the LSB of the base register.
40  *
41  */
42 static void ds1216_read(u8 __iomem *ioaddr, u8 *buf)
43 {
44 	unsigned char c;
45 	int i, j;
46 
47 	for (i = 0; i < 8; i++) {
48 		c = 0;
49 		for (j = 0; j < 8; j++)
50 			c |= (readb(ioaddr) & 0x1) << j;
51 		buf[i] = c;
52 	}
53 }
54 
55 static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf)
56 {
57 	unsigned char c;
58 	int i, j;
59 
60 	for (i = 0; i < 8; i++) {
61 		c = buf[i];
62 		for (j = 0; j < 8; j++) {
63 			writeb(c, ioaddr);
64 			c = c >> 1;
65 		}
66 	}
67 }
68 
69 static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr)
70 {
71 	/* Reset magic pointer */
72 	readb(ioaddr);
73 	/* Write 64 bit magic to DS1216 */
74 	ds1216_write(ioaddr, magic);
75 }
76 
77 static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
78 {
79 	struct ds1216_priv *priv = dev_get_drvdata(dev);
80 	struct ds1216_regs regs;
81 
82 	ds1216_switch_ds_to_clock(priv->ioaddr);
83 	ds1216_read(priv->ioaddr, (u8 *)&regs);
84 
85 	tm->tm_sec = bcd2bin(regs.sec);
86 	tm->tm_min = bcd2bin(regs.min);
87 	if (regs.hour & DS1216_HOUR_1224) {
88 		/* AM/PM mode */
89 		tm->tm_hour = bcd2bin(regs.hour & 0x1f);
90 		if (regs.hour & DS1216_HOUR_AMPM)
91 			tm->tm_hour += 12;
92 	} else
93 		tm->tm_hour = bcd2bin(regs.hour & 0x3f);
94 	tm->tm_wday = (regs.wday & 7) - 1;
95 	tm->tm_mday = bcd2bin(regs.mday & 0x3f);
96 	tm->tm_mon = bcd2bin(regs.month & 0x1f);
97 	tm->tm_year = bcd2bin(regs.year);
98 	if (tm->tm_year < 70)
99 		tm->tm_year += 100;
100 
101 	return 0;
102 }
103 
104 static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
105 {
106 	struct ds1216_priv *priv = dev_get_drvdata(dev);
107 	struct ds1216_regs regs;
108 
109 	ds1216_switch_ds_to_clock(priv->ioaddr);
110 	ds1216_read(priv->ioaddr, (u8 *)&regs);
111 
112 	regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
113 	regs.sec = bin2bcd(tm->tm_sec);
114 	regs.min = bin2bcd(tm->tm_min);
115 	regs.hour &= DS1216_HOUR_1224;
116 	if (regs.hour && tm->tm_hour > 12) {
117 		regs.hour |= DS1216_HOUR_AMPM;
118 		tm->tm_hour -= 12;
119 	}
120 	regs.hour |= bin2bcd(tm->tm_hour);
121 	regs.wday &= ~7;
122 	regs.wday |= tm->tm_wday;
123 	regs.mday = bin2bcd(tm->tm_mday);
124 	regs.month = bin2bcd(tm->tm_mon);
125 	regs.year = bin2bcd(tm->tm_year % 100);
126 
127 	ds1216_switch_ds_to_clock(priv->ioaddr);
128 	ds1216_write(priv->ioaddr, (u8 *)&regs);
129 	return 0;
130 }
131 
132 static const struct rtc_class_ops ds1216_rtc_ops = {
133 	.read_time	= ds1216_rtc_read_time,
134 	.set_time	= ds1216_rtc_set_time,
135 };
136 
137 static int __init ds1216_rtc_probe(struct platform_device *pdev)
138 {
139 	struct resource *res;
140 	struct ds1216_priv *priv;
141 	u8 dummy[8];
142 
143 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
144 	if (!priv)
145 		return -ENOMEM;
146 
147 	platform_set_drvdata(pdev, priv);
148 
149 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 	priv->ioaddr = devm_ioremap_resource(&pdev->dev, res);
151 	if (IS_ERR(priv->ioaddr))
152 		return PTR_ERR(priv->ioaddr);
153 
154 	priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216",
155 					&ds1216_rtc_ops, THIS_MODULE);
156 	if (IS_ERR(priv->rtc))
157 		return PTR_ERR(priv->rtc);
158 
159 	/* dummy read to get clock into a known state */
160 	ds1216_read(priv->ioaddr, dummy);
161 	return 0;
162 }
163 
164 static struct platform_driver ds1216_rtc_platform_driver = {
165 	.driver		= {
166 		.name	= "rtc-ds1216",
167 	},
168 };
169 
170 module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe);
171 
172 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
173 MODULE_DESCRIPTION("DS1216 RTC driver");
174 MODULE_LICENSE("GPL");
175 MODULE_ALIAS("platform:rtc-ds1216");
176