xref: /linux/drivers/gpu/drm/amd/display/dc/dce/dmub_abm.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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 #include "dmub_cmd.h"
31 #include "dc_dmub_srv.h"
32 #include "dmub/dmub_srv.h"
33 
34 #define TO_DMUB_ABM(abm)\
35 	container_of(abm, struct dce_abm, base)
36 
37 #define ABM_FEATURE_NO_SUPPORT	0
38 #define ABM_LCD_SUPPORT			1
39 
abm_feature_support(struct abm * abm,unsigned int panel_inst)40 static unsigned int abm_feature_support(struct abm *abm, unsigned int panel_inst)
41 {
42 	struct dc_context *dc = abm->ctx;
43 	struct dc_link *edp_links[MAX_NUM_EDP];
44 	int i;
45 	int edp_num;
46 	unsigned int ret = ABM_FEATURE_NO_SUPPORT;
47 
48 	dc_get_edp_links(dc->dc, edp_links, &edp_num);
49 
50 	for (i = 0; i < edp_num; i++) {
51 		if (panel_inst == i)
52 			break;
53 	}
54 
55 	if (i < edp_num) {
56 		ret = ABM_LCD_SUPPORT;
57 	}
58 
59 	return ret;
60 }
61 
dmub_abm_init_ex(struct abm * abm,uint32_t backlight,uint32_t user_level)62 static void dmub_abm_init_ex(struct abm *abm, uint32_t backlight, uint32_t user_level)
63 {
64 	dmub_abm_init(abm, backlight, user_level);
65 }
66 
dmub_abm_get_current_backlight_ex(struct abm * abm)67 static unsigned int dmub_abm_get_current_backlight_ex(struct abm *abm)
68 {
69 	dc_allow_idle_optimizations(abm->ctx->dc, false);
70 
71 	return dmub_abm_get_current_backlight(abm);
72 }
73 
dmub_abm_get_target_backlight_ex(struct abm * abm)74 static unsigned int dmub_abm_get_target_backlight_ex(struct abm *abm)
75 {
76 	dc_allow_idle_optimizations(abm->ctx->dc, false);
77 
78 	return dmub_abm_get_target_backlight(abm);
79 }
80 
dmub_abm_set_level_ex(struct abm * abm,uint32_t level)81 static bool dmub_abm_set_level_ex(struct abm *abm, uint32_t level)
82 {
83 	bool ret = false;
84 	unsigned int feature_support, i;
85 	uint8_t panel_mask0 = 0;
86 
87 	for (i = 0; i < MAX_NUM_EDP; i++) {
88 		feature_support = abm_feature_support(abm, i);
89 
90 		if (feature_support == ABM_LCD_SUPPORT)
91 			panel_mask0 |= (0x01 << i);
92 	}
93 
94 	if (panel_mask0)
95 		ret = dmub_abm_set_level(abm, level, panel_mask0);
96 
97 	return ret;
98 }
99 
dmub_abm_init_config_ex(struct abm * abm,const char * src,unsigned int bytes,unsigned int inst)100 static bool dmub_abm_init_config_ex(struct abm *abm,
101 	const char *src,
102 	unsigned int bytes,
103 	unsigned int inst)
104 {
105 	unsigned int feature_support;
106 
107 	feature_support = abm_feature_support(abm, inst);
108 
109 	if (feature_support == ABM_LCD_SUPPORT)
110 		dmub_abm_init_config(abm, src, bytes, inst);
111 
112 	return true;
113 }
114 
dmub_abm_set_pause_ex(struct abm * abm,bool pause,unsigned int panel_inst,unsigned int stream_inst)115 static bool dmub_abm_set_pause_ex(struct abm *abm, bool pause, unsigned int panel_inst, unsigned int stream_inst)
116 {
117 	bool ret = false;
118 	unsigned int feature_support;
119 
120 	feature_support = abm_feature_support(abm, panel_inst);
121 
122 	if (feature_support == ABM_LCD_SUPPORT)
123 		ret = dmub_abm_set_pause(abm, pause, panel_inst, stream_inst);
124 
125 	return ret;
126 }
127 
128 /*****************************************************************************
129  *  dmub_abm_save_restore_ex() - calls dmub_abm_save_restore for preserving DMUB's
130  *                              Varibright states for LCD only. OLED is TBD
131  *  @abm: used to check get dc context
132  *  @panel_inst: panel instance index
133  *  @pData: contains command to pause/un-pause abm and abm parameters
134  *
135  *
136  ***************************************************************************/
dmub_abm_save_restore_ex(struct abm * abm,unsigned int panel_inst,struct abm_save_restore * pData)137 static bool dmub_abm_save_restore_ex(
138 		struct abm *abm,
139 		unsigned int panel_inst,
140 		struct abm_save_restore *pData)
141 {
142 	bool ret = false;
143 	unsigned int feature_support;
144 	struct dc_context *dc = abm->ctx;
145 
146 	feature_support = abm_feature_support(abm, panel_inst);
147 
148 	if (feature_support == ABM_LCD_SUPPORT)
149 		ret = dmub_abm_save_restore(dc, panel_inst, pData);
150 
151 	return ret;
152 }
153 
dmub_abm_set_pipe_ex(struct abm * abm,uint32_t otg_inst,uint32_t option,uint32_t panel_inst,uint32_t pwrseq_inst)154 static bool dmub_abm_set_pipe_ex(struct abm *abm,
155 		uint32_t otg_inst,
156 		uint32_t option,
157 		uint32_t panel_inst,
158 		uint32_t pwrseq_inst)
159 {
160 	bool ret = false;
161 	unsigned int feature_support;
162 
163 	feature_support = abm_feature_support(abm, panel_inst);
164 
165 	if (feature_support == ABM_LCD_SUPPORT)
166 		ret = dmub_abm_set_pipe(abm, otg_inst, option, panel_inst, pwrseq_inst);
167 
168 	return ret;
169 }
170 
dmub_abm_set_backlight_level_pwm_ex(struct abm * abm,unsigned int backlight_pwm_u16_16,unsigned int frame_ramp,unsigned int controller_id,unsigned int panel_inst)171 static bool dmub_abm_set_backlight_level_pwm_ex(struct abm *abm,
172 		unsigned int backlight_pwm_u16_16,
173 		unsigned int frame_ramp,
174 		unsigned int controller_id,
175 		unsigned int panel_inst)
176 {
177 	bool ret = false;
178 	unsigned int feature_support;
179 
180 	feature_support = abm_feature_support(abm, panel_inst);
181 
182 	if (feature_support == ABM_LCD_SUPPORT)
183 		ret = dmub_abm_set_backlight_level(abm, backlight_pwm_u16_16, frame_ramp, panel_inst);
184 
185 	return ret;
186 }
187 
188 static const struct abm_funcs abm_funcs = {
189 	.abm_init = dmub_abm_init_ex,
190 	.set_abm_level = dmub_abm_set_level_ex,
191 	.get_current_backlight = dmub_abm_get_current_backlight_ex,
192 	.get_target_backlight = dmub_abm_get_target_backlight_ex,
193 	.init_abm_config = dmub_abm_init_config_ex,
194 	.set_abm_pause = dmub_abm_set_pause_ex,
195 	.save_restore = dmub_abm_save_restore_ex,
196 	.set_pipe_ex = dmub_abm_set_pipe_ex,
197 	.set_backlight_level_pwm = dmub_abm_set_backlight_level_pwm_ex,
198 };
199 
dmub_abm_construct(struct dce_abm * abm_dce,struct dc_context * ctx,const struct dce_abm_registers * regs,const struct dce_abm_shift * abm_shift,const struct dce_abm_mask * abm_mask)200 static void dmub_abm_construct(
201 	struct dce_abm *abm_dce,
202 	struct dc_context *ctx,
203 	const struct dce_abm_registers *regs,
204 	const struct dce_abm_shift *abm_shift,
205 	const struct dce_abm_mask *abm_mask)
206 {
207 	struct abm *base = &abm_dce->base;
208 
209 	base->ctx = ctx;
210 	base->funcs = &abm_funcs;
211 	base->dmcu_is_running = false;
212 
213 	abm_dce->regs = regs;
214 	abm_dce->abm_shift = abm_shift;
215 	abm_dce->abm_mask = abm_mask;
216 }
217 
dmub_abm_create(struct dc_context * ctx,const struct dce_abm_registers * regs,const struct dce_abm_shift * abm_shift,const struct dce_abm_mask * abm_mask)218 struct abm *dmub_abm_create(
219 	struct dc_context *ctx,
220 	const struct dce_abm_registers *regs,
221 	const struct dce_abm_shift *abm_shift,
222 	const struct dce_abm_mask *abm_mask)
223 {
224 	if (ctx->dc->caps.dmcub_support) {
225 		struct dce_abm *abm_dce = kzalloc(sizeof(*abm_dce), GFP_KERNEL);
226 
227 		if (abm_dce == NULL) {
228 			BREAK_TO_DEBUGGER();
229 			return NULL;
230 		}
231 
232 		dmub_abm_construct(abm_dce, ctx, regs, abm_shift, abm_mask);
233 
234 		return &abm_dce->base;
235 	}
236 	return NULL;
237 }
238 
dmub_abm_destroy(struct abm ** abm)239 void dmub_abm_destroy(struct abm **abm)
240 {
241 	struct dce_abm *abm_dce = TO_DMUB_ABM(*abm);
242 
243 	kfree(abm_dce);
244 	*abm = NULL;
245 }
246