Home | History | Annotate | Line # | Download | only in fdt
      1  1.6  thorpej /*	$NetBSD: panel_fdt.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $	*/
      2  1.1   bouyer 
      3  1.1   bouyer /*-
      4  1.1   bouyer  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1   bouyer  * All rights reserved.
      6  1.1   bouyer  *
      7  1.1   bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   bouyer  * by Manuel Bouyer.
      9  1.1   bouyer  *
     10  1.1   bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.1   bouyer  * modification, are permitted provided that the following conditions
     12  1.1   bouyer  * are met:
     13  1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.1   bouyer  *
     19  1.1   bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   bouyer  */
     31  1.1   bouyer 
     32  1.1   bouyer /*
     33  1.1   bouyer  * lvds panel driver.
     34  1.1   bouyer  * specified in linux/Documentation/devicetree/bindings/display/panel/
     35  1.1   bouyer  * Simple RGB panels could be added as well
     36  1.1   bouyer  * registers an endpoint for use by graphic controller drivers
     37  1.1   bouyer  *
     38  1.1   bouyer  */
     39  1.1   bouyer 
     40  1.1   bouyer #include <sys/cdefs.h>
     41  1.1   bouyer 
     42  1.6  thorpej __KERNEL_RCSID(1, "$NetBSD: panel_fdt.c,v 1.6 2021/01/27 03:10:21 thorpej Exp $");
     43  1.1   bouyer 
     44  1.1   bouyer #include <sys/param.h>
     45  1.1   bouyer #include <sys/systm.h>
     46  1.1   bouyer #include <sys/device.h>
     47  1.1   bouyer #include <sys/bus.h>
     48  1.1   bouyer #include <sys/gpio.h>
     49  1.1   bouyer 
     50  1.1   bouyer #include <dev/fdt/fdtvar.h>
     51  1.1   bouyer #include <dev/fdt/panel_fdt.h>
     52  1.1   bouyer 
     53  1.1   bouyer static int fdt_panel_match(device_t, cfdata_t, void *);
     54  1.1   bouyer static void fdt_panel_attach(device_t, device_t, void *);
     55  1.1   bouyer static void *fdt_panel_get_data(device_t, struct fdt_endpoint *);
     56  1.1   bouyer static int fdt_panel_enable(device_t, struct fdt_endpoint *, bool);
     57  1.1   bouyer 
     58  1.1   bouyer struct fdt_panel_softc {
     59  1.1   bouyer 	device_t sc_dev;
     60  1.1   bouyer 	int sc_phandle;
     61  1.1   bouyer 	struct fdt_panel sc_panel;
     62  1.1   bouyer 	struct fdt_device_ports sc_ports;
     63  1.1   bouyer #define MAX_GPIO_ENABLES 8
     64  1.1   bouyer 	struct fdtbus_gpio_pin *sc_gpios_enable[MAX_GPIO_ENABLES];
     65  1.1   bouyer };
     66  1.1   bouyer 
     67  1.1   bouyer 
     68  1.1   bouyer CFATTACH_DECL_NEW(fdt_panel, sizeof(struct fdt_panel_softc),
     69  1.1   bouyer     fdt_panel_match, fdt_panel_attach, NULL, NULL);
     70  1.1   bouyer 
     71  1.3  thorpej static const struct device_compatible_entry compat_data[] = {
     72  1.3  thorpej 	{ .compat = "panel-lvds",	.value = PANEL_LVDS},
     73  1.3  thorpej 	{ .compat = "panel-dual-lvds",	.value = PANEL_DUAL_LVDS},
     74  1.5  thorpej 	DEVICE_COMPAT_EOL
     75  1.1   bouyer };
     76  1.1   bouyer 
     77  1.1   bouyer static int
     78  1.1   bouyer fdt_panel_match(device_t parent, cfdata_t cf, void *aux)
     79  1.1   bouyer {
     80  1.1   bouyer 	const struct fdt_attach_args *faa = aux;
     81  1.1   bouyer 
     82  1.6  thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     83  1.1   bouyer }
     84  1.1   bouyer 
     85  1.1   bouyer static void
     86  1.1   bouyer fdt_panel_attach(device_t parent, device_t self, void *aux)
     87  1.1   bouyer {
     88  1.1   bouyer 	struct fdt_panel_softc *sc = device_private(self);
     89  1.1   bouyer 	const struct fdt_attach_args * const faa = aux;
     90  1.1   bouyer 	const int phandle = faa->faa_phandle;
     91  1.1   bouyer 	const struct display_timing * const timing =
     92  1.1   bouyer 	    &sc->sc_panel.panel_timing;
     93  1.1   bouyer 	int child;
     94  1.1   bouyer 	char buf[16];
     95  1.1   bouyer 	const char *val;
     96  1.1   bouyer 	int i;
     97  1.1   bouyer 
     98  1.1   bouyer 	sc->sc_dev = self;
     99  1.1   bouyer 	sc->sc_phandle = phandle;
    100  1.1   bouyer 	sc->sc_panel.panel_type =
    101  1.6  thorpej 	    of_compatible_lookup(phandle, compat_data)->value;
    102  1.1   bouyer 
    103  1.1   bouyer 	if (of_getprop_uint32(phandle, "width-mm", &sc->sc_panel.panel_width) ||
    104  1.1   bouyer 	    of_getprop_uint32(phandle, "height-mm", &sc->sc_panel.panel_height)){
    105  1.1   bouyer 		aprint_error(": missing width-mm or height-mm properties\n");
    106  1.1   bouyer 		return;
    107  1.1   bouyer 	}
    108  1.1   bouyer 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    109  1.1   bouyer 		if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
    110  1.1   bouyer 			continue;
    111  1.1   bouyer 		if (strcmp(buf, "panel-timing") != 0)
    112  1.1   bouyer 		    continue;
    113  1.1   bouyer 
    114  1.1   bouyer 		if (display_timing_parse(child,
    115  1.1   bouyer 		    &sc->sc_panel.panel_timing) != 0) {
    116  1.1   bouyer 			aprint_error(": failed to parse panel-timing\n");
    117  1.1   bouyer 			return;
    118  1.1   bouyer 		}
    119  1.1   bouyer 	}
    120  1.1   bouyer 	if (sc->sc_panel.panel_timing.clock_freq == 0) {
    121  1.1   bouyer 		aprint_error(": missing panel-timing\n");
    122  1.1   bouyer 		return;
    123  1.1   bouyer 	}
    124  1.1   bouyer 	switch(sc->sc_panel.panel_type) {
    125  1.1   bouyer 	case PANEL_LVDS:
    126  1.1   bouyer 	case PANEL_DUAL_LVDS:
    127  1.1   bouyer 		val = fdtbus_get_string(phandle, "data-mapping");
    128  1.1   bouyer 		if (val == NULL) {
    129  1.1   bouyer 			aprint_error(": missing data-mapping\n");
    130  1.1   bouyer 			return;
    131  1.1   bouyer 		}
    132  1.1   bouyer 		if (strcmp(val, "jeida-18") == 0)
    133  1.1   bouyer 			sc->sc_panel.panel_lvds_format = LVDS_JEIDA_18;
    134  1.1   bouyer 		else if (strcmp(val, "jeida-24") == 0)
    135  1.1   bouyer 			sc->sc_panel.panel_lvds_format = LVDS_JEIDA_24;
    136  1.1   bouyer 		else if (strcmp(val, "vesa-24") == 0)
    137  1.1   bouyer 			sc->sc_panel.panel_lvds_format = LVDS_VESA_24;
    138  1.1   bouyer 		else {
    139  1.2    skrll 			aprint_error(": unknown data-mapping \"%s\"\n", val);
    140  1.1   bouyer 			return;
    141  1.1   bouyer 		}
    142  1.1   bouyer 		break;
    143  1.1   bouyer 	default:
    144  1.1   bouyer 		panic("unknown panel type %d", sc->sc_panel.panel_type);
    145  1.1   bouyer 	}
    146  1.1   bouyer 
    147  1.1   bouyer 	aprint_naive("\n");
    148  1.1   bouyer 	aprint_normal(": %dx%d", timing->hactive, timing->vactive);
    149  1.1   bouyer 	switch(sc->sc_panel.panel_type) {
    150  1.1   bouyer 	case PANEL_LVDS:
    151  1.1   bouyer 		aprint_normal(" LVDS");
    152  1.1   bouyer 		break;
    153  1.1   bouyer 	case PANEL_DUAL_LVDS:
    154  1.1   bouyer 		aprint_normal(" dual-link LVDS");
    155  1.1   bouyer 		break;
    156  1.1   bouyer 	default:
    157  1.1   bouyer 		panic(" unknown panel type %d", sc->sc_panel.panel_type);
    158  1.1   bouyer 	}
    159  1.1   bouyer 	aprint_normal(" panel\n");
    160  1.1   bouyer 
    161  1.1   bouyer 	for (i = 0; i < MAX_GPIO_ENABLES ; i++) {
    162  1.1   bouyer 		sc->sc_gpios_enable[i] = fdtbus_gpio_acquire_index(phandle,
    163  1.1   bouyer 		    "enable-gpios", i, GPIO_PIN_OUTPUT);
    164  1.1   bouyer 		if (sc->sc_gpios_enable[i] == NULL)
    165  1.1   bouyer 			break;
    166  1.1   bouyer 	}
    167  1.1   bouyer 
    168  1.1   bouyer 	aprint_verbose_dev(self, "%d enable GPIO%c\n", i, i > 1 ? 's' : ' ');
    169  1.1   bouyer 
    170  1.1   bouyer 	sc->sc_ports.dp_ep_get_data = fdt_panel_get_data;
    171  1.1   bouyer 	sc->sc_ports.dp_ep_enable = fdt_panel_enable;
    172  1.1   bouyer 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_PANEL);
    173  1.1   bouyer }
    174  1.1   bouyer 
    175  1.1   bouyer static void *
    176  1.1   bouyer fdt_panel_get_data(device_t dev, struct fdt_endpoint *ep)
    177  1.1   bouyer {
    178  1.1   bouyer 	struct fdt_panel_softc *sc = device_private(dev);
    179  1.1   bouyer 	return &sc->sc_panel;
    180  1.1   bouyer }
    181  1.1   bouyer 
    182  1.1   bouyer static int
    183  1.1   bouyer fdt_panel_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
    184  1.1   bouyer {
    185  1.1   bouyer 	struct fdt_panel_softc *sc = device_private(dev);
    186  1.1   bouyer 	for (int i = 0; i < MAX_GPIO_ENABLES ; i++) {
    187  1.1   bouyer 		if (sc->sc_gpios_enable[i] == NULL)
    188  1.1   bouyer 			break;
    189  1.1   bouyer 		fdtbus_gpio_write(sc->sc_gpios_enable[i], enable);
    190  1.1   bouyer 	}
    191  1.1   bouyer 	return 0;
    192  1.1   bouyer }
    193