Skip to contentSkip to conversation

lm87: Add support for configuration

PATCH In review Ben Hutchings 2 participants 5 messages Aug 19, 2008 – Aug 20, 2008 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 lm87: Add support for configuration

The lm87 driver normally assumes that firmware configured the chip correctly. Since this is not always the case, alllow platform code to set the channel register value via platform_data. All other configuration registers can be changed after driver initialisation.

---
 drivers/hwmon/lm87.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)
drivers/hwmon/lm87.c +24 -9
index ddad6fc..1f5cac5 100644
@@ -21,11 +21,12 @@
  *   http://www.national.com/pf/LM/LM87.html
  *
  * Some functions share pins, so not all functions are available at the same
- * time. Which are depends on the hardware setup. This driver assumes that
- * the BIOS configured the chip correctly. In that respect, it  differs from
- * the original driver (from lm_sensors for Linux 2.4), which would force the
- * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
- * chipset wiring.
+ * time. Which are depends on the hardware setup. This driver normally
+ * assumes that firmware configured the chip correctly. Where this is not
+ * the case, platform code must set the I2C client's platform_data to point
+ * to a u8 value to be written to the channel register. In that case the
+ * driver will assume that it is the only user of the chip and will reset
+ * it in the probe and remove functions.
  * For reference, here is the list of exclusive functions:
  *  - in0+in5 (default) or temp3
  *  - fan1 (default) or in6
