Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/i2c-2.6
[linux-2.6] / drivers / media / video / tvp5150.c
1 /*
2  * tvp5150 - Texas Instruments TVP5150A(M) video decoder driver
3  *
4  * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@brturbo.com.br)
5  * This code is placed under the terms of the GNU General Public License
6  */
7
8 #include <linux/i2c.h>
9 #include <linux/videodev.h>
10 #include <linux/delay.h>
11 #include <linux/video_decoder.h>
12
13 #include "tvp5150_reg.h"
14
15 MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");  /* standard i2c insmod options */
16 MODULE_AUTHOR("Mauro Carvalho Chehab");
17 MODULE_LICENSE("GPL");
18
19 static unsigned short normal_i2c[] = {
20         0xb8 >> 1,
21         0xba >> 1,
22         I2C_CLIENT_END
23 };
24
25 I2C_CLIENT_INSMOD;
26
27 static int debug = 0;
28 module_param(debug, int, 0);
29 MODULE_PARM_DESC(debug, "Debug level (0-1)");
30
31 #define dprintk(num, format, args...) \
32         do { \
33                 if (debug >= num) \
34                         printk(format, ##args); \
35         } while (0)
36
37 /* supported controls */
38 static struct v4l2_queryctrl tvp5150_qctrl[] = {
39         {
40          .id = V4L2_CID_BRIGHTNESS,
41          .type = V4L2_CTRL_TYPE_INTEGER,
42          .name = "Brightness",
43          .minimum = 0,
44          .maximum = 255,
45          .step = 1,
46          .default_value = 0,
47          .flags = 0,
48          }, {
49              .id = V4L2_CID_CONTRAST,
50              .type = V4L2_CTRL_TYPE_INTEGER,
51              .name = "Contrast",
52              .minimum = 0,
53              .maximum = 255,
54              .step = 0x1,
55              .default_value = 0x10,
56              .flags = 0,
57              }, {
58                  .id = V4L2_CID_SATURATION,
59                  .type = V4L2_CTRL_TYPE_INTEGER,
60                  .name = "Saturation",
61                  .minimum = 0,
62                  .maximum = 255,
63                  .step = 0x1,
64                  .default_value = 0x10,
65                  .flags = 0,
66                  }, {
67                      .id = V4L2_CID_HUE,
68                      .type = V4L2_CTRL_TYPE_INTEGER,
69                      .name = "Hue",
70                      .minimum = -128,
71                      .maximum = 127,
72                      .step = 0x1,
73                      .default_value = 0x10,
74                      .flags = 0,
75                      }
76 };
77
78 struct tvp5150 {
79         struct i2c_client *client;
80
81         int norm;
82         int input;
83         int enable;
84         int bright;
85         int contrast;
86         int hue;
87         int sat;
88 };
89
90 static inline int tvp5150_read(struct i2c_client *c, unsigned char addr)
91 {
92         unsigned char buffer[1];
93         int rc;
94
95         buffer[0] = addr;
96         if (1 != (rc = i2c_master_send(c, buffer, 1)))
97                 dprintk(0, "i2c i/o error: rc == %d (should be 1)\n", rc);
98
99         msleep(10);
100
101         if (1 != (rc = i2c_master_recv(c, buffer, 1)))
102                 dprintk(0, "i2c i/o error: rc == %d (should be 1)\n", rc);
103
104         return (buffer[0]);
105 }
106
107 static inline void tvp5150_write(struct i2c_client *c, unsigned char addr,
108                                  unsigned char value)
109 {
110         unsigned char buffer[2];
111         int rc;
112 /*      struct tvp5150 *core = i2c_get_clientdata(c); */
113
114         buffer[0] = addr;
115         buffer[1] = value;
116         dprintk(1, "tvp5150: writing 0x%02x 0x%02x\n", buffer[0], buffer[1]);
117         if (2 != (rc = i2c_master_send(c, buffer, 2)))
118                 dprintk(0, "i2c i/o error: rc == %d (should be 2)\n", rc);
119 }
120
121 static void dump_reg(struct i2c_client *c)
122 {
123         printk("tvp5150: Video input source selection #1 = 0x%02x\n",
124                tvp5150_read(c, TVP5150_VD_IN_SRC_SEL_1));
125         printk("tvp5150: Analog channel controls = 0x%02x\n",
126                tvp5150_read(c, TVP5150_ANAL_CHL_CTL));
127         printk("tvp5150: Operation mode controls = 0x%02x\n",
128                tvp5150_read(c, TVP5150_OP_MODE_CTL));
129         printk("tvp5150: Miscellaneous controls = 0x%02x\n",
130                tvp5150_read(c, TVP5150_MISC_CTL));
131         printk("tvp5150: Autoswitch mask: TVP5150A / TVP5150AM = 0x%02x\n",
132                tvp5150_read(c, TVP5150_AUTOSW_MSK));
133         printk("tvp5150: Color killer threshold control = 0x%02x\n",
134                tvp5150_read(c, TVP5150_COLOR_KIL_THSH_CTL));
135         printk("tvp5150: Luminance processing control #1 = 0x%02x\n",
136                tvp5150_read(c, TVP5150_LUMA_PROC_CTL_1));
137         printk("tvp5150: Luminance processing control #2 = 0x%02x\n",
138                tvp5150_read(c, TVP5150_LUMA_PROC_CTL_2));
139         printk("tvp5150: Brightness control = 0x%02x\n",
140                tvp5150_read(c, TVP5150_BRIGHT_CTL));
141         printk("tvp5150: Color saturation control = 0x%02x\n",
142                tvp5150_read(c, TVP5150_SATURATION_CTL));
143         printk("tvp5150: Hue control = 0x%02x\n",
144                tvp5150_read(c, TVP5150_HUE_CTL));
145         printk("tvp5150: Contrast control = 0x%02x\n",
146                tvp5150_read(c, TVP5150_CONTRAST_CTL));
147         printk("tvp5150: Outputs and data rates select = 0x%02x\n",
148                tvp5150_read(c, TVP5150_DATA_RATE_SEL));
149         printk("tvp5150: Luminance processing control #3 = 0x%02x\n",
150                tvp5150_read(c, TVP5150_LUMA_PROC_CTL_3));
151         printk("tvp5150: Configuration shared pins = 0x%02x\n",
152                tvp5150_read(c, TVP5150_CONF_SHARED_PIN));
153         printk("tvp5150: Active video cropping start MSB = 0x%02x\n",
154                tvp5150_read(c, TVP5150_ACT_VD_CROP_ST_MSB));
155         printk("tvp5150: Active video cropping start LSB = 0x%02x\n",
156                tvp5150_read(c, TVP5150_ACT_VD_CROP_ST_LSB));
157         printk("tvp5150: Active video cropping stop MSB = 0x%02x\n",
158                tvp5150_read(c, TVP5150_ACT_VD_CROP_STP_MSB));
159         printk("tvp5150: Active video cropping stop LSB = 0x%02x\n",
160                tvp5150_read(c, TVP5150_ACT_VD_CROP_STP_LSB));
161         printk("tvp5150: Genlock/RTC = 0x%02x\n",
162                tvp5150_read(c, TVP5150_GENLOCK));
163         printk("tvp5150: Horizontal sync start = 0x%02x\n",
164                tvp5150_read(c, TVP5150_HORIZ_SYNC_START));
165         printk("tvp5150: Vertical blanking start = 0x%02x\n",
166                tvp5150_read(c, TVP5150_VERT_BLANKING_START));
167         printk("tvp5150: Vertical blanking stop = 0x%02x\n",
168                tvp5150_read(c, TVP5150_VERT_BLANKING_STOP));
169         printk("tvp5150: Chrominance processing control #1 = 0x%02x\n",
170                tvp5150_read(c, TVP5150_CHROMA_PROC_CTL_1));
171         printk("tvp5150: Chrominance processing control #2 = 0x%02x\n",
172                tvp5150_read(c, TVP5150_CHROMA_PROC_CTL_2));
173         printk("tvp5150: Interrupt reset register B = 0x%02x\n",
174                tvp5150_read(c, TVP5150_INT_RESET_REG_B));
175         printk("tvp5150: Interrupt enable register B = 0x%02x\n",
176                tvp5150_read(c, TVP5150_INT_ENABLE_REG_B));
177         printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
178                tvp5150_read(c, TVP5150_INTT_CONFIG_REG_B));
179         printk("tvp5150: Video standard = 0x%02x\n",
180                tvp5150_read(c, TVP5150_VIDEO_STD));
181         printk("tvp5150: Cb gain factor = 0x%02x\n",
182                tvp5150_read(c, TVP5150_CB_GAIN_FACT));
183         printk("tvp5150: Cr gain factor = 0x%02x\n",
184                tvp5150_read(c, TVP5150_CR_GAIN_FACTOR));
185         printk("tvp5150: Macrovision on counter = 0x%02x\n",
186                tvp5150_read(c, TVP5150_MACROVISION_ON_CTR));
187         printk("tvp5150: Macrovision off counter = 0x%02x\n",
188                tvp5150_read(c, TVP5150_MACROVISION_OFF_CTR));
189         printk("tvp5150: revision select (TVP5150AM1 only) = 0x%02x\n",
190                tvp5150_read(c, TVP5150_REV_SELECT));
191         printk("tvp5150: MSB of device ID = 0x%02x\n",
192                tvp5150_read(c, TVP5150_MSB_DEV_ID));
193         printk("tvp5150: LSB of device ID = 0x%02x\n",
194                tvp5150_read(c, TVP5150_LSB_DEV_ID));
195         printk("tvp5150: ROM major version = 0x%02x\n",
196                tvp5150_read(c, TVP5150_ROM_MAJOR_VER));
197         printk("tvp5150: ROM minor version = 0x%02x\n",
198                tvp5150_read(c, TVP5150_ROM_MINOR_VER));
199         printk("tvp5150: Vertical line count MSB = 0x%02x\n",
200                tvp5150_read(c, TVP5150_VERT_LN_COUNT_MSB));
201         printk("tvp5150: Vertical line count LSB = 0x%02x\n",
202                tvp5150_read(c, TVP5150_VERT_LN_COUNT_LSB));
203         printk("tvp5150: Interrupt status register B = 0x%02x\n",
204                tvp5150_read(c, TVP5150_INT_STATUS_REG_B));
205         printk("tvp5150: Interrupt active register B = 0x%02x\n",
206                tvp5150_read(c, TVP5150_INT_ACTIVE_REG_B));
207         printk("tvp5150: Status register #1 = 0x%02x\n",
208                tvp5150_read(c, TVP5150_STATUS_REG_1));
209         printk("tvp5150: Status register #2 = 0x%02x\n",
210                tvp5150_read(c, TVP5150_STATUS_REG_2));
211         printk("tvp5150: Status register #3 = 0x%02x\n",
212                tvp5150_read(c, TVP5150_STATUS_REG_3));
213         printk("tvp5150: Status register #4 = 0x%02x\n",
214                tvp5150_read(c, TVP5150_STATUS_REG_4));
215         printk("tvp5150: Status register #5 = 0x%02x\n",
216                tvp5150_read(c, TVP5150_STATUS_REG_5));
217         printk("tvp5150: Closed caption data registers = 0x%02x\n",
218                tvp5150_read(c, TVP5150_CC_DATA_REG1));
219         printk("tvp5150: Closed caption data registers = 0x%02x\n",
220                tvp5150_read(c, TVP5150_CC_DATA_REG2));
221         printk("tvp5150: Closed caption data registers = 0x%02x\n",
222                tvp5150_read(c, TVP5150_CC_DATA_REG3));
223         printk("tvp5150: Closed caption data registers = 0x%02x\n",
224                tvp5150_read(c, TVP5150_CC_DATA_REG4));
225         printk("tvp5150: WSS data registers = 0x%02x\n",
226                tvp5150_read(c, TVP5150_WSS_DATA_REG1));
227         printk("tvp5150: WSS data registers = 0x%02x\n",
228                tvp5150_read(c, TVP5150_WSS_DATA_REG2));
229         printk("tvp5150: WSS data registers = 0x%02x\n",
230                tvp5150_read(c, TVP5150_WSS_DATA_REG3));
231         printk("tvp5150: WSS data registers = 0x%02x\n",
232                tvp5150_read(c, TVP5150_WSS_DATA_REG4));
233         printk("tvp5150: WSS data registers = 0x%02x\n",
234                tvp5150_read(c, TVP5150_WSS_DATA_REG5));
235         printk("tvp5150: WSS data registers = 0x%02x\n",
236                tvp5150_read(c, TVP5150_WSS_DATA_REG6));
237         printk("tvp5150: VPS data registers = 0x%02x\n",
238                tvp5150_read(c, TVP5150_VPS_DATA_REG1));
239         printk("tvp5150: VPS data registers = 0x%02x\n",
240                tvp5150_read(c, TVP5150_VPS_DATA_REG2));
241         printk("tvp5150: VPS data registers = 0x%02x\n",
242                tvp5150_read(c, TVP5150_VPS_DATA_REG3));
243         printk("tvp5150: VPS data registers = 0x%02x\n",
244                tvp5150_read(c, TVP5150_VPS_DATA_REG4));
245         printk("tvp5150: VPS data registers = 0x%02x\n",
246                tvp5150_read(c, TVP5150_VPS_DATA_REG5));
247         printk("tvp5150: VPS data registers = 0x%02x\n",
248                tvp5150_read(c, TVP5150_VPS_DATA_REG6));
249         printk("tvp5150: VPS data registers = 0x%02x\n",
250                tvp5150_read(c, TVP5150_VPS_DATA_REG7));
251         printk("tvp5150: VPS data registers = 0x%02x\n",
252                tvp5150_read(c, TVP5150_VPS_DATA_REG8));
253         printk("tvp5150: VPS data registers = 0x%02x\n",
254                tvp5150_read(c, TVP5150_VPS_DATA_REG9));
255         printk("tvp5150: VPS data registers = 0x%02x\n",
256                tvp5150_read(c, TVP5150_VPS_DATA_REG10));
257         printk("tvp5150: VPS data registers = 0x%02x\n",
258                tvp5150_read(c, TVP5150_VPS_DATA_REG11));
259         printk("tvp5150: VPS data registers = 0x%02x\n",
260                tvp5150_read(c, TVP5150_VPS_DATA_REG12));
261         printk("tvp5150: VPS data registers = 0x%02x\n",
262                tvp5150_read(c, TVP5150_VPS_DATA_REG13));
263         printk("tvp5150: VITC data registers = 0x%02x\n",
264                tvp5150_read(c, TVP5150_VITC_DATA_REG1));
265         printk("tvp5150: VITC data registers = 0x%02x\n",
266                tvp5150_read(c, TVP5150_VITC_DATA_REG2));
267         printk("tvp5150: VITC data registers = 0x%02x\n",
268                tvp5150_read(c, TVP5150_VITC_DATA_REG3));
269         printk("tvp5150: VITC data registers = 0x%02x\n",
270                tvp5150_read(c, TVP5150_VITC_DATA_REG4));
271         printk("tvp5150: VITC data registers = 0x%02x\n",
272                tvp5150_read(c, TVP5150_VITC_DATA_REG5));
273         printk("tvp5150: VITC data registers = 0x%02x\n",
274                tvp5150_read(c, TVP5150_VITC_DATA_REG6));
275         printk("tvp5150: VITC data registers = 0x%02x\n",
276                tvp5150_read(c, TVP5150_VITC_DATA_REG7));
277         printk("tvp5150: VITC data registers = 0x%02x\n",
278                tvp5150_read(c, TVP5150_VITC_DATA_REG8));
279         printk("tvp5150: VITC data registers = 0x%02x\n",
280                tvp5150_read(c, TVP5150_VITC_DATA_REG9));
281         printk("tvp5150: VBI FIFO read data = 0x%02x\n",
282                tvp5150_read(c, TVP5150_VBI_FIFO_READ_DATA));
283         printk("tvp5150: Teletext filter 1 = 0x%02x\n",
284                tvp5150_read(c, TVP5150_TELETEXT_FIL_1_1));
285         printk("tvp5150: Teletext filter 1 = 0x%02x\n",
286                tvp5150_read(c, TVP5150_TELETEXT_FIL_1_2));
287         printk("tvp5150: Teletext filter 1 = 0x%02x\n",
288                tvp5150_read(c, TVP5150_TELETEXT_FIL_1_3));
289         printk("tvp5150: Teletext filter 1 = 0x%02x\n",
290                tvp5150_read(c, TVP5150_TELETEXT_FIL_1_4));
291         printk("tvp5150: Teletext filter 1 = 0x%02x\n",
292                tvp5150_read(c, TVP5150_TELETEXT_FIL_1_5));
293         printk("tvp5150: Teletext filter 2 = 0x%02x\n",
294                tvp5150_read(c, TVP5150_TELETEXT_FIL_2_1));
295         printk("tvp5150: Teletext filter 2 = 0x%02x\n",
296                tvp5150_read(c, TVP5150_TELETEXT_FIL_2_2));
297         printk("tvp5150: Teletext filter 2 = 0x%02x\n",
298                tvp5150_read(c, TVP5150_TELETEXT_FIL_2_3));
299         printk("tvp5150: Teletext filter 2 = 0x%02x\n",
300                tvp5150_read(c, TVP5150_TELETEXT_FIL_2_4));
301         printk("tvp5150: Teletext filter 2 = 0x%02x\n",
302                tvp5150_read(c, TVP5150_TELETEXT_FIL_2_5));
303         printk("tvp5150: Teletext filter enable = 0x%02x\n",
304                tvp5150_read(c, TVP5150_TELETEXT_FIL_ENA));
305         printk("tvp5150: Interrupt status register A = 0x%02x\n",
306                tvp5150_read(c, TVP5150_INT_STATUS_REG_A));
307         printk("tvp5150: Interrupt enable register A = 0x%02x\n",
308                tvp5150_read(c, TVP5150_INT_ENABLE_REG_A));
309         printk("tvp5150: Interrupt configuration = 0x%02x\n",
310                tvp5150_read(c, TVP5150_INT_CONF));
311         printk("tvp5150: VDP configuration RAM data = 0x%02x\n",
312                tvp5150_read(c, TVP5150_VDP_CONF_RAM_DATA));
313         printk("tvp5150: Configuration RAM address low byte = 0x%02x\n",
314                tvp5150_read(c, TVP5150_CONF_RAM_ADDR_LOW));
315         printk("tvp5150: Configuration RAM address high byte = 0x%02x\n",
316                tvp5150_read(c, TVP5150_CONF_RAM_ADDR_HIGH));
317         printk("tvp5150: VDP status register = 0x%02x\n",
318                tvp5150_read(c, TVP5150_VDP_STATUS_REG));
319         printk("tvp5150: FIFO word count = 0x%02x\n",
320                tvp5150_read(c, TVP5150_FIFO_WORD_COUNT));
321         printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
322                tvp5150_read(c, TVP5150_FIFO_INT_THRESHOLD));
323         printk("tvp5150: FIFO reset = 0x%02x\n",
324                tvp5150_read(c, TVP5150_FIFO_RESET));
325         printk("tvp5150: Line number interrupt = 0x%02x\n",
326                tvp5150_read(c, TVP5150_LINE_NUMBER_INT));
327         printk("tvp5150: Pixel alignment register low byte = 0x%02x\n",
328                tvp5150_read(c, TVP5150_PIX_ALIGN_REG_LOW));
329         printk("tvp5150: Pixel alignment register high byte = 0x%02x\n",
330                tvp5150_read(c, TVP5150_PIX_ALIGN_REG_HIGH));
331         printk("tvp5150: FIFO output control = 0x%02x\n",
332                tvp5150_read(c, TVP5150_FIFO_OUT_CTRL));
333         printk("tvp5150: Full field enable 1 = 0x%02x\n",
334                tvp5150_read(c, TVP5150_FULL_FIELD_ENA_1));
335         printk("tvp5150: Full field enable 2 = 0x%02x\n",
336                tvp5150_read(c, TVP5150_FULL_FIELD_ENA_2));
337         printk("tvp5150: Line mode registers = 0x%02x\n",
338                tvp5150_read(c, TVP5150_LINE_MODE_REG_1));
339         printk("tvp5150: Line mode registers = 0x%02x\n",
340                tvp5150_read(c, TVP5150_LINE_MODE_REG_2));
341         printk("tvp5150: Line mode registers = 0x%02x\n",
342                tvp5150_read(c, TVP5150_LINE_MODE_REG_3));
343         printk("tvp5150: Line mode registers = 0x%02x\n",
344                tvp5150_read(c, TVP5150_LINE_MODE_REG_4));
345         printk("tvp5150: Line mode registers = 0x%02x\n",
346                tvp5150_read(c, TVP5150_LINE_MODE_REG_5));
347         printk("tvp5150: Line mode registers = 0x%02x\n",
348                tvp5150_read(c, TVP5150_LINE_MODE_REG_6));
349         printk("tvp5150: Line mode registers = 0x%02x\n",
350                tvp5150_read(c, TVP5150_LINE_MODE_REG_7));
351         printk("tvp5150: Line mode registers = 0x%02x\n",
352                tvp5150_read(c, TVP5150_LINE_MODE_REG_8));
353         printk("tvp5150: Line mode registers = 0x%02x\n",
354                tvp5150_read(c, TVP5150_LINE_MODE_REG_9));
355         printk("tvp5150: Line mode registers = 0x%02x\n",
356                tvp5150_read(c, TVP5150_LINE_MODE_REG_10));
357         printk("tvp5150: Line mode registers = 0x%02x\n",
358                tvp5150_read(c, TVP5150_LINE_MODE_REG_11));
359         printk("tvp5150: Line mode registers = 0x%02x\n",
360                tvp5150_read(c, TVP5150_LINE_MODE_REG_12));
361         printk("tvp5150: Line mode registers = 0x%02x\n",
362                tvp5150_read(c, TVP5150_LINE_MODE_REG_13));
363         printk("tvp5150: Line mode registers = 0x%02x\n",
364                tvp5150_read(c, TVP5150_LINE_MODE_REG_14));
365         printk("tvp5150: Line mode registers = 0x%02x\n",
366                tvp5150_read(c, TVP5150_LINE_MODE_REG_15));
367         printk("tvp5150: Line mode registers = 0x%02x\n",
368                tvp5150_read(c, TVP5150_LINE_MODE_REG_16));
369         printk("tvp5150: Line mode registers = 0x%02x\n",
370                tvp5150_read(c, TVP5150_LINE_MODE_REG_17));
371         printk("tvp5150: Line mode registers = 0x%02x\n",
372                tvp5150_read(c, TVP5150_LINE_MODE_REG_18));
373         printk("tvp5150: Line mode registers = 0x%02x\n",
374                tvp5150_read(c, TVP5150_LINE_MODE_REG_19));
375         printk("tvp5150: Line mode registers = 0x%02x\n",
376                tvp5150_read(c, TVP5150_LINE_MODE_REG_20));
377         printk("tvp5150: Line mode registers = 0x%02x\n",
378                tvp5150_read(c, TVP5150_LINE_MODE_REG_21));
379         printk("tvp5150: Line mode registers = 0x%02x\n",
380                tvp5150_read(c, TVP5150_LINE_MODE_REG_22));
381         printk("tvp5150: Line mode registers = 0x%02x\n",
382                tvp5150_read(c, TVP5150_LINE_MODE_REG_23));
383         printk("tvp5150: Line mode registers = 0x%02x\n",
384                tvp5150_read(c, TVP5150_LINE_MODE_REG_24));
385         printk("tvp5150: Line mode registers = 0x%02x\n",
386                tvp5150_read(c, TVP5150_LINE_MODE_REG_25));
387         printk("tvp5150: Line mode registers = 0x%02x\n",
388                tvp5150_read(c, TVP5150_LINE_MODE_REG_27));
389         printk("tvp5150: Line mode registers = 0x%02x\n",
390                tvp5150_read(c, TVP5150_LINE_MODE_REG_28));
391         printk("tvp5150: Line mode registers = 0x%02x\n",
392                tvp5150_read(c, TVP5150_LINE_MODE_REG_29));
393         printk("tvp5150: Line mode registers = 0x%02x\n",
394                tvp5150_read(c, TVP5150_LINE_MODE_REG_30));
395         printk("tvp5150: Line mode registers = 0x%02x\n",
396                tvp5150_read(c, TVP5150_LINE_MODE_REG_31));
397         printk("tvp5150: Line mode registers = 0x%02x\n",
398                tvp5150_read(c, TVP5150_LINE_MODE_REG_32));
399         printk("tvp5150: Line mode registers = 0x%02x\n",
400                tvp5150_read(c, TVP5150_LINE_MODE_REG_33));
401         printk("tvp5150: Line mode registers = 0x%02x\n",
402                tvp5150_read(c, TVP5150_LINE_MODE_REG_34));
403         printk("tvp5150: Line mode registers = 0x%02x\n",
404                tvp5150_read(c, TVP5150_LINE_MODE_REG_35));
405         printk("tvp5150: Line mode registers = 0x%02x\n",
406                tvp5150_read(c, TVP5150_LINE_MODE_REG_36));
407         printk("tvp5150: Line mode registers = 0x%02x\n",
408                tvp5150_read(c, TVP5150_LINE_MODE_REG_37));
409         printk("tvp5150: Line mode registers = 0x%02x\n",
410                tvp5150_read(c, TVP5150_LINE_MODE_REG_38));
411         printk("tvp5150: Line mode registers = 0x%02x\n",
412                tvp5150_read(c, TVP5150_LINE_MODE_REG_39));
413         printk("tvp5150: Line mode registers = 0x%02x\n",
414                tvp5150_read(c, TVP5150_LINE_MODE_REG_40));
415         printk("tvp5150: Line mode registers = 0x%02x\n",
416                tvp5150_read(c, TVP5150_LINE_MODE_REG_41));
417         printk("tvp5150: Line mode registers = 0x%02x\n",
418                tvp5150_read(c, TVP5150_LINE_MODE_REG_42));
419         printk("tvp5150: Line mode registers = 0x%02x\n",
420                tvp5150_read(c, TVP5150_LINE_MODE_REG_43));
421         printk("tvp5150: Line mode registers = 0x%02x\n",
422                tvp5150_read(c, TVP5150_LINE_MODE_REG_44));
423         printk("tvp5150: Full field mode register = 0x%02x\n",
424                tvp5150_read(c, TVP5150_FULL_FIELD_MODE_REG));
425 }
426
427 /****************************************************************************
428                         Basic functions
429  ****************************************************************************/
430 enum tvp5150_input {
431         TVP5150_ANALOG_CH0 = 0,
432         TVP5150_SVIDEO = 1,
433         TVP5150_ANALOG_CH1 = 2,
434         TVP5150_BLACK_SCREEN = 8
435 };
436
437 static inline void tvp5150_selmux(struct i2c_client *c,
438                                   enum tvp5150_input input)
439 {
440         struct tvp5150 *decoder = i2c_get_clientdata(c);
441
442         if (!decoder->enable)
443                 input |= TVP5150_BLACK_SCREEN;
444
445         tvp5150_write(c, TVP5150_VD_IN_SRC_SEL_1, input);
446 };
447
448 static inline void tvp5150_reset(struct i2c_client *c)
449 {
450         struct tvp5150 *decoder = i2c_get_clientdata(c);
451
452         tvp5150_write(c, TVP5150_CONF_SHARED_PIN, 2);
453
454         /* Automatic offset and AGC enabled */
455         tvp5150_write(c, TVP5150_ANAL_CHL_CTL, 0x15);
456
457         /* Normal Operation */
458 //      tvp5150_write(c, TVP5150_OP_MODE_CTL, 0x00);
459
460         /* Activate YCrCb output 0x9 or 0xd ? */
461         tvp5150_write(c, TVP5150_MISC_CTL, 0x6f);
462
463         /* Activates video std autodetection for all standards */
464         tvp5150_write(c, TVP5150_AUTOSW_MSK, 0x0);
465
466         /* Default format: 0x47, 4:2:2: 0x40 */
467         tvp5150_write(c, TVP5150_DATA_RATE_SEL, 0x47);
468
469         tvp5150_selmux(c, decoder->input);
470
471         tvp5150_write(c, TVP5150_CHROMA_PROC_CTL_1, 0x0c);
472         tvp5150_write(c, TVP5150_CHROMA_PROC_CTL_2, 0x54);
473
474         tvp5150_write(c, 0x27, 0x20);   /* ?????????? */
475
476         tvp5150_write(c, TVP5150_VIDEO_STD, 0x0);       /* Auto switch */
477
478         tvp5150_write(c, TVP5150_BRIGHT_CTL, decoder->bright >> 8);
479         tvp5150_write(c, TVP5150_CONTRAST_CTL, decoder->contrast >> 8);
480         tvp5150_write(c, TVP5150_SATURATION_CTL, decoder->contrast >> 8);
481         tvp5150_write(c, TVP5150_HUE_CTL, (decoder->hue - 32768) >> 8);
482 };
483
484 static int tvp5150_get_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
485 {
486 /*      struct tvp5150 *decoder = i2c_get_clientdata(c); */
487
488         switch (ctrl->id) {
489         case V4L2_CID_BRIGHTNESS:
490                 ctrl->value = tvp5150_read(c, TVP5150_BRIGHT_CTL);
491                 return 0;
492         case V4L2_CID_CONTRAST:
493                 ctrl->value = tvp5150_read(c, TVP5150_CONTRAST_CTL);
494                 return 0;
495         case V4L2_CID_SATURATION:
496                 ctrl->value = tvp5150_read(c, TVP5150_SATURATION_CTL);
497                 return 0;
498         case V4L2_CID_HUE:
499                 ctrl->value = tvp5150_read(c, TVP5150_HUE_CTL);
500                 return 0;
501         default:
502                 return -EINVAL;
503         }
504 }
505
506 static int tvp5150_set_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
507 {
508 /*      struct tvp5150 *decoder = i2c_get_clientdata(c); */
509
510         switch (ctrl->id) {
511         case V4L2_CID_BRIGHTNESS:
512                 tvp5150_write(c, TVP5150_BRIGHT_CTL, ctrl->value);
513                 return 0;
514         case V4L2_CID_CONTRAST:
515                 tvp5150_write(c, TVP5150_CONTRAST_CTL, ctrl->value);
516                 return 0;
517         case V4L2_CID_SATURATION:
518                 tvp5150_write(c, TVP5150_SATURATION_CTL, ctrl->value);
519                 return 0;
520         case V4L2_CID_HUE:
521                 tvp5150_write(c, TVP5150_HUE_CTL, ctrl->value);
522                 return 0;
523         default:
524                 return -EINVAL;
525         }
526 }
527
528 /****************************************************************************
529                         I2C Command
530  ****************************************************************************/
531 static int tvp5150_command(struct i2c_client *client,
532                            unsigned int cmd, void *arg)
533 {
534         struct tvp5150 *decoder = i2c_get_clientdata(client);
535
536         switch (cmd) {
537
538         case 0:
539         case DECODER_INIT:
540                 tvp5150_reset(client);
541                 break;
542
543         case DECODER_DUMP:
544                 dump_reg(client);
545                 break;
546
547         case DECODER_GET_CAPABILITIES:
548                 {
549                         struct video_decoder_capability *cap = arg;
550
551                         cap->flags = VIDEO_DECODER_PAL |
552                             VIDEO_DECODER_NTSC |
553                             VIDEO_DECODER_SECAM |
554                             VIDEO_DECODER_AUTO | VIDEO_DECODER_CCIR;
555                         cap->inputs = 3;
556                         cap->outputs = 1;
557                         break;
558                 }
559         case DECODER_GET_STATUS:
560                 {
561                         break;
562                 }
563
564         case DECODER_SET_GPIO:
565                 break;
566
567         case DECODER_SET_VBI_BYPASS:
568                 break;
569
570         case DECODER_SET_NORM:
571                 {
572                         int *iarg = arg;
573
574                         switch (*iarg) {
575
576                         case VIDEO_MODE_NTSC:
577                                 break;
578
579                         case VIDEO_MODE_PAL:
580                                 break;
581
582                         case VIDEO_MODE_SECAM:
583                                 break;
584
585                         case VIDEO_MODE_AUTO:
586                                 break;
587
588                         default:
589                                 return -EINVAL;
590
591                         }
592                         decoder->norm = *iarg;
593                         break;
594                 }
595         case DECODER_SET_INPUT:
596                 {
597                         int *iarg = arg;
598                         if (*iarg < 0 || *iarg > 3) {
599                                 return -EINVAL;
600                         }
601
602                         decoder->input = *iarg;
603                         tvp5150_selmux(client, decoder->input);
604
605                         break;
606                 }
607         case DECODER_SET_OUTPUT:
608                 {
609                         int *iarg = arg;
610
611                         /* not much choice of outputs */
612                         if (*iarg != 0) {
613                                 return -EINVAL;
614                         }
615                         break;
616                 }
617         case DECODER_ENABLE_OUTPUT:
618                 {
619                         int *iarg = arg;
620
621                         decoder->enable = (*iarg != 0);
622
623                         tvp5150_selmux(client, decoder->input);
624
625                         break;
626                 }
627         case VIDIOC_QUERYCTRL:
628                 {
629                         struct v4l2_queryctrl *qc = arg;
630                         u8 i, n;
631
632                         dprintk(1, KERN_DEBUG "VIDIOC_QUERYCTRL");
633
634                         n = sizeof(tvp5150_qctrl) / sizeof(tvp5150_qctrl[0]);
635                         for (i = 0; i < n; i++)
636                                 if (qc->id && qc->id == tvp5150_qctrl[i].id) {
637                                         memcpy(qc, &(tvp5150_qctrl[i]),
638                                                sizeof(*qc));
639                                         return 0;
640                                 }
641
642                         return -EINVAL;
643                 }
644         case VIDIOC_G_CTRL:
645                 {
646                         struct v4l2_control *ctrl = arg;
647                         dprintk(1, KERN_DEBUG "VIDIOC_G_CTRL");
648
649                         return tvp5150_get_ctrl(client, ctrl);
650                 }
651         case VIDIOC_S_CTRL_OLD: /* ??? */
652         case VIDIOC_S_CTRL:
653                 {
654                         struct v4l2_control *ctrl = arg;
655                         u8 i, n;
656                         dprintk(1, KERN_DEBUG "VIDIOC_S_CTRL");
657                         n = sizeof(tvp5150_qctrl) / sizeof(tvp5150_qctrl[0]);
658                         for (i = 0; i < n; i++)
659                                 if (ctrl->id == tvp5150_qctrl[i].id) {
660                                         if (ctrl->value <
661                                             tvp5150_qctrl[i].minimum
662                                             || ctrl->value >
663                                             tvp5150_qctrl[i].maximum)
664                                                 return -ERANGE;
665                                         dprintk(1,
666                                                 KERN_DEBUG
667                                                 "VIDIOC_S_CTRL: id=%d, value=%d",
668                                                 ctrl->id, ctrl->value);
669                                         return tvp5150_set_ctrl(client, ctrl);
670                                 }
671                         return -EINVAL;
672                 }
673
674         case DECODER_SET_PICTURE:
675                 {
676                         struct video_picture *pic = arg;
677                         if (decoder->bright != pic->brightness) {
678                                 /* We want 0 to 255 we get 0-65535 */
679                                 decoder->bright = pic->brightness;
680                                 tvp5150_write(client, TVP5150_BRIGHT_CTL,
681                                               decoder->bright >> 8);
682                         }
683                         if (decoder->contrast != pic->contrast) {
684                                 /* We want 0 to 255 we get 0-65535 */
685                                 decoder->contrast = pic->contrast;
686                                 tvp5150_write(client, TVP5150_CONTRAST_CTL,
687                                               decoder->contrast >> 8);
688                         }
689                         if (decoder->sat != pic->colour) {
690                                 /* We want 0 to 255 we get 0-65535 */
691                                 decoder->sat = pic->colour;
692                                 tvp5150_write(client, TVP5150_SATURATION_CTL,
693                                               decoder->contrast >> 8);
694                         }
695                         if (decoder->hue != pic->hue) {
696                                 /* We want -128 to 127 we get 0-65535 */
697                                 decoder->hue = pic->hue;
698                                 tvp5150_write(client, TVP5150_HUE_CTL,
699                                               (decoder->hue - 32768) >> 8);
700                         }
701                         break;
702                 }
703         default:
704                 return -EINVAL;
705         }
706
707         return 0;
708 }
709
710 /****************************************************************************
711                         I2C Client & Driver
712  ****************************************************************************/
713 static struct i2c_driver driver;
714
715 static struct i2c_client client_template = {
716         .name = "(unset)",
717         .driver = &driver,
718 };
719
720 static int tvp5150_detect_client(struct i2c_adapter *adapter,
721                                  int address, int kind)
722 {
723         struct i2c_client *client;
724         struct tvp5150 *core;
725         int rv;
726
727         dprintk(1,
728                 KERN_INFO
729                 "tvp5150.c: detecting tvp5150 client on address 0x%x\n",
730                 address << 1);
731
732         client_template.adapter = adapter;
733         client_template.addr = address;
734
735         /* Check if the adapter supports the needed features */
736         if (!i2c_check_functionality
737             (adapter,
738              I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
739                 return 0;
740
741         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
742         if (client == 0)
743                 return -ENOMEM;
744         memcpy(client, &client_template, sizeof(struct i2c_client));
745
746         core = kmalloc(sizeof(struct tvp5150), GFP_KERNEL);
747         if (core == 0) {
748                 kfree(client);
749                 return -ENOMEM;
750         }
751         memset(core, 0, sizeof(struct tvp5150));
752         i2c_set_clientdata(client, core);
753
754         rv = i2c_attach_client(client);
755
756         core->norm = VIDEO_MODE_AUTO;
757         core->input = 2;
758         core->enable = 1;
759         core->bright = 32768;
760         core->contrast = 32768;
761         core->hue = 32768;
762         core->sat = 32768;
763
764         if (rv) {
765                 kfree(client);
766                 kfree(core);
767                 return rv;
768         }
769
770         if (debug > 1)
771                 dump_reg(client);
772         return 0;
773 }
774
775 static int tvp5150_attach_adapter(struct i2c_adapter *adapter)
776 {
777         dprintk(1,
778                 KERN_INFO
779                 "tvp5150.c: starting probe for adapter %s (0x%x)\n",
780                 adapter->name, adapter->id);
781         return i2c_probe(adapter, &addr_data, &tvp5150_detect_client);
782 }
783
784 static int tvp5150_detach_client(struct i2c_client *client)
785 {
786         struct tvp5150 *decoder = i2c_get_clientdata(client);
787         int err;
788
789         err = i2c_detach_client(client);
790         if (err) {
791                 return err;
792         }
793
794         kfree(decoder);
795         kfree(client);
796
797         return 0;
798 }
799
800 /* ----------------------------------------------------------------------- */
801
802 static struct i2c_driver driver = {
803         .driver = {
804                 .name = "tvp5150",
805         },
806
807         /* FIXME */
808         .id = I2C_DRIVERID_SAA7110,
809
810         .attach_adapter = tvp5150_attach_adapter,
811         .detach_client = tvp5150_detach_client,
812
813         .command = tvp5150_command,
814 };
815
816 static int __init tvp5150_init(void)
817 {
818         return i2c_add_driver(&driver);
819 }
820
821 static void __exit tvp5150_exit(void)
822 {
823         i2c_del_driver(&driver);
824 }
825
826 module_init(tvp5150_init);
827 module_exit(tvp5150_exit);