Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtc: ds1302: rewrite using SPI

DS1302 is an half-duplex SPI device. The driver respects this fact now.
Pin configurations should be implemented using SPI subsystem.

Signed-off-by: Sergei Ianovich <ynvich@gmail.com>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>

authored by

Sergey Yanovich and committed by
Alexandre Belloni
d25a5ed3 2dcd0af5

+212 -197
+46
Documentation/devicetree/bindings/rtc/maxim-ds1302.txt
··· 1 + * Maxim/Dallas Semiconductor DS-1302 RTC 2 + 3 + Simple device which could be used to store date/time between reboots. 4 + 5 + The device uses the standard MicroWire half-duplex transfer timing. 6 + Master output is set on low clock and sensed by the RTC on the rising 7 + edge. Master input is set by the RTC on the trailing edge and is sensed 8 + by the master on low clock. 9 + 10 + Required properties: 11 + 12 + - compatible : Should be "maxim,ds1302" 13 + 14 + Required SPI properties: 15 + 16 + - reg : Should be address of the device chip select within 17 + the controller. 18 + 19 + - spi-max-frequency : DS-1302 has 500 kHz if powered at 2.2V, 20 + and 2MHz if powered at 5V. 21 + 22 + - spi-3wire : The device has a shared signal IN/OUT line. 23 + 24 + - spi-lsb-first : DS-1302 requires least significant bit first 25 + transfers. 26 + 27 + - spi-cs-high: DS-1302 has active high chip select line. This is 28 + required unless inverted in hardware. 29 + 30 + Example: 31 + 32 + spi@901c { 33 + #address-cells = <1>; 34 + #size-cells = <0>; 35 + compatible = "icpdas,lp8841-spi-rtc"; 36 + reg = <0x901c 0x1>; 37 + 38 + rtc@0 { 39 + compatible = "maxim,ds1302"; 40 + reg = <0>; 41 + spi-max-frequency = <500000>; 42 + spi-3wire; 43 + spi-lsb-first; 44 + spi-cs-high; 45 + }; 46 + };
+9 -6
drivers/rtc/Kconfig
··· 634 634 This driver can also be built as a module. If so, the module 635 635 will be called rtc-m41t94. 636 636 637 + config RTC_DRV_DS1302 638 + tristate "Dallas/Maxim DS1302" 639 + depends on SPI 640 + help 641 + If you say yes here you get support for the Dallas DS1302 RTC chips. 642 + 643 + This driver can also be built as a module. If so, the module 644 + will be called rtc-ds1302. 645 + 637 646 config RTC_DRV_DS1305 638 647 tristate "Dallas/Maxim DS1305/DS1306" 639 648 help ··· 842 833 depends on HAS_IOMEM 843 834 help 844 835 If you say yes here you get support for the Dallas DS1286 RTC chips. 845 - 846 - config RTC_DRV_DS1302 847 - tristate "Dallas DS1302" 848 - depends on SH_SECUREEDGE5410 849 - help 850 - If you say yes here you get support for the Dallas DS1302 RTC chips. 851 836 852 837 config RTC_DRV_DS1511 853 838 tristate "Dallas DS1511"
+157 -191
drivers/rtc/rtc-ds1302.c
··· 9 9 * this archive for more details. 10 10 */ 11 11 12 - #include <linux/init.h> 13 - #include <linux/module.h> 14 - #include <linux/kernel.h> 15 - #include <linux/platform_device.h> 16 - #include <linux/rtc.h> 17 - #include <linux/io.h> 18 12 #include <linux/bcd.h> 13 + #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> 19 20 20 21 #define DRV_NAME "rtc-ds1302" 21 - #define DRV_VERSION "0.1.1" 22 + #define DRV_VERSION "1.0.0" 22 23 23 24 #define RTC_CMD_READ 0x81 /* Read command */ 24 25 #define RTC_CMD_WRITE 0x80 /* Write command */ ··· 29 28 30 29 #define RTC_ADDR_RAM0 0x20 /* Address of RAM0 */ 31 30 #define RTC_ADDR_TCR 0x08 /* Address of trickle charge register */ 31 + #define RTC_CLCK_BURST 0x1F /* Address of clock burst */ 32 + #define RTC_CLCK_LEN 0x08 /* Size of clock burst */ 32 33 #define RTC_ADDR_CTRL 0x07 /* Address of control register */ 33 34 #define RTC_ADDR_YEAR 0x06 /* Address of year register */ 34 35 #define RTC_ADDR_DAY 0x05 /* Address of day of week register */ ··· 40 37 #define RTC_ADDR_MIN 0x01 /* Address of minute register */ 41 38 #define RTC_ADDR_SEC 0x00 /* Address of second register */ 42 39 43 - #ifdef CONFIG_SH_SECUREEDGE5410 44 - #include <asm/rtc.h> 45 - #include <mach/secureedge5410.h> 46 - 47 - #define RTC_RESET 0x1000 48 - #define RTC_IODATA 0x0800 49 - #define RTC_SCLK 0x0400 50 - 51 - #define set_dp(x) SECUREEDGE_WRITE_IOPORT(x, 0x1c00) 52 - #define get_dp() SECUREEDGE_READ_IOPORT() 53 - #define ds1302_set_tx() 54 - #define ds1302_set_rx() 55 - 56 - static inline int ds1302_hw_init(void) 40 + static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *time) 57 41 { 58 - return 0; 42 + struct spi_device *spi = dev_get_drvdata(dev); 43 + u8 buf[1 + RTC_CLCK_LEN]; 44 + u8 *bp = buf; 45 + int status; 46 + 47 + /* Enable writing */ 48 + bp = buf; 49 + *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE; 50 + *bp++ = RTC_CMD_WRITE_ENABLE; 51 + 52 + status = spi_write_then_read(spi, buf, 2, 53 + NULL, 0); 54 + if (!status) 55 + return status; 56 + 57 + /* Write registers starting at the first time/date address. */ 58 + bp = buf; 59 + *bp++ = RTC_CLCK_BURST << 1 | RTC_CMD_WRITE; 60 + 61 + *bp++ = bin2bcd(time->tm_sec); 62 + *bp++ = bin2bcd(time->tm_min); 63 + *bp++ = bin2bcd(time->tm_hour); 64 + *bp++ = bin2bcd(time->tm_mday); 65 + *bp++ = bin2bcd(time->tm_mon + 1); 66 + *bp++ = time->tm_wday; 67 + *bp++ = bin2bcd(time->tm_year % 100); 68 + *bp++ = RTC_CMD_WRITE_DISABLE; 69 + 70 + /* use write-then-read since dma from stack is nonportable */ 71 + return spi_write_then_read(spi, buf, sizeof(buf), 72 + NULL, 0); 59 73 } 60 74 61 - static inline void ds1302_reset(void) 75 + static int ds1302_rtc_get_time(struct device *dev, struct rtc_time *time) 62 76 { 63 - set_dp(get_dp() & ~(RTC_RESET | RTC_IODATA | RTC_SCLK)); 64 - } 77 + struct spi_device *spi = dev_get_drvdata(dev); 78 + u8 addr = RTC_CLCK_BURST << 1 | RTC_CMD_READ; 79 + u8 buf[RTC_CLCK_LEN - 1]; 80 + int status; 65 81 66 - static inline void ds1302_clock(void) 67 - { 68 - set_dp(get_dp() | RTC_SCLK); /* clock high */ 69 - set_dp(get_dp() & ~RTC_SCLK); /* clock low */ 70 - } 82 + /* Use write-then-read to get all the date/time registers 83 + * since dma from stack is nonportable 84 + */ 85 + status = spi_write_then_read(spi, &addr, sizeof(addr), 86 + buf, sizeof(buf)); 87 + if (status < 0) 88 + return status; 71 89 72 - static inline void ds1302_start(void) 73 - { 74 - set_dp(get_dp() | RTC_RESET); 75 - } 90 + /* Decode the registers */ 91 + time->tm_sec = bcd2bin(buf[RTC_ADDR_SEC]); 92 + time->tm_min = bcd2bin(buf[RTC_ADDR_MIN]); 93 + time->tm_hour = bcd2bin(buf[RTC_ADDR_HOUR]); 94 + time->tm_wday = buf[RTC_ADDR_DAY] - 1; 95 + time->tm_mday = bcd2bin(buf[RTC_ADDR_DATE]); 96 + time->tm_mon = bcd2bin(buf[RTC_ADDR_MON]) - 1; 97 + time->tm_year = bcd2bin(buf[RTC_ADDR_YEAR]) + 100; 76 98 77 - static inline void ds1302_stop(void) 78 - { 79 - set_dp(get_dp() & ~RTC_RESET); 80 - } 81 - 82 - static inline void ds1302_txbit(int bit) 83 - { 84 - set_dp((get_dp() & ~RTC_IODATA) | (bit ? RTC_IODATA : 0)); 85 - } 86 - 87 - static inline int ds1302_rxbit(void) 88 - { 89 - return !!(get_dp() & RTC_IODATA); 90 - } 91 - 92 - #else 93 - #error "Add support for your platform" 94 - #endif 95 - 96 - static void ds1302_sendbits(unsigned int val) 97 - { 98 - int i; 99 - 100 - ds1302_set_tx(); 101 - 102 - for (i = 8; (i); i--, val >>= 1) { 103 - ds1302_txbit(val & 0x1); 104 - ds1302_clock(); 105 - } 106 - } 107 - 108 - static unsigned int ds1302_recvbits(void) 109 - { 110 - unsigned int val; 111 - int i; 112 - 113 - ds1302_set_rx(); 114 - 115 - for (i = 0, val = 0; (i < 8); i++) { 116 - val |= (ds1302_rxbit() << i); 117 - ds1302_clock(); 118 - } 119 - 120 - return val; 121 - } 122 - 123 - static unsigned int ds1302_readbyte(unsigned int addr) 124 - { 125 - unsigned int val; 126 - 127 - ds1302_reset(); 128 - 129 - ds1302_start(); 130 - ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_READ); 131 - val = ds1302_recvbits(); 132 - ds1302_stop(); 133 - 134 - return val; 135 - } 136 - 137 - static void ds1302_writebyte(unsigned int addr, unsigned int val) 138 - { 139 - ds1302_reset(); 140 - 141 - ds1302_start(); 142 - ds1302_sendbits(((addr & 0x3f) << 1) | RTC_CMD_WRITE); 143 - ds1302_sendbits(val); 144 - ds1302_stop(); 145 - } 146 - 147 - static int ds1302_rtc_read_time(struct device *dev, struct rtc_time *tm) 148 - { 149 - tm->tm_sec = bcd2bin(ds1302_readbyte(RTC_ADDR_SEC)); 150 - tm->tm_min = bcd2bin(ds1302_readbyte(RTC_ADDR_MIN)); 151 - tm->tm_hour = bcd2bin(ds1302_readbyte(RTC_ADDR_HOUR)); 152 - tm->tm_wday = bcd2bin(ds1302_readbyte(RTC_ADDR_DAY)); 153 - tm->tm_mday = bcd2bin(ds1302_readbyte(RTC_ADDR_DATE)); 154 - tm->tm_mon = bcd2bin(ds1302_readbyte(RTC_ADDR_MON)) - 1; 155 - tm->tm_year = bcd2bin(ds1302_readbyte(RTC_ADDR_YEAR)); 156 - 157 - if (tm->tm_year < 70) 158 - tm->tm_year += 100; 159 - 160 - dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 161 - "mday=%d, mon=%d, year=%d, wday=%d\n", 162 - __func__, 163 - tm->tm_sec, tm->tm_min, tm->tm_hour, 164 - tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); 165 - 166 - return rtc_valid_tm(tm); 167 - } 168 - 169 - static int ds1302_rtc_set_time(struct device *dev, struct rtc_time *tm) 170 - { 171 - ds1302_writebyte(RTC_ADDR_CTRL, RTC_CMD_WRITE_ENABLE); 172 - /* Stop RTC */ 173 - ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) | 0x80); 174 - 175 - ds1302_writebyte(RTC_ADDR_SEC, bin2bcd(tm->tm_sec)); 176 - ds1302_writebyte(RTC_ADDR_MIN, bin2bcd(tm->tm_min)); 177 - ds1302_writebyte(RTC_ADDR_HOUR, bin2bcd(tm->tm_hour)); 178 - ds1302_writebyte(RTC_ADDR_DAY, bin2bcd(tm->tm_wday)); 179 - ds1302_writebyte(RTC_ADDR_DATE, bin2bcd(tm->tm_mday)); 180 - ds1302_writebyte(RTC_ADDR_MON, bin2bcd(tm->tm_mon + 1)); 181 - ds1302_writebyte(RTC_ADDR_YEAR, bin2bcd(tm->tm_year % 100)); 182 - 183 - /* Start RTC */ 184 - ds1302_writebyte(RTC_ADDR_SEC, ds1302_readbyte(RTC_ADDR_SEC) & ~0x80); 185 - 186 - ds1302_writebyte(RTC_ADDR_CTRL, RTC_CMD_WRITE_DISABLE); 187 - 188 - return 0; 189 - } 190 - 191 - static int ds1302_rtc_ioctl(struct device *dev, unsigned int cmd, 192 - unsigned long arg) 193 - { 194 - switch (cmd) { 195 - #ifdef RTC_SET_CHARGE 196 - case RTC_SET_CHARGE: 197 - { 198 - int tcs_val; 199 - 200 - if (copy_from_user(&tcs_val, (int __user *)arg, sizeof(int))) 201 - return -EFAULT; 202 - 203 - ds1302_writebyte(RTC_ADDR_TCR, (0xa0 | tcs_val * 0xf)); 204 - return 0; 205 - } 206 - #endif 207 - } 208 - 209 - return -ENOIOCTLCMD; 99 + /* Time may not be set */ 100 + return rtc_valid_tm(time); 210 101 } 211 102 212 103 static struct rtc_class_ops ds1302_rtc_ops = { 213 - .read_time = ds1302_rtc_read_time, 104 + .read_time = ds1302_rtc_get_time, 214 105 .set_time = ds1302_rtc_set_time, 215 - .ioctl = ds1302_rtc_ioctl, 216 106 }; 217 107 218 - static int __init ds1302_rtc_probe(struct platform_device *pdev) 108 + static int ds1302_probe(struct spi_device *spi) 219 109 { 220 - struct rtc_device *rtc; 110 + struct rtc_device *rtc; 111 + u8 addr; 112 + u8 buf[4]; 113 + u8 *bp = buf; 114 + int status; 221 115 222 - if (ds1302_hw_init()) { 223 - dev_err(&pdev->dev, "Failed to init communication channel"); 116 + /* Sanity check board setup data. This may be hooked up 117 + * in 3wire mode, but we don't care. Note that unless 118 + * there's an inverter in place, this needs SPI_CS_HIGH! 119 + */ 120 + if (spi->bits_per_word && (spi->bits_per_word != 8)) { 121 + dev_err(&spi->dev, "bad word length\n"); 122 + return -EINVAL; 123 + } else if (spi->max_speed_hz > 2000000) { 124 + dev_err(&spi->dev, "speed is too high\n"); 125 + return -EINVAL; 126 + } else if (spi->mode & SPI_CPHA) { 127 + dev_err(&spi->dev, "bad mode\n"); 224 128 return -EINVAL; 225 129 } 226 130 227 - /* Reset */ 228 - ds1302_reset(); 229 - 230 - /* Write a magic value to the DS1302 RAM, and see if it sticks. */ 231 - ds1302_writebyte(RTC_ADDR_RAM0, 0x42); 232 - if (ds1302_readbyte(RTC_ADDR_RAM0) != 0x42) { 233 - dev_err(&pdev->dev, "Failed to probe"); 234 - return -ENODEV; 131 + addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ; 132 + status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); 133 + if (status < 0) { 134 + dev_err(&spi->dev, "control register read error %d\n", 135 + status); 136 + return status; 235 137 } 236 138 237 - rtc = devm_rtc_device_register(&pdev->dev, "ds1302", 238 - &ds1302_rtc_ops, THIS_MODULE); 239 - if (IS_ERR(rtc)) 240 - return PTR_ERR(rtc); 139 + if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) { 140 + status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); 141 + if (status < 0) { 142 + dev_err(&spi->dev, "control register read error %d\n", 143 + status); 144 + return status; 145 + } 241 146 242 - platform_set_drvdata(pdev, rtc); 147 + if ((buf[0] & ~RTC_CMD_WRITE_DISABLE) != 0) { 148 + dev_err(&spi->dev, "junk in control register\n"); 149 + return -ENODEV; 150 + } 151 + } 152 + if (buf[0] == 0) { 153 + bp = buf; 154 + *bp++ = RTC_ADDR_CTRL << 1 | RTC_CMD_WRITE; 155 + *bp++ = RTC_CMD_WRITE_DISABLE; 156 + 157 + status = spi_write_then_read(spi, buf, 2, NULL, 0); 158 + if (status < 0) { 159 + dev_err(&spi->dev, "control register write error %d\n", 160 + status); 161 + return status; 162 + } 163 + 164 + addr = RTC_ADDR_CTRL << 1 | RTC_CMD_READ; 165 + status = spi_write_then_read(spi, &addr, sizeof(addr), buf, 1); 166 + if (status < 0) { 167 + dev_err(&spi->dev, 168 + "error %d reading control register\n", 169 + status); 170 + return status; 171 + } 172 + 173 + if (buf[0] != RTC_CMD_WRITE_DISABLE) { 174 + dev_err(&spi->dev, "failed to detect chip\n"); 175 + return -ENODEV; 176 + } 177 + } 178 + 179 + spi_set_drvdata(spi, spi); 180 + 181 + rtc = devm_rtc_device_register(&spi->dev, "ds1302", 182 + &ds1302_rtc_ops, THIS_MODULE); 183 + if (IS_ERR(rtc)) { 184 + status = PTR_ERR(rtc); 185 + dev_err(&spi->dev, "error %d registering rtc\n", status); 186 + return status; 187 + } 243 188 244 189 return 0; 245 190 } 246 191 247 - static struct platform_driver ds1302_platform_driver = { 248 - .driver = { 249 - .name = DRV_NAME, 250 - }, 192 + static int ds1302_remove(struct spi_device *spi) 193 + { 194 + spi_set_drvdata(spi, NULL); 195 + return 0; 196 + } 197 + 198 + #ifdef CONFIG_OF 199 + static const struct of_device_id ds1302_dt_ids[] = { 200 + { .compatible = "maxim,ds1302", }, 201 + { /* sentinel */ } 202 + }; 203 + MODULE_DEVICE_TABLE(of, ds1302_dt_ids); 204 + #endif 205 + 206 + static struct spi_driver ds1302_driver = { 207 + .driver.name = "rtc-ds1302", 208 + .driver.of_match_table = of_match_ptr(ds1302_dt_ids), 209 + .probe = ds1302_probe, 210 + .remove = ds1302_remove, 251 211 }; 252 212 253 - module_platform_driver_probe(ds1302_platform_driver, ds1302_rtc_probe); 213 + module_spi_driver(ds1302_driver); 254 214 255 215 MODULE_DESCRIPTION("Dallas DS1302 RTC driver"); 256 216 MODULE_VERSION(DRV_VERSION);