1 /* $NetBSD: panel_fdt.c,v 1.6 2021/01/27 03:10:21 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.6 2021/01/27 03:10:21 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 DEVICE_COMPAT_EOL
75 };
76
77 static int
fdt_panel_match(device_t parent,cfdata_t cf,void * aux)78 fdt_panel_match(device_t parent, cfdata_t cf, void *aux)
79 {
80 const struct fdt_attach_args *faa = aux;
81
82 return of_compatible_match(faa->faa_phandle, compat_data);
83 }
84
85 static void
fdt_panel_attach(device_t parent,device_t self,void * aux)86 fdt_panel_attach(device_t parent, device_t self, void *aux)
87 {
88 struct fdt_panel_softc *sc = device_private(self);
89 const struct fdt_attach_args * const faa = aux;
90 const int phandle = faa->faa_phandle;
91 const struct display_timing * const timing =
92 &sc->sc_panel.panel_timing;
93 int child;
94 char buf[16];
95 const char *val;
96 int i;
97
98 sc->sc_dev = self;
99 sc->sc_phandle = phandle;
100 sc->sc_panel.panel_type =
101 of_compatible_lookup(phandle, compat_data)->value;
102
103 if (of_getprop_uint32(phandle, "width-mm", &sc->sc_panel.panel_width) ||
104 of_getprop_uint32(phandle, "height-mm", &sc->sc_panel.panel_height)){
105 aprint_error(": missing width-mm or height-mm properties\n");
106 return;
107 }
108 for (child = OF_child(phandle); child; child = OF_peer(child)) {
109 if (OF_getprop(child, "name", buf, sizeof(buf)) <= 0)
110 continue;
111 if (strcmp(buf, "panel-timing") != 0)
112 continue;
113
114 if (display_timing_parse(child,
115 &sc->sc_panel.panel_timing) != 0) {
116 aprint_error(": failed to parse panel-timing\n");
117 return;
118 }
119 }
120 if (sc->sc_panel.panel_timing.clock_freq == 0) {
121 aprint_error(": missing panel-timing\n");
122 return;
123 }
124 switch(sc->sc_panel.panel_type) {
125 case PANEL_LVDS:
126 case PANEL_DUAL_LVDS:
127 val = fdtbus_get_string(phandle, "data-mapping");
128 if (val == NULL) {
129 aprint_error(": missing data-mapping\n");
130 return;
131 }
132 if (strcmp(val, "jeida-18") == 0)
133 sc->sc_panel.panel_lvds_format = LVDS_JEIDA_18;
134 else if (strcmp(val, "jeida-24") == 0)
135 sc->sc_panel.panel_lvds_format = LVDS_JEIDA_24;
136 else if (strcmp(val, "vesa-24") == 0)
137 sc->sc_panel.panel_lvds_format = LVDS_VESA_24;
138 else {
139 aprint_error(": unknown data-mapping \"%s\"\n", val);
140 return;
141 }
142 break;
143 default:
144 panic("unknown panel type %d", sc->sc_panel.panel_type);
145 }
146
147 aprint_naive("\n");
148 aprint_normal(": %dx%d", timing->hactive, timing->vactive);
149 switch(sc->sc_panel.panel_type) {
150 case PANEL_LVDS:
151 aprint_normal(" LVDS");
152 break;
153 case PANEL_DUAL_LVDS:
154 aprint_normal(" dual-link LVDS");
155 break;
156 default:
157 panic(" unknown panel type %d", sc->sc_panel.panel_type);
158 }
159 aprint_normal(" panel\n");
160
161 for (i = 0; i < MAX_GPIO_ENABLES ; i++) {
162 sc->sc_gpios_enable[i] = fdtbus_gpio_acquire_index(phandle,
163 "enable-gpios", i, GPIO_PIN_OUTPUT);
164 if (sc->sc_gpios_enable[i] == NULL)
165 break;
166 }
167
168 aprint_verbose_dev(self, "%d enable GPIO%c\n", i, i > 1 ? 's' : ' ');
169
170 sc->sc_ports.dp_ep_get_data = fdt_panel_get_data;
171 sc->sc_ports.dp_ep_enable = fdt_panel_enable;
172 fdt_ports_register(&sc->sc_ports, self, phandle, EP_PANEL);
173 }
174
175 static void *
fdt_panel_get_data(device_t dev,struct fdt_endpoint * ep)176 fdt_panel_get_data(device_t dev, struct fdt_endpoint *ep)
177 {
178 struct fdt_panel_softc *sc = device_private(dev);
179 return &sc->sc_panel;
180 }
181
182 static int
fdt_panel_enable(device_t dev,struct fdt_endpoint * ep,bool enable)183 fdt_panel_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
184 {
185 struct fdt_panel_softc *sc = device_private(dev);
186 for (int i = 0; i < MAX_GPIO_ENABLES ; i++) {
187 if (sc->sc_gpios_enable[i] == NULL)
188 break;
189 fdtbus_gpio_write(sc->sc_gpios_enable[i], enable);
190 }
191 return 0;
192 }
193