2 * rtc-ds1390.c -- driver for DS1390/93/94
4 * Copyright (C) 2008 Mercury IMC Ltd
5 * Written by Mark Jackson <mpfj@mimc.co.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * NOTE : Currently this driver only supports the bare minimum for read
12 * and write the RTC. The extra features provided by the chip family
13 * (alarms, trickle charger, different control registers) are unavailable.
16 #include <linux/platform_device.h>
17 #include <linux/rtc.h>
18 #include <linux/spi/spi.h>
19 #include <linux/bcd.h>
21 #define DS1390_REG_100THS 0x00
22 #define DS1390_REG_SECONDS 0x01
23 #define DS1390_REG_MINUTES 0x02
24 #define DS1390_REG_HOURS 0x03
25 #define DS1390_REG_DAY 0x04
26 #define DS1390_REG_DATE 0x05
27 #define DS1390_REG_MONTH_CENT 0x06
28 #define DS1390_REG_YEAR 0x07
30 #define DS1390_REG_ALARM_100THS 0x08
31 #define DS1390_REG_ALARM_SECONDS 0x09
32 #define DS1390_REG_ALARM_MINUTES 0x0A
33 #define DS1390_REG_ALARM_HOURS 0x0B
34 #define DS1390_REG_ALARM_DAY_DATE 0x0C
36 #define DS1390_REG_CONTROL 0x0D
37 #define DS1390_REG_STATUS 0x0E
38 #define DS1390_REG_TRICKLE 0x0F
41 struct rtc_device *rtc;
42 u8 txrx_buf[9]; /* cmd + 8 registers */
45 static void ds1390_set_reg(struct device *dev, unsigned char address,
48 struct spi_device *spi = to_spi_device(dev);
49 struct ds1390 *chip = dev_get_drvdata(dev);
51 /* Set MSB to indicate write */
52 chip->txrx_buf[0] = address | 0x80;
53 chip->txrx_buf[1] = data;
56 spi_write_then_read(spi, chip->txrx_buf, 2, NULL, 0);
59 static int ds1390_get_reg(struct device *dev, unsigned char address,
62 struct spi_device *spi = to_spi_device(dev);
63 struct ds1390 *chip = dev_get_drvdata(dev);
69 /* Clear MSB to indicate read */
70 chip->txrx_buf[0] = address & 0x7f;
72 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
76 *data = chip->txrx_buf[1];
81 static int ds1390_get_datetime(struct device *dev, struct rtc_time *dt)
83 struct spi_device *spi = to_spi_device(dev);
84 struct ds1390 *chip = dev_get_drvdata(dev);
87 /* build the message */
88 chip->txrx_buf[0] = DS1390_REG_SECONDS;
91 status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
95 /* The chip sends data in this order:
96 * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
97 dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
98 dt->tm_min = bcd2bin(chip->txrx_buf[1]);
99 dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
100 dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
101 dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
102 /* mask off century bit */
103 dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
104 /* adjust for century bit */
105 dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
107 return rtc_valid_tm(dt);
110 static int ds1390_set_datetime(struct device *dev, struct rtc_time *dt)
112 struct spi_device *spi = to_spi_device(dev);
113 struct ds1390 *chip = dev_get_drvdata(dev);
115 /* build the message */
116 chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
117 chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
118 chip->txrx_buf[2] = bin2bcd(dt->tm_min);
119 chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
120 chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
121 chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
122 chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
123 ((dt->tm_year > 99) ? 0x80 : 0x00);
124 chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
127 return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
130 static int ds1390_read_time(struct device *dev, struct rtc_time *tm)
132 return ds1390_get_datetime(dev, tm);
135 static int ds1390_set_time(struct device *dev, struct rtc_time *tm)
137 return ds1390_set_datetime(dev, tm);
140 static const struct rtc_class_ops ds1390_rtc_ops = {
141 .read_time = ds1390_read_time,
142 .set_time = ds1390_set_time,
145 static int __devinit ds1390_probe(struct spi_device *spi)
147 struct rtc_device *rtc;
152 printk(KERN_DEBUG "DS1390 SPI RTC driver\n");
154 rtc = rtc_device_register("ds1390",
155 &spi->dev, &ds1390_rtc_ops, THIS_MODULE);
157 printk(KERN_ALERT "RTC : unable to register device\n");
161 spi->mode = SPI_MODE_3;
162 spi->bits_per_word = 8;
165 chip = kzalloc(sizeof *chip, GFP_KERNEL);
167 printk(KERN_ALERT "RTC : unable to allocate device memory\n");
168 rtc_device_unregister(rtc);
172 dev_set_drvdata(&spi->dev, chip);
174 res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
176 printk(KERN_ALERT "RTC : unable to read device\n");
177 rtc_device_unregister(rtc);
184 static int __devexit ds1390_remove(struct spi_device *spi)
186 struct ds1390 *chip = platform_get_drvdata(spi);
187 struct rtc_device *rtc = chip->rtc;
190 rtc_device_unregister(rtc);
197 static struct spi_driver ds1390_driver = {
199 .name = "rtc-ds1390",
200 .owner = THIS_MODULE,
202 .probe = ds1390_probe,
203 .remove = __devexit_p(ds1390_remove),
206 static __init int ds1390_init(void)
208 return spi_register_driver(&ds1390_driver);
210 module_init(ds1390_init);
212 static __exit void ds1390_exit(void)
214 spi_unregister_driver(&ds1390_driver);
216 module_exit(ds1390_exit);
218 MODULE_DESCRIPTION("DS1390/93/94 SPI RTC driver");
219 MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
220 MODULE_LICENSE("GPL");