RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / pvr_platform_drv.c
1 /* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* vi: set ts=8 sw=8 sts=8: */
3 /*************************************************************************/ /*!
4 @File
5 @Title          PowerVR DRM platform driver
6 @Codingstyle    LinuxKernel
7 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
8 @License        Dual MIT/GPLv2
9
10 The contents of this file are subject to the MIT license as set out below.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
21
22 Alternatively, the contents of this file may be used under the terms of
23 the GNU General Public License Version 2 ("GPL") in which case the provisions
24 of GPL are applicable instead of those above.
25
26 If you wish to allow use of your version of this file only under the terms of
27 GPL, and not to allow others to use your version of this file under the terms
28 of the MIT license, indicate your decision by deleting the provisions above
29 and replace them with the notice and other provisions required by GPL as set
30 out in the file called "GPL-COPYING" included in this distribution. If you do
31 not delete the provisions above, a recipient may use your version of this file
32 under the terms of either the MIT license or GPL.
33
34 This License is also included in this distribution in the file called
35 "MIT-COPYING".
36
37 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
38 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
39 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
40 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
41 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
42 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 */ /**************************************************************************/
45
46 #include <drm/drmP.h>
47 #include <linux/module.h>
48 #include <linux/platform_device.h>
49 #include <linux/version.h>
50
51 #include "module_common.h"
52 #include "pvr_drv.h"
53 #include "pvrmodule.h"
54 #include "sysinfo.h"
55 #include "pvrversion.h"  //add by zxl
56
57 #if defined(CONFIG_OF)
58 #include <linux/of.h>
59 #include <linux/of_device.h>
60 #endif
61
62 struct platform_device *gpsPVRLDMDev=NULL;
63
64 static struct drm_driver pvr_drm_platform_driver;
65
66 #if defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED)
67 /*
68  * This is an arbitrary value. If it's changed then the 'num_devices' module
69  * parameter description should also be updated to match.
70  */
71 #define MAX_DEVICES 16
72
73 static unsigned int pvr_num_devices = 1;
74 static struct platform_device **pvr_devices;
75
76 #if defined(NO_HARDWARE)
77 static int pvr_num_devices_set(const char *val,
78                                const struct kernel_param *param)
79 {
80         int err;
81
82         err = param_set_uint(val, param);
83         if (err)
84                 return err;
85
86         if (pvr_num_devices == 0 || pvr_num_devices > MAX_DEVICES)
87                 return -EINVAL;
88
89         return 0;
90 }
91
92 static const struct kernel_param_ops pvr_num_devices_ops = {
93         .set = pvr_num_devices_set,
94         .get = param_get_uint,
95 };
96
97 module_param_cb(num_devices, &pvr_num_devices_ops, &pvr_num_devices,
98                 S_IRUSR | S_IRGRP | S_IROTH);
99 MODULE_PARM_DESC(num_devices,
100                  "Number of platform devices to register (default: 1 - max: 16)");
101 #endif /* defined(NO_HARDWARE) */
102 #endif /* defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED) */
103
104 static int pvr_devices_register(void)
105 {
106 #if defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED)
107         struct platform_device_info pvr_dev_info = {
108                 .name = SYS_RGX_DEV_NAME,
109                 .id = -2,
110 #if defined(NO_HARDWARE)
111                 /* Not all cores have 40 bit physical support, but this
112                  * will work unless > 32 bit address is returned on those cores.
113                  * In the future this will be fixed more correctly.
114                  */
115                 .dma_mask = DMA_BIT_MASK(40),
116 #else
117                 .dma_mask = DMA_BIT_MASK(32),
118 #endif
119         };
120         unsigned int i;
121
122         BUG_ON(pvr_num_devices == 0 || pvr_num_devices > MAX_DEVICES);
123
124         pvr_devices = kmalloc_array(pvr_num_devices, sizeof(*pvr_devices),
125                                     GFP_KERNEL);
126         if (!pvr_devices)
127                 return -ENOMEM;
128
129         for (i = 0; i < pvr_num_devices; i++) {
130                 pvr_devices[i] = platform_device_register_full(&pvr_dev_info);
131                 if (IS_ERR(pvr_devices[i])) {
132                         DRM_ERROR("unable to register device %u (err=%ld)\n",
133                                   i, PTR_ERR(pvr_devices[i]));
134                         pvr_devices[i] = NULL;
135                         return -ENODEV;
136                 }
137         }
138 #endif /* defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED) */
139
140         return 0;
141 }
142
143 static void pvr_devices_unregister(void)
144 {
145 #if defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED)
146         unsigned int i;
147
148         BUG_ON(!pvr_devices);
149
150         for (i = 0; i < pvr_num_devices && pvr_devices[i]; i++)
151                 platform_device_unregister(pvr_devices[i]);
152
153         kfree(pvr_devices);
154         pvr_devices = NULL;
155 #endif /* defined(MODULE) && !defined(PVR_LDM_PLATFORM_PRE_REGISTERED) */
156 }
157
158 static int pvr_probe(struct platform_device *pdev)
159 {
160         DRM_DEBUG_DRIVER("device %p\n", &pdev->dev);
161
162         //zxl:print gpu version on boot time
163         printk("PVR_K: sys.gpvr.version=%s\n",RKVERSION);
164         gpsPVRLDMDev = pdev;
165
166         return drm_platform_init(&pvr_drm_platform_driver, pdev);
167 }
168
169 static int pvr_remove(struct platform_device *pdev)
170 {
171         struct drm_device *ddev = platform_get_drvdata(pdev);
172
173         DRM_DEBUG_DRIVER("device %p\n", &pdev->dev);
174
175         drm_put_dev(ddev);
176
177         return 0;
178 }
179
180 static void pvr_shutdown(struct platform_device *pdev)
181 {
182         struct drm_device *ddev = platform_get_drvdata(pdev);
183
184         DRM_DEBUG_DRIVER("device %p\n", &pdev->dev);
185
186         PVRSRVCommonDeviceShutdown(ddev->dev_private);
187 }
188
189 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
190 static const struct of_device_id pvr_of_ids[] = {
191 #if defined(SYS_RGX_OF_COMPATIBLE)
192         { .compatible = SYS_RGX_OF_COMPATIBLE, },
193 #endif
194        { .compatible = "arm,rogue-G6110", },
195        { .compatible = "arm,rk3368-gpu", },
196         {},
197 };
198
199 MODULE_DEVICE_TABLE(of, pvr_of_ids);
200 #endif
201
202 static struct platform_device_id pvr_platform_ids[] = {
203 #if defined(SYS_RGX_DEV_NAME)
204         { SYS_RGX_DEV_NAME, 0 },
205 #endif
206         { }
207 };
208 MODULE_DEVICE_TABLE(platform, pvr_platform_ids);
209
210 static struct platform_driver pvr_platform_driver = {
211         .driver = {
212                 .name           = DRVNAME,
213 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
214                 .of_match_table = of_match_ptr(pvr_of_ids),
215 #endif
216                 .pm             = &pvr_pm_ops,
217         },
218         .id_table               = pvr_platform_ids,
219         .probe                  = pvr_probe,
220         .remove                 = pvr_remove,
221         .shutdown               = pvr_shutdown,
222 };
223
224 static int __init pvr_init(void)
225 {
226         int err;
227         int i = 0;
228         struct device_node *np;
229
230         DRM_DEBUG_DRIVER("\n");
231
232        for_each_compatible_node(np, NULL, "arm,rogue-G6110") {
233                 i++;
234         }
235         if (0 == i) {
236                 printk("It doesn't contain Rogue gpu\n");
237                 return -1;
238         }
239
240         pvr_drm_platform_driver = pvr_drm_generic_driver;
241 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)) && \
242         (LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0))
243         pvr_drm_platform_driver.set_busid = drm_platform_set_busid;
244 #endif
245
246         err = PVRSRVCommonDriverInit();
247         if (err)
248                 return err;
249
250         err = platform_driver_register(&pvr_platform_driver);
251         if (err)
252                 return err;
253
254         return pvr_devices_register();
255 }
256
257 static void __exit pvr_exit(void)
258 {
259         DRM_DEBUG_DRIVER("\n");
260
261         pvr_devices_unregister();
262         platform_driver_unregister(&pvr_platform_driver);
263         PVRSRVCommonDriverDeinit();
264
265         DRM_DEBUG_DRIVER("done\n");
266 }
267
268 late_initcall(pvr_init);
269 module_exit(pvr_exit);