From d7bcfd428fd91ed5753fdd9fa3a1a495b2a405d1 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.org>
Date: Wed, 31 Oct 2018 14:57:46 +0000
Subject: [PATCH] media: adv7180: Default to the first valid input

The hardware default is differential CVBS on AIN1 & 2, which
isn't very useful.

Select the first input that is defined as valid for the
chip variant (typically CVBS_AIN1).

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
---
 drivers/media/i2c/adv7180.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

--- a/drivers/media/i2c/adv7180.c
+++ b/drivers/media/i2c/adv7180.c
@@ -859,6 +859,9 @@ static int init_device(struct adv7180_st
 		return ret;
 
 	adv7180_set_field_mode(state);
+	ret = state->chip_info->select_input(state, state->input);
+	if (ret)
+		return ret;
 
 	__v4l2_ctrl_handler_setup(&state->ctrl_hdl);
 
@@ -1415,6 +1418,7 @@ static int adv7180_probe(struct i2c_clie
 	struct device_node *np = client->dev.of_node;
 	struct adv7180_state *state;
 	struct v4l2_subdev *sd;
+	unsigned int i;
 	int ret;
 
 	/* Check if the adapter supports the needed features */
@@ -1470,6 +1474,14 @@ static int adv7180_probe(struct i2c_clie
 	state->curr_norm = V4L2_STD_NTSC;
 
 	state->input = 0;
+	/* Select first valid input */
+	for (i = 0; i < 32; i++) {
+		if (BIT(i) & state->chip_info->valid_input_mask) {
+			state->input = i;
+			break;
+		}
+	}
+
 	sd = &state->sd;
 	v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
 	sd->internal_ops = &adv7180_internal_ops;
