Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tvp514x.c
Go to the documentation of this file.
1 /*
2  * drivers/media/i2c/tvp514x.c
3  *
4  * TI TVP5146/47 decoder driver
5  *
6  * Copyright (C) 2008 Texas Instruments Inc
7  * Author: Vaibhav Hiremath <[email protected]>
8  *
9  * Contributors:
10  * Sivaraj R <[email protected]>
11  * Brijesh R Jadav <[email protected]>
12  * Hardik Shah <[email protected]>
13  * Manjunath Hadli <[email protected]>
14  * Karicheri Muralidharan <[email protected]>
15  *
16  * This package is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License version 2 as
18  * published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  */
30 
31 #include <linux/i2c.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include <linux/videodev2.h>
35 #include <linux/module.h>
36 
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-mediabus.h>
40 #include <media/v4l2-chip-ident.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/tvp514x.h>
43 
44 #include "tvp514x_regs.h"
45 
46 /* Module Name */
47 #define TVP514X_MODULE_NAME "tvp514x"
48 
49 /* Private macros for TVP */
50 #define I2C_RETRY_COUNT (5)
51 #define LOCK_RETRY_COUNT (5)
52 #define LOCK_RETRY_DELAY (200)
53 
54 /* Debug functions */
55 static bool debug;
56 module_param(debug, bool, 0644);
57 MODULE_PARM_DESC(debug, "Debug level (0-1)");
58 
59 MODULE_AUTHOR("Texas Instruments");
60 MODULE_DESCRIPTION("TVP514X linux decoder driver");
61 MODULE_LICENSE("GPL");
62 
63 /* enum tvp514x_std - enum for supported standards */
68 };
69 
78  unsigned long width;
79  unsigned long height;
82 };
83 
84 static struct tvp514x_reg tvp514x_reg_list_default[0x40];
85 
86 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
101  struct v4l2_subdev sd;
103  struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
105 
106  int ver;
108 
110  int num_stds;
111  const struct tvp514x_std_info *std_list;
112  /* Input and Output Routing parameters */
115 };
116 
117 /* TVP514x default register values */
118 static struct tvp514x_reg tvp514x_reg_list_default[] = {
119  /* Composite selected */
120  {TOK_WRITE, REG_INPUT_SEL, 0x05},
121  {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
122  /* Auto mode */
123  {TOK_WRITE, REG_VIDEO_STD, 0x00},
124  {TOK_WRITE, REG_OPERATION_MODE, 0x00},
125  {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
126  {TOK_WRITE, REG_COLOR_KILLER, 0x10},
127  {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
128  {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
129  {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
130  {TOK_WRITE, REG_BRIGHTNESS, 0x80},
131  {TOK_WRITE, REG_CONTRAST, 0x80},
132  {TOK_WRITE, REG_SATURATION, 0x80},
133  {TOK_WRITE, REG_HUE, 0x00},
136  /* Reserved */
137  {TOK_SKIP, 0x0F, 0x00},
141  /* Reserved */
142  {TOK_SKIP, 0x13, 0x00},
144  /* Reserved */
145  {TOK_SKIP, 0x15, 0x00},
146  /* NTSC timing */
151  /* NTSC timing */
156  /* NTSC timing */
161  /* NTSC timing */
166  /* Reserved */
167  {TOK_SKIP, 0x26, 0x00},
168  /* Reserved */
169  {TOK_SKIP, 0x27, 0x00},
171  /* Reserved */
172  {TOK_SKIP, 0x29, 0x00},
174  /* Reserved */
175  {TOK_SKIP, 0x2B, 0x00},
176  {TOK_SKIP, REG_SCART_DELAY, 0x00},
177  {TOK_SKIP, REG_CTI_DELAY, 0x00},
178  {TOK_SKIP, REG_CTI_CONTROL, 0x00},
179  /* Reserved */
180  {TOK_SKIP, 0x2F, 0x00},
181  /* Reserved */
182  {TOK_SKIP, 0x30, 0x00},
183  /* Reserved */
184  {TOK_SKIP, 0x31, 0x00},
185  /* HS, VS active high */
186  {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
187  /* 10-bit BT.656 */
189  /* Enable clk & data */
191  /* Enable AVID & FLD */
193  /* Enable VS & HS */
197  /* Clear status */
199  {TOK_TERM, 0, 0},
200 };
201 
208 static const struct tvp514x_std_info tvp514x_std_list[] = {
209  /* Standard: STD_NTSC_MJ */
210  [STD_NTSC_MJ] = {
211  .width = NTSC_NUM_ACTIVE_PIXELS,
212  .height = NTSC_NUM_ACTIVE_LINES,
213  .video_std = VIDEO_STD_NTSC_MJ_BIT,
214  .standard = {
215  .index = 0,
216  .id = V4L2_STD_NTSC,
217  .name = "NTSC",
218  .frameperiod = {1001, 30000},
219  .framelines = 525
220  },
221  /* Standard: STD_PAL_BDGHIN */
222  },
223  [STD_PAL_BDGHIN] = {
224  .width = PAL_NUM_ACTIVE_PIXELS,
225  .height = PAL_NUM_ACTIVE_LINES,
226  .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
227  .standard = {
228  .index = 1,
229  .id = V4L2_STD_PAL,
230  .name = "PAL",
231  .frameperiod = {1, 25},
232  .framelines = 625
233  },
234  },
235  /* Standard: need to add for additional standard */
236 };
237 
238 
239 static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
240 {
241  return container_of(sd, struct tvp514x_decoder, sd);
242 }
243 
244 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
245 {
246  return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
247 }
248 
249 
257 static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
258 {
259  int err, retry = 0;
260  struct i2c_client *client = v4l2_get_subdevdata(sd);
261 
262 read_again:
263 
264  err = i2c_smbus_read_byte_data(client, reg);
265  if (err < 0) {
266  if (retry <= I2C_RETRY_COUNT) {
267  v4l2_warn(sd, "Read: retry ... %d\n", retry);
268  retry++;
270  goto read_again;
271  }
272  }
273 
274  return err;
275 }
276 
282 static void dump_reg(struct v4l2_subdev *sd, u8 reg)
283 {
284  u32 val;
285 
286  val = tvp514x_read_reg(sd, reg);
287  v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
288 }
289 
299 static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
300 {
301  int err, retry = 0;
302  struct i2c_client *client = v4l2_get_subdevdata(sd);
303 
304 write_again:
305 
306  err = i2c_smbus_write_byte_data(client, reg, val);
307  if (err) {
308  if (retry <= I2C_RETRY_COUNT) {
309  v4l2_warn(sd, "Write: retry ... %d\n", retry);
310  retry++;
312  goto write_again;
313  }
314  }
315 
316  return err;
317 }
318 
331 static int tvp514x_write_regs(struct v4l2_subdev *sd,
332  const struct tvp514x_reg reglist[])
333 {
334  int err;
335  const struct tvp514x_reg *next = reglist;
336 
337  for (; next->token != TOK_TERM; next++) {
338  if (next->token == TOK_DELAY) {
339  msleep(next->val);
340  continue;
341  }
342 
343  if (next->token == TOK_SKIP)
344  continue;
345 
346  err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
347  if (err) {
348  v4l2_err(sd, "Write failed. Err[%d]\n", err);
349  return err;
350  }
351  }
352  return 0;
353 }
354 
362 static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
363 {
364  u8 std, std_status;
365 
366  std = tvp514x_read_reg(sd, REG_VIDEO_STD);
368  /* use the standard status register */
369  std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
370  else
371  /* use the standard register itself */
372  std_status = std;
373 
374  switch (std_status & VIDEO_STD_MASK) {
376  return STD_NTSC_MJ;
377 
379  return STD_PAL_BDGHIN;
380 
381  default:
382  return STD_INVALID;
383  }
384 
385  return STD_INVALID;
386 }
387 
388 /* TVP5146/47 register dump function */
389 static void tvp514x_reg_dump(struct v4l2_subdev *sd)
390 {
391  dump_reg(sd, REG_INPUT_SEL);
392  dump_reg(sd, REG_AFE_GAIN_CTRL);
393  dump_reg(sd, REG_VIDEO_STD);
394  dump_reg(sd, REG_OPERATION_MODE);
395  dump_reg(sd, REG_COLOR_KILLER);
396  dump_reg(sd, REG_LUMA_CONTROL1);
397  dump_reg(sd, REG_LUMA_CONTROL2);
398  dump_reg(sd, REG_LUMA_CONTROL3);
399  dump_reg(sd, REG_BRIGHTNESS);
400  dump_reg(sd, REG_CONTRAST);
401  dump_reg(sd, REG_SATURATION);
402  dump_reg(sd, REG_HUE);
403  dump_reg(sd, REG_CHROMA_CONTROL1);
404  dump_reg(sd, REG_CHROMA_CONTROL2);
405  dump_reg(sd, REG_COMP_PR_SATURATION);
406  dump_reg(sd, REG_COMP_Y_CONTRAST);
407  dump_reg(sd, REG_COMP_PB_SATURATION);
408  dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
409  dump_reg(sd, REG_AVID_START_PIXEL_LSB);
410  dump_reg(sd, REG_AVID_START_PIXEL_MSB);
411  dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
412  dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
413  dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
414  dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
415  dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
416  dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
417  dump_reg(sd, REG_VSYNC_START_LINE_LSB);
418  dump_reg(sd, REG_VSYNC_START_LINE_MSB);
419  dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
420  dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
421  dump_reg(sd, REG_VBLK_START_LINE_LSB);
422  dump_reg(sd, REG_VBLK_START_LINE_MSB);
423  dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
424  dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
425  dump_reg(sd, REG_SYNC_CONTROL);
426  dump_reg(sd, REG_OUTPUT_FORMATTER1);
427  dump_reg(sd, REG_OUTPUT_FORMATTER2);
428  dump_reg(sd, REG_OUTPUT_FORMATTER3);
429  dump_reg(sd, REG_OUTPUT_FORMATTER4);
430  dump_reg(sd, REG_OUTPUT_FORMATTER5);
431  dump_reg(sd, REG_OUTPUT_FORMATTER6);
432  dump_reg(sd, REG_CLEAR_LOST_LOCK);
433 }
434 
442 static int tvp514x_configure(struct v4l2_subdev *sd,
443  struct tvp514x_decoder *decoder)
444 {
445  int err;
446 
447  /* common register initialization */
448  err =
449  tvp514x_write_regs(sd, decoder->tvp514x_regs);
450  if (err)
451  return err;
452 
453  if (debug)
454  tvp514x_reg_dump(sd);
455 
456  return 0;
457 }
458 
470 static int tvp514x_detect(struct v4l2_subdev *sd,
471  struct tvp514x_decoder *decoder)
472 {
473  u8 chip_id_msb, chip_id_lsb, rom_ver;
474  struct i2c_client *client = v4l2_get_subdevdata(sd);
475 
476  chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
477  chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
478  rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
479 
480  v4l2_dbg(1, debug, sd,
481  "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
482  chip_id_msb, chip_id_lsb, rom_ver);
483  if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
484  || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
485  && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
486  /* We didn't read the values we expected, so this must not be
487  * an TVP5146/47.
488  */
489  v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
490  chip_id_msb, chip_id_lsb);
491  return -ENODEV;
492  }
493 
494  decoder->ver = rom_ver;
495 
496  v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
497  client->name, decoder->ver,
498  client->addr << 1, client->adapter->name);
499  return 0;
500 }
501 
510 static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
511 {
512  struct tvp514x_decoder *decoder = to_decoder(sd);
514  enum tvp514x_input input_sel;
515  u8 sync_lock_status, lock_mask;
516 
517  if (std_id == NULL)
518  return -EINVAL;
519 
520  *std_id = V4L2_STD_UNKNOWN;
521 
522  /* To query the standard the TVP514x must power on the ADCs. */
523  if (!decoder->streaming) {
524  tvp514x_s_stream(sd, 1);
526  }
527 
528  /* query the current standard */
529  current_std = tvp514x_query_current_std(sd);
530  if (current_std == STD_INVALID)
531  return 0;
532 
533  input_sel = decoder->input;
534 
535  switch (input_sel) {
536  case INPUT_CVBS_VI1A:
537  case INPUT_CVBS_VI1B:
538  case INPUT_CVBS_VI1C:
539  case INPUT_CVBS_VI2A:
540  case INPUT_CVBS_VI2B:
541  case INPUT_CVBS_VI2C:
542  case INPUT_CVBS_VI3A:
543  case INPUT_CVBS_VI3B:
544  case INPUT_CVBS_VI3C:
545  case INPUT_CVBS_VI4A:
546  lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
549  break;
550 
563  lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
565  break;
566  /*Need to add other interfaces*/
567  default:
568  return -EINVAL;
569  }
570  /* check whether signal is locked */
571  sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
572  if (lock_mask != (sync_lock_status & lock_mask))
573  return 0; /* No input detected */
574 
575  *std_id = decoder->std_list[current_std].standard.id;
576 
577  v4l2_dbg(1, debug, sd, "Current STD: %s\n",
578  decoder->std_list[current_std].standard.name);
579  return 0;
580 }
581 
590 static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
591 {
592  struct tvp514x_decoder *decoder = to_decoder(sd);
593  int err, i;
594 
595  for (i = 0; i < decoder->num_stds; i++)
596  if (std_id & decoder->std_list[i].standard.id)
597  break;
598 
599  if ((i == decoder->num_stds) || (i == STD_INVALID))
600  return -EINVAL;
601 
602  err = tvp514x_write_reg(sd, REG_VIDEO_STD,
603  decoder->std_list[i].video_std);
604  if (err)
605  return err;
606 
607  decoder->current_std = i;
608  decoder->tvp514x_regs[REG_VIDEO_STD].val =
609  decoder->std_list[i].video_std;
610 
611  v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
612  decoder->std_list[i].standard.name);
613  return 0;
614 }
615 
627 static int tvp514x_s_routing(struct v4l2_subdev *sd,
629 {
630  struct tvp514x_decoder *decoder = to_decoder(sd);
631  int err;
632  enum tvp514x_input input_sel;
633  enum tvp514x_output output_sel;
634 
635  if ((input >= INPUT_INVALID) ||
636  (output >= OUTPUT_INVALID))
637  /* Index out of bound */
638  return -EINVAL;
639 
640  input_sel = input;
641  output_sel = output;
642 
643  err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
644  if (err)
645  return err;
646 
647  output_sel |= tvp514x_read_reg(sd,
648  REG_OUTPUT_FORMATTER1) & 0x7;
649  err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
650  output_sel);
651  if (err)
652  return err;
653 
654  decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
655  decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
656  decoder->input = input;
657  decoder->output = output;
658 
659  v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
660 
661  return 0;
662 }
663 
671 static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
672 {
673  struct v4l2_subdev *sd = to_sd(ctrl);
674  struct tvp514x_decoder *decoder = to_decoder(sd);
675  int err = -EINVAL, value;
676 
677  value = ctrl->val;
678 
679  switch (ctrl->id) {
680  case V4L2_CID_BRIGHTNESS:
681  err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
682  if (!err)
683  decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
684  break;
685  case V4L2_CID_CONTRAST:
686  err = tvp514x_write_reg(sd, REG_CONTRAST, value);
687  if (!err)
688  decoder->tvp514x_regs[REG_CONTRAST].val = value;
689  break;
690  case V4L2_CID_SATURATION:
691  err = tvp514x_write_reg(sd, REG_SATURATION, value);
692  if (!err)
693  decoder->tvp514x_regs[REG_SATURATION].val = value;
694  break;
695  case V4L2_CID_HUE:
696  if (value == 180)
697  value = 0x7F;
698  else if (value == -180)
699  value = 0x80;
700  err = tvp514x_write_reg(sd, REG_HUE, value);
701  if (!err)
702  decoder->tvp514x_regs[REG_HUE].val = value;
703  break;
704  case V4L2_CID_AUTOGAIN:
705  err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
706  if (!err)
707  decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
708  break;
709  }
710 
711  v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
712  ctrl->id, ctrl->val);
713  return err;
714 }
715 
724 static int
725 tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
727 {
728  if (index)
729  return -EINVAL;
730 
732  return 0;
733 }
734 
742 static int
743 tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
744 {
745  struct tvp514x_decoder *decoder = to_decoder(sd);
747 
748  if (f == NULL)
749  return -EINVAL;
750 
751  /* Calculate height and width based on current standard */
752  current_std = decoder->current_std;
753 
754  f->code = V4L2_MBUS_FMT_YUYV10_2X10;
755  f->width = decoder->std_list[current_std].width;
756  f->height = decoder->std_list[current_std].height;
757  f->field = V4L2_FIELD_INTERLACED;
758  f->colorspace = V4L2_COLORSPACE_SMPTE170M;
759 
760  v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
761  f->width, f->height);
762  return 0;
763 }
764 
772 static int
773 tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
774 {
775  struct tvp514x_decoder *decoder = to_decoder(sd);
776  struct v4l2_captureparm *cparm;
777  enum tvp514x_std current_std;
778 
779  if (a == NULL)
780  return -EINVAL;
781 
783  /* only capture is supported */
784  return -EINVAL;
785 
786  /* get the current standard */
787  current_std = decoder->current_std;
788 
789  cparm = &a->parm.capture;
791  cparm->timeperframe =
792  decoder->std_list[current_std].standard.frameperiod;
793 
794  return 0;
795 }
796 
805 static int
806 tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
807 {
808  struct tvp514x_decoder *decoder = to_decoder(sd);
809  struct v4l2_fract *timeperframe;
810  enum tvp514x_std current_std;
811 
812  if (a == NULL)
813  return -EINVAL;
814 
816  /* only capture is supported */
817  return -EINVAL;
818 
819  timeperframe = &a->parm.capture.timeperframe;
820 
821  /* get the current standard */
822  current_std = decoder->current_std;
823 
824  *timeperframe =
825  decoder->std_list[current_std].standard.frameperiod;
826 
827  return 0;
828 }
829 
837 static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
838 {
839  int err = 0;
840  struct i2c_client *client = v4l2_get_subdevdata(sd);
841  struct tvp514x_decoder *decoder = to_decoder(sd);
842 
843  if (decoder->streaming == enable)
844  return 0;
845 
846  switch (enable) {
847  case 0:
848  {
849  /* Power Down Sequence */
850  err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
851  if (err) {
852  v4l2_err(sd, "Unable to turn off decoder\n");
853  return err;
854  }
855  decoder->streaming = enable;
856  break;
857  }
858  case 1:
859  {
860  struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
861  client->driver->id_table->driver_data;
862 
863  /* Power Up Sequence */
864  err = tvp514x_write_regs(sd, int_seq);
865  if (err) {
866  v4l2_err(sd, "Unable to turn on decoder\n");
867  return err;
868  }
869  /* Detect if not already detected */
870  err = tvp514x_detect(sd, decoder);
871  if (err) {
872  v4l2_err(sd, "Unable to detect decoder\n");
873  return err;
874  }
875  err = tvp514x_configure(sd, decoder);
876  if (err) {
877  v4l2_err(sd, "Unable to configure decoder\n");
878  return err;
879  }
880  decoder->streaming = enable;
881  break;
882  }
883  default:
884  err = -ENODEV;
885  break;
886  }
887 
888  return err;
889 }
890 
891 static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
892  .s_ctrl = tvp514x_s_ctrl,
893 };
894 
895 static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
897  .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
898  .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
899  .g_ctrl = v4l2_subdev_g_ctrl,
900  .s_ctrl = v4l2_subdev_s_ctrl,
901  .queryctrl = v4l2_subdev_queryctrl,
902  .querymenu = v4l2_subdev_querymenu,
903  .s_std = tvp514x_s_std,
904 };
905 
906 static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
907  .s_routing = tvp514x_s_routing,
908  .querystd = tvp514x_querystd,
909  .enum_mbus_fmt = tvp514x_enum_mbus_fmt,
910  .g_mbus_fmt = tvp514x_mbus_fmt,
911  .try_mbus_fmt = tvp514x_mbus_fmt,
912  .s_mbus_fmt = tvp514x_mbus_fmt,
913  .g_parm = tvp514x_g_parm,
914  .s_parm = tvp514x_s_parm,
915  .s_stream = tvp514x_s_stream,
916 };
917 
918 static const struct v4l2_subdev_ops tvp514x_ops = {
919  .core = &tvp514x_core_ops,
920  .video = &tvp514x_video_ops,
921 };
922 
923 static struct tvp514x_decoder tvp514x_dev = {
924  .streaming = 0,
925  .current_std = STD_NTSC_MJ,
926  .std_list = tvp514x_std_list,
927  .num_stds = ARRAY_SIZE(tvp514x_std_list),
928 
929 };
930 
939 static int
940 tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
941 {
942  struct tvp514x_decoder *decoder;
943  struct v4l2_subdev *sd;
944 
945  /* Check if the adapter supports the needed features */
946  if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
947  return -EIO;
948 
949  if (!client->dev.platform_data) {
950  v4l2_err(client, "No platform data!!\n");
951  return -ENODEV;
952  }
953 
954  decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
955  if (!decoder)
956  return -ENOMEM;
957 
958  /* Initialize the tvp514x_decoder with default configuration */
959  *decoder = tvp514x_dev;
960  /* Copy default register configuration */
961  memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
962  sizeof(tvp514x_reg_list_default));
963 
964  /* Copy board specific information here */
965  decoder->pdata = client->dev.platform_data;
966 
972  decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
973  (decoder->pdata->clk_polarity << 1);
974  decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
975  ((decoder->pdata->hs_polarity << 2) |
976  (decoder->pdata->vs_polarity << 3));
977  /* Set default standard to auto */
978  decoder->tvp514x_regs[REG_VIDEO_STD].val =
980 
981  /* Register with V4L2 layer as slave device */
982  sd = &decoder->sd;
983  v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
984 
985  v4l2_ctrl_handler_init(&decoder->hdl, 5);
986  v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
987  V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
988  v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
989  V4L2_CID_CONTRAST, 0, 255, 1, 128);
990  v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
991  V4L2_CID_SATURATION, 0, 255, 1, 128);
992  v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
993  V4L2_CID_HUE, -180, 180, 180, 0);
994  v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
995  V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
996  sd->ctrl_handler = &decoder->hdl;
997  if (decoder->hdl.error) {
998  int err = decoder->hdl.error;
999 
1000  v4l2_ctrl_handler_free(&decoder->hdl);
1001  kfree(decoder);
1002  return err;
1003  }
1004  v4l2_ctrl_handler_setup(&decoder->hdl);
1005 
1006  v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1007 
1008  return 0;
1009 
1010 }
1011 
1019 static int tvp514x_remove(struct i2c_client *client)
1020 {
1021  struct v4l2_subdev *sd = i2c_get_clientdata(client);
1022  struct tvp514x_decoder *decoder = to_decoder(sd);
1023 
1025  v4l2_ctrl_handler_free(&decoder->hdl);
1026  kfree(decoder);
1027  return 0;
1028 }
1029 /* TVP5146 Init/Power on Sequence */
1030 static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1040  {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1041  {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1042  {TOK_TERM, 0, 0},
1043 };
1044 
1045 /* TVP5147 Init/Power on Sequence */
1046 static const struct tvp514x_reg tvp5147_init_reg_seq[] = {
1063  {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1064  {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1065  {TOK_TERM, 0, 0},
1066 };
1067 
1068 /* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1069 static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1070  {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1071  {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1072  {TOK_TERM, 0, 0},
1073 };
1074 
1081 static const struct i2c_device_id tvp514x_id[] = {
1082  {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1083  {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1084  {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1085  {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1086  {},
1087 };
1088 
1089 MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1090 
1091 static struct i2c_driver tvp514x_driver = {
1092  .driver = {
1093  .owner = THIS_MODULE,
1094  .name = TVP514X_MODULE_NAME,
1095  },
1096  .probe = tvp514x_probe,
1097  .remove = tvp514x_remove,
1098  .id_table = tvp514x_id,
1099 };
1100 
1101 module_i2c_driver(tvp514x_driver);