blob: f3c22ded4b74ba9a1f9ccde32d51bb621159fabd [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"
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +020023
24#define DRIVER_NAME "sti"
25#define DRIVER_DESC "STMicroelectronics SoC DRM"
26#define DRIVER_DATE "20140601"
27#define DRIVER_MAJOR 1
28#define DRIVER_MINOR 0
29
30#define STI_MAX_FB_HEIGHT 4096
31#define STI_MAX_FB_WIDTH 4096
32
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020033static void sti_atomic_schedule(struct sti_private *private,
34 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010035{
36 private->commit.state = state;
37 schedule_work(&private->commit.work);
38}
39
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020040static void sti_atomic_complete(struct sti_private *private,
41 struct drm_atomic_state *state)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010042{
43 struct drm_device *drm = private->drm_dev;
44
45 /*
46 * Everything below can be run asynchronously without the need to grab
47 * any modeset locks at all under one condition: It must be guaranteed
48 * that the asynchronous work has either been cancelled (if the driver
49 * supports it, which at least requires that the framebuffers get
50 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
51 * before the new state gets committed on the software side with
52 * drm_atomic_helper_swap_state().
53 *
54 * This scheme allows new atomic state updates to be prepared and
55 * checked in parallel to the asynchronous completion of the previous
56 * update. Which is important since compositors need to figure out the
57 * composition of the next frame right after having submitted the
58 * current layout.
59 */
60
61 drm_atomic_helper_commit_modeset_disables(drm, state);
Daniel Vetteraef9dbb2015-09-08 12:02:07 +020062 drm_atomic_helper_commit_planes(drm, state, false);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010063 drm_atomic_helper_commit_modeset_enables(drm, state);
64
65 drm_atomic_helper_wait_for_vblanks(drm, state);
66
67 drm_atomic_helper_cleanup_planes(drm, state);
68 drm_atomic_state_free(state);
69}
70
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020071static void sti_atomic_work(struct work_struct *work)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010072{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020073 struct sti_private *private = container_of(work,
74 struct sti_private, commit.work);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010075
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020076 sti_atomic_complete(private, private->commit.state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010077}
78
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020079static int sti_atomic_commit(struct drm_device *drm,
80 struct drm_atomic_state *state, bool async)
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010081{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +020082 struct sti_private *private = drm->dev_private;
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +010083 int err;
84
85 err = drm_atomic_helper_prepare_planes(drm, state);
86 if (err)
87 return err;
88
89 /* serialize outstanding asynchronous commits */
90 mutex_lock(&private->commit.lock);
91 flush_work(&private->commit.work);
92
93 /*
94 * This is the point of no return - everything below never fails except
95 * when the hw goes bonghits. Which means we can commit the new state on
96 * the software side now.
97 */
98
99 drm_atomic_helper_swap_state(drm, state);
100
101 if (async)
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200102 sti_atomic_schedule(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100103 else
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200104 sti_atomic_complete(private, state);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100105
106 mutex_unlock(&private->commit.lock);
107 return 0;
108}
109
Ville Syrjäläc5de4852015-09-02 13:44:15 +0300110static const struct drm_mode_config_funcs sti_mode_config_funcs = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200111 .fb_create = drm_fb_cma_create,
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100112 .atomic_check = drm_atomic_helper_check,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200113 .atomic_commit = sti_atomic_commit,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200114};
115
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200116static void sti_mode_config_init(struct drm_device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200117{
118 dev->mode_config.min_width = 0;
119 dev->mode_config.min_height = 0;
120
121 /*
122 * set max width and height as default value.
123 * this value would be used to check framebuffer size limitation
124 * at drm_mode_addfb().
125 */
126 dev->mode_config.max_width = STI_MAX_FB_HEIGHT;
127 dev->mode_config.max_height = STI_MAX_FB_WIDTH;
128
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200129 dev->mode_config.funcs = &sti_mode_config_funcs;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200130}
131
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200132static int sti_load(struct drm_device *dev, unsigned long flags)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200133{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200134 struct sti_private *private;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200135 int ret;
136
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200137 private = kzalloc(sizeof(*private), GFP_KERNEL);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200138 if (!private) {
139 DRM_ERROR("Failed to allocate private\n");
140 return -ENOMEM;
141 }
142 dev->dev_private = (void *)private;
143 private->drm_dev = dev;
144
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100145 mutex_init(&private->commit.lock);
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200146 INIT_WORK(&private->commit.work, sti_atomic_work);
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100147
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200148 drm_mode_config_init(dev);
149 drm_kms_helper_poll_init(dev);
150
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200151 sti_mode_config_init(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200152
153 ret = component_bind_all(dev->dev, dev);
Benjamin Gaignardf78e7722014-12-11 13:35:29 +0100154 if (ret) {
155 drm_kms_helper_poll_fini(dev);
156 drm_mode_config_cleanup(dev);
157 kfree(private);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200158 return ret;
Benjamin Gaignardf78e7722014-12-11 13:35:29 +0100159 }
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200160
Benjamin Gaignardde4b00b2015-03-19 13:35:16 +0100161 drm_mode_config_reset(dev);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200162
163#ifdef CONFIG_DRM_STI_FBDEV
164 drm_fbdev_cma_init(dev, 32,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200165 dev->mode_config.num_crtc,
166 dev->mode_config.num_connector);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200167#endif
168 return 0;
169}
170
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200171static const struct file_operations sti_driver_fops = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200172 .owner = THIS_MODULE,
173 .open = drm_open,
174 .mmap = drm_gem_cma_mmap,
175 .poll = drm_poll,
176 .read = drm_read,
177 .unlocked_ioctl = drm_ioctl,
178#ifdef CONFIG_COMPAT
179 .compat_ioctl = drm_compat_ioctl,
180#endif
181 .release = drm_release,
182};
183
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200184static struct dma_buf *sti_gem_prime_export(struct drm_device *dev,
185 struct drm_gem_object *obj,
186 int flags)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200187{
188 /* we want to be able to write in mmapped buffer */
189 flags |= O_RDWR;
190 return drm_gem_prime_export(dev, obj, flags);
191}
192
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200193static struct drm_driver sti_driver = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200194 .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
195 DRIVER_GEM | DRIVER_PRIME,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200196 .load = sti_load,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200197 .gem_free_object = drm_gem_cma_free_object,
198 .gem_vm_ops = &drm_gem_cma_vm_ops,
199 .dumb_create = drm_gem_cma_dumb_create,
200 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
201 .dumb_destroy = drm_gem_dumb_destroy,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200202 .fops = &sti_driver_fops,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200203
Ville Syrjäläb44f8402015-09-30 16:46:48 +0300204 .get_vblank_counter = drm_vblank_no_hw_counter,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200205 .enable_vblank = sti_crtc_enable_vblank,
206 .disable_vblank = sti_crtc_disable_vblank,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200207
208 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
209 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200210 .gem_prime_export = sti_gem_prime_export,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200211 .gem_prime_import = drm_gem_prime_import,
212 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
213 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
214 .gem_prime_vmap = drm_gem_cma_prime_vmap,
215 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
216 .gem_prime_mmap = drm_gem_cma_prime_mmap,
217
218 .name = DRIVER_NAME,
219 .desc = DRIVER_DESC,
220 .date = DRIVER_DATE,
221 .major = DRIVER_MAJOR,
222 .minor = DRIVER_MINOR,
223};
224
225static int compare_of(struct device *dev, void *data)
226{
227 return dev->of_node == data;
228}
229
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200230static int sti_bind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200231{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200232 return drm_platform_init(&sti_driver, to_platform_device(dev));
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200233}
234
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200235static void sti_unbind(struct device *dev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200236{
237 drm_put_dev(dev_get_drvdata(dev));
238}
239
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200240static const struct component_master_ops sti_ops = {
241 .bind = sti_bind,
242 .unbind = sti_unbind,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200243};
244
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200245static int sti_platform_probe(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200246{
247 struct device *dev = &pdev->dev;
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200248 struct device_node *node = dev->of_node;
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200249 struct device_node *child_np;
250 struct component_match *match = NULL;
251
252 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
253
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200254 of_platform_populate(node, NULL, NULL, dev);
255
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200256 child_np = of_get_next_available_child(node, NULL);
257
258 while (child_np) {
259 component_match_add(dev, &match, compare_of, child_np);
260 of_node_put(child_np);
261 child_np = of_get_next_available_child(node, child_np);
262 }
263
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200264 return component_master_add_with_match(dev, &sti_ops, match);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200265}
266
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200267static int sti_platform_remove(struct platform_device *pdev)
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200268{
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200269 component_master_del(&pdev->dev, &sti_ops);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200270 of_platform_depopulate(&pdev->dev);
Benjamin Gaignard53bdcf52015-07-17 12:06:11 +0200271
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200272 return 0;
273}
274
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200275static const struct of_device_id sti_dt_ids[] = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200276 { .compatible = "st,sti-display-subsystem", },
277 { /* end node */ },
278};
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200279MODULE_DEVICE_TABLE(of, sti_dt_ids);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200280
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200281static struct platform_driver sti_platform_driver = {
282 .probe = sti_platform_probe,
283 .remove = sti_platform_remove,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200284 .driver = {
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200285 .name = DRIVER_NAME,
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200286 .of_match_table = sti_dt_ids,
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200287 },
288};
289
Vincent Abriou9e1f05b2015-07-31 11:32:34 +0200290module_platform_driver(sti_platform_driver);
Benjamin Gaignard9bbf86f2014-07-31 09:39:11 +0200291
292MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
293MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
294MODULE_LICENSE("GPL");