diff options
| author | Dennis Aberilla <denzzzhome@yahoo.com> | 2008-10-16 01:02:57 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-10-16 14:21:39 -0400 |
| commit | 2f9b75e09ec3f62f2ebecec0ac9aec58656c2459 (patch) | |
| tree | 889da2365d6f76f817b24beb40a937ae248822eb | |
| parent | 986e36a5b817de01733d46aa623406106d661cec (diff) | |
rtc: add device driver for Dallas DS3234 SPI RTC chip
Add support for the Dallas DS3234 chip - extremely accurate SPI bus RTC
with integrated crystal and SRAM.
[akpm@linux-foundation.org: don't use BIN2BCD/BCD2BIN]
Signed-off-by: Dennis Aberilla <denzzzhome@yahoo.com>
Signed-off-by: Alessandro Zummo <a.zummo@towertech.it>
Cc: David Brownell <david-b@pacbell.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
| -rw-r--r-- | drivers/rtc/Kconfig | 9 | ||||
| -rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
| -rw-r--r-- | drivers/rtc/rtc-ds3234.c | 290 |
3 files changed, 300 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index f3d7fd3406a6..16bc33cbadf5 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
| @@ -319,6 +319,15 @@ config RTC_DRV_RS5C348 | |||
| 319 | This driver can also be built as a module. If so, the module | 319 | This driver can also be built as a module. If so, the module |
| 320 | will be called rtc-rs5c348. | 320 | will be called rtc-rs5c348. |
| 321 | 321 | ||
| 322 | config RTC_DRV_DS3234 | ||
| 323 | tristate "Maxim/Dallas DS3234" | ||
| 324 | help | ||
| 325 | If you say yes here you get support for the | ||
| 326 | Maxim/Dallas DS3234 SPI RTC chip. | ||
| 327 | |||
| 328 | This driver can also be built as a module. If so, the module | ||
| 329 | will be called rtc-ds3234. | ||
| 330 | |||
| 322 | endif # SPI_MASTER | 331 | endif # SPI_MASTER |
| 323 | 332 | ||
| 324 | comment "Platform RTC drivers" | 333 | comment "Platform RTC drivers" |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 37a71b727262..d05928b3ca94 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
| @@ -32,6 +32,7 @@ obj-$(CONFIG_RTC_DRV_DS1511) += rtc-ds1511.o | |||
| 32 | obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o | 32 | obj-$(CONFIG_RTC_DRV_DS1553) += rtc-ds1553.o |
| 33 | obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o | 33 | obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o |
| 34 | obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o | 34 | obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o |
| 35 | obj-$(CONFIG_RTC_DRV_DS3234) += rtc-ds3234.o | ||
| 35 | obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o | 36 | obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o |
| 36 | obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o | 37 | obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o |
| 37 | obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o | 38 | obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o |
diff --git a/drivers/rtc/rtc-ds3234.c b/drivers/rtc/rtc-ds3234.c new file mode 100644 index 000000000000..37d131d03f33 --- /dev/null +++ b/drivers/rtc/rtc-ds3234.c | |||
| @@ -0,0 +1,290 @@ | |||
| 1 | /* drivers/rtc/rtc-ds3234.c | ||
| 2 | * | ||
| 3 | * Driver for Dallas Semiconductor (DS3234) SPI RTC with Integrated Crystal | ||
| 4 | * and SRAM. | ||
| 5 | * | ||
| 6 | * Copyright (C) 2008 MIMOMax Wireless Ltd. | ||
| 7 | * | ||
| 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. | ||
| 11 | * | ||
| 12 | * Changelog: | ||
| 13 | * | ||
| 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. | ||
| 17 | */ | ||
| 18 | |||
| 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> | ||
| 24 | |||
| 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 */ | ||
| 33 | |||
| 34 | #define DS3234_REG_CONTROL 0x0E | ||
| 35 | #define DS3234_REG_CONT_STAT 0x0F | ||
| 36 | |||
| 37 | #undef DS3234_DEBUG | ||
| 38 | |||
| 39 | struct ds3234 { | ||
| 40 | struct rtc_device *rtc; | ||
| 41 | u8 buf[8]; /* Burst read: addr + 7 regs */ | ||
| 42 | u8 tx_buf[2]; | ||
| 43 | u8 rx_buf[2]; | ||
| 44 | }; | ||
| 45 | |||
| 46 | static void ds3234_set_reg(struct device *dev, unsigned char address, | ||
| 47 | unsigned char data) | ||
| 48 | { | ||
| 49 | struct spi_device *spi = to_spi_device(dev); | ||
| 50 | unsigned char buf[2]; | ||
| 51 | |||
| 52 | /* MSB must be '1' to indicate write */ | ||
| 53 | buf[0] = address | 0x80; | ||
| 54 | buf[1] = data; | ||
| 55 | |||
| 56 | spi_write(spi, buf, 2); | ||
| 57 | } | ||
| 58 | |||
| 59 | static int ds3234_get_reg(struct device *dev, unsigned char address, | ||
| 60 | unsigned char *data) | ||
| 61 | { | ||
| 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; | ||
| 66 | int status; | ||
| 67 | |||
| 68 | if (!data) | ||
| 69 | return -EINVAL; | ||
| 70 | |||
| 71 | /* Build our spi message */ | ||
| 72 | spi_message_init(&message); | ||
| 73 | memset(&xfer, 0, sizeof(xfer)); | ||
| 74 | |||
| 75 | /* Address + dummy tx byte */ | ||
| 76 | xfer.len = 2; | ||
| 77 | xfer.tx_buf = chip->tx_buf; | ||
| 78 | xfer.rx_buf = chip->rx_buf; | ||
| 79 | |||
| 80 | chip->tx_buf[0] = address; | ||
| 81 | chip->tx_buf[1] = 0xff; | ||
| 82 | |||
| 83 | spi_message_add_tail(&xfer, &message); | ||
| 84 | |||
| 85 | /* do the i/o */ | ||
| 86 | status = spi_sync(spi, &message); | ||
| 87 | if (status == 0) | ||
| 88 | status = message.status; | ||
| 89 | else | ||
| 90 | return status; | ||
| 91 | |||
| 92 | *data = chip->rx_buf[1]; | ||
| 93 | |||
| 94 | return status; | ||
| 95 | } | ||
| 96 | |||
| 97 | static int ds3234_get_datetime(struct device *dev, struct rtc_time *dt) | ||
| 98 | { | ||
| 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; | ||
| 103 | int status; | ||
| 104 | |||
| 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); | ||
| 113 | |||
| 114 | /* do the i/o */ | ||
| 115 | status = spi_sync(spi, &message); | ||
| 116 | if (status == 0) | ||
| 117 | status = message.status; | ||
| 118 | else | ||
| 119 | return status; | ||
| 120 | |||
| 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 */ | ||
| 129 | |||
| 130 | #ifdef DS3234_DEBUG | ||
| 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); | ||
| 139 | #endif | ||
| 140 | |||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | |||
| 144 | static int ds3234_set_datetime(struct device *dev, struct rtc_time *dt) | ||
| 145 | { | ||
| 146 | #ifdef DS3234_DEBUG | ||
| 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); | ||
| 155 | #endif | ||
| 156 | |||
| 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); | ||
| 160 | |||
| 161 | /* 0 = Sun */ | ||
