xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/ast/ast_drv.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /*	$NetBSD: ast_drv.c,v 1.3 2018/08/28 03:41:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  */
27 /*
28  * Authors: Dave Airlie <airlied@redhat.com>
29  */
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ast_drv.c,v 1.3 2018/08/28 03:41:38 riastradh Exp $");
32 
33 #include <linux/module.h>
34 #include <linux/console.h>
35 
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc_helper.h>
38 
39 #include "ast_drv.h"
40 
41 int ast_modeset = -1;
42 
43 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
44 module_param_named(modeset, ast_modeset, int, 0400);
45 
46 #define PCI_VENDOR_ASPEED 0x1a03
47 
48 static struct drm_driver driver;
49 
50 #define AST_VGA_DEVICE(id, info) {		\
51 	.class = PCI_BASE_CLASS_DISPLAY << 16,	\
52 	.class_mask = 0xff0000,			\
53 	.vendor = PCI_VENDOR_ASPEED,			\
54 	.device = id,				\
55 	.subvendor = PCI_ANY_ID,		\
56 	.subdevice = PCI_ANY_ID,		\
57 	.driver_data = (unsigned long) info }
58 
59 static const struct pci_device_id pciidlist[] = {
60 	AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
61 	AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
62 	/*	AST_VGA_DEVICE(PCI_CHIP_AST1180, NULL), - don't bind to 1180 for now */
63 	{0, 0, 0},
64 };
65 
66 MODULE_DEVICE_TABLE(pci, pciidlist);
67 
68 static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
69 {
70 	return drm_get_pci_dev(pdev, ent, &driver);
71 }
72 
73 static void
74 ast_pci_remove(struct pci_dev *pdev)
75 {
76 	struct drm_device *dev = pci_get_drvdata(pdev);
77 
78 	drm_put_dev(dev);
79 }
80 
81 
82 
83 static int ast_drm_freeze(struct drm_device *dev)
84 {
85 	drm_kms_helper_poll_disable(dev);
86 
87 	pci_save_state(dev->pdev);
88 
89 	console_lock();
90 	ast_fbdev_set_suspend(dev, 1);
91 	console_unlock();
92 	return 0;
93 }
94 
95 static int ast_drm_thaw(struct drm_device *dev)
96 {
97 	int error = 0;
98 
99 	ast_post_gpu(dev);
100 
101 	drm_mode_config_reset(dev);
102 	drm_helper_resume_force_mode(dev);
103 
104 	console_lock();
105 	ast_fbdev_set_suspend(dev, 0);
106 	console_unlock();
107 	return error;
108 }
109 
110 static int ast_drm_resume(struct drm_device *dev)
111 {
112 	int ret;
113 
114 	if (pci_enable_device(dev->pdev))
115 		return -EIO;
116 
117 	ret = ast_drm_thaw(dev);
118 	if (ret)
119 		return ret;
120 
121 	drm_kms_helper_poll_enable(dev);
122 	return 0;
123 }
124 
125 static int ast_pm_suspend(struct device *dev)
126 {
127 	struct pci_dev *pdev = to_pci_dev(dev);
128 	struct drm_device *ddev = pci_get_drvdata(pdev);
129 	int error;
130 
131 	error = ast_drm_freeze(ddev);
132 	if (error)
133 		return error;
134 
135 	pci_disable_device(pdev);
136 	pci_set_power_state(pdev, PCI_D3hot);
137 	return 0;
138 }
139 static int ast_pm_resume(struct device *dev)
140 {
141 	struct pci_dev *pdev = to_pci_dev(dev);
142 	struct drm_device *ddev = pci_get_drvdata(pdev);
143 	return ast_drm_resume(ddev);
144 }
145 
146 static int ast_pm_freeze(struct device *dev)
147 {
148 	struct pci_dev *pdev = to_pci_dev(dev);
149 	struct drm_device *ddev = pci_get_drvdata(pdev);
150 
151 	if (!ddev || !ddev->dev_private)
152 		return -ENODEV;
153 	return ast_drm_freeze(ddev);
154 
155 }
156 
157 static int ast_pm_thaw(struct device *dev)
158 {
159 	struct pci_dev *pdev = to_pci_dev(dev);
160 	struct drm_device *ddev = pci_get_drvdata(pdev);
161 	return ast_drm_thaw(ddev);
162 }
163 
164 static int ast_pm_poweroff(struct device *dev)
165 {
166 	struct pci_dev *pdev = to_pci_dev(dev);
167 	struct drm_device *ddev = pci_get_drvdata(pdev);
168 
169 	return ast_drm_freeze(ddev);
170 }
171 
172 static const struct dev_pm_ops ast_pm_ops = {
173 	.suspend = ast_pm_suspend,
174 	.resume = ast_pm_resume,
175 	.freeze = ast_pm_freeze,
176 	.thaw = ast_pm_thaw,
177 	.poweroff = ast_pm_poweroff,
178 	.restore = ast_pm_resume,
179 };
180 
181 static struct pci_driver ast_pci_driver = {
182 	.name = DRIVER_NAME,
183 	.id_table = pciidlist,
184 	.probe = ast_pci_probe,
185 	.remove = ast_pci_remove,
186 	.driver.pm = &ast_pm_ops,
187 };
188 
189 static const struct file_operations ast_fops = {
190 	.owner = THIS_MODULE,
191 	.open = drm_open,
192 	.release = drm_release,
193 	.unlocked_ioctl = drm_ioctl,
194 	.mmap = ast_mmap,
195 	.poll = drm_poll,
196 #ifdef CONFIG_COMPAT
197 	.compat_ioctl = drm_compat_ioctl,
198 #endif
199 	.read = drm_read,
200 };
201 
202 static struct drm_driver driver = {
203 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
204 
205 	.load = ast_driver_load,
206 	.unload = ast_driver_unload,
207 	.set_busid = drm_pci_set_busid,
208 	.set_unique = drm_pci_set_unique,
209 
210 	.fops = &ast_fops,
211 	.name = DRIVER_NAME,
212 	.desc = DRIVER_DESC,
213 	.date = DRIVER_DATE,
214 	.major = DRIVER_MAJOR,
215 	.minor = DRIVER_MINOR,
216 	.patchlevel = DRIVER_PATCHLEVEL,
217 
218 	.gem_free_object = ast_gem_free_object,
219 	.dumb_create = ast_dumb_create,
220 	.dumb_map_offset = ast_dumb_mmap_offset,
221 	.dumb_destroy = drm_gem_dumb_destroy,
222 
223 };
224 
225 static int __init ast_init(void)
226 {
227 #ifdef CONFIG_VGA_CONSOLE
228 	if (vgacon_text_force() && ast_modeset == -1)
229 		return -EINVAL;
230 #endif
231 
232 	if (ast_modeset == 0)
233 		return -EINVAL;
234 	return drm_pci_init(&driver, &ast_pci_driver);
235 }
236 static void __exit ast_exit(void)
237 {
238 	drm_pci_exit(&driver, &ast_pci_driver);
239 }
240 
241 module_init(ast_init);
242 module_exit(ast_exit);
243 
244 MODULE_AUTHOR(DRIVER_AUTHOR);
245 MODULE_DESCRIPTION(DRIVER_DESC);
246 MODULE_LICENSE("GPL and additional rights");
247 
248