drm: arcpgu: Fix module unload
[sfrench/cifs-2.6.git] / drivers / gpu / drm / arc / arcpgu_drv.c
1 /*
2  * ARC PGU DRM driver.
3  *
4  * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/clk.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <linux/of_reserved_mem.h>
23
24 #include "arcpgu.h"
25 #include "arcpgu_regs.h"
26
27 static void arcpgu_fb_output_poll_changed(struct drm_device *dev)
28 {
29         struct arcpgu_drm_private *arcpgu = dev->dev_private;
30
31         drm_fbdev_cma_hotplug_event(arcpgu->fbdev);
32 }
33
34 static struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = {
35         .fb_create  = drm_fb_cma_create,
36         .output_poll_changed = arcpgu_fb_output_poll_changed,
37         .atomic_check = drm_atomic_helper_check,
38         .atomic_commit = drm_atomic_helper_commit,
39 };
40
41 static void arcpgu_setup_mode_config(struct drm_device *drm)
42 {
43         drm_mode_config_init(drm);
44         drm->mode_config.min_width = 0;
45         drm->mode_config.min_height = 0;
46         drm->mode_config.max_width = 1920;
47         drm->mode_config.max_height = 1080;
48         drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs;
49 }
50
51 DEFINE_DRM_GEM_CMA_FOPS(arcpgu_drm_ops);
52
53 static void arcpgu_lastclose(struct drm_device *drm)
54 {
55         struct arcpgu_drm_private *arcpgu = drm->dev_private;
56
57         drm_fbdev_cma_restore_mode(arcpgu->fbdev);
58 }
59
60 static int arcpgu_load(struct drm_device *drm)
61 {
62         struct platform_device *pdev = to_platform_device(drm->dev);
63         struct arcpgu_drm_private *arcpgu;
64         struct device_node *encoder_node;
65         struct resource *res;
66         int ret;
67
68         arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL);
69         if (arcpgu == NULL)
70                 return -ENOMEM;
71
72         drm->dev_private = arcpgu;
73
74         arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
75         if (IS_ERR(arcpgu->clk))
76                 return PTR_ERR(arcpgu->clk);
77
78         arcpgu_setup_mode_config(drm);
79
80         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
81         arcpgu->regs = devm_ioremap_resource(&pdev->dev, res);
82         if (IS_ERR(arcpgu->regs))
83                 return PTR_ERR(arcpgu->regs);
84
85         dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
86                  arc_pgu_read(arcpgu, ARCPGU_REG_ID));
87
88         /* Get the optional framebuffer memory resource */
89         ret = of_reserved_mem_device_init(drm->dev);
90         if (ret && ret != -ENODEV)
91                 return ret;
92
93         if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
94                 return -ENODEV;
95
96         if (arc_pgu_setup_crtc(drm) < 0)
97                 return -ENODEV;
98
99         /* find the encoder node and initialize it */
100         encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0);
101         if (encoder_node) {
102                 ret = arcpgu_drm_hdmi_init(drm, encoder_node);
103                 of_node_put(encoder_node);
104                 if (ret < 0)
105                         return ret;
106         } else {
107                 ret = arcpgu_drm_sim_init(drm, NULL);
108                 if (ret < 0)
109                         return ret;
110         }
111
112         drm_mode_config_reset(drm);
113         drm_kms_helper_poll_init(drm);
114
115         arcpgu->fbdev = drm_fbdev_cma_init(drm, 16,
116                                            drm->mode_config.num_connector);
117         if (IS_ERR(arcpgu->fbdev)) {
118                 ret = PTR_ERR(arcpgu->fbdev);
119                 arcpgu->fbdev = NULL;
120                 return -ENODEV;
121         }
122
123         platform_set_drvdata(pdev, drm);
124         return 0;
125 }
126
127 static int arcpgu_unload(struct drm_device *drm)
128 {
129         struct arcpgu_drm_private *arcpgu = drm->dev_private;
130
131         if (arcpgu->fbdev) {
132                 drm_fbdev_cma_fini(arcpgu->fbdev);
133                 arcpgu->fbdev = NULL;
134         }
135         drm_kms_helper_poll_fini(drm);
136         drm_mode_config_cleanup(drm);
137
138         return 0;
139 }
140
141 #ifdef CONFIG_DEBUG_FS
142 static int arcpgu_show_pxlclock(struct seq_file *m, void *arg)
143 {
144         struct drm_info_node *node = (struct drm_info_node *)m->private;
145         struct drm_device *drm = node->minor->dev;
146         struct arcpgu_drm_private *arcpgu = drm->dev_private;
147         unsigned long clkrate = clk_get_rate(arcpgu->clk);
148         unsigned long mode_clock = arcpgu->crtc.mode.crtc_clock * 1000;
149
150         seq_printf(m, "hw  : %lu\n", clkrate);
151         seq_printf(m, "mode: %lu\n", mode_clock);
152         return 0;
153 }
154
155 static struct drm_info_list arcpgu_debugfs_list[] = {
156         { "clocks", arcpgu_show_pxlclock, 0 },
157         { "fb", drm_fb_cma_debugfs_show, 0 },
158 };
159
160 static int arcpgu_debugfs_init(struct drm_minor *minor)
161 {
162         return drm_debugfs_create_files(arcpgu_debugfs_list,
163                 ARRAY_SIZE(arcpgu_debugfs_list), minor->debugfs_root, minor);
164 }
165 #endif
166
167 static struct drm_driver arcpgu_drm_driver = {
168         .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
169                            DRIVER_ATOMIC,
170         .lastclose = arcpgu_lastclose,
171         .name = "arcpgu",
172         .desc = "ARC PGU Controller",
173         .date = "20160219",
174         .major = 1,
175         .minor = 0,
176         .patchlevel = 0,
177         .fops = &arcpgu_drm_ops,
178         .dumb_create = drm_gem_cma_dumb_create,
179         .dumb_map_offset = drm_gem_cma_dumb_map_offset,
180         .dumb_destroy = drm_gem_dumb_destroy,
181         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
182         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
183         .gem_free_object_unlocked = drm_gem_cma_free_object,
184         .gem_vm_ops = &drm_gem_cma_vm_ops,
185         .gem_prime_export = drm_gem_prime_export,
186         .gem_prime_import = drm_gem_prime_import,
187         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
188         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
189         .gem_prime_vmap = drm_gem_cma_prime_vmap,
190         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
191         .gem_prime_mmap = drm_gem_cma_prime_mmap,
192 #ifdef CONFIG_DEBUG_FS
193         .debugfs_init = arcpgu_debugfs_init,
194 #endif
195 };
196
197 static int arcpgu_probe(struct platform_device *pdev)
198 {
199         struct drm_device *drm;
200         int ret;
201
202         drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
203         if (IS_ERR(drm))
204                 return PTR_ERR(drm);
205
206         ret = arcpgu_load(drm);
207         if (ret)
208                 goto err_unref;
209
210         ret = drm_dev_register(drm, 0);
211         if (ret)
212                 goto err_unload;
213
214         return 0;
215
216 err_unload:
217         arcpgu_unload(drm);
218
219 err_unref:
220         drm_dev_unref(drm);
221
222         return ret;
223 }
224
225 static int arcpgu_remove(struct platform_device *pdev)
226 {
227         struct drm_device *drm = platform_get_drvdata(pdev);
228
229         drm_dev_unregister(drm);
230         arcpgu_unload(drm);
231         drm_dev_unref(drm);
232
233         return 0;
234 }
235
236 static const struct of_device_id arcpgu_of_table[] = {
237         {.compatible = "snps,arcpgu"},
238         {}
239 };
240
241 MODULE_DEVICE_TABLE(of, arcpgu_of_table);
242
243 static struct platform_driver arcpgu_platform_driver = {
244         .probe = arcpgu_probe,
245         .remove = arcpgu_remove,
246         .driver = {
247                    .name = "arcpgu",
248                    .of_match_table = arcpgu_of_table,
249                    },
250 };
251
252 module_platform_driver(arcpgu_platform_driver);
253
254 MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
255 MODULE_DESCRIPTION("ARC PGU DRM driver");
256 MODULE_LICENSE("GPL");