1 /* drivers/rtc/rtc-ds3234.c
3 * Driver for Dallas Semiconductor (DS3234) SPI RTC with Integrated Crystal
6 * Copyright (C) 2008 MIMOMax Wireless Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * 07-May-2008: Dennis Aberilla <denzzzhome@yahoo.com>
15 * - Created based on the max6902 code. Only implements the
16 * date/time keeping functions; no SRAM yet.
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtc.h>
22 #include <linux/spi/spi.h>
23 #include <linux/bcd.h>
25 #define DS3234_REG_SECONDS 0x00
26 #define DS3234_REG_MINUTES 0x01
27 #define DS3234_REG_HOURS 0x02
28 #define DS3234_REG_DAY 0x03
29 #define DS3234_REG_DATE 0x04
30 #define DS3234_REG_MONTH 0x05
31 #define DS3234_REG_YEAR 0x06
32 #define DS3234_REG_CENTURY (1 << 7) /* Bit 7 of the Month register */
34 #define DS3234_REG_CONTROL 0x0E
35 #define DS3234_REG_CONT_STAT 0x0F
40 struct rtc_device *rtc;
41 u8 buf[8]; /* Burst read: addr + 7 regs */
46 static void ds3234_set_reg(struct device *dev, unsigned char address,
49 struct spi_device *spi = to_spi_device(dev);
52 /* MSB must be '1' to indicate write */
53 buf[0] = address | 0x80;
56 spi_write(spi, buf, 2);
59 static int ds3234_get_reg(struct device *dev, unsigned char address,
62 struct spi_device *spi = to_spi_device(dev);
63 struct ds3234 *chip = dev_get_drvdata(dev);
64 struct spi_message message;
65 struct spi_transfer xfer;
71 /* Build our spi message */
72 spi_message_init(&message);
73 memset(&xfer, 0, sizeof(xfer));
75 /* Address + dummy tx byte */
77 xfer.tx_buf = chip->tx_buf;
78 xfer.rx_buf = chip->rx_buf;
80 chip->tx_buf[0] = address;
81 chip->tx_buf[1] = 0xff;
83 spi_message_add_tail(&xfer, &message);
86 status = spi_sync(spi, &message);
88 status = message.status;
92 *data = chip->rx_buf[1];
97 static int ds3234_get_datetime(struct device *dev, struct rtc_time *dt)
99 struct spi_device *spi = to_spi_device(dev);
100 struct ds3234 *chip = dev_get_drvdata(dev);
101 struct spi_message message;
102 struct spi_transfer xfer;
105 /* build the message */
106 spi_message_init(&message);
107 memset(&xfer, 0, sizeof(xfer));
108 xfer.len = 1 + 7; /* Addr + 7 registers */
109 xfer.tx_buf = chip->buf;
110 xfer.rx_buf = chip->buf;
111 chip->buf[0] = 0x00; /* Start address */
112 spi_message_add_tail(&xfer, &message);
115 status = spi_sync(spi, &message);
117 status = message.status;
121 /* Seconds, Minutes, Hours, Day, Date, Month, Year */
122 dt->tm_sec = bcd2bin(chip->buf[1]);
123 dt->tm_min = bcd2bin(chip->buf[2]);
124 dt->tm_hour = bcd2bin(chip->buf[3] & 0x3f);
125 dt->tm_wday = bcd2bin(chip->buf[4]) - 1; /* 0 = Sun */
126 dt->tm_mday = bcd2bin(chip->buf[5]);
127 dt->tm_mon = bcd2bin(chip->buf[6] & 0x1f) - 1; /* 0 = Jan */
128 dt->tm_year = bcd2bin(chip->buf[7] & 0xff) + 100; /* Assume 20YY */
131 dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
132 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
133 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
134 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
135 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
136 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
137 dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
138 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
144 static int ds3234_set_datetime(struct device *dev, struct rtc_time *dt)
147 dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
148 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
149 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
150 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
151 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
152 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
153 dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
154 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
157 ds3234_set_reg(dev, DS3234_REG_SECONDS, bin2bcd(dt->tm_sec));
158 ds3234_set_reg(dev, DS3234_REG_MINUTES, bin2bcd(dt->tm_min));
159 ds3234_set_reg(dev, DS3234_REG_HOURS, bin2bcd(dt->tm_hour) & 0x3f);
162 ds3234_set_reg(dev, DS3234_REG_DAY, bin2bcd(dt->tm_wday + 1));
163 ds3234_set_reg(dev, DS3234_REG_DATE, bin2bcd(dt->tm_mday));
166 ds3234_set_reg(dev, DS3234_REG_MONTH, bin2bcd(dt->tm_mon + 1));
168 /* Assume 20YY although we just want to make sure not to go negative. */
169 if (dt->tm_year > 100)
172 ds3234_set_reg(dev, DS3234_REG_YEAR, bin2bcd(dt->tm_year));
177 static int ds3234_read_time(struct device *dev, struct rtc_time *tm)
179 return ds3234_get_datetime(dev, tm);
182 static int ds3234_set_time(struct device *dev, struct rtc_time *tm)
184 return ds3234_set_datetime(dev, tm);
187 static const struct rtc_class_ops ds3234_rtc_ops = {
188 .read_time = ds3234_read_time,
189 .set_time = ds3234_set_time,
192 static int __devinit ds3234_probe(struct spi_device *spi)
194 struct rtc_device *rtc;
199 rtc = rtc_device_register("ds3234",
200 &spi->dev, &ds3234_rtc_ops, THIS_MODULE);
204 spi->mode = SPI_MODE_3;
205 spi->bits_per_word = 8;
208 chip = kzalloc(sizeof(struct ds3234), GFP_KERNEL);
210 rtc_device_unregister(rtc);
214 dev_set_drvdata(&spi->dev, chip);
216 res = ds3234_get_reg(&spi->dev, DS3234_REG_SECONDS, &tmp);
218 rtc_device_unregister(rtc);
225 * BIT 7 6 5 4 3 2 1 0
226 * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
231 * BIT 7 6 5 4 3 2 1 0
232 * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
236 ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
237 ds3234_set_reg(&spi->dev, DS3234_REG_CONTROL, tmp & 0x1c);
239 ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
240 ds3234_set_reg(&spi->dev, DS3234_REG_CONT_STAT, tmp & 0x88);
242 /* Print our settings */
243 ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
244 dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp);
246 ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
247 dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp);
252 static int __devexit ds3234_remove(struct spi_device *spi)
254 struct ds3234 *chip = platform_get_drvdata(spi);
255 struct rtc_device *rtc = chip->rtc;
258 rtc_device_unregister(rtc);
265 static struct spi_driver ds3234_driver = {
268 .bus = &spi_bus_type,
269 .owner = THIS_MODULE,
271 .probe = ds3234_probe,
272 .remove = __devexit_p(ds3234_remove),
275 static __init int ds3234_init(void)
277 printk(KERN_INFO "DS3234 SPI RTC Driver\n");
278 return spi_register_driver(&ds3234_driver);
280 module_init(ds3234_init);
282 static __exit void ds3234_exit(void)
284 spi_unregister_driver(&ds3234_driver);
286 module_exit(ds3234_exit);
288 MODULE_DESCRIPTION("DS3234 SPI RTC driver");
289 MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
290 MODULE_LICENSE("GPL");