1 /* $NetBSD: fdt_panel.c,v 1.6 2021/12/19 11:01:10 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jonathan A. Kollasch <jakllsch@kollasch.net>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_panel.c,v 1.6 2021/12/19 11:01:10 riastradh Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/gpio.h>
36 #include <sys/systm.h>
37
38 #include <dev/fdt/fdt_port.h>
39 #include <dev/fdt/fdtvar.h>
40
41 #include <dev/i2c/ddcvar.h>
42
43 #include <drm/drm_drv.h>
44 #include <drm/drm_edid.h>
45 #include <drm/drm_panel.h>
46
47 static const struct device_compatible_entry compat_data[] = {
48 { .compat = "simple-panel" },
49 { .compat = "boe,nv140fhmn49" },
50 DEVICE_COMPAT_EOL
51 };
52
53 struct panel_fdt_softc {
54 device_t sc_dev;
55 struct fdt_device_ports sc_ports;
56 struct drm_panel sc_panel;
57 struct fdtbus_gpio_pin * sc_enable;
58 struct fdtbus_regulator * sc_regulator;
59 };
60
61 #define to_panel_fdt(x) container_of(x, struct panel_fdt_softc, sc_panel)
62
63 static int
panel_fdt_enable(struct drm_panel * panel)64 panel_fdt_enable(struct drm_panel * panel)
65 {
66 struct panel_fdt_softc * const sc = to_panel_fdt(panel);
67
68 if (sc->sc_enable) {
69 fdtbus_gpio_write(sc->sc_enable, true);
70 }
71
72 if (!cold)
73 kpause("panele", false, mstohz(200), NULL);
74
75 return 0;
76 }
77
78 static int
panel_fdt_prepare(struct drm_panel * panel)79 panel_fdt_prepare(struct drm_panel * panel)
80 {
81 struct panel_fdt_softc * const sc = to_panel_fdt(panel);
82
83 if (sc->sc_regulator) {
84 fdtbus_regulator_enable(sc->sc_regulator);
85 }
86
87 if (cold)
88 delay(210000);
89 else
90 kpause("panelp", false, mstohz(210), NULL);
91
92 return 0;
93 }
94
95 static const struct drm_panel_funcs panel_fdt_funcs = {
96 .disable = NULL,
97 .enable = panel_fdt_enable,
98 .get_modes = NULL,
99 .get_timings = NULL,
100 .prepare = panel_fdt_prepare,
101 .unprepare = NULL,
102 };
103
104 static int
panel_fdt_ep_activate(device_t dev,struct fdt_endpoint * ep,bool activate)105 panel_fdt_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
106 {
107 struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
108
109 if (fdt_endpoint_port_index(ep) != 0)
110 return EINVAL;
111
112 if (fdt_endpoint_type(rep) != EP_DRM_ENCODER)
113 return EINVAL;
114
115 return 0;
116 }
117
118 static void *
panel_fdt_ep_get_data(device_t dev,struct fdt_endpoint * ep)119 panel_fdt_ep_get_data(device_t dev, struct fdt_endpoint *ep)
120 {
121 struct panel_fdt_softc * const sc = device_private(dev);
122
123 return &sc->sc_panel;
124 }
125
126 static int
panel_fdt_match(device_t parent,cfdata_t cf,void * aux)127 panel_fdt_match(device_t parent, cfdata_t cf, void *aux)
128 {
129 struct fdt_attach_args * const faa = aux;
130
131 return of_compatible_match(faa->faa_phandle, compat_data);
132 }
133
134 static void
panel_fdt_attach(device_t parent,device_t self,void * aux)135 panel_fdt_attach(device_t parent, device_t self, void *aux)
136 {
137 struct panel_fdt_softc * const sc = device_private(self);
138 struct fdt_attach_args * const faa = aux;
139 const int phandle = faa->faa_phandle;
140
141 aprint_naive("\n");
142 aprint_normal(": panel\n");
143
144 sc->sc_dev = self;
145
146 /* required for "simple-panel" */
147 sc->sc_regulator = fdtbus_regulator_acquire(phandle, "power-supply");
148 if (sc->sc_regulator == NULL) {
149 aprint_error_dev(self, "regulator not found\n");
150 return;
151 }
152
153 /* optional for "simple-panel" */
154 sc->sc_enable = fdtbus_gpio_acquire_index(phandle,
155 "enable-gpios", 0, GPIO_PIN_OUTPUT);
156
157 sc->sc_ports.dp_ep_activate = panel_fdt_ep_activate;
158 sc->sc_ports.dp_ep_get_data = panel_fdt_ep_get_data;
159 fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_PANEL);
160
161 drm_panel_init(&sc->sc_panel, self, &panel_fdt_funcs, DRM_MODE_CONNECTOR_DPI);
162 sc->sc_panel.funcs = &panel_fdt_funcs;
163
164 drm_panel_add(&sc->sc_panel);
165 }
166
167 CFATTACH_DECL_NEW(panel_fdt, sizeof(struct panel_fdt_softc),
168 panel_fdt_match, panel_fdt_attach, NULL, NULL);
169