@@ -731,7 +732,7 @@ static int lm87_probe(struct i2c_client *new_client,
 
 	/* Register sysfs hooks */
 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
-		goto exit_free;
+		goto exit_reset;
 
 	if (data->channel & CHAN_NO_FAN(0)) {
 		if ((err = device_create_file(&new_client->dev,
@@ -831,7 +832,10 @@ static int lm87_probe(struct i2c_client *new_client,
 exit_remove:
 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
-exit_free:
+exit_reset:
+	if (new_client->dev.platform_data)
+		lm87_write_value(new_client, LM87_REG_CONFIG, 0x80);
+
 	kfree(data);
 exit:
 	return err;
@@ -842,9 +846,17 @@ static void lm87_init_client(struct i2c_client *client)
 	struct lm87_data *data = i2c_get_clientdata(client);
 	u8 config;
 
-	data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+	if (client->dev.platform_data) {
+		config = 0x80;
+		lm87_write_value(client, LM87_REG_CONFIG, config);
+		data->channel = *(u8 *)client->dev.platform_data;
+		lm87_write_value(client,
+				 LM87_REG_CHANNEL_MODE, data->channel);
+	} else {
+		data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+		config = lm87_read_value(client, LM87_REG_CONFIG);
+	}
 
-	config = lm87_read_value(client, LM87_REG_CONFIG);
 	if (!(config & 0x01)) {
 		int i;
 
@@ -882,6 +894,9 @@ static int lm87_remove(struct i2c_client *client)
 	sysfs_remove_group(&client->dev.kobj, &lm87_group);
 	sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
 
+	if (client->dev.platform_data)
+		lm87_write_value(client, LM87_REG_CONFIG, 0x80);
+
 	kfree(data);
 	return 0;
 }
Signature · 8 lines
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Ben,

Quoted 4 lines · Ben Hutchings

The lm87 driver normally assumes that firmware configured the chip correctly. Since this is not always the case, alllow platform code to set the channel register value via platform_data. All other configuration registers can be changed after driver initialisation.

Sounds almost sane to me, except...

Quoted 15 lines
---
 drivers/hwmon/lm87.c |   33 ++++++++++++++++++++++++---------
 1 files changed, 24 insertions(+), 9 deletions(-)
drivers/hwmon/lm87.c +0 -1
index ddad6fc..1f5cac5 100644
@@ -21,11 +21,12 @@
  *   http://www.national.com/pf/LM/LM87.html
  *
  * Some functions share pins, so not all functions are available at the same
- * time. Which are depends on the hardware setup. This driver assumes that

drivers/hwmon/lm87.c line 29

Quoted 10 lines
drivers/hwmon/lm87.c +6 -4
- * the BIOS configured the chip correctly. In that respect, it  differs from
- * the original driver (from lm_sensors for Linux 2.4), which would force the
- * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
- * chipset wiring.
+ * time. Which are depends on the hardware setup. This driver normally
+ * assumes that firmware configured the chip correctly. Where this is not
+ * the case, platform code must set the I2C client's platform_data to point
+ * to a u8 value to be written to the channel register. In that case the
+ * driver will assume that it is the only user of the chip and will reset
+ * it in the probe and remove functions.

I fail to see how setting the channel mode is related to resetting the chip. Not that I really get the point of resetting the chip in the first place, but if you want to do that, it should be independent of setting the channel mode.

Quoted 25 lines
drivers/hwmon/lm87.c +5 -2
  * For reference, here is the list of exclusive functions:
  *  - in0+in5 (default) or temp3
  *  - fan1 (default) or in6
@@ -731,7 +732,7 @@ static int lm87_probe(struct i2c_client *new_client,
 
 	/* Register sysfs hooks */
 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
-		goto exit_free;
+		goto exit_reset;
 
 	if (data->channel & CHAN_NO_FAN(0)) {
 		if ((err = device_create_file(&new_client->dev,
@@ -831,7 +832,10 @@ static int lm87_probe(struct i2c_client *new_client,
 exit_remove:
 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
 	sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
-exit_free:
+exit_reset:
+	if (new_client->dev.platform_data)
+		lm87_write_value(new_client, LM87_REG_CONFIG, 0x80);
+
 	kfree(data);
 exit:
 	return err;

drivers/hwmon/lm87.c static void lm87_init_client(struct i2c_client *client) · line 852

Quoted 8 lines
drivers/hwmon/lm87.c +4 -1
@@ -842,9 +846,17 @@ static void lm87_init_client(struct i2c_client *client)
 	struct lm87_data *data = i2c_get_clientdata(client);
 	u8 config;
 
-	data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+	if (client->dev.platform_data) {
+		config = 0x80;
+		lm87_write_value(client, LM87_REG_CONFIG, config);
+		data->channel = *(u8 *)client->dev.platform_data;

I'm not fond of that kind of cast. Which leads me to the following
porposal: define a proper structure controlling the LM87 device initialization.

struct lm87_platform_data {
	u8 channel_mode;
	unsigned int reset:1;
}

So that the caller has control over the channel mode and the resetting of the chip, in a clear and independent way. This is much better than (ab)using the platform_data pointer for everything.

Quoted 21 lines
drivers/hwmon/lm87.c +9 -1
+		lm87_write_value(client,
+				 LM87_REG_CHANNEL_MODE, data->channel);
+	} else {
+		data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+		config = lm87_read_value(client, LM87_REG_CONFIG);
+	}
 
-	config = lm87_read_value(client, LM87_REG_CONFIG);
 	if (!(config & 0x01)) {
 		int i;
 
@@ -882,6 +894,9 @@ static int lm87_remove(struct i2c_client *client)
 	sysfs_remove_group(&client->dev.kobj, &lm87_group);
 	sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
 
+	if (client->dev.platform_data)
+		lm87_write_value(client, LM87_REG_CONFIG, 0x80);
+
 	kfree(data);
 	return 0;
 }
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
In reply to Ben Hutchings
Quoted 3 lines · Jean Delvare

Hi Ben,

On Tue, 19 Aug 2008 16:02:29 +0100, Ben Hutchings wrote:

[...]

Quoted 14 lines
Quoted 14 lines
drivers/hwmon/lm87.c +3 -5
@@ -21,11 +21,12 @@
  *   http://www.national.com/pf/LM/LM87.html
  *
  * Some functions share pins, so not all functions are available at the same
- * time. Which are depends on the hardware setup. This driver assumes that
- * the BIOS configured the chip correctly. In that respect, it  differs from
- * the original driver (from lm_sensors for Linux 2.4), which would force the
- * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
- * chipset wiring.
+ * time. Which are depends on the hardware setup. This driver normally
+ * assumes that firmware configured the chip correctly. Where this is not
+ * the case, platform code must set the I2C client's platform_data to point
Quoted 8 lines

drivers/hwmon/lm87.c line 29

Quoted 3 lines
drivers/hwmon/lm87.c +3 -0
+ * to a u8 value to be written to the channel register. In that case the
+ * driver will assume that it is the only user of the chip and will reset
+ * it in the probe and remove functions.

I fail to see how setting the channel mode is related to resetting the chip. Not that I really get the point of resetting the chip in the first place, but if you want to do that, it should be independent of setting the channel mode.

I'm expecting that either
(1) firmware does most of the initialisation and may use the chip behind our back, so we make minimal configuration changes
or
(2) firmware does nothing and we are in exclusive control of the chip, so we initialise it from scratch and disable it when removed

In case (1) platform_data is not set and in case (2) it is. I don't think there is a third case where we would want to set the channel but not reset the chip.

Quoted 14 lines

drivers/hwmon/lm87.c static void lm87_init_client(struct i2c_client *client) · line 852

Quoted 9 lines
drivers/hwmon/lm87.c +4 -1
@@ -842,9 +846,17 @@ static void lm87_init_client(struct i2c_client *client)
 	struct lm87_data *data = i2c_get_clientdata(client);
 	u8 config;
 
-	data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+	if (client->dev.platform_data) {
+		config = 0x80;
+		lm87_write_value(client, LM87_REG_CONFIG, config);
+		data->channel = *(u8 *)client->dev.platform_data;

I'm not fond of that kind of cast. Which leads me to the following
porposal: define a proper structure controlling the LM87 device initialization.

Quoted 8 lines
struct lm87_platform_data {
	u8 channel_mode;
	unsigned int reset:1;
}

So that the caller has control over the channel mode and the resetting of the chip, in a clear and independent way. This is much better than (ab)using the platform_data pointer for everything.

[...]

This is what I proposed before. But you didn't seem so happy with it, so I changed to this minimal "structure" which doesn't even need a new header file.

Ben.

Signature · 8 lines
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#3 lore raw
In reply to Ben Hutchings

Hi Ben,

Quoted 28 lines · Ben Hutchings
Quoted 3 lines · Jean Delvare

Hi Ben,

On Tue, 19 Aug 2008 16:02:29 +0100, Ben Hutchings wrote:

[...]

Quoted 14 lines
Quoted 14 lines
drivers/hwmon/lm87.c +3 -5
@@ -21,11 +21,12 @@
  *   http://www.national.com/pf/LM/LM87.html
  *
  * Some functions share pins, so not all functions are available at the same
- * time. Which are depends on the hardware setup. This driver assumes that
- * the BIOS configured the chip correctly. In that respect, it  differs from
- * the original driver (from lm_sensors for Linux 2.4), which would force the
- * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
- * chipset wiring.
+ * time. Which are depends on the hardware setup. This driver normally
+ * assumes that firmware configured the chip correctly. Where this is not
+ * the case, platform code must set the I2C client's platform_data to point
Quoted 8 lines

drivers/hwmon/lm87.c line 29

Quoted 3 lines
drivers/hwmon/lm87.c +3 -0
+ * to a u8 value to be written to the channel register. In that case the
+ * driver will assume that it is the only user of the chip and will reset
+ * it in the probe and remove functions.

I fail to see how setting the channel mode is related to resetting the chip. Not that I really get the point of resetting the chip in the first place, but if you want to do that, it should be independent of setting the channel mode.

Quoted 6 lines

I'm expecting that either
(1) firmware does most of the initialisation and may use the chip behind our back, so we make minimal configuration changes
or
(2) firmware does nothing and we are in exclusive control of the chip, so we initialise it from scratch and disable it when removed

In fact there are many cases where the firmware (or actually BIOS) initializes the chip but doesn't access it in our back. These are independent things.

Quoted 4 lines

In case (1) platform_data is not set and in case (2) it is. I don't think there is a third case where we would want to set the channel but not reset the chip.

Hmm, OK, this makes some sense. I'm still not sure what is the point of resetting the chip though. If the firmware had initialized it, you loose the settings. If it didn't, the reset is a no-op.

And resetting the chip at removal time makes even less sense to me. While I would understand that you stop the monitoring (if and only if the chip wasn't already running at load time), resetting it but letting it run seems pointless.

Quoted 20 lines
Quoted 20 lines

drivers/hwmon/lm87.c static void lm87_init_client(struct i2c_client *client) · line 852

Quoted 10 lines
drivers/hwmon/lm87.c +4 -1
@@ -842,9 +846,17 @@ static void lm87_init_client(struct i2c_client *client)
 	struct lm87_data *data = i2c_get_clientdata(client);
 	u8 config;
 
-	data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+	if (client->dev.platform_data) {
+		config = 0x80;
+		lm87_write_value(client, LM87_REG_CONFIG, config);
+		data->channel = *(u8 *)client->dev.platform_data;

I'm not fond of that kind of cast. Which leads me to the following
porposal: define a proper structure controlling the LM87 device initialization.

struct lm87_platform_data {
	u8 channel_mode;
	unsigned int reset:1;
}
Quoted 8 lines
Quoted 3 lines

So that the caller has control over the channel mode and the resetting of the chip, in a clear and independent way. This is much better than (ab)using the platform_data pointer for everything.

[...]

This is what I proposed before. But you didn't seem so happy with it, so I changed to this minimal "structure" which doesn't even need a new header file.

As I recall, what you proposed before was a complete reorganization of struct lm87_data, with the aim to let the platform code initialize the whole chip, including all the limit registers, while we have the whole infrastructure to handle this in user-space already. It seems to me that you went from one extreme to the other. Now you don't even want to define a structure to transmit the initialization information from the platform to the driver.

Not that I care much... After all, you'll be the only user of that code, at least for now, and it can evolve later if others have different needs. But I am still worried that you are defining a non-intuitive interface and do not document it. If you were to add a paragraph to Documentation/hwmon/lm87 about how platform code can influence the initialization, I'd probably buy it.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#4 lore raw
In reply to Ben Hutchings
Quoted 3 lines · Jean Delvare

Hi Ben,

On Tue, 19 Aug 2008 19:30:36 +0100, Ben Hutchings wrote:

[...]

Quoted 7 lines
Quoted 3 lines

In case (1) platform_data is not set and in case (2) it is. I don't think there is a third case where we would want to set the channel but not reset the chip.

Hmm, OK, this makes some sense. I'm still not sure what is the point of resetting the chip though. If the firmware had initialized it, you loose the settings. If it didn't, the reset is a no-op.

It seems like a good idea to make sure we're starting from a known state.

Quoted 4 lines

And resetting the chip at removal time makes even less sense to me. While I would understand that you stop the monitoring (if and only if the chip wasn't already running at load time), resetting it but letting it run seems pointless.

Resetting it does stop it. But what I'll do in the next iteration is to restore the initial configuration register on removal.

[...]

Quoted 6 lines

Not that I care much... After all, you'll be the only user of that code, at least for now, and it can evolve later if others have different needs. But I am still worried that you are defining a non-intuitive interface and do not document it. If you were to add a paragraph to Documentation/hwmon/lm87 about how platform code can influence the initialization, I'd probably buy it.

[...]

I'll synch it with the header comment in lm87.c.

Ben.

Signature · 8 lines
Ben Hutchings, Senior Software Engineer, Solarflare Communications
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors