PageRenderTime 56ms CodeModel.GetById 11ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/acorn/char/i2c.c

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