Skip to contentSkip to conversation

at24c i2c EEPROM driver

lm-sensors David Brownell 2 participants 12 messages Jul 12, 2005 – Jul 24, 2005

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

I ran into one too many board with an I2C EEPROM that Linux handled poorly, and got a hack attack. Here's the result ... tested on at24c04 but it should work on most other chips in the at24c series without much effort. (Including on the at24c1024, 1 MBit of EEPROM... more than enough to hold say "u-Boot", along with product config data.)

If someone wants to pick this up and do things with this, feel free; there's an obvious gap in how it initializes, since these chips can't very usefully be probed. I have no current plans to submit this, but feel free to fling patches my way.

This is modeled more or less after "eeprom" but of course it handles not only read/write access, but also chips with more than 2 Kbits of storage. So there's the notion that on some systems it might replace "eeprom".

- Dave

-------------- next part --------------
A non-text attachment was scrubbed...
Name: at24c.patch
Type: text/x-diff
Size: 14851 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20050711/10f5b0bc/at24c.bin

#1 lore raw
Quoted 3 lines · David Brownell

I ran into one too many board with an I2C EEPROM that Linux handled poorly, and got a hack attack. Here's the result ... tested on at24c04 but it should work on most other chips in the at24c series without much effort.

Hmm, and it appears most other I2C EEPROMs too; I happened across a chart with about eight vendors showing part number equivalence. And the AT24C column was big, but maybe not the biggest.

So now what I need is some 8 pin DIP sockets on a Linux machine and then I can use this driver to program the 24LCxx EEPROMs used on some 8 bit microcontrollers I've got sitting around (on the grounds that they won't run Linux). Hmm. :)

- Dave

#2 lore raw
In reply to David Brownell

Hi David,

Quoted 2 lines · David Brownell

I ran into one too many board with an I2C EEPROM that Linux handled poorly, and got a hack attack.

Could you please define "handled poorly"?

Quoted 4 lines

If someone wants to pick this up and do things with this, feel free; there's an obvious gap in how it initializes, since these chips can't very usefully be probed. I have no current plans to submit this, but feel free to fling patches my way.

You are not the first one complaining about the limitations of the simple eeprom driver. So far we have been able to work around them, typically by using user-space tools to program the eeproms. The question is whether your case is different or not. Is eeprog not sufficient for your needs?

http://www2.lm-sensors.nu/~lm78/cvs/browse.cgi/lm_sensors2/prog/eepromer

If you followed the story of Ben Gardner's max6875 driver, you must realize that drivers for I2C devices in the 0x50-0x57 range should be VERY careful not to accidentally write to an innocent EEPROM. The I2C/SMBus protocols make it happen very easily, unfortunately.

Quoted 3 lines

This is modeled more or less after "eeprom" but of course it handles not only read/write access, but also chips with more than 2 Kbits of storage. So there's the notion that on some systems it might replace "eeprom".

The eeprom driver *did* support EEPROMs of more than 2 Kbits (actually up to 16 Kbits) although maybe not in a very elegant way.

If your driver is to replace the eeprom driver (you will have to convince me it is needed and safe), then it better replace it completely on all systems. Let's not duplicate such a simple functionality.

Quoted 3 lines

Tested only with AT24C04 so far, but support for many others is coded and ready for you once you decide how you want to handle the board-specific configuration issues.

What "board-specific configuration issues" are you thinking of?

Now, my comments on the patch itself.

Quoted 1 line

+obj-$(CONFIG_I2C_AT24C) += at24c.o

FWIW, CONFIG_I2C_* have always been used for i2c core drivers and i2c adapter drivers (aka i2c bus drivers), not for i2c chip drivers. Those were using either CONFIG_SENSORS_* for hardware monitoring drivers (and some others, but this is a mistake), or no particular prefix for the other ones. Now that we moved the hardware monitoring drivers outside of the i2c/chips subdirectory, I'd like to make things a bit more standard. I have no strong opinion on what naming scheme we should use, but I sure would like us to pick one and stick to it.

drivers/i2c/chips/at24c.c line 2

