11.5Sthorpej/*	$NetBSD: connector_fdt.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $	*/
21.1Sbouyer
31.1Sbouyer/*-
41.1Sbouyer * Copyright (c) 2018 The NetBSD Foundation, Inc.
51.1Sbouyer * All rights reserved.
61.1Sbouyer *
71.1Sbouyer * This code is derived from software contributed to The NetBSD Foundation
81.1Sbouyer * by Manuel Bouyer.
91.1Sbouyer *
101.1Sbouyer * Redistribution and use in source and binary forms, with or without
111.1Sbouyer * modification, are permitted provided that the following conditions
121.1Sbouyer * are met:
131.1Sbouyer * 1. Redistributions of source code must retain the above copyright
141.1Sbouyer *    notice, this list of conditions and the following disclaimer.
151.1Sbouyer * 2. Redistributions in binary form must reproduce the above copyright
161.1Sbouyer *    notice, this list of conditions and the following disclaimer in the
171.1Sbouyer *    documentation and/or other materials provided with the distribution.
181.1Sbouyer *
191.1Sbouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sbouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sbouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sbouyer * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sbouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sbouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sbouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sbouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sbouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sbouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sbouyer * POSSIBILITY OF SUCH DAMAGE.
301.1Sbouyer */
311.1Sbouyer
321.1Sbouyer/*
331.1Sbouyer * connector driver.
341.1Sbouyer * specified in linux/Documentation/devicetree/bindings/display/connector/
351.1Sbouyer * basically it only register its endpoint.
361.1Sbouyer */
371.1Sbouyer
381.1Sbouyer#include <sys/cdefs.h>
391.1Sbouyer
401.5Sthorpej__KERNEL_RCSID(1, "$NetBSD: connector_fdt.c,v 1.5 2021/01/27 03:10:21 thorpej Exp $");
411.1Sbouyer
421.1Sbouyer#include <sys/param.h>
431.1Sbouyer#include <sys/systm.h>
441.1Sbouyer#include <sys/device.h>
451.1Sbouyer#include <sys/bus.h>
461.1Sbouyer#include <sys/kmem.h>
471.1Sbouyer
481.1Sbouyer#include <dev/fdt/fdtvar.h>
491.1Sbouyer#include <dev/fdt/fdt_port.h>
501.1Sbouyer#include <dev/fdt/connector_fdt.h>
511.1Sbouyer
521.1Sbouyerstatic int fdt_connector_match(device_t, cfdata_t, void *);
531.1Sbouyerstatic void fdt_connector_attach(device_t, device_t, void *);
541.1Sbouyerstatic void *fdt_connector_get_data(device_t, struct fdt_endpoint *);
551.1Sbouyer
561.1SbouyerSLIST_HEAD(, fdt_connector_softc) fdt_connectors =
571.1Sbouyer    SLIST_HEAD_INITIALIZER(&fdt_connectors);
581.1Sbouyer
591.1Sbouyerstruct fdt_connector_softc {
601.1Sbouyer	device_t sc_dev;
611.1Sbouyer	int sc_phandle;
621.1Sbouyer	struct fdt_connector sc_con;
631.1Sbouyer	SLIST_ENTRY(fdt_connector_softc) sc_list;
641.1Sbouyer	struct fdt_device_ports sc_ports;
651.1Sbouyer};
661.1Sbouyer
671.1Sbouyer#define sc_type sc_con.con_type
681.1Sbouyer
691.1SbouyerCFATTACH_DECL_NEW(fdt_connector, sizeof(struct fdt_connector_softc),
701.1Sbouyer    fdt_connector_match, fdt_connector_attach, NULL, NULL);
711.1Sbouyer
721.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
731.2Sthorpej	{ .compat = "composite-video-connector",	.value = CON_TV},
741.2Sthorpej	{ .compat = "dvi-connector",			.value = CON_DVI},
751.2Sthorpej	{ .compat = "hdmi-connector",			.value = CON_HDMI},
761.2Sthorpej	{ .compat = "vga-connector",			.value = CON_VGA},
771.4Sthorpej	DEVICE_COMPAT_EOL
781.1Sbouyer};
791.1Sbouyer
801.1Sbouyerstatic int
811.1Sbouyerfdt_connector_match(device_t parent, cfdata_t cf, void *aux)
821.1Sbouyer{
831.1Sbouyer	const struct fdt_attach_args *faa = aux;
841.1Sbouyer
851.5Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
861.1Sbouyer}
871.1Sbouyer
881.1Sbouyerstatic void
891.1Sbouyerfdt_connector_attach(device_t parent, device_t self, void *aux)
901.1Sbouyer{
911.1Sbouyer	struct fdt_connector_softc *sc = device_private(self);
921.1Sbouyer	const struct fdt_attach_args * const faa = aux;
931.1Sbouyer	const int phandle = faa->faa_phandle;
941.1Sbouyer
951.1Sbouyer	sc->sc_dev = self;
961.1Sbouyer	sc->sc_phandle = phandle;
971.5Sthorpej	sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
981.1Sbouyer
991.1Sbouyer	SLIST_INSERT_HEAD(&fdt_connectors, sc, sc_list);
1001.1Sbouyer
1011.1Sbouyer	aprint_naive("\n");
1021.1Sbouyer	switch(sc->sc_type) {
1031.1Sbouyer	case CON_VGA:
1041.1Sbouyer		aprint_normal(": VGA");
1051.1Sbouyer		break;
1061.1Sbouyer	case CON_DVI:
1071.1Sbouyer		aprint_normal(": DVI");
1081.1Sbouyer		break;
1091.1Sbouyer	case CON_HDMI:
1101.1Sbouyer		aprint_normal(": HDMI");
1111.1Sbouyer		break;
1121.1Sbouyer	case CON_TV:
1131.1Sbouyer		aprint_normal(": composite");
1141.1Sbouyer		break;
1151.1Sbouyer	default:
1161.1Sbouyer		panic("unknown connector type %d\n", sc->sc_type);
1171.1Sbouyer	}
1181.1Sbouyer	aprint_normal(" connector\n");
1191.1Sbouyer	sc->sc_ports.dp_ep_get_data = fdt_connector_get_data;
1201.1Sbouyer	fdt_ports_register(&sc->sc_ports, self, phandle, EP_CONNECTOR);
1211.1Sbouyer}
1221.1Sbouyer
1231.1Sbouyerstatic void *
1241.1Sbouyerfdt_connector_get_data(device_t dev, struct fdt_endpoint *ep)
1251.1Sbouyer{
1261.1Sbouyer	struct fdt_connector_softc *sc = device_private(dev);
1271.1Sbouyer
1281.1Sbouyer	return &sc->sc_con;
1291.1Sbouyer}
130