Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c: Convert the pca9539 driver to a new-style i2c driver

The new-style pca9539 driver implements the optional detect() callback
to cover the use cases of the legacy driver.

Warning: users will now have to use the force module parameter to get
the driver to attach to their device. That's not a bad thing as these
devices can't be detected anyway.

Note that this doesn't change the fact that this driver is deprecated
in favor of gpio/pca953x.

Signed-off-by: Jean Delvare <khali@linux-fr.org>

authored by

Jean Delvare and committed by
Jean Delvare
3d63430a 97addff6

+42 -77
+9 -1
Documentation/i2c/chips/pca9539
··· 7 7 Supported chips: 8 8 * Philips PCA9539 9 9 Prefix: 'pca9539' 10 - Addresses scanned: 0x74 - 0x77 10 + Addresses scanned: none 11 11 Datasheet: 12 12 http://www.semiconductors.philips.com/acrobat/datasheets/PCA9539_2.pdf 13 13 ··· 21 21 All 16 lines can be individually configured as an input or output. 22 22 The input sense can also be inverted. 23 23 The 16 lines are split between two bytes. 24 + 25 + 26 + Detection 27 + --------- 28 + 29 + The PCA9539 is difficult to detect and not commonly found in PC machines, 30 + so you have to pass the I2C bus and address of the installed PCA9539 31 + devices explicitly to the driver at load time via the force=... parameter. 24 32 25 33 26 34 Sysfs entries
+33 -76
drivers/i2c/chips/pca9539.c
··· 14 14 #include <linux/i2c.h> 15 15 #include <linux/hwmon-sysfs.h> 16 16 17 - /* Addresses to scan */ 18 - static unsigned short normal_i2c[] = {0x74, 0x75, 0x76, 0x77, I2C_CLIENT_END}; 17 + /* Addresses to scan: none, device is not autodetected */ 18 + static const unsigned short normal_i2c[] = { I2C_CLIENT_END }; 19 19 20 20 /* Insmod parameters */ 21 21 I2C_CLIENT_INSMOD_1(pca9539); ··· 30 30 PCA9539_INVERT_1 = 5, 31 31 PCA9539_DIRECTION_0 = 6, 32 32 PCA9539_DIRECTION_1 = 7, 33 - }; 34 - 35 - static int pca9539_attach_adapter(struct i2c_adapter *adapter); 36 - static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind); 37 - static int pca9539_detach_client(struct i2c_client *client); 38 - 39 - /* This is the driver that will be inserted */ 40 - static struct i2c_driver pca9539_driver = { 41 - .driver = { 42 - .name = "pca9539", 43 - }, 44 - .attach_adapter = pca9539_attach_adapter, 45 - .detach_client = pca9539_detach_client, 46 - }; 47 - 48 - struct pca9539_data { 49 - struct i2c_client client; 50 33 }; 51 34 52 35 /* following are the sysfs callback functions */ ··· 88 105 .attrs = pca9539_attributes, 89 106 }; 90 107 91 - static int pca9539_attach_adapter(struct i2c_adapter *adapter) 108 + /* Return 0 if detection is successful, -ENODEV otherwise */ 109 + static int pca9539_detect(struct i2c_client *client, int kind, 110 + struct i2c_board_info *info) 92 111 { 93 - return i2c_probe(adapter, &addr_data, pca9539_detect); 94 - } 95 - 96 - /* This function is called by i2c_probe */ 97 - static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind) 98 - { 99 - struct i2c_client *client; 100 - struct pca9539_data *data; 101 - int err = 0; 112 + struct i2c_adapter *adapter = client->adapter; 102 113 103 114 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 104 - goto exit; 115 + return -ENODEV; 105 116 106 - /* OK. For now, we presume we have a valid client. We now create the 107 - client structure, even though we cannot fill it completely yet. */ 108 - if (!(data = kzalloc(sizeof(struct pca9539_data), GFP_KERNEL))) { 109 - err = -ENOMEM; 110 - goto exit; 111 - } 112 - 113 - client = &data->client; 114 - i2c_set_clientdata(client, data); 115 - client->addr = address; 116 - client->adapter = adapter; 117 - client->driver = &pca9539_driver; 118 - 119 - if (kind < 0) { 120 - /* Detection: the pca9539 only has 8 registers (0-7). 121 - A read of 7 should succeed, but a read of 8 should fail. */ 122 - if ((i2c_smbus_read_byte_data(client, 7) < 0) || 123 - (i2c_smbus_read_byte_data(client, 8) >= 0)) 124 - goto exit_kfree; 125 - } 126 - 127 - strlcpy(client->name, "pca9539", I2C_NAME_SIZE); 128 - 129 - /* Tell the I2C layer a new client has arrived */ 130 - if ((err = i2c_attach_client(client))) 131 - goto exit_kfree; 132 - 133 - /* Register sysfs hooks */ 134 - err = sysfs_create_group(&client->dev.kobj, 135 - &pca9539_defattr_group); 136 - if (err) 137 - goto exit_detach; 117 + strlcpy(info->type, "pca9539", I2C_NAME_SIZE); 138 118 139 119 return 0; 140 - 141 - exit_detach: 142 - i2c_detach_client(client); 143 - exit_kfree: 144 - kfree(data); 145 - exit: 146 - return err; 147 120 } 148 121 149 - static int pca9539_detach_client(struct i2c_client *client) 122 + static int pca9539_probe(struct i2c_client *client, 123 + const struct i2c_device_id *id) 150 124 { 151 - int err; 125 + /* Register sysfs hooks */ 126 + return sysfs_create_group(&client->dev.kobj, 127 + &pca9539_defattr_group); 128 + } 152 129 130 + static int pca9539_remove(struct i2c_client *client) 131 + { 153 132 sysfs_remove_group(&client->dev.kobj, &pca9539_defattr_group); 154 - 155 - if ((err = i2c_detach_client(client))) 156 - return err; 157 - 158 - kfree(i2c_get_clientdata(client)); 159 133 return 0; 160 134 } 135 + 136 + static const struct i2c_device_id pca9539_id[] = { 137 + { "pca9539", 0 }, 138 + { } 139 + }; 140 + 141 + static struct i2c_driver pca9539_driver = { 142 + .driver = { 143 + .name = "pca9539", 144 + }, 145 + .probe = pca9539_probe, 146 + .remove = pca9539_remove, 147 + .id_table = pca9539_id, 148 + 149 + .detect = pca9539_detect, 150 + .address_data = &addr_data, 151 + }; 161 152 162 153 static int __init pca9539_init(void) 163 154 {