Home | History | Annotate | Line # | Download | only in vr
vrc4172pwm.c revision 1.2
      1  1.2  sato /*	$NetBSD: vrc4172pwm.c,v 1.2 2000/11/11 10:08:12 sato Exp $	*/
      2  1.1  sato 
      3  1.1  sato /*
      4  1.1  sato  * Copyright (c) 2000 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.1  sato 
     28  1.1  sato #include <sys/param.h>
     29  1.1  sato #include <sys/systm.h>
     30  1.1  sato #include <sys/device.h>
     31  1.1  sato #include <sys/reboot.h>
     32  1.1  sato 
     33  1.1  sato #include <machine/bus.h>
     34  1.1  sato #include <machine/config_hook.h>
     35  1.1  sato #include <machine/platid.h>
     36  1.1  sato 
     37  1.1  sato #include <hpcmips/vr/vrc4172pwmvar.h>
     38  1.1  sato #include <hpcmips/vr/vrc4172pwmreg.h>
     39  1.1  sato 
     40  1.1  sato 
     41  1.1  sato #ifdef VRC2PWMDEBUG
     42  1.1  sato #ifndef VRC2PWMDEBUG_CONF
     43  1.1  sato #define VRC2PWMDEBUG_CONF 0
     44  1.1  sato #endif /* VRC2PWMDEBUG_CONF */
     45  1.1  sato int vrc4172pwmdebug = VRC2PWMDEBUG_CONF;
     46  1.1  sato #define DPRINTF(arg) if (vrc4172pwmdebug) printf arg;
     47  1.1  sato #define VPRINTF(arg) if (bootverbose||vrc4172pwmdebug) printf arg;
     48  1.1  sato #define VDUMPREG(arg) if (bootverbose||vrc4172pwmdebug) vrc4172_dumpreg arg;
     49  1.1  sato #else /* VRC2PWMDEBUG */
     50  1.1  sato #define DPRINTF(arg)
     51  1.1  sato #define VPRINTF(arg) if (bootverbose) printf arg;
     52  1.1  sato #define VDUMPREG(arg) if (bootverbose) vrc4172_dumpreg arg;
     53  1.1  sato #endif /* VRC2PWMDEBUG */
     54  1.1  sato 
     55  1.1  sato static int vrc4172pwmmatch __P((struct device *, struct cfdata *, void *));
     56  1.1  sato static void vrc4172pwmattach __P((struct device *, struct device *, void *));
     57  1.1  sato 
     58  1.1  sato static void vrc4172pwm_write __P((struct vrc4172pwm_softc *, int, unsigned short));
     59  1.1  sato static unsigned short vrc4172pwm_read __P((struct vrc4172pwm_softc *, int));
     60  1.1  sato 
     61  1.1  sato static int vrc4172pwm_event __P((void *, int, long, void *));
     62  1.1  sato static int vrc4172pwm_pmevent __P((void *, int, long, void *));
     63  1.1  sato 
     64  1.1  sato static void vrc4172pwm_dumpreg __P((struct vrc4172pwm_softc *));
     65  1.2  sato static void vrc4172pwm_init_brightness __P((struct vr4172pwm_softc *));
     66  1.2  sato void vrc4172pwm_light __P((struct vr4172pwm_softc *, int));
     67  1.2  sato int vrc4172pwm_get_brightness __P((struct vr4172pwm_softc *));
     68  1.2  sato void vrc4172pwm_set_brightness __P((struct vr4172pwm_softc *), int);
     69  1.1  sato 
     70  1.1  sato 
     71  1.1  sato struct cfattach vrc4172pwm_ca = {
     72  1.1  sato 	sizeof(struct vrc4172pwm_softc), vrc4172pwmmatch, vrc4172pwmattach
     73  1.1  sato };
     74  1.1  sato 
     75  1.1  sato struct vrc4172pwm_softc *this_pwm;
     76  1.1  sato 
     77  1.1  sato static inline void
     78  1.1  sato vrc4172pwm_write(sc, port, val)
     79  1.1  sato 	struct vrc4172pwm_softc *sc;
     80  1.1  sato 	int port;
     81  1.1  sato 	unsigned short val;
     82  1.1  sato {
     83  1.1  sato 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
     84  1.1  sato }
     85  1.1  sato 
     86  1.1  sato static inline unsigned short
     87  1.1  sato vrc4172pwm_read(sc, port)
     88  1.1  sato 	struct vrc4172pwm_softc *sc;
     89  1.1  sato 	int port;
     90  1.1  sato {
     91  1.1  sato 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
     92  1.1  sato }
     93  1.1  sato 
     94  1.1  sato static int
     95  1.1  sato vrc4172pwmmatch(parent, cf, aux)
     96  1.1  sato 	struct device *parent;
     97  1.1  sato 	struct cfdata *cf;
     98  1.1  sato 	void *aux;
     99  1.1  sato {
    100  1.1  sato 	return 1;
    101  1.1  sato }
    102  1.1  sato 
    103  1.1  sato static void
    104  1.1  sato vrc4172pwmattach(parent, self, aux)
    105  1.1  sato 	struct device *parent;
    106  1.1  sato 	struct device *self;
    107  1.1  sato 	void *aux;
    108  1.1  sato {
    109  1.1  sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)self;
    110  1.1  sato 	struct vrip_attach_args *va = aux;
    111  1.1  sato 
    112  1.1  sato 	bus_space_tag_t iot = va->va_iot;
    113  1.1  sato 	bus_space_handle_t ioh;
    114  1.1  sato 
    115  1.1  sato 	if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
    116  1.1  sato 		printf(": can't map bus space\n");
    117  1.1  sato 		return;
    118  1.1  sato 	}
    119  1.1  sato 
    120  1.1  sato 	sc->sc_iot = iot;
    121  1.1  sato 	sc->sc_ioh = ioh;
    122  1.1  sato 
    123  1.1  sato 	printf("\n");
    124  1.1  sato 
    125  1.1  sato 	VDUMPREG(sc);
    126  1.1  sato 	/* basic setup */
    127  1.1  sato 	sc->sc_pmhook = config_hook(CONFIG_HOOK_PMEVENT,
    128  1.1  sato 					CONFIG_PMEVENT_HARDPOWER,
    129  1.1  sato 					CONFIG_HOOK_SHARE,
    130  1.1  sato 					vrc4172pwm_pmevent, sc);
    131  1.1  sato 	sc->sc_hook = config_hook(CONFIG_HOOK_POWERCONTROL,
    132  1.1  sato 					CONFIG_POWERCONTROL_LCDLIGHT,
    133  1.1  sato 					CONFIG_HOOK_SHARE,
    134  1.1  sato 					vrc4172pwm_event, sc);
    135  1.1  sato 	sc->sc_sethook = config_hook(CONFIG_HOOK_SET,
    136  1.1  sato 					CONFIG_SET_BRIGHTNESS,
    137  1.1  sato 					CONFIG_HOOK_SHARE,
    138  1.1  sato 					vrc4172pwm_event, sc);
    139  1.1  sato 	sc->sc_gethook = config_hook(CONFIG_HOOK_GET,
    140  1.1  sato 					CONFIG_GET_BRIGHTNESS,
    141  1.1  sato 					CONFIG_HOOK_SHARE,
    142  1.1  sato 					vrc4172pwm_event, sc);
    143  1.1  sato 
    144  1.2  sato 	vr4172pwm_init_brightness(sc);
    145  1.1  sato 	this_pwm = sc;
    146  1.1  sato }
    147  1.1  sato 
    148  1.2  sato /*
    149  1.2  sato  *
    150  1.2  sato  * Initialize PWM brightness parameters
    151  1.2  sato  *
    152  1.2  sato  */
    153  1.2  sato void
    154  1.2  sato vrc4172pwm_init_brightness(sc)
    155  1.2  sato {
    156  1.2  sato 	sc->sc_raw_freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
    157  1.2  sato 	sc->sc_raw_duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
    158  1.2  sato 	sc->sc_param = vrc4172pwm_getparam();
    159  1.2  sato 	sc->sc_brightness = vrc4172pwm_raw_duty2brightness(sc);
    160  1.2  sato }
    161  1.1  sato 
    162  1.1  sato /*
    163  1.1  sato  * PWM config hook events
    164  1.1  sato  *
    165  1.1  sato  */
    166  1.1  sato int
    167  1.1  sato vrc4172pwm_event(ctx, type, id, msg)
    168  1.1  sato 	void *ctx;
    169  1.1  sato         int type;
    170  1.1  sato         long id;
    171  1.1  sato         void *msg;
    172  1.1  sato {
    173  1.1  sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
    174  1.1  sato         int why =(int)msg;
    175  1.1  sato 
    176  1.1  sato 	if (type == CONFIG_HOOK_POWERCONTROL
    177  1.1  sato 		&& id == CONFIG_HOOK_POWERCONTROL_LCDLIGHT) {
    178  1.2  sato 		vrc4172pwm_light(sc, why);
    179  1.1  sato 	} else if (type == CONFIG_HOOK_GET
    180  1.2  sato 		&& id == CONFIG_HOOK_GET_BRIGHTNESS) {
    181  1.1  sato 		*(int *)msg = vr4172pwm_get_brightness(sc);
    182  1.1  sato 	} else if (type == CONFIG_HOOK_SET
    183  1.2  sato 		&& id == CONFIG_HOOK_GET_BRIGHTNESS) {
    184  1.1  sato 		vr4172pwm_set_brightness(sc, *(int *)msg);
    185  1.1  sato 	} else
    186  1.1  sato 		return 1;
    187  1.1  sato 
    188  1.1  sato         return (0);
    189  1.1  sato }
    190  1.1  sato 
    191  1.1  sato 
    192  1.1  sato /*
    193  1.1  sato  * PWM config hook events
    194  1.1  sato  *
    195  1.1  sato  */
    196  1.1  sato int
    197  1.1  sato vrc4172pwm_pmevent(ctx, type, id, msg)
    198  1.1  sato 	void *ctx;
    199  1.1  sato         int type;
    200  1.1  sato         long id;
    201  1.1  sato         void *msg;
    202  1.1  sato {
    203  1.1  sato 	struct vrc4172pwm_softc *sc = (struct vrc4172pwm_softc *)ctx;
    204  1.1  sato         int why =(int)msg;
    205  1.1  sato 
    206  1.1  sato 	if (type != CONFIG_HOOK_PMEVENT)
    207  1.1  sato 		return 1;
    208  1.1  sato 
    209  1.1  sato         switch (why) {
    210  1.2  sato 	case PWR_STANBY:
    211  1.2  sato 	case PWR_SUSPEND:
    212  1.2  sato 		vrc4172pwm_light(sc, 0);
    213  1.2  sato 		break;
    214  1.2  sato 	case PWR_RESUME:
    215  1.2  sato 		vrc4172pwm_light(sc, 1);
    216  1.2  sato 		break;
    217  1.2  sato 	default:
    218  1.2  sato 		return 1;
    219  1.1  sato         }
    220  1.2  sato 
    221  1.1  sato         return (0);
    222  1.1  sato }
    223  1.1  sato 
    224  1.1  sato /*
    225  1.2  sato  *
    226  1.2  sato  */
    227  1.2  sato void
    228  1.2  sato vrc4172pwm_light(sc, on)
    229  1.2  sato 	struct vrc4172pwm_softc *sc;
    230  1.2  sato 	int on;
    231  1.2  sato {
    232  1.2  sato 	if (on)
    233  1.2  sato 		vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCDEN);
    234  1.2  sato 	else
    235  1.2  sato 		vrc4172pwm_write(sc, VRC2_PWM_LCDDUTYEN, VRC2_PWM_LCDDIS);
    236  1.2  sato }
    237  1.2  sato 
    238  1.2  sato /*
    239  1.2  sato  * set brightness
    240  1.2  sato  */
    241  1.2  sato void
    242  1.2  sato vrc4172pwm_set_brightness(sc, val)
    243  1.2  sato 	struct vrc4172pwm_softc *sc;
    244  1.2  sato 	int val;
    245  1.2  sato {
    246  1.2  sato 	int raw;
    247  1.2  sato 
    248  1.2  sato 	if (val > VRC2_PWM_MAX_BRIGHTNESS)
    249  1.2  sato 		val = VRC2_PWM_MAX_BRIGHTNESS;
    250  1.2  sato 	if (val > sc->sc_param->max_brightness)
    251  1.2  sato 		val = sc->sc_param->max_brightness;
    252  1.2  sato 	raw = vrc4172pwm_brightness2rawduty(sc, val);
    253  1.2  sato 	vrc4172pwm_write(sc, VRC2_PWM_LCDDUTY, raw);
    254  1.2  sato 	sc->sc_brightness = val;
    255  1.2  sato }
    256  1.2  sato 
    257  1.2  sato /*
    258  1.2  sato  * get brightness
    259  1.2  sato  */
    260  1.2  sato int
    261  1.2  sato vrc4172pwm_set_brightness(sc)
    262  1.2  sato 	struct vrc4172pwm_softc *sc;
    263  1.2  sato {
    264  1.2  sato 	return sc->sc_brightness;
    265  1.2  sato }
    266  1.2  sato 
    267  1.2  sato /*
    268  1.1  sato  * dump pwm registers
    269  1.1  sato  */
    270  1.1  sato void
    271  1.1  sato vrc4172pwm_dumpreg(sc)
    272  1.1  sato 	struct vrc4172pwm_softc *sc;
    273  1.1  sato {
    274  1.1  sato 	int en, freq, duty;
    275  1.1  sato 
    276  1.1  sato 	en = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTYEN);
    277  1.1  sato 	freq = vrc4172pwm_read(sc, VRC2_PWM_LCDFREQ);
    278  1.1  sato 	duty = vrc4172pwm_read(sc, VRC2_PWM_LCDDUTY);
    279  1.1  sato 
    280  1.1  sato 	printf("vrc4172pwm: lightenable = %d, freq = 0x%x, duty = 0x%x\n",
    281  1.1  sato 		en, freq, duty);
    282  1.1  sato }
    283  1.2  sato 
    284  1.1  sato /* end */
    285