GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / media / i2c / tvp5150.c
1 /*
2  * tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver
3  *
4  * Copyright (c) 2005,2006 Mauro Carvalho Chehab (mchehab@infradead.org)
5  * This code is placed under the terms of the GNU General Public License v2
6  */
7
8 #include <dt-bindings/media/tvp5150.h>
9 #include <linux/i2c.h>
10 #include <linux/slab.h>
11 #include <linux/videodev2.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <media/v4l2-async.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-of.h>
19 #include <media/v4l2-mc.h>
20
21 #include "tvp5150_reg.h"
22
23 #define TVP5150_H_MAX           720U
24 #define TVP5150_V_MAX_525_60    480U
25 #define TVP5150_V_MAX_OTHERS    576U
26 #define TVP5150_MAX_CROP_LEFT   511
27 #define TVP5150_MAX_CROP_TOP    127
28 #define TVP5150_CROP_SHIFT      2
29
30 MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
31 MODULE_AUTHOR("Mauro Carvalho Chehab");
32 MODULE_LICENSE("GPL");
33
34
35 static int debug;
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Debug level (0-2)");
38
39 struct tvp5150 {
40         struct v4l2_subdev sd;
41 #ifdef CONFIG_MEDIA_CONTROLLER
42         struct media_pad pads[DEMOD_NUM_PADS];
43         struct media_entity input_ent[TVP5150_INPUT_NUM];
44         struct media_pad input_pad[TVP5150_INPUT_NUM];
45 #endif
46         struct v4l2_ctrl_handler hdl;
47         struct v4l2_rect rect;
48
49         v4l2_std_id norm;       /* Current set standard */
50         u32 input;
51         u32 output;
52         int enable;
53
54         u16 dev_id;
55         u16 rom_ver;
56
57         enum v4l2_mbus_type mbus_type;
58 };
59
60 static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
61 {
62         return container_of(sd, struct tvp5150, sd);
63 }
64
65 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
66 {
67         return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
68 }
69
70 static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
71 {
72         struct i2c_client *c = v4l2_get_subdevdata(sd);
73         int rc;
74
75         rc = i2c_smbus_read_byte_data(c, addr);
76         if (rc < 0) {
77                 v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
78                 return rc;
79         }
80
81         v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, rc);
82
83         return rc;
84 }
85
86 static int tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
87                                  unsigned char value)
88 {
89         struct i2c_client *c = v4l2_get_subdevdata(sd);
90         int rc;
91
92         v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", addr, value);
93         rc = i2c_smbus_write_byte_data(c, addr, value);
94         if (rc < 0)
95                 v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
96
97         return rc;
98 }
99
100 static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
101                                 const u8 end, int max_line)
102 {
103         int i = 0;
104
105         while (init != (u8)(end + 1)) {
106                 if ((i % max_line) == 0) {
107                         if (i > 0)
108                                 printk("\n");
109                         printk("tvp5150: %s reg 0x%02x = ", s, init);
110                 }
111                 printk("%02x ", tvp5150_read(sd, init));
112
113                 init++;
114                 i++;
115         }
116         printk("\n");
117 }
118
119 static int tvp5150_log_status(struct v4l2_subdev *sd)
120 {
121         printk("tvp5150: Video input source selection #1 = 0x%02x\n",
122                         tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
123         printk("tvp5150: Analog channel controls = 0x%02x\n",
124                         tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
125         printk("tvp5150: Operation mode controls = 0x%02x\n",
126                         tvp5150_read(sd, TVP5150_OP_MODE_CTL));
127         printk("tvp5150: Miscellaneous controls = 0x%02x\n",
128                         tvp5150_read(sd, TVP5150_MISC_CTL));
129         printk("tvp5150: Autoswitch mask= 0x%02x\n",
130                         tvp5150_read(sd, TVP5150_AUTOSW_MSK));
131         printk("tvp5150: Color killer threshold control = 0x%02x\n",
132                         tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
133         printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
134                         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
135                         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
136                         tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
137         printk("tvp5150: Brightness control = 0x%02x\n",
138                         tvp5150_read(sd, TVP5150_BRIGHT_CTL));
139         printk("tvp5150: Color saturation control = 0x%02x\n",
140                         tvp5150_read(sd, TVP5150_SATURATION_CTL));
141         printk("tvp5150: Hue control = 0x%02x\n",
142                         tvp5150_read(sd, TVP5150_HUE_CTL));
143         printk("tvp5150: Contrast control = 0x%02x\n",
144                         tvp5150_read(sd, TVP5150_CONTRAST_CTL));
145         printk("tvp5150: Outputs and data rates select = 0x%02x\n",
146                         tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
147         printk("tvp5150: Configuration shared pins = 0x%02x\n",
148                         tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
149         printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
150                         tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
151                         tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
152         printk("tvp5150: Active video cropping stop  = 0x%02x%02x\n",
153                         tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
154                         tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
155         printk("tvp5150: Genlock/RTC = 0x%02x\n",
156                         tvp5150_read(sd, TVP5150_GENLOCK));
157         printk("tvp5150: Horizontal sync start = 0x%02x\n",
158                         tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
159         printk("tvp5150: Vertical blanking start = 0x%02x\n",
160                         tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
161         printk("tvp5150: Vertical blanking stop = 0x%02x\n",
162                         tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
163         printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
164                         tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
165                         tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
166         printk("tvp5150: Interrupt reset register B = 0x%02x\n",
167                         tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
168         printk("tvp5150: Interrupt enable register B = 0x%02x\n",
169                         tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
170         printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
171                         tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
172         printk("tvp5150: Video standard = 0x%02x\n",
173                         tvp5150_read(sd, TVP5150_VIDEO_STD));
174         printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
175                         tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
176                         tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
177         printk("tvp5150: Macrovision on counter = 0x%02x\n",
178                         tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
179         printk("tvp5150: Macrovision off counter = 0x%02x\n",
180                         tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
181         printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
182                         (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
183         printk("tvp5150: Device ID = %02x%02x\n",
184                         tvp5150_read(sd, TVP5150_MSB_DEV_ID),
185                         tvp5150_read(sd, TVP5150_LSB_DEV_ID));
186         printk("tvp5150: ROM version = (hex) %02x.%02x\n",
187                         tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
188                         tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
189         printk("tvp5150: Vertical line count = 0x%02x%02x\n",
190                         tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
191                         tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
192         printk("tvp5150: Interrupt status register B = 0x%02x\n",
193                         tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
194         printk("tvp5150: Interrupt active register B = 0x%02x\n",
195                         tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
196         printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
197                         tvp5150_read(sd, TVP5150_STATUS_REG_1),
198                         tvp5150_read(sd, TVP5150_STATUS_REG_2),
199                         tvp5150_read(sd, TVP5150_STATUS_REG_3),
200                         tvp5150_read(sd, TVP5150_STATUS_REG_4),
201                         tvp5150_read(sd, TVP5150_STATUS_REG_5));
202
203         dump_reg_range(sd, "Teletext filter 1",   TVP5150_TELETEXT_FIL1_INI,
204                         TVP5150_TELETEXT_FIL1_END, 8);
205         dump_reg_range(sd, "Teletext filter 2",   TVP5150_TELETEXT_FIL2_INI,
206                         TVP5150_TELETEXT_FIL2_END, 8);
207
208         printk("tvp5150: Teletext filter enable = 0x%02x\n",
209                         tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
210         printk("tvp5150: Interrupt status register A = 0x%02x\n",
211                         tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
212         printk("tvp5150: Interrupt enable register A = 0x%02x\n",
213                         tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
214         printk("tvp5150: Interrupt configuration = 0x%02x\n",
215                         tvp5150_read(sd, TVP5150_INT_CONF));
216         printk("tvp5150: VDP status register = 0x%02x\n",
217                         tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
218         printk("tvp5150: FIFO word count = 0x%02x\n",
219                         tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
220         printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
221                         tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
222         printk("tvp5150: FIFO reset = 0x%02x\n",
223                         tvp5150_read(sd, TVP5150_FIFO_RESET));
224         printk("tvp5150: Line number interrupt = 0x%02x\n",
225                         tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
226         printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
227                         tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
228                         tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
229         printk("tvp5150: FIFO output control = 0x%02x\n",
230                         tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
231         printk("tvp5150: Full field enable = 0x%02x\n",
232                         tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
233         printk("tvp5150: Full field mode register = 0x%02x\n",
234                         tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
235
236         dump_reg_range(sd, "CC   data",   TVP5150_CC_DATA_INI,
237                         TVP5150_CC_DATA_END, 8);
238
239         dump_reg_range(sd, "WSS  data",   TVP5150_WSS_DATA_INI,
240                         TVP5150_WSS_DATA_END, 8);
241
242         dump_reg_range(sd, "VPS  data",   TVP5150_VPS_DATA_INI,
243                         TVP5150_VPS_DATA_END, 8);
244
245         dump_reg_range(sd, "VITC data",   TVP5150_VITC_DATA_INI,
246                         TVP5150_VITC_DATA_END, 10);
247
248         dump_reg_range(sd, "Line mode",   TVP5150_LINE_MODE_INI,
249                         TVP5150_LINE_MODE_END, 8);
250         return 0;
251 }
252
253 /****************************************************************************
254                         Basic functions
255  ****************************************************************************/
256
257 static inline void tvp5150_selmux(struct v4l2_subdev *sd)
258 {
259         int opmode = 0;
260         struct tvp5150 *decoder = to_tvp5150(sd);
261         int input = 0;
262         int val;
263
264         /* Only tvp5150am1 and tvp5151 have signal generator support */
265         if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
266             (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
267                 if (!decoder->enable)
268                         input = 8;
269         }
270
271         switch (decoder->input) {
272         case TVP5150_COMPOSITE1:
273                 input |= 2;
274                 /* fall through */
275         case TVP5150_COMPOSITE0:
276                 break;
277         case TVP5150_SVIDEO:
278         default:
279                 input |= 1;
280                 break;
281         }
282
283         v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
284                         "=> tvp5150 input=%i, opmode=%i\n",
285                         decoder->input, decoder->output,
286                         input, opmode);
287
288         tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
289         tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
290
291         /*
292          * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
293          * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
294          * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
295          * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
296          * INTREQ/GPCL/VBLK to logic 1.
297          */
298         val = tvp5150_read(sd, TVP5150_MISC_CTL);
299         if (val < 0) {
300                 v4l2_err(sd, "%s: failed with error = %d\n", __func__, val);
301                 return;
302         }
303
304         if (decoder->input == TVP5150_SVIDEO)
305                 val = (val & ~TVP5150_MISC_CTL_GPCL) | TVP5150_MISC_CTL_HVLK;
306         else
307                 val = (val & ~TVP5150_MISC_CTL_HVLK) | TVP5150_MISC_CTL_GPCL;
308         tvp5150_write(sd, TVP5150_MISC_CTL, val);
309 };
310
311 struct i2c_reg_value {
312         unsigned char reg;
313         unsigned char value;
314 };
315
316 /* Default values as sugested at TVP5150AM1 datasheet */
317 static const struct i2c_reg_value tvp5150_init_default[] = {
318         { /* 0x00 */
319                 TVP5150_VD_IN_SRC_SEL_1,0x00
320         },
321         { /* 0x01 */
322                 TVP5150_ANAL_CHL_CTL,0x15
323         },
324         { /* 0x02 */
325                 TVP5150_OP_MODE_CTL,0x00
326         },
327         { /* 0x03 */
328                 TVP5150_MISC_CTL,0x01
329         },
330         { /* 0x06 */
331                 TVP5150_COLOR_KIL_THSH_CTL,0x10
332         },
333         { /* 0x07 */
334                 TVP5150_LUMA_PROC_CTL_1,0x60
335         },
336         { /* 0x08 */
337                 TVP5150_LUMA_PROC_CTL_2,0x00
338         },
339         { /* 0x09 */
340                 TVP5150_BRIGHT_CTL,0x80
341         },
342         { /* 0x0a */
343                 TVP5150_SATURATION_CTL,0x80
344         },
345         { /* 0x0b */
346                 TVP5150_HUE_CTL,0x00
347         },
348         { /* 0x0c */
349                 TVP5150_CONTRAST_CTL,0x80
350         },
351         { /* 0x0d */
352                 TVP5150_DATA_RATE_SEL,0x47
353         },
354         { /* 0x0e */
355                 TVP5150_LUMA_PROC_CTL_3,0x00
356         },
357         { /* 0x0f */
358                 TVP5150_CONF_SHARED_PIN,0x08
359         },
360         { /* 0x11 */
361                 TVP5150_ACT_VD_CROP_ST_MSB,0x00
362         },
363         { /* 0x12 */
364                 TVP5150_ACT_VD_CROP_ST_LSB,0x00
365         },
366         { /* 0x13 */
367                 TVP5150_ACT_VD_CROP_STP_MSB,0x00
368         },
369         { /* 0x14 */
370                 TVP5150_ACT_VD_CROP_STP_LSB,0x00
371         },
372         { /* 0x15 */
373                 TVP5150_GENLOCK,0x01
374         },
375         { /* 0x16 */
376                 TVP5150_HORIZ_SYNC_START,0x80
377         },
378         { /* 0x18 */
379                 TVP5150_VERT_BLANKING_START,0x00
380         },
381         { /* 0x19 */
382                 TVP5150_VERT_BLANKING_STOP,0x00
383         },
384         { /* 0x1a */
385                 TVP5150_CHROMA_PROC_CTL_1,0x0c
386         },
387         { /* 0x1b */
388                 TVP5150_CHROMA_PROC_CTL_2,0x14
389         },
390         { /* 0x1c */
391                 TVP5150_INT_RESET_REG_B,0x00
392         },
393         { /* 0x1d */
394                 TVP5150_INT_ENABLE_REG_B,0x00
395         },
396         { /* 0x1e */
397                 TVP5150_INTT_CONFIG_REG_B,0x00
398         },
399         { /* 0x28 */
400                 TVP5150_VIDEO_STD,0x00
401         },
402         { /* 0x2e */
403                 TVP5150_MACROVISION_ON_CTR,0x0f
404         },
405         { /* 0x2f */
406                 TVP5150_MACROVISION_OFF_CTR,0x01
407         },
408         { /* 0xbb */
409                 TVP5150_TELETEXT_FIL_ENA,0x00
410         },
411         { /* 0xc0 */
412                 TVP5150_INT_STATUS_REG_A,0x00
413         },
414         { /* 0xc1 */
415                 TVP5150_INT_ENABLE_REG_A,0x00
416         },
417         { /* 0xc2 */
418                 TVP5150_INT_CONF,0x04
419         },
420         { /* 0xc8 */
421                 TVP5150_FIFO_INT_THRESHOLD,0x80
422         },
423         { /* 0xc9 */
424                 TVP5150_FIFO_RESET,0x00
425         },
426         { /* 0xca */
427                 TVP5150_LINE_NUMBER_INT,0x00
428         },
429         { /* 0xcb */
430                 TVP5150_PIX_ALIGN_REG_LOW,0x4e
431         },
432         { /* 0xcc */
433                 TVP5150_PIX_ALIGN_REG_HIGH,0x00
434         },
435         { /* 0xcd */
436                 TVP5150_FIFO_OUT_CTRL,0x01
437         },
438         { /* 0xcf */
439                 TVP5150_FULL_FIELD_ENA,0x00
440         },
441         { /* 0xd0 */
442                 TVP5150_LINE_MODE_INI,0x00
443         },
444         { /* 0xfc */
445                 TVP5150_FULL_FIELD_MODE_REG,0x7f
446         },
447         { /* end of data */
448                 0xff,0xff
449         }
450 };
451
452 /* Default values as sugested at TVP5150AM1 datasheet */
453 static const struct i2c_reg_value tvp5150_init_enable[] = {
454         {
455                 TVP5150_CONF_SHARED_PIN, 2
456         },{     /* Automatic offset and AGC enabled */
457                 TVP5150_ANAL_CHL_CTL, 0x15
458         },{     /* Activate YCrCb output 0x9 or 0xd ? */
459                 TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
460                                   TVP5150_MISC_CTL_INTREQ_OE |
461                                   TVP5150_MISC_CTL_YCBCR_OE |
462                                   TVP5150_MISC_CTL_SYNC_OE |
463                                   TVP5150_MISC_CTL_VBLANK |
464                                   TVP5150_MISC_CTL_CLOCK_OE,
465         },{     /* Activates video std autodetection for all standards */
466                 TVP5150_AUTOSW_MSK, 0x0
467         },{     /* Default format: 0x47. For 4:2:2: 0x40 */
468                 TVP5150_DATA_RATE_SEL, 0x47
469         },{
470                 TVP5150_CHROMA_PROC_CTL_1, 0x0c
471         },{
472                 TVP5150_CHROMA_PROC_CTL_2, 0x54
473         },{     /* Non documented, but initialized on WinTV USB2 */
474                 0x27, 0x20
475         },{
476                 0xff,0xff
477         }
478 };
479
480 struct tvp5150_vbi_type {
481         unsigned int vbi_type;
482         unsigned int ini_line;
483         unsigned int end_line;
484         unsigned int by_field :1;
485 };
486
487 struct i2c_vbi_ram_value {
488         u16 reg;
489         struct tvp5150_vbi_type type;
490         unsigned char values[16];
491 };
492
493 /* This struct have the values for each supported VBI Standard
494  * by
495  tvp5150_vbi_types should follow the same order as vbi_ram_default
496  * value 0 means rom position 0x10, value 1 means rom position 0x30
497  * and so on. There are 16 possible locations from 0 to 15.
498  */
499
500 static struct i2c_vbi_ram_value vbi_ram_default[] =
501 {
502         /* FIXME: Current api doesn't handle all VBI types, those not
503            yet supported are placed under #if 0 */
504 #if 0
505         {0x010, /* Teletext, SECAM, WST System A */
506                 {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
507                 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
508                   0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
509         },
510 #endif
511         {0x030, /* Teletext, PAL, WST System B */
512                 {V4L2_SLICED_TELETEXT_B,6,22,1},
513                 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
514                   0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
515         },
516 #if 0
517         {0x050, /* Teletext, PAL, WST System C */
518                 {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
519                 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
520                   0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
521         },
522         {0x070, /* Teletext, NTSC, WST System B */
523                 {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
524                 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
525                   0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
526         },
527         {0x090, /* Tetetext, NTSC NABTS System C */
528                 {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
529                 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
530                   0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
531         },
532         {0x0b0, /* Teletext, NTSC-J, NABTS System D */
533                 {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
534                 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
535                   0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
536         },
537         {0x0d0, /* Closed Caption, PAL/SECAM */
538                 {V4L2_SLICED_CAPTION_625,22,22,1},
539                 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
540                   0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
541         },
542 #endif
543         {0x0f0, /* Closed Caption, NTSC */
544                 {V4L2_SLICED_CAPTION_525,21,21,1},
545                 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
546                   0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
547         },
548         {0x110, /* Wide Screen Signal, PAL/SECAM */
549                 {V4L2_SLICED_WSS_625,23,23,1},
550                 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
551                   0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
552         },
553 #if 0
554         {0x130, /* Wide Screen Signal, NTSC C */
555                 {V4L2_SLICED_WSS_525,20,20,1},
556                 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
557                   0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
558         },
559         {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
560                 {V4l2_SLICED_VITC_625,6,22,0},
561                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
562                   0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
563         },
564         {0x170, /* Vertical Interval Timecode (VITC), NTSC */
565                 {V4l2_SLICED_VITC_525,10,20,0},
566                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
567                   0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
568         },
569 #endif
570         {0x190, /* Video Program System (VPS), PAL */
571                 {V4L2_SLICED_VPS,16,16,0},
572                 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
573                   0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
574         },
575         /* 0x1d0 User programmable */
576
577         /* End of struct */
578         { (u16)-1 }
579 };
580
581 static int tvp5150_write_inittab(struct v4l2_subdev *sd,
582                                 const struct i2c_reg_value *regs)
583 {
584         while (regs->reg != 0xff) {
585                 tvp5150_write(sd, regs->reg, regs->value);
586                 regs++;
587         }
588         return 0;
589 }
590
591 static int tvp5150_vdp_init(struct v4l2_subdev *sd,
592                                 const struct i2c_vbi_ram_value *regs)
593 {
594         unsigned int i;
595
596         /* Disable Full Field */
597         tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
598
599         /* Before programming, Line mode should be at 0xff */
600         for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
601                 tvp5150_write(sd, i, 0xff);
602
603         /* Load Ram Table */
604         while (regs->reg != (u16)-1) {
605                 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
606                 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
607
608                 for (i = 0; i < 16; i++)
609                         tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
610
611                 regs++;
612         }
613         return 0;
614 }
615
616 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
617 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
618                                 struct v4l2_sliced_vbi_cap *cap)
619 {
620         const struct i2c_vbi_ram_value *regs = vbi_ram_default;
621         int line;
622
623         v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
624         memset(cap, 0, sizeof *cap);
625
626         while (regs->reg != (u16)-1 ) {
627                 for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
628                         cap->service_lines[0][line] |= regs->type.vbi_type;
629                 }
630                 cap->service_set |= regs->type.vbi_type;
631
632                 regs++;
633         }
634         return 0;
635 }
636
637 /* Set vbi processing
638  * type - one of tvp5150_vbi_types
639  * line - line to gather data
640  * fields: bit 0 field1, bit 1, field2
641  * flags (default=0xf0) is a bitmask, were set means:
642  *      bit 7: enable filtering null bytes on CC
643  *      bit 6: send data also to FIFO
644  *      bit 5: don't allow data with errors on FIFO
645  *      bit 4: enable ECC when possible
646  * pix_align = pix alignment:
647  *      LSB = field1
648  *      MSB = field2
649  */
650 static int tvp5150_set_vbi(struct v4l2_subdev *sd,
651                         const struct i2c_vbi_ram_value *regs,
652                         unsigned int type,u8 flags, int line,
653                         const int fields)
654 {
655         struct tvp5150 *decoder = to_tvp5150(sd);
656         v4l2_std_id std = decoder->norm;
657         u8 reg;
658         int pos=0;
659
660         if (std == V4L2_STD_ALL) {
661                 v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
662                 return 0;
663         } else if (std & V4L2_STD_625_50) {
664                 /* Don't follow NTSC Line number convension */
665                 line += 3;
666         }
667
668         if (line<6||line>27)
669                 return 0;
670
671         while (regs->reg != (u16)-1 ) {
672                 if ((type & regs->type.vbi_type) &&
673                     (line>=regs->type.ini_line) &&
674                     (line<=regs->type.end_line)) {
675                         type=regs->type.vbi_type;
676                         break;
677                 }
678
679                 regs++;
680                 pos++;
681         }
682         if (regs->reg == (u16)-1)
683                 return 0;
684
685         type=pos | (flags & 0xf0);
686         reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
687
688         if (fields&1) {
689                 tvp5150_write(sd, reg, type);
690         }
691
692         if (fields&2) {
693                 tvp5150_write(sd, reg+1, type);
694         }
695
696         return type;
697 }
698
699 static int tvp5150_get_vbi(struct v4l2_subdev *sd,
700                         const struct i2c_vbi_ram_value *regs, int line)
701 {
702         struct tvp5150 *decoder = to_tvp5150(sd);
703         v4l2_std_id std = decoder->norm;
704         u8 reg;
705         int pos, type = 0;
706         int i, ret = 0;
707
708         if (std == V4L2_STD_ALL) {
709                 v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
710                 return 0;
711         } else if (std & V4L2_STD_625_50) {
712                 /* Don't follow NTSC Line number convension */
713                 line += 3;
714         }
715
716         if (line < 6 || line > 27)
717                 return 0;
718
719         reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
720
721         for (i = 0; i <= 1; i++) {
722                 ret = tvp5150_read(sd, reg + i);
723                 if (ret < 0) {
724                         v4l2_err(sd, "%s: failed with error = %d\n",
725                                  __func__, ret);
726                         return 0;
727                 }
728                 pos = ret & 0x0f;
729                 if (pos < 0x0f)
730                         type |= regs[pos].type.vbi_type;
731         }
732
733         return type;
734 }
735
736 static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
737 {
738         struct tvp5150 *decoder = to_tvp5150(sd);
739         int fmt = 0;
740
741         decoder->norm = std;
742
743         /* First tests should be against specific std */
744
745         if (std == V4L2_STD_NTSC_443) {
746                 fmt = VIDEO_STD_NTSC_4_43_BIT;
747         } else if (std == V4L2_STD_PAL_M) {
748                 fmt = VIDEO_STD_PAL_M_BIT;
749         } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
750                 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
751         } else {
752                 /* Then, test against generic ones */
753                 if (std & V4L2_STD_NTSC)
754                         fmt = VIDEO_STD_NTSC_MJ_BIT;
755                 else if (std & V4L2_STD_PAL)
756                         fmt = VIDEO_STD_PAL_BDGHIN_BIT;
757                 else if (std & V4L2_STD_SECAM)
758                         fmt = VIDEO_STD_SECAM_BIT;
759         }
760
761         v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
762         tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
763         return 0;
764 }
765
766 static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
767 {
768         struct tvp5150 *decoder = to_tvp5150(sd);
769
770         if (decoder->norm == std)
771                 return 0;
772
773         /* Change cropping height limits */
774         if (std & V4L2_STD_525_60)
775                 decoder->rect.height = TVP5150_V_MAX_525_60;
776         else
777                 decoder->rect.height = TVP5150_V_MAX_OTHERS;
778
779
780         return tvp5150_set_std(sd, std);
781 }
782
783 static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
784 {
785         struct tvp5150 *decoder = to_tvp5150(sd);
786
787         /* Initializes TVP5150 to its default values */
788         tvp5150_write_inittab(sd, tvp5150_init_default);
789
790         /* Initializes VDP registers */
791         tvp5150_vdp_init(sd, vbi_ram_default);
792
793         /* Selects decoder input */
794         tvp5150_selmux(sd);
795
796         /* Initializes TVP5150 to stream enabled values */
797         tvp5150_write_inittab(sd, tvp5150_init_enable);
798
799         /* Initialize image preferences */
800         v4l2_ctrl_handler_setup(&decoder->hdl);
801
802         tvp5150_set_std(sd, decoder->norm);
803
804         if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
805                 tvp5150_write(sd, TVP5150_DATA_RATE_SEL, 0x40);
806
807         return 0;
808 };
809
810 static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
811 {
812         struct v4l2_subdev *sd = to_sd(ctrl);
813         struct tvp5150 *decoder = to_tvp5150(sd);
814
815         switch (ctrl->id) {
816         case V4L2_CID_BRIGHTNESS:
817                 tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
818                 return 0;
819         case V4L2_CID_CONTRAST:
820                 tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
821                 return 0;
822         case V4L2_CID_SATURATION:
823                 tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
824                 return 0;
825         case V4L2_CID_HUE:
826                 tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
827                 return 0;
828         case V4L2_CID_TEST_PATTERN:
829                 decoder->enable = ctrl->val ? false : true;
830                 tvp5150_selmux(sd);
831                 return 0;
832         }
833         return -EINVAL;
834 }
835
836 static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
837 {
838         int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
839
840         switch (val & 0x0F) {
841         case 0x01:
842                 return V4L2_STD_NTSC;
843         case 0x03:
844                 return V4L2_STD_PAL;
845         case 0x05:
846                 return V4L2_STD_PAL_M;
847         case 0x07:
848                 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
849         case 0x09:
850                 return V4L2_STD_NTSC_443;
851         case 0xb:
852                 return V4L2_STD_SECAM;
853         default:
854                 return V4L2_STD_UNKNOWN;
855         }
856 }
857
858 static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
859                 struct v4l2_subdev_pad_config *cfg,
860                 struct v4l2_subdev_format *format)
861 {
862         struct v4l2_mbus_framefmt *f;
863         struct tvp5150 *decoder = to_tvp5150(sd);
864
865         if (!format || format->pad)
866                 return -EINVAL;
867
868         f = &format->format;
869
870         f->width = decoder->rect.width;
871         f->height = decoder->rect.height / 2;
872
873         f->code = MEDIA_BUS_FMT_UYVY8_2X8;
874         f->field = V4L2_FIELD_ALTERNATE;
875         f->colorspace = V4L2_COLORSPACE_SMPTE170M;
876
877         v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
878                         f->height);
879         return 0;
880 }
881
882 static int tvp5150_set_selection(struct v4l2_subdev *sd,
883                                  struct v4l2_subdev_pad_config *cfg,
884                                  struct v4l2_subdev_selection *sel)
885 {
886         struct tvp5150 *decoder = to_tvp5150(sd);
887         struct v4l2_rect rect = sel->r;
888         v4l2_std_id std;
889         int hmax;
890
891         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
892             sel->target != V4L2_SEL_TGT_CROP)
893                 return -EINVAL;
894
895         v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
896                 __func__, rect.left, rect.top, rect.width, rect.height);
897
898         /* tvp5150 has some special limits */
899         rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
900         rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
901
902         /* Calculate height based on current standard */
903         if (decoder->norm == V4L2_STD_ALL)
904                 std = tvp5150_read_std(sd);
905         else
906                 std = decoder->norm;
907
908         if (std & V4L2_STD_525_60)
909                 hmax = TVP5150_V_MAX_525_60;
910         else
911                 hmax = TVP5150_V_MAX_OTHERS;
912
913         /*
914          * alignments:
915          *  - width = 2 due to UYVY colorspace
916          *  - height, image = no special alignment
917          */
918         v4l_bound_align_image(&rect.width,
919                               TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
920                               TVP5150_H_MAX - rect.left, 1, &rect.height,
921                               hmax - TVP5150_MAX_CROP_TOP - rect.top,
922                               hmax - rect.top, 0, 0);
923
924         tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
925         tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
926                       rect.top + rect.height - hmax);
927         tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
928                       rect.left >> TVP5150_CROP_SHIFT);
929         tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
930                       rect.left | (1 << TVP5150_CROP_SHIFT));
931         tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
932                       (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
933                       TVP5150_CROP_SHIFT);
934         tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
935                       rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
936
937         decoder->rect = rect;
938
939         return 0;
940 }
941
942 static int tvp5150_get_selection(struct v4l2_subdev *sd,
943                                  struct v4l2_subdev_pad_config *cfg,
944                                  struct v4l2_subdev_selection *sel)
945 {
946         struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
947         v4l2_std_id std;
948
949         if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
950                 return -EINVAL;
951
952         switch (sel->target) {
953         case V4L2_SEL_TGT_CROP_BOUNDS:
954         case V4L2_SEL_TGT_CROP_DEFAULT:
955                 sel->r.left = 0;
956                 sel->r.top = 0;
957                 sel->r.width = TVP5150_H_MAX;
958
959                 /* Calculate height based on current standard */
960                 if (decoder->norm == V4L2_STD_ALL)
961                         std = tvp5150_read_std(sd);
962                 else
963                         std = decoder->norm;
964                 if (std & V4L2_STD_525_60)
965                         sel->r.height = TVP5150_V_MAX_525_60;
966                 else
967                         sel->r.height = TVP5150_V_MAX_OTHERS;
968                 return 0;
969         case V4L2_SEL_TGT_CROP:
970                 sel->r = decoder->rect;
971                 return 0;
972         default:
973                 return -EINVAL;
974         }
975 }
976
977 static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
978                                  struct v4l2_mbus_config *cfg)
979 {
980         struct tvp5150 *decoder = to_tvp5150(sd);
981
982         cfg->type = decoder->mbus_type;
983         cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
984                    | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
985
986         return 0;
987 }
988
989 /****************************************************************************
990                         V4L2 subdev pad ops
991  ****************************************************************************/
992 static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
993                 struct v4l2_subdev_pad_config *cfg,
994                 struct v4l2_subdev_mbus_code_enum *code)
995 {
996         if (code->pad || code->index)
997                 return -EINVAL;
998
999         code->code = MEDIA_BUS_FMT_UYVY8_2X8;
1000         return 0;
1001 }
1002
1003 static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
1004                                    struct v4l2_subdev_pad_config *cfg,
1005                                    struct v4l2_subdev_frame_size_enum *fse)
1006 {
1007         struct tvp5150 *decoder = to_tvp5150(sd);
1008
1009         if (fse->index >= 8 || fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
1010                 return -EINVAL;
1011
1012         fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
1013         fse->min_width = decoder->rect.width;
1014         fse->max_width = decoder->rect.width;
1015         fse->min_height = decoder->rect.height / 2;
1016         fse->max_height = decoder->rect.height / 2;
1017
1018         return 0;
1019 }
1020
1021 /****************************************************************************
1022                         Media entity ops
1023  ****************************************************************************/
1024
1025 static int tvp5150_link_setup(struct media_entity *entity,
1026                               const struct media_pad *local,
1027                               const struct media_pad *remote, u32 flags)
1028 {
1029 #ifdef CONFIG_MEDIA_CONTROLLER
1030         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1031         struct tvp5150 *decoder = to_tvp5150(sd);
1032         int i;
1033
1034         for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1035                 if (remote->entity == &decoder->input_ent[i])
1036                         break;
1037         }
1038
1039         /* Do nothing for entities that are not input connectors */
1040         if (i == TVP5150_INPUT_NUM)
1041                 return 0;
1042
1043         decoder->input = i;
1044
1045         tvp5150_selmux(sd);
1046 #endif
1047
1048         return 0;
1049 }
1050
1051 static const struct media_entity_operations tvp5150_sd_media_ops = {
1052         .link_setup = tvp5150_link_setup,
1053 };
1054
1055 /****************************************************************************
1056                         I2C Command
1057  ****************************************************************************/
1058
1059 static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1060 {
1061         struct tvp5150 *decoder = to_tvp5150(sd);
1062         int val;
1063
1064         /* Enable or disable the video output signals. */
1065         val = tvp5150_read(sd, TVP5150_MISC_CTL);
1066         if (val < 0)
1067                 return val;
1068
1069         val &= ~(TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
1070                  TVP5150_MISC_CTL_CLOCK_OE);
1071
1072         if (enable) {
1073                 /*
1074                  * Enable the YCbCr and clock outputs. In discrete sync mode
1075                  * (non-BT.656) additionally enable the the sync outputs.
1076                  */
1077                 val |= TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_CLOCK_OE;
1078                 if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
1079                         val |= TVP5150_MISC_CTL_SYNC_OE;
1080         }
1081
1082         tvp5150_write(sd, TVP5150_MISC_CTL, val);
1083
1084         return 0;
1085 }
1086
1087 static int tvp5150_s_routing(struct v4l2_subdev *sd,
1088                              u32 input, u32 output, u32 config)
1089 {
1090         struct tvp5150 *decoder = to_tvp5150(sd);
1091
1092         decoder->input = input;
1093         decoder->output = output;
1094
1095         if (output == TVP5150_BLACK_SCREEN)
1096                 decoder->enable = false;
1097         else
1098                 decoder->enable = true;
1099
1100         tvp5150_selmux(sd);
1101         return 0;
1102 }
1103
1104 static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
1105 {
1106         /* this is for capturing 36 raw vbi lines
1107            if there's a way to cut off the beginning 2 vbi lines
1108            with the tvp5150 then the vbi line count could be lowered
1109            to 17 lines/field again, although I couldn't find a register
1110            which could do that cropping */
1111         if (fmt->sample_format == V4L2_PIX_FMT_GREY)
1112                 tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
1113         if (fmt->count[0] == 18 && fmt->count[1] == 18) {
1114                 tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
1115                 tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
1116         }
1117         return 0;
1118 }
1119
1120 static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1121 {
1122         int i;
1123
1124         if (svbi->service_set != 0) {
1125                 for (i = 0; i <= 23; i++) {
1126                         svbi->service_lines[1][i] = 0;
1127                         svbi->service_lines[0][i] =
1128                                 tvp5150_set_vbi(sd, vbi_ram_default,
1129                                        svbi->service_lines[0][i], 0xf0, i, 3);
1130                 }
1131                 /* Enables FIFO */
1132                 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
1133         } else {
1134                 /* Disables FIFO*/
1135                 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
1136
1137                 /* Disable Full Field */
1138                 tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
1139
1140                 /* Disable Line modes */
1141                 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
1142                         tvp5150_write(sd, i, 0xff);
1143         }
1144         return 0;
1145 }
1146
1147 static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1148 {
1149         int i, mask = 0;
1150
1151         memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
1152
1153         for (i = 0; i <= 23; i++) {
1154                 svbi->service_lines[0][i] =
1155                         tvp5150_get_vbi(sd, vbi_ram_default, i);
1156                 mask |= svbi->service_lines[0][i];
1157         }
1158         svbi->service_set = mask;
1159         return 0;
1160 }
1161
1162 #ifdef CONFIG_VIDEO_ADV_DEBUG
1163 static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1164 {
1165         int res;
1166
1167         res = tvp5150_read(sd, reg->reg & 0xff);
1168         if (res < 0) {
1169                 v4l2_err(sd, "%s: failed with error = %d\n", __func__, res);
1170                 return res;
1171         }
1172
1173         reg->val = res;
1174         reg->size = 1;
1175         return 0;
1176 }
1177
1178 static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
1179 {
1180         return tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
1181 }
1182 #endif
1183
1184 static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1185 {
1186         int status = tvp5150_read(sd, 0x88);
1187
1188         vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1189         return 0;
1190 }
1191
1192 static int tvp5150_registered(struct v4l2_subdev *sd)
1193 {
1194 #ifdef CONFIG_MEDIA_CONTROLLER
1195         struct tvp5150 *decoder = to_tvp5150(sd);
1196         int ret = 0;
1197         int i;
1198
1199         for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1200                 struct media_entity *input = &decoder->input_ent[i];
1201                 struct media_pad *pad = &decoder->input_pad[i];
1202
1203                 if (!input->name)
1204                         continue;
1205
1206                 decoder->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1207
1208                 ret = media_entity_pads_init(input, 1, pad);
1209                 if (ret < 0)
1210                         return ret;
1211
1212                 ret = media_device_register_entity(sd->v4l2_dev->mdev, input);
1213                 if (ret < 0)
1214                         return ret;
1215
1216                 ret = media_create_pad_link(input, 0, &sd->entity,
1217                                             DEMOD_PAD_IF_INPUT, 0);
1218                 if (ret < 0) {
1219                         media_device_unregister_entity(input);
1220                         return ret;
1221                 }
1222         }
1223 #endif
1224
1225         return 0;
1226 }
1227
1228 /* ----------------------------------------------------------------------- */
1229
1230 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1231         .s_ctrl = tvp5150_s_ctrl,
1232 };
1233
1234 static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1235         .log_status = tvp5150_log_status,
1236         .reset = tvp5150_reset,
1237 #ifdef CONFIG_VIDEO_ADV_DEBUG
1238         .g_register = tvp5150_g_register,
1239         .s_register = tvp5150_s_register,
1240 #endif
1241 };
1242
1243 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
1244         .g_tuner = tvp5150_g_tuner,
1245 };
1246
1247 static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
1248         .s_std = tvp5150_s_std,
1249         .s_stream = tvp5150_s_stream,
1250         .s_routing = tvp5150_s_routing,
1251         .g_mbus_config = tvp5150_g_mbus_config,
1252 };
1253
1254 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
1255         .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
1256         .g_sliced_fmt = tvp5150_g_sliced_fmt,
1257         .s_sliced_fmt = tvp5150_s_sliced_fmt,
1258         .s_raw_fmt = tvp5150_s_raw_fmt,
1259 };
1260
1261 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
1262         .enum_mbus_code = tvp5150_enum_mbus_code,
1263         .enum_frame_size = tvp5150_enum_frame_size,
1264         .set_fmt = tvp5150_fill_fmt,
1265         .get_fmt = tvp5150_fill_fmt,
1266         .get_selection = tvp5150_get_selection,
1267         .set_selection = tvp5150_set_selection,
1268 };
1269
1270 static const struct v4l2_subdev_ops tvp5150_ops = {
1271         .core = &tvp5150_core_ops,
1272         .tuner = &tvp5150_tuner_ops,
1273         .video = &tvp5150_video_ops,
1274         .vbi = &tvp5150_vbi_ops,
1275         .pad = &tvp5150_pad_ops,
1276 };
1277
1278 static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
1279         .registered = tvp5150_registered,
1280 };
1281
1282
1283 /****************************************************************************
1284                         I2C Client & Driver
1285  ****************************************************************************/
1286
1287 static int tvp5150_detect_version(struct tvp5150 *core)
1288 {
1289         struct v4l2_subdev *sd = &core->sd;
1290         struct i2c_client *c = v4l2_get_subdevdata(sd);
1291         unsigned int i;
1292         u8 regs[4];
1293         int res;
1294
1295         /*
1296          * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1297          * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1298          */
1299         for (i = 0; i < 4; i++) {
1300                 res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
1301                 if (res < 0)
1302                         return res;
1303                 regs[i] = res;
1304         }
1305
1306         core->dev_id = (regs[0] << 8) | regs[1];
1307         core->rom_ver = (regs[2] << 8) | regs[3];
1308
1309         v4l2_info(sd, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
1310                   core->dev_id, regs[2], regs[3], c->addr << 1,
1311                   c->adapter->name);
1312
1313         if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
1314                 v4l2_info(sd, "tvp5150a detected.\n");
1315         } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
1316                 v4l2_info(sd, "tvp5150am1 detected.\n");
1317
1318                 /* ITU-T BT.656.4 timing */
1319                 tvp5150_write(sd, TVP5150_REV_SELECT, 0);
1320         } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
1321                 v4l2_info(sd, "tvp5151 detected.\n");
1322         } else {
1323                 v4l2_info(sd, "*** unknown tvp%04x chip detected.\n",
1324                           core->dev_id);
1325         }
1326
1327         return 0;
1328 }
1329
1330 static int tvp5150_init(struct i2c_client *c)
1331 {
1332         struct gpio_desc *pdn_gpio;
1333         struct gpio_desc *reset_gpio;
1334
1335         pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1336         if (IS_ERR(pdn_gpio))
1337                 return PTR_ERR(pdn_gpio);
1338
1339         if (pdn_gpio) {
1340                 gpiod_set_value_cansleep(pdn_gpio, 0);
1341                 /* Delay time between power supplies active and reset */
1342                 msleep(20);
1343         }
1344
1345         reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1346         if (IS_ERR(reset_gpio))
1347                 return PTR_ERR(reset_gpio);
1348
1349         if (reset_gpio) {
1350                 /* RESETB pulse duration */
1351                 ndelay(500);
1352                 gpiod_set_value_cansleep(reset_gpio, 0);
1353                 /* Delay time between end of reset to I2C active */
1354                 usleep_range(200, 250);
1355         }
1356
1357         return 0;
1358 }
1359
1360 static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1361 {
1362         struct v4l2_of_endpoint bus_cfg;
1363         struct device_node *ep;
1364 #ifdef CONFIG_MEDIA_CONTROLLER
1365         struct device_node *connectors, *child;
1366         struct media_entity *input;
1367         const char *name;
1368         u32 input_type;
1369 #endif
1370         unsigned int flags;
1371         int ret = 0;
1372
1373         ep = of_graph_get_next_endpoint(np, NULL);
1374         if (!ep)
1375                 return -EINVAL;
1376
1377         ret = v4l2_of_parse_endpoint(ep, &bus_cfg);
1378         if (ret)
1379                 goto err;
1380
1381         flags = bus_cfg.bus.parallel.flags;
1382
1383         if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
1384             !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
1385               flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
1386               flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
1387                 ret = -EINVAL;
1388                 goto err;
1389         }
1390
1391         decoder->mbus_type = bus_cfg.bus_type;
1392
1393 #ifdef CONFIG_MEDIA_CONTROLLER
1394         connectors = of_get_child_by_name(np, "connectors");
1395
1396         if (!connectors)
1397                 goto err;
1398
1399         for_each_available_child_of_node(connectors, child) {
1400                 ret = of_property_read_u32(child, "input", &input_type);
1401                 if (ret) {
1402                         v4l2_err(&decoder->sd,
1403                                  "missing type property in node %s\n",
1404                                  child->name);
1405                         goto err_connector;
1406                 }
1407
1408                 if (input_type >= TVP5150_INPUT_NUM) {
1409                         ret = -EINVAL;
1410                         goto err_connector;
1411                 }
1412
1413                 input = &decoder->input_ent[input_type];
1414
1415                 /* Each input connector can only be defined once */
1416                 if (input->name) {
1417                         v4l2_err(&decoder->sd,
1418                                  "input %s with same type already exists\n",
1419                                  input->name);
1420                         ret = -EINVAL;
1421                         goto err_connector;
1422                 }
1423
1424                 switch (input_type) {
1425                 case TVP5150_COMPOSITE0:
1426                 case TVP5150_COMPOSITE1:
1427                         input->function = MEDIA_ENT_F_CONN_COMPOSITE;
1428                         break;
1429                 case TVP5150_SVIDEO:
1430                         input->function = MEDIA_ENT_F_CONN_SVIDEO;
1431                         break;
1432                 }
1433
1434                 input->flags = MEDIA_ENT_FL_CONNECTOR;
1435
1436                 ret = of_property_read_string(child, "label", &name);
1437                 if (ret < 0) {
1438                         v4l2_err(&decoder->sd,
1439                                  "missing label property in node %s\n",
1440                                  child->name);
1441                         goto err_connector;
1442                 }
1443
1444                 input->name = name;
1445         }
1446
1447 err_connector:
1448         of_node_put(connectors);
1449 #endif
1450 err:
1451         of_node_put(ep);
1452         return ret;
1453 }
1454
1455 static const char * const tvp5150_test_patterns[2] = {
1456         "Disabled",
1457         "Black screen"
1458 };
1459
1460 static int tvp5150_probe(struct i2c_client *c,
1461                          const struct i2c_device_id *id)
1462 {
1463         struct tvp5150 *core;
1464         struct v4l2_subdev *sd;
1465         struct device_node *np = c->dev.of_node;
1466         int res;
1467
1468         /* Check if the adapter supports the needed features */
1469         if (!i2c_check_functionality(c->adapter,
1470              I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1471                 return -EIO;
1472
1473         res = tvp5150_init(c);
1474         if (res)
1475                 return res;
1476
1477         core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
1478         if (!core)
1479                 return -ENOMEM;
1480
1481         sd = &core->sd;
1482
1483         if (IS_ENABLED(CONFIG_OF) && np) {
1484                 res = tvp5150_parse_dt(core, np);
1485                 if (res) {
1486                         v4l2_err(sd, "DT parsing error: %d\n", res);
1487                         return res;
1488                 }
1489         } else {
1490                 /* Default to BT.656 embedded sync */
1491                 core->mbus_type = V4L2_MBUS_BT656;
1492         }
1493
1494         v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
1495         sd->internal_ops = &tvp5150_internal_ops;
1496         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1497
1498 #if defined(CONFIG_MEDIA_CONTROLLER)
1499         core->pads[DEMOD_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
1500         core->pads[DEMOD_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
1501         core->pads[DEMOD_PAD_VBI_OUT].flags = MEDIA_PAD_FL_SOURCE;
1502
1503         sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1504
1505         res = media_entity_pads_init(&sd->entity, DEMOD_NUM_PADS, core->pads);
1506         if (res < 0)
1507                 return res;
1508
1509         sd->entity.ops = &tvp5150_sd_media_ops;
1510 #endif
1511
1512         res = tvp5150_detect_version(core);
1513         if (res < 0)
1514                 return res;
1515
1516         core->norm = V4L2_STD_ALL;      /* Default is autodetect */
1517         core->input = TVP5150_COMPOSITE1;
1518         core->enable = true;
1519
1520         v4l2_ctrl_handler_init(&core->hdl, 5);
1521         v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1522                         V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1523         v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1524                         V4L2_CID_CONTRAST, 0, 255, 1, 128);
1525         v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1526                         V4L2_CID_SATURATION, 0, 255, 1, 128);
1527         v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1528                         V4L2_CID_HUE, -128, 127, 1, 0);
1529         v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1530                         V4L2_CID_PIXEL_RATE, 27000000,
1531                         27000000, 1, 27000000);
1532         v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
1533                                      V4L2_CID_TEST_PATTERN,
1534                                      ARRAY_SIZE(tvp5150_test_patterns) - 1,
1535                                      0, 0, tvp5150_test_patterns);
1536         sd->ctrl_handler = &core->hdl;
1537         if (core->hdl.error) {
1538                 res = core->hdl.error;
1539                 goto err;
1540         }
1541
1542         /* Default is no cropping */
1543         core->rect.top = 0;
1544         if (tvp5150_read_std(sd) & V4L2_STD_525_60)
1545                 core->rect.height = TVP5150_V_MAX_525_60;
1546         else
1547                 core->rect.height = TVP5150_V_MAX_OTHERS;
1548         core->rect.left = 0;
1549         core->rect.width = TVP5150_H_MAX;
1550
1551         tvp5150_reset(sd, 0);   /* Calls v4l2_ctrl_handler_setup() */
1552
1553         res = v4l2_async_register_subdev(sd);
1554         if (res < 0)
1555                 goto err;
1556
1557         if (debug > 1)
1558                 tvp5150_log_status(sd);
1559         return 0;
1560
1561 err:
1562         v4l2_ctrl_handler_free(&core->hdl);
1563         return res;
1564 }
1565
1566 static int tvp5150_remove(struct i2c_client *c)
1567 {
1568         struct v4l2_subdev *sd = i2c_get_clientdata(c);
1569         struct tvp5150 *decoder = to_tvp5150(sd);
1570
1571         v4l2_dbg(1, debug, sd,
1572                 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1573                 c->addr << 1);
1574
1575         v4l2_async_unregister_subdev(sd);
1576         v4l2_ctrl_handler_free(&decoder->hdl);
1577         return 0;
1578 }
1579
1580 /* ----------------------------------------------------------------------- */
1581
1582 static const struct i2c_device_id tvp5150_id[] = {
1583         { "tvp5150", 0 },
1584         { }
1585 };
1586 MODULE_DEVICE_TABLE(i2c, tvp5150_id);
1587
1588 #if IS_ENABLED(CONFIG_OF)
1589 static const struct of_device_id tvp5150_of_match[] = {
1590         { .compatible = "ti,tvp5150", },
1591         { /* sentinel */ },
1592 };
1593 MODULE_DEVICE_TABLE(of, tvp5150_of_match);
1594 #endif
1595
1596 static struct i2c_driver tvp5150_driver = {
1597         .driver = {
1598                 .of_match_table = of_match_ptr(tvp5150_of_match),
1599                 .name   = "tvp5150",
1600         },
1601         .probe          = tvp5150_probe,
1602         .remove         = tvp5150_remove,
1603         .id_table       = tvp5150_id,
1604 };
1605
1606 module_i2c_driver(tvp5150_driver);