Loading...
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
27static 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
34static 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
41static 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
51static 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
63static 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 .compat_ioctl = drm_compat_ioctl,
69 .poll = drm_poll,
70 .read = drm_read,
71 .llseek = no_llseek,
72 .mmap = arcpgu_gem_mmap,
73};
74
75static void arcpgu_lastclose(struct drm_device *drm)
76{
77 struct arcpgu_drm_private *arcpgu = drm->dev_private;
78
79 drm_fbdev_cma_restore_mode(arcpgu->fbdev);
80}
81
82static int arcpgu_load(struct drm_device *drm)
83{
84 struct platform_device *pdev = to_platform_device(drm->dev);
85 struct arcpgu_drm_private *arcpgu;
86 struct device_node *encoder_node;
87 struct resource *res;
88 int ret;
89
90 arcpgu = devm_kzalloc(&pdev->dev, sizeof(*arcpgu), GFP_KERNEL);
91 if (arcpgu == NULL)
92 return -ENOMEM;
93
94 drm->dev_private = arcpgu;
95
96 arcpgu->clk = devm_clk_get(drm->dev, "pxlclk");
97 if (IS_ERR(arcpgu->clk))
98 return PTR_ERR(arcpgu->clk);
99
100 arcpgu_setup_mode_config(drm);
101
102 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
103 arcpgu->regs = devm_ioremap_resource(&pdev->dev, res);
104 if (IS_ERR(arcpgu->regs))
105 return PTR_ERR(arcpgu->regs);
106
107 dev_info(drm->dev, "arc_pgu ID: 0x%x\n",
108 arc_pgu_read(arcpgu, ARCPGU_REG_ID));
109
110 /* Get the optional framebuffer memory resource */
111 ret = of_reserved_mem_device_init(drm->dev);
112 if (ret && ret != -ENODEV)
113 return ret;
114
115 if (dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32)))
116 return -ENODEV;
117
118 if (arc_pgu_setup_crtc(drm) < 0)
119 return -ENODEV;
120
121 /* find the encoder node and initialize it */
122 encoder_node = of_parse_phandle(drm->dev->of_node, "encoder-slave", 0);
123 if (encoder_node) {
124 ret = arcpgu_drm_hdmi_init(drm, encoder_node);
125 of_node_put(encoder_node);
126 if (ret < 0)
127 return ret;
128 } else {
129 ret = arcpgu_drm_sim_init(drm, NULL);
130 if (ret < 0)
131 return ret;
132 }
133
134 drm_mode_config_reset(drm);
135 drm_kms_helper_poll_init(drm);
136
137 arcpgu->fbdev = drm_fbdev_cma_init(drm, 16,
138 drm->mode_config.num_crtc,
139 drm->mode_config.num_connector);
140 if (IS_ERR(arcpgu->fbdev)) {
141 ret = PTR_ERR(arcpgu->fbdev);
142 arcpgu->fbdev = NULL;
143 return -ENODEV;
144 }
145
146 platform_set_drvdata(pdev, arcpgu);
147 return 0;
148}
149
150static int arcpgu_unload(struct drm_device *drm)
151{
152 struct arcpgu_drm_private *arcpgu = drm->dev_private;
153
154 if (arcpgu->fbdev) {
155 drm_fbdev_cma_fini(arcpgu->fbdev);
156 arcpgu->fbdev = NULL;
157 }
158 drm_kms_helper_poll_fini(drm);
159 drm_vblank_cleanup(drm);
160 drm_mode_config_cleanup(drm);
161
162 return 0;
163}
164
165static struct drm_driver arcpgu_drm_driver = {
166 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
167 DRIVER_ATOMIC,
168 .lastclose = arcpgu_lastclose,
169 .name = "drm-arcpgu",
170 .desc = "ARC PGU Controller",
171 .date = "20160219",
172 .major = 1,
173 .minor = 0,
174 .patchlevel = 0,
175 .fops = &arcpgu_drm_ops,
176 .dumb_create = drm_gem_cma_dumb_create,
177 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
178 .dumb_destroy = drm_gem_dumb_destroy,
179 .get_vblank_counter = drm_vblank_no_hw_counter,
180 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
181 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
182 .gem_free_object_unlocked = drm_gem_cma_free_object,
183 .gem_vm_ops = &drm_gem_cma_vm_ops,
184 .gem_prime_export = drm_gem_prime_export,
185 .gem_prime_import = drm_gem_prime_import,
186 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
187 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
188 .gem_prime_vmap = drm_gem_cma_prime_vmap,
189 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
190 .gem_prime_mmap = drm_gem_cma_prime_mmap,
191};
192
193static int arcpgu_probe(struct platform_device *pdev)
194{
195 struct drm_device *drm;
196 int ret;
197
198 drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
199 if (IS_ERR(drm))
200 return PTR_ERR(drm);
201
202 ret = arcpgu_load(drm);
203 if (ret)
204 goto err_unref;
205
206 ret = drm_dev_register(drm, 0);
207 if (ret)
208 goto err_unload;
209
210 return 0;
211
212err_unload:
213 arcpgu_unload(drm);
214
215err_unref:
216 drm_dev_unref(drm);
217
218 return ret;
219}
220
221static int arcpgu_remove(struct platform_device *pdev)
222{
223 struct drm_device *drm = platform_get_drvdata(pdev);
224
225 drm_dev_unregister(drm);
226 arcpgu_unload(drm);
227 drm_dev_unref(drm);
228
229 return 0;
230}
231
232static const struct of_device_id arcpgu_of_table[] = {
233 {.compatible = "snps,arcpgu"},
234 {}
235};
236
237MODULE_DEVICE_TABLE(of, arcpgu_of_table);
238
239static struct platform_driver arcpgu_platform_driver = {
240 .probe = arcpgu_probe,
241 .remove = arcpgu_remove,
242 .driver = {
243 .name = "arcpgu",
244 .of_match_table = arcpgu_of_table,
245 },
246};
247
248module_platform_driver(arcpgu_platform_driver);
249
250MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
251MODULE_DESCRIPTION("ARC PGU DRM driver");
252MODULE_LICENSE("GPL");
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
27static 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
33static 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
43DEFINE_DRM_GEM_CMA_FOPS(arcpgu_drm_ops);
44
45static 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
113static 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
123static 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
136static struct drm_info_list arcpgu_debugfs_list[] = {
137 { "clocks", arcpgu_show_pxlclock, 0 },
138};
139
140static void arcpgu_debugfs_init(struct drm_minor *minor)
141{
142 drm_debugfs_create_files(arcpgu_debugfs_list,
143 ARRAY_SIZE(arcpgu_debugfs_list),
144 minor->debugfs_root, minor);
145}
146#endif
147
148static struct drm_driver arcpgu_drm_driver = {
149 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
150 .name = "arcpgu",
151 .desc = "ARC PGU Controller",
152 .date = "20160219",
153 .major = 1,
154 .minor = 0,
155 .patchlevel = 0,
156 .fops = &arcpgu_drm_ops,
157 DRM_GEM_CMA_DRIVER_OPS,
158#ifdef CONFIG_DEBUG_FS
159 .debugfs_init = arcpgu_debugfs_init,
160#endif
161};
162
163static int arcpgu_probe(struct platform_device *pdev)
164{
165 struct drm_device *drm;
166 int ret;
167
168 drm = drm_dev_alloc(&arcpgu_drm_driver, &pdev->dev);
169 if (IS_ERR(drm))
170 return PTR_ERR(drm);
171
172 ret = arcpgu_load(drm);
173 if (ret)
174 goto err_unref;
175
176 ret = drm_dev_register(drm, 0);
177 if (ret)
178 goto err_unload;
179
180 drm_fbdev_generic_setup(drm, 16);
181
182 return 0;
183
184err_unload:
185 arcpgu_unload(drm);
186
187err_unref:
188 drm_dev_put(drm);
189
190 return ret;
191}
192
193static int arcpgu_remove(struct platform_device *pdev)
194{
195 struct drm_device *drm = platform_get_drvdata(pdev);
196
197 drm_dev_unregister(drm);
198 arcpgu_unload(drm);
199 drm_dev_put(drm);
200
201 return 0;
202}
203
204static const struct of_device_id arcpgu_of_table[] = {
205 {.compatible = "snps,arcpgu"},
206 {}
207};
208
209MODULE_DEVICE_TABLE(of, arcpgu_of_table);
210
211static struct platform_driver arcpgu_platform_driver = {
212 .probe = arcpgu_probe,
213 .remove = arcpgu_remove,
214 .driver = {
215 .name = "arcpgu",
216 .of_match_table = arcpgu_of_table,
217 },
218};
219
220module_platform_driver(arcpgu_platform_driver);
221
222MODULE_AUTHOR("Carlos Palminha <palminha@synopsys.com>");
223MODULE_DESCRIPTION("ARC PGU DRM driver");
224MODULE_LICENSE("GPL");