Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[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 #pragma pack(push, 1)
61 /* NOTE: iRAM is 256B in size */
62 struct iram_table_v_2 {
63         /* flags                      */
64         uint16_t flags;                                                 /* 0x00 U16  */
65
66         /* parameters for ABM2.0 algorithm */
67         uint8_t min_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x02 U0.8 */
68         uint8_t max_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x16 U0.8 */
69         uint8_t bright_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];        /* 0x2a U2.6 */
70         uint8_t bright_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];        /* 0x3e U2.6 */
71         uint8_t dark_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x52 U2.6 */
72         uint8_t dark_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];          /* 0x66 U2.6 */
73         uint8_t iir_curve[NUM_AMBI_LEVEL];                              /* 0x7a U0.8 */
74         uint8_t deviation_gain;                                         /* 0x7f U0.8 */
75
76         /* parameters for crgb conversion */
77         uint16_t crgb_thresh[NUM_POWER_FN_SEGS];                        /* 0x80 U3.13 */
78         uint16_t crgb_offset[NUM_POWER_FN_SEGS];                        /* 0x90 U1.15 */
79         uint16_t crgb_slope[NUM_POWER_FN_SEGS];                         /* 0xa0 U4.12 */
80
81         /* parameters for custom curve */
82         /* thresholds for brightness --> backlight */
83         uint16_t backlight_thresholds[NUM_BL_CURVE_SEGS];               /* 0xb0 U16.0 */
84         /* offsets for brightness --> backlight */
85         uint16_t backlight_offsets[NUM_BL_CURVE_SEGS];                  /* 0xd0 U16.0 */
86
87         /* For reading PSR State directly from IRAM */
88         uint8_t psr_state;                                              /* 0xf0       */
89         uint8_t dmcu_interface_version;                                 /* 0xf1       */
90         uint8_t dmcu_date_version_year_b0;                              /* 0xf2       */
91         uint8_t dmcu_date_version_year_b1;                              /* 0xf3       */
92         uint8_t dmcu_date_version_month;                                /* 0xf4       */
93         uint8_t dmcu_date_version_day;                                  /* 0xf5       */
94         uint8_t dmcu_state;                                             /* 0xf6       */
95
96         uint16_t blRampReduction;                                       /* 0xf7       */
97         uint16_t blRampStart;                                           /* 0xf9       */
98         uint8_t dummy5;                                                 /* 0xfb       */
99         uint8_t dummy6;                                                 /* 0xfc       */
100         uint8_t dummy7;                                                 /* 0xfd       */
101         uint8_t dummy8;                                                 /* 0xfe       */
102         uint8_t dummy9;                                                 /* 0xff       */
103 };
104 #pragma pack(pop)
105
106 static uint16_t backlight_8_to_16(unsigned int backlight_8bit)
107 {
108         return (uint16_t)(backlight_8bit * 0x101);
109 }
110
111 static void fill_backlight_transform_table(struct dmcu_iram_parameters params,
112                 struct iram_table_v_2 *table)
113 {
114         unsigned int i;
115         unsigned int num_entries = NUM_BL_CURVE_SEGS;
116         unsigned int query_input_8bit;
117         unsigned int query_output_8bit;
118         unsigned int lut_index;
119
120         table->backlight_thresholds[0] = 0;
121         table->backlight_offsets[0] = params.backlight_lut_array[0];
122         table->backlight_thresholds[num_entries-1] = 0xFFFF;
123         table->backlight_offsets[num_entries-1] =
124                 params.backlight_lut_array[params.backlight_lut_array_size - 1];
125
126         /* Setup all brightness levels between 0% and 100% exclusive
127          * Fills brightness-to-backlight transform table. Backlight custom curve
128          * describes transform from brightness to backlight. It will be defined
129          * as set of thresholds and set of offsets, together, implying
130          * extrapolation of custom curve into 16 uniformly spanned linear
131          * segments.  Each threshold/offset represented by 16 bit entry in
132          * format U4.10.
133          */
134         for (i = 1; i+1 < num_entries; i++) {
135                 query_input_8bit = DIV_ROUNDUP((i * 256), num_entries);
136
137                 lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1);
138                 ASSERT(lut_index < params.backlight_lut_array_size);
139                 query_output_8bit = params.backlight_lut_array[lut_index] >> 8;
140
141                 table->backlight_thresholds[i] =
142                                 backlight_8_to_16(query_input_8bit);
143                 table->backlight_offsets[i] =
144                                 backlight_8_to_16(query_output_8bit);
145         }
146 }
147
148 bool dmcu_load_iram(struct dmcu *dmcu,
149         struct dmcu_iram_parameters params)
150 {
151         struct iram_table_v_2 ram_table;
152         unsigned int set = params.set;
153
154         if (dmcu == NULL)
155                 return false;
156
157         if (!dmcu->funcs->is_dmcu_initialized(dmcu))
158                 return true;
159
160         memset(&ram_table, 0, sizeof(ram_table));
161
162         ram_table.flags = 0x0;
163         ram_table.deviation_gain = 0xb3;
164
165         ram_table.blRampReduction =
166                 cpu_to_be16(params.backlight_ramping_reduction);
167         ram_table.blRampStart =
168                 cpu_to_be16(params.backlight_ramping_start);
169
170         ram_table.min_reduction[0][0] = min_reduction_table[abm_config[set][0]];
171         ram_table.min_reduction[1][0] = min_reduction_table[abm_config[set][0]];
172         ram_table.min_reduction[2][0] = min_reduction_table[abm_config[set][0]];
173         ram_table.min_reduction[3][0] = min_reduction_table[abm_config[set][0]];
174         ram_table.min_reduction[4][0] = min_reduction_table[abm_config[set][0]];
175         ram_table.max_reduction[0][0] = max_reduction_table[abm_config[set][0]];
176         ram_table.max_reduction[1][0] = max_reduction_table[abm_config[set][0]];
177         ram_table.max_reduction[2][0] = max_reduction_table[abm_config[set][0]];
178         ram_table.max_reduction[3][0] = max_reduction_table[abm_config[set][0]];
179         ram_table.max_reduction[4][0] = max_reduction_table[abm_config[set][0]];
180
181         ram_table.min_reduction[0][1] = min_reduction_table[abm_config[set][1]];
182         ram_table.min_reduction[1][1] = min_reduction_table[abm_config[set][1]];
183         ram_table.min_reduction[2][1] = min_reduction_table[abm_config[set][1]];
184         ram_table.min_reduction[3][1] = min_reduction_table[abm_config[set][1]];
185         ram_table.min_reduction[4][1] = min_reduction_table[abm_config[set][1]];
186         ram_table.max_reduction[0][1] = max_reduction_table[abm_config[set][1]];
187         ram_table.max_reduction[1][1] = max_reduction_table[abm_config[set][1]];
188         ram_table.max_reduction[2][1] = max_reduction_table[abm_config[set][1]];
189         ram_table.max_reduction[3][1] = max_reduction_table[abm_config[set][1]];
190         ram_table.max_reduction[4][1] = max_reduction_table[abm_config[set][1]];
191
192         ram_table.min_reduction[0][2] = min_reduction_table[abm_config[set][2]];
193         ram_table.min_reduction[1][2] = min_reduction_table[abm_config[set][2]];
194         ram_table.min_reduction[2][2] = min_reduction_table[abm_config[set][2]];
195         ram_table.min_reduction[3][2] = min_reduction_table[abm_config[set][2]];
196         ram_table.min_reduction[4][2] = min_reduction_table[abm_config[set][2]];
197         ram_table.max_reduction[0][2] = max_reduction_table[abm_config[set][2]];
198         ram_table.max_reduction[1][2] = max_reduction_table[abm_config[set][2]];
199         ram_table.max_reduction[2][2] = max_reduction_table[abm_config[set][2]];
200         ram_table.max_reduction[3][2] = max_reduction_table[abm_config[set][2]];
201         ram_table.max_reduction[4][2] = max_reduction_table[abm_config[set][2]];
202
203         ram_table.min_reduction[0][3] = min_reduction_table[abm_config[set][3]];
204         ram_table.min_reduction[1][3] = min_reduction_table[abm_config[set][3]];
205         ram_table.min_reduction[2][3] = min_reduction_table[abm_config[set][3]];
206         ram_table.min_reduction[3][3] = min_reduction_table[abm_config[set][3]];
207         ram_table.min_reduction[4][3] = min_reduction_table[abm_config[set][3]];
208         ram_table.max_reduction[0][3] = max_reduction_table[abm_config[set][3]];
209         ram_table.max_reduction[1][3] = max_reduction_table[abm_config[set][3]];
210         ram_table.max_reduction[2][3] = max_reduction_table[abm_config[set][3]];
211         ram_table.max_reduction[3][3] = max_reduction_table[abm_config[set][3]];
212         ram_table.max_reduction[4][3] = max_reduction_table[abm_config[set][3]];
213
214         ram_table.bright_pos_gain[0][0] = 0x20;
215         ram_table.bright_pos_gain[0][1] = 0x20;
216         ram_table.bright_pos_gain[0][2] = 0x20;
217         ram_table.bright_pos_gain[0][3] = 0x20;
218         ram_table.bright_pos_gain[1][0] = 0x20;
219         ram_table.bright_pos_gain[1][1] = 0x20;
220         ram_table.bright_pos_gain[1][2] = 0x20;
221         ram_table.bright_pos_gain[1][3] = 0x20;
222         ram_table.bright_pos_gain[2][0] = 0x20;
223         ram_table.bright_pos_gain[2][1] = 0x20;
224         ram_table.bright_pos_gain[2][2] = 0x20;
225         ram_table.bright_pos_gain[2][3] = 0x20;
226         ram_table.bright_pos_gain[3][0] = 0x20;
227         ram_table.bright_pos_gain[3][1] = 0x20;
228         ram_table.bright_pos_gain[3][2] = 0x20;
229         ram_table.bright_pos_gain[3][3] = 0x20;
230         ram_table.bright_pos_gain[4][0] = 0x20;
231         ram_table.bright_pos_gain[4][1] = 0x20;
232         ram_table.bright_pos_gain[4][2] = 0x20;
233         ram_table.bright_pos_gain[4][3] = 0x20;
234         ram_table.bright_neg_gain[0][1] = 0x00;
235         ram_table.bright_neg_gain[0][2] = 0x00;
236         ram_table.bright_neg_gain[0][3] = 0x00;
237         ram_table.bright_neg_gain[1][0] = 0x00;
238         ram_table.bright_neg_gain[1][1] = 0x00;
239         ram_table.bright_neg_gain[1][2] = 0x00;
240         ram_table.bright_neg_gain[1][3] = 0x00;
241         ram_table.bright_neg_gain[2][0] = 0x00;
242         ram_table.bright_neg_gain[2][1] = 0x00;
243         ram_table.bright_neg_gain[2][2] = 0x00;
244         ram_table.bright_neg_gain[2][3] = 0x00;
245         ram_table.bright_neg_gain[3][0] = 0x00;
246         ram_table.bright_neg_gain[3][1] = 0x00;
247         ram_table.bright_neg_gain[3][2] = 0x00;
248         ram_table.bright_neg_gain[3][3] = 0x00;
249         ram_table.bright_neg_gain[4][0] = 0x00;
250         ram_table.bright_neg_gain[4][1] = 0x00;
251         ram_table.bright_neg_gain[4][2] = 0x00;
252         ram_table.bright_neg_gain[4][3] = 0x00;
253         ram_table.dark_pos_gain[0][0] = 0x00;
254         ram_table.dark_pos_gain[0][1] = 0x00;
255         ram_table.dark_pos_gain[0][2] = 0x00;
256         ram_table.dark_pos_gain[0][3] = 0x00;
257         ram_table.dark_pos_gain[1][0] = 0x00;
258         ram_table.dark_pos_gain[1][1] = 0x00;
259         ram_table.dark_pos_gain[1][2] = 0x00;
260         ram_table.dark_pos_gain[1][3] = 0x00;
261         ram_table.dark_pos_gain[2][0] = 0x00;
262         ram_table.dark_pos_gain[2][1] = 0x00;
263         ram_table.dark_pos_gain[2][2] = 0x00;
264         ram_table.dark_pos_gain[2][3] = 0x00;
265         ram_table.dark_pos_gain[3][0] = 0x00;
266         ram_table.dark_pos_gain[3][1] = 0x00;
267         ram_table.dark_pos_gain[3][2] = 0x00;
268         ram_table.dark_pos_gain[3][3] = 0x00;
269         ram_table.dark_pos_gain[4][0] = 0x00;
270         ram_table.dark_pos_gain[4][1] = 0x00;
271         ram_table.dark_pos_gain[4][2] = 0x00;
272         ram_table.dark_pos_gain[4][3] = 0x00;
273         ram_table.dark_neg_gain[0][0] = 0x00;
274         ram_table.dark_neg_gain[0][1] = 0x00;
275         ram_table.dark_neg_gain[0][2] = 0x00;
276         ram_table.dark_neg_gain[0][3] = 0x00;
277         ram_table.dark_neg_gain[1][0] = 0x00;
278         ram_table.dark_neg_gain[1][1] = 0x00;
279         ram_table.dark_neg_gain[1][2] = 0x00;
280         ram_table.dark_neg_gain[1][3] = 0x00;
281         ram_table.dark_neg_gain[2][0] = 0x00;
282         ram_table.dark_neg_gain[2][1] = 0x00;
283         ram_table.dark_neg_gain[2][2] = 0x00;
284         ram_table.dark_neg_gain[2][3] = 0x00;
285         ram_table.dark_neg_gain[3][0] = 0x00;
286         ram_table.dark_neg_gain[3][1] = 0x00;
287         ram_table.dark_neg_gain[3][2] = 0x00;
288         ram_table.dark_neg_gain[3][3] = 0x00;
289         ram_table.dark_neg_gain[4][0] = 0x00;
290         ram_table.dark_neg_gain[4][1] = 0x00;
291         ram_table.dark_neg_gain[4][2] = 0x00;
292         ram_table.dark_neg_gain[4][3] = 0x00;
293         ram_table.iir_curve[0] = 0x65;
294         ram_table.iir_curve[1] = 0x65;
295         ram_table.iir_curve[2] = 0x65;
296         ram_table.iir_curve[3] = 0x65;
297         ram_table.iir_curve[4] = 0x65;
298         ram_table.crgb_thresh[0] = cpu_to_be16(0x13b6);
299         ram_table.crgb_thresh[1] = cpu_to_be16(0x1648);
300         ram_table.crgb_thresh[2] = cpu_to_be16(0x18e3);
301         ram_table.crgb_thresh[3] = cpu_to_be16(0x1b41);
302         ram_table.crgb_thresh[4] = cpu_to_be16(0x1d46);
303         ram_table.crgb_thresh[5] = cpu_to_be16(0x1f21);
304         ram_table.crgb_thresh[6] = cpu_to_be16(0x2167);
305         ram_table.crgb_thresh[7] = cpu_to_be16(0x2384);
306         ram_table.crgb_offset[0] = cpu_to_be16(0x2999);
307         ram_table.crgb_offset[1] = cpu_to_be16(0x3999);
308         ram_table.crgb_offset[2] = cpu_to_be16(0x4666);
309         ram_table.crgb_offset[3] = cpu_to_be16(0x5999);
310         ram_table.crgb_offset[4] = cpu_to_be16(0x6333);
311         ram_table.crgb_offset[5] = cpu_to_be16(0x7800);
312         ram_table.crgb_offset[6] = cpu_to_be16(0x8c00);
313         ram_table.crgb_offset[7] = cpu_to_be16(0xa000);
314         ram_table.crgb_slope[0]  = cpu_to_be16(0x3147);
315         ram_table.crgb_slope[1]  = cpu_to_be16(0x2978);
316         ram_table.crgb_slope[2]  = cpu_to_be16(0x23a2);
317         ram_table.crgb_slope[3]  = cpu_to_be16(0x1f55);
318         ram_table.crgb_slope[4]  = cpu_to_be16(0x1c63);
319         ram_table.crgb_slope[5]  = cpu_to_be16(0x1a0f);
320         ram_table.crgb_slope[6]  = cpu_to_be16(0x178d);
321         ram_table.crgb_slope[7]  = cpu_to_be16(0x15ab);
322
323         fill_backlight_transform_table(
324                         params, &ram_table);
325
326         return dmcu->funcs->load_iram(
327                         dmcu, 0, (char *)(&ram_table), sizeof(ram_table));
328 }