| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips. | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) 2005 James Chapman (ds1337 core) | 
 | 5 |  *  Copyright (C) 2006 David Brownell | 
 | 6 |  * | 
 | 7 |  * This program is free software; you can redistribute it and/or modify | 
 | 8 |  * it under the terms of the GNU General Public License version 2 as | 
 | 9 |  * published by the Free Software Foundation. | 
 | 10 |  */ | 
 | 11 |  | 
 | 12 | #include <linux/module.h> | 
 | 13 | #include <linux/init.h> | 
 | 14 | #include <linux/slab.h> | 
 | 15 | #include <linux/i2c.h> | 
 | 16 | #include <linux/string.h> | 
 | 17 | #include <linux/rtc.h> | 
 | 18 | #include <linux/bcd.h> | 
 | 19 |  | 
 | 20 |  | 
 | 21 |  | 
 | 22 | /* We can't determine type by probing, but if we expect pre-Linux code | 
 | 23 |  * to have set the chip up as a clock (turning on the oscillator and | 
 | 24 |  * setting the date and time), Linux can ignore the non-clock features. | 
 | 25 |  * That's a natural job for a factory or repair bench. | 
 | 26 |  * | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 27 |  * This is currently a simple no-alarms driver.  If your board has the | 
 | 28 |  * alarm irq wired up on a ds1337 or ds1339, and you want to use that, | 
 | 29 |  * then look at the rtc-rs5c372 driver for code to steal... | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 30 |  */ | 
 | 31 | enum ds_type { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 32 | 	ds_1307, | 
 | 33 | 	ds_1337, | 
 | 34 | 	ds_1338, | 
 | 35 | 	ds_1339, | 
 | 36 | 	ds_1340, | 
 | 37 | 	m41t00, | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 38 | 	// rs5c372 too?  different address... | 
 | 39 | }; | 
 | 40 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 41 |  | 
 | 42 | /* RTC registers don't differ much, except for the century flag */ | 
 | 43 | #define DS1307_REG_SECS		0x00	/* 00-59 */ | 
 | 44 | #	define DS1307_BIT_CH		0x80 | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 45 | #	define DS1340_BIT_nEOSC		0x80 | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 46 | #define DS1307_REG_MIN		0x01	/* 00-59 */ | 
 | 47 | #define DS1307_REG_HOUR		0x02	/* 00-23, or 1-12{am,pm} */ | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 48 | #	define DS1307_BIT_12HR		0x40	/* in REG_HOUR */ | 
 | 49 | #	define DS1307_BIT_PM		0x20	/* in REG_HOUR */ | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 50 | #	define DS1340_BIT_CENTURY_EN	0x80	/* in REG_HOUR */ | 
 | 51 | #	define DS1340_BIT_CENTURY	0x40	/* in REG_HOUR */ | 
 | 52 | #define DS1307_REG_WDAY		0x03	/* 01-07 */ | 
 | 53 | #define DS1307_REG_MDAY		0x04	/* 01-31 */ | 
 | 54 | #define DS1307_REG_MONTH	0x05	/* 01-12 */ | 
 | 55 | #	define DS1337_BIT_CENTURY	0x80	/* in REG_MONTH */ | 
 | 56 | #define DS1307_REG_YEAR		0x06	/* 00-99 */ | 
 | 57 |  | 
 | 58 | /* Other registers (control, status, alarms, trickle charge, NVRAM, etc) | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 59 |  * start at 7, and they differ a LOT. Only control and status matter for | 
 | 60 |  * basic RTC date and time functionality; be careful using them. | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 61 |  */ | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 62 | #define DS1307_REG_CONTROL	0x07		/* or ds1338 */ | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 63 | #	define DS1307_BIT_OUT		0x80 | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 64 | #	define DS1338_BIT_OSF		0x20 | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 65 | #	define DS1307_BIT_SQWE		0x10 | 
 | 66 | #	define DS1307_BIT_RS1		0x02 | 
 | 67 | #	define DS1307_BIT_RS0		0x01 | 
 | 68 | #define DS1337_REG_CONTROL	0x0e | 
 | 69 | #	define DS1337_BIT_nEOSC		0x80 | 
 | 70 | #	define DS1337_BIT_RS2		0x10 | 
 | 71 | #	define DS1337_BIT_RS1		0x08 | 
 | 72 | #	define DS1337_BIT_INTCN		0x04 | 
 | 73 | #	define DS1337_BIT_A2IE		0x02 | 
 | 74 | #	define DS1337_BIT_A1IE		0x01 | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 75 | #define DS1340_REG_CONTROL	0x07 | 
 | 76 | #	define DS1340_BIT_OUT		0x80 | 
 | 77 | #	define DS1340_BIT_FT		0x40 | 
 | 78 | #	define DS1340_BIT_CALIB_SIGN	0x20 | 
 | 79 | #	define DS1340_M_CALIBRATION	0x1f | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 80 | #define DS1340_REG_FLAG		0x09 | 
 | 81 | #	define DS1340_BIT_OSF		0x80 | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 82 | #define DS1337_REG_STATUS	0x0f | 
 | 83 | #	define DS1337_BIT_OSF		0x80 | 
 | 84 | #	define DS1337_BIT_A2I		0x02 | 
 | 85 | #	define DS1337_BIT_A1I		0x01 | 
 | 86 | #define DS1339_REG_TRICKLE	0x10 | 
 | 87 |  | 
 | 88 |  | 
 | 89 |  | 
 | 90 | struct ds1307 { | 
 | 91 | 	u8			reg_addr; | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 92 | 	bool			has_nvram; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 93 | 	u8			regs[8]; | 
 | 94 | 	enum ds_type		type; | 
 | 95 | 	struct i2c_msg		msg[2]; | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 96 | 	struct i2c_client	*client; | 
 | 97 | 	struct i2c_client	dev; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 98 | 	struct rtc_device	*rtc; | 
 | 99 | }; | 
 | 100 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 101 | struct chip_desc { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 102 | 	unsigned		nvram56:1; | 
 | 103 | 	unsigned		alarm:1; | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 104 | }; | 
 | 105 |  | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 106 | static const struct chip_desc chips[] = { | 
 | 107 | [ds_1307] = { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 108 | 	.nvram56	= 1, | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 109 | }, | 
 | 110 | [ds_1337] = { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 111 | 	.alarm		= 1, | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 112 | }, | 
 | 113 | [ds_1338] = { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 114 | 	.nvram56	= 1, | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 115 | }, | 
 | 116 | [ds_1339] = { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 117 | 	.alarm		= 1, | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 118 | }, | 
 | 119 | [ds_1340] = { | 
 | 120 | }, | 
 | 121 | [m41t00] = { | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 122 | }, }; | 
 | 123 |  | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 124 | static const struct i2c_device_id ds1307_id[] = { | 
 | 125 | 	{ "ds1307", ds_1307 }, | 
 | 126 | 	{ "ds1337", ds_1337 }, | 
 | 127 | 	{ "ds1338", ds_1338 }, | 
 | 128 | 	{ "ds1339", ds_1339 }, | 
 | 129 | 	{ "ds1340", ds_1340 }, | 
 | 130 | 	{ "m41t00", m41t00 }, | 
 | 131 | 	{ } | 
 | 132 | }; | 
 | 133 | MODULE_DEVICE_TABLE(i2c, ds1307_id); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 134 |  | 
 | 135 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) | 
 | 136 | { | 
 | 137 | 	struct ds1307	*ds1307 = dev_get_drvdata(dev); | 
 | 138 | 	int		tmp; | 
 | 139 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 140 | 	/* read the RTC date and time registers all at once */ | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 141 | 	ds1307->msg[1].flags = I2C_M_RD; | 
 | 142 | 	ds1307->msg[1].len = 7; | 
 | 143 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 144 | 	tmp = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent), | 
 | 145 | 			ds1307->msg, 2); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 146 | 	if (tmp != 2) { | 
 | 147 | 		dev_err(dev, "%s error %d\n", "read", tmp); | 
 | 148 | 		return -EIO; | 
 | 149 | 	} | 
 | 150 |  | 
 | 151 | 	dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", | 
 | 152 | 			"read", | 
 | 153 | 			ds1307->regs[0], ds1307->regs[1], | 
 | 154 | 			ds1307->regs[2], ds1307->regs[3], | 
 | 155 | 			ds1307->regs[4], ds1307->regs[5], | 
 | 156 | 			ds1307->regs[6]); | 
 | 157 |  | 
 | 158 | 	t->tm_sec = BCD2BIN(ds1307->regs[DS1307_REG_SECS] & 0x7f); | 
 | 159 | 	t->tm_min = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); | 
 | 160 | 	tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f; | 
 | 161 | 	t->tm_hour = BCD2BIN(tmp); | 
 | 162 | 	t->tm_wday = BCD2BIN(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1; | 
 | 163 | 	t->tm_mday = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); | 
 | 164 | 	tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f; | 
 | 165 | 	t->tm_mon = BCD2BIN(tmp) - 1; | 
 | 166 |  | 
 | 167 | 	/* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */ | 
 | 168 | 	t->tm_year = BCD2BIN(ds1307->regs[DS1307_REG_YEAR]) + 100; | 
 | 169 |  | 
 | 170 | 	dev_dbg(dev, "%s secs=%d, mins=%d, " | 
 | 171 | 		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | 
 | 172 | 		"read", t->tm_sec, t->tm_min, | 
 | 173 | 		t->tm_hour, t->tm_mday, | 
 | 174 | 		t->tm_mon, t->tm_year, t->tm_wday); | 
 | 175 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 176 | 	/* initial clock setting can be undefined */ | 
 | 177 | 	return rtc_valid_tm(t); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 178 | } | 
 | 179 |  | 
 | 180 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) | 
 | 181 | { | 
 | 182 | 	struct ds1307	*ds1307 = dev_get_drvdata(dev); | 
 | 183 | 	int		result; | 
 | 184 | 	int		tmp; | 
 | 185 | 	u8		*buf = ds1307->regs; | 
 | 186 |  | 
 | 187 | 	dev_dbg(dev, "%s secs=%d, mins=%d, " | 
 | 188 | 		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | 
| Jeff Garzik | 11966ad | 2006-10-04 04:41:53 -0400 | [diff] [blame] | 189 | 		"write", t->tm_sec, t->tm_min, | 
 | 190 | 		t->tm_hour, t->tm_mday, | 
 | 191 | 		t->tm_mon, t->tm_year, t->tm_wday); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 192 |  | 
 | 193 | 	*buf++ = 0;		/* first register addr */ | 
 | 194 | 	buf[DS1307_REG_SECS] = BIN2BCD(t->tm_sec); | 
 | 195 | 	buf[DS1307_REG_MIN] = BIN2BCD(t->tm_min); | 
 | 196 | 	buf[DS1307_REG_HOUR] = BIN2BCD(t->tm_hour); | 
 | 197 | 	buf[DS1307_REG_WDAY] = BIN2BCD(t->tm_wday + 1); | 
 | 198 | 	buf[DS1307_REG_MDAY] = BIN2BCD(t->tm_mday); | 
 | 199 | 	buf[DS1307_REG_MONTH] = BIN2BCD(t->tm_mon + 1); | 
 | 200 |  | 
 | 201 | 	/* assume 20YY not 19YY */ | 
 | 202 | 	tmp = t->tm_year - 100; | 
 | 203 | 	buf[DS1307_REG_YEAR] = BIN2BCD(tmp); | 
 | 204 |  | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 205 | 	switch (ds1307->type) { | 
 | 206 | 	case ds_1337: | 
 | 207 | 	case ds_1339: | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 208 | 		buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY; | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 209 | 		break; | 
 | 210 | 	case ds_1340: | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 211 | 		buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN | 
 | 212 | 				| DS1340_BIT_CENTURY; | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 213 | 		break; | 
 | 214 | 	default: | 
 | 215 | 		break; | 
 | 216 | 	} | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 217 |  | 
 | 218 | 	ds1307->msg[1].flags = 0; | 
 | 219 | 	ds1307->msg[1].len = 8; | 
 | 220 |  | 
 | 221 | 	dev_dbg(dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", | 
 | 222 | 		"write", buf[0], buf[1], buf[2], buf[3], | 
 | 223 | 		buf[4], buf[5], buf[6]); | 
 | 224 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 225 | 	result = i2c_transfer(to_i2c_adapter(ds1307->client->dev.parent), | 
 | 226 | 			&ds1307->msg[1], 1); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 227 | 	if (result != 1) { | 
 | 228 | 		dev_err(dev, "%s error %d\n", "write", tmp); | 
 | 229 | 		return -EIO; | 
 | 230 | 	} | 
 | 231 | 	return 0; | 
 | 232 | } | 
 | 233 |  | 
