blob: 6c3769d44b75ccf6cc113dece96d2ab2e6244b2b [file] [log] [blame]
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001/*
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -02002 * tvp5150 - Texas Instruments TVP5150A/AM1 video decoder driver
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08003 *
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -02004 * 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
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08006 */
7
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08008#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030010#include <linux/videodev2.h>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080011#include <linux/delay.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040012#include <linux/module.h>
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -030013#include <media/v4l2-async.h>
Hans Verkuil6b8fe022008-12-18 11:17:25 -030014#include <media/v4l2-device.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020015#include <media/i2c/tvp5150.h>
Hans Verkuil6c45ec72010-12-12 08:45:43 -030016#include <media/v4l2-ctrls.h>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080017
18#include "tvp5150_reg.h"
19
Philipp Zabel785a3de2014-02-27 13:44:47 -030020#define TVP5150_H_MAX 720U
21#define TVP5150_V_MAX_525_60 480U
22#define TVP5150_V_MAX_OTHERS 576U
Javier Martin963ddc62012-01-31 05:23:46 -030023#define TVP5150_MAX_CROP_LEFT 511
24#define TVP5150_MAX_CROP_TOP 127
25#define TVP5150_CROP_SHIFT 2
26
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -020027MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080028MODULE_AUTHOR("Mauro Carvalho Chehab");
29MODULE_LICENSE("GPL");
30
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080031
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030032static int debug;
Philipp Zabel2a0489d2014-02-27 13:44:48 -030033module_param(debug, int, 0644);
Hans Verkuil6b8fe022008-12-18 11:17:25 -030034MODULE_PARM_DESC(debug, "Debug level (0-2)");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080035
36struct tvp5150 {
Hans Verkuil6b8fe022008-12-18 11:17:25 -030037 struct v4l2_subdev sd;
Hans Verkuil6c45ec72010-12-12 08:45:43 -030038 struct v4l2_ctrl_handler hdl;
Javier Martin963ddc62012-01-31 05:23:46 -030039 struct v4l2_rect rect;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080040
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020041 v4l2_std_id norm; /* Current set standard */
Hans Verkuil5325b422009-04-02 11:26:22 -030042 u32 input;
43 u32 output;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080044 int enable;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080045};
46
Hans Verkuil6b8fe022008-12-18 11:17:25 -030047static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080048{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030049 return container_of(sd, struct tvp5150, sd);
50}
51
Hans Verkuil6c45ec72010-12-12 08:45:43 -030052static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
53{
54 return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
55}
56
Hans Verkuil6b8fe022008-12-18 11:17:25 -030057static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
58{
59 struct i2c_client *c = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080060 int rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080061
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030062 rc = i2c_smbus_read_byte_data(c, addr);
63 if (rc < 0) {
64 v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
65 return rc;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -030066 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -020067
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030068 v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, rc);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080069
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030070 return rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080071}
72
Hans Verkuil6b8fe022008-12-18 11:17:25 -030073static inline void tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080074 unsigned char value)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080075{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030076 struct i2c_client *c = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080077 int rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080078
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030079 v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", addr, value);
80 rc = i2c_smbus_write_byte_data(c, addr, value);
81 if (rc < 0)
82 v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d\n", rc);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080083}
84
Hans Verkuil6b8fe022008-12-18 11:17:25 -030085static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
86 const u8 end, int max_line)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020087{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030088 int i = 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020089
Hans Verkuil6b8fe022008-12-18 11:17:25 -030090 while (init != (u8)(end + 1)) {
91 if ((i % max_line) == 0) {
92 if (i > 0)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020093 printk("\n");
Hans Verkuil6b8fe022008-12-18 11:17:25 -030094 printk("tvp5150: %s reg 0x%02x = ", s, init);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020095 }
Hans Verkuil6b8fe022008-12-18 11:17:25 -030096 printk("%02x ", tvp5150_read(sd, init));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020097
98 init++;
99 i++;
100 }
101 printk("\n");
102}
103
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300104static int tvp5150_log_status(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800105{
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800106 printk("tvp5150: Video input source selection #1 = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300107 tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800108 printk("tvp5150: Analog channel controls = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300109 tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800110 printk("tvp5150: Operation mode controls = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300111 tvp5150_read(sd, TVP5150_OP_MODE_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800112 printk("tvp5150: Miscellaneous controls = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300113 tvp5150_read(sd, TVP5150_MISC_CTL));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200114 printk("tvp5150: Autoswitch mask= 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300115 tvp5150_read(sd, TVP5150_AUTOSW_MSK));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800116 printk("tvp5150: Color killer threshold control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300117 tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200118 printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300119 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
120 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
121 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800122 printk("tvp5150: Brightness control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300123 tvp5150_read(sd, TVP5150_BRIGHT_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800124 printk("tvp5150: Color saturation control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300125 tvp5150_read(sd, TVP5150_SATURATION_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800126 printk("tvp5150: Hue control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300127 tvp5150_read(sd, TVP5150_HUE_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800128 printk("tvp5150: Contrast control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300129 tvp5150_read(sd, TVP5150_CONTRAST_CTL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800130 printk("tvp5150: Outputs and data rates select = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300131 tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800132 printk("tvp5150: Configuration shared pins = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300133 tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200134 printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300135 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
136 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200137 printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300138 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
139 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800140 printk("tvp5150: Genlock/RTC = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300141 tvp5150_read(sd, TVP5150_GENLOCK));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800142 printk("tvp5150: Horizontal sync start = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300143 tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800144 printk("tvp5150: Vertical blanking start = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300145 tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800146 printk("tvp5150: Vertical blanking stop = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300147 tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200148 printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300149 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
150 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800151 printk("tvp5150: Interrupt reset register B = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300152 tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800153 printk("tvp5150: Interrupt enable register B = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300154 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800155 printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300156 tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800157 printk("tvp5150: Video standard = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300158 tvp5150_read(sd, TVP5150_VIDEO_STD));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200159 printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300160 tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
161 tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800162 printk("tvp5150: Macrovision on counter = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300163 tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800164 printk("tvp5150: Macrovision off counter = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300165 tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200166 printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300167 (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200168 printk("tvp5150: Device ID = %02x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300169 tvp5150_read(sd, TVP5150_MSB_DEV_ID),
170 tvp5150_read(sd, TVP5150_LSB_DEV_ID));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200171 printk("tvp5150: ROM version = (hex) %02x.%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300172 tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
173 tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200174 printk("tvp5150: Vertical line count = 0x%02x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300175 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
176 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800177 printk("tvp5150: Interrupt status register B = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300178 tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800179 printk("tvp5150: Interrupt active register B = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300180 tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200181 printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300182 tvp5150_read(sd, TVP5150_STATUS_REG_1),
183 tvp5150_read(sd, TVP5150_STATUS_REG_2),
184 tvp5150_read(sd, TVP5150_STATUS_REG_3),
185 tvp5150_read(sd, TVP5150_STATUS_REG_4),
186 tvp5150_read(sd, TVP5150_STATUS_REG_5));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200187
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300188 dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
189 TVP5150_TELETEXT_FIL1_END, 8);
190 dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
191 TVP5150_TELETEXT_FIL2_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200192
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800193 printk("tvp5150: Teletext filter enable = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300194 tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800195 printk("tvp5150: Interrupt status register A = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300196 tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800197 printk("tvp5150: Interrupt enable register A = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300198 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800199 printk("tvp5150: Interrupt configuration = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300200 tvp5150_read(sd, TVP5150_INT_CONF));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800201 printk("tvp5150: VDP status register = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300202 tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800203 printk("tvp5150: FIFO word count = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300204 tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800205 printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300206 tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800207 printk("tvp5150: FIFO reset = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300208 tvp5150_read(sd, TVP5150_FIFO_RESET));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800209 printk("tvp5150: Line number interrupt = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300210 tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200211 printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300212 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
213 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800214 printk("tvp5150: FIFO output control = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300215 tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200216 printk("tvp5150: Full field enable = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300217 tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800218 printk("tvp5150: Full field mode register = 0x%02x\n",
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300219 tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200220
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300221 dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
222 TVP5150_CC_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200223
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300224 dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
225 TVP5150_WSS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200226
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300227 dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
228 TVP5150_VPS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200229
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300230 dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
231 TVP5150_VITC_DATA_END, 10);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200232
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300233 dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
234 TVP5150_LINE_MODE_END, 8);
235 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800236}
237
238/****************************************************************************
239 Basic functions
240 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800241
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300242static inline void tvp5150_selmux(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800243{
Paul Walmsley2962fc02010-10-09 01:31:40 -0300244 int opmode = 0;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300245 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300246 int input = 0;
Dan Carpenterafcc8e82012-07-12 10:47:28 -0300247 int val;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800248
Hans Verkuil5325b422009-04-02 11:26:22 -0300249 if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300250 input = 8;
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800251
Hans Verkuil5325b422009-04-02 11:26:22 -0300252 switch (decoder->input) {
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300253 case TVP5150_COMPOSITE1:
254 input |= 2;
255 /* fall through */
256 case TVP5150_COMPOSITE0:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200257 break;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300258 case TVP5150_SVIDEO:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200259 default:
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300260 input |= 1;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200261 break;
262 }
263
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300264 v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
Mauro Carvalho Chehab12500f02006-08-18 07:31:10 -0300265 "=> tvp5150 input=%i, opmode=%i\n",
Hans Verkuil5325b422009-04-02 11:26:22 -0300266 decoder->input, decoder->output,
267 input, opmode);
Mauro Carvalho Chehab12500f02006-08-18 07:31:10 -0300268
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300269 tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
270 tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300271
272 /* Svideo should enable YCrCb output and disable GPCL output
273 * For Composite and TV, it should be the reverse
274 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300275 val = tvp5150_read(sd, TVP5150_MISC_CTL);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300276 if (val < 0) {
277 v4l2_err(sd, "%s: failed with error = %d\n", __func__, val);
278 return;
279 }
280
Hans Verkuil5325b422009-04-02 11:26:22 -0300281 if (decoder->input == TVP5150_SVIDEO)
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300282 val = (val & ~0x40) | 0x10;
283 else
284 val = (val & ~0x10) | 0x40;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300285 tvp5150_write(sd, TVP5150_MISC_CTL, val);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800286};
287
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200288struct i2c_reg_value {
289 unsigned char reg;
290 unsigned char value;
291};
292
293/* Default values as sugested at TVP5150AM1 datasheet */
294static const struct i2c_reg_value tvp5150_init_default[] = {
295 { /* 0x00 */
296 TVP5150_VD_IN_SRC_SEL_1,0x00
297 },
298 { /* 0x01 */
299 TVP5150_ANAL_CHL_CTL,0x15
300 },
301 { /* 0x02 */
302 TVP5150_OP_MODE_CTL,0x00
303 },
304 { /* 0x03 */
305 TVP5150_MISC_CTL,0x01
306 },
307 { /* 0x06 */
308 TVP5150_COLOR_KIL_THSH_CTL,0x10
309 },
310 { /* 0x07 */
311 TVP5150_LUMA_PROC_CTL_1,0x60
312 },
313 { /* 0x08 */
314 TVP5150_LUMA_PROC_CTL_2,0x00
315 },
316 { /* 0x09 */
317 TVP5150_BRIGHT_CTL,0x80
318 },
319 { /* 0x0a */
320 TVP5150_SATURATION_CTL,0x80
321 },
322 { /* 0x0b */
323 TVP5150_HUE_CTL,0x00
324 },
325 { /* 0x0c */
326 TVP5150_CONTRAST_CTL,0x80
327 },
328 { /* 0x0d */
329 TVP5150_DATA_RATE_SEL,0x47
330 },
331 { /* 0x0e */
332 TVP5150_LUMA_PROC_CTL_3,0x00
333 },
334 { /* 0x0f */
335 TVP5150_CONF_SHARED_PIN,0x08
336 },
337 { /* 0x11 */
338 TVP5150_ACT_VD_CROP_ST_MSB,0x00
339 },
340 { /* 0x12 */
341 TVP5150_ACT_VD_CROP_ST_LSB,0x00
342 },
343 { /* 0x13 */
344 TVP5150_ACT_VD_CROP_STP_MSB,0x00
345 },
346 { /* 0x14 */
347 TVP5150_ACT_VD_CROP_STP_LSB,0x00
348 },
349 { /* 0x15 */
350 TVP5150_GENLOCK,0x01
351 },
352 { /* 0x16 */
353 TVP5150_HORIZ_SYNC_START,0x80
354 },
355 { /* 0x18 */
356 TVP5150_VERT_BLANKING_START,0x00
357 },
358 { /* 0x19 */
359 TVP5150_VERT_BLANKING_STOP,0x00
360 },
361 { /* 0x1a */
362 TVP5150_CHROMA_PROC_CTL_1,0x0c
363 },
364 { /* 0x1b */
365 TVP5150_CHROMA_PROC_CTL_2,0x14
366 },
367 { /* 0x1c */
368 TVP5150_INT_RESET_REG_B,0x00
369 },
370 { /* 0x1d */
371 TVP5150_INT_ENABLE_REG_B,0x00
372 },
373 { /* 0x1e */
374 TVP5150_INTT_CONFIG_REG_B,0x00
375 },
376 { /* 0x28 */
377 TVP5150_VIDEO_STD,0x00
378 },
379 { /* 0x2e */
380 TVP5150_MACROVISION_ON_CTR,0x0f
381 },
382 { /* 0x2f */
383 TVP5150_MACROVISION_OFF_CTR,0x01
384 },
385 { /* 0xbb */
386 TVP5150_TELETEXT_FIL_ENA,0x00
387 },
388 { /* 0xc0 */
389 TVP5150_INT_STATUS_REG_A,0x00
390 },
391 { /* 0xc1 */
392 TVP5150_INT_ENABLE_REG_A,0x00
393 },
394 { /* 0xc2 */
395 TVP5150_INT_CONF,0x04
396 },
397 { /* 0xc8 */
398 TVP5150_FIFO_INT_THRESHOLD,0x80
399 },
400 { /* 0xc9 */
401 TVP5150_FIFO_RESET,0x00
402 },
403 { /* 0xca */
404 TVP5150_LINE_NUMBER_INT,0x00
405 },
406 { /* 0xcb */
407 TVP5150_PIX_ALIGN_REG_LOW,0x4e
408 },
409 { /* 0xcc */
410 TVP5150_PIX_ALIGN_REG_HIGH,0x00
411 },
412 { /* 0xcd */
413 TVP5150_FIFO_OUT_CTRL,0x01
414 },
415 { /* 0xcf */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200416 TVP5150_FULL_FIELD_ENA,0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200417 },
418 { /* 0xd0 */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200419 TVP5150_LINE_MODE_INI,0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200420 },
421 { /* 0xfc */
422 TVP5150_FULL_FIELD_MODE_REG,0x7f
423 },
424 { /* end of data */
425 0xff,0xff
426 }
427};
428
429/* Default values as sugested at TVP5150AM1 datasheet */
430static const struct i2c_reg_value tvp5150_init_enable[] = {
431 {
432 TVP5150_CONF_SHARED_PIN, 2
433 },{ /* Automatic offset and AGC enabled */
434 TVP5150_ANAL_CHL_CTL, 0x15
435 },{ /* Activate YCrCb output 0x9 or 0xd ? */
436 TVP5150_MISC_CTL, 0x6f
437 },{ /* Activates video std autodetection for all standards */
438 TVP5150_AUTOSW_MSK, 0x0
439 },{ /* Default format: 0x47. For 4:2:2: 0x40 */
440 TVP5150_DATA_RATE_SEL, 0x47
441 },{
442 TVP5150_CHROMA_PROC_CTL_1, 0x0c
443 },{
444 TVP5150_CHROMA_PROC_CTL_2, 0x54
445 },{ /* Non documented, but initialized on WinTV USB2 */
446 0x27, 0x20
447 },{
448 0xff,0xff
449 }
450};
451
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200452struct tvp5150_vbi_type {
453 unsigned int vbi_type;
454 unsigned int ini_line;
455 unsigned int end_line;
456 unsigned int by_field :1;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200457};
458
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200459struct i2c_vbi_ram_value {
460 u16 reg;
461 struct tvp5150_vbi_type type;
462 unsigned char values[16];
463};
464
465/* This struct have the values for each supported VBI Standard
466 * by
467 tvp5150_vbi_types should follow the same order as vbi_ram_default
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200468 * value 0 means rom position 0x10, value 1 means rom position 0x30
469 * and so on. There are 16 possible locations from 0 to 15.
470 */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200471
Adrian Bunka9cff902006-01-15 06:56:15 -0200472static struct i2c_vbi_ram_value vbi_ram_default[] =
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200473{
Hans Verkuil9bc74002006-03-29 18:02:51 -0300474 /* FIXME: Current api doesn't handle all VBI types, those not
475 yet supported are placed under #if 0 */
476#if 0
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200477 {0x010, /* Teletext, SECAM, WST System A */
478 {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
479 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
480 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200481 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300482#endif
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200483 {0x030, /* Teletext, PAL, WST System B */
Hans Verkuil9bc74002006-03-29 18:02:51 -0300484 {V4L2_SLICED_TELETEXT_B,6,22,1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200485 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
486 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200487 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300488#if 0
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200489 {0x050, /* Teletext, PAL, WST System C */
490 {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
491 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
492 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200493 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200494 {0x070, /* Teletext, NTSC, WST System B */
495 {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
496 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
497 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200498 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200499 {0x090, /* Tetetext, NTSC NABTS System C */
500 {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
501 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
502 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200503 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200504 {0x0b0, /* Teletext, NTSC-J, NABTS System D */
505 {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
506 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
507 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200508 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200509 {0x0d0, /* Closed Caption, PAL/SECAM */
510 {V4L2_SLICED_CAPTION_625,22,22,1},
511 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
512 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200513 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300514#endif
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200515 {0x0f0, /* Closed Caption, NTSC */
516 {V4L2_SLICED_CAPTION_525,21,21,1},
517 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
518 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200519 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200520 {0x110, /* Wide Screen Signal, PAL/SECAM */
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200521 {V4L2_SLICED_WSS_625,23,23,1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200522 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
523 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200524 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300525#if 0
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200526 {0x130, /* Wide Screen Signal, NTSC C */
527 {V4L2_SLICED_WSS_525,20,20,1},
528 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
529 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200530 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200531 {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
532 {V4l2_SLICED_VITC_625,6,22,0},
533 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
534 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200535 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200536 {0x170, /* Vertical Interval Timecode (VITC), NTSC */
537 {V4l2_SLICED_VITC_525,10,20,0},
538 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
539 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200540 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300541#endif
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200542 {0x190, /* Video Program System (VPS), PAL */
543 {V4L2_SLICED_VPS,16,16,0},
544 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
545 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200546 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200547 /* 0x1d0 User programmable */
548
549 /* End of struct */
550 { (u16)-1 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200551};
552
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300553static int tvp5150_write_inittab(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200554 const struct i2c_reg_value *regs)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200555{
556 while (regs->reg != 0xff) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300557 tvp5150_write(sd, regs->reg, regs->value);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200558 regs++;
559 }
560 return 0;
561}
562
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300563static int tvp5150_vdp_init(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200564 const struct i2c_vbi_ram_value *regs)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200565{
566 unsigned int i;
567
568 /* Disable Full Field */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300569 tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200570
571 /* Before programming, Line mode should be at 0xff */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300572 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
573 tvp5150_write(sd, i, 0xff);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200574
575 /* Load Ram Table */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300576 while (regs->reg != (u16)-1) {
577 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
578 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200579
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300580 for (i = 0; i < 16; i++)
581 tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200582
583 regs++;
584 }
585 return 0;
586}
587
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200588/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300589static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200590 struct v4l2_sliced_vbi_cap *cap)
591{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300592 const struct i2c_vbi_ram_value *regs = vbi_ram_default;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200593 int line;
594
Hans Verkuilbccfa442009-03-30 06:55:27 -0300595 v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200596 memset(cap, 0, sizeof *cap);
597
598 while (regs->reg != (u16)-1 ) {
599 for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
600 cap->service_lines[0][line] |= regs->type.vbi_type;
601 }
602 cap->service_set |= regs->type.vbi_type;
603
604 regs++;
605 }
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300606 return 0;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200607}
608
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200609/* Set vbi processing
610 * type - one of tvp5150_vbi_types
611 * line - line to gather data
612 * fields: bit 0 field1, bit 1, field2
613 * flags (default=0xf0) is a bitmask, were set means:
614 * bit 7: enable filtering null bytes on CC
615 * bit 6: send data also to FIFO
616 * bit 5: don't allow data with errors on FIFO
617 * bit 4: enable ECC when possible
618 * pix_align = pix alignment:
619 * LSB = field1
620 * MSB = field2
621 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300622static int tvp5150_set_vbi(struct v4l2_subdev *sd,
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200623 const struct i2c_vbi_ram_value *regs,
624 unsigned int type,u8 flags, int line,
625 const int fields)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200626{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300627 struct tvp5150 *decoder = to_tvp5150(sd);
628 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200629 u8 reg;
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200630 int pos=0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200631
632 if (std == V4L2_STD_ALL) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300633 v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200634 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300635 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200636 /* Don't follow NTSC Line number convension */
637 line += 3;
638 }
639
640 if (line<6||line>27)
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200641 return 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200642
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200643 while (regs->reg != (u16)-1 ) {
644 if ((type & regs->type.vbi_type) &&
645 (line>=regs->type.ini_line) &&
646 (line<=regs->type.end_line)) {
647 type=regs->type.vbi_type;
648 break;
649 }
650
651 regs++;
652 pos++;
653 }
654 if (regs->reg == (u16)-1)
655 return 0;
656
657 type=pos | (flags & 0xf0);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200658 reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
659
660 if (fields&1) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300661 tvp5150_write(sd, reg, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200662 }
663
664 if (fields&2) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300665 tvp5150_write(sd, reg+1, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200666 }
667
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200668 return type;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200669}
670
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300671static int tvp5150_get_vbi(struct v4l2_subdev *sd,
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200672 const struct i2c_vbi_ram_value *regs, int line)
673{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300674 struct tvp5150 *decoder = to_tvp5150(sd);
675 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200676 u8 reg;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300677 int pos, type = 0;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300678 int i, ret = 0;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200679
680 if (std == V4L2_STD_ALL) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300681 v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200682 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300683 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200684 /* Don't follow NTSC Line number convension */
685 line += 3;
686 }
687
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300688 if (line < 6 || line > 27)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200689 return 0;
690
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300691 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200692
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300693 for (i = 0; i <= 1; i++) {
694 ret = tvp5150_read(sd, reg + i);
695 if (ret < 0) {
696 v4l2_err(sd, "%s: failed with error = %d\n",
697 __func__, ret);
698 return 0;
699 }
700 pos = ret & 0x0f;
701 if (pos < 0x0f)
702 type |= regs[pos].type.vbi_type;
703 }
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200704
705 return type;
706}
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800707
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300708static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
709{
710 struct tvp5150 *decoder = to_tvp5150(sd);
711 int fmt = 0;
712
713 decoder->norm = std;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800714
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200715 /* First tests should be against specific std */
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800716
Hans Verkuil26811ae2013-05-29 10:19:06 -0300717 if (std == V4L2_STD_NTSC_443) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300718 fmt = VIDEO_STD_NTSC_4_43_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300719 } else if (std == V4L2_STD_PAL_M) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300720 fmt = VIDEO_STD_PAL_M_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300721 } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300722 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200723 } else {
724 /* Then, test against generic ones */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300725 if (std & V4L2_STD_NTSC)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300726 fmt = VIDEO_STD_NTSC_MJ_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300727 else if (std & V4L2_STD_PAL)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300728 fmt = VIDEO_STD_PAL_BDGHIN_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300729 else if (std & V4L2_STD_SECAM)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300730 fmt = VIDEO_STD_SECAM_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200731 }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800732
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300733 v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
734 tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200735 return 0;
736}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800737
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300738static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200739{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300740 struct tvp5150 *decoder = to_tvp5150(sd);
741
742 if (decoder->norm == std)
743 return 0;
744
Javier Martin963ddc62012-01-31 05:23:46 -0300745 /* Change cropping height limits */
746 if (std & V4L2_STD_525_60)
747 decoder->rect.height = TVP5150_V_MAX_525_60;
748 else
749 decoder->rect.height = TVP5150_V_MAX_OTHERS;
750
751
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300752 return tvp5150_set_std(sd, std);
753}
754
755static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
756{
757 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200758
759 /* Initializes TVP5150 to its default values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300760 tvp5150_write_inittab(sd, tvp5150_init_default);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200761
762 /* Initializes VDP registers */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300763 tvp5150_vdp_init(sd, vbi_ram_default);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200764
765 /* Selects decoder input */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300766 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800767
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200768 /* Initializes TVP5150 to stream enabled values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300769 tvp5150_write_inittab(sd, tvp5150_init_enable);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800770
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200771 /* Initialize image preferences */
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300772 v4l2_ctrl_handler_setup(&decoder->hdl);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200773
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300774 tvp5150_set_std(sd, decoder->norm);
775 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800776};
777
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300778static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800779{
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300780 struct v4l2_subdev *sd = to_sd(ctrl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800781
782 switch (ctrl->id) {
783 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300784 tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800785 return 0;
786 case V4L2_CID_CONTRAST:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300787 tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800788 return 0;
789 case V4L2_CID_SATURATION:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300790 tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800791 return 0;
792 case V4L2_CID_HUE:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300793 tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800794 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800795 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200796 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800797}
798
Javier Martinec2c4f32012-01-05 10:57:39 -0300799static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
800{
801 int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
802
803 switch (val & 0x0F) {
804 case 0x01:
805 return V4L2_STD_NTSC;
806 case 0x03:
807 return V4L2_STD_PAL;
808 case 0x05:
809 return V4L2_STD_PAL_M;
810 case 0x07:
811 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
812 case 0x09:
813 return V4L2_STD_NTSC_443;
814 case 0xb:
815 return V4L2_STD_SECAM;
816 default:
817 return V4L2_STD_UNKNOWN;
818 }
819}
820
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300821static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
822 struct v4l2_subdev_pad_config *cfg,
823 struct v4l2_subdev_mbus_code_enum *code)
Javier Martinec2c4f32012-01-05 10:57:39 -0300824{
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300825 if (code->pad || code->index)
Javier Martinec2c4f32012-01-05 10:57:39 -0300826 return -EINVAL;
827
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300828 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
Javier Martinec2c4f32012-01-05 10:57:39 -0300829 return 0;
830}
831
Hans Verkuilda298c62015-04-09 04:02:34 -0300832static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
833 struct v4l2_subdev_pad_config *cfg,
834 struct v4l2_subdev_format *format)
Javier Martinec2c4f32012-01-05 10:57:39 -0300835{
Hans Verkuilda298c62015-04-09 04:02:34 -0300836 struct v4l2_mbus_framefmt *f;
Javier Martinec2c4f32012-01-05 10:57:39 -0300837 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martinec2c4f32012-01-05 10:57:39 -0300838
Hans Verkuilda298c62015-04-09 04:02:34 -0300839 if (!format || format->pad)
Javier Martinec2c4f32012-01-05 10:57:39 -0300840 return -EINVAL;
841
Hans Verkuilda298c62015-04-09 04:02:34 -0300842 f = &format->format;
843
Javier Martinec2c4f32012-01-05 10:57:39 -0300844 tvp5150_reset(sd, 0);
845
Javier Martin963ddc62012-01-31 05:23:46 -0300846 f->width = decoder->rect.width;
847 f->height = decoder->rect.height;
Javier Martinec2c4f32012-01-05 10:57:39 -0300848
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300849 f->code = MEDIA_BUS_FMT_UYVY8_2X8;
Javier Martinec2c4f32012-01-05 10:57:39 -0300850 f->field = V4L2_FIELD_SEQ_TB;
851 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
852
853 v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
854 f->height);
855 return 0;
856}
857
Hans Verkuil4f996592012-09-05 05:10:48 -0300858static int tvp5150_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
Javier Martin963ddc62012-01-31 05:23:46 -0300859{
860 struct v4l2_rect rect = a->c;
861 struct tvp5150 *decoder = to_tvp5150(sd);
862 v4l2_std_id std;
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -0300863 unsigned int hmax;
Javier Martin963ddc62012-01-31 05:23:46 -0300864
865 v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
866 __func__, rect.left, rect.top, rect.width, rect.height);
867
868 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
869 return -EINVAL;
870
871 /* tvp5150 has some special limits */
872 rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -0300873 rect.width = clamp_t(unsigned int, rect.width,
874 TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
875 TVP5150_H_MAX - rect.left);
Javier Martin963ddc62012-01-31 05:23:46 -0300876 rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
877
878 /* Calculate height based on current standard */
879 if (decoder->norm == V4L2_STD_ALL)
880 std = tvp5150_read_std(sd);
881 else
882 std = decoder->norm;
883
884 if (std & V4L2_STD_525_60)
885 hmax = TVP5150_V_MAX_525_60;
886 else
887 hmax = TVP5150_V_MAX_OTHERS;
888
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -0300889 rect.height = clamp_t(unsigned int, rect.height,
890 hmax - TVP5150_MAX_CROP_TOP - rect.top,
891 hmax - rect.top);
Javier Martin963ddc62012-01-31 05:23:46 -0300892
893 tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
894 tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
895 rect.top + rect.height - hmax);
896 tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
897 rect.left >> TVP5150_CROP_SHIFT);
898 tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
899 rect.left | (1 << TVP5150_CROP_SHIFT));
900 tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
901 (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
902 TVP5150_CROP_SHIFT);
903 tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
904 rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
905
906 decoder->rect = rect;
907
908 return 0;
909}
910
911static int tvp5150_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
912{
Laurent Pinchart12bd10c2014-05-15 22:53:31 -0300913 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martin963ddc62012-01-31 05:23:46 -0300914
915 a->c = decoder->rect;
916 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
917
918 return 0;
919}
920
921static int tvp5150_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
922{
Laurent Pinchart12bd10c2014-05-15 22:53:31 -0300923 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martin963ddc62012-01-31 05:23:46 -0300924 v4l2_std_id std;
925
926 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
927 return -EINVAL;
928
929 a->bounds.left = 0;
930 a->bounds.top = 0;
931 a->bounds.width = TVP5150_H_MAX;
932
933 /* Calculate height based on current standard */
934 if (decoder->norm == V4L2_STD_ALL)
935 std = tvp5150_read_std(sd);
936 else
937 std = decoder->norm;
938
939 if (std & V4L2_STD_525_60)
940 a->bounds.height = TVP5150_V_MAX_525_60;
941 else
942 a->bounds.height = TVP5150_V_MAX_OTHERS;
943
944 a->defrect = a->bounds;
945 a->pixelaspect.numerator = 1;
946 a->pixelaspect.denominator = 1;
947
948 return 0;
949}
950
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800951/****************************************************************************
952 I2C Command
953 ****************************************************************************/
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300954
Hans Verkuil5325b422009-04-02 11:26:22 -0300955static int tvp5150_s_routing(struct v4l2_subdev *sd,
956 u32 input, u32 output, u32 config)
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800957{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300958 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800959
Hans Verkuil5325b422009-04-02 11:26:22 -0300960 decoder->input = input;
961 decoder->output = output;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300962 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800963 return 0;
964}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800965
Hans Verkuild37dad42010-03-14 10:59:16 -0300966static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300967{
Hans Verkuild37dad42010-03-14 10:59:16 -0300968 /* this is for capturing 36 raw vbi lines
969 if there's a way to cut off the beginning 2 vbi lines
970 with the tvp5150 then the vbi line count could be lowered
971 to 17 lines/field again, although I couldn't find a register
972 which could do that cropping */
973 if (fmt->sample_format == V4L2_PIX_FMT_GREY)
974 tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
975 if (fmt->count[0] == 18 && fmt->count[1] == 18) {
976 tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
977 tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
978 }
979 return 0;
980}
981
982static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
983{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300984 int i;
985
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300986 if (svbi->service_set != 0) {
987 for (i = 0; i <= 23; i++) {
988 svbi->service_lines[1][i] = 0;
989 svbi->service_lines[0][i] =
990 tvp5150_set_vbi(sd, vbi_ram_default,
991 svbi->service_lines[0][i], 0xf0, i, 3);
992 }
993 /* Enables FIFO */
994 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
995 } else {
996 /* Disables FIFO*/
997 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
998
999 /* Disable Full Field */
1000 tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
1001
1002 /* Disable Line modes */
1003 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
1004 tvp5150_write(sd, i, 0xff);
1005 }
1006 return 0;
1007}
1008
Hans Verkuild37dad42010-03-14 10:59:16 -03001009static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1010{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001011 int i, mask = 0;
1012
Hans Verkuil30634e82012-09-05 10:38:10 -03001013 memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001014
1015 for (i = 0; i <= 23; i++) {
1016 svbi->service_lines[0][i] =
1017 tvp5150_get_vbi(sd, vbi_ram_default, i);
1018 mask |= svbi->service_lines[0][i];
1019 }
1020 svbi->service_set = mask;
1021 return 0;
1022}
1023
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001024#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001025static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001026{
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001027 int res;
1028
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001029 res = tvp5150_read(sd, reg->reg & 0xff);
1030 if (res < 0) {
1031 v4l2_err(sd, "%s: failed with error = %d\n", __func__, res);
1032 return res;
1033 }
1034
1035 reg->val = res;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001036 reg->size = 1;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001037 return 0;
1038}
1039
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001040static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001041{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001042 tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
1043 return 0;
1044}
1045#endif
1046
1047static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1048{
1049 int status = tvp5150_read(sd, 0x88);
1050
1051 vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1052 return 0;
1053}
1054
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001055/* ----------------------------------------------------------------------- */
1056
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001057static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1058 .s_ctrl = tvp5150_s_ctrl,
1059};
1060
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001061static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1062 .log_status = tvp5150_log_status,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001063 .reset = tvp5150_reset,
1064#ifdef CONFIG_VIDEO_ADV_DEBUG
1065 .g_register = tvp5150_g_register,
1066 .s_register = tvp5150_s_register,
1067#endif
1068};
1069
1070static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001071 .g_tuner = tvp5150_g_tuner,
1072};
1073
1074static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001075 .s_std = tvp5150_s_std,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001076 .s_routing = tvp5150_s_routing,
Javier Martin963ddc62012-01-31 05:23:46 -03001077 .s_crop = tvp5150_s_crop,
1078 .g_crop = tvp5150_g_crop,
1079 .cropcap = tvp5150_cropcap,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001080};
1081
1082static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001083 .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
Hans Verkuild37dad42010-03-14 10:59:16 -03001084 .g_sliced_fmt = tvp5150_g_sliced_fmt,
1085 .s_sliced_fmt = tvp5150_s_sliced_fmt,
1086 .s_raw_fmt = tvp5150_s_raw_fmt,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001087};
1088
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001089static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
1090 .enum_mbus_code = tvp5150_enum_mbus_code,
Hans Verkuilda298c62015-04-09 04:02:34 -03001091 .set_fmt = tvp5150_fill_fmt,
1092 .get_fmt = tvp5150_fill_fmt,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001093};
1094
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001095static const struct v4l2_subdev_ops tvp5150_ops = {
1096 .core = &tvp5150_core_ops,
1097 .tuner = &tvp5150_tuner_ops,
1098 .video = &tvp5150_video_ops,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001099 .vbi = &tvp5150_vbi_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001100 .pad = &tvp5150_pad_ops,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001101};
1102
1103
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001104/****************************************************************************
1105 I2C Client & Driver
1106 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001107
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001108static int tvp5150_probe(struct i2c_client *c,
1109 const struct i2c_device_id *id)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001110{
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001111 struct tvp5150 *core;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001112 struct v4l2_subdev *sd;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001113 int tvp5150_id[4];
1114 int i, res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001115
1116 /* Check if the adapter supports the needed features */
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001117 if (!i2c_check_functionality(c->adapter,
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001118 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001119 return -EIO;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001120
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001121 core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
1122 if (!core)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001123 return -ENOMEM;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001124 sd = &core->sd;
1125 v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001126
1127 /*
1128 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1129 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1130 */
1131 for (i = 0; i < 4; i++) {
1132 res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
1133 if (res < 0)
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001134 return res;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001135 tvp5150_id[i] = res;
1136 }
1137
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001138 v4l_info(c, "chip found @ 0x%02x (%s)\n",
1139 c->addr << 1, c->adapter->name);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001140
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001141 if (tvp5150_id[2] == 4 && tvp5150_id[3] == 0) { /* Is TVP5150AM1 */
1142 v4l2_info(sd, "tvp%02x%02xam1 detected.\n",
1143 tvp5150_id[0], tvp5150_id[1]);
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03001144
1145 /* ITU-T BT.656.4 timing */
1146 tvp5150_write(sd, TVP5150_REV_SELECT, 0);
1147 } else {
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001148 /* Is TVP5150A */
1149 if (tvp5150_id[2] == 3 || tvp5150_id[3] == 0x21) {
1150 v4l2_info(sd, "tvp%02x%02xa detected.\n",
Laurent Pinchart385dded2014-05-26 16:28:31 -03001151 tvp5150_id[0], tvp5150_id[1]);
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03001152 } else {
1153 v4l2_info(sd, "*** unknown tvp%02x%02x chip detected.\n",
Laurent Pinchart385dded2014-05-26 16:28:31 -03001154 tvp5150_id[0], tvp5150_id[1]);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001155 v4l2_info(sd, "*** Rom ver is %d.%d\n",
1156 tvp5150_id[2], tvp5150_id[3]);
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03001157 }
1158 }
1159
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -02001160 core->norm = V4L2_STD_ALL; /* Default is autodetect */
Hans Verkuil5325b422009-04-02 11:26:22 -03001161 core->input = TVP5150_COMPOSITE1;
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -08001162 core->enable = 1;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001163
1164 v4l2_ctrl_handler_init(&core->hdl, 4);
1165 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1166 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1167 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1168 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1169 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1170 V4L2_CID_SATURATION, 0, 255, 1, 128);
1171 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1172 V4L2_CID_HUE, -128, 127, 1, 0);
1173 sd->ctrl_handler = &core->hdl;
1174 if (core->hdl.error) {
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001175 res = core->hdl.error;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001176 goto err;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001177 }
1178 v4l2_ctrl_handler_setup(&core->hdl);
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -08001179
Javier Martin963ddc62012-01-31 05:23:46 -03001180 /* Default is no cropping */
1181 core->rect.top = 0;
1182 if (tvp5150_read_std(sd) & V4L2_STD_525_60)
1183 core->rect.height = TVP5150_V_MAX_525_60;
1184 else
1185 core->rect.height = TVP5150_V_MAX_OTHERS;
1186 core->rect.left = 0;
1187 core->rect.width = TVP5150_H_MAX;
1188
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001189 res = v4l2_async_register_subdev(sd);
1190 if (res < 0)
1191 goto err;
1192
Markus Rechbergerf1e5ee42006-02-07 04:01:19 +01001193 if (debug > 1)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001194 tvp5150_log_status(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001195 return 0;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001196
1197err:
1198 v4l2_ctrl_handler_free(&core->hdl);
1199 return res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001200}
1201
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001202static int tvp5150_remove(struct i2c_client *c)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001203{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001204 struct v4l2_subdev *sd = i2c_get_clientdata(c);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001205 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001206
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001207 v4l2_dbg(1, debug, sd,
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -02001208 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1209 c->addr << 1);
1210
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001211 v4l2_async_unregister_subdev(sd);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001212 v4l2_ctrl_handler_free(&decoder->hdl);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001213 return 0;
1214}
1215
1216/* ----------------------------------------------------------------------- */
1217
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001218static const struct i2c_device_id tvp5150_id[] = {
1219 { "tvp5150", 0 },
1220 { }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001221};
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001222MODULE_DEVICE_TABLE(i2c, tvp5150_id);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001223
Hans Verkuilc7711452010-09-15 15:45:20 -03001224static struct i2c_driver tvp5150_driver = {
1225 .driver = {
Hans Verkuilc7711452010-09-15 15:45:20 -03001226 .name = "tvp5150",
1227 },
1228 .probe = tvp5150_probe,
1229 .remove = tvp5150_remove,
1230 .id_table = tvp5150_id,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001231};
Hans Verkuilc7711452010-09-15 15:45:20 -03001232
Axel Linc6e8d862012-02-12 06:56:32 -03001233module_i2c_driver(tvp5150_driver);