aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/rtc/rtc-max6902.c176
1 files changed, 38 insertions, 138 deletions
diff --git a/drivers/rtc/rtc-max6902.c b/drivers/rtc/rtc-max6902.c
index 2f6507df7b49..36a8ea9ed8ba 100644
--- a/drivers/rtc/rtc-max6902.c
+++ b/drivers/rtc/rtc-max6902.c
@@ -9,14 +9,6 @@
9 * 9 *
10 * Driver for MAX6902 spi RTC 10 * Driver for MAX6902 spi RTC
11 * 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 */ 12 */
21 13
22#include <linux/module.h> 14#include <linux/module.h>
@@ -26,7 +18,6 @@
26#include <linux/rtc.h> 18#include <linux/rtc.h>
27#include <linux/spi/spi.h> 19#include <linux/spi/spi.h>
28#include <linux/bcd.h> 20#include <linux/bcd.h>
29#include <linux/delay.h>
30 21
31#define MAX6902_REG_SECONDS 0x01 22#define MAX6902_REG_SECONDS 0x01
32#define MAX6902_REG_MINUTES 0x03 23#define MAX6902_REG_MINUTES 0x03
@@ -38,16 +29,7 @@
38#define MAX6902_REG_CONTROL 0x0F 29#define MAX6902_REG_CONTROL 0x0F
39#define MAX6902_REG_CENTURY 0x13 30#define MAX6902_REG_CENTURY 0x13
40 31
41#undef MAX6902_DEBUG 32static int max6902_set_reg(struct device *dev, unsigned char address,
42
43struct 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];
48};
49
50static void max6902_set_reg(struct device *dev, unsigned char address,
51 unsigned char data) 33 unsigned char data)
52{ 34{
53 struct spi_device *spi = to_spi_device(dev); 35 struct spi_device *spi = to_spi_device(dev);
@@ -57,113 +39,58 @@ static void max6902_set_reg(struct device *dev, unsigned char address,
57 buf[0] = address & 0x7f; 39 buf[0] = address & 0x7f;
58 buf[1] = data; 40 buf[1] = data;
59 41
60 spi_write(spi, buf, 2); 42 return spi_write_then_read(spi, buf, 2, NULL, 0);
61} 43}
62 44
63static int max6902_get_reg(struct device *dev, unsigned char address, 45static int max6902_get_reg(struct device *dev, unsigned char address,
64 unsigned char *data) 46 unsigned char *data)
65{ 47{
66 struct spi_device *spi = to_spi_device(dev); 48 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;
71
72 if (!data)
73 return -EINVAL;
74
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;
82 49
83 /* Set MSB to indicate read */ 50 /* Set MSB to indicate read */
84 chip->tx_buf[0] = address | 0x80; 51 *data = address | 0x80;
85
86 spi_message_add_tail(&xfer, &message);
87 52
88 /* do the i/o */ 53 return spi_write_then_read(spi, data, 1, data, 1);
89 status = spi_sync(spi, &message);
90
91 if (status == 0)
92 *data = chip->rx_buf[1];
93 return status;
94} 54}
95 55
96static int max6902_get_datetime(struct device *dev, struct rtc_time *dt) 56static int max6902_read_time(struct device *dev, struct rtc_time *dt)
97{ 57{
98 unsigned char tmp; 58 int err, century;
99 int century;
100 int err;
101 struct spi_device *spi = to_spi_device(dev); 59 struct spi_device *spi = to_spi_device(dev);
102 struct max6902 *chip = dev_get_drvdata(dev); 60 unsigned char buf[8];
103 struct spi_message message;
104 struct spi_transfer xfer;
105 int status;
106 61
107 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp); 62 buf[0] = 0xbf; /* Burst read */
108 if (err)
109 return err;
110
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);
119 63
120 /* do the i/o */ 64 err = spi_write_then_read(spi, buf, 1, buf, 8);
121 status = spi_sync(spi, &message); 65 if (err != 0)
122 if (status) 66 return err;
123 return status;
124 67
125 /* The chip sends data in this order: 68 /* The chip sends data in this order:
126 * Seconds, Minutes, Hours, Date, Month, Day, Year */ 69 * Seconds, Minutes, Hours, Date, Month, Day, Year */
127 dt->tm_sec = bcd2bin(chip->buf[1]); 70 dt->tm_sec = bcd2bin(buf[0]);
128 dt->tm_min = bcd2bin(chip->buf[2]); 71 dt->tm_min = bcd2bin(buf[1]);
129 dt->tm_hour = bcd2bin(chip->buf[3]); 72 dt->tm_hour = bcd2bin(buf[2]);
130 dt->tm_mday = bcd2bin(chip->buf[4]); 73 dt->tm_mday = bcd2bin(buf[3]);
131 dt->tm_mon = bcd2bin(chip->buf[5]) - 1; 74 dt->tm_mon = bcd2bin(buf[4]) - 1;
132 dt->tm_wday = bcd2bin(chip->buf[6]); 75 dt->tm_wday = bcd2bin(buf[5]);
133 dt->tm_year = bcd2bin(chip->buf[7]); 76 dt->tm_year = bcd2bin(buf[6]);
77
78 /* Read century */
79 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
80 if (err != 0)
81 return err;
134 82
135 century = bcd2bin(tmp) * 100; 83 century = bcd2bin(buf[0]) * 100;
136 84
137 dt->tm_year += century; 85 dt->tm_year += century;
138 dt->tm_year -= 1900; 86 dt->tm_year -= 1900;
139 87
140#ifdef MAX6902_DEBUG 88 return rtc_valid_tm(dt);
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
150
151 return 0;
152} 89}
153 90
154static int max6902_set_datetime(struct device *dev, struct rtc_time *dt) 91static int max6902_set_time(struct device *dev, struct rtc_time *dt)
155{ 92{
156 dt->tm_year = dt->tm_year+1900; 93 dt->tm_year = dt->tm_year + 1900;
157
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
167 94
168 /* Remove write protection */ 95 /* Remove write protection */
169 max6902_set_reg(dev, 0xF, 0); 96 max6902_set_reg(dev, 0xF, 0);
@@ -173,10 +100,10 @@ static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
173 max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour)); 100 max6902_set_reg(dev, 0x05, bin2bcd(dt->tm_hour));
174 101
175 max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday)); 102 max6902_set_reg(dev, 0x07, bin2bcd(dt->tm_mday));
176 max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon+1)); 103 max6902_set_reg(dev, 0x09, bin2bcd(dt->tm_mon + 1));
177 max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday)); 104 max6902_set_reg(dev, 0x0B, bin2bcd(dt->tm_wday));
178 max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year%100)); 105 max6902_set_reg(dev, 0x0D, bin2bcd(dt->tm_year % 100));
179 max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year/100)); 106 max6902_set_reg(dev, 0x13, bin2bcd(dt->tm_year / 100));
180 107
181 /* Compulab used a delay here. However, the datasheet 108 /* Compulab used a delay here. However, the datasheet
182 * does not mention a delay being required anywhere... */ 109 * does not mention a delay being required anywhere... */
@@ -188,16 +115,6 @@ static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
188 return 0; 115 return 0;
189} 116}
190 117
191static int max6902_read_time(struct device *dev, struct rtc_time *tm)
192{
193 return max6902_get_datetime(dev, tm);
194}
195
196static int max6902_set_time(struct device *dev, struct rtc_time *tm)
197{
198 return max6902_set_datetime(dev, tm);
199}
200
201static const struct rtc_class_ops max6902_rtc_ops = { 118static const struct rtc_class_ops max6902_rtc_ops = {
202 .read_time = max6902_read_time, 119 .read_time = max6902_read_time,
203 .set_time = max6902_set_time, 120 .set_time = max6902_set_time,
@@ -207,45 +124,29 @@ static int __devinit max6902_probe(struct spi_device *spi)
207{ 124{
208 struct rtc_device *rtc; 125 struct rtc_device *rtc;
209 unsigned char tmp; 126 unsigned char tmp;
210 struct max6902 *chip;
211 int res; 127 int res;
212 128
213 rtc = rtc_device_register("max6902",
214 &spi->dev, &max6902_rtc_ops, THIS_MODULE);
215 if (IS_ERR(rtc))
216 return PTR_ERR(rtc);
217
218 spi->mode = SPI_MODE_3; 129 spi->mode = SPI_MODE_3;
219 spi->bits_per_word = 8; 130 spi->bits_per_word = 8;
220 spi_setup(spi); 131 spi_setup(spi);
221 132
222 chip = kzalloc(sizeof *chip, GFP_KERNEL);
223 if (!chip) {
224 rtc_device_unregister(rtc);
225 return -ENOMEM;
226 }
227 chip->rtc = rtc;
228 dev_set_drvdata(&spi->dev, chip);
229
230 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp); 133 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
231 if (res) { 134 if (res != 0)
232 rtc_device_unregister(rtc);
233 return res; 135 return res;
234 } 136
137 rtc = rtc_device_register("max6902",
138 &spi->dev, &max6902_rtc_ops, THIS_MODULE);
139 if (IS_ERR(rtc))
140 return PTR_ERR(rtc);
235 141
236 return 0; 142 return 0;
237} 143}
238 144
239static int __devexit max6902_remove(struct spi_device *spi) 145static int __devexit max6902_remove(struct spi_device *spi)
240{ 146{
241 struct max6902 *chip = platform_get_drvdata(spi); 147 struct rtc_device *rtc = platform_get_drvdata(spi);
242 struct rtc_device *rtc = chip->rtc;
243
244 if (rtc)
245 rtc_device_unregister(rtc);
246
247 kfree(chip);
248 148
149 rtc_device_unregister(rtc);
249 return 0; 150 return 0;
250} 151}
251 152
@@ -261,7 +162,6 @@ static struct spi_driver max6902_driver = {
261 162
262static __init int max6902_init(void) 163static __init int max6902_init(void)
263{ 164{
264 printk("max6902 spi driver\n");
265 return spi_register_driver(&max6902_driver); 165 return spi_register_driver(&max6902_driver);
266} 166}
267module_init(max6902_init); 167module_init(max6902_init);