Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 1 | /* rtc-ds3234.c |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 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 | * |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/rtc.h> |
| 19 | #include <linux/spi/spi.h> |
| 20 | #include <linux/bcd.h> |
| 21 | |
| 22 | #define DS3234_REG_SECONDS 0x00 |
| 23 | #define DS3234_REG_MINUTES 0x01 |
| 24 | #define DS3234_REG_HOURS 0x02 |
| 25 | #define DS3234_REG_DAY 0x03 |
| 26 | #define DS3234_REG_DATE 0x04 |
| 27 | #define DS3234_REG_MONTH 0x05 |
| 28 | #define DS3234_REG_YEAR 0x06 |
| 29 | #define DS3234_REG_CENTURY (1 << 7) /* Bit 7 of the Month register */ |
| 30 | |
| 31 | #define DS3234_REG_CONTROL 0x0E |
| 32 | #define DS3234_REG_CONT_STAT 0x0F |
| 33 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 34 | static int ds3234_set_reg(struct device *dev, unsigned char address, |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 35 | unsigned char data) |
| 36 | { |
| 37 | struct spi_device *spi = to_spi_device(dev); |
| 38 | unsigned char buf[2]; |
| 39 | |
| 40 | /* MSB must be '1' to indicate write */ |
| 41 | buf[0] = address | 0x80; |
| 42 | buf[1] = data; |
| 43 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 44 | return spi_write_then_read(spi, buf, 2, NULL, 0); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int ds3234_get_reg(struct device *dev, unsigned char address, |
| 48 | unsigned char *data) |
| 49 | { |
| 50 | struct spi_device *spi = to_spi_device(dev); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 51 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 52 | *data = address & 0x7f; |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 53 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 54 | return spi_write_then_read(spi, data, 1, data, 1); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 57 | static int ds3234_read_time(struct device *dev, struct rtc_time *dt) |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 58 | { |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 59 | int err; |
| 60 | unsigned char buf[8]; |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 61 | struct spi_device *spi = to_spi_device(dev); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 62 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 63 | buf[0] = 0x00; /* Start address */ |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 64 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 65 | err = spi_write_then_read(spi, buf, 1, buf, 8); |
| 66 | if (err != 0) |
| 67 | return err; |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 68 | |
| 69 | /* Seconds, Minutes, Hours, Day, Date, Month, Year */ |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 70 | dt->tm_sec = bcd2bin(buf[0]); |
| 71 | dt->tm_min = bcd2bin(buf[1]); |
| 72 | dt->tm_hour = bcd2bin(buf[2] & 0x3f); |
| 73 | dt->tm_wday = bcd2bin(buf[3]) - 1; /* 0 = Sun */ |
| 74 | dt->tm_mday = bcd2bin(buf[4]); |
| 75 | dt->tm_mon = bcd2bin(buf[5] & 0x1f) - 1; /* 0 = Jan */ |
| 76 | dt->tm_year = bcd2bin(buf[6] & 0xff) + 100; /* Assume 20YY */ |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 77 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 78 | return rtc_valid_tm(dt); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 81 | static int ds3234_set_time(struct device *dev, struct rtc_time *dt) |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 82 | { |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 83 | ds3234_set_reg(dev, DS3234_REG_SECONDS, bin2bcd(dt->tm_sec)); |
| 84 | ds3234_set_reg(dev, DS3234_REG_MINUTES, bin2bcd(dt->tm_min)); |
| 85 | ds3234_set_reg(dev, DS3234_REG_HOURS, bin2bcd(dt->tm_hour) & 0x3f); |
| 86 | |
| 87 | /* 0 = Sun */ |
| 88 | ds3234_set_reg(dev, DS3234_REG_DAY, bin2bcd(dt->tm_wday + 1)); |
| 89 | ds3234_set_reg(dev, DS3234_REG_DATE, bin2bcd(dt->tm_mday)); |
| 90 | |
| 91 | /* 0 = Jan */ |
| 92 | ds3234_set_reg(dev, DS3234_REG_MONTH, bin2bcd(dt->tm_mon + 1)); |
| 93 | |
| 94 | /* Assume 20YY although we just want to make sure not to go negative. */ |
| 95 | if (dt->tm_year > 100) |
| 96 | dt->tm_year -= 100; |
| 97 | |
| 98 | ds3234_set_reg(dev, DS3234_REG_YEAR, bin2bcd(dt->tm_year)); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 103 | static const struct rtc_class_ops ds3234_rtc_ops = { |
| 104 | .read_time = ds3234_read_time, |
| 105 | .set_time = ds3234_set_time, |
| 106 | }; |
| 107 | |
Geert Uytterhoeven | bcc378e | 2008-10-29 14:01:13 -0700 | [diff] [blame] | 108 | static int __devinit ds3234_probe(struct spi_device *spi) |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 109 | { |
| 110 | struct rtc_device *rtc; |
| 111 | unsigned char tmp; |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 112 | int res; |
| 113 | |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 114 | spi->mode = SPI_MODE_3; |
| 115 | spi->bits_per_word = 8; |
| 116 | spi_setup(spi); |
| 117 | |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 118 | res = ds3234_get_reg(&spi->dev, DS3234_REG_SECONDS, &tmp); |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 119 | if (res != 0) |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 120 | return res; |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 121 | |
| 122 | /* Control settings |
| 123 | * |
| 124 | * CONTROL_REG |
| 125 | * BIT 7 6 5 4 3 2 1 0 |
| 126 | * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE |
| 127 | * |
| 128 | * 0 0 0 1 1 1 0 0 |
| 129 | * |
| 130 | * CONTROL_STAT_REG |
| 131 | * BIT 7 6 5 4 3 2 1 0 |
| 132 | * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F |
| 133 | * |
| 134 | * 1 0 0 0 1 0 0 0 |
| 135 | */ |
| 136 | ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp); |
| 137 | ds3234_set_reg(&spi->dev, DS3234_REG_CONTROL, tmp & 0x1c); |
| 138 | |
| 139 | ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp); |
| 140 | ds3234_set_reg(&spi->dev, DS3234_REG_CONT_STAT, tmp & 0x88); |
| 141 | |
| 142 | /* Print our settings */ |
| 143 | ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp); |
| 144 | dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp); |
| 145 | |
| 146 | ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp); |
| 147 | dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp); |
| 148 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 149 | rtc = rtc_device_register("ds3234", |
| 150 | &spi->dev, &ds3234_rtc_ops, THIS_MODULE); |
| 151 | if (IS_ERR(rtc)) |
| 152 | return PTR_ERR(rtc); |
| 153 | |
| 154 | dev_set_drvdata(&spi->dev, rtc); |
| 155 | |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Geert Uytterhoeven | bcc378e | 2008-10-29 14:01:13 -0700 | [diff] [blame] | 159 | static int __devexit ds3234_remove(struct spi_device *spi) |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 160 | { |
Geert Uytterhoeven | 42fea15 | 2011-03-17 14:47:56 +0100 | [diff] [blame] | 161 | struct rtc_device *rtc = spi_get_drvdata(spi); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 162 | |
Alessandro Zummo | f600915 | 2009-01-06 14:42:20 -0800 | [diff] [blame] | 163 | rtc_device_unregister(rtc); |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static struct spi_driver ds3234_driver = { |
| 168 | .driver = { |
| 169 | .name = "ds3234", |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 170 | .owner = THIS_MODULE, |
| 171 | }, |
| 172 | .probe = ds3234_probe, |
| 173 | .remove = __devexit_p(ds3234_remove), |
| 174 | }; |
| 175 | |
| 176 | static __init int ds3234_init(void) |
| 177 | { |
Dennis Aberilla | 2f9b75e | 2008-10-15 22:02:57 -0700 | [diff] [blame] | 178 | return spi_register_driver(&ds3234_driver); |
| 179 | } |
| 180 | module_init(ds3234_init); |
| 181 | |
| 182 | static __exit void ds3234_exit(void) |
| 183 | { |
| 184 | spi_unregister_driver(&ds3234_driver); |
| 185 | } |
| 186 | module_exit(ds3234_exit); |
| 187 | |
| 188 | MODULE_DESCRIPTION("DS3234 SPI RTC driver"); |
| 189 | MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>"); |
| 190 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 191 | MODULE_ALIAS("spi:ds3234"); |