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