Merge remote-tracking branch 'lightnvm/for-next'
[deliverable/linux.git] / drivers / gpu / drm / sti / sti_drv.c
CommitLineData
9bbf86fe
BG
1/*
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
de4b00b0
BG
15#include <drm/drm_atomic.h>
16#include <drm/drm_atomic_helper.h>
9bbf86fe
BG
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_gem_cma_helper.h>
19#include <drm/drm_fb_cma_helper.h>
20
9e1f05b2
VA
21#include "sti_crtc.h"
22#include "sti_drv.h"
bf8f9e4a 23#include "sti_plane.h"
9bbf86fe
BG
24
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
bf8f9e4a
VA
34static 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;
bf8f9e4a
VA
75
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
bf8f9e4a
VA
84 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
9e1f05b2
VA
152static void sti_atomic_schedule(struct sti_private *private,
153 struct drm_atomic_state *state)
de4b00b0
BG
154{
155 private->commit.state = state;
156 schedule_work(&private->commit.work);
157}
158
9e1f05b2
VA
159static void sti_atomic_complete(struct sti_private *private,
160 struct drm_atomic_state *state)
de4b00b0
BG
161{
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);
2b58e98d 181 drm_atomic_helper_commit_planes(drm, state, 0);
de4b00b0
BG
182 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
9e1f05b2 190static void sti_atomic_work(struct work_struct *work)
de4b00b0 191{
9e1f05b2
VA
192 struct sti_private *private = container_of(work,
193 struct sti_private, commit.work);
de4b00b0 194
9e1f05b2 195 sti_atomic_complete(private, private->commit.state);
de4b00b0
BG
196}
197
9e1f05b2 198static int sti_atomic_commit(struct drm_device *drm,
ab575184 199 struct drm_atomic_state *state, bool nonblock)
de4b00b0 200{
9e1f05b2 201 struct sti_private *private = drm->dev_private;
de4b00b0
BG
202 int err;
203
204 err = drm_atomic_helper_prepare_planes(drm, state);
205 if (err)
206 return err;
207
ab575184 208 /* serialize outstanding nonblocking commits */
de4b00b0
BG
209 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
5e84c269 218 drm_atomic_helper_swap_state(state, true);
de4b00b0 219
ab575184 220 if (nonblock)
9e1f05b2 221 sti_atomic_schedule(private, state);
de4b00b0 222 else
9e1f05b2 223 sti_atomic_complete(private, state);
de4b00b0
BG
224
225 mutex_unlock(&private->commit.lock);
226 return 0;
227}
228
84601dbd
BG
229static void sti_output_poll_changed(struct drm_device *ddev)
230{
231 struct sti_private *private = ddev->dev_private;
232
233 if (!ddev->mode_config.num_connector)
234 return;
235
236 if (private->fbdev) {
237 drm_fbdev_cma_hotplug_event(private->fbdev);
238 return;
239 }
240
241 private->fbdev = drm_fbdev_cma_init(ddev, 32,
242 ddev->mode_config.num_crtc,
243 ddev->mode_config.num_connector);
244 if (IS_ERR(private->fbdev))
245 private->fbdev = NULL;
246}
247
c5de4853 248static const struct drm_mode_config_funcs sti_mode_config_funcs = {
9bbf86fe 249 .fb_create = drm_fb_cma_create,
84601dbd 250 .output_poll_changed = sti_output_poll_changed,
de4b00b0 251 .atomic_check = drm_atomic_helper_check,
9e1f05b2 252 .atomic_commit = sti_atomic_commit,
9bbf86fe
BG
253};
254
9e1f05b2 255static void sti_mode_config_init(struct drm_device *dev)
9bbf86fe
BG
256{
257 dev->mode_config.min_width = 0;
258 dev->mode_config.min_height = 0;
259
260 /*
261 * set max width and height as default value.
262 * this value would be used to check framebuffer size limitation
263 * at drm_mode_addfb().
264 */
738be9d6
VA
265 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
266 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
9bbf86fe 267
9e1f05b2 268 dev->mode_config.funcs = &sti_mode_config_funcs;
9bbf86fe
BG
269}
270
9e1f05b2 271static const struct file_operations sti_driver_fops = {
9bbf86fe
BG
272 .owner = THIS_MODULE,
273 .open = drm_open,
274 .mmap = drm_gem_cma_mmap,
275 .poll = drm_poll,
276 .read = drm_read,
277 .unlocked_ioctl = drm_ioctl,
278#ifdef CONFIG_COMPAT
279 .compat_ioctl = drm_compat_ioctl,
280#endif
281 .release = drm_release,
282};
283
9e1f05b2 284static struct drm_driver sti_driver = {
e1f96ef4 285 .driver_features = DRIVER_MODESET |
f29ddaf1 286 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
d41ec9ca 287 .gem_free_object_unlocked = drm_gem_cma_free_object,
9bbf86fe
BG
288 .gem_vm_ops = &drm_gem_cma_vm_ops,
289 .dumb_create = drm_gem_cma_dumb_create,
290 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
291 .dumb_destroy = drm_gem_dumb_destroy,
9e1f05b2 292 .fops = &sti_driver_fops,
9bbf86fe 293
b44f8408 294 .get_vblank_counter = drm_vblank_no_hw_counter,
9e1f05b2
VA
295 .enable_vblank = sti_crtc_enable_vblank,
296 .disable_vblank = sti_crtc_disable_vblank,
9bbf86fe
BG
297
298 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
299 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
ffd157ce 300 .gem_prime_export = drm_gem_prime_export,
9bbf86fe
BG
301 .gem_prime_import = drm_gem_prime_import,
302 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
303 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
304 .gem_prime_vmap = drm_gem_cma_prime_vmap,
305 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
306 .gem_prime_mmap = drm_gem_cma_prime_mmap,
307
bf8f9e4a
VA
308 .debugfs_init = sti_drm_dbg_init,
309 .debugfs_cleanup = sti_drm_dbg_cleanup,
310
9bbf86fe
BG
311 .name = DRIVER_NAME,
312 .desc = DRIVER_DESC,
313 .date = DRIVER_DATE,
314 .major = DRIVER_MAJOR,
315 .minor = DRIVER_MINOR,
316};
317
318static int compare_of(struct device *dev, void *data)
319{
320 return dev->of_node == data;
321}
322
84601dbd
BG
323static int sti_init(struct drm_device *ddev)
324{
325 struct sti_private *private;
326
327 private = kzalloc(sizeof(*private), GFP_KERNEL);
328 if (!private)
329 return -ENOMEM;
330
331 ddev->dev_private = (void *)private;
332 dev_set_drvdata(ddev->dev, ddev);
333 private->drm_dev = ddev;
334
335 mutex_init(&private->commit.lock);
336 INIT_WORK(&private->commit.work, sti_atomic_work);
337
338 drm_mode_config_init(ddev);
339
340 sti_mode_config_init(ddev);
341
342 drm_kms_helper_poll_init(ddev);
343
344 return 0;
345}
346
347static void sti_cleanup(struct drm_device *ddev)
348{
349 struct sti_private *private = ddev->dev_private;
350
351 if (private->fbdev) {
352 drm_fbdev_cma_fini(private->fbdev);
353 private->fbdev = NULL;
354 }
355
356 drm_kms_helper_poll_fini(ddev);
357 drm_vblank_cleanup(ddev);
358 kfree(private);
359 ddev->dev_private = NULL;
360}
361
9e1f05b2 362static int sti_bind(struct device *dev)
9bbf86fe 363{
84601dbd
BG
364 struct drm_device *ddev;
365 int ret;
366
367 ddev = drm_dev_alloc(&sti_driver, dev);
368 if (!ddev)
369 return -ENOMEM;
370
371 ddev->platformdev = to_platform_device(dev);
372
373 ret = sti_init(ddev);
374 if (ret)
375 goto err_drm_dev_unref;
376
377 ret = component_bind_all(ddev->dev, ddev);
378 if (ret)
379 goto err_cleanup;
380
381 ret = drm_dev_register(ddev, 0);
382 if (ret)
383 goto err_register;
384
385 drm_mode_config_reset(ddev);
386
387 return 0;
388
389err_register:
390 drm_mode_config_cleanup(ddev);
391err_cleanup:
392 sti_cleanup(ddev);
393err_drm_dev_unref:
394 drm_dev_unref(ddev);
395 return ret;
9bbf86fe
BG
396}
397
9e1f05b2 398static void sti_unbind(struct device *dev)
9bbf86fe 399{
84601dbd
BG
400 struct drm_device *ddev = dev_get_drvdata(dev);
401
402 drm_dev_unregister(ddev);
403 sti_cleanup(ddev);
404 drm_dev_unref(ddev);
9bbf86fe
BG
405}
406
9e1f05b2
VA
407static const struct component_master_ops sti_ops = {
408 .bind = sti_bind,
409 .unbind = sti_unbind,
9bbf86fe
BG
410};
411
9e1f05b2 412static int sti_platform_probe(struct platform_device *pdev)
9bbf86fe
BG
413{
414 struct device *dev = &pdev->dev;
53bdcf5f 415 struct device_node *node = dev->of_node;
9bbf86fe
BG
416 struct device_node *child_np;
417 struct component_match *match = NULL;
418
419 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
420
53bdcf5f
BG
421 of_platform_populate(node, NULL, NULL, dev);
422
9bbf86fe
BG
423 child_np = of_get_next_available_child(node, NULL);
424
425 while (child_np) {
426 component_match_add(dev, &match, compare_of, child_np);
427 of_node_put(child_np);
428 child_np = of_get_next_available_child(node, child_np);
429 }
430
9e1f05b2 431 return component_master_add_with_match(dev, &sti_ops, match);
9bbf86fe
BG
432}
433
9e1f05b2 434static int sti_platform_remove(struct platform_device *pdev)
9bbf86fe 435{
9e1f05b2 436 component_master_del(&pdev->dev, &sti_ops);
9bbf86fe 437 of_platform_depopulate(&pdev->dev);
53bdcf5f 438
9bbf86fe
BG
439 return 0;
440}
441
9e1f05b2 442static const struct of_device_id sti_dt_ids[] = {
9bbf86fe
BG
443 { .compatible = "st,sti-display-subsystem", },
444 { /* end node */ },
445};
9e1f05b2 446MODULE_DEVICE_TABLE(of, sti_dt_ids);
9bbf86fe 447
9e1f05b2
VA
448static struct platform_driver sti_platform_driver = {
449 .probe = sti_platform_probe,
450 .remove = sti_platform_remove,
9bbf86fe 451 .driver = {
9bbf86fe 452 .name = DRIVER_NAME,
9e1f05b2 453 .of_match_table = sti_dt_ids,
9bbf86fe
BG
454 },
455};
456
dcec16ef
TR
457static struct platform_driver * const drivers[] = {
458 &sti_tvout_driver,
459 &sti_vtac_driver,
460 &sti_hqvdp_driver,
461 &sti_hdmi_driver,
462 &sti_hda_driver,
463 &sti_dvo_driver,
464 &sti_vtg_driver,
465 &sti_compositor_driver,
466 &sti_platform_driver,
467};
468
469static int sti_drm_init(void)
470{
471 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
472}
473module_init(sti_drm_init);
474
475static void sti_drm_exit(void)
476{
477 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
478}
479module_exit(sti_drm_exit);
9bbf86fe
BG
480
481MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
482MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
483MODULE_LICENSE("GPL");
This page took 0.113825 seconds and 5 git commands to generate.