2 * Copyright (C) 2012 Avionic Design GmbH
3 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
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.
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
14 #include <linux/dma-mapping.h>
15 #include <asm/dma-iommu.h>
17 #include "host1x_client.h"
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
27 struct host1x_drm_client {
28 struct host1x_client *client;
29 struct device_node *np;
30 struct list_head list;
33 static int host1x_add_drm_client(struct host1x_drm *host1x,
34 struct device_node *np)
36 struct host1x_drm_client *client;
38 client = kzalloc(sizeof(*client), GFP_KERNEL);
42 INIT_LIST_HEAD(&client->list);
43 client->np = of_node_get(np);
45 list_add_tail(&client->list, &host1x->drm_clients);
50 static int host1x_activate_drm_client(struct host1x_drm *host1x,
51 struct host1x_drm_client *drm,
52 struct host1x_client *client)
54 mutex_lock(&host1x->drm_clients_lock);
55 list_del_init(&drm->list);
56 list_add_tail(&drm->list, &host1x->drm_active);
58 mutex_unlock(&host1x->drm_clients_lock);
63 static int host1x_remove_drm_client(struct host1x_drm *host1x,
64 struct host1x_drm_client *client)
66 mutex_lock(&host1x->drm_clients_lock);
67 list_del_init(&client->list);
68 mutex_unlock(&host1x->drm_clients_lock);
70 of_node_put(client->np);
76 static int host1x_parse_dt(struct host1x_drm *host1x)
78 static const char * const compat[] = {
80 "nvidia,tegra20-hdmi",
82 "nvidia,tegra30-hdmi",
87 for (i = 0; i < ARRAY_SIZE(compat); i++) {
88 struct device_node *np;
90 for_each_child_of_node(host1x->dev->of_node, np) {
91 if (of_device_is_compatible(np, compat[i]) &&
92 of_device_is_available(np)) {
93 err = host1x_add_drm_client(host1x, np);
103 int host1x_drm_alloc(struct platform_device *pdev)
105 struct host1x_drm *host1x;
108 host1x = devm_kzalloc(&pdev->dev, sizeof(*host1x), GFP_KERNEL);
112 mutex_init(&host1x->drm_clients_lock);
113 INIT_LIST_HEAD(&host1x->drm_clients);
114 INIT_LIST_HEAD(&host1x->drm_active);
115 mutex_init(&host1x->clients_lock);
116 INIT_LIST_HEAD(&host1x->clients);
117 host1x->dev = &pdev->dev;
119 err = host1x_parse_dt(host1x);
121 dev_err(&pdev->dev, "failed to parse DT: %d\n", err);
125 host1x_set_drm_data(&pdev->dev, host1x);
130 int host1x_drm_init(struct host1x_drm *host1x, struct drm_device *drm)
132 struct host1x_client *client;
134 mutex_lock(&host1x->clients_lock);
136 list_for_each_entry(client, &host1x->clients, list) {
137 if (client->ops && client->ops->drm_init) {
138 int err = client->ops->drm_init(client, drm);
141 "DRM setup failed for %s: %d\n",
142 dev_name(client->dev), err);
148 mutex_unlock(&host1x->clients_lock);
153 int host1x_drm_exit(struct host1x_drm *host1x)
155 struct platform_device *pdev = to_platform_device(host1x->dev);
156 struct host1x_client *client;
161 mutex_lock(&host1x->clients_lock);
163 list_for_each_entry_reverse(client, &host1x->clients, list) {
164 if (client->ops && client->ops->drm_exit) {
165 int err = client->ops->drm_exit(client);
168 "DRM cleanup failed for %s: %d\n",
169 dev_name(client->dev), err);
175 mutex_unlock(&host1x->clients_lock);
177 drm_platform_exit(&tegra_drm_driver, pdev);
183 int host1x_register_client(struct host1x_drm *host1x,
184 struct host1x_client *client)
186 struct host1x_drm_client *drm, *tmp;
189 mutex_lock(&host1x->clients_lock);
190 list_add_tail(&client->list, &host1x->clients);
191 mutex_unlock(&host1x->clients_lock);
193 list_for_each_entry_safe(drm, tmp, &host1x->drm_clients, list)
194 if (drm->np == client->dev->of_node)
195 host1x_activate_drm_client(host1x, drm, client);
197 if (list_empty(&host1x->drm_clients)) {
198 struct platform_device *pdev = to_platform_device(host1x->dev);
200 err = drm_platform_init(&tegra_drm_driver, pdev);
202 dev_err(host1x->dev, "drm_platform_init(): %d\n", err);
210 int host1x_unregister_client(struct host1x_drm *host1x,
211 struct host1x_client *client)
213 struct host1x_drm_client *drm, *tmp;
216 list_for_each_entry_safe(drm, tmp, &host1x->drm_active, list) {
217 if (drm->client == client) {
218 err = host1x_drm_exit(host1x);
220 dev_err(host1x->dev, "host1x_drm_exit(): %d\n",
225 host1x_remove_drm_client(host1x, drm);
230 mutex_lock(&host1x->clients_lock);
231 list_del_init(&client->list);
232 mutex_unlock(&host1x->clients_lock);
237 static int tegra_drm_load(struct drm_device *drm, unsigned long flags)
239 struct host1x_drm *host1x;
242 host1x = host1x_get_drm_data(drm->dev);
243 drm->dev_private = host1x;
246 drm_mode_config_init(drm);
248 err = host1x_drm_init(host1x, drm);
252 err = drm_vblank_init(drm, drm->mode_config.num_crtc);
256 err = tegra_drm_fb_init(drm);
260 drm_kms_helper_poll_init(drm);
265 static int tegra_drm_unload(struct drm_device *drm)
267 drm_kms_helper_poll_fini(drm);
268 tegra_drm_fb_exit(drm);
270 drm_mode_config_cleanup(drm);
275 static int tegra_drm_open(struct drm_device *drm, struct drm_file *filp)
280 static void tegra_drm_lastclose(struct drm_device *drm)
282 struct host1x_drm *host1x = drm->dev_private;
284 drm_fbdev_cma_restore_mode(host1x->fbdev);
287 static struct drm_ioctl_desc tegra_drm_ioctls[] = {
290 static const struct file_operations tegra_drm_fops = {
291 .owner = THIS_MODULE,
293 .release = drm_release,
294 .unlocked_ioctl = drm_ioctl,
295 .mmap = drm_gem_cma_mmap,
297 .fasync = drm_fasync,
300 .compat_ioctl = drm_compat_ioctl,
302 .llseek = noop_llseek,
305 static struct drm_crtc *tegra_crtc_from_pipe(struct drm_device *drm, int pipe)
307 struct drm_crtc *crtc;
309 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head) {
310 struct tegra_dc *dc = to_tegra_dc(crtc);
312 if (dc->pipe == pipe)
319 static u32 tegra_drm_get_vblank_counter(struct drm_device *dev, int crtc)
321 /* TODO: implement real hardware counter using syncpoints */
322 return drm_vblank_count(dev, crtc);
325 static int tegra_drm_enable_vblank(struct drm_device *drm, int pipe)
327 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
328 struct tegra_dc *dc = to_tegra_dc(crtc);
333 tegra_dc_enable_vblank(dc);
338 static void tegra_drm_disable_vblank(struct drm_device *drm, int pipe)
340 struct drm_crtc *crtc = tegra_crtc_from_pipe(drm, pipe);
341 struct tegra_dc *dc = to_tegra_dc(crtc);
344 tegra_dc_disable_vblank(dc);
347 static void tegra_drm_preclose(struct drm_device *drm, struct drm_file *file)
349 struct drm_crtc *crtc;
351 list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
352 tegra_dc_cancel_page_flip(crtc, file);
355 #ifdef CONFIG_DEBUG_FS
356 static int tegra_debugfs_framebuffers(struct seq_file *s, void *data)
358 struct drm_info_node *node = (struct drm_info_node *)s->private;
359 struct drm_device *drm = node->minor->dev;
360 struct drm_framebuffer *fb;
362 mutex_lock(&drm->mode_config.fb_lock);
364 list_for_each_entry(fb, &drm->mode_config.fb_list, head) {
365 seq_printf(s, "%3d: user size: %d x %d, depth %d, %d bpp, refcount %d\n",
366 fb->base.id, fb->width, fb->height, fb->depth,
368 atomic_read(&fb->refcount.refcount));
371 mutex_unlock(&drm->mode_config.fb_lock);
376 static struct drm_info_list tegra_debugfs_list[] = {
377 { "framebuffers", tegra_debugfs_framebuffers, 0 },
380 static int tegra_debugfs_init(struct drm_minor *minor)
382 return drm_debugfs_create_files(tegra_debugfs_list,
383 ARRAY_SIZE(tegra_debugfs_list),
384 minor->debugfs_root, minor);
387 static void tegra_debugfs_cleanup(struct drm_minor *minor)
389 drm_debugfs_remove_files(tegra_debugfs_list,
390 ARRAY_SIZE(tegra_debugfs_list), minor);
394 struct drm_driver tegra_drm_driver = {
395 .driver_features = DRIVER_BUS_PLATFORM | DRIVER_MODESET | DRIVER_GEM,
396 .load = tegra_drm_load,
397 .unload = tegra_drm_unload,
398 .open = tegra_drm_open,
399 .preclose = tegra_drm_preclose,
400 .lastclose = tegra_drm_lastclose,
402 .get_vblank_counter = tegra_drm_get_vblank_counter,
403 .enable_vblank = tegra_drm_enable_vblank,
404 .disable_vblank = tegra_drm_disable_vblank,
406 #if defined(CONFIG_DEBUG_FS)
407 .debugfs_init = tegra_debugfs_init,
408 .debugfs_cleanup = tegra_debugfs_cleanup,
411 .gem_free_object = drm_gem_cma_free_object,
412 .gem_vm_ops = &drm_gem_cma_vm_ops,
413 .dumb_create = drm_gem_cma_dumb_create,
414 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
415 .dumb_destroy = drm_gem_cma_dumb_destroy,
417 .ioctls = tegra_drm_ioctls,
418 .num_ioctls = ARRAY_SIZE(tegra_drm_ioctls),
419 .fops = &tegra_drm_fops,
424 .major = DRIVER_MAJOR,
425 .minor = DRIVER_MINOR,
426 .patchlevel = DRIVER_PATCHLEVEL,