| .. | .. |
|---|
| 6 | 6 | * Copyright (C) 2008 Wolfram Sang, Pengutronix |
|---|
| 7 | 7 | */ |
|---|
| 8 | 8 | |
|---|
| 9 | +#define DEBUG |
|---|
| 9 | 10 | #include <linux/kernel.h> |
|---|
| 10 | 11 | #include <linux/init.h> |
|---|
| 11 | 12 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 106 | 107 | module_param_named(write_timeout, at24_write_timeout, uint, 0); |
|---|
| 107 | 108 | MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)"); |
|---|
| 108 | 109 | |
|---|
| 110 | +//Ben |
|---|
| 111 | +struct at24_data *at24_private=NULL; |
|---|
| 109 | 112 | struct at24_chip_data { |
|---|
| 110 | 113 | /* |
|---|
| 111 | 114 | * these fields mirror their equivalents in |
|---|
| .. | .. |
|---|
| 421 | 424 | return 0; |
|---|
| 422 | 425 | } |
|---|
| 423 | 426 | |
|---|
| 427 | +//add ben |
|---|
| 428 | +static ssize_t at24_read_private(struct at24_data *at24, |
|---|
| 429 | + char *buf, loff_t off, size_t count) |
|---|
| 430 | +{ |
|---|
| 431 | + ssize_t retval = 0; |
|---|
| 432 | + |
|---|
| 433 | + if (unlikely(!count)) |
|---|
| 434 | + return count; |
|---|
| 435 | + |
|---|
| 436 | + if (off + count > at24->byte_len) |
|---|
| 437 | + return -EINVAL; |
|---|
| 438 | + |
|---|
| 439 | + /* |
|---|
| 440 | + * Read data from chip, protecting against concurrent updates |
|---|
| 441 | + * from this host, but not from other I2C masters. |
|---|
| 442 | + */ |
|---|
| 443 | + mutex_lock(&at24->lock); |
|---|
| 444 | + |
|---|
| 445 | + while (count) { |
|---|
| 446 | + ssize_t status; |
|---|
| 447 | + |
|---|
| 448 | + //status = at24_eeprom_read_i2c(at24, buf, off, count); |
|---|
| 449 | + status = at24_regmap_read(at24, buf, off, count); |
|---|
| 450 | + if (status <= 0) { |
|---|
| 451 | + if (retval == 0) |
|---|
| 452 | + retval = status; |
|---|
| 453 | + break; |
|---|
| 454 | + } |
|---|
| 455 | + buf += status; |
|---|
| 456 | + off += status; |
|---|
| 457 | + count -= status; |
|---|
| 458 | + retval += status; |
|---|
| 459 | + } |
|---|
| 460 | + |
|---|
| 461 | + mutex_unlock(&at24->lock); |
|---|
| 462 | + |
|---|
| 463 | + return retval; |
|---|
| 464 | +} |
|---|
| 465 | + |
|---|
| 466 | +#if 0 |
|---|
| 467 | +static unsigned char AscToHex(unsigned char aChar) |
|---|
| 468 | +{ |
|---|
| 469 | + if((aChar>=0x30)&&(aChar<=0x39)) |
|---|
| 470 | + aChar -= 0x30; |
|---|
| 471 | + else if((aChar>=0x41)&&(aChar<=0x46)) |
|---|
| 472 | + aChar -= 0x37; |
|---|
| 473 | + else if((aChar>=0x61)&&(aChar<=0x66)) |
|---|
| 474 | + aChar -= 0x57; |
|---|
| 475 | + else aChar = 0xff; |
|---|
| 476 | + |
|---|
| 477 | + return aChar; |
|---|
| 478 | +} |
|---|
| 479 | +#endif |
|---|
| 480 | + |
|---|
| 481 | +#if 0 |
|---|
| 482 | +ssize_t at24_mac_read(unsigned char* addr) |
|---|
| 483 | +{ |
|---|
| 484 | + char buf[20]; |
|---|
| 485 | + char buf_tmp[12]; |
|---|
| 486 | + int i; |
|---|
| 487 | + ssize_t ret; |
|---|
| 488 | + if (at24_private == NULL) |
|---|
| 489 | + { |
|---|
| 490 | + printk("ben %s: at24_private==null error\n", __func__); |
|---|
| 491 | + return 0; |
|---|
| 492 | + } |
|---|
| 493 | + memset(buf, 0x00, 20); |
|---|
| 494 | + memset(buf_tmp, 0x00, 12); |
|---|
| 495 | + ret = at24_read(at24_private, 0, buf, 12); |
|---|
| 496 | + if (ret > 0) |
|---|
| 497 | + { |
|---|
| 498 | + for(i=0; i<12; i++) |
|---|
| 499 | + { |
|---|
| 500 | + buf_tmp[i] = AscToHex(buf[i]); |
|---|
| 501 | + } |
|---|
| 502 | + addr[0] = (buf_tmp[0] << 4) | buf_tmp[1]; |
|---|
| 503 | + addr[1] = (buf_tmp[2] << 4) | buf_tmp[3]; |
|---|
| 504 | + addr[2] = (buf_tmp[4] << 4) | buf_tmp[5]; |
|---|
| 505 | + addr[3] = (buf_tmp[6] << 4) | buf_tmp[7]; |
|---|
| 506 | + addr[4] = (buf_tmp[8] << 4) | buf_tmp[9]; |
|---|
| 507 | + addr[5] = (buf_tmp[10] << 4) | buf_tmp[11]; |
|---|
| 508 | + } |
|---|
| 509 | + return ret; |
|---|
| 510 | +} |
|---|
| 511 | +#endif |
|---|
| 512 | + |
|---|
| 513 | +ssize_t at24_mac_read(unsigned char* addr) |
|---|
| 514 | +{ |
|---|
| 515 | + char buf[20]; |
|---|
| 516 | + char buf_tmp[12]; |
|---|
| 517 | + ssize_t ret; |
|---|
| 518 | + if (at24_private == NULL) |
|---|
| 519 | + { |
|---|
| 520 | + printk("ben: at24_mac_read at24_private==null error"); |
|---|
| 521 | + return 0; |
|---|
| 522 | + } |
|---|
| 523 | + memset(buf, 0x00, 20); |
|---|
| 524 | + memset(buf_tmp, 0x00, 12); |
|---|
| 525 | + ret = at24_read_private(at24_private, buf, 0, 6); |
|---|
| 526 | + if (ret > 0) |
|---|
| 527 | + { |
|---|
| 528 | + addr[0] = buf[0]; |
|---|
| 529 | + addr[1] = buf[1]; |
|---|
| 530 | + addr[2] = buf[2]; |
|---|
| 531 | + addr[3] = buf[3]; |
|---|
| 532 | + addr[4] = buf[4]; |
|---|
| 533 | + addr[5] = buf[5]; |
|---|
| 534 | + } |
|---|
| 535 | + printk("at24_mac_read ...............\n"); |
|---|
| 536 | + return ret; |
|---|
| 537 | +} |
|---|
| 538 | +EXPORT_SYMBOL(at24_mac_read); |
|---|
| 539 | + |
|---|
| 540 | +ssize_t at24_mac1_read(unsigned char* mac) |
|---|
| 541 | +{ |
|---|
| 542 | + char buf[20]; |
|---|
| 543 | + char buf_tmp[12]; |
|---|
| 544 | + ssize_t ret; |
|---|
| 545 | + if (at24_private == NULL) |
|---|
| 546 | + { |
|---|
| 547 | + printk("zcl: at24_mac_read at24_private==null error"); |
|---|
| 548 | + return 0; |
|---|
| 549 | + } |
|---|
| 550 | + memset(buf, 0x00, 20); |
|---|
| 551 | + memset(buf_tmp, 0x00, 12); |
|---|
| 552 | + ret = at24_read_private(at24_private, buf, 0x10, 6); |
|---|
| 553 | + if (ret > 0) |
|---|
| 554 | + { |
|---|
| 555 | + *mac = buf[0]; |
|---|
| 556 | + *(mac + 1) = buf[1]; |
|---|
| 557 | + *(mac + 2) = buf[2]; |
|---|
| 558 | + *(mac + 3) = buf[3]; |
|---|
| 559 | + *(mac + 4) = buf[4]; |
|---|
| 560 | + *(mac + 5) = buf[5]; |
|---|
| 561 | + } |
|---|
| 562 | + printk("at24_mac1_read ...............\n"); |
|---|
| 563 | + return ret; |
|---|
| 564 | +} |
|---|
| 565 | +EXPORT_SYMBOL(at24_mac1_read); |
|---|
| 566 | + |
|---|
| 424 | 567 | static int at24_write(void *priv, unsigned int off, void *val, size_t count) |
|---|
| 425 | 568 | { |
|---|
| 426 | 569 | struct at24_data *at24; |
|---|
| .. | .. |
|---|
| 630 | 773 | u8 test_byte; |
|---|
| 631 | 774 | int err; |
|---|
| 632 | 775 | |
|---|
| 776 | + printk("ben %s ...\n", __func__); |
|---|
| 633 | 777 | i2c_fn_i2c = i2c_check_functionality(client->adapter, I2C_FUNC_I2C); |
|---|
| 634 | 778 | i2c_fn_block = i2c_check_functionality(client->adapter, |
|---|
| 635 | 779 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK); |
|---|
| .. | .. |
|---|
| 674 | 818 | if (!at24) |
|---|
| 675 | 819 | return -ENOMEM; |
|---|
| 676 | 820 | |
|---|
| 821 | + at24_private = at24; |
|---|
| 677 | 822 | mutex_init(&at24->lock); |
|---|
| 678 | 823 | at24->byte_len = pdata.byte_len; |
|---|
| 679 | 824 | at24->page_size = pdata.page_size; |
|---|
| .. | .. |
|---|
| 792 | 937 | at24_io_limit = rounddown_pow_of_two(at24_io_limit); |
|---|
| 793 | 938 | return i2c_add_driver(&at24_driver); |
|---|
| 794 | 939 | } |
|---|
| 795 | | -module_init(at24_init); |
|---|
| 940 | +//module_init(at24_init); |
|---|
| 941 | +postcore_initcall_sync(at24_init); |
|---|
| 796 | 942 | |
|---|
| 797 | 943 | static void __exit at24_exit(void) |
|---|
| 798 | 944 | { |
|---|