18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
58c2ecf20Sopenharmony_ci *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci   This source file is specifically designed to interface with the
118c2ecf20Sopenharmony_ci   saa711x support that is available in the v4l available starting
128c2ecf20Sopenharmony_ci   with linux 2.6.15.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci*/
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "pvrusb2-video-v4l.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "pvrusb2-hdw-internal.h"
218c2ecf20Sopenharmony_ci#include "pvrusb2-debug.h"
228c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
248c2ecf20Sopenharmony_ci#include <media/i2c/saa7115.h>
258c2ecf20Sopenharmony_ci#include <linux/errno.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct routing_scheme {
288c2ecf20Sopenharmony_ci	const int *def;
298c2ecf20Sopenharmony_ci	unsigned int cnt;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic const int routing_scheme0[] = {
348c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
358c2ecf20Sopenharmony_ci	/* In radio mode, we mute the video, but point at one
368c2ecf20Sopenharmony_ci	   spot just to stay consistent */
378c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
388c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
398c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_SVIDEO] =  SAA7115_SVIDEO2,
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic const struct routing_scheme routing_def0 = {
438c2ecf20Sopenharmony_ci	.def = routing_scheme0,
448c2ecf20Sopenharmony_ci	.cnt = ARRAY_SIZE(routing_scheme0),
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const int routing_scheme1[] = {
488c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
498c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
508c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE3,
518c2ecf20Sopenharmony_ci	[PVR2_CVAL_INPUT_SVIDEO] =  SAA7115_SVIDEO2, /* or SVIDEO0, it seems */
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const struct routing_scheme routing_def1 = {
558c2ecf20Sopenharmony_ci	.def = routing_scheme1,
568c2ecf20Sopenharmony_ci	.cnt = ARRAY_SIZE(routing_scheme1),
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic const struct routing_scheme *routing_schemes[] = {
608c2ecf20Sopenharmony_ci	[PVR2_ROUTING_SCHEME_HAUPPAUGE] = &routing_def0,
618c2ecf20Sopenharmony_ci	[PVR2_ROUTING_SCHEME_ONAIR] = &routing_def1,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_civoid pvr2_saa7115_subdev_update(struct pvr2_hdw *hdw, struct v4l2_subdev *sd)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	if (hdw->input_dirty || hdw->force_dirty) {
678c2ecf20Sopenharmony_ci		const struct routing_scheme *sp;
688c2ecf20Sopenharmony_ci		unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
698c2ecf20Sopenharmony_ci		u32 input;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		pvr2_trace(PVR2_TRACE_CHIPS, "subdev v4l2 set_input(%d)",
728c2ecf20Sopenharmony_ci			   hdw->input_val);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		sp = (sid < ARRAY_SIZE(routing_schemes)) ?
758c2ecf20Sopenharmony_ci			routing_schemes[sid] : NULL;
768c2ecf20Sopenharmony_ci		if ((sp == NULL) ||
778c2ecf20Sopenharmony_ci		    (hdw->input_val < 0) ||
788c2ecf20Sopenharmony_ci		    (hdw->input_val >= sp->cnt)) {
798c2ecf20Sopenharmony_ci			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
808c2ecf20Sopenharmony_ci				   "*** WARNING *** subdev v4l2 set_input: Invalid routing scheme (%u) and/or input (%d)",
818c2ecf20Sopenharmony_ci				   sid, hdw->input_val);
828c2ecf20Sopenharmony_ci			return;
838c2ecf20Sopenharmony_ci		}
848c2ecf20Sopenharmony_ci		input = sp->def[hdw->input_val];
858c2ecf20Sopenharmony_ci		sd->ops->video->s_routing(sd, input, 0, 0);
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci}
88