All of lore.kernel.org
 help / color / mirror / Atom feed
* New Lenovo Legion Fan, Temperature, Power Mode Driver
@ 2023-01-10 13:26 John Martens
  2023-01-12 19:07 ` Hans de Goede
  2023-02-02 14:44 ` Hans de Goede
  0 siblings, 2 replies; 4+ messages in thread
From: John Martens @ 2023-01-10 13:26 UTC (permalink / raw)
  To: platform-driver-x86, ike.pan, hdegoede

Dear Kernel devs, Mr. Hans de Goede, Mr. Ike Panhc,

I am currently working on a driver for fan control, fan speed, temperature sensors, and power mode (platform profile) for Lenovo Legion Laptops. Switching iGPU/dGPU could also be possible. It is a port of the closed and open tools in Windows LenovoLegionToolkit, Vantage, LegionFanControl. I am testing it on different laptops with the help of a forum/chat and its working quite good.

There is a README (https://github.com/johnfanv2/LenovoLegionLinux) and code (https://github.com/johnfanv2/LenovoLegionLinux/blob/main/kernel_module/legion-laptop.c).

I would be interested to get your opinion.

Questions

Should this extend ideapad_laptop.c or a new file?
    - pro:
        - both access parts of the same hardware
    - con:
        - both files are already quite large
        - it only works on Lenovo Legion laptops that have this 
          custom control firmware in the embedded controller (EC)
        - there is almost no reuse of code

Which method do you prefer writing to EC memory for older models? With ioremap or outb? 
    - To use ioremap one needs to get the start address. It is
      different on Intel vs AMD. It is the same as a OperationRegion
      in the ACPI tables, e.g. "OperationRegion (ERAX, SystemMemory,  
      0xFE00D400, 0xFF)". However, I have found no kernel functions  
      to get the address (here 0xFE00D400) of a  OperationRegion.  
      One could also hardcode it for each model/firmware.
    - alternative (which I am currently using) is sending commands    
      to IO ports 0x4E/0x4F (Super IO controller). 

Background

The laptops come with an embedded controller (EC) from ITE. These usually come with a 3 point fan curve in ROM, but also can be flashed with a small additional custom program. Lenovo implemented implemented a 10 point fan curve. The program is also shipped with each EFI update.

The fan curve can be edited by writing to some memory locations in the EC. These locations are

The driver works by:
- directly writing/reading embedded controller memory
    - older models (2020-2021): there are two possibilities
        - the EC memory is already memory mapped, so one can 
          use ioremap
        - one can use outb/inb and write sequenc of commands to 
          port 0x43, 0x4F (super IO ports)
        - ideapad_laptop.c writes to some parts of EC memory 
          with ACPI methods VPCR, VPCW. However, these do not seem  
          to work in the memory region with the fan curve.
    - newer models (2022): these provide ACPI/WMI methods  
      setFanCurve/getFanCurve to write to these regions. However, I  
      have implemented that and have no models for testing

The driver works for the models 2020-2022. The code layout is heavily inspired by the ideapad_laptop driver.

Best regards,

John Martens


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: New Lenovo Legion Fan, Temperature, Power Mode Driver
  2023-01-10 13:26 New Lenovo Legion Fan, Temperature, Power Mode Driver John Martens
