i2c.c 7.92 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  linux/drivers/acorn/char/i2c.c
 *
 *  Copyright (C) 2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  ARM IOC/IOMD i2c driver.
 *
 *  On Acorn machines, the following i2c devices are on the bus:
 *	- PCF8583 real time clock & static RAM
 */
#include <linux/init.h>
#include <linux/sched.h>
17 18 19
#include <linux/time.h>
#include <linux/miscdevice.h>
#include <linux/rtc.h>
Linus Torvalds's avatar
Linus Torvalds committed
20 21
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
22
#include <linux/fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26 27

#include <asm/hardware.h>
#include <asm/io.h>
#include <asm/hardware/ioc.h>
#include <asm/system.h>
28
#include <asm/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34

#include "pcf8583.h"

extern int (*set_rtc)(void);

static struct i2c_client *rtc_client;
35 36 37 38
static const unsigned char days_in_mon[] = 
	{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

#define CMOS_CHECKSUM	(63)
39 40 41 42 43

/*
 * Acorn machines store the year in the static RAM at
 * location 128.
 */
44
#define CMOS_YEAR	(64 + 128)
Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52 53 54 55

static inline int rtc_command(int cmd, void *data)
{
	int ret = -EIO;

	if (rtc_client)
		ret = rtc_client->driver->command(rtc_client, cmd, data);

	return ret;
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/*
 * Update the century + year bytes in the CMOS RAM, ensuring
 * that the check byte is correctly adjusted for the change.
 */
static int rtc_update_year(unsigned int new_year)
{
	unsigned char yr[2], chk;
	struct mem cmos_year  = { CMOS_YEAR, sizeof(yr), yr };
	struct mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
	int ret;

	ret = rtc_command(MEM_READ, &cmos_check);
	if (ret)
		goto out;
	ret = rtc_command(MEM_READ, &cmos_year);
	if (ret)
		goto out;

	chk -= yr[1] + yr[0];

	yr[1] = new_year / 100;
	yr[0] = new_year % 100;

	chk += yr[1] + yr[0];

	ret = rtc_command(MEM_WRITE, &cmos_year);
	if (ret == 0)
		ret = rtc_command(MEM_WRITE, &cmos_check);
 out:
	return ret;
}

Linus Torvalds's avatar
Linus Torvalds committed
88 89 90
/*
 * Read the current RTC time and date, and update xtime.
 */
91
static void get_rtc_time(struct rtc_tm *rtctm, unsigned int *year)
Linus Torvalds's avatar
Linus Torvalds committed
92
{
93 94
	unsigned char ctrl, yr[2];
	struct mem rtcmem = { CMOS_YEAR, sizeof(yr), yr };
95
	int real_year, year_offset;
Linus Torvalds's avatar
Linus Torvalds committed
96 97 98 99 100 101

	/*
	 * Ensure that the RTC is running.
	 */
	rtc_command(RTC_GETCTRL, &ctrl);
	if (ctrl & 0xc0) {
102
		unsigned char new_ctrl = ctrl & ~0xc0;
Linus Torvalds's avatar
Linus Torvalds committed
103

104
		printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107 108 109
		       ctrl, new_ctrl);

		rtc_command(RTC_SETCTRL, &new_ctrl);
	}

110 111
	if (rtc_command(RTC_GETDATETIME, rtctm) ||
	    rtc_command(MEM_READ, &rtcmem))
Linus Torvalds's avatar
Linus Torvalds committed
112 113
		return;

114
	real_year = yr[0];
Linus Torvalds's avatar
Linus Torvalds committed
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129
	/*
	 * The RTC year holds the LSB two bits of the current
	 * year, which should reflect the LSB two bits of the
	 * CMOS copy of the year.  Any difference indicates
	 * that we have to correct the CMOS version.
	 */
	year_offset = rtctm->year_off - (real_year & 3);
	if (year_offset < 0)
		/*
		 * RTC year wrapped.  Adjust it appropriately.
		 */
		year_offset += 4;

	*year = real_year + year_offset + yr[1] * 100;
130 131 132 133
}

static int set_rtc_time(struct rtc_tm *rtctm, unsigned int year)
{
134
	unsigned char leap;
135 136 137 138
	int ret;

	leap = (!(year % 4) && (year % 100)) || !(year % 400);

139
	if (rtctm->mon > 12 || rtctm->mon == 0 || rtctm->mday == 0)
140 141 142 143 144 145 146
		return -EINVAL;

	if (rtctm->mday > (days_in_mon[rtctm->mon] + (rtctm->mon == 2 && leap)))
		return -EINVAL;

	if (rtctm->hours >= 24 || rtctm->mins >= 60 || rtctm->secs >= 60)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
147

148 149 150 151 152
	/*
	 * The RTC's own 2-bit year must reflect the least
	 * significant two bits of the CMOS year.
	 */
	rtctm->year_off = (year % 100) & 3;
153

154 155 156
	ret = rtc_command(RTC_SETDATETIME, rtctm);
	if (ret == 0)
		ret = rtc_update_year(year);
157 158

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161 162 163 164
}

/*
 * Set the RTC time only.  Note that
 * we do not touch the date.
 */
165
static int k_set_rtc_time(void)
Linus Torvalds's avatar
Linus Torvalds committed
166 167 168 169 170 171 172
{
	struct rtc_tm new_rtctm, old_rtctm;
	unsigned long nowtime = xtime.tv_sec;

	if (rtc_command(RTC_GETDATETIME, &old_rtctm))
		return 0;

173
	new_rtctm.cs    = xtime.tv_nsec / 10000000;
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178 179 180 181 182 183 184 185 186
	new_rtctm.secs  = nowtime % 60;	nowtime /= 60;
	new_rtctm.mins  = nowtime % 60;	nowtime /= 60;
	new_rtctm.hours = nowtime % 24;

	/*
	 * avoid writing when we're going to change the day
	 * of the month.  We will retry in the next minute.
	 * This basically means that if the RTC must not drift
	 * by more than 1 minute in 11 minutes.
	 *
	 * [ rtc: 1/1/2000 23:58:00, real 2/1/2000 00:01:00,
	 *   rtc gets set to 1/1/2000 00:01:00 ]
	 */
187 188
	if ((old_rtctm.hours == 23 && old_rtctm.mins == 59) ||
	    (new_rtctm.hours == 23 && new_rtctm.mins == 59))
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193
		return 1;

	return rtc_command(RTC_SETTIME, &new_rtctm);
}

194 195 196 197 198 199 200 201 202 203 204 205 206
static int rtc_ioctl(struct inode *inode, struct file *file,
		     unsigned int cmd, unsigned long arg)
{
	unsigned int year;
	struct rtc_time rtctm;
	struct rtc_tm rtc_raw;

	switch (cmd) {
	case RTC_ALM_READ:
	case RTC_ALM_SET:
		break;

	case RTC_RD_TIME:
Andrew Morton's avatar
Andrew Morton committed
207
		memset(&rtctm, 0, sizeof(struct rtc_time));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		get_rtc_time(&rtc_raw, &year);
		rtctm.tm_sec  = rtc_raw.secs;
		rtctm.tm_min  = rtc_raw.mins;
		rtctm.tm_hour = rtc_raw.hours;
		rtctm.tm_mday = rtc_raw.mday;
		rtctm.tm_mon  = rtc_raw.mon - 1; /* month starts at 0 */
		rtctm.tm_year = year - 1900; /* starts at 1900 */
		return copy_to_user((void *)arg, &rtctm, sizeof(rtctm))
				 ? -EFAULT : 0;

	case RTC_SET_TIME:
		if (!capable(CAP_SYS_TIME))
			return -EACCES;

		if (copy_from_user(&rtctm, (void *)arg, sizeof(rtctm)))
			return -EFAULT;
		rtc_raw.secs     = rtctm.tm_sec;
		rtc_raw.mins     = rtctm.tm_min;
		rtc_raw.hours    = rtctm.tm_hour;
		rtc_raw.mday     = rtctm.tm_mday;
		rtc_raw.mon      = rtctm.tm_mon + 1;
		year             = rtctm.tm_year + 1900;
		return set_rtc_time(&rtc_raw, year);
		break;

	case RTC_EPOCH_READ:
234
		return put_user(1900, (unsigned long *)arg);
235 236 237 238 239 240

	}
	return -EINVAL;
}

static struct file_operations rtc_fops = {
241
	.ioctl	= rtc_ioctl,
242 243 244
};

static struct miscdevice rtc_dev = {
245 246 247
	.minor	= RTC_MINOR,
	.name	= "rtc",
	.fops	= &rtc_fops,
248 249 250
};

/* IOC / IOMD i2c driver */
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254 255

#define FORCE_ONES	0xdc
#define SCL		0x02
#define SDA		0x01

Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260 261 262
/*
 * We must preserve all non-i2c output bits in IOC_CONTROL.
 * Note also that we need to preserve the value of SCL and
 * SDA outputs as well (which may be different from the
 * values read back from IOC_CONTROL).
 */
static u_int force_ones;
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265

static void ioc_setscl(void *data, int state)
{
Linus Torvalds's avatar
Linus Torvalds committed
266 267 268
	u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
	u_int ones = force_ones;

Linus Torvalds's avatar
Linus Torvalds committed
269
	if (state)
Linus Torvalds's avatar
Linus Torvalds committed
270
		ones |= SCL;
Linus Torvalds's avatar
Linus Torvalds committed
271
	else
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275 276
		ones &= ~SCL;

	force_ones = ones;

 	ioc_writeb(ioc_control | ones, IOC_CONTROL);
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280
}

static void ioc_setsda(void *data, int state)
{
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283
	u_int ioc_control = ioc_readb(IOC_CONTROL) & ~(SCL | SDA);
	u_int ones = force_ones;

Linus Torvalds's avatar
Linus Torvalds committed
284
	if (state)
Linus Torvalds's avatar
Linus Torvalds committed
285
		ones |= SDA;
Linus Torvalds's avatar
Linus Torvalds committed
286
	else
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289 290 291
		ones &= ~SDA;

	force_ones = ones;

 	ioc_writeb(ioc_control | ones, IOC_CONTROL);
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295
}

static int ioc_getscl(void *data)
{
Linus Torvalds's avatar
Linus Torvalds committed
296
	return (ioc_readb(IOC_CONTROL) & SCL) != 0;
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300
}

static int ioc_getsda(void *data)
{
Linus Torvalds's avatar
Linus Torvalds committed
301
	return (ioc_readb(IOC_CONTROL) & SDA) != 0;
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304
}

static struct i2c_algo_bit_data ioc_data = {
305 306 307 308 309 310 311
	.setsda		= ioc_setsda,
	.setscl		= ioc_setscl,
	.getsda		= ioc_getsda,
	.getscl		= ioc_getscl,
	.udelay		= 80,
	.mdelay		= 80,
	.timeout	= 100
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316 317
};

static int ioc_client_reg(struct i2c_client *client)
{
	if (client->id == I2C_DRIVERID_PCF8583 &&
	    client->addr == 0x50) {
318 319
		struct rtc_tm rtctm;
		unsigned int year;
320
		struct timespec tv;
321

Linus Torvalds's avatar
Linus Torvalds committed
322
		rtc_client = client;
323 324
		get_rtc_time(&rtctm, &year);

325 326 327 328
		tv.tv_nsec = rtctm.cs * 10000000;
		tv.tv_sec  = mktime(year, rtctm.mon, rtctm.mday,
				    rtctm.hours, rtctm.mins, rtctm.secs);
		do_settimeofday(&tv);
329
		set_rtc = k_set_rtc_time;
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	}

	return 0;
}

static int ioc_client_unreg(struct i2c_client *client)
{
	if (client == rtc_client) {
		set_rtc = NULL;
		rtc_client = NULL;
	}

	return 0;
}

static struct i2c_adapter ioc_ops = {
346 347 348
	.id			= I2C_HW_B_IOC,
	.algo_data		= &ioc_data,
	.client_register	= ioc_client_reg,
349
	.client_unregister	= ioc_client_unreg,
Linus Torvalds's avatar
Linus Torvalds committed
350 351 352 353
};

static int __init i2c_ioc_init(void)
{
354 355
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
356
	force_ones = FORCE_ONES | SCL | SDA;
Linus Torvalds's avatar
Linus Torvalds committed
357

358 359
	ret = i2c_bit_add_bus(&ioc_ops);

360 361 362 363 364
	if (ret >= 0){
		ret = misc_register(&rtc_dev);
		if(ret < 0)
			i2c_bit_del_bus(&ioc_ops);
	}
365 366

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369
}

__initcall(i2c_ioc_init);