1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include "dpu_writeback.h"
7 
8 static int dpu_wb_conn_get_modes(struct drm_connector *connector)
9 {
10 	struct drm_device *dev = connector->dev;
11 	struct msm_drm_private *priv = dev->dev_private;
12 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
13 
14 	return drm_add_modes_noedid(connector, dpu_kms->catalog->caps->max_linewidth,
15 			dev->mode_config.max_height);
16 }
17 
18 static const struct drm_connector_funcs dpu_wb_conn_funcs = {
19 	.reset = drm_atomic_helper_connector_reset,
20 	.fill_modes = drm_helper_probe_single_connector_modes,
21 	.destroy = drm_connector_cleanup,
22 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
23 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
24 };
25 
26 static int dpu_wb_conn_prepare_job(struct drm_writeback_connector *connector,
27 		struct drm_writeback_job *job)
28 {
29 
30 	struct dpu_wb_connector *dpu_wb_conn = to_dpu_wb_conn(connector);
31 
32 	if (!job->fb)
33 		return 0;
34 
35 	dpu_encoder_prepare_wb_job(dpu_wb_conn->wb_enc, job);
36 
37 	return 0;
38 }
39 
40 static void dpu_wb_conn_cleanup_job(struct drm_writeback_connector *connector,
41 		struct drm_writeback_job *job)
42 {
43 	struct dpu_wb_connector *dpu_wb_conn = to_dpu_wb_conn(connector);
44 
45 	if (!job->fb)
46 		return;
47 
48 	dpu_encoder_cleanup_wb_job(dpu_wb_conn->wb_enc, job);
49 }
50 
51 static const struct drm_connector_helper_funcs dpu_wb_conn_helper_funcs = {
52 	.get_modes = dpu_wb_conn_get_modes,
53 	.prepare_writeback_job = dpu_wb_conn_prepare_job,
54 	.cleanup_writeback_job = dpu_wb_conn_cleanup_job,
55 };
56 
57 int dpu_writeback_init(struct drm_device *dev, struct drm_encoder *enc,
58 		const u32 *format_list, u32 num_formats)
59 {
60 	struct dpu_wb_connector *dpu_wb_conn;
61 	int rc = 0;
62 
63 	dpu_wb_conn = devm_kzalloc(dev->dev, sizeof(*dpu_wb_conn), GFP_KERNEL);
64 
65 	drm_connector_helper_add(&dpu_wb_conn->base.base, &dpu_wb_conn_helper_funcs);
66 
67 	/* DPU initializes the encoder and sets it up completely for writeback
68 	 * cases and hence should use the new API drm_writeback_connector_init_with_encoder
69 	 * to initialize the writeback connector
70 	 */
71 	rc = drm_writeback_connector_init_with_encoder(dev, &dpu_wb_conn->base, enc,
72 			&dpu_wb_conn_funcs, format_list, num_formats);
73 
74 	if (!rc)
75 		dpu_wb_conn->wb_enc = enc;
76 
77 	return rc;
78 }
79