MAINTAINERS: mmc: Move the mmc tree to kernel.org
[cascardo/linux.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 static int arcpgu_gem_mmap(struct file *filp, struct vm_area_struct *vma)
52 {
53         int ret;
54
55         ret = drm_gem_mmap(filp, vma);
56         if (ret)
57                 return ret;
58
59         vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
60         return 0;
61 }
62
63 static const struct file_operations arcpgu_drm_ops = {
64         .owner = THIS_MODULE,
65         .open = drm_open,
66         .release = drm_release,
67         .unlocked_ioctl = drm_ioctl,
68 #ifdef CONFIG_COMPAT
69         .compat_ioctl = drm_compat_ioctl,
70 #endif
71         .poll = drm_poll,
72         .read = drm_read,
73         .llseek = no_llseek,
74         .mmap = arcpgu_gem_mmap,
75 };
76
77 static void arcpgu_lastclose(struct drm_device *drm)
78 {
79         struct arcpgu_drm_private *arcpgu = drm->dev_private;
80
81         drm_fbdev_cma_restore_mode(arcpgu->fbdev);
82 }
83
84 static int arcpgu_load(struct drm_device *drm)
85 {
86         struct platform_device *pdev = to_platform_device(drm->dev);
87         struct arcpgu_drm_private *arcpgu;
88         struct device_node *encoder_node;
89         struct resource *res;
90         int ret;
91
92         arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL);
93         if (arcpgu == NULL)
94                 return -ENOMEM;
95
96         drm->dev_private = arcpgu;
97
98         arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
99         if (IS_ERR(arcpgu->clk))
100                 return PTR_ERR(arcpgu->clk);
101
102         arcpgu_setup_mode_config(drm);
103
104         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105         arcpgu->regs = devm_ioremap_resource(&pdev->dev, res);
106         if (IS_ERR(arcpgu->regs))
107                 return PTR_ERR(arcpgu->regs);
108
109         dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
110                  arc_pgu_read(arcpgu, ARCPGU_REG_ID));
111
112         /* Get the optional framebuffer memory resource */
113         ret = of_reserved_mem_device_init(drm->dev);
114         if (ret && ret != -ENODEV)
115                 return ret;
116
117         if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
118                 return -ENODEV;
119
120         if (arc_pgu_setup_crtc(drm) < 0)
121                 return -ENODEV;
122
123         /* find the encoder node and initialize it */
124         encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0);
125         if (encoder_node) {
126                 ret = arcpgu_drm_hdmi_init(drm, encoder_node);
127                 of_node_put(encoder_node);
128                 if (ret < 0)
129                         return ret;
130         } else {
131                 ret = arcpgu_drm_sim_init(drm, NULL);
132                 if (ret < 0)
133                         return ret;
134         }
135
136         drm_mode_config_reset(drm);
137         drm_kms_helper_poll_init(drm);
138
139         arcpgu->fbdev = drm_fbdev_cma_init(drm, 16,
140                                               drm->mode_config.num_crtc,
141                                               drm->mode_config.num_connector);
142         if (IS_ERR(arcpgu->fbdev)) {
143                 ret = PTR_ERR(arcpgu->fbdev);
144                 arcpgu->fbdev = NULL;
145                 return -ENODEV;
146         }
147
148         platform_set_drvdata(pdev, arcpgu);
149         return 0;
150 }
151
152 static int arcpgu_unload(struct drm_device *drm)
153 {
154         struct arcpgu_drm_private *arcpgu = drm->dev_private;
155
156         if (arcpgu->fbdev) {
157                 drm_fbdev_cma_fini(arcpgu->fbdev);
158                 arcpgu->fbdev = NULL;
159         }
160         drm_kms_helper_poll_fini(drm);
161         drm_vblank_cleanup(drm);
162         drm_mode_config_cleanup(drm);
163
164         return 0;
165 }
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 = "drm-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         .get_vblank_counter = drm_vblank_no_hw_counter,
182         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
183         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
184         .gem_free_object_unlocked = drm_gem_cma_free_object,
185         .gem_vm_ops = &drm_gem_cma_vm_ops,
186         .gem_prime_export = drm_gem_prime_export,
187         .gem_prime_import = drm_gem_prime_import,
188         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
189         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
190         .gem_prime_vmap = drm_gem_cma_prime_vmap,
191         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
192         .gem_prime_mmap = drm_gem_cma_prime_mmap,
193 };
194
195 static int arcpgu_probe(struct platform_device *pdev)
196 {
197         struct drm_device *drm;
198         int ret;
199
200         drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
201         if (!drm)
202                 return -ENOMEM;
203
204         ret = arcpgu_load(drm);
205         if (ret)
206                 goto err_unref;
207
208         ret = drm_dev_register(drm, 0);
209         if (ret)
210                 goto err_unload;
211
212         return 0;
213
214 err_unload:
215         arcpgu_unload(drm);
216
217 err_unref:
218         drm_dev_unref(drm);
219
220         return ret;
221 }
222
223 static int arcpgu_remove(struct platform_device *pdev)
224 {
225         struct drm_device *drm = platform_get_drvdata(pdev);
226
227         drm_dev_unregister(drm);
228         arcpgu_unload(drm);
229         drm_dev_unref(drm);
230
231         return 0;
232 }
233
234 static const struct of_device_id arcpgu_of_table[] = {
235         {.compatible = "snps,arcpgu"},
236         {}
237 };
238
239 MODULE_DEVICE_TABLE(of, arcpgu_of_table);
240
241 static struct platform_driver arcpgu_platform_driver = {
242         .probe = arcpgu_probe,
243         .remove = arcpgu_remove,
244         .driver = {
245                    .name = "arcpgu",
246                    .of_match_table = arcpgu_of_table,
247                    },
248 };
249
250 module_platform_driver(arcpgu_platform_driver);
251
252 MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
253 MODULE_DESCRIPTION("ARC PGU DRM driver");
254 MODULE_LICENSE("GPL");