diff options
Diffstat (limited to 'drivers/w1/masters/ds2482.c')
-rw-r--r-- | drivers/w1/masters/ds2482.c | 104 |
1 files changed, 44 insertions, 60 deletions
diff --git a/drivers/w1/masters/ds2482.c b/drivers/w1/masters/ds2482.c index 0fd5820d5c61..df52cb355f7d 100644 --- a/drivers/w1/masters/ds2482.c +++ b/drivers/w1/masters/ds2482.c | |||
@@ -94,21 +94,31 @@ static const u8 ds2482_chan_rd[8] = | |||
94 | #define DS2482_REG_STS_1WB 0x01 | 94 | #define DS2482_REG_STS_1WB 0x01 |
95 | 95 | ||
96 | 96 | ||
97 | static int ds2482_attach_adapter(struct i2c_adapter *adapter); | 97 | static int ds2482_probe(struct i2c_client *client, |
98 | static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind); | 98 | const struct i2c_device_id *id); |
99 | static int ds2482_detach_client(struct i2c_client *client); | 99 | static int ds2482_detect(struct i2c_client *client, int kind, |
100 | struct i2c_board_info *info); | ||
101 | static int ds2482_remove(struct i2c_client *client); | ||
100 | 102 | ||
101 | 103 | ||
102 | /** | 104 | /** |
103 | * Driver data (common to all clients) | 105 | * Driver data (common to all clients) |
104 | */ | 106 | */ |
107 | static const struct i2c_device_id ds2482_id[] = { | ||
108 | { "ds2482", 0 }, | ||
109 | { } | ||
110 | }; | ||
111 | |||
105 | static struct i2c_driver ds2482_driver = { | 112 | static struct i2c_driver ds2482_driver = { |
106 | .driver = { | 113 | .driver = { |
107 | .owner = THIS_MODULE, | 114 | .owner = THIS_MODULE, |
108 | .name = "ds2482", | 115 | .name = "ds2482", |
109 | }, | 116 | }, |
110 | .attach_adapter = ds2482_attach_adapter, | 117 | .probe = ds2482_probe, |
111 | .detach_client = ds2482_detach_client, | 118 | .remove = ds2482_remove, |
119 | .id_table = ds2482_id, | ||
120 | .detect = ds2482_detect, | ||
121 | .address_data = &addr_data, | ||
112 | }; | 122 | }; |
113 | 123 | ||
114 | /* | 124 | /* |
@@ -124,7 +134,7 @@ struct ds2482_w1_chan { | |||
124 | }; | 134 | }; |
125 | 135 | ||
126 | struct ds2482_data { | 136 | struct ds2482_data { |
127 | struct i2c_client client; | 137 | struct i2c_client *client; |
128 | struct mutex access_lock; | 138 | struct mutex access_lock; |
129 | 139 | ||
130 | /* 1-wire interface(s) */ | 140 | /* 1-wire interface(s) */ |
@@ -147,7 +157,7 @@ struct ds2482_data { | |||
147 | static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) | 157 | static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) |
148 | { | 158 | { |
149 | if (pdev->read_prt != read_ptr) { | 159 | if (pdev->read_prt != read_ptr) { |
150 | if (i2c_smbus_write_byte_data(&pdev->client, | 160 | if (i2c_smbus_write_byte_data(pdev->client, |
151 | DS2482_CMD_SET_READ_PTR, | 161 | DS2482_CMD_SET_READ_PTR, |
152 | read_ptr) < 0) | 162 | read_ptr) < 0) |
153 | return -1; | 163 | return -1; |
@@ -167,7 +177,7 @@ static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) | |||
167 | */ | 177 | */ |
168 | static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) | 178 | static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) |
169 | { | 179 | { |
170 | if (i2c_smbus_write_byte(&pdev->client, cmd) < 0) | 180 | if (i2c_smbus_write_byte(pdev->client, cmd) < 0) |
171 | return -1; | 181 | return -1; |
172 | 182 | ||
173 | pdev->read_prt = DS2482_PTR_CODE_STATUS; | 183 | pdev->read_prt = DS2482_PTR_CODE_STATUS; |
@@ -187,7 +197,7 @@ static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) | |||
187 | static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, | 197 | static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, |
188 | u8 cmd, u8 byte) | 198 | u8 cmd, u8 byte) |
189 | { | 199 | { |
190 | if (i2c_smbus_write_byte_data(&pdev->client, cmd, byte) < 0) | 200 | if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) |
191 | return -1; | 201 | return -1; |
192 | 202 | ||
193 | /* all cmds leave in STATUS, except CONFIG */ | 203 | /* all cmds leave in STATUS, except CONFIG */ |
@@ -216,7 +226,7 @@ static int ds2482_wait_1wire_idle(struct ds2482_data *pdev) | |||
216 | 226 | ||
217 | if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) { | 227 | if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) { |
218 | do { | 228 | do { |
219 | temp = i2c_smbus_read_byte(&pdev->client); | 229 | temp = i2c_smbus_read_byte(pdev->client); |
220 | } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && | 230 | } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && |
221 | (++retries < DS2482_WAIT_IDLE_TIMEOUT)); | 231 | (++retries < DS2482_WAIT_IDLE_TIMEOUT)); |
222 | } | 232 | } |
@@ -238,13 +248,13 @@ static int ds2482_wait_1wire_idle(struct ds2482_data *pdev) | |||
238 | */ | 248 | */ |
239 | static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) | 249 | static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) |
240 | { | 250 | { |
241 | if (i2c_smbus_write_byte_data(&pdev->client, DS2482_CMD_CHANNEL_SELECT, | 251 | if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, |
242 | ds2482_chan_wr[channel]) < 0) | 252 | ds2482_chan_wr[channel]) < 0) |
243 | return -1; | 253 | return -1; |
244 | 254 | ||
245 | pdev->read_prt = DS2482_PTR_CODE_CHANNEL; | 255 | pdev->read_prt = DS2482_PTR_CODE_CHANNEL; |
246 | pdev->channel = -1; | 256 | pdev->channel = -1; |
247 | if (i2c_smbus_read_byte(&pdev->client) == ds2482_chan_rd[channel]) { | 257 | if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) { |
248 | pdev->channel = channel; | 258 | pdev->channel = channel; |
249 | return 0; | 259 | return 0; |
250 | } | 260 | } |
@@ -368,7 +378,7 @@ static u8 ds2482_w1_read_byte(void *data) | |||
368 | ds2482_select_register(pdev, DS2482_PTR_CODE_DATA); | 378 | ds2482_select_register(pdev, DS2482_PTR_CODE_DATA); |
369 | 379 | ||
370 | /* Read the data byte */ | 380 | /* Read the data byte */ |
371 | result = i2c_smbus_read_byte(&pdev->client); | 381 | result = i2c_smbus_read_byte(pdev->client); |
372 | 382 | ||
373 | mutex_unlock(&pdev->access_lock); | 383 | mutex_unlock(&pdev->access_lock); |
374 | 384 | ||
@@ -415,47 +425,38 @@ static u8 ds2482_w1_reset_bus(void *data) | |||
415 | } | 425 | } |
416 | 426 | ||
417 | 427 | ||
418 | /** | 428 | static int ds2482_detect(struct i2c_client *client, int kind, |
419 | * Called to see if the device exists on an i2c bus. | 429 | struct i2c_board_info *info) |
420 | */ | ||
421 | static int ds2482_attach_adapter(struct i2c_adapter *adapter) | ||
422 | { | 430 | { |
423 | return i2c_probe(adapter, &addr_data, ds2482_detect); | 431 | if (!i2c_check_functionality(client->adapter, |
424 | } | 432 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA | |
433 | I2C_FUNC_SMBUS_BYTE)) | ||
434 | return -ENODEV; | ||
425 | 435 | ||
436 | strlcpy(info->type, "ds2482", I2C_NAME_SIZE); | ||
426 | 437 | ||
427 | /* | 438 | return 0; |
428 | * The following function does more than just detection. If detection | 439 | } |
429 | * succeeds, it also registers the new chip. | 440 | |
430 | */ | 441 | static int ds2482_probe(struct i2c_client *client, |
431 | static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind) | 442 | const struct i2c_device_id *id) |
432 | { | 443 | { |
433 | struct ds2482_data *data; | 444 | struct ds2482_data *data; |
434 | struct i2c_client *new_client; | 445 | int err = -ENODEV; |
435 | int err = 0; | ||
436 | int temp1; | 446 | int temp1; |
437 | int idx; | 447 | int idx; |
438 | 448 | ||
439 | if (!i2c_check_functionality(adapter, | ||
440 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA | | ||
441 | I2C_FUNC_SMBUS_BYTE)) | ||
442 | goto exit; | ||
443 | |||
444 | if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) { | 449 | if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) { |
445 | err = -ENOMEM; | 450 | err = -ENOMEM; |
446 | goto exit; | 451 | goto exit; |
447 | } | 452 | } |
448 | 453 | ||
449 | new_client = &data->client; | 454 | data->client = client; |
450 | i2c_set_clientdata(new_client, data); | 455 | i2c_set_clientdata(client, data); |
451 | new_client->addr = address; | ||
452 | new_client->driver = &ds2482_driver; | ||
453 | new_client->adapter = adapter; | ||
454 | 456 | ||
455 | /* Reset the device (sets the read_ptr to status) */ | 457 | /* Reset the device (sets the read_ptr to status) */ |
456 | if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { | 458 | if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { |
457 | dev_dbg(&adapter->dev, "DS2482 reset failed at 0x%02x.\n", | 459 | dev_warn(&client->dev, "DS2482 reset failed.\n"); |
458 | address); | ||
459 | goto exit_free; | 460 | goto exit_free; |
460 | } | 461 | } |
461 | 462 | ||
@@ -463,10 +464,10 @@ static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind) | |||
463 | ndelay(525); | 464 | ndelay(525); |
464 | 465 | ||
465 | /* Read the status byte - only reset bit and line should be set */ | 466 | /* Read the status byte - only reset bit and line should be set */ |
466 | temp1 = i2c_smbus_read_byte(new_client); | 467 | temp1 = i2c_smbus_read_byte(client); |
467 | if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { | 468 | if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { |
468 | dev_dbg(&adapter->dev, "DS2482 (0x%02x) reset status " | 469 | dev_warn(&client->dev, "DS2482 reset status " |
469 | "0x%02X - not a DS2482\n", address, temp1); | 470 | "0x%02X - not a DS2482\n", temp1); |
470 | goto exit_free; | 471 | goto exit_free; |
471 | } | 472 | } |
472 | 473 | ||
@@ -478,16 +479,8 @@ static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind) | |||
478 | /* Set all config items to 0 (off) */ | 479 | /* Set all config items to 0 (off) */ |
479 | ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 0xF0); | 480 | ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 0xF0); |
480 | 481 | ||
481 | /* We can fill in the remaining client fields */ | ||
482 | snprintf(new_client->name, sizeof(new_client->name), "ds2482-%d00", | ||
483 | data->w1_count); | ||
484 | |||
485 | mutex_init(&data->access_lock); | 482 | mutex_init(&data->access_lock); |
486 | 483 | ||
487 | /* Tell the I2C layer a new client has arrived */ | ||
488 | if ((err = i2c_attach_client(new_client))) | ||
489 | goto exit_free; | ||
490 | |||
491 | /* Register 1-wire interface(s) */ | 484 | /* Register 1-wire interface(s) */ |
492 | for (idx = 0; idx < data->w1_count; idx++) { | 485 | for (idx = 0; idx < data->w1_count; idx++) { |
493 | data->w1_ch[idx].pdev = data; | 486 | data->w1_ch[idx].pdev = data; |
@@ -511,8 +504,6 @@ static int ds2482_detect(struct i2c_adapter *adapter, int address, int kind) | |||
511 | return 0; | 504 | return 0; |
512 | 505 | ||
513 | exit_w1_remove: | 506 | exit_w1_remove: |
514 | i2c_detach_client(new_client); | ||
515 | |||
516 | for (idx = 0; idx < data->w1_count; idx++) { | 507 | for (idx = 0; idx < data->w1_count; idx++) { |
517 | if (data->w1_ch[idx].pdev != NULL) | 508 | if (data->w1_ch[idx].pdev != NULL) |
518 | w1_remove_master_device(&data->w1_ch[idx].w1_bm); | 509 | w1_remove_master_device(&data->w1_ch[idx].w1_bm); |
@@ -523,10 +514,10 @@ exit: | |||
523 | return err; | 514 | return err; |
524 | } | 515 | } |
525 | 516 | ||
526 | static int ds2482_detach_client(struct i2c_client *client) | 517 | static int ds2482_remove(struct i2c_client *client) |
527 | { | 518 | { |
528 | struct ds2482_data *data = i2c_get_clientdata(client); | 519 | struct ds2482_data *data = i2c_get_clientdata(client); |
529 | int err, idx; | 520 | int idx; |
530 | 521 | ||
531 | /* Unregister the 1-wire bridge(s) */ | 522 | /* Unregister the 1-wire bridge(s) */ |
532 | for (idx = 0; idx < data->w1_count; idx++) { | 523 | for (idx = 0; idx < data->w1_count; idx++) { |
@@ -534,13 +525,6 @@ static int ds2482_detach_client(struct i2c_client *client) | |||
534 | w1_remove_master_device(&data->w1_ch[idx].w1_bm); | 525 | w1_remove_master_device(&data->w1_ch[idx].w1_bm); |
535 | } | 526 | } |
536 | 527 | ||
537 | /* Detach the i2c device */ | ||
538 | if ((err = i2c_detach_client(client))) { | ||
539 | dev_err(&client->dev, | ||
540 | "Deregistration failed, client not detached.\n"); | ||
541 | return err; | ||
542 | } | ||
543 | |||
544 | /* Free the memory */ | 528 | /* Free the memory */ |
545 | kfree(data); | 529 | kfree(data); |
546 | return 0; | 530 | return 0; |