1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "dmub_abm.h"
27 #include "dmub_abm_lcd.h"
28 #include "dc.h"
29 #include "core_types.h"
30 
31 #define TO_DMUB_ABM(abm)\
32 	container_of(abm, struct dce_abm, base)
33 
34 #define ABM_FEATURE_NO_SUPPORT	0
35 #define ABM_LCD_SUPPORT			1
36 
37 static unsigned int abm_feature_support(struct abm *abm, unsigned int panel_inst)
38 {
39 	struct dc_context *dc = abm->ctx;
40 	struct dc_link *edp_links[MAX_NUM_EDP];
41 	int i;
42 	int edp_num;
43 	unsigned int ret = ABM_FEATURE_NO_SUPPORT;
44 
45 	dc_get_edp_links(dc->dc, edp_links, &edp_num);
46 
47 	for (i = 0; i < edp_num; i++) {
48 		if (panel_inst == i)
49 			break;
50 	}
51 
52 	if (i < edp_num) {
53 		ret = ABM_LCD_SUPPORT;
54 	}
55 
56 	return ret;
57 }
58 
59 static void dmub_abm_init_ex(struct abm *abm, uint32_t backlight)
60 {
61 	dmub_abm_init(abm, backlight);
62 }
63 
64 static unsigned int dmub_abm_get_current_backlight_ex(struct abm *abm)
65 {
66 	return dmub_abm_get_current_backlight(abm);
67 }
68 
69 static unsigned int dmub_abm_get_target_backlight_ex(struct abm *abm)
70 {
71 	return dmub_abm_get_target_backlight(abm);
72 }
73 
74 static bool dmub_abm_set_level_ex(struct abm *abm, uint32_t level)
75 {
76 	bool ret = false;
77 	unsigned int feature_support, i;
78 	uint8_t panel_mask0 = 0;
79 
80 	for (i = 0; i < MAX_NUM_EDP; i++) {
81 		feature_support = abm_feature_support(abm, i);
82 
83 		if (feature_support == ABM_LCD_SUPPORT)
84 			panel_mask0 |= (0x01 << i);
85 	}
86 
87 	if (panel_mask0)
88 		ret = dmub_abm_set_level(abm, level, panel_mask0);
89 
90 	return ret;
91 }
92 
93 static bool dmub_abm_init_config_ex(struct abm *abm,
94 	const char *src,
95 	unsigned int bytes,
96 	unsigned int inst)
97 {
98 	unsigned int feature_support;
99 
100 	feature_support = abm_feature_support(abm, inst);
101 
102 	if (feature_support == ABM_LCD_SUPPORT)
103 		dmub_abm_init_config(abm, src, bytes, inst);
104 
105 	return true;
106 }
107 
108 static bool dmub_abm_set_pause_ex(struct abm *abm, bool pause, unsigned int panel_inst, unsigned int stream_inst)
109 {
110 	bool ret = false;
111 	unsigned int feature_support;
112 
113 	feature_support = abm_feature_support(abm, panel_inst);
114 
115 	if (feature_support == ABM_LCD_SUPPORT)
116 		ret = dmub_abm_set_pause(abm, pause, panel_inst, stream_inst);
117 
118 	return ret;
119 }
120 
121 static bool dmub_abm_set_pipe_ex(struct abm *abm, uint32_t otg_inst, uint32_t option, uint32_t panel_inst)
122 {
123 	bool ret = false;
124 	unsigned int feature_support;
125 
126 	feature_support = abm_feature_support(abm, panel_inst);
127 
128 	if (feature_support == ABM_LCD_SUPPORT)
129 		ret = dmub_abm_set_pipe(abm, otg_inst, option, panel_inst);
130 
131 	return ret;
132 }
133 
134 static bool dmub_abm_set_backlight_level_pwm_ex(struct abm *abm,
135 		unsigned int backlight_pwm_u16_16,
136 		unsigned int frame_ramp,
137 		unsigned int controller_id,
138 		unsigned int panel_inst)
139 {
140 	bool ret = false;
141 	unsigned int feature_support;
142 
143 	feature_support = abm_feature_support(abm, panel_inst);
144 
145 	if (feature_support == ABM_LCD_SUPPORT)
146 		ret = dmub_abm_set_backlight_level(abm, backlight_pwm_u16_16, frame_ramp, panel_inst);
147 
148 	return ret;
149 }
150 
151 static const struct abm_funcs abm_funcs = {
152 	.abm_init = dmub_abm_init_ex,
153 	.set_abm_level = dmub_abm_set_level_ex,
154 	.get_current_backlight = dmub_abm_get_current_backlight_ex,
155 	.get_target_backlight = dmub_abm_get_target_backlight_ex,
156 	.init_abm_config = dmub_abm_init_config_ex,
157 	.set_abm_pause = dmub_abm_set_pause_ex,
158 	.set_pipe_ex = dmub_abm_set_pipe_ex,
159 	.set_backlight_level_pwm = dmub_abm_set_backlight_level_pwm_ex,
160 };
161 
162 static void dmub_abm_construct(
163 	struct dce_abm *abm_dce,
164 	struct dc_context *ctx,
165 	const struct dce_abm_registers *regs,
166 	const struct dce_abm_shift *abm_shift,
167 	const struct dce_abm_mask *abm_mask)
168 {
169 	struct abm *base = &abm_dce->base;
170 
171 	base->ctx = ctx;
172 	base->funcs = &abm_funcs;
173 	base->dmcu_is_running = false;
174 
175 	abm_dce->regs = regs;
176 	abm_dce->abm_shift = abm_shift;
177 	abm_dce->abm_mask = abm_mask;
178 }
179 
180 struct abm *dmub_abm_create(
181 	struct dc_context *ctx,
182 	const struct dce_abm_registers *regs,
183 	const struct dce_abm_shift *abm_shift,
184 	const struct dce_abm_mask *abm_mask)
185 {
186 	if (ctx->dc->caps.dmcub_support) {
187 		struct dce_abm *abm_dce = kzalloc(sizeof(*abm_dce), GFP_KERNEL);
188 
189 		if (abm_dce == NULL) {
190 			BREAK_TO_DEBUGGER();
191 			return NULL;
192 		}
193 
194 		dmub_abm_construct(abm_dce, ctx, regs, abm_shift, abm_mask);
195 
196 		return &abm_dce->base;
197 	}
198 	return NULL;
199 }
200 
201 void dmub_abm_destroy(struct abm **abm)
202 {
203 	struct dce_abm *abm_dce = TO_DMUB_ABM(*abm);
204 
205 	kfree(abm_dce);
206 	*abm = NULL;
207 }
208