Home | History | Annotate | Line # | Download | only in vr
vrc4172pwm.c revision 1.18
      1  1.18     lukem /*	$NetBSD: vrc4172pwm.c,v 1.18 2003/07/15 02:29:35 lukem Exp $	*/
      2   1.1      sato 
      3   1.1      sato /*
      4   1.8      sato  * Copyright (c) 2000,2001 SATO Kazumi. All rights reserved.
      5   1.1      sato  *
      6   1.1      sato  * Redistribution and use in source and binary forms, with or without
      7   1.1      sato  * modification, are permitted provided that the following conditions
      8   1.1      sato  * are met:
      9   1.1      sato  * 1. Redistributions of source code must retain the above copyright
     10   1.1      sato  *    notice, this list of conditions and the following disclaimer.
     11   1.1      sato  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1      sato  *    notice, this list of conditions and the following disclaimer in the
     13   1.1      sato  *    documentation and/or other materials provided with the distribution.
     14   1.1      sato  *
     15   1.1      sato  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16   1.1      sato  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17   1.1      sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18   1.1      sato  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1      sato  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1      sato  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21   1.1      sato  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1      sato  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1      sato  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1      sato  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1      sato  * SUCH DAMAGE.
     26   1.1      sato  */
     27  1.18     lukem 
     28  1.18     lukem #include <sys/cdefs.h>
     29  1.18     lukem __KERNEL_RCSID(0, "$NetBSD: vrc4172pwm.c,v 1.18 2003/07/15 02:29:35 lukem Exp $");
     30   1.1      sato 
     31   1.1      sato #include <sys/param.h>
     32   1.1      sato #include <sys/systm.h>
     33   1.1      sato #include <sys/device.h>
     34   1.1      sato #include <sys/reboot.h>
     35   1.1      sato 
     36   1.1      sato #include <machine/bus.h>
     37   1.1      sato #include <machine/config_hook.h>
     38   1.1      sato #include <machine/platid.h>
     39   1.3      sato #include <machine/platid_mask.h>
     40   1.1      sato 
     41   1.3      sato #include <hpcmips/vr/vr.h>
     42  1.15  takemura #include <hpcmips/vr/vripif.h>
     43   1.1      sato #include <hpcmips/vr/vrc4172pwmvar.h>
     44   1.1      sato #include <hpcmips/vr/vrc4172pwmreg.h>
     45   1.1      sato 
     46   1.3      sato #include "locators.h"
     47   1.1      sato 
     48   1.1      sato #ifdef VRC2PWMDEBUG
     49   1.1      sato #ifndef VRC2PWMDEBUG_CONF
     50   1.1      sato #define VRC2PWMDEBUG_CONF 0
     51   1.1      sato #endif /* VRC2PWMDEBUG_CONF */
     52   1.1      sato int vrc4172pwmdebug = VRC2PWMDEBUG_CONF;
     53   1.1      sato #define DPRINTF(arg) if (vrc4172pwmdebug) printf arg;
     54   1.1      sato #define VPRINTF(arg) if (bootverbose||vrc4172pwmdebug) printf arg;
     55   1.3      sato #define VDUMPREG(arg) if (bootverbose||vrc4172pwmdebug) vrc4172pwm_dumpreg(arg);
     56   1.1      sato #else /* VRC2PWMDEBUG */
     57   1.1      sato #define DPRINTF(arg)
     58   1.1      sato #define VPRINTF(arg) if (bootverbose) printf arg;
     59   1.3      sato #define VDUMPREG(arg) if (bootverbose) vrc4172pwm_dumpreg(arg);
     60   1.1      sato #endif /* VRC2PWMDEBUG */
     61   1.1      sato 
     62  1.13       uch static int vrc4172pwmprobe(struct device *, struct cfdata *, void *);
     63  1.13       uch static void vrc4172pwmattach(struct device *, struct device *, void *);
     64   1.1      sato 
     65  1.13       uch static void vrc4172pwm_write(struct vrc4172pwm_softc *, int, unsigned short);
     66  1.13       uch static unsigned short vrc4172pwm_read(struct vrc4172pwm_softc *, int);
     67   1.1      sato 
     68  1.13       uch static int vrc4172pwm_event(void *, int, long, void *);
     69  1.13       uch static int vrc4172pwm_pmevent(void *, int, long, void *);
     70   1.1      sato 
     71  1.13       uch static void vrc4172pwm_dumpreg(struct vrc4172pwm_softc *);
     72  1.13       uch static void vrc4172pwm_init_brightness(struct vrc4172pwm_softc *);
     73  1.13       uch void vrc4172pwm_light(struct vrc4172pwm_softc *, int);
     74  1.13       uch int vrc4172pwm_get_light(struct vrc4172pwm_softc *);
     75  1.13       uch int vrc4172pwm_get_brightness(struct vrc4172pwm_softc *);
     76  1.13       uch void vrc4172pwm_set_brightness(struct vrc4172pwm_softc *, int);
     77  1.13       uch int vrc4172pwm_rawduty2brightness(struct vrc4172pwm_softc *);
     78  1.13       uch int vrc4172pwm_brightness2rawduty(struct vrc4172pwm_softc *);
     79  1.13       uch struct vrc4172pwm_param * vrc4172pwm_getparam(void);
     80  1.13       uch void vrc4172pwm_dumpreg(struct vrc4172pwm_softc *);
     81   1.1      sato 
     82  1.17   thorpej CFATTACH_DECL(vrc4172pwm, sizeof(struct vrc4172pwm_softc),
     83  1.17   thorpej     vrc4172pwmprobe, vrc4172pwmattach, NULL, NULL);
     84   1.3      sato 
     85   1.3      sato /*
     86   1.3      sato  * platform related parameters
     87   1.3      sato  */
     88  1.11      sato struct vrc4172pwm_param vrc4172pwm_mcr520_param = {
     89  1.11      sato 	1, /* probe broken */
     90  1.11      sato 	8, /* levels */
     91  1.11      sato 	{ 0x16, 0x1f, 0x24, 0x2a, 0x2f, 0x34, 0x3a, 0x3f }
     92  1.11      sato };
     93  1.11      sato 
     94   1.3      sato struct vrc4172pwm_param vrc4172pwm_mcr530_param = {
     95   1.8      sato 	0,
     96  1.11      sato 	8, /* levels */
     97   1.3      sato 	{ 0x16, 0x1b, 0x20, 0x25, 0x2a, 0x30, 0x37, 0x3f }
     98   1.3      sato };
     99   1.1      sato 
    100   1.8      sato struct vrc4172pwm_param vrc4172pwm_mcr700_param = {
    101   1.8      sato 	1, /* probe broken */
    102  1.11      sato 	8, /* levels */
    103   1.8      sato 	{ 0x12, 0x15, 0x18, 0x1d, 0x24, 0x2d, 0x38, 0x3f }
    104   1.8      sato };
    105   1.8      sato 
    106   1.9      sato struct vrc4172pwm_param vrc4172pwm_sigmarion_param = {
    107   1.9      sato 	0,
    108  1.11      sato 	8, /* levels */
    109   1.9      sato 	{ 0xe, 0x13, 0x18, 0x1c, 0x23, 0x29, 0x32, 0x3f }
    110   1.9      sato };
    111   1.9      sato 
    112   1.9      sato 
    113   1.4      sato struct platid_data vrc4172pwm_platid_param_table[] = {
    114  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_430,	&vrc4172pwm_mcr530_param},
    115  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_510,	&vrc4172pwm_mcr520_param},
    116  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_520,	&vrc4172pwm_mcr520_param},
    117  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_520A,	&vrc4172pwm_mcr520_param},
    118  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_530,	&vrc4172pwm_mcr530_param},
    119  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_530A,	&vrc4172pwm_mcr530_param},
    120  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_SIGMARION,	&vrc4172pwm_sigmarion_param},
    121  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_700,	&vrc4172pwm_mcr700_param},
    122  1.13       uch 	{ &platid_mask_MACH_NEC_MCR_700A,	&vrc4172pwm_mcr700_param},
    123   1.3      sato 	{ NULL, NULL}
    124   1.1      sato };
    125   1.1      sato 
    126   1.1      sato struct vrc4172pwm_softc *this_pwm;
    127   1.1      sato 
    128   1.1      sato static inline void
    129  1.13       uch vrc4172pwm_write(struct vrc4172pwm_softc *sc, int port, unsigned short val)
    130   1.1      sato {
    131  1.13       uch 
    132   1.1      sato 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    133   1.1      sato }
    134   1.1      sato 
    135   1.1      sato static inline unsigned short
    136  1.13       uch vrc4172pwm_read(struct vrc4172pwm_softc *sc, int port)
    137   1.1      sato {
    138  1.13       uch 
    139  1.13       uch 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, port));
    140   1.1      sato }
    141   1.1      sato 
    142   1.1      sato static int
    143  1.13       uch vrc4172pwmprobe(struct device *parent, struct cfdata *cf, void *aux)
    144   1.1      sato {
    145   1.3      sato 	platid_mask_t mask;
    146   1.3      sato 	struct vrip_attach_args *va = aux;
    147   1.3      sato 	bus_space_handle_t ioh;
    148   1.8      sato #ifdef VRC4172PWM_BROKEN_PROBE
    149   1.8      sato 	int probe = 0;
    150   1.8      sato #else /* VRC4172PWM_BROKEN_PROBE */
    151   1.8      sato 	int probe = 1;
    152   1.8      sato #endif /* VRC4172PWM_BROKEN_PROBE */
    153   1.3      sato 	int data;
    154   1.8      sato 	int data2;
    155   1.8      sato 	struct vrc4172pwm_param *param;
    156   1.3      sato 	int ret = 0;
    157   1.3      sato 
    158  1.15  takemura 	if (va->va_addr == VRIPIFCF_ADDR_DEFAULT)
    159  1.13       uch 		return (0);
    160   1.3      sato 
    161  1.15  takemura 	if (cf->cf_loc[VRIPIFCF_PLATFORM] == 0)
    162  1.13       uch 		return (0);
    163  1.15  takemura 	if (cf->cf_loc[VRIPIFCF_PLATFORM] != -1) { /* if specify */
    164  1.15  takemura 		mask = PLATID_DEREF(cf->cf_loc[VRIPIFCF_PLATFORM]);
    165   1.8      sato 		VPRINTF(("vrc4172pwmprobe: check platid\n"));
    166   1.3      sato 		if (platid_match(&platid, &mask) == 0)
    167  1.13       uch 			return (0);
    168   1.8      sato 		param = vrc4172pwm_getparam();
    169   1.8      sato 		if (param != NULL && param->brokenprobe)
    170   1.8      sato 			probe = 0;
    171   1.3      sato 	}
    172   1.8      sato 	if (probe) {
    173  1.13       uch 		if (bus_space_map(va->va_iot, va->va_addr, va->va_size, 0,
    174  1.13       uch 		    &ioh)) {
    175  1.13       uch 			return (0);
    176   1.8      sato 		}
    177   1.8      sato 		data =  bus_space_read_2(va->va_iot, ioh, VRC2_PWM_LCDDUTYEN);
    178   1.8      sato 		bus_space_write_2(va->va_iot, ioh, VRC2_PWM_LCDDUTYEN, 0xff);
    179  1.13       uch 		if ((data2 = bus_space_read_2(va->va_iot, ioh,
    180  1.13       uch 		    VRC2_PWM_LCDDUTYEN)) == VRC2_PWM_LCDEN_MASK) {
    181  1.13       uch 			VPRINTF(("vrc4172pwmprobe:"
    182  1.13       uch 			    " VRC2_PWM_LCDDUTYEN found\n"));
    183   1.8      sato 			ret = 1;
    184   1.8      sato 		} else {
    185  1.13       uch 			VPRINTF(("vrc4172pwmprobe: VRC2_PWM_LCDDUTYEN"
    186  1.13       uch 			    " not found org=%x, data=%x!=%x\n",
    187  1.13       uch 			    data, data2, VRC2_PWM_LCDEN_MASK));
    188   1.8      sato 		}
    189   1.8      sato 		bus_space_write_2(va->va_iot, ioh, VRC2_PWM_LCDDUTYEN, data);
    190   1.8      sato 		bus_space_unmap(va->va_iot, ioh, va->va_size);
    191   1.8      sato 	} else
    192   1.3      sato 		ret = 1;
    193   1.8      sato 	VPRINTF(("vrc4172pwmprobe: return %d\n", ret));
    194  1.13       uch 
    195  1.13       uch 	return (ret);
    196   1.1      sato }
    197   1.1      sato 
    198   1.1      sato static void
    199  1.13       uch vrc4172pwmattach(struct device *parent, struct device *self, void *aux)
    200   1.1      sato {
    201   1.1      sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)self;
    202   1.1      sato 	struct vrip_attach_args *va = aux;
    203   1.1      sato 
    204   1.1      sato 	bus_space_tag_t iot = va->va_iot;
    205   1.1      sato 	bus_space_handle_t ioh;
    206   1.1      sato 
    207   1.1      sato 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    208   1.1      sato 		printf(": can't map bus space\n");
    209   1.1      sato 		return;
    210   1.1      sato 	}
    211   1.1      sato 
    212   1.1      sato 	sc->sc_iot = iot;
    213   1.1      sato 	sc->sc_ioh = ioh;
    214   1.1      sato 
    215   1.1      sato 	printf("\n");
    216   1.1      sato 
    217   1.1      sato 	VDUMPREG(sc);
    218   1.1      sato 	/* basic setup */
    219   1.1      sato 	sc->sc_pmhook = config_hook(CONFIG_HOOK_PMEVENT,
    220  1.13       uch 	    CONFIG_HOOK_PMEVENT_HARDPOWER, CONFIG_HOOK_SHARE,
    221  1.13       uch 	    vrc4172pwm_pmevent, sc);
    222   1.3      sato 	sc->sc_lcdhook = config_hook(CONFIG_HOOK_POWERCONTROL,
    223  1.13       uch 	    CONFIG_HOOK_POWERCONTROL_LCDLIGHT, CONFIG_HOOK_SHARE,
    224  1.13       uch 	    vrc4172pwm_event, sc);
    225   1.3      sato 	sc->sc_getlcdhook = config_hook(CONFIG_HOOK_GET,
    226  1.13       uch 	    CONFIG_HOOK_POWER_LCDLIGHT, CONFIG_HOOK_SHARE,
    227  1.13       uch 	    vrc4172pwm_event, sc);
    228   1.1      sato 	sc->sc_sethook = config_hook(CONFIG_HOOK_SET,
    229  1.13       uch 	    CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE,
    230  1.13       uch 	    vrc4172pwm_event, sc);
    231   1.1      sato 	sc->sc_gethook = config_hook(CONFIG_HOOK_GET,
    232  1.13       uch 	    CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE,
    233  1.13       uch 	    vrc4172pwm_event, sc);
    234   1.3      sato 	sc->sc_getmaxhook = config_hook(CONFIG_HOOK_GET,
    235  1.13       uch 	    CONFIG_HOOK_BRIGHTNESS_MAX, CONFIG_HOOK_SHARE,
    236  1.13       uch 	    vrc4172pwm_event, sc);
    237   1.1      sato 
    238   1.3      sato 	vrc4172pwm_init_brightness(sc);
    239  1.11      sato 	if (sc->sc_param == NULL)
    240  1.13       uch 		printf("vrc4172pwm: NO parameter found. DISABLE pwm control\n");
    241   1.1      sato 	this_pwm = sc;
    242   1.1      sato }
    243   1.1      sato 
    244   1.3      sato /*
    245   1.3      sato  * get platform related brightness paramerters
    246   1.3      sato  */
    247   1.3      sato struct vrc4172pwm_param *
    248   1.3      sato vrc4172pwm_getparam()
    249   1.3      sato {
    250   1.4      sato 	struct platid_data *p;
    251   1.3      sato 
    252  1.14  takemura 	if ((p = platid_search_data(&platid, vrc4172pwm_platid_param_table)))
    253  1.13       uch 		return (p->data);
    254  1.13       uch 
    255  1.13       uch 	return (NULL);
    256   1.3      sato }
    257   1.3      sato 
    258   1.2      sato /*
    259   1.2      sato  *
    260   1.2      sato  * Initialize PWM brightness parameters
    261   1.2      sato  *
    262   1.2      sato  */
    263   1.2      sato void
    264  1.13       uch vrc4172pwm_init_brightness(struct vrc4172pwm_softc *sc)
    265   1.2      sato {
    266   1.3      sato 	sc->sc_param = vrc4172pwm_getparam();
    267   1.2      sato 	sc->sc_raw_freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
    268   1.2      sato 	sc->sc_raw_duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
    269   1.3      sato 	sc->sc_brightness = vrc4172pwm_rawduty2brightness(sc);
    270  1.10      sato 	sc->sc_light = vrc4172pwm_get_light(sc);
    271  1.13       uch 	DPRINTF(("vrc4172pwm_init_brightness: param=0x%x, freq=0x%x,"
    272  1.13       uch 	    " duty=0x%x, blightness=%d light=%d\n", (int)sc->sc_param,
    273  1.13       uch 	    sc->sc_raw_freq, sc->sc_raw_duty, sc->sc_brightness,
    274  1.13       uch 	    sc->sc_light));
    275   1.3      sato }
    276   1.3      sato /*
    277   1.3      sato  * backlight on/off
    278   1.3      sato  */
    279   1.3      sato void
    280  1.13       uch vrc4172pwm_light(struct vrc4172pwm_softc *sc, int on)
    281   1.3      sato {
    282   1.6      sato 	int brightness;
    283   1.6      sato 
    284   1.5      sato 	DPRINTF(("vrc4172pwm_light: %s\n", on?"ON":"OFF"));
    285   1.6      sato 	if (on) {
    286   1.6      sato 		vrc4172pwm_set_brightness(sc, sc->sc_brightness);
    287   1.3      sato 		vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCD_EN);
    288   1.6      sato 	} else {
    289   1.6      sato 		brightness = sc->sc_brightness; /* save */
    290   1.6      sato 		vrc4172pwm_set_brightness(sc, 0);
    291  1.13       uch 		/* need this, break sc->sc_brightness */
    292   1.6      sato 		sc->sc_brightness = brightness; /* resume */
    293   1.3      sato 		vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCD_DIS);
    294   1.6      sato 	}
    295  1.10      sato 	sc->sc_light = on;
    296   1.3      sato }
    297   1.3      sato 
    298   1.3      sato /*
    299   1.3      sato  * get backlight on/off
    300   1.3      sato  */
    301  1.10      sato int
    302  1.13       uch vrc4172pwm_get_light(struct vrc4172pwm_softc *sc)
    303   1.3      sato {
    304  1.13       uch 
    305  1.13       uch 	return (VRC2_PWM_LCDEN_MASK&vrc4172pwm_read(sc, VRC2_PWM_LCDDUTYEN));
    306   1.3      sato }
    307   1.3      sato 
    308   1.3      sato /*
    309   1.3      sato  * set brightness
    310   1.3      sato  */
    311   1.3      sato void
    312  1.13       uch vrc4172pwm_set_brightness(struct vrc4172pwm_softc *sc, int val)
    313   1.3      sato {
    314   1.3      sato 	int raw;
    315   1.3      sato 
    316   1.3      sato 	if (sc->sc_param == NULL)
    317   1.3      sato 		return;
    318   1.5      sato 	if (val < 0)
    319   1.5      sato 		val = 0;
    320   1.3      sato 	if (val > VRC2_PWM_MAX_BRIGHTNESS)
    321   1.3      sato 		val = VRC2_PWM_MAX_BRIGHTNESS;
    322   1.3      sato 	if (val > sc->sc_param->n_brightness)
    323   1.3      sato 		val = sc->sc_param->n_brightness;
    324   1.3      sato 	sc->sc_brightness = val;
    325   1.3      sato 	raw = vrc4172pwm_brightness2rawduty(sc);
    326   1.3      sato 	vrc4172pwm_write(sc, VRC2_PWM_LCDDUTY, raw);
    327   1.5      sato 	DPRINTF(("vrc4172pwm_set_brightness: val=%d raw=0x%x\n", val, raw));
    328   1.3      sato }
    329   1.3      sato 
    330   1.3      sato /*
    331   1.3      sato  * get brightness
    332   1.3      sato  */
    333   1.3      sato int
    334  1.13       uch vrc4172pwm_get_brightness(struct vrc4172pwm_softc *sc)
    335   1.3      sato {
    336  1.13       uch 
    337   1.3      sato 	if (sc->sc_param == NULL)
    338  1.13       uch 		return (VRC2_PWM_MAX_BRIGHTNESS);
    339  1.13       uch 
    340  1.13       uch 	return (sc->sc_brightness);
    341   1.3      sato }
    342   1.3      sato 
    343   1.3      sato /*
    344   1.3      sato  * PWM duty to brightness
    345   1.3      sato  */
    346   1.3      sato int
    347  1.13       uch vrc4172pwm_rawduty2brightness(struct vrc4172pwm_softc *sc)
    348   1.3      sato {
    349   1.3      sato 	int i;
    350   1.3      sato 
    351   1.3      sato 	if (sc->sc_param == NULL)
    352  1.13       uch 		return (VRC2_PWM_MAX_BRIGHTNESS);
    353   1.3      sato 	for (i = 0; i < sc->sc_param->n_brightness; i++) {
    354   1.8      sato 		if (sc->sc_raw_duty <= sc->sc_param->bvalues[i])
    355   1.3      sato 			break;
    356   1.3      sato 	}
    357   1.3      sato 	if (i >= sc->sc_param->n_brightness-1)
    358  1.13       uch 		return (sc->sc_param->n_brightness-1);
    359   1.3      sato 	else
    360  1.13       uch 		return (i);
    361   1.3      sato 
    362   1.3      sato }
    363   1.3      sato 
    364   1.3      sato /*
    365   1.3      sato  * brightness to raw duty
    366   1.3      sato  */
    367   1.3      sato int
    368  1.13       uch vrc4172pwm_brightness2rawduty(struct vrc4172pwm_softc *sc)
    369   1.3      sato {
    370  1.13       uch 
    371   1.3      sato 	if (sc->sc_param == NULL)
    372  1.13       uch 		return (VRC2_PWM_LCDDUTY_MASK);
    373  1.13       uch 
    374  1.13       uch 	return (sc->sc_param->bvalues[sc->sc_brightness]);
    375   1.2      sato }
    376   1.1      sato 
    377   1.3      sato 
    378   1.1      sato /*
    379   1.1      sato  * PWM config hook events
    380   1.1      sato  *
    381   1.1      sato  */
    382   1.1      sato int
    383  1.13       uch vrc4172pwm_event(void *ctx, int type, long id, void *msg)
    384   1.1      sato {
    385   1.1      sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
    386   1.1      sato         int why =(int)msg;
    387   1.1      sato 
    388   1.1      sato 	if (type == CONFIG_HOOK_POWERCONTROL
    389  1.13       uch 	    && id == CONFIG_HOOK_POWERCONTROL_LCDLIGHT) {
    390   1.7      sato 		DPRINTF(("vrc4172pwm:POWERCONTROL_LCDLIGHT: %d\n", why));
    391   1.2      sato 		vrc4172pwm_light(sc, why);
    392   1.1      sato 	} else if (type == CONFIG_HOOK_GET
    393  1.13       uch 	    && id == CONFIG_HOOK_POWER_LCDLIGHT) {
    394   1.3      sato 		*(int *)msg = vrc4172pwm_get_light(sc);
    395   1.7      sato 		DPRINTF(("vrc4172pwm:GET LCDLIGHT: %d\n", *(int *)msg));
    396   1.3      sato 	} else if (type == CONFIG_HOOK_GET
    397  1.13       uch 	    && id == CONFIG_HOOK_BRIGHTNESS) {
    398   1.3      sato 		*(int *)msg = vrc4172pwm_get_brightness(sc);
    399   1.7      sato 		DPRINTF(("vrc4172pwm:GET BRIGHTNESS: %d\n", *(int *)msg));
    400   1.3      sato 	} else if (type == CONFIG_HOOK_GET
    401  1.13       uch 	    && id == CONFIG_HOOK_BRIGHTNESS_MAX) {
    402   1.3      sato 		if (sc->sc_param == NULL)
    403   1.3      sato 			*(int *)msg = VRC2_PWM_MAX_BRIGHTNESS;
    404   1.3      sato 		else
    405   1.3      sato 			*(int *)msg = sc->sc_param->n_brightness-1;
    406   1.7      sato 		DPRINTF(("vrc4172pwm:GET MAX BRIGHTNESS: %d\n", *(int *)msg));
    407   1.1      sato 	} else if (type == CONFIG_HOOK_SET
    408  1.13       uch 	    && id == CONFIG_HOOK_BRIGHTNESS) {
    409   1.7      sato 		DPRINTF(("vrc4172pwm:SET BRIGHTNESS: %d\n", *(int *)msg));
    410   1.3      sato 		vrc4172pwm_set_brightness(sc, *(int *)msg);
    411   1.6      sato 	} else {
    412  1.13       uch 		DPRINTF(("vrc4172pwm:unknown event: type %d id %ld\n",
    413  1.13       uch 		    type, id));
    414  1.13       uch 		return (1);
    415   1.6      sato 	}
    416   1.1      sato 
    417   1.1      sato         return (0);
    418   1.1      sato }
    419   1.1      sato 
    420   1.1      sato /*
    421   1.1      sato  * PWM config hook events
    422   1.1      sato  *
    423   1.1      sato  */
    424   1.1      sato int
    425  1.13       uch vrc4172pwm_pmevent(void *ctx, int type, long id, void *msg)
    426   1.1      sato {
    427   1.1      sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
    428   1.1      sato         int why =(int)msg;
    429   1.1      sato 
    430   1.1      sato 	if (type != CONFIG_HOOK_PMEVENT)
    431  1.13       uch 		return (1);
    432   1.1      sato 
    433   1.1      sato         switch (why) {
    434   1.3      sato 	case PWR_STANDBY:
    435   1.2      sato 	case PWR_SUSPEND:
    436  1.10      sato 		sc->sc_light_save = sc->sc_light;
    437   1.2      sato 		vrc4172pwm_light(sc, 0);
    438   1.2      sato 		break;
    439   1.2      sato 	case PWR_RESUME:
    440  1.10      sato 		vrc4172pwm_light(sc, sc->sc_light_save);
    441   1.2      sato 		break;
    442   1.2      sato 	default:
    443  1.13       uch 		return (1);
    444   1.1      sato         }
    445   1.2      sato 
    446   1.1      sato         return (0);
    447   1.2      sato }
    448   1.2      sato 
    449   1.2      sato /*
    450   1.1      sato  * dump pwm registers
    451   1.1      sato  */
    452   1.1      sato void
    453  1.13       uch vrc4172pwm_dumpreg(struct vrc4172pwm_softc *sc)
    454   1.1      sato {
    455   1.1      sato 	int en, freq, duty;
    456   1.1      sato 
    457   1.1      sato 	en = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTYEN);
    458   1.1      sato 	freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
    459   1.1      sato 	duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
    460   1.1      sato 
    461  1.13       uch 	printf("vrc4172pwm: dumpreg: lightenable = %d,"
    462  1.13       uch 	    " freq = 0x%x, duty = 0x%x\n", en, freq, duty);
    463   1.1      sato }
    464   1.2      sato 
    465   1.1      sato /* end */
    466