Quoted 5 lines
drivers/i2c/chips/at24c.c new file +2 -0
@@ -0,0 +1,502 @@
+/*
+ * drivers/i2c/chips/at24c.c -- support some Atmel AT24C EEPROMs

Having the full path in the comment is redundant and error-prone. We know where the file currently is, but that may change in the future, as was seen recently with all the hardware monitoring drivers.

drivers/i2c/chips/at24c.c line 3

Quoted 1 line
drivers/i2c/chips/at24c.c new file +1 -0
+#undef	DEBUG

Needless.

drivers/i2c/chips/at24c.c line 7

Quoted 4 lines
drivers/i2c/chips/at24c.c new file +4 -0
+ *   - for one 4Kbit EEPROM with A0..A2 grounded, kernel params
+ *       at24c.force=0,0x50,-1 at24c.sizes=4
+ *   - one 2Kbit EEPROM with A0..A2 grounded; one with them pulled high
+ *       at24c.force=0,0x50,0,0x57,-1 at24c.sizes=2,2

I don't know where you got these "-1" from but they are not correct. i2c "force" parameters should always be given by pair.

As the legacy parameter names were all singular, I would suggest "size" rather than "sizes" for the new parameter name.

Also, why do you use "force" rather than "probe" here?

drivers/i2c/chips/at24c.c line 8

Quoted 1 line
drivers/i2c/chips/at24c.c new file +1 -0
+ * It's also OK to leave off the 0x5 prefix.

I consider this a bad idea, as this adds code with no benefit and is even error-prone as it breaks the compatibility with the way all other i2c chip drivers handle that parameter.

drivers/i2c/chips/at24c.c line 15

Quoted 7 lines
drivers/i2c/chips/at24c.c new file +7 -0
+/* to a first approximation:  kbits = chiptype */
+enum chiptype {
+	/* generic 2Kbit, read-only; as seen on memory DIMMS etc */
+	generic = 0,
+	/* single byte addressing, sometimes using I2C addresses too */
+	at24c01 = 1,
+	at24c02 = 2,

What you call "generic" are perfectly standard at24c02-like EEPROMs, so I see no point in making them a particular case. If it's just to make them read-only and all the other ones read-write, this looks a bad idea to merge the writability with the size in a single parameter. Sooner or later someone will need a read-only access to a larger EEPROM, and this will break.

What do you mean with "sometimes using I2C addresses too"?

drivers/i2c/chips/at24c.c line 24

Quoted 9 lines
drivers/i2c/chips/at24c.c new file +9 -0
+struct at24c_data {
+	struct i2c_client	client;
+	struct semaphore	lock;
+	struct bin_attribute	bin;
+	u32			next_addr;
+	u16			chiptype;
+	u16			pagesize;
+	u8			addr_len;
+};

Maybe some comments on what next_addr, pagesize and addr_len are for? (The mix of member names using underscores with names not using them isn't exactly nice, BTW.)

drivers/i2c/chips/at24c.c line 31

Quoted 7 lines
drivers/i2c/chips/at24c.c new file +7 -0
+static int at24c_ee_address(
+	unsigned	chiptype,
+	struct i2c_msg	*msg,
+	unsigned	*offset,
+	u32		*next_addr
+)
+{

Eek. What's this coding style?

Quoted 8 lines

/* advantages of not using i2c_smbus_read_i2c_block_data() here
* include both functionality and performance:
* (a) uses addresses after at24c->client.addr, as needed;
* (b) handles two-byte "register" addresses;
* (c) this can often omit the initial dummy write;
* (d) reads everything at once, not in 32-byte niblets;
* (e) less data copying.
*/

Points (a) and (c) would need clarification.

Point (b): You could as well implement a generic function in i2c-core. I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 is already reserved for that in i2c.h, which suggests that SMBus 2.0 defines it (although I can't remember of any hardware implementimg it).

Points (d) and (e) suggest that the i2c-core API is not adapted to the needs. You are not the first one to complain about the i2c block transfers API, yet everyone always go coding its own implementation on its side. I'd like someone to properly analyze the situation and make concrete proposals for a change.

Note that your implementation requires a plain I2C bus master. SMBus masters won't possibly work with it. This is a significant drawback. If you want this driver to ever replace the eeprom driver, you will have to make it work on SMBus as well.

Quoted 1 line

at24c_bin_write(struct kobject *kobj, char *buf, loff_t off, size_t count)

Could you possibly split the i2c-write part out of this function, just like you did for the read part? This would make it more modular and readable.

Quoted 3 lines
	default:
		/* don't know how to handle this chip type yet! */
		return -ENOSYS;

You have a memory leak here, and also a few lines later in the same function. You really should centralize your failure path to prevent that.

Quoted 1 line
		strcpy(at24c->client.name, "generic EEPROM");

Please use strlcpy instead of strcpy.

Quoted 3 lines
	/* some chips take two or more addresses ... register using the
	 * first one, and hope no other driver claims the others!
	 */

No, don't just hope. Properly register the other addresses as subclients. See the asb100 driver for an example of how to do that.

Quoted 1 line
	address |= 0x50;

As said earlier, this is dangerous and shouldn't be done at all.

Quoted 1 line
	/* FIXME poke it, to see if there's actually a chip there! */

This is the i2c-core job and you would have it for free if you were using "probe" rather than "force" as suggested above. Note that using "probe" would require you to include the 24RF08 corruption preventer (until I find some time to work around the problem more globally).

Quoted 9 lines

static int at24c_attach_adapter(struct i2c_adapter *adapter)

{
	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
		dev_dbg(&adapter->dev, "%s probe, no I2C\n",
			at24c_driver.name);
		return 0;
	}
	return i2c_probe(adapter, &addr_data, at24c_probe);
}

This is interesting. In all other drivers we are checking for the functionality inside the *_probe (or *_detect) function rather than in the *_attach_adapter function. Now that I think of it, this sounds sub-optimal, your approach should be more efficient. Unless I'm missing something, we should probably change all other drivers to match this one.

Quoted 1 line
	kfree(container_of(client, struct at24c_data, client));

You should use i2c_get_clientdata() (just like you should have used i2c_set_clientdata() earlier.)

No MODULE_AUTHOR?

Note that your driver does not cache the reads to the EEPROM. While it may be on purpose, it raises concerns because you gave read access to everyone through sysfs, which means that any random user could saturate the I2C bus the EEPROM is on. If there are other critical chips on the bus (think battery controller, hardware monitor...) then we are in trouble.

I really appreciate that you have made your driver non-intrusive, in that it won't attach to any device by default. However, I would go one step further, by having a "writable" attribute separate from the concept of "default eeprom" which I don't really like. This could either be a module parameter, or a second sysfs file (I think we had a proposal in that direction some times ago).

--
Jean Delvare

#3 lore raw
In reply to David Brownell
Quoted 10 lines

Date: Tue, 12 Jul 2005 15:54:42 +0200 (CEST)
From: "Jean Delvare" <[email protected]>

Hi David,

Quoted 2 lines · David Brownell

I ran into one too many board with an I2C EEPROM that Linux handled poorly, and got a hack attack.

Could you please define "handled poorly"?

In the case of the board with an AT24C1024, no kernel support at all. (That's got 2-byte addressing.) I wanted to put U-Boot on that, and have some way to access the leftover space, so the NOR flash (at a whopping 2 MBytes!) had more space available for Real Stuff.

For the AT24C04, it was misdetected as two chips (by "eeprom"), and there was no write support.

Quoted 2 lines

You are not the first one complaining about the limitations of the simple eeprom driver.

Those weren't exactly the words I used. In public, anyway. :)

Quoted 6 lines

So far we have been able to work around them, typically
by using user-space tools to program the eeproms. The question is whether your case is different or not. Is eeprog not sufficient for your needs?

http://www2.lm-sensors.nu/~lm78/cvs/browse.cgi/lm_sensors2/prog/eepromer

Interesting, it might be worth mentioning that in the Kconfig helptext for the "eeprom" driver. I did some googling, and that never came up. (Mention that along with the fact that "eeprom" only handles chips of up to about 16 Kbits, and misdetects many of those as multiple chips ... )

I've got a few applications in mind. If I stay un-demanding, "eeprog" could suffice -- though clearly it'd be slower. The moment I want other kernel drivers to use that state, it wouldn't be enough.

Quoted 4 lines

If you followed the story of Ben Gardner's max6875 driver, you must realize that drivers for I2C devices in the 0x50-0x57 range should be VERY careful not to accidentally write to an innocent EEPROM. The I2C/SMBus protocols make it happen very easily, unfortunately.

Didn't follow that, but I'm fully aware of the issue. That's why I set this driver up to use only static configuration, no probing.

It's unrealistic to expect the kernel to defend against user error, but it's highly appropriate to expect it not to misconfigure.

Quoted 3 lines

If your driver is to replace the eeprom driver (you will have to convince me it is needed and safe), then it better replace it completely on all systems. Let's not duplicate such a simple functionality.

I only noted that such a thing was a possibility. For the moment, I've met my own goals with respect to this driver. Anyone wanting to take that next step now has a better tool to start with. :)

The main thing I'd put on the agenda if anyone were were to pursue that route would be how to provide a decent static declaration of boards' I2C configuration, to prevent needing to probe things that don't probe well. Such a thing might be done as part of updating the I2C stack for better driver model support, whenever time for such a thing becomes available.

For example, the "platform_bus" is often prepared, in board-specific setup code, with a platform_device for each device that's present, plus platform_data with information needed by the driver (like what GPIOs it uses on this board). That'd be a useful model for this
case: "at address N on this bus there is/may-be a B Kilobit EEPROM to expose as read-only".

I'll follow up on your code comments in a separate email/thread; thanks for the feedback!

- Dave

#4 lore raw
In reply to David Brownell
Quoted 9 lines

Date: Tue, 12 Jul 2005 15:54:42 +0200 (CEST)
From: "Jean Delvare" <[email protected]>

...

Quoted 3 lines

Tested only with AT24C04 so far, but support for many others is coded and ready for you once you decide how you want to handle the board-specific configuration issues.

What "board-specific configuration issues" are you thinking of?

How to declare to the driver -- ideally as part of board-specific init, rather than by modifying the driver -- what chips a given board has, and where. That shouldn't live in the driver, since so many boards have I2C EEPROMS for various purposes.

This does _not_ look very amenable to probing, and from what you wrote I suspect that other folk have confirmed that in practice.

Quoted 4 lines

Now, my comments on the patch itself.

Quoted 1 line

+obj-$(CONFIG_I2C_AT24C) += at24c.o

Quoted 8 lines

FWIW, CONFIG_I2C_* have always been used for i2c core drivers and i2c adapter drivers (aka i2c bus drivers), not for i2c chip drivers. Those were using either CONFIG_SENSORS_* for hardware monitoring drivers (and some others, but this is a mistake), or no particular prefix for the other ones. Now that we moved the hardware monitoring drivers outside of the i2c/chips subdirectory, I'd like to make things a bit more standard. I have no strong opinion on what naming scheme we should use, but I sure would like us to pick one and stick to it.

Me too. Though another option is to have no prefix at all.

I hadn't realized CONFIG_I2C_ was "claimed" in any sense; in USB-land, the CONFIG_USB_ prefix is used for most everything, both host side and peripheral side.

I did decide to NOT use the CONFIG_SENSORS_* stuff though. :)

How would for example SPI sensors fit in? I recently noticed that chips like the TSC2101 and ADS7846 aren't just used for touchscreen X/Y/presure sensors ... they also have temperature and battery voltage sensors. SENSOR should not imply I2C...

Quoted 9 lines

drivers/i2c/chips/at24c.c line 2

Quoted 5 lines
drivers/i2c/chips/at24c.c new file +2 -0
@@ -0,0 +1,502 @@
+/*
+ * drivers/i2c/chips/at24c.c -- support some Atmel AT24C EEPROMs

Having the full path in the comment is redundant and error-prone. We know where the file currently is, but that may change in the future, as was seen recently with all the hardware monitoring drivers.

Easily changed. (Background noise: keyboard clicking.) There.

Quoted 3 lines

drivers/i2c/chips/at24c.c line 3

Quoted 1 line
drivers/i2c/chips/at24c.c new file +1 -0
+#undef	DEBUG

Needless.

Though conventional in many drivers, highlighting the fact that to get _selective_ debugging (this driver only!) you just change that one line ...

Quoted 7 lines

drivers/i2c/chips/at24c.c line 7

Quoted 4 lines
drivers/i2c/chips/at24c.c new file +4 -0
+ *   - for one 4Kbit EEPROM with A0..A2 grounded, kernel params
+ *       at24c.force=0,0x50,-1 at24c.sizes=4
+ *   - one 2Kbit EEPROM with A0..A2 grounded; one with them pulled high
+ *       at24c.force=0,0x50,0,0x57,-1 at24c.sizes=2,2

I don't know where you got these "-1" from but they are not correct. i2c "force" parameters should always be given by pair.

I'm still not used to the way the I2C parameters are done. The notion of pre-allocating several large arrays, pre-initialized with "end" tags, still feels odd to me.

Quoted 2 lines

As the legacy parameter names were all singular, I would suggest "size" rather than "sizes" for the new parameter name.

Good enough, that part's not yet implemented anyway. There should also be per-chip "is it readonly?" flag. And maybe a non-I2C parameter limiting bus utilization.

Quoted 1 line

Also, why do you use "force" rather than "probe" here?

That's the model: the driver will do NOTHING unless it's explicitly told to. Initializing "probe" (like "eeprom" does) would mean the driver will mis-detect some chips.

And wasn't that 24RF08 problem a consequence of using probing?

Quoted 5 lines

drivers/i2c/chips/at24c.c line 8

Quoted 1 line
drivers/i2c/chips/at24c.c new file +1 -0
+ * It's also OK to leave off the 0x5 prefix.

I consider this a bad idea, as this adds code with no benefit and is even error-prone as it breaks the compatibility with the way all other i2c chip drivers handle that parameter.

Thing is, the addresses do need to be filtered/munged since they are user inputs and not all values are legit. The code I used to munge the things forced the 0x5 rather than create error cases. Not that it couldn't be a bit cleaner, but that's what it is now.

Quoted 4 lines
Quoted 4 lines
drivers/i2c/chips/at24c.c new file +4 -0
+/* to a first approximation:  kbits = chiptype */
+enum chiptype {
+	/* generic 2Kbit, read-only; as seen on memory DIMMS etc */
+	generic = 0,
Quoted 8 lines

drivers/i2c/chips/at24c.c line 15

Quoted 3 lines
drivers/i2c/chips/at24c.c new file +3 -0
+	/* single byte addressing, sometimes using I2C addresses too */
+	at24c01 = 1,
+	at24c02 = 2,

What you call "generic" are perfectly standard at24c02-like EEPROMs, so I see no point in making them a particular case. If it's just to make them read-only and all the other ones read-write, this looks a bad idea to merge the writability with the size in a single parameter.

The issue was how to handle backward compat with "eeprom"... and the plan was to treat all uninitialized sizes as "generic".

Which is _exactly_ what "eeprom" does. If it's good enough for that driver, it should be good enough here ... though as I commented, "eeprom" mis-handles at least double byte address cases, also chips of size != 2 Kbits, so maybe it's not really "good enough" there. ;)

Quoted 3 lines

Sooner or
later someone will need a read-only access to a larger EEPROM, and this will break.

As I noted. The whole board-specific config issue is incomplete, and that's clearly one issue in that mix.

Quoted 1 line

What do you mean with "sometimes using I2C addresses too"?

Should have read "multiple" I2C addresses. This is your issue with (a) below. I updated the code to have a comment at the top explaining this a bit, using the xample of an at24c08 which uses four I2C address for one chip.

Quoted 11 lines

drivers/i2c/chips/at24c.c line 24

Quoted 9 lines
drivers/i2c/chips/at24c.c new file +9 -0
+struct at24c_data {
+	struct i2c_client	client;
+	struct semaphore	lock;
+	struct bin_attribute	bin;
+	u32			next_addr;
+	u16			chiptype;
+	u16			pagesize;
+	u8			addr_len;
+};

Maybe some comments on what next_addr, pagesize and addr_len are for?

Comments are fine things. I avoid documenting data structures much while they're in flux though.

Quoted 2 lines

(The mix of member names using underscores with names not using them isn't exactly nice, BTW.)

There must be a lot of code you count as "not exactly nice" then!! I added underscores to chip_type and page_size.

Quoted 9 lines

drivers/i2c/chips/at24c.c line 31

Quoted 7 lines
drivers/i2c/chips/at24c.c new file +7 -0
+static int at24c_ee_address(
+	unsigned	chiptype,
+	struct i2c_msg	*msg,
+	unsigned	*offset,
+	u32		*next_addr
+)
+{

Eek. What's this coding style?

You mean, "wrap lines at 80 characters"? Or are you maybe feeling religious about where the right paren goes? :)

One could argue that function parameters are just single-purpose structures that (often) live on the stack ... many style guidelines say to format long parameter lists like structures. The kernel guidelines are agnostic on that point, and I've always found that structured lists like that are easier to make sense of.

Quoted 10 lines
Quoted 8 lines

/* advantages of not using i2c_smbus_read_i2c_block_data() here
* include both functionality and performance:
* (a) uses addresses after at24c->client.addr, as needed;
* (b) handles two-byte "register" addresses;
* (c) this can often omit the initial dummy write;
* (d) reads everything at once, not in 32-byte niblets;
* (e) less data copying.
*/

Points (a) and (c) would need clarification.

Done.

Quoted 4 lines

Point (b): You could as well implement a generic function in i2c-core. I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 is already reserved for that in i2c.h, which suggests that SMBus 2.0 defines it (although I can't remember of any hardware implementimg it).

If no SMBUS hardware implements it, I'm not sure what the point of such a function would be! For now, this version is portable enough.

Quoted 5 lines

Points (d) and (e) suggest that the i2c-core API is not adapted to the needs. You are not the first one to complain about the i2c block transfers API, yet everyone always go coding its own implementation on its side. I'd like someone to properly analyze the situation and make concrete proposals for a change.

Hmm? I thought I _was_ using the I2C block transfer API. The limitations I'm thinking of are SMBUS policy (32 byte niblets) or from its API. (Note that this driver doesn't escape copying things written to EEPROM -- but that's relatively rare, and not a case where zerocopy arguments should apply.)

You touched on a minor downside to (d) later: this can mean some BIG reads, denying the bus to other drivers.. Easily addressed by putting a ceiling on the amount of data read at once; and better a per-system ceiling than a global one.

Quoted 4 lines

Note that your implementation requires a plain I2C bus master. SMBus masters won't possibly work with it. This is a significant drawback. If you want this driver to ever replace the eeprom driver, you will have to make it work on SMBus as well.

All the Linux systems I do I2C on seem to have full I2C controllers, and don't need SMBUS limits. They'll have no problem with this driver; after all, it's for "I2C EEPROMs" not "SMBUS EEPROMs". :)

Maybe there should be drivers that in Kconfig depend on SMBUS, and others that depend on Real I2C(tm). Configs that don't enable a real I2C controller wouldn't be able to configure I2C-only drivers.

Quoted 5 lines
Quoted 1 line

at24c_bin_write(struct kobject *kobj, char *buf, loff_t off, size_t count)

Could you possibly split the i2c-write part out of this function, just like you did for the read part? This would make it more modular and readable.

Maybe later; ideally, that'd be done as part of exporting some API to other kernel drivers to let them maintain their persistent state. I'm not sure having a kobject vs an at24c_data parameter helps much.

Quoted 6 lines
Quoted 3 lines
	default:
		/* don't know how to handle this chip type yet! */
		return -ENOSYS;

You have a memory leak here, and also a few lines later in the same function. You really should centralize your failure path to prevent that.

Done, thanks.

Quoted 3 lines
Quoted 1 line
		strcpy(at24c->client.name, "generic EEPROM");

Please use strlcpy instead of strcpy.

I changed that a bit differently.

Quoted 6 lines
Quoted 3 lines
	/* some chips take two or more addresses ... register using the
	 * first one, and hope no other driver claims the others!
	 */

No, don't just hope. Properly register the other addresses as subclients. See the asb100 driver for an example of how to do that.

For now, this suffices. Especially if the model is that all the addresses in that range are statically configured. It may be worth cleaning that up at some point though, yes.

Quoted 3 lines
Quoted 1 line
	address |= 0x50;

As said earlier, this is dangerous and shouldn't be done at all.

Dangerous how? More dangerous than any other user error? It's not as if an at24c08 has flexibility about the addresses it will be using.

Quoted 6 lines
Quoted 1 line
	/* FIXME poke it, to see if there's actually a chip there! */

This is the i2c-core job and you would have it for free if you were using "probe" rather than "force" as suggested above. Note that using "probe" would require you to include the 24RF08 corruption preventer (until I find some time to work around the problem more globally).

I see two models for system configuration here, loosely "hotplug" and "static config". They aren't always in conflict, but in the case of these chips the "hotplug" dynamic config model fails since the chips aren't self-identifying.

This driver uses _only_ static configuration since there's no safe way to detect basics like whether the chip at the address needs one byte addressing or two byte addressing; or what size it is...

Quoted 10 lines
Quoted 9 lines

static int at24c_attach_adapter(struct i2c_adapter *adapter)

{
	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
		dev_dbg(&adapter->dev, "%s probe, no I2C\n",
			at24c_driver.name);
		return 0;
	}
	return i2c_probe(adapter, &addr_data, at24c_probe);
}
Quoted 5 lines

This is interesting. In all other drivers we are checking for the functionality inside the *_probe (or *_detect) function rather than in the *_attach_adapter function. Now that I think of it, this sounds sub-optimal, your approach should be more efficient. Unless I'm missing something, we should probably change all other drivers to match this one.

Well, tps65010 and isp1301_omap do it in attach(), so maybe not quite all drivers! :)

But yes, I obviously agree this is a more natural place to do this particular checking. Luckily those changes will be simple.

Maybe an even better place for it would be in the I2C core, by having the I2C drivers declare what level of functionality it needs from the adapter? One word of data in each driver (taken from struct padding?) replacing several instructions and fault paths ... moving to one fault path in the core.

Quoted 4 lines
Quoted 1 line
	kfree(container_of(client, struct at24c_data, client));

You should use i2c_get_clientdata() (just like you should have used i2c_set_clientdata() earlier.)

I don't see why. It's not incorrect; it's just a simpler style.

In this case it's less expensive; the compiler will generate less code since it will notice that the two pointer values are always the same.

Quoted 1 line

No MODULE_AUTHOR?

Redundant given the copyright statement; unless there's something special about showing up in "modinfo" output?

Quoted 6 lines

Note that your driver does not cache the reads to the EEPROM. While it may be on purpose, it raises concerns because you gave read access to everyone through sysfs, which means that any random user could saturate the I2C bus the EEPROM is on. If there are other critical chips on the bus (think battery controller, hardware monitor...) then we are in trouble.

I'll remove group and "other" read access then; good point. And add a parameter to constrain the read sizes.

I don't want to maintain any caches for data that's not performance-critical.

Quoted 6 lines

I really appreciate that you have made your driver non-intrusive, in that it won't attach to any device by default. However, I would go one step further, by having a "writable" attribute separate from the concept of "default eeprom" which I don't really like. This could either be a module parameter, or a second sysfs file (I think we had a proposal in that direction some times ago).

I'd been thinking of doing this with a module parameter.

However, the REALLY nice way to do this would be if the I2C bus framework had a clean way to handle static device configuration (like "platform_bus" does). Then such a flag could be just one of several bits of information provided to driver using driver model infrastructure like platform_data.

- Dave

Quoted 3 lines

--
Jean Delvare

#5 lore raw
In reply to David Brownell

Hi David,

Quoted 2 lines

For the AT24C04, it was misdetected as two chips (by "eeprom"), and there was no write support.

"Misdetected as two chips" is a view of the mind. The driver just doesn't care whether there are two physical 24C02 EEPROMs or one physical 24C04 EEPROM. It will export the EEPROM space as chucks of 256 bytes, regardless of the physical reality. If might not be convenient, but that's the way it was designed (for the sake of simplicity).

Keep in mind that the eeprom driver was a test/debug thing in the first place. But it happens to be used by almost everyone now, due to a lack of alternative or sufficient publicizing thereof.

That being said, it is still not clear to me whether we really need to use a kernel driver for writing to EEPROMs. EEPROMs programming doesn't sound like something you need to do very frequently, which is why user-space would make sense (and was used so far as a matter of fact). More on that below.

Quoted 9 lines
Quoted 4 lines

So far we have been able to work around them, typically by using user-space tools to program the eeproms. The question is whether your case is different or not. Is eeprog not sufficient for your needs?

Interesting, it might be worth mentioning that in the Kconfig helptext for the "eeprom" driver. I did some googling, and that never came up. (Mention that along with the fact that "eeprom" only handles chips of up to about 16 Kbits, and misdetects many of those as multiple chips ... )

Good idea. Care to propose a patch?

Quoted 3 lines

I've got a few applications in mind. If I stay un-demanding, "eeprog" could suffice -- though clearly it'd be slower. The moment I want other kernel drivers to use that state, it wouldn't be enough.

That's right, this is something the current i2c chip drivers (and the i2c subsystem as a whole, I fear) are not good at. They are meant as individual drivers, only interacting with user-space. They do not integrate with the rest of the kernel, as would sometimes be needed. That being said, the video i2c chip drivers are working differently and seem to succeed here, it might be worth taking a look.

Quoted 2 lines

It's unrealistic to expect the kernel to defend against user error, but it's highly appropriate to expect it not to misconfigure.

When an error can prevent a machine from ever booting, we better add some serious checks that the user did not misconfigure. It's not unusual for users to try random module parameters to see if it helps. This is the reason why I insist that the writability should be handles through an explicit feature (module parameter, sysfs file, whatever) rather than associated with a size parameter.

Quoted 4 lines

The main thing I'd put on the agenda if anyone were were to pursue that route would be how to provide a decent static declaration of boards' I2C configuration, to prevent needing to probe things that don't probe well.

Agreed, this would be helpful. Note however that this won't cover all cases. Some EEPROMs may or may not be present on a given system (think SPD), and you can't list all motherboard models for x86 anyway. So some probing or at least manual setting will still be needed.

Quoted 10 lines

Such a thing might be done as part of updating the I2C stack for better driver model support, whenever time for such a thing becomes available.

For example, the "platform_bus" is often prepared, in board-specific setup code, with a platform_device for each device that's present, plus platform_data with information needed by the driver (like what GPIOs it uses on this board). That'd be a useful model for this
case: "at address N on this bus there is/may-be a B Kilobit EEPROM to expose as read-only".

Agreed as well. That's beyong my own knowledge at least at the moment, but I'd always comment on patches if they were available.

Thanks,
--
Jean Delvare

#6 lore raw
In reply to David Brownell

Hi David,

A few comments on top of your comments to my comments ;)

Quoted 2 lines

This does _not_ look very amenable to probing, and from what you wrote I suspect that other folk have confirmed that in practice.

There are several levels of problems. First, the quick writes used for I2C probing aren't 24RF08-safe. This could be worked around (I have plans). Second, you can really only probe for the existence of an EEPROM at a given address. You can't reliably detect the size of the EEPROM, as you found yourself. Third, there are a few chips living in the 0x50-0x57 range which are NOT EEPROMs. Hopefully we can do some positive discrimination for these, but that's tricky.

Overall I agree that doing as much as possible based on the platform makes full sense, especially for all these systems heavily relying on large EEPROMs.

Quoted 4 lines
Quoted 2 lines

I have no strong opinion on what naming scheme we should use, but I sure would like us to pick one and stick to it.

Me too. Though another option is to have no prefix at all.

"No prefix at all" is one possible naming scheme ;)

Quoted 3 lines

I hadn't realized CONFIG_I2C_ was "claimed" in any sense; in USB-land, the CONFIG_USB_ prefix is used for most everything, both host side and peripheral side.

I didn't say it was claimed, not officially anyway. It just happens to be that way at the moment. I am fine with changing this, but then we'd do it consistently. Now that the hardware mnonitoring drivers have gone away, there are really only a few chip drivers remaining.

Quoted 1 line

I did decide to NOT use the CONFIG_SENSORS_* stuff though. :)

