Home | History | Annotate | Line # | Download | only in nvidia
tegra_drm_fb.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: tegra_drm_fb.c,v 1.2 2015/11/12 00:43:52 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_drm_fb.c,v 1.2 2015/11/12 00:43:52 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <drm/drmP.h>
     33  1.1  jmcneill #include <drm/drm_crtc.h>
     34  1.1  jmcneill #include <drm/drm_fb_helper.h>
     35  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <arm/nvidia/tegra_drm.h>
     38  1.1  jmcneill 
     39  1.2  jmcneill static int	tegra_fb_init(struct drm_device *, struct drm_framebuffer *,
     40  1.2  jmcneill 		    struct drm_fb_helper_surface_size *);
     41  1.2  jmcneill 
     42  1.1  jmcneill static int	tegra_fb_probe(struct drm_fb_helper *,
     43  1.1  jmcneill 		    struct drm_fb_helper_surface_size *);
     44  1.1  jmcneill 
     45  1.1  jmcneill static struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
     46  1.1  jmcneill 	.fb_probe = tegra_fb_probe
     47  1.1  jmcneill };
     48  1.1  jmcneill 
     49  1.1  jmcneill int
     50  1.1  jmcneill tegra_drm_fb_init(struct drm_device *ddev)
     51  1.1  jmcneill {
     52  1.1  jmcneill 	struct tegra_fbdev *fbdev;
     53  1.1  jmcneill 	int error;
     54  1.1  jmcneill 
     55  1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
     56  1.1  jmcneill 	if (fbdev == NULL)
     57  1.1  jmcneill 		return -ENOMEM;
     58  1.1  jmcneill 	fbdev->helper.funcs = &tegra_fb_helper_funcs;
     59  1.1  jmcneill 
     60  1.1  jmcneill 	error = drm_fb_helper_init(ddev, &fbdev->helper, 2, 1);
     61  1.1  jmcneill 	if (error) {
     62  1.1  jmcneill 		kmem_free(fbdev, sizeof(*fbdev));
     63  1.1  jmcneill 		return error;
     64  1.1  jmcneill 	}
     65  1.1  jmcneill 
     66  1.2  jmcneill 	/*
     67  1.2  jmcneill 	 * This might seem silly, but it saves us from having to allocate
     68  1.2  jmcneill 	 * enough memory for the largest possible framebuffer (around 35MB).
     69  1.2  jmcneill 	 *
     70  1.2  jmcneill 	 * Allocate the fb_helper's framebuffer here but don't initialize
     71  1.2  jmcneill 	 * it yet. The fb helper won't configure a CRTC unless this field is
     72  1.2  jmcneill 	 * set, and we don't know the preferred size at this time.
     73  1.2  jmcneill 	 *
     74  1.2  jmcneill 	 * The fb_probe callback will eventually be called with the preferred
     75  1.2  jmcneill 	 * surface size, so defer allocating the buffer object until then.
     76  1.2  jmcneill 	 */
     77  1.2  jmcneill 	fbdev->helper.fb =
     78  1.2  jmcneill 	    kmem_zalloc(sizeof(struct tegra_framebuffer), KM_SLEEP);
     79  1.2  jmcneill 	if (fbdev->helper.fb == NULL) {
     80  1.2  jmcneill 		DRM_ERROR("failed to create framebuffer\n");
     81  1.1  jmcneill 	}
     82  1.1  jmcneill 
     83  1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
     84  1.1  jmcneill 
     85  1.1  jmcneill 	drm_helper_disable_unused_functions(ddev);
     86  1.1  jmcneill 
     87  1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
     88  1.1  jmcneill 
     89  1.1  jmcneill 	return 0;
     90  1.1  jmcneill }
     91  1.1  jmcneill 
     92  1.1  jmcneill static int
     93  1.1  jmcneill tegra_fb_probe(struct drm_fb_helper *helper,
     94  1.1  jmcneill     struct drm_fb_helper_surface_size *sizes)
     95  1.1  jmcneill {
     96  1.1  jmcneill 	struct tegra_drm_softc * const sc = tegra_drm_private(helper->dev);
     97  1.1  jmcneill 	struct drm_device *ddev = helper->dev;
     98  1.1  jmcneill 	struct tegra_drmfb_attach_args tfa;
     99  1.1  jmcneill 
    100  1.2  jmcneill 	if (tegra_fb_init(ddev, helper->fb, sizes) != 0) {
    101  1.2  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    102  1.2  jmcneill 		return -ENOMEM;
    103  1.2  jmcneill 	}
    104  1.2  jmcneill 
    105  1.1  jmcneill 	memset(&tfa, 0, sizeof(tfa));
    106  1.1  jmcneill 	tfa.tfa_drm_dev = ddev;
    107  1.1  jmcneill 	tfa.tfa_fb_helper = helper;
    108  1.1  jmcneill 	tfa.tfa_fb_sizes = *sizes;
    109  1.1  jmcneill 	tfa.tfa_fb_bst = sc->sc_bst;
    110  1.1  jmcneill 	tfa.tfa_fb_dmat = sc->sc_dmat;
    111  1.1  jmcneill 
    112  1.1  jmcneill 	helper->fbdev = config_found_ia(ddev->dev, "tegrafbbus", &tfa, NULL);
    113  1.1  jmcneill 	if (helper->fbdev == NULL) {
    114  1.1  jmcneill 		DRM_ERROR("unable to attach tegrafb\n");
    115  1.1  jmcneill 		return -ENXIO;
    116  1.1  jmcneill 	}
    117  1.1  jmcneill 
    118  1.1  jmcneill 	return 0;
    119  1.1  jmcneill }
    120  1.2  jmcneill 
    121  1.2  jmcneill static int
    122  1.2  jmcneill tegra_fb_init(struct drm_device *ddev, struct drm_framebuffer *fb,
    123  1.2  jmcneill     struct drm_fb_helper_surface_size *sizes)
    124  1.2  jmcneill {
    125  1.2  jmcneill 	struct tegra_framebuffer *tegra_fb = to_tegra_framebuffer(fb);
    126  1.2  jmcneill 	const u_int width = sizes->surface_width;
    127  1.2  jmcneill 	const u_int height = sizes->surface_height;
    128  1.2  jmcneill 	const u_int pitch = width * (32 / 8);
    129  1.2  jmcneill 
    130  1.2  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    131  1.2  jmcneill 
    132  1.2  jmcneill 	tegra_fb->obj = tegra_drm_obj_alloc(ddev, size);
    133  1.2  jmcneill 	if (tegra_fb->obj == NULL)
    134  1.2  jmcneill 		return -ENOMEM;
    135  1.2  jmcneill 
    136  1.2  jmcneill         fb->pitches[0] = pitch;
    137  1.2  jmcneill         fb->offsets[0] = 0;
    138  1.2  jmcneill         fb->width = width;
    139  1.2  jmcneill         fb->height = height;
    140  1.2  jmcneill         fb->pixel_format = DRM_FORMAT_ARGB8888;
    141  1.2  jmcneill         drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth,
    142  1.2  jmcneill             &fb->bits_per_pixel);
    143  1.2  jmcneill 	tegra_drm_framebuffer_init(ddev, tegra_fb);
    144  1.2  jmcneill 
    145  1.2  jmcneill 	return 0;
    146  1.2  jmcneill }
    147