Merge tag 'vfs-5.7-merge-1' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-microblaze.git] / drivers / gpu / drm / arc / arcpgu_drv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ARC PGU DRM driver.
4  *
5  * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
6  */
7
8 #include <linux/clk.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_debugfs.h>
11 #include <drm/drm_device.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_fb_cma_helper.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_gem_cma_helper.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_probe_helper.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/module.h>
21 #include <linux/of_reserved_mem.h>
22 #include <linux/platform_device.h>
23
24 #include "arcpgu.h"
25 #include "arcpgu_regs.h"
26
27 static const struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = {
28         .fb_create  = drm_gem_fb_create,
29         .atomic_check = drm_atomic_helper_check,
30         .atomic_commit = drm_atomic_helper_commit,
31 };
32
33 static void arcpgu_setup_mode_config(struct drm_device *drm)
34 {
35         drm_mode_config_init(drm);
36         drm->mode_config.min_width = 0;
37         drm->mode_config.min_height = 0;
38         drm->mode_config.max_width = 1920;
39         drm->mode_config.max_height = 1080;
40         drm->mode_config.funcs = &arcpgu_drm_modecfg_funcs;
41 }
42
43 DEFINE_DRM_GEM_CMA_FOPS(arcpgu_drm_ops);
44
45 static int arcpgu_load(struct drm_device *drm)
46 {
47         struct platform_device *pdev = to_platform_device(drm->dev);
48         struct arcpgu_drm_private *arcpgu;
49         struct device_node *encoder_node = NULL, *endpoint_node = NULL;
50         struct resource *res;
51         int ret;
52
53         arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL);
54         if (arcpgu == NULL)
55                 return -ENOMEM;
56
57         drm->dev_private = arcpgu;
58
59         arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
60         if (IS_ERR(arcpgu->clk))
61                 return PTR_ERR(arcpgu->clk);
62
63         arcpgu_setup_mode_config(drm);
64
65         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
66         arcpgu->regs = devm_ioremap_resource(&pdev->dev, res);
67         if (IS_ERR(arcpgu->regs))
68                 return PTR_ERR(arcpgu->regs);
69
70         dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
71                  arc_pgu_read(arcpgu, ARCPGU_REG_ID));
72
73         /* Get the optional framebuffer memory resource */
74         ret = of_reserved_mem_device_init(drm->dev);
75         if (ret && ret != -ENODEV)
76                 return ret;
77
78         if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
79                 return -ENODEV;
80
81         if (arc_pgu_setup_crtc(drm) < 0)
82                 return -ENODEV;
83
84         /*
85          * There is only one output port inside each device. It is linked with
86          * encoder endpoint.
87          */
88         endpoint_node = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
89         if (endpoint_node) {
90                 encoder_node = of_graph_get_remote_port_parent(endpoint_node);
91                 of_node_put(endpoint_node);
92         }
93
94         if (encoder_node) {
95                 ret = arcpgu_drm_hdmi_init(drm, encoder_node);
96                 of_node_put(encoder_node);
97                 if (ret < 0)
98                         return ret;
99         } else {
100                 dev_info(drm->dev, "no encoder found. Assumed virtual LCD on simulation platform\n");
101                 ret = arcpgu_drm_sim_init(drm, NULL);
102                 if (ret < 0)
103                         return ret;
104         }
105
106         drm_mode_config_reset(drm);
107         drm_kms_helper_poll_init(drm);
108
109         platform_set_drvdata(pdev, drm);
110         return 0;
111 }
112
113 static int arcpgu_unload(struct drm_device *drm)
114 {
115         drm_kms_helper_poll_fini(drm);
116         drm_atomic_helper_shutdown(drm);
117         drm_mode_config_cleanup(drm);
118
119         return 0;
120 }
121
122 #ifdef CONFIG_DEBUG_FS
123 static int arcpgu_show_pxlclock(struct seq_file *m, void *arg)
124 {
125         struct drm_info_node *node = (struct drm_info_node *)m->private;
126         struct drm_device *drm = node->minor->dev;
127         struct arcpgu_drm_private *arcpgu = drm->dev_private;
128         unsigned long clkrate = clk_get_rate(arcpgu->clk);
129         unsigned long mode_clock = arcpgu->crtc.mode.crtc_clock * 1000;
130
131         seq_printf(m, "hw  : %lu\n", clkrate);
132         seq_printf(m, "mode: %lu\n", mode_clock);
133         return 0;
134 }
135
136 static struct drm_info_list arcpgu_debugfs_list[] = {
137         { "clocks", arcpgu_show_pxlclock, 0 },
138 };
139
140 static int arcpgu_debugfs_init(struct drm_minor *minor)
141 {
142         return drm_debugfs_create_files(arcpgu_debugfs_list,
143                 ARRAY_SIZE(arcpgu_debugfs_list), minor->debugfs_root, minor);
144 }
145 #endif
146
147 static struct drm_driver arcpgu_drm_driver = {
148         .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
149         .name = "arcpgu",
150         .desc = "ARC PGU Controller",
151         .date = "20160219",
152         .major = 1,
153         .minor = 0,
154         .patchlevel = 0,
155         .fops = &arcpgu_drm_ops,
156         .dumb_create = drm_gem_cma_dumb_create,
157         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
158         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
159         .gem_free_object_unlocked = drm_gem_cma_free_object,
160         .gem_print_info = drm_gem_cma_print_info,
161         .gem_vm_ops = &drm_gem_cma_vm_ops,
162         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
163         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
164         .gem_prime_vmap = drm_gem_cma_prime_vmap,
165         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
166         .gem_prime_mmap = drm_gem_cma_prime_mmap,
167 #ifdef CONFIG_DEBUG_FS
168         .debugfs_init = arcpgu_debugfs_init,
169 #endif
170 };
171
172 static int arcpgu_probe(struct platform_device *pdev)
173 {
174         struct drm_device *drm;
175         int ret;
176
177         drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
178         if (IS_ERR(drm))
179                 return PTR_ERR(drm);
180
181         ret = arcpgu_load(drm);
182         if (ret)
183                 goto err_unref;
184
185         ret = drm_dev_register(drm, 0);
186         if (ret)
187                 goto err_unload;
188
189         drm_fbdev_generic_setup(drm, 16);
190
191         return 0;
192
193 err_unload:
194         arcpgu_unload(drm);
195
196 err_unref:
197         drm_dev_put(drm);
198
199         return ret;
200 }
201
202 static int arcpgu_remove(struct platform_device *pdev)
203 {
204         struct drm_device *drm = platform_get_drvdata(pdev);
205
206         drm_dev_unregister(drm);
207         arcpgu_unload(drm);
208         drm_dev_put(drm);
209
210         return 0;
211 }
212
213 static const struct of_device_id arcpgu_of_table[] = {
214         {.compatible = "snps,arcpgu"},
215         {}
216 };
217
218 MODULE_DEVICE_TABLE(of, arcpgu_of_table);
219
220 static struct platform_driver arcpgu_platform_driver = {
221         .probe = arcpgu_probe,
222         .remove = arcpgu_remove,
223         .driver = {
224                    .name = "arcpgu",
225                    .of_match_table = arcpgu_of_table,
226                    },
227 };
228
229 module_platform_driver(arcpgu_platform_driver);
230
231 MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
232 MODULE_DESCRIPTION("ARC PGU DRM driver");
233 MODULE_LICENSE("GPL");