And quite rightly so, thanks :) I will have to fix the few ones using it improperly (which is why I insist on agreeing on a naming scheme now rather sooner than later).

Quoted 4 lines

How would for example SPI sensors fit in? I recently noticed that chips like the TSC2101 and ADS7846 aren't just used for touchscreen X/Y/presure sensors ... they also have temperature and battery voltage sensors. SENSOR should not imply I2C...

This is the very reason why we just moved all the hardware monitoring stuff (AKA sensors) to a different place. Drivers for W1 or SPI hardware monitoring decices will be welcome there. There are many articicial links between the hardware monitoring drivers and the i2c subsystem, mostly for historical reasons. These prevent us from doing a number of cleanups. I will work on separating both sides as clearly as possible in the next few weeks. Hopefully this will open the doors to better integration and lower footprint as well.

Quoted 3 lines

I'm still not used to the way the I2C parameters are done. The notion of pre-allocating several large arrays, pre-initialized with "end" tags, still feels odd to me.

True. Historical. On my list of things to cleanup.

Quoted 1 line

There should also be per-chip "is it readonly?" flag.

Yup, either as a module parameter or a second sysfs file.

Quoted 1 line

And maybe a non-I2C parameter limiting bus utilization.

I would make it mandatory, like the original eeprom does. Reading from or writing to an EEPROM isn't time-critical enough that anyone wouldn't possibly cope with a limit.

