blob: 181a370c56c11dd059655ec374fa13ddd1c9deb4 [file] [log] [blame]
Thierry Redingd8f4a9e2012-11-15 21:28:22 +00001/*
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
27static 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 Reding6e5ff992012-11-28 11:45:47 +010043 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
44 if (err < 0)
45 return err;
46
Thierry Redingd8f4a9e2012-11-15 21:28:22 +000047 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
56static 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
66static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
67{
68 return 0;
69}
70
71static 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
78static struct drm_ioctl_desc tegra_drm_ioctls[] = {
79};
80
81static 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 Reding6e5ff992012-11-28 11:45:47 +010096static 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
110static 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
116static 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
129static 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 Reding3c03c462012-11-28 12:00:18 +0100138static 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 Redinge450fcc2013-02-13 16:13:16 +0100146#ifdef CONFIG_DEBUG_FS
147static 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
167static struct drm_info_list tegra_debugfs_list[] = {
168 { "framebuffers", tegra_debugfs_framebuffers, 0 },
169};
170
171static 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
178static 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 Redingd8f4a9e2012-11-15 21:28:22 +0000185struct 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 Reding3c03c462012-11-28 12:00:18 +0100190 .preclose = tegra_drm_preclose,
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000191 .lastclose = tegra_drm_lastclose,
192
Thierry Reding6e5ff992012-11-28 11:45:47 +0100193 .get_vblank_counter = tegra_drm_get_vblank_counter,
194 .enable_vblank = tegra_drm_enable_vblank,
195 .disable_vblank = tegra_drm_disable_vblank,
196
Thierry Redinge450fcc2013-02-13 16:13:16 +0100197#if defined(CONFIG_DEBUG_FS)
198 .debugfs_init = tegra_debugfs_init,
199 .debugfs_cleanup = tegra_debugfs_cleanup,
200#endif
201
Thierry Redingd8f4a9e2012-11-15 21:28:22 +0000202 .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};