diff mbox series

[v2,3/6] ACPI: AC: Replace acpi_driver with platform_driver

Message ID 20231006173055.2938160-4-michal.wilczynski@intel.com (mailing list archive)
State Superseded
Headers show
Series Replace acpi_driver with platform_driver | expand

Commit Message

Wilczynski, Michal Oct. 6, 2023, 5:30 p.m. UTC
AC driver uses struct acpi_driver incorrectly to register itself. This
is wrong as the instances of the ACPI devices are not meant to
be literal devices, they're supposed to describe ACPI entry of a
particular device.

Use platform_driver instead of acpi_driver. In relevant places call
platform devices instances pdev to make a distinction with ACPI
devices instances.

Drop unnecessary casts from acpi_bus_generate_netlink_event() and
acpi_notifier_call_chain().

Add a blank line to distinguish pdev API vs local ACPI notify function.

Suggested-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
---
 drivers/acpi/ac.c | 70 +++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 33 deletions(-)

Comments

Andy Shevchenko Oct. 6, 2023, 5:45 p.m. UTC | #1
On Fri, Oct 06, 2023 at 08:30:52PM +0300, Michal Wilczynski wrote:
> AC driver uses struct acpi_driver incorrectly to register itself. This
> is wrong as the instances of the ACPI devices are not meant to
> be literal devices, they're supposed to describe ACPI entry of a
> particular device.
> 
> Use platform_driver instead of acpi_driver. In relevant places call
> platform devices instances pdev to make a distinction with ACPI
> devices instances.
> 
> Drop unnecessary casts from acpi_bus_generate_netlink_event() and
> acpi_notifier_call_chain().
> 
> Add a blank line to distinguish pdev API vs local ACPI notify function.

...

>  struct acpi_ac {
>  	struct power_supply *charger;
>  	struct power_supply_desc charger_desc;
> -	struct acpi_device *device;
> +	struct device *dev;
>  	unsigned long long state;
>  	struct notifier_block battery_nb;
>  };

When changing this, also makes sense just to check if the moving a member in
the data structure makes code shorter, but it's not a show stopper.

...