Quoted 5 lines
Quoted 1 line

Also, why do you use "force" rather than "probe" here?

That's the model: the driver will do NOTHING unless it's explicitly told to. Initializing "probe" (like "eeprom" does) would mean the driver will mis-detect some chips.

The only difference between probe and force at the i2c-core level is that force won't even try to check whether there is anything responsive at the given address. Then the driver itself may or may not want to make a difference between both (using the kind parameter of the detect function), but your driver doesn't.

Quoted 1 line

And wasn't that 24RF08 problem a consequence of using probing?

True, although a weak workaround exist.

I am fine with you using force for now, but once I have come up with a real, safe workaround, you may consider switching to probe, as it offers an additional check of the EEPROM presence.

Quoted 10 lines
Quoted 4 lines

What you call "generic" are perfectly standard at24c02-like EEPROMs, so I see no point in making them a particular case. If it's just to make them read-only and all the other ones read-write, this looks a bad idea to merge the writability with the size in a single parameter.

The issue was how to handle backward compat with "eeprom"... and the plan was to treat all uninitialized sizes as "generic".

Which is _exactly_ what "eeprom" does. If it's good enough for that driver, it should be good enough here ...

This ain't compatible, as you still need to provide the addresses while they are built-in for the "eeprom" driver. A really backward compatible driver would need to behave like the old driver did when no module parameter are provided.

