clk: mediatek: using CLK_MUX_ROUND_CLOSEST for the clock of dpi1_sel
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / modules / power / power_helpers.c
1 /* Copyright 2018 Advanced Micro Devices, Inc.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19  * OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Authors: AMD
22  *
23  */
24
25 #include "power_helpers.h"
26 #include "dc/inc/hw/dmcu.h"
27
28 #define DIV_ROUNDUP(a, b) (((a)+((b)/2))/(b))
29
30 /* Possible Min Reduction config from least aggressive to most aggressive
31  *  0    1     2     3     4     5     6     7     8     9     10    11   12
32  * 100  98.0 94.1  94.1  85.1  80.3  75.3  69.4  60.0  57.6  50.2  49.8  40.0 %
33  */
34 static const unsigned char min_reduction_table[13] = {
35 0xff, 0xfa, 0xf0, 0xf0, 0xd9, 0xcd, 0xc0, 0xb1, 0x99, 0x93, 0x80, 0x82, 0x66};
36
37 /* Possible Max Reduction configs from least aggressive to most aggressive
38  *  0    1     2     3     4     5     6     7     8     9     10    11   12
39  * 96.1 89.8 85.1  80.3  69.4  64.7  64.7  50.2  39.6  30.2  30.2  30.2  19.6 %
40  */
41 static const unsigned char max_reduction_table[13] = {
42 0xf5, 0xe5, 0xd9, 0xcd, 0xb1, 0xa5, 0xa5, 0x80, 0x65, 0x4d, 0x4d, 0x4d, 0x32};
43
44 /* Predefined ABM configuration sets. We may have different configuration sets
45  * in order to satisfy different power/quality requirements.
46  */
47 static const unsigned char abm_config[abm_defines_max_config][abm_defines_max_level] = {
48 /*  ABM Level 1,    ABM Level 2,    ABM Level 3,    ABM Level 4 */
49 {       2,              5,              7,              8       },      /* Default - Medium aggressiveness */
50 {       2,              5,              8,              11      },      /* Alt #1  - Increased aggressiveness */
51 {       0,              2,              4,              8       },      /* Alt #2  - Minimal aggressiveness */
52 {       3,              6,              10,             12      },      /* Alt #3  - Super aggressiveness */
53 };
54
55 #define NUM_AMBI_LEVEL    5
56 #define NUM_AGGR_LEVEL    4
57 #define NUM_POWER_FN_SEGS 8
58 #define NUM_BL_CURVE_SEGS 16
59
60 /* NOTE: iRAM is 256B in size */
61 struct iram_table_v_2 {
62         /* flags                      */
63         uint16_t flags;                                                 /* 0x00 U16  */
64
65         /* parameters for ABM2.0 algorithm */
66         uint8_t min_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x02 U0.8 */
67         uint8_t max_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x16 U0.8 */
68         uint8_t bright_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];        /* 0x2a U2.6 */
69         uint8_t bright_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];        /* 0x3e U2.6 */
70         uint8_t dark_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x52 U2.6 */
71         uint8_t dark_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x66 U2.6 */
72         uint8_t iir_curve[NUM_AMBI_LEVEL];                              /* 0x7a U0.8 */
73         uint8_t deviation_gain;                                         /* 0x7f U0.8 */
74
75         /* parameters for crgb conversion */
76         uint16_t crgb_thresh[NUM_POWER_FN_SEGS];                        /* 0x80 U3.13 */
77         uint16_t crgb_offset[NUM_POWER_FN_SEGS];                        /* 0x90 U1.15 */
78         uint16_t crgb_slope[NUM_POWER_FN_SEGS];                         /* 0xa0 U4.12 */
79
80         /* parameters for custom curve */
81         /* thresholds for brightness --> backlight */
82         uint16_t backlight_thresholds[NUM_BL_CURVE_SEGS];               /* 0xb0 U16.0 */
83         /* offsets for brightness --> backlight */
84         uint16_t backlight_offsets[NUM_BL_CURVE_SEGS];                  /* 0xd0 U16.0 */
85
86         /* For reading PSR State directly from IRAM */
87         uint8_t psr_state;                                              /* 0xf0       */
88         uint8_t dmcu_interface_version;                                 /* 0xf1       */
89         uint8_t dmcu_date_version_year_b0;                              /* 0xf2       */
90         uint8_t dmcu_date_version_year_b1;                              /* 0xf3       */
91         uint8_t dmcu_date_version_month;                                /* 0xf4       */
92         uint8_t dmcu_date_version_day;                                  /* 0xf5       */
93         uint8_t dmcu_state;                                             /* 0xf6       */
94
95         uint16_t blRampReduction;                                       /* 0xf7       */
96         uint16_t blRampStart;                                           /* 0xf9       */
97         uint8_t dummy5;                                                 /* 0xfb       */
98         uint8_t dummy6;                                                 /* 0xfc       */
99         uint8_t dummy7;                                                 /* 0xfd       */
100         uint8_t dummy8;                                                 /* 0xfe       */
101         uint8_t dummy9;                                                 /* 0xff       */
102 };
103
104 static uint16_t backlight_8_to_16(unsigned int backlight_8bit)
105 {
106         return (uint16_t)(backlight_8bit * 0x101);
107 }
108
109 static void fill_backlight_transform_table(struct dmcu_iram_parameters params,
110                 struct iram_table_v_2 *table)
111 {
112         unsigned int i;
113         unsigned int num_entries = NUM_BL_CURVE_SEGS;
114         unsigned int query_input_8bit;
115         unsigned int query_output_8bit;
116         unsigned int lut_index;
117
118         table->backlight_thresholds[0] = 0;
119         table->backlight_offsets[0] = params.backlight_lut_array[0];
120         table->backlight_thresholds[num_entries-1] = 0xFFFF;
121         table->backlight_offsets[num_entries-1] =
122                 params.backlight_lut_array[params.backlight_lut_array_size - 1];
123
124         /* Setup all brightness levels between 0% and 100% exclusive
125          * Fills brightness-to-backlight transform table. Backlight custom curve
126          * describes transform from brightness to backlight. It will be defined
127          * as set of thresholds and set of offsets, together, implying
128          * extrapolation of custom curve into 16 uniformly spanned linear
129          * segments.  Each threshold/offset represented by 16 bit entry in
130          * format U4.10.
131          */
132         for (i = 1; i+1 < num_entries; i++) {
133                 query_input_8bit = DIV_ROUNDUP((i * 256), num_entries);
134
135                 lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1);
136                 ASSERT(lut_index < params.backlight_lut_array_size);
137                 query_output_8bit = params.backlight_lut_array[lut_index] >> 8;
138
139                 table->backlight_thresholds[i] =
140                                 backlight_8_to_16(query_input_8bit);
141                 table->backlight_offsets[i] =
142                                 backlight_8_to_16(query_output_8bit);
143         }
144 }
145
146 bool dmcu_load_iram(struct dmcu *dmcu,
147         struct dmcu_iram_parameters params)
148 {
149         struct iram_table_v_2 ram_table;
150         unsigned int set = params.set;
151
152         if (dmcu == NULL)
153                 return false;
154
155         if (!dmcu->funcs->is_dmcu_initialized(dmcu))
156                 return true;
157
158         memset(&ram_table, 0, sizeof(ram_table));
159
160         ram_table.flags = 0x0;
161         ram_table.deviation_gain = 0xb3;
162
163         ram_table.blRampReduction =
164                 cpu_to_be16(params.backlight_ramping_reduction);
165         ram_table.blRampStart =
166                 cpu_to_be16(params.backlight_ramping_start);
167
168         ram_table.min_reduction[0][0] = min_reduction_table[abm_config[set][0]];
169         ram_table.min_reduction[1][0] = min_reduction_table[abm_config[set][0]];
170         ram_table.min_reduction[2][0] = min_reduction_table[abm_config[set][0]];
171         ram_table.min_reduction[3][0] = min_reduction_table[abm_config[set][0]];
172         ram_table.min_reduction[4][0] = min_reduction_table[abm_config[set][0]];
173         ram_table.max_reduction[0][0] = max_reduction_table[abm_config[set][0]];
174         ram_table.max_reduction[1][0] = max_reduction_table[abm_config[set][0]];
175         ram_table.max_reduction[2][0] = max_reduction_table[abm_config[set][0]];
176         ram_table.max_reduction[3][0] = max_reduction_table[abm_config[set][0]];
177         ram_table.max_reduction[4][0] = max_reduction_table[abm_config[set][0]];
178
179         ram_table.min_reduction[0][1] = min_reduction_table[abm_config[set][1]];
180         ram_table.min_reduction[1][1] = min_reduction_table[abm_config[set][1]];
181         ram_table.min_reduction[2][1] = min_reduction_table[abm_config[set][1]];
182         ram_table.min_reduction[3][1] = min_reduction_table[abm_config[set][1]];
183         ram_table.min_reduction[4][1] = min_reduction_table[abm_config[set][1]];
184         ram_table.max_reduction[0][1] = max_reduction_table[abm_config[set][1]];
185         ram_table.max_reduction[1][1] = max_reduction_table[abm_config[set][1]];
186         ram_table.max_reduction[2][1] = max_reduction_table[abm_config[set][1]];
187         ram_table.max_reduction[3][1] = max_reduction_table[abm_config[set][1]];
188         ram_table.max_reduction[4][1] = max_reduction_table[abm_config[set][1]];
189
190         ram_table.min_reduction[0][2] = min_reduction_table[abm_config[set][2]];
191         ram_table.min_reduction[1][2] = min_reduction_table[abm_config[set][2]];
192         ram_table.min_reduction[2][2] = min_reduction_table[abm_config[set][2]];
193         ram_table.min_reduction[3][2] = min_reduction_table[abm_config[set][2]];
194         ram_table.min_reduction[4][2] = min_reduction_table[abm_config[set][2]];
195         ram_table.max_reduction[0][2] = max_reduction_table[abm_config[set][2]];
196         ram_table.max_reduction[1][2] = max_reduction_table[abm_config[set][2]];
197         ram_table.max_reduction[2][2] = max_reduction_table[abm_config[set][2]];
198         ram_table.max_reduction[3][2] = max_reduction_table[abm_config[set][2]];
199         ram_table.max_reduction[4][2] = max_reduction_table[abm_config[set][2]];
200
201         ram_table.min_reduction[0][3] = min_reduction_table[abm_config[set][3]];
202         ram_table.min_reduction[1][3] = min_reduction_table[abm_config[set][3]];
203         ram_table.min_reduction[2][3] = min_reduction_table[abm_config[set][3]];
204         ram_table.min_reduction[3][3] = min_reduction_table[abm_config[set][3]];
205         ram_table.min_reduction[4][3] = min_reduction_table[abm_config[set][3]];
206         ram_table.max_reduction[0][3] = max_reduction_table[abm_config[set][3]];
207         ram_table.max_reduction[1][3] = max_reduction_table[abm_config[set][3]];
208         ram_table.max_reduction[2][3] = max_reduction_table[abm_config[set][3]];
209         ram_table.max_reduction[3][3] = max_reduction_table[abm_config[set][3]];
210         ram_table.max_reduction[4][3] = max_reduction_table[abm_config[set][3]];
211
212         ram_table.bright_pos_gain[0][0] = 0x20;
213         ram_table.bright_pos_gain[0][1] = 0x20;
214         ram_table.bright_pos_gain[0][2] = 0x20;
215         ram_table.bright_pos_gain[0][3] = 0x20;
216         ram_table.bright_pos_gain[1][0] = 0x20;
217         ram_table.bright_pos_gain[1][1] = 0x20;
218         ram_table.bright_pos_gain[1][2] = 0x20;
219         ram_table.bright_pos_gain[1][3] = 0x20;
220         ram_table.bright_pos_gain[2][0] = 0x20;
221         ram_table.bright_pos_gain[2][1] = 0x20;
222         ram_table.bright_pos_gain[2][2] = 0x20;
223         ram_table.bright_pos_gain[2][3] = 0x20;
224         ram_table.bright_pos_gain[3][0] = 0x20;
225         ram_table.bright_pos_gain[3][1] = 0x20;
226         ram_table.bright_pos_gain[3][2] = 0x20;
227         ram_table.bright_pos_gain[3][3] = 0x20;
228         ram_table.bright_pos_gain[4][0] = 0x20;
229         ram_table.bright_pos_gain[4][1] = 0x20;
230         ram_table.bright_pos_gain[4][2] = 0x20;
231         ram_table.bright_pos_gain[4][3] = 0x20;
232         ram_table.bright_neg_gain[0][1] = 0x00;
233         ram_table.bright_neg_gain[0][2] = 0x00;
234         ram_table.bright_neg_gain[0][3] = 0x00;
235         ram_table.bright_neg_gain[1][0] = 0x00;
236         ram_table.bright_neg_gain[1][1] = 0x00;
237         ram_table.bright_neg_gain[1][2] = 0x00;
238         ram_table.bright_neg_gain[1][3] = 0x00;
239         ram_table.bright_neg_gain[2][0] = 0x00;
240         ram_table.bright_neg_gain[2][1] = 0x00;
241         ram_table.bright_neg_gain[2][2] = 0x00;
242         ram_table.bright_neg_gain[2][3] = 0x00;
243         ram_table.bright_neg_gain[3][0] = 0x00;
244         ram_table.bright_neg_gain[3][1] = 0x00;
245         ram_table.bright_neg_gain[3][2] = 0x00;
246         ram_table.bright_neg_gain[3][3] = 0x00;
247         ram_table.bright_neg_gain[4][0] = 0x00;
248         ram_table.bright_neg_gain[4][1] = 0x00;
249         ram_table.bright_neg_gain[4][2] = 0x00;
250         ram_table.bright_neg_gain[4][3] = 0x00;
251         ram_table.dark_pos_gain[0][0] = 0x00;
252         ram_table.dark_pos_gain[0][1] = 0x00;
253         ram_table.dark_pos_gain[0][2] = 0x00;
254         ram_table.dark_pos_gain[0][3] = 0x00;
255         ram_table.dark_pos_gain[1][0] = 0x00;
256         ram_table.dark_pos_gain[1][1] = 0x00;
257         ram_table.dark_pos_gain[1][2] = 0x00;
258         ram_table.dark_pos_gain[1][3] = 0x00;
259         ram_table.dark_pos_gain[2][0] = 0x00;
260         ram_table.dark_pos_gain[2][1] = 0x00;
261         ram_table.dark_pos_gain[2][2] = 0x00;
262         ram_table.dark_pos_gain[2][3] = 0x00;
263         ram_table.dark_pos_gain[3][0] = 0x00;
264         ram_table.dark_pos_gain[3][1] = 0x00;
265         ram_table.dark_pos_gain[3][2] = 0x00;
266         ram_table.dark_pos_gain[3][3] = 0x00;
267         ram_table.dark_pos_gain[4][0] = 0x00;
268         ram_table.dark_pos_gain[4][1] = 0x00;
269         ram_table.dark_pos_gain[4][2] = 0x00;
270         ram_table.dark_pos_gain[4][3] = 0x00;
271         ram_table.dark_neg_gain[0][0] = 0x00;
272         ram_table.dark_neg_gain[0][1] = 0x00;
273         ram_table.dark_neg_gain[0][2] = 0x00;
274         ram_table.dark_neg_gain[0][3] = 0x00;
275         ram_table.dark_neg_gain[1][0] = 0x00;
276         ram_table.dark_neg_gain[1][1] = 0x00;
277         ram_table.dark_neg_gain[1][2] = 0x00;
278         ram_table.dark_neg_gain[1][3] = 0x00;
279         ram_table.dark_neg_gain[2][0] = 0x00;
280         ram_table.dark_neg_gain[2][1] = 0x00;
281         ram_table.dark_neg_gain[2][2] = 0x00;
282         ram_table.dark_neg_gain[2][3] = 0x00;
283         ram_table.dark_neg_gain[3][0] = 0x00;
284         ram_table.dark_neg_gain[3][1] = 0x00;
285         ram_table.dark_neg_gain[3][2] = 0x00;
286         ram_table.dark_neg_gain[3][3] = 0x00;
287         ram_table.dark_neg_gain[4][0] = 0x00;
288         ram_table.dark_neg_gain[4][1] = 0x00;
289         ram_table.dark_neg_gain[4][2] = 0x00;
290         ram_table.dark_neg_gain[4][3] = 0x00;
291         ram_table.iir_curve[0] = 0x65;
292         ram_table.iir_curve[1] = 0x65;
293         ram_table.iir_curve[2] = 0x65;
294         ram_table.iir_curve[3] = 0x65;
295         ram_table.iir_curve[4] = 0x65;
296         ram_table.crgb_thresh[0] = cpu_to_be16(0x13b6);
297         ram_table.crgb_thresh[1] = cpu_to_be16(0x1648);
298         ram_table.crgb_thresh[2] = cpu_to_be16(0x18e3);
299         ram_table.crgb_thresh[3] = cpu_to_be16(0x1b41);
300         ram_table.crgb_thresh[4] = cpu_to_be16(0x1d46);
301         ram_table.crgb_thresh[5] = cpu_to_be16(0x1f21);
302         ram_table.crgb_thresh[6] = cpu_to_be16(0x2167);
303         ram_table.crgb_thresh[7] = cpu_to_be16(0x2384);
304         ram_table.crgb_offset[0] = cpu_to_be16(0x2999);
305         ram_table.crgb_offset[1] = cpu_to_be16(0x3999);
306         ram_table.crgb_offset[2] = cpu_to_be16(0x4666);
307         ram_table.crgb_offset[3] = cpu_to_be16(0x5999);
308         ram_table.crgb_offset[4] = cpu_to_be16(0x6333);
309         ram_table.crgb_offset[5] = cpu_to_be16(0x7800);
310         ram_table.crgb_offset[6] = cpu_to_be16(0x8c00);
311         ram_table.crgb_offset[7] = cpu_to_be16(0xa000);
312         ram_table.crgb_slope[0]  = cpu_to_be16(0x3147);
313         ram_table.crgb_slope[1]  = cpu_to_be16(0x2978);
314         ram_table.crgb_slope[2]  = cpu_to_be16(0x23a2);
315         ram_table.crgb_slope[3]  = cpu_to_be16(0x1f55);
316         ram_table.crgb_slope[4]  = cpu_to_be16(0x1c63);
317         ram_table.crgb_slope[5]  = cpu_to_be16(0x1a0f);
318         ram_table.crgb_slope[6]  = cpu_to_be16(0x178d);
319         ram_table.crgb_slope[7]  = cpu_to_be16(0x15ab);
320
321         fill_backlight_transform_table(
322                         params, &ram_table);
323
324         return dmcu->funcs->load_iram(
325                         dmcu, 0, (char *)(&ram_table), sizeof(ram_table));
326 }