Merge branch 'upstream'
[pandora-kernel.git] / drivers / acorn / char / i2c.c
1 /*
2  *  linux/drivers/acorn/char/i2c.c
3  *
4  *  Copyright (C) 2000 Russell King
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  *  ARM IOC/IOMD i2c driver.
11  *
12  *  On Acorn machines, the following i2c devices are on the bus:
13  *      - PCF8583 real time clock & static RAM
14  */
15 #include <linux/capability.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/time.h>
19 #include <linux/miscdevice.h>
20 #include <linux/rtc.h>
21 #include <linux/i2c.h>
22 #include <linux/i2c-algo-bit.h>
23 #include <linux/fs.h>
24
25 #include <asm/hardware.h>
26 #include <asm/io.h>
27 #include <asm/hardware/ioc.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30
31 #include "pcf8583.h"
32
33 extern int (*set_rtc)(void);
34
35 static struct i2c_client *rtc_client;
36 static const unsigned char days_in_mon[] = 
37         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
38
39 #define CMOS_CHECKSUM   (63)
40
41 /*
42  * Acorn machines store the year in the static RAM at
43  * location 128.
44  */
45 #define CMOS_YEAR       (64 + 128)
46
47 static inline int rtc_command(int cmd, void *data)
48 {
49         int ret = -EIO;
50
51         if (rtc_client)
52                 ret = rtc_client->driver->command(rtc_client, cmd, data);
53
54         return ret;
55 }
56
57 /*
58  * Update the century + year bytes in the CMOS RAM, ensuring
59  * that the check byte is correctly adjusted for the change.
60  */
61 static int rtc_update_year(unsigned int new_year)
62 {
63         unsigned char yr[2], chk;
64         struct mem cmos_year  = { CMOS_YEAR, sizeof(yr), yr };
65         struct mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
66         int ret;
67
68         ret = rtc_command(MEM_READ, &cmos_check);
69         if (ret)
70                 goto out;
71         ret = rtc_command(MEM_READ, &cmos_year);
72         if (ret)
73                 goto out;
74
75         chk -= yr[1] + yr[0];
76
77         yr[1] = new_year / 100;
78         yr[0] = new_year % 100;
79
80         chk += yr[1] + yr[0];
81
82         ret = rtc_command(MEM_WRITE, &cmos_year);
83         if (ret == 0)
84                 ret = rtc_command(MEM_WRITE, &cmos_check);
85  out:
86         return ret;
87 }
88
89 /*
90  * Read the current RTC time and date, and update xtime.
91  */
92 static void get_rtc_time(struct rtc_tm *rtctm, unsigned int *year)
93 {
94         unsigned char ctrl, yr[2];
95         struct mem rtcmem = { CMOS_YEAR, sizeof(yr), yr };
96         int real_year, year_offset;
97
98         /*
99          * Ensure that the RTC is running.
100          */
101         rtc_command(RTC_GETCTRL, &ctrl);
102         if (ctrl & 0xc0) {
103                 unsigned char new_ctrl = ctrl & ~0xc0;
104
105                 printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
106                        ctrl, new_ctrl);
107
108                 rtc_command(RTC_SETCTRL, &new_ctrl);
109         }
110
111         if (rtc_command(RTC_GETDATETIME, rtctm) ||
112             rtc_command(MEM_READ, &rtcmem))
113                 return;
114
115         real_year = yr[0];
116
117         /*
118          * The RTC year holds the LSB two bits of the current
119          * year, which should reflect the LSB two bits of the
120          * CMOS copy of the year.  Any difference indicates
121          * that we have to correct the CMOS version.
122          */
123         year_offset = rtctm->year_off - (real_year & 3);
124         if (year_offset < 0)
125                 /*
126                  * RTC year wrapped.  Adjust it appropriately.
127                  */
128                 year_offset += 4;
129
130         *year = real_year + year_offset + yr[1] * 100;
131 }
132
133 static int set_rtc_time(struct rtc_tm *rtctm, unsigned int year)
134 {
135         unsigned char leap;
136         int ret;
137
138         leap = (!(year % 4) && (year % 100)) || !(year % 400);
139
140         if (rtctm->mon > 12 || rtctm->mon == 0 || rtctm->mday == 0)
141                 return -EINVAL;
142
143         if (rtctm->mday > (days_in_mon[rtctm->mon] + (rtctm->mon == 2 && leap)))
144                 return -EINVAL;
145
146         if (rtctm->hours >= 24 || rtctm->mins >= 60 || rtctm->secs >= 60)
147                 return -EINVAL;
148
149         /*
150          * The RTC's own 2-bit year must reflect the least
151          * significant two bits of the CMOS year.
152          */
153         rtctm->year_off = (year % 100) & 3;
154
155         ret = rtc_command(RTC_SETDATETIME, rtctm);
156         if (ret == 0)
157                 ret = rtc_update_year(year);
158
159         return ret;
160 }
161
162 /*
163  * Set the RTC time only.  Note that
164  * we do not touch the date.
165  */
166 static int k_set_rtc_time(void)
167 {
168         struct rtc_tm new_rtctm, old_rtctm;
169         unsigned long nowtime = xtime.tv_sec;
170
171         if (rtc_command(RTC_GETDATETIME, &old_rtctm))
172                 return 0;
173
174         new_rtctm.cs    = xtime.tv_nsec / 10000000;
175         new_rtctm.secs  = nowtime % 60; nowtime /= 60;
176         new_rtctm.mins  = nowtime % 60; nowtime /= 60;
177         new_rtctm.hours = nowtime % 24;
178
179         /*
180          * avoid writing when we're going to change the day
181          * of the month.  We will retry in the next minute.
182          * This basically means that if the RTC must not drift
183          * by more than 1 minute in 11 minutes.
184          *
185          * [ rtc: 1/1/2000 23:58:00, real 2/1/2000 00:01:00,
186          *   rtc gets set to 1/1/2000 00:01:00 ]
187          */
188         if ((old_rtctm.hours == 23 && old_rtctm.mins == 59) ||
189             (new_rtctm.hours == 23 && new_rtctm.mins == 59))
190                 return 1;
191
192         return rtc_command(RTC_SETTIME, &new_rtctm);
193 }
194
195 static int rtc_ioctl(struct inode *inode, struct file *file,
196                      unsigned int cmd, unsigned long arg)
197 {
198         unsigned int year;
199         struct rtc_time rtctm;
200         struct rtc_tm rtc_raw;
201
202         switch (cmd) {
203         case RTC_ALM_READ:
204         case RTC_ALM_SET:
205                 break;
206
207         case RTC_RD_TIME:
208                 memset(&rtctm, 0, sizeof(struct rtc_time));
209                 get_rtc_time(&rtc_raw, &year);
210                 rtctm.tm_sec  = rtc_raw.secs;
211                 rtctm.tm_min  = rtc_raw.mins;
212                 rtctm.tm_hour = rtc_raw.hours;
213                 rtctm.tm_mday = rtc_raw.mday;
214                 rtctm.tm_mon  = rtc_raw.mon - 1; /* month starts at 0 */
215                 rtctm.tm_year = year - 1900; /* starts at 1900 */
216                 return copy_to_user((void *)arg, &rtctm, sizeof(rtctm))
217                                  ? -EFAULT : 0;
218
219         case RTC_SET_TIME:
220                 if (!capable(CAP_SYS_TIME))
221                         return -EACCES;
222
223                 if (copy_from_user(&rtctm, (void *)arg, sizeof(rtctm)))
224                         return -EFAULT;
225                 rtc_raw.secs     = rtctm.tm_sec;
226                 rtc_raw.mins     = rtctm.tm_min;
227                 rtc_raw.hours    = rtctm.tm_hour;
228                 rtc_raw.mday     = rtctm.tm_mday;
229                 rtc_raw.mon      = rtctm.tm_mon + 1;
230                 year             = rtctm.tm_year + 1900;
231                 return set_rtc_time(&rtc_raw, year);
232                 break;
233
234         case RTC_EPOCH_READ:
235                 return put_user(1900, (unsigned long *)arg);
236
237         }
238         return -EINVAL;
239 }
240
241 static struct file_operations rtc_fops = {
242         .ioctl  = rtc_ioctl,
243 };
244
245 static struct miscdevice rtc_dev = {
246         .minor  = RTC_MINOR,
247         .name   = "rtc",
248         .fops   = &rtc_fops,
249 };
250
251 /* IOC / IOMD i2c driver */
252
253 #define FORCE_ONES      0xdc
254 #define SCL             0x02
255 #define SDA             0x01
256
257 /*
258  * We must preserve all non-i2c output bits in IOC_CONTROL.
259  * Note also that we need to preserve the value of SCL and
260  * SDA outputs as well (which may be different from the
261  * values read back from IOC_CONTROL).
262  */
263 static u_int force_ones;
264
265 static void ioc_setscl(void *data, int state)
266 {
267         u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
268         u_int ones = force_ones;
269
270         if (state)
271                 ones |= SCL;
272         else
273                 ones &= ~SCL;
274
275         force_ones = ones;
276
277         ioc_writeb(ioc_control | ones, IOC_CONTROL);
278 }
279
280 static void ioc_setsda(void *data, int state)
281 {
282         u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
283         u_int ones = force_ones;
284
285         if (state)
286                 ones |= SDA;
287         else
288                 ones &= ~SDA;
289
290         force_ones = ones;
291
292         ioc_writeb(ioc_control | ones, IOC_CONTROL);
293 }
294
295 static int ioc_getscl(void *data)
296 {
297         return (ioc_readb(IOC_CONTROL) & SCL) != 0;
298 }
299
300 static int ioc_getsda(void *data)
301 {
302         return (ioc_readb(IOC_CONTROL) & SDA) != 0;
303 }
304
305 static struct i2c_algo_bit_data ioc_data = {
306         .setsda         = ioc_setsda,
307         .setscl         = ioc_setscl,
308         .getsda         = ioc_getsda,
309         .getscl         = ioc_getscl,
310         .udelay         = 80,
311         .mdelay         = 80,
312         .timeout        = 100
313 };
314
315 static int ioc_client_reg(struct i2c_client *client)
316 {
317         if (client->driver->id == I2C_DRIVERID_PCF8583 &&
318             client->addr == 0x50) {
319                 struct rtc_tm rtctm;
320                 unsigned int year;
321                 struct timespec tv;
322
323                 rtc_client = client;
324                 get_rtc_time(&rtctm, &year);
325
326                 tv.tv_nsec = rtctm.cs * 10000000;
327                 tv.tv_sec  = mktime(year, rtctm.mon, rtctm.mday,
328                                     rtctm.hours, rtctm.mins, rtctm.secs);
329                 do_settimeofday(&tv);
330                 set_rtc = k_set_rtc_time;
331         }
332
333         return 0;
334 }
335
336 static int ioc_client_unreg(struct i2c_client *client)
337 {
338         if (client == rtc_client) {
339                 set_rtc = NULL;
340                 rtc_client = NULL;
341         }
342
343         return 0;
344 }
345
346 static struct i2c_adapter ioc_ops = {
347         .id                     = I2C_HW_B_IOC,
348         .algo_data              = &ioc_data,
349         .client_register        = ioc_client_reg,
350         .client_unregister      = ioc_client_unreg,
351 };
352
353 static int __init i2c_ioc_init(void)
354 {
355         int ret;
356
357         force_ones = FORCE_ONES | SCL | SDA;
358
359         ret = i2c_bit_add_bus(&ioc_ops);
360
361         if (ret >= 0){
362                 ret = misc_register(&rtc_dev);
363                 if(ret < 0)
364                         i2c_bit_del_bus(&ioc_ops);
365         }
366
367         return ret;
368 }
369
370 __initcall(i2c_ioc_init);