blob: 6880eabf13809e002c83e8499e15d7dcab254ada [file] [log] [blame]
Alessandro Zummo4d4e5ce2005-10-17 23:04:42 +02001/*
2 * x1205.c - An i2c driver for the Xicor X1205 RTC
3 * Copyright 2004 Karen Spearel
4 * Copyright 2005 Alessandro Zummo
5 *
6 * please send all reports to:
7 * kas11 at tampabay dot rr dot com
8 * a dot zummo at towertech dot it
9 *
10 * based on the other drivers in this same directory.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/i2c.h>
22#include <linux/string.h>
23#include <linux/bcd.h>
24#include <linux/rtc.h>
25#include <linux/list.h>
26
27#include <linux/x1205.h>
28
29#define DRV_VERSION "0.9.9"
30
31/* Addresses to scan: none. This chip is located at
32 * 0x6f and uses a two bytes register addressing.
33 * Two bytes need to be written to read a single register,
34 * while most other chips just require one and take the second
35 * one as the data to be written. To prevent corrupting
36 * unknown chips, the user must explicitely set the probe parameter.
37 */
38
39static unsigned short normal_i2c[] = { I2C_CLIENT_END };
40
41/* Insmod parameters */
42I2C_CLIENT_INSMOD;
43I2C_CLIENT_MODULE_PARM(hctosys,
44 "Set the system time from the hardware clock upon initialization");
45
46/* offsets into CCR area */
47
48#define CCR_SEC 0
49#define CCR_MIN 1
50#define CCR_HOUR 2
51#define CCR_MDAY 3
52#define CCR_MONTH 4
53#define CCR_YEAR 5
54#define CCR_WDAY 6
55#define CCR_Y2K 7
56
57#define X1205_REG_SR 0x3F /* status register */
58#define X1205_REG_Y2K 0x37
59#define X1205_REG_DW 0x36
60#define X1205_REG_YR 0x35
61#define X1205_REG_MO 0x34
62#define X1205_REG_DT 0x33
63#define X1205_REG_HR 0x32
64#define X1205_REG_MN 0x31
65#define X1205_REG_SC 0x30
66#define X1205_REG_DTR 0x13
67#define X1205_REG_ATR 0x12
68#define X1205_REG_INT 0x11
69#define X1205_REG_0 0x10
70#define X1205_REG_Y2K1 0x0F
71#define X1205_REG_DWA1 0x0E
72#define X1205_REG_YRA1 0x0D
73#define X1205_REG_MOA1 0x0C
74#define X1205_REG_DTA1 0x0B
75#define X1205_REG_HRA1 0x0A
76#define X1205_REG_MNA1 0x09
77#define X1205_REG_SCA1 0x08
78#define X1205_REG_Y2K0 0x07
79#define X1205_REG_DWA0 0x06
80#define X1205_REG_YRA0 0x05
81#define X1205_REG_MOA0 0x04
82#define X1205_REG_DTA0 0x03
83#define X1205_REG_HRA0 0x02
84#define X1205_REG_MNA0 0x01
85#define X1205_REG_SCA0 0x00
86
87#define X1205_CCR_BASE 0x30 /* Base address of CCR */
88#define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
89
90#define X1205_SR_RTCF 0x01 /* Clock failure */
91#define X1205_SR_WEL 0x02 /* Write Enable Latch */
92#define X1205_SR_RWEL 0x04 /* Register Write Enable */
93
94#define X1205_DTR_DTR0 0x01
95#define X1205_DTR_DTR1 0x02
96#define X1205_DTR_DTR2 0x04
97
98#define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */
99
100/* Prototypes */
101static int x1205_attach(struct i2c_adapter *adapter);
102static int x1205_detach(struct i2c_client *client);
103static int x1205_probe(struct i2c_adapter *adapter, int address, int kind);
104static int x1205_command(struct i2c_client *client, unsigned int cmd,
105 void *arg);
106
107static struct i2c_driver x1205_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +0100108 .driver = {
109 .owner = THIS_MODULE,
110 .name = "x1205",
111 },
Alessandro Zummo4d4e5ce2005-10-17 23:04:42 +0200112 .attach_adapter = &x1205_attach,
113 .detach_client = &x1205_detach,
114};
115
116struct x1205_data {
117 struct i2c_client client;
118 struct list_head list;
119 unsigned int epoch;
120};
121
122static const unsigned char days_in_mo[] =
123 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
124
125static LIST_HEAD(x1205_clients);
126
127/* Workaround until the I2C subsytem will allow to send
128 * commands to a specific client. This function will send the command
129 * to the first client.
130 */
131int x1205_do_command(unsigned int cmd, void *arg)
132{
133 struct list_head *walk;
134 struct list_head *tmp;
135 struct x1205_data *data;
136
137 list_for_each_safe(walk, tmp, &x1205_clients) {
138 data = list_entry(walk, struct x1205_data, list);
139 return x1205_command(&data->client, cmd, arg);
140 }
141
142 return -ENODEV;
143}
144
145#define is_leap(year) \
146 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
147
148/* make sure the rtc_time values are in bounds */
149static int x1205_validate_tm(struct rtc_time *tm)
150{
151 int year = tm->tm_year + 1900;
152
153 if ((tm->tm_year < 70) || (tm->tm_year > 255))
154 return -EINVAL;
155
156 if ((tm->tm_mon > 11) || (tm->tm_mday == 0))
157 return -EINVAL;
158
159 if (tm->tm_mday > days_in_mo[tm->tm_mon]
160 + ((tm->tm_mon == 1) && is_leap(year)))
161 return -EINVAL;
162
163 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
164 return -EINVAL;
165
166 return 0;
167}
168
169/*
170 * In the routines that deal directly with the x1205 hardware, we use
171 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
172 * Epoch is initialized as 2000. Time is set to UTC.
173 */
174static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
175 u8 reg_base)
176{
177 unsigned char dt_addr[2] = { 0, reg_base };
178 static unsigned char sr_addr[2] = { 0, X1205_REG_SR };
179
180 unsigned char buf[8], sr;
181
182 struct i2c_msg msgs[] = {
183 { client->addr, 0, 2, sr_addr }, /* setup read ptr */
184 { client->addr, I2C_M_RD, 1, &sr }, /* read status */
185 { client->addr, 0, 2, dt_addr }, /* setup read ptr */
186 { client->addr, I2C_M_RD, 8, buf }, /* read date */
187 };
188
189 struct x1205_data *data = i2c_get_clientdata(client);
190
191 /* read status register */
192 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
193 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
194 return -EIO;
195 }
196
197 /* check for battery failure */
198 if (sr & X1205_SR_RTCF) {
199 dev_warn(&client->dev,
200 "Clock had a power failure, you must set the date.\n");
201 return -EINVAL;
202 }
203
204 /* read date registers */
205 if ((i2c_transfer(client->adapter, &msgs[2], 2)) != 2) {
206 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
207 return -EIO;
208 }
209
210 dev_dbg(&client->dev,
211 "%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
212 "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
213 __FUNCTION__,
214 buf[0], buf[1], buf[2], buf[3],
215 buf[4], buf[5], buf[6], buf[7]);
216
217 tm->tm_sec = BCD2BIN(buf[CCR_SEC]);
218 tm->tm_min = BCD2BIN(buf[CCR_MIN]);
219 tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
220 tm->tm_mday = BCD2BIN(buf[CCR_MDAY]);
221 tm->tm_mon = BCD2BIN(buf[CCR_MONTH]);
222 data->epoch = BCD2BIN(buf[CCR_Y2K]) * 100;
223 tm->tm_year = BCD2BIN(buf[CCR_YEAR]) + data->epoch - 1900;
224 tm->tm_wday = buf[CCR_WDAY];
225
226 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
227 "mday=%d, mon=%d, year=%d, wday=%d\n",
228 __FUNCTION__,
229 tm->tm_sec, tm->tm_min, tm->tm_hour,
230 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
231
232 return 0;
233}
234
235static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
236 int datetoo, u8 reg_base)
237{
238 int i, err, xfer;
239
240 unsigned char buf[8];
241
242 static const unsigned char wel[3] = { 0, X1205_REG_SR,
243 X1205_SR_WEL };
244
245 static const unsigned char rwel[3] = { 0, X1205_REG_SR,
246 X1205_SR_WEL | X1205_SR_RWEL };
247
248 static const unsigned char diswe[3] = { 0, X1205_REG_SR, 0 };
249
250 struct x1205_data *data = i2c_get_clientdata(client);
251
252 /* check if all values in the tm struct are correct */
253 if ((err = x1205_validate_tm(tm)) < 0)
254 return err;
255
256 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
257 "mday=%d, mon=%d, year=%d, wday=%d\n",
258 __FUNCTION__,
259 tm->tm_sec, tm->tm_min, tm->tm_hour,
260 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
261
262 buf[CCR_SEC] = BIN2BCD(tm->tm_sec);
263 buf[CCR_MIN] = BIN2BCD(tm->tm_min);
264
265 /* set hour and 24hr bit */
266 buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL;
267
268 /* should we also set the date? */
269 if (datetoo) {
270 buf[CCR_MDAY] = BIN2BCD(tm->tm_mday);
271
272 /* month, 0 - 11 */
273 buf[CCR_MONTH] = BIN2BCD(tm->tm_mon);
274
275 /* year, since 1900 */
276 buf[CCR_YEAR] = BIN2BCD(tm->tm_year + 1900 - data->epoch);
277 buf[CCR_WDAY] = tm->tm_wday & 0x07;
278 buf[CCR_Y2K] = BIN2BCD(data->epoch / 100);
279 }
280
281 /* this sequence is required to unlock the chip */
282 xfer = i2c_master_send(client, wel, 3);
283 if (xfer != 3) {
284 dev_err(&client->dev, "%s: wel - %d\n", __FUNCTION__, xfer);
285 return -EIO;
286 }
287
288 xfer = i2c_master_send(client, rwel, 3);
289 if (xfer != 3) {
290 dev_err(&client->dev, "%s: rwel - %d\n", __FUNCTION__, xfer);
291 return -EIO;
292 }
293
294 /* write register's data */
295 for (i = 0; i < (datetoo ? 8 : 3); i++) {
296 unsigned char rdata[3] = { 0, reg_base + i, buf[i] };
297
298 xfer = i2c_master_send(client, rdata, 3);
299 if (xfer != 3) {
300 dev_err(&client->dev,
301 "%s: xfer=%d addr=%02x, data=%02x\n",
302 __FUNCTION__,
303 xfer, rdata[1], rdata[2]);
304 return -EIO;
305 }
306 };
307
308 /* disable further writes */
309 xfer = i2c_master_send(client, diswe, 3);
310 if (xfer != 3) {
311 dev_err(&client->dev, "%s: diswe - %d\n", __FUNCTION__, xfer);
312 return -EIO;
313 }
314
315 return 0;
316}
317
318static int x1205_get_dtrim(struct i2c_client *client, int *trim)
319{
320 unsigned char dtr;
321 static unsigned char dtr_addr[2] = { 0, X1205_REG_DTR };
322
323 struct i2c_msg msgs[] = {
324 { client->addr, 0, 2, dtr_addr }, /* setup read ptr */
325 { client->addr, I2C_M_RD, 1, &dtr }, /* read dtr */
326 };
327
328 /* read dtr register */
329 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
330 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
331 return -EIO;
332 }
333
334 dev_dbg(&client->dev, "%s: raw dtr=%x\n", __FUNCTION__, dtr);
335
336 *trim = 0;
337
338 if (dtr & X1205_DTR_DTR0)
339 *trim += 20;
340
341 if (dtr & X1205_DTR_DTR1)
342 *trim += 10;
343
344 if (dtr & X1205_DTR_DTR2)
345 *trim = -*trim;
346
347 return 0;
348}
349
350static int x1205_get_atrim(struct i2c_client *client, int *trim)
351{
352 s8 atr;
353 static unsigned char atr_addr[2] = { 0, X1205_REG_ATR };
354
355 struct i2c_msg msgs[] = {
356 { client->addr, 0, 2, atr_addr }, /* setup read ptr */
357 { client->addr, I2C_M_RD, 1, &atr }, /* read atr */
358 };
359
360 /* read atr register */
361 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
362 dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
363 return -EIO;
364 }
365
366 dev_dbg(&client->dev, "%s: raw atr=%x\n", __FUNCTION__, atr);
367
368 /* atr is a two's complement value on 6 bits,
369 * perform sign extension. The formula is
370 * Catr = (atr * 0.25pF) + 11.00pF.
371 */
372 if (atr & 0x20)
373 atr |= 0xC0;
374
375 dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __FUNCTION__, atr, atr);
376
377 *trim = (atr * 250) + 11000;
378
379 dev_dbg(&client->dev, "%s: real=%d\n", __FUNCTION__, *trim);
380
381 return 0;
382}
383
384static int x1205_hctosys(struct i2c_client *client)
385{
386 int err;
387
388 struct rtc_time tm;
389 struct timespec tv;
390
391 err = x1205_command(client, X1205_CMD_GETDATETIME, &tm);
392
393 if (err) {
394 dev_err(&client->dev,
395 "Unable to set the system clock\n");
396 return err;
397 }
398
399 /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
400 * whether it stores the most close value or the value with partial
401 * seconds truncated. However, it is important that we use it to store
402 * the truncated value. This is because otherwise it is necessary,
403 * in an rtc sync function, to read both xtime.tv_sec and
404 * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
405 * of >32bits is not possible. So storing the most close value would
406 * slow down the sync API. So here we have the truncated value and
407 * the best guess is to add 0.5s.
408 */
409
410 tv.tv_nsec = NSEC_PER_SEC >> 1;
411
412 /* WARNING: this is not the C library 'mktime' call, it is a built in
413 * inline function from include/linux/time.h. It expects (requires)
414 * the month to be in the range 1-12
415 */
416
417 tv.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1,
418 tm.tm_mday, tm.tm_hour,
419 tm.tm_min, tm.tm_sec);
420
421 do_settimeofday(&tv);
422
423 dev_info(&client->dev,
424 "setting the system clock to %d-%d-%d %d:%d:%d\n",
425 tm.tm_year + 1900, tm.tm_mon + 1,
426 tm.tm_mday, tm.tm_hour, tm.tm_min,
427 tm.tm_sec);
428
429 return 0;
430}
431
432struct x1205_limit
433{
434 unsigned char reg;
435 unsigned char mask;
436 unsigned char min;
437 unsigned char max;
438};
439
440static int x1205_validate_client(struct i2c_client *client)
441{
442 int i, xfer;
443
444 /* Probe array. We will read the register at the specified
445 * address and check if the given bits are zero.
446 */
447 static const unsigned char probe_zero_pattern[] = {
448 /* register, mask */
449 X1205_REG_SR, 0x18,
450 X1205_REG_DTR, 0xF8,
451 X1205_REG_ATR, 0xC0,
452 X1205_REG_INT, 0x18,
453 X1205_REG_0, 0xFF,
454 };
455
456 static const struct x1205_limit probe_limits_pattern[] = {
457 /* register, mask, min, max */
458 { X1205_REG_Y2K, 0xFF, 19, 20 },
459 { X1205_REG_DW, 0xFF, 0, 6 },
460 { X1205_REG_YR, 0xFF, 0, 99 },
461 { X1205_REG_MO, 0xFF, 0, 12 },
462 { X1205_REG_DT, 0xFF, 0, 31 },
463 { X1205_REG_HR, 0x7F, 0, 23 },
464 { X1205_REG_MN, 0xFF, 0, 59 },
465 { X1205_REG_SC, 0xFF, 0, 59 },
466 { X1205_REG_Y2K1, 0xFF, 19, 20 },
467 { X1205_REG_Y2K0, 0xFF, 19, 20 },
468 };
469
470 /* check that registers have bits a 0 where expected */
471 for (i = 0; i < ARRAY_SIZE(probe_zero_pattern); i += 2) {
472 unsigned char buf;
473
474 unsigned char addr[2] = { 0, probe_zero_pattern[i] };
475
476 struct i2c_msg msgs[2] = {
477 { client->addr, 0, 2, addr },
478 { client->addr, I2C_M_RD, 1, &buf },
479 };
480
481 xfer = i2c_transfer(client->adapter, msgs, 2);
482 if (xfer != 2) {
483 dev_err(&client->adapter->dev,
484 "%s: could not read register %x\n",
485 __FUNCTION__, addr[1]);
486
487 return -EIO;
488 }
489
490 if ((buf & probe_zero_pattern[i+1]) != 0) {
491 dev_err(&client->adapter->dev,
492 "%s: register=%02x, zero pattern=%d, value=%x\n",
493 __FUNCTION__, addr[1], i, buf);
494
495 return -ENODEV;
496 }
497 }
498
499 /* check limits (only registers with bcd values) */
500 for (i = 0; i < ARRAY_SIZE(probe_limits_pattern); i++) {
501 unsigned char reg, value;
502
503 unsigned char addr[2] = { 0, probe_limits_pattern[i].reg };
504
505 struct i2c_msg msgs[2] = {
506 { client->addr, 0, 2, addr },
507 { client->addr, I2C_M_RD, 1, &reg },
508 };
509
510 xfer = i2c_transfer(client->adapter, msgs, 2);
511
512 if (xfer != 2) {
513 dev_err(&client->adapter->dev,
514 "%s: could not read register %x\n",
515 __FUNCTION__, addr[1]);
516
517 return -EIO;
518 }
519
520 value = BCD2BIN(reg & probe_limits_pattern[i].mask);
521
522 if (value > probe_limits_pattern[i].max ||
523 value < probe_limits_pattern[i].min) {
524 dev_dbg(&client->adapter->dev,
525 "%s: register=%x, lim pattern=%d, value=%d\n",
526 __FUNCTION__, addr[1], i, value);
527
528 return -ENODEV;
529 }
530 }
531
532 return 0;
533}
534
535static int x1205_attach(struct i2c_adapter *adapter)
536{
537 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
538
539 return i2c_probe(adapter, &addr_data, x1205_probe);
540}
541
542int x1205_direct_attach(int adapter_id,
543 struct i2c_client_address_data *address_data)
544{
545 int err;
546 struct i2c_adapter *adapter = i2c_get_adapter(adapter_id);
547
548 if (adapter) {
549 err = i2c_probe(adapter,
550 address_data, x1205_probe);
551
552 i2c_put_adapter(adapter);
553
554 return err;
555 }
556
557 return -ENODEV;
558}
559
560static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
561{
562 struct i2c_client *client;
563 struct x1205_data *data;
564
565 int err = 0;
566
567 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
568
569 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
570 err = -ENODEV;
571 goto exit;
572 }
573
574 if (!(data = kzalloc(sizeof(struct x1205_data), GFP_KERNEL))) {
575 err = -ENOMEM;
576 goto exit;
577 }
578
579 /* Initialize our structures */
580 data->epoch = 2000;
581
582 client = &data->client;
583 client->addr = address;
584 client->driver = &x1205_driver;
585 client->adapter = adapter;
586
587 strlcpy(client->name, "x1205", I2C_NAME_SIZE);
588
589 i2c_set_clientdata(client, data);
590
591 /* Verify the chip is really an X1205 */
592 if (kind < 0) {
593 if (x1205_validate_client(client) < 0) {
594 err = -ENODEV;
595 goto exit_kfree;
596 }
597 }
598
599 /* Inform the i2c layer */
600 if ((err = i2c_attach_client(client)))
601 goto exit_kfree;
602
603 list_add(&data->list, &x1205_clients);
604
605 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
606
607 /* If requested, set the system time */
608 if (hctosys)
609 x1205_hctosys(client);
610
611 return 0;
612
613exit_kfree:
614 kfree(data);
615
616exit:
617 return err;
618}
619
620static int x1205_detach(struct i2c_client *client)
621{
622 int err;
623 struct x1205_data *data = i2c_get_clientdata(client);
624
625 dev_dbg(&client->dev, "%s\n", __FUNCTION__);
626
627 if ((err = i2c_detach_client(client)))
628 return err;
629
630 list_del(&data->list);
631
632 kfree(data);
633
634 return 0;
635}
636
637static int x1205_command(struct i2c_client *client, unsigned int cmd,
638 void *param)
639{
640 if (param == NULL)
641 return -EINVAL;
642
643 if (!capable(CAP_SYS_TIME))
644 return -EACCES;
645
646 dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
647
648 switch (cmd) {
649 case X1205_CMD_GETDATETIME:
650 return x1205_get_datetime(client, param, X1205_CCR_BASE);
651
652 case X1205_CMD_SETTIME:
653 return x1205_set_datetime(client, param, 0,
654 X1205_CCR_BASE);
655
656 case X1205_CMD_SETDATETIME:
657 return x1205_set_datetime(client, param, 1,
658 X1205_CCR_BASE);
659
660 case X1205_CMD_GETALARM:
661 return x1205_get_datetime(client, param, X1205_ALM0_BASE);
662
663 case X1205_CMD_SETALARM:
664 return x1205_set_datetime(client, param, 1,
665 X1205_ALM0_BASE);
666
667 case X1205_CMD_GETDTRIM:
668 return x1205_get_dtrim(client, param);
669
670 case X1205_CMD_GETATRIM:
671 return x1205_get_atrim(client, param);
672
673 default:
674 return -EINVAL;
675 }
676}
677
678static int __init x1205_init(void)
679{
680 return i2c_add_driver(&x1205_driver);
681}
682
683static void __exit x1205_exit(void)
684{
685 i2c_del_driver(&x1205_driver);
686}
687
688MODULE_AUTHOR(
689 "Karen Spearel <kas11@tampabay.rr.com>, "
690 "Alessandro Zummo <a.zummo@towertech.it>");
691MODULE_DESCRIPTION("Xicor X1205 RTC driver");
692MODULE_LICENSE("GPL");
693MODULE_VERSION(DRV_VERSION);
694
695EXPORT_SYMBOL_GPL(x1205_do_command);
696EXPORT_SYMBOL_GPL(x1205_direct_attach);
697
698module_init(x1205_init);
699module_exit(x1205_exit);