| David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 234 | static const struct rtc_class_ops ds13xx_rtc_ops = { | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 235 | 	.read_time	= ds1307_get_time, | 
 | 236 | 	.set_time	= ds1307_set_time, | 
 | 237 | }; | 
 | 238 |  | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 239 | /*----------------------------------------------------------------------*/ | 
 | 240 |  | 
 | 241 | #define NVRAM_SIZE	56 | 
 | 242 |  | 
 | 243 | static ssize_t | 
 | 244 | ds1307_nvram_read(struct kobject *kobj, struct bin_attribute *attr, | 
 | 245 | 		char *buf, loff_t off, size_t count) | 
 | 246 | { | 
 | 247 | 	struct i2c_client	*client; | 
 | 248 | 	struct ds1307		*ds1307; | 
 | 249 | 	struct i2c_msg		msg[2]; | 
 | 250 | 	int			result; | 
 | 251 |  | 
| frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 252 | 	client = kobj_to_i2c_client(kobj); | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 253 | 	ds1307 = i2c_get_clientdata(client); | 
 | 254 |  | 
 | 255 | 	if (unlikely(off >= NVRAM_SIZE)) | 
 | 256 | 		return 0; | 
 | 257 | 	if ((off + count) > NVRAM_SIZE) | 
 | 258 | 		count = NVRAM_SIZE - off; | 
 | 259 | 	if (unlikely(!count)) | 
 | 260 | 		return count; | 
 | 261 |  | 
 | 262 | 	msg[0].addr = client->addr; | 
 | 263 | 	msg[0].flags = 0; | 
 | 264 | 	msg[0].len = 1; | 
 | 265 | 	msg[0].buf = buf; | 
 | 266 |  | 
 | 267 | 	buf[0] = 8 + off; | 
 | 268 |  | 
 | 269 | 	msg[1].addr = client->addr; | 
 | 270 | 	msg[1].flags = I2C_M_RD; | 
 | 271 | 	msg[1].len = count; | 
 | 272 | 	msg[1].buf = buf; | 
 | 273 |  | 
 | 274 | 	result = i2c_transfer(to_i2c_adapter(client->dev.parent), msg, 2); | 
 | 275 | 	if (result != 2) { | 
 | 276 | 		dev_err(&client->dev, "%s error %d\n", "nvram read", result); | 
 | 277 | 		return -EIO; | 
 | 278 | 	} | 
 | 279 | 	return count; | 
 | 280 | } | 
 | 281 |  | 
 | 282 | static ssize_t | 
 | 283 | ds1307_nvram_write(struct kobject *kobj, struct bin_attribute *attr, | 
 | 284 | 		char *buf, loff_t off, size_t count) | 
 | 285 | { | 
 | 286 | 	struct i2c_client	*client; | 
 | 287 | 	u8			buffer[NVRAM_SIZE + 1]; | 
 | 288 | 	int			ret; | 
 | 289 |  | 
| frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 290 | 	client = kobj_to_i2c_client(kobj); | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 291 |  | 
 | 292 | 	if (unlikely(off >= NVRAM_SIZE)) | 
 | 293 | 		return -EFBIG; | 
 | 294 | 	if ((off + count) > NVRAM_SIZE) | 
 | 295 | 		count = NVRAM_SIZE - off; | 
 | 296 | 	if (unlikely(!count)) | 
 | 297 | 		return count; | 
 | 298 |  | 
 | 299 | 	buffer[0] = 8 + off; | 
 | 300 | 	memcpy(buffer + 1, buf, count); | 
 | 301 |  | 
 | 302 | 	ret = i2c_master_send(client, buffer, count + 1); | 
 | 303 | 	return (ret < 0) ? ret : (ret - 1); | 
 | 304 | } | 
 | 305 |  | 
 | 306 | static struct bin_attribute nvram = { | 
 | 307 | 	.attr = { | 
 | 308 | 		.name	= "nvram", | 
 | 309 | 		.mode	= S_IRUGO | S_IWUSR, | 
 | 310 | 		.owner	= THIS_MODULE, | 
 | 311 | 	}, | 
 | 312 |  | 
 | 313 | 	.read	= ds1307_nvram_read, | 
 | 314 | 	.write	= ds1307_nvram_write, | 
 | 315 | 	.size	= NVRAM_SIZE, | 
 | 316 | }; | 
 | 317 |  | 
 | 318 | /*----------------------------------------------------------------------*/ | 
 | 319 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 320 | static struct i2c_driver ds1307_driver; | 
 | 321 |  | 
| Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 322 | static int __devinit ds1307_probe(struct i2c_client *client, | 
 | 323 | 				  const struct i2c_device_id *id) | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 324 | { | 
 | 325 | 	struct ds1307		*ds1307; | 
 | 326 | 	int			err = -ENODEV; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 327 | 	int			tmp; | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 328 | 	const struct chip_desc	*chip = &chips[id->driver_data]; | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 329 | 	struct i2c_adapter	*adapter = to_i2c_adapter(client->dev.parent); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 330 |  | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 331 | 	if (!i2c_check_functionality(adapter, | 
 | 332 | 			I2C_FUNC_I2C | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) | 
 | 333 | 		return -EIO; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 334 |  | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 335 | 	if (!(ds1307 = kzalloc(sizeof(struct ds1307), GFP_KERNEL))) | 
 | 336 | 		return -ENOMEM; | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 337 |  | 
 | 338 | 	ds1307->client = client; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 339 | 	i2c_set_clientdata(client, ds1307); | 
 | 340 |  | 
 | 341 | 	ds1307->msg[0].addr = client->addr; | 
 | 342 | 	ds1307->msg[0].flags = 0; | 
 | 343 | 	ds1307->msg[0].len = 1; | 
 | 344 | 	ds1307->msg[0].buf = &ds1307->reg_addr; | 
 | 345 |  | 
 | 346 | 	ds1307->msg[1].addr = client->addr; | 
 | 347 | 	ds1307->msg[1].flags = I2C_M_RD; | 
 | 348 | 	ds1307->msg[1].len = sizeof(ds1307->regs); | 
 | 349 | 	ds1307->msg[1].buf = ds1307->regs; | 
 | 350 |  | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 351 | 	ds1307->type = id->driver_data; | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 352 |  | 
 | 353 | 	switch (ds1307->type) { | 
 | 354 | 	case ds_1337: | 
 | 355 | 	case ds_1339: | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 356 | 		ds1307->reg_addr = DS1337_REG_CONTROL; | 
 | 357 | 		ds1307->msg[1].len = 2; | 
 | 358 |  | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 359 | 		/* get registers that the "rtc" read below won't read... */ | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 360 | 		tmp = i2c_transfer(adapter, ds1307->msg, 2); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 361 | 		if (tmp != 2) { | 
 | 362 | 			pr_debug("read error %d\n", tmp); | 
 | 363 | 			err = -EIO; | 
 | 364 | 			goto exit_free; | 
 | 365 | 		} | 
 | 366 |  | 
 | 367 | 		ds1307->reg_addr = 0; | 
 | 368 | 		ds1307->msg[1].len = sizeof(ds1307->regs); | 
 | 369 |  | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 370 | 		/* oscillator off?  turn it on, so clock can tick. */ | 
 | 371 | 		if (ds1307->regs[0] & DS1337_BIT_nEOSC) | 
 | 372 | 			i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, | 
 | 373 | 				ds1307->regs[0] & ~DS1337_BIT_nEOSC); | 
 | 374 |  | 
 | 375 | 		/* oscillator fault?  clear flag, and warn */ | 
 | 376 | 		if (ds1307->regs[1] & DS1337_BIT_OSF) { | 
 | 377 | 			i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, | 
 | 378 | 				ds1307->regs[1] & ~DS1337_BIT_OSF); | 
 | 379 | 			dev_warn(&client->dev, "SET TIME!\n"); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 380 | 		} | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 381 | 		break; | 
 | 382 | 	default: | 
 | 383 | 		break; | 
 | 384 | 	} | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 385 |  | 
 | 386 | read_rtc: | 
 | 387 | 	/* read RTC registers */ | 
 | 388 |  | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 389 | 	tmp = i2c_transfer(adapter, ds1307->msg, 2); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 390 | 	if (tmp != 2) { | 
 | 391 | 		pr_debug("read error %d\n", tmp); | 
 | 392 | 		err = -EIO; | 
 | 393 | 		goto exit_free; | 
 | 394 | 	} | 
 | 395 |  | 
 | 396 | 	/* minimal sanity checking; some chips (like DS1340) don't | 
 | 397 | 	 * specify the extra bits as must-be-zero, but there are | 
 | 398 | 	 * still a few values that are clearly out-of-range. | 
 | 399 | 	 */ | 
 | 400 | 	tmp = ds1307->regs[DS1307_REG_SECS]; | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 401 | 	switch (ds1307->type) { | 
 | 402 | 	case ds_1307: | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 403 | 	case m41t00: | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 404 | 		/* clock halted?  turn it on, so clock can tick. */ | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 405 | 		if (tmp & DS1307_BIT_CH) { | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 406 | 			i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); | 
 | 407 | 			dev_warn(&client->dev, "SET TIME!\n"); | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 408 | 			goto read_rtc; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 409 | 		} | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 410 | 		break; | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 411 | 	case ds_1338: | 
 | 412 | 		/* clock halted?  turn it on, so clock can tick. */ | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 413 | 		if (tmp & DS1307_BIT_CH) | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 414 | 			i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); | 
 | 415 |  | 
 | 416 | 		/* oscillator fault?  clear flag, and warn */ | 
 | 417 | 		if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { | 
 | 418 | 			i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL, | 
| David Brownell | bd16f9e | 2007-07-26 10:41:00 -0700 | [diff] [blame] | 419 | 					ds1307->regs[DS1307_REG_CONTROL] | 
| Rodolfo Giometti | be5f59f | 2007-07-17 04:05:06 -0700 | [diff] [blame] | 420 | 					& ~DS1338_BIT_OSF); | 
 | 421 | 			dev_warn(&client->dev, "SET TIME!\n"); | 
 | 422 | 			goto read_rtc; | 
 | 423 | 		} | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 424 | 		break; | 
