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