> -	status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
> +	status = acpi_evaluate_integer(ACPI_HANDLE(ac->dev), "_PSR", NULL,
>  				       &ac->state);
>  	if (ACPI_FAILURE(status)) {
> -		acpi_handle_info(ac->device->handle,
> +		acpi_handle_info(ACPI_HANDLE(ac->dev),

Can we call ACPI_HANDLE() only once and cache that in a local variable and use
in all places?

...

> -	struct acpi_ac *ac = acpi_driver_data(device);
> +	struct acpi_ac *ac = data;
> +	struct acpi_device *device = ACPI_COMPANION(ac->dev);
>  
>  	switch (event) {
>  	default:

> -		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
> +		acpi_handle_debug(ACPI_HANDLE(ac->dev), "Unsupported event [0x%x]\n",
>  				  event);

Does it makes any sense now? Basically it duplicates the ACPI_COMPANION() call
as Rafael pointed out in previous version discussion.

>  		fallthrough;
Rafael J. Wysocki Oct. 6, 2023, 7:47 p.m. UTC | #2
On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
<michal.wilczynski@intel.com> wrote:
>
> AC driver uses struct acpi_driver incorrectly to register itself. This
> is wrong as the instances of the ACPI devices are not meant to
> be literal devices, they're supposed to describe ACPI entry of a
> particular device.
>
> Use platform_driver instead of acpi_driver. In relevant places call
> platform devices instances pdev to make a distinction with ACPI
> devices instances.
>
> Drop unnecessary casts from acpi_bus_generate_netlink_event() and
> acpi_notifier_call_chain().
>
> Add a blank line to distinguish pdev API vs local ACPI notify function.
>
> Suggested-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
> ---
>  drivers/acpi/ac.c | 70 +++++++++++++++++++++++++----------------------
>  1 file changed, 37 insertions(+), 33 deletions(-)
>
> diff --git a/drivers/acpi/ac.c b/drivers/acpi/ac.c
> index f809f6889b4a..298defeb5301 100644
> --- a/drivers/acpi/ac.c
> +++ b/drivers/acpi/ac.c
> @@ -33,8 +33,9 @@ MODULE_AUTHOR("Paul Diefenbaugh");
>  MODULE_DESCRIPTION("ACPI AC Adapter Driver");
>  MODULE_LICENSE("GPL");
>
> -static int acpi_ac_add(struct acpi_device *device);
> -static void acpi_ac_remove(struct acpi_device *device);
> +static int acpi_ac_probe(struct platform_device *pdev);
> +static void acpi_ac_remove(struct platform_device *pdev);
> +
>  static void acpi_ac_notify(acpi_handle handle, u32 event, void *data);
>
>  static const struct acpi_device_id ac_device_ids[] = {
> @@ -51,21 +52,10 @@ static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
>  static int ac_sleep_before_get_state_ms;
>  static int ac_only;
>
> -static struct acpi_driver acpi_ac_driver = {
> -       .name = "ac",
> -       .class = ACPI_AC_CLASS,
> -       .ids = ac_device_ids,
> -       .ops = {
> -               .add = acpi_ac_add,
> -               .remove = acpi_ac_remove,
> -               },
> -       .drv.pm = &acpi_ac_pm,
> -};
> -
>  struct acpi_ac {
>         struct power_supply *charger;
>         struct power_supply_desc charger_desc;
> -       struct acpi_device *device;
> +       struct device *dev;

I'm not convinced about this change.

If I'm not mistaken, you only use the dev pointer above to get the
ACPI_COMPANION() of it, but the latter is already found in _probe(),
so it can be stored in struct acpi_ac for later use and then the dev
pointer in there will not be necessary any more.

That will save you a bunch of ACPI_HANDLE() evaluations and there's
nothing wrong with using ac->device->handle.  The patch will then
become almost trivial AFAICS and if you really need to get from ac to
the underlying platform device, a pointer to it can be added to struct
acpi_ac without removing the ACPI device pointer from it.

>         unsigned long long state;
>         struct notifier_block battery_nb;
>  };
Andy Shevchenko Oct. 7, 2023, 7:56 a.m. UTC | #3
On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
> On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
> <michal.wilczynski@intel.com> wrote:

...

> >  struct acpi_ac {
> >         struct power_supply *charger;
> >         struct power_supply_desc charger_desc;
> > -       struct acpi_device *device;
> > +       struct device *dev;
> 
> I'm not convinced about this change.
> 
> If I'm not mistaken, you only use the dev pointer above to get the
> ACPI_COMPANION() of it, but the latter is already found in _probe(),
> so it can be stored in struct acpi_ac for later use and then the dev
> pointer in there will not be necessary any more.
> 
> That will save you a bunch of ACPI_HANDLE() evaluations and there's
> nothing wrong with using ac->device->handle.  The patch will then
> become almost trivial AFAICS and if you really need to get from ac to
> the underlying platform device, a pointer to it can be added to struct
> acpi_ac without removing the ACPI device pointer from it.

The idea behind is to eliminate data duplication.

> >         unsigned long long state;
> >         struct notifier_block battery_nb;
> >  };
Rafael J. Wysocki Oct. 7, 2023, 10:41 a.m. UTC | #4
On Sat, Oct 7, 2023 at 9:56 AM Andy Shevchenko
<andriy.shevchenko@intel.com> wrote:
>
> On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
> > On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
> > <michal.wilczynski@intel.com> wrote:
>
> ...
>
> > >  struct acpi_ac {
> > >         struct power_supply *charger;
> > >         struct power_supply_desc charger_desc;
> > > -       struct acpi_device *device;
> > > +       struct device *dev;
> >
> > I'm not convinced about this change.
> >
> > If I'm not mistaken, you only use the dev pointer above to get the
> > ACPI_COMPANION() of it, but the latter is already found in _probe(),
> > so it can be stored in struct acpi_ac for later use and then the dev
> > pointer in there will not be necessary any more.
> >
> > That will save you a bunch of ACPI_HANDLE() evaluations and there's
> > nothing wrong with using ac->device->handle.  The patch will then
> > become almost trivial AFAICS and if you really need to get from ac to
> > the underlying platform device, a pointer to it can be added to struct
> > acpi_ac without removing the ACPI device pointer from it.
>
> The idea behind is to eliminate data duplication.

What data duplication exactly do you mean?

struct acpi_device *device is replaced with struct device *dev which
is the same size.  The latter is then used to obtain a struct
acpi_device pointer.  Why is it better to do this than to store the
struct acpi_device itself?
Rafael J. Wysocki Oct. 7, 2023, 10:43 a.m. UTC | #5
On Sat, Oct 7, 2023 at 12:41 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Sat, Oct 7, 2023 at 9:56 AM Andy Shevchenko
> <andriy.shevchenko@intel.com> wrote:
> >
> > On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
> > > On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
> > > <michal.wilczynski@intel.com> wrote:
> >
> > ...
> >
> > > >  struct acpi_ac {
> > > >         struct power_supply *charger;
> > > >         struct power_supply_desc charger_desc;
> > > > -       struct acpi_device *device;
> > > > +       struct device *dev;
> > >
> > > I'm not convinced about this change.
> > >
> > > If I'm not mistaken, you only use the dev pointer above to get the
> > > ACPI_COMPANION() of it, but the latter is already found in _probe(),
> > > so it can be stored in struct acpi_ac for later use and then the dev
> > > pointer in there will not be necessary any more.
> > >
> > > That will save you a bunch of ACPI_HANDLE() evaluations and there's
> > > nothing wrong with using ac->device->handle.  The patch will then
> > > become almost trivial AFAICS and if you really need to get from ac to
> > > the underlying platform device, a pointer to it can be added to struct
> > > acpi_ac without removing the ACPI device pointer from it.
> >
> > The idea behind is to eliminate data duplication.
>
> What data duplication exactly do you mean?
>
> struct acpi_device *device is replaced with struct device *dev which
> is the same size.  The latter is then used to obtain a struct
> acpi_device pointer.  Why is it better to do this than to store the
> struct acpi_device itself?

This should be "store the struct acpi_device pointer itself", sorry.
Wilczynski, Michal Oct. 9, 2023, 8:40 a.m. UTC | #6
Hi !

Thanks a lot for a review, to both of you ! :-)

On 10/7/2023 12:43 PM, Rafael J. Wysocki wrote:
> On Sat, Oct 7, 2023 at 12:41 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>> On Sat, Oct 7, 2023 at 9:56 AM Andy Shevchenko
>> <andriy.shevchenko@intel.com> wrote:
>>> On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
>>>> On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
>>>> <michal.wilczynski@intel.com> wrote:
>>> ...
>>>
>>>>>  struct acpi_ac {
>>>>>         struct power_supply *charger;
>>>>>         struct power_supply_desc charger_desc;
>>>>> -       struct acpi_device *device;
>>>>> +       struct device *dev;
>>>> I'm not convinced about this change.
>>>>
>>>> If I'm not mistaken, you only use the dev pointer above to get the
>>>> ACPI_COMPANION() of it, but the latter is already found in _probe(),
>>>> so it can be stored in struct acpi_ac for later use and then the dev
>>>> pointer in there will not be necessary any more.
>>>>
>>>> That will save you a bunch of ACPI_HANDLE() evaluations and there's
>>>> nothing wrong with using ac->device->handle.  The patch will then
>>>> become almost trivial AFAICS and if you really need to get from ac to
>>>> the underlying platform device, a pointer to it can be added to struct
>>>> acpi_ac without removing the ACPI device pointer from it.

Yeah we could add platform device without removing acpi device, and
yes that would introduce data duplication, like Andy noticed. And yes,
maybe for this particular driver there is little impact (as struct device is not
really used), but in my opinion we should adhere to some common coding
pattern among all acpi drivers in order for the code to be easier to maintain
and improve readability, also for any newcomer.

>>> The idea behind is to eliminate data duplication.
>> What data duplication exactly do you mean?
>>
>> struct acpi_device *device is replaced with struct device *dev which
>> is the same size.  The latter is then used to obtain a struct
>> acpi_device pointer.  Why is it better to do this than to store the
>> struct acpi_device itself?
> This should be "store the struct acpi_device pointer itself", sorry.

Hi,
So let me explain the reasoning here:

1) I've had a pleasure to work with different drivers in ACPI directory. In my
    opinion the best ones I've seen, were build around the concept of struct
    device (e.g NFIT). It's a struct that's well understood in the kernel, and
    it's easier to understand without having to read any ACPI specific code.
    If I see something like ACPI_HANDLE(dev), I think 'ah-ha -  having a struct
    device I can easily get struct acpi_device - they're connected'. And I think
    using a standardized macro, instead of manually dereferencing pointers is
    also much easier for ACPI newbs reading the code, as it hides a bit complexity
    of getting acpi device from struct device. And to be honest I don't think there would
    be any measurable performance change, as those code paths are far from
    being considered 'hot paths'. So if we can get the code easier to understand
    from a newcomer perspective, why not do it.
   
   
2) I think it would be good to stick to the convention, and introduce some coding
     pattern, for now some drivers store the struct device pointer, and other store
     acpi device pointer . As I'm doing this change acpi device pointer become less
     relevant, as we're using platform device. So to reflect that loss of relevance
     we can choose to adhere to a pattern where we use it less and less, and the
     winning approach would be to use 'struct device' by default everywhere we can
     so maybe eventually we would be able to lose acpi_device altogether at some point,
     as most of the usage is to retrieve acpi handle and execute some AML method.
     So in my understanding acpi device is already obsolete at this point, if we can
     manage to use it less and less, and eventually wipe it out then why not ;-)
   

Thanks again !

Michał
Rafael J. Wysocki Oct. 9, 2023, 12:27 p.m. UTC | #7
On Mon, Oct 9, 2023 at 10:40 AM Wilczynski, Michal
<michal.wilczynski@intel.com> wrote:
>
>
> Hi !
>
> Thanks a lot for a review, to both of you ! :-)
>
> On 10/7/2023 12:43 PM, Rafael J. Wysocki wrote:
> > On Sat, Oct 7, 2023 at 12:41 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
> >> On Sat, Oct 7, 2023 at 9:56 AM Andy Shevchenko
> >> <andriy.shevchenko@intel.com> wrote:
> >>> On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
> >>>> On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
> >>>> <michal.wilczynski@intel.com> wrote:
> >>> ...
> >>>
> >>>>>  struct acpi_ac {
> >>>>>         struct power_supply *charger;
> >>>>>         struct power_supply_desc charger_desc;
> >>>>> -       struct acpi_device *device;
> >>>>> +       struct device *dev;
> >>>> I'm not convinced about this change.
> >>>>
> >>>> If I'm not mistaken, you only use the dev pointer above to get the
> >>>> ACPI_COMPANION() of it, but the latter is already found in _probe(),
> >>>> so it can be stored in struct acpi_ac for later use and then the dev
> >>>> pointer in there will not be necessary any more.
> >>>>
> >>>> That will save you a bunch of ACPI_HANDLE() evaluations and there's
> >>>> nothing wrong with using ac->device->handle.  The patch will then
> >>>> become almost trivial AFAICS and if you really need to get from ac to
> >>>> the underlying platform device, a pointer to it can be added to struct
> >>>> acpi_ac without removing the ACPI device pointer from it.
>
> Yeah we could add platform device without removing acpi device, and
> yes that would introduce data duplication, like Andy noticed.

But he hasn't answered my question regarding what data duplication he
meant, exactly.

So what data duplication do you mean?

> And yes, maybe for this particular driver there is little impact (as struct device is not
> really used), but in my opinion we should adhere to some common coding
> pattern among all acpi drivers in order for the code to be easier to maintain
> and improve readability, also for any newcomer.

Well, maybe.

But then please minimize the number of code lines changed in this
particular patch and please avoid changes that are not directly
related to the purpose of the patch.

> >>> The idea behind is to eliminate data duplication.
> >> What data duplication exactly do you mean?
> >>
> >> struct acpi_device *device is replaced with struct device *dev which
> >> is the same size.  The latter is then used to obtain a struct
> >> acpi_device pointer.  Why is it better to do this than to store the
> >> struct acpi_device itself?
> > This should be "store the struct acpi_device pointer itself", sorry.
>
> Hi,
> So let me explain the reasoning here:
>
> 1) I've had a pleasure to work with different drivers in ACPI directory. In my
>     opinion the best ones I've seen, were build around the concept of struct
>     device (e.g NFIT). It's a struct that's well understood in the kernel, and
>     it's easier to understand without having to read any ACPI specific code.
>     If I see something like ACPI_HANDLE(dev), I think 'ah-ha -  having a struct
>     device I can easily get struct acpi_device - they're connected'. And I think
>     using a standardized macro, instead of manually dereferencing pointers is
>     also much easier for ACPI newbs reading the code, as it hides a bit complexity
>     of getting acpi device from struct device. And to be honest I don't think there would
>     be any measurable performance change, as those code paths are far from
>     being considered 'hot paths'. So if we can get the code easier to understand
>     from a newcomer perspective, why not do it.

I have a differing opinion on a couple of points above, and let's make
one change at a time.

>
> 2) I think it would be good to stick to the convention, and introduce some coding
>      pattern, for now some drivers store the struct device pointer, and other store
>      acpi device pointer . As I'm doing this change acpi device pointer become less
>      relevant, as we're using platform device. So to reflect that loss of relevance
>      we can choose to adhere to a pattern where we use it less and less, and the
>      winning approach would be to use 'struct device' by default everywhere we can
>      so maybe eventually we would be able to lose acpi_device altogether at some point,
>      as most of the usage is to retrieve acpi handle and execute some AML method.
>      So in my understanding acpi device is already obsolete at this point, if we can
>      manage to use it less and less, and eventually wipe it out then why not ;-)

No, ACPI device is not obsolete, it will still be needed for various
things, like power management and hotplug.

Also, I'd rather store a struct acpi_device than acpi_handle, because
the latter is much better from the compile-time type correctness
checks and similar.

I can send my version of the $subject patch just fine if you strongly
disagree with me.
Wilczynski, Michal Oct. 9, 2023, 1:03 p.m. UTC | #8
On 10/9/2023 2:27 PM, Rafael J. Wysocki wrote:
> On Mon, Oct 9, 2023 at 10:40 AM Wilczynski, Michal
> <michal.wilczynski@intel.com> wrote:
>>
>> Hi !
>>
>> Thanks a lot for a review, to both of you ! :-)
>>
>> On 10/7/2023 12:43 PM, Rafael J. Wysocki wrote:
>>> On Sat, Oct 7, 2023 at 12:41 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>>>> On Sat, Oct 7, 2023 at 9:56 AM Andy Shevchenko
>>>> <andriy.shevchenko@intel.com> wrote:
>>>>> On Fri, Oct 06, 2023 at 09:47:57PM +0200, Rafael J. Wysocki wrote:
>>>>>> On Fri, Oct 6, 2023 at 8:33 PM Michal Wilczynski
>>>>>> <michal.wilczynski@intel.com> wrote:
>>>>> ...
>>>>>
>>>>>>>  struct acpi_ac {
>>>>>>>         struct power_supply *charger;
>>>>>>>         struct power_supply_desc charger_desc;
>>>>>>> -       struct acpi_device *device;
>>>>>>> +       struct device *dev;
>>>>>> I'm not convinced about this change.
>>>>>>
>>>>>> If I'm not mistaken, you only use the dev pointer above to get the
>>>>>> ACPI_COMPANION() of it, but the latter is already found in _probe(),
>>>>>> so it can be stored in struct acpi_ac for later use and then the dev
>>>>>> pointer in there will not be necessary any more.
>>>>>>
>>>>>> That will save you a bunch of ACPI_HANDLE() evaluations and there's
>>>>>> nothing wrong with using ac->device->handle.  The patch will then
>>>>>> become almost trivial AFAICS and if you really need to get from ac to
>>>>>> the underlying platform device, a pointer to it can be added to struct
>>>>>> acpi_ac without removing the ACPI device pointer from it.
>> Yeah we could add platform device without removing acpi device, and
>> yes that would introduce data duplication, like Andy noticed.
> But he hasn't answered my question regarding what data duplication he
> meant, exactly.
>
> So what data duplication do you mean?

So what I meant was that many drivers would find it useful to have
a struct device embedded in their 'private structure', and in that case
there would be a duplication of platform_device and acpi_device as
both pointers would be needed. Mind this if you only have struct device
it's easy to get acpi device, but it's not the case the other way around.

So for this driver it's just a matter of sticking to convention, for the others
like it would be duplication.

In my version of this patch we managed to avoid this duplication, thanks
to the contextual argument introduced before, but look at this patch:
https://github.com/mwilczy/linux-pm/commit/cc8ef52707341e67a12067d6ead991d56ea017ca

Author of this patch had to introduce platform_device and acpi_device to the struct ac, so
there was some duplication. That is the case for many drivers to come, so I decided it's better
to change this and have a pattern with keeping only 'struct device'.

>
>> And yes, maybe for this particular driver there is little impact (as struct device is not
>> really used), but in my opinion we should adhere to some common coding
>> pattern among all acpi drivers in order for the code to be easier to maintain
>> and improve readability, also for any newcomer.
> Well, maybe.
>
> But then please minimize the number of code lines changed in this
> particular patch and please avoid changes that are not directly
> related to the purpose of the patch.

Sure, like I've stated before I felt this is part of this patch, we only narrowly
escaped the duplication by introducing contextual argument before ;-)

