Batocera Fan Control

Which system do you use? Android, Ubuntu, OOWOW or others?

Batocera VIM4

Which version of system do you use? Please provide the version of the system here:

v40 / v39

Please describe your issue below:

Fan does not work in Batocera. The fan works when booted with Android and Ubuntu Kernel 5.4.

Post a console log of your issue below:

Tried the following and none of them make the fan work

[root@BATOCERA /]# echo 1 > /sys/class/fan/enable 
[root@BATOCERA /]# echo 0 > /sys/class/fan/mode
[root@BATOCERA /]# echo 3 > /sys/class/fan/level
[root@BATOCERA /]# i2cset -y -f 6 0x18 0x8a 0x64
[root@BATOCERA /]# uname -a
Linux BATOCERA 5.4.180 #2 SMP PREEMPT Wed Jul 17 01:36:21 Europe 2024 aarch64 GNU/Linux

@shyAm I will check this issue and let you know

@Electr1 Thank you. I pushed few changes to the Batocera repo to fix some VIM4 issues.

Even
$ echo 1 > /sys/class/mcu/poweroff
doesn’t work

I also tried with Kernel 5.15 and upgraded uboot but the fan still doesn’t work.

To avoid building from scratch you can try the following pre-built images

batocera-a3gen2-khadas-vim4-39-20240305.img.gz
batocera-a3gen2-khadas-vim4-40-20240622.img.gz

oowow image
vim4-batocera-a3gen2-39-20231217.img.xz

@shyAm I have replicated your issue, it does seem the fan isn’t functioning, the mcu is able to control other things such as the onboard LEDs, but the fan is not operational.

will check this later and let you know

Happy to report that I was able to root cause and fix the issue. Batocera compiles the Kernel using GCC 12. Maybe that is triggering the issues. Anyway here was my finding before the fix.

The i2c driver was sending incorrect value to i2c device
[ 1581.469825] meson-i2c fe076000.i2c: meson_i2c_set_clk_div_std: clk 166666664, freq 100000, div_h 819, div_l 417
[ 1581.469862] meson-i2c fe076000.i2c: meson_i2c_put_data: data ffffff8a 00000000 len 2
[ 1581.470199] meson-i2c fe076000.i2c: irq: state 2, pos 0, count 2, ctrl 06333050

As you can see the value ffffff8a should have been 0000yy8a where yy is fan value instead it was 0xff. This only happens if the MSB byte (0x8a) is any value above 0x7f. To confirm I enabled the WOL which correctly populated the word.

[ 2585.123104] meson-i2c fe076000.i2c: meson_i2c_set_clk_div_std: clk 166666664, freq 100000, div_h 819, div_l 417
[ 2585.123117] meson-i2c fe076000.i2c: meson_i2c_put_data: data 00000121 00000000 len 2
[ 2585.123442] meson-i2c fe076000.i2c: irq: state 2, pos 0, count 2, ctrl 06333050

Khadas Team, Please consider applying the following patch in your branches. This can manifest itself if not compiled with your toolchain. Either way the functions should not have converted original i2c u8 pointer to char pointer.

 diff --git a/drivers/i2c/busses/i2c-meson.c b/drivers/i2c/busses/i2c-meson.c
 index 0133170f5..7b59ac86a 100644
 --- a/drivers/i2c/busses/i2c-meson.c
 +++ b/drivers/i2c/busses/i2c-meson.c
 @@ -273,7 +273,7 @@ static void meson_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
  }
  #endif
  
 -static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
 +static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len)
  {
  	u32 rdata0, rdata1;
  	int i;
 @@ -291,7 +291,7 @@ static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
  		*buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
  }
  
 -static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
 +static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len)
  {
  	u32 wdata0 = 0, wdata1 = 0;
  	int i;

After the patch, Here is the result

[ 408.562688] meson-i2c fe076000.i2c: meson_i2c_set_clk_div_std: clk 166666664, freq 100000, div_h 819, div_l 417
[ 408.562716] meson-i2c fe076000.i2c: meson_i2c_put_data: data 0000008a 00000000 len 2
[ 408.563041] meson-i2c fe076000.i2c: irq: state 2, pos 0, count 2, ctrl 06333050
[ 439.283308] meson-i2c fe076000.i2c: meson_i2c_set_clk_div_std: clk 166666664, freq 100000, div_h 819, div_l 417
[ 439.283335] meson-i2c fe076000.i2c: meson_i2c_put_data: data 0000328a 00000000 len 2
[ 439.283659] meson-i2c fe076000.i2c: irq: state 2, pos 0, count 2, ctrl 06333050

Among other VIM4 optimizations for Batocera that I have been working hopefully this change will get merged in the upstream Batocera repo.

2 Likes

Hello @shyAm

Thanks for dig it out, you are based on Batocera repo to do this?

Yes. Here is my pending pull request

Here is another one that got merged

1 Like