1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
4  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5  */
6 
7 #include <linux/export.h>
8 
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_of.h>
12 #include <drm/drm_panel.h>
13 #include <drm/drm_simple_kms_helper.h>
14 
15 #include "tidss_crtc.h"
16 #include "tidss_drv.h"
17 #include "tidss_encoder.h"
18 
19 static int tidss_encoder_atomic_check(struct drm_encoder *encoder,
20 				      struct drm_crtc_state *crtc_state,
21 				      struct drm_connector_state *conn_state)
22 {
23 	struct drm_device *ddev = encoder->dev;
24 	struct tidss_crtc_state *tcrtc_state = to_tidss_crtc_state(crtc_state);
25 	struct drm_display_info *di = &conn_state->connector->display_info;
26 	struct drm_bridge *bridge;
27 	bool bus_flags_set = false;
28 
29 	dev_dbg(ddev->dev, "%s\n", __func__);
30 
31 	/*
32 	 * Take the bus_flags from the first bridge that defines
33 	 * bridge timings, or from the connector's display_info if no
34 	 * bridge defines the timings.
35 	 */
36 	drm_for_each_bridge_in_chain(encoder, bridge) {
37 		if (!bridge->timings)
38 			continue;
39 
40 		tcrtc_state->bus_flags = bridge->timings->input_bus_flags;
41 		bus_flags_set = true;
42 		break;
43 	}
44 
45 	if (!di->bus_formats || di->num_bus_formats == 0)  {
46 		dev_err(ddev->dev, "%s: No bus_formats in connected display\n",
47 			__func__);
48 		return -EINVAL;
49 	}
50 
51 	// XXX any cleaner way to set bus format and flags?
52 	tcrtc_state->bus_format = di->bus_formats[0];
53 	if (!bus_flags_set)
54 		tcrtc_state->bus_flags = di->bus_flags;
55 
56 	return 0;
57 }
58 
59 static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
60 	.atomic_check = tidss_encoder_atomic_check,
61 };
62 
63 struct drm_encoder *tidss_encoder_create(struct tidss_device *tidss,
64 					 u32 encoder_type, u32 possible_crtcs)
65 {
66 	struct drm_encoder *enc;
67 	int ret;
68 
69 	enc = devm_kzalloc(tidss->dev, sizeof(*enc), GFP_KERNEL);
70 	if (!enc)
71 		return ERR_PTR(-ENOMEM);
72 
73 	enc->possible_crtcs = possible_crtcs;
74 
75 	ret = drm_simple_encoder_init(&tidss->ddev, enc, encoder_type);
76 	if (ret < 0)
77 		return ERR_PTR(ret);
78 
79 	drm_encoder_helper_add(enc, &encoder_helper_funcs);
80 
81 	dev_dbg(tidss->dev, "Encoder create done\n");
82 
83 	return enc;
84 }
85