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