GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / media / v4l2-core / v4l2-dv-timings.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * v4l2-dv-timings - dv-timings helper functions
4  *
5  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/rational.h>
13 #include <linux/videodev2.h>
14 #include <linux/v4l2-dv-timings.h>
15 #include <media/v4l2-dv-timings.h>
16 #include <linux/math64.h>
17 #include <linux/hdmi.h>
18 #include <media/cec.h>
19
20 MODULE_AUTHOR("Hans Verkuil");
21 MODULE_DESCRIPTION("V4L2 DV Timings Helper Functions");
22 MODULE_LICENSE("GPL");
23
24 const struct v4l2_dv_timings v4l2_dv_timings_presets[] = {
25         V4L2_DV_BT_CEA_640X480P59_94,
26         V4L2_DV_BT_CEA_720X480I59_94,
27         V4L2_DV_BT_CEA_720X480P59_94,
28         V4L2_DV_BT_CEA_720X576I50,
29         V4L2_DV_BT_CEA_720X576P50,
30         V4L2_DV_BT_CEA_1280X720P24,
31         V4L2_DV_BT_CEA_1280X720P25,
32         V4L2_DV_BT_CEA_1280X720P30,
33         V4L2_DV_BT_CEA_1280X720P50,
34         V4L2_DV_BT_CEA_1280X720P60,
35         V4L2_DV_BT_CEA_1920X1080P24,
36         V4L2_DV_BT_CEA_1920X1080P25,
37         V4L2_DV_BT_CEA_1920X1080P30,
38         V4L2_DV_BT_CEA_1920X1080I50,
39         V4L2_DV_BT_CEA_1920X1080P50,
40         V4L2_DV_BT_CEA_1920X1080I60,
41         V4L2_DV_BT_CEA_1920X1080P60,
42         V4L2_DV_BT_DMT_640X350P85,
43         V4L2_DV_BT_DMT_640X400P85,
44         V4L2_DV_BT_DMT_720X400P85,
45         V4L2_DV_BT_DMT_640X480P72,
46         V4L2_DV_BT_DMT_640X480P75,
47         V4L2_DV_BT_DMT_640X480P85,
48         V4L2_DV_BT_DMT_800X600P56,
49         V4L2_DV_BT_DMT_800X600P60,
50         V4L2_DV_BT_DMT_800X600P72,
51         V4L2_DV_BT_DMT_800X600P75,
52         V4L2_DV_BT_DMT_800X600P85,
53         V4L2_DV_BT_DMT_800X600P120_RB,
54         V4L2_DV_BT_DMT_848X480P60,
55         V4L2_DV_BT_DMT_1024X768I43,
56         V4L2_DV_BT_DMT_1024X768P60,
57         V4L2_DV_BT_DMT_1024X768P70,
58         V4L2_DV_BT_DMT_1024X768P75,
59         V4L2_DV_BT_DMT_1024X768P85,
60         V4L2_DV_BT_DMT_1024X768P120_RB,
61         V4L2_DV_BT_DMT_1152X864P75,
62         V4L2_DV_BT_DMT_1280X768P60_RB,
63         V4L2_DV_BT_DMT_1280X768P60,
64         V4L2_DV_BT_DMT_1280X768P75,
65         V4L2_DV_BT_DMT_1280X768P85,
66         V4L2_DV_BT_DMT_1280X768P120_RB,
67         V4L2_DV_BT_DMT_1280X800P60_RB,
68         V4L2_DV_BT_DMT_1280X800P60,
69         V4L2_DV_BT_DMT_1280X800P75,
70         V4L2_DV_BT_DMT_1280X800P85,
71         V4L2_DV_BT_DMT_1280X800P120_RB,
72         V4L2_DV_BT_DMT_1280X960P60,
73         V4L2_DV_BT_DMT_1280X960P85,
74         V4L2_DV_BT_DMT_1280X960P120_RB,
75         V4L2_DV_BT_DMT_1280X1024P60,
76         V4L2_DV_BT_DMT_1280X1024P75,
77         V4L2_DV_BT_DMT_1280X1024P85,
78         V4L2_DV_BT_DMT_1280X1024P120_RB,
79         V4L2_DV_BT_DMT_1360X768P60,
80         V4L2_DV_BT_DMT_1360X768P120_RB,
81         V4L2_DV_BT_DMT_1366X768P60,
82         V4L2_DV_BT_DMT_1366X768P60_RB,
83         V4L2_DV_BT_DMT_1400X1050P60_RB,
84         V4L2_DV_BT_DMT_1400X1050P60,
85         V4L2_DV_BT_DMT_1400X1050P75,
86         V4L2_DV_BT_DMT_1400X1050P85,
87         V4L2_DV_BT_DMT_1400X1050P120_RB,
88         V4L2_DV_BT_DMT_1440X900P60_RB,
89         V4L2_DV_BT_DMT_1440X900P60,
90         V4L2_DV_BT_DMT_1440X900P75,
91         V4L2_DV_BT_DMT_1440X900P85,
92         V4L2_DV_BT_DMT_1440X900P120_RB,
93         V4L2_DV_BT_DMT_1600X900P60_RB,
94         V4L2_DV_BT_DMT_1600X1200P60,
95         V4L2_DV_BT_DMT_1600X1200P65,
96         V4L2_DV_BT_DMT_1600X1200P70,
97         V4L2_DV_BT_DMT_1600X1200P75,
98         V4L2_DV_BT_DMT_1600X1200P85,
99         V4L2_DV_BT_DMT_1600X1200P120_RB,
100         V4L2_DV_BT_DMT_1680X1050P60_RB,
101         V4L2_DV_BT_DMT_1680X1050P60,
102         V4L2_DV_BT_DMT_1680X1050P75,
103         V4L2_DV_BT_DMT_1680X1050P85,
104         V4L2_DV_BT_DMT_1680X1050P120_RB,
105         V4L2_DV_BT_DMT_1792X1344P60,
106         V4L2_DV_BT_DMT_1792X1344P75,
107         V4L2_DV_BT_DMT_1792X1344P120_RB,
108         V4L2_DV_BT_DMT_1856X1392P60,
109         V4L2_DV_BT_DMT_1856X1392P75,
110         V4L2_DV_BT_DMT_1856X1392P120_RB,
111         V4L2_DV_BT_DMT_1920X1200P60_RB,
112         V4L2_DV_BT_DMT_1920X1200P60,
113         V4L2_DV_BT_DMT_1920X1200P75,
114         V4L2_DV_BT_DMT_1920X1200P85,
115         V4L2_DV_BT_DMT_1920X1200P120_RB,
116         V4L2_DV_BT_DMT_1920X1440P60,
117         V4L2_DV_BT_DMT_1920X1440P75,
118         V4L2_DV_BT_DMT_1920X1440P120_RB,
119         V4L2_DV_BT_DMT_2048X1152P60_RB,
120         V4L2_DV_BT_DMT_2560X1600P60_RB,
121         V4L2_DV_BT_DMT_2560X1600P60,
122         V4L2_DV_BT_DMT_2560X1600P75,
123         V4L2_DV_BT_DMT_2560X1600P85,
124         V4L2_DV_BT_DMT_2560X1600P120_RB,
125         V4L2_DV_BT_CEA_3840X2160P24,
126         V4L2_DV_BT_CEA_3840X2160P25,
127         V4L2_DV_BT_CEA_3840X2160P30,
128         V4L2_DV_BT_CEA_3840X2160P50,
129         V4L2_DV_BT_CEA_3840X2160P60,
130         V4L2_DV_BT_CEA_4096X2160P24,
131         V4L2_DV_BT_CEA_4096X2160P25,
132         V4L2_DV_BT_CEA_4096X2160P30,
133         V4L2_DV_BT_CEA_4096X2160P50,
134         V4L2_DV_BT_DMT_4096X2160P59_94_RB,
135         V4L2_DV_BT_CEA_4096X2160P60,
136         { }
137 };
138 EXPORT_SYMBOL_GPL(v4l2_dv_timings_presets);
139
140 bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
141                            const struct v4l2_dv_timings_cap *dvcap,
142                            v4l2_check_dv_timings_fnc fnc,
143                            void *fnc_handle)
144 {
145         const struct v4l2_bt_timings *bt = &t->bt;
146         const struct v4l2_bt_timings_cap *cap = &dvcap->bt;
147         u32 caps = cap->capabilities;
148
149         if (t->type != V4L2_DV_BT_656_1120)
150                 return false;
151         if (t->type != dvcap->type ||
152             bt->height < cap->min_height ||
153             bt->height > cap->max_height ||
154             bt->width < cap->min_width ||
155             bt->width > cap->max_width ||
156             bt->pixelclock < cap->min_pixelclock ||
157             bt->pixelclock > cap->max_pixelclock ||
158             (!(caps & V4L2_DV_BT_CAP_CUSTOM) &&
159              cap->standards && bt->standards &&
160              !(bt->standards & cap->standards)) ||
161             (bt->interlaced && !(caps & V4L2_DV_BT_CAP_INTERLACED)) ||
162             (!bt->interlaced && !(caps & V4L2_DV_BT_CAP_PROGRESSIVE)))
163                 return false;
164         return fnc == NULL || fnc(t, fnc_handle);
165 }
166 EXPORT_SYMBOL_GPL(v4l2_valid_dv_timings);
167
168 int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
169                              const struct v4l2_dv_timings_cap *cap,
170                              v4l2_check_dv_timings_fnc fnc,
171                              void *fnc_handle)
172 {
173         u32 i, idx;
174
175         memset(t->reserved, 0, sizeof(t->reserved));
176         for (i = idx = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
177                 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
178                                           fnc, fnc_handle) &&
179                     idx++ == t->index) {
180                         t->timings = v4l2_dv_timings_presets[i];
181                         return 0;
182                 }
183         }
184         return -EINVAL;
185 }
186 EXPORT_SYMBOL_GPL(v4l2_enum_dv_timings_cap);
187
188 bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
189                               const struct v4l2_dv_timings_cap *cap,
190                               unsigned pclock_delta,
191                               v4l2_check_dv_timings_fnc fnc,
192                               void *fnc_handle)
193 {
194         int i;
195
196         if (!v4l2_valid_dv_timings(t, cap, fnc, fnc_handle))
197                 return false;
198
199         for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
200                 if (v4l2_valid_dv_timings(v4l2_dv_timings_presets + i, cap,
201                                           fnc, fnc_handle) &&
202                     v4l2_match_dv_timings(t, v4l2_dv_timings_presets + i,
203                                           pclock_delta, false)) {
204                         u32 flags = t->bt.flags & V4L2_DV_FL_REDUCED_FPS;
205
206                         *t = v4l2_dv_timings_presets[i];
207                         if (can_reduce_fps(&t->bt))
208                                 t->bt.flags |= flags;
209
210                         return true;
211                 }
212         }
213         return false;
214 }
215 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
216
217 bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic)
218 {
219         unsigned int i;
220
221         for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
222                 const struct v4l2_bt_timings *bt =
223                         &v4l2_dv_timings_presets[i].bt;
224
225                 if ((bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) &&
226                     bt->cea861_vic == vic) {
227                         *t = v4l2_dv_timings_presets[i];
228                         return true;
229                 }
230         }
231         return false;
232 }
233 EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cea861_vic);
234
235 /**
236  * v4l2_match_dv_timings - check if two timings match
237  * @t1: compare this v4l2_dv_timings struct...
238  * @t2: with this struct.
239  * @pclock_delta: the allowed pixelclock deviation.
240  * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
241  *      match.
242  *
243  * Compare t1 with t2 with a given margin of error for the pixelclock.
244  */
245 bool v4l2_match_dv_timings(const struct v4l2_dv_timings *t1,
246                            const struct v4l2_dv_timings *t2,
247                            unsigned pclock_delta, bool match_reduced_fps)
248 {
249         if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
250                 return false;
251         if (t1->bt.width == t2->bt.width &&
252             t1->bt.height == t2->bt.height &&
253             t1->bt.interlaced == t2->bt.interlaced &&
254             t1->bt.polarities == t2->bt.polarities &&
255             t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
256             t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
257             t1->bt.hfrontporch == t2->bt.hfrontporch &&
258             t1->bt.hsync == t2->bt.hsync &&
259             t1->bt.hbackporch == t2->bt.hbackporch &&
260             t1->bt.vfrontporch == t2->bt.vfrontporch &&
261             t1->bt.vsync == t2->bt.vsync &&
262             t1->bt.vbackporch == t2->bt.vbackporch &&
263             (!match_reduced_fps ||
264              (t1->bt.flags & V4L2_DV_FL_REDUCED_FPS) ==
265                 (t2->bt.flags & V4L2_DV_FL_REDUCED_FPS)) &&
266             (!t1->bt.interlaced ||
267                 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
268                  t1->bt.il_vsync == t2->bt.il_vsync &&
269                  t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
270                 return true;
271         return false;
272 }
273 EXPORT_SYMBOL_GPL(v4l2_match_dv_timings);
274
275 void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
276                            const struct v4l2_dv_timings *t, bool detailed)
277 {
278         const struct v4l2_bt_timings *bt = &t->bt;
279         u32 htot, vtot;
280         u32 fps;
281
282         if (t->type != V4L2_DV_BT_656_1120)
283                 return;
284
285         htot = V4L2_DV_BT_FRAME_WIDTH(bt);
286         vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
287         if (bt->interlaced)
288                 vtot /= 2;
289
290         fps = (htot * vtot) > 0 ? div_u64((100 * (u64)bt->pixelclock),
291                                   (htot * vtot)) : 0;
292
293         if (prefix == NULL)
294                 prefix = "";
295
296         pr_info("%s: %s%ux%u%s%u.%u (%ux%u)\n", dev_prefix, prefix,
297                 bt->width, bt->height, bt->interlaced ? "i" : "p",
298                 fps / 100, fps % 100, htot, vtot);
299
300         if (!detailed)
301                 return;
302
303         pr_info("%s: horizontal: fp = %u, %ssync = %u, bp = %u\n",
304                         dev_prefix, bt->hfrontporch,
305                         (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
306                         bt->hsync, bt->hbackporch);
307         pr_info("%s: vertical: fp = %u, %ssync = %u, bp = %u\n",
308                         dev_prefix, bt->vfrontporch,
309                         (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
310                         bt->vsync, bt->vbackporch);
311         if (bt->interlaced)
312                 pr_info("%s: vertical bottom field: fp = %u, %ssync = %u, bp = %u\n",
313                         dev_prefix, bt->il_vfrontporch,
314                         (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
315                         bt->il_vsync, bt->il_vbackporch);
316         pr_info("%s: pixelclock: %llu\n", dev_prefix, bt->pixelclock);
317         pr_info("%s: flags (0x%x):%s%s%s%s%s%s%s%s%s%s\n",
318                         dev_prefix, bt->flags,
319                         (bt->flags & V4L2_DV_FL_REDUCED_BLANKING) ?
320                         " REDUCED_BLANKING" : "",
321                         ((bt->flags & V4L2_DV_FL_REDUCED_BLANKING) &&
322                          bt->vsync == 8) ? " (V2)" : "",
323                         (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS) ?
324                         " CAN_REDUCE_FPS" : "",
325                         (bt->flags & V4L2_DV_FL_REDUCED_FPS) ?
326                         " REDUCED_FPS" : "",
327                         (bt->flags & V4L2_DV_FL_HALF_LINE) ?
328                         " HALF_LINE" : "",
329                         (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) ?
330                         " CE_VIDEO" : "",
331                         (bt->flags & V4L2_DV_FL_FIRST_FIELD_EXTRA_LINE) ?
332                         " FIRST_FIELD_EXTRA_LINE" : "",
333                         (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT) ?
334                         " HAS_PICTURE_ASPECT" : "",
335                         (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC) ?
336                         " HAS_CEA861_VIC" : "",
337                         (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC) ?
338                         " HAS_HDMI_VIC" : "");
339         pr_info("%s: standards (0x%x):%s%s%s%s%s\n", dev_prefix, bt->standards,
340                         (bt->standards & V4L2_DV_BT_STD_CEA861) ?  " CEA" : "",
341                         (bt->standards & V4L2_DV_BT_STD_DMT) ?  " DMT" : "",
342                         (bt->standards & V4L2_DV_BT_STD_CVT) ?  " CVT" : "",
343                         (bt->standards & V4L2_DV_BT_STD_GTF) ?  " GTF" : "",
344                         (bt->standards & V4L2_DV_BT_STD_SDI) ?  " SDI" : "");
345         if (bt->flags & V4L2_DV_FL_HAS_PICTURE_ASPECT)
346                 pr_info("%s: picture aspect (hor:vert): %u:%u\n", dev_prefix,
347                         bt->picture_aspect.numerator,
348                         bt->picture_aspect.denominator);
349         if (bt->flags & V4L2_DV_FL_HAS_CEA861_VIC)
350                 pr_info("%s: CEA-861 VIC: %u\n", dev_prefix, bt->cea861_vic);
351         if (bt->flags & V4L2_DV_FL_HAS_HDMI_VIC)
352                 pr_info("%s: HDMI VIC: %u\n", dev_prefix, bt->hdmi_vic);
353 }
354 EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);
355
356 struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t)
357 {
358         struct v4l2_fract ratio = { 1, 1 };
359         unsigned long n, d;
360
361         if (t->type != V4L2_DV_BT_656_1120)
362                 return ratio;
363         if (!(t->bt.flags & V4L2_DV_FL_HAS_PICTURE_ASPECT))
364                 return ratio;
365
366         ratio.numerator = t->bt.width * t->bt.picture_aspect.denominator;
367         ratio.denominator = t->bt.height * t->bt.picture_aspect.numerator;
368
369         rational_best_approximation(ratio.numerator, ratio.denominator,
370                                     ratio.numerator, ratio.denominator, &n, &d);
371         ratio.numerator = n;
372         ratio.denominator = d;
373         return ratio;
374 }
375 EXPORT_SYMBOL_GPL(v4l2_dv_timings_aspect_ratio);
376
377 /*
378  * CVT defines
379  * Based on Coordinated Video Timings Standard
380  * version 1.1 September 10, 2003
381  */
382
383 #define CVT_PXL_CLK_GRAN        250000  /* pixel clock granularity */
384 #define CVT_PXL_CLK_GRAN_RB_V2 1000     /* granularity for reduced blanking v2*/
385
386 /* Normal blanking */
387 #define CVT_MIN_V_BPORCH        7       /* lines */
388 #define CVT_MIN_V_PORCH_RND     3       /* lines */
389 #define CVT_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
390 #define CVT_HSYNC_PERCENT       8       /* nominal hsync as percentage of line */
391
392 /* Normal blanking for CVT uses GTF to calculate horizontal blanking */
393 #define CVT_CELL_GRAN           8       /* character cell granularity */
394 #define CVT_M                   600     /* blanking formula gradient */
395 #define CVT_C                   40      /* blanking formula offset */
396 #define CVT_K                   128     /* blanking formula scaling factor */
397 #define CVT_J                   20      /* blanking formula scaling factor */
398 #define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
399 #define CVT_M_PRIME (CVT_K * CVT_M / 256)
400
401 /* Reduced Blanking */
402 #define CVT_RB_MIN_V_BPORCH    7       /* lines  */
403 #define CVT_RB_V_FPORCH        3       /* lines  */
404 #define CVT_RB_MIN_V_BLANK   460       /* us     */
405 #define CVT_RB_H_SYNC         32       /* pixels */
406 #define CVT_RB_H_BLANK       160       /* pixels */
407 /* Reduce blanking Version 2 */
408 #define CVT_RB_V2_H_BLANK     80       /* pixels */
409 #define CVT_RB_MIN_V_FPORCH    3       /* lines  */
410 #define CVT_RB_V2_MIN_V_FPORCH 1       /* lines  */
411 #define CVT_RB_V_BPORCH        6       /* lines  */
412
413 /** v4l2_detect_cvt - detect if the given timings follow the CVT standard
414  * @frame_height - the total height of the frame (including blanking) in lines.
415  * @hfreq - the horizontal frequency in Hz.
416  * @vsync - the height of the vertical sync in lines.
417  * @active_width - active width of image (does not include blanking). This
418  * information is needed only in case of version 2 of reduced blanking.
419  * In other cases, this parameter does not have any effect on timings.
420  * @polarities - the horizontal and vertical polarities (same as struct
421  *              v4l2_bt_timings polarities).
422  * @interlaced - if this flag is true, it indicates interlaced format
423  * @fmt - the resulting timings.
424  *
425  * This function will attempt to detect if the given values correspond to a
426  * valid CVT format. If so, then it will return true, and fmt will be filled
427  * in with the found CVT timings.
428  */
429 bool v4l2_detect_cvt(unsigned frame_height,
430                      unsigned hfreq,
431                      unsigned vsync,
432                      unsigned active_width,
433                      u32 polarities,
434                      bool interlaced,
435                      struct v4l2_dv_timings *fmt)
436 {
437         int  v_fp, v_bp, h_fp, h_bp, hsync;
438         int  frame_width, image_height, image_width;
439         bool reduced_blanking;
440         bool rb_v2 = false;
441         unsigned pix_clk;
442
443         if (vsync < 4 || vsync > 8)
444                 return false;
445
446         if (polarities == V4L2_DV_VSYNC_POS_POL)
447                 reduced_blanking = false;
448         else if (polarities == V4L2_DV_HSYNC_POS_POL)
449                 reduced_blanking = true;
450         else
451                 return false;
452
453         if (reduced_blanking && vsync == 8)
454                 rb_v2 = true;
455
456         if (rb_v2 && active_width == 0)
457                 return false;
458
459         if (!rb_v2 && vsync > 7)
460                 return false;
461
462         if (hfreq == 0)
463                 return false;
464
465         /* Vertical */
466         if (reduced_blanking) {
467                 if (rb_v2) {
468                         v_bp = CVT_RB_V_BPORCH;
469                         v_fp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
470                         v_fp -= vsync + v_bp;
471
472                         if (v_fp < CVT_RB_V2_MIN_V_FPORCH)
473                                 v_fp = CVT_RB_V2_MIN_V_FPORCH;
474                 } else {
475                         v_fp = CVT_RB_V_FPORCH;
476                         v_bp = (CVT_RB_MIN_V_BLANK * hfreq) / 1000000 + 1;
477                         v_bp -= vsync + v_fp;
478
479                         if (v_bp < CVT_RB_MIN_V_BPORCH)
480                                 v_bp = CVT_RB_MIN_V_BPORCH;
481                 }
482         } else {
483                 v_fp = CVT_MIN_V_PORCH_RND;
484                 v_bp = (CVT_MIN_VSYNC_BP * hfreq) / 1000000 + 1 - vsync;
485
486                 if (v_bp < CVT_MIN_V_BPORCH)
487                         v_bp = CVT_MIN_V_BPORCH;
488         }
489
490         if (interlaced)
491                 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
492         else
493                 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
494
495         if (image_height < 0)
496                 return false;
497
498         /* Aspect ratio based on vsync */
499         switch (vsync) {
500         case 4:
501                 image_width = (image_height * 4) / 3;
502                 break;
503         case 5:
504                 image_width = (image_height * 16) / 9;
505                 break;
506         case 6:
507                 image_width = (image_height * 16) / 10;
508                 break;
509         case 7:
510                 /* special case */
511                 if (image_height == 1024)
512                         image_width = (image_height * 5) / 4;
513                 else if (image_height == 768)
514                         image_width = (image_height * 15) / 9;
515                 else
516                         return false;
517                 break;
518         case 8:
519                 image_width = active_width;
520                 break;
521         default:
522                 return false;
523         }
524
525         if (!rb_v2)
526                 image_width = image_width & ~7;
527
528         /* Horizontal */
529         if (reduced_blanking) {
530                 int h_blank;
531                 int clk_gran;
532
533                 h_blank = rb_v2 ? CVT_RB_V2_H_BLANK : CVT_RB_H_BLANK;
534                 clk_gran = rb_v2 ? CVT_PXL_CLK_GRAN_RB_V2 : CVT_PXL_CLK_GRAN;
535
536                 pix_clk = (image_width + h_blank) * hfreq;
537                 pix_clk = (pix_clk / clk_gran) * clk_gran;
538
539                 h_bp  = h_blank / 2;
540                 hsync = CVT_RB_H_SYNC;
541                 h_fp  = h_blank - h_bp - hsync;
542
543                 frame_width = image_width + h_blank;
544         } else {
545                 unsigned ideal_duty_cycle_per_myriad =
546                         100 * CVT_C_PRIME - (CVT_M_PRIME * 100000) / hfreq;
547                 int h_blank;
548
549                 if (ideal_duty_cycle_per_myriad < 2000)
550                         ideal_duty_cycle_per_myriad = 2000;
551
552                 h_blank = image_width * ideal_duty_cycle_per_myriad /
553                                         (10000 - ideal_duty_cycle_per_myriad);
554                 h_blank = (h_blank / (2 * CVT_CELL_GRAN)) * 2 * CVT_CELL_GRAN;
555
556                 pix_clk = (image_width + h_blank) * hfreq;
557                 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
558
559                 h_bp = h_blank / 2;
560                 frame_width = image_width + h_blank;
561
562                 hsync = frame_width * CVT_HSYNC_PERCENT / 100;
563                 hsync = (hsync / CVT_CELL_GRAN) * CVT_CELL_GRAN;
564                 h_fp = h_blank - hsync - h_bp;
565         }
566
567         fmt->type = V4L2_DV_BT_656_1120;
568         fmt->bt.polarities = polarities;
569         fmt->bt.width = image_width;
570         fmt->bt.height = image_height;
571         fmt->bt.hfrontporch = h_fp;
572         fmt->bt.vfrontporch = v_fp;
573         fmt->bt.hsync = hsync;
574         fmt->bt.vsync = vsync;
575         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
576
577         if (!interlaced) {
578                 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
579                 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
580         } else {
581                 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
582                                       2 * vsync) / 2;
583                 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
584                                         2 * vsync - fmt->bt.vbackporch;
585                 fmt->bt.il_vfrontporch = v_fp;
586                 fmt->bt.il_vsync = vsync;
587                 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
588                 fmt->bt.interlaced = V4L2_DV_INTERLACED;
589         }
590
591         fmt->bt.pixelclock = pix_clk;
592         fmt->bt.standards = V4L2_DV_BT_STD_CVT;
593
594         if (reduced_blanking)
595                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
596
597         return true;
598 }
599 EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
600
601 /*
602  * GTF defines
603  * Based on Generalized Timing Formula Standard
604  * Version 1.1 September 2, 1999
605  */
606
607 #define GTF_PXL_CLK_GRAN        250000  /* pixel clock granularity */
608
609 #define GTF_MIN_VSYNC_BP        550     /* min time of vsync + back porch (us) */
610 #define GTF_V_FP                1       /* vertical front porch (lines) */
611 #define GTF_CELL_GRAN           8       /* character cell granularity */
612
613 /* Default */
614 #define GTF_D_M                 600     /* blanking formula gradient */
615 #define GTF_D_C                 40      /* blanking formula offset */
616 #define GTF_D_K                 128     /* blanking formula scaling factor */
617 #define GTF_D_J                 20      /* blanking formula scaling factor */
618 #define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
619 #define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
620
621 /* Secondary */
622 #define GTF_S_M                 3600    /* blanking formula gradient */
623 #define GTF_S_C                 40      /* blanking formula offset */
624 #define GTF_S_K                 128     /* blanking formula scaling factor */
625 #define GTF_S_J                 35      /* blanking formula scaling factor */
626 #define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
627 #define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
628
629 /** v4l2_detect_gtf - detect if the given timings follow the GTF standard
630  * @frame_height - the total height of the frame (including blanking) in lines.
631  * @hfreq - the horizontal frequency in Hz.
632  * @vsync - the height of the vertical sync in lines.
633  * @polarities - the horizontal and vertical polarities (same as struct
634  *              v4l2_bt_timings polarities).
635  * @interlaced - if this flag is true, it indicates interlaced format
636  * @aspect - preferred aspect ratio. GTF has no method of determining the
637  *              aspect ratio in order to derive the image width from the
638  *              image height, so it has to be passed explicitly. Usually
639  *              the native screen aspect ratio is used for this. If it
640  *              is not filled in correctly, then 16:9 will be assumed.
641  * @fmt - the resulting timings.
642  *
643  * This function will attempt to detect if the given values correspond to a
644  * valid GTF format. If so, then it will return true, and fmt will be filled
645  * in with the found GTF timings.
646  */
647 bool v4l2_detect_gtf(unsigned frame_height,
648                 unsigned hfreq,
649                 unsigned vsync,
650                 u32 polarities,
651                 bool interlaced,
652                 struct v4l2_fract aspect,
653                 struct v4l2_dv_timings *fmt)
654 {
655         int pix_clk;
656         int  v_fp, v_bp, h_fp, hsync;
657         int frame_width, image_height, image_width;
658         bool default_gtf;
659         int h_blank;
660
661         if (vsync != 3)
662                 return false;
663
664         if (polarities == V4L2_DV_VSYNC_POS_POL)
665                 default_gtf = true;
666         else if (polarities == V4L2_DV_HSYNC_POS_POL)
667                 default_gtf = false;
668         else
669                 return false;
670
671         if (hfreq == 0)
672                 return false;
673
674         /* Vertical */
675         v_fp = GTF_V_FP;
676         v_bp = (GTF_MIN_VSYNC_BP * hfreq + 500000) / 1000000 - vsync;
677         if (interlaced)
678                 image_height = (frame_height - 2 * v_fp - 2 * vsync - 2 * v_bp) & ~0x1;
679         else
680                 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
681
682         if (image_height < 0)
683                 return false;
684
685         if (aspect.numerator == 0 || aspect.denominator == 0) {
686                 aspect.numerator = 16;
687                 aspect.denominator = 9;
688         }
689         image_width = ((image_height * aspect.numerator) / aspect.denominator);
690         image_width = (image_width + GTF_CELL_GRAN/2) & ~(GTF_CELL_GRAN - 1);
691
692         /* Horizontal */
693         if (default_gtf) {
694                 u64 num;
695                 u32 den;
696
697                 num = ((image_width * GTF_D_C_PRIME * (u64)hfreq) -
698                       ((u64)image_width * GTF_D_M_PRIME * 1000));
699                 den = (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) *
700                       (2 * GTF_CELL_GRAN);
701                 h_blank = div_u64((num + (den >> 1)), den);
702                 h_blank *= (2 * GTF_CELL_GRAN);
703         } else {
704                 u64 num;
705                 u32 den;
706
707                 num = ((image_width * GTF_S_C_PRIME * (u64)hfreq) -
708                       ((u64)image_width * GTF_S_M_PRIME * 1000));
709                 den = (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) *
710                       (2 * GTF_CELL_GRAN);
711                 h_blank = div_u64((num + (den >> 1)), den);
712                 h_blank *= (2 * GTF_CELL_GRAN);
713         }
714
715         frame_width = image_width + h_blank;
716
717         pix_clk = (image_width + h_blank) * hfreq;
718         pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
719
720         hsync = (frame_width * 8 + 50) / 100;
721         hsync = ((hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN) * GTF_CELL_GRAN;
722
723         h_fp = h_blank / 2 - hsync;
724
725         fmt->type = V4L2_DV_BT_656_1120;
726         fmt->bt.polarities = polarities;
727         fmt->bt.width = image_width;
728         fmt->bt.height = image_height;
729         fmt->bt.hfrontporch = h_fp;
730         fmt->bt.vfrontporch = v_fp;
731         fmt->bt.hsync = hsync;
732         fmt->bt.vsync = vsync;
733         fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
734
735         if (!interlaced) {
736                 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
737                 fmt->bt.interlaced = V4L2_DV_PROGRESSIVE;
738         } else {
739                 fmt->bt.vbackporch = (frame_height - image_height - 2 * v_fp -
740                                       2 * vsync) / 2;
741                 fmt->bt.il_vbackporch = frame_height - image_height - 2 * v_fp -
742                                         2 * vsync - fmt->bt.vbackporch;
743                 fmt->bt.il_vfrontporch = v_fp;
744                 fmt->bt.il_vsync = vsync;
745                 fmt->bt.flags |= V4L2_DV_FL_HALF_LINE;
746                 fmt->bt.interlaced = V4L2_DV_INTERLACED;
747         }
748
749         fmt->bt.pixelclock = pix_clk;
750         fmt->bt.standards = V4L2_DV_BT_STD_GTF;
751
752         if (!default_gtf)
753                 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
754
755         return true;
756 }
757 EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
758
759 /** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
760  *      0x15 and 0x16 from the EDID.
761  * @hor_landscape - byte 0x15 from the EDID.
762  * @vert_portrait - byte 0x16 from the EDID.
763  *
764  * Determines the aspect ratio from the EDID.
765  * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
766  * "Horizontal and Vertical Screen Size or Aspect Ratio"
767  */
768 struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
769 {
770         struct v4l2_fract aspect = { 16, 9 };
771         u8 ratio;
772
773         /* Nothing filled in, fallback to 16:9 */
774         if (!hor_landscape && !vert_portrait)
775                 return aspect;
776         /* Both filled in, so they are interpreted as the screen size in cm */
777         if (hor_landscape && vert_portrait) {
778                 aspect.numerator = hor_landscape;
779                 aspect.denominator = vert_portrait;
780                 return aspect;
781         }
782         /* Only one is filled in, so interpret them as a ratio:
783            (val + 99) / 100 */
784         ratio = hor_landscape | vert_portrait;
785         /* Change some rounded values into the exact aspect ratio */
786         if (ratio == 79) {
787                 aspect.numerator = 16;
788                 aspect.denominator = 9;
789         } else if (ratio == 34) {
790                 aspect.numerator = 4;
791                 aspect.denominator = 3;
792         } else if (ratio == 68) {
793                 aspect.numerator = 15;
794                 aspect.denominator = 9;
795         } else {
796                 aspect.numerator = hor_landscape + 99;
797                 aspect.denominator = 100;
798         }
799         if (hor_landscape)
800                 return aspect;
801         /* The aspect ratio is for portrait, so swap numerator and denominator */
802         swap(aspect.denominator, aspect.numerator);
803         return aspect;
804 }
805 EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
806
807 /** v4l2_hdmi_rx_colorimetry - determine HDMI colorimetry information
808  *      based on various InfoFrames.
809  * @avi: the AVI InfoFrame
810  * @hdmi: the HDMI Vendor InfoFrame, may be NULL
811  * @height: the frame height
812  *
813  * Determines the HDMI colorimetry information, i.e. how the HDMI
814  * pixel color data should be interpreted.
815  *
816  * Note that some of the newer features (DCI-P3, HDR) are not yet
817  * implemented: the hdmi.h header needs to be updated to the HDMI 2.0
818  * and CTA-861-G standards.
819  */
820 struct v4l2_hdmi_colorimetry
821 v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
822                          const struct hdmi_vendor_infoframe *hdmi,
823                          unsigned int height)
824 {
825         struct v4l2_hdmi_colorimetry c = {
826                 V4L2_COLORSPACE_SRGB,
827                 V4L2_YCBCR_ENC_DEFAULT,
828                 V4L2_QUANTIZATION_FULL_RANGE,
829                 V4L2_XFER_FUNC_SRGB
830         };
831         bool is_ce = avi->video_code || (hdmi && hdmi->vic);
832         bool is_sdtv = height <= 576;
833         bool default_is_lim_range_rgb = avi->video_code > 1;
834
835         switch (avi->colorspace) {
836         case HDMI_COLORSPACE_RGB:
837                 /* RGB pixel encoding */
838                 switch (avi->colorimetry) {
839                 case HDMI_COLORIMETRY_EXTENDED:
840                         switch (avi->extended_colorimetry) {
841                         case HDMI_EXTENDED_COLORIMETRY_OPRGB:
842                                 c.colorspace = V4L2_COLORSPACE_OPRGB;
843                                 c.xfer_func = V4L2_XFER_FUNC_OPRGB;
844                                 break;
845                         case HDMI_EXTENDED_COLORIMETRY_BT2020:
846                                 c.colorspace = V4L2_COLORSPACE_BT2020;
847                                 c.xfer_func = V4L2_XFER_FUNC_709;
848                                 break;
849                         default:
850                                 break;
851                         }
852                         break;
853                 default:
854                         break;
855                 }
856                 switch (avi->quantization_range) {
857                 case HDMI_QUANTIZATION_RANGE_LIMITED:
858                         c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
859                         break;
860                 case HDMI_QUANTIZATION_RANGE_FULL:
861                         break;
862                 default:
863                         if (default_is_lim_range_rgb)
864                                 c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
865                         break;
866                 }
867                 break;
868
869         default:
870                 /* YCbCr pixel encoding */
871                 c.quantization = V4L2_QUANTIZATION_LIM_RANGE;
872                 switch (avi->colorimetry) {
873                 case HDMI_COLORIMETRY_NONE:
874                         if (!is_ce)
875                                 break;
876                         if (is_sdtv) {
877                                 c.colorspace = V4L2_COLORSPACE_SMPTE170M;
878                                 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
879                         } else {
880                                 c.colorspace = V4L2_COLORSPACE_REC709;
881                                 c.ycbcr_enc = V4L2_YCBCR_ENC_709;
882                         }
883                         c.xfer_func = V4L2_XFER_FUNC_709;
884                         break;
885                 case HDMI_COLORIMETRY_ITU_601:
886                         c.colorspace = V4L2_COLORSPACE_SMPTE170M;
887                         c.ycbcr_enc = V4L2_YCBCR_ENC_601;
888                         c.xfer_func = V4L2_XFER_FUNC_709;
889                         break;
890                 case HDMI_COLORIMETRY_ITU_709:
891                         c.colorspace = V4L2_COLORSPACE_REC709;
892                         c.ycbcr_enc = V4L2_YCBCR_ENC_709;
893                         c.xfer_func = V4L2_XFER_FUNC_709;
894                         break;
895                 case HDMI_COLORIMETRY_EXTENDED:
896                         switch (avi->extended_colorimetry) {
897                         case HDMI_EXTENDED_COLORIMETRY_XV_YCC_601:
898                                 c.colorspace = V4L2_COLORSPACE_REC709;
899                                 c.ycbcr_enc = V4L2_YCBCR_ENC_XV709;
900                                 c.xfer_func = V4L2_XFER_FUNC_709;
901                                 break;
902                         case HDMI_EXTENDED_COLORIMETRY_XV_YCC_709:
903                                 c.colorspace = V4L2_COLORSPACE_REC709;
904                                 c.ycbcr_enc = V4L2_YCBCR_ENC_XV601;
905                                 c.xfer_func = V4L2_XFER_FUNC_709;
906                                 break;
907                         case HDMI_EXTENDED_COLORIMETRY_S_YCC_601:
908                                 c.colorspace = V4L2_COLORSPACE_SRGB;
909                                 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
910                                 c.xfer_func = V4L2_XFER_FUNC_SRGB;
911                                 break;
912                         case HDMI_EXTENDED_COLORIMETRY_OPYCC_601:
913                                 c.colorspace = V4L2_COLORSPACE_OPRGB;
914                                 c.ycbcr_enc = V4L2_YCBCR_ENC_601;
915                                 c.xfer_func = V4L2_XFER_FUNC_OPRGB;
916                                 break;
917                         case HDMI_EXTENDED_COLORIMETRY_BT2020:
918                                 c.colorspace = V4L2_COLORSPACE_BT2020;
919                                 c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020;
920                                 c.xfer_func = V4L2_XFER_FUNC_709;
921                                 break;
922                         case HDMI_EXTENDED_COLORIMETRY_BT2020_CONST_LUM:
923                                 c.colorspace = V4L2_COLORSPACE_BT2020;
924                                 c.ycbcr_enc = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
925                                 c.xfer_func = V4L2_XFER_FUNC_709;
926                                 break;
927                         default: /* fall back to ITU_709 */
928                                 c.colorspace = V4L2_COLORSPACE_REC709;
929                                 c.ycbcr_enc = V4L2_YCBCR_ENC_709;
930                                 c.xfer_func = V4L2_XFER_FUNC_709;
931                                 break;
932                         }
933                         break;
934                 default:
935                         break;
936                 }
937                 /*
938                  * YCC Quantization Range signaling is more-or-less broken,
939                  * let's just ignore this.
940                  */
941                 break;
942         }
943         return c;
944 }
945 EXPORT_SYMBOL_GPL(v4l2_hdmi_rx_colorimetry);
946
947 /**
948  * v4l2_get_edid_phys_addr() - find and return the physical address
949  *
950  * @edid:       pointer to the EDID data
951  * @size:       size in bytes of the EDID data
952  * @offset:     If not %NULL then the location of the physical address
953  *              bytes in the EDID will be returned here. This is set to 0
954  *              if there is no physical address found.
955  *
956  * Return: the physical address or CEC_PHYS_ADDR_INVALID if there is none.
957  */
958 u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
959                             unsigned int *offset)
960 {
961         unsigned int loc = cec_get_edid_spa_location(edid, size);
962
963         if (offset)
964                 *offset = loc;
965         if (loc == 0)
966                 return CEC_PHYS_ADDR_INVALID;
967         return (edid[loc] << 8) | edid[loc + 1];
968 }
969 EXPORT_SYMBOL_GPL(v4l2_get_edid_phys_addr);
970
971 /**
972  * v4l2_set_edid_phys_addr() - find and set the physical address
973  *
974  * @edid:       pointer to the EDID data
975  * @size:       size in bytes of the EDID data
976  * @phys_addr:  the new physical address
977  *
978  * This function finds the location of the physical address in the EDID
979  * and fills in the given physical address and updates the checksum
980  * at the end of the EDID block. It does nothing if the EDID doesn't
981  * contain a physical address.
982  */
983 void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
984 {
985         unsigned int loc = cec_get_edid_spa_location(edid, size);
986         u8 sum = 0;
987         unsigned int i;
988
989         if (loc == 0)
990                 return;
991         edid[loc] = phys_addr >> 8;
992         edid[loc + 1] = phys_addr & 0xff;
993         loc &= ~0x7f;
994
995         /* update the checksum */
996         for (i = loc; i < loc + 127; i++)
997                 sum += edid[i];
998         edid[i] = 256 - sum;
999 }
1000 EXPORT_SYMBOL_GPL(v4l2_set_edid_phys_addr);
1001
1002 /**
1003  * v4l2_phys_addr_for_input() - calculate the PA for an input
1004  *
1005  * @phys_addr:  the physical address of the parent
1006  * @input:      the number of the input port, must be between 1 and 15
1007  *
1008  * This function calculates a new physical address based on the input
1009  * port number. For example:
1010  *
1011  * PA = 0.0.0.0 and input = 2 becomes 2.0.0.0
1012  *
1013  * PA = 3.0.0.0 and input = 1 becomes 3.1.0.0
1014  *
1015  * PA = 3.2.1.0 and input = 5 becomes 3.2.1.5
1016  *
1017  * PA = 3.2.1.3 and input = 5 becomes f.f.f.f since it maxed out the depth.
1018  *
1019  * Return: the new physical address or CEC_PHYS_ADDR_INVALID.
1020  */
1021 u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input)
1022 {
1023         /* Check if input is sane */
1024         if (WARN_ON(input == 0 || input > 0xf))
1025                 return CEC_PHYS_ADDR_INVALID;
1026
1027         if (phys_addr == 0)
1028                 return input << 12;
1029
1030         if ((phys_addr & 0x0fff) == 0)
1031                 return phys_addr | (input << 8);
1032
1033         if ((phys_addr & 0x00ff) == 0)
1034                 return phys_addr | (input << 4);
1035
1036         if ((phys_addr & 0x000f) == 0)
1037                 return phys_addr | input;
1038
1039         /*
1040          * All nibbles are used so no valid physical addresses can be assigned
1041          * to the input.
1042          */
1043         return CEC_PHYS_ADDR_INVALID;
1044 }
1045 EXPORT_SYMBOL_GPL(v4l2_phys_addr_for_input);
1046
1047 /**
1048  * v4l2_phys_addr_validate() - validate a physical address from an EDID
1049  *
1050  * @phys_addr:  the physical address to validate
1051  * @parent:     if not %NULL, then this is filled with the parents PA.
1052  * @port:       if not %NULL, then this is filled with the input port.
1053  *
1054  * This validates a physical address as read from an EDID. If the
1055  * PA is invalid (such as 1.0.1.0 since '0' is only allowed at the end),
1056  * then it will return -EINVAL.
1057  *
1058  * The parent PA is passed into %parent and the input port is passed into
1059  * %port. For example:
1060  *
1061  * PA = 0.0.0.0: has parent 0.0.0.0 and input port 0.
1062  *
1063  * PA = 1.0.0.0: has parent 0.0.0.0 and input port 1.
1064  *
1065  * PA = 3.2.0.0: has parent 3.0.0.0 and input port 2.
1066  *
1067  * PA = f.f.f.f: has parent f.f.f.f and input port 0.
1068  *
1069  * Return: 0 if the PA is valid, -EINVAL if not.
1070  */
1071 int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
1072 {
1073         int i;
1074
1075         if (parent)
1076                 *parent = phys_addr;
1077         if (port)
1078                 *port = 0;
1079         if (phys_addr == CEC_PHYS_ADDR_INVALID)
1080                 return 0;
1081         for (i = 0; i < 16; i += 4)
1082                 if (phys_addr & (0xf << i))
1083                         break;
1084         if (i == 16)
1085                 return 0;
1086         if (parent)
1087                 *parent = phys_addr & (0xfff0 << i);
1088         if (port)
1089                 *port = (phys_addr >> i) & 0xf;
1090         for (i += 4; i < 16; i += 4)
1091                 if ((phys_addr & (0xf << i)) == 0)
1092                         return -EINVAL;
1093         return 0;
1094 }
1095 EXPORT_SYMBOL_GPL(v4l2_phys_addr_validate);