]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/rtc/rtc-max6902.c
ide: sanitize ide_unregister() usage
[linux-2.6.git] / drivers / rtc / rtc-max6902.c
1 /* drivers/rtc/rtc-max6902.c
2  *
3  * Copyright (C) 2006 8D Technologies inc.
4  * Copyright (C) 2004 Compulab Ltd.
5  *
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.
9  *
10  * Driver for MAX6902 spi RTC
11  *
12  * Changelog:
13  *
14  * 24-May-2006: Raphael Assenat <raph@8d.com>
15  *                - Major rework
16  *                              Converted to rtc_device and uses the SPI layer.
17  *
18  * ??-???-2005: Someone at Compulab
19  *                - Initial driver creation.
20  */
21
22 #include <linux/module.h>
23 #include <linux/version.h>
24
25 #include <linux/kernel.h>
26 #include <linux/platform_device.h>
27 #include <linux/init.h>
28 #include <linux/rtc.h>
29 #include <linux/spi/spi.h>
30 #include <linux/bcd.h>
31 #include <linux/delay.h>
32
33 #define MAX6902_REG_SECONDS             0x01
34 #define MAX6902_REG_MINUTES             0x03
35 #define MAX6902_REG_HOURS               0x05
36 #define MAX6902_REG_DATE                0x07
37 #define MAX6902_REG_MONTH               0x09
38 #define MAX6902_REG_DAY                 0x0B
39 #define MAX6902_REG_YEAR                0x0D
40 #define MAX6902_REG_CONTROL             0x0F
41 #define MAX6902_REG_CENTURY             0x13
42
43 #undef MAX6902_DEBUG
44
45 struct max6902 {
46         struct rtc_device *rtc;
47         u8 buf[9]; /* Burst read cmd + 8 registers */
48         u8 tx_buf[2];
49         u8 rx_buf[2];
50 };
51
52 static void max6902_set_reg(struct device *dev, unsigned char address,
53                                 unsigned char data)
54 {
55         struct spi_device *spi = to_spi_device(dev);
56         unsigned char buf[2];
57
58         /* MSB must be '0' to write */
59         buf[0] = address & 0x7f;
60         buf[1] = data;
61
62         spi_write(spi, buf, 2);
63 }
64
65 static int max6902_get_reg(struct device *dev, unsigned char address,
66                                 unsigned char *data)
67 {
68         struct spi_device *spi = to_spi_device(dev);
69         struct max6902 *chip = dev_get_drvdata(dev);
70         struct spi_message message;
71         struct spi_transfer xfer;
72         int status;
73
74         if (!data)
75                 return -EINVAL;
76
77         /* Build our spi message */
78         spi_message_init(&message);
79         memset(&xfer, 0, sizeof(xfer));
80         xfer.len = 2;
81         /* Can tx_buf and rx_buf be equal? The doc in spi.h is not sure... */
82         xfer.tx_buf = chip->tx_buf;
83         xfer.rx_buf = chip->rx_buf;
84
85         /* Set MSB to indicate read */
86         chip->tx_buf[0] = address | 0x80;
87
88         spi_message_add_tail(&xfer, &message);
89
90         /* do the i/o */
91         status = spi_sync(spi, &message);
92
93         if (status == 0)
94                 *data = chip->rx_buf[1];
95         return status;
96 }
97
98 static int max6902_get_datetime(struct device *dev, struct rtc_time *dt)
99 {
100         unsigned char tmp;
101         int century;
102         int err;
103         struct spi_device *spi = to_spi_device(dev);
104         struct max6902 *chip = dev_get_drvdata(dev);
105         struct spi_message message;
106         struct spi_transfer xfer;
107         int status;
108
109         err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp);
110         if (err)
111                 return err;
112
113         /* build the message */
114         spi_message_init(&message);
115         memset(&xfer, 0, sizeof(xfer));
116         xfer.len = 1 + 7;       /* Burst read command + 7 registers */
117         xfer.tx_buf = chip->buf;
118         xfer.rx_buf = chip->buf;
119         chip->buf[0] = 0xbf;    /* Burst read */
120         spi_message_add_tail(&xfer, &message);
121
122         /* do the i/o */
123         status = spi_sync(spi, &message);
124         if (status)
125                 return status;
126
127         /* The chip sends data in this order:
128          * Seconds, Minutes, Hours, Date, Month, Day, Year */
129         dt->tm_sec      = BCD2BIN(chip->buf[1]);
130         dt->tm_min      = BCD2BIN(chip->buf[2]);
131         dt->tm_hour     = BCD2BIN(chip->buf[3]);
132         dt->tm_mday     = BCD2BIN(chip->buf[4]);
133         dt->tm_mon      = BCD2BIN(chip->buf[5]) - 1;
134         dt->tm_wday     = BCD2BIN(chip->buf[6]);
135         dt->tm_year = BCD2BIN(chip->buf[7]);
136
137         century = BCD2BIN(tmp) * 100;
138
139         dt->tm_year += century;
140         dt->tm_year -= 1900;
141
142 #ifdef MAX6902_DEBUG
143         printk("\n%s : Read RTC values\n",__FUNCTION__);
144         printk("tm_hour: %i\n",dt->tm_hour);
145         printk("tm_min : %i\n",dt->tm_min);
146         printk("tm_sec : %i\n",dt->tm_sec);
147         printk("tm_year: %i\n",dt->tm_year);
148         printk("tm_mon : %i\n",dt->tm_mon);
149         printk("tm_mday: %i\n",dt->tm_mday);
150         printk("tm_wday: %i\n",dt->tm_wday);
151 #endif
152
153         return 0;
154 }
155
156 static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
157 {
158         dt->tm_year = dt->tm_year+1900;
159
160 #ifdef MAX6902_DEBUG
161         printk("\n%s : Setting RTC values\n",__FUNCTION__);
162         printk("tm_sec : %i\n",dt->tm_sec);
163         printk("tm_min : %i\n",dt->tm_min);
164         printk("tm_hour: %i\n",dt->tm_hour);
165         printk("tm_mday: %i\n",dt->tm_mday);
166         printk("tm_wday: %i\n",dt->tm_wday);
167         printk("tm_year: %i\n",dt->tm_year);
168 #endif
169
170         /* Remove write protection */
171         max6902_set_reg(dev, 0xF, 0);
172
173         max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec));
174         max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min));
175         max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour));
176
177         max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday));
178         max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1));
179         max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday));
180         max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100));
181         max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100));
182
183         /* Compulab used a delay here. However, the datasheet
184          * does not mention a delay being required anywhere... */
185         /* delay(2000); */
186
187         /* Write protect */
188         max6902_set_reg(dev, 0xF, 0x80);
189
190         return 0;
191 }
192
193 static int max6902_read_time(struct device *dev, struct rtc_time *tm)
194 {
195         return max6902_get_datetime(dev, tm);
196 }
197
198 static int max6902_set_time(struct device *dev, struct rtc_time *tm)
199 {
200         return max6902_set_datetime(dev, tm);
201 }
202
203 static const struct rtc_class_ops max6902_rtc_ops = {
204         .read_time      = max6902_read_time,
205         .set_time       = max6902_set_time,
206 };
207
208 static int __devinit max6902_probe(struct spi_device *spi)
209 {
210         struct rtc_device *rtc;
211         unsigned char tmp;
212         struct max6902 *chip;
213         int res;
214
215         rtc = rtc_device_register("max6902",
216                                 &spi->dev, &max6902_rtc_ops, THIS_MODULE);
217         if (IS_ERR(rtc))
218                 return PTR_ERR(rtc);
219
220         spi->mode = SPI_MODE_3;
221         spi->bits_per_word = 8;
222         spi_setup(spi);
223
224         chip = kzalloc(sizeof *chip, GFP_KERNEL);
225         if (!chip) {
226                 rtc_device_unregister(rtc);
227                 return -ENOMEM;
228         }
229         chip->rtc = rtc;
230         dev_set_drvdata(&spi->dev, chip);
231
232         res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
233         if (res) {
234                 rtc_device_unregister(rtc);
235                 return res;
236         }
237
238         return 0;
239 }
240
241 static int __devexit max6902_remove(struct spi_device *spi)
242 {
243         struct max6902 *chip = platform_get_drvdata(spi);
244         struct rtc_device *rtc = chip->rtc;
245
246         if (rtc)
247                 rtc_device_unregister(rtc);
248
249         kfree(chip);
250
251         return 0;
252 }
253
254 static struct spi_driver max6902_driver = {
255         .driver = {
256                 .name   = "rtc-max6902",
257                 .bus    = &spi_bus_type,
258                 .owner  = THIS_MODULE,
259         },
260         .probe  = max6902_probe,
261         .remove = __devexit_p(max6902_remove),
262 };
263
264 static __init int max6902_init(void)
265 {
266         printk("max6902 spi driver\n");
267         return spi_register_driver(&max6902_driver);
268 }
269 module_init(max6902_init);
270
271 static __exit void max6902_exit(void)
272 {
273         spi_unregister_driver(&max6902_driver);
274 }
275 module_exit(max6902_exit);
276
277 MODULE_DESCRIPTION ("max6902 spi RTC driver");
278 MODULE_AUTHOR ("Raphael Assenat");
279 MODULE_LICENSE ("GPL");