1 --- linux-2.6.14-rc2/drivers/i2c/chips/Kconfig 2005-09-24 13:17:13.000000000 +0200
2 +++ test3/drivers/i2c/chips/Kconfig 2005-09-24 16:09:51.000000000 +0200
4 This driver can also be built as a module. If so, the module
5 will be called max6875.
8 + tristate "Xicor X1205 RTC chip"
12 + If you say yes here you get support for the Xicor X1205 RTC chip.
14 + This driver can also be built as a module. If so, the module
15 + will be called x1205
18 --- linux-2.6.14-rc2/drivers/i2c/chips/Makefile 2005-09-17 12:42:33.000000000 +0200
19 +++ test3/drivers/i2c/chips/Makefile 2005-09-24 16:49:34.000000000 +0200
21 obj-$(CONFIG_SENSORS_RTC8564) += rtc8564.o
22 obj-$(CONFIG_ISP1301_OMAP) += isp1301_omap.o
23 obj-$(CONFIG_TPS65010) += tps65010.o
24 +obj-$(CONFIG_SENSORS_X1205) += x1205.o
26 ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
27 EXTRA_CFLAGS += -DDEBUG
28 diff -urN linux-2.6.14-rc2/drivers/i2c/chips/x1205.c test3/drivers/i2c/chips/x1205.c
29 --- linux-2.6.14-rc2/drivers/i2c/chips/x1205.c 1970-01-01 01:00:00.000000000 +0100
30 +++ test3/drivers/i2c/chips/x1205.c 2005-09-24 16:11:16.000000000 +0200
33 + * linux/drivers/i2c/chips/x1205.c
35 + * x1205.c - An 12c driver for the Xicor X1205 RTC
36 + * Copyright 2004 Karen Spearel
37 + * Copyright 2005 Alessandro Zummo
39 + * please send all reports to:
40 + * kas11 at tampabay dot rr dot com
41 + * a dot zummo at towertech dot it
43 + * based on the other drivers in this same directory.
45 + * This program is free software; you can redistribute it and/or modify
46 + * it under the terms of the GNU General Public License as published by
47 + * the Free Software Foundation; either version 2 of the License, or
48 + * (at your option) any later version.
51 +#include <linux/module.h>
52 +#include <linux/init.h>
53 +#include <linux/slab.h>
54 +#include <linux/i2c.h>
55 +#include <linux/string.h>
56 +#include <linux/bcd.h>
57 +#include <linux/rtc.h>
58 +#include <linux/list.h>
60 +#include <linux/x1205.h>
62 +#define EPOCH_1900 1900
63 +#define EPOCH_1970 1970
65 +#define DRIVER_VERSION "0.9.5"
66 +#define DRIVER_NAME (x1205_driver.name)
69 +/* Addresses to scan */
70 +static unsigned short normal_i2c[] = { X1205_I2C_BUS_ADDR, I2C_CLIENT_END };
72 +/* Insmod parameters */
74 +I2C_CLIENT_MODULE_PARM(hctosys,
75 + "Set the system time from the hardware clock upon initialization");
79 +static int x1205_attach(struct i2c_adapter *adapter);
80 +static int x1205_detach(struct i2c_client *client);
81 +static int x1205_probe(struct i2c_adapter *adapter, int address, int kind);
82 +static int x1205_validate_client(struct i2c_client *client);
84 +static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
86 +static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
87 + int datetoo, u8 reg_base);
88 +static int x1205_validate_tm(struct rtc_time *tm);
89 +static int x1205_command(struct i2c_client *client, unsigned int cmd, void *arg);
90 +static int x1205_hctosys(struct i2c_client *client);
93 +static struct i2c_driver x1205_driver = {
94 + .owner = THIS_MODULE,
96 + .flags = I2C_DF_NOTIFY,
97 + .attach_adapter = &x1205_attach,
98 + .detach_client = &x1205_detach,
99 +/* .command = &x1205_command,*/
103 + struct i2c_client client;
104 + struct list_head list;
105 + unsigned int epoch;
109 +static const unsigned char days_in_mo[] =
110 +{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
112 +static LIST_HEAD(x1205_clients);
114 +/* Workaround until the I2C subsytem will allow to send
115 + * commands to a specific client. This function will send the command
116 + * to the first client.
119 +int x1205_do_command(unsigned int cmd, void *arg)
121 + struct list_head *walk;
122 + struct list_head *tmp;
123 + struct x1205_data *data;
125 + list_for_each_safe(walk, tmp, &x1205_clients) {
126 + data = list_entry(walk, struct x1205_data, list);
127 + return x1205_command(&data->client, cmd, arg);
135 + * in the routines that deal directly with the x1205 hardware, we use
136 + * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
137 + * Epoch is inited as 2000. Time is set to UT
140 +static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
143 + static unsigned char addr[2] = { 0, };
144 + unsigned char buf[8];
146 + struct i2c_msg msgs[2] = {
147 + { client->addr, 0, 2, addr }, /* random read */
148 + { client->addr, I2C_M_RD, 8, buf },
151 + struct x1205_data *xdata = i2c_get_clientdata(client);
153 + addr[1] = reg_base;
155 + if ((i2c_transfer(client->adapter, msgs, 2)) == 2) {
156 + /* did we read 2 messages? */
158 + dev_dbg(&client->dev,
159 + "%s: raw read data - sec-%02x min-%02x hr-%02x"
160 + " mday-%02x mon-%02x year-%02x wday-%02x y2k-%02x\n",
162 + buf[0], buf[1], buf[2], buf[3],
163 + buf[4], buf[5], buf[6], buf[7]);
165 + tm->tm_sec = BCD2BIN(buf[CCR_SEC]);
166 + tm->tm_min = BCD2BIN(buf[CCR_MIN]);
167 + buf[CCR_HOUR] &= ~X1205_HR_MIL;
168 + tm->tm_hour = BCD2BIN(buf[CCR_HOUR]); /* hr is 0-23 */
169 + tm->tm_mday = BCD2BIN(buf[CCR_MDAY]);
170 + tm->tm_mon = BCD2BIN(buf[CCR_MONTH]);
171 + xdata->epoch = BCD2BIN(buf[CCR_Y2K]) * 100;
172 + tm->tm_year = BCD2BIN(buf[CCR_YEAR]) + xdata->epoch - EPOCH_1900;
173 + tm->tm_wday = buf[CCR_WDAY];
175 + dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
176 + "mday=%d, mon=%d, year=%d, wday=%d\n",
178 + tm->tm_sec, tm->tm_min, tm->tm_hour,
179 + tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
182 + dev_dbg(&client->dev, "%s: read error\n", __FUNCTION__);
190 +static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
191 + int datetoo, u8 reg_base)
195 + static unsigned char wel[3] = { 0, X1205_REG_SR,
199 + static unsigned char rwel[3] = { 0, X1205_REG_SR,
200 + X1205_SR_WEL | X1205_SR_RWEL };
202 + static unsigned char diswe[3] = { 0, X1205_REG_SR, 0 };
204 + static unsigned char data[3] = { 0, };
205 + static unsigned char buf[8];
207 + struct x1205_data *xdata = i2c_get_clientdata(client);
209 + if ((err = x1205_validate_tm(tm)) < 0)
212 + dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
213 + "mday=%d, mon=%d, year=%d, wday=%d\n",
215 + tm->tm_sec, tm->tm_min, tm->tm_hour,
216 + tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
219 + buf[CCR_SEC] = BIN2BCD(tm->tm_sec);
220 + buf[CCR_MIN] = BIN2BCD(tm->tm_min);
222 + /* Set 24HR format */
223 + buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL;
225 + if (datetoo == X1205_DATETOO) {
226 + buf[CCR_MDAY] = BIN2BCD(tm->tm_mday);
228 + /* month, 0 - 11 */
229 + buf[CCR_MONTH] = BIN2BCD(tm->tm_mon); /* input is 0-11 */
231 + /* year, since 1900 */
232 + buf[CCR_YEAR] = BIN2BCD((tm->tm_year + EPOCH_1900 - xdata->epoch));
233 + buf[CCR_WDAY] = tm->tm_wday & 7;
234 + buf[CCR_Y2K] = BIN2BCD(xdata->epoch / 100);
237 + xfer = i2c_master_send(client, wel, 3);
238 + dev_dbg(&client->dev, "%s: wen - %x\n", __FUNCTION__, xfer);
242 + xfer = i2c_master_send(client, rwel, 3);
243 + dev_dbg(&client->dev, "%s: wenb - %x\n", __FUNCTION__, xfer);
247 + for (i = 0; i < 8; i++) {
248 + data[1] = i + reg_base;
250 + xfer = i2c_master_send(client, data, 3);
252 + dev_dbg(&client->dev, "%s: xfer %d addr, %02x, data %02x\n",
254 + xfer, data[1], data[2]);
260 + xfer = i2c_master_send(client, diswe, 3);
261 + dev_dbg(&client->dev, "%s: wdis - %x\n", __FUNCTION__, xfer);
268 +static int x1205_hctosys(struct i2c_client *client)
272 + struct rtc_time tm;
273 + struct timespec tv;
276 + err = x1205_command(client, X1205_CMD_GETDATETIME, &tm);
280 + dev_info(&client->adapter->dev,
281 + "%s: Unable to set the system clock\n",
288 + /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary whether
289 + * it stores the most close value or the value with partial seconds
290 + * truncated, however it is important for x1205_sync_rtc that it be
291 + * defined to store the truncated value. This is because otherwise it
292 + * is necessary to read both xtime.tv_sec and xtime.tv_nsec in the
293 + * sync function, and atomic reads of >32bits on ARM are not possible.
294 + * So storing the most close value would slow down the sync API. So
295 + * Here we have the truncated value and the best guess is to add 0.5s
298 + tv.tv_nsec = NSEC_PER_SEC >> 1;
300 + /* WARNING: this is not the C library 'mktime' call, it is a built in
301 + * inline function from include/linux/time.h. It expects (requires)
302 + * the month to be in the range 1-12
305 + tv.tv_sec = mktime(tm.tm_year + EPOCH_1900, tm.tm_mon + 1,
306 + tm.tm_mday, tm.tm_hour,
307 + tm.tm_min, tm.tm_sec);
309 + do_settimeofday(&tv);
311 + dev_info(&client->adapter->dev,
312 + "%s: Setting the system clock to %d-%d-%d %d:%d:%d\n",
314 + tm.tm_year + EPOCH_1900, tm.tm_mon + 1,
315 + tm.tm_mday, tm.tm_hour, tm.tm_min,
321 +static int x1205_validate_client(struct i2c_client *client)
325 + /* Probe array. We will read the register at the specified
326 + * address and check if the given bits are zero.
329 + const unsigned char probe_pattern[] = {
331 + X1205_REG_SR, 0x18,
332 + X1205_REG_Y2K, 0xC6,
333 + X1205_REG_DW, 0xF8,
334 + X1205_REG_MO, 0xE0,
335 + X1205_REG_DT, 0xC0,
336 + X1205_REG_HR, 0x40,
337 + X1205_REG_MN, 0x80,
338 + X1205_REG_SC, 0x80,
339 + X1205_REG_DTR, 0xF8,
340 + X1205_REG_ATR, 0x18,
341 + X1205_REG_INT, 0x18,
343 + X1205_REG_Y2K1, 0xC6,
344 + X1205_REG_DWA1, 0x78,
345 + X1205_REG_MOA1, 0x60,
346 + X1205_REG_DTA1, 0x40,
347 + X1205_REG_HRA1, 0x40,
348 + X1205_REG_Y2K0, 0xC6,
349 + X1205_REG_DWA0, 0x78,
350 + X1205_REG_MOA0, 0x60,
351 + X1205_REG_DTA0, 0x40,
352 + X1205_REG_HRA0, 0x40,
355 + for (i = 0; i < sizeof(probe_pattern); i += 2)
359 + static unsigned char addr[2];
361 + struct i2c_msg msgs[2] = {
362 + { client->addr, 0, 2, addr }, /* random read */
363 + { client->addr, I2C_M_RD, 1, &buf },
367 + addr[1] = probe_pattern[i];
369 + xfer = i2c_transfer(client->adapter, msgs, 2);
372 + dev_dbg(&client->adapter->dev,
373 + "%s: could not read register %x\n",
374 + __FUNCTION__, addr[1]);
379 + if ((buf & probe_pattern[i+1]) != 0) {
380 + dev_dbg(&client->adapter->dev,
381 + "%s: register %x, pattern %d: %x\n",
382 + __FUNCTION__, addr[1], i, buf);
391 +static int x1205_attach(struct i2c_adapter *adapter)
393 + dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
395 + return i2c_probe(adapter, &addr_data, x1205_probe);
398 +static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
400 + struct i2c_client *new_client;
401 + struct x1205_data *xdata;
405 + dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
407 + if (!(xdata = kmalloc(sizeof(struct x1205_data), GFP_KERNEL))) {
412 + /* Fill our data structure */
414 + memset(xdata, 0, sizeof(struct x1205_data));
416 + xdata->epoch = 2000;
419 + /* Prepare i2c_client structure */
421 + new_client = &xdata->client;
422 + i2c_set_clientdata(new_client, xdata);
423 + new_client->addr = address;
424 + new_client->adapter = adapter;
425 + new_client->driver = &x1205_driver;
426 + new_client->flags = 0;
428 + strlcpy(new_client->name, "x1205", I2C_NAME_SIZE);
431 + /* Verify the chip is really an X1205 */
435 + if (x1205_validate_client(new_client) < 0) {
441 + /* Inform the i2c layer */
442 + if ((err = i2c_attach_client(new_client)))
445 + list_add(&xdata->list, &x1205_clients);
447 + dev_info(&adapter->dev, "%s: chip found, driver " DRIVER_VERSION "\n",
450 + /* If requested, se the system time */
452 + x1205_hctosys(new_client);
463 +static int x1205_detach(struct i2c_client *client)
466 + struct x1205_data *data = i2c_get_clientdata(client);
468 + dev_dbg(&client->dev, "%s\n", __FUNCTION__);
470 + if ((err = i2c_detach_client(client)))
473 + list_del(&data->list);
478 +/* make sure the rtc_time values are in bounds */
479 +static int x1205_validate_tm(struct rtc_time *tm)
481 + tm->tm_year += 1900;
483 + if (tm->tm_year < EPOCH_1970)
486 + if ((tm->tm_mon > 11) || (tm->tm_mday == 0))
489 + if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ( (tm->tm_mon == 1) &&
490 + ((!(tm->tm_year % 4) && (tm->tm_year % 100) ) || !(tm->tm_year % 400)))))
493 + if ((tm->tm_year -= EPOCH_1900) > 255)
496 + if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
502 +static int x1205_command(struct i2c_client *client, unsigned int cmd, void *tm)
504 + if (client == NULL || tm == NULL)
507 + if (!capable(CAP_SYS_TIME))
510 + dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
513 + case X1205_CMD_GETDATETIME:
514 + return x1205_get_datetime(client, tm, X1205_CCR_BASE);
516 + case X1205_CMD_SETTIME:
517 + return x1205_set_datetime(client, tm, X1205_NODATE, X1205_CCR_BASE);
519 + case X1205_CMD_SETDATETIME:
520 + return x1205_set_datetime(client, tm, X1205_DATETOO, X1205_CCR_BASE);
522 + case X1205_CMD_GETALARM:
523 + return x1205_get_datetime(client, tm, X1205_ALM0_BASE);
525 + case X1205_CMD_SETALARM:
526 + return x1205_set_datetime(client, tm, X1205_DATETOO, X1205_ALM0_BASE);
534 +static int __init x1205_init(void)
536 + return i2c_add_driver(&x1205_driver);
539 +static void __exit x1205_exit(void)
541 + i2c_del_driver(&x1205_driver);
545 + "Karen Spearel <kas11@tampabay.rr.com>, "
546 + "Alessandro Zummo <a.zummo@towertech.it>");
547 +MODULE_DESCRIPTION("Xicor X1205 RTC driver");
548 +MODULE_LICENSE("GPL");
550 +EXPORT_SYMBOL_GPL(x1205_do_command);
552 +module_init(x1205_init);
553 +module_exit(x1205_exit);
554 --- linux-2.6.14-rc2/include/linux/x1205.h 1970-01-01 01:00:00.000000000 +0100
555 +++ test3/include/linux/x1205.h 2005-09-24 16:59:28.000000000 +0200
560 +#define X1205_CMD_GETDATETIME 0
561 +#define X1205_CMD_SETTIME 1
562 +#define X1205_CMD_SETDATETIME 2
563 +#define X1205_CMD_GETALARM 3
564 +#define X1205_CMD_SETALARM 4
568 +#define X1205_NODATE 0
569 +#define X1205_DATETOO 1
571 +/* offsets into read buf - add 2 for write buf */
582 +#define X1205_REG_SR 0x3F /* status register */
583 +#define X1205_REG_Y2K 0x37
584 +#define X1205_REG_DW 0x36
585 +#define X1205_REG_YR 0x35
586 +#define X1205_REG_MO 0x34
587 +#define X1205_REG_DT 0x33
588 +#define X1205_REG_HR 0x32
589 +#define X1205_REG_MN 0x31
590 +#define X1205_REG_SC 0x30
591 +#define X1205_REG_DTR 0x13
592 +#define X1205_REG_ATR 0x12
593 +#define X1205_REG_INT 0x11
594 +#define X1205_REG_0 0x10
595 +#define X1205_REG_Y2K1 0x0F
596 +#define X1205_REG_DWA1 0x0E
597 +#define X1205_REG_YRA1 0x0D
598 +#define X1205_REG_MOA1 0x0C
599 +#define X1205_REG_DTA1 0x0B
600 +#define X1205_REG_HRA1 0x0A
601 +#define X1205_REG_MNA1 0x09
602 +#define X1205_REG_SCA1 0x08
603 +#define X1205_REG_Y2K0 0x07
604 +#define X1205_REG_DWA0 0x06
605 +#define X1205_REG_YRA0 0x05
606 +#define X1205_REG_MOA0 0x04
607 +#define X1205_REG_DTA0 0x03
608 +#define X1205_REG_HRA0 0x02
609 +#define X1205_REG_MNA0 0x01
610 +#define X1205_REG_SCA0 0x00
612 +#define X1205_I2C_BUS_ADDR 0x6f /* hardwired into x1205 */
613 +#define X1205_CCR_BASE 0x30 /* Base address of CCR */
614 +#define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
616 +#define X1205_SR_WEL 0x02 /* Write Enable Latch bit */
617 +#define X1205_SR_RWEL 0x04 /* Register Write Enable Bit */
619 +#define X1205_HR_MIL 0x80 /* set in ccr.hour for 24 hr mode */
621 +extern int x1205_do_command(unsigned int cmd, void *arg);