1274823Sbrooks/*-
2274823Sbrooks * Copyright (c) 2014 Ed Maste
3274823Sbrooks * All rights reserved.
4274823Sbrooks *
5274823Sbrooks * This software was developed by SRI International and the University of
6274823Sbrooks * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7274823Sbrooks * ("CTSRD"), as part of the DARPA CRASH research programme.
8274823Sbrooks *
9274823Sbrooks * Redistribution and use in source and binary forms, with or without
10274823Sbrooks * modification, are permitted provided that the following conditions
11274823Sbrooks * are met:
12274823Sbrooks * 1. Redistributions of source code must retain the above copyright
13274823Sbrooks *    notice, this list of conditions and the following disclaimer.
14274823Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
15274823Sbrooks *    notice, this list of conditions and the following disclaimer in the
16274823Sbrooks *    documentation and/or other materials provided with the distribution.
17274823Sbrooks *
18274823Sbrooks * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19274823Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20274823Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21274823Sbrooks * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22274823Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23274823Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24274823Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25274823Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26274823Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27274823Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28274823Sbrooks * SUCH DAMAGE.
29274823Sbrooks */
30274823Sbrooks
31274823Sbrooks#include <sys/cdefs.h>
32274823Sbrooks__FBSDID("$FreeBSD$");
33274823Sbrooks
34274823Sbrooks#include <sys/param.h>
35274823Sbrooks#include <sys/bus.h>
36274823Sbrooks#include <sys/fbio.h>
37274823Sbrooks#include <sys/kernel.h>
38274823Sbrooks#include <sys/module.h>
39274823Sbrooks#include <sys/rman.h>
40274823Sbrooks
41274823Sbrooks#include <dev/fdt/fdt_common.h>
42274823Sbrooks#include <dev/ofw/ofw_bus.h>
43274823Sbrooks#include <dev/ofw/ofw_bus_subr.h>
44274823Sbrooks
45274823Sbrooks#include <dev/terasic/mtl/terasic_mtl.h>
46274823Sbrooks
47274823Sbrooks#include <dev/vt/colors/vt_termcolors.h>
48274823Sbrooks
49274823Sbrooks/*
50274823Sbrooks * Terasic Multitouch LCD (MTL) vt(4) framebuffer driver.
51274823Sbrooks */
52274823Sbrooks
53274823Sbrooks#include <vm/vm.h>
54274823Sbrooks#include <vm/pmap.h>
55274823Sbrooks
56274823Sbrooksstatic int
57274823Sbrooksterasic_mtl_fbd_panel_info(struct terasic_mtl_softc *sc, struct fb_info *info)
58274823Sbrooks{
59274823Sbrooks	phandle_t node;
60274823Sbrooks	pcell_t dts_value[2];
61274823Sbrooks	int len;
62274823Sbrooks
63274823Sbrooks	if ((node = ofw_bus_get_node(sc->mtl_dev)) == -1)
64274823Sbrooks		return (ENXIO);
65274823Sbrooks
66274823Sbrooks	/* panel size */
67274823Sbrooks	if ((len = OF_getproplen(node, "panel-size")) != sizeof(dts_value))
68274823Sbrooks		return (ENXIO);
69274823Sbrooks	OF_getencprop(node, "panel-size", dts_value, len);
70274823Sbrooks	info->fb_width = dts_value[0];
71274823Sbrooks	info->fb_height = dts_value[1];
72274823Sbrooks	info->fb_bpp = info->fb_depth = 32;
73274823Sbrooks	info->fb_stride = info->fb_width * (info->fb_depth / 8);
74274823Sbrooks
75274823Sbrooks	/*
76274823Sbrooks	 * Safety belt to ensure framebuffer params are as expected.  May be
77274823Sbrooks	 * removed when we have full confidence in fdt / hints params.
78274823Sbrooks	 */
79274823Sbrooks	if (info->fb_width != TERASIC_MTL_FB_WIDTH ||
80274823Sbrooks	    info->fb_height != TERASIC_MTL_FB_HEIGHT ||
81274823Sbrooks	    info->fb_stride != 3200 ||
82274823Sbrooks	    info->fb_bpp != 32 || info->fb_depth != 32) {
83274823Sbrooks		device_printf(sc->mtl_dev,
84274823Sbrooks		    "rejecting invalid panel params width=%u height=%u\n",
85274823Sbrooks		    (unsigned)info->fb_width, (unsigned)info->fb_height);
86274823Sbrooks		return (EINVAL);
87274823Sbrooks	}
88274823Sbrooks
89274823Sbrooks	return (0);
90274823Sbrooks}
91274823Sbrooks
92274823Sbrooksint
93274823Sbrooksterasic_mtl_fbd_attach(struct terasic_mtl_softc *sc)
94274823Sbrooks{
95274823Sbrooks	struct fb_info *info;
96274823Sbrooks	device_t fbd;
97274823Sbrooks
98274823Sbrooks	info = &sc->mtl_fb_info;
99274823Sbrooks	info->fb_name = device_get_nameunit(sc->mtl_dev);
100274823Sbrooks	info->fb_pbase = rman_get_start(sc->mtl_pixel_res);
101274823Sbrooks	info->fb_size = rman_get_size(sc->mtl_pixel_res);
102274823Sbrooks	info->fb_vbase = (intptr_t)pmap_mapdev(info->fb_pbase, info->fb_size);
103274823Sbrooks	if (terasic_mtl_fbd_panel_info(sc, info) != 0) {
104274823Sbrooks		device_printf(sc->mtl_dev, "using default panel params\n");
105274823Sbrooks		info->fb_bpp = info->fb_depth = 32;
106274823Sbrooks		info->fb_width = 800;
107274823Sbrooks		info->fb_height = 480;
108274823Sbrooks		info->fb_stride = info->fb_width * (info->fb_depth / 8);
109274823Sbrooks	}
110274823Sbrooks
111274823Sbrooks	fbd = device_add_child(sc->mtl_dev, "fbd",
112274823Sbrooks	    device_get_unit(sc->mtl_dev));
113274823Sbrooks	if (fbd == NULL) {
114274823Sbrooks		device_printf(sc->mtl_dev, "Failed to attach fbd child\n");
115274823Sbrooks		return (ENXIO);
116274823Sbrooks	}
117274823Sbrooks	if (device_probe_and_attach(fbd) != 0) {
118274823Sbrooks		device_printf(sc->mtl_dev,
119274823Sbrooks		    "Failed to attach fbd device\n");
120274823Sbrooks		return (ENXIO);
121274823Sbrooks	}
122274823Sbrooks	return (0);
123274823Sbrooks}
124274823Sbrooks
125274823Sbrooksvoid
126274823Sbrooksterasic_mtl_fbd_detach(struct terasic_mtl_softc *sc)
127274823Sbrooks{
128274823Sbrooks	panic("%s: detach not implemented", __func__);
129274823Sbrooks}
130274823Sbrooks
131274823Sbrooksextern device_t fbd_driver;
132274823Sbrooksextern devclass_t fbd_devclass;
133274823SbrooksDRIVER_MODULE(fbd, terasic_mtl, fbd_driver, fbd_devclass, 0, 0);
134