xref: /linux/drivers/media/test-drivers/vivid/vivid-ctrls.c (revision 3a38ef2b3cb6b63c105247b5ea4a9cf600e673f0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-ctrls.c - control support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/videodev2.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-common.h>
13 
14 #include "vivid-core.h"
15 #include "vivid-vid-cap.h"
16 #include "vivid-vid-out.h"
17 #include "vivid-vid-common.h"
18 #include "vivid-radio-common.h"
19 #include "vivid-osd.h"
20 #include "vivid-ctrls.h"
21 #include "vivid-cec.h"
22 
23 #define VIVID_CID_CUSTOM_BASE		(V4L2_CID_USER_BASE | 0xf000)
24 #define VIVID_CID_BUTTON		(VIVID_CID_CUSTOM_BASE + 0)
25 #define VIVID_CID_BOOLEAN		(VIVID_CID_CUSTOM_BASE + 1)
26 #define VIVID_CID_INTEGER		(VIVID_CID_CUSTOM_BASE + 2)
27 #define VIVID_CID_INTEGER64		(VIVID_CID_CUSTOM_BASE + 3)
28 #define VIVID_CID_MENU			(VIVID_CID_CUSTOM_BASE + 4)
29 #define VIVID_CID_STRING		(VIVID_CID_CUSTOM_BASE + 5)
30 #define VIVID_CID_BITMASK		(VIVID_CID_CUSTOM_BASE + 6)
31 #define VIVID_CID_INTMENU		(VIVID_CID_CUSTOM_BASE + 7)
32 #define VIVID_CID_U32_ARRAY		(VIVID_CID_CUSTOM_BASE + 8)
33 #define VIVID_CID_U16_MATRIX		(VIVID_CID_CUSTOM_BASE + 9)
34 #define VIVID_CID_U8_4D_ARRAY		(VIVID_CID_CUSTOM_BASE + 10)
35 #define VIVID_CID_AREA			(VIVID_CID_CUSTOM_BASE + 11)
36 #define VIVID_CID_RO_INTEGER		(VIVID_CID_CUSTOM_BASE + 12)
37 #define VIVID_CID_U32_DYN_ARRAY		(VIVID_CID_CUSTOM_BASE + 13)
38 #define VIVID_CID_U8_PIXEL_ARRAY	(VIVID_CID_CUSTOM_BASE + 14)
39 
40 #define VIVID_CID_VIVID_BASE		(0x00f00000 | 0xf000)
41 #define VIVID_CID_VIVID_CLASS		(0x00f00000 | 1)
42 #define VIVID_CID_TEST_PATTERN		(VIVID_CID_VIVID_BASE + 0)
43 #define VIVID_CID_OSD_TEXT_MODE		(VIVID_CID_VIVID_BASE + 1)
44 #define VIVID_CID_HOR_MOVEMENT		(VIVID_CID_VIVID_BASE + 2)
45 #define VIVID_CID_VERT_MOVEMENT		(VIVID_CID_VIVID_BASE + 3)
46 #define VIVID_CID_SHOW_BORDER		(VIVID_CID_VIVID_BASE + 4)
47 #define VIVID_CID_SHOW_SQUARE		(VIVID_CID_VIVID_BASE + 5)
48 #define VIVID_CID_INSERT_SAV		(VIVID_CID_VIVID_BASE + 6)
49 #define VIVID_CID_INSERT_EAV		(VIVID_CID_VIVID_BASE + 7)
50 #define VIVID_CID_VBI_CAP_INTERLACED	(VIVID_CID_VIVID_BASE + 8)
51 #define VIVID_CID_INSERT_HDMI_VIDEO_GUARD_BAND (VIVID_CID_VIVID_BASE + 9)
52 
53 #define VIVID_CID_HFLIP			(VIVID_CID_VIVID_BASE + 20)
54 #define VIVID_CID_VFLIP			(VIVID_CID_VIVID_BASE + 21)
55 #define VIVID_CID_STD_ASPECT_RATIO	(VIVID_CID_VIVID_BASE + 22)
56 #define VIVID_CID_DV_TIMINGS_ASPECT_RATIO	(VIVID_CID_VIVID_BASE + 23)
57 #define VIVID_CID_TSTAMP_SRC		(VIVID_CID_VIVID_BASE + 24)
58 #define VIVID_CID_COLORSPACE		(VIVID_CID_VIVID_BASE + 25)
59 #define VIVID_CID_XFER_FUNC		(VIVID_CID_VIVID_BASE + 26)
60 #define VIVID_CID_YCBCR_ENC		(VIVID_CID_VIVID_BASE + 27)
61 #define VIVID_CID_QUANTIZATION		(VIVID_CID_VIVID_BASE + 28)
62 #define VIVID_CID_LIMITED_RGB_RANGE	(VIVID_CID_VIVID_BASE + 29)
63 #define VIVID_CID_ALPHA_MODE		(VIVID_CID_VIVID_BASE + 30)
64 #define VIVID_CID_HAS_CROP_CAP		(VIVID_CID_VIVID_BASE + 31)
65 #define VIVID_CID_HAS_COMPOSE_CAP	(VIVID_CID_VIVID_BASE + 32)
66 #define VIVID_CID_HAS_SCALER_CAP	(VIVID_CID_VIVID_BASE + 33)
67 #define VIVID_CID_HAS_CROP_OUT		(VIVID_CID_VIVID_BASE + 34)
68 #define VIVID_CID_HAS_COMPOSE_OUT	(VIVID_CID_VIVID_BASE + 35)
69 #define VIVID_CID_HAS_SCALER_OUT	(VIVID_CID_VIVID_BASE + 36)
70 #define VIVID_CID_LOOP_VIDEO		(VIVID_CID_VIVID_BASE + 37)
71 #define VIVID_CID_SEQ_WRAP		(VIVID_CID_VIVID_BASE + 38)
72 #define VIVID_CID_TIME_WRAP		(VIVID_CID_VIVID_BASE + 39)
73 #define VIVID_CID_MAX_EDID_BLOCKS	(VIVID_CID_VIVID_BASE + 40)
74 #define VIVID_CID_PERCENTAGE_FILL	(VIVID_CID_VIVID_BASE + 41)
75 #define VIVID_CID_REDUCED_FPS		(VIVID_CID_VIVID_BASE + 42)
76 #define VIVID_CID_HSV_ENC		(VIVID_CID_VIVID_BASE + 43)
77 #define VIVID_CID_DISPLAY_PRESENT	(VIVID_CID_VIVID_BASE + 44)
78 
79 #define VIVID_CID_STD_SIGNAL_MODE	(VIVID_CID_VIVID_BASE + 60)
80 #define VIVID_CID_STANDARD		(VIVID_CID_VIVID_BASE + 61)
81 #define VIVID_CID_DV_TIMINGS_SIGNAL_MODE	(VIVID_CID_VIVID_BASE + 62)
82 #define VIVID_CID_DV_TIMINGS		(VIVID_CID_VIVID_BASE + 63)
83 #define VIVID_CID_PERC_DROPPED		(VIVID_CID_VIVID_BASE + 64)
84 #define VIVID_CID_DISCONNECT		(VIVID_CID_VIVID_BASE + 65)
85 #define VIVID_CID_DQBUF_ERROR		(VIVID_CID_VIVID_BASE + 66)
86 #define VIVID_CID_QUEUE_SETUP_ERROR	(VIVID_CID_VIVID_BASE + 67)
87 #define VIVID_CID_BUF_PREPARE_ERROR	(VIVID_CID_VIVID_BASE + 68)
88 #define VIVID_CID_START_STR_ERROR	(VIVID_CID_VIVID_BASE + 69)
89 #define VIVID_CID_QUEUE_ERROR		(VIVID_CID_VIVID_BASE + 70)
90 #define VIVID_CID_CLEAR_FB		(VIVID_CID_VIVID_BASE + 71)
91 #define VIVID_CID_REQ_VALIDATE_ERROR	(VIVID_CID_VIVID_BASE + 72)
92 
93 #define VIVID_CID_RADIO_SEEK_MODE	(VIVID_CID_VIVID_BASE + 90)
94 #define VIVID_CID_RADIO_SEEK_PROG_LIM	(VIVID_CID_VIVID_BASE + 91)
95 #define VIVID_CID_RADIO_RX_RDS_RBDS	(VIVID_CID_VIVID_BASE + 92)
96 #define VIVID_CID_RADIO_RX_RDS_BLOCKIO	(VIVID_CID_VIVID_BASE + 93)
97 
98 #define VIVID_CID_RADIO_TX_RDS_BLOCKIO	(VIVID_CID_VIVID_BASE + 94)
99 
100 #define VIVID_CID_SDR_CAP_FM_DEVIATION	(VIVID_CID_VIVID_BASE + 110)
101 
102 #define VIVID_CID_META_CAP_GENERATE_PTS	(VIVID_CID_VIVID_BASE + 111)
103 #define VIVID_CID_META_CAP_GENERATE_SCR	(VIVID_CID_VIVID_BASE + 112)
104 
105 /* General User Controls */
106 
107 static void vivid_unregister_dev(bool valid, struct video_device *vdev)
108 {
109 	if (!valid)
110 		return;
111 	clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
112 	v4l2_event_wake_all(vdev);
113 }
114 
115 static int vivid_user_gen_s_ctrl(struct v4l2_ctrl *ctrl)
116 {
117 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_user_gen);
118 
119 	switch (ctrl->id) {
120 	case VIVID_CID_DISCONNECT:
121 		v4l2_info(&dev->v4l2_dev, "disconnect\n");
122 		dev->disconnect_error = true;
123 		vivid_unregister_dev(dev->has_vid_cap, &dev->vid_cap_dev);
124 		vivid_unregister_dev(dev->has_vid_out, &dev->vid_out_dev);
125 		vivid_unregister_dev(dev->has_vbi_cap, &dev->vbi_cap_dev);
126 		vivid_unregister_dev(dev->has_vbi_out, &dev->vbi_out_dev);
127 		vivid_unregister_dev(dev->has_radio_rx, &dev->radio_rx_dev);
128 		vivid_unregister_dev(dev->has_radio_tx, &dev->radio_tx_dev);
129 		vivid_unregister_dev(dev->has_sdr_cap, &dev->sdr_cap_dev);
130 		vivid_unregister_dev(dev->has_meta_cap, &dev->meta_cap_dev);
131 		vivid_unregister_dev(dev->has_meta_out, &dev->meta_out_dev);
132 		vivid_unregister_dev(dev->has_touch_cap, &dev->touch_cap_dev);
133 		break;
134 	case VIVID_CID_BUTTON:
135 		dev->button_pressed = 30;
136 		break;
137 	}
138 	return 0;
139 }
140 
141 static const struct v4l2_ctrl_ops vivid_user_gen_ctrl_ops = {
142 	.s_ctrl = vivid_user_gen_s_ctrl,
143 };
144 
145 static const struct v4l2_ctrl_config vivid_ctrl_button = {
146 	.ops = &vivid_user_gen_ctrl_ops,
147 	.id = VIVID_CID_BUTTON,
148 	.name = "Button",
149 	.type = V4L2_CTRL_TYPE_BUTTON,
150 };
151 
152 static const struct v4l2_ctrl_config vivid_ctrl_boolean = {
153 	.ops = &vivid_user_gen_ctrl_ops,
154 	.id = VIVID_CID_BOOLEAN,
155 	.name = "Boolean",
156 	.type = V4L2_CTRL_TYPE_BOOLEAN,
157 	.min = 0,
158 	.max = 1,
159 	.step = 1,
160 	.def = 1,
161 };
162 
163 static const struct v4l2_ctrl_config vivid_ctrl_int32 = {
164 	.ops = &vivid_user_gen_ctrl_ops,
165 	.id = VIVID_CID_INTEGER,
166 	.name = "Integer 32 Bits",
167 	.type = V4L2_CTRL_TYPE_INTEGER,
168 	.min = 0xffffffff80000000ULL,
169 	.max = 0x7fffffff,
170 	.step = 1,
171 };
172 
173 static const struct v4l2_ctrl_config vivid_ctrl_int64 = {
174 	.ops = &vivid_user_gen_ctrl_ops,
175 	.id = VIVID_CID_INTEGER64,
176 	.name = "Integer 64 Bits",
177 	.type = V4L2_CTRL_TYPE_INTEGER64,
178 	.min = 0x8000000000000000ULL,
179 	.max = 0x7fffffffffffffffLL,
180 	.step = 1,
181 };
182 
183 static const struct v4l2_ctrl_config vivid_ctrl_u32_array = {
184 	.ops = &vivid_user_gen_ctrl_ops,
185 	.id = VIVID_CID_U32_ARRAY,
186 	.name = "U32 1 Element Array",
187 	.type = V4L2_CTRL_TYPE_U32,
188 	.def = 0x18,
189 	.min = 0x10,
190 	.max = 0x20000,
191 	.step = 1,
192 	.dims = { 1 },
193 };
194 
195 static const struct v4l2_ctrl_config vivid_ctrl_u32_dyn_array = {
196 	.ops = &vivid_user_gen_ctrl_ops,
197 	.id = VIVID_CID_U32_DYN_ARRAY,
198 	.name = "U32 Dynamic Array",
199 	.type = V4L2_CTRL_TYPE_U32,
200 	.flags = V4L2_CTRL_FLAG_DYNAMIC_ARRAY,
201 	.def = 50,
202 	.min = 10,
203 	.max = 90,
204 	.step = 1,
205 	.dims = { 100 },
206 };
207 
208 static const struct v4l2_ctrl_config vivid_ctrl_u16_matrix = {
209 	.ops = &vivid_user_gen_ctrl_ops,
210 	.id = VIVID_CID_U16_MATRIX,
211 	.name = "U16 8x16 Matrix",
212 	.type = V4L2_CTRL_TYPE_U16,
213 	.def = 0x18,
214 	.min = 0x10,
215 	.max = 0x2000,
216 	.step = 1,
217 	.dims = { 8, 16 },
218 };
219 
220 static const struct v4l2_ctrl_config vivid_ctrl_u8_4d_array = {
221 	.ops = &vivid_user_gen_ctrl_ops,
222 	.id = VIVID_CID_U8_4D_ARRAY,
223 	.name = "U8 2x3x4x5 Array",
224 	.type = V4L2_CTRL_TYPE_U8,
225 	.def = 0x18,
226 	.min = 0x10,
227 	.max = 0x20,
228 	.step = 1,
229 	.dims = { 2, 3, 4, 5 },
230 };
231 
232 static const struct v4l2_ctrl_config vivid_ctrl_u8_pixel_array = {
233 	.ops = &vivid_user_gen_ctrl_ops,
234 	.id = VIVID_CID_U8_PIXEL_ARRAY,
235 	.name = "U8 Pixel Array",
236 	.type = V4L2_CTRL_TYPE_U8,
237 	.def = 0x80,
238 	.min = 0x00,
239 	.max = 0xff,
240 	.step = 1,
241 	.dims = { 640 / PIXEL_ARRAY_DIV, 360 / PIXEL_ARRAY_DIV },
242 };
243 
244 static const char * const vivid_ctrl_menu_strings[] = {
245 	"Menu Item 0 (Skipped)",
246 	"Menu Item 1",
247 	"Menu Item 2 (Skipped)",
248 	"Menu Item 3",
249 	"Menu Item 4",
250 	"Menu Item 5 (Skipped)",
251 	NULL,
252 };
253 
254 static const struct v4l2_ctrl_config vivid_ctrl_menu = {
255 	.ops = &vivid_user_gen_ctrl_ops,
256 	.id = VIVID_CID_MENU,
257 	.name = "Menu",
258 	.type = V4L2_CTRL_TYPE_MENU,
259 	.min = 1,
260 	.max = 4,
261 	.def = 3,
262 	.menu_skip_mask = 0x04,
263 	.qmenu = vivid_ctrl_menu_strings,
264 };
265 
266 static const struct v4l2_ctrl_config vivid_ctrl_string = {
267 	.ops = &vivid_user_gen_ctrl_ops,
268 	.id = VIVID_CID_STRING,
269 	.name = "String",
270 	.type = V4L2_CTRL_TYPE_STRING,
271 	.min = 2,
272 	.max = 4,
273 	.step = 1,
274 };
275 
276 static const struct v4l2_ctrl_config vivid_ctrl_bitmask = {
277 	.ops = &vivid_user_gen_ctrl_ops,
278 	.id = VIVID_CID_BITMASK,
279 	.name = "Bitmask",
280 	.type = V4L2_CTRL_TYPE_BITMASK,
281 	.def = 0x80002000,
282 	.min = 0,
283 	.max = 0x80402010,
284 	.step = 0,
285 };
286 
287 static const s64 vivid_ctrl_int_menu_values[] = {
288 	1, 1, 2, 3, 5, 8, 13, 21, 42,
289 };
290 
291 static const struct v4l2_ctrl_config vivid_ctrl_int_menu = {
292 	.ops = &vivid_user_gen_ctrl_ops,
293 	.id = VIVID_CID_INTMENU,
294 	.name = "Integer Menu",
295 	.type = V4L2_CTRL_TYPE_INTEGER_MENU,
296 	.min = 1,
297 	.max = 8,
298 	.def = 4,
299 	.menu_skip_mask = 0x02,
300 	.qmenu_int = vivid_ctrl_int_menu_values,
301 };
302 
303 static const struct v4l2_ctrl_config vivid_ctrl_disconnect = {
304 	.ops = &vivid_user_gen_ctrl_ops,
305 	.id = VIVID_CID_DISCONNECT,
306 	.name = "Disconnect",
307 	.type = V4L2_CTRL_TYPE_BUTTON,
308 };
309 
310 static const struct v4l2_area area = {
311 	.width = 1000,
312 	.height = 2000,
313 };
314 
315 static const struct v4l2_ctrl_config vivid_ctrl_area = {
316 	.ops = &vivid_user_gen_ctrl_ops,
317 	.id = VIVID_CID_AREA,
318 	.name = "Area",
319 	.type = V4L2_CTRL_TYPE_AREA,
320 	.p_def.p_const = &area,
321 };
322 
323 static const struct v4l2_ctrl_config vivid_ctrl_ro_int32 = {
324 	.ops = &vivid_user_gen_ctrl_ops,
325 	.id = VIVID_CID_RO_INTEGER,
326 	.name = "Read-Only Integer 32 Bits",
327 	.type = V4L2_CTRL_TYPE_INTEGER,
328 	.flags = V4L2_CTRL_FLAG_READ_ONLY,
329 	.min = 0,
330 	.max = 255,
331 	.step = 1,
332 };
333 
334 /* Framebuffer Controls */
335 
336 static int vivid_fb_s_ctrl(struct v4l2_ctrl *ctrl)
337 {
338 	struct vivid_dev *dev = container_of(ctrl->handler,
339 					     struct vivid_dev, ctrl_hdl_fb);
340 
341 	switch (ctrl->id) {
342 	case VIVID_CID_CLEAR_FB:
343 		vivid_clear_fb(dev);
344 		break;
345 	}
346 	return 0;
347 }
348 
349 static const struct v4l2_ctrl_ops vivid_fb_ctrl_ops = {
350 	.s_ctrl = vivid_fb_s_ctrl,
351 };
352 
353 static const struct v4l2_ctrl_config vivid_ctrl_clear_fb = {
354 	.ops = &vivid_fb_ctrl_ops,
355 	.id = VIVID_CID_CLEAR_FB,
356 	.name = "Clear Framebuffer",
357 	.type = V4L2_CTRL_TYPE_BUTTON,
358 };
359 
360 
361 /* Video User Controls */
362 
363 static int vivid_user_vid_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
364 {
365 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_user_vid);
366 
367 	switch (ctrl->id) {
368 	case V4L2_CID_AUTOGAIN:
369 		dev->gain->val = (jiffies_to_msecs(jiffies) / 1000) & 0xff;
370 		break;
371 	}
372 	return 0;
373 }
374 
375 static int vivid_user_vid_s_ctrl(struct v4l2_ctrl *ctrl)
376 {
377 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_user_vid);
378 
379 	switch (ctrl->id) {
380 	case V4L2_CID_BRIGHTNESS:
381 		dev->input_brightness[dev->input] = ctrl->val - dev->input * 128;
382 		tpg_s_brightness(&dev->tpg, dev->input_brightness[dev->input]);
383 		break;
384 	case V4L2_CID_CONTRAST:
385 		tpg_s_contrast(&dev->tpg, ctrl->val);
386 		break;
387 	case V4L2_CID_SATURATION:
388 		tpg_s_saturation(&dev->tpg, ctrl->val);
389 		break;
390 	case V4L2_CID_HUE:
391 		tpg_s_hue(&dev->tpg, ctrl->val);
392 		break;
393 	case V4L2_CID_HFLIP:
394 		dev->hflip = ctrl->val;
395 		tpg_s_hflip(&dev->tpg, dev->sensor_hflip ^ dev->hflip);
396 		break;
397 	case V4L2_CID_VFLIP:
398 		dev->vflip = ctrl->val;
399 		tpg_s_vflip(&dev->tpg, dev->sensor_vflip ^ dev->vflip);
400 		break;
401 	case V4L2_CID_ALPHA_COMPONENT:
402 		tpg_s_alpha_component(&dev->tpg, ctrl->val);
403 		break;
404 	}
405 	return 0;
406 }
407 
408 static const struct v4l2_ctrl_ops vivid_user_vid_ctrl_ops = {
409 	.g_volatile_ctrl = vivid_user_vid_g_volatile_ctrl,
410 	.s_ctrl = vivid_user_vid_s_ctrl,
411 };
412 
413 
414 /* Video Capture Controls */
415 
416 static int vivid_vid_cap_s_ctrl(struct v4l2_ctrl *ctrl)
417 {
418 	static const u32 colorspaces[] = {
419 		V4L2_COLORSPACE_SMPTE170M,
420 		V4L2_COLORSPACE_REC709,
421 		V4L2_COLORSPACE_SRGB,
422 		V4L2_COLORSPACE_OPRGB,
423 		V4L2_COLORSPACE_BT2020,
424 		V4L2_COLORSPACE_DCI_P3,
425 		V4L2_COLORSPACE_SMPTE240M,
426 		V4L2_COLORSPACE_470_SYSTEM_M,
427 		V4L2_COLORSPACE_470_SYSTEM_BG,
428 	};
429 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_vid_cap);
430 	unsigned int i, j;
431 
432 	switch (ctrl->id) {
433 	case VIVID_CID_TEST_PATTERN:
434 		vivid_update_quality(dev);
435 		tpg_s_pattern(&dev->tpg, ctrl->val);
436 		break;
437 	case VIVID_CID_COLORSPACE:
438 		tpg_s_colorspace(&dev->tpg, colorspaces[ctrl->val]);
439 		vivid_send_source_change(dev, TV);
440 		vivid_send_source_change(dev, SVID);
441 		vivid_send_source_change(dev, HDMI);
442 		vivid_send_source_change(dev, WEBCAM);
443 		break;
444 	case VIVID_CID_XFER_FUNC:
445 		tpg_s_xfer_func(&dev->tpg, ctrl->val);
446 		vivid_send_source_change(dev, TV);
447 		vivid_send_source_change(dev, SVID);
448 		vivid_send_source_change(dev, HDMI);
449 		vivid_send_source_change(dev, WEBCAM);
450 		break;
451 	case VIVID_CID_YCBCR_ENC:
452 		tpg_s_ycbcr_enc(&dev->tpg, ctrl->val);
453 		vivid_send_source_change(dev, TV);
454 		vivid_send_source_change(dev, SVID);
455 		vivid_send_source_change(dev, HDMI);
456 		vivid_send_source_change(dev, WEBCAM);
457 		break;
458 	case VIVID_CID_HSV_ENC:
459 		tpg_s_hsv_enc(&dev->tpg, ctrl->val ? V4L2_HSV_ENC_256 :
460 						     V4L2_HSV_ENC_180);
461 		vivid_send_source_change(dev, TV);
462 		vivid_send_source_change(dev, SVID);
463 		vivid_send_source_change(dev, HDMI);
464 		vivid_send_source_change(dev, WEBCAM);
465 		break;
466 	case VIVID_CID_QUANTIZATION:
467 		tpg_s_quantization(&dev->tpg, ctrl->val);
468 		vivid_send_source_change(dev, TV);
469 		vivid_send_source_change(dev, SVID);
470 		vivid_send_source_change(dev, HDMI);
471 		vivid_send_source_change(dev, WEBCAM);
472 		break;
473 	case V4L2_CID_DV_RX_RGB_RANGE:
474 		if (!vivid_is_hdmi_cap(dev))
475 			break;
476 		tpg_s_rgb_range(&dev->tpg, ctrl->val);
477 		break;
478 	case VIVID_CID_LIMITED_RGB_RANGE:
479 		tpg_s_real_rgb_range(&dev->tpg, ctrl->val ?
480 				V4L2_DV_RGB_RANGE_LIMITED : V4L2_DV_RGB_RANGE_FULL);
481 		break;
482 	case VIVID_CID_ALPHA_MODE:
483 		tpg_s_alpha_mode(&dev->tpg, ctrl->val);
484 		break;
485 	case VIVID_CID_HOR_MOVEMENT:
486 		tpg_s_mv_hor_mode(&dev->tpg, ctrl->val);
487 		break;
488 	case VIVID_CID_VERT_MOVEMENT:
489 		tpg_s_mv_vert_mode(&dev->tpg, ctrl->val);
490 		break;
491 	case VIVID_CID_OSD_TEXT_MODE:
492 		dev->osd_mode = ctrl->val;
493 		break;
494 	case VIVID_CID_PERCENTAGE_FILL:
495 		tpg_s_perc_fill(&dev->tpg, ctrl->val);
496 		for (i = 0; i < VIDEO_MAX_FRAME; i++)
497 			dev->must_blank[i] = ctrl->val < 100;
498 		break;
499 	case VIVID_CID_INSERT_SAV:
500 		tpg_s_insert_sav(&dev->tpg, ctrl->val);
501 		break;
502 	case VIVID_CID_INSERT_EAV:
503 		tpg_s_insert_eav(&dev->tpg, ctrl->val);
504 		break;
505 	case VIVID_CID_INSERT_HDMI_VIDEO_GUARD_BAND:
506 		tpg_s_insert_hdmi_video_guard_band(&dev->tpg, ctrl->val);
507 		break;
508 	case VIVID_CID_HFLIP:
509 		dev->sensor_hflip = ctrl->val;
510 		tpg_s_hflip(&dev->tpg, dev->sensor_hflip ^ dev->hflip);
511 		break;
512 	case VIVID_CID_VFLIP:
513 		dev->sensor_vflip = ctrl->val;
514 		tpg_s_vflip(&dev->tpg, dev->sensor_vflip ^ dev->vflip);
515 		break;
516 	case VIVID_CID_REDUCED_FPS:
517 		dev->reduced_fps = ctrl->val;
518 		vivid_update_format_cap(dev, true);
519 		break;
520 	case VIVID_CID_HAS_CROP_CAP:
521 		dev->has_crop_cap = ctrl->val;
522 		vivid_update_format_cap(dev, true);
523 		break;
524 	case VIVID_CID_HAS_COMPOSE_CAP:
525 		dev->has_compose_cap = ctrl->val;
526 		vivid_update_format_cap(dev, true);
527 		break;
528 	case VIVID_CID_HAS_SCALER_CAP:
529 		dev->has_scaler_cap = ctrl->val;
530 		vivid_update_format_cap(dev, true);
531 		break;
532 	case VIVID_CID_SHOW_BORDER:
533 		tpg_s_show_border(&dev->tpg, ctrl->val);
534 		break;
535 	case VIVID_CID_SHOW_SQUARE:
536 		tpg_s_show_square(&dev->tpg, ctrl->val);
537 		break;
538 	case VIVID_CID_STD_ASPECT_RATIO:
539 		dev->std_aspect_ratio[dev->input] = ctrl->val;
540 		tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
541 		break;
542 	case VIVID_CID_DV_TIMINGS_SIGNAL_MODE:
543 		dev->dv_timings_signal_mode[dev->input] =
544 			dev->ctrl_dv_timings_signal_mode->val;
545 		dev->query_dv_timings[dev->input] = dev->ctrl_dv_timings->val;
546 
547 		dev->power_present = 0;
548 		for (i = 0, j = 0;
549 		     i < ARRAY_SIZE(dev->dv_timings_signal_mode);
550 		     i++)
551 			if (dev->input_type[i] == HDMI) {
552 				if (dev->dv_timings_signal_mode[i] != NO_SIGNAL)
553 					dev->power_present |= (1 << j);
554 				j++;
555 			}
556 		__v4l2_ctrl_s_ctrl(dev->ctrl_rx_power_present,
557 				   dev->power_present);
558 
559 		v4l2_ctrl_activate(dev->ctrl_dv_timings,
560 			dev->dv_timings_signal_mode[dev->input] ==
561 				SELECTED_DV_TIMINGS);
562 
563 		vivid_update_quality(dev);
564 		vivid_send_source_change(dev, HDMI);
565 		break;
566 	case VIVID_CID_DV_TIMINGS_ASPECT_RATIO:
567 		dev->dv_timings_aspect_ratio[dev->input] = ctrl->val;
568 		tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
569 		break;
570 	case VIVID_CID_TSTAMP_SRC:
571 		dev->tstamp_src_is_soe = ctrl->val;
572 		dev->vb_vid_cap_q.timestamp_flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
573 		if (dev->tstamp_src_is_soe)
574 			dev->vb_vid_cap_q.timestamp_flags |= V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
575 		break;
576 	case VIVID_CID_MAX_EDID_BLOCKS:
577 		dev->edid_max_blocks = ctrl->val;
578 		if (dev->edid_blocks > dev->edid_max_blocks)
579 			dev->edid_blocks = dev->edid_max_blocks;
580 		break;
581 	}
582 	return 0;
583 }
584 
585 static const struct v4l2_ctrl_ops vivid_vid_cap_ctrl_ops = {
586 	.s_ctrl = vivid_vid_cap_s_ctrl,
587 };
588 
589 static const char * const vivid_ctrl_hor_movement_strings[] = {
590 	"Move Left Fast",
591 	"Move Left",
592 	"Move Left Slow",
593 	"No Movement",
594 	"Move Right Slow",
595 	"Move Right",
596 	"Move Right Fast",
597 	NULL,
598 };
599 
600 static const struct v4l2_ctrl_config vivid_ctrl_hor_movement = {
601 	.ops = &vivid_vid_cap_ctrl_ops,
602 	.id = VIVID_CID_HOR_MOVEMENT,
603 	.name = "Horizontal Movement",
604 	.type = V4L2_CTRL_TYPE_MENU,
605 	.max = TPG_MOVE_POS_FAST,
606 	.def = TPG_MOVE_NONE,
607 	.qmenu = vivid_ctrl_hor_movement_strings,
608 };
609 
610 static const char * const vivid_ctrl_vert_movement_strings[] = {
611 	"Move Up Fast",
612 	"Move Up",
613 	"Move Up Slow",
614 	"No Movement",
615 	"Move Down Slow",
616 	"Move Down",
617 	"Move Down Fast",
618 	NULL,
619 };
620 
621 static const struct v4l2_ctrl_config vivid_ctrl_vert_movement = {
622 	.ops = &vivid_vid_cap_ctrl_ops,
623 	.id = VIVID_CID_VERT_MOVEMENT,
624 	.name = "Vertical Movement",
625 	.type = V4L2_CTRL_TYPE_MENU,
626 	.max = TPG_MOVE_POS_FAST,
627 	.def = TPG_MOVE_NONE,
628 	.qmenu = vivid_ctrl_vert_movement_strings,
629 };
630 
631 static const struct v4l2_ctrl_config vivid_ctrl_show_border = {
632 	.ops = &vivid_vid_cap_ctrl_ops,
633 	.id = VIVID_CID_SHOW_BORDER,
634 	.name = "Show Border",
635 	.type = V4L2_CTRL_TYPE_BOOLEAN,
636 	.max = 1,
637 	.step = 1,
638 };
639 
640 static const struct v4l2_ctrl_config vivid_ctrl_show_square = {
641 	.ops = &vivid_vid_cap_ctrl_ops,
642 	.id = VIVID_CID_SHOW_SQUARE,
643 	.name = "Show Square",
644 	.type = V4L2_CTRL_TYPE_BOOLEAN,
645 	.max = 1,
646 	.step = 1,
647 };
648 
649 static const char * const vivid_ctrl_osd_mode_strings[] = {
650 	"All",
651 	"Counters Only",
652 	"None",
653 	NULL,
654 };
655 
656 static const struct v4l2_ctrl_config vivid_ctrl_osd_mode = {
657 	.ops = &vivid_vid_cap_ctrl_ops,
658 	.id = VIVID_CID_OSD_TEXT_MODE,
659 	.name = "OSD Text Mode",
660 	.type = V4L2_CTRL_TYPE_MENU,
661 	.max = ARRAY_SIZE(vivid_ctrl_osd_mode_strings) - 2,
662 	.qmenu = vivid_ctrl_osd_mode_strings,
663 };
664 
665 static const struct v4l2_ctrl_config vivid_ctrl_perc_fill = {
666 	.ops = &vivid_vid_cap_ctrl_ops,
667 	.id = VIVID_CID_PERCENTAGE_FILL,
668 	.name = "Fill Percentage of Frame",
669 	.type = V4L2_CTRL_TYPE_INTEGER,
670 	.min = 0,
671 	.max = 100,
672 	.def = 100,
673 	.step = 1,
674 };
675 
676 static const struct v4l2_ctrl_config vivid_ctrl_insert_sav = {
677 	.ops = &vivid_vid_cap_ctrl_ops,
678 	.id = VIVID_CID_INSERT_SAV,
679 	.name = "Insert SAV Code in Image",
680 	.type = V4L2_CTRL_TYPE_BOOLEAN,
681 	.max = 1,
682 	.step = 1,
683 };
684 
685 static const struct v4l2_ctrl_config vivid_ctrl_insert_eav = {
686 	.ops = &vivid_vid_cap_ctrl_ops,
687 	.id = VIVID_CID_INSERT_EAV,
688 	.name = "Insert EAV Code in Image",
689 	.type = V4L2_CTRL_TYPE_BOOLEAN,
690 	.max = 1,
691 	.step = 1,
692 };
693 
694 static const struct v4l2_ctrl_config vivid_ctrl_insert_hdmi_video_guard_band = {
695 	.ops = &vivid_vid_cap_ctrl_ops,
696 	.id = VIVID_CID_INSERT_HDMI_VIDEO_GUARD_BAND,
697 	.name = "Insert Video Guard Band",
698 	.type = V4L2_CTRL_TYPE_BOOLEAN,
699 	.max = 1,
700 	.step = 1,
701 };
702 
703 static const struct v4l2_ctrl_config vivid_ctrl_hflip = {
704 	.ops = &vivid_vid_cap_ctrl_ops,
705 	.id = VIVID_CID_HFLIP,
706 	.name = "Sensor Flipped Horizontally",
707 	.type = V4L2_CTRL_TYPE_BOOLEAN,
708 	.max = 1,
709 	.step = 1,
710 };
711 
712 static const struct v4l2_ctrl_config vivid_ctrl_vflip = {
713 	.ops = &vivid_vid_cap_ctrl_ops,
714 	.id = VIVID_CID_VFLIP,
715 	.name = "Sensor Flipped Vertically",
716 	.type = V4L2_CTRL_TYPE_BOOLEAN,
717 	.max = 1,
718 	.step = 1,
719 };
720 
721 static const struct v4l2_ctrl_config vivid_ctrl_reduced_fps = {
722 	.ops = &vivid_vid_cap_ctrl_ops,
723 	.id = VIVID_CID_REDUCED_FPS,
724 	.name = "Reduced Framerate",
725 	.type = V4L2_CTRL_TYPE_BOOLEAN,
726 	.max = 1,
727 	.step = 1,
728 };
729 
730 static const struct v4l2_ctrl_config vivid_ctrl_has_crop_cap = {
731 	.ops = &vivid_vid_cap_ctrl_ops,
732 	.id = VIVID_CID_HAS_CROP_CAP,
733 	.name = "Enable Capture Cropping",
734 	.type = V4L2_CTRL_TYPE_BOOLEAN,
735 	.max = 1,
736 	.def = 1,
737 	.step = 1,
738 };
739 
740 static const struct v4l2_ctrl_config vivid_ctrl_has_compose_cap = {
741 	.ops = &vivid_vid_cap_ctrl_ops,
742 	.id = VIVID_CID_HAS_COMPOSE_CAP,
743 	.name = "Enable Capture Composing",
744 	.type = V4L2_CTRL_TYPE_BOOLEAN,
745 	.max = 1,
746 	.def = 1,
747 	.step = 1,
748 };
749 
750 static const struct v4l2_ctrl_config vivid_ctrl_has_scaler_cap = {
751 	.ops = &vivid_vid_cap_ctrl_ops,
752 	.id = VIVID_CID_HAS_SCALER_CAP,
753 	.name = "Enable Capture Scaler",
754 	.type = V4L2_CTRL_TYPE_BOOLEAN,
755 	.max = 1,
756 	.def = 1,
757 	.step = 1,
758 };
759 
760 static const char * const vivid_ctrl_tstamp_src_strings[] = {
761 	"End of Frame",
762 	"Start of Exposure",
763 	NULL,
764 };
765 
766 static const struct v4l2_ctrl_config vivid_ctrl_tstamp_src = {
767 	.ops = &vivid_vid_cap_ctrl_ops,
768 	.id = VIVID_CID_TSTAMP_SRC,
769 	.name = "Timestamp Source",
770 	.type = V4L2_CTRL_TYPE_MENU,
771 	.max = ARRAY_SIZE(vivid_ctrl_tstamp_src_strings) - 2,
772 	.qmenu = vivid_ctrl_tstamp_src_strings,
773 };
774 
775 static const struct v4l2_ctrl_config vivid_ctrl_std_aspect_ratio = {
776 	.ops = &vivid_vid_cap_ctrl_ops,
777 	.id = VIVID_CID_STD_ASPECT_RATIO,
778 	.name = "Standard Aspect Ratio",
779 	.type = V4L2_CTRL_TYPE_MENU,
780 	.min = 1,
781 	.max = 4,
782 	.def = 1,
783 	.qmenu = tpg_aspect_strings,
784 };
785 
786 static const char * const vivid_ctrl_dv_timings_signal_mode_strings[] = {
787 	"Current DV Timings",
788 	"No Signal",
789 	"No Lock",
790 	"Out of Range",
791 	"Selected DV Timings",
792 	"Cycle Through All DV Timings",
793 	"Custom DV Timings",
794 	NULL,
795 };
796 
797 static const struct v4l2_ctrl_config vivid_ctrl_dv_timings_signal_mode = {
798 	.ops = &vivid_vid_cap_ctrl_ops,
799 	.id = VIVID_CID_DV_TIMINGS_SIGNAL_MODE,
800 	.name = "DV Timings Signal Mode",
801 	.type = V4L2_CTRL_TYPE_MENU,
802 	.max = 5,
803 	.qmenu = vivid_ctrl_dv_timings_signal_mode_strings,
804 };
805 
806 static const struct v4l2_ctrl_config vivid_ctrl_dv_timings_aspect_ratio = {
807 	.ops = &vivid_vid_cap_ctrl_ops,
808 	.id = VIVID_CID_DV_TIMINGS_ASPECT_RATIO,
809 	.name = "DV Timings Aspect Ratio",
810 	.type = V4L2_CTRL_TYPE_MENU,
811 	.max = 3,
812 	.qmenu = tpg_aspect_strings,
813 };
814 
815 static const struct v4l2_ctrl_config vivid_ctrl_max_edid_blocks = {
816 	.ops = &vivid_vid_cap_ctrl_ops,
817 	.id = VIVID_CID_MAX_EDID_BLOCKS,
818 	.name = "Maximum EDID Blocks",
819 	.type = V4L2_CTRL_TYPE_INTEGER,
820 	.min = 1,
821 	.max = 256,
822 	.def = 2,
823 	.step = 1,
824 };
825 
826 static const char * const vivid_ctrl_colorspace_strings[] = {
827 	"SMPTE 170M",
828 	"Rec. 709",
829 	"sRGB",
830 	"opRGB",
831 	"BT.2020",
832 	"DCI-P3",
833 	"SMPTE 240M",
834 	"470 System M",
835 	"470 System BG",
836 	NULL,
837 };
838 
839 static const struct v4l2_ctrl_config vivid_ctrl_colorspace = {
840 	.ops = &vivid_vid_cap_ctrl_ops,
841 	.id = VIVID_CID_COLORSPACE,
842 	.name = "Colorspace",
843 	.type = V4L2_CTRL_TYPE_MENU,
844 	.max = ARRAY_SIZE(vivid_ctrl_colorspace_strings) - 2,
845 	.def = 2,
846 	.qmenu = vivid_ctrl_colorspace_strings,
847 };
848 
849 static const char * const vivid_ctrl_xfer_func_strings[] = {
850 	"Default",
851 	"Rec. 709",
852 	"sRGB",
853 	"opRGB",
854 	"SMPTE 240M",
855 	"None",
856 	"DCI-P3",
857 	"SMPTE 2084",
858 	NULL,
859 };
860 
861 static const struct v4l2_ctrl_config vivid_ctrl_xfer_func = {
862 	.ops = &vivid_vid_cap_ctrl_ops,
863 	.id = VIVID_CID_XFER_FUNC,
864 	.name = "Transfer Function",
865 	.type = V4L2_CTRL_TYPE_MENU,
866 	.max = ARRAY_SIZE(vivid_ctrl_xfer_func_strings) - 2,
867 	.qmenu = vivid_ctrl_xfer_func_strings,
868 };
869 
870 static const char * const vivid_ctrl_ycbcr_enc_strings[] = {
871 	"Default",
872 	"ITU-R 601",
873 	"Rec. 709",
874 	"xvYCC 601",
875 	"xvYCC 709",
876 	"",
877 	"BT.2020",
878 	"BT.2020 Constant Luminance",
879 	"SMPTE 240M",
880 	NULL,
881 };
882 
883 static const struct v4l2_ctrl_config vivid_ctrl_ycbcr_enc = {
884 	.ops = &vivid_vid_cap_ctrl_ops,
885 	.id = VIVID_CID_YCBCR_ENC,
886 	.name = "Y'CbCr Encoding",
887 	.type = V4L2_CTRL_TYPE_MENU,
888 	.menu_skip_mask = 1 << 5,
889 	.max = ARRAY_SIZE(vivid_ctrl_ycbcr_enc_strings) - 2,
890 	.qmenu = vivid_ctrl_ycbcr_enc_strings,
891 };
892 
893 static const char * const vivid_ctrl_hsv_enc_strings[] = {
894 	"Hue 0-179",
895 	"Hue 0-256",
896 	NULL,
897 };
898 
899 static const struct v4l2_ctrl_config vivid_ctrl_hsv_enc = {
900 	.ops = &vivid_vid_cap_ctrl_ops,
901 	.id = VIVID_CID_HSV_ENC,
902 	.name = "HSV Encoding",
903 	.type = V4L2_CTRL_TYPE_MENU,
904 	.max = ARRAY_SIZE(vivid_ctrl_hsv_enc_strings) - 2,
905 	.qmenu = vivid_ctrl_hsv_enc_strings,
906 };
907 
908 static const char * const vivid_ctrl_quantization_strings[] = {
909 	"Default",
910 	"Full Range",
911 	"Limited Range",
912 	NULL,
913 };
914 
915 static const struct v4l2_ctrl_config vivid_ctrl_quantization = {
916 	.ops = &vivid_vid_cap_ctrl_ops,
917 	.id = VIVID_CID_QUANTIZATION,
918 	.name = "Quantization",
919 	.type = V4L2_CTRL_TYPE_MENU,
920 	.max = ARRAY_SIZE(vivid_ctrl_quantization_strings) - 2,
921 	.qmenu = vivid_ctrl_quantization_strings,
922 };
923 
924 static const struct v4l2_ctrl_config vivid_ctrl_alpha_mode = {
925 	.ops = &vivid_vid_cap_ctrl_ops,
926 	.id = VIVID_CID_ALPHA_MODE,
927 	.name = "Apply Alpha To Red Only",
928 	.type = V4L2_CTRL_TYPE_BOOLEAN,
929 	.max = 1,
930 	.step = 1,
931 };
932 
933 static const struct v4l2_ctrl_config vivid_ctrl_limited_rgb_range = {
934 	.ops = &vivid_vid_cap_ctrl_ops,
935 	.id = VIVID_CID_LIMITED_RGB_RANGE,
936 	.name = "Limited RGB Range (16-235)",
937 	.type = V4L2_CTRL_TYPE_BOOLEAN,
938 	.max = 1,
939 	.step = 1,
940 };
941 
942 
943 /* Video Loop Control */
944 
945 static int vivid_loop_cap_s_ctrl(struct v4l2_ctrl *ctrl)
946 {
947 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_loop_cap);
948 
949 	switch (ctrl->id) {
950 	case VIVID_CID_LOOP_VIDEO:
951 		dev->loop_video = ctrl->val;
952 		vivid_update_quality(dev);
953 		vivid_send_source_change(dev, SVID);
954 		vivid_send_source_change(dev, HDMI);
955 		break;
956 	}
957 	return 0;
958 }
959 
960 static const struct v4l2_ctrl_ops vivid_loop_cap_ctrl_ops = {
961 	.s_ctrl = vivid_loop_cap_s_ctrl,
962 };
963 
964 static const struct v4l2_ctrl_config vivid_ctrl_loop_video = {
965 	.ops = &vivid_loop_cap_ctrl_ops,
966 	.id = VIVID_CID_LOOP_VIDEO,
967 	.name = "Loop Video",
968 	.type = V4L2_CTRL_TYPE_BOOLEAN,
969 	.max = 1,
970 	.step = 1,
971 };
972 
973 
974 /* VBI Capture Control */
975 
976 static int vivid_vbi_cap_s_ctrl(struct v4l2_ctrl *ctrl)
977 {
978 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_vbi_cap);
979 
980 	switch (ctrl->id) {
981 	case VIVID_CID_VBI_CAP_INTERLACED:
982 		dev->vbi_cap_interlaced = ctrl->val;
983 		break;
984 	}
985 	return 0;
986 }
987 
988 static const struct v4l2_ctrl_ops vivid_vbi_cap_ctrl_ops = {
989 	.s_ctrl = vivid_vbi_cap_s_ctrl,
990 };
991 
992 static const struct v4l2_ctrl_config vivid_ctrl_vbi_cap_interlaced = {
993 	.ops = &vivid_vbi_cap_ctrl_ops,
994 	.id = VIVID_CID_VBI_CAP_INTERLACED,
995 	.name = "Interlaced VBI Format",
996 	.type = V4L2_CTRL_TYPE_BOOLEAN,
997 	.max = 1,
998 	.step = 1,
999 };
1000 
1001 
1002 /* Video Output Controls */
1003 
1004 static int vivid_vid_out_s_ctrl(struct v4l2_ctrl *ctrl)
1005 {
1006 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_vid_out);
1007 	struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
1008 	u32 display_present = 0;
1009 	unsigned int i, j, bus_idx;
1010 
1011 	switch (ctrl->id) {
1012 	case VIVID_CID_HAS_CROP_OUT:
1013 		dev->has_crop_out = ctrl->val;
1014 		vivid_update_format_out(dev);
1015 		break;
1016 	case VIVID_CID_HAS_COMPOSE_OUT:
1017 		dev->has_compose_out = ctrl->val;
1018 		vivid_update_format_out(dev);
1019 		break;
1020 	case VIVID_CID_HAS_SCALER_OUT:
1021 		dev->has_scaler_out = ctrl->val;
1022 		vivid_update_format_out(dev);
1023 		break;
1024 	case V4L2_CID_DV_TX_MODE:
1025 		dev->dvi_d_out = ctrl->val == V4L2_DV_TX_MODE_DVI_D;
1026 		if (!vivid_is_hdmi_out(dev))
1027 			break;
1028 		if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
1029 			if (bt->width == 720 && bt->height <= 576)
1030 				dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
1031 			else
1032 				dev->colorspace_out = V4L2_COLORSPACE_REC709;
1033 			dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
1034 		} else {
1035 			dev->colorspace_out = V4L2_COLORSPACE_SRGB;
1036 			dev->quantization_out = dev->dvi_d_out ?
1037 					V4L2_QUANTIZATION_LIM_RANGE :
1038 					V4L2_QUANTIZATION_DEFAULT;
1039 		}
1040 		if (dev->loop_video)
1041 			vivid_send_source_change(dev, HDMI);
1042 		break;
1043 	case VIVID_CID_DISPLAY_PRESENT:
1044 		if (dev->output_type[dev->output] != HDMI)
1045 			break;
1046 
1047 		dev->display_present[dev->output] = ctrl->val;
1048 		for (i = 0, j = 0; i < dev->num_outputs; i++)
1049 			if (dev->output_type[i] == HDMI)
1050 				display_present |=
1051 					dev->display_present[i] << j++;
1052 
1053 		__v4l2_ctrl_s_ctrl(dev->ctrl_tx_rxsense, display_present);
1054 
1055 		if (dev->edid_blocks) {
1056 			__v4l2_ctrl_s_ctrl(dev->ctrl_tx_edid_present,
1057 					   display_present);
1058 			__v4l2_ctrl_s_ctrl(dev->ctrl_tx_hotplug,
1059 					   display_present);
1060 		}
1061 
1062 		bus_idx = dev->cec_output2bus_map[dev->output];
1063 		if (!dev->cec_tx_adap[bus_idx])
1064 			break;
1065 
1066 		if (ctrl->val && dev->edid_blocks)
1067 			cec_s_phys_addr(dev->cec_tx_adap[bus_idx],
1068 					dev->cec_tx_adap[bus_idx]->phys_addr,
1069 					false);
1070 		else
1071 			cec_phys_addr_invalidate(dev->cec_tx_adap[bus_idx]);
1072 
1073 		break;
1074 	}
1075 	return 0;
1076 }
1077 
1078 static const struct v4l2_ctrl_ops vivid_vid_out_ctrl_ops = {
1079 	.s_ctrl = vivid_vid_out_s_ctrl,
1080 };
1081 
1082 static const struct v4l2_ctrl_config vivid_ctrl_has_crop_out = {
1083 	.ops = &vivid_vid_out_ctrl_ops,
1084 	.id = VIVID_CID_HAS_CROP_OUT,
1085 	.name = "Enable Output Cropping",
1086 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1087 	.max = 1,
1088 	.def = 1,
1089 	.step = 1,
1090 };
1091 
1092 static const struct v4l2_ctrl_config vivid_ctrl_has_compose_out = {
1093 	.ops = &vivid_vid_out_ctrl_ops,
1094 	.id = VIVID_CID_HAS_COMPOSE_OUT,
1095 	.name = "Enable Output Composing",
1096 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1097 	.max = 1,
1098 	.def = 1,
1099 	.step = 1,
1100 };
1101 
1102 static const struct v4l2_ctrl_config vivid_ctrl_has_scaler_out = {
1103 	.ops = &vivid_vid_out_ctrl_ops,
1104 	.id = VIVID_CID_HAS_SCALER_OUT,
1105 	.name = "Enable Output Scaler",
1106 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1107 	.max = 1,
1108 	.def = 1,
1109 	.step = 1,
1110 };
1111 
1112 static const struct v4l2_ctrl_config vivid_ctrl_display_present = {
1113 	.ops = &vivid_vid_out_ctrl_ops,
1114 	.id = VIVID_CID_DISPLAY_PRESENT,
1115 	.name = "Display Present",
1116 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1117 	.max = 1,
1118 	.def = 1,
1119 	.step = 1,
1120 };
1121 
1122 /* Streaming Controls */
1123 
1124 static int vivid_streaming_s_ctrl(struct v4l2_ctrl *ctrl)
1125 {
1126 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_streaming);
1127 
1128 	switch (ctrl->id) {
1129 	case VIVID_CID_DQBUF_ERROR:
1130 		dev->dqbuf_error = true;
1131 		break;
1132 	case VIVID_CID_PERC_DROPPED:
1133 		dev->perc_dropped_buffers = ctrl->val;
1134 		break;
1135 	case VIVID_CID_QUEUE_SETUP_ERROR:
1136 		dev->queue_setup_error = true;
1137 		break;
1138 	case VIVID_CID_BUF_PREPARE_ERROR:
1139 		dev->buf_prepare_error = true;
1140 		break;
1141 	case VIVID_CID_START_STR_ERROR:
1142 		dev->start_streaming_error = true;
1143 		break;
1144 	case VIVID_CID_REQ_VALIDATE_ERROR:
1145 		dev->req_validate_error = true;
1146 		break;
1147 	case VIVID_CID_QUEUE_ERROR:
1148 		if (vb2_start_streaming_called(&dev->vb_vid_cap_q))
1149 			vb2_queue_error(&dev->vb_vid_cap_q);
1150 		if (vb2_start_streaming_called(&dev->vb_vbi_cap_q))
1151 			vb2_queue_error(&dev->vb_vbi_cap_q);
1152 		if (vb2_start_streaming_called(&dev->vb_vid_out_q))
1153 			vb2_queue_error(&dev->vb_vid_out_q);
1154 		if (vb2_start_streaming_called(&dev->vb_vbi_out_q))
1155 			vb2_queue_error(&dev->vb_vbi_out_q);
1156 		if (vb2_start_streaming_called(&dev->vb_sdr_cap_q))
1157 			vb2_queue_error(&dev->vb_sdr_cap_q);
1158 		break;
1159 	case VIVID_CID_SEQ_WRAP:
1160 		dev->seq_wrap = ctrl->val;
1161 		break;
1162 	case VIVID_CID_TIME_WRAP:
1163 		dev->time_wrap = ctrl->val;
1164 		if (dev->time_wrap == 1)
1165 			dev->time_wrap = (1ULL << 63) - NSEC_PER_SEC * 16ULL;
1166 		else if (dev->time_wrap == 2)
1167 			dev->time_wrap = ((1ULL << 31) - 16) * NSEC_PER_SEC;
1168 		break;
1169 	}
1170 	return 0;
1171 }
1172 
1173 static const struct v4l2_ctrl_ops vivid_streaming_ctrl_ops = {
1174 	.s_ctrl = vivid_streaming_s_ctrl,
1175 };
1176 
1177 static const struct v4l2_ctrl_config vivid_ctrl_dqbuf_error = {
1178 	.ops = &vivid_streaming_ctrl_ops,
1179 	.id = VIVID_CID_DQBUF_ERROR,
1180 	.name = "Inject V4L2_BUF_FLAG_ERROR",
1181 	.type = V4L2_CTRL_TYPE_BUTTON,
1182 };
1183 
1184 static const struct v4l2_ctrl_config vivid_ctrl_perc_dropped = {
1185 	.ops = &vivid_streaming_ctrl_ops,
1186 	.id = VIVID_CID_PERC_DROPPED,
1187 	.name = "Percentage of Dropped Buffers",
1188 	.type = V4L2_CTRL_TYPE_INTEGER,
1189 	.min = 0,
1190 	.max = 100,
1191 	.step = 1,
1192 };
1193 
1194 static const struct v4l2_ctrl_config vivid_ctrl_queue_setup_error = {
1195 	.ops = &vivid_streaming_ctrl_ops,
1196 	.id = VIVID_CID_QUEUE_SETUP_ERROR,
1197 	.name = "Inject VIDIOC_REQBUFS Error",
1198 	.type = V4L2_CTRL_TYPE_BUTTON,
1199 };
1200 
1201 static const struct v4l2_ctrl_config vivid_ctrl_buf_prepare_error = {
1202 	.ops = &vivid_streaming_ctrl_ops,
1203 	.id = VIVID_CID_BUF_PREPARE_ERROR,
1204 	.name = "Inject VIDIOC_QBUF Error",
1205 	.type = V4L2_CTRL_TYPE_BUTTON,
1206 };
1207 
1208 static const struct v4l2_ctrl_config vivid_ctrl_start_streaming_error = {
1209 	.ops = &vivid_streaming_ctrl_ops,
1210 	.id = VIVID_CID_START_STR_ERROR,
1211 	.name = "Inject VIDIOC_STREAMON Error",
1212 	.type = V4L2_CTRL_TYPE_BUTTON,
1213 };
1214 
1215 static const struct v4l2_ctrl_config vivid_ctrl_queue_error = {
1216 	.ops = &vivid_streaming_ctrl_ops,
1217 	.id = VIVID_CID_QUEUE_ERROR,
1218 	.name = "Inject Fatal Streaming Error",
1219 	.type = V4L2_CTRL_TYPE_BUTTON,
1220 };
1221 
1222 #ifdef CONFIG_MEDIA_CONTROLLER
1223 static const struct v4l2_ctrl_config vivid_ctrl_req_validate_error = {
1224 	.ops = &vivid_streaming_ctrl_ops,
1225 	.id = VIVID_CID_REQ_VALIDATE_ERROR,
1226 	.name = "Inject req_validate() Error",
1227 	.type = V4L2_CTRL_TYPE_BUTTON,
1228 };
1229 #endif
1230 
1231 static const struct v4l2_ctrl_config vivid_ctrl_seq_wrap = {
1232 	.ops = &vivid_streaming_ctrl_ops,
1233 	.id = VIVID_CID_SEQ_WRAP,
1234 	.name = "Wrap Sequence Number",
1235 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1236 	.max = 1,
1237 	.step = 1,
1238 };
1239 
1240 static const char * const vivid_ctrl_time_wrap_strings[] = {
1241 	"None",
1242 	"64 Bit",
1243 	"32 Bit",
1244 	NULL,
1245 };
1246 
1247 static const struct v4l2_ctrl_config vivid_ctrl_time_wrap = {
1248 	.ops = &vivid_streaming_ctrl_ops,
1249 	.id = VIVID_CID_TIME_WRAP,
1250 	.name = "Wrap Timestamp",
1251 	.type = V4L2_CTRL_TYPE_MENU,
1252 	.max = ARRAY_SIZE(vivid_ctrl_time_wrap_strings) - 2,
1253 	.qmenu = vivid_ctrl_time_wrap_strings,
1254 };
1255 
1256 
1257 /* SDTV Capture Controls */
1258 
1259 static int vivid_sdtv_cap_s_ctrl(struct v4l2_ctrl *ctrl)
1260 {
1261 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_sdtv_cap);
1262 
1263 	switch (ctrl->id) {
1264 	case VIVID_CID_STD_SIGNAL_MODE:
1265 		dev->std_signal_mode[dev->input] =
1266 			dev->ctrl_std_signal_mode->val;
1267 		if (dev->std_signal_mode[dev->input] == SELECTED_STD)
1268 			dev->query_std[dev->input] =
1269 				vivid_standard[dev->ctrl_standard->val];
1270 		v4l2_ctrl_activate(dev->ctrl_standard,
1271 				   dev->std_signal_mode[dev->input] ==
1272 					SELECTED_STD);
1273 		vivid_update_quality(dev);
1274 		vivid_send_source_change(dev, TV);
1275 		vivid_send_source_change(dev, SVID);
1276 		break;
1277 	}
1278 	return 0;
1279 }
1280 
1281 static const struct v4l2_ctrl_ops vivid_sdtv_cap_ctrl_ops = {
1282 	.s_ctrl = vivid_sdtv_cap_s_ctrl,
1283 };
1284 
1285 static const char * const vivid_ctrl_std_signal_mode_strings[] = {
1286 	"Current Standard",
1287 	"No Signal",
1288 	"No Lock",
1289 	"",
1290 	"Selected Standard",
1291 	"Cycle Through All Standards",
1292 	NULL,
1293 };
1294 
1295 static const struct v4l2_ctrl_config vivid_ctrl_std_signal_mode = {
1296 	.ops = &vivid_sdtv_cap_ctrl_ops,
1297 	.id = VIVID_CID_STD_SIGNAL_MODE,
1298 	.name = "Standard Signal Mode",
1299 	.type = V4L2_CTRL_TYPE_MENU,
1300 	.max = ARRAY_SIZE(vivid_ctrl_std_signal_mode_strings) - 2,
1301 	.menu_skip_mask = 1 << 3,
1302 	.qmenu = vivid_ctrl_std_signal_mode_strings,
1303 };
1304 
1305 static const struct v4l2_ctrl_config vivid_ctrl_standard = {
1306 	.ops = &vivid_sdtv_cap_ctrl_ops,
1307 	.id = VIVID_CID_STANDARD,
1308 	.name = "Standard",
1309 	.type = V4L2_CTRL_TYPE_MENU,
1310 	.max = 14,
1311 	.qmenu = vivid_ctrl_standard_strings,
1312 };
1313 
1314 
1315 
1316 /* Radio Receiver Controls */
1317 
1318 static int vivid_radio_rx_s_ctrl(struct v4l2_ctrl *ctrl)
1319 {
1320 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_radio_rx);
1321 
1322 	switch (ctrl->id) {
1323 	case VIVID_CID_RADIO_SEEK_MODE:
1324 		dev->radio_rx_hw_seek_mode = ctrl->val;
1325 		break;
1326 	case VIVID_CID_RADIO_SEEK_PROG_LIM:
1327 		dev->radio_rx_hw_seek_prog_lim = ctrl->val;
1328 		break;
1329 	case VIVID_CID_RADIO_RX_RDS_RBDS:
1330 		dev->rds_gen.use_rbds = ctrl->val;
1331 		break;
1332 	case VIVID_CID_RADIO_RX_RDS_BLOCKIO:
1333 		dev->radio_rx_rds_controls = ctrl->val;
1334 		dev->radio_rx_caps &= ~V4L2_CAP_READWRITE;
1335 		dev->radio_rx_rds_use_alternates = false;
1336 		if (!dev->radio_rx_rds_controls) {
1337 			dev->radio_rx_caps |= V4L2_CAP_READWRITE;
1338 			__v4l2_ctrl_s_ctrl(dev->radio_rx_rds_pty, 0);
1339 			__v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ta, 0);
1340 			__v4l2_ctrl_s_ctrl(dev->radio_rx_rds_tp, 0);
1341 			__v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ms, 0);
1342 			__v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_psname, "");
1343 			__v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_radiotext, "");
1344 		}
1345 		v4l2_ctrl_activate(dev->radio_rx_rds_pty, dev->radio_rx_rds_controls);
1346 		v4l2_ctrl_activate(dev->radio_rx_rds_psname, dev->radio_rx_rds_controls);
1347 		v4l2_ctrl_activate(dev->radio_rx_rds_radiotext, dev->radio_rx_rds_controls);
1348 		v4l2_ctrl_activate(dev->radio_rx_rds_ta, dev->radio_rx_rds_controls);
1349 		v4l2_ctrl_activate(dev->radio_rx_rds_tp, dev->radio_rx_rds_controls);
1350 		v4l2_ctrl_activate(dev->radio_rx_rds_ms, dev->radio_rx_rds_controls);
1351 		dev->radio_rx_dev.device_caps = dev->radio_rx_caps;
1352 		break;
1353 	case V4L2_CID_RDS_RECEPTION:
1354 		dev->radio_rx_rds_enabled = ctrl->val;
1355 		break;
1356 	}
1357 	return 0;
1358 }
1359 
1360 static const struct v4l2_ctrl_ops vivid_radio_rx_ctrl_ops = {
1361 	.s_ctrl = vivid_radio_rx_s_ctrl,
1362 };
1363 
1364 static const char * const vivid_ctrl_radio_rds_mode_strings[] = {
1365 	"Block I/O",
1366 	"Controls",
1367 	NULL,
1368 };
1369 
1370 static const struct v4l2_ctrl_config vivid_ctrl_radio_rx_rds_blockio = {
1371 	.ops = &vivid_radio_rx_ctrl_ops,
1372 	.id = VIVID_CID_RADIO_RX_RDS_BLOCKIO,
1373 	.name = "RDS Rx I/O Mode",
1374 	.type = V4L2_CTRL_TYPE_MENU,
1375 	.qmenu = vivid_ctrl_radio_rds_mode_strings,
1376 	.max = 1,
1377 };
1378 
1379 static const struct v4l2_ctrl_config vivid_ctrl_radio_rx_rds_rbds = {
1380 	.ops = &vivid_radio_rx_ctrl_ops,
1381 	.id = VIVID_CID_RADIO_RX_RDS_RBDS,
1382 	.name = "Generate RBDS Instead of RDS",
1383 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1384 	.max = 1,
1385 	.step = 1,
1386 };
1387 
1388 static const char * const vivid_ctrl_radio_hw_seek_mode_strings[] = {
1389 	"Bounded",
1390 	"Wrap Around",
1391 	"Both",
1392 	NULL,
1393 };
1394 
1395 static const struct v4l2_ctrl_config vivid_ctrl_radio_hw_seek_mode = {
1396 	.ops = &vivid_radio_rx_ctrl_ops,
1397 	.id = VIVID_CID_RADIO_SEEK_MODE,
1398 	.name = "Radio HW Seek Mode",
1399 	.type = V4L2_CTRL_TYPE_MENU,
1400 	.max = 2,
1401 	.qmenu = vivid_ctrl_radio_hw_seek_mode_strings,
1402 };
1403 
1404 static const struct v4l2_ctrl_config vivid_ctrl_radio_hw_seek_prog_lim = {
1405 	.ops = &vivid_radio_rx_ctrl_ops,
1406 	.id = VIVID_CID_RADIO_SEEK_PROG_LIM,
1407 	.name = "Radio Programmable HW Seek",
1408 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1409 	.max = 1,
1410 	.step = 1,
1411 };
1412 
1413 
1414 /* Radio Transmitter Controls */
1415 
1416 static int vivid_radio_tx_s_ctrl(struct v4l2_ctrl *ctrl)
1417 {
1418 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_radio_tx);
1419 
1420 	switch (ctrl->id) {
1421 	case VIVID_CID_RADIO_TX_RDS_BLOCKIO:
1422 		dev->radio_tx_rds_controls = ctrl->val;
1423 		dev->radio_tx_caps &= ~V4L2_CAP_READWRITE;
1424 		if (!dev->radio_tx_rds_controls)
1425 			dev->radio_tx_caps |= V4L2_CAP_READWRITE;
1426 		dev->radio_tx_dev.device_caps = dev->radio_tx_caps;
1427 		break;
1428 	case V4L2_CID_RDS_TX_PTY:
1429 		if (dev->radio_rx_rds_controls)
1430 			v4l2_ctrl_s_ctrl(dev->radio_rx_rds_pty, ctrl->val);
1431 		break;
1432 	case V4L2_CID_RDS_TX_PS_NAME:
1433 		if (dev->radio_rx_rds_controls)
1434 			v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_psname, ctrl->p_new.p_char);
1435 		break;
1436 	case V4L2_CID_RDS_TX_RADIO_TEXT:
1437 		if (dev->radio_rx_rds_controls)
1438 			v4l2_ctrl_s_ctrl_string(dev->radio_rx_rds_radiotext, ctrl->p_new.p_char);
1439 		break;
1440 	case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
1441 		if (dev->radio_rx_rds_controls)
1442 			v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ta, ctrl->val);
1443 		break;
1444 	case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
1445 		if (dev->radio_rx_rds_controls)
1446 			v4l2_ctrl_s_ctrl(dev->radio_rx_rds_tp, ctrl->val);
1447 		break;
1448 	case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1449 		if (dev->radio_rx_rds_controls)
1450 			v4l2_ctrl_s_ctrl(dev->radio_rx_rds_ms, ctrl->val);
1451 		break;
1452 	}
1453 	return 0;
1454 }
1455 
1456 static const struct v4l2_ctrl_ops vivid_radio_tx_ctrl_ops = {
1457 	.s_ctrl = vivid_radio_tx_s_ctrl,
1458 };
1459 
1460 static const struct v4l2_ctrl_config vivid_ctrl_radio_tx_rds_blockio = {
1461 	.ops = &vivid_radio_tx_ctrl_ops,
1462 	.id = VIVID_CID_RADIO_TX_RDS_BLOCKIO,
1463 	.name = "RDS Tx I/O Mode",
1464 	.type = V4L2_CTRL_TYPE_MENU,
1465 	.qmenu = vivid_ctrl_radio_rds_mode_strings,
1466 	.max = 1,
1467 	.def = 1,
1468 };
1469 
1470 
1471 /* SDR Capture Controls */
1472 
1473 static int vivid_sdr_cap_s_ctrl(struct v4l2_ctrl *ctrl)
1474 {
1475 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev, ctrl_hdl_sdr_cap);
1476 
1477 	switch (ctrl->id) {
1478 	case VIVID_CID_SDR_CAP_FM_DEVIATION:
1479 		dev->sdr_fm_deviation = ctrl->val;
1480 		break;
1481 	}
1482 	return 0;
1483 }
1484 
1485 static const struct v4l2_ctrl_ops vivid_sdr_cap_ctrl_ops = {
1486 	.s_ctrl = vivid_sdr_cap_s_ctrl,
1487 };
1488 
1489 static const struct v4l2_ctrl_config vivid_ctrl_sdr_cap_fm_deviation = {
1490 	.ops = &vivid_sdr_cap_ctrl_ops,
1491 	.id = VIVID_CID_SDR_CAP_FM_DEVIATION,
1492 	.name = "FM Deviation",
1493 	.type = V4L2_CTRL_TYPE_INTEGER,
1494 	.min =    100,
1495 	.max = 200000,
1496 	.def =  75000,
1497 	.step =     1,
1498 };
1499 
1500 /* Metadata Capture Control */
1501 
1502 static int vivid_meta_cap_s_ctrl(struct v4l2_ctrl *ctrl)
1503 {
1504 	struct vivid_dev *dev = container_of(ctrl->handler, struct vivid_dev,
1505 					     ctrl_hdl_meta_cap);
1506 
1507 	switch (ctrl->id) {
1508 	case VIVID_CID_META_CAP_GENERATE_PTS:
1509 		dev->meta_pts = ctrl->val;
1510 		break;
1511 	case VIVID_CID_META_CAP_GENERATE_SCR:
1512 		dev->meta_scr = ctrl->val;
1513 		break;
1514 	}
1515 	return 0;
1516 }
1517 
1518 static const struct v4l2_ctrl_ops vivid_meta_cap_ctrl_ops = {
1519 	.s_ctrl = vivid_meta_cap_s_ctrl,
1520 };
1521 
1522 static const struct v4l2_ctrl_config vivid_ctrl_meta_has_pts = {
1523 	.ops = &vivid_meta_cap_ctrl_ops,
1524 	.id = VIVID_CID_META_CAP_GENERATE_PTS,
1525 	.name = "Generate PTS",
1526 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1527 	.max = 1,
1528 	.def = 1,
1529 	.step = 1,
1530 };
1531 
1532 static const struct v4l2_ctrl_config vivid_ctrl_meta_has_src_clk = {
1533 	.ops = &vivid_meta_cap_ctrl_ops,
1534 	.id = VIVID_CID_META_CAP_GENERATE_SCR,
1535 	.name = "Generate SCR",
1536 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1537 	.max = 1,
1538 	.def = 1,
1539 	.step = 1,
1540 };
1541 
1542 static const struct v4l2_ctrl_config vivid_ctrl_class = {
1543 	.ops = &vivid_user_gen_ctrl_ops,
1544 	.flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY,
1545 	.id = VIVID_CID_VIVID_CLASS,
1546 	.name = "Vivid Controls",
1547 	.type = V4L2_CTRL_TYPE_CTRL_CLASS,
1548 };
1549 
1550 int vivid_create_controls(struct vivid_dev *dev, bool show_ccs_cap,
1551 		bool show_ccs_out, bool no_error_inj,
1552 		bool has_sdtv, bool has_hdmi)
1553 {
1554 	struct v4l2_ctrl_handler *hdl_user_gen = &dev->ctrl_hdl_user_gen;
1555 	struct v4l2_ctrl_handler *hdl_user_vid = &dev->ctrl_hdl_user_vid;
1556 	struct v4l2_ctrl_handler *hdl_user_aud = &dev->ctrl_hdl_user_aud;
1557 	struct v4l2_ctrl_handler *hdl_streaming = &dev->ctrl_hdl_streaming;
1558 	struct v4l2_ctrl_handler *hdl_sdtv_cap = &dev->ctrl_hdl_sdtv_cap;
1559 	struct v4l2_ctrl_handler *hdl_loop_cap = &dev->ctrl_hdl_loop_cap;
1560 	struct v4l2_ctrl_handler *hdl_fb = &dev->ctrl_hdl_fb;
1561 	struct v4l2_ctrl_handler *hdl_vid_cap = &dev->ctrl_hdl_vid_cap;
1562 	struct v4l2_ctrl_handler *hdl_vid_out = &dev->ctrl_hdl_vid_out;
1563 	struct v4l2_ctrl_handler *hdl_vbi_cap = &dev->ctrl_hdl_vbi_cap;
1564 	struct v4l2_ctrl_handler *hdl_vbi_out = &dev->ctrl_hdl_vbi_out;
1565 	struct v4l2_ctrl_handler *hdl_radio_rx = &dev->ctrl_hdl_radio_rx;
1566 	struct v4l2_ctrl_handler *hdl_radio_tx = &dev->ctrl_hdl_radio_tx;
1567 	struct v4l2_ctrl_handler *hdl_sdr_cap = &dev->ctrl_hdl_sdr_cap;
1568 	struct v4l2_ctrl_handler *hdl_meta_cap = &dev->ctrl_hdl_meta_cap;
1569 	struct v4l2_ctrl_handler *hdl_meta_out = &dev->ctrl_hdl_meta_out;
1570 	struct v4l2_ctrl_handler *hdl_tch_cap = &dev->ctrl_hdl_touch_cap;
1571 
1572 	struct v4l2_ctrl_config vivid_ctrl_dv_timings = {
1573 		.ops = &vivid_vid_cap_ctrl_ops,
1574 		.id = VIVID_CID_DV_TIMINGS,
1575 		.name = "DV Timings",
1576 		.type = V4L2_CTRL_TYPE_MENU,
1577 	};
1578 	int i;
1579 
1580 	v4l2_ctrl_handler_init(hdl_user_gen, 10);
1581 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_class, NULL);
1582 	v4l2_ctrl_handler_init(hdl_user_vid, 9);
1583 	v4l2_ctrl_new_custom(hdl_user_vid, &vivid_ctrl_class, NULL);
1584 	v4l2_ctrl_handler_init(hdl_user_aud, 2);
1585 	v4l2_ctrl_new_custom(hdl_user_aud, &vivid_ctrl_class, NULL);
1586 	v4l2_ctrl_handler_init(hdl_streaming, 8);
1587 	v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_class, NULL);
1588 	v4l2_ctrl_handler_init(hdl_sdtv_cap, 2);
1589 	v4l2_ctrl_new_custom(hdl_sdtv_cap, &vivid_ctrl_class, NULL);
1590 	v4l2_ctrl_handler_init(hdl_loop_cap, 1);
1591 	v4l2_ctrl_new_custom(hdl_loop_cap, &vivid_ctrl_class, NULL);
1592 	v4l2_ctrl_handler_init(hdl_fb, 1);
1593 	v4l2_ctrl_new_custom(hdl_fb, &vivid_ctrl_class, NULL);
1594 	v4l2_ctrl_handler_init(hdl_vid_cap, 55);
1595 	v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_class, NULL);
1596 	v4l2_ctrl_handler_init(hdl_vid_out, 26);
1597 	if (!no_error_inj || dev->has_fb || dev->num_hdmi_outputs)
1598 		v4l2_ctrl_new_custom(hdl_vid_out, &vivid_ctrl_class, NULL);
1599 	v4l2_ctrl_handler_init(hdl_vbi_cap, 21);
1600 	v4l2_ctrl_new_custom(hdl_vbi_cap, &vivid_ctrl_class, NULL);
1601 	v4l2_ctrl_handler_init(hdl_vbi_out, 19);
1602 	if (!no_error_inj)
1603 		v4l2_ctrl_new_custom(hdl_vbi_out, &vivid_ctrl_class, NULL);
1604 	v4l2_ctrl_handler_init(hdl_radio_rx, 17);
1605 	v4l2_ctrl_new_custom(hdl_radio_rx, &vivid_ctrl_class, NULL);
1606 	v4l2_ctrl_handler_init(hdl_radio_tx, 17);
1607 	v4l2_ctrl_new_custom(hdl_radio_tx, &vivid_ctrl_class, NULL);
1608 	v4l2_ctrl_handler_init(hdl_sdr_cap, 19);
1609 	v4l2_ctrl_new_custom(hdl_sdr_cap, &vivid_ctrl_class, NULL);
1610 	v4l2_ctrl_handler_init(hdl_meta_cap, 2);
1611 	v4l2_ctrl_new_custom(hdl_meta_cap, &vivid_ctrl_class, NULL);
1612 	v4l2_ctrl_handler_init(hdl_meta_out, 2);
1613 	v4l2_ctrl_new_custom(hdl_meta_out, &vivid_ctrl_class, NULL);
1614 	v4l2_ctrl_handler_init(hdl_tch_cap, 2);
1615 	v4l2_ctrl_new_custom(hdl_tch_cap, &vivid_ctrl_class, NULL);
1616 
1617 	/* User Controls */
1618 	dev->volume = v4l2_ctrl_new_std(hdl_user_aud, NULL,
1619 		V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200);
1620 	dev->mute = v4l2_ctrl_new_std(hdl_user_aud, NULL,
1621 		V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1622 	if (dev->has_vid_cap) {
1623 		dev->brightness = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1624 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1625 		for (i = 0; i < MAX_INPUTS; i++)
1626 			dev->input_brightness[i] = 128;
1627 		dev->contrast = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1628 			V4L2_CID_CONTRAST, 0, 255, 1, 128);
1629 		dev->saturation = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1630 			V4L2_CID_SATURATION, 0, 255, 1, 128);
1631 		dev->hue = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1632 			V4L2_CID_HUE, -128, 128, 1, 0);
1633 		v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1634 			V4L2_CID_HFLIP, 0, 1, 1, 0);
1635 		v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1636 			V4L2_CID_VFLIP, 0, 1, 1, 0);
1637 		dev->autogain = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1638 			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1639 		dev->gain = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1640 			V4L2_CID_GAIN, 0, 255, 1, 100);
1641 		dev->alpha = v4l2_ctrl_new_std(hdl_user_vid, &vivid_user_vid_ctrl_ops,
1642 			V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 0);
1643 	}
1644 	dev->button = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_button, NULL);
1645 	dev->int32 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int32, NULL);
1646 	dev->int64 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int64, NULL);
1647 	dev->boolean = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_boolean, NULL);
1648 	dev->menu = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_menu, NULL);
1649 	dev->string = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_string, NULL);
1650 	dev->bitmask = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_bitmask, NULL);
1651 	dev->int_menu = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_int_menu, NULL);
1652 	dev->ro_int32 = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_ro_int32, NULL);
1653 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_area, NULL);
1654 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_u32_array, NULL);
1655 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_u32_dyn_array, NULL);
1656 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_u16_matrix, NULL);
1657 	v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_u8_4d_array, NULL);
1658 	dev->pixel_array = v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_u8_pixel_array, NULL);
1659 
1660 	if (dev->has_vid_cap) {
1661 		/* Image Processing Controls */
1662 		struct v4l2_ctrl_config vivid_ctrl_test_pattern = {
1663 			.ops = &vivid_vid_cap_ctrl_ops,
1664 			.id = VIVID_CID_TEST_PATTERN,
1665 			.name = "Test Pattern",
1666 			.type = V4L2_CTRL_TYPE_MENU,
1667 			.max = TPG_PAT_NOISE,
1668 			.qmenu = tpg_pattern_strings,
1669 		};
1670 
1671 		dev->test_pattern = v4l2_ctrl_new_custom(hdl_vid_cap,
1672 				&vivid_ctrl_test_pattern, NULL);
1673 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_perc_fill, NULL);
1674 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_hor_movement, NULL);
1675 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_vert_movement, NULL);
1676 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_osd_mode, NULL);
1677 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_show_border, NULL);
1678 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_show_square, NULL);
1679 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_hflip, NULL);
1680 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_vflip, NULL);
1681 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_insert_sav, NULL);
1682 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_insert_eav, NULL);
1683 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_insert_hdmi_video_guard_band, NULL);
1684 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_reduced_fps, NULL);
1685 		if (show_ccs_cap) {
1686 			dev->ctrl_has_crop_cap = v4l2_ctrl_new_custom(hdl_vid_cap,
1687 				&vivid_ctrl_has_crop_cap, NULL);
1688 			dev->ctrl_has_compose_cap = v4l2_ctrl_new_custom(hdl_vid_cap,
1689 				&vivid_ctrl_has_compose_cap, NULL);
1690 			dev->ctrl_has_scaler_cap = v4l2_ctrl_new_custom(hdl_vid_cap,
1691 				&vivid_ctrl_has_scaler_cap, NULL);
1692 		}
1693 
1694 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_tstamp_src, NULL);
1695 		dev->colorspace = v4l2_ctrl_new_custom(hdl_vid_cap,
1696 			&vivid_ctrl_colorspace, NULL);
1697 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_xfer_func, NULL);
1698 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_ycbcr_enc, NULL);
1699 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_hsv_enc, NULL);
1700 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_quantization, NULL);
1701 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_alpha_mode, NULL);
1702 	}
1703 
1704 	if (dev->has_vid_out && show_ccs_out) {
1705 		dev->ctrl_has_crop_out = v4l2_ctrl_new_custom(hdl_vid_out,
1706 			&vivid_ctrl_has_crop_out, NULL);
1707 		dev->ctrl_has_compose_out = v4l2_ctrl_new_custom(hdl_vid_out,
1708 			&vivid_ctrl_has_compose_out, NULL);
1709 		dev->ctrl_has_scaler_out = v4l2_ctrl_new_custom(hdl_vid_out,
1710 			&vivid_ctrl_has_scaler_out, NULL);
1711 	}
1712 
1713 	/*
1714 	 * Testing this driver with v4l2-compliance will trigger the error
1715 	 * injection controls, and after that nothing will work as expected.
1716 	 * So we have a module option to drop these error injecting controls
1717 	 * allowing us to run v4l2_compliance again.
1718 	 */
1719 	if (!no_error_inj) {
1720 		v4l2_ctrl_new_custom(hdl_user_gen, &vivid_ctrl_disconnect, NULL);
1721 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_dqbuf_error, NULL);
1722 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_perc_dropped, NULL);
1723 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_queue_setup_error, NULL);
1724 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_buf_prepare_error, NULL);
1725 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_start_streaming_error, NULL);
1726 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_queue_error, NULL);
1727 #ifdef CONFIG_MEDIA_CONTROLLER
1728 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_req_validate_error, NULL);
1729 #endif
1730 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_seq_wrap, NULL);
1731 		v4l2_ctrl_new_custom(hdl_streaming, &vivid_ctrl_time_wrap, NULL);
1732 	}
1733 
1734 	if (has_sdtv && (dev->has_vid_cap || dev->has_vbi_cap)) {
1735 		if (dev->has_vid_cap)
1736 			v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_std_aspect_ratio, NULL);
1737 		dev->ctrl_std_signal_mode = v4l2_ctrl_new_custom(hdl_sdtv_cap,
1738 			&vivid_ctrl_std_signal_mode, NULL);
1739 		dev->ctrl_standard = v4l2_ctrl_new_custom(hdl_sdtv_cap,
1740 			&vivid_ctrl_standard, NULL);
1741 		if (dev->ctrl_std_signal_mode)
1742 			v4l2_ctrl_cluster(2, &dev->ctrl_std_signal_mode);
1743 		if (dev->has_raw_vbi_cap)
1744 			v4l2_ctrl_new_custom(hdl_vbi_cap, &vivid_ctrl_vbi_cap_interlaced, NULL);
1745 	}
1746 
1747 	if (dev->num_hdmi_inputs) {
1748 		s64 hdmi_input_mask = GENMASK(dev->num_hdmi_inputs - 1, 0);
1749 
1750 		dev->ctrl_dv_timings_signal_mode = v4l2_ctrl_new_custom(hdl_vid_cap,
1751 					&vivid_ctrl_dv_timings_signal_mode, NULL);
1752 
1753 		vivid_ctrl_dv_timings.max = dev->query_dv_timings_size - 1;
1754 		vivid_ctrl_dv_timings.qmenu =
1755 			(const char * const *)dev->query_dv_timings_qmenu;
1756 		dev->ctrl_dv_timings = v4l2_ctrl_new_custom(hdl_vid_cap,
1757 			&vivid_ctrl_dv_timings, NULL);
1758 		if (dev->ctrl_dv_timings_signal_mode)
1759 			v4l2_ctrl_cluster(2, &dev->ctrl_dv_timings_signal_mode);
1760 
1761 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_dv_timings_aspect_ratio, NULL);
1762 		v4l2_ctrl_new_custom(hdl_vid_cap, &vivid_ctrl_max_edid_blocks, NULL);
1763 		dev->real_rgb_range_cap = v4l2_ctrl_new_custom(hdl_vid_cap,
1764 			&vivid_ctrl_limited_rgb_range, NULL);
1765 		dev->rgb_range_cap = v4l2_ctrl_new_std_menu(hdl_vid_cap,
1766 			&vivid_vid_cap_ctrl_ops,
1767 			V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
1768 			0, V4L2_DV_RGB_RANGE_AUTO);
1769 		dev->ctrl_rx_power_present = v4l2_ctrl_new_std(hdl_vid_cap,
1770 			NULL, V4L2_CID_DV_RX_POWER_PRESENT, 0, hdmi_input_mask,
1771 			0, hdmi_input_mask);
1772 
1773 	}
1774 	if (dev->num_hdmi_outputs) {
1775 		s64 hdmi_output_mask = GENMASK(dev->num_hdmi_outputs - 1, 0);
1776 
1777 		/*
1778 		 * We aren't doing anything with this at the moment, but
1779 		 * HDMI outputs typically have this controls.
1780 		 */
1781 		dev->ctrl_tx_rgb_range = v4l2_ctrl_new_std_menu(hdl_vid_out, NULL,
1782 			V4L2_CID_DV_TX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
1783 			0, V4L2_DV_RGB_RANGE_AUTO);
1784 		dev->ctrl_tx_mode = v4l2_ctrl_new_std_menu(hdl_vid_out, NULL,
1785 			V4L2_CID_DV_TX_MODE, V4L2_DV_TX_MODE_HDMI,
1786 			0, V4L2_DV_TX_MODE_HDMI);
1787 		dev->ctrl_display_present = v4l2_ctrl_new_custom(hdl_vid_out,
1788 			&vivid_ctrl_display_present, NULL);
1789 		dev->ctrl_tx_hotplug = v4l2_ctrl_new_std(hdl_vid_out,
1790 			NULL, V4L2_CID_DV_TX_HOTPLUG, 0, hdmi_output_mask,
1791 			0, hdmi_output_mask);
1792 		dev->ctrl_tx_rxsense = v4l2_ctrl_new_std(hdl_vid_out,
1793 			NULL, V4L2_CID_DV_TX_RXSENSE, 0, hdmi_output_mask,
1794 			0, hdmi_output_mask);
1795 		dev->ctrl_tx_edid_present = v4l2_ctrl_new_std(hdl_vid_out,
1796 			NULL, V4L2_CID_DV_TX_EDID_PRESENT, 0, hdmi_output_mask,
1797 			0, hdmi_output_mask);
1798 	}
1799 	if ((dev->has_vid_cap && dev->has_vid_out) ||
1800 	    (dev->has_vbi_cap && dev->has_vbi_out))
1801 		v4l2_ctrl_new_custom(hdl_loop_cap, &vivid_ctrl_loop_video, NULL);
1802 
1803 	if (dev->has_fb)
1804 		v4l2_ctrl_new_custom(hdl_fb, &vivid_ctrl_clear_fb, NULL);
1805 
1806 	if (dev->has_radio_rx) {
1807 		v4l2_ctrl_new_custom(hdl_radio_rx, &vivid_ctrl_radio_hw_seek_mode, NULL);
1808 		v4l2_ctrl_new_custom(hdl_radio_rx, &vivid_ctrl_radio_hw_seek_prog_lim, NULL);
1809 		v4l2_ctrl_new_custom(hdl_radio_rx, &vivid_ctrl_radio_rx_rds_blockio, NULL);
1810 		v4l2_ctrl_new_custom(hdl_radio_rx, &vivid_ctrl_radio_rx_rds_rbds, NULL);
1811 		v4l2_ctrl_new_std(hdl_radio_rx, &vivid_radio_rx_ctrl_ops,
1812 			V4L2_CID_RDS_RECEPTION, 0, 1, 1, 1);
1813 		dev->radio_rx_rds_pty = v4l2_ctrl_new_std(hdl_radio_rx,
1814 			&vivid_radio_rx_ctrl_ops,
1815 			V4L2_CID_RDS_RX_PTY, 0, 31, 1, 0);
1816 		dev->radio_rx_rds_psname = v4l2_ctrl_new_std(hdl_radio_rx,
1817 			&vivid_radio_rx_ctrl_ops,
1818 			V4L2_CID_RDS_RX_PS_NAME, 0, 8, 8, 0);
1819 		dev->radio_rx_rds_radiotext = v4l2_ctrl_new_std(hdl_radio_rx,
1820 			&vivid_radio_rx_ctrl_ops,
1821 			V4L2_CID_RDS_RX_RADIO_TEXT, 0, 64, 64, 0);
1822 		dev->radio_rx_rds_ta = v4l2_ctrl_new_std(hdl_radio_rx,
1823 			&vivid_radio_rx_ctrl_ops,
1824 			V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
1825 		dev->radio_rx_rds_tp = v4l2_ctrl_new_std(hdl_radio_rx,
1826 			&vivid_radio_rx_ctrl_ops,
1827 			V4L2_CID_RDS_RX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
1828 		dev->radio_rx_rds_ms = v4l2_ctrl_new_std(hdl_radio_rx,
1829 			&vivid_radio_rx_ctrl_ops,
1830 			V4L2_CID_RDS_RX_MUSIC_SPEECH, 0, 1, 1, 1);
1831 	}
1832 	if (dev->has_radio_tx) {
1833 		v4l2_ctrl_new_custom(hdl_radio_tx,
1834 			&vivid_ctrl_radio_tx_rds_blockio, NULL);
1835 		dev->radio_tx_rds_pi = v4l2_ctrl_new_std(hdl_radio_tx,
1836 			&vivid_radio_tx_ctrl_ops,
1837 			V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, 0x8088);
1838 		dev->radio_tx_rds_pty = v4l2_ctrl_new_std(hdl_radio_tx,
1839 			&vivid_radio_tx_ctrl_ops,
1840 			V4L2_CID_RDS_TX_PTY, 0, 31, 1, 3);
1841 		dev->radio_tx_rds_psname = v4l2_ctrl_new_std(hdl_radio_tx,
1842 			&vivid_radio_tx_ctrl_ops,
1843 			V4L2_CID_RDS_TX_PS_NAME, 0, 8, 8, 0);
1844 		if (dev->radio_tx_rds_psname)
1845 			v4l2_ctrl_s_ctrl_string(dev->radio_tx_rds_psname, "VIVID-TX");
1846 		dev->radio_tx_rds_radiotext = v4l2_ctrl_new_std(hdl_radio_tx,
1847 			&vivid_radio_tx_ctrl_ops,
1848 			V4L2_CID_RDS_TX_RADIO_TEXT, 0, 64 * 2, 64, 0);
1849 		if (dev->radio_tx_rds_radiotext)
1850 			v4l2_ctrl_s_ctrl_string(dev->radio_tx_rds_radiotext,
1851 			       "This is a VIVID default Radio Text template text, change at will");
1852 		dev->radio_tx_rds_mono_stereo = v4l2_ctrl_new_std(hdl_radio_tx,
1853 			&vivid_radio_tx_ctrl_ops,
1854 			V4L2_CID_RDS_TX_MONO_STEREO, 0, 1, 1, 1);
1855 		dev->radio_tx_rds_art_head = v4l2_ctrl_new_std(hdl_radio_tx,
1856 			&vivid_radio_tx_ctrl_ops,
1857 			V4L2_CID_RDS_TX_ARTIFICIAL_HEAD, 0, 1, 1, 0);
1858 		dev->radio_tx_rds_compressed = v4l2_ctrl_new_std(hdl_radio_tx,
1859 			&vivid_radio_tx_ctrl_ops,
1860 			V4L2_CID_RDS_TX_COMPRESSED, 0, 1, 1, 0);
1861 		dev->radio_tx_rds_dyn_pty = v4l2_ctrl_new_std(hdl_radio_tx,
1862 			&vivid_radio_tx_ctrl_ops,
1863 			V4L2_CID_RDS_TX_DYNAMIC_PTY, 0, 1, 1, 0);
1864 		dev->radio_tx_rds_ta = v4l2_ctrl_new_std(hdl_radio_tx,
1865 			&vivid_radio_tx_ctrl_ops,
1866 			V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
1867 		dev->radio_tx_rds_tp = v4l2_ctrl_new_std(hdl_radio_tx,
1868 			&vivid_radio_tx_ctrl_ops,
1869 			V4L2_CID_RDS_TX_TRAFFIC_PROGRAM, 0, 1, 1, 1);
1870 		dev->radio_tx_rds_ms = v4l2_ctrl_new_std(hdl_radio_tx,
1871 			&vivid_radio_tx_ctrl_ops,
1872 			V4L2_CID_RDS_TX_MUSIC_SPEECH, 0, 1, 1, 1);
1873 	}
1874 	if (dev->has_sdr_cap) {
1875 		v4l2_ctrl_new_custom(hdl_sdr_cap,
1876 			&vivid_ctrl_sdr_cap_fm_deviation, NULL);
1877 	}
1878 	if (dev->has_meta_cap) {
1879 		v4l2_ctrl_new_custom(hdl_meta_cap,
1880 				     &vivid_ctrl_meta_has_pts, NULL);
1881 		v4l2_ctrl_new_custom(hdl_meta_cap,
1882 				     &vivid_ctrl_meta_has_src_clk, NULL);
1883 	}
1884 
1885 	if (hdl_user_gen->error)
1886 		return hdl_user_gen->error;
1887 	if (hdl_user_vid->error)
1888 		return hdl_user_vid->error;
1889 	if (hdl_user_aud->error)
1890 		return hdl_user_aud->error;
1891 	if (hdl_streaming->error)
1892 		return hdl_streaming->error;
1893 	if (hdl_sdr_cap->error)
1894 		return hdl_sdr_cap->error;
1895 	if (hdl_loop_cap->error)
1896 		return hdl_loop_cap->error;
1897 
1898 	if (dev->autogain)
1899 		v4l2_ctrl_auto_cluster(2, &dev->autogain, 0, true);
1900 
1901 	if (dev->has_vid_cap) {
1902 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_user_gen, NULL, false);
1903 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_user_vid, NULL, false);
1904 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_user_aud, NULL, false);
1905 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_streaming, NULL, false);
1906 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_sdtv_cap, NULL, false);
1907 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_loop_cap, NULL, false);
1908 		v4l2_ctrl_add_handler(hdl_vid_cap, hdl_fb, NULL, false);
1909 		if (hdl_vid_cap->error)
1910 			return hdl_vid_cap->error;
1911 		dev->vid_cap_dev.ctrl_handler = hdl_vid_cap;
1912 	}
1913 	if (dev->has_vid_out) {
1914 		v4l2_ctrl_add_handler(hdl_vid_out, hdl_user_gen, NULL, false);
1915 		v4l2_ctrl_add_handler(hdl_vid_out, hdl_user_aud, NULL, false);
1916 		v4l2_ctrl_add_handler(hdl_vid_out, hdl_streaming, NULL, false);
1917 		v4l2_ctrl_add_handler(hdl_vid_out, hdl_fb, NULL, false);
1918 		if (hdl_vid_out->error)
1919 			return hdl_vid_out->error;
1920 		dev->vid_out_dev.ctrl_handler = hdl_vid_out;
1921 	}
1922 	if (dev->has_vbi_cap) {
1923 		v4l2_ctrl_add_handler(hdl_vbi_cap, hdl_user_gen, NULL, false);
1924 		v4l2_ctrl_add_handler(hdl_vbi_cap, hdl_streaming, NULL, false);
1925 		v4l2_ctrl_add_handler(hdl_vbi_cap, hdl_sdtv_cap, NULL, false);
1926 		v4l2_ctrl_add_handler(hdl_vbi_cap, hdl_loop_cap, NULL, false);
1927 		if (hdl_vbi_cap->error)
1928 			return hdl_vbi_cap->error;
1929 		dev->vbi_cap_dev.ctrl_handler = hdl_vbi_cap;
1930 	}
1931 	if (dev->has_vbi_out) {
1932 		v4l2_ctrl_add_handler(hdl_vbi_out, hdl_user_gen, NULL, false);
1933 		v4l2_ctrl_add_handler(hdl_vbi_out, hdl_streaming, NULL, false);
1934 		if (hdl_vbi_out->error)
1935 			return hdl_vbi_out->error;
1936 		dev->vbi_out_dev.ctrl_handler = hdl_vbi_out;
1937 	}
1938 	if (dev->has_radio_rx) {
1939 		v4l2_ctrl_add_handler(hdl_radio_rx, hdl_user_gen, NULL, false);
1940 		v4l2_ctrl_add_handler(hdl_radio_rx, hdl_user_aud, NULL, false);
1941 		if (hdl_radio_rx->error)
1942 			return hdl_radio_rx->error;
1943 		dev->radio_rx_dev.ctrl_handler = hdl_radio_rx;
1944 	}
1945 	if (dev->has_radio_tx) {
1946 		v4l2_ctrl_add_handler(hdl_radio_tx, hdl_user_gen, NULL, false);
1947 		v4l2_ctrl_add_handler(hdl_radio_tx, hdl_user_aud, NULL, false);
1948 		if (hdl_radio_tx->error)
1949 			return hdl_radio_tx->error;
1950 		dev->radio_tx_dev.ctrl_handler = hdl_radio_tx;
1951 	}
1952 	if (dev->has_sdr_cap) {
1953 		v4l2_ctrl_add_handler(hdl_sdr_cap, hdl_user_gen, NULL, false);
1954 		v4l2_ctrl_add_handler(hdl_sdr_cap, hdl_streaming, NULL, false);
1955 		if (hdl_sdr_cap->error)
1956 			return hdl_sdr_cap->error;
1957 		dev->sdr_cap_dev.ctrl_handler = hdl_sdr_cap;
1958 	}
1959 	if (dev->has_meta_cap) {
1960 		v4l2_ctrl_add_handler(hdl_meta_cap, hdl_user_gen, NULL, false);
1961 		v4l2_ctrl_add_handler(hdl_meta_cap, hdl_streaming, NULL, false);
1962 		if (hdl_meta_cap->error)
1963 			return hdl_meta_cap->error;
1964 		dev->meta_cap_dev.ctrl_handler = hdl_meta_cap;
1965 	}
1966 	if (dev->has_meta_out) {
1967 		v4l2_ctrl_add_handler(hdl_meta_out, hdl_user_gen, NULL, false);
1968 		v4l2_ctrl_add_handler(hdl_meta_out, hdl_streaming, NULL, false);
1969 		if (hdl_meta_out->error)
1970 			return hdl_meta_out->error;
1971 		dev->meta_out_dev.ctrl_handler = hdl_meta_out;
1972 	}
1973 	if (dev->has_touch_cap) {
1974 		v4l2_ctrl_add_handler(hdl_tch_cap, hdl_user_gen, NULL, false);
1975 		v4l2_ctrl_add_handler(hdl_tch_cap, hdl_streaming, NULL, false);
1976 		if (hdl_tch_cap->error)
1977 			return hdl_tch_cap->error;
1978 		dev->touch_cap_dev.ctrl_handler = hdl_tch_cap;
1979 	}
1980 	return 0;
1981 }
1982 
1983 void vivid_free_controls(struct vivid_dev *dev)
1984 {
1985 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_vid_cap);
1986 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_vid_out);
1987 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_vbi_cap);
1988 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_vbi_out);
1989 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_radio_rx);
1990 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_radio_tx);
1991 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_sdr_cap);
1992 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_user_gen);
1993 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_user_vid);
1994 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_user_aud);
1995 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_streaming);
1996 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_sdtv_cap);
1997 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_loop_cap);
1998 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_fb);
1999 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_meta_cap);
2000 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_meta_out);
2001 	v4l2_ctrl_handler_free(&dev->ctrl_hdl_touch_cap);
2002 }
2003