[i2c] [PATCH] i2c-amd8111: Proposed cleanups

Jean Delvare khali at linux-fr.org
Mon Jan 29 14:35:00 CET 2007


Proposed cleanups to the i2c-amd8111 SMBus driver:
* Fold long lines.
* Add an explicit mask when writing the low byte of a word.
* Use I2C_SMBUS_BLOCK_MAX instead of hardcoding 32.
* Discard extra blank lines.
* Use boolean not instead of bitwise not for bit tests, it's clearer.
* Return -EBUSY rather than -1 on I/O resource conflict.
* Fix a race on device registration, initialization should be done
  before the bus is registered.

Signed-off-by: Jean Delvare <khali at linux-fr.org>
---
I've got the idea of cleaning up this driver after reviewing another
driver for which i2c-amd8111 was used as an example, and a number of
mistakes were copied from it. I don't have a device to test my changes
though, so I'd appreciate if someone else could test them.

 drivers/i2c/busses/i2c-amd8111.c |   70 +++++++++++++++++++++++---------------
 1 file changed, 42 insertions(+), 28 deletions(-)

--- linux-2.6.20-rc6.orig/drivers/i2c/busses/i2c-amd8111.c	2007-01-29 14:20:48.000000000 +0100
+++ linux-2.6.20-rc6/drivers/i2c/busses/i2c-amd8111.c	2007-01-29 14:21:06.000000000 +0100
@@ -76,7 +76,8 @@ static unsigned int amd_ec_wait_write(st
 		udelay(1);
 
 	if (!timeout) {
-		dev_warn(&smbus->dev->dev, "Timeout while waiting for IBF to clear\n");
+		dev_warn(&smbus->dev->dev,
+			 "Timeout while waiting for IBF to clear\n");
 		return -1;
 	}
 
@@ -91,14 +92,16 @@ static unsigned int amd_ec_wait_read(str
 		udelay(1);
 
 	if (!timeout) {
-		dev_warn(&smbus->dev->dev, "Timeout while waiting for OBF to set\n");
+		dev_warn(&smbus->dev->dev,
+			 "Timeout while waiting for OBF to set\n");
 		return -1;
 	}
 
 	return 0;
 }
 
-static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address, unsigned char *data)
+static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address,
+		unsigned char *data)
 {
 	if (amd_ec_wait_write(smbus))
 		return -1;
@@ -115,7 +118,8 @@ static unsigned int amd_ec_read(struct a
 	return 0;
 }
 
-static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address, unsigned char data)
+static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address,
+		unsigned char data)
 {
 	if (amd_ec_wait_write(smbus))
 		return -1;
@@ -175,18 +179,19 @@ static unsigned int amd_ec_write(struct 
 #define AMD_SMB_PRTCL_PEC		0x80
 
 
-static s32 amd8111_access(struct i2c_adapter * adap, u16 addr, unsigned short flags,
-		char read_write, u8 command, int size, union i2c_smbus_data * data)
+static s32 amd8111_access(struct i2c_adapter * adap, u16 addr,
+		unsigned short flags, char read_write, u8 command, int size,
+		union i2c_smbus_data * data)
 {
 	struct amd_smbus *smbus = adap->algo_data;
 	unsigned char protocol, len, pec, temp[2];
 	int i;
 
-	protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ : AMD_SMB_PRTCL_WRITE;
+	protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ
+						  : AMD_SMB_PRTCL_WRITE;
 	pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
 
 	switch (size) {
-
 		case I2C_SMBUS_QUICK:
 			protocol |= AMD_SMB_PRTCL_QUICK;
 			read_write = I2C_SMBUS_WRITE;
@@ -208,8 +213,10 @@ static s32 amd8111_access(struct i2c_ada
 		case I2C_SMBUS_WORD_DATA:
 			amd_ec_write(smbus, AMD_SMB_CMD, command);
 			if (read_write == I2C_SMBUS_WRITE) {
-				amd_ec_write(smbus, AMD_SMB_DATA, data->word);
-				amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
+				amd_ec_write(smbus, AMD_SMB_DATA,
+					     data->word & 0xff);
+				amd_ec_write(smbus, AMD_SMB_DATA + 1,
+					     data->word >> 8);
 			}
 			protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
 			break;
@@ -217,27 +224,31 @@ static s32 amd8111_access(struct i2c_ada
 		case I2C_SMBUS_BLOCK_DATA:
 			amd_ec_write(smbus, AMD_SMB_CMD, command);
 			if (read_write == I2C_SMBUS_WRITE) {
-				len = min_t(u8, data->block[0], 32);
+				len = min_t(u8, data->block[0],
+					    I2C_SMBUS_BLOCK_MAX);
 				amd_ec_write(smbus, AMD_SMB_BCNT, len);
 				for (i = 0; i < len; i++)
-					amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
+					amd_ec_write(smbus, AMD_SMB_DATA + i,
+						     data->block[i + 1]);
 			}
 			protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
 			break;
 
 		case I2C_SMBUS_I2C_BLOCK_DATA:
-			len = min_t(u8, data->block[0], 32);
+			len = min_t(u8, data->block[0],
+				    I2C_SMBUS_BLOCK_MAX);
 			amd_ec_write(smbus, AMD_SMB_CMD, command);
 			amd_ec_write(smbus, AMD_SMB_BCNT, len);
 			if (read_write == I2C_SMBUS_WRITE)
 				for (i = 0; i < len; i++)
-					amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
+					amd_ec_write(smbus, AMD_SMB_DATA + i,
+						     data->block[i + 1]);
 			protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
 			break;
 
 		case I2C_SMBUS_PROC_CALL:
 			amd_ec_write(smbus, AMD_SMB_CMD, command);
-			amd_ec_write(smbus, AMD_SMB_DATA, data->word);
+			amd_ec_write(smbus, AMD_SMB_DATA, data->word & 0xff);
 			amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
 			protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
 			read_write = I2C_SMBUS_READ;
@@ -248,7 +259,8 @@ static s32 amd8111_access(struct i2c_ada
 			amd_ec_write(smbus, AMD_SMB_CMD, command);
 			amd_ec_write(smbus, AMD_SMB_BCNT, len);
 			for (i = 0; i < len; i++)
-				amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
+				amd_ec_write(smbus, AMD_SMB_DATA + i,
+					     data->block[i + 1]);
 			protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
 			read_write = I2C_SMBUS_READ;
 			break;
@@ -280,7 +292,6 @@ static s32 amd8111_access(struct i2c_ada
 		return 0;
 
 	switch (size) {
-
 		case I2C_SMBUS_BYTE:
 		case I2C_SMBUS_BYTE_DATA:
 			amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
@@ -296,10 +307,11 @@ static s32 amd8111_access(struct i2c_ada
 		case I2C_SMBUS_BLOCK_DATA:
 		case I2C_SMBUS_BLOCK_PROC_CALL:
 			amd_ec_read(smbus, AMD_SMB_BCNT, &len);
-			len = min_t(u8, len, 32);
+			len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
 		case I2C_SMBUS_I2C_BLOCK_DATA:
 			for (i = 0; i < len; i++)
-				amd_ec_read(smbus, AMD_SMB_DATA + i, data->block + i + 1);
+				amd_ec_read(smbus, AMD_SMB_DATA + i,
+					    data->block + i + 1);
 			data->block[0] = len;
 			break;
 	}
@@ -310,7 +322,8 @@ static s32 amd8111_access(struct i2c_ada
 
 static u32 amd8111_func(struct i2c_adapter *adapter)
 {
-	return	I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
+	return	I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
+		I2C_FUNC_SMBUS_BYTE_DATA |
 		I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
 		I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
 		I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_HWPEC_CALC;
@@ -329,12 +342,13 @@ static struct pci_device_id amd8111_ids[
 
 MODULE_DEVICE_TABLE (pci, amd8111_ids);
 
-static int __devinit amd8111_probe(struct pci_dev *dev, const struct pci_device_id *id)
+static int __devinit amd8111_probe(struct pci_dev *dev,
+		const struct pci_device_id *id)
 {
 	struct amd_smbus *smbus;
-	int error = -ENODEV;
+	int error;
 
-	if (~pci_resource_flags(dev, 0) & IORESOURCE_IO)
+	if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
 		return -ENODEV;
 
 	smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
@@ -345,8 +359,10 @@ static int __devinit amd8111_probe(struc
 	smbus->base = pci_resource_start(dev, 0);
 	smbus->size = pci_resource_len(dev, 0);
 
-	if (!request_region(smbus->base, smbus->size, amd8111_driver.name))
+	if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
+		error = -EBUSY;
 		goto out_kfree;
+	}
 
 	smbus->adapter.owner = THIS_MODULE;
 	snprintf(smbus->adapter.name, I2C_NAME_SIZE,
@@ -359,11 +375,11 @@ static int __devinit amd8111_probe(struc
 	/* set up the driverfs linkage to our parent device */
 	smbus->adapter.dev.parent = &dev->dev;
 
+	pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
 	error = i2c_add_adapter(&smbus->adapter);
 	if (error)
 		goto out_release_region;
 
-	pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
 	pci_set_drvdata(dev, smbus);
 	return 0;
 
@@ -371,10 +387,9 @@ static int __devinit amd8111_probe(struc
 	release_region(smbus->base, smbus->size);
  out_kfree:
 	kfree(smbus);
-	return -1;
+	return error;
 }
 
-
 static void __devexit amd8111_remove(struct pci_dev *dev)
 {
 	struct amd_smbus *smbus = pci_get_drvdata(dev);
@@ -396,7 +411,6 @@ static int __init i2c_amd8111_init(void)
 	return pci_register_driver(&amd8111_driver);
 }
 
-
 static void __exit i2c_amd8111_exit(void)
 {
 	pci_unregister_driver(&amd8111_driver);


-- 
Jean Delvare



More information about the i2c mailing list