@ 2023-01-12 19:07 ` Hans de Goede
  2023-01-30 14:35   ` john.martens.linux
  2023-02-02 14:44 ` Hans de Goede
  1 sibling, 1 reply; 4+ messages in thread
From: Hans de Goede @ 2023-01-12 19:07 UTC (permalink / raw)
  To: John Martens, platform-driver-x86, ike.pan

Hi John,

On 1/10/23 14:26, John Martens wrote:
> Dear Kernel devs, Mr. Hans de Goede, Mr. Ike Panhc,
> 
> I am currently working on a driver for fan control, fan speed, temperature sensors, and power mode (platform profile) for Lenovo Legion Laptops. Switching iGPU/dGPU could also be possible. It is a port of the closed and open tools in Windows LenovoLegionToolkit, Vantage, LegionFanControl. I am testing it on different laptops with the help of a forum/chat and its working quite good.

Thank you for reaching out. I'm currently catching up with
quite a big patch/bug backlog after being on holiday for
2 weeks. I'll get back to you on this, but please give
me some time.

If you have not heard back from me in 2 weeks, feel free
to ping me about this.

Regards,

Hans

 


> 
> There is a README (https://github.com/johnfanv2/LenovoLegionLinux) and code (https://github.com/johnfanv2/LenovoLegionLinux/blob/main/kernel_module/legion-laptop.c).
> 
> I would be interested to get your opinion.
> 
> Questions
> 
> Should this extend ideapad_laptop.c or a new file?
>     - pro:
>         - both access parts of the same hardware
>     - con:
>         - both files are already quite large
>         - it only works on Lenovo Legion laptops that have this 
>           custom control firmware in the embedded controller (EC)
>         - there is almost no reuse of code
> 
> Which method do you prefer writing to EC memory for older models? With ioremap or outb? 
>     - To use ioremap one needs to get the start address. It is
>       different on Intel vs AMD. It is the same as a OperationRegion
>       in the ACPI tables, e.g. "OperationRegion (ERAX, SystemMemory,  
>       0xFE00D400, 0xFF)". However, I have found no kernel functions  
>       to get the address (here 0xFE00D400) of a  OperationRegion.  
>       One could also hardcode it for each model/firmware.
>     - alternative (which I am currently using) is sending commands    
>       to IO ports 0x4E/0x4F (Super IO controller). 
> 
> Background
> 
> The laptops come with an embedded controller (EC) from ITE. These usually come with a 3 point fan curve in ROM, but also can be flashed with a small additional custom program. Lenovo implemented implemented a 10 point fan curve. The program is also shipped with each EFI update.
> 
> The fan curve can be edited by writing to some memory locations in the EC. These locations are
> 
> The driver works by:
> - directly writing/reading embedded controller memory
>     - older models (2020-2021): there are two possibilities
>         - the EC memory is already memory mapped, so one can 
>           use ioremap
>         - one can use outb/inb and write sequenc of commands to 
>           port 0x43, 0x4F (super IO ports)
>         - ideapad_laptop.c writes to some parts of EC memory 
>           with ACPI methods VPCR, VPCW. However, these do not seem  
>           to work in the memory region with the fan curve.
>     - newer models (2022): these provide ACPI/WMI methods  
>       setFanCurve/getFanCurve to write to these regions. However, I  
>       have implemented that and have no models for testing
> 
> The driver works for the models 2020-2022. The code layout is heavily inspired by the ideapad_laptop driver.
> 
> Best regards,
> 
> John Martens
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: New Lenovo Legion Fan, Temperature, Power Mode Driver
  2023-01-12 19:07 ` Hans de Goede
@ 2023-01-30 14:35   ` john.martens.linux
  0 siblings, 0 replies; 4+ messages in thread
From: john.martens.linux @ 2023-01-30 14:35 UTC (permalink / raw)
  To: Hans de Goede; +Cc: John Martens, platform-driver-x86, ike.pan

Hi Hans,

On Thursday, January 12th, 2023 at 8:07 PM, Hans de Goede wrote:
> Thank you for reaching out. I'm currently catching up with
> quite a big patch/bug backlog after being on holiday for
> 2 weeks. I'll get back to you on this, but please give
> me some time.
> 
> If you have not heard back from me in 2 weeks, feel free
> to ping me about this.
> 

I would be happy to get your opinion if you find some time. 

In the meantime I have tested it successfully with more 
different laptop models. It seems to work on all the expected 
models and does not work on other models as expected - just 
like the Windows tools. 
Moreover, it will probably get patched into the Nobara kernel, 
a Fedora based distro for gaming.

Best regards,

John Martens

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: New Lenovo Legion Fan, Temperature, Power Mode Driver
  2023-01-10 13:26 New Lenovo Legion Fan, Temperature, Power Mode Driver John Martens
  2023-01-12 19:07 ` Hans de Goede
