blob: ff71e25ab5bf30cb288c607fd5e03391172e0d8a [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>
Russell King97ac0e42016-10-19 11:28:27 +010020#include <drm/drm_of.h>
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020021
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020022#include "sti_crtc.h"
23#include "sti_drv.h"
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010024#include "sti_plane.h"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020025
26#define DRIVER_NAME "sti"
27#define DRIVER_DESC "STMicroelectronics SoC DRM"
28#define DRIVER_DATE "20140601"
29#define DRIVER_MAJOR 1
30#define DRIVER_MINOR 0
31
32#define STI_MAX_FB_HEIGHT 4096
33#define STI_MAX_FB_WIDTH 4096
34
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010035static int sti_drm_fps_get(void *data, u64 *val)
36{
37 struct drm_device *drm_dev = data;
38 struct drm_plane *p;
39 unsigned int i = 0;
40
41 *val = 0;
42 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
43 struct sti_plane *plane = to_sti_plane(p);
44
45 *val |= plane->fps_info.output << i;
46 i++;
47 }
48
49 return 0;
50}
51
52static int sti_drm_fps_set(void *data, u64 val)
53{
54 struct drm_device *drm_dev = data;
55 struct drm_plane *p;
56 unsigned int i = 0;
57
58 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
59 struct sti_plane *plane = to_sti_plane(p);
60
61 plane->fps_info.output = (val >> i) & 1;
62 i++;
63 }
64
65 return 0;
66}
67
68DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
69 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
70
71static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
72{
73 struct drm_info_node *node = s->private;
74 struct drm_device *dev = node->minor->dev;
75 struct drm_plane *p;
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010076
77 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
78 struct sti_plane *plane = to_sti_plane(p);
79
80 seq_printf(s, "%s%s\n",
81 plane->fps_info.fps_str,
82 plane->fps_info.fips_str);
83 }
84
Vincent Abrioubf8f9e42016-02-08 11:34:31 +010085 return 0;
86}
87
88static struct drm_info_list sti_drm_dbg_list[] = {
89 {"fps_get", sti_drm_fps_dbg_show, 0},
90};
91
92static int sti_drm_debugfs_create(struct dentry *root,
93 struct drm_minor *minor,
94 const char *name,
95 const struct file_operations *fops)
96{
97 struct drm_device *dev = minor->dev;
98 struct drm_info_node *node;
99 struct dentry *ent;
100
101 ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops);
102 if (IS_ERR(ent))
103 return PTR_ERR(ent);
104
105 node = kmalloc(sizeof(*node), GFP_KERNEL);
106 if (!node) {
107 debugfs_remove(ent);
108 return -ENOMEM;
109 }
110
111 node->minor = minor;
112 node->dent = ent;
113 node->info_ent = (void *)fops;
114
115 mutex_lock(&minor->debugfs_lock);
116 list_add(&node->list, &minor->debugfs_list);
117 mutex_unlock(&minor->debugfs_lock);
118
119 return 0;
120}
121
122static int sti_drm_dbg_init(struct drm_minor *minor)
123{
124 int ret;
125
126 ret = drm_debugfs_create_files(sti_drm_dbg_list,
127 ARRAY_SIZE(sti_drm_dbg_list),
128 minor->debugfs_root, minor);
129 if (ret)
130 goto err;
131
132 ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show",
133 &sti_drm_fps_fops);
134 if (ret)
135 goto err;
136
137 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
138 return 0;
139err:
140 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME);
141 return ret;
142}
143
Ville Syrjäläbdfd36e2016-09-19 16:33:53 +0300144static void sti_drm_dbg_cleanup(struct drm_minor *minor)
Vincent Abrioubf8f9e42016-02-08 11:34:31 +0100145{
146 drm_debugfs_remove_files(sti_drm_dbg_list,
147 ARRAY_SIZE(sti_drm_dbg_list), minor);
148
149 drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops,
150 1, minor);
151}
152
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200153static void sti_atomic_schedule(struct sti_private *private,
154 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100155{
156 private->commit.state = state;
157 schedule_work(&private->commit.work);
158}
159
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200160static void sti_atomic_complete(struct sti_private *private,
161 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100162{
163 struct drm_device *drm = private->drm_dev;
164
165 /*
166 * Everything below can be run asynchronously without the need to grab
167 * any modeset locks at all under one condition: It must be guaranteed
168 * that the asynchronous work has either been cancelled (if the driver
169 * supports it, which at least requires that the framebuffers get
170 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
171 * before the new state gets committed on the software side with
172 * drm_atomic_helper_swap_state().
173 *
174 * This scheme allows new atomic state updates to be prepared and
175 * checked in parallel to the asynchronous completion of the previous
176 * update. Which is important since compositors need to figure out the
177 * composition of the next frame right after having submitted the
178 * current layout.
179 */
180
181 drm_atomic_helper_commit_modeset_disables(drm, state);
Liu Ying2b58e982016-08-29 17:12:03 +0800182 drm_atomic_helper_commit_planes(drm, state, 0);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100183 drm_atomic_helper_commit_modeset_enables(drm, state);
184
185 drm_atomic_helper_wait_for_vblanks(drm, state);
186
187 drm_atomic_helper_cleanup_planes(drm, state);
Chris Wilson08536952016-10-14 13:18:18 +0100188 drm_atomic_state_put(state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100189}
190
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200191static void sti_atomic_work(struct work_struct *work)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100192{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200193 struct sti_private *private = container_of(work,
194 struct sti_private, commit.work);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100195
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200196 sti_atomic_complete(private, private->commit.state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100197}
198
Ville Syrjälä38d868e2016-10-10 17:50:56 +0300199static int sti_atomic_check(struct drm_device *dev,
200 struct drm_atomic_state *state)
201{
202 int ret;
203
204 ret = drm_atomic_helper_check_modeset(dev, state);
205 if (ret)
206 return ret;
207
208 ret = drm_atomic_normalize_zpos(dev, state);
209 if (ret)
210 return ret;
211
212 ret = drm_atomic_helper_check_planes(dev, state);
213 if (ret)
214 return ret;
215
216 return ret;
217}
218
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200219static int sti_atomic_commit(struct drm_device *drm,
Maarten Lankhorstab575182016-04-26 16:11:41 +0200220 struct drm_atomic_state *state, bool nonblock)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100221{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200222 struct sti_private *private = drm->dev_private;
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100223 int err;
224
225 err = drm_atomic_helper_prepare_planes(drm, state);
226 if (err)
227 return err;
228
Maarten Lankhorstab575182016-04-26 16:11:41 +0200229 /* serialize outstanding nonblocking commits */
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100230 mutex_lock(&private->commit.lock);
231 flush_work(&private->commit.work);
232
233 /*
234 * This is the point of no return - everything below never fails except
235 * when the hw goes bonghits. Which means we can commit the new state on
236 * the software side now.
237 */
238
Daniel Vetter5e84c262016-06-10 00:06:32 +0200239 drm_atomic_helper_swap_state(state, true);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100240
Chris Wilson08536952016-10-14 13:18:18 +0100241 drm_atomic_state_get(state);
Maarten Lankhorstab575182016-04-26 16:11:41 +0200242 if (nonblock)
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200243 sti_atomic_schedule(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100244 else
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200245 sti_atomic_complete(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100246
247 mutex_unlock(&private->commit.lock);
248 return 0;
249}
250
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200251static void sti_output_poll_changed(struct drm_device *ddev)
252{
253 struct sti_private *private = ddev->dev_private;
254
255 if (!ddev->mode_config.num_connector)
256 return;
257
258 if (private->fbdev) {
259 drm_fbdev_cma_hotplug_event(private->fbdev);
260 return;
261 }
262
263 private->fbdev = drm_fbdev_cma_init(ddev, 32,
264 ddev->mode_config.num_crtc,
265 ddev->mode_config.num_connector);
266 if (IS_ERR(private->fbdev))
267 private->fbdev = NULL;
268}
269
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300270static const struct drm_mode_config_funcs sti_mode_config_funcs = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200271 .fb_create = drm_fb_cma_create,
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200272 .output_poll_changed = sti_output_poll_changed,
Ville Syrjälä38d868e2016-10-10 17:50:56 +0300273 .atomic_check = sti_atomic_check,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200274 .atomic_commit = sti_atomic_commit,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200275};
276
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200277static void sti_mode_config_init(struct drm_device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200278{
279 dev->mode_config.min_width = 0;
280 dev->mode_config.min_height = 0;
281
282 /*
283 * set max width and height as default value.
284 * this value would be used to check framebuffer size limitation
285 * at drm_mode_addfb().
286 */
Vincent Abriou738be9d2015-10-29 14:20:27 +0100287 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
288 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200289
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200290 dev->mode_config.funcs = &sti_mode_config_funcs;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200291}
292
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200293static const struct file_operations sti_driver_fops = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200294 .owner = THIS_MODULE,
295 .open = drm_open,
296 .mmap = drm_gem_cma_mmap,
297 .poll = drm_poll,
298 .read = drm_read,
299 .unlocked_ioctl = drm_ioctl,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200300 .compat_ioctl = drm_compat_ioctl,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200301 .release = drm_release,
302};
303
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200304static struct drm_driver sti_driver = {
Shawn Guoe1f96ef2016-08-16 15:06:08 +0800305 .driver_features = DRIVER_MODESET |
benjamin.gaignard@linaro.orgf29ddaf2016-01-07 14:30:41 +0100306 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
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
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200343static int sti_init(struct drm_device *ddev)
344{
345 struct sti_private *private;
346
347 private = kzalloc(sizeof(*private), GFP_KERNEL);
348 if (!private)
349 return -ENOMEM;
350
351 ddev->dev_private = (void *)private;
352 dev_set_drvdata(ddev->dev, ddev);
353 private->drm_dev = ddev;
354
355 mutex_init(&private->commit.lock);
356 INIT_WORK(&private->commit.work, sti_atomic_work);
357
358 drm_mode_config_init(ddev);
359
360 sti_mode_config_init(ddev);
361
362 drm_kms_helper_poll_init(ddev);
363
364 return 0;
365}
366
367static void sti_cleanup(struct drm_device *ddev)
368{
369 struct sti_private *private = ddev->dev_private;
370
371 if (private->fbdev) {
372 drm_fbdev_cma_fini(private->fbdev);
373 private->fbdev = NULL;
374 }
375
376 drm_kms_helper_poll_fini(ddev);
377 drm_vblank_cleanup(ddev);
378 kfree(private);
379 ddev->dev_private = NULL;
380}
381
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200382static int sti_bind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200383{
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200384 struct drm_device *ddev;
385 int ret;
386
387 ddev = drm_dev_alloc(&sti_driver, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200388 if (IS_ERR(ddev))
389 return PTR_ERR(ddev);
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200390
391 ddev->platformdev = to_platform_device(dev);
392
393 ret = sti_init(ddev);
394 if (ret)
395 goto err_drm_dev_unref;
396
397 ret = component_bind_all(ddev->dev, ddev);
398 if (ret)
399 goto err_cleanup;
400
401 ret = drm_dev_register(ddev, 0);
402 if (ret)
403 goto err_register;
404
405 drm_mode_config_reset(ddev);
406
407 return 0;
408
409err_register:
410 drm_mode_config_cleanup(ddev);
411err_cleanup:
412 sti_cleanup(ddev);
413err_drm_dev_unref:
414 drm_dev_unref(ddev);
415 return ret;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200416}
417
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200418static void sti_unbind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200419{
Benjamin Gaignard84601db2016-06-21 15:09:40 +0200420 struct drm_device *ddev = dev_get_drvdata(dev);
421
422 drm_dev_unregister(ddev);
423 sti_cleanup(ddev);
424 drm_dev_unref(ddev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200425}
426
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200427static const struct component_master_ops sti_ops = {
428 .bind = sti_bind,
429 .unbind = sti_unbind,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200430};
431
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200432static int sti_platform_probe(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200433{
434 struct device *dev = &pdev->dev;
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200435 struct device_node *node = dev->of_node;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200436 struct device_node *child_np;
437 struct component_match *match = NULL;
438
439 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
440
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200441 of_platform_populate(node, NULL, NULL, dev);
442
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200443 child_np = of_get_next_available_child(node, NULL);
444
445 while (child_np) {
Russell King97ac0e42016-10-19 11:28:27 +0100446 drm_of_component_match_add(dev, &match, compare_of,
447 child_np);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200448 child_np = of_get_next_available_child(node, child_np);
449 }
450
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200451 return component_master_add_with_match(dev, &sti_ops, match);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200452}
453
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200454static int sti_platform_remove(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200455{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200456 component_master_del(&pdev->dev, &sti_ops);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200457 of_platform_depopulate(&pdev->dev);
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200458
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200459 return 0;
460}
461
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200462static const struct of_device_id sti_dt_ids[] = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200463 { .compatible = "st,sti-display-subsystem", },
464 { /* end node */ },
465};
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200466MODULE_DEVICE_TABLE(of, sti_dt_ids);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200467
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200468static struct platform_driver sti_platform_driver = {
469 .probe = sti_platform_probe,
470 .remove = sti_platform_remove,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200471 .driver = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200472 .name = DRIVER_NAME,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200473 .of_match_table = sti_dt_ids,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200474 },
475};
476
Thierry Redingdcec16e2015-09-24 19:02:40 +0200477static struct platform_driver * const drivers[] = {
478 &sti_tvout_driver,
479 &sti_vtac_driver,
480 &sti_hqvdp_driver,
481 &sti_hdmi_driver,
482 &sti_hda_driver,
483 &sti_dvo_driver,
484 &sti_vtg_driver,
485 &sti_compositor_driver,
486 &sti_platform_driver,
487};
488
489static int sti_drm_init(void)
490{
491 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
492}
493module_init(sti_drm_init);
494
495static void sti_drm_exit(void)
496{
497 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
498}
499module_exit(sti_drm_exit);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200500
501MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
502MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
503MODULE_LICENSE("GPL");