xref: /linux/drivers/media/i2c/ks0127.c (revision c02b211df6fc54e51ee554c27a6736a11255a764)
1cb7a01acSMauro Carvalho Chehab /*
2cb7a01acSMauro Carvalho Chehab  * Video Capture Driver (Video for Linux 1/2)
3cb7a01acSMauro Carvalho Chehab  * for the Matrox Marvel G200,G400 and Rainbow Runner-G series
4cb7a01acSMauro Carvalho Chehab  *
5cb7a01acSMauro Carvalho Chehab  * This module is an interface to the KS0127 video decoder chip.
6cb7a01acSMauro Carvalho Chehab  *
7cb7a01acSMauro Carvalho Chehab  * Copyright (C) 1999  Ryan Drake <stiletto@mediaone.net>
8cb7a01acSMauro Carvalho Chehab  *
9cb7a01acSMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or
10cb7a01acSMauro Carvalho Chehab  * modify it under the terms of the GNU General Public License
11cb7a01acSMauro Carvalho Chehab  * as published by the Free Software Foundation; either version 2
12cb7a01acSMauro Carvalho Chehab  * of the License, or (at your option) any later version.
13cb7a01acSMauro Carvalho Chehab  *
14cb7a01acSMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
15cb7a01acSMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16cb7a01acSMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17cb7a01acSMauro Carvalho Chehab  * GNU General Public License for more details.
18cb7a01acSMauro Carvalho Chehab  *
19cb7a01acSMauro Carvalho Chehab  * You should have received a copy of the GNU General Public License
20cb7a01acSMauro Carvalho Chehab  * along with this program; if not, write to the Free Software
21cb7a01acSMauro Carvalho Chehab  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22cb7a01acSMauro Carvalho Chehab  *
23cb7a01acSMauro Carvalho Chehab  *****************************************************************************
24cb7a01acSMauro Carvalho Chehab  *
25cb7a01acSMauro Carvalho Chehab  * Modified and extended by
26cb7a01acSMauro Carvalho Chehab  *	Mike Bernson <mike@mlb.org>
27cb7a01acSMauro Carvalho Chehab  *	Gerard v.d. Horst
28cb7a01acSMauro Carvalho Chehab  *	Leon van Stuivenberg <l.vanstuivenberg@chello.nl>
29cb7a01acSMauro Carvalho Chehab  *	Gernot Ziegler <gz@lysator.liu.se>
30cb7a01acSMauro Carvalho Chehab  *
31cb7a01acSMauro Carvalho Chehab  * Version History:
32cb7a01acSMauro Carvalho Chehab  * V1.0 Ryan Drake	   Initial version by Ryan Drake
33cb7a01acSMauro Carvalho Chehab  * V1.1 Gerard v.d. Horst  Added some debugoutput, reset the video-standard
34cb7a01acSMauro Carvalho Chehab  */
35cb7a01acSMauro Carvalho Chehab 
36cb7a01acSMauro Carvalho Chehab #include <linux/init.h>
37cb7a01acSMauro Carvalho Chehab #include <linux/module.h>
38cb7a01acSMauro Carvalho Chehab #include <linux/delay.h>
39cb7a01acSMauro Carvalho Chehab #include <linux/errno.h>
40cb7a01acSMauro Carvalho Chehab #include <linux/kernel.h>
41cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h>
42cb7a01acSMauro Carvalho Chehab #include <linux/videodev2.h>
43cb7a01acSMauro Carvalho Chehab #include <linux/slab.h>
44cb7a01acSMauro Carvalho Chehab #include <media/v4l2-device.h>
45cb7a01acSMauro Carvalho Chehab #include <media/v4l2-chip-ident.h>
46cb7a01acSMauro Carvalho Chehab #include "ks0127.h"
47cb7a01acSMauro Carvalho Chehab 
48cb7a01acSMauro Carvalho Chehab MODULE_DESCRIPTION("KS0127 video decoder driver");
49cb7a01acSMauro Carvalho Chehab MODULE_AUTHOR("Ryan Drake");
50cb7a01acSMauro Carvalho Chehab MODULE_LICENSE("GPL");
51cb7a01acSMauro Carvalho Chehab 
52cb7a01acSMauro Carvalho Chehab /* Addresses */
53cb7a01acSMauro Carvalho Chehab #define I2C_KS0127_ADDON   0xD8
54cb7a01acSMauro Carvalho Chehab #define I2C_KS0127_ONBOARD 0xDA
55cb7a01acSMauro Carvalho Chehab 
56cb7a01acSMauro Carvalho Chehab 
57cb7a01acSMauro Carvalho Chehab /* ks0127 control registers */
58cb7a01acSMauro Carvalho Chehab #define KS_STAT     0x00
59cb7a01acSMauro Carvalho Chehab #define KS_CMDA     0x01
60cb7a01acSMauro Carvalho Chehab #define KS_CMDB     0x02
61cb7a01acSMauro Carvalho Chehab #define KS_CMDC     0x03
62cb7a01acSMauro Carvalho Chehab #define KS_CMDD     0x04
63cb7a01acSMauro Carvalho Chehab #define KS_HAVB     0x05
64cb7a01acSMauro Carvalho Chehab #define KS_HAVE     0x06
65cb7a01acSMauro Carvalho Chehab #define KS_HS1B     0x07
66cb7a01acSMauro Carvalho Chehab #define KS_HS1E     0x08
67cb7a01acSMauro Carvalho Chehab #define KS_HS2B     0x09
68cb7a01acSMauro Carvalho Chehab #define KS_HS2E     0x0a
69cb7a01acSMauro Carvalho Chehab #define KS_AGC      0x0b
70cb7a01acSMauro Carvalho Chehab #define KS_HXTRA    0x0c
71cb7a01acSMauro Carvalho Chehab #define KS_CDEM     0x0d
72cb7a01acSMauro Carvalho Chehab #define KS_PORTAB   0x0e
73cb7a01acSMauro Carvalho Chehab #define KS_LUMA     0x0f
74cb7a01acSMauro Carvalho Chehab #define KS_CON      0x10
75cb7a01acSMauro Carvalho Chehab #define KS_BRT      0x11
76cb7a01acSMauro Carvalho Chehab #define KS_CHROMA   0x12
77cb7a01acSMauro Carvalho Chehab #define KS_CHROMB   0x13
78cb7a01acSMauro Carvalho Chehab #define KS_DEMOD    0x14
79cb7a01acSMauro Carvalho Chehab #define KS_SAT      0x15
80cb7a01acSMauro Carvalho Chehab #define KS_HUE      0x16
81cb7a01acSMauro Carvalho Chehab #define KS_VERTIA   0x17
82cb7a01acSMauro Carvalho Chehab #define KS_VERTIB   0x18
83cb7a01acSMauro Carvalho Chehab #define KS_VERTIC   0x19
84cb7a01acSMauro Carvalho Chehab #define KS_HSCLL    0x1a
85cb7a01acSMauro Carvalho Chehab #define KS_HSCLH    0x1b
86cb7a01acSMauro Carvalho Chehab #define KS_VSCLL    0x1c
87cb7a01acSMauro Carvalho Chehab #define KS_VSCLH    0x1d
88cb7a01acSMauro Carvalho Chehab #define KS_OFMTA    0x1e
89cb7a01acSMauro Carvalho Chehab #define KS_OFMTB    0x1f
90cb7a01acSMauro Carvalho Chehab #define KS_VBICTL   0x20
91cb7a01acSMauro Carvalho Chehab #define KS_CCDAT2   0x21
92cb7a01acSMauro Carvalho Chehab #define KS_CCDAT1   0x22
93cb7a01acSMauro Carvalho Chehab #define KS_VBIL30   0x23
94cb7a01acSMauro Carvalho Chehab #define KS_VBIL74   0x24
95cb7a01acSMauro Carvalho Chehab #define KS_VBIL118  0x25
96cb7a01acSMauro Carvalho Chehab #define KS_VBIL1512 0x26
97cb7a01acSMauro Carvalho Chehab #define KS_TTFRAM   0x27
98cb7a01acSMauro Carvalho Chehab #define KS_TESTA    0x28
99cb7a01acSMauro Carvalho Chehab #define KS_UVOFFH   0x29
100cb7a01acSMauro Carvalho Chehab #define KS_UVOFFL   0x2a
101cb7a01acSMauro Carvalho Chehab #define KS_UGAIN    0x2b
102cb7a01acSMauro Carvalho Chehab #define KS_VGAIN    0x2c
103cb7a01acSMauro Carvalho Chehab #define KS_VAVB     0x2d
104cb7a01acSMauro Carvalho Chehab #define KS_VAVE     0x2e
105cb7a01acSMauro Carvalho Chehab #define KS_CTRACK   0x2f
106cb7a01acSMauro Carvalho Chehab #define KS_POLCTL   0x30
107cb7a01acSMauro Carvalho Chehab #define KS_REFCOD   0x31
108cb7a01acSMauro Carvalho Chehab #define KS_INVALY   0x32
109cb7a01acSMauro Carvalho Chehab #define KS_INVALU   0x33
110cb7a01acSMauro Carvalho Chehab #define KS_INVALV   0x34
111cb7a01acSMauro Carvalho Chehab #define KS_UNUSEY   0x35
112cb7a01acSMauro Carvalho Chehab #define KS_UNUSEU   0x36
113cb7a01acSMauro Carvalho Chehab #define KS_UNUSEV   0x37
114cb7a01acSMauro Carvalho Chehab #define KS_USRSAV   0x38
115cb7a01acSMauro Carvalho Chehab #define KS_USREAV   0x39
116cb7a01acSMauro Carvalho Chehab #define KS_SHS1A    0x3a
117cb7a01acSMauro Carvalho Chehab #define KS_SHS1B    0x3b
118cb7a01acSMauro Carvalho Chehab #define KS_SHS1C    0x3c
119cb7a01acSMauro Carvalho Chehab #define KS_CMDE     0x3d
120cb7a01acSMauro Carvalho Chehab #define KS_VSDEL    0x3e
121cb7a01acSMauro Carvalho Chehab #define KS_CMDF     0x3f
122cb7a01acSMauro Carvalho Chehab #define KS_GAMMA0   0x40
123cb7a01acSMauro Carvalho Chehab #define KS_GAMMA1   0x41
124cb7a01acSMauro Carvalho Chehab #define KS_GAMMA2   0x42
125cb7a01acSMauro Carvalho Chehab #define KS_GAMMA3   0x43
126cb7a01acSMauro Carvalho Chehab #define KS_GAMMA4   0x44
127cb7a01acSMauro Carvalho Chehab #define KS_GAMMA5   0x45
128cb7a01acSMauro Carvalho Chehab #define KS_GAMMA6   0x46
129cb7a01acSMauro Carvalho Chehab #define KS_GAMMA7   0x47
130cb7a01acSMauro Carvalho Chehab #define KS_GAMMA8   0x48
131cb7a01acSMauro Carvalho Chehab #define KS_GAMMA9   0x49
132cb7a01acSMauro Carvalho Chehab #define KS_GAMMA10  0x4a
133cb7a01acSMauro Carvalho Chehab #define KS_GAMMA11  0x4b
134cb7a01acSMauro Carvalho Chehab #define KS_GAMMA12  0x4c
135cb7a01acSMauro Carvalho Chehab #define KS_GAMMA13  0x4d
136cb7a01acSMauro Carvalho Chehab #define KS_GAMMA14  0x4e
137cb7a01acSMauro Carvalho Chehab #define KS_GAMMA15  0x4f
138cb7a01acSMauro Carvalho Chehab #define KS_GAMMA16  0x50
139cb7a01acSMauro Carvalho Chehab #define KS_GAMMA17  0x51
140cb7a01acSMauro Carvalho Chehab #define KS_GAMMA18  0x52
141cb7a01acSMauro Carvalho Chehab #define KS_GAMMA19  0x53
142cb7a01acSMauro Carvalho Chehab #define KS_GAMMA20  0x54
143cb7a01acSMauro Carvalho Chehab #define KS_GAMMA21  0x55
144cb7a01acSMauro Carvalho Chehab #define KS_GAMMA22  0x56
145cb7a01acSMauro Carvalho Chehab #define KS_GAMMA23  0x57
146cb7a01acSMauro Carvalho Chehab #define KS_GAMMA24  0x58
147cb7a01acSMauro Carvalho Chehab #define KS_GAMMA25  0x59
148cb7a01acSMauro Carvalho Chehab #define KS_GAMMA26  0x5a
149cb7a01acSMauro Carvalho Chehab #define KS_GAMMA27  0x5b
150cb7a01acSMauro Carvalho Chehab #define KS_GAMMA28  0x5c
151cb7a01acSMauro Carvalho Chehab #define KS_GAMMA29  0x5d
152cb7a01acSMauro Carvalho Chehab #define KS_GAMMA30  0x5e
153cb7a01acSMauro Carvalho Chehab #define KS_GAMMA31  0x5f
154cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD0  0x60
155cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD1  0x61
156cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD2  0x62
157cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD3  0x63
158cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD4  0x64
159cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD5  0x65
160cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD6  0x66
161cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD7  0x67
162cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD8  0x68
163cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD9  0x69
164cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD10 0x6a
165cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD11 0x6b
166cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD12 0x6c
167cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD13 0x6d
168cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD14 0x6e
169cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD15 0x6f
170cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD16 0x70
171cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD17 0x71
172cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD18 0x72
173cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD19 0x73
174cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD20 0x74
175cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD21 0x75
176cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD22 0x76
177cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD23 0x77
178cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD24 0x78
179cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD25 0x79
180cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD26 0x7a
181cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD27 0x7b
182cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD28 0x7c
183cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD29 0x7d
184cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD30 0x7e
185cb7a01acSMauro Carvalho Chehab #define KS_GAMMAD31 0x7f
186cb7a01acSMauro Carvalho Chehab 
187cb7a01acSMauro Carvalho Chehab 
188cb7a01acSMauro Carvalho Chehab /****************************************************************************
189cb7a01acSMauro Carvalho Chehab * mga_dev : represents one ks0127 chip.
190cb7a01acSMauro Carvalho Chehab ****************************************************************************/
191cb7a01acSMauro Carvalho Chehab 
192cb7a01acSMauro Carvalho Chehab struct adjust {
193cb7a01acSMauro Carvalho Chehab 	int	contrast;
194cb7a01acSMauro Carvalho Chehab 	int	bright;
195cb7a01acSMauro Carvalho Chehab 	int	hue;
196cb7a01acSMauro Carvalho Chehab 	int	ugain;
197cb7a01acSMauro Carvalho Chehab 	int	vgain;
198cb7a01acSMauro Carvalho Chehab };
199cb7a01acSMauro Carvalho Chehab 
200cb7a01acSMauro Carvalho Chehab struct ks0127 {
201cb7a01acSMauro Carvalho Chehab 	struct v4l2_subdev sd;
202cb7a01acSMauro Carvalho Chehab 	v4l2_std_id	norm;
203cb7a01acSMauro Carvalho Chehab 	int		ident;
204cb7a01acSMauro Carvalho Chehab 	u8 		regs[256];
205cb7a01acSMauro Carvalho Chehab };
206cb7a01acSMauro Carvalho Chehab 
207cb7a01acSMauro Carvalho Chehab static inline struct ks0127 *to_ks0127(struct v4l2_subdev *sd)
208cb7a01acSMauro Carvalho Chehab {
209cb7a01acSMauro Carvalho Chehab 	return container_of(sd, struct ks0127, sd);
210cb7a01acSMauro Carvalho Chehab }
211cb7a01acSMauro Carvalho Chehab 
212cb7a01acSMauro Carvalho Chehab 
213cb7a01acSMauro Carvalho Chehab static int debug; /* insmod parameter */
214cb7a01acSMauro Carvalho Chehab 
215cb7a01acSMauro Carvalho Chehab module_param(debug, int, 0);
216cb7a01acSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Debug output");
217cb7a01acSMauro Carvalho Chehab 
218cb7a01acSMauro Carvalho Chehab static u8 reg_defaults[64];
219cb7a01acSMauro Carvalho Chehab 
220cb7a01acSMauro Carvalho Chehab static void init_reg_defaults(void)
221cb7a01acSMauro Carvalho Chehab {
222cb7a01acSMauro Carvalho Chehab 	static int initialized;
223cb7a01acSMauro Carvalho Chehab 	u8 *table = reg_defaults;
224cb7a01acSMauro Carvalho Chehab 
225cb7a01acSMauro Carvalho Chehab 	if (initialized)
226cb7a01acSMauro Carvalho Chehab 		return;
227cb7a01acSMauro Carvalho Chehab 	initialized = 1;
228cb7a01acSMauro Carvalho Chehab 
229cb7a01acSMauro Carvalho Chehab 	table[KS_CMDA]     = 0x2c;  /* VSE=0, CCIR 601, autodetect standard */
230cb7a01acSMauro Carvalho Chehab 	table[KS_CMDB]     = 0x12;  /* VALIGN=0, AGC control and input */
231cb7a01acSMauro Carvalho Chehab 	table[KS_CMDC]     = 0x00;  /* Test options */
232cb7a01acSMauro Carvalho Chehab 	/* clock & input select, write 1 to PORTA */
233cb7a01acSMauro Carvalho Chehab 	table[KS_CMDD]     = 0x01;
234cb7a01acSMauro Carvalho Chehab 	table[KS_HAVB]     = 0x00;  /* HAV Start Control */
235cb7a01acSMauro Carvalho Chehab 	table[KS_HAVE]     = 0x00;  /* HAV End Control */
236cb7a01acSMauro Carvalho Chehab 	table[KS_HS1B]     = 0x10;  /* HS1 Start Control */
237cb7a01acSMauro Carvalho Chehab 	table[KS_HS1E]     = 0x00;  /* HS1 End Control */
238cb7a01acSMauro Carvalho Chehab 	table[KS_HS2B]     = 0x00;  /* HS2 Start Control */
239cb7a01acSMauro Carvalho Chehab 	table[KS_HS2E]     = 0x00;  /* HS2 End Control */
240cb7a01acSMauro Carvalho Chehab 	table[KS_AGC]      = 0x53;  /* Manual setting for AGC */
241cb7a01acSMauro Carvalho Chehab 	table[KS_HXTRA]    = 0x00;  /* Extra Bits for HAV and HS1/2 */
242cb7a01acSMauro Carvalho Chehab 	table[KS_CDEM]     = 0x00;  /* Chroma Demodulation Control */
243cb7a01acSMauro Carvalho Chehab 	table[KS_PORTAB]   = 0x0f;  /* port B is input, port A output GPPORT */
244cb7a01acSMauro Carvalho Chehab 	table[KS_LUMA]     = 0x01;  /* Luma control */
245cb7a01acSMauro Carvalho Chehab 	table[KS_CON]      = 0x00;  /* Contrast Control */
246cb7a01acSMauro Carvalho Chehab 	table[KS_BRT]      = 0x00;  /* Brightness Control */
247cb7a01acSMauro Carvalho Chehab 	table[KS_CHROMA]   = 0x2a;  /* Chroma control A */
248cb7a01acSMauro Carvalho Chehab 	table[KS_CHROMB]   = 0x90;  /* Chroma control B */
249cb7a01acSMauro Carvalho Chehab 	table[KS_DEMOD]    = 0x00;  /* Chroma Demodulation Control & Status */
250cb7a01acSMauro Carvalho Chehab 	table[KS_SAT]      = 0x00;  /* Color Saturation Control*/
251cb7a01acSMauro Carvalho Chehab 	table[KS_HUE]      = 0x00;  /* Hue Control */
252cb7a01acSMauro Carvalho Chehab 	table[KS_VERTIA]   = 0x00;  /* Vertical Processing Control A */
253cb7a01acSMauro Carvalho Chehab 	/* Vertical Processing Control B, luma 1 line delayed */
254cb7a01acSMauro Carvalho Chehab 	table[KS_VERTIB]   = 0x12;
255cb7a01acSMauro Carvalho Chehab 	table[KS_VERTIC]   = 0x0b;  /* Vertical Processing Control C */
256cb7a01acSMauro Carvalho Chehab 	table[KS_HSCLL]    = 0x00;  /* Horizontal Scaling Ratio Low */
257cb7a01acSMauro Carvalho Chehab 	table[KS_HSCLH]    = 0x00;  /* Horizontal Scaling Ratio High */
258cb7a01acSMauro Carvalho Chehab 	table[KS_VSCLL]    = 0x00;  /* Vertical Scaling Ratio Low */
259cb7a01acSMauro Carvalho Chehab 	table[KS_VSCLH]    = 0x00;  /* Vertical Scaling Ratio High */
260cb7a01acSMauro Carvalho Chehab 	/* 16 bit YCbCr 4:2:2 output; I can't make the bt866 like 8 bit /Sam */
261cb7a01acSMauro Carvalho Chehab 	table[KS_OFMTA]    = 0x30;
262cb7a01acSMauro Carvalho Chehab 	table[KS_OFMTB]    = 0x00;  /* Output Control B */
263cb7a01acSMauro Carvalho Chehab 	/* VBI Decoder Control; 4bit fmt: avoid Y overflow */
264cb7a01acSMauro Carvalho Chehab 	table[KS_VBICTL]   = 0x5d;
265cb7a01acSMauro Carvalho Chehab 	table[KS_CCDAT2]   = 0x00;  /* Read Only register */
266cb7a01acSMauro Carvalho Chehab 	table[KS_CCDAT1]   = 0x00;  /* Read Only register */
267cb7a01acSMauro Carvalho Chehab 	table[KS_VBIL30]   = 0xa8;  /* VBI data decoding options */
268cb7a01acSMauro Carvalho Chehab 	table[KS_VBIL74]   = 0xaa;  /* VBI data decoding options */
269cb7a01acSMauro Carvalho Chehab 	table[KS_VBIL118]  = 0x2a;  /* VBI data decoding options */
270cb7a01acSMauro Carvalho Chehab 	table[KS_VBIL1512] = 0x00;  /* VBI data decoding options */
271cb7a01acSMauro Carvalho Chehab 	table[KS_TTFRAM]   = 0x00;  /* Teletext frame alignment pattern */
272cb7a01acSMauro Carvalho Chehab 	table[KS_TESTA]    = 0x00;  /* test register, shouldn't be written */
273cb7a01acSMauro Carvalho Chehab 	table[KS_UVOFFH]   = 0x00;  /* UV Offset Adjustment High */
274cb7a01acSMauro Carvalho Chehab 	table[KS_UVOFFL]   = 0x00;  /* UV Offset Adjustment Low */
275cb7a01acSMauro Carvalho Chehab 	table[KS_UGAIN]    = 0x00;  /* U Component Gain Adjustment */
276cb7a01acSMauro Carvalho Chehab 	table[KS_VGAIN]    = 0x00;  /* V Component Gain Adjustment */
277cb7a01acSMauro Carvalho Chehab 	table[KS_VAVB]     = 0x07;  /* VAV Begin */
278cb7a01acSMauro Carvalho Chehab 	table[KS_VAVE]     = 0x00;  /* VAV End */
279cb7a01acSMauro Carvalho Chehab 	table[KS_CTRACK]   = 0x00;  /* Chroma Tracking Control */
280cb7a01acSMauro Carvalho Chehab 	table[KS_POLCTL]   = 0x41;  /* Timing Signal Polarity Control */
281cb7a01acSMauro Carvalho Chehab 	table[KS_REFCOD]   = 0x80;  /* Reference Code Insertion Control */
282cb7a01acSMauro Carvalho Chehab 	table[KS_INVALY]   = 0x10;  /* Invalid Y Code */
283cb7a01acSMauro Carvalho Chehab 	table[KS_INVALU]   = 0x80;  /* Invalid U Code */
284cb7a01acSMauro Carvalho Chehab 	table[KS_INVALV]   = 0x80;  /* Invalid V Code */
285cb7a01acSMauro Carvalho Chehab 	table[KS_UNUSEY]   = 0x10;  /* Unused Y Code */
286cb7a01acSMauro Carvalho Chehab 	table[KS_UNUSEU]   = 0x80;  /* Unused U Code */
287cb7a01acSMauro Carvalho Chehab 	table[KS_UNUSEV]   = 0x80;  /* Unused V Code */
288cb7a01acSMauro Carvalho Chehab 	table[KS_USRSAV]   = 0x00;  /* reserved */
289cb7a01acSMauro Carvalho Chehab 	table[KS_USREAV]   = 0x00;  /* reserved */
290cb7a01acSMauro Carvalho Chehab 	table[KS_SHS1A]    = 0x00;  /* User Defined SHS1 A */
291cb7a01acSMauro Carvalho Chehab 	/* User Defined SHS1 B, ALT656=1 on 0127B */
292cb7a01acSMauro Carvalho Chehab 	table[KS_SHS1B]    = 0x80;
293cb7a01acSMauro Carvalho Chehab 	table[KS_SHS1C]    = 0x00;  /* User Defined SHS1 C */
294cb7a01acSMauro Carvalho Chehab 	table[KS_CMDE]     = 0x00;  /* Command Register E */
295cb7a01acSMauro Carvalho Chehab 	table[KS_VSDEL]    = 0x00;  /* VS Delay Control */
296cb7a01acSMauro Carvalho Chehab 	/* Command Register F, update -immediately- */
297cb7a01acSMauro Carvalho Chehab 	/* (there might come no vsync)*/
298cb7a01acSMauro Carvalho Chehab 	table[KS_CMDF]     = 0x02;
299cb7a01acSMauro Carvalho Chehab }
300cb7a01acSMauro Carvalho Chehab 
301cb7a01acSMauro Carvalho Chehab 
302cb7a01acSMauro Carvalho Chehab /* We need to manually read because of a bug in the KS0127 chip.
303cb7a01acSMauro Carvalho Chehab  *
304cb7a01acSMauro Carvalho Chehab  * An explanation from kayork@mail.utexas.edu:
305cb7a01acSMauro Carvalho Chehab  *
306cb7a01acSMauro Carvalho Chehab  * During I2C reads, the KS0127 only samples for a stop condition
307cb7a01acSMauro Carvalho Chehab  * during the place where the acknowledge bit should be. Any standard
308cb7a01acSMauro Carvalho Chehab  * I2C implementation (correctly) throws in another clock transition
309cb7a01acSMauro Carvalho Chehab  * at the 9th bit, and the KS0127 will not recognize the stop condition
310cb7a01acSMauro Carvalho Chehab  * and will continue to clock out data.
311cb7a01acSMauro Carvalho Chehab  *
312cb7a01acSMauro Carvalho Chehab  * So we have to do the read ourself.  Big deal.
313cb7a01acSMauro Carvalho Chehab  *	   workaround in i2c-algo-bit
314cb7a01acSMauro Carvalho Chehab  */
315cb7a01acSMauro Carvalho Chehab 
316cb7a01acSMauro Carvalho Chehab 
317cb7a01acSMauro Carvalho Chehab static u8 ks0127_read(struct v4l2_subdev *sd, u8 reg)
318cb7a01acSMauro Carvalho Chehab {
319cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(sd);
320cb7a01acSMauro Carvalho Chehab 	char val = 0;
321cb7a01acSMauro Carvalho Chehab 	struct i2c_msg msgs[] = {
3222a9d2472SShubhrajyoti D 		{
3232a9d2472SShubhrajyoti D 			.addr = client->addr,
3242a9d2472SShubhrajyoti D 			.len = sizeof(reg),
3252a9d2472SShubhrajyoti D 			.buf = &reg
3262a9d2472SShubhrajyoti D 		},
3272a9d2472SShubhrajyoti D 		{
3282a9d2472SShubhrajyoti D 			.addr = client->addr,
3292a9d2472SShubhrajyoti D 			.flags = I2C_M_RD | I2C_M_NO_RD_ACK,
3302a9d2472SShubhrajyoti D 			.len = sizeof(val),
3312a9d2472SShubhrajyoti D 			.buf = &val
3322a9d2472SShubhrajyoti D 		}
333cb7a01acSMauro Carvalho Chehab 	};
334cb7a01acSMauro Carvalho Chehab 	int ret;
335cb7a01acSMauro Carvalho Chehab 
336cb7a01acSMauro Carvalho Chehab 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
337cb7a01acSMauro Carvalho Chehab 	if (ret != ARRAY_SIZE(msgs))
338cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "read error\n");
339cb7a01acSMauro Carvalho Chehab 
340cb7a01acSMauro Carvalho Chehab 	return val;
341cb7a01acSMauro Carvalho Chehab }
342cb7a01acSMauro Carvalho Chehab 
343cb7a01acSMauro Carvalho Chehab 
344cb7a01acSMauro Carvalho Chehab static void ks0127_write(struct v4l2_subdev *sd, u8 reg, u8 val)
345cb7a01acSMauro Carvalho Chehab {
346cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(sd);
347cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
348cb7a01acSMauro Carvalho Chehab 	char msg[] = { reg, val };
349cb7a01acSMauro Carvalho Chehab 
350cb7a01acSMauro Carvalho Chehab 	if (i2c_master_send(client, msg, sizeof(msg)) != sizeof(msg))
351cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "write error\n");
352cb7a01acSMauro Carvalho Chehab 
353cb7a01acSMauro Carvalho Chehab 	ks->regs[reg] = val;
354cb7a01acSMauro Carvalho Chehab }
355cb7a01acSMauro Carvalho Chehab 
356cb7a01acSMauro Carvalho Chehab 
357cb7a01acSMauro Carvalho Chehab /* generic bit-twiddling */
358cb7a01acSMauro Carvalho Chehab static void ks0127_and_or(struct v4l2_subdev *sd, u8 reg, u8 and_v, u8 or_v)
359cb7a01acSMauro Carvalho Chehab {
360cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
361cb7a01acSMauro Carvalho Chehab 
362cb7a01acSMauro Carvalho Chehab 	u8 val = ks->regs[reg];
363cb7a01acSMauro Carvalho Chehab 	val = (val & and_v) | or_v;
364cb7a01acSMauro Carvalho Chehab 	ks0127_write(sd, reg, val);
365cb7a01acSMauro Carvalho Chehab }
366cb7a01acSMauro Carvalho Chehab 
367cb7a01acSMauro Carvalho Chehab 
368cb7a01acSMauro Carvalho Chehab 
369cb7a01acSMauro Carvalho Chehab /****************************************************************************
370cb7a01acSMauro Carvalho Chehab * ks0127 private api
371cb7a01acSMauro Carvalho Chehab ****************************************************************************/
372cb7a01acSMauro Carvalho Chehab static void ks0127_init(struct v4l2_subdev *sd)
373cb7a01acSMauro Carvalho Chehab {
374cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
375cb7a01acSMauro Carvalho Chehab 	u8 *table = reg_defaults;
376cb7a01acSMauro Carvalho Chehab 	int i;
377cb7a01acSMauro Carvalho Chehab 
378cb7a01acSMauro Carvalho Chehab 	ks->ident = V4L2_IDENT_KS0127;
379cb7a01acSMauro Carvalho Chehab 
380cb7a01acSMauro Carvalho Chehab 	v4l2_dbg(1, debug, sd, "reset\n");
381cb7a01acSMauro Carvalho Chehab 	msleep(1);
382cb7a01acSMauro Carvalho Chehab 
383cb7a01acSMauro Carvalho Chehab 	/* initialize all registers to known values */
384cb7a01acSMauro Carvalho Chehab 	/* (except STAT, 0x21, 0x22, TEST and 0x38,0x39) */
385cb7a01acSMauro Carvalho Chehab 
386cb7a01acSMauro Carvalho Chehab 	for (i = 1; i < 33; i++)
387cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, i, table[i]);
388cb7a01acSMauro Carvalho Chehab 
389cb7a01acSMauro Carvalho Chehab 	for (i = 35; i < 40; i++)
390cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, i, table[i]);
391cb7a01acSMauro Carvalho Chehab 
392cb7a01acSMauro Carvalho Chehab 	for (i = 41; i < 56; i++)
393cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, i, table[i]);
394cb7a01acSMauro Carvalho Chehab 
395cb7a01acSMauro Carvalho Chehab 	for (i = 58; i < 64; i++)
396cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, i, table[i]);
397cb7a01acSMauro Carvalho Chehab 
398cb7a01acSMauro Carvalho Chehab 
399cb7a01acSMauro Carvalho Chehab 	if ((ks0127_read(sd, KS_STAT) & 0x80) == 0) {
400cb7a01acSMauro Carvalho Chehab 		ks->ident = V4L2_IDENT_KS0122S;
401cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "ks0122s found\n");
402cb7a01acSMauro Carvalho Chehab 		return;
403cb7a01acSMauro Carvalho Chehab 	}
404cb7a01acSMauro Carvalho Chehab 
405cb7a01acSMauro Carvalho Chehab 	switch (ks0127_read(sd, KS_CMDE) & 0x0f) {
406cb7a01acSMauro Carvalho Chehab 	case 0:
407cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "ks0127 found\n");
408cb7a01acSMauro Carvalho Chehab 		break;
409cb7a01acSMauro Carvalho Chehab 
410cb7a01acSMauro Carvalho Chehab 	case 9:
411cb7a01acSMauro Carvalho Chehab 		ks->ident = V4L2_IDENT_KS0127B;
412cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "ks0127B Revision A found\n");
413cb7a01acSMauro Carvalho Chehab 		break;
414cb7a01acSMauro Carvalho Chehab 
415cb7a01acSMauro Carvalho Chehab 	default:
416cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "unknown revision\n");
417cb7a01acSMauro Carvalho Chehab 		break;
418cb7a01acSMauro Carvalho Chehab 	}
419cb7a01acSMauro Carvalho Chehab }
420cb7a01acSMauro Carvalho Chehab 
421cb7a01acSMauro Carvalho Chehab static int ks0127_s_routing(struct v4l2_subdev *sd,
422cb7a01acSMauro Carvalho Chehab 			    u32 input, u32 output, u32 config)
423cb7a01acSMauro Carvalho Chehab {
424cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
425cb7a01acSMauro Carvalho Chehab 
426cb7a01acSMauro Carvalho Chehab 	switch (input) {
427cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_1:
428cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_2:
429cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_3:
430cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_4:
431cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_5:
432cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_COMPOSITE_6:
433cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
434cb7a01acSMauro Carvalho Chehab 			"s_routing %d: Composite\n", input);
435cb7a01acSMauro Carvalho Chehab 		/* autodetect 50/60 Hz */
436cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDA,   0xfc, 0x00);
437cb7a01acSMauro Carvalho Chehab 		/* VSE=0 */
438cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDA,   ~0x40, 0x00);
439cb7a01acSMauro Carvalho Chehab 		/* set input line */
440cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDB,   0xb0, input);
441cb7a01acSMauro Carvalho Chehab 		/* non-freerunning mode */
442cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDC,   0x70, 0x0a);
443cb7a01acSMauro Carvalho Chehab 		/* analog input */
444cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDD,   0x03, 0x00);
445cb7a01acSMauro Carvalho Chehab 		/* enable chroma demodulation */
446cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x00);
447cb7a01acSMauro Carvalho Chehab 		/* chroma trap, HYBWR=1 */
448cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_LUMA,   0x00,
449cb7a01acSMauro Carvalho Chehab 			       (reg_defaults[KS_LUMA])|0x0c);
450cb7a01acSMauro Carvalho Chehab 		/* scaler fullbw, luma comb off */
451cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIA, 0x08, 0x81);
452cb7a01acSMauro Carvalho Chehab 		/* manual chroma comb .25 .5 .25 */
453cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIC, 0x0f, 0x90);
454cb7a01acSMauro Carvalho Chehab 
455cb7a01acSMauro Carvalho Chehab 		/* chroma path delay */
456cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMB, 0x0f, 0x90);
457cb7a01acSMauro Carvalho Chehab 
458cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UGAIN, reg_defaults[KS_UGAIN]);
459cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_VGAIN, reg_defaults[KS_VGAIN]);
460cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
461cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
462cb7a01acSMauro Carvalho Chehab 		break;
463cb7a01acSMauro Carvalho Chehab 
464cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_SVIDEO_1:
465cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_SVIDEO_2:
466cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_SVIDEO_3:
467cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
468cb7a01acSMauro Carvalho Chehab 			"s_routing %d: S-Video\n", input);
469cb7a01acSMauro Carvalho Chehab 		/* autodetect 50/60 Hz */
470cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDA,   0xfc, 0x00);
471cb7a01acSMauro Carvalho Chehab 		/* VSE=0 */
472cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDA,   ~0x40, 0x00);
473cb7a01acSMauro Carvalho Chehab 		/* set input line */
474cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDB,   0xb0, input);
475cb7a01acSMauro Carvalho Chehab 		/* non-freerunning mode */
476cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDC,   0x70, 0x0a);
477cb7a01acSMauro Carvalho Chehab 		/* analog input */
478cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDD,   0x03, 0x00);
479cb7a01acSMauro Carvalho Chehab 		/* enable chroma demodulation */
480cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x00);
481cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_LUMA, 0x00,
482cb7a01acSMauro Carvalho Chehab 			       reg_defaults[KS_LUMA]);
483cb7a01acSMauro Carvalho Chehab 		/* disable luma comb */
484cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIA, 0x08,
485cb7a01acSMauro Carvalho Chehab 			       (reg_defaults[KS_VERTIA]&0xf0)|0x01);
486cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIC, 0x0f,
487cb7a01acSMauro Carvalho Chehab 			       reg_defaults[KS_VERTIC]&0xf0);
488cb7a01acSMauro Carvalho Chehab 
489cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMB, 0x0f,
490cb7a01acSMauro Carvalho Chehab 			       reg_defaults[KS_CHROMB]&0xf0);
491cb7a01acSMauro Carvalho Chehab 
492cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UGAIN, reg_defaults[KS_UGAIN]);
493cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_VGAIN, reg_defaults[KS_VGAIN]);
494cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UVOFFH, reg_defaults[KS_UVOFFH]);
495cb7a01acSMauro Carvalho Chehab 		ks0127_write(sd, KS_UVOFFL, reg_defaults[KS_UVOFFL]);
496cb7a01acSMauro Carvalho Chehab 		break;
497cb7a01acSMauro Carvalho Chehab 
498cb7a01acSMauro Carvalho Chehab 	case KS_INPUT_YUV656:
499cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "s_routing 15: YUV656\n");
500cb7a01acSMauro Carvalho Chehab 		if (ks->norm & V4L2_STD_525_60)
501cb7a01acSMauro Carvalho Chehab 			/* force 60 Hz */
502cb7a01acSMauro Carvalho Chehab 			ks0127_and_or(sd, KS_CMDA,   0xfc, 0x03);
503cb7a01acSMauro Carvalho Chehab 		else
504cb7a01acSMauro Carvalho Chehab 			/* force 50 Hz */
505cb7a01acSMauro Carvalho Chehab 			ks0127_and_or(sd, KS_CMDA,   0xfc, 0x02);
506cb7a01acSMauro Carvalho Chehab 
507cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDA,   0xff, 0x40); /* VSE=1 */
508cb7a01acSMauro Carvalho Chehab 		/* set input line and VALIGN */
509cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDB,   0xb0, (input | 0x40));
510cb7a01acSMauro Carvalho Chehab 		/* freerunning mode, */
511cb7a01acSMauro Carvalho Chehab 		/* TSTGEN = 1 TSTGFR=11 TSTGPH=0 TSTGPK=0  VMEM=1*/
512cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDC,   0x70, 0x87);
513cb7a01acSMauro Carvalho Chehab 		/* digital input, SYNDIR = 0 INPSL=01 CLKDIR=0 EAV=0 */
514cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CMDD,   0x03, 0x08);
515cb7a01acSMauro Carvalho Chehab 		/* disable chroma demodulation */
516cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CTRACK, 0xcf, 0x30);
517cb7a01acSMauro Carvalho Chehab 		/* HYPK =01 CTRAP = 0 HYBWR=0 PED=1 RGBH=1 UNIT=1 */
518cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_LUMA,   0x00, 0x71);
519cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIC, 0x0f,
520cb7a01acSMauro Carvalho Chehab 			       reg_defaults[KS_VERTIC]&0xf0);
521cb7a01acSMauro Carvalho Chehab 
522cb7a01acSMauro Carvalho Chehab 		/* scaler fullbw, luma comb off */
523cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VERTIA, 0x08, 0x81);
524cb7a01acSMauro Carvalho Chehab 
525cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMB, 0x0f,
526cb7a01acSMauro Carvalho Chehab 			       reg_defaults[KS_CHROMB]&0xf0);
527cb7a01acSMauro Carvalho Chehab 
528cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CON, 0x00, 0x00);
529cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_BRT, 0x00, 32);	/* spec: 34 */
530cb7a01acSMauro Carvalho Chehab 			/* spec: 229 (e5) */
531cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_SAT, 0x00, 0xe8);
532cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_HUE, 0x00, 0);
533cb7a01acSMauro Carvalho Chehab 
534cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_UGAIN, 0x00, 238);
535cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_VGAIN, 0x00, 0x00);
536cb7a01acSMauro Carvalho Chehab 
537cb7a01acSMauro Carvalho Chehab 		/*UOFF:0x30, VOFF:0x30, TSTCGN=1 */
538cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_UVOFFH, 0x00, 0x4f);
539cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_UVOFFL, 0x00, 0x00);
540cb7a01acSMauro Carvalho Chehab 		break;
541cb7a01acSMauro Carvalho Chehab 
542cb7a01acSMauro Carvalho Chehab 	default:
543cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
544cb7a01acSMauro Carvalho Chehab 			"s_routing: Unknown input %d\n", input);
545cb7a01acSMauro Carvalho Chehab 		break;
546cb7a01acSMauro Carvalho Chehab 	}
547cb7a01acSMauro Carvalho Chehab 
548cb7a01acSMauro Carvalho Chehab 	/* hack: CDMLPF sometimes spontaneously switches on; */
549cb7a01acSMauro Carvalho Chehab 	/* force back off */
550cb7a01acSMauro Carvalho Chehab 	ks0127_write(sd, KS_DEMOD, reg_defaults[KS_DEMOD]);
551cb7a01acSMauro Carvalho Chehab 	return 0;
552cb7a01acSMauro Carvalho Chehab }
553cb7a01acSMauro Carvalho Chehab 
554cb7a01acSMauro Carvalho Chehab static int ks0127_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
555cb7a01acSMauro Carvalho Chehab {
556cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
557cb7a01acSMauro Carvalho Chehab 
558cb7a01acSMauro Carvalho Chehab 	/* Set to automatic SECAM/Fsc mode */
559cb7a01acSMauro Carvalho Chehab 	ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x00);
560cb7a01acSMauro Carvalho Chehab 
561cb7a01acSMauro Carvalho Chehab 	ks->norm = std;
562cb7a01acSMauro Carvalho Chehab 	if (std & V4L2_STD_NTSC) {
563cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
564cb7a01acSMauro Carvalho Chehab 			"s_std: NTSC_M\n");
565cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x20);
566cb7a01acSMauro Carvalho Chehab 	} else if (std & V4L2_STD_PAL_N) {
567cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
568cb7a01acSMauro Carvalho Chehab 			"s_std: NTSC_N (fixme)\n");
569cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x40);
570cb7a01acSMauro Carvalho Chehab 	} else if (std & V4L2_STD_PAL) {
571cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
572cb7a01acSMauro Carvalho Chehab 			"s_std: PAL_N\n");
573cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x20);
574cb7a01acSMauro Carvalho Chehab 	} else if (std & V4L2_STD_PAL_M) {
575cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
576cb7a01acSMauro Carvalho Chehab 			"s_std: PAL_M (fixme)\n");
577cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMA, 0x9f, 0x40);
578cb7a01acSMauro Carvalho Chehab 	} else if (std & V4L2_STD_SECAM) {
579cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd,
580cb7a01acSMauro Carvalho Chehab 			"s_std: SECAM\n");
581cb7a01acSMauro Carvalho Chehab 
582cb7a01acSMauro Carvalho Chehab 		/* set to secam autodetection */
583cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CHROMA, 0xdf, 0x20);
584cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x00);
585cb7a01acSMauro Carvalho Chehab 		schedule_timeout_interruptible(HZ/10+1);
586cb7a01acSMauro Carvalho Chehab 
587cb7a01acSMauro Carvalho Chehab 		/* did it autodetect? */
588cb7a01acSMauro Carvalho Chehab 		if (!(ks0127_read(sd, KS_DEMOD) & 0x40))
589cb7a01acSMauro Carvalho Chehab 			/* force to secam mode */
590cb7a01acSMauro Carvalho Chehab 			ks0127_and_or(sd, KS_DEMOD, 0xf0, 0x0f);
591cb7a01acSMauro Carvalho Chehab 	} else {
592cb7a01acSMauro Carvalho Chehab 		v4l2_dbg(1, debug, sd, "s_std: Unknown norm %llx\n",
593cb7a01acSMauro Carvalho Chehab 			       (unsigned long long)std);
594cb7a01acSMauro Carvalho Chehab 	}
595cb7a01acSMauro Carvalho Chehab 	return 0;
596cb7a01acSMauro Carvalho Chehab }
597cb7a01acSMauro Carvalho Chehab 
598cb7a01acSMauro Carvalho Chehab static int ks0127_s_stream(struct v4l2_subdev *sd, int enable)
599cb7a01acSMauro Carvalho Chehab {
600cb7a01acSMauro Carvalho Chehab 	v4l2_dbg(1, debug, sd, "s_stream(%d)\n", enable);
601cb7a01acSMauro Carvalho Chehab 	if (enable) {
602cb7a01acSMauro Carvalho Chehab 		/* All output pins on */
603cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_OFMTA, 0xcf, 0x30);
604cb7a01acSMauro Carvalho Chehab 		/* Obey the OEN pin */
605cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CDEM, 0x7f, 0x00);
606cb7a01acSMauro Carvalho Chehab 	} else {
607cb7a01acSMauro Carvalho Chehab 		/* Video output pins off */
608cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_OFMTA, 0xcf, 0x00);
609cb7a01acSMauro Carvalho Chehab 		/* Ignore the OEN pin */
610cb7a01acSMauro Carvalho Chehab 		ks0127_and_or(sd, KS_CDEM, 0x7f, 0x80);
611cb7a01acSMauro Carvalho Chehab 	}
612cb7a01acSMauro Carvalho Chehab 	return 0;
613cb7a01acSMauro Carvalho Chehab }
614cb7a01acSMauro Carvalho Chehab 
615cb7a01acSMauro Carvalho Chehab static int ks0127_status(struct v4l2_subdev *sd, u32 *pstatus, v4l2_std_id *pstd)
616cb7a01acSMauro Carvalho Chehab {
617cb7a01acSMauro Carvalho Chehab 	int stat = V4L2_IN_ST_NO_SIGNAL;
618cb7a01acSMauro Carvalho Chehab 	u8 status;
619cb7a01acSMauro Carvalho Chehab 	v4l2_std_id std = V4L2_STD_ALL;
620cb7a01acSMauro Carvalho Chehab 
621cb7a01acSMauro Carvalho Chehab 	status = ks0127_read(sd, KS_STAT);
622cb7a01acSMauro Carvalho Chehab 	if (!(status & 0x20))		 /* NOVID not set */
623cb7a01acSMauro Carvalho Chehab 		stat = 0;
624cb7a01acSMauro Carvalho Chehab 	if (!(status & 0x01))		      /* CLOCK set */
625cb7a01acSMauro Carvalho Chehab 		stat |= V4L2_IN_ST_NO_COLOR;
626cb7a01acSMauro Carvalho Chehab 	if ((status & 0x08))		   /* PALDET set */
627cb7a01acSMauro Carvalho Chehab 		std = V4L2_STD_PAL;
628cb7a01acSMauro Carvalho Chehab 	else
629cb7a01acSMauro Carvalho Chehab 		std = V4L2_STD_NTSC;
630cb7a01acSMauro Carvalho Chehab 	if (pstd)
631cb7a01acSMauro Carvalho Chehab 		*pstd = std;
632cb7a01acSMauro Carvalho Chehab 	if (pstatus)
633cb7a01acSMauro Carvalho Chehab 		*pstatus = stat;
634cb7a01acSMauro Carvalho Chehab 	return 0;
635cb7a01acSMauro Carvalho Chehab }
636cb7a01acSMauro Carvalho Chehab 
637cb7a01acSMauro Carvalho Chehab static int ks0127_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
638cb7a01acSMauro Carvalho Chehab {
639cb7a01acSMauro Carvalho Chehab 	v4l2_dbg(1, debug, sd, "querystd\n");
640cb7a01acSMauro Carvalho Chehab 	return ks0127_status(sd, NULL, std);
641cb7a01acSMauro Carvalho Chehab }
642cb7a01acSMauro Carvalho Chehab 
643cb7a01acSMauro Carvalho Chehab static int ks0127_g_input_status(struct v4l2_subdev *sd, u32 *status)
644cb7a01acSMauro Carvalho Chehab {
645cb7a01acSMauro Carvalho Chehab 	v4l2_dbg(1, debug, sd, "g_input_status\n");
646cb7a01acSMauro Carvalho Chehab 	return ks0127_status(sd, status, NULL);
647cb7a01acSMauro Carvalho Chehab }
648cb7a01acSMauro Carvalho Chehab 
649cb7a01acSMauro Carvalho Chehab static int ks0127_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
650cb7a01acSMauro Carvalho Chehab {
651cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(sd);
652cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks = to_ks0127(sd);
653cb7a01acSMauro Carvalho Chehab 
654cb7a01acSMauro Carvalho Chehab 	return v4l2_chip_ident_i2c_client(client, chip, ks->ident, 0);
655cb7a01acSMauro Carvalho Chehab }
656cb7a01acSMauro Carvalho Chehab 
657cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
658cb7a01acSMauro Carvalho Chehab 
659cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_core_ops ks0127_core_ops = {
660cb7a01acSMauro Carvalho Chehab 	.g_chip_ident = ks0127_g_chip_ident,
661cb7a01acSMauro Carvalho Chehab 	.s_std = ks0127_s_std,
662cb7a01acSMauro Carvalho Chehab };
663cb7a01acSMauro Carvalho Chehab 
664cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_video_ops ks0127_video_ops = {
665cb7a01acSMauro Carvalho Chehab 	.s_routing = ks0127_s_routing,
666cb7a01acSMauro Carvalho Chehab 	.s_stream = ks0127_s_stream,
667cb7a01acSMauro Carvalho Chehab 	.querystd = ks0127_querystd,
668cb7a01acSMauro Carvalho Chehab 	.g_input_status = ks0127_g_input_status,
669cb7a01acSMauro Carvalho Chehab };
670cb7a01acSMauro Carvalho Chehab 
671cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_ops ks0127_ops = {
672cb7a01acSMauro Carvalho Chehab 	.core = &ks0127_core_ops,
673cb7a01acSMauro Carvalho Chehab 	.video = &ks0127_video_ops,
674cb7a01acSMauro Carvalho Chehab };
675cb7a01acSMauro Carvalho Chehab 
676cb7a01acSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
677cb7a01acSMauro Carvalho Chehab 
678cb7a01acSMauro Carvalho Chehab 
679cb7a01acSMauro Carvalho Chehab static int ks0127_probe(struct i2c_client *client, const struct i2c_device_id *id)
680cb7a01acSMauro Carvalho Chehab {
681cb7a01acSMauro Carvalho Chehab 	struct ks0127 *ks;
682cb7a01acSMauro Carvalho Chehab 	struct v4l2_subdev *sd;
683cb7a01acSMauro Carvalho Chehab 
684cb7a01acSMauro Carvalho Chehab 	v4l_info(client, "%s chip found @ 0x%x (%s)\n",
685cb7a01acSMauro Carvalho Chehab 		client->addr == (I2C_KS0127_ADDON >> 1) ? "addon" : "on-board",
686cb7a01acSMauro Carvalho Chehab 		client->addr << 1, client->adapter->name);
687cb7a01acSMauro Carvalho Chehab 
688*c02b211dSLaurent Pinchart 	ks = devm_kzalloc(&client->dev, sizeof(*ks), GFP_KERNEL);
689cb7a01acSMauro Carvalho Chehab 	if (ks == NULL)
690cb7a01acSMauro Carvalho Chehab 		return -ENOMEM;
691cb7a01acSMauro Carvalho Chehab 	sd = &ks->sd;
692cb7a01acSMauro Carvalho Chehab 	v4l2_i2c_subdev_init(sd, client, &ks0127_ops);
693cb7a01acSMauro Carvalho Chehab 
694cb7a01acSMauro Carvalho Chehab 	/* power up */
695cb7a01acSMauro Carvalho Chehab 	init_reg_defaults();
696cb7a01acSMauro Carvalho Chehab 	ks0127_write(sd, KS_CMDA, 0x2c);
697cb7a01acSMauro Carvalho Chehab 	mdelay(10);
698cb7a01acSMauro Carvalho Chehab 
699cb7a01acSMauro Carvalho Chehab 	/* reset the device */
700cb7a01acSMauro Carvalho Chehab 	ks0127_init(sd);
701cb7a01acSMauro Carvalho Chehab 	return 0;
702cb7a01acSMauro Carvalho Chehab }
703cb7a01acSMauro Carvalho Chehab 
704cb7a01acSMauro Carvalho Chehab static int ks0127_remove(struct i2c_client *client)
705cb7a01acSMauro Carvalho Chehab {
706cb7a01acSMauro Carvalho Chehab 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
707cb7a01acSMauro Carvalho Chehab 
708cb7a01acSMauro Carvalho Chehab 	v4l2_device_unregister_subdev(sd);
709cb7a01acSMauro Carvalho Chehab 	ks0127_write(sd, KS_OFMTA, 0x20); /* tristate */
710cb7a01acSMauro Carvalho Chehab 	ks0127_write(sd, KS_CMDA, 0x2c | 0x80); /* power down */
711cb7a01acSMauro Carvalho Chehab 	return 0;
712cb7a01acSMauro Carvalho Chehab }
713cb7a01acSMauro Carvalho Chehab 
714cb7a01acSMauro Carvalho Chehab static const struct i2c_device_id ks0127_id[] = {
715cb7a01acSMauro Carvalho Chehab 	{ "ks0127", 0 },
716cb7a01acSMauro Carvalho Chehab 	{ "ks0127b", 0 },
717cb7a01acSMauro Carvalho Chehab 	{ "ks0122s", 0 },
718cb7a01acSMauro Carvalho Chehab 	{ }
719cb7a01acSMauro Carvalho Chehab };
720cb7a01acSMauro Carvalho Chehab MODULE_DEVICE_TABLE(i2c, ks0127_id);
721cb7a01acSMauro Carvalho Chehab 
722cb7a01acSMauro Carvalho Chehab static struct i2c_driver ks0127_driver = {
723cb7a01acSMauro Carvalho Chehab 	.driver = {
724cb7a01acSMauro Carvalho Chehab 		.owner	= THIS_MODULE,
725cb7a01acSMauro Carvalho Chehab 		.name	= "ks0127",
726cb7a01acSMauro Carvalho Chehab 	},
727cb7a01acSMauro Carvalho Chehab 	.probe		= ks0127_probe,
728cb7a01acSMauro Carvalho Chehab 	.remove		= ks0127_remove,
729cb7a01acSMauro Carvalho Chehab 	.id_table	= ks0127_id,
730cb7a01acSMauro Carvalho Chehab };
731cb7a01acSMauro Carvalho Chehab 
732cb7a01acSMauro Carvalho Chehab module_i2c_driver(ks0127_driver);
733