2019-05-29 17:17:56 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2014-08-22 14:36:26 +04:00
/*
* Copyright ( C ) Fuzhou Rockchip Electronics Co . Ltd
* Author : Mark Yao < mark . yao @ rock - chips . com >
*
* based on exynos_drm_drv . c
*/
# include <linux/dma-mapping.h>
2016-06-24 05:13:32 +03:00
# include <linux/dma-iommu.h>
2014-08-22 14:36:26 +04:00
# include <linux/pm_runtime.h>
2015-05-02 03:02:30 +03:00
# include <linux/module.h>
2014-08-22 14:36:26 +04:00
# include <linux/of_graph.h>
2018-08-31 00:12:05 +03:00
# include <linux/of_platform.h>
2014-08-22 14:36:26 +04:00
# include <linux/component.h>
2016-06-06 17:53:32 +03:00
# include <linux/console.h>
2016-06-24 05:13:32 +03:00
# include <linux/iommu.h>
2014-08-22 14:36:26 +04:00
2021-05-16 10:48:33 +03:00
# include <drm/drm_aperture.h>
2019-07-16 09:42:19 +03:00
# include <drm/drm_drv.h>
# include <drm/drm_fb_helper.h>
# include <drm/drm_gem_cma_helper.h>
# include <drm/drm_of.h>
# include <drm/drm_probe_helper.h>
# include <drm/drm_vblank.h>
2014-08-22 14:36:26 +04:00
# include "rockchip_drm_drv.h"
# include "rockchip_drm_fb.h"
# include "rockchip_drm_fbdev.h"
# include "rockchip_drm_gem.h"
# define DRIVER_NAME "rockchip"
# define DRIVER_DESC "RockChip Soc DRM"
# define DRIVER_DATE "20140818"
# define DRIVER_MAJOR 1
# define DRIVER_MINOR 0
2016-04-19 05:13:27 +03:00
static bool is_support_iommu = true ;
2020-11-04 13:04:24 +03:00
static const struct drm_driver rockchip_drm_driver ;
2016-04-19 05:13:27 +03:00
2014-08-22 14:36:26 +04:00
/*
* Attach a ( component ) device to the shared drm dma mapping from master drm
* device . This is used by the VOPs to map GEM buffers to a common DMA
* mapping .
*/
int rockchip_drm_dma_attach_device ( struct drm_device * drm_dev ,
struct device * dev )
{
2016-06-24 05:13:32 +03:00
struct rockchip_drm_private * private = drm_dev - > dev_private ;
2014-08-22 14:36:26 +04:00
int ret ;
2016-04-19 05:13:27 +03:00
if ( ! is_support_iommu )
return 0 ;
2016-06-24 05:13:32 +03:00
ret = iommu_attach_device ( private - > domain , dev ) ;
if ( ret ) {
2017-09-15 11:36:03 +03:00
DRM_DEV_ERROR ( dev , " Failed to attach iommu device \n " ) ;
2014-08-22 14:36:26 +04:00
return ret ;
2016-06-24 05:13:32 +03:00
}
2014-08-22 14:36:26 +04:00
2016-06-24 05:13:32 +03:00
return 0 ;
2014-08-22 14:36:26 +04:00
}
void rockchip_drm_dma_detach_device ( struct drm_device * drm_dev ,
struct device * dev )
{
2016-06-24 05:13:32 +03:00
struct rockchip_drm_private * private = drm_dev - > dev_private ;
struct iommu_domain * domain = private - > domain ;
2016-04-19 05:13:27 +03:00
if ( ! is_support_iommu )
return ;
2016-06-24 05:13:32 +03:00
iommu_detach_device ( domain , dev ) ;
2014-08-22 14:36:26 +04:00
}
2016-06-24 05:13:32 +03:00
static int rockchip_drm_init_iommu ( struct drm_device * drm_dev )
{
struct rockchip_drm_private * private = drm_dev - > dev_private ;
struct iommu_domain_geometry * geometry ;
u64 start , end ;
if ( ! is_support_iommu )
return 0 ;
private - > domain = iommu_domain_alloc ( & platform_bus_type ) ;
if ( ! private - > domain )
return - ENOMEM ;
geometry = & private - > domain - > geometry ;
start = geometry - > aperture_start ;
end = geometry - > aperture_end ;
DRM_DEBUG ( " IOMMU context initialized (aperture: %#llx-%#llx) \n " ,
start , end ) ;
drm_mm_init ( & private - > mm , start , end - start + 1 ) ;
mutex_init ( & private - > mm_lock ) ;
return 0 ;
}
static void rockchip_iommu_cleanup ( struct drm_device * drm_dev )
{
struct rockchip_drm_private * private = drm_dev - > dev_private ;
if ( ! is_support_iommu )
return ;
drm_mm_takedown ( & private - > mm ) ;
iommu_domain_free ( private - > domain ) ;
2014-08-22 14:36:26 +04:00
}
2016-06-10 14:14:13 +03:00
static int rockchip_drm_bind ( struct device * dev )
2014-08-22 14:36:26 +04:00
{
2016-06-10 14:14:13 +03:00
struct drm_device * drm_dev ;
2014-08-22 14:36:26 +04:00
struct rockchip_drm_private * private ;
int ret ;
2021-05-16 10:48:33 +03:00
/* Remove existing drivers that may own the framebuffer memory. */
ret = drm_aperture_remove_framebuffers ( false , " rockchip-drm-fb " ) ;
if ( ret ) {
DRM_DEV_ERROR ( dev ,
" Failed to remove existing framebuffers - %d. \n " ,
ret ) ;
return ret ;
}
2016-06-10 14:14:13 +03:00
drm_dev = drm_dev_alloc ( & rockchip_drm_driver , dev ) ;
2016-09-21 17:59:19 +03:00
if ( IS_ERR ( drm_dev ) )
return PTR_ERR ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
2016-06-10 14:14:13 +03:00
dev_set_drvdata ( dev , drm_dev ) ;
private = devm_kzalloc ( drm_dev - > dev , sizeof ( * private ) , GFP_KERNEL ) ;
if ( ! private ) {
ret = - ENOMEM ;
2016-06-21 07:27:34 +03:00
goto err_free ;
2016-06-10 14:14:13 +03:00
}
2014-08-22 14:36:26 +04:00
drm_dev - > dev_private = private ;
2016-07-24 09:57:44 +03:00
INIT_LIST_HEAD ( & private - > psr_list ) ;
2018-03-06 01:22:54 +03:00
mutex_init ( & private - > psr_list_lock ) ;
2016-07-24 09:57:44 +03:00
2017-04-06 15:31:23 +03:00
ret = rockchip_drm_init_iommu ( drm_dev ) ;
if ( ret )
goto err_free ;
2020-03-23 17:49:36 +03:00
ret = drmm_mode_config_init ( drm_dev ) ;
if ( ret )
goto err_iommu_cleanup ;
2014-08-22 14:36:26 +04:00
rockchip_drm_mode_config_init ( drm_dev ) ;
/* Try to bind all sub drivers. */
ret = component_bind_all ( dev , drm_dev ) ;
if ( ret )
2020-03-23 17:49:36 +03:00
goto err_iommu_cleanup ;
2014-08-22 14:36:26 +04:00
2017-04-06 15:31:23 +03:00
ret = drm_vblank_init ( drm_dev , drm_dev - > mode_config . num_crtc ) ;
if ( ret )
goto err_unbind_all ;
drm_mode_config_reset ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
/*
* enable drm irq mode .
* - with irq_enabled = true , we can use the vblank feature .
*/
drm_dev - > irq_enabled = true ;
ret = rockchip_drm_fbdev_init ( drm_dev ) ;
if ( ret )
2017-08-01 11:11:43 +03:00
goto err_unbind_all ;
/* init kms poll for handling hpd */
drm_kms_helper_poll_init ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
2016-06-21 07:27:34 +03:00
ret = drm_dev_register ( drm_dev , 0 ) ;
if ( ret )
2017-08-01 11:11:43 +03:00
goto err_kms_helper_poll_fini ;
2016-06-21 07:27:34 +03:00
2014-08-22 14:36:26 +04:00
return 0 ;
err_kms_helper_poll_fini :
drm_kms_helper_poll_fini ( drm_dev ) ;
2017-08-01 11:11:43 +03:00
rockchip_drm_fbdev_fini ( drm_dev ) ;
2017-04-06 15:31:23 +03:00
err_unbind_all :
2014-08-22 14:36:26 +04:00
component_unbind_all ( dev , drm_dev ) ;
2020-03-23 17:49:36 +03:00
err_iommu_cleanup :
2017-04-06 15:31:23 +03:00
rockchip_iommu_cleanup ( drm_dev ) ;
2016-06-10 14:14:13 +03:00
err_free :
2018-07-17 14:09:27 +03:00
drm_dev_put ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
return ret ;
}
2016-06-10 14:14:13 +03:00
static void rockchip_drm_unbind ( struct device * dev )
2014-08-22 14:36:26 +04:00
{
2016-06-10 14:14:13 +03:00
struct drm_device * drm_dev = dev_get_drvdata ( dev ) ;
2014-08-22 14:36:26 +04:00
2017-04-06 15:31:23 +03:00
drm_dev_unregister ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
rockchip_drm_fbdev_fini ( drm_dev ) ;
drm_kms_helper_poll_fini ( drm_dev ) ;
2017-04-06 15:31:23 +03:00
2017-04-06 15:31:24 +03:00
drm_atomic_helper_shutdown ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
component_unbind_all ( dev , drm_dev ) ;
2017-04-06 15:31:23 +03:00
rockchip_iommu_cleanup ( drm_dev ) ;
2018-07-17 14:09:27 +03:00
drm_dev_put ( drm_dev ) ;
2014-08-22 14:36:26 +04:00
}
static const struct file_operations rockchip_drm_driver_fops = {
. owner = THIS_MODULE ,
. open = drm_open ,
. mmap = rockchip_gem_mmap ,
. poll = drm_poll ,
. read = drm_read ,
. unlocked_ioctl = drm_ioctl ,
. compat_ioctl = drm_compat_ioctl ,
. release = drm_release ,
} ;
2020-11-04 13:04:24 +03:00
static const struct drm_driver rockchip_drm_driver = {
2019-06-17 18:39:24 +03:00
. driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC ,
2017-12-05 21:25:03 +03:00
. lastclose = drm_fb_helper_lastclose ,
2014-08-22 14:36:26 +04:00
. dumb_create = rockchip_gem_dumb_create ,
. prime_handle_to_fd = drm_gem_prime_handle_to_fd ,
. prime_fd_to_handle = drm_gem_prime_fd_to_handle ,
2018-01-30 23:28:32 +03:00
. gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table ,
2014-08-22 14:36:26 +04:00
. gem_prime_mmap = rockchip_gem_mmap_buf ,
. fops = & rockchip_drm_driver_fops ,
. name = DRIVER_NAME ,
. desc = DRIVER_DESC ,
. date = DRIVER_DATE ,
. major = DRIVER_MAJOR ,
. minor = DRIVER_MINOR ,
} ;
# ifdef CONFIG_PM_SLEEP
2016-06-06 17:53:32 +03:00
static int rockchip_drm_sys_suspend ( struct device * dev )
{
struct drm_device * drm = dev_get_drvdata ( dev ) ;
2018-07-31 23:34:30 +03:00
return drm_mode_config_helper_suspend ( drm ) ;
2014-08-22 14:36:26 +04:00
}
static int rockchip_drm_sys_resume ( struct device * dev )
{
struct drm_device * drm = dev_get_drvdata ( dev ) ;
2017-08-09 13:41:03 +03:00
2018-07-31 23:34:30 +03:00
return drm_mode_config_helper_resume ( drm ) ;
2014-08-22 14:36:26 +04:00
}
# endif
static const struct dev_pm_ops rockchip_drm_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS ( rockchip_drm_sys_suspend ,
rockchip_drm_sys_resume )
} ;
2017-03-22 06:21:20 +03:00
# define MAX_ROCKCHIP_SUB_DRIVERS 16
static struct platform_driver * rockchip_sub_drivers [ MAX_ROCKCHIP_SUB_DRIVERS ] ;
static int num_rockchip_sub_drivers ;
2014-08-22 14:36:26 +04:00
2018-08-31 00:12:05 +03:00
/*
* Check if a vop endpoint is leading to a rockchip subdriver or bridge .
* Should be called from the component bind stage of the drivers
* to ensure that all subdrivers are probed .
*
* @ ep : endpoint of a rockchip vop
*
* returns true if subdriver , false if external bridge and - ENODEV
* if remote port does not contain a device .
*/
int rockchip_drm_endpoint_is_subdriver ( struct device_node * ep )
{
struct device_node * node = of_graph_get_remote_port_parent ( ep ) ;
struct platform_device * pdev ;
struct device_driver * drv ;
int i ;
if ( ! node )
return - ENODEV ;
/* status disabled will prevent creation of platform-devices */
pdev = of_find_device_by_node ( node ) ;
of_node_put ( node ) ;
if ( ! pdev )
return - ENODEV ;
/*
* All rockchip subdrivers have probed at this point , so
* any device not having a driver now is an external bridge .
*/
drv = pdev - > dev . driver ;
if ( ! drv ) {
platform_device_put ( pdev ) ;
return false ;
}
for ( i = 0 ; i < num_rockchip_sub_drivers ; i + + ) {
if ( rockchip_sub_drivers [ i ] = = to_platform_driver ( drv ) ) {
platform_device_put ( pdev ) ;
return true ;
}
}
platform_device_put ( pdev ) ;
return false ;
}
2017-03-22 06:21:20 +03:00
static int compare_dev ( struct device * dev , void * data )
{
return dev = = ( struct device * ) data ;
2014-08-22 14:36:26 +04:00
}
2018-02-07 20:53:09 +03:00
static void rockchip_drm_match_remove ( struct device * dev )
{
struct device_link * link ;
list_for_each_entry ( link , & dev - > links . consumers , s_node )
device_link_del ( link ) ;
}
2017-03-22 06:21:20 +03:00
static struct component_match * rockchip_drm_match_add ( struct device * dev )
2015-11-10 11:47:19 +03:00
{
2017-03-22 06:21:20 +03:00
struct component_match * match = NULL ;
int i ;
2015-11-10 11:47:19 +03:00
2017-03-22 06:21:20 +03:00
for ( i = 0 ; i < num_rockchip_sub_drivers ; i + + ) {
struct platform_driver * drv = rockchip_sub_drivers [ i ] ;
struct device * p = NULL , * d ;
do {
2019-07-24 01:18:38 +03:00
d = platform_find_device_by_driver ( p , & drv - > driver ) ;
2017-03-22 06:21:20 +03:00
put_device ( p ) ;
p = d ;
2015-11-10 11:47:19 +03:00
2017-03-22 06:21:20 +03:00
if ( ! d )
break ;
2018-02-07 20:53:09 +03:00
device_link_add ( dev , d , DL_FLAG_STATELESS ) ;
2017-03-22 06:21:20 +03:00
component_match_add ( dev , & match , compare_dev , d ) ;
} while ( true ) ;
2015-11-10 11:47:19 +03:00
}
2017-03-22 06:21:20 +03:00
2018-02-07 20:53:09 +03:00
if ( IS_ERR ( match ) )
rockchip_drm_match_remove ( dev ) ;
2017-03-22 06:21:20 +03:00
return match ? : ERR_PTR ( - ENODEV ) ;
2015-11-10 11:47:19 +03:00
}
2014-08-22 14:36:26 +04:00
static const struct component_master_ops rockchip_drm_ops = {
. bind = rockchip_drm_bind ,
. unbind = rockchip_drm_unbind ,
} ;
2017-03-22 06:21:20 +03:00
static int rockchip_drm_platform_of_probe ( struct device * dev )
2014-08-22 14:36:26 +04:00
{
2015-11-10 11:47:19 +03:00
struct device_node * np = dev - > of_node ;
struct device_node * port ;
2017-03-22 06:21:20 +03:00
bool found = false ;
2015-11-10 11:47:19 +03:00
int i ;
2014-08-22 14:36:26 +04:00
2015-11-10 11:47:19 +03:00
if ( ! np )
2014-08-22 14:36:26 +04:00
return - ENODEV ;
2017-03-22 06:21:20 +03:00
2015-11-10 11:47:19 +03:00
for ( i = 0 ; ; i + + ) {
2016-04-19 05:13:27 +03:00
struct device_node * iommu ;
2015-11-10 11:47:19 +03:00
port = of_parse_phandle ( np , " ports " , i ) ;
if ( ! port )
break ;
if ( ! of_device_is_available ( port - > parent ) ) {
of_node_put ( port ) ;
continue ;
}
2014-08-22 14:36:26 +04:00
2016-04-19 05:13:27 +03:00
iommu = of_parse_phandle ( port - > parent , " iommus " , 0 ) ;
if ( ! iommu | | ! of_device_is_available ( iommu - > parent ) ) {
2017-09-15 11:36:03 +03:00
DRM_DEV_DEBUG ( dev ,
" no iommu attached for %pOF, using non-iommu buffers \n " ,
port - > parent ) ;
2016-04-19 05:13:27 +03:00
/*
* if there is a crtc not support iommu , force set all
* crtc use non - iommu buffer .
*/
is_support_iommu = false ;
}
2017-03-22 06:21:20 +03:00
found = true ;
2016-07-05 05:04:48 +03:00
of_node_put ( iommu ) ;
2015-11-10 11:47:19 +03:00
of_node_put ( port ) ;
}
if ( i = = 0 ) {
2017-09-15 11:36:03 +03:00
DRM_DEV_ERROR ( dev , " missing 'ports' property \n " ) ;
2015-11-10 11:47:19 +03:00
return - ENODEV ;
}
2017-03-22 06:21:20 +03:00
if ( ! found ) {
2017-09-15 11:36:03 +03:00
DRM_DEV_ERROR ( dev ,
" No available vop found for display-subsystem. \n " ) ;
2015-11-10 11:47:19 +03:00
return - ENODEV ;
}
2017-03-22 06:21:20 +03:00
return 0 ;
}
2015-11-10 11:47:19 +03:00
2017-03-22 06:21:20 +03:00
static int rockchip_drm_platform_probe ( struct platform_device * pdev )
{
struct device * dev = & pdev - > dev ;
struct component_match * match = NULL ;
int ret ;
ret = rockchip_drm_platform_of_probe ( dev ) ;
if ( ret )
return ret ;
match = rockchip_drm_match_add ( dev ) ;
if ( IS_ERR ( match ) )
return PTR_ERR ( match ) ;
2015-11-10 11:47:19 +03:00
2018-02-07 20:53:09 +03:00
ret = component_master_add_with_match ( dev , & rockchip_drm_ops , match ) ;
if ( ret < 0 ) {
rockchip_drm_match_remove ( dev ) ;
return ret ;
}
return 0 ;
2014-08-22 14:36:26 +04:00
}
static int rockchip_drm_platform_remove ( struct platform_device * pdev )
{
component_master_del ( & pdev - > dev , & rockchip_drm_ops ) ;
2018-02-07 20:53:09 +03:00
rockchip_drm_match_remove ( & pdev - > dev ) ;
2014-08-22 14:36:26 +04:00
return 0 ;
}
2019-04-02 14:37:53 +03:00
static void rockchip_drm_platform_shutdown ( struct platform_device * pdev )
{
struct drm_device * drm = platform_get_drvdata ( pdev ) ;
if ( drm )
drm_atomic_helper_shutdown ( drm ) ;
}
2014-08-22 14:36:26 +04:00
static const struct of_device_id rockchip_drm_dt_ids [ ] = {
{ . compatible = " rockchip,display-subsystem " , } ,
{ /* sentinel */ } ,
} ;
MODULE_DEVICE_TABLE ( of , rockchip_drm_dt_ids ) ;
static struct platform_driver rockchip_drm_platform_driver = {
. probe = rockchip_drm_platform_probe ,
. remove = rockchip_drm_platform_remove ,
2019-04-02 14:37:53 +03:00
. shutdown = rockchip_drm_platform_shutdown ,
2014-08-22 14:36:26 +04:00
. driver = {
. name = " rockchip-drm " ,
. of_match_table = rockchip_drm_dt_ids ,
. pm = & rockchip_drm_pm_ops ,
} ,
} ;
2017-03-22 06:21:20 +03:00
# define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
if ( IS_ENABLED ( cond ) & & \
! WARN_ON ( num_rockchip_sub_drivers > = MAX_ROCKCHIP_SUB_DRIVERS ) ) \
rockchip_sub_drivers [ num_rockchip_sub_drivers + + ] = & drv ; \
}
static int __init rockchip_drm_init ( void )
{
int ret ;
num_rockchip_sub_drivers = 0 ;
ADD_ROCKCHIP_SUB_DRIVER ( vop_platform_driver , CONFIG_DRM_ROCKCHIP ) ;
2017-09-02 14:28:54 +03:00
ADD_ROCKCHIP_SUB_DRIVER ( rockchip_lvds_driver ,
CONFIG_ROCKCHIP_LVDS ) ;
2017-03-22 06:21:20 +03:00
ADD_ROCKCHIP_SUB_DRIVER ( rockchip_dp_driver ,
CONFIG_ROCKCHIP_ANALOGIX_DP ) ;
ADD_ROCKCHIP_SUB_DRIVER ( cdn_dp_driver , CONFIG_ROCKCHIP_CDN_DP ) ;
ADD_ROCKCHIP_SUB_DRIVER ( dw_hdmi_rockchip_pltfm_driver ,
CONFIG_ROCKCHIP_DW_HDMI ) ;
2018-10-01 15:38:43 +03:00
ADD_ROCKCHIP_SUB_DRIVER ( dw_mipi_dsi_rockchip_driver ,
2017-03-22 06:21:20 +03:00
CONFIG_ROCKCHIP_DW_MIPI_DSI ) ;
ADD_ROCKCHIP_SUB_DRIVER ( inno_hdmi_driver , CONFIG_ROCKCHIP_INNO_HDMI ) ;
2019-03-30 12:56:36 +03:00
ADD_ROCKCHIP_SUB_DRIVER ( rk3066_hdmi_driver ,
CONFIG_ROCKCHIP_RK3066_HDMI ) ;
2017-03-22 06:21:20 +03:00
ret = platform_register_drivers ( rockchip_sub_drivers ,
num_rockchip_sub_drivers ) ;
if ( ret )
return ret ;
ret = platform_driver_register ( & rockchip_drm_platform_driver ) ;
if ( ret )
goto err_unreg_drivers ;
return 0 ;
err_unreg_drivers :
platform_unregister_drivers ( rockchip_sub_drivers ,
num_rockchip_sub_drivers ) ;
return ret ;
}
static void __exit rockchip_drm_fini ( void )
{
platform_driver_unregister ( & rockchip_drm_platform_driver ) ;
platform_unregister_drivers ( rockchip_sub_drivers ,
num_rockchip_sub_drivers ) ;
}
module_init ( rockchip_drm_init ) ;
module_exit ( rockchip_drm_fini ) ;
2014-08-22 14:36:26 +04:00
MODULE_AUTHOR ( " Mark Yao <mark.yao@rock-chips.com> " ) ;
MODULE_DESCRIPTION ( " ROCKCHIP DRM Driver " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;