Quoted 3 lines

though as I commented, "eeprom" mis-handles at least double byte address cases, also chips of size != 2 Kbits, so maybe it's not really "good enough" there. ;)

I think we really only need to be backward compatible with "eeprom" for the 24C02. This is the most popular EEPROM in consumer PCs. It's probably not a problem to break comaptibility for larger EEPROMs, as there are less users and these users (hopefully) knew better support could come later, which would imply incompatibility.

Quoted 1 line

There must be a lot of code you count as "not exactly nice" then!!

You have no idea. People hate me for that. It's not really important and you might as well ignore my pointless rants.

Quoted 10 lines
Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static int at24c_ee_address(
+	unsigned	chiptype,
+	struct i2c_msg	*msg,
+	unsigned	*offset,
+	u32		*next_addr
+)
+{

Eek. What's this coding style?

Quoted 2 lines

You mean, "wrap lines at 80 characters"? Or are you maybe feeling religious about where the right paren goes? :)

I mean that, among the various coding styles that exist for function declarations, I had never seen this one, neither in the kernel code nor even elsewhere. And this isn't exactly easy to read, as it ressembles a structure declaration.

Quoted 7 lines
Quoted 4 lines

Point (b): You could as well implement a generic function in i2c-core. I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 is already reserved for that in i2c.h, which suggests that SMBus 2.0 defines it (although I can't remember of any hardware implementimg it).

If no SMBUS hardware implements it, I'm not sure what the point of such a function would be! For now, this version is portable enough.

If nothing else, this could avoid code duplication (the max6875 driver needs something similar, for example). And hardware support might be added later, and we get immediate benefit.

Quoted 11 lines
Quoted 5 lines

Points (d) and (e) suggest that the i2c-core API is not adapted to the needs. You are not the first one to complain about the i2c block transfers API, yet everyone always go coding its own implementation on its side. I'd like someone to properly analyze the situation and make concrete proposals for a change.

Hmm? I thought I _was_ using the I2C block transfer API. The limitations I'm thinking of are SMBUS policy (32 byte niblets) or from its API. (Note that this driver doesn't escape copying things written to EEPROM -- but that's relatively rare, and not a case where zerocopy arguments should apply.)

Yup, I misexpressed myself. The API offered by i2c-core is really the SMBus one, as I2C doesn't exactly have an API. It happens that most I2C devices do work with I2C commands that have a name in the SMBus API, so we easily mix both.

Still, what I meant is that this SMBus API doesn't seem to make everyone happy, especially the block transfer functions. I am wondering if something better could be done so that people actually use that API rather than implementing their own.

Quoted 3 lines

All the Linux systems I do I2C on seem to have full I2C controllers, and don't need SMBUS limits. They'll have no problem with this driver; after all, it's for "I2C EEPROMs" not "SMBUS EEPROMs". :)

There is no such thing as SMBus EEPROMS, all EEPROMs I know of are I2C chips, and it happens that EEPROMs up to 24C16 can be accessed using standard SMBus commands, and manufacturers are relying on this. All x86 motherboards have an SMBus master and most use it to give access to some 24C02 EEPROMs, be it SPD on memory modules, laptop proprietary ones or ones on network adapters. Most EEPROMs I have access to myself are on SMBus (except for EDID/DDC).

Quoted 3 lines

Maybe there should be drivers that in Kconfig depend on SMBUS, and others that depend on Real I2C(tm). Configs that don't enable a real I2C controller wouldn't be able to configure I2C-only drivers.

This is handled by i2c capabilities, which are more flexible. There is no such thing as a SMBus master, each physical device (and each driver on top of that) may implement a subset of the SMBus commands. Even Real I2C(tm) masters can differ, some can do protocol mangling, some can't.

What you propose would prevent some I2C/SMBus interactions which exist now and we are perfectly happy with. Additionally, a given system can have both an SMBus and an I2C master, with different devices on them. Your approach wouldn't play well with this.

Now, I agree that mixing I2C and SMBus led us to a rather complex situation in the Linux kernel. Blame it to Philips and Intel, as we only reproduced the mess that physically exist.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
	/* some chips take two or more addresses ... register using the
	 * first one, and hope no other driver claims the others!
	 */

No, don't just hope. Properly register the other addresses as subclients. See the asb100 driver for an example of how to do that.

For now, this suffices. Especially if the model is that all the addresses in that range are statically configured. It may be worth cleaning that up at some point though, yes.

Sorry but no, it doesn't suffice, not even for now.

If you don't request all addresses for a given device, it means that anyone else may do. This includes: the eeprom driver, and user-space through i2c-dev, for example the sensors-detect driver.

If both your driver and a third party access the chip at the same time, the internal address register of the EEPROM will be overwritten and/or reads will be interlaced and the read/written data will be corrupted in no time. You don't want this to happen.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
	address |= 0x50;

As said earlier, this is dangerous and shouldn't be done at all.

Dangerous how? More dangerous than any other user error? It's not as if an at24c08 has flexibility about the addresses it will be using.

Dangerous in that someone asking for address 0x30 will end up with 0x70, which is irrational, and someone asking for 0x5C (which happens to be the page protection address of the 24RF08) will get it. As much as I'd like to agree with you that people are responsible for the module parameter they pass, I see no point in not checking strictly for the parameter value. These checks are cheap, and can prevent huge damage.

Quoted 13 lines
Quoted 5 lines

This is interesting. In all other drivers we are checking for the functionality inside the *_probe (or *_detect) function rather than in the *_attach_adapter function. Now that I think of it, this sounds sub-optimal, your approach should be more efficient. Unless I'm missing something, we should probably change all other drivers to match this one.

Well, tps65010 and isp1301_omap do it in attach(), so maybe not quite all drivers! :)

