Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-max6902.c
blob78b2551fb19def5e380aa73f70f424c6e500e91e
1 /* drivers/rtc/rtc-max6902.c
3 * Copyright (C) 2006 8D Technologies inc.
4 * Copyright (C) 2004 Compulab Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Driver for MAX6902 spi RTC
12 * Changelog:
14 * 24-May-2006: Raphael Assenat <raph@8d.com>
15 * - Major rework
16 * Converted to rtc_device and uses the SPI layer.
18 * ??-???-2005: Someone at Compulab
19 * - Initial driver creation.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/init.h>
26 #include <linux/rtc.h>
27 #include <linux/spi/spi.h>
28 #include <linux/bcd.h>
29 #include <linux/delay.h>
31 #define MAX6902_REG_SECONDS 0x01
32 #define MAX6902_REG_MINUTES 0x03
33 #define MAX6902_REG_HOURS 0x05
34 #define MAX6902_REG_DATE 0x07
35 #define MAX6902_REG_MONTH 0x09
36 #define MAX6902_REG_DAY 0x0B
37 #define MAX6902_REG_YEAR 0x0D
38 #define MAX6902_REG_CONTROL 0x0F
39 #define MAX6902_REG_CENTURY 0x13
41 #undef MAX6902_DEBUG
43 struct max6902 {
44 struct rtc_device *rtc;
45 u8 buf[9]; /* Burst read cmd + 8 registers */
46 u8 tx_buf[2];
47 u8 rx_buf[2];
50 static void max6902_set_reg(struct device *dev, unsigned char address,
51 unsigned char data)
53 struct spi_device *spi = to_spi_device(dev);
54 unsigned char buf[2];
56 /* MSB must be '0' to write */
57 buf[0] = address & 0x7f;
58 buf[1] = data;
60 spi_write(spi, buf, 2);
63 static int max6902_get_reg(struct device *dev, unsigned char address,
64 unsigned char *data)
66 struct spi_device *spi = to_spi_device(dev);
67 struct max6902 *chip = dev_get_drvdata(dev);
68 struct spi_message message;
69 struct spi_transfer xfer;
70 int status;
72 if (!data)
73 return -EINVAL;
75 /* Build our spi message */
76 spi_message_init(&message);
77 memset(&xfer, 0, sizeof(xfer));
78 xfer.len = 2;
79 /* Can tx_buf and rx_buf be equal? The doc in spi.h is not sure... */
80 xfer.tx_buf = chip->tx_buf;
81 xfer.rx_buf = chip->rx_buf;
83 /* Set MSB to indicate read */
84 chip->tx_buf[0] = address | 0x80;
86 spi_message_add_tail(&xfer, &message);
88 /* do the i/o */
89 status = spi_sync(spi, &message);
91 if (status == 0)
92 *data = chip->rx_buf[1];
93 return status;
96 static int max6902_get_datetime(struct device *dev, struct rtc_time *dt)
98 unsigned char tmp;
99 int century;
100 int err;
101 struct spi_device *spi = to_spi_device(dev);
102 struct max6902 *chip = dev_get_drvdata(dev);
103 struct spi_message message;
104 struct spi_transfer xfer;
105 int status;
107 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp);
108 if (err)
109 return err;
111 /* build the message */
112 spi_message_init(&message);
113 memset(&xfer, 0, sizeof(xfer));
114 xfer.len = 1 + 7; /* Burst read command + 7 registers */
115 xfer.tx_buf = chip->buf;
116 xfer.rx_buf = chip->buf;
117 chip->buf[0] = 0xbf; /* Burst read */
118 spi_message_add_tail(&xfer, &message);
120 /* do the i/o */
121 status = spi_sync(spi, &message);
122 if (status)
123 return status;
125 /* The chip sends data in this order:
126 * Seconds, Minutes, Hours, Date, Month, Day, Year */
127 dt->tm_sec = BCD2BIN(chip->buf[1]);
128 dt->tm_min = BCD2BIN(chip->buf[2]);
129 dt->tm_hour = BCD2BIN(chip->buf[3]);
130 dt->tm_mday = BCD2BIN(chip->buf[4]);
131 dt->tm_mon = BCD2BIN(chip->buf[5]) - 1;
132 dt->tm_wday = BCD2BIN(chip->buf[6]);
133 dt->tm_year = BCD2BIN(chip->buf[7]);
135 century = BCD2BIN(tmp) * 100;
137 dt->tm_year += century;
138 dt->tm_year -= 1900;
140 #ifdef MAX6902_DEBUG
141 printk("\n%s : Read RTC values\n",__func__);
142 printk("tm_hour: %i\n",dt->tm_hour);
143 printk("tm_min : %i\n",dt->tm_min);
144 printk("tm_sec : %i\n",dt->tm_sec);
145 printk("tm_year: %i\n",dt->tm_year);
146 printk("tm_mon : %i\n",dt->tm_mon);
147 printk("tm_mday: %i\n",dt->tm_mday);
148 printk("tm_wday: %i\n",dt->tm_wday);
149 #endif
151 return 0;
154 static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
156 dt->tm_year = dt->tm_year+1900;
158 #ifdef MAX6902_DEBUG
159 printk("\n%s : Setting RTC values\n",__func__);
160 printk("tm_sec : %i\n",dt->tm_sec);
161 printk("tm_min : %i\n",dt->tm_min);
162 printk("tm_hour: %i\n",dt->tm_hour);
163 printk("tm_mday: %i\n",dt->tm_mday);
164 printk("tm_wday: %i\n",dt->tm_wday);
165 printk("tm_year: %i\n",dt->tm_year);
166 #endif
168 /* Remove write protection */
169 max6902_set_reg(dev, 0xF, 0);
171 max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec));
172 max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min));
173 max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour));
175 max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday));
176 max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1));
177 max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday));
178 max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100));
179 max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100));
181 /* Compulab used a delay here. However, the datasheet
182 * does not mention a delay being required anywhere... */
183 /* delay(2000); */
185 /* Write protect */
186 max6902_set_reg(dev, 0xF, 0x80);
188 return 0;
191 static int max6902_read_time(struct device *dev, struct rtc_time *tm)
193 return max6902_get_datetime(dev, tm);
196 static int max6902_set_time(struct device *dev, struct rtc_time *tm)
198 return max6902_set_datetime(dev, tm);
201 static const struct rtc_class_ops max6902_rtc_ops = {
202 .read_time = max6902_read_time,
203 .set_time = max6902_set_time,
206 static int __devinit max6902_probe(struct spi_device *spi)
208 struct rtc_device *rtc;
209 unsigned char tmp;
210 struct max6902 *chip;
211 int res;
213 rtc = rtc_device_register("max6902",
214 &spi->dev, &max6902_rtc_ops, THIS_MODULE);
215 if (IS_ERR(rtc))
216 return PTR_ERR(rtc);
218 spi->mode = SPI_MODE_3;
219 spi->bits_per_word = 8;
220 spi_setup(spi);
222 chip = kzalloc(sizeof *chip, GFP_KERNEL);
223 if (!chip) {
224 rtc_device_unregister(rtc);
225 return -ENOMEM;
227 chip->rtc = rtc;
228 dev_set_drvdata(&spi->dev, chip);
230 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
231 if (res) {
232 rtc_device_unregister(rtc);
233 return res;
236 return 0;
239 static int __devexit max6902_remove(struct spi_device *spi)
241 struct max6902 *chip = platform_get_drvdata(spi);
242 struct rtc_device *rtc = chip->rtc;
244 if (rtc)
245 rtc_device_unregister(rtc);
247 kfree(chip);
249 return 0;
252 static struct spi_driver max6902_driver = {
253 .driver = {
254 .name = "rtc-max6902",
255 .bus = &spi_bus_type,
256 .owner = THIS_MODULE,
258 .probe = max6902_probe,
259 .remove = __devexit_p(max6902_remove),
262 static __init int max6902_init(void)
264 printk("max6902 spi driver\n");
265 return spi_register_driver(&max6902_driver);
267 module_init(max6902_init);
269 static __exit void max6902_exit(void)
271 spi_unregister_driver(&max6902_driver);
273 module_exit(max6902_exit);
275 MODULE_DESCRIPTION ("max6902 spi RTC driver");
276 MODULE_AUTHOR ("Raphael Assenat");
277 MODULE_LICENSE ("GPL");