| frederic Rodo | fcd8db0 | 2008-02-06 01:38:55 -0800 | [diff] [blame] | 425 | 	case ds_1340: | 
 | 426 | 		/* clock halted?  turn it on, so clock can tick. */ | 
 | 427 | 		if (tmp & DS1340_BIT_nEOSC) | 
 | 428 | 			i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0); | 
 | 429 |  | 
 | 430 | 		tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG); | 
 | 431 | 		if (tmp < 0) { | 
 | 432 | 			pr_debug("read error %d\n", tmp); | 
 | 433 | 			err = -EIO; | 
 | 434 | 			goto exit_free; | 
 | 435 | 		} | 
 | 436 |  | 
 | 437 | 		/* oscillator fault?  clear flag, and warn */ | 
 | 438 | 		if (tmp & DS1340_BIT_OSF) { | 
 | 439 | 			i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0); | 
 | 440 | 			dev_warn(&client->dev, "SET TIME!\n"); | 
 | 441 | 		} | 
 | 442 | 		break; | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 443 | 	case ds_1337: | 
 | 444 | 	case ds_1339: | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 445 | 		break; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 446 | 	} | 
| David Brownell | 045e0e8 | 2007-07-17 04:04:55 -0700 | [diff] [blame] | 447 |  | 
 | 448 | 	tmp = ds1307->regs[DS1307_REG_SECS]; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 449 | 	tmp = BCD2BIN(tmp & 0x7f); | 
 | 450 | 	if (tmp > 60) | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 451 | 		goto exit_bad; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 452 | 	tmp = BCD2BIN(ds1307->regs[DS1307_REG_MIN] & 0x7f); | 
 | 453 | 	if (tmp > 60) | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 454 | 		goto exit_bad; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 455 |  | 
 | 456 | 	tmp = BCD2BIN(ds1307->regs[DS1307_REG_MDAY] & 0x3f); | 
 | 457 | 	if (tmp == 0 || tmp > 31) | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 458 | 		goto exit_bad; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 459 |  | 
 | 460 | 	tmp = BCD2BIN(ds1307->regs[DS1307_REG_MONTH] & 0x1f); | 
 | 461 | 	if (tmp == 0 || tmp > 12) | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 462 | 		goto exit_bad; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 463 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 464 | 	tmp = ds1307->regs[DS1307_REG_HOUR]; | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 465 | 	switch (ds1307->type) { | 
 | 466 | 	case ds_1340: | 
 | 467 | 	case m41t00: | 
 | 468 | 		/* NOTE: ignores century bits; fix before deploying | 
 | 469 | 		 * systems that will run through year 2100. | 
 | 470 | 		 */ | 
 | 471 | 		break; | 
 | 472 | 	default: | 
 | 473 | 		if (!(tmp & DS1307_BIT_12HR)) | 
 | 474 | 			break; | 
 | 475 |  | 
 | 476 | 		/* Be sure we're in 24 hour mode.  Multi-master systems | 
 | 477 | 		 * take note... | 
 | 478 | 		 */ | 
 | 479 | 		tmp = BCD2BIN(tmp & 0x1f); | 
 | 480 | 		if (tmp == 12) | 
 | 481 | 			tmp = 0; | 
 | 482 | 		if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM) | 
 | 483 | 			tmp += 12; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 484 | 		i2c_smbus_write_byte_data(client, | 
 | 485 | 				DS1307_REG_HOUR, | 
 | 486 | 				BIN2BCD(tmp)); | 
 | 487 | 	} | 
 | 488 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 489 | 	ds1307->rtc = rtc_device_register(client->name, &client->dev, | 
 | 490 | 				&ds13xx_rtc_ops, THIS_MODULE); | 
 | 491 | 	if (IS_ERR(ds1307->rtc)) { | 
 | 492 | 		err = PTR_ERR(ds1307->rtc); | 
 | 493 | 		dev_err(&client->dev, | 
 | 494 | 			"unable to register the class device\n"); | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 495 | 		goto exit_free; | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 496 | 	} | 
 | 497 |  | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 498 | 	if (chip->nvram56) { | 
 | 499 | 		err = sysfs_create_bin_file(&client->dev.kobj, &nvram); | 
 | 500 | 		if (err == 0) { | 
 | 501 | 			ds1307->has_nvram = true; | 
 | 502 | 			dev_info(&client->dev, "56 bytes nvram\n"); | 
 | 503 | 		} | 
 | 504 | 	} | 
 | 505 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 506 | 	return 0; | 
 | 507 |  | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 508 | exit_bad: | 
 | 509 | 	dev_dbg(&client->dev, "%s: %02x %02x %02x %02x %02x %02x %02x\n", | 
 | 510 | 			"bogus register", | 
 | 511 | 			ds1307->regs[0], ds1307->regs[1], | 
 | 512 | 			ds1307->regs[2], ds1307->regs[3], | 
 | 513 | 			ds1307->regs[4], ds1307->regs[5], | 
 | 514 | 			ds1307->regs[6]); | 
 | 515 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 516 | exit_free: | 
 | 517 | 	kfree(ds1307); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 518 | 	return err; | 
 | 519 | } | 
 | 520 |  | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 521 | static int __devexit ds1307_remove(struct i2c_client *client) | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 522 | { | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 523 | 	struct ds1307	*ds1307 = i2c_get_clientdata(client); | 
 | 524 |  | 
| David Brownell | 682d73f | 2007-11-14 16:58:32 -0800 | [diff] [blame] | 525 | 	if (ds1307->has_nvram) | 
 | 526 | 		sysfs_remove_bin_file(&client->dev.kobj, &nvram); | 
 | 527 |  | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 528 | 	rtc_device_unregister(ds1307->rtc); | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 529 | 	kfree(ds1307); | 
 | 530 | 	return 0; | 
 | 531 | } | 
 | 532 |  | 
 | 533 | static struct i2c_driver ds1307_driver = { | 
 | 534 | 	.driver = { | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 535 | 		.name	= "rtc-ds1307", | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 536 | 		.owner	= THIS_MODULE, | 
 | 537 | 	}, | 
| David Brownell | c065f35 | 2007-07-17 04:05:10 -0700 | [diff] [blame] | 538 | 	.probe		= ds1307_probe, | 
 | 539 | 	.remove		= __devexit_p(ds1307_remove), | 
| Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 540 | 	.id_table	= ds1307_id, | 
| David Brownell | 1abb0dc | 2006-06-25 05:48:17 -0700 | [diff] [blame] | 541 | }; | 
 | 542 |  | 
 | 543 | static int __init ds1307_init(void) | 
 | 544 | { | 
 | 545 | 	return i2c_add_driver(&ds1307_driver); | 
 | 546 | } | 
 | 547 | module_init(ds1307_init); | 
 | 548 |  | 
 | 549 | static void __exit ds1307_exit(void) | 
 | 550 | { | 
 | 551 | 	i2c_del_driver(&ds1307_driver); | 
 | 552 | } | 
 | 553 | module_exit(ds1307_exit); | 
 | 554 |  | 
 | 555 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); | 
 | 556 | MODULE_LICENSE("GPL"); |