xref: /linux/drivers/rtc/rtc-rx4581.c (revision cce2da9a1fc6ef144d830587080476fec283d670)
1*cce2da9aSTorben Hohn /* drivers/rtc/rtc-rx4581.c
2*cce2da9aSTorben Hohn  *
3*cce2da9aSTorben Hohn  * written by Torben Hohn <torbenh@linutronix.de>
4*cce2da9aSTorben Hohn  *
5*cce2da9aSTorben Hohn  * Based on:
6*cce2da9aSTorben Hohn  * drivers/rtc/rtc-max6902.c
7*cce2da9aSTorben Hohn  *
8*cce2da9aSTorben Hohn  * Copyright (C) 2006 8D Technologies inc.
9*cce2da9aSTorben Hohn  * Copyright (C) 2004 Compulab Ltd.
10*cce2da9aSTorben Hohn  *
11*cce2da9aSTorben Hohn  * This program is free software; you can redistribute it and/or modify
12*cce2da9aSTorben Hohn  * it under the terms of the GNU General Public License version 2 as
13*cce2da9aSTorben Hohn  * published by the Free Software Foundation.
14*cce2da9aSTorben Hohn  *
15*cce2da9aSTorben Hohn  * Driver for MAX6902 spi RTC
16*cce2da9aSTorben Hohn  *
17*cce2da9aSTorben Hohn  * and based on:
18*cce2da9aSTorben Hohn  * drivers/rtc/rtc-rx8581.c
19*cce2da9aSTorben Hohn  *
20*cce2da9aSTorben Hohn  * An I2C driver for the Epson RX8581 RTC
21*cce2da9aSTorben Hohn  *
22*cce2da9aSTorben Hohn  * Author: Martyn Welch <martyn.welch@ge.com>
23*cce2da9aSTorben Hohn  * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
24*cce2da9aSTorben Hohn  *
25*cce2da9aSTorben Hohn  * This program is free software; you can redistribute it and/or modify
26*cce2da9aSTorben Hohn  * it under the terms of the GNU General Public License version 2 as
27*cce2da9aSTorben Hohn  * published by the Free Software Foundation.
28*cce2da9aSTorben Hohn  *
29*cce2da9aSTorben Hohn  * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
30*cce2da9aSTorben Hohn  * Copyright 2005-06 Tower Technologies
31*cce2da9aSTorben Hohn  *
32*cce2da9aSTorben Hohn  */
33*cce2da9aSTorben Hohn 
34*cce2da9aSTorben Hohn #include <linux/module.h>
35*cce2da9aSTorben Hohn #include <linux/kernel.h>
36*cce2da9aSTorben Hohn #include <linux/platform_device.h>
37*cce2da9aSTorben Hohn #include <linux/init.h>
38*cce2da9aSTorben Hohn #include <linux/rtc.h>
39*cce2da9aSTorben Hohn #include <linux/spi/spi.h>
40*cce2da9aSTorben Hohn #include <linux/bcd.h>
41*cce2da9aSTorben Hohn 
42*cce2da9aSTorben Hohn #define RX4581_REG_SC		0x00 /* Second in BCD */
43*cce2da9aSTorben Hohn #define RX4581_REG_MN		0x01 /* Minute in BCD */
44*cce2da9aSTorben Hohn #define RX4581_REG_HR		0x02 /* Hour in BCD */
45*cce2da9aSTorben Hohn #define RX4581_REG_DW		0x03 /* Day of Week */
46*cce2da9aSTorben Hohn #define RX4581_REG_DM		0x04 /* Day of Month in BCD */
47*cce2da9aSTorben Hohn #define RX4581_REG_MO		0x05 /* Month in BCD */
48*cce2da9aSTorben Hohn #define RX4581_REG_YR		0x06 /* Year in BCD */
49*cce2da9aSTorben Hohn #define RX4581_REG_RAM		0x07 /* RAM */
50*cce2da9aSTorben Hohn #define RX4581_REG_AMN		0x08 /* Alarm Min in BCD*/
51*cce2da9aSTorben Hohn #define RX4581_REG_AHR		0x09 /* Alarm Hour in BCD */
52*cce2da9aSTorben Hohn #define RX4581_REG_ADM		0x0A
53*cce2da9aSTorben Hohn #define RX4581_REG_ADW		0x0A
54*cce2da9aSTorben Hohn #define RX4581_REG_TMR0		0x0B
55*cce2da9aSTorben Hohn #define RX4581_REG_TMR1		0x0C
56*cce2da9aSTorben Hohn #define RX4581_REG_EXT		0x0D /* Extension Register */
57*cce2da9aSTorben Hohn #define RX4581_REG_FLAG		0x0E /* Flag Register */
58*cce2da9aSTorben Hohn #define RX4581_REG_CTRL		0x0F /* Control Register */
59*cce2da9aSTorben Hohn 
60*cce2da9aSTorben Hohn 
61*cce2da9aSTorben Hohn /* Flag Register bit definitions */
62*cce2da9aSTorben Hohn #define RX4581_FLAG_UF		0x20 /* Update */
63*cce2da9aSTorben Hohn #define RX4581_FLAG_TF		0x10 /* Timer */
64*cce2da9aSTorben Hohn #define RX4581_FLAG_AF		0x08 /* Alarm */
65*cce2da9aSTorben Hohn #define RX4581_FLAG_VLF		0x02 /* Voltage Low */
66*cce2da9aSTorben Hohn 
67*cce2da9aSTorben Hohn /* Control Register bit definitions */
68*cce2da9aSTorben Hohn #define RX4581_CTRL_UIE		0x20 /* Update Interrupt Enable */
69*cce2da9aSTorben Hohn #define RX4581_CTRL_TIE		0x10 /* Timer Interrupt Enable */
70*cce2da9aSTorben Hohn #define RX4581_CTRL_AIE		0x08 /* Alarm Interrupt Enable */
71*cce2da9aSTorben Hohn #define RX4581_CTRL_STOP	0x02 /* STOP bit */
72*cce2da9aSTorben Hohn #define RX4581_CTRL_RESET	0x01 /* RESET bit */
73*cce2da9aSTorben Hohn 
74*cce2da9aSTorben Hohn static int rx4581_set_reg(struct device *dev, unsigned char address,
75*cce2da9aSTorben Hohn 				unsigned char data)
76*cce2da9aSTorben Hohn {
77*cce2da9aSTorben Hohn 	struct spi_device *spi = to_spi_device(dev);
78*cce2da9aSTorben Hohn 	unsigned char buf[2];
79*cce2da9aSTorben Hohn 
80*cce2da9aSTorben Hohn 	/* high nibble must be '0' to write */
81*cce2da9aSTorben Hohn 	buf[0] = address & 0x0f;
82*cce2da9aSTorben Hohn 	buf[1] = data;
83*cce2da9aSTorben Hohn 
84*cce2da9aSTorben Hohn 	return spi_write_then_read(spi, buf, 2, NULL, 0);
85*cce2da9aSTorben Hohn }
86*cce2da9aSTorben Hohn 
87*cce2da9aSTorben Hohn static int rx4581_get_reg(struct device *dev, unsigned char address,
88*cce2da9aSTorben Hohn 				unsigned char *data)
89*cce2da9aSTorben Hohn {
90*cce2da9aSTorben Hohn 	struct spi_device *spi = to_spi_device(dev);
91*cce2da9aSTorben Hohn 
92*cce2da9aSTorben Hohn 	/* Set MSB to indicate read */
93*cce2da9aSTorben Hohn 	*data = address | 0x80;
94*cce2da9aSTorben Hohn 
95*cce2da9aSTorben Hohn 	return spi_write_then_read(spi, data, 1, data, 1);
96*cce2da9aSTorben Hohn }
97*cce2da9aSTorben Hohn 
98*cce2da9aSTorben Hohn /*
99*cce2da9aSTorben Hohn  * In the routines that deal directly with the rx8581 hardware, we use
100*cce2da9aSTorben Hohn  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
101*cce2da9aSTorben Hohn  */
102*cce2da9aSTorben Hohn static int rx4581_get_datetime(struct device *dev, struct rtc_time *tm)
103*cce2da9aSTorben Hohn {
104*cce2da9aSTorben Hohn 	struct spi_device *spi = to_spi_device(dev);
105*cce2da9aSTorben Hohn 	unsigned char date[7];
106*cce2da9aSTorben Hohn 	unsigned char data;
107*cce2da9aSTorben Hohn 	int err;
108*cce2da9aSTorben Hohn 
109*cce2da9aSTorben Hohn 	/* First we ensure that the "update flag" is not set, we read the
110*cce2da9aSTorben Hohn 	 * time and date then re-read the "update flag". If the update flag
111*cce2da9aSTorben Hohn 	 * has been set, we know that the time has changed during the read so
112*cce2da9aSTorben Hohn 	 * we repeat the whole process again.
113*cce2da9aSTorben Hohn 	 */
114*cce2da9aSTorben Hohn 	err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
115*cce2da9aSTorben Hohn 	if (err != 0) {
116*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to read device flags\n");
117*cce2da9aSTorben Hohn 		return -EIO;
118*cce2da9aSTorben Hohn 	}
119*cce2da9aSTorben Hohn 
120*cce2da9aSTorben Hohn 	do {
121*cce2da9aSTorben Hohn 		/* If update flag set, clear it */
122*cce2da9aSTorben Hohn 		if (data & RX4581_FLAG_UF) {
123*cce2da9aSTorben Hohn 			err = rx4581_set_reg(dev,
124*cce2da9aSTorben Hohn 				RX4581_REG_FLAG, (data & ~RX4581_FLAG_UF));
125*cce2da9aSTorben Hohn 			if (err != 0) {
126*cce2da9aSTorben Hohn 				dev_err(dev, "Unable to write device "
127*cce2da9aSTorben Hohn 					"flags\n");
128*cce2da9aSTorben Hohn 				return -EIO;
129*cce2da9aSTorben Hohn 			}
130*cce2da9aSTorben Hohn 		}
131*cce2da9aSTorben Hohn 
132*cce2da9aSTorben Hohn 		/* Now read time and date */
133*cce2da9aSTorben Hohn 		date[0] = 0x80;
134*cce2da9aSTorben Hohn 		err = spi_write_then_read(spi, date, 1, date, 7);
135*cce2da9aSTorben Hohn 		if (err < 0) {
136*cce2da9aSTorben Hohn 			dev_err(dev, "Unable to read date\n");
137*cce2da9aSTorben Hohn 			return -EIO;
138*cce2da9aSTorben Hohn 		}
139*cce2da9aSTorben Hohn 
140*cce2da9aSTorben Hohn 		/* Check flag register */
141*cce2da9aSTorben Hohn 		err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
142*cce2da9aSTorben Hohn 		if (err != 0) {
143*cce2da9aSTorben Hohn 			dev_err(dev, "Unable to read device flags\n");
144*cce2da9aSTorben Hohn 			return -EIO;
145*cce2da9aSTorben Hohn 		}
146*cce2da9aSTorben Hohn 	} while (data & RX4581_FLAG_UF);
147*cce2da9aSTorben Hohn 
148*cce2da9aSTorben Hohn 	if (data & RX4581_FLAG_VLF)
149*cce2da9aSTorben Hohn 		dev_info(dev,
150*cce2da9aSTorben Hohn 			"low voltage detected, date/time is not reliable.\n");
151*cce2da9aSTorben Hohn 
152*cce2da9aSTorben Hohn 	dev_dbg(dev,
153*cce2da9aSTorben Hohn 		"%s: raw data is sec=%02x, min=%02x, hr=%02x, "
154*cce2da9aSTorben Hohn 		"wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
155*cce2da9aSTorben Hohn 		__func__,
156*cce2da9aSTorben Hohn 		date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
157*cce2da9aSTorben Hohn 
158*cce2da9aSTorben Hohn 	tm->tm_sec = bcd2bin(date[RX4581_REG_SC] & 0x7F);
159*cce2da9aSTorben Hohn 	tm->tm_min = bcd2bin(date[RX4581_REG_MN] & 0x7F);
160*cce2da9aSTorben Hohn 	tm->tm_hour = bcd2bin(date[RX4581_REG_HR] & 0x3F); /* rtc hr 0-23 */
161*cce2da9aSTorben Hohn 	tm->tm_wday = ilog2(date[RX4581_REG_DW] & 0x7F);
162*cce2da9aSTorben Hohn 	tm->tm_mday = bcd2bin(date[RX4581_REG_DM] & 0x3F);
163*cce2da9aSTorben Hohn 	tm->tm_mon = bcd2bin(date[RX4581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
164*cce2da9aSTorben Hohn 	tm->tm_year = bcd2bin(date[RX4581_REG_YR]);
165*cce2da9aSTorben Hohn 	if (tm->tm_year < 70)
166*cce2da9aSTorben Hohn 		tm->tm_year += 100;	/* assume we are in 1970...2069 */
167*cce2da9aSTorben Hohn 
168*cce2da9aSTorben Hohn 
169*cce2da9aSTorben Hohn 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
170*cce2da9aSTorben Hohn 		"mday=%d, mon=%d, year=%d, wday=%d\n",
171*cce2da9aSTorben Hohn 		__func__,
172*cce2da9aSTorben Hohn 		tm->tm_sec, tm->tm_min, tm->tm_hour,
173*cce2da9aSTorben Hohn 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
174*cce2da9aSTorben Hohn 
175*cce2da9aSTorben Hohn 	err = rtc_valid_tm(tm);
176*cce2da9aSTorben Hohn 	if (err < 0)
177*cce2da9aSTorben Hohn 		dev_err(dev, "retrieved date/time is not valid.\n");
178*cce2da9aSTorben Hohn 
179*cce2da9aSTorben Hohn 	return err;
180*cce2da9aSTorben Hohn }
181*cce2da9aSTorben Hohn 
182*cce2da9aSTorben Hohn static int rx4581_set_datetime(struct device *dev, struct rtc_time *tm)
183*cce2da9aSTorben Hohn {
184*cce2da9aSTorben Hohn 	struct spi_device *spi = to_spi_device(dev);
185*cce2da9aSTorben Hohn 	int err;
186*cce2da9aSTorben Hohn 	unsigned char buf[8], data;
187*cce2da9aSTorben Hohn 
188*cce2da9aSTorben Hohn 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
189*cce2da9aSTorben Hohn 		"mday=%d, mon=%d, year=%d, wday=%d\n",
190*cce2da9aSTorben Hohn 		__func__,
191*cce2da9aSTorben Hohn 		tm->tm_sec, tm->tm_min, tm->tm_hour,
192*cce2da9aSTorben Hohn 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
193*cce2da9aSTorben Hohn 
194*cce2da9aSTorben Hohn 	buf[0] = 0x00;
195*cce2da9aSTorben Hohn 	/* hours, minutes and seconds */
196*cce2da9aSTorben Hohn 	buf[RX4581_REG_SC+1] = bin2bcd(tm->tm_sec);
197*cce2da9aSTorben Hohn 	buf[RX4581_REG_MN+1] = bin2bcd(tm->tm_min);
198*cce2da9aSTorben Hohn 	buf[RX4581_REG_HR+1] = bin2bcd(tm->tm_hour);
199*cce2da9aSTorben Hohn 
200*cce2da9aSTorben Hohn 	buf[RX4581_REG_DM+1] = bin2bcd(tm->tm_mday);
201*cce2da9aSTorben Hohn 
202*cce2da9aSTorben Hohn 	/* month, 1 - 12 */
203*cce2da9aSTorben Hohn 	buf[RX4581_REG_MO+1] = bin2bcd(tm->tm_mon + 1);
204*cce2da9aSTorben Hohn 
205*cce2da9aSTorben Hohn 	/* year and century */
206*cce2da9aSTorben Hohn 	buf[RX4581_REG_YR+1] = bin2bcd(tm->tm_year % 100);
207*cce2da9aSTorben Hohn 	buf[RX4581_REG_DW+1] = (0x1 << tm->tm_wday);
208*cce2da9aSTorben Hohn 
209*cce2da9aSTorben Hohn 	/* Stop the clock */
210*cce2da9aSTorben Hohn 	err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
211*cce2da9aSTorben Hohn 	if (err != 0) {
212*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to read control register\n");
213*cce2da9aSTorben Hohn 		return -EIO;
214*cce2da9aSTorben Hohn 	}
215*cce2da9aSTorben Hohn 
216*cce2da9aSTorben Hohn 	err = rx4581_set_reg(dev, RX4581_REG_CTRL,
217*cce2da9aSTorben Hohn 		(data | RX4581_CTRL_STOP));
218*cce2da9aSTorben Hohn 	if (err != 0) {
219*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to write control register\n");
220*cce2da9aSTorben Hohn 		return -EIO;
221*cce2da9aSTorben Hohn 	}
222*cce2da9aSTorben Hohn 
223*cce2da9aSTorben Hohn 	/* write register's data */
224*cce2da9aSTorben Hohn 	err = spi_write_then_read(spi, buf, 8, NULL, 0);
225*cce2da9aSTorben Hohn 	if (err != 0) {
226*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to write to date registers\n");
227*cce2da9aSTorben Hohn 		return -EIO;
228*cce2da9aSTorben Hohn 	}
229*cce2da9aSTorben Hohn 
230*cce2da9aSTorben Hohn 	/* get VLF and clear it */
231*cce2da9aSTorben Hohn 	err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
232*cce2da9aSTorben Hohn 	if (err != 0) {
233*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to read flag register\n");
234*cce2da9aSTorben Hohn 		return -EIO;
235*cce2da9aSTorben Hohn 	}
236*cce2da9aSTorben Hohn 
237*cce2da9aSTorben Hohn 	err = rx4581_set_reg(dev, RX4581_REG_FLAG,
238*cce2da9aSTorben Hohn 		(data & ~(RX4581_FLAG_VLF)));
239*cce2da9aSTorben Hohn 	if (err != 0) {
240*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to write flag register\n");
241*cce2da9aSTorben Hohn 		return -EIO;
242*cce2da9aSTorben Hohn 	}
243*cce2da9aSTorben Hohn 
244*cce2da9aSTorben Hohn 	/* Restart the clock */
245*cce2da9aSTorben Hohn 	err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
246*cce2da9aSTorben Hohn 	if (err != 0) {
247*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to read control register\n");
248*cce2da9aSTorben Hohn 		return -EIO;
249*cce2da9aSTorben Hohn 	}
250*cce2da9aSTorben Hohn 
251*cce2da9aSTorben Hohn 	err = rx4581_set_reg(dev, RX4581_REG_CTRL,
252*cce2da9aSTorben Hohn 		(data & ~(RX4581_CTRL_STOP)));
253*cce2da9aSTorben Hohn 	if (err != 0) {
254*cce2da9aSTorben Hohn 		dev_err(dev, "Unable to write control register\n");
255*cce2da9aSTorben Hohn 		return -EIO;
256*cce2da9aSTorben Hohn 	}
257*cce2da9aSTorben Hohn 
258*cce2da9aSTorben Hohn 	return 0;
259*cce2da9aSTorben Hohn }
260*cce2da9aSTorben Hohn 
261*cce2da9aSTorben Hohn static const struct rtc_class_ops rx4581_rtc_ops = {
262*cce2da9aSTorben Hohn 	.read_time	= rx4581_get_datetime,
263*cce2da9aSTorben Hohn 	.set_time	= rx4581_set_datetime,
264*cce2da9aSTorben Hohn };
265*cce2da9aSTorben Hohn 
266*cce2da9aSTorben Hohn static int rx4581_probe(struct spi_device *spi)
267*cce2da9aSTorben Hohn {
268*cce2da9aSTorben Hohn 	struct rtc_device *rtc;
269*cce2da9aSTorben Hohn 	unsigned char tmp;
270*cce2da9aSTorben Hohn 	int res;
271*cce2da9aSTorben Hohn 
272*cce2da9aSTorben Hohn 	res = rx4581_get_reg(&spi->dev, RX4581_REG_SC, &tmp);
273*cce2da9aSTorben Hohn 	if (res != 0)
274*cce2da9aSTorben Hohn 		return res;
275*cce2da9aSTorben Hohn 
276*cce2da9aSTorben Hohn 	rtc = rtc_device_register("rx4581",
277*cce2da9aSTorben Hohn 				&spi->dev, &rx4581_rtc_ops, THIS_MODULE);
278*cce2da9aSTorben Hohn 	if (IS_ERR(rtc))
279*cce2da9aSTorben Hohn 		return PTR_ERR(rtc);
280*cce2da9aSTorben Hohn 
281*cce2da9aSTorben Hohn 	dev_set_drvdata(&spi->dev, rtc);
282*cce2da9aSTorben Hohn 	return 0;
283*cce2da9aSTorben Hohn }
284*cce2da9aSTorben Hohn 
285*cce2da9aSTorben Hohn static int rx4581_remove(struct spi_device *spi)
286*cce2da9aSTorben Hohn {
287*cce2da9aSTorben Hohn 	struct rtc_device *rtc = dev_get_drvdata(&spi->dev);
288*cce2da9aSTorben Hohn 
289*cce2da9aSTorben Hohn 	rtc_device_unregister(rtc);
290*cce2da9aSTorben Hohn 	return 0;
291*cce2da9aSTorben Hohn }
292*cce2da9aSTorben Hohn 
293*cce2da9aSTorben Hohn static const struct spi_device_id rx4581_id[] = {
294*cce2da9aSTorben Hohn 	{ "rx4581", 0 },
295*cce2da9aSTorben Hohn 	{ }
296*cce2da9aSTorben Hohn };
297*cce2da9aSTorben Hohn MODULE_DEVICE_TABLE(spi, rx4581_id);
298*cce2da9aSTorben Hohn 
299*cce2da9aSTorben Hohn static struct spi_driver rx4581_driver = {
300*cce2da9aSTorben Hohn 	.driver = {
301*cce2da9aSTorben Hohn 		.name	= "rtc-rx4581",
302*cce2da9aSTorben Hohn 		.owner	= THIS_MODULE,
303*cce2da9aSTorben Hohn 	},
304*cce2da9aSTorben Hohn 	.probe	= rx4581_probe,
305*cce2da9aSTorben Hohn 	.remove = rx4581_remove,
306*cce2da9aSTorben Hohn 	.id_table = rx4581_id,
307*cce2da9aSTorben Hohn };
308*cce2da9aSTorben Hohn 
309*cce2da9aSTorben Hohn module_spi_driver(rx4581_driver);
310*cce2da9aSTorben Hohn 
311*cce2da9aSTorben Hohn MODULE_DESCRIPTION("rx4581 spi RTC driver");
312*cce2da9aSTorben Hohn MODULE_AUTHOR("Torben Hohn");
313*cce2da9aSTorben Hohn MODULE_LICENSE("GPL");
314*cce2da9aSTorben Hohn MODULE_ALIAS("spi:rtc-rx4581");
315