1/*-
2 * Copyright (c) 2014 Ed Maste
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/fbio.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/rman.h>
40
41#include <dev/fdt/fdt_common.h>
42#include <dev/ofw/ofw_bus.h>
43#include <dev/ofw/ofw_bus_subr.h>
44
45#include <dev/terasic/mtl/terasic_mtl.h>
46
47#include <dev/vt/colors/vt_termcolors.h>
48
49/*
50 * Terasic Multitouch LCD (MTL) vt(4) framebuffer driver.
51 */
52
53#include <vm/vm.h>
54#include <vm/pmap.h>
55
56static int
57terasic_mtl_fbd_panel_info(struct terasic_mtl_softc *sc, struct fb_info *info)
58{
59	phandle_t node;
60	pcell_t dts_value[2];
61	int len;
62
63	if ((node = ofw_bus_get_node(sc->mtl_dev)) == -1)
64		return (ENXIO);
65
66	/* panel size */
67	if ((len = OF_getproplen(node, "panel-size")) != sizeof(dts_value))
68		return (ENXIO);
69	OF_getencprop(node, "panel-size", dts_value, len);
70	info->fb_width = dts_value[0];
71	info->fb_height = dts_value[1];
72	info->fb_bpp = info->fb_depth = 32;
73	info->fb_stride = info->fb_width * (info->fb_depth / 8);
74
75	/*
76	 * Safety belt to ensure framebuffer params are as expected.  May be
77	 * removed when we have full confidence in fdt / hints params.
78	 */
79	if (info->fb_width != TERASIC_MTL_FB_WIDTH ||
80	    info->fb_height != TERASIC_MTL_FB_HEIGHT ||
81	    info->fb_stride != 3200 ||
82	    info->fb_bpp != 32 || info->fb_depth != 32) {
83		device_printf(sc->mtl_dev,
84		    "rejecting invalid panel params width=%u height=%u\n",
85		    (unsigned)info->fb_width, (unsigned)info->fb_height);
86		return (EINVAL);
87	}
88
89	return (0);
90}
91
92int
93terasic_mtl_fbd_attach(struct terasic_mtl_softc *sc)
94{
95	struct fb_info *info;
96	device_t fbd;
97
98	info = &sc->mtl_fb_info;
99	info->fb_name = device_get_nameunit(sc->mtl_dev);
100	info->fb_pbase = rman_get_start(sc->mtl_pixel_res);
101	info->fb_size = rman_get_size(sc->mtl_pixel_res);
102	info->fb_vbase = (intptr_t)pmap_mapdev(info->fb_pbase, info->fb_size);
103	if (terasic_mtl_fbd_panel_info(sc, info) != 0) {
104		device_printf(sc->mtl_dev, "using default panel params\n");
105		info->fb_bpp = info->fb_depth = 32;
106		info->fb_width = 800;
107		info->fb_height = 480;
108		info->fb_stride = info->fb_width * (info->fb_depth / 8);
109	}
110
111	fbd = device_add_child(sc->mtl_dev, "fbd",
112	    device_get_unit(sc->mtl_dev));
113	if (fbd == NULL) {
114		device_printf(sc->mtl_dev, "Failed to attach fbd child\n");
115		return (ENXIO);
116	}
117	if (device_probe_and_attach(fbd) != 0) {
118		device_printf(sc->mtl_dev,
119		    "Failed to attach fbd device\n");
120		return (ENXIO);
121	}
122	return (0);
123}
124
125void
126terasic_mtl_fbd_detach(struct terasic_mtl_softc *sc)
127{
128	panic("%s: detach not implemented", __func__);
129}
130
131extern device_t fbd_driver;
132extern devclass_t fbd_devclass;
133DRIVER_MODULE(fbd, terasic_mtl, fbd_driver, fbd_devclass, 0, 0);
134