xref: /netbsd/sys/dev/fdt/fdt_panel.c (revision 34cf509c)
1 /* $NetBSD: fdt_panel.c,v 1.1 2019/12/19 00:35:01 jakllsch 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.1 2019/12/19 00:35:01 jakllsch Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/gpio.h>
37 
38 #include <dev/fdt/fdtvar.h>
39 #include <dev/fdt/fdt_port.h>
40 
41 #include <dev/i2c/ddcvar.h>
42 
43 #include <drm/drmP.h>
44 #include <drm/drm_panel.h>
45 #include <drm/drm_edid.h>
46 
47 static const char * const compatible[] = {
48 	"simple-panel",
49 	NULL
50 };
51 
52 struct panel_fdt_softc {
53 	device_t			sc_dev;
54 	struct fdt_device_ports		sc_ports;
55 	struct drm_panel		sc_panel;
56 	struct fdtbus_gpio_pin *	sc_enable;
57 	struct fdtbus_regulator *	sc_regulator;
58 };
59 
60 #define	to_panel_fdt(x)	container_of(x, struct panel_fdt_softc, sc_panel)
61 
62 static int
63 panel_fdt_enable(struct drm_panel * panel)
64 {
65 	struct panel_fdt_softc * const sc = to_panel_fdt(panel);
66 
67         if (sc->sc_enable) {
68 		fdtbus_gpio_write(sc->sc_enable, true);
69         }
70 
71 	if (!cold)
72 		kpause("panele", false, mstohz(200), NULL);
73 
74 	return 0;
75 }
76 
77 static int
78 panel_fdt_prepare(struct drm_panel * panel)
79 {
80 	struct panel_fdt_softc * const sc = to_panel_fdt(panel);
81 
82         if (sc->sc_regulator) {
83                 fdtbus_regulator_enable(sc->sc_regulator);
84         }
85 
86 	if (cold)
87 		delay(210000);
88 	else
89 		kpause("panelp", false, mstohz(210), NULL);
90 
91 	return 0;
92 }
93 
94 static const struct drm_panel_funcs panel_fdt_funcs = {
95 	.disable = NULL,
96 	.enable = panel_fdt_enable,
97 	.get_modes = NULL,
98 	.get_timings = NULL,
99 	.prepare = panel_fdt_prepare,
100 	.unprepare = NULL,
101 };
102 
103 static int
104 panel_fdt_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
105 {
106 	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
107 
108 	if (fdt_endpoint_port_index(ep) != 0)
109 		return EINVAL;
110 
111 	if (fdt_endpoint_type(rep) != EP_DRM_ENCODER)
112 		return EINVAL;
113 
114 	return 0;
115 }
116 
117 static void *
118 panel_fdt_ep_get_data(device_t dev, struct fdt_endpoint *ep)
119 {
120 	struct panel_fdt_softc * const sc = device_private(dev);
121 
122 	return &sc->sc_panel;
123 }
124 
125 static int
126 panel_fdt_match(device_t parent, cfdata_t cf, void *aux)
127 {
128 	struct fdt_attach_args * const faa = aux;
129 
130 	return of_match_compatible(faa->faa_phandle, compatible);
131 }
132 
133 static void
134 panel_fdt_attach(device_t parent, device_t self, void *aux)
135 {
136 	struct panel_fdt_softc * const sc = device_private(self);
137 	struct fdt_attach_args * const faa = aux;
138 	const int phandle = faa->faa_phandle;
139 
140 	aprint_naive("\n");
141 	aprint_normal(": panel\n");
142 
143 	sc->sc_dev = self;
144 
145 	/* required for "simple-panel" */
146         sc->sc_regulator = fdtbus_regulator_acquire(phandle, "power-supply");
147 
148 	/* optional for "simple-panel" */
149 	sc->sc_enable = fdtbus_gpio_acquire_index(phandle,
150 	    "enable-gpios", 0, GPIO_PIN_OUTPUT);
151 
152 	sc->sc_ports.dp_ep_activate = panel_fdt_ep_activate;
153 	sc->sc_ports.dp_ep_get_data = panel_fdt_ep_get_data;
154 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_PANEL);
155 
156 	drm_panel_init(&sc->sc_panel);
157 	sc->sc_panel.funcs = &panel_fdt_funcs;
158 
159 	drm_panel_add(&sc->sc_panel);
160 }
161 
162 CFATTACH_DECL_NEW(panel_fdt, sizeof(struct panel_fdt_softc),
163 	panel_fdt_match, panel_fdt_attach, NULL, NULL);
164