[PATCH 08/10] i2c: pasemi: Refactor _probe to use devm_*

Sven Peter
2021-09-26T10:00:06Z

Using managed device resources means there's nothing left to be done in
pasemi_smb_pci_remove and also allows to remove base and size from
struct pasemi_smbus.

Signed-off-by: Sven Peter <sven@svenpeter.dev>
---
drivers/i2c/busses/i2c-pasemi-core.c | 10 +++----
drivers/i2c/busses/i2c-pasemi-core.h | 2 --
drivers/i2c/busses/i2c-pasemi-pci.c | 45 ++++++++--------------------
3 files changed, 16 insertions(+), 41 deletions(-)

diff --git a/drivers/i2c/busses/i2c-pasemi-core.c b/drivers/i2c/busses/i2c-pasemi-core.c
index a39e3258b162..0ec65263fd08 100644
--- a/drivers/i2c/busses/i2c-pasemi-core.c
+++ b/drivers/i2c/busses/i2c-pasemi-core.c
@@ -41,8 +41,7 @@

static inline void reg_write(struct pasemi_smbus *smbus, int reg, int val)
{
- dev_dbg(smbus->dev, "smbus write reg %lx val %08x\n",
- smbus->base + reg, val);
+ dev_dbg(smbus->dev, "smbus write reg %x val %08x\n", reg, val);
iowrite32(val, smbus->ioaddr + reg);
}

@@ -50,8 +49,7 @@ static inline int reg_read(struct pasemi_smbus *smbus, int reg)
{
int ret;
ret = ioread32(smbus->ioaddr + reg);
- dev_dbg(smbus->dev, "smbus read reg %lx val %08x\n",
- smbus->base + reg, ret);
+ dev_dbg(smbus->dev, "smbus read reg %x val %08x\n", reg, ret);
return ret;
}

@@ -329,7 +327,7 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)

smbus->adapter.owner = THIS_MODULE;
snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
- "PA Semi SMBus adapter at 0x%lx", smbus->base);
+ "PA Semi SMBus adapter at 0x%p", smbus->ioaddr);
smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
smbus->adapter.algo = &smbus_algorithm;
smbus->adapter.algo_data = smbus;
@@ -339,7 +337,7 @@ int pasemi_i2c_common_probe(struct pasemi_smbus *smbus)

pasemi_reset(smbus);

- error = i2c_add_adapter(&smbus->adapter);
+ error = devm_i2c_add_adapter(smbus->dev, &smbus->adapter);
if (error)
return error;

diff --git a/drivers/i2c/busses/i2c-pasemi-core.h b/drivers/i2c/busses/i2c-pasemi-core.h
index 30a7990825ef..aca4e2da9089 100644
--- a/drivers/i2c/busses/i2c-pasemi-core.h
+++ b/drivers/i2c/busses/i2c-pasemi-core.h
@@ -12,8 +12,6 @@ struct pasemi_smbus {
struct device *dev;
struct i2c_adapter adapter;
void __iomem *ioaddr;
- unsigned long base;
- int size;
unsigned int clk_div;
};

diff --git a/drivers/i2c/busses/i2c-pasemi-pci.c b/drivers/i2c/busses/i2c-pasemi-pci.c
index 7405e0b48514..c1b8901110c0 100644
--- a/drivers/i2c/busses/i2c-pasemi-pci.c
+++ b/drivers/i2c/busses/i2c-pasemi-pci.c
@@ -26,57 +26,37 @@ static int pasemi_smb_pci_probe(struct pci_dev *dev,
const struct pci_device_id *id)
{
struct pasemi_smbus *smbus;
+ unsigned long base;
+ int size;
int error;

if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
return -ENODEV;

- smbus = kzalloc(sizeof(struct pasemi_smbus), GFP_KERNEL);
+ smbus = devm_kzalloc(&dev->dev, sizeof(*smbus), GFP_KERNEL);
if (!smbus)
return -ENOMEM;

smbus->dev = &dev->dev;
- smbus->base = pci_resource_start(dev, 0);
- smbus->size = pci_resource_len(dev, 0);
+ base = pci_resource_start(dev, 0);
+ size = pci_resource_len(dev, 0);
smbus->clk_div = CLK_100K_DIV;

- if (!request_region(smbus->base, smbus->size,
- pasemi_smb_pci_driver.name)) {
- error = -EBUSY;
- goto out_kfree;
- }
+ if (!devm_request_region(&dev->dev, base, size,
+ pasemi_smb_pci_driver.name))
+ return -EBUSY;

- smbus->ioaddr = ioport_map(smbus->base, smbus->size);
- if (!smbus->ioaddr) {
- error = -EBUSY;
- goto out_release_region;
- }
+ smbus->ioaddr = devm_ioport_map(&dev->dev, base, size);
+ if (!smbus->ioaddr)
+ return -EBUSY;

error = pasemi_i2c_common_probe(smbus);
if (error)
- goto out_ioport_unmap;
+ return error;

pci_set_drvdata(dev, smbus);

return 0;
-
- out_ioport_unmap:
- ioport_unmap(smbus->ioaddr);
- out_release_region:
- release_region(smbus->base, smbus->size);
- out_kfree:
- kfree(smbus);
- return error;
-}
-
-static void pasemi_smb_pci_remove(struct pci_dev *dev)
-{
- struct pasemi_smbus *smbus = pci_get_drvdata(dev);
-
- i2c_del_adapter(&smbus->adapter);
- ioport_unmap(smbus->ioaddr);
- release_region(smbus->base, smbus->size);
- kfree(smbus);
}

static const struct pci_device_id pasemi_smb_pci_ids[] = {
@@ -90,7 +70,6 @@ static struct pci_driver pasemi_smb_pci_driver = {
.name = "i2c-pasemi",
.id_table = pasemi_smb_pci_ids,
.probe = pasemi_smb_pci_probe,
- .remove = pasemi_smb_pci_remove,
};

module_pci_driver(pasemi_smb_pci_driver);
--
2.25.1