blob: 7dda77a4e6dc0cf75f4974e361d4928407f043f0 [file] [log] [blame]
Mauro Carvalho Chehab459ee172017-12-01 08:47:12 -05001// SPDX-License-Identifier: GPL-2.0
2//
3// tvp5150 - Texas Instruments TVP5150A/AM1 and TVP5151 video decoder driver
4//
Mauro Carvalho Chehab32590812018-04-25 05:34:48 -04005// Copyright (c) 2005,2006 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08006
Javier Martinez Canillasb802fb92016-02-05 17:09:56 -02007#include <dt-bindings/media/tvp5150.h>
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>
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -020012#include <linux/gpio/consumer.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040013#include <linux/module.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030014#include <linux/of_graph.h>
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -030015#include <media/v4l2-async.h>
Hans Verkuil6b8fe022008-12-18 11:17:25 -030016#include <media/v4l2-device.h>
Hans Verkuil6c45ec72010-12-12 08:45:43 -030017#include <media/v4l2-ctrls.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030018#include <media/v4l2-fwnode.h>
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020019#include <media/v4l2-mc.h>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080020
21#include "tvp5150_reg.h"
22
Philipp Zabel785a3de2014-02-27 13:44:47 -030023#define TVP5150_H_MAX 720U
24#define TVP5150_V_MAX_525_60 480U
25#define TVP5150_V_MAX_OTHERS 576U
Javier Martin963ddc62012-01-31 05:23:46 -030026#define TVP5150_MAX_CROP_LEFT 511
27#define TVP5150_MAX_CROP_TOP 127
28#define TVP5150_CROP_SHIFT 2
29
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -020030MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080031MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab459ee172017-12-01 08:47:12 -050032MODULE_LICENSE("GPL v2");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080033
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080034
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030035static int debug;
Philipp Zabel2a0489d2014-02-27 13:44:48 -030036module_param(debug, int, 0644);
Hans Verkuil6b8fe022008-12-18 11:17:25 -030037MODULE_PARM_DESC(debug, "Debug level (0-2)");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080038
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -020039#define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
40
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040041enum tvp5150_pads {
42 TVP5150_PAD_IF_INPUT,
43 TVP5150_PAD_VID_OUT,
44 TVP5150_NUM_PADS
45};
46
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080047struct tvp5150 {
Hans Verkuil6b8fe022008-12-18 11:17:25 -030048 struct v4l2_subdev sd;
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020049#ifdef CONFIG_MEDIA_CONTROLLER
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040050 struct media_pad pads[TVP5150_NUM_PADS];
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -020051 struct media_entity input_ent[TVP5150_INPUT_NUM];
52 struct media_pad input_pad[TVP5150_INPUT_NUM];
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020053#endif
Hans Verkuil6c45ec72010-12-12 08:45:43 -030054 struct v4l2_ctrl_handler hdl;
Javier Martin963ddc62012-01-31 05:23:46 -030055 struct v4l2_rect rect;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080056
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020057 v4l2_std_id norm; /* Current set standard */
Hans Verkuil5325b422009-04-02 11:26:22 -030058 u32 input;
59 u32 output;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080060 int enable;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020061
Javier Martinez Canillas82275132016-02-05 17:09:54 -020062 u16 dev_id;
63 u16 rom_ver;
64
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020065 enum v4l2_mbus_type mbus_type;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080066};
67
Hans Verkuil6b8fe022008-12-18 11:17:25 -030068static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080069{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030070 return container_of(sd, struct tvp5150, sd);
71}
72
Hans Verkuil6c45ec72010-12-12 08:45:43 -030073static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
74{
75 return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
76}
77
Hans Verkuil6b8fe022008-12-18 11:17:25 -030078static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
79{
80 struct i2c_client *c = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080081 int rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080082
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030083 rc = i2c_smbus_read_byte_data(c, addr);
84 if (rc < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -020085 dev_err(sd->dev, "i2c i/o error: rc == %d\n", rc);
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030086 return rc;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -030087 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -020088
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -020089 dev_dbg_lvl(sd->dev, 2, debug, "tvp5150: read 0x%02x = %02x\n", addr, rc);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080090
Laurent Pincharte35ce2e2014-05-26 16:31:28 -030091 return rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080092}
93
Javier Martinez Canillascacdd6a2016-04-14 22:00:07 -030094static int tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080095 unsigned char value)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080096{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030097 struct i2c_client *c = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080098 int rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080099
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200100 dev_dbg_lvl(sd->dev, 2, debug, "tvp5150: writing %02x %02x\n", addr, value);
Laurent Pincharte35ce2e2014-05-26 16:31:28 -0300101 rc = i2c_smbus_write_byte_data(c, addr, value);
102 if (rc < 0)
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200103 dev_err(sd->dev, "i2c i/o error: rc == %d\n", rc);
Javier Martinez Canillascacdd6a2016-04-14 22:00:07 -0300104
105 return rc;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800106}
107
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300108static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
109 const u8 end, int max_line)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200110{
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200111 u8 buf[16];
112 int i = 0, j, len;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200113
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200114 if (max_line > 16) {
115 dprintk0(sd->dev, "too much data to dump\n");
116 return;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200117 }
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200118
119 for (i = init; i < end; i += max_line) {
120 len = (end - i > max_line) ? max_line : end - i;
121
122 for (j = 0; j < len; j++)
123 buf[j] = tvp5150_read(sd, i + j);
124
125 dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf);
126 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200127}
128
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300129static int tvp5150_log_status(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800130{
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200131 dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n",
132 tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
133 dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n",
134 tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
135 dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n",
136 tvp5150_read(sd, TVP5150_OP_MODE_CTL));
137 dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n",
138 tvp5150_read(sd, TVP5150_MISC_CTL));
139 dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n",
140 tvp5150_read(sd, TVP5150_AUTOSW_MSK));
141 dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n",
142 tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
143 dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
144 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
145 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
146 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
147 dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n",
148 tvp5150_read(sd, TVP5150_BRIGHT_CTL));
149 dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n",
150 tvp5150_read(sd, TVP5150_SATURATION_CTL));
151 dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n",
152 tvp5150_read(sd, TVP5150_HUE_CTL));
153 dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n",
154 tvp5150_read(sd, TVP5150_CONTRAST_CTL));
155 dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n",
156 tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
157 dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n",
158 tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
159 dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n",
160 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
161 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
162 dprintk0(sd->dev, "tvp5150: Active video cropping stop = 0x%02x%02x\n",
163 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
164 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
165 dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n",
166 tvp5150_read(sd, TVP5150_GENLOCK));
167 dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n",
168 tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
169 dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n",
170 tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
171 dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n",
172 tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
173 dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
174 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
175 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
176 dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n",
177 tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
178 dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n",
179 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
180 dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n",
181 tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
182 dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n",
183 tvp5150_read(sd, TVP5150_VIDEO_STD));
184 dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
185 tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
186 tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
187 dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n",
188 tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
189 dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n",
190 tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
191 dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
192 (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
193 dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n",
194 tvp5150_read(sd, TVP5150_MSB_DEV_ID),
195 tvp5150_read(sd, TVP5150_LSB_DEV_ID));
196 dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n",
197 tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
198 tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
199 dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n",
200 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
201 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
202 dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n",
203 tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
204 dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n",
205 tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
206 dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
207 tvp5150_read(sd, TVP5150_STATUS_REG_1),
208 tvp5150_read(sd, TVP5150_STATUS_REG_2),
209 tvp5150_read(sd, TVP5150_STATUS_REG_3),
210 tvp5150_read(sd, TVP5150_STATUS_REG_4),
211 tvp5150_read(sd, TVP5150_STATUS_REG_5));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200212
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300213 dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
214 TVP5150_TELETEXT_FIL1_END, 8);
215 dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
216 TVP5150_TELETEXT_FIL2_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200217
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200218 dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n",
219 tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
220 dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n",
221 tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
222 dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n",
223 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
224 dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n",
225 tvp5150_read(sd, TVP5150_INT_CONF));
226 dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n",
227 tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
228 dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n",
229 tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
230 dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
231 tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
232 dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n",
233 tvp5150_read(sd, TVP5150_FIFO_RESET));
234 dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n",
235 tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
236 dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
237 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
238 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
239 dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n",
240 tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
241 dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n",
242 tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
243 dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n",
244 tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200245
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300246 dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
247 TVP5150_CC_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200248
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300249 dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
250 TVP5150_WSS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200251
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300252 dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
253 TVP5150_VPS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200254
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300255 dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
256 TVP5150_VITC_DATA_END, 10);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200257
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300258 dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
259 TVP5150_LINE_MODE_END, 8);
260 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800261}
262
263/****************************************************************************
264 Basic functions
265 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800266
Laurent Pinchart6e98bee2016-12-08 20:22:42 -0200267static void tvp5150_selmux(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800268{
Paul Walmsley2962fc02010-10-09 01:31:40 -0300269 int opmode = 0;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300270 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300271 int input = 0;
Dan Carpenterafcc8e82012-07-12 10:47:28 -0300272 int val;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800273
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200274 /* Only tvp5150am1 and tvp5151 have signal generator support */
275 if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
276 (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
277 if (!decoder->enable)
278 input = 8;
279 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800280
Hans Verkuil5325b422009-04-02 11:26:22 -0300281 switch (decoder->input) {
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300282 case TVP5150_COMPOSITE1:
283 input |= 2;
284 /* fall through */
285 case TVP5150_COMPOSITE0:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200286 break;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300287 case TVP5150_SVIDEO:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200288 default:
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300289 input |= 1;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200290 break;
291 }
292
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200293 dev_dbg_lvl(sd->dev, 1, debug, "Selecting video route: route input=%i, output=%i => tvp5150 input=%i, opmode=%i\n",
Hans Verkuil5325b422009-04-02 11:26:22 -0300294 decoder->input, decoder->output,
295 input, opmode);
Mauro Carvalho Chehab12500f02006-08-18 07:31:10 -0300296
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300297 tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
298 tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300299
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200300 /*
301 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
302 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
303 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
304 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
305 * INTREQ/GPCL/VBLK to logic 1.
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300306 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300307 val = tvp5150_read(sd, TVP5150_MISC_CTL);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300308 if (val < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200309 dev_err(sd->dev, "%s: failed with error = %d\n", __func__, val);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300310 return;
311 }
312
Hans Verkuil5325b422009-04-02 11:26:22 -0300313 if (decoder->input == TVP5150_SVIDEO)
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200314 val = (val & ~TVP5150_MISC_CTL_GPCL) | TVP5150_MISC_CTL_HVLK;
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300315 else
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200316 val = (val & ~TVP5150_MISC_CTL_HVLK) | TVP5150_MISC_CTL_GPCL;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300317 tvp5150_write(sd, TVP5150_MISC_CTL, val);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800318};
319
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200320struct i2c_reg_value {
321 unsigned char reg;
322 unsigned char value;
323};
324
325/* Default values as sugested at TVP5150AM1 datasheet */
326static const struct i2c_reg_value tvp5150_init_default[] = {
327 { /* 0x00 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400328 TVP5150_VD_IN_SRC_SEL_1, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200329 },
330 { /* 0x01 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400331 TVP5150_ANAL_CHL_CTL, 0x15
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200332 },
333 { /* 0x02 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400334 TVP5150_OP_MODE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200335 },
336 { /* 0x03 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400337 TVP5150_MISC_CTL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200338 },
339 { /* 0x06 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400340 TVP5150_COLOR_KIL_THSH_CTL, 0x10
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200341 },
342 { /* 0x07 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400343 TVP5150_LUMA_PROC_CTL_1, 0x60
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200344 },
345 { /* 0x08 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400346 TVP5150_LUMA_PROC_CTL_2, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200347 },
348 { /* 0x09 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400349 TVP5150_BRIGHT_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200350 },
351 { /* 0x0a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400352 TVP5150_SATURATION_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200353 },
354 { /* 0x0b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400355 TVP5150_HUE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200356 },
357 { /* 0x0c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400358 TVP5150_CONTRAST_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200359 },
360 { /* 0x0d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400361 TVP5150_DATA_RATE_SEL, 0x47
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200362 },
363 { /* 0x0e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400364 TVP5150_LUMA_PROC_CTL_3, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200365 },
366 { /* 0x0f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400367 TVP5150_CONF_SHARED_PIN, 0x08
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200368 },
369 { /* 0x11 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400370 TVP5150_ACT_VD_CROP_ST_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200371 },
372 { /* 0x12 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400373 TVP5150_ACT_VD_CROP_ST_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200374 },
375 { /* 0x13 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400376 TVP5150_ACT_VD_CROP_STP_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200377 },
378 { /* 0x14 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400379 TVP5150_ACT_VD_CROP_STP_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200380 },
381 { /* 0x15 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400382 TVP5150_GENLOCK, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200383 },
384 { /* 0x16 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400385 TVP5150_HORIZ_SYNC_START, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200386 },
387 { /* 0x18 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400388 TVP5150_VERT_BLANKING_START, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200389 },
390 { /* 0x19 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400391 TVP5150_VERT_BLANKING_STOP, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200392 },
393 { /* 0x1a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400394 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200395 },
396 { /* 0x1b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400397 TVP5150_CHROMA_PROC_CTL_2, 0x14
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200398 },
399 { /* 0x1c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400400 TVP5150_INT_RESET_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200401 },
402 { /* 0x1d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400403 TVP5150_INT_ENABLE_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200404 },
405 { /* 0x1e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400406 TVP5150_INTT_CONFIG_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200407 },
408 { /* 0x28 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400409 TVP5150_VIDEO_STD, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200410 },
411 { /* 0x2e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400412 TVP5150_MACROVISION_ON_CTR, 0x0f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200413 },
414 { /* 0x2f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400415 TVP5150_MACROVISION_OFF_CTR, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200416 },
417 { /* 0xbb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400418 TVP5150_TELETEXT_FIL_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200419 },
420 { /* 0xc0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400421 TVP5150_INT_STATUS_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200422 },
423 { /* 0xc1 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400424 TVP5150_INT_ENABLE_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200425 },
426 { /* 0xc2 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400427 TVP5150_INT_CONF, 0x04
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200428 },
429 { /* 0xc8 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400430 TVP5150_FIFO_INT_THRESHOLD, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200431 },
432 { /* 0xc9 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400433 TVP5150_FIFO_RESET, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200434 },
435 { /* 0xca */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400436 TVP5150_LINE_NUMBER_INT, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200437 },
438 { /* 0xcb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400439 TVP5150_PIX_ALIGN_REG_LOW, 0x4e
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200440 },
441 { /* 0xcc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400442 TVP5150_PIX_ALIGN_REG_HIGH, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200443 },
444 { /* 0xcd */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400445 TVP5150_FIFO_OUT_CTRL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200446 },
447 { /* 0xcf */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400448 TVP5150_FULL_FIELD_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200449 },
450 { /* 0xd0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400451 TVP5150_LINE_MODE_INI, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200452 },
453 { /* 0xfc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400454 TVP5150_FULL_FIELD_MODE_REG, 0x7f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200455 },
456 { /* end of data */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400457 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200458 }
459};
460
461/* Default values as sugested at TVP5150AM1 datasheet */
462static const struct i2c_reg_value tvp5150_init_enable[] = {
463 {
464 TVP5150_CONF_SHARED_PIN, 2
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400465 }, { /* Automatic offset and AGC enabled */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200466 TVP5150_ANAL_CHL_CTL, 0x15
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400467 }, { /* Activate YCrCb output 0x9 or 0xd ? */
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200468 TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
469 TVP5150_MISC_CTL_INTREQ_OE |
470 TVP5150_MISC_CTL_YCBCR_OE |
471 TVP5150_MISC_CTL_SYNC_OE |
472 TVP5150_MISC_CTL_VBLANK |
473 TVP5150_MISC_CTL_CLOCK_OE,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400474 }, { /* Activates video std autodetection for all standards */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200475 TVP5150_AUTOSW_MSK, 0x0
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400476 }, { /* Default format: 0x47. For 4:2:2: 0x40 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200477 TVP5150_DATA_RATE_SEL, 0x47
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400478 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200479 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400480 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200481 TVP5150_CHROMA_PROC_CTL_2, 0x54
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400482 }, { /* Non documented, but initialized on WinTV USB2 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200483 0x27, 0x20
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400484 }, {
485 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200486 }
487};
488
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200489struct tvp5150_vbi_type {
490 unsigned int vbi_type;
491 unsigned int ini_line;
492 unsigned int end_line;
493 unsigned int by_field :1;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200494};
495
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200496struct i2c_vbi_ram_value {
497 u16 reg;
498 struct tvp5150_vbi_type type;
499 unsigned char values[16];
500};
501
502/* This struct have the values for each supported VBI Standard
503 * by
504 tvp5150_vbi_types should follow the same order as vbi_ram_default
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200505 * value 0 means rom position 0x10, value 1 means rom position 0x30
506 * and so on. There are 16 possible locations from 0 to 15.
507 */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200508
Nasser Afshin65dc7602018-04-02 18:23:19 -0400509static struct i2c_vbi_ram_value vbi_ram_default[] = {
510
Nasser Afshin9bfd8f82018-04-02 18:23:18 -0400511 /*
512 * FIXME: Current api doesn't handle all VBI types, those not
513 * yet supported are placed under #if 0
514 */
Hans Verkuil9bc74002006-03-29 18:02:51 -0300515#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500516 [0] = {0x010, /* Teletext, SECAM, WST System A */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400517 {V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200518 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
519 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200520 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300521#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500522 [1] = {0x030, /* Teletext, PAL, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400523 {V4L2_SLICED_TELETEXT_B, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200524 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
525 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200526 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300527#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500528 [2] = {0x050, /* Teletext, PAL, WST System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400529 {V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200530 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
531 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200532 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500533 [3] = {0x070, /* Teletext, NTSC, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400534 {V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200535 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
536 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200537 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500538 [4] = {0x090, /* Tetetext, NTSC NABTS System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400539 {V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200540 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
541 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200542 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500543 [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400544 {V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200545 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
546 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200547 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500548 [6] = {0x0d0, /* Closed Caption, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400549 {V4L2_SLICED_CAPTION_625, 22, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200550 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
551 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200552 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300553#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500554 [7] = {0x0f0, /* Closed Caption, NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400555 {V4L2_SLICED_CAPTION_525, 21, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200556 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
557 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200558 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500559 [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400560 {V4L2_SLICED_WSS_625, 23, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200561 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
562 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200563 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300564#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500565 [9] = {0x130, /* Wide Screen Signal, NTSC C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400566 {V4L2_SLICED_WSS_525, 20, 20, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200567 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
568 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200569 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500570 [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400571 {V4l2_SLICED_VITC_625, 6, 22, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200572 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
573 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200574 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500575 [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400576 {V4l2_SLICED_VITC_525, 10, 20, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200577 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
578 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200579 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300580#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500581 [12] = {0x190, /* Video Program System (VPS), PAL */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400582 {V4L2_SLICED_VPS, 16, 16, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200583 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
584 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200585 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200586 /* 0x1d0 User programmable */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200587};
588
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300589static int tvp5150_write_inittab(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200590 const struct i2c_reg_value *regs)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200591{
592 while (regs->reg != 0xff) {
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300593 tvp5150_write(sd, regs->reg, regs->value);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200594 regs++;
595 }
596 return 0;
597}
598
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500599static int tvp5150_vdp_init(struct v4l2_subdev *sd)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200600{
601 unsigned int i;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500602 int j;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200603
604 /* Disable Full Field */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300605 tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200606
607 /* Before programming, Line mode should be at 0xff */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300608 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
609 tvp5150_write(sd, i, 0xff);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200610
611 /* Load Ram Table */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500612 for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) {
613 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j];
614
615 if (!regs->type.vbi_type)
616 continue;
617
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300618 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
619 tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200620
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300621 for (i = 0; i < 16; i++)
622 tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200623 }
624 return 0;
625}
626
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200627/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300628static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200629 struct v4l2_sliced_vbi_cap *cap)
630{
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500631 int line, i;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200632
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200633 dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n");
Nasser Afshinbb7a3682018-04-02 18:23:20 -0400634 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200635
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500636 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
637 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
638
639 if (!regs->type.vbi_type)
640 continue;
641
642 for (line = regs->type.ini_line;
643 line <= regs->type.end_line;
644 line++) {
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200645 cap->service_lines[0][line] |= regs->type.vbi_type;
646 }
647 cap->service_set |= regs->type.vbi_type;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200648 }
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300649 return 0;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200650}
651
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200652/* Set vbi processing
653 * type - one of tvp5150_vbi_types
654 * line - line to gather data
655 * fields: bit 0 field1, bit 1, field2
656 * flags (default=0xf0) is a bitmask, were set means:
657 * bit 7: enable filtering null bytes on CC
658 * bit 6: send data also to FIFO
659 * bit 5: don't allow data with errors on FIFO
660 * bit 4: enable ECC when possible
661 * pix_align = pix alignment:
662 * LSB = field1
663 * MSB = field2
664 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300665static int tvp5150_set_vbi(struct v4l2_subdev *sd,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400666 unsigned int type, u8 flags, int line,
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200667 const int fields)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200668{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300669 struct tvp5150 *decoder = to_tvp5150(sd);
670 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200671 u8 reg;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500672 int i, pos = 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200673
674 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200675 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200676 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300677 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200678 /* Don't follow NTSC Line number convension */
679 line += 3;
680 }
681
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300682 if (line < 6 || line > 27)
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200683 return 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200684
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500685 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
686 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
687
688 if (!regs->type.vbi_type)
689 continue;
690
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200691 if ((type & regs->type.vbi_type) &&
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300692 (line >= regs->type.ini_line) &&
693 (line <= regs->type.end_line))
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200694 break;
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200695 pos++;
696 }
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300697
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300698 type = pos | (flags & 0xf0);
699 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200700
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300701 if (fields & 1)
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300702 tvp5150_write(sd, reg, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200703
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300704 if (fields & 2)
705 tvp5150_write(sd, reg + 1, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200706
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200707 return type;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200708}
709
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500710static int tvp5150_get_vbi(struct v4l2_subdev *sd, int line)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200711{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300712 struct tvp5150 *decoder = to_tvp5150(sd);
713 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200714 u8 reg;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300715 int pos, type = 0;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300716 int i, ret = 0;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200717
718 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200719 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200720 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300721 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200722 /* Don't follow NTSC Line number convension */
723 line += 3;
724 }
725
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300726 if (line < 6 || line > 27)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200727 return 0;
728
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300729 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200730
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300731 for (i = 0; i <= 1; i++) {
732 ret = tvp5150_read(sd, reg + i);
733 if (ret < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200734 dev_err(sd->dev, "%s: failed with error = %d\n",
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300735 __func__, ret);
736 return 0;
737 }
738 pos = ret & 0x0f;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500739 if (pos < ARRAY_SIZE(vbi_ram_default))
740 type |= vbi_ram_default[pos].type.vbi_type;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300741 }
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200742
743 return type;
744}
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800745
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300746static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
747{
748 struct tvp5150 *decoder = to_tvp5150(sd);
749 int fmt = 0;
750
751 decoder->norm = std;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800752
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200753 /* First tests should be against specific std */
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800754
Hans Verkuil26811ae2013-05-29 10:19:06 -0300755 if (std == V4L2_STD_NTSC_443) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300756 fmt = VIDEO_STD_NTSC_4_43_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300757 } else if (std == V4L2_STD_PAL_M) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300758 fmt = VIDEO_STD_PAL_M_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300759 } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300760 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200761 } else {
762 /* Then, test against generic ones */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300763 if (std & V4L2_STD_NTSC)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300764 fmt = VIDEO_STD_NTSC_MJ_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300765 else if (std & V4L2_STD_PAL)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300766 fmt = VIDEO_STD_PAL_BDGHIN_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300767 else if (std & V4L2_STD_SECAM)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300768 fmt = VIDEO_STD_SECAM_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200769 }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800770
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200771 dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt);
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300772 tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200773 return 0;
774}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800775
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300776static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200777{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300778 struct tvp5150 *decoder = to_tvp5150(sd);
779
780 if (decoder->norm == std)
781 return 0;
782
Javier Martin963ddc62012-01-31 05:23:46 -0300783 /* Change cropping height limits */
784 if (std & V4L2_STD_525_60)
785 decoder->rect.height = TVP5150_V_MAX_525_60;
786 else
787 decoder->rect.height = TVP5150_V_MAX_OTHERS;
788
789
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300790 return tvp5150_set_std(sd, std);
791}
792
793static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
794{
795 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200796
797 /* Initializes TVP5150 to its default values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300798 tvp5150_write_inittab(sd, tvp5150_init_default);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200799
800 /* Initializes VDP registers */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500801 tvp5150_vdp_init(sd);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200802
803 /* Selects decoder input */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300804 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800805
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200806 /* Initializes TVP5150 to stream enabled values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300807 tvp5150_write_inittab(sd, tvp5150_init_enable);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800808
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200809 /* Initialize image preferences */
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300810 v4l2_ctrl_handler_setup(&decoder->hdl);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200811
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300812 tvp5150_set_std(sd, decoder->norm);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200813
814 if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
815 tvp5150_write(sd, TVP5150_DATA_RATE_SEL, 0x40);
816
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300817 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800818};
819
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300820static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800821{
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300822 struct v4l2_subdev *sd = to_sd(ctrl);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200823 struct tvp5150 *decoder = to_tvp5150(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800824
825 switch (ctrl->id) {
826 case V4L2_CID_BRIGHTNESS:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300827 tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800828 return 0;
829 case V4L2_CID_CONTRAST:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300830 tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800831 return 0;
832 case V4L2_CID_SATURATION:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300833 tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800834 return 0;
835 case V4L2_CID_HUE:
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300836 tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
Laurent Pinchartd183e4e2016-12-08 20:22:43 -0200837 break;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200838 case V4L2_CID_TEST_PATTERN:
839 decoder->enable = ctrl->val ? false : true;
840 tvp5150_selmux(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800841 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800842 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200843 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800844}
845
Javier Martinec2c4f32012-01-05 10:57:39 -0300846static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
847{
848 int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
849
850 switch (val & 0x0F) {
851 case 0x01:
852 return V4L2_STD_NTSC;
853 case 0x03:
854 return V4L2_STD_PAL;
855 case 0x05:
856 return V4L2_STD_PAL_M;
857 case 0x07:
858 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
859 case 0x09:
860 return V4L2_STD_NTSC_443;
861 case 0xb:
862 return V4L2_STD_SECAM;
863 default:
864 return V4L2_STD_UNKNOWN;
865 }
866}
867
Hans Verkuilda298c62015-04-09 04:02:34 -0300868static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
869 struct v4l2_subdev_pad_config *cfg,
870 struct v4l2_subdev_format *format)
Javier Martinec2c4f32012-01-05 10:57:39 -0300871{
Hans Verkuilda298c62015-04-09 04:02:34 -0300872 struct v4l2_mbus_framefmt *f;
Javier Martinec2c4f32012-01-05 10:57:39 -0300873 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martinec2c4f32012-01-05 10:57:39 -0300874
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -0400875 if (!format || (format->pad != TVP5150_PAD_VID_OUT))
Javier Martinec2c4f32012-01-05 10:57:39 -0300876 return -EINVAL;
877
Hans Verkuilda298c62015-04-09 04:02:34 -0300878 f = &format->format;
879
Javier Martin963ddc62012-01-31 05:23:46 -0300880 f->width = decoder->rect.width;
Javier Martinez Canillas1831af02018-06-10 16:43:02 -0400881 f->height = decoder->rect.height / 2;
Javier Martinec2c4f32012-01-05 10:57:39 -0300882
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300883 f->code = MEDIA_BUS_FMT_UYVY8_2X8;
Laurent Pinchart4f57d27be2016-01-26 10:46:23 -0200884 f->field = V4L2_FIELD_ALTERNATE;
Javier Martinec2c4f32012-01-05 10:57:39 -0300885 f->colorspace = V4L2_COLORSPACE_SMPTE170M;
886
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200887 dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width,
Javier Martinec2c4f32012-01-05 10:57:39 -0300888 f->height);
889 return 0;
890}
891
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200892static int tvp5150_set_selection(struct v4l2_subdev *sd,
893 struct v4l2_subdev_pad_config *cfg,
894 struct v4l2_subdev_selection *sel)
Javier Martin963ddc62012-01-31 05:23:46 -0300895{
Javier Martin963ddc62012-01-31 05:23:46 -0300896 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200897 struct v4l2_rect rect = sel->r;
Javier Martin963ddc62012-01-31 05:23:46 -0300898 v4l2_std_id std;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200899 int hmax;
900
901 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
902 sel->target != V4L2_SEL_TGT_CROP)
903 return -EINVAL;
Javier Martin963ddc62012-01-31 05:23:46 -0300904
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200905 dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
Javier Martin963ddc62012-01-31 05:23:46 -0300906 __func__, rect.left, rect.top, rect.width, rect.height);
907
Javier Martin963ddc62012-01-31 05:23:46 -0300908 /* tvp5150 has some special limits */
909 rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
Javier Martin963ddc62012-01-31 05:23:46 -0300910 rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
911
912 /* Calculate height based on current standard */
913 if (decoder->norm == V4L2_STD_ALL)
914 std = tvp5150_read_std(sd);
915 else
916 std = decoder->norm;
917
918 if (std & V4L2_STD_525_60)
919 hmax = TVP5150_V_MAX_525_60;
920 else
921 hmax = TVP5150_V_MAX_OTHERS;
922
Marco Felschbd24db02018-06-28 12:20:33 -0400923 /*
924 * alignments:
925 * - width = 2 due to UYVY colorspace
926 * - height, image = no special alignment
927 */
928 v4l_bound_align_image(&rect.width,
929 TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
930 TVP5150_H_MAX - rect.left, 1, &rect.height,
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -0300931 hmax - TVP5150_MAX_CROP_TOP - rect.top,
Marco Felschbd24db02018-06-28 12:20:33 -0400932 hmax - rect.top, 0, 0);
Javier Martin963ddc62012-01-31 05:23:46 -0300933
934 tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
935 tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
936 rect.top + rect.height - hmax);
937 tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
938 rect.left >> TVP5150_CROP_SHIFT);
939 tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
940 rect.left | (1 << TVP5150_CROP_SHIFT));
941 tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
942 (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
943 TVP5150_CROP_SHIFT);
944 tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
945 rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
946
947 decoder->rect = rect;
948
949 return 0;
950}
951
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200952static int tvp5150_get_selection(struct v4l2_subdev *sd,
953 struct v4l2_subdev_pad_config *cfg,
954 struct v4l2_subdev_selection *sel)
Javier Martin963ddc62012-01-31 05:23:46 -0300955{
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200956 struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
Javier Martin963ddc62012-01-31 05:23:46 -0300957 v4l2_std_id std;
958
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200959 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
Javier Martin963ddc62012-01-31 05:23:46 -0300960 return -EINVAL;
961
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200962 switch (sel->target) {
963 case V4L2_SEL_TGT_CROP_BOUNDS:
964 case V4L2_SEL_TGT_CROP_DEFAULT:
965 sel->r.left = 0;
966 sel->r.top = 0;
967 sel->r.width = TVP5150_H_MAX;
Javier Martin963ddc62012-01-31 05:23:46 -0300968
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200969 /* Calculate height based on current standard */
970 if (decoder->norm == V4L2_STD_ALL)
971 std = tvp5150_read_std(sd);
972 else
973 std = decoder->norm;
974 if (std & V4L2_STD_525_60)
975 sel->r.height = TVP5150_V_MAX_525_60;
976 else
977 sel->r.height = TVP5150_V_MAX_OTHERS;
978 return 0;
979 case V4L2_SEL_TGT_CROP:
980 sel->r = decoder->rect;
981 return 0;
982 default:
983 return -EINVAL;
984 }
Javier Martin963ddc62012-01-31 05:23:46 -0300985}
986
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -0200987static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
988 struct v4l2_mbus_config *cfg)
989{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200990 struct tvp5150 *decoder = to_tvp5150(sd);
991
992 cfg->type = decoder->mbus_type;
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -0200993 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
994 | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
995
996 return 0;
997}
998
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800999/****************************************************************************
Laurent Pincharte545ac82016-01-26 10:46:24 -02001000 V4L2 subdev pad ops
1001 ****************************************************************************/
1002static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
1003 struct v4l2_subdev_pad_config *cfg,
1004 struct v4l2_subdev_mbus_code_enum *code)
1005{
1006 if (code->pad || code->index)
1007 return -EINVAL;
1008
1009 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
1010 return 0;
1011}
1012
1013static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
1014 struct v4l2_subdev_pad_config *cfg,
1015 struct v4l2_subdev_frame_size_enum *fse)
1016{
1017 struct tvp5150 *decoder = to_tvp5150(sd);
1018
1019 if (fse->index >= 8 || fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
1020 return -EINVAL;
1021
1022 fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
1023 fse->min_width = decoder->rect.width;
1024 fse->max_width = decoder->rect.width;
1025 fse->min_height = decoder->rect.height / 2;
1026 fse->max_height = decoder->rect.height / 2;
1027
1028 return 0;
1029}
1030
1031/****************************************************************************
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001032 Media entity ops
1033 ****************************************************************************/
1034
Laurent Pinchart406ff672016-12-08 20:22:41 -02001035#ifdef CONFIG_MEDIA_CONTROLLER
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001036static int tvp5150_link_setup(struct media_entity *entity,
1037 const struct media_pad *local,
1038 const struct media_pad *remote, u32 flags)
1039{
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001040 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1041 struct tvp5150 *decoder = to_tvp5150(sd);
1042 int i;
1043
1044 for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1045 if (remote->entity == &decoder->input_ent[i])
1046 break;
1047 }
1048
1049 /* Do nothing for entities that are not input connectors */
1050 if (i == TVP5150_INPUT_NUM)
1051 return 0;
1052
1053 decoder->input = i;
1054
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001055 tvp5150_selmux(sd);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001056
1057 return 0;
1058}
1059
1060static const struct media_entity_operations tvp5150_sd_media_ops = {
1061 .link_setup = tvp5150_link_setup,
1062};
Laurent Pinchart406ff672016-12-08 20:22:41 -02001063#endif
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001064
1065/****************************************************************************
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001066 I2C Command
1067 ****************************************************************************/
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001068
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001069static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1070{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001071 struct tvp5150 *decoder = to_tvp5150(sd);
Laurent Pinchart79d62052016-12-09 09:47:19 -02001072 int val;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001073
Laurent Pinchart79d62052016-12-09 09:47:19 -02001074 /* Enable or disable the video output signals. */
1075 val = tvp5150_read(sd, TVP5150_MISC_CTL);
1076 if (val < 0)
1077 return val;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001078
Laurent Pinchart79d62052016-12-09 09:47:19 -02001079 val &= ~(TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
1080 TVP5150_MISC_CTL_CLOCK_OE);
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001081
Laurent Pinchart79d62052016-12-09 09:47:19 -02001082 if (enable) {
1083 /*
1084 * Enable the YCbCr and clock outputs. In discrete sync mode
1085 * (non-BT.656) additionally enable the the sync outputs.
1086 */
1087 val |= TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_CLOCK_OE;
1088 if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
1089 val |= TVP5150_MISC_CTL_SYNC_OE;
1090 }
1091
1092 tvp5150_write(sd, TVP5150_MISC_CTL, val);
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001093
1094 return 0;
1095}
1096
Hans Verkuil5325b422009-04-02 11:26:22 -03001097static int tvp5150_s_routing(struct v4l2_subdev *sd,
1098 u32 input, u32 output, u32 config)
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001099{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001100 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001101
Hans Verkuil5325b422009-04-02 11:26:22 -03001102 decoder->input = input;
1103 decoder->output = output;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001104
1105 if (output == TVP5150_BLACK_SCREEN)
1106 decoder->enable = false;
1107 else
1108 decoder->enable = true;
1109
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001110 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001111 return 0;
1112}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001113
Hans Verkuild37dad42010-03-14 10:59:16 -03001114static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001115{
Nasser Afshin9bfd8f82018-04-02 18:23:18 -04001116 /*
1117 * this is for capturing 36 raw vbi lines
1118 * if there's a way to cut off the beginning 2 vbi lines
1119 * with the tvp5150 then the vbi line count could be lowered
1120 * to 17 lines/field again, although I couldn't find a register
1121 * which could do that cropping
1122 */
1123
Hans Verkuild37dad42010-03-14 10:59:16 -03001124 if (fmt->sample_format == V4L2_PIX_FMT_GREY)
1125 tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
1126 if (fmt->count[0] == 18 && fmt->count[1] == 18) {
1127 tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
1128 tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
1129 }
1130 return 0;
1131}
1132
1133static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1134{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001135 int i;
1136
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001137 if (svbi->service_set != 0) {
1138 for (i = 0; i <= 23; i++) {
1139 svbi->service_lines[1][i] = 0;
1140 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001141 tvp5150_set_vbi(sd, svbi->service_lines[0][i],
1142 0xf0, i, 3);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001143 }
1144 /* Enables FIFO */
1145 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
1146 } else {
1147 /* Disables FIFO*/
1148 tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
1149
1150 /* Disable Full Field */
1151 tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
1152
1153 /* Disable Line modes */
1154 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
1155 tvp5150_write(sd, i, 0xff);
1156 }
1157 return 0;
1158}
1159
Hans Verkuild37dad42010-03-14 10:59:16 -03001160static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1161{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001162 int i, mask = 0;
1163
Hans Verkuil30634e82012-09-05 10:38:10 -03001164 memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001165
1166 for (i = 0; i <= 23; i++) {
1167 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001168 tvp5150_get_vbi(sd, i);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001169 mask |= svbi->service_lines[0][i];
1170 }
1171 svbi->service_set = mask;
1172 return 0;
1173}
1174
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001175#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001176static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001177{
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001178 int res;
1179
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001180 res = tvp5150_read(sd, reg->reg & 0xff);
1181 if (res < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001182 dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001183 return res;
1184 }
1185
1186 reg->val = res;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001187 reg->size = 1;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001188 return 0;
1189}
1190
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001191static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001192{
Javier Martinez Canillaseca4ca82016-04-14 22:00:08 -03001193 return tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001194}
1195#endif
1196
1197static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1198{
1199 int status = tvp5150_read(sd, 0x88);
1200
1201 vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1202 return 0;
1203}
1204
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001205static int tvp5150_registered(struct v4l2_subdev *sd)
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001206{
1207#ifdef CONFIG_MEDIA_CONTROLLER
1208 struct tvp5150 *decoder = to_tvp5150(sd);
1209 int ret = 0;
1210 int i;
1211
1212 for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1213 struct media_entity *input = &decoder->input_ent[i];
1214 struct media_pad *pad = &decoder->input_pad[i];
1215
1216 if (!input->name)
1217 continue;
1218
1219 decoder->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1220
1221 ret = media_entity_pads_init(input, 1, pad);
1222 if (ret < 0)
1223 return ret;
1224
1225 ret = media_device_register_entity(sd->v4l2_dev->mdev, input);
1226 if (ret < 0)
1227 return ret;
1228
1229 ret = media_create_pad_link(input, 0, &sd->entity,
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001230 TVP5150_PAD_IF_INPUT, 0);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001231 if (ret < 0) {
1232 media_device_unregister_entity(input);
1233 return ret;
1234 }
1235 }
1236#endif
1237
1238 return 0;
1239}
1240
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001241/* ----------------------------------------------------------------------- */
1242
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001243static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1244 .s_ctrl = tvp5150_s_ctrl,
1245};
1246
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001247static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1248 .log_status = tvp5150_log_status,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001249 .reset = tvp5150_reset,
1250#ifdef CONFIG_VIDEO_ADV_DEBUG
1251 .g_register = tvp5150_g_register,
1252 .s_register = tvp5150_s_register,
1253#endif
1254};
1255
1256static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001257 .g_tuner = tvp5150_g_tuner,
1258};
1259
1260static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001261 .s_std = tvp5150_s_std,
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001262 .s_stream = tvp5150_s_stream,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001263 .s_routing = tvp5150_s_routing,
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001264 .g_mbus_config = tvp5150_g_mbus_config,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001265};
1266
1267static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001268 .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
Hans Verkuild37dad42010-03-14 10:59:16 -03001269 .g_sliced_fmt = tvp5150_g_sliced_fmt,
1270 .s_sliced_fmt = tvp5150_s_sliced_fmt,
1271 .s_raw_fmt = tvp5150_s_raw_fmt,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001272};
1273
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001274static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
1275 .enum_mbus_code = tvp5150_enum_mbus_code,
Laurent Pincharte545ac82016-01-26 10:46:24 -02001276 .enum_frame_size = tvp5150_enum_frame_size,
Hans Verkuilda298c62015-04-09 04:02:34 -03001277 .set_fmt = tvp5150_fill_fmt,
1278 .get_fmt = tvp5150_fill_fmt,
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001279 .get_selection = tvp5150_get_selection,
1280 .set_selection = tvp5150_set_selection,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001281};
1282
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001283static const struct v4l2_subdev_ops tvp5150_ops = {
1284 .core = &tvp5150_core_ops,
1285 .tuner = &tvp5150_tuner_ops,
1286 .video = &tvp5150_video_ops,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001287 .vbi = &tvp5150_vbi_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001288 .pad = &tvp5150_pad_ops,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001289};
1290
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001291static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
1292 .registered = tvp5150_registered,
1293};
1294
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001295
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001296/****************************************************************************
1297 I2C Client & Driver
1298 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001299
Laurent Pinchart78715972016-01-07 10:46:41 -02001300static int tvp5150_detect_version(struct tvp5150 *core)
1301{
1302 struct v4l2_subdev *sd = &core->sd;
1303 struct i2c_client *c = v4l2_get_subdevdata(sd);
1304 unsigned int i;
Laurent Pinchart78715972016-01-07 10:46:41 -02001305 u8 regs[4];
1306 int res;
1307
1308 /*
1309 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1310 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1311 */
1312 for (i = 0; i < 4; i++) {
1313 res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
1314 if (res < 0)
1315 return res;
1316 regs[i] = res;
1317 }
1318
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001319 core->dev_id = (regs[0] << 8) | regs[1];
1320 core->rom_ver = (regs[2] << 8) | regs[3];
Laurent Pinchart78715972016-01-07 10:46:41 -02001321
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001322 dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001323 core->dev_id, regs[2], regs[3], c->addr << 1,
1324 c->adapter->name);
Laurent Pinchart78715972016-01-07 10:46:41 -02001325
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001326 if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001327 dev_info(sd->dev, "tvp5150a detected.\n");
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001328 } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001329 dev_info(sd->dev, "tvp5150am1 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001330
1331 /* ITU-T BT.656.4 timing */
1332 tvp5150_write(sd, TVP5150_REV_SELECT, 0);
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001333 } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001334 dev_info(sd->dev, "tvp5151 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001335 } else {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001336 dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001337 core->dev_id);
Laurent Pinchart78715972016-01-07 10:46:41 -02001338 }
1339
1340 return 0;
1341}
1342
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02001343static int tvp5150_init(struct i2c_client *c)
1344{
1345 struct gpio_desc *pdn_gpio;
1346 struct gpio_desc *reset_gpio;
1347
1348 pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1349 if (IS_ERR(pdn_gpio))
1350 return PTR_ERR(pdn_gpio);
1351
1352 if (pdn_gpio) {
1353 gpiod_set_value_cansleep(pdn_gpio, 0);
1354 /* Delay time between power supplies active and reset */
1355 msleep(20);
1356 }
1357
1358 reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1359 if (IS_ERR(reset_gpio))
1360 return PTR_ERR(reset_gpio);
1361
1362 if (reset_gpio) {
1363 /* RESETB pulse duration */
1364 ndelay(500);
1365 gpiod_set_value_cansleep(reset_gpio, 0);
1366 /* Delay time between end of reset to I2C active */
1367 usleep_range(200, 250);
1368 }
1369
1370 return 0;
1371}
1372
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001373static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1374{
Sakari Ailus859969b2016-08-26 20:17:25 -03001375 struct v4l2_fwnode_endpoint bus_cfg;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001376 struct device_node *ep;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001377#ifdef CONFIG_MEDIA_CONTROLLER
1378 struct device_node *connectors, *child;
1379 struct media_entity *input;
1380 const char *name;
1381 u32 input_type;
1382#endif
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001383 unsigned int flags;
1384 int ret = 0;
1385
1386 ep = of_graph_get_next_endpoint(np, NULL);
1387 if (!ep)
1388 return -EINVAL;
1389
Sakari Ailus859969b2016-08-26 20:17:25 -03001390 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001391 if (ret)
1392 goto err;
1393
1394 flags = bus_cfg.bus.parallel.flags;
1395
1396 if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
1397 !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
1398 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
Javier Martinez Canillas2bd5e432016-02-05 17:09:53 -02001399 flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
1400 ret = -EINVAL;
1401 goto err;
1402 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001403
1404 decoder->mbus_type = bus_cfg.bus_type;
1405
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001406#ifdef CONFIG_MEDIA_CONTROLLER
1407 connectors = of_get_child_by_name(np, "connectors");
1408
1409 if (!connectors)
1410 goto err;
1411
1412 for_each_available_child_of_node(connectors, child) {
1413 ret = of_property_read_u32(child, "input", &input_type);
1414 if (ret) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001415 dev_err(decoder->sd.dev,
Rob Herringf764e6d2018-08-27 21:52:29 -04001416 "missing type property in node %pOFn\n",
1417 child);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001418 goto err_connector;
1419 }
1420
Mauro Carvalho Chehab60ad7682016-02-19 15:02:30 -02001421 if (input_type >= TVP5150_INPUT_NUM) {
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001422 ret = -EINVAL;
1423 goto err_connector;
1424 }
1425
1426 input = &decoder->input_ent[input_type];
1427
1428 /* Each input connector can only be defined once */
1429 if (input->name) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001430 dev_err(decoder->sd.dev,
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001431 "input %s with same type already exists\n",
1432 input->name);
1433 ret = -EINVAL;
1434 goto err_connector;
1435 }
1436
1437 switch (input_type) {
1438 case TVP5150_COMPOSITE0:
1439 case TVP5150_COMPOSITE1:
1440 input->function = MEDIA_ENT_F_CONN_COMPOSITE;
1441 break;
1442 case TVP5150_SVIDEO:
1443 input->function = MEDIA_ENT_F_CONN_SVIDEO;
1444 break;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001445 }
1446
1447 input->flags = MEDIA_ENT_FL_CONNECTOR;
1448
1449 ret = of_property_read_string(child, "label", &name);
1450 if (ret < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001451 dev_err(decoder->sd.dev,
Rob Herringf764e6d2018-08-27 21:52:29 -04001452 "missing label property in node %pOFn\n",
1453 child);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001454 goto err_connector;
1455 }
1456
1457 input->name = name;
1458 }
1459
1460err_connector:
1461 of_node_put(connectors);
1462#endif
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001463err:
1464 of_node_put(ep);
1465 return ret;
1466}
1467
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001468static const char * const tvp5150_test_patterns[2] = {
1469 "Disabled",
1470 "Black screen"
1471};
1472
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001473static int tvp5150_probe(struct i2c_client *c,
1474 const struct i2c_device_id *id)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001475{
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001476 struct tvp5150 *core;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001477 struct v4l2_subdev *sd;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001478 struct device_node *np = c->dev.of_node;
Laurent Pinchart78715972016-01-07 10:46:41 -02001479 int res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001480
1481 /* Check if the adapter supports the needed features */
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001482 if (!i2c_check_functionality(c->adapter,
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001483 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001484 return -EIO;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001485
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02001486 res = tvp5150_init(c);
1487 if (res)
1488 return res;
1489
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001490 core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
1491 if (!core)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001492 return -ENOMEM;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001493
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001494 sd = &core->sd;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001495
1496 if (IS_ENABLED(CONFIG_OF) && np) {
1497 res = tvp5150_parse_dt(core, np);
1498 if (res) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001499 dev_err(sd->dev, "DT parsing error: %d\n", res);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001500 return res;
1501 }
1502 } else {
1503 /* Default to BT.656 embedded sync */
1504 core->mbus_type = V4L2_MBUS_BT656;
1505 }
1506
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001507 v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001508 sd->internal_ops = &tvp5150_internal_ops;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001509 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1510
1511#if defined(CONFIG_MEDIA_CONTROLLER)
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001512 core->pads[TVP5150_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
1513 core->pads[TVP5150_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
1514 core->pads[TVP5150_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
1515 core->pads[TVP5150_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
Mauro Carvalho Chehabf92c70a2016-01-27 15:09:15 -02001516
1517 sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1518
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001519 res = media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS, core->pads);
Laurent Pincharte545ac82016-01-26 10:46:24 -02001520 if (res < 0)
1521 return res;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001522
1523 sd->entity.ops = &tvp5150_sd_media_ops;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001524#endif
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001525
Laurent Pinchart78715972016-01-07 10:46:41 -02001526 res = tvp5150_detect_version(core);
1527 if (res < 0)
1528 return res;
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03001529
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -02001530 core->norm = V4L2_STD_ALL; /* Default is autodetect */
Hans Verkuil5325b422009-04-02 11:26:22 -03001531 core->input = TVP5150_COMPOSITE1;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001532 core->enable = true;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001533
Laurent Pinchartb1950b82016-01-07 10:46:44 -02001534 v4l2_ctrl_handler_init(&core->hdl, 5);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001535 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1536 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1537 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1538 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1539 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1540 V4L2_CID_SATURATION, 0, 255, 1, 128);
1541 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1542 V4L2_CID_HUE, -128, 127, 1, 0);
Laurent Pinchartb1950b82016-01-07 10:46:44 -02001543 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1544 V4L2_CID_PIXEL_RATE, 27000000,
1545 27000000, 1, 27000000);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001546 v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
1547 V4L2_CID_TEST_PATTERN,
Mauro Carvalho Chehab5c4c4502018-09-13 16:49:51 -04001548 ARRAY_SIZE(tvp5150_test_patterns) - 1,
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001549 0, 0, tvp5150_test_patterns);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001550 sd->ctrl_handler = &core->hdl;
1551 if (core->hdl.error) {
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001552 res = core->hdl.error;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001553 goto err;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001554 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -08001555
Javier Martin963ddc62012-01-31 05:23:46 -03001556 /* Default is no cropping */
1557 core->rect.top = 0;
1558 if (tvp5150_read_std(sd) & V4L2_STD_525_60)
1559 core->rect.height = TVP5150_V_MAX_525_60;
1560 else
1561 core->rect.height = TVP5150_V_MAX_OTHERS;
1562 core->rect.left = 0;
1563 core->rect.width = TVP5150_H_MAX;
1564
Laurent Pinchartaff808e2016-12-09 09:47:17 -02001565 tvp5150_reset(sd, 0); /* Calls v4l2_ctrl_handler_setup() */
1566
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001567 res = v4l2_async_register_subdev(sd);
1568 if (res < 0)
1569 goto err;
1570
Markus Rechbergerf1e5ee42006-02-07 04:01:19 +01001571 if (debug > 1)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001572 tvp5150_log_status(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001573 return 0;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001574
1575err:
1576 v4l2_ctrl_handler_free(&core->hdl);
1577 return res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001578}
1579
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001580static int tvp5150_remove(struct i2c_client *c)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001581{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001582 struct v4l2_subdev *sd = i2c_get_clientdata(c);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001583 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001584
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001585 dev_dbg_lvl(sd->dev, 1, debug,
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -02001586 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1587 c->addr << 1);
1588
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001589 v4l2_async_unregister_subdev(sd);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001590 v4l2_ctrl_handler_free(&decoder->hdl);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001591 return 0;
1592}
1593
1594/* ----------------------------------------------------------------------- */
1595
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001596static const struct i2c_device_id tvp5150_id[] = {
1597 { "tvp5150", 0 },
1598 { }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001599};
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001600MODULE_DEVICE_TABLE(i2c, tvp5150_id);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001601
Eduard Gavin7ef930a2016-01-07 10:46:48 -02001602#if IS_ENABLED(CONFIG_OF)
1603static const struct of_device_id tvp5150_of_match[] = {
1604 { .compatible = "ti,tvp5150", },
1605 { /* sentinel */ },
1606};
1607MODULE_DEVICE_TABLE(of, tvp5150_of_match);
1608#endif
1609
Hans Verkuilc7711452010-09-15 15:45:20 -03001610static struct i2c_driver tvp5150_driver = {
1611 .driver = {
Eduard Gavin7ef930a2016-01-07 10:46:48 -02001612 .of_match_table = of_match_ptr(tvp5150_of_match),
Hans Verkuilc7711452010-09-15 15:45:20 -03001613 .name = "tvp5150",
1614 },
1615 .probe = tvp5150_probe,
1616 .remove = tvp5150_remove,
1617 .id_table = tvp5150_id,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001618};
Hans Verkuilc7711452010-09-15 15:45:20 -03001619
Axel Linc6e8d862012-02-12 06:56:32 -03001620module_i2c_driver(tvp5150_driver);