1/*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25/*
26 * Authors: Dave Airlie <airlied@redhat.com>
27 */
28
29#include <linux/module.h>
30#include <linux/pci.h>
31
32#include <drm/drm_aperture.h>
33#include <drm/drm_atomic_helper.h>
34#include <drm/drm_drv.h>
35#include <drm/drm_fbdev_generic.h>
36#include <drm/drm_gem_shmem_helper.h>
37#include <drm/drm_module.h>
38#include <drm/drm_probe_helper.h>
39
40#include "ast_drv.h"
41
42static int ast_modeset = -1;
43
44MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
45module_param_named(modeset, ast_modeset, int, 0400);
46
47/*
48 * DRM driver
49 */
50
51DEFINE_DRM_GEM_FOPS(ast_fops);
52
53static const struct drm_driver ast_driver = {
54	.driver_features = DRIVER_ATOMIC |
55			   DRIVER_GEM |
56			   DRIVER_MODESET,
57
58	.fops = &ast_fops,
59	.name = DRIVER_NAME,
60	.desc = DRIVER_DESC,
61	.date = DRIVER_DATE,
62	.major = DRIVER_MAJOR,
63	.minor = DRIVER_MINOR,
64	.patchlevel = DRIVER_PATCHLEVEL,
65
66	DRM_GEM_SHMEM_DRIVER_OPS
67};
68
69/*
70 * PCI driver
71 */
72
73#define PCI_VENDOR_ASPEED 0x1a03
74
75#define AST_VGA_DEVICE(id, info) {		\
76	.class = PCI_BASE_CLASS_DISPLAY << 16,	\
77	.class_mask = 0xff0000,			\
78	.vendor = PCI_VENDOR_ASPEED,			\
79	.device = id,				\
80	.subvendor = PCI_ANY_ID,		\
81	.subdevice = PCI_ANY_ID,		\
82	.driver_data = (unsigned long) info }
83
84static const struct pci_device_id ast_pciidlist[] = {
85	AST_VGA_DEVICE(PCI_CHIP_AST2000, NULL),
86	AST_VGA_DEVICE(PCI_CHIP_AST2100, NULL),
87	{0, 0, 0},
88};
89
90MODULE_DEVICE_TABLE(pci, ast_pciidlist);
91
92static int ast_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
93{
94	struct ast_device *ast;
95	struct drm_device *dev;
96	int ret;
97
98	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &ast_driver);
99	if (ret)
100		return ret;
101
102	ret = pcim_enable_device(pdev);
103	if (ret)
104		return ret;
105
106	ast = ast_device_create(&ast_driver, pdev, ent->driver_data);
107	if (IS_ERR(ast))
108		return PTR_ERR(ast);
109	dev = &ast->base;
110
111	ret = drm_dev_register(dev, ent->driver_data);
112	if (ret)
113		return ret;
114
115	drm_fbdev_generic_setup(dev, 32);
116
117	return 0;
118}
119
120static void ast_pci_remove(struct pci_dev *pdev)
121{
122	struct drm_device *dev = pci_get_drvdata(pdev);
123
124	drm_dev_unregister(dev);
125	drm_atomic_helper_shutdown(dev);
126}
127
128static int ast_drm_freeze(struct drm_device *dev)
129{
130	int error;
131
132	error = drm_mode_config_helper_suspend(dev);
133	if (error)
134		return error;
135	pci_save_state(to_pci_dev(dev->dev));
136	return 0;
137}
138
139static int ast_drm_thaw(struct drm_device *dev)
140{
141	ast_post_gpu(dev);
142
143	return drm_mode_config_helper_resume(dev);
144}
145
146static int ast_drm_resume(struct drm_device *dev)
147{
148	if (pci_enable_device(to_pci_dev(dev->dev)))
149		return -EIO;
150
151	return ast_drm_thaw(dev);
152}
153
154static int ast_pm_suspend(struct device *dev)
155{
156	struct pci_dev *pdev = to_pci_dev(dev);
157	struct drm_device *ddev = pci_get_drvdata(pdev);
158	int error;
159
160	error = ast_drm_freeze(ddev);
161	if (error)
162		return error;
163
164	pci_disable_device(pdev);
165	pci_set_power_state(pdev, PCI_D3hot);
166	return 0;
167}
168
169static int ast_pm_resume(struct device *dev)
170{
171	struct pci_dev *pdev = to_pci_dev(dev);
172	struct drm_device *ddev = pci_get_drvdata(pdev);
173	return ast_drm_resume(ddev);
174}
175
176static int ast_pm_freeze(struct device *dev)
177{
178	struct pci_dev *pdev = to_pci_dev(dev);
179	struct drm_device *ddev = pci_get_drvdata(pdev);
180	return ast_drm_freeze(ddev);
181}
182
183static int ast_pm_thaw(struct device *dev)
184{
185	struct pci_dev *pdev = to_pci_dev(dev);
186	struct drm_device *ddev = pci_get_drvdata(pdev);
187	return ast_drm_thaw(ddev);
188}
189
190static int ast_pm_poweroff(struct device *dev)
191{
192	struct pci_dev *pdev = to_pci_dev(dev);
193	struct drm_device *ddev = pci_get_drvdata(pdev);
194
195	return ast_drm_freeze(ddev);
196}
197
198static const struct dev_pm_ops ast_pm_ops = {
199	.suspend = ast_pm_suspend,
200	.resume = ast_pm_resume,
201	.freeze = ast_pm_freeze,
202	.thaw = ast_pm_thaw,
203	.poweroff = ast_pm_poweroff,
204	.restore = ast_pm_resume,
205};
206
207static struct pci_driver ast_pci_driver = {
208	.name = DRIVER_NAME,
209	.id_table = ast_pciidlist,
210	.probe = ast_pci_probe,
211	.remove = ast_pci_remove,
212	.driver.pm = &ast_pm_ops,
213};
214
215drm_module_pci_driver_if_modeset(ast_pci_driver, ast_modeset);
216
217MODULE_AUTHOR(DRIVER_AUTHOR);
218MODULE_DESCRIPTION(DRIVER_DESC);
219MODULE_LICENSE("GPL and additional rights");
220