Blame view
drivers/rtc/rtc-ds1302.c
5.65 KB
c51669eaf treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
739d340db rtc: ds1302 rtc s... |
2 3 4 |
/* * Dallas DS1302 RTC Support * |
2bfc3305f rtc: rtc-ds1302 f... |
5 6 |
* Copyright (C) 2002 David McCullough * Copyright (C) 2003 - 2007 Paul Mundt |
739d340db rtc: ds1302 rtc s... |
7 |
*/ |
2bfc3305f rtc: rtc-ds1302 f... |
8 |
|
d25a5ed37 rtc: ds1302: rewr... |
9 |
#include <linux/bcd.h> |
739d340db rtc: ds1302 rtc s... |
10 |
#include <linux/init.h> |
d25a5ed37 rtc: ds1302: rewr... |
11 |
#include <linux/io.h> |
739d340db rtc: ds1302 rtc s... |
12 |
#include <linux/kernel.h> |
d25a5ed37 rtc: ds1302: rewr... |
13 14 |
#include <linux/module.h> #include <linux/of.h> |
739d340db rtc: ds1302 rtc s... |
15 |
#include <linux/rtc.h> |
d25a5ed37 rtc: ds1302: rewr... |
16 |
#include <linux/spi/spi.h> |
739d340db rtc: ds1302 rtc s... |
17 |
|
739d340db rtc: ds1302 rtc s... |
18 19 |
#define RTC_CMD_READ 0x81 /* Read command */ #define RTC_CMD_WRITE 0x80 /* Write command */ |
dfc657b13 drivers/rtc/rtc-d... |
20 21 |
#define RTC_CMD_WRITE_ENABLE 0x00 /* Write enable */ #define RTC_CMD_WRITE_DISABLE 0x80 /* Write disable */ |
739d340db rtc: ds1302 rtc s... |
22 23 |
#define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */ #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */ |
d25a5ed37 rtc: ds1302: rewr... |
24 25 |
#define RTC_CLCK_BURST 0x1F /* Address of clock burst */ #define RTC_CLCK_LEN 0x08 /* Size of clock burst */ |
dfc657b13 drivers/rtc/rtc-d... |
26 |
#define RTC_ADDR_CTRL 0x07 /* Address of control register */ |
739d340db rtc: ds1302 rtc s... |
27 28 29 30 31 32 33 |
#define RTC_ADDR_YEAR 0x06 /* Address of year register */ #define RTC_ADDR_DAY 0x05 /* Address of day of week register */ #define RTC_ADDR_MON 0x04 /* Address of month register */ #define RTC_ADDR_DATE 0x03 /* Address of day of month register */ #define RTC_ADDR_HOUR 0x02 /* Address of hour register */ #define RTC_ADDR_MIN 0x01 /* Address of minute register */ #define RTC_ADDR_SEC 0x00 /* Address of second register */ |
d25a5ed37 rtc: ds1302: rewr... |
34 |
static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *time) |
72cc8e51c rtc-ds1302: add s... |
35 |
{ |
d25a5ed37 rtc: ds1302: rewr... |
36 37 |
struct spi_device *spi = dev_get_drvdata(dev); u8 buf[1 + RTC_CLCK_LEN]; |
5134d2fd9 rtc: ds1302: remo... |
38 |
u8 *bp; |
d25a5ed37 rtc: ds1302: rewr... |
39 40 41 42 43 44 45 46 47 |
int status; /* Enable writing */ bp = buf; *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE; *bp++ = RTC_CMD_WRITE_ENABLE; status = spi_write_then_read(spi, buf, 2, NULL, 0); |
bc83a141b rtc: ds1302: fix ... |
48 |
if (status) |
d25a5ed37 rtc: ds1302: rewr... |
49 50 51 52 53 54 55 56 57 58 59 |
return status; /* Write registers starting at the first time/date address. */ bp = buf; *bp++ = RTC_CLCK_BURST << 1 | RTC_CMD_WRITE; *bp++ = bin2bcd(time->tm_sec); *bp++ = bin2bcd(time->tm_min); *bp++ = bin2bcd(time->tm_hour); *bp++ = bin2bcd(time->tm_mday); *bp++ = bin2bcd(time->tm_mon + 1); |
ef50f86e1 rtc: ds1302: fix ... |
60 |
*bp++ = time->tm_wday + 1; |
d25a5ed37 rtc: ds1302: rewr... |
61 62 63 64 65 66 |
*bp++ = bin2bcd(time->tm_year % 100); *bp++ = RTC_CMD_WRITE_DISABLE; /* use write-then-read since dma from stack is nonportable */ return spi_write_then_read(spi, buf, sizeof(buf), NULL, 0); |
72cc8e51c rtc-ds1302: add s... |
67 |
} |
d25a5ed37 rtc: ds1302: rewr... |
68 |
static int ds1302_rtc_get_time(struct device *dev, struct rtc_time *time) |
72cc8e51c rtc-ds1302: add s... |
69 |
{ |
d25a5ed37 rtc: ds1302: rewr... |
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
struct spi_device *spi = dev_get_drvdata(dev); u8 addr = RTC_CLCK_BURST << 1 | RTC_CMD_READ; u8 buf[RTC_CLCK_LEN - 1]; int status; /* Use write-then-read to get all the date/time registers * since dma from stack is nonportable */ status = spi_write_then_read(spi, &addr, sizeof(addr), buf, sizeof(buf)); if (status < 0) return status; /* Decode the registers */ time->tm_sec = bcd2bin(buf[RTC_ADDR_SEC]); time->tm_min = bcd2bin(buf[RTC_ADDR_MIN]); time->tm_hour = bcd2bin(buf[RTC_ADDR_HOUR]); time->tm_wday = buf[RTC_ADDR_DAY] - 1; time->tm_mday = bcd2bin(buf[RTC_ADDR_DATE]); time->tm_mon = bcd2bin(buf[RTC_ADDR_MON]) - 1; time->tm_year = bcd2bin(buf[RTC_ADDR_YEAR]) + 100; |
22652ba72 rtc: stop validat... |
91 |
return 0; |
72cc8e51c rtc-ds1302: add s... |
92 |
} |
34c7b3ac4 rtc: constify rtc... |
93 |
static const struct rtc_class_ops ds1302_rtc_ops = { |
d25a5ed37 rtc: ds1302: rewr... |
94 95 96 |
.read_time = ds1302_rtc_get_time, .set_time = ds1302_rtc_set_time, }; |
739d340db rtc: ds1302 rtc s... |
97 |
|
d25a5ed37 rtc: ds1302: rewr... |
98 |
static int ds1302_probe(struct spi_device *spi) |
739d340db rtc: ds1302 rtc s... |
99 |
{ |
d25a5ed37 rtc: ds1302: rewr... |
100 101 102 |
struct rtc_device *rtc; u8 addr; u8 buf[4]; |
5134d2fd9 rtc: ds1302: remo... |
103 |
u8 *bp; |
d25a5ed37 rtc: ds1302: rewr... |
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
int status; /* Sanity check board setup data. This may be hooked up * in 3wire mode, but we don't care. Note that unless * there's an inverter in place, this needs SPI_CS_HIGH! */ if (spi->bits_per_word && (spi->bits_per_word != 8)) { dev_err(&spi->dev, "bad word length "); return -EINVAL; } else if (spi->max_speed_hz > 2000000) { dev_err(&spi->dev, "speed is too high "); return -EINVAL; } else if (spi->mode & SPI_CPHA) { dev_err(&spi->dev, "bad mode "); return -EINVAL; |
739d340db rtc: ds1302 rtc s... |
122 |
} |
72cc8e51c rtc-ds1302: add s... |
123 |
|
d25a5ed37 rtc: ds1302: rewr... |
124 125 126 127 128 129 130 |
addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ; status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); if (status < 0) { dev_err(&spi->dev, "control register read error %d ", status); return status; |
739d340db rtc: ds1302 rtc s... |
131 |
} |
d25a5ed37 rtc: ds1302: rewr... |
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) { status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); if (status < 0) { dev_err(&spi->dev, "control register read error %d ", status); return status; } if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) { dev_err(&spi->dev, "junk in control register "); return -ENODEV; } } if (buf[0] == 0) { bp = buf; *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE; *bp++ = RTC_CMD_WRITE_DISABLE; status = spi_write_then_read(spi, buf, 2, NULL, 0); if (status < 0) { dev_err(&spi->dev, "control register write error %d ", status); return status; } addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ; status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); if (status < 0) { dev_err(&spi->dev, "error %d reading control register ", status); return status; } if (buf[0] != RTC_CMD_WRITE_DISABLE) { dev_err(&spi->dev, "failed to detect chip "); return -ENODEV; } } |
739d340db rtc: ds1302 rtc s... |
176 |
|
d25a5ed37 rtc: ds1302: rewr... |
177 |
spi_set_drvdata(spi, spi); |
739d340db rtc: ds1302 rtc s... |
178 |
|
d25a5ed37 rtc: ds1302: rewr... |
179 180 181 182 183 184 185 186 |
rtc = devm_rtc_device_register(&spi->dev, "ds1302", &ds1302_rtc_ops, THIS_MODULE); if (IS_ERR(rtc)) { status = PTR_ERR(rtc); dev_err(&spi->dev, "error %d registering rtc ", status); return status; } |
dfc657b13 drivers/rtc/rtc-d... |
187 |
|
739d340db rtc: ds1302 rtc s... |
188 189 |
return 0; } |
d25a5ed37 rtc: ds1302: rewr... |
190 |
static int ds1302_remove(struct spi_device *spi) |
739d340db rtc: ds1302 rtc s... |
191 |
{ |
d25a5ed37 rtc: ds1302: rewr... |
192 193 |
spi_set_drvdata(spi, NULL); return 0; |
739d340db rtc: ds1302 rtc s... |
194 |
} |
d25a5ed37 rtc: ds1302: rewr... |
195 196 197 198 |
#ifdef CONFIG_OF static const struct of_device_id ds1302_dt_ids[] = { { .compatible = "maxim,ds1302", }, { /* sentinel */ } |
739d340db rtc: ds1302 rtc s... |
199 |
}; |
d25a5ed37 rtc: ds1302: rewr... |
200 201 |
MODULE_DEVICE_TABLE(of, ds1302_dt_ids); #endif |
739d340db rtc: ds1302 rtc s... |
202 |
|
d25a5ed37 rtc: ds1302: rewr... |
203 204 205 206 207 |
static struct spi_driver ds1302_driver = { .driver.name = "rtc-ds1302", .driver.of_match_table = of_match_ptr(ds1302_dt_ids), .probe = ds1302_probe, .remove = ds1302_remove, |
739d340db rtc: ds1302 rtc s... |
208 |
}; |
d25a5ed37 rtc: ds1302: rewr... |
209 |
module_spi_driver(ds1302_driver); |
739d340db rtc: ds1302 rtc s... |
210 211 |
MODULE_DESCRIPTION("Dallas DS1302 RTC driver"); |
739d340db rtc: ds1302 rtc s... |
212 213 |
MODULE_AUTHOR("Paul Mundt, David McCullough"); MODULE_LICENSE("GPL v2"); |