>
>>>>> The idea behind is to eliminate data duplication.
>>>> What data duplication exactly do you mean?
>>>>
>>>> struct acpi_device *device is replaced with struct device *dev which
>>>> is the same size.  The latter is then used to obtain a struct
>>>> acpi_device pointer.  Why is it better to do this than to store the
>>>> struct acpi_device itself?
>>> This should be "store the struct acpi_device pointer itself", sorry.
>> Hi,
>> So let me explain the reasoning here:
>>
>> 1) I've had a pleasure to work with different drivers in ACPI directory. In my
>>     opinion the best ones I've seen, were build around the concept of struct
>>     device (e.g NFIT). It's a struct that's well understood in the kernel, and
>>     it's easier to understand without having to read any ACPI specific code.
>>     If I see something like ACPI_HANDLE(dev), I think 'ah-ha -  having a struct
>>     device I can easily get struct acpi_device - they're connected'. And I think
>>     using a standardized macro, instead of manually dereferencing pointers is
>>     also much easier for ACPI newbs reading the code, as it hides a bit complexity
>>     of getting acpi device from struct device. And to be honest I don't think there would
>>     be any measurable performance change, as those code paths are far from
>>     being considered 'hot paths'. So if we can get the code easier to understand
>>     from a newcomer perspective, why not do it.
> I have a differing opinion on a couple of points above, and let's make
> one change at a time.

