From: Hans de Goede <hdegoede@redhat.com>
To: Kai-Heng Feng <kai.heng.feng@canonical.com>
Cc: ktsai@capellamicro.com, jic23@kernel.org, lars@metafoo.de,
Wahaj <wahajaved@protonmail.com>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iio: light: cm32181: Fix PM support on system with 2 I2C resources
Date: Wed, 18 Jan 2023 11:52:48 +0100 [thread overview]
Message-ID: <a9db708f-59a6-586a-8728-81622a93ef3e@redhat.com> (raw)
In-Reply-To: <CAAd53p5XXCniBN7x4uhp4XW=qr2U72_UntgAR0BV2viRtd+8EA@mail.gmail.com>
Hi,
On 1/18/23 06:15, Kai-Heng Feng wrote:
> On Wed, Jan 18, 2023 at 11:29 AM Kai-Heng Feng
> <kai.heng.feng@canonical.com> wrote:
>>
>> Hi Hans,
>>
>> On Wed, Jan 18, 2023 at 1:21 AM Hans de Goede <hdegoede@redhat.com> wrote:
>>>
>>> Hi,
>>>
>>> On 1/17/23 17:09, Kai-Heng Feng wrote:
>>>> Commit c1e62062ff54 ("iio: light: cm32181: Handle CM3218 ACPI devices
>>>> with 2 I2C resources") creates a second client for the actual I2C
>>>> address, but the "struct device" passed to PM ops is the first client
>>>> that can't talk to the sensor.
>>>>
>>>> That means the I2C transfers in both suspend and resume routines can
>>>> fail and blocking the whole suspend process.
>>>>
>>>> Instead of using the first client for I2C transfer, store the cm32181
>>>> private struct on both cases so the PM ops can get the correct I2C
>>>> client to perfrom suspend and resume.
>>>>
>>>> Fixes: 68c1b3dd5c48 ("iio: light: cm32181: Add PM support")
>>>> Tested-by: Wahaj <wahajaved@protonmail.com>
>>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>>
>>> Thank you for this fix. I had looking into this on my todo list,
>>> since I have been seeing some bug reports about this too.
>>>
>>> One remark inline:
>>>
>>>> ---
>>>> drivers/iio/light/cm32181.c | 11 +++++++----
>>>> 1 file changed, 7 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
>>>> index 001055d097509..0f319c891353c 100644
>>>> --- a/drivers/iio/light/cm32181.c
>>>> +++ b/drivers/iio/light/cm32181.c
>>>> @@ -440,6 +440,8 @@ static int cm32181_probe(struct i2c_client *client)
>>>> if (!indio_dev)
>>>> return -ENOMEM;
>>>>
>>>> + i2c_set_clientdata(client, indio_dev);
>>>> +
>>>
>>> Why move this up, the suspend/resume callbacks cannot run until
>>> probe() completes, so no need for this change.
>>
>> The intention is to save indio_dev as drvdata in the first (i.e.
>> original) i2c_client's dev.
>>
>>>
>>>> /*
>>>> * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
>>>> * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
>>>> @@ -458,9 +460,9 @@ static int cm32181_probe(struct i2c_client *client)
>>>> client = i2c_acpi_new_device(dev, 1, &board_info);
>>>> if (IS_ERR(client))
>>>> return PTR_ERR(client);
>>>> - }
>>>>
>>>> - i2c_set_clientdata(client, indio_dev);
>>>> + i2c_set_clientdata(client, indio_dev);
>>>> + }
>>>
>>> And moving it inside the if block here (instead of just dropping it)
>>> is also weird. I guess you meant to just delete it since you moved it up.
>>
>> Doesn't i2c_acpi_new_device() creates a new i2c_client (and its dev embedded)?
>>
>> So the intention is to save indio_dev for the second (ARA case) i2c_client too.
>>
>>>
>>>>
>>>> cm32181 = iio_priv(indio_dev);
>>>> cm32181->client = client;
>>>
>>> Also note that the ->client used in suspend/resume now is not set until
>>> here, so moving the i2c_set_clientdata() up really does not do anything.
>>>
>>> I beleive it would be best to just these 2 hunks from the patch and
>>> only keep the changes to the suspend/resume callbacks.
>>
>> Yes, it seems like those 2 hunks are not necessary. Let me send a new patch.
>
> if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
> ...
> client = i2c_acpi_new_device(dev, 1, &board_info);
> ...
> }
> i2c_set_clientdata(client, indio_dev);
>
> It means the indio_dev is only assigned to the new i2c_client->dev's
> drvdata, the original dev's drvdata remains NULL.
> So we need to assign it before the original client gets replaced by
> the new one, otherwise we can't get cm32181 in PM ops.
You are right, my bad. The original code has a bug where it indeed was
making the i2c_set_clientdata() call on the wrong client device.
So the i2c_set_clientdata() call needs to be moved up.
There is no need to also call i2c_set_clientdata() on the dummy
i2c-client though. That one does not have a driver attached.
The suspend/resume callbacks are made on the original client-dev,
not on the one of the dummy-client (which is the one which we
actually use to communicate).
>> But I do wonder what happens for the removing case? Will the second
>> i2c_client leak?
Yes it does, good point. That should probably also be fixed, but
that needs to be a different / second patch.
Regards,
Hans
>>>> @@ -490,7 +492,8 @@ static int cm32181_probe(struct i2c_client *client)
>>>>
>>>> static int cm32181_suspend(struct device *dev)
>>>> {
>>>> - struct i2c_client *client = to_i2c_client(dev);
>>>> + struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
>>>> + struct i2c_client *client = cm32181->client;
>>>>
>>>> return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
>>>> CM32181_CMD_ALS_DISABLE);
>>>> @@ -498,8 +501,8 @@ static int cm32181_suspend(struct device *dev)
>>>>
>>>> static int cm32181_resume(struct device *dev)
>>>> {
>>>> - struct i2c_client *client = to_i2c_client(dev);
>>>> struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
>>>> + struct i2c_client *client = cm32181->client;
>>>>
>>>> return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
>>>> cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
>>>
>
next prev parent reply other threads:[~2023-01-18 11:35 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-17 16:09 [PATCH] iio: light: cm32181: Fix PM support on system with 2 I2C resources Kai-Heng Feng
2023-01-17 17:19 ` Hans de Goede
2023-01-17 21:02 ` Hans de Goede
2023-01-18 3:29 ` Kai-Heng Feng
2023-01-18 5:15 ` Kai-Heng Feng
2023-01-18 10:52 ` Hans de Goede [this message]
2023-01-18 17:03 ` Kai-Heng Feng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a9db708f-59a6-586a-8728-81622a93ef3e@redhat.com \
--to=hdegoede@redhat.com \
--cc=jic23@kernel.org \
--cc=kai.heng.feng@canonical.com \
--cc=ktsai@capellamicro.com \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wahajaved@protonmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox