blob: dd2c400c4a46e2943f1ec0d49c41fae2ed2c8a34 [file] [log] [blame]
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +02001/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
5 */
6
7#include <drm/drmP.h>
8
9#include <linux/component.h>
10#include <linux/debugfs.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of_platform.h>
14
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010015#include <drm/drm_atomic.h>
16#include <drm/drm_atomic_helper.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020017#include <drm/drm_crtc_helper.h>
18#include <drm/drm_gem_cma_helper.h>
19#include <drm/drm_fb_cma_helper.h>
20
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020021#include "sti_crtc.h"
22#include "sti_drv.h"
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010023#include "sti_plane.h"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020024
25#define DRIVER_NAME "sti"
26#define DRIVER_DESC "STMicroelectronics SoC DRM"
27#define DRIVER_DATE "20140601"
28#define DRIVER_MAJOR 1
29#define DRIVER_MINOR 0
30
31#define STI_MAX_FB_HEIGHT 4096
32#define STI_MAX_FB_WIDTH 4096
33
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010034static int sti_drm_fps_get(void *data, u64 *val)
35{
36 struct drm_device *drm_dev = data;
37 struct drm_plane *p;
38 unsigned int i = 0;
39
40 *val = 0;
41 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
42 struct sti_plane *plane = to_sti_plane(p);
43
44 *val |= plane->fps_info.output << i;
45 i++;
46 }
47
48 return 0;
49}
50
51static int sti_drm_fps_set(void *data, u64 val)
52{
53 struct drm_device *drm_dev = data;
54 struct drm_plane *p;
55 unsigned int i = 0;
56
57 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
58 struct sti_plane *plane = to_sti_plane(p);
59
60 plane->fps_info.output = (val >> i) & 1;
61 i++;
62 }
63
64 return 0;
65}
66
67DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
68 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
69
70static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
71{
72 struct drm_info_node *node = s->private;
73 struct drm_device *dev = node->minor->dev;
74 struct drm_plane *p;
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010075
76 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
77 struct sti_plane *plane = to_sti_plane(p);
78
79 seq_printf(s, "%s%s\n",
80 plane->fps_info.fps_str,
81 plane->fps_info.fips_str);
82 }
83
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010084 return 0;
85}
86
87static struct drm_info_list sti_drm_dbg_list[] = {
88 {"fps_get", sti_drm_fps_dbg_show, 0},
89};
90
91static int sti_drm_debugfs_create(struct dentry *root,
92 struct drm_minor *minor,
93 const char *name,
94 const struct file_operations *fops)
95{
96 struct drm_device *dev = minor->dev;
97 struct drm_info_node *node;
98 struct dentry *ent;
99
100 ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops);
101 if (IS_ERR(ent))
102 return PTR_ERR(ent);
103
104 node = kmalloc(sizeof(*node), GFP_KERNEL);
105 if (!node) {
106 debugfs_remove(ent);
107 return -ENOMEM;
108 }
109
110 node->minor = minor;
111 node->dent = ent;
112 node->info_ent = (void *)fops;
113
114 mutex_lock(&minor->debugfs_lock);
115 list_add(&node->list, &minor->debugfs_list);
116 mutex_unlock(&minor->debugfs_lock);
117
118 return 0;
119}
120
121static int sti_drm_dbg_init(struct drm_minor *minor)
122{
123 int ret;
124
125 ret = drm_debugfs_create_files(sti_drm_dbg_list,
126 ARRAY_SIZE(sti_drm_dbg_list),
127 minor->debugfs_root, minor);
128 if (ret)
129 goto err;
130
131 ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show",
132 &sti_drm_fps_fops);
133 if (ret)
134 goto err;
135
136 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
137 return 0;
138err:
139 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
140 return ret;
141}
142
143void sti_drm_dbg_cleanup(struct drm_minor *minor)
144{
145 drm_debugfs_remove_files(sti_drm_dbg_list,
146 ARRAY_SIZE(sti_drm_dbg_list), minor);
147
148 drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops,
149 1, minor);
150}
151
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200152static void sti_atomic_schedule(struct sti_private *private,
153 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100154{
155 private->commit.state = state;
156 schedule_work(&private->commit.work);
157}
158
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200159static void sti_atomic_complete(struct sti_private *private,
160 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100161{
162 struct drm_device *drm = private->drm_dev;
163
164 /*
165 * Everything below can be run asynchronously without the need to grab
166 * any modeset locks at all under one condition: It must be guaranteed
167 * that the asynchronous work has either been cancelled (if the driver
168 * supports it, which at least requires that the framebuffers get
169 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
170 * before the new state gets committed on the software side with
171 * drm_atomic_helper_swap_state().
172 *
173 * This scheme allows new atomic state updates to be prepared and
174 * checked in parallel to the asynchronous completion of the previous
175 * update. Which is important since compositors need to figure out the
176 * composition of the next frame right after having submitted the
177 * current layout.
178 */
179
180 drm_atomic_helper_commit_modeset_disables(drm, state);
Daniel Vetteraef9dbb2015-09-08 12:02:07 +0200181 drm_atomic_helper_commit_planes(drm, state, false);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100182 drm_atomic_helper_commit_modeset_enables(drm, state);
183
184 drm_atomic_helper_wait_for_vblanks(drm, state);
185
186 drm_atomic_helper_cleanup_planes(drm, state);
187 drm_atomic_state_free(state);
188}
189
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200190static void sti_atomic_work(struct work_struct *work)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100191{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200192 struct sti_private *private = container_of(work,
193 struct sti_private, commit.work);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100194
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200195 sti_atomic_complete(private, private->commit.state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100196}
197
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200198static int sti_atomic_commit(struct drm_device *drm,
Maarten Lankhorstab575182016-04-26 16:11:41 +0200199 struct drm_atomic_state *state, bool nonblock)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100200{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200201 struct sti_private *private = drm->dev_private;
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100202 int err;
203
204 err = drm_atomic_helper_prepare_planes(drm, state);
205 if (err)
206 return err;
207
Maarten Lankhorstab575182016-04-26 16:11:41 +0200208 /* serialize outstanding nonblocking commits */
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100209 mutex_lock(&private->commit.lock);
210 flush_work(&private->commit.work);
211
212 /*
213 * This is the point of no return - everything below never fails except
214 * when the hw goes bonghits. Which means we can commit the new state on
215 * the software side now.
216 */
217
Daniel Vetter5e84c262016-06-10 00:06:32 +0200218 drm_atomic_helper_swap_state(state, true);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100219
Maarten Lankhorstab575182016-04-26 16:11:41 +0200220 if (nonblock)
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200221 sti_atomic_schedule(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100222 else
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200223 sti_atomic_complete(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100224
225 mutex_unlock(&private->commit.lock);
226 return 0;
227}
228
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300229static const struct drm_mode_config_funcs sti_mode_config_funcs = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200230 .fb_create = drm_fb_cma_create,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100231 .atomic_check = drm_atomic_helper_check,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200232 .atomic_commit = sti_atomic_commit,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200233};
234
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200235static void sti_mode_config_init(struct drm_device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200236{
237 dev->mode_config.min_width = 0;
238 dev->mode_config.min_height = 0;
239
240 /*
241 * set max width and height as default value.
242 * this value would be used to check framebuffer size limitation
243 * at drm_mode_addfb().
244 */
Vincent Abriou738be9d2015-10-29 14:20:27 +0100245 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
246 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200247
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200248 dev->mode_config.funcs = &sti_mode_config_funcs;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200249}
250
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200251static int sti_load(struct drm_device *dev, unsigned long flags)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200252{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200253 struct sti_private *private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200254 int ret;
255
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200256 private = kzalloc(sizeof(*private), GFP_KERNEL);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200257 if (!private) {
258 DRM_ERROR("Failed to allocate private\n");
259 return -ENOMEM;
260 }
261 dev->dev_private = (void *)private;
262 private->drm_dev = dev;
263
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100264 mutex_init(&private->commit.lock);
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200265 INIT_WORK(&private->commit.work, sti_atomic_work);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100266
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200267 drm_mode_config_init(dev);
268 drm_kms_helper_poll_init(dev);
269
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200270 sti_mode_config_init(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200271
272 ret = component_bind_all(dev->dev, dev);
Benjamin Gaignardf78e7722014-12-11 13:35:29 +0100273 if (ret) {
274 drm_kms_helper_poll_fini(dev);
275 drm_mode_config_cleanup(dev);
276 kfree(private);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200277 return ret;
Benjamin Gaignardf78e7722014-12-11 13:35:29 +0100278 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200279
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100280 drm_mode_config_reset(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200281
Maxime Ripard4314e192016-01-14 16:24:56 +0100282 drm_helper_disable_unused_functions(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200283 drm_fbdev_cma_init(dev, 32,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200284 dev->mode_config.num_crtc,
285 dev->mode_config.num_connector);
Archit Tanejab5d34a22015-10-27 13:40:58 +0530286
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200287 return 0;
288}
289
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200290static const struct file_operations sti_driver_fops = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200291 .owner = THIS_MODULE,
292 .open = drm_open,
293 .mmap = drm_gem_cma_mmap,
294 .poll = drm_poll,
295 .read = drm_read,
296 .unlocked_ioctl = drm_ioctl,
297#ifdef CONFIG_COMPAT
298 .compat_ioctl = drm_compat_ioctl,
299#endif
300 .release = drm_release,
301};
302
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200303static struct drm_driver sti_driver = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200304 .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
benjamin.gaignard@linaro.orgf29ddaf2016-01-07 14:30:41 +0100305 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200306 .load = sti_load,
Daniel Vetterd41ec9c2016-05-30 19:53:11 +0200307 .gem_free_object_unlocked = drm_gem_cma_free_object,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200308 .gem_vm_ops = &drm_gem_cma_vm_ops,
309 .dumb_create = drm_gem_cma_dumb_create,
310 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
311 .dumb_destroy = drm_gem_dumb_destroy,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200312 .fops = &sti_driver_fops,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200313
Ville Syrjäläb44f8402015-09-30 16:46:48 +0300314 .get_vblank_counter = drm_vblank_no_hw_counter,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200315 .enable_vblank = sti_crtc_enable_vblank,
316 .disable_vblank = sti_crtc_disable_vblank,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200317
318 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
319 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Benjamin Gaignardffd157c2016-02-26 11:19:06 +0100320 .gem_prime_export = drm_gem_prime_export,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200321 .gem_prime_import = drm_gem_prime_import,
322 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
323 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
324 .gem_prime_vmap = drm_gem_cma_prime_vmap,
325 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
326 .gem_prime_mmap = drm_gem_cma_prime_mmap,
327
Vincent Abrioubf8f9e42016-02-08 11:34:31 +0100328 .debugfs_init = sti_drm_dbg_init,
329 .debugfs_cleanup = sti_drm_dbg_cleanup,
330
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200331 .name = DRIVER_NAME,
332 .desc = DRIVER_DESC,
333 .date = DRIVER_DATE,
334 .major = DRIVER_MAJOR,
335 .minor = DRIVER_MINOR,
336};
337
338static int compare_of(struct device *dev, void *data)
339{
340 return dev->of_node == data;
341}
342
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200343static int sti_bind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200344{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200345 return drm_platform_init(&sti_driver, to_platform_device(dev));
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200346}
347
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200348static void sti_unbind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200349{
350 drm_put_dev(dev_get_drvdata(dev));
351}
352
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200353static const struct component_master_ops sti_ops = {
354 .bind = sti_bind,
355 .unbind = sti_unbind,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200356};
357
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200358static int sti_platform_probe(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200359{
360 struct device *dev = &pdev->dev;
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200361 struct device_node *node = dev->of_node;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200362 struct device_node *child_np;
363 struct component_match *match = NULL;
364
365 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
366
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200367 of_platform_populate(node, NULL, NULL, dev);
368
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200369 child_np = of_get_next_available_child(node, NULL);
370
371 while (child_np) {
372 component_match_add(dev, &match, compare_of, child_np);
373 of_node_put(child_np);
374 child_np = of_get_next_available_child(node, child_np);
375 }
376
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200377 return component_master_add_with_match(dev, &sti_ops, match);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200378}
379
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200380static int sti_platform_remove(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200381{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200382 component_master_del(&pdev->dev, &sti_ops);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200383 of_platform_depopulate(&pdev->dev);
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200384
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200385 return 0;
386}
387
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200388static const struct of_device_id sti_dt_ids[] = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200389 { .compatible = "st,sti-display-subsystem", },
390 { /* end node */ },
391};
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200392MODULE_DEVICE_TABLE(of, sti_dt_ids);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200393
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200394static struct platform_driver sti_platform_driver = {
395 .probe = sti_platform_probe,
396 .remove = sti_platform_remove,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200397 .driver = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200398 .name = DRIVER_NAME,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200399 .of_match_table = sti_dt_ids,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200400 },
401};
402
Thierry Redingdcec16e2015-09-24 19:02:40 +0200403static struct platform_driver * const drivers[] = {
404 &sti_tvout_driver,
405 &sti_vtac_driver,
406 &sti_hqvdp_driver,
407 &sti_hdmi_driver,
408 &sti_hda_driver,
409 &sti_dvo_driver,
410 &sti_vtg_driver,
411 &sti_compositor_driver,
412 &sti_platform_driver,
413};
414
415static int sti_drm_init(void)
416{
417 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
418}
419module_init(sti_drm_init);
420
421static void sti_drm_exit(void)
422{
423 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
424}
425module_exit(sti_drm_exit);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200426
427MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
428MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
429MODULE_LICENSE("GPL");