OK

>
>> 2) I think it would be good to stick to the convention, and introduce some coding
>>      pattern, for now some drivers store the struct device pointer, and other store
>>      acpi device pointer . As I'm doing this change acpi device pointer become less
>>      relevant, as we're using platform device. So to reflect that loss of relevance
>>      we can choose to adhere to a pattern where we use it less and less, and the
>>      winning approach would be to use 'struct device' by default everywhere we can
>>      so maybe eventually we would be able to lose acpi_device altogether at some point,
>>      as most of the usage is to retrieve acpi handle and execute some AML method.
>>      So in my understanding acpi device is already obsolete at this point, if we can
>>      manage to use it less and less, and eventually wipe it out then why not ;-)
> No, ACPI device is not obsolete, it will still be needed for various
> things, like power management and hotplug.

Sure, haven't reviewed all that use cases yet, but the name 'acpi device'
implies like it's part of a driver framework, and it won't be anymore after
transformations are completed.

>
> Also, I'd rather store a struct acpi_device than acpi_handle, because
> the latter is much better from the compile-time type correctness
> checks and similar.

Sure that makes sense

>
> I can send my version of the $subject patch just fine if you strongly
> disagree with me.

Well I can disagree, but still change it ;-). Just explained my reasoning so
you can make a decision with all the data points provided.

