Re: [PATCH 00/34] treewide: simplify getting the adapter of an I2C client

From: Peter Rosin
Date: Sun Jun 09 2019 - 12:42:36 EST


On 2019-06-08 12:55, Wolfram Sang wrote:
> While preparing a refactoring series, I noticed that some drivers use a
> complicated way of determining the adapter of a client. The easy way is
> to use the intended pointer: client->adapter
>
> These drivers do:
> to_i2c_adapter(client->dev.parent);
>
> The I2C core populates the parent pointer as:
> client->dev.parent = &client->adapter->dev;
>
> Now take into consideration that
> to_i2c_adapter(&adapter->dev);
>
> is a complicated way of saying 'adapter', then we can even formally
> prove that the complicated expression can be simplified by using
> client->adapter.
>
> The conversion was done using a coccinelle script with some manual
> indentation fixes applied on top.
>
> To avoid a brown paper bag mistake, I double checked this on a Renesas
> Salvator-XS board (R-Car M3N) and verified both expression result in the
> same pointer. Other than that, the series is only build tested.

Similar things go on in:

drivers/hwmon/lm90.c
drivers/leds/leds-is31fl319x.c
drivers/of/unittest.c

Those have this pattern:

struct device *dev = &client->dev;
struct i2c_adapter *adapter = to_i2c_adapter(dev->parent);

And drivers/rtc/rtc-fm3130.c has a couple of these:

tmp = i2c_transfer(to_i2c_adapter(fm3130->client->dev.parent),
...);

where fm3130->client is of type "struct i2c_client *"

Cheers,
Peter