| @@ -22,10 +22,24 @@ · 21 unmodified lines |
| 22 | 22 | #include <linux/i2c.h> |
| 23 | 23 | #include <linux/nvmem-provider.h> |
| 24 | 24 | #include <linux/regmap.h> |
| 25 | | −#include <linux/platform_data/at24.h> |
| 26 | 25 | #include <linux/pm_runtime.h> |
| 27 | 26 | #include <linux/gpio/consumer.h> |
| 28 | 27 | |
| 28 | +/* Address pointer is 16 bit. */ |
| 29 | +#define AT24_FLAG_ADDR16 BIT(7) |
| 30 | +/* sysfs-entry will be read-only. */ |
| 31 | +#define AT24_FLAG_READONLY BIT(6) |
| 32 | +/* sysfs-entry will be world-readable. */ |
| 33 | +#define AT24_FLAG_IRUGO BIT(5) |
| 34 | +/* Take always 8 addresses (24c00). */ |
| 35 | +#define AT24_FLAG_TAKE8ADDR BIT(4) |
| 36 | +/* Factory-programmed serial number. */ |
| 37 | +#define AT24_FLAG_SERIAL BIT(3) |
| 38 | +/* Factory-programmed mac address. */ |
| 39 | +#define AT24_FLAG_MAC BIT(2) |
| 40 | +/* Does not auto-rollover reads to the next slave address. */ |
| 41 | +#define AT24_FLAG_NO_RDROL BIT(1) |
| 42 | + |
| 29 | 43 | /* |
| 30 | 44 | * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable. |
| 31 | 45 | * Differences between different vendor product lines (like Atmel AT24C or |
| @@ -107,10 +121,6 @@ module_param_named(write_timeout, at24_write_timeout, uint, 0); · 75 unmodified lines |
| 107 | 121 | MODULE_PARM_DESC(at24_write_timeout, "Time (in ms) to try writes (default 25)"); |
| 108 | 122 | |
| 109 | 123 | struct at24_chip_data { |
| 110 | | − /* |
| 111 | | − * these fields mirror their equivalents in |
| 112 | | − * struct at24_platform_data |
| 113 | | − */ |
| 114 | 124 | u32 byte_len; |
| 115 | 125 | u8 flags; |
| 116 | 126 | }; |
| @@ -471,63 +481,11 @@ static int at24_write(void *priv, unsigned int off, void *val, size_t count) · 354 unmodified lines |
| 471 | 481 | return 0; |
| 472 | 482 | } |
| 473 | 483 | |
| 474 | | −static void at24_properties_to_pdata(struct device *dev, |
| 475 | | − struct at24_platform_data *chip) |
| 476 | | −{ |
| 477 | | − int err; |
| 478 | | − u32 val; |
| 479 | | − |
| 480 | | − if (device_property_present(dev, "read-only")) |
| 481 | | − chip->flags |= AT24_FLAG_READONLY; |
| 482 | | − if (device_property_present(dev, "no-read-rollover")) |
| 483 | | − chip->flags |= AT24_FLAG_NO_RDROL; |
| 484 | | − |
| 485 | | − err = device_property_read_u32(dev, "address-width", &val); |
| 486 | | − if (!err) { |
| 487 | | − switch (val) { |
| 488 | | − case 8: |
| 489 | | − if (chip->flags & AT24_FLAG_ADDR16) |
| 490 | | − dev_warn(dev, "Override address width to be 8, while default is 16\n"); |
| 491 | | − chip->flags &= ~AT24_FLAG_ADDR16; |
| 492 | | − break; |
| 493 | | − case 16: |
| 494 | | − chip->flags |= AT24_FLAG_ADDR16; |
| 495 | | − break; |
| 496 | | − default: |
| 497 | | − dev_warn(dev, "Bad \"address-width\" property: %u\n", |
| 498 | | − val); |
| 499 | | − } |
| 500 | | − } |
| 501 | | − |
| 502 | | − err = device_property_read_u32(dev, "size", &val); |
| 503 | | − if (!err) |
| 504 | | − chip->byte_len = val; |
| 505 | | − |
| 506 | | − err = device_property_read_u32(dev, "pagesize", &val); |
| 507 | | − if (!err) { |
| 508 | | − chip->page_size = val; |
| 509 | | − } else { |
| 510 | | − /* |
| 511 | | − * This is slow, but we can't know all eeproms, so we better |
| 512 | | − * play safe. Specifying custom eeprom-types via platform_data |
| 513 | | − * is recommended anyhow. |
| 514 | | − */ |
| 515 | | − chip->page_size = 1; |
| 516 | | − } |
| 517 | | −} |
| 518 | | − |
| 519 | | −static int at24_get_pdata(struct device *dev, struct at24_platform_data *pdata) |
| 484 | +static const struct at24_chip_data *at24_get_chip_data(struct device *dev) |
| 520 | 485 | { |
| 521 | 486 | struct device_node *of_node = dev->of_node; |
| 522 | 487 | const struct at24_chip_data *cdata; |
| 523 | 488 | const struct i2c_device_id *id; |
| 524 | | − struct at24_platform_data *pd; |
| 525 | | − |
| 526 | | − pd = dev_get_platdata(dev); |
| 527 | | − if (pd) { |
| 528 | | − memcpy(pdata, pd, sizeof(*pdata)); |
| 529 | | − return 0; |
| 530 | | − } |
| 531 | 489 | |
| 532 | 490 | id = i2c_match_id(at24_ids, to_i2c_client(dev)); |
| 533 | 491 | |
| @@ -544,13 +502,9 @@ static int at24_get_pdata(struct device *dev, struct at24_platform_data *pdata) · 10 unmodified lines |
| 544 | 502 | cdata = acpi_device_get_match_data(dev); |
| 545 | 503 | |
| 546 | 504 | if (!cdata) |
| 547 | | − return -ENODEV; |
| 505 | + return ERR_PTR(-ENODEV); |
| 548 | 506 | |
| 549 | | − pdata->byte_len = cdata->byte_len; |
| 550 | | − pdata->flags = cdata->flags; |
| 551 | | − at24_properties_to_pdata(dev, pdata); |
| 552 | | − |
| 553 | | − return 0; |
| 507 | + return cdata; |
| 554 | 508 | } |
| 555 | 509 | |
| 556 | 510 | static void at24_remove_dummy_clients(struct at24_data *at24) |
| @@ -619,7 +573,8 @@ static int at24_probe(struct i2c_client *client) · 62 unmodified lines |
| 619 | 573 | { |
| 620 | 574 | struct regmap_config regmap_config = { }; |
| 621 | 575 | struct nvmem_config nvmem_config = { }; |
| 622 | | − struct at24_platform_data pdata = { }; |
| 576 | + u32 byte_len, page_size, flags, addrw; |
| 577 | + const struct at24_chip_data *cdata; |
| 623 | 578 | struct device *dev = &client->dev; |
| 624 | 579 | bool i2c_fn_i2c, i2c_fn_block; |
| 625 | 580 | unsigned int i, num_addresses; |
| @@ -634,35 +589,72 @@ static int at24_probe(struct i2c_client *client) · 8 unmodified lines |
| 634 | 589 | i2c_fn_block = i2c_check_functionality(client->adapter, |
| 635 | 590 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK); |
| 636 | 591 | |
| 637 | | − err = at24_get_pdata(dev, &pdata); |
| 592 | + cdata = at24_get_chip_data(dev); |
| 593 | + if (IS_ERR(cdata)) |
| 594 | + return PTR_ERR(cdata); |
| 595 | + |
| 596 | + err = device_property_read_u32(dev, "pagesize", &page_size); |
| 638 | 597 | if (err) |
| 639 | | − return err; |
| 598 | + /* |
| 599 | + * This is slow, but we can't know all eeproms, so we better |
| 600 | + * play safe. Specifying custom eeprom-types via platform_data |
| 601 | + * is recommended anyhow. |
| 602 | + */ |
| 603 | + page_size = 1; |
| 604 | + |
| 605 | + flags = cdata->flags; |
| 606 | + if (device_property_present(dev, "read-only")) |
| 607 | + flags |= AT24_FLAG_READONLY; |
| 608 | + if (device_property_present(dev, "no-read-rollover")) |
| 609 | + flags |= AT24_FLAG_NO_RDROL; |
| 610 | + |
| 611 | + err = device_property_read_u32(dev, "address-width", &addrw); |
| 612 | + if (!err) { |
| 613 | + switch (addrw) { |
| 614 | + case 8: |
| 615 | + if (flags & AT24_FLAG_ADDR16) |
| 616 | + dev_warn(dev, |
| 617 | + "Override address width to be 8, while default is 16\n"); |
| 618 | + flags &= ~AT24_FLAG_ADDR16; |
| 619 | + break; |
| 620 | + case 16: |
| 621 | + flags |= AT24_FLAG_ADDR16; |
| 622 | + break; |
| 623 | + default: |
| 624 | + dev_warn(dev, "Bad \"address-width\" property: %u\n", |
| 625 | + addrw); |
| 626 | + } |
| 627 | + } |
| 628 | + |
| 629 | + err = device_property_read_u32(dev, "size", &byte_len); |
| 630 | + if (err) |
| 631 | + byte_len = cdata->byte_len; |
| 640 | 632 | |
| 641 | 633 | if (!i2c_fn_i2c && !i2c_fn_block) |
| 642 | | − pdata.page_size = 1; |
| 634 | + page_size = 1; |
| 643 | 635 | |
| 644 | | − if (!pdata.page_size) { |
| 636 | + if (!page_size) { |
| 645 | 637 | dev_err(dev, "page_size must not be 0!\n"); |
| 646 | 638 | return -EINVAL; |
| 647 | 639 | } |
| 648 | 640 | |
| 649 | | − if (!is_power_of_2(pdata.page_size)) |
| 641 | + if (!is_power_of_2(page_size)) |
| 650 | 642 | dev_warn(dev, "page_size looks suspicious (no power of 2)!\n"); |
| 651 | 643 | |
| 652 | | − if (pdata.flags & AT24_FLAG_TAKE8ADDR) |
| 644 | + if (flags & AT24_FLAG_TAKE8ADDR) |
| 653 | 645 | num_addresses = 8; |
| 654 | 646 | else |
| 655 | | − num_addresses = DIV_ROUND_UP(pdata.byte_len, |
| 656 | | − (pdata.flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 647 | + num_addresses = DIV_ROUND_UP(byte_len, |
| 648 | + (flags & AT24_FLAG_ADDR16) ? 65536 : 256); |
| 657 | 649 | |
| 658 | | − if ((pdata.flags & AT24_FLAG_SERIAL) && (pdata.flags & AT24_FLAG_MAC)) { |
| 650 | + if ((flags & AT24_FLAG_SERIAL) && (flags & AT24_FLAG_MAC)) { |
| 659 | 651 | dev_err(dev, |
| 660 | 652 | "invalid device data - cannot have both AT24_FLAG_SERIAL & AT24_FLAG_MAC."); |
| 661 | 653 | return -EINVAL; |
| 662 | 654 | } |
| 663 | 655 | |
| 664 | 656 | regmap_config.val_bits = 8; |
| 665 | | − regmap_config.reg_bits = (pdata.flags & AT24_FLAG_ADDR16) ? 16 : 8; |
| 657 | + regmap_config.reg_bits = (flags & AT24_FLAG_ADDR16) ? 16 : 8; |
| 666 | 658 | regmap_config.disable_locking = true; |
| 667 | 659 | |
| 668 | 660 | regmap = devm_regmap_init_i2c(client, ®map_config); |
| @@ -675,11 +667,11 @@ static int at24_probe(struct i2c_client *client) · 6 unmodified lines |
| 675 | 667 | return -ENOMEM; |
| 676 | 668 | |
| 677 | 669 | mutex_init(&at24->lock); |
| 678 | | − at24->byte_len = pdata.byte_len; |
| 679 | | − at24->page_size = pdata.page_size; |
| 680 | | − at24->flags = pdata.flags; |
| 670 | + at24->byte_len = byte_len; |
| 671 | + at24->page_size = page_size; |
| 672 | + at24->flags = flags; |
| 681 | 673 | at24->num_addresses = num_addresses; |
| 682 | | − at24->offset_adj = at24_get_offset_adj(pdata.flags, pdata.byte_len); |
| 674 | + at24->offset_adj = at24_get_offset_adj(flags, byte_len); |
| 683 | 675 | at24->client[0].client = client; |
| 684 | 676 | at24->client[0].regmap = regmap; |
| 685 | 677 | |
| @@ -687,10 +679,10 @@ static int at24_probe(struct i2c_client *client) · 1 unmodified line |
| 687 | 679 | if (IS_ERR(at24->wp_gpio)) |
| 688 | 680 | return PTR_ERR(at24->wp_gpio); |
| 689 | 681 | |
| 690 | | − writable = !(pdata.flags & AT24_FLAG_READONLY); |
| 682 | + writable = !(flags & AT24_FLAG_READONLY); |
| 691 | 683 | if (writable) { |
| 692 | 684 | at24->write_max = min_t(unsigned int, |
| 693 | | − pdata.page_size, at24_io_limit); |
| 685 | + page_size, at24_io_limit); |
| 694 | 686 | if (!i2c_fn_i2c && at24->write_max > I2C_SMBUS_BLOCK_MAX) |
| 695 | 687 | at24->write_max = I2C_SMBUS_BLOCK_MAX; |
| 696 | 688 | } |
| @@ -733,7 +725,7 @@ static int at24_probe(struct i2c_client *client) · 36 unmodified lines |
| 733 | 725 | nvmem_config.priv = at24; |
| 734 | 726 | nvmem_config.stride = 1; |
| 735 | 727 | nvmem_config.word_size = 1; |
| 736 | | − nvmem_config.size = pdata.byte_len; |
| 728 | + nvmem_config.size = byte_len; |
| 737 | 729 | |
| 738 | 730 | at24->nvmem = devm_nvmem_register(dev, &nvmem_config); |
| 739 | 731 | if (IS_ERR(at24->nvmem)) { |
| @@ -742,13 +734,9 @@ static int at24_probe(struct i2c_client *client) · 2 unmodified lines |
| 742 | 734 | } |
| 743 | 735 | |
| 744 | 736 | dev_info(dev, "%u byte %s EEPROM, %s, %u bytes/write\n", |
| 745 | | − pdata.byte_len, client->name, |
| 737 | + byte_len, client->name, |
| 746 | 738 | writable ? "writable" : "read-only", at24->write_max); |
| 747 | 739 | |
| 748 | | − /* export data to kernel code */ |
| 749 | | − if (pdata.setup) |
| 750 | | − pdata.setup(at24->nvmem, pdata.context); |
| 751 | | − |
| 752 | 740 | return 0; |
| 753 | 741 | |
| 754 | 742 | err_clients: |