Thanks a lot !
Michał

>
Rafael J. Wysocki Oct. 9, 2023, 5:51 p.m. UTC | #9
On Mon, Oct 9, 2023 at 3:04 PM Wilczynski, Michal
<michal.wilczynski@intel.com> wrote:
>
>
>
> On 10/9/2023 2:27 PM, Rafael J. Wysocki wrote:
> > On Mon, Oct 9, 2023 at 10:40 AM Wilczynski, Michal
> > <michal.wilczynski@intel.com> wrote:
> >>

[cut]

> >> Yeah we could add platform device without removing acpi device, and
> >> yes that would introduce data duplication, like Andy noticed.
> > But he hasn't answered my question regarding what data duplication he
> > meant, exactly.
> >
> > So what data duplication do you mean?
>
> So what I meant was that many drivers would find it useful to have
> a struct device embedded in their 'private structure', and in that case
> there would be a duplication of platform_device and acpi_device as
> both pointers would be needed.

It all depends on how often each of them is going to be used in the
given driver.

This particular driver only needs a struct acpi_device pointer if I'm
not mistaken.

> Mind this if you only have struct device
> it's easy to get acpi device, but it's not the case the other way around.
>
> So for this driver it's just a matter of sticking to convention,

There is no convention in this respect and there is always a tradeoff
between using more memory and using more CPU time in computing in
general, but none of them should be wasted just for the sake of
following a convention.

