aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon
diff options
context:
space:
mode:
authorJean Delvare <khali@linux-fr.org>2008-07-16 13:30:17 -0400
committerJean Delvare <khali@mahadeva.delvare>2008-07-16 13:30:17 -0400
commitcb0c1af37996f3016e34e9c709e5f727646f7207 (patch)
treee23570869a8a8b728bd80022fa127d8e9f1f02cb /drivers/hwmon
parentccf37488322429bf8709f2227f3d48466add2b6b (diff)
hwmon: (w83791d) Convert to a new-style i2c driver
The new-style w83791d driver implements the optional detect() callback to cover the use cases of the legacy driver. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Marc Hulsman <m.hulsman@tudelft.nl>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r--drivers/hwmon/w83791d.c205
1 files changed, 71 insertions, 134 deletions
diff --git a/drivers/hwmon/w83791d.c b/drivers/hwmon/w83791d.c
index 85077c4c8039..e4e91c9d480a 100644
--- a/drivers/hwmon/w83791d.c
+++ b/drivers/hwmon/w83791d.c
@@ -247,7 +247,6 @@ static u8 div_to_reg(int nr, long val)
247} 247}
248 248
249struct w83791d_data { 249struct w83791d_data {
250 struct i2c_client client;
251 struct device *hwmon_dev; 250 struct device *hwmon_dev;
252 struct mutex update_lock; 251 struct mutex update_lock;
253 252
@@ -286,9 +285,11 @@ struct w83791d_data {
286 u8 vrm; /* hwmon-vid */ 285 u8 vrm; /* hwmon-vid */
287}; 286};
288 287
289static int w83791d_attach_adapter(struct i2c_adapter *adapter); 288static int w83791d_probe(struct i2c_client *client,
290static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind); 289 const struct i2c_device_id *id);
291static int w83791d_detach_client(struct i2c_client *client); 290static int w83791d_detect(struct i2c_client *client, int kind,
291 struct i2c_board_info *info);
292static int w83791d_remove(struct i2c_client *client);
292 293
293static int w83791d_read(struct i2c_client *client, u8 register); 294static int w83791d_read(struct i2c_client *client, u8 register);
294static int w83791d_write(struct i2c_client *client, u8 register, u8 value); 295static int w83791d_write(struct i2c_client *client, u8 register, u8 value);
@@ -300,12 +301,22 @@ static void w83791d_print_debug(struct w83791d_data *data, struct device *dev);
300 301
301static void w83791d_init_client(struct i2c_client *client); 302static void w83791d_init_client(struct i2c_client *client);
302 303
304static const struct i2c_device_id w83791d_id[] = {
305 { "w83791d", w83791d },
306 { }
307};
308MODULE_DEVICE_TABLE(i2c, w83791d_id);
309
303static struct i2c_driver w83791d_driver = { 310static struct i2c_driver w83791d_driver = {
311 .class = I2C_CLASS_HWMON,
304 .driver = { 312 .driver = {
305 .name = "w83791d", 313 .name = "w83791d",
306 }, 314 },
307 .attach_adapter = w83791d_attach_adapter, 315 .probe = w83791d_probe,
308 .detach_client = w83791d_detach_client, 316 .remove = w83791d_remove,
317 .id_table = w83791d_id,
318 .detect = w83791d_detect,
319 .address_data = &addr_data,
309}; 320};
310 321
311/* following are the sysfs callback functions */ 322/* following are the sysfs callback functions */
@@ -905,49 +916,12 @@ static const struct attribute_group w83791d_group = {
905 .attrs = w83791d_attributes, 916 .attrs = w83791d_attributes,
906}; 917};
907 918
908/* This function is called when:
909 * w83791d_driver is inserted (when this module is loaded), for each
910 available adapter
911 * when a new adapter is inserted (and w83791d_driver is still present) */
912static int w83791d_attach_adapter(struct i2c_adapter *adapter)
913{
914 if (!(adapter->class & I2C_CLASS_HWMON))
915 return 0;
916 return i2c_probe(adapter, &addr_data, w83791d_detect);
917}
918
919 919
920static int w83791d_create_subclient(struct i2c_adapter *adapter, 920static int w83791d_detect_subclients(struct i2c_client *client)
921 struct i2c_client *client, int addr,
922 struct i2c_client **sub_cli)
923{
924 int err;
925 struct i2c_client *sub_client;
926
927 (*sub_cli) = sub_client =
928 kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
929 if (!(sub_client)) {
930 return -ENOMEM;
931 }
932 sub_client->addr = 0x48 + addr;
933 i2c_set_clientdata(sub_client, NULL);
934 sub_client->adapter = adapter;
935 sub_client->driver = &w83791d_driver;
936 strlcpy(sub_client->name, "w83791d subclient", I2C_NAME_SIZE);
937 if ((err = i2c_attach_client(sub_client))) {
938 dev_err(&client->dev, "subclient registration "
939 "at address 0x%x failed\n", sub_client->addr);
940 kfree(sub_client);
941 return err;
942 }
943 return 0;
944}
945
946
947static int w83791d_detect_subclients(struct i2c_adapter *adapter, int address,
948 int kind, struct i2c_client *client)
949{ 921{
922 struct i2c_adapter *adapter = client->adapter;
950 struct w83791d_data *data = i2c_get_clientdata(client); 923 struct w83791d_data *data = i2c_get_clientdata(client);
924 int address = client->addr;
951 int i, id, err; 925 int i, id, err;
952 u8 val; 926 u8 val;
953 927
@@ -971,10 +945,7 @@ static int w83791d_detect_subclients(struct i2c_adapter *adapter, int address,
971 945
972 val = w83791d_read(client, W83791D_REG_I2C_SUBADDR); 946 val = w83791d_read(client, W83791D_REG_I2C_SUBADDR);
973 if (!(val & 0x08)) { 947 if (!(val & 0x08)) {
974 err = w83791d_create_subclient(adapter, client, 948 data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (val & 0x7));
975 val & 0x7, &data->lm75[0]);
976 if (err < 0)
977 goto error_sc_0;
978 } 949 }
979 if (!(val & 0x80)) { 950 if (!(val & 0x80)) {
980 if ((data->lm75[0] != NULL) && 951 if ((data->lm75[0] != NULL) &&
@@ -986,10 +957,8 @@ static int w83791d_detect_subclients(struct i2c_adapter *adapter, int address,
986 err = -ENODEV; 957 err = -ENODEV;
987 goto error_sc_1; 958 goto error_sc_1;
988 } 959 }
989 err = w83791d_create_subclient(adapter, client, 960 data->lm75[1] = i2c_new_dummy(adapter,
990 (val >> 4) & 0x7, &data->lm75[1]); 961 0x48 + ((val >> 4) & 0x7));
991 if (err < 0)
992 goto error_sc_1;
993 } 962 }
994 963
995 return 0; 964 return 0;
@@ -997,53 +966,31 @@ static int w83791d_detect_subclients(struct i2c_adapter *adapter, int address,
997/* Undo inits in case of errors */ 966/* Undo inits in case of errors */
998 967
999error_sc_1: 968error_sc_1:
1000 if (data->lm75[0] != NULL) { 969 if (data->lm75[0] != NULL)
1001 i2c_detach_client(data->lm75[0]); 970 i2c_unregister_device(data->lm75[0]);
1002 kfree(data->lm75[0]);
1003 }
1004error_sc_0: 971error_sc_0:
1005 return err; 972 return err;
1006} 973}
1007 974
1008 975
1009static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind) 976/* Return 0 if detection is successful, -ENODEV otherwise */
977static int w83791d_detect(struct i2c_client *client, int kind,
978 struct i2c_board_info *info)
1010{ 979{
1011 struct i2c_client *client; 980 struct i2c_adapter *adapter = client->adapter;
1012 struct device *dev; 981 int val1, val2;
1013 struct w83791d_data *data; 982 unsigned short address = client->addr;
1014 int i, val1, val2;
1015 int err = 0;
1016 const char *client_name = "";
1017 983
1018 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 984 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1019 goto error0; 985 return -ENODEV;
1020 } 986 }
1021 987
1022 /* OK. For now, we presume we have a valid client. We now create the
1023 client structure, even though we cannot fill it completely yet.
1024 But it allows us to access w83791d_{read,write}_value. */
1025 if (!(data = kzalloc(sizeof(struct w83791d_data), GFP_KERNEL))) {
1026 err = -ENOMEM;
1027 goto error0;
1028 }
1029
1030 client = &data->client;
1031 dev = &client->dev;
1032 i2c_set_clientdata(client, data);
1033 client->addr = address;
1034 client->adapter = adapter;
1035 client->driver = &w83791d_driver;
1036 mutex_init(&data->update_lock);
1037
1038 /* Now, we do the remaining detection. */
1039
1040 /* The w83791d may be stuck in some other bank than bank 0. This may 988 /* The w83791d may be stuck in some other bank than bank 0. This may
1041 make reading other information impossible. Specify a force=... 989 make reading other information impossible. Specify a force=...
1042 parameter, and the Winbond will be reset to the right bank. */ 990 parameter, and the Winbond will be reset to the right bank. */
1043 if (kind < 0) { 991 if (kind < 0) {
1044 if (w83791d_read(client, W83791D_REG_CONFIG) & 0x80) { 992 if (w83791d_read(client, W83791D_REG_CONFIG) & 0x80) {
1045 dev_dbg(dev, "Detection failed at step 1\n"); 993 return -ENODEV;
1046 goto error1;
1047 } 994 }
1048 val1 = w83791d_read(client, W83791D_REG_BANK); 995 val1 = w83791d_read(client, W83791D_REG_BANK);
1049 val2 = w83791d_read(client, W83791D_REG_CHIPMAN); 996 val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
@@ -1052,15 +999,13 @@ static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind)
1052 /* yes it is Bank0 */ 999 /* yes it is Bank0 */
1053 if (((!(val1 & 0x80)) && (val2 != 0xa3)) || 1000 if (((!(val1 & 0x80)) && (val2 != 0xa3)) ||
1054 ((val1 & 0x80) && (val2 != 0x5c))) { 1001 ((val1 & 0x80) && (val2 != 0x5c))) {
1055 dev_dbg(dev, "Detection failed at step 2\n"); 1002 return -ENODEV;
1056 goto error1;
1057 } 1003 }
1058 } 1004 }
1059 /* If Winbond chip, address of chip and W83791D_REG_I2C_ADDR 1005 /* If Winbond chip, address of chip and W83791D_REG_I2C_ADDR
1060 should match */ 1006 should match */
1061 if (w83791d_read(client, W83791D_REG_I2C_ADDR) != address) { 1007 if (w83791d_read(client, W83791D_REG_I2C_ADDR) != address) {
1062 dev_dbg(dev, "Detection failed at step 3\n"); 1008 return -ENODEV;
1063 goto error1;
1064 } 1009 }
1065 } 1010 }
1066 1011
@@ -1075,30 +1020,33 @@ static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind)
1075 /* get vendor ID */ 1020 /* get vendor ID */
1076 val2 = w83791d_read(client, W83791D_REG_CHIPMAN); 1021 val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
1077 if (val2 != 0x5c) { /* the vendor is NOT Winbond */ 1022 if (val2 != 0x5c) { /* the vendor is NOT Winbond */
1078 dev_dbg(dev, "Detection failed at step 4\n"); 1023 return -ENODEV;
1079 goto error1;
1080 } 1024 }
1081 val1 = w83791d_read(client, W83791D_REG_WCHIPID); 1025 val1 = w83791d_read(client, W83791D_REG_WCHIPID);
1082 if (val1 == 0x71) { 1026 if (val1 == 0x71) {
1083 kind = w83791d; 1027 kind = w83791d;
1084 } else { 1028 } else {
1085 if (kind == 0) 1029 if (kind == 0)
1086 dev_warn(dev, 1030 dev_warn(&adapter->dev,
1087 "w83791d: Ignoring 'force' parameter " 1031 "w83791d: Ignoring 'force' parameter "
1088 "for unknown chip at adapter %d, " 1032 "for unknown chip at adapter %d, "
1089 "address 0x%02x\n", 1033 "address 0x%02x\n",
1090 i2c_adapter_id(adapter), address); 1034 i2c_adapter_id(adapter), address);
1091 goto error1; 1035 return -ENODEV;
1092 } 1036 }
1093 } 1037 }
1094 1038
1095 if (kind == w83791d) { 1039 strlcpy(info->type, "w83791d", I2C_NAME_SIZE);
1096 client_name = "w83791d"; 1040
1097 } else { 1041 return 0;
1098 dev_err(dev, "w83791d: Internal error: unknown kind (%d)?!?\n", 1042}
1099 kind); 1043
1100 goto error1; 1044static int w83791d_probe(struct i2c_client *client,
1101 } 1045 const struct i2c_device_id *id)
1046{
1047 struct w83791d_data *data;
1048 struct device *dev = &client->dev;
1049 int i, val1, err;
1102 1050
1103#ifdef DEBUG 1051#ifdef DEBUG
1104 val1 = w83791d_read(client, W83791D_REG_DID_VID4); 1052 val1 = w83791d_read(client, W83791D_REG_DID_VID4);
@@ -1106,15 +1054,18 @@ static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind)
1106 (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1); 1054 (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1);
1107#endif 1055#endif
1108 1056
1109 /* Fill in the remaining client fields and put into the global list */ 1057 data = kzalloc(sizeof(struct w83791d_data), GFP_KERNEL);
1110 strlcpy(client->name, client_name, I2C_NAME_SIZE); 1058 if (!data) {
1059 err = -ENOMEM;
1060 goto error0;
1061 }
1111 1062
1112 /* Tell the I2C layer a new client has arrived */ 1063 i2c_set_clientdata(client, data);
1113 if ((err = i2c_attach_client(client))) 1064 mutex_init(&data->update_lock);
1114 goto error1;
1115 1065
1116 if ((err = w83791d_detect_subclients(adapter, address, kind, client))) 1066 err = w83791d_detect_subclients(client);
1117 goto error2; 1067 if (err)
1068 goto error1;
1118 1069
1119 /* Initialize the chip */ 1070 /* Initialize the chip */
1120 w83791d_init_client(client); 1071 w83791d_init_client(client);
@@ -1141,43 +1092,29 @@ static int w83791d_detect(struct i2c_adapter *adapter, int address, int kind)
1141error4: 1092error4:
1142 sysfs_remove_group(&client->dev.kobj, &w83791d_group); 1093 sysfs_remove_group(&client->dev.kobj, &w83791d_group);
1143error3: 1094error3:
1144 if (data->lm75[0] != NULL) { 1095 if (data->lm75[0] != NULL)
1145 i2c_detach_client(data->lm75[0]); 1096 i2c_unregister_device(data->lm75[0]);
1146 kfree(data->lm75[0]); 1097 if (data->lm75[1] != NULL)
1147 } 1098 i2c_unregister_device(data->lm75[1]);
1148 if (data->lm75[1] != NULL) {
1149 i2c_detach_client(data->lm75[1]);
1150 kfree(data->lm75[1]);
1151 }
1152error2:
1153 i2c_detach_client(client);
1154error1: 1099error1:
1155 kfree(data); 1100 kfree(data);
1156error0: 1101error0:
1157 return err; 1102 return err;
1158} 1103}
1159 1104
1160static int w83791d_detach_client(struct i2c_client *client) 1105static int w83791d_remove(struct i2c_client *client)
1161{ 1106{
1162 struct w83791d_data *data = i2c_get_clientdata(client); 1107 struct w83791d_data *data = i2c_get_clientdata(client);
1163 int err;
1164
1165 /* main client */
1166 if (data) {
1167 hwmon_device_unregister(data->hwmon_dev);
1168 sysfs_remove_group(&client->dev.kobj, &w83791d_group);
1169 }
1170 1108
1171 if ((err = i2c_detach_client(client))) 1109 hwmon_device_unregister(data->hwmon_dev);
1172 return err; 1110 sysfs_remove_group(&client->dev.kobj, &w83791d_group);
1173 1111
1174 /* main client */ 1112 if (data->lm75[0] != NULL)
1175 if (data) 1113 i2c_unregister_device(data->lm75[0]);
1176 kfree(data); 1114 if (data->lm75[1] != NULL)
1177 /* subclient */ 1115 i2c_unregister_device(data->lm75[1]);
1178 else
1179 kfree(client);
1180 1116
1117 kfree(data);
1181 return 0; 1118 return 0;
1182} 1119}
1183 1120