==== //depot/projects/soc2007/rpaulo-macbook/dev/backlight/backlight.c#5 - /home/rpaulo/p4/rpaulo-macbook/dev/backlight/backlight.c ==== --- /tmp/tmp.72675.16 Sat Jun 2 15:14:36 2007 +++ /home/rpaulo/p4/rpaulo-macbook/dev/backlight/backlight.c Sat Jun 2 15:13:02 2007 @@ -65,8 +65,15 @@ static int backlight_detach(device_t); static int macbook_attach(device_t); static int macbook_enable(SYSCTL_HANDLER_ARGS); static int macbook_level(SYSCTL_HANDLER_ARGS); +static int macbookpro_attach(device_t); +static int macbookpro_enable(SYSCTL_HANDLER_ARGS); +static int macbookpro_level(SYSCTL_HANDLER_ARGS); + struct backlight_model backlight_models[] = { + /* + * MacBook. + */ { "MacBook1,1", 0x8086, 0x27a2, "MacBook Core Duo Backlight Control", macbook_attach, macbook_enable, macbook_level }, @@ -75,6 +82,26 @@ struct backlight_model backlight_models[ "MacBook Core 2 Duo Backlight Control", macbook_attach, macbook_enable, macbook_level }, + /* + * MacBook Pro. + */ + { "MacBookPro1,1", 0x1002, 0x71c5, + "MacBook Pro Core Duo (15-inch) Backlight Control", + macbookpro_attach, macbookpro_enable, macbookpro_level }, + + { "MacBookPro1,2", 0x1002, 0x71c5, + "MacBook Pro Core Duo (17-inch) Backlight Control", + macbookpro_attach, macbookpro_enable, macbookpro_level }, + + { "MacBookPro2,1", 0x1002, 0x71c5, + "MacBook Pro Core 2 Duo (17-inch) Backlight Control", + macbookpro_attach, macbookpro_enable, macbookpro_level }, + + { "MacBookPro2,2", 0x1002, 0x71c5, + "MacBook Pro Core 2 Duo (15-inch) Backlight Control", + macbookpro_attach, macbookpro_enable, macbookpro_level }, + + { NULL, 0, 0 } }; @@ -348,6 +375,117 @@ macbook_level(SYSCTL_HANDLER_ARGS) curlevel = (level * (BACKLIGHT_MB_MAX - BACKLIGHT_MB_MIN) / 100) + BACKLIGHT_MB_MIN; macbook_set_current(sc, curlevel); + + *(unsigned int *)oidp->oid_arg1 = level; + sc->sc_level = curlevel; + + } + + return error; +} + + +/* + * Apple's MacBook Pro specific functions. + */ + +static uint32_t +macbookpro_get_current(struct backlight_softc *sc) +{ + uint32_t level; + + level = bus_read_4(sc->sc_res, BACKLIGHT_MBP_OFFSET); + level = level >> BACKLIGHT_MBP_CUR_SHIFT; + + return level; +} + +static void +macbookpro_set_current(struct backlight_softc *sc, uint32_t value) +{ + uint32_t newlevel; + + newlevel = 0x00000001 | (value << BACKLIGHT_MBP_CUR_SHIFT); + + bus_write_4(sc->sc_res, BACKLIGHT_MBP_OFFSET, newlevel); + + return; +} + +static int +macbookpro_attach(device_t dev) +{ + struct backlight_softc *sc = device_get_softc(dev); + int rid; + + rid = PCIR_BAR(0); + sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (sc->sc_res == NULL) + return ENOMEM; + + sc->sc_enable = macbookpro_get_current(sc) >= BACKLIGHT_MBP_MIN ? 1 : 0; + sc->sc_level = macbookpro_get_current(sc); + + return 0; +} + +static int +macbookpro_enable(SYSCTL_HANDLER_ARGS) +{ + struct backlight_softc *sc = (struct backlight_softc *) arg1; + int error; + unsigned int enable; + + enable = macbookpro_get_current(sc) >= BACKLIGHT_MBP_MIN ? 1 : 0; + + error = sysctl_handle_int(oidp, &enable, 0, req); + + if (error == 0 && req->newptr != NULL) { + enable = *(unsigned int *)req->newptr; + + switch (enable) { + case 0: + macbookpro_set_current(sc, 0); + break; + case 1: + macbookpro_set_current(sc, sc->sc_level); + break; + default: + return EINVAL; + + } + *(unsigned int *)oidp->oid_arg1 = enable; + } + + + return error; +} + +static int +macbookpro_level(SYSCTL_HANDLER_ARGS) +{ + struct backlight_softc *sc = (struct backlight_softc *) arg1; + int error; + uint32_t curlevel; + unsigned int level; + + curlevel = macbookpro_get_current(sc); + + level = (curlevel - BACKLIGHT_MBP_MIN) * 100 / + (BACKLIGHT_MBP_MAX - BACKLIGHT_MBP_MIN); + + error = sysctl_handle_int(oidp, &level, 0, req); + + if (error == 0 && req->newptr != NULL) { + level = *(unsigned int *)req->newptr; + + if (level > 100) + return EINVAL; + + curlevel = (level * (BACKLIGHT_MBP_MAX - BACKLIGHT_MBP_MIN) + / 100) + BACKLIGHT_MBP_MIN; + macbookpro_set_current(sc, curlevel); *(unsigned int *)oidp->oid_arg1 = level; sc->sc_level = curlevel; ==== //depot/projects/soc2007/rpaulo-macbook/dev/backlight/backlightvar.h#2 - /home/rpaulo/p4/rpaulo-macbook/dev/backlight/backlightvar.h ==== --- /tmp/tmp.72675.89 Sat Jun 2 15:14:37 2007 +++ /home/rpaulo/p4/rpaulo-macbook/dev/backlight/backlightvar.h Sat Jun 2 13:47:11 2007 @@ -65,3 +65,11 @@ struct backlight_model { #define BACKLIGHT_MB_MAX 0x94 #define BACKLIGHT_MB_OFF 0x12 #define BACKLIGHT_MB_ON 0x1f + +/* + * MacBook Pro specific. + */ +#define BACKLIGHT_MBP_OFFSET 0x7af8 /* register offset */ +#define BACKLIGHT_MBP_CUR_SHIFT 0x08 +#define BACKLIGHT_MBP_MIN 0x00 +#define BACKLIGHT_MBP_MAX 0xff