But yes, I obviously agree this is a more natural place to do this particular checking. Luckily those changes will be simple.

Quoted 5 lines

Maybe an even better place for it would be in the I2C core, by having the I2C drivers declare what level of functionality it needs from the adapter? One word of data in each driver (taken from struct padding?) replacing several instructions and fault paths ... moving to one fault path in the core.

Exactly the conclusion I had come to. Just like adapter drivers can say what they can do, device drivers would say what they require for proper operation (but are still free to use more later for improved speed or whatever). I need to think about it some more. but that sounds promising for sure.

(But I want to kill i2c-isa first, as it's in the way somehow.)

Quoted 4 lines
Quoted 2 lines

You should use i2c_get_clientdata() (just like you should have used i2c_set_clientdata() earlier.)

I don't see why. It's not incorrect; it's just a simpler style.

For maintainability I suppose. I'd guess there is a reason why Greg introduced i2c_get/set_clientdata in the first place. I admit I don't exactly know myself.

Quoted 3 lines

In this case it's less expensive; the compiler will generate less code since it will notice that the two pointer values are always the same.

I don't think we really care, speed is not really important here. You don't unload kernel modules every now and then.

Quoted 4 lines
Quoted 1 line

No MODULE_AUTHOR?

Redundant given the copyright statement; unless there's something special about showing up in "modinfo" output?

Not every user reads the source code, while simple users may want to get in touch with a driver author for some reason. If not, MODULE_AUTHOR wouldn't even exist, as all drivers do have their author name in the source.

Quoted 8 lines
Quoted 6 lines

Note that your driver does not cache the reads to the EEPROM. While it may be on purpose, it raises concerns because you gave read access to everyone through sysfs, which means that any random user could saturate the I2C bus the EEPROM is on. If there are other critical chips on the bus (think battery controller, hardware monitor...) then we are in trouble.

I'll remove group and "other" read access then; good point.

If you do, you will never be compatible with the legacy eeprom driver.

Quoted 2 lines

I don't want to maintain any caches for data that's not performance-critical.

The cache isn't about performance, although it incidentally helps on that front. It's about preventing local DoS through bus saturation.

Quoted 5 lines

However, the REALLY nice way to do this would be if the I2C bus framework had a clean way to handle static device configuration (like "platform_bus" does). Then such a flag could be just one of several bits of information provided to driver using driver model infrastructure like platform_data.

Where are the patches? ;)

You might want to wait for the i2c-isa cleanup to happen first, as the i2c subsystem will be much cleaner and somewhat less sensors-centric after I am done.

Thanks,
--
Jean Delvare

#7 lore raw
In reply to David Brownell

Here's a refreshed version of the patch ... addressing much of your feedback and replacing those nasty switch statements with some more reasonable table driven logic. It's more generic.

It now has working chip configuration, using probing as you suggested (simpler etc) including that AT28RF04 workaround.

Modulo the need for more testing, probably the biggest potential problem with this version is that it still doesn't claim all the addresses some chips use. Safe in controlled environments...

This is a better version for anyone else to hack on. :)

- Dave

Add a simple driver for most I2C EEPROMs, giving sysfs read/write access to their contents.

Tested only with AT24C04 so far, but support for many others is coded and ready for you.

drivers/i2c/chips/Kconfig +19 -0
@@ -406,6 +406,25 @@
 menu "Other I2C Chip support"
 	depends on I2C
 
+config I2C_AT24C
+	tristate "Atmel AT24C series EEPROMs (and compatibles)"
+	depends on I2C && EXPERIMENTAL
+	help
+	  This driver should handle most I2C EEPROMS, including the AT24C
+	  series and compatible ones like those from Microchip and other
+	  vendors.  For example, most memory DIMMS have 24c02 EEPROMs.
+
+	     24c00, 24c01, 24c02, 24c04, 24c08, 24c16,
+	     24c32, 24c64, 24c128, 24c256, 24c512, 24c1024
+
+	  If you say yes here you get support for most Atmel AT24C series
+	  serial EEPROMS.  The driver provides read and write access to the
+	  EEPROM data through sysfs, once you've configured it to know about
+	  the particular chips on your target board.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called at24c.
+
 config SENSORS_DS1337
 	tristate "Dallas Semiconductor DS1337 and DS1339 Real Time Clock"
 	depends on I2C && EXPERIMENTAL
drivers/i2c/chips/Makefile +1 -0
@@ -47,6 +47,7 @@
 obj-$(CONFIG_SENSORS_W83627EHF)	+= w83627ehf.o
 obj-$(CONFIG_SENSORS_W83L785TS)	+= w83l785ts.o
 
