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>
20 #include <drm/drm_of.h>
24 #include "sti_plane.h"
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
32 #define STI_MAX_FB_HEIGHT 4096
33 #define STI_MAX_FB_WIDTH 4096
35 static int sti_drm_fps_get(void *data
, u64
*val
)
37 struct drm_device
*drm_dev
= data
;
42 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
43 struct sti_plane
*plane
= to_sti_plane(p
);
45 *val
|= plane
->fps_info
.output
<< i
;
52 static int sti_drm_fps_set(void *data
, u64 val
)
54 struct drm_device
*drm_dev
= data
;
58 list_for_each_entry(p
, &drm_dev
->mode_config
.plane_list
, head
) {
59 struct sti_plane
*plane
= to_sti_plane(p
);
61 memset(&plane
->fps_info
, 0, sizeof(plane
->fps_info
));
62 plane
->fps_info
.output
= (val
>> i
) & 1;
70 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops
,
71 sti_drm_fps_get
, sti_drm_fps_set
, "%llu\n");
73 static int sti_drm_fps_dbg_show(struct seq_file
*s
, void *data
)
75 struct drm_info_node
*node
= s
->private;
76 struct drm_device
*dev
= node
->minor
->dev
;
79 list_for_each_entry(p
, &dev
->mode_config
.plane_list
, head
) {
80 struct sti_plane
*plane
= to_sti_plane(p
);
82 seq_printf(s
, "%s%s\n",
83 plane
->fps_info
.fps_str
,
84 plane
->fps_info
.fips_str
);
90 static struct drm_info_list sti_drm_dbg_list
[] = {
91 {"fps_get", sti_drm_fps_dbg_show
, 0},
94 static int sti_drm_dbg_init(struct drm_minor
*minor
)
96 struct dentry
*dentry
;
99 ret
= drm_debugfs_create_files(sti_drm_dbg_list
,
100 ARRAY_SIZE(sti_drm_dbg_list
),
101 minor
->debugfs_root
, minor
);
105 dentry
= debugfs_create_file("fps_show", S_IRUGO
| S_IWUSR
,
106 minor
->debugfs_root
, minor
->dev
,
113 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME
);
116 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME
);
120 static int sti_atomic_check(struct drm_device
*dev
,
121 struct drm_atomic_state
*state
)
125 ret
= drm_atomic_helper_check_modeset(dev
, state
);
129 ret
= drm_atomic_normalize_zpos(dev
, state
);
133 ret
= drm_atomic_helper_check_planes(dev
, state
);
140 static void sti_output_poll_changed(struct drm_device
*ddev
)
142 struct sti_private
*private = ddev
->dev_private
;
144 drm_fbdev_cma_hotplug_event(private->fbdev
);
147 static const struct drm_mode_config_funcs sti_mode_config_funcs
= {
148 .fb_create
= drm_fb_cma_create
,
149 .output_poll_changed
= sti_output_poll_changed
,
150 .atomic_check
= sti_atomic_check
,
151 .atomic_commit
= drm_atomic_helper_commit
,
154 static void sti_mode_config_init(struct drm_device
*dev
)
156 dev
->mode_config
.min_width
= 0;
157 dev
->mode_config
.min_height
= 0;
160 * set max width and height as default value.
161 * this value would be used to check framebuffer size limitation
162 * at drm_mode_addfb().
164 dev
->mode_config
.max_width
= STI_MAX_FB_WIDTH
;
165 dev
->mode_config
.max_height
= STI_MAX_FB_HEIGHT
;
167 dev
->mode_config
.funcs
= &sti_mode_config_funcs
;
170 DEFINE_DRM_GEM_CMA_FOPS(sti_driver_fops
);
172 static struct drm_driver sti_driver
= {
173 .driver_features
= DRIVER_MODESET
|
174 DRIVER_GEM
| DRIVER_PRIME
| DRIVER_ATOMIC
,
175 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
176 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
177 .dumb_create
= drm_gem_cma_dumb_create
,
178 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
179 .dumb_destroy
= drm_gem_dumb_destroy
,
180 .fops
= &sti_driver_fops
,
182 .enable_vblank
= sti_crtc_enable_vblank
,
183 .disable_vblank
= sti_crtc_disable_vblank
,
185 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
186 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
187 .gem_prime_export
= drm_gem_prime_export
,
188 .gem_prime_import
= drm_gem_prime_import
,
189 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
190 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
191 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
192 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
193 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
195 .debugfs_init
= sti_drm_dbg_init
,
200 .major
= DRIVER_MAJOR
,
201 .minor
= DRIVER_MINOR
,
204 static int compare_of(struct device
*dev
, void *data
)
206 return dev
->of_node
== data
;
209 static int sti_init(struct drm_device
*ddev
)
211 struct sti_private
*private;
213 private = kzalloc(sizeof(*private), GFP_KERNEL
);
217 ddev
->dev_private
= (void *)private;
218 dev_set_drvdata(ddev
->dev
, ddev
);
219 private->drm_dev
= ddev
;
221 drm_mode_config_init(ddev
);
223 sti_mode_config_init(ddev
);
225 drm_kms_helper_poll_init(ddev
);
230 static void sti_cleanup(struct drm_device
*ddev
)
232 struct sti_private
*private = ddev
->dev_private
;
234 if (private->fbdev
) {
235 drm_fbdev_cma_fini(private->fbdev
);
236 private->fbdev
= NULL
;
239 drm_kms_helper_poll_fini(ddev
);
240 drm_vblank_cleanup(ddev
);
241 component_unbind_all(ddev
->dev
, ddev
);
243 ddev
->dev_private
= NULL
;
246 static int sti_bind(struct device
*dev
)
248 struct drm_device
*ddev
;
249 struct sti_private
*private;
250 struct drm_fbdev_cma
*fbdev
;
253 ddev
= drm_dev_alloc(&sti_driver
, dev
);
255 return PTR_ERR(ddev
);
257 ret
= sti_init(ddev
);
259 goto err_drm_dev_unref
;
261 ret
= component_bind_all(ddev
->dev
, ddev
);
265 ret
= drm_dev_register(ddev
, 0);
269 drm_mode_config_reset(ddev
);
271 private = ddev
->dev_private
;
272 if (ddev
->mode_config
.num_connector
) {
273 fbdev
= drm_fbdev_cma_init(ddev
, 32,
274 ddev
->mode_config
.num_connector
);
276 DRM_DEBUG_DRIVER("Warning: fails to create fbdev\n");
279 private->fbdev
= fbdev
;
285 drm_mode_config_cleanup(ddev
);
293 static void sti_unbind(struct device
*dev
)
295 struct drm_device
*ddev
= dev_get_drvdata(dev
);
297 drm_dev_unregister(ddev
);
302 static const struct component_master_ops sti_ops
= {
304 .unbind
= sti_unbind
,
307 static int sti_platform_probe(struct platform_device
*pdev
)
309 struct device
*dev
= &pdev
->dev
;
310 struct device_node
*node
= dev
->of_node
;
311 struct device_node
*child_np
;
312 struct component_match
*match
= NULL
;
314 dma_set_coherent_mask(dev
, DMA_BIT_MASK(32));
316 devm_of_platform_populate(dev
);
318 child_np
= of_get_next_available_child(node
, NULL
);
321 drm_of_component_match_add(dev
, &match
, compare_of
,
323 child_np
= of_get_next_available_child(node
, child_np
);
326 return component_master_add_with_match(dev
, &sti_ops
, match
);
329 static int sti_platform_remove(struct platform_device
*pdev
)
331 component_master_del(&pdev
->dev
, &sti_ops
);
336 static const struct of_device_id sti_dt_ids
[] = {
337 { .compatible
= "st,sti-display-subsystem", },
340 MODULE_DEVICE_TABLE(of
, sti_dt_ids
);
342 static struct platform_driver sti_platform_driver
= {
343 .probe
= sti_platform_probe
,
344 .remove
= sti_platform_remove
,
347 .of_match_table
= sti_dt_ids
,
351 static struct platform_driver
* const drivers
[] = {
358 &sti_compositor_driver
,
359 &sti_platform_driver
,
362 static int sti_drm_init(void)
364 return platform_register_drivers(drivers
, ARRAY_SIZE(drivers
));
366 module_init(sti_drm_init
);
368 static void sti_drm_exit(void)
370 platform_unregister_drivers(drivers
, ARRAY_SIZE(drivers
));
372 module_exit(sti_drm_exit
);
374 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
375 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
376 MODULE_LICENSE("GPL");