blob: 5cdc0f201b3423733f61e4198186b80c1ea43814 [file] [log] [blame]
Paul Mundt739d3402008-02-06 01:38:44 -08001/*
2 * Dallas DS1302 RTC Support
3 *
Alessandro Zummo2bfc3302009-08-20 12:31:49 +09004 * Copyright (C) 2002 David McCullough
5 * Copyright (C) 2003 - 2007 Paul Mundt
Paul Mundt739d3402008-02-06 01:38:44 -08006 *
7 * This file is subject to the terms and conditions of the GNU General Public
Alessandro Zummo2bfc3302009-08-20 12:31:49 +09008 * License version 2. See the file "COPYING" in the main directory of
Paul Mundt739d3402008-02-06 01:38:44 -08009 * this archive for more details.
10 */
Alessandro Zummo2bfc3302009-08-20 12:31:49 +090011
Paul Mundt739d3402008-02-06 01:38:44 -080012#include <linux/bcd.h>
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030013#include <linux/init.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/rtc.h>
19#include <linux/spi/spi.h>
Paul Mundt739d3402008-02-06 01:38:44 -080020
21#define DRV_NAME "rtc-ds1302"
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030022#define DRV_VERSION "1.0.0"
Paul Mundt739d3402008-02-06 01:38:44 -080023
24#define RTC_CMD_READ 0x81 /* Read command */
25#define RTC_CMD_WRITE 0x80 /* Write command */
26
Sergey Yanovichdfc657b2013-07-03 15:07:46 -070027#define RTC_CMD_WRITE_ENABLE 0x00 /* Write enable */
28#define RTC_CMD_WRITE_DISABLE 0x80 /* Write disable */
29
Paul Mundt739d3402008-02-06 01:38:44 -080030#define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */
31#define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030032#define RTC_CLCK_BURST 0x1F /* Address of clock burst */
33#define RTC_CLCK_LEN 0x08 /* Size of clock burst */
Sergey Yanovichdfc657b2013-07-03 15:07:46 -070034#define RTC_ADDR_CTRL 0x07 /* Address of control register */
Paul Mundt739d3402008-02-06 01:38:44 -080035#define RTC_ADDR_YEAR 0x06 /* Address of year register */
36#define RTC_ADDR_DAY 0x05 /* Address of day of week register */
37#define RTC_ADDR_MON 0x04 /* Address of month register */
38#define RTC_ADDR_DATE 0x03 /* Address of day of month register */
39#define RTC_ADDR_HOUR 0x02 /* Address of hour register */
40#define RTC_ADDR_MIN 0x01 /* Address of minute register */
41#define RTC_ADDR_SEC 0x00 /* Address of second register */
42
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030043static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *time)
Marc Zyngier72cc8e52010-05-24 14:33:47 -070044{
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030045 struct spi_device *spi = dev_get_drvdata(dev);
46 u8 buf[1 + RTC_CLCK_LEN];
47 u8 *bp = buf;
48 int status;
49
50 /* Enable writing */
51 bp = buf;
52 *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE;
53 *bp++ = RTC_CMD_WRITE_ENABLE;
54
55 status = spi_write_then_read(spi, buf, 2,
56 NULL, 0);
57 if (!status)
58 return status;
59
60 /* Write registers starting at the first time/date address. */
61 bp = buf;
62 *bp++ = RTC_CLCK_BURST << 1 | RTC_CMD_WRITE;
63
64 *bp++ = bin2bcd(time->tm_sec);
65 *bp++ = bin2bcd(time->tm_min);
66 *bp++ = bin2bcd(time->tm_hour);
67 *bp++ = bin2bcd(time->tm_mday);
68 *bp++ = bin2bcd(time->tm_mon + 1);
69 *bp++ = time->tm_wday;
70 *bp++ = bin2bcd(time->tm_year % 100);
71 *bp++ = RTC_CMD_WRITE_DISABLE;
72
73 /* use write-then-read since dma from stack is nonportable */
74 return spi_write_then_read(spi, buf, sizeof(buf),
75 NULL, 0);
Marc Zyngier72cc8e52010-05-24 14:33:47 -070076}
77
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030078static int ds1302_rtc_get_time(struct device *dev, struct rtc_time *time)
Marc Zyngier72cc8e52010-05-24 14:33:47 -070079{
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030080 struct spi_device *spi = dev_get_drvdata(dev);
81 u8 addr = RTC_CLCK_BURST << 1 | RTC_CMD_READ;
82 u8 buf[RTC_CLCK_LEN - 1];
83 int status;
Marc Zyngier72cc8e52010-05-24 14:33:47 -070084
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030085 /* Use write-then-read to get all the date/time registers
86 * since dma from stack is nonportable
87 */
88 status = spi_write_then_read(spi, &addr, sizeof(addr),
89 buf, sizeof(buf));
90 if (status < 0)
91 return status;
Marc Zyngier72cc8e52010-05-24 14:33:47 -070092
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +030093 /* Decode the registers */
94 time->tm_sec = bcd2bin(buf[RTC_ADDR_SEC]);
95 time->tm_min = bcd2bin(buf[RTC_ADDR_MIN]);
96 time->tm_hour = bcd2bin(buf[RTC_ADDR_HOUR]);
97 time->tm_wday = buf[RTC_ADDR_DAY] - 1;
98 time->tm_mday = bcd2bin(buf[RTC_ADDR_DATE]);
99 time->tm_mon = bcd2bin(buf[RTC_ADDR_MON]) - 1;
100 time->tm_year = bcd2bin(buf[RTC_ADDR_YEAR]) + 100;
Marc Zyngier72cc8e52010-05-24 14:33:47 -0700101
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300102 /* Time may not be set */
103 return rtc_valid_tm(time);
Paul Mundt739d3402008-02-06 01:38:44 -0800104}
105
106static struct rtc_class_ops ds1302_rtc_ops = {
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300107 .read_time = ds1302_rtc_get_time,
Paul Mundt739d3402008-02-06 01:38:44 -0800108 .set_time = ds1302_rtc_set_time,
Paul Mundt739d3402008-02-06 01:38:44 -0800109};
110
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300111static int ds1302_probe(struct spi_device *spi)
Paul Mundt739d3402008-02-06 01:38:44 -0800112{
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300113 struct rtc_device *rtc;
114 u8 addr;
115 u8 buf[4];
116 u8 *bp = buf;
117 int status;
Paul Mundt739d3402008-02-06 01:38:44 -0800118
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300119 /* Sanity check board setup data. This may be hooked up
120 * in 3wire mode, but we don't care. Note that unless
121 * there's an inverter in place, this needs SPI_CS_HIGH!
122 */
123 if (spi->bits_per_word && (spi->bits_per_word != 8)) {
124 dev_err(&spi->dev, "bad word length\n");
125 return -EINVAL;
126 } else if (spi->max_speed_hz > 2000000) {
127 dev_err(&spi->dev, "speed is too high\n");
128 return -EINVAL;
129 } else if (spi->mode & SPI_CPHA) {
130 dev_err(&spi->dev, "bad mode\n");
Marc Zyngier72cc8e52010-05-24 14:33:47 -0700131 return -EINVAL;
132 }
133
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300134 addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ;
135 status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1);
136 if (status < 0) {
137 dev_err(&spi->dev, "control register read error %d\n",
138 status);
139 return status;
Marc Zyngier72cc8e52010-05-24 14:33:47 -0700140 }
Paul Mundt739d3402008-02-06 01:38:44 -0800141
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300142 if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) {
143 status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1);
144 if (status < 0) {
145 dev_err(&spi->dev, "control register read error %d\n",
146 status);
147 return status;
148 }
Paul Mundt739d3402008-02-06 01:38:44 -0800149
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300150 if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) {
151 dev_err(&spi->dev, "junk in control register\n");
152 return -ENODEV;
153 }
154 }
155 if (buf[0] == 0) {
156 bp = buf;
157 *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE;
158 *bp++ = RTC_CMD_WRITE_DISABLE;
159
160 status = spi_write_then_read(spi, buf, 2, NULL, 0);
161 if (status < 0) {
162 dev_err(&spi->dev, "control register write error %d\n",
163 status);
164 return status;
165 }
166
167 addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ;
168 status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1);
169 if (status < 0) {
170 dev_err(&spi->dev,
171 "error %d reading control register\n",
172 status);
173 return status;
174 }
175
176 if (buf[0] != RTC_CMD_WRITE_DISABLE) {
177 dev_err(&spi->dev, "failed to detect chip\n");
178 return -ENODEV;
179 }
180 }
181
182 spi_set_drvdata(spi, spi);
183
184 rtc = devm_rtc_device_register(&spi->dev, "ds1302",
185 &ds1302_rtc_ops, THIS_MODULE);
186 if (IS_ERR(rtc)) {
187 status = PTR_ERR(rtc);
188 dev_err(&spi->dev, "error %d registering rtc\n", status);
189 return status;
190 }
Paul Mundt739d3402008-02-06 01:38:44 -0800191
192 return 0;
Paul Mundt739d3402008-02-06 01:38:44 -0800193}
194
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300195static int ds1302_remove(struct spi_device *spi)
196{
197 spi_set_drvdata(spi, NULL);
198 return 0;
199}
200
201#ifdef CONFIG_OF
202static const struct of_device_id ds1302_dt_ids[] = {
203 { .compatible = "maxim,ds1302", },
204 { /* sentinel */ }
205};
206MODULE_DEVICE_TABLE(of, ds1302_dt_ids);
207#endif
208
209static struct spi_driver ds1302_driver = {
210 .driver.name = "rtc-ds1302",
211 .driver.of_match_table = of_match_ptr(ds1302_dt_ids),
212 .probe = ds1302_probe,
213 .remove = ds1302_remove,
Paul Mundt739d3402008-02-06 01:38:44 -0800214};
215
Sergey Yanovichd25a5ed2016-02-23 13:54:57 +0300216module_spi_driver(ds1302_driver);
Paul Mundt739d3402008-02-06 01:38:44 -0800217
218MODULE_DESCRIPTION("Dallas DS1302 RTC driver");
219MODULE_VERSION(DRV_VERSION);
220MODULE_AUTHOR("Paul Mundt, David McCullough");
221MODULE_LICENSE("GPL v2");