+obj-$(CONFIG_I2C_AT24C)		+= at24c.o
 obj-$(CONFIG_ISP1301_OMAP)	+= isp1301_omap.o
 obj-$(CONFIG_TPS65010)		+= tps65010.o
 obj-$(CONFIG_SENSORS_TLV320AIC23) += tlv320aic23.o
drivers/i2c/chips/at24c.c new file +610 -0

610 lines not on this page · raw message

#8 lore raw
In reply to David Brownell
Quoted 7 lines
Quoted 2 lines

Which is _exactly_ what "eeprom" does. If it's good enough for that driver, it should be good enough here ...

This ain't compatible, as you still need to provide the addresses while they are built-in for the "eeprom" driver. A really backward compatible driver would need to behave like the old driver did when no module parameter are provided.

I'm thinking full backward compat with "eeprom" is not necessarily worth having as a goal.

One aspect of that is size sensing thing, where "eeprom" assumes everything it sees is a 24c02 EEPROM (vs being told what's where), and can use 8 bit addressing. This is the wrong idea. :)

Another is the various additional tweaks, like special support to hide the VAIO password. Simpler would be just to make that data readable only by root.

Quoted 6 lines
Quoted 3 lines

though as I commented, "eeprom" mis-handles at least double byte address cases, also chips of size != 2 Kbits, so maybe it's not really "good enough" there. ;)

I think we really only need to be backward compatible with "eeprom" for the 24C02. This is the most popular EEPROM in consumer PCs.

What do you mean "compatible" ... never being writable? Supporting the VAIO hack? Having the same driver name?

Folk have said on this list before they don't think that the "eeprom" driver is much more than an example. So I'm reasonably sure that there's no point in being identical to it. If both drivers can provide an "eeprom" binary file -- that should be close enough.

Quoted 4 lines

It's
probably not a problem to break comaptibility for larger EEPROMs, as there are less users and these users (hopefully) knew better support could come later, which would imply incompatibility.

Ones using 16 bit addressing never worked before, so breaking compatibility there is the topmost goal! :)

Chips of 4, 8, or 16 Kbits being handled as one chip rather than several chips is surely the better model, but that's the main incompatibility I can see.

Other than saner support for smaller eeproms ... e.g. right now the driver assumes everything's got 2Kbit capacity, which means smaller eeproms (notably 1 Kbit and 128 bits) misbehave.

Quoted 11 lines
Quoted 7 lines
Quoted 4 lines

Point (b): You could as well implement a generic function in i2c-core. I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 is already reserved for that in i2c.h, which suggests that SMBus 2.0 defines it (although I can't remember of any hardware implementimg it).

If no SMBUS hardware implements it, I'm not sure what the point of such a function would be! For now, this version is portable enough.

If nothing else, this could avoid code duplication (the max6875 driver needs something similar, for example). And hardware support might be added later, and we get immediate benefit.

So that'd be a capability that drivers would need to advertise, and which would have a straightforward implementation in terms of I2C primitives. Sure. No problem; that seems like a fine thing to have.

If someone were to submit patches for this in the core, and teaching my at24c driver how to work on SMBUS, then I suspect we'd both be happier. :)

Quoted 6 lines
Quoted 5 lines

Maybe an even better place for it would be in the I2C core, by having the I2C drivers declare what level of functionality it needs from the adapter? One word of data in each driver (taken from struct padding?) replacing several instructions and fault paths ... moving to one fault path in the core.

Quoted 7 lines

Exactly the conclusion I had come to. Just like adapter drivers can say what they can do, device drivers would say what they require for proper operation (but are still free to use more later for improved speed or whatever). I need to think about it some more. but that sounds promising for sure.

(But I want to kill i2c-isa first, as it's in the way somehow.)

Take your time. :)

Random side note: I think an SPI driver framework might need the same sort of thing, wherein drivers say what SPI mode(s) they need.

Quoted 3 lines
Quoted 1 line

I'll remove group and "other" read access then; good point.

If you do, you will never be compatible with the legacy eeprom driver.

That wasn't necessarily a good default choice. FLASH eeproms aren't normally world readable, for example. Why should I2C ones be?

Quoted 7 lines
Quoted 5 lines

However, the REALLY nice way to do this would be if the I2C bus framework had a clean way to handle static device configuration (like "platform_bus" does). Then such a flag could be just one of several bits of information provided to driver using driver model infrastructure like platform_data.

Where are the patches? ;)

The only bright idea I've had so far involves board-specific code providing a platform pseudo-device whose platform_data would be morphed (by a "pseudo-driver", registered and deregistered before the "at24c" i2c driver) into the addr_data (and coupled fields) that are currently used to init that i2c driver.

That's a bit hacky though. Better if there were some real device node that the at24c driver would bind to ... which would sort of turn the current I2C model on its head.

Or maybe not ... maybe a static I2C device tree could just be set up early by board-specific code, and the I2C infrastructure could view it as an alternate way to specify more "probe" or "force" entries, without using module params.

Quoted 3 lines

You might want to wait for the i2c-isa cleanup to happen first, as the i2c subsystem will be much cleaner and somewhat less sensors-centric after I am done.

Wasn't there some work afoot (Greg in his copious spare time?) to make I2C play better in the driver model world?

- Dave

#9 lore raw
In reply to David Brownell
Quoted 17 lines

Date: Wed, 13 Jul 2005 16:06:48 -0700
From: [email protected]

Quoted 7 lines
Quoted 5 lines

However, the REALLY nice way to do this would be if the I2C bus framework had a clean way to handle static device configuration (like "platform_bus" does). Then such a flag could be just one of several bits of information provided to driver using driver model infrastructure like platform_data.

Where are the patches? ;)

The only bright idea I've had so far involves board-specific code providing a platform pseudo-device whose platform_data would be morphed (by a "pseudo-driver", registered and deregistered before the "at24c" i2c driver) into the addr_data (and coupled fields) that are currently used to init that i2c driver.

Quoted 8 lines

That's a bit hacky though. Better if there were some real device node that the at24c driver would bind to ... which would sort of turn the current I2C model on its head.

Or maybe not ... maybe a static I2C device tree could just be set up early by board-specific code, and the I2C infrastructure could view it as an alternate way to specify more "probe" or "force" entries, without using module params.

Yeah, the best way to do that is really going to involve turning the I2C stack a bit on its head ... making it work more like most other Linux driver stacks.

That is, significant API surgery ... stuff I suspect folk believe is needed in any case, but still it's a lot of change.

One way to start thinking about that is to look at the attached patch (NOT suitable for merging! Though it does solve the problem) and see how the new probe() gets used. And then observe, "hey, if i2c core creates and registers the i2c client, like it would in other Linux bus frameworks, and then passed it into this routine...
then the second two parameters would be implicit." A significant
change to the driver's role.

- Dave

This is an experimental patch for system-specific static config support in the I2C framework.

- New i2c core interfaces:
* New driver probe() method, with platform string data
* Board-specific data storage for i2c
- OSK board init provides static i2c config data
- at24c implements the new probe()
- i2c-core uses it
* retrieves the new device descriptions
* new probe only after attach() gets a first whack
- new static i2c code implements it
* optional, CONFIG_I2C_BOOTDATA
* get/set device record array

It might be better push the driver model deeper into this patch, but that would involve formally adding a lifecycle where the device drivers are no longer responsible for creating device nodes (which are embedded in a "struct i2c_client").

include/linux/i2c.h +5 -0
@@ -129,6 +129,11 @@
 	 */
 	int (*command)(struct i2c_client *client,unsigned int cmd, void *arg);
 
+	/* set up devices and drivers according to system description
+	 * tables; see <linux/i2c-config.h>
+	 */
+	void (*probe)(struct i2c_adapter *adapter, int address, char *data);
+
 	struct device_driver driver;
 	struct list_head list;
 };
