Merge tag 'qcom-soc-for-3.16-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / gpu / drm / cirrus / cirrus_drv.c
1 /*
2  * Copyright 2012 Red Hat <mjg@redhat.com>
3  *
4  * This file is subject to the terms and conditions of the GNU General
5  * Public License version 2. See the file COPYING in the main
6  * directory of this archive for more details.
7  *
8  * Authors: Matthew Garrett
9  *          Dave Airlie
10  */
11 #include <linux/module.h>
12 #include <linux/console.h>
13 #include <drm/drmP.h>
14 #include <drm/drm_crtc_helper.h>
15
16 #include "cirrus_drv.h"
17
18 int cirrus_modeset = -1;
19
20 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
21 module_param_named(modeset, cirrus_modeset, int, 0400);
22
23 /*
24  * This is the generic driver code. This binds the driver to the drm core,
25  * which then performs further device association and calls our graphics init
26  * functions
27  */
28
29 static struct drm_driver driver;
30
31 /* only bind to the cirrus chip in qemu */
32 static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
33         { PCI_VENDOR_ID_CIRRUS, PCI_DEVICE_ID_CIRRUS_5446, 0x1af4, 0x1100, 0,
34           0, 0 },
35         {0,}
36 };
37
38
39 static int cirrus_kick_out_firmware_fb(struct pci_dev *pdev)
40 {
41         struct apertures_struct *ap;
42         bool primary = false;
43
44         ap = alloc_apertures(1);
45         if (!ap)
46                 return -ENOMEM;
47
48         ap->ranges[0].base = pci_resource_start(pdev, 0);
49         ap->ranges[0].size = pci_resource_len(pdev, 0);
50
51 #ifdef CONFIG_X86
52         primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
53 #endif
54         remove_conflicting_framebuffers(ap, "cirrusdrmfb", primary);
55         kfree(ap);
56
57         return 0;
58 }
59
60 static int cirrus_pci_probe(struct pci_dev *pdev,
61                             const struct pci_device_id *ent)
62 {
63         int ret;
64
65         ret = cirrus_kick_out_firmware_fb(pdev);
66         if (ret)
67                 return ret;
68
69         return drm_get_pci_dev(pdev, ent, &driver);
70 }
71
72 static void cirrus_pci_remove(struct pci_dev *pdev)
73 {
74         struct drm_device *dev = pci_get_drvdata(pdev);
75
76         drm_put_dev(dev);
77 }
78
79 static int cirrus_pm_suspend(struct device *dev)
80 {
81         struct pci_dev *pdev = to_pci_dev(dev);
82         struct drm_device *drm_dev = pci_get_drvdata(pdev);
83         struct cirrus_device *cdev = drm_dev->dev_private;
84
85         drm_kms_helper_poll_disable(drm_dev);
86
87         if (cdev->mode_info.gfbdev) {
88                 console_lock();
89                 fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 1);
90                 console_unlock();
91         }
92
93         return 0;
94 }
95
96 static int cirrus_pm_resume(struct device *dev)
97 {
98         struct pci_dev *pdev = to_pci_dev(dev);
99         struct drm_device *drm_dev = pci_get_drvdata(pdev);
100         struct cirrus_device *cdev = drm_dev->dev_private;
101
102         drm_helper_resume_force_mode(drm_dev);
103
104         if (cdev->mode_info.gfbdev) {
105                 console_lock();
106                 fb_set_suspend(cdev->mode_info.gfbdev->helper.fbdev, 0);
107                 console_unlock();
108         }
109
110         drm_kms_helper_poll_enable(drm_dev);
111         return 0;
112 }
113
114 static const struct file_operations cirrus_driver_fops = {
115         .owner = THIS_MODULE,
116         .open = drm_open,
117         .release = drm_release,
118         .unlocked_ioctl = drm_ioctl,
119         .mmap = cirrus_mmap,
120         .poll = drm_poll,
121 #ifdef CONFIG_COMPAT
122         .compat_ioctl = drm_compat_ioctl,
123 #endif
124 };
125 static struct drm_driver driver = {
126         .driver_features = DRIVER_MODESET | DRIVER_GEM,
127         .load = cirrus_driver_load,
128         .unload = cirrus_driver_unload,
129         .fops = &cirrus_driver_fops,
130         .name = DRIVER_NAME,
131         .desc = DRIVER_DESC,
132         .date = DRIVER_DATE,
133         .major = DRIVER_MAJOR,
134         .minor = DRIVER_MINOR,
135         .patchlevel = DRIVER_PATCHLEVEL,
136         .gem_free_object = cirrus_gem_free_object,
137         .dumb_create = cirrus_dumb_create,
138         .dumb_map_offset = cirrus_dumb_mmap_offset,
139         .dumb_destroy = drm_gem_dumb_destroy,
140 };
141
142 static const struct dev_pm_ops cirrus_pm_ops = {
143         SET_SYSTEM_SLEEP_PM_OPS(cirrus_pm_suspend,
144                                 cirrus_pm_resume)
145 };
146
147 static struct pci_driver cirrus_pci_driver = {
148         .name = DRIVER_NAME,
149         .id_table = pciidlist,
150         .probe = cirrus_pci_probe,
151         .remove = cirrus_pci_remove,
152         .driver.pm = &cirrus_pm_ops,
153 };
154
155 static int __init cirrus_init(void)
156 {
157 #ifdef CONFIG_VGA_CONSOLE
158         if (vgacon_text_force() && cirrus_modeset == -1)
159                 return -EINVAL;
160 #endif
161
162         if (cirrus_modeset == 0)
163                 return -EINVAL;
164         return drm_pci_init(&driver, &cirrus_pci_driver);
165 }
166
167 static void __exit cirrus_exit(void)
168 {
169         drm_pci_exit(&driver, &cirrus_pci_driver);
170 }
171
172 module_init(cirrus_init);
173 module_exit(cirrus_exit);
174
175 MODULE_DEVICE_TABLE(pci, pciidlist);
176 MODULE_AUTHOR(DRIVER_AUTHOR);
177 MODULE_DESCRIPTION(DRIVER_DESC);
178 MODULE_LICENSE("GPL");