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