> for the others like it would be duplication.

So I'm only talking about the driver modified by the patch at hand.

> In my version of this patch we managed to avoid this duplication, thanks
> to the contextual argument introduced before, but look at this patch:
> https://github.com/mwilczy/linux-pm/commit/cc8ef52707341e67a12067d6ead991d56ea017ca
>
> Author of this patch had to introduce platform_device and acpi_device to the struct ac, so
> there was some duplication. That is the case for many drivers to come, so I decided it's better
> to change this and have a pattern with keeping only 'struct device'.

Well, if the only thing you need from a struct device is its
ACPI_COMPANION(), it is better to store a pointer to the latter.
diff mbox series

Patch

diff --git a/drivers/acpi/ac.c b/drivers/acpi/ac.c
index f809f6889b4a..298defeb5301 100644
--- a/drivers/acpi/ac.c
+++ b/drivers/acpi/ac.c
@@ -33,8 +33,9 @@  MODULE_AUTHOR("Paul Diefenbaugh");
 MODULE_DESCRIPTION("ACPI AC Adapter Driver");
 MODULE_LICENSE("GPL");
 
-static int acpi_ac_add(struct acpi_device *device);
-static void acpi_ac_remove(struct acpi_device *device);
+static int acpi_ac_probe(struct platform_device *pdev);
+static void acpi_ac_remove(struct platform_device *pdev);
+
 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data);
 
 static const struct acpi_device_id ac_device_ids[] = {
@@ -51,21 +52,10 @@  static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
 static int ac_sleep_before_get_state_ms;
 static int ac_only;
 
-static struct acpi_driver acpi_ac_driver = {
-	.name = "ac",
-	.class = ACPI_AC_CLASS,
-	.ids = ac_device_ids,
-	.ops = {
-		.add = acpi_ac_add,
-		.remove = acpi_ac_remove,
-		},
-	.drv.pm = &acpi_ac_pm,
-};
-
 struct acpi_ac {
 	struct power_supply *charger;
 	struct power_supply_desc charger_desc;
-	struct acpi_device *device;
+	struct device *dev;
 	unsigned long long state;
 	struct notifier_block battery_nb;
 };
@@ -85,10 +75,10 @@  static int acpi_ac_get_state(struct acpi_ac *ac)
 		return 0;
 	}
 
