Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Avionic Design GmbH |
| 3 | * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of_address.h> |
| 12 | #include <linux/of_platform.h> |
| 13 | |
| 14 | #include <mach/clk.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <asm/dma-iommu.h> |
| 17 | |
| 18 | #include "drm.h" |
| 19 | |
| 20 | #define DRIVER_NAME "tegra" |
| 21 | #define DRIVER_DESC "NVIDIA Tegra graphics" |
| 22 | #define DRIVER_DATE "20120330" |
| 23 | #define DRIVER_MAJOR 0 |
| 24 | #define DRIVER_MINOR 0 |
| 25 | #define DRIVER_PATCHLEVEL 0 |
| 26 | |
| 27 | static int tegra_drm_load(struct drm_device *drm, unsigned long flags) |
| 28 | { |
| 29 | struct device *dev = drm->dev; |
| 30 | struct host1x *host1x; |
| 31 | int err; |
| 32 | |
| 33 | host1x = dev_get_drvdata(dev); |
| 34 | drm->dev_private = host1x; |
| 35 | host1x->drm = drm; |
| 36 | |
| 37 | drm_mode_config_init(drm); |
| 38 | |
| 39 | err = host1x_drm_init(host1x, drm); |
| 40 | if (err < 0) |
| 41 | return err; |
| 42 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 43 | err = drm_vblank_init(drm, drm->mode_config.num_crtc); |
| 44 | if (err < 0) |
| 45 | return err; |
| 46 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 47 | err = tegra_drm_fb_init(drm); |
| 48 | if (err < 0) |
| 49 | return err; |
| 50 | |
| 51 | drm_kms_helper_poll_init(drm); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int tegra_drm_unload(struct drm_device *drm) |
| 57 | { |
| 58 | drm_kms_helper_poll_fini(drm); |
| 59 | tegra_drm_fb_exit(drm); |
| 60 | |
| 61 | drm_mode_config_cleanup(drm); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp) |
| 67 | { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static void tegra_drm_lastclose(struct drm_device *drm) |
| 72 | { |
| 73 | struct host1x *host1x = drm->dev_private; |
| 74 | |
| 75 | drm_fbdev_cma_restore_mode(host1x->fbdev); |
| 76 | } |
| 77 | |
| 78 | static struct drm_ioctl_desc tegra_drm_ioctls[] = { |
| 79 | }; |
| 80 | |
| 81 | static const struct file_operations tegra_drm_fops = { |
| 82 | .owner = THIS_MODULE, |
| 83 | .open = drm_open, |
| 84 | .release = drm_release, |
| 85 | .unlocked_ioctl = drm_ioctl, |
| 86 | .mmap = drm_gem_cma_mmap, |
| 87 | .poll = drm_poll, |
| 88 | .fasync = drm_fasync, |
| 89 | .read = drm_read, |
| 90 | #ifdef CONFIG_COMPAT |
| 91 | .compat_ioctl = drm_compat_ioctl, |
| 92 | #endif |
| 93 | .llseek = noop_llseek, |
| 94 | }; |
| 95 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 96 | static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe) |
| 97 | { |
| 98 | struct drm_crtc *crtc; |
| 99 | |
| 100 | list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) { |
| 101 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 102 | |
| 103 | if (dc->pipe == pipe) |
| 104 | return crtc; |
| 105 | } |
| 106 | |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc) |
| 111 | { |
| 112 | /* TODO: implement real hardware counter using syncpoints */ |
| 113 | return drm_vblank_count(dev, crtc); |
| 114 | } |
| 115 | |
| 116 | static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe) |
| 117 | { |
| 118 | struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe); |
| 119 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 120 | |
| 121 | if (!crtc) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | tegra_dc_enable_vblank(dc); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe) |
| 130 | { |
| 131 | struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe); |
| 132 | struct tegra_dc *dc = to_tegra_dc(crtc); |
| 133 | |
| 134 | if (crtc) |
| 135 | tegra_dc_disable_vblank(dc); |
| 136 | } |
| 137 | |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 138 | static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file) |
| 139 | { |
| 140 | struct drm_crtc *crtc; |
| 141 | |
| 142 | list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) |
| 143 | tegra_dc_cancel_page_flip(crtc, file); |
| 144 | } |
| 145 | |
Thierry Reding | e450fcc | 2013-02-13 16:13:16 +0100 | [diff] [blame^] | 146 | #ifdef CONFIG_DEBUG_FS |
| 147 | static int tegra_debugfs_framebuffers(struct seq_file *s, void *data) |
| 148 | { |
| 149 | struct drm_info_node *node = (struct drm_info_node *)s->private; |
| 150 | struct drm_device *drm = node->minor->dev; |
| 151 | struct drm_framebuffer *fb; |
| 152 | |
| 153 | mutex_lock(&drm->mode_config.fb_lock); |
| 154 | |
| 155 | list_for_each_entry(fb, &drm->mode_config.fb_list, head) { |
| 156 | seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n", |
| 157 | fb->base.id, fb->width, fb->height, fb->depth, |
| 158 | fb->bits_per_pixel, |
| 159 | atomic_read(&fb->refcount.refcount)); |
| 160 | } |
| 161 | |
| 162 | mutex_unlock(&drm->mode_config.fb_lock); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static struct drm_info_list tegra_debugfs_list[] = { |
| 168 | { "framebuffers", tegra_debugfs_framebuffers, 0 }, |
| 169 | }; |
| 170 | |
| 171 | static int tegra_debugfs_init(struct drm_minor *minor) |
| 172 | { |
| 173 | return drm_debugfs_create_files(tegra_debugfs_list, |
| 174 | ARRAY_SIZE(tegra_debugfs_list), |
| 175 | minor->debugfs_root, minor); |
| 176 | } |
| 177 | |
| 178 | static void tegra_debugfs_cleanup(struct drm_minor *minor) |
| 179 | { |
| 180 | drm_debugfs_remove_files(tegra_debugfs_list, |
| 181 | ARRAY_SIZE(tegra_debugfs_list), minor); |
| 182 | } |
| 183 | #endif |
| 184 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 185 | struct drm_driver tegra_drm_driver = { |
| 186 | .driver_features = DRIVER_BUS_PLATFORM | DRIVER_MODESET | DRIVER_GEM, |
| 187 | .load = tegra_drm_load, |
| 188 | .unload = tegra_drm_unload, |
| 189 | .open = tegra_drm_open, |
Thierry Reding | 3c03c46 | 2012-11-28 12:00:18 +0100 | [diff] [blame] | 190 | .preclose = tegra_drm_preclose, |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 191 | .lastclose = tegra_drm_lastclose, |
| 192 | |
Thierry Reding | 6e5ff99 | 2012-11-28 11:45:47 +0100 | [diff] [blame] | 193 | .get_vblank_counter = tegra_drm_get_vblank_counter, |
| 194 | .enable_vblank = tegra_drm_enable_vblank, |
| 195 | .disable_vblank = tegra_drm_disable_vblank, |
| 196 | |
Thierry Reding | e450fcc | 2013-02-13 16:13:16 +0100 | [diff] [blame^] | 197 | #if defined(CONFIG_DEBUG_FS) |
| 198 | .debugfs_init = tegra_debugfs_init, |
| 199 | .debugfs_cleanup = tegra_debugfs_cleanup, |
| 200 | #endif |
| 201 | |
Thierry Reding | d8f4a9e | 2012-11-15 21:28:22 +0000 | [diff] [blame] | 202 | .gem_free_object = drm_gem_cma_free_object, |
| 203 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 204 | .dumb_create = drm_gem_cma_dumb_create, |
| 205 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
| 206 | .dumb_destroy = drm_gem_cma_dumb_destroy, |
| 207 | |
| 208 | .ioctls = tegra_drm_ioctls, |
| 209 | .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls), |
| 210 | .fops = &tegra_drm_fops, |
| 211 | |
| 212 | .name = DRIVER_NAME, |
| 213 | .desc = DRIVER_DESC, |
| 214 | .date = DRIVER_DATE, |
| 215 | .major = DRIVER_MAJOR, |
| 216 | .minor = DRIVER_MINOR, |
| 217 | .patchlevel = DRIVER_PATCHLEVEL, |
| 218 | }; |