]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/linux/nslu2-kernel/2.6.13/30-i2c-x1205.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / linux / nslu2-kernel / 2.6.13 / 30-i2c-x1205.patch
1 diff -urN linux-2.6.13.1/drivers/i2c/chips/Kconfig nslu2-2.6.13.1/drivers/i2c/chips/Kconfig
2 --- linux-2.6.13.1/drivers/i2c/chips/Kconfig    2005-09-17 12:42:33.000000000 +0200
3 +++ nslu2-2.6.13.1/drivers/i2c/chips/Kconfig    2005-09-17 13:46:42.000000000 +0200
4 @@ -136,4 +136,14 @@
5           This driver can also be built as a module.  If so, the module
6           will be called max6875.
7  
8 +config SENSORS_X1205
9 +       tristate "Xicor X1205 RTC chip"
10 +       depends on I2C && EXPERIMENTAL
11 +       select I2C_SENSOR
12 +       help
13 +         If you say yes here you get support for the Xicor x1205 RTC chip.
14 +
15 +         This driver can also be built as a module.  If so, the module
16 +         will be called x1205-rtc
17 +
18  endmenu
19 diff -urN linux-2.6.13.1/drivers/i2c/chips/Makefile nslu2-2.6.13.1/drivers/i2c/chips/Makefile
20 --- linux-2.6.13.1/drivers/i2c/chips/Makefile   2005-09-17 12:42:33.000000000 +0200
21 +++ nslu2-2.6.13.1/drivers/i2c/chips/Makefile   2005-09-17 13:35:39.000000000 +0200
22 @@ -13,6 +13,7 @@
23  obj-$(CONFIG_SENSORS_RTC8564)  += rtc8564.o
24  obj-$(CONFIG_ISP1301_OMAP)     += isp1301_omap.o
25  obj-$(CONFIG_TPS65010)         += tps65010.o
26 +obj-$(CONFIG_SENSORS_X1205)     += x1205-rtc.o
27  
28  ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
29  EXTRA_CFLAGS += -DDEBUG
30 diff -urN linux-2.6.13.1/drivers/i2c/chips/x1205-rtc.c nslu2-2.6.13.1/drivers/i2c/chips/x1205-rtc.c
31 --- linux-2.6.13.1/drivers/i2c/chips/x1205-rtc.c        1970-01-01 01:00:00.000000000 +0100
32 +++ nslu2-2.6.13.1/drivers/i2c/chips/x1205-rtc.c        2005-09-17 13:51:03.000000000 +0200
33 @@ -0,0 +1,547 @@
34 +/*
35 +    x1205 - an 12c driver for the Xicor X1205 RTC
36 +    Copyright 2004 Karen Spearel
37 +
38 +    please send all reports to:
39 +       kas11 at tampabay dot rr dot com
40 +      
41 +    based on linux/drivers/acron/char/pcf8583.h
42 +    Copyright (C) 2000 Russell King
43 +    
44 +    This program is free software; you can redistribute it and/or modify
45 +    it under the terms of the GNU General Public License as published by
46 +    the Free Software Foundation; either version 2 of the License, or
47 +    (at your option) any later version.
48 +
49 +    This program is distributed in the hope that it will be useful,
50 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
51 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52 +    GNU General Public License for more details.
53 +
54 +    You should have received a copy of the GNU General Public License
55 +    along with this program; if not, write to the Free Software
56 +    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
57 +*/
58 +/*
59 +
60 + * i2c_adapter is the structure used to identify a physical i2c bus along
61 + * with the access algorithms necessary to access it.
62 +
63 +struct i2c_adapter {
64 +       struct module *owner;
65 +       unsigned int id;  == is algo->id | hwdep.struct->id, for registered values see below
66 +       unsigned int class;
67 +       struct i2c_algorithm *algo; the algorithm to access the bus
68 +       void *algo_data;
69 +
70 +       --- administration stuff.
71 +       int (*client_register)(struct i2c_client *);
72 +       int (*client_unregister)(struct i2c_client *);
73 +
74 +        data fields that are valid for all devices
75 +       struct semaphore bus_lock;
76 +       struct semaphore clist_lock;
77 +
78 +       int timeout;
79 +       int retries;
80 +       struct device dev;              the adapter device 
81 +       struct class_device class_dev;  the class device
82 +
83 +#ifdef CONFIG_PROC_FS 
84 +       No need to set this when you initialize the adapter
85 +       int inode;
86 +#endif def CONFIG_PROC_FS
87 +
88 +       int nr;
89 +       struct list_head clients;
90 +       struct list_head list;
91 +       char name[I2C_NAME_SIZE];
92 +       struct completion dev_released;
93 +       struct completion class_dev_released;
94 +};
95 +*/
96 +
97 +
98 +/*========== Driver for the X1205 on the Linksys NSLU2 ==================*/
99 +
100 +#include <linux/init.h>
101 +#include <linux/i2c.h>
102 +#include <linux/slab.h>
103 +#include <linux/string.h>
104 +#include <linux/errno.h>
105 +#include <linux/bcd.h>
106 +#include <linux/rtc.h>
107 +#include <linux/fs.h>
108 +#include <linux/proc_fs.h>
109 +#include <linux/miscdevice.h>
110 +#include <linux/device.h>
111 +#include <asm/uaccess.h>
112 +#include <asm/system.h>
113 +#include <linux/moduleparam.h>
114 +
115 +#define                RTC_GETDATETIME         0
116 +#define                RTC_SETTIME             1
117 +#define                RTC_SETDATETIME         2
118 +
119 +#define                I2C_M_WR                0       // just for consistancy
120 +
121 +//  offsets into read buf - add 2 for write buf
122 +#define                CCR_SEC                 0
123 +#define                CCR_MIN                 1
124 +#define                CCR_HOUR                2
125 +#define                CCR_MDAY                3
126 +#define                CCR_MONTH               4
127 +#define                CCR_YEAR                5
128 +#define                CCR_WDAY                6
129 +#define                CCR_Y2K                 7
130 +
131 +#define                X1205_I2C_BUS_ADDR      0x6f    // hardwired into x1205
132 +#define                X1205_ALM0_BASE         0x00    // Base address of the ALM0
133 +#define                X1205_CCR_BASE          0x30    // Base address of the CCR
134 +#define                X1205_SR_ADDR           0x3f    // Status Register
135 +#define                X1205_SR_WEL            0x02    // Write Enable Latch bit
136 +#define                X1205_SR_RWEL           0x04    // Register Write Enable Bit
137 +#define                X1205_MILBIT            0x80    // this bit set in ccr.hour for 24 hr mode
138 +#define                NOERR                   0
139 +#define                RTC_NODATE              0
140 +#define                RTC_DATETOO             1
141 +
142 +// comment out next line is your x1205 can't do page writes
143 +//#define      X1205PAGEWRITE          1
144 +#ifdef X1205PAGEWRITE
145 +#define                DRIVERNAME              "Xicor x1205 RTC Driver v0.9.3.3"
146 +#else
147 +#define                DRIVERNAME              "Xicor x1205 RTC Dvr v0.9.3.3NPW"
148 +#endif
149 +
150 +#define                DEBUG                   KERN_DEBUG
151 +
152 +
153 +static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm, u8 reg_base);
154 +static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, int datetoo, u8 reg_base);
155 +static int x1205_attach(struct i2c_adapter *adapter);
156 +static int x1205_detach(struct i2c_client *client);
157 +static int x1205_validate_tm(struct rtc_time *tm);
158 +static int x1205_command(struct i2c_client *client, unsigned int cmd, void *arg);
159 +static int x1205_sync_rtc(void);
160 +static int x1205_read(struct file *file, char *buf, size_t count, loff_t *ptr);
161 +static int x1205_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
162 +static int x1205_read_proc(char *buf, char **start, off_t off, int len, int *eof, void *data);
163 +
164 +static struct i2c_driver x1205_driver = {
165 +       .owner          = THIS_MODULE,
166 +       .name           = DRIVERNAME,
167 +       .id             = I2C_DRIVERID_EXP0,
168 +       .flags          = I2C_DF_NOTIFY,
169 +       .attach_adapter = &x1205_attach,                //we don't need to probe...x1205 is hardwired @ 0x6f
170 +       .detach_client  = &x1205_detach,
171 +       .command        = &x1205_command,               //this prolly never gets called...used internally tho
172 +};
173 +
174 +static struct i2c_client x1205_i2c_client = {
175 +       .flags          =       0,
176 +       .addr           =       X1205_I2C_BUS_ADDR,     // chip address - NOTE: 7bit
177 +       .adapter        =       NULL,                   // the adapter we sit on assigned in attach
178 +       .driver         =       &x1205_driver,          // and our access routines
179 +       .usage_count    =       0,                      // How many accesses currently to this client
180 +       .dev            =       {},                     // the device structure
181 +       .list           =       {},
182 +       .name           =       DRIVERNAME,
183 +       .released       =       {},
184 +};
185 +
186 +static struct file_operations rtc_fops = {
187 +       owner:          THIS_MODULE,
188 +       ioctl:          x1205_ioctl,
189 +       read:           x1205_read,
190 +};
191 +
192 +static struct miscdevice x1205_miscdev = {
193 +       .minor          = RTC_MINOR,
194 +       .name           = "rtc",
195 +       .fops           = &rtc_fops,
196 +};
197 +extern int (*set_rtc)(void);
198 +static unsigned epoch = 1900;          //coresponds to year 0
199 +static unsigned  rtc_epoch = 2000;
200 +static const unsigned char days_in_mo[] = 
201 +{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
202 +
203 +//===================================CODE======================================
204 +// in the routines that deal directly with the x1205 hardware, we use
205 +// rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
206 +// Epoch is inited as 2000. Time is set to UT
207 +//=============================================================================   
208 +static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm, u8 reg_base)
209 +{
210 +       static unsigned char addr[2] = { 0,} ;
211 +       unsigned char buf[8];   
212 +       struct i2c_msg msgs[2] = {
213 +               { client->addr, I2C_M_WR, 2, addr },    //msg 1 = send base address
214 +               { client->addr, I2C_M_RD, 8, buf },     //msg 2 = read sequential data
215 +       };
216 +       addr[1] = reg_base;
217 +       if ((i2c_transfer(client->adapter, msgs, 2)) == 2) {    //did we read 2 messages?
218 +               printk(KERN_DEBUG "raw x1205 read data  - sec-%02x min-%02x hr-%02x mday-%02x mon-%02x year-%02x wday-%02x y2k-%02x\n", 
219 +                       buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6], buf[7]);
220 +               tm->tm_sec  = BCD2BIN(buf[CCR_SEC]);
221 +               tm->tm_min  = BCD2BIN(buf[CCR_MIN]);
222 +               buf[CCR_HOUR] &= ~X1205_MILBIT;
223 +               tm->tm_hour = BCD2BIN(buf[CCR_HOUR]);           //hr is 0-23
224 +               tm->tm_mday = BCD2BIN(buf[CCR_MDAY]);
225 +               tm->tm_mon  = BCD2BIN(buf[CCR_MONTH]);
226 +               rtc_epoch   = BCD2BIN(buf[CCR_Y2K]) * 100;
227 +               tm->tm_year = BCD2BIN(buf[CCR_YEAR]) + rtc_epoch - epoch;
228 +               tm->tm_wday = buf[CCR_WDAY];
229 +               printk(KERN_DEBUG "rtc_time output data - sec-%02d min-%02d hr-%02d mday-%02d mon-%02d year-%02d wday-%02d epoch-%d rtc_epoch-%d\n",
230 +                       tm->tm_sec,tm->tm_min,tm->tm_hour,tm->tm_mday,tm->tm_mon,tm->tm_year,tm->tm_wday,epoch, rtc_epoch);
231 +       } else {
232 +               printk(KERN_DEBUG "i2c_transfer Read Error\n");
233 +               return -EIO;
234 +       }               
235 +       
236 +       return NOERR;
237 +}
238 +// x1205pagewrite allows writing a block of registers in msg3 even though the x1205 says
239 +// nothing about this in its spec. 
240 +// it needs more testing as it is possible some x1205s are actually not-completely-
241 +// functional x1226s and there is a reason for the multiple write to not be in the spec.
242 +// anyhow, it is enabled for the time being...and we even push out luck by sending 10 bytes
243 +
244 +static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm, int datetoo, u8 reg_base)
245 +{
246 +       static unsigned char wel[3]   = { 0, X1205_SR_ADDR, X1205_SR_WEL };
247 +       static unsigned char rwel[3]  = { 0, X1205_SR_ADDR, X1205_SR_WEL | X1205_SR_RWEL };
248 +       static unsigned char diswe[3] = { 0, X1205_SR_ADDR, 0 };
249 +
250 +#ifdef X1205PAGEWRITE
251 +
252 +       static unsigned char buf[10]   = { 0, X1205_CCR_BASE, };                
253 +       struct i2c_msg msgs[4] = {
254 +               { client->addr, I2C_M_WR, 3, wel   },   //msg 1 = write WEL to to ccr sr
255 +               { client->addr, I2C_M_WR, 3, rwel  },   //msg 2 = write RWEL to ccr sr
256 +               { client->addr, I2C_M_WR, 10, buf   },  //msg 3 = write ccr base addr +seq data
257 +               { client->addr, I2C_M_WR, 3, diswe },   //msg 4 = 0 to ccr sr to disable writes
258 +       };
259 +
260 +       msgs[2].len = 5;                                        // 5 bytes + addr to set time only
261 +       buf [1] = reg_base;
262 +       buf[CCR_SEC+2]  = BIN2BCD(tm->tm_sec);
263 +       buf[CCR_MIN+2]  = BIN2BCD(tm->tm_min);
264 +       buf[CCR_HOUR+2] = BIN2BCD(tm->tm_hour) | X1205_MILBIT; // set 24 hour format
265 +       if (datetoo == 1) {
266 +               buf[CCR_MDAY+2]  = BIN2BCD(tm->tm_mday);
267 +               buf[CCR_MONTH+2] = BIN2BCD(tm->tm_mon);         // input is 0-11        
268 +               buf[CCR_YEAR+2]  = BIN2BCD((tm->tm_year + epoch - rtc_epoch));  // input is yrs since 1900
269 +               buf[CCR_WDAY+2]  = tm->tm_wday & 7;
270 +               buf[CCR_Y2K+2]   = BIN2BCD((rtc_epoch/100));
271 +               msgs[2].len += 5;                               //5 more bytes to set date
272 +       }
273 +       printk(KERN_DEBUG "rtc_time input - sec-%02d min-%02d hour-%02d mday-%02d mon-%02d year-%02d wday-%02d epoch-%d rtc_epoch-%d\n",
274 +               tm->tm_sec,tm->tm_min,tm->tm_hour,tm->tm_mday,tm->tm_mon,tm->tm_year,tm->tm_wday, epoch, rtc_epoch);
275 +       printk(KERN_DEBUG "BCD write data - sec-%02x min-%02x hour-%02x mday-%02x mon-%02x year-%02x wday-%02x y2k-%02x\n",
276 +               buf[2],buf[3],buf[4],buf[5],buf[6], buf[7], buf[8], buf[9]);
277 +
278 +       if ((i2c_transfer(client->adapter, msgs, 4)) != 4)
279 +               return -EIO;
280 +       return NOERR;
281 +       
282 +#else          //do this if page writes aren't working
283 +
284 +       int i,xfer;
285 +       static unsigned char data[3]  = { 0,};
286 +       static unsigned char buf[8];
287 +
288 +       buf[CCR_SEC]  = BIN2BCD(tm->tm_sec);
289 +       buf[CCR_MIN]  = BIN2BCD(tm->tm_min);
290 +       buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_MILBIT; // set 24 hour format
291 +       if (datetoo == 1) {
292 +               buf[CCR_MDAY]  = BIN2BCD(tm->tm_mday);
293 +               buf[CCR_MONTH] = BIN2BCD(tm->tm_mon);           // input is 0-11        
294 +               buf[CCR_YEAR]  = BIN2BCD((tm->tm_year + epoch - rtc_epoch));    // input is yrs since 1900
295 +               buf[CCR_WDAY]  = tm->tm_wday & 7;
296 +               buf[CCR_Y2K]   = BIN2BCD((rtc_epoch/100));
297 +       }
298 +       printk(KERN_DEBUG "rtc_time input - sec-%02d min-%02d hour-%02d mday-%02d mon-%02d year-%02d wday-%02d epoch-%d rtc_epoch-%d\n",
299 +               tm->tm_sec,tm->tm_min,tm->tm_hour,tm->tm_mday,tm->tm_mon,tm->tm_year,tm->tm_wday, epoch, rtc_epoch);
300 +
301 +       xfer = i2c_master_send(client, wel, 3);
302 +       printk(KERN_DEBUG "wen - %x\n", xfer);
303 +       if (xfer != 3)
304 +               return -EIO;
305 +
306 +       xfer = i2c_master_send(client, rwel, 3);
307 +       printk(KERN_DEBUG "wenb - %x\n", xfer);
308 +       if (xfer != 3)
309 +               return -EIO;
310 +
311 +       for (i = 0; i < 8; i++) {
312 +               data[1] = i + reg_base;
313 +               data[2] =  buf[i];
314 +               xfer = i2c_master_send(client, data, 3);
315 +               printk(KERN_DEBUG "xfer - %d addr - %02x  data - %02x\n", xfer, data[1], data[2]);
316 +               if (xfer != 3)
317 +                       return -EIO;
318 +       };
319 +
320 +       xfer = i2c_master_send(client, diswe, 3);
321 +       printk(KERN_DEBUG "wdis - %x\n", xfer);
322 +       if (xfer != 3)
323 +               return -EIO;            
324 +       return NOERR;
325 +#endif
326 +}
327 +//=============================================================================
328 +
329 +static int x1205_attach(struct i2c_adapter *adapter)
330 +{
331 +       struct rtc_time tm;
332 +       struct timespec tv;
333 +       int errno;
334 +               
335 +       x1205_i2c_client.adapter = adapter;
336 +//     x1205_i2c_client.id++;
337 +
338 +       if ((x1205_get_datetime(&x1205_i2c_client, &tm, X1205_CCR_BASE)) != NOERR)      //test for functional driver 
339 +               return -EIO;
340 +       
341 +       if ((errno = i2c_attach_client(&x1205_i2c_client)) != NOERR)
342 +               return errno;
343 +
344 +       tv.tv_nsec = tm.tm_sec * 10000000;
345 +       tv.tv_sec  = mktime(tm.tm_year+epoch, tm.tm_mon, tm.tm_mday, tm.tm_hour,
346 +                                               tm.tm_min, tm.tm_sec);
347 +       do_settimeofday(&tv);
348 +       set_rtc = x1205_sync_rtc;
349 +       
350 +       printk(KERN_DEBUG "%s attached on adapter %s\n",x1205_i2c_client.name,
351 +               x1205_i2c_client.adapter->name); //why is this name a null string?
352 +
353 +       return NOERR;
354 +}
355 +
356 +static int x1205_detach(struct i2c_client *client)
357 +{
358 +       int errno;
359 +       
360 +       if ((errno = i2c_detach_client(client)) != 0) {
361 +               printk(KERN_DEBUG "i2c_detach failed - errno = %d\n", errno);
362 +               return errno;
363 +       }
364 +
365 +       return NOERR;
366 +}
367 +
368 +// make sure the rtc_time values are in bounds
369 +static int x1205_validate_tm(struct rtc_time *tm)
370 +{
371 +       tm->tm_year += 1900;
372 +
373 +       if (tm->tm_year < 1970)
374 +               return -EINVAL;
375 +
376 +       if ((tm->tm_mon > 11) || (tm->tm_mday == 0))
377 +               return -EINVAL;
378 +
379 +       if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ( (tm->tm_mon == 1) && 
380 +               ((!(tm->tm_year % 4) && (tm->tm_year % 100) ) || !(tm->tm_year % 400)))))
381 +               return -EINVAL;
382 +
383 +       if ((tm->tm_year -= epoch) > 255)
384 +               return -EINVAL;
385 +                       
386 +       if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
387 +               return -EINVAL;
388 +       return NOERR;
389 +}
390 +
391 +static int x1205_command(struct i2c_client *client, unsigned int cmd, void *tm)
392 +{
393 +       int errno, dodate = RTC_DATETOO;
394 +
395 +       if (client == NULL || tm == NULL)
396 +               return -EINVAL;
397 +       if (!capable(CAP_SYS_TIME))
398 +               return -EACCES;
399 +
400 +       printk(KERN_DEBUG "x1205_command %d\n", cmd);
401 +
402 +       switch (cmd) {
403 +       case RTC_GETDATETIME:
404 +               return x1205_get_datetime(client, tm, X1205_CCR_BASE);
405 +
406 +       case RTC_SETTIME:               // note fall thru
407 +               dodate = RTC_NODATE;
408 +       case RTC_SETDATETIME:
409 +               if ((errno = x1205_validate_tm(tm)) < NOERR)
410 +                       return errno;
411 +               return x1205_set_datetime(client, tm, dodate, X1205_CCR_BASE);
412 +
413 +       default:
414 +               return -EINVAL;
415 +       }
416 +}
417 +
418 +static int x1205_sync_rtc(void)
419 +{
420 +       struct rtc_time new_tm, old_tm;
421 +       unsigned long cur_secs = xtime.tv_sec;
422 +
423 +       printk(KERN_DEBUG "x1205_sync_rtc entry\n");
424 +
425 +       if (x1205_command(&x1205_i2c_client, RTC_GETDATETIME, &old_tm))
426 +               return 0;
427 +
428 +//     xtime.tv_nsec = old_tm.tm_sec * 10000000;   //FIXME:
429 +       new_tm.tm_sec  = cur_secs % 60;
430 +       cur_secs /= 60;
431 +       new_tm.tm_min  = cur_secs % 60;
432 +       cur_secs /= 60;
433 +       new_tm.tm_hour = cur_secs % 24;
434 +
435 +       /*
436 +        * avoid writing when we're going to change the day
437 +        * of the month.  We will retry in the next minute.
438 +        * This basically means that if the RTC must not drift
439 +        * by more than 1 minute in 11 minutes.
440 +        */
441 +       if ((old_tm.tm_hour == 23 && old_tm.tm_min == 59) ||
442 +           (new_tm.tm_hour == 23 && new_tm.tm_min == 59))
443 +               return 1;
444 +       printk(KERN_DEBUG "x1205_sync_rtc exit\n");
445 +
446 +       return x1205_command(&x1205_i2c_client, RTC_SETTIME, &new_tm);
447 +}
448 +
449 +static int x1205_read(struct file *file, char *buf, size_t count, loff_t *ptr)
450 +{
451 +       struct rtc_time tm;
452 +
453 +       if ((x1205_get_datetime(&x1205_i2c_client, &tm, X1205_CCR_BASE)) < NOERR)
454 +               return -EIO;
455 +       return copy_to_user(buf, &tm, sizeof(tm)) ? -EFAULT : NOERR;
456 +}
457 +
458 +//==============================================================================
459 +
460 +static int x1205_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
461 +                    unsigned long arg)
462 +{
463 +       struct rtc_time tm;
464 +       int errno;
465 +
466 +       printk(KERN_DEBUG "ioctl = %x\n", cmd);
467 +       
468 +       switch (cmd) {
469 +       case RTC_RD_TIME:
470 +               if ((x1205_get_datetime(&x1205_i2c_client, &tm, X1205_CCR_BASE)) < NOERR)
471 +                       return -EIO;
472 +               break;
473 +               
474 +       case RTC_SET_TIME:
475 +               if (!capable(CAP_SYS_TIME))
476 +                       return -EACCES;
477 +
478 +               if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time))) 
479 +                       return -EFAULT;
480 +               if ((errno = x1205_validate_tm(&tm)) < NOERR)
481 +                       return errno;
482 +               return x1205_set_datetime(&x1205_i2c_client, &tm, RTC_DATETOO, X1205_CCR_BASE);
483 +
484 +       case RTC_ALM_SET:                                               //FIXME: set Control Regs
485 +               if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time))) 
486 +                       return -EFAULT;
487 +               return x1205_set_datetime(&x1205_i2c_client, &tm, RTC_DATETOO, X1205_ALM0_BASE);
488 +
489 +       case RTC_ALM_READ:
490 +               if ((x1205_get_datetime(&x1205_i2c_client, &tm, X1205_ALM0_BASE)) < NOERR)
491 +                       return -EIO;
492 +               break;
493 +
494 +       case RTC_EPOCH_READ:
495 +
496 +               return put_user (epoch, (unsigned long __user *)arg);
497 +
498 +       case RTC_EPOCH_SET:
499 +               if (arg < 1900)
500 +                       return -EINVAL;
501 +
502 +               if (!capable(CAP_SYS_TIME))
503 +                       return -EACCES;
504 +
505 +               epoch = arg;
506 +               return 0;
507 +
508 +       default:
509 +               return -ENOTTY;
510 +       }
511 +       return copy_to_user((void __user *)arg, &tm, sizeof tm) ? -EFAULT : 0;
512 +
513 +}
514 +
515 +static int x1205_read_proc(char *buf, char **start, off_t off, int len, int *eof, void *data)
516 +{
517 +       struct rtc_time tm;
518 +       int slen, errno;
519 +
520 +       if ((errno = x1205_get_datetime(&x1205_i2c_client, &tm, X1205_CCR_BASE)) < NOERR)
521 +               return errno;
522 +
523 +//     here we return the real year and the month as 1-12 since it is human-readable
524 +       slen = sprintf(buf, "rtc_time\t: %02d:%02d:%02d\nrtc_date\t: %04d-%02d-%02d\n",
525 +               tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year + 1900, tm.tm_mon+1, tm.tm_mday);
526 +       printk(KERN_DEBUG "raw rtc_time\t: %02d:%02d:%02d\nraw rtc_date\t: %04d-%02d-%02d\n",
527 +               tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year, tm.tm_mon, tm.tm_mday);
528 +
529 +       if (slen <= off + len)
530 +               *eof = 1;
531 +       *start = buf + off;
532 +       slen -= off;
533 +       if (slen > len)
534 +               slen = len;
535 +       if ( slen < 0 )
536 +               slen = 0;
537 +
538 +       return slen;
539 +}
540 +
541 +static int __init x1205_init(void)
542 +{
543 +       struct  rtc_time tm;
544 +       int errno;
545 +       printk(KERN_INFO "LOADED %s\n", DRIVERNAME);
546 +
547 +       if ((errno = i2c_add_driver(&x1205_driver)) != NOERR) {
548 +               dev_dbg(x1205_i2c_client.dev, "x1205_init failed - errno = %d\n", errno);
549 +               return (errno);
550 +       }
551 +       if ((errno = misc_register(&x1205_miscdev)) != NOERR) {
552 +               dev_dbg(x1205_i2c_client.dev, "Register Misc Driver failed - errno = %d\n", errno);
553 +               i2c_del_driver(&x1205_driver);
554 +               return errno; 
555 +       }
556 +       if (create_proc_read_entry("driver/rtc", 0, NULL, x1205_read_proc, NULL) < NOERR)
557 +               return -ENOMEM;
558 +       if ((x1205_get_datetime(&x1205_i2c_client, &tm, X1205_CCR_BASE)) != NOERR)      //test for functionality
559 +               return -EIO;
560 +
561 +       return NOERR;   
562 +}
563 +
564 +static void __exit x1205_exit(void)
565 +{
566 +       remove_proc_entry("driver/rtc", NULL);
567 +       misc_deregister(&x1205_miscdev);
568 +       i2c_del_driver(&x1205_driver);
569 +       set_rtc = NULL;
570 +}
571 +
572 +MODULE_AUTHOR("Karen Spearel <kas11@tampabay.rr.com>");
573 +MODULE_DESCRIPTION("Xicor X1205-RTC Driver");
574 +MODULE_LICENSE("GPL");
575 +static int debug = 7;
576 +module_param(debug, bool, 0644);
577 +MODULE_PARM_DESC(debug, "Debugging enabled = 1");
578 +
579 +module_init(x1205_init);
580 +module_exit(x1205_exit);