Home | History | Annotate | Line # | Download | only in netwalker
netwalker_backlight.c revision 1.1.2.2
      1  1.1.2.2  rmind /*	$NetBSD: netwalker_backlight.c,v 1.1.2.2 2014/05/18 17:45:05 rmind Exp $	*/
      2  1.1.2.2  rmind 
      3  1.1.2.2  rmind /*
      4  1.1.2.2  rmind  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
      5  1.1.2.2  rmind  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  1.1.2.2  rmind  *
      7  1.1.2.2  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  rmind  * modification, are permitted provided that the following conditions
      9  1.1.2.2  rmind  * are met:
     10  1.1.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  rmind  *
     16  1.1.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     17  1.1.2.2  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     20  1.1.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  rmind  */
     28  1.1.2.2  rmind 
     29  1.1.2.2  rmind #include <sys/cdefs.h>
     30  1.1.2.2  rmind __KERNEL_RCSID(0, "$NetBSD: netwalker_backlight.c,v 1.1.2.2 2014/05/18 17:45:05 rmind Exp $");
     31  1.1.2.2  rmind 
     32  1.1.2.2  rmind #include <sys/param.h>
     33  1.1.2.2  rmind #include <sys/bus.h>
     34  1.1.2.2  rmind #include <sys/systm.h>
     35  1.1.2.2  rmind #include <sys/device.h>
     36  1.1.2.2  rmind #include <sys/pmf.h>
     37  1.1.2.2  rmind 
     38  1.1.2.2  rmind #include <dev/wscons/wsconsio.h>
     39  1.1.2.2  rmind #include <dev/wscons/wsdisplayvar.h>
     40  1.1.2.2  rmind 
     41  1.1.2.2  rmind #include <dev/sysmon/sysmonvar.h>
     42  1.1.2.2  rmind #include <dev/sysmon/sysmon_taskq.h>
     43  1.1.2.2  rmind 
     44  1.1.2.2  rmind #include <arm/imx/imx51reg.h>
     45  1.1.2.2  rmind #include <arm/imx/imx51var.h>
     46  1.1.2.2  rmind #include <arm/imx/imx51_ccmvar.h>
     47  1.1.2.2  rmind #include <arm/imx/imxpwmvar.h>
     48  1.1.2.2  rmind 
     49  1.1.2.2  rmind #include <evbarm/netwalker/netwalker_backlightvar.h>
     50  1.1.2.2  rmind 
     51  1.1.2.2  rmind #define BRIGHTNESS_MAX	16
     52  1.1.2.2  rmind 
     53  1.1.2.2  rmind struct netwalker_backlight_softc {
     54  1.1.2.2  rmind 	struct imxpwm_softc sc_imxpwm;
     55  1.1.2.2  rmind 
     56  1.1.2.2  rmind 	int sc_brightness;
     57  1.1.2.2  rmind 	bool sc_islit;
     58  1.1.2.2  rmind };
     59  1.1.2.2  rmind 
     60  1.1.2.2  rmind static struct netwalker_backlight_softc *netwalker_backlight_sc;
     61  1.1.2.2  rmind 
     62  1.1.2.2  rmind static int netwalker_backlight_match(device_t, cfdata_t, void *);
     63  1.1.2.2  rmind static void netwalker_backlight_attach(device_t, device_t, void *);
     64  1.1.2.2  rmind static int netwalker_backlight_detach(device_t, int);
     65  1.1.2.2  rmind 
     66  1.1.2.2  rmind CFATTACH_DECL_NEW(netwalker_backlight, sizeof(struct netwalker_backlight_softc),
     67  1.1.2.2  rmind     netwalker_backlight_match, netwalker_backlight_attach, netwalker_backlight_detach, NULL);
     68  1.1.2.2  rmind 
     69  1.1.2.2  rmind static bool netwalker_backlight_resume(device_t, const pmf_qual_t *);
     70  1.1.2.2  rmind static bool netwalker_backlight_suspend(device_t, const pmf_qual_t *);
     71  1.1.2.2  rmind static void netwalker_backlight_on(device_t);
     72  1.1.2.2  rmind static void netwalker_backlight_off(device_t);
     73  1.1.2.2  rmind static void netwalker_brightness_up(device_t);
     74  1.1.2.2  rmind static void netwalker_brightness_down(device_t);
     75  1.1.2.2  rmind static void netwalker_set_brightness(struct netwalker_backlight_softc *, int);
     76  1.1.2.2  rmind 
     77  1.1.2.2  rmind static int
     78  1.1.2.2  rmind netwalker_backlight_match(device_t parent, cfdata_t cf, void * aux)
     79  1.1.2.2  rmind {
     80  1.1.2.2  rmind 	return imxpwm_match(parent, cf, aux);
     81  1.1.2.2  rmind }
     82  1.1.2.2  rmind 
     83  1.1.2.2  rmind static void
     84  1.1.2.2  rmind netwalker_backlight_attach(device_t parent, device_t self, void *aux)
     85  1.1.2.2  rmind {
     86  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(self);
     87  1.1.2.2  rmind 	struct imxpwm_softc *imxpwm = &sc->sc_imxpwm;
     88  1.1.2.2  rmind 
     89  1.1.2.2  rmind 	imxpwm->sc_dev = self;
     90  1.1.2.2  rmind 	imxpwm->sc_hz = 1000; /* 1000 Hz */
     91  1.1.2.2  rmind 	imxpwm->sc_handler = NULL;
     92  1.1.2.2  rmind 	imxpwm->sc_cookie = sc;
     93  1.1.2.2  rmind 	imxpwm_attach(imxpwm, aux);
     94  1.1.2.2  rmind 
     95  1.1.2.2  rmind 	aprint_normal(": LCD BackLight Control\n");
     96  1.1.2.2  rmind 	aprint_naive(": LCD BackLight Control\n");
     97  1.1.2.2  rmind 
     98  1.1.2.2  rmind 	netwalker_backlight_sc = sc;
     99  1.1.2.2  rmind 
    100  1.1.2.2  rmind 	/* BackLight 100% On */
    101  1.1.2.2  rmind 	sc->sc_brightness = BRIGHTNESS_MAX;
    102  1.1.2.2  rmind 	sc->sc_islit = true;
    103  1.1.2.2  rmind 	imxpwm_set_pwm(imxpwm, 1000);
    104  1.1.2.2  rmind 
    105  1.1.2.2  rmind 	if (!pmf_device_register(self, netwalker_backlight_suspend,
    106  1.1.2.2  rmind 		netwalker_backlight_resume))
    107  1.1.2.2  rmind 		aprint_error_dev(self,
    108  1.1.2.2  rmind 		    "couldn't establish backlight handler\n");
    109  1.1.2.2  rmind 
    110  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    111  1.1.2.2  rmind 		netwalker_brightness_up, true))
    112  1.1.2.2  rmind 		aprint_error_dev(self,
    113  1.1.2.2  rmind 		    "couldn't register BRIGHTNESS UP event handler\n");
    114  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    115  1.1.2.2  rmind 		netwalker_brightness_down, true))
    116  1.1.2.2  rmind 		aprint_error_dev(self,
    117  1.1.2.2  rmind 		    "couldn't register BRIGHTNESS DOWN event handler\n");
    118  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_DISPLAY_ON,
    119  1.1.2.2  rmind 		netwalker_backlight_on, true))
    120  1.1.2.2  rmind 		aprint_error_dev(self,
    121  1.1.2.2  rmind 		    "couldn't register DISPLAY ON event handler\n");
    122  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_DISPLAY_OFF,
    123  1.1.2.2  rmind 		netwalker_backlight_off, true))
    124  1.1.2.2  rmind 		aprint_error_dev(self,
    125  1.1.2.2  rmind 		    "couldn't register DISPLAY OFF event handler\n");
    126  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_CHASSIS_LID_OPEN,
    127  1.1.2.2  rmind 		netwalker_backlight_on, true))
    128  1.1.2.2  rmind 		aprint_error_dev(self,
    129  1.1.2.2  rmind 		    "couldn't register LID OPEN event handler\n");
    130  1.1.2.2  rmind 	if (!pmf_event_register(self, PMFE_CHASSIS_LID_CLOSE,
    131  1.1.2.2  rmind 		netwalker_backlight_off, true))
    132  1.1.2.2  rmind 		aprint_error_dev(self,
    133  1.1.2.2  rmind 		    "couldn't register LID CLOSE event handler\n");
    134  1.1.2.2  rmind }
    135  1.1.2.2  rmind 
    136  1.1.2.2  rmind static int
    137  1.1.2.2  rmind netwalker_backlight_detach(device_t self, int flags)
    138  1.1.2.2  rmind {
    139  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(self);
    140  1.1.2.2  rmind 	struct imxpwm_softc *imxpwm = &sc->sc_imxpwm;
    141  1.1.2.2  rmind 
    142  1.1.2.2  rmind 	imxpwm_set_pwm(imxpwm, 0);
    143  1.1.2.2  rmind 	pmf_device_deregister(self);
    144  1.1.2.2  rmind 	return 0;
    145  1.1.2.2  rmind }
    146  1.1.2.2  rmind 
    147  1.1.2.2  rmind /*
    148  1.1.2.2  rmind  * Power management
    149  1.1.2.2  rmind  */
    150  1.1.2.2  rmind static bool
    151  1.1.2.2  rmind netwalker_backlight_suspend(device_t dv, const pmf_qual_t *qual)
    152  1.1.2.2  rmind {
    153  1.1.2.2  rmind 	netwalker_backlight_off(dv);
    154  1.1.2.2  rmind  	return true;
    155  1.1.2.2  rmind }
    156  1.1.2.2  rmind 
    157  1.1.2.2  rmind static bool
    158  1.1.2.2  rmind netwalker_backlight_resume(device_t dv, const pmf_qual_t *qual)
    159  1.1.2.2  rmind {
    160  1.1.2.2  rmind 	netwalker_backlight_on(dv);
    161  1.1.2.2  rmind 	return true;
    162  1.1.2.2  rmind }
    163  1.1.2.2  rmind 
    164  1.1.2.2  rmind static void
    165  1.1.2.2  rmind netwalker_backlight_on(device_t dv)
    166  1.1.2.2  rmind {
    167  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(dv);
    168  1.1.2.2  rmind 	sc->sc_islit = true;
    169  1.1.2.2  rmind 	netwalker_set_brightness(sc, sc->sc_brightness);
    170  1.1.2.2  rmind }
    171  1.1.2.2  rmind 
    172  1.1.2.2  rmind static void
    173  1.1.2.2  rmind netwalker_backlight_off(device_t dv)
    174  1.1.2.2  rmind {
    175  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(dv);
    176  1.1.2.2  rmind 	sc->sc_islit = false;
    177  1.1.2.2  rmind 	netwalker_set_brightness(sc, sc->sc_brightness);
    178  1.1.2.2  rmind }
    179  1.1.2.2  rmind 
    180  1.1.2.2  rmind static void
    181  1.1.2.2  rmind netwalker_brightness_up(device_t dv)
    182  1.1.2.2  rmind {
    183  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(dv);
    184  1.1.2.2  rmind 	netwalker_set_brightness(sc, sc->sc_brightness + 1);
    185  1.1.2.2  rmind }
    186  1.1.2.2  rmind 
    187  1.1.2.2  rmind static void
    188  1.1.2.2  rmind netwalker_brightness_down(device_t dv)
    189  1.1.2.2  rmind {
    190  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = device_private(dv);
    191  1.1.2.2  rmind 	netwalker_set_brightness(sc, sc->sc_brightness - 1);
    192  1.1.2.2  rmind }
    193  1.1.2.2  rmind 
    194  1.1.2.2  rmind static void
    195  1.1.2.2  rmind netwalker_set_brightness(struct netwalker_backlight_softc *sc, int newval)
    196  1.1.2.2  rmind {
    197  1.1.2.2  rmind 	struct imxpwm_softc *imxpwm = &sc->sc_imxpwm;
    198  1.1.2.2  rmind 
    199  1.1.2.2  rmind 	if (newval < 0)
    200  1.1.2.2  rmind 		newval = 0;
    201  1.1.2.2  rmind 	else if (newval > BRIGHTNESS_MAX)
    202  1.1.2.2  rmind 		newval = BRIGHTNESS_MAX;
    203  1.1.2.2  rmind 	sc->sc_brightness = newval;
    204  1.1.2.2  rmind 
    205  1.1.2.2  rmind 	if (sc->sc_islit)
    206  1.1.2.2  rmind 		imxpwm_set_pwm(imxpwm, 1000 * sc->sc_brightness / BRIGHTNESS_MAX);
    207  1.1.2.2  rmind 	else
    208  1.1.2.2  rmind 		imxpwm_set_pwm(imxpwm, 0);
    209  1.1.2.2  rmind }
    210  1.1.2.2  rmind 
    211  1.1.2.2  rmind int
    212  1.1.2.2  rmind netwalker_lcd_param_ioctl(u_long cmd, struct wsdisplay_param *dp)
    213  1.1.2.2  rmind {
    214  1.1.2.2  rmind 	struct netwalker_backlight_softc *sc = netwalker_backlight_sc;
    215  1.1.2.2  rmind 	int rv = EINVAL;
    216  1.1.2.2  rmind 
    217  1.1.2.2  rmind 	switch (dp->param) {
    218  1.1.2.2  rmind 	case WSDISPLAYIO_PARAM_BACKLIGHT:
    219  1.1.2.2  rmind 		if (cmd == WSDISPLAYIO_GETPARAM) {
    220  1.1.2.2  rmind 			dp->min = 0;
    221  1.1.2.2  rmind 			dp->max = 1;
    222  1.1.2.2  rmind 			dp->curval = sc->sc_islit ? 1 : 0;
    223  1.1.2.2  rmind 			rv = 0;
    224  1.1.2.2  rmind 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
    225  1.1.2.2  rmind 			if (dp->curval != 0)
    226  1.1.2.2  rmind 				netwalker_backlight_on(sc->sc_imxpwm.sc_dev);
    227  1.1.2.2  rmind 			else
    228  1.1.2.2  rmind 				netwalker_backlight_off(sc->sc_imxpwm.sc_dev);
    229  1.1.2.2  rmind 			rv = 0;
    230  1.1.2.2  rmind 		}
    231  1.1.2.2  rmind 		break;
    232  1.1.2.2  rmind 
    233  1.1.2.2  rmind 	case WSDISPLAYIO_PARAM_CONTRAST:
    234  1.1.2.2  rmind 		/* unsupported */
    235  1.1.2.2  rmind 		rv = ENOTSUP;
    236  1.1.2.2  rmind 		break;
    237  1.1.2.2  rmind 
    238  1.1.2.2  rmind 	case WSDISPLAYIO_PARAM_BRIGHTNESS:
    239  1.1.2.2  rmind 		if (cmd == WSDISPLAYIO_GETPARAM) {
    240  1.1.2.2  rmind 			dp->min = 0;
    241  1.1.2.2  rmind 			dp->max = BRIGHTNESS_MAX;
    242  1.1.2.2  rmind 			dp->curval = sc->sc_brightness;
    243  1.1.2.2  rmind 			rv = 0;
    244  1.1.2.2  rmind 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
    245  1.1.2.2  rmind 			netwalker_set_brightness(sc, dp->curval);
    246  1.1.2.2  rmind 			rv = 0;
    247  1.1.2.2  rmind 		}
    248  1.1.2.2  rmind 		break;
    249  1.1.2.2  rmind 	}
    250  1.1.2.2  rmind 
    251  1.1.2.2  rmind 	return rv;
    252  1.1.2.2  rmind }
    253