@ 2023-02-02 14:44 ` Hans de Goede
  1 sibling, 0 replies; 4+ messages in thread
From: Hans de Goede @ 2023-02-02 14:44 UTC (permalink / raw)
  To: John Martens, platform-driver-x86, ike.pan

Hi John,

On 1/10/23 14:26, John Martens wrote:
> Dear Kernel devs, Mr. Hans de Goede, Mr. Ike Panhc,
> 
> I am currently working on a driver for fan control, fan speed, temperature sensors, and power mode (platform profile) for Lenovo Legion Laptops. Switching iGPU/dGPU could also be possible. It is a port of the closed and open tools in Windows LenovoLegionToolkit, Vantage, LegionFanControl. I am testing it on different laptops with the help of a forum/chat and its working quite good.
> 
> There is a README (https://github.com/johnfanv2/LenovoLegionLinux) and code (https://github.com/johnfanv2/LenovoLegionLinux/blob/main/kernel_module/legion-laptop.c).
> 
> I would be interested to get your opinion.
> 
> Questions
> 
> Should this extend ideapad_laptop.c or a new file?
>     - pro:
>         - both access parts of the same hardware
>     - con:
>         - both files are already quite large
>         - it only works on Lenovo Legion laptops that have this 
>           custom control firmware in the embedded controller (EC)
>         - there is almost no reuse of code

As you say there is almost 0 code re-use so I don't think that
adding this to ideapad_laptop.c is worth the trouble, ideapad_laptop.c
already is big enough as is.

I do see that you bind to the PNP0C09 or VPC2004 device. Since you
poke the EC RAM and don't make any ACPI calls AFAICT it would be
best to just create your own lenovo_legion platform_device to
bind to, using platform_create_bundle() which will create
a platform_device + platform_driver for you in one go.

This will avoid conflicts with other drivers binding to
the VPC2004 device such as ideapad_laptop.

And you can then also specify the used superio mem or io-ports
as resources (and claim then in your probe()) so that these
properly show up in /proc/ioports and so that conflicts
with other drivers also banging on these ports get detected.

> Which method do you prefer writing to EC memory for older models? With ioremap or outb? 
>     - To use ioremap one needs to get the start address. It is
>       different on Intel vs AMD. It is the same as a OperationRegion
>       in the ACPI tables, e.g. "OperationRegion (ERAX, SystemMemory,  
>       0xFE00D400, 0xFF)". However, I have found no kernel functions  
>       to get the address (here 0xFE00D400) of a  OperationRegion.  
>       One could also hardcode it for each model/firmware.

These addresses sometimes change at boot-time when different BIOS
options are enabled/disabled so I would advice against
hardcoding them.

Have you looked in:

sudo cat /proc/iomem

To see if the range is perhaps known to the kernel somehow already ?

>     - alternative (which I am currently using) is sending commands    
>       to IO ports 0x4E/0x4F (Super IO controller). 

I wonder if these ports are not already in use by some other driver
causing possible conflicts. Are they not listed in "sudo cat /proc/ioports" /

> Background
> 
> The laptops come with an embedded controller (EC) from ITE. These usually come with a 3 point fan curve in ROM, but also can be flashed with a small additional custom program. Lenovo implemented implemented a 10 point fan curve. The program is also shipped with each EFI update.
> 
> The fan curve can be edited by writing to some memory locations in the EC. These locations are
> 
> The driver works by:
> - directly writing/reading embedded controller memory
>     - older models (2020-2021): there are two possibilities
>         - the EC memory is already memory mapped, so one can 
>           use ioremap
>         - one can use outb/inb and write sequenc of commands to 
>           port 0x43, 0x4F (super IO ports)
>         - ideapad_laptop.c writes to some parts of EC memory 
>           with ACPI methods VPCR, VPCW. However, these do not seem  
>           to work in the memory region with the fan curve.

Ok, so lets see of we can figure out a better way to get the EC
memory address and if not I guess outb/inb it is.

>     - newer models (2022): these provide ACPI/WMI methods  
>       setFanCurve/getFanCurve to write to these regions. However, I  
>       have implemented that and have no models for testing

From the sound of it we should definitely use this new methods
on these newer models, this should protect us against the
EC RAM layout changing.  This might be best done in separate
driver, that depends on how much code we can re-use if we
put support for the new models inside the same driver.

Regards,

Hans



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-02-02 14:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-10 13:26 New Lenovo Legion Fan, Temperature, Power Mode Driver John Martens
2023-01-12 19:07 ` Hans de Goede
2023-01-30 14:35   ` john.martens.linux
2023-02-02 14:44 ` Hans de Goede

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.