> Date: Thu, 24 Nov 2022 19:04:03 +0100
> From: Tobias Heider <tobias.hei...@stusta.de>
> 
> The diff below disables the screen backlight on apple silicon macs when the
> lid is closed.

Can we distinguish lid close events from lid open events?  Might make
more sense to base the decision to turn the display off based on that
instead of toggling a variable.

> Normally, we suspend or hibernate depending on the value of
> machdep.lid_action.  Since suspend doesn't work reliably yet I think
> this is a good intermediate solution to save some power while the
> laptop is idling.

Even though acpibtn(4) documents 0 as "Do nothing", doesn't it make
sense to always turn off the display backlight when the lid gets
closed?

Then we could actually implement 1 as suspend but just set lid_action
to 0?

> ok?
> 
> Index: arch/arm64/arm64/acpi_machdep.c
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/arm64/acpi_machdep.c,v
> retrieving revision 1.20
> diff -u -p -r1.20 acpi_machdep.c
> --- arch/arm64/arm64/acpi_machdep.c   13 Sep 2022 17:14:54 -0000      1.20
> +++ arch/arm64/arm64/acpi_machdep.c   24 Nov 2022 17:57:09 -0000
> @@ -36,7 +36,6 @@
>  
>  #include "apm.h"
>  
> -int  lid_action;
>  int  pwr_action = 1;
>  
>  int  acpi_fdt_match(struct device *, void *, void *);
> Index: arch/arm64/arm64/machdep.c
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/arm64/machdep.c,v
> retrieving revision 1.77
> diff -u -p -r1.77 machdep.c
> --- arch/arm64/arm64/machdep.c        24 Nov 2022 14:43:16 -0000      1.77
> +++ arch/arm64/arm64/machdep.c        24 Nov 2022 17:57:09 -0000
> @@ -71,6 +71,7 @@ void (*cpuresetfn)(void);
>  void (*powerdownfn)(void);
>  
>  int cold = 1;
> +int lid_action = 1;
>  
>  struct vm_map *exec_map = NULL;
>  struct vm_map *phys_map = NULL;
> @@ -322,6 +323,10 @@ extern uint64_t cpu_id_aa64pfr1;
>   * machine dependent system variables.
>   */
>  
> +const struct sysctl_bounded_args cpuctl_vars[] = {
> +     { CPU_LIDACTION, &lid_action, 0, 2 },
> +};
> +
>  int
>  cpu_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
>      size_t newlen, struct proc *p)
> @@ -372,7 +377,8 @@ cpu_sysctl(int *name, u_int namelen, voi
>       case CPU_ID_AA64ZFR0:
>               return sysctl_rdquad(oldp, oldlenp, newp, 0);
>       default:
> -             return (EOPNOTSUPP);
> +             return (sysctl_bounded_arr(cpuctl_vars, nitems(cpuctl_vars),
> +                 name, namelen, oldp, oldlenp, newp, newlen));
>       }
>       /* NOTREACHED */
>  }
> Index: arch/arm64/dev/aplsmc.c
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/dev/aplsmc.c,v
> retrieving revision 1.18
> diff -u -p -r1.18 aplsmc.c
> --- arch/arm64/dev/aplsmc.c   14 Nov 2022 11:11:17 -0000      1.18
> +++ arch/arm64/dev/aplsmc.c   24 Nov 2022 17:57:09 -0000
> @@ -37,6 +37,9 @@
>  
>  #include "apm.h"
>  
> +extern int lid_action;
> +extern void (*simplefb_burn_hook)(u_int);
> +
>  extern void (*cpuresetfn)(void);
>  extern void (*powerdownfn)(void);
>  
> @@ -124,6 +127,8 @@ struct aplsmc_softc {
>       struct ksensor          sc_sensors[APLSMC_MAX_SENSORS];
>       int                     sc_nsensors;
>       struct ksensordev       sc_sensordev;
> +
> +     int                     sc_wakeup;
>  };
>  
>  struct aplsmc_softc *aplsmc_sc;
> @@ -326,6 +331,8 @@ aplsmc_attach(struct device *parent, str
>       sensordev_install(&sc->sc_sensordev);
>       sensor_task_register(sc, aplsmc_refresh_sensors, 5);
>  
> +     sc->sc_wakeup = 0;
> +
>  #if NAPM > 0
>       apm_setinfohook(aplsmc_apminfo);
>  #endif
> @@ -372,7 +379,14 @@ aplsmc_handle_notification(struct aplsmc
>               }
>               break;
>       case SMC_EV_TYPE_LID:
> -             /* XXX Handle lid events. */
> +             switch (lid_action) {
> +             case 1: /* Suspend */
> +             case 2: /* Hibernate */
> +                     if (simplefb_burn_hook)
> +                             simplefb_burn_hook(sc->sc_wakeup);
> +                     sc->sc_wakeup = !sc->sc_wakeup;
> +                     break;
> +             }
>               break;
>       default:
>  #ifdef APLSMC_DEBUG
> Index: arch/arm64/include/cpu.h
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/include/cpu.h,v
> retrieving revision 1.31
> diff -u -p -r1.31 cpu.h
> --- arch/arm64/include/cpu.h  24 Nov 2022 14:43:16 -0000      1.31
> +++ arch/arm64/include/cpu.h  24 Nov 2022 17:57:09 -0000
> @@ -36,7 +36,8 @@
>  #define      CPU_ID_AA64PFR1         9
>  #define      CPU_ID_AA64SMFR0       10
>  #define      CPU_ID_AA64ZFR0        11
> -#define      CPU_MAXID              12       /* number of valid machdep ids 
> */
> +#define      CPU_LIDACTION          12
> +#define      CPU_MAXID              13       /* number of valid machdep ids 
> */
>  
>  #define      CTL_MACHDEP_NAMES { \
>       { 0, 0 }, \
> 
> 

Reply via email to