-	status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
+	status = acpi_evaluate_integer(ACPI_HANDLE(ac->dev), "_PSR", NULL,
 				       &ac->state);
 	if (ACPI_FAILURE(status)) {
-		acpi_handle_info(ac->device->handle,
+		acpi_handle_info(ACPI_HANDLE(ac->dev),
 				"Error reading AC Adapter state: %s\n",
 				acpi_format_exception(status));
 		ac->state = ACPI_AC_STATUS_UNKNOWN;
@@ -129,12 +119,12 @@  static enum power_supply_property ac_props[] = {
 /* Driver Model */
 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
 {
-	struct acpi_device *device = data;
-	struct acpi_ac *ac = acpi_driver_data(device);
+	struct acpi_ac *ac = data;
+	struct acpi_device *device = ACPI_COMPANION(ac->dev);
 
 	switch (event) {
 	default:
-		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
+		acpi_handle_debug(ACPI_HANDLE(ac->dev), "Unsupported event [0x%x]\n",
 				  event);
 		fallthrough;
 	case ACPI_AC_NOTIFY_STATUS:
@@ -152,9 +142,10 @@  static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
 
 		acpi_ac_get_state(ac);
 		acpi_bus_generate_netlink_event(device->pnp.device_class,
-						  dev_name(&device->dev), event,
-						  (u32) ac->state);
-		acpi_notifier_call_chain(device, event, (u32) ac->state);
+						dev_name(ac->dev),
+						event,
+						ac->state);
+		acpi_notifier_call_chain(device, event, ac->state);
 		kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
 	}
 }
@@ -211,8 +202,9 @@  static const struct dmi_system_id ac_dmi_table[]  __initconst = {
 	{},
 };
 
-static int acpi_ac_add(struct acpi_device *device)
+static int acpi_ac_probe(struct platform_device *pdev)
 {
+	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
 	struct power_supply_config psy_cfg = {};
 	struct acpi_ac *ac;
 	int result;
@@ -221,10 +213,11 @@  static int acpi_ac_add(struct acpi_device *device)
 	if (!ac)
 		return -ENOMEM;
 
-	ac->device = device;
+	ac->dev = &pdev->dev;
 	strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
 	strcpy(acpi_device_class(device), ACPI_AC_CLASS);
-	device->driver_data = ac;
+
+	platform_set_drvdata(pdev, ac);
 
 	result = acpi_ac_get_state(ac);
 	if (result)
@@ -237,7 +230,7 @@  static int acpi_ac_add(struct acpi_device *device)
 	ac->charger_desc.properties = ac_props;
 	ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
 	ac->charger_desc.get_property = get_ac_property;
-	ac->charger = power_supply_register(&ac->device->dev,
+	ac->charger = power_supply_register(&pdev->dev,
 					    &ac->charger_desc, &psy_cfg);
 	if (IS_ERR(ac->charger)) {
 		result = PTR_ERR(ac->charger);
@@ -251,7 +244,7 @@  static int acpi_ac_add(struct acpi_device *device)
 	register_acpi_notifier(&ac->battery_nb);
 
 	result = acpi_dev_install_notify_handler(device, ACPI_ALL_NOTIFY,
-						 acpi_ac_notify, device);
+						 acpi_ac_notify, ac);
 	if (result)
 		goto err_unregister;
 
@@ -269,7 +262,7 @@  static int acpi_ac_add(struct acpi_device *device)
 #ifdef CONFIG_PM_SLEEP
 static int acpi_ac_resume(struct device *dev)
 {
-	struct acpi_ac *ac = acpi_driver_data(to_acpi_device(dev));
+	struct acpi_ac *ac = dev_get_drvdata(dev);
 	unsigned int old_state;
 
 	old_state = ac->state;
@@ -284,11 +277,12 @@  static int acpi_ac_resume(struct device *dev)
 #define acpi_ac_resume NULL
 #endif
 
-static void acpi_ac_remove(struct acpi_device *device)
+static void acpi_ac_remove(struct platform_device *pdev)
 {
-	struct acpi_ac *ac = acpi_driver_data(device);
+	struct acpi_ac *ac = platform_get_drvdata(pdev);
 
-	acpi_dev_remove_notify_handler(device, ACPI_ALL_NOTIFY,
+	acpi_dev_remove_notify_handler(ACPI_COMPANION(ac->dev),
+				       ACPI_ALL_NOTIFY,
 				       acpi_ac_notify);
 	power_supply_unregister(ac->charger);
 	unregister_acpi_notifier(&ac->battery_nb);
@@ -296,6 +290,16 @@  static void acpi_ac_remove(struct acpi_device *device)
 	kfree(ac);
 }
 
+static struct platform_driver acpi_ac_driver = {
+	.probe = acpi_ac_probe,
+	.remove_new = acpi_ac_remove,
+	.driver = {
+		.name = "ac",
+		.acpi_match_table = ac_device_ids,
+		.pm = &acpi_ac_pm,
+	},
+};
+
 static int __init acpi_ac_init(void)
 {
 	int result;
@@ -308,7 +312,7 @@  static int __init acpi_ac_init(void)
 
 	dmi_check_system(ac_dmi_table);
 
-	result = acpi_bus_register_driver(&acpi_ac_driver);
+	result = platform_driver_register(&acpi_ac_driver);
 	if (result < 0)
 		return -ENODEV;
 
@@ -317,7 +321,7 @@  static int __init acpi_ac_init(void)
 
 static void __exit acpi_ac_exit(void)
 {
-	acpi_bus_unregister_driver(&acpi_ac_driver);
+	platform_driver_unregister(&acpi_ac_driver);
 }
 module_init(acpi_ac_init);
 module_exit(acpi_ac_exit);