blob: 28385a1f6b1bcaa770f802ee5f9768e33f19e04d [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>
Philipp Zabel8e4c97e2018-06-28 12:20:44 -040013#include <linux/interrupt.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040014#include <linux/module.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030015#include <linux/of_graph.h>
Philipp Zabel28b9e222018-06-28 12:20:35 -040016#include <linux/regmap.h>
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -030017#include <media/v4l2-async.h>
Hans Verkuil6b8fe022008-12-18 11:17:25 -030018#include <media/v4l2-device.h>
Hans Verkuil6c45ec72010-12-12 08:45:43 -030019#include <media/v4l2-ctrls.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030020#include <media/v4l2-fwnode.h>
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020021#include <media/v4l2-mc.h>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080022
23#include "tvp5150_reg.h"
24
Philipp Zabel785a3de2014-02-27 13:44:47 -030025#define TVP5150_H_MAX 720U
26#define TVP5150_V_MAX_525_60 480U
27#define TVP5150_V_MAX_OTHERS 576U
Javier Martin963ddc62012-01-31 05:23:46 -030028#define TVP5150_MAX_CROP_LEFT 511
29#define TVP5150_MAX_CROP_TOP 127
30#define TVP5150_CROP_SHIFT 2
Marco Felsch5cb82942018-06-28 12:20:39 -040031#define TVP5150_MBUS_FMT MEDIA_BUS_FMT_UYVY8_2X8
32#define TVP5150_FIELD V4L2_FIELD_ALTERNATE
33#define TVP5150_COLORSPACE V4L2_COLORSPACE_SMPTE170M
Javier Martin963ddc62012-01-31 05:23:46 -030034
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -020035MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080036MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab459ee172017-12-01 08:47:12 -050037MODULE_LICENSE("GPL v2");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080038
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080039
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030040static int debug;
Philipp Zabel2a0489d2014-02-27 13:44:48 -030041module_param(debug, int, 0644);
Hans Verkuil6b8fe022008-12-18 11:17:25 -030042MODULE_PARM_DESC(debug, "Debug level (0-2)");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080043
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -020044#define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
45
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040046enum tvp5150_pads {
47 TVP5150_PAD_IF_INPUT,
48 TVP5150_PAD_VID_OUT,
49 TVP5150_NUM_PADS
50};
51
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080052struct tvp5150 {
Hans Verkuil6b8fe022008-12-18 11:17:25 -030053 struct v4l2_subdev sd;
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020054#ifdef CONFIG_MEDIA_CONTROLLER
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040055 struct media_pad pads[TVP5150_NUM_PADS];
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -020056 struct media_entity input_ent[TVP5150_INPUT_NUM];
57 struct media_pad input_pad[TVP5150_INPUT_NUM];
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020058#endif
Hans Verkuil6c45ec72010-12-12 08:45:43 -030059 struct v4l2_ctrl_handler hdl;
Javier Martin963ddc62012-01-31 05:23:46 -030060 struct v4l2_rect rect;
Philipp Zabel28b9e222018-06-28 12:20:35 -040061 struct regmap *regmap;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -040062 int irq;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080063
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020064 v4l2_std_id norm; /* Current set standard */
Philipp Zabelb440b732018-06-28 12:20:40 -040065 v4l2_std_id detected_norm;
Hans Verkuil5325b422009-04-02 11:26:22 -030066 u32 input;
67 u32 output;
Philipp Zabel62a764e2018-06-28 12:20:45 -040068 u32 oe;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080069 int enable;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -040070 bool lock;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020071
Javier Martinez Canillas82275132016-02-05 17:09:54 -020072 u16 dev_id;
73 u16 rom_ver;
74
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020075 enum v4l2_mbus_type mbus_type;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080076};
77
Hans Verkuil6b8fe022008-12-18 11:17:25 -030078static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080079{
Hans Verkuil6b8fe022008-12-18 11:17:25 -030080 return container_of(sd, struct tvp5150, sd);
81}
82
Hans Verkuil6c45ec72010-12-12 08:45:43 -030083static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
84{
85 return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
86}
87
Hans Verkuil6b8fe022008-12-18 11:17:25 -030088static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
89{
Philipp Zabel28b9e222018-06-28 12:20:35 -040090 struct tvp5150 *decoder = to_tvp5150(sd);
91 int ret, val;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080092
Philipp Zabel28b9e222018-06-28 12:20:35 -040093 ret = regmap_read(decoder->regmap, addr, &val);
94 if (ret < 0)
95 return ret;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -020096
Philipp Zabel28b9e222018-06-28 12:20:35 -040097 return val;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080098}
99
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300100static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
101 const u8 end, int max_line)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200102{
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200103 u8 buf[16];
104 int i = 0, j, len;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200105
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200106 if (max_line > 16) {
107 dprintk0(sd->dev, "too much data to dump\n");
108 return;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200109 }
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200110
111 for (i = init; i < end; i += max_line) {
112 len = (end - i > max_line) ? max_line : end - i;
113
114 for (j = 0; j < len; j++)
115 buf[j] = tvp5150_read(sd, i + j);
116
117 dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf);
118 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200119}
120
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300121static int tvp5150_log_status(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800122{
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200123 dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n",
124 tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
125 dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n",
126 tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
127 dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n",
128 tvp5150_read(sd, TVP5150_OP_MODE_CTL));
129 dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n",
130 tvp5150_read(sd, TVP5150_MISC_CTL));
131 dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n",
132 tvp5150_read(sd, TVP5150_AUTOSW_MSK));
133 dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n",
134 tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
135 dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
136 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
137 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
138 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
139 dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n",
140 tvp5150_read(sd, TVP5150_BRIGHT_CTL));
141 dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n",
142 tvp5150_read(sd, TVP5150_SATURATION_CTL));
143 dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n",
144 tvp5150_read(sd, TVP5150_HUE_CTL));
145 dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n",
146 tvp5150_read(sd, TVP5150_CONTRAST_CTL));
147 dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n",
148 tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
149 dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n",
150 tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
151 dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n",
152 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
153 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
154 dprintk0(sd->dev, "tvp5150: Active video cropping stop = 0x%02x%02x\n",
155 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
156 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
157 dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n",
158 tvp5150_read(sd, TVP5150_GENLOCK));
159 dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n",
160 tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
161 dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n",
162 tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
163 dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n",
164 tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
165 dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
166 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
167 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
168 dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n",
169 tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
170 dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n",
171 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
172 dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n",
173 tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
174 dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n",
175 tvp5150_read(sd, TVP5150_VIDEO_STD));
176 dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
177 tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
178 tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
179 dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n",
180 tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
181 dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n",
182 tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
183 dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
184 (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
185 dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n",
186 tvp5150_read(sd, TVP5150_MSB_DEV_ID),
187 tvp5150_read(sd, TVP5150_LSB_DEV_ID));
188 dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n",
189 tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
190 tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
191 dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n",
192 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
193 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
194 dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n",
195 tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
196 dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n",
197 tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
198 dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
199 tvp5150_read(sd, TVP5150_STATUS_REG_1),
200 tvp5150_read(sd, TVP5150_STATUS_REG_2),
201 tvp5150_read(sd, TVP5150_STATUS_REG_3),
202 tvp5150_read(sd, TVP5150_STATUS_REG_4),
203 tvp5150_read(sd, TVP5150_STATUS_REG_5));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200204
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300205 dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
206 TVP5150_TELETEXT_FIL1_END, 8);
207 dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
208 TVP5150_TELETEXT_FIL2_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200209
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200210 dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n",
211 tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
212 dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n",
213 tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
214 dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n",
215 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
216 dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n",
217 tvp5150_read(sd, TVP5150_INT_CONF));
218 dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n",
219 tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
220 dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n",
221 tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
222 dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
223 tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
224 dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n",
225 tvp5150_read(sd, TVP5150_FIFO_RESET));
226 dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n",
227 tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
228 dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
229 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
230 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
231 dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n",
232 tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
233 dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n",
234 tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
235 dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n",
236 tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200237
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300238 dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
239 TVP5150_CC_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200240
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300241 dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
242 TVP5150_WSS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200243
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300244 dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
245 TVP5150_VPS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200246
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300247 dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
248 TVP5150_VITC_DATA_END, 10);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200249
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300250 dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
251 TVP5150_LINE_MODE_END, 8);
252 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800253}
254
255/****************************************************************************
256 Basic functions
257 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800258
Laurent Pinchart6e98bee2016-12-08 20:22:42 -0200259static void tvp5150_selmux(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800260{
Paul Walmsley2962fc02010-10-09 01:31:40 -0300261 int opmode = 0;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300262 struct tvp5150 *decoder = to_tvp5150(sd);
Marco Felsch8a7441b2018-06-28 12:20:36 -0400263 unsigned int mask, val;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300264 int input = 0;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800265
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200266 /* Only tvp5150am1 and tvp5151 have signal generator support */
267 if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
268 (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
269 if (!decoder->enable)
270 input = 8;
271 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800272
Hans Verkuil5325b422009-04-02 11:26:22 -0300273 switch (decoder->input) {
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300274 case TVP5150_COMPOSITE1:
275 input |= 2;
276 /* fall through */
277 case TVP5150_COMPOSITE0:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200278 break;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300279 case TVP5150_SVIDEO:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200280 default:
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300281 input |= 1;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200282 break;
283 }
284
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200285 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 -0300286 decoder->input, decoder->output,
287 input, opmode);
Mauro Carvalho Chehab12500f02006-08-18 07:31:10 -0300288
Philipp Zabel28b9e222018-06-28 12:20:35 -0400289 regmap_write(decoder->regmap, TVP5150_OP_MODE_CTL, opmode);
290 regmap_write(decoder->regmap, TVP5150_VD_IN_SRC_SEL_1, input);
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300291
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200292 /*
293 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
294 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
295 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
296 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
297 * INTREQ/GPCL/VBLK to logic 1.
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300298 */
Marco Felsch8a7441b2018-06-28 12:20:36 -0400299 mask = TVP5150_MISC_CTL_GPCL | TVP5150_MISC_CTL_HVLK;
Hans Verkuil5325b422009-04-02 11:26:22 -0300300 if (decoder->input == TVP5150_SVIDEO)
Marco Felsch8a7441b2018-06-28 12:20:36 -0400301 val = TVP5150_MISC_CTL_HVLK;
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300302 else
Marco Felsch8a7441b2018-06-28 12:20:36 -0400303 val = TVP5150_MISC_CTL_GPCL;
304 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800305};
306
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200307struct i2c_reg_value {
308 unsigned char reg;
309 unsigned char value;
310};
311
312/* Default values as sugested at TVP5150AM1 datasheet */
313static const struct i2c_reg_value tvp5150_init_default[] = {
314 { /* 0x00 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400315 TVP5150_VD_IN_SRC_SEL_1, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200316 },
317 { /* 0x01 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400318 TVP5150_ANAL_CHL_CTL, 0x15
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200319 },
320 { /* 0x02 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400321 TVP5150_OP_MODE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200322 },
323 { /* 0x03 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400324 TVP5150_MISC_CTL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200325 },
326 { /* 0x06 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400327 TVP5150_COLOR_KIL_THSH_CTL, 0x10
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200328 },
329 { /* 0x07 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400330 TVP5150_LUMA_PROC_CTL_1, 0x60
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200331 },
332 { /* 0x08 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400333 TVP5150_LUMA_PROC_CTL_2, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200334 },
335 { /* 0x09 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400336 TVP5150_BRIGHT_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200337 },
338 { /* 0x0a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400339 TVP5150_SATURATION_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200340 },
341 { /* 0x0b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400342 TVP5150_HUE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200343 },
344 { /* 0x0c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400345 TVP5150_CONTRAST_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200346 },
347 { /* 0x0d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400348 TVP5150_DATA_RATE_SEL, 0x47
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200349 },
350 { /* 0x0e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400351 TVP5150_LUMA_PROC_CTL_3, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200352 },
353 { /* 0x0f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400354 TVP5150_CONF_SHARED_PIN, 0x08
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200355 },
356 { /* 0x11 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400357 TVP5150_ACT_VD_CROP_ST_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200358 },
359 { /* 0x12 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400360 TVP5150_ACT_VD_CROP_ST_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200361 },
362 { /* 0x13 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400363 TVP5150_ACT_VD_CROP_STP_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200364 },
365 { /* 0x14 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400366 TVP5150_ACT_VD_CROP_STP_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200367 },
368 { /* 0x15 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400369 TVP5150_GENLOCK, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200370 },
371 { /* 0x16 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400372 TVP5150_HORIZ_SYNC_START, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200373 },
374 { /* 0x18 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400375 TVP5150_VERT_BLANKING_START, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200376 },
377 { /* 0x19 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400378 TVP5150_VERT_BLANKING_STOP, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200379 },
380 { /* 0x1a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400381 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200382 },
383 { /* 0x1b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400384 TVP5150_CHROMA_PROC_CTL_2, 0x14
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200385 },
386 { /* 0x1c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400387 TVP5150_INT_RESET_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200388 },
389 { /* 0x1d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400390 TVP5150_INT_ENABLE_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200391 },
392 { /* 0x1e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400393 TVP5150_INTT_CONFIG_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200394 },
395 { /* 0x28 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400396 TVP5150_VIDEO_STD, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200397 },
398 { /* 0x2e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400399 TVP5150_MACROVISION_ON_CTR, 0x0f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200400 },
401 { /* 0x2f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400402 TVP5150_MACROVISION_OFF_CTR, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200403 },
404 { /* 0xbb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400405 TVP5150_TELETEXT_FIL_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200406 },
407 { /* 0xc0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400408 TVP5150_INT_STATUS_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200409 },
410 { /* 0xc1 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400411 TVP5150_INT_ENABLE_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200412 },
413 { /* 0xc2 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400414 TVP5150_INT_CONF, 0x04
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200415 },
416 { /* 0xc8 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400417 TVP5150_FIFO_INT_THRESHOLD, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200418 },
419 { /* 0xc9 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400420 TVP5150_FIFO_RESET, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200421 },
422 { /* 0xca */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400423 TVP5150_LINE_NUMBER_INT, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200424 },
425 { /* 0xcb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400426 TVP5150_PIX_ALIGN_REG_LOW, 0x4e
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200427 },
428 { /* 0xcc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400429 TVP5150_PIX_ALIGN_REG_HIGH, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200430 },
431 { /* 0xcd */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400432 TVP5150_FIFO_OUT_CTRL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200433 },
434 { /* 0xcf */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400435 TVP5150_FULL_FIELD_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200436 },
437 { /* 0xd0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400438 TVP5150_LINE_MODE_INI, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200439 },
440 { /* 0xfc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400441 TVP5150_FULL_FIELD_MODE_REG, 0x7f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200442 },
443 { /* end of data */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400444 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200445 }
446};
447
448/* Default values as sugested at TVP5150AM1 datasheet */
449static const struct i2c_reg_value tvp5150_init_enable[] = {
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400450 { /* Automatic offset and AGC enabled */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200451 TVP5150_ANAL_CHL_CTL, 0x15
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400452 }, { /* Activate YCrCb output 0x9 or 0xd ? */
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200453 TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
454 TVP5150_MISC_CTL_INTREQ_OE |
455 TVP5150_MISC_CTL_YCBCR_OE |
456 TVP5150_MISC_CTL_SYNC_OE |
457 TVP5150_MISC_CTL_VBLANK |
458 TVP5150_MISC_CTL_CLOCK_OE,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400459 }, { /* Activates video std autodetection for all standards */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200460 TVP5150_AUTOSW_MSK, 0x0
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400461 }, { /* Default format: 0x47. For 4:2:2: 0x40 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200462 TVP5150_DATA_RATE_SEL, 0x47
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400463 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200464 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400465 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200466 TVP5150_CHROMA_PROC_CTL_2, 0x54
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400467 }, { /* Non documented, but initialized on WinTV USB2 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200468 0x27, 0x20
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400469 }, {
470 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200471 }
472};
473
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200474struct tvp5150_vbi_type {
475 unsigned int vbi_type;
476 unsigned int ini_line;
477 unsigned int end_line;
478 unsigned int by_field :1;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200479};
480
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200481struct i2c_vbi_ram_value {
482 u16 reg;
483 struct tvp5150_vbi_type type;
484 unsigned char values[16];
485};
486
487/* This struct have the values for each supported VBI Standard
488 * by
489 tvp5150_vbi_types should follow the same order as vbi_ram_default
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200490 * value 0 means rom position 0x10, value 1 means rom position 0x30
491 * and so on. There are 16 possible locations from 0 to 15.
492 */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200493
Nasser Afshin65dc7602018-04-02 18:23:19 -0400494static struct i2c_vbi_ram_value vbi_ram_default[] = {
495
Nasser Afshin9bfd8f82018-04-02 18:23:18 -0400496 /*
497 * FIXME: Current api doesn't handle all VBI types, those not
498 * yet supported are placed under #if 0
499 */
Hans Verkuil9bc74002006-03-29 18:02:51 -0300500#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500501 [0] = {0x010, /* Teletext, SECAM, WST System A */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400502 {V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200503 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
504 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200505 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300506#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500507 [1] = {0x030, /* Teletext, PAL, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400508 {V4L2_SLICED_TELETEXT_B, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200509 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
510 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200511 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300512#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500513 [2] = {0x050, /* Teletext, PAL, WST System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400514 {V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200515 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
516 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200517 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500518 [3] = {0x070, /* Teletext, NTSC, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400519 {V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200520 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
521 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200522 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500523 [4] = {0x090, /* Tetetext, NTSC NABTS System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400524 {V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200525 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
526 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200527 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500528 [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400529 {V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200530 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
531 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200532 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500533 [6] = {0x0d0, /* Closed Caption, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400534 {V4L2_SLICED_CAPTION_625, 22, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200535 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
536 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200537 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300538#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500539 [7] = {0x0f0, /* Closed Caption, NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400540 {V4L2_SLICED_CAPTION_525, 21, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200541 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
542 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200543 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500544 [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400545 {V4L2_SLICED_WSS_625, 23, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200546 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
547 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200548 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300549#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500550 [9] = {0x130, /* Wide Screen Signal, NTSC C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400551 {V4L2_SLICED_WSS_525, 20, 20, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200552 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
553 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200554 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500555 [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400556 {V4l2_SLICED_VITC_625, 6, 22, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200557 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
558 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200559 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500560 [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400561 {V4l2_SLICED_VITC_525, 10, 20, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200562 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
563 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200564 },
Hans Verkuil9bc74002006-03-29 18:02:51 -0300565#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500566 [12] = {0x190, /* Video Program System (VPS), PAL */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400567 {V4L2_SLICED_VPS, 16, 16, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200568 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
569 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200570 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200571 /* 0x1d0 User programmable */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200572};
573
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300574static int tvp5150_write_inittab(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200575 const struct i2c_reg_value *regs)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200576{
Philipp Zabel28b9e222018-06-28 12:20:35 -0400577 struct tvp5150 *decoder = to_tvp5150(sd);
578
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200579 while (regs->reg != 0xff) {
Philipp Zabel28b9e222018-06-28 12:20:35 -0400580 regmap_write(decoder->regmap, regs->reg, regs->value);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200581 regs++;
582 }
583 return 0;
584}
585
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500586static int tvp5150_vdp_init(struct v4l2_subdev *sd)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200587{
Philipp Zabel28b9e222018-06-28 12:20:35 -0400588 struct tvp5150 *decoder = to_tvp5150(sd);
589 struct regmap *map = decoder->regmap;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200590 unsigned int i;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500591 int j;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200592
593 /* Disable Full Field */
Philipp Zabel28b9e222018-06-28 12:20:35 -0400594 regmap_write(map, TVP5150_FULL_FIELD_ENA, 0);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200595
596 /* Before programming, Line mode should be at 0xff */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300597 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400598 regmap_write(map, i, 0xff);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200599
600 /* Load Ram Table */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500601 for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) {
602 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j];
603
604 if (!regs->type.vbi_type)
605 continue;
606
Philipp Zabel28b9e222018-06-28 12:20:35 -0400607 regmap_write(map, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
608 regmap_write(map, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200609
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300610 for (i = 0; i < 16; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400611 regmap_write(map, TVP5150_VDP_CONF_RAM_DATA,
612 regs->values[i]);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200613 }
614 return 0;
615}
616
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200617/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300618static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200619 struct v4l2_sliced_vbi_cap *cap)
620{
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500621 int line, i;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200622
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200623 dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n");
Nasser Afshinbb7a3682018-04-02 18:23:20 -0400624 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200625
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500626 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
627 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
628
629 if (!regs->type.vbi_type)
630 continue;
631
632 for (line = regs->type.ini_line;
633 line <= regs->type.end_line;
634 line++) {
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200635 cap->service_lines[0][line] |= regs->type.vbi_type;
636 }
637 cap->service_set |= regs->type.vbi_type;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200638 }
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300639 return 0;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200640}
641
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200642/* Set vbi processing
643 * type - one of tvp5150_vbi_types
644 * line - line to gather data
645 * fields: bit 0 field1, bit 1, field2
646 * flags (default=0xf0) is a bitmask, were set means:
647 * bit 7: enable filtering null bytes on CC
648 * bit 6: send data also to FIFO
649 * bit 5: don't allow data with errors on FIFO
650 * bit 4: enable ECC when possible
651 * pix_align = pix alignment:
652 * LSB = field1
653 * MSB = field2
654 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300655static int tvp5150_set_vbi(struct v4l2_subdev *sd,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400656 unsigned int type, u8 flags, int line,
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200657 const int fields)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200658{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300659 struct tvp5150 *decoder = to_tvp5150(sd);
660 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200661 u8 reg;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500662 int i, pos = 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200663
664 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200665 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200666 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300667 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200668 /* Don't follow NTSC Line number convension */
669 line += 3;
670 }
671
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300672 if (line < 6 || line > 27)
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200673 return 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200674
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500675 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
676 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
677
678 if (!regs->type.vbi_type)
679 continue;
680
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200681 if ((type & regs->type.vbi_type) &&
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300682 (line >= regs->type.ini_line) &&
683 (line <= regs->type.end_line))
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200684 break;
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200685 pos++;
686 }
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300687
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300688 type = pos | (flags & 0xf0);
689 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200690
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300691 if (fields & 1)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400692 regmap_write(decoder->regmap, reg, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200693
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300694 if (fields & 2)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400695 regmap_write(decoder->regmap, reg + 1, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200696
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200697 return type;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200698}
699
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500700static int tvp5150_get_vbi(struct v4l2_subdev *sd, int line)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200701{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300702 struct tvp5150 *decoder = to_tvp5150(sd);
703 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200704 u8 reg;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300705 int pos, type = 0;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300706 int i, ret = 0;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200707
708 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200709 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200710 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300711 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200712 /* Don't follow NTSC Line number convension */
713 line += 3;
714 }
715
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300716 if (line < 6 || line > 27)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200717 return 0;
718
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300719 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200720
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300721 for (i = 0; i <= 1; i++) {
722 ret = tvp5150_read(sd, reg + i);
723 if (ret < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200724 dev_err(sd->dev, "%s: failed with error = %d\n",
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300725 __func__, ret);
726 return 0;
727 }
728 pos = ret & 0x0f;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500729 if (pos < ARRAY_SIZE(vbi_ram_default))
730 type |= vbi_ram_default[pos].type.vbi_type;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300731 }
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200732
733 return type;
734}
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800735
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300736static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
737{
738 struct tvp5150 *decoder = to_tvp5150(sd);
739 int fmt = 0;
740
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200741 /* First tests should be against specific std */
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800742
Hans Verkuil26811ae2013-05-29 10:19:06 -0300743 if (std == V4L2_STD_NTSC_443) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300744 fmt = VIDEO_STD_NTSC_4_43_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300745 } else if (std == V4L2_STD_PAL_M) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300746 fmt = VIDEO_STD_PAL_M_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300747 } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300748 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200749 } else {
750 /* Then, test against generic ones */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300751 if (std & V4L2_STD_NTSC)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300752 fmt = VIDEO_STD_NTSC_MJ_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300753 else if (std & V4L2_STD_PAL)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300754 fmt = VIDEO_STD_PAL_BDGHIN_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300755 else if (std & V4L2_STD_SECAM)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300756 fmt = VIDEO_STD_SECAM_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200757 }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800758
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200759 dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt);
Philipp Zabel28b9e222018-06-28 12:20:35 -0400760 regmap_write(decoder->regmap, TVP5150_VIDEO_STD, fmt);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200761 return 0;
762}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800763
Marco Felsch0db87cc2018-06-28 12:20:49 -0400764static int tvp5150_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
765{
766 struct tvp5150 *decoder = to_tvp5150(sd);
767
768 *std = decoder->norm;
769
770 return 0;
771}
772
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300773static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200774{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300775 struct tvp5150 *decoder = to_tvp5150(sd);
776
777 if (decoder->norm == std)
778 return 0;
779
Javier Martin963ddc62012-01-31 05:23:46 -0300780 /* Change cropping height limits */
781 if (std & V4L2_STD_525_60)
782 decoder->rect.height = TVP5150_V_MAX_525_60;
783 else
784 decoder->rect.height = TVP5150_V_MAX_OTHERS;
785
Philipp Zabel15695862018-06-28 12:20:41 -0400786 decoder->norm = std;
Javier Martin963ddc62012-01-31 05:23:46 -0300787
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300788 return tvp5150_set_std(sd, std);
789}
790
Philipp Zabel15695862018-06-28 12:20:41 -0400791static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
792{
793 int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
794
795 switch (val & 0x0F) {
796 case 0x01:
797 return V4L2_STD_NTSC;
798 case 0x03:
799 return V4L2_STD_PAL;
800 case 0x05:
801 return V4L2_STD_PAL_M;
802 case 0x07:
803 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
804 case 0x09:
805 return V4L2_STD_NTSC_443;
806 case 0xb:
807 return V4L2_STD_SECAM;
808 default:
809 return V4L2_STD_UNKNOWN;
810 }
811}
812
Philipp Zabel2f0a5c62018-06-28 12:20:46 -0400813static const struct v4l2_event tvp5150_ev_fmt = {
814 .type = V4L2_EVENT_SOURCE_CHANGE,
815 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
816};
817
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400818static irqreturn_t tvp5150_isr(int irq, void *dev_id)
819{
820 struct tvp5150 *decoder = dev_id;
821 struct regmap *map = decoder->regmap;
Philipp Zabel62a764e2018-06-28 12:20:45 -0400822 unsigned int mask, active = 0, status = 0;
823
824 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
825 TVP5150_MISC_CTL_CLOCK_OE;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400826
827 regmap_read(map, TVP5150_INT_STATUS_REG_A, &status);
828 if (status) {
829 regmap_write(map, TVP5150_INT_STATUS_REG_A, status);
830
Philipp Zabel62a764e2018-06-28 12:20:45 -0400831 if (status & TVP5150_INT_A_LOCK) {
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400832 decoder->lock = !!(status & TVP5150_INT_A_LOCK_STATUS);
Philipp Zabel7bb3c332018-06-28 12:20:47 -0400833 dev_dbg_lvl(decoder->sd.dev, 1, debug,
834 "sync lo%s signal\n",
835 decoder->lock ? "ck" : "ss");
Philipp Zabel2f0a5c62018-06-28 12:20:46 -0400836 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
Philipp Zabel62a764e2018-06-28 12:20:45 -0400837 regmap_update_bits(map, TVP5150_MISC_CTL, mask,
838 decoder->lock ? decoder->oe : 0);
839 }
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400840
841 return IRQ_HANDLED;
842 }
843
844 regmap_read(map, TVP5150_INT_ACTIVE_REG_B, &active);
845 if (active) {
846 status = 0;
847 regmap_read(map, TVP5150_INT_STATUS_REG_B, &status);
848 if (status)
849 regmap_write(map, TVP5150_INT_RESET_REG_B, status);
850 }
851
852 return IRQ_HANDLED;
853}
854
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300855static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
856{
857 struct tvp5150 *decoder = to_tvp5150(sd);
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400858 struct regmap *map = decoder->regmap;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200859
860 /* Initializes TVP5150 to its default values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300861 tvp5150_write_inittab(sd, tvp5150_init_default);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200862
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400863 if (decoder->irq) {
864 /* Configure pins: FID, VSYNC, INTREQ, SCLK */
865 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x0);
866 /* Set interrupt polarity to active high */
867 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE | 0x1);
868 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x1);
869 } else {
870 /* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */
871 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x2);
872 /* Keep interrupt polarity active low */
873 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE);
874 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x0);
875 }
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400876
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200877 /* Initializes VDP registers */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500878 tvp5150_vdp_init(sd);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200879
880 /* Selects decoder input */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300881 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800882
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200883 /* Initialize image preferences */
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300884 v4l2_ctrl_handler_setup(&decoder->hdl);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200885
Philipp Zabel1bb086b2018-06-28 12:20:42 -0400886 return 0;
887}
888
889static int tvp5150_enable(struct v4l2_subdev *sd)
890{
891 struct tvp5150 *decoder = to_tvp5150(sd);
892 v4l2_std_id std;
893
894 /* Initializes TVP5150 to stream enabled values */
895 tvp5150_write_inittab(sd, tvp5150_init_enable);
896
Philipp Zabel15695862018-06-28 12:20:41 -0400897 if (decoder->norm == V4L2_STD_ALL)
898 std = tvp5150_read_std(sd);
899 else
900 std = decoder->norm;
901
902 /* Disable autoswitch mode */
903 tvp5150_set_std(sd, std);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200904
Philipp Zabel62a764e2018-06-28 12:20:45 -0400905 /*
906 * Enable the YCbCr and clock outputs. In discrete sync mode
907 * (non-BT.656) additionally enable the the sync outputs.
908 */
909 switch (decoder->mbus_type) {
910 case V4L2_MBUS_PARALLEL:
Marco Felsch8a7441b2018-06-28 12:20:36 -0400911 /* 8-bit 4:2:2 YUV with discrete sync output */
912 regmap_update_bits(decoder->regmap, TVP5150_DATA_RATE_SEL,
913 0x7, 0x0);
Philipp Zabel62a764e2018-06-28 12:20:45 -0400914 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
915 TVP5150_MISC_CTL_CLOCK_OE |
916 TVP5150_MISC_CTL_SYNC_OE;
917 break;
918 case V4L2_MBUS_BT656:
919 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
920 TVP5150_MISC_CTL_CLOCK_OE;
921 break;
922 default:
923 return -EINVAL;
924 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200925
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300926 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800927};
928
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300929static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800930{
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300931 struct v4l2_subdev *sd = to_sd(ctrl);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200932 struct tvp5150 *decoder = to_tvp5150(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800933
934 switch (ctrl->id) {
935 case V4L2_CID_BRIGHTNESS:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400936 regmap_write(decoder->regmap, TVP5150_BRIGHT_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800937 return 0;
938 case V4L2_CID_CONTRAST:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400939 regmap_write(decoder->regmap, TVP5150_CONTRAST_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800940 return 0;
941 case V4L2_CID_SATURATION:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400942 regmap_write(decoder->regmap, TVP5150_SATURATION_CTL,
943 ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800944 return 0;
945 case V4L2_CID_HUE:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400946 regmap_write(decoder->regmap, TVP5150_HUE_CTL, ctrl->val);
Marco Felsch2d29bcc2018-06-28 12:20:34 -0400947 return 0;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200948 case V4L2_CID_TEST_PATTERN:
949 decoder->enable = ctrl->val ? false : true;
950 tvp5150_selmux(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800951 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800952 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200953 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800954}
955
Marco Felsch5cb82942018-06-28 12:20:39 -0400956static void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop)
957{
958 /* Default is no cropping */
959 crop->top = 0;
960 crop->left = 0;
961 crop->width = TVP5150_H_MAX;
962 if (std & V4L2_STD_525_60)
963 crop->height = TVP5150_V_MAX_525_60;
964 else
965 crop->height = TVP5150_V_MAX_OTHERS;
966}
967
Hans Verkuilda298c62015-04-09 04:02:34 -0300968static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
Marco Felsch5cb82942018-06-28 12:20:39 -0400969 struct v4l2_subdev_pad_config *cfg,
970 struct v4l2_subdev_format *format)
Javier Martinec2c4f32012-01-05 10:57:39 -0300971{
Hans Verkuilda298c62015-04-09 04:02:34 -0300972 struct v4l2_mbus_framefmt *f;
Javier Martinec2c4f32012-01-05 10:57:39 -0300973 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martinec2c4f32012-01-05 10:57:39 -0300974
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -0400975 if (!format || (format->pad != TVP5150_PAD_VID_OUT))
Javier Martinec2c4f32012-01-05 10:57:39 -0300976 return -EINVAL;
977
Hans Verkuilda298c62015-04-09 04:02:34 -0300978 f = &format->format;
979
Javier Martin963ddc62012-01-31 05:23:46 -0300980 f->width = decoder->rect.width;
Javier Martinez Canillas1831af02018-06-10 16:43:02 -0400981 f->height = decoder->rect.height / 2;
Javier Martinec2c4f32012-01-05 10:57:39 -0300982
Marco Felsch5cb82942018-06-28 12:20:39 -0400983 f->code = TVP5150_MBUS_FMT;
984 f->field = TVP5150_FIELD;
985 f->colorspace = TVP5150_COLORSPACE;
Javier Martinec2c4f32012-01-05 10:57:39 -0300986
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200987 dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width,
Marco Felsch5cb82942018-06-28 12:20:39 -0400988 f->height);
Javier Martinec2c4f32012-01-05 10:57:39 -0300989 return 0;
990}
991
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200992static int tvp5150_set_selection(struct v4l2_subdev *sd,
993 struct v4l2_subdev_pad_config *cfg,
994 struct v4l2_subdev_selection *sel)
Javier Martin963ddc62012-01-31 05:23:46 -0300995{
Javier Martin963ddc62012-01-31 05:23:46 -0300996 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200997 struct v4l2_rect rect = sel->r;
Javier Martin963ddc62012-01-31 05:23:46 -0300998 v4l2_std_id std;
Hans Verkuil10d5509c2015-12-14 08:25:32 -0200999 int hmax;
1000
1001 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
1002 sel->target != V4L2_SEL_TGT_CROP)
1003 return -EINVAL;
Javier Martin963ddc62012-01-31 05:23:46 -03001004
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001005 dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
Javier Martin963ddc62012-01-31 05:23:46 -03001006 __func__, rect.left, rect.top, rect.width, rect.height);
1007
Javier Martin963ddc62012-01-31 05:23:46 -03001008 /* tvp5150 has some special limits */
1009 rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
Javier Martin963ddc62012-01-31 05:23:46 -03001010 rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
1011
1012 /* Calculate height based on current standard */
1013 if (decoder->norm == V4L2_STD_ALL)
1014 std = tvp5150_read_std(sd);
1015 else
1016 std = decoder->norm;
1017
1018 if (std & V4L2_STD_525_60)
1019 hmax = TVP5150_V_MAX_525_60;
1020 else
1021 hmax = TVP5150_V_MAX_OTHERS;
1022
Marco Felschbd24db02018-06-28 12:20:33 -04001023 /*
1024 * alignments:
1025 * - width = 2 due to UYVY colorspace
1026 * - height, image = no special alignment
1027 */
1028 v4l_bound_align_image(&rect.width,
1029 TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
1030 TVP5150_H_MAX - rect.left, 1, &rect.height,
Ricardo Ribaldaf90580c2013-11-26 05:31:42 -03001031 hmax - TVP5150_MAX_CROP_TOP - rect.top,
Marco Felschbd24db02018-06-28 12:20:33 -04001032 hmax - rect.top, 0, 0);
Javier Martin963ddc62012-01-31 05:23:46 -03001033
Philipp Zabel28b9e222018-06-28 12:20:35 -04001034 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect.top);
1035 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
1036 rect.top + rect.height - hmax);
1037 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
1038 rect.left >> TVP5150_CROP_SHIFT);
1039 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
1040 rect.left | (1 << TVP5150_CROP_SHIFT));
1041 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
1042 (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
1043 TVP5150_CROP_SHIFT);
1044 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
1045 rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
Javier Martin963ddc62012-01-31 05:23:46 -03001046
1047 decoder->rect = rect;
1048
1049 return 0;
1050}
1051
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001052static int tvp5150_get_selection(struct v4l2_subdev *sd,
1053 struct v4l2_subdev_pad_config *cfg,
1054 struct v4l2_subdev_selection *sel)
Javier Martin963ddc62012-01-31 05:23:46 -03001055{
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001056 struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
Javier Martin963ddc62012-01-31 05:23:46 -03001057 v4l2_std_id std;
1058
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001059 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
Javier Martin963ddc62012-01-31 05:23:46 -03001060 return -EINVAL;
1061
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001062 switch (sel->target) {
1063 case V4L2_SEL_TGT_CROP_BOUNDS:
1064 case V4L2_SEL_TGT_CROP_DEFAULT:
1065 sel->r.left = 0;
1066 sel->r.top = 0;
1067 sel->r.width = TVP5150_H_MAX;
Javier Martin963ddc62012-01-31 05:23:46 -03001068
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001069 /* Calculate height based on current standard */
1070 if (decoder->norm == V4L2_STD_ALL)
1071 std = tvp5150_read_std(sd);
1072 else
1073 std = decoder->norm;
1074 if (std & V4L2_STD_525_60)
1075 sel->r.height = TVP5150_V_MAX_525_60;
1076 else
1077 sel->r.height = TVP5150_V_MAX_OTHERS;
1078 return 0;
1079 case V4L2_SEL_TGT_CROP:
1080 sel->r = decoder->rect;
1081 return 0;
1082 default:
1083 return -EINVAL;
1084 }
Javier Martin963ddc62012-01-31 05:23:46 -03001085}
1086
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001087static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
1088 struct v4l2_mbus_config *cfg)
1089{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001090 struct tvp5150 *decoder = to_tvp5150(sd);
1091
1092 cfg->type = decoder->mbus_type;
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001093 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
1094 | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
1095
1096 return 0;
1097}
1098
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001099/****************************************************************************
Laurent Pincharte545ac82016-01-26 10:46:24 -02001100 V4L2 subdev pad ops
1101 ****************************************************************************/
Philipp Zabelb440b732018-06-28 12:20:40 -04001102static int tvp5150_init_cfg(struct v4l2_subdev *sd,
1103 struct v4l2_subdev_pad_config *cfg)
1104{
1105 struct tvp5150 *decoder = to_tvp5150(sd);
1106 v4l2_std_id std;
1107
1108 /*
1109 * Reset selection to maximum on subdev_open() if autodetection is on
1110 * and a standard change is detected.
1111 */
1112 if (decoder->norm == V4L2_STD_ALL) {
1113 std = tvp5150_read_std(sd);
1114 if (std != decoder->detected_norm) {
1115 decoder->detected_norm = std;
1116 tvp5150_set_default(std, &decoder->rect);
1117 }
1118 }
1119
1120 return 0;
1121}
1122
Laurent Pincharte545ac82016-01-26 10:46:24 -02001123static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
1124 struct v4l2_subdev_pad_config *cfg,
1125 struct v4l2_subdev_mbus_code_enum *code)
1126{
1127 if (code->pad || code->index)
1128 return -EINVAL;
1129
Marco Felsch5cb82942018-06-28 12:20:39 -04001130 code->code = TVP5150_MBUS_FMT;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001131 return 0;
1132}
1133
1134static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
1135 struct v4l2_subdev_pad_config *cfg,
1136 struct v4l2_subdev_frame_size_enum *fse)
1137{
1138 struct tvp5150 *decoder = to_tvp5150(sd);
1139
Marco Felsch5cb82942018-06-28 12:20:39 -04001140 if (fse->index >= 8 || fse->code != TVP5150_MBUS_FMT)
Laurent Pincharte545ac82016-01-26 10:46:24 -02001141 return -EINVAL;
1142
Marco Felsch5cb82942018-06-28 12:20:39 -04001143 fse->code = TVP5150_MBUS_FMT;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001144 fse->min_width = decoder->rect.width;
1145 fse->max_width = decoder->rect.width;
1146 fse->min_height = decoder->rect.height / 2;
1147 fse->max_height = decoder->rect.height / 2;
1148
1149 return 0;
1150}
1151
1152/****************************************************************************
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001153 Media entity ops
1154 ****************************************************************************/
1155
Laurent Pinchart406ff672016-12-08 20:22:41 -02001156#ifdef CONFIG_MEDIA_CONTROLLER
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001157static int tvp5150_link_setup(struct media_entity *entity,
1158 const struct media_pad *local,
1159 const struct media_pad *remote, u32 flags)
1160{
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001161 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1162 struct tvp5150 *decoder = to_tvp5150(sd);
1163 int i;
1164
1165 for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1166 if (remote->entity == &decoder->input_ent[i])
1167 break;
1168 }
1169
1170 /* Do nothing for entities that are not input connectors */
1171 if (i == TVP5150_INPUT_NUM)
1172 return 0;
1173
1174 decoder->input = i;
1175
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001176 tvp5150_selmux(sd);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001177
1178 return 0;
1179}
1180
1181static const struct media_entity_operations tvp5150_sd_media_ops = {
1182 .link_setup = tvp5150_link_setup,
1183};
Laurent Pinchart406ff672016-12-08 20:22:41 -02001184#endif
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001185
1186/****************************************************************************
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001187 I2C Command
1188 ****************************************************************************/
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001189
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001190static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1191{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001192 struct tvp5150 *decoder = to_tvp5150(sd);
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04001193 unsigned int mask, val = 0, int_val = 0;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001194
Marco Felsch8a7441b2018-06-28 12:20:36 -04001195 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
1196 TVP5150_MISC_CTL_CLOCK_OE;
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001197
Laurent Pinchart79d62052016-12-09 09:47:19 -02001198 if (enable) {
Philipp Zabel1bb086b2018-06-28 12:20:42 -04001199 tvp5150_enable(sd);
1200
Philipp Zabel62a764e2018-06-28 12:20:45 -04001201 /* Enable outputs if decoder is locked */
1202 val = decoder->lock ? decoder->oe : 0;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04001203 int_val = TVP5150_INT_A_LOCK;
Philipp Zabel2f0a5c62018-06-28 12:20:46 -04001204 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
Laurent Pinchart79d62052016-12-09 09:47:19 -02001205 }
1206
Marco Felsch8a7441b2018-06-28 12:20:36 -04001207 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04001208 if (decoder->irq)
1209 /* Enable / Disable lock interrupt */
1210 regmap_update_bits(decoder->regmap, TVP5150_INT_ENABLE_REG_A,
1211 TVP5150_INT_A_LOCK, int_val);
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001212
1213 return 0;
1214}
1215
Hans Verkuil5325b422009-04-02 11:26:22 -03001216static int tvp5150_s_routing(struct v4l2_subdev *sd,
1217 u32 input, u32 output, u32 config)
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001218{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001219 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001220
Hans Verkuil5325b422009-04-02 11:26:22 -03001221 decoder->input = input;
1222 decoder->output = output;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001223
1224 if (output == TVP5150_BLACK_SCREEN)
1225 decoder->enable = false;
1226 else
1227 decoder->enable = true;
1228
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001229 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001230 return 0;
1231}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001232
Hans Verkuild37dad42010-03-14 10:59:16 -03001233static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001234{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001235 struct tvp5150 *decoder = to_tvp5150(sd);
1236
Nasser Afshin9bfd8f82018-04-02 18:23:18 -04001237 /*
1238 * this is for capturing 36 raw vbi lines
1239 * if there's a way to cut off the beginning 2 vbi lines
1240 * with the tvp5150 then the vbi line count could be lowered
1241 * to 17 lines/field again, although I couldn't find a register
1242 * which could do that cropping
1243 */
1244
Hans Verkuild37dad42010-03-14 10:59:16 -03001245 if (fmt->sample_format == V4L2_PIX_FMT_GREY)
Philipp Zabel28b9e222018-06-28 12:20:35 -04001246 regmap_write(decoder->regmap, TVP5150_LUMA_PROC_CTL_1, 0x70);
Hans Verkuild37dad42010-03-14 10:59:16 -03001247 if (fmt->count[0] == 18 && fmt->count[1] == 18) {
Philipp Zabel28b9e222018-06-28 12:20:35 -04001248 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START,
1249 0x00);
1250 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 0x01);
Hans Verkuild37dad42010-03-14 10:59:16 -03001251 }
1252 return 0;
1253}
1254
1255static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1256{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001257 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001258 int i;
1259
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001260 if (svbi->service_set != 0) {
1261 for (i = 0; i <= 23; i++) {
1262 svbi->service_lines[1][i] = 0;
1263 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001264 tvp5150_set_vbi(sd, svbi->service_lines[0][i],
1265 0xf0, i, 3);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001266 }
1267 /* Enables FIFO */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001268 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 1);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001269 } else {
1270 /* Disables FIFO*/
Philipp Zabel28b9e222018-06-28 12:20:35 -04001271 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 0);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001272
1273 /* Disable Full Field */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001274 regmap_write(decoder->regmap, TVP5150_FULL_FIELD_ENA, 0);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001275
1276 /* Disable Line modes */
1277 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -04001278 regmap_write(decoder->regmap, i, 0xff);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001279 }
1280 return 0;
1281}
1282
Hans Verkuild37dad42010-03-14 10:59:16 -03001283static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1284{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001285 int i, mask = 0;
1286
Hans Verkuil30634e82012-09-05 10:38:10 -03001287 memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001288
1289 for (i = 0; i <= 23; i++) {
1290 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001291 tvp5150_get_vbi(sd, i);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001292 mask |= svbi->service_lines[0][i];
1293 }
1294 svbi->service_set = mask;
1295 return 0;
1296}
1297
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001298#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001299static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001300{
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001301 int res;
1302
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001303 res = tvp5150_read(sd, reg->reg & 0xff);
1304 if (res < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001305 dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001306 return res;
1307 }
1308
1309 reg->val = res;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001310 reg->size = 1;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001311 return 0;
1312}
1313
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001314static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001315{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001316 struct tvp5150 *decoder = to_tvp5150(sd);
1317
1318 return regmap_write(decoder->regmap, reg->reg & 0xff, reg->val & 0xff);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001319}
1320#endif
1321
1322static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1323{
1324 int status = tvp5150_read(sd, 0x88);
1325
1326 vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1327 return 0;
1328}
1329
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001330static int tvp5150_registered(struct v4l2_subdev *sd)
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001331{
1332#ifdef CONFIG_MEDIA_CONTROLLER
1333 struct tvp5150 *decoder = to_tvp5150(sd);
1334 int ret = 0;
1335 int i;
1336
1337 for (i = 0; i < TVP5150_INPUT_NUM; i++) {
1338 struct media_entity *input = &decoder->input_ent[i];
1339 struct media_pad *pad = &decoder->input_pad[i];
1340
1341 if (!input->name)
1342 continue;
1343
1344 decoder->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1345
1346 ret = media_entity_pads_init(input, 1, pad);
1347 if (ret < 0)
1348 return ret;
1349
1350 ret = media_device_register_entity(sd->v4l2_dev->mdev, input);
1351 if (ret < 0)
1352 return ret;
1353
1354 ret = media_create_pad_link(input, 0, &sd->entity,
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001355 TVP5150_PAD_IF_INPUT, 0);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001356 if (ret < 0) {
1357 media_device_unregister_entity(input);
1358 return ret;
1359 }
1360 }
1361#endif
1362
1363 return 0;
1364}
1365
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001366/* ----------------------------------------------------------------------- */
1367
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001368static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1369 .s_ctrl = tvp5150_s_ctrl,
1370};
1371
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001372static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1373 .log_status = tvp5150_log_status,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001374 .reset = tvp5150_reset,
1375#ifdef CONFIG_VIDEO_ADV_DEBUG
1376 .g_register = tvp5150_g_register,
1377 .s_register = tvp5150_s_register,
1378#endif
1379};
1380
1381static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001382 .g_tuner = tvp5150_g_tuner,
1383};
1384
1385static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001386 .s_std = tvp5150_s_std,
Marco Felsch0db87cc2018-06-28 12:20:49 -04001387 .g_std = tvp5150_g_std,
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001388 .s_stream = tvp5150_s_stream,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001389 .s_routing = tvp5150_s_routing,
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001390 .g_mbus_config = tvp5150_g_mbus_config,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001391};
1392
1393static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001394 .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
Hans Verkuild37dad42010-03-14 10:59:16 -03001395 .g_sliced_fmt = tvp5150_g_sliced_fmt,
1396 .s_sliced_fmt = tvp5150_s_sliced_fmt,
1397 .s_raw_fmt = tvp5150_s_raw_fmt,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001398};
1399
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001400static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
Philipp Zabelb440b732018-06-28 12:20:40 -04001401 .init_cfg = tvp5150_init_cfg,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001402 .enum_mbus_code = tvp5150_enum_mbus_code,
Laurent Pincharte545ac82016-01-26 10:46:24 -02001403 .enum_frame_size = tvp5150_enum_frame_size,
Hans Verkuilda298c62015-04-09 04:02:34 -03001404 .set_fmt = tvp5150_fill_fmt,
1405 .get_fmt = tvp5150_fill_fmt,
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001406 .get_selection = tvp5150_get_selection,
1407 .set_selection = tvp5150_set_selection,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001408};
1409
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001410static const struct v4l2_subdev_ops tvp5150_ops = {
1411 .core = &tvp5150_core_ops,
1412 .tuner = &tvp5150_tuner_ops,
1413 .video = &tvp5150_video_ops,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001414 .vbi = &tvp5150_vbi_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001415 .pad = &tvp5150_pad_ops,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001416};
1417
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001418static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
1419 .registered = tvp5150_registered,
1420};
1421
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001422/****************************************************************************
1423 I2C Client & Driver
1424 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001425
Philipp Zabel28b9e222018-06-28 12:20:35 -04001426static const struct regmap_range tvp5150_readable_ranges[] = {
1427 {
1428 .range_min = TVP5150_VD_IN_SRC_SEL_1,
1429 .range_max = TVP5150_AUTOSW_MSK,
1430 }, {
1431 .range_min = TVP5150_COLOR_KIL_THSH_CTL,
1432 .range_max = TVP5150_CONF_SHARED_PIN,
1433 }, {
1434 .range_min = TVP5150_ACT_VD_CROP_ST_MSB,
1435 .range_max = TVP5150_HORIZ_SYNC_START,
1436 }, {
1437 .range_min = TVP5150_VERT_BLANKING_START,
1438 .range_max = TVP5150_INTT_CONFIG_REG_B,
1439 }, {
1440 .range_min = TVP5150_VIDEO_STD,
1441 .range_max = TVP5150_VIDEO_STD,
1442 }, {
1443 .range_min = TVP5150_CB_GAIN_FACT,
1444 .range_max = TVP5150_REV_SELECT,
1445 }, {
1446 .range_min = TVP5150_MSB_DEV_ID,
1447 .range_max = TVP5150_STATUS_REG_5,
1448 }, {
1449 .range_min = TVP5150_CC_DATA_INI,
1450 .range_max = TVP5150_TELETEXT_FIL_ENA,
1451 }, {
1452 .range_min = TVP5150_INT_STATUS_REG_A,
1453 .range_max = TVP5150_FIFO_OUT_CTRL,
1454 }, {
1455 .range_min = TVP5150_FULL_FIELD_ENA,
1456 .range_max = TVP5150_FULL_FIELD_MODE_REG,
1457 },
1458};
1459
1460bool tvp5150_volatile_reg(struct device *dev, unsigned int reg)
1461{
1462 switch (reg) {
1463 case TVP5150_VERT_LN_COUNT_MSB:
1464 case TVP5150_VERT_LN_COUNT_LSB:
1465 case TVP5150_INT_STATUS_REG_A:
1466 case TVP5150_INT_STATUS_REG_B:
1467 case TVP5150_INT_ACTIVE_REG_B:
1468 case TVP5150_STATUS_REG_1:
1469 case TVP5150_STATUS_REG_2:
1470 case TVP5150_STATUS_REG_3:
1471 case TVP5150_STATUS_REG_4:
1472 case TVP5150_STATUS_REG_5:
1473 /* CC, WSS, VPS, VITC data? */
1474 case TVP5150_VBI_FIFO_READ_DATA:
1475 case TVP5150_VDP_STATUS_REG:
1476 case TVP5150_FIFO_WORD_COUNT:
1477 return true;
1478 default:
1479 return false;
1480 }
1481}
1482
1483static const struct regmap_access_table tvp5150_readable_table = {
1484 .yes_ranges = tvp5150_readable_ranges,
1485 .n_yes_ranges = ARRAY_SIZE(tvp5150_readable_ranges),
1486};
1487
1488static struct regmap_config tvp5150_config = {
1489 .reg_bits = 8,
1490 .val_bits = 8,
1491 .max_register = 0xff,
1492
1493 .cache_type = REGCACHE_RBTREE,
1494
1495 .rd_table = &tvp5150_readable_table,
1496 .volatile_reg = tvp5150_volatile_reg,
1497};
1498
Laurent Pinchart78715972016-01-07 10:46:41 -02001499static int tvp5150_detect_version(struct tvp5150 *core)
1500{
1501 struct v4l2_subdev *sd = &core->sd;
1502 struct i2c_client *c = v4l2_get_subdevdata(sd);
Laurent Pinchart78715972016-01-07 10:46:41 -02001503 u8 regs[4];
1504 int res;
1505
1506 /*
1507 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1508 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1509 */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001510 res = regmap_bulk_read(core->regmap, TVP5150_MSB_DEV_ID, regs, 4);
1511 if (res < 0) {
1512 dev_err(&c->dev, "reading ID registers failed: %d\n", res);
1513 return res;
Laurent Pinchart78715972016-01-07 10:46:41 -02001514 }
1515
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001516 core->dev_id = (regs[0] << 8) | regs[1];
1517 core->rom_ver = (regs[2] << 8) | regs[3];
Laurent Pinchart78715972016-01-07 10:46:41 -02001518
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001519 dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001520 core->dev_id, regs[2], regs[3], c->addr << 1,
1521 c->adapter->name);
Laurent Pinchart78715972016-01-07 10:46:41 -02001522
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001523 if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001524 dev_info(sd->dev, "tvp5150a detected.\n");
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001525 } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001526 dev_info(sd->dev, "tvp5150am1 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001527
1528 /* ITU-T BT.656.4 timing */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001529 regmap_write(core->regmap, TVP5150_REV_SELECT, 0);
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001530 } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001531 dev_info(sd->dev, "tvp5151 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001532 } else {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001533 dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001534 core->dev_id);
Laurent Pinchart78715972016-01-07 10:46:41 -02001535 }
1536
1537 return 0;
1538}
1539
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02001540static int tvp5150_init(struct i2c_client *c)
1541{
1542 struct gpio_desc *pdn_gpio;
1543 struct gpio_desc *reset_gpio;
1544
1545 pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1546 if (IS_ERR(pdn_gpio))
1547 return PTR_ERR(pdn_gpio);
1548
1549 if (pdn_gpio) {
1550 gpiod_set_value_cansleep(pdn_gpio, 0);
1551 /* Delay time between power supplies active and reset */
1552 msleep(20);
1553 }
1554
1555 reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1556 if (IS_ERR(reset_gpio))
1557 return PTR_ERR(reset_gpio);
1558
1559 if (reset_gpio) {
1560 /* RESETB pulse duration */
1561 ndelay(500);
1562 gpiod_set_value_cansleep(reset_gpio, 0);
1563 /* Delay time between end of reset to I2C active */
1564 usleep_range(200, 250);
1565 }
1566
1567 return 0;
1568}
1569
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001570static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1571{
Sakari Ailus859969b2016-08-26 20:17:25 -03001572 struct v4l2_fwnode_endpoint bus_cfg;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001573 struct device_node *ep;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001574#ifdef CONFIG_MEDIA_CONTROLLER
1575 struct device_node *connectors, *child;
1576 struct media_entity *input;
1577 const char *name;
1578 u32 input_type;
1579#endif
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001580 unsigned int flags;
1581 int ret = 0;
1582
1583 ep = of_graph_get_next_endpoint(np, NULL);
1584 if (!ep)
1585 return -EINVAL;
1586
Sakari Ailus859969b2016-08-26 20:17:25 -03001587 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001588 if (ret)
1589 goto err;
1590
1591 flags = bus_cfg.bus.parallel.flags;
1592
1593 if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
1594 !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
1595 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
Javier Martinez Canillas2bd5e432016-02-05 17:09:53 -02001596 flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
1597 ret = -EINVAL;
1598 goto err;
1599 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001600
1601 decoder->mbus_type = bus_cfg.bus_type;
1602
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001603#ifdef CONFIG_MEDIA_CONTROLLER
1604 connectors = of_get_child_by_name(np, "connectors");
1605
1606 if (!connectors)
1607 goto err;
1608
1609 for_each_available_child_of_node(connectors, child) {
1610 ret = of_property_read_u32(child, "input", &input_type);
1611 if (ret) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001612 dev_err(decoder->sd.dev,
Rob Herringf764e6d2018-08-27 21:52:29 -04001613 "missing type property in node %pOFn\n",
1614 child);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001615 goto err_connector;
1616 }
1617
Mauro Carvalho Chehab60ad7682016-02-19 15:02:30 -02001618 if (input_type >= TVP5150_INPUT_NUM) {
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001619 ret = -EINVAL;
1620 goto err_connector;
1621 }
1622
1623 input = &decoder->input_ent[input_type];
1624
1625 /* Each input connector can only be defined once */
1626 if (input->name) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001627 dev_err(decoder->sd.dev,
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001628 "input %s with same type already exists\n",
1629 input->name);
1630 ret = -EINVAL;
1631 goto err_connector;
1632 }
1633
1634 switch (input_type) {
1635 case TVP5150_COMPOSITE0:
1636 case TVP5150_COMPOSITE1:
1637 input->function = MEDIA_ENT_F_CONN_COMPOSITE;
1638 break;
1639 case TVP5150_SVIDEO:
1640 input->function = MEDIA_ENT_F_CONN_SVIDEO;
1641 break;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001642 }
1643
1644 input->flags = MEDIA_ENT_FL_CONNECTOR;
1645
1646 ret = of_property_read_string(child, "label", &name);
1647 if (ret < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001648 dev_err(decoder->sd.dev,
Rob Herringf764e6d2018-08-27 21:52:29 -04001649 "missing label property in node %pOFn\n",
1650 child);
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001651 goto err_connector;
1652 }
1653
1654 input->name = name;
1655 }
1656
1657err_connector:
1658 of_node_put(connectors);
1659#endif
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001660err:
1661 of_node_put(ep);
1662 return ret;
1663}
1664
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001665static const char * const tvp5150_test_patterns[2] = {
1666 "Disabled",
1667 "Black screen"
1668};
1669
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001670static int tvp5150_probe(struct i2c_client *c,
1671 const struct i2c_device_id *id)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001672{
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001673 struct tvp5150 *core;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001674 struct v4l2_subdev *sd;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001675 struct device_node *np = c->dev.of_node;
Philipp Zabel28b9e222018-06-28 12:20:35 -04001676 struct regmap *map;
Laurent Pinchart78715972016-01-07 10:46:41 -02001677 int res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001678
1679 /* Check if the adapter supports the needed features */
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001680 if (!i2c_check_functionality(c->adapter,
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001681 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001682 return -EIO;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001683
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02001684 res = tvp5150_init(c);
1685 if (res)
1686 return res;
1687
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001688 core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
1689 if (!core)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001690 return -ENOMEM;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001691
Philipp Zabel28b9e222018-06-28 12:20:35 -04001692 map = devm_regmap_init_i2c(c, &tvp5150_config);
1693 if (IS_ERR(map))
1694 return PTR_ERR(map);
1695
1696 core->regmap = map;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001697 sd = &core->sd;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001698
1699 if (IS_ENABLED(CONFIG_OF) && np) {
1700 res = tvp5150_parse_dt(core, np);
1701 if (res) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001702 dev_err(sd->dev, "DT parsing error: %d\n", res);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001703 return res;
1704 }
1705 } else {
1706 /* Default to BT.656 embedded sync */
1707 core->mbus_type = V4L2_MBUS_BT656;
1708 }
1709
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001710 v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
Javier Martinez Canillas5a08bc02016-08-11 13:28:15 -03001711 sd->internal_ops = &tvp5150_internal_ops;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001712 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1713
1714#if defined(CONFIG_MEDIA_CONTROLLER)
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001715 core->pads[TVP5150_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
1716 core->pads[TVP5150_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
1717 core->pads[TVP5150_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
1718 core->pads[TVP5150_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
Mauro Carvalho Chehabf92c70a2016-01-27 15:09:15 -02001719
1720 sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1721
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001722 res = media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS, core->pads);
Laurent Pincharte545ac82016-01-26 10:46:24 -02001723 if (res < 0)
1724 return res;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02001725
1726 sd->entity.ops = &tvp5150_sd_media_ops;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001727#endif
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001728
Laurent Pinchart78715972016-01-07 10:46:41 -02001729 res = tvp5150_detect_version(core);
1730 if (res < 0)
1731 return res;
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03001732
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -02001733 core->norm = V4L2_STD_ALL; /* Default is autodetect */
Philipp Zabelb440b732018-06-28 12:20:40 -04001734 core->detected_norm = V4L2_STD_UNKNOWN;
Hans Verkuil5325b422009-04-02 11:26:22 -03001735 core->input = TVP5150_COMPOSITE1;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001736 core->enable = true;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001737
Laurent Pinchartb1950b82016-01-07 10:46:44 -02001738 v4l2_ctrl_handler_init(&core->hdl, 5);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001739 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1740 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1741 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1742 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1743 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1744 V4L2_CID_SATURATION, 0, 255, 1, 128);
1745 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1746 V4L2_CID_HUE, -128, 127, 1, 0);
Laurent Pinchartb1950b82016-01-07 10:46:44 -02001747 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1748 V4L2_CID_PIXEL_RATE, 27000000,
1749 27000000, 1, 27000000);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001750 v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
1751 V4L2_CID_TEST_PATTERN,
Mauro Carvalho Chehab5c4c4502018-09-13 16:49:51 -04001752 ARRAY_SIZE(tvp5150_test_patterns) - 1,
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001753 0, 0, tvp5150_test_patterns);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001754 sd->ctrl_handler = &core->hdl;
1755 if (core->hdl.error) {
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001756 res = core->hdl.error;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001757 goto err;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001758 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -08001759
Marco Felsch5cb82942018-06-28 12:20:39 -04001760 tvp5150_set_default(tvp5150_read_std(sd), &core->rect);
Javier Martin963ddc62012-01-31 05:23:46 -03001761
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04001762 core->irq = c->irq;
Laurent Pinchartaff808e2016-12-09 09:47:17 -02001763 tvp5150_reset(sd, 0); /* Calls v4l2_ctrl_handler_setup() */
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04001764 if (c->irq) {
1765 res = devm_request_threaded_irq(&c->dev, c->irq, NULL,
1766 tvp5150_isr, IRQF_TRIGGER_HIGH |
1767 IRQF_ONESHOT, "tvp5150", core);
1768 if (res)
1769 return res;
1770 } else {
1771 core->lock = true;
1772 }
Laurent Pinchartaff808e2016-12-09 09:47:17 -02001773
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001774 res = v4l2_async_register_subdev(sd);
1775 if (res < 0)
1776 goto err;
1777
Markus Rechbergerf1e5ee42006-02-07 04:01:19 +01001778 if (debug > 1)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001779 tvp5150_log_status(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001780 return 0;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001781
1782err:
1783 v4l2_ctrl_handler_free(&core->hdl);
1784 return res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001785}
1786
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001787static int tvp5150_remove(struct i2c_client *c)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001788{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001789 struct v4l2_subdev *sd = i2c_get_clientdata(c);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001790 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001791
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001792 dev_dbg_lvl(sd->dev, 1, debug,
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -02001793 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1794 c->addr << 1);
1795
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03001796 v4l2_async_unregister_subdev(sd);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001797 v4l2_ctrl_handler_free(&decoder->hdl);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001798 return 0;
1799}
1800
1801/* ----------------------------------------------------------------------- */
1802
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001803static const struct i2c_device_id tvp5150_id[] = {
1804 { "tvp5150", 0 },
1805 { }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001806};
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001807MODULE_DEVICE_TABLE(i2c, tvp5150_id);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001808
Eduard Gavin7ef930a2016-01-07 10:46:48 -02001809#if IS_ENABLED(CONFIG_OF)
1810static const struct of_device_id tvp5150_of_match[] = {
1811 { .compatible = "ti,tvp5150", },
1812 { /* sentinel */ },
1813};
1814MODULE_DEVICE_TABLE(of, tvp5150_of_match);
1815#endif
1816
Hans Verkuilc7711452010-09-15 15:45:20 -03001817static struct i2c_driver tvp5150_driver = {
1818 .driver = {
Eduard Gavin7ef930a2016-01-07 10:46:48 -02001819 .of_match_table = of_match_ptr(tvp5150_of_match),
Hans Verkuilc7711452010-09-15 15:45:20 -03001820 .name = "tvp5150",
1821 },
1822 .probe = tvp5150_probe,
1823 .remove = tvp5150_remove,
1824 .id_table = tvp5150_id,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001825};
Hans Verkuilc7711452010-09-15 15:45:20 -03001826
Axel Linc6e8d862012-02-12 06:56:32 -03001827module_i2c_driver(tvp5150_driver);