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
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>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
19 #include <drm/drm_fb_cma_helper.h>
21 #include "sti_drm_drv.h"
22 #include "sti_drm_crtc.h"
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
30 #define STI_MAX_FB_HEIGHT 4096
31 #define STI_MAX_FB_WIDTH 4096
33 static void sti_drm_atomic_schedule(struct sti_drm_private
*private,
34 struct drm_atomic_state
*state
)
36 private->commit
.state
= state
;
37 schedule_work(&private->commit
.work
);
40 static void sti_drm_atomic_complete(struct sti_drm_private
*private,
41 struct drm_atomic_state
*state
)
43 struct drm_device
*drm
= private->drm_dev
;
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().
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
61 drm_atomic_helper_commit_modeset_disables(drm
, state
);
62 drm_atomic_helper_commit_planes(drm
, state
);
63 drm_atomic_helper_commit_modeset_enables(drm
, state
);
65 drm_atomic_helper_wait_for_vblanks(drm
, state
);
67 drm_atomic_helper_cleanup_planes(drm
, state
);
68 drm_atomic_state_free(state
);
71 static void sti_drm_atomic_work(struct work_struct
*work
)
73 struct sti_drm_private
*private = container_of(work
,
74 struct sti_drm_private
, commit
.work
);
76 sti_drm_atomic_complete(private, private->commit
.state
);
79 static int sti_drm_atomic_commit(struct drm_device
*drm
,
80 struct drm_atomic_state
*state
, bool async
)
82 struct sti_drm_private
*private = drm
->dev_private
;
85 err
= drm_atomic_helper_prepare_planes(drm
, state
);
89 /* serialize outstanding asynchronous commits */
90 mutex_lock(&private->commit
.lock
);
91 flush_work(&private->commit
.work
);
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.
99 drm_atomic_helper_swap_state(drm
, state
);
102 sti_drm_atomic_schedule(private, state
);
104 sti_drm_atomic_complete(private, state
);
106 mutex_unlock(&private->commit
.lock
);
110 static struct drm_mode_config_funcs sti_drm_mode_config_funcs
= {
111 .fb_create
= drm_fb_cma_create
,
112 .atomic_check
= drm_atomic_helper_check
,
113 .atomic_commit
= sti_drm_atomic_commit
,
116 static void sti_drm_mode_config_init(struct drm_device
*dev
)
118 dev
->mode_config
.min_width
= 0;
119 dev
->mode_config
.min_height
= 0;
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().
126 dev
->mode_config
.max_width
= STI_MAX_FB_HEIGHT
;
127 dev
->mode_config
.max_height
= STI_MAX_FB_WIDTH
;
129 dev
->mode_config
.funcs
= &sti_drm_mode_config_funcs
;
132 static int sti_drm_load(struct drm_device
*dev
, unsigned long flags
)
134 struct sti_drm_private
*private;
137 private = kzalloc(sizeof(struct sti_drm_private
), GFP_KERNEL
);
139 DRM_ERROR("Failed to allocate private\n");
142 dev
->dev_private
= (void *)private;
143 private->drm_dev
= dev
;
145 mutex_init(&private->commit
.lock
);
146 INIT_WORK(&private->commit
.work
, sti_drm_atomic_work
);
148 drm_mode_config_init(dev
);
149 drm_kms_helper_poll_init(dev
);
151 sti_drm_mode_config_init(dev
);
153 ret
= component_bind_all(dev
->dev
, dev
);
155 drm_kms_helper_poll_fini(dev
);
156 drm_mode_config_cleanup(dev
);
161 drm_mode_config_reset(dev
);
163 #ifdef CONFIG_DRM_STI_FBDEV
164 drm_fbdev_cma_init(dev
, 32,
165 dev
->mode_config
.num_crtc
,
166 dev
->mode_config
.num_connector
);
171 static const struct file_operations sti_drm_driver_fops
= {
172 .owner
= THIS_MODULE
,
174 .mmap
= drm_gem_cma_mmap
,
177 .unlocked_ioctl
= drm_ioctl
,
179 .compat_ioctl
= drm_compat_ioctl
,
181 .release
= drm_release
,
184 static struct dma_buf
*sti_drm_gem_prime_export(struct drm_device
*dev
,
185 struct drm_gem_object
*obj
,
188 /* we want to be able to write in mmapped buffer */
190 return drm_gem_prime_export(dev
, obj
, flags
);
193 static struct drm_driver sti_drm_driver
= {
194 .driver_features
= DRIVER_HAVE_IRQ
| DRIVER_MODESET
|
195 DRIVER_GEM
| DRIVER_PRIME
,
196 .load
= sti_drm_load
,
197 .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
,
202 .fops
= &sti_drm_driver_fops
,
204 .get_vblank_counter
= drm_vblank_count
,
205 .enable_vblank
= sti_drm_crtc_enable_vblank
,
206 .disable_vblank
= sti_drm_crtc_disable_vblank
,
208 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
209 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
210 .gem_prime_export
= sti_drm_gem_prime_export
,
211 .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
,
221 .major
= DRIVER_MAJOR
,
222 .minor
= DRIVER_MINOR
,
225 static int compare_of(struct device
*dev
, void *data
)
227 return dev
->of_node
== data
;
230 static int sti_drm_bind(struct device
*dev
)
232 return drm_platform_init(&sti_drm_driver
, to_platform_device(dev
));
235 static void sti_drm_unbind(struct device
*dev
)
237 drm_put_dev(dev_get_drvdata(dev
));
240 static const struct component_master_ops sti_drm_ops
= {
241 .bind
= sti_drm_bind
,
242 .unbind
= sti_drm_unbind
,
245 static int sti_drm_master_probe(struct platform_device
*pdev
)
247 struct device
*dev
= &pdev
->dev
;
248 struct device_node
*node
= dev
->parent
->of_node
;
249 struct device_node
*child_np
;
250 struct component_match
*match
= NULL
;
252 dma_set_coherent_mask(dev
, DMA_BIT_MASK(32));
254 child_np
= of_get_next_available_child(node
, NULL
);
257 component_match_add(dev
, &match
, compare_of
, child_np
);
258 of_node_put(child_np
);
259 child_np
= of_get_next_available_child(node
, child_np
);
262 return component_master_add_with_match(dev
, &sti_drm_ops
, match
);
265 static int sti_drm_master_remove(struct platform_device
*pdev
)
267 component_master_del(&pdev
->dev
, &sti_drm_ops
);
271 static struct platform_driver sti_drm_master_driver
= {
272 .probe
= sti_drm_master_probe
,
273 .remove
= sti_drm_master_remove
,
275 .name
= DRIVER_NAME
"__master",
279 static int sti_drm_platform_probe(struct platform_device
*pdev
)
281 struct device
*dev
= &pdev
->dev
;
282 struct device_node
*node
= dev
->of_node
;
283 struct platform_device
*master
;
285 of_platform_populate(node
, NULL
, NULL
, dev
);
287 platform_driver_register(&sti_drm_master_driver
);
288 master
= platform_device_register_resndata(dev
,
289 DRIVER_NAME
"__master", -1,
292 return PTR_ERR(master
);
294 platform_set_drvdata(pdev
, master
);
298 static int sti_drm_platform_remove(struct platform_device
*pdev
)
300 struct platform_device
*master
= platform_get_drvdata(pdev
);
302 of_platform_depopulate(&pdev
->dev
);
303 platform_device_unregister(master
);
304 platform_driver_unregister(&sti_drm_master_driver
);
308 static const struct of_device_id sti_drm_dt_ids
[] = {
309 { .compatible
= "st,sti-display-subsystem", },
312 MODULE_DEVICE_TABLE(of
, sti_drm_dt_ids
);
314 static struct platform_driver sti_drm_platform_driver
= {
315 .probe
= sti_drm_platform_probe
,
316 .remove
= sti_drm_platform_remove
,
319 .of_match_table
= sti_drm_dt_ids
,
323 module_platform_driver(sti_drm_platform_driver
);
325 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
326 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
327 MODULE_LICENSE("GPL");