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