include/linux/i2c-config.h new file +56 -0

56 lines not on this page · raw message

drivers/i2c/chips/at24c.c +18 -1
@@ -639,6 +639,7 @@
 }
 
 
+/* "legacy" i2c driver api entry:  bind to bus, not device */
 static int __devinit at24c_attach_adapter(struct i2c_adapter *adapter)
 {
 	/* REVISIT: using SMBUS calls would improve portability, though
@@ -652,6 +653,20 @@
 	return i2c_probe(adapter, &addr_data, at24c_old_probe);
 }
 
+/* experimental static config support: bind to device */
+static void __devinit
+at24c_probe(struct i2c_adapter *adapter, int address, char *data)
+{
+	const struct chip_desc		*chip;
+
+	chip = find_chip(data);
+	if (!chip)
+		dev_err(&adapter->dev, "unknown chipname %s\n", data);
+	else
+		at24c_activate(adapter, address, chip,
+				!(chip->flags & EE_READONLY));
+}
+
 static int __devexit at24c_detach_client(struct i2c_client *client)
 {
 	int			err;
@@ -678,11 +693,13 @@
 	.flags		= I2C_DF_NOTIFY,
 	.attach_adapter	= at24c_attach_adapter,
 	.detach_client	= __devexit_p(at24c_detach_client),
+	.probe		= at24c_probe,
 };
 
 static int __init at24c_init(void)
 {
-#ifdef CONFIG_OMAP_OSK_MISTRAL
+#if 0
+//#ifdef CONFIG_OMAP_OSK_MISTRAL
 	/* REVISIT ...  */
 	if (machine_is_omap_osk()) {
 		n_chip_names = 1;
arch/arm/mach-omap1/board-osk.c +29 -0
@@ -34,6 +34,8 @@
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/partitions.h>
 
+#include <linux/i2c-config.h>
+
 #include <asm/hardware.h>
 #include <asm/mach-types.h>
 #include <asm/mach/arch.h>
@@ -191,6 +193,31 @@
 	{ OMAP_TAG_USB,           &osk_usb_config },
 };
 
+
+static struct i2c_device_record i2c_devs[] __initdata = {
+{
+	/* this autprobes OK, but there are several chip variants */
+	.driver_name	= "tps65010",
+	.adapter_id	= 0,
+	.dev_num	= 0x48,
+	.data 		= "tps65010",	/* vs 65013 etc */
+}, {
+	.driver_name	= "tlv320aic23",
+	.adapter_id	= 0,
+	.dev_num	= 0x1b,
+	// clock setup is board-specific ...
+},
+#ifdef	CONFIG_OMAP_OSK_MISTRAL
+{
+	.driver_name	= "at24c",
+	.adapter_id	= 0,
+	.dev_num	= 0x50,
+	.data 		= "24c04",
+},
+	/* the camera connector may also have an ov9640 sensor */
+#endif
+};
+
 #ifdef	CONFIG_OMAP_OSK_MISTRAL
 
 #ifdef	CONFIG_PM
@@ -248,6 +275,8 @@
 	omap_board_config_size = ARRAY_SIZE(osk_config);
 	USB_TRANSCEIVER_CTRL_REG |= (3 << 1);
 
+	i2c_set_boot_data(i2c_devs, ARRAY_SIZE(i2c_devs));
+
 	osk_mistral_init();
 }
 
drivers/i2c/i2c-core.c +59 -0

89 lines not on this page · raw message

drivers/i2c/Kconfig +8 -0
@@ -74,5 +74,13 @@
 	  a problem with I2C support and want to see more of what is going
 	  on.
 
+# for testing, this is always forced on ...  systems that want to
+# pass board data this way should eventually enable it selectively
+
+config I2C_BOOTDATA
+	bool
+	depends on I2C
+	default y
+
 endmenu
 
drivers/i2c/Makefile +1 -0
@@ -5,6 +5,7 @@
 obj-$(CONFIG_I2C)		+= i2c-core.o
 obj-$(CONFIG_I2C_CHARDEV)	+= i2c-dev.o
 obj-$(CONFIG_I2C_SENSOR)	+= i2c-sensor.o
+obj-$(CONFIG_I2C_BOOTDATA)	+= i2c-boot.o
 obj-y				+= busses/ chips/ algos/
 
 i2c-sensor-objs := i2c-sensor-detect.o i2c-sensor-vid.o
drivers/i2c/i2c-boot.c new file +45 -0

45 lines not on this page · raw message

#10 lore raw
In reply to David Brownell
Quoted 3 lines

Here's a refreshed version of the patch ... addressing much of your feedback and replacing those nasty switch statements with some more reasonable table driven logic. It's more generic.

And here's what will be the last update for a while. The main patch I'd like to see someone contribute is SMBUS support so this can run in PC type hardware too.

- Dave

Add a simple driver for most I2C EEPROMs, giving sysfs read/write access to their contents. Tested with 24LC00, AT24C04, and 24LC64 so far; at both 100 and 400 KHz.

Changes from the last snapshot include bugfixes to address chip write delays (an msleep, making things veeerrry slow at HZ0), cleanups, and claiming all a chip's addresses, to lock out any other drivers. Also Kconfig doesn't headline Atmel AT24C series any more, since they're far from the only chips supported.

drivers/i2c/chips/Kconfig +23 -0
@@ -406,6 +406,29 @@
 menu "Other I2C Chip support"
 	depends on I2C
 
+config I2C_AT24C
+	tristate "EEPROMs from most vendors"
+	depends on I2C && EXPERIMENTAL
+	help
+	  Enable this driver to get read/write support to most I2C EEPROMs,
+	  after you configure the driver to know about each eeprom on on
+	  your target board.  Use these generic chip names, instead of
+	  vendor-specific ones like at24c64 or 24lc02:
+
+	     24c00, 24c01, 24c02, dimm (readonly 24c02), 24c04, 24c08,
+	     24c16, 24c32, 24c64, 24c128, 24c256, 24c512, 24c1024
+
+	  Unless you like data loss puzzles, always be sure that any chip
+	  you configure as a 24c32 (32 Kbits) or larger is NOT really a
+	  24c16 (16 Kbits) or smaller.  (Marking the chip as readonly won't
+	  help recover from this.)
+
+	  The current version of this driver demands full I2C bus support,
+	  so it won't yet work on most PCs (limited to SMBUS).
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called at24c.
+
 config SENSORS_DS1337
 	tristate "Dallas Semiconductor DS1337 and DS1339 Real Time Clock"
 	depends on I2C && EXPERIMENTAL
drivers/i2c/chips/Makefile +3 -0
@@ -7,6 +7,9 @@
 obj-$(CONFIG_SENSORS_W83627HF)	+= w83627hf.o
 obj-$(CONFIG_SENSORS_W83781D)	+= w83781d.o
 
+# let 'at24c' get a chance before the less powerful 'eeprom'
+obj-$(CONFIG_I2C_AT24C)		+= at24c.o
+
 obj-$(CONFIG_SENSORS_ADM1021)	+= adm1021.o
 obj-$(CONFIG_SENSORS_ADM1025)	+= adm1025.o
 obj-$(CONFIG_SENSORS_ADM1026)	+= adm1026.o
drivers/i2c/chips/at24c.c new file +737 -0

737 lines not on this page · raw message

#11 lore raw
In reply to David Brownell

Hi David,

Quoted 3 lines

And here's what will be the last update for a while. The main patch I'd like to see someone contribute is SMBUS support so this can run in PC type hardware too.

I've linked from our "new drivers" page to this post in the archive so that people looking for 2-byte addressed eeprom support will hopefully find it.

Signature · 1 line
Jean Delvare