blob: 9df575238952adc76941c64fedd41553b9b5b98e [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>
Marco Felsch73549a62020-03-12 11:31:50 +010016#include <linux/pm_runtime.h>
Philipp Zabel28b9e222018-06-28 12:20:35 -040017#include <linux/regmap.h>
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -030018#include <media/v4l2-async.h>
Hans Verkuil6b8fe022008-12-18 11:17:25 -030019#include <media/v4l2-device.h>
Marco Felsche953c102020-03-12 11:31:51 +010020#include <media/v4l2-event.h>
Hans Verkuil6c45ec72010-12-12 08:45:43 -030021#include <media/v4l2-ctrls.h>
Sakari Ailus859969b2016-08-26 20:17:25 -030022#include <media/v4l2-fwnode.h>
Mauro Carvalho Chehab55606312016-01-27 12:08:13 -020023#include <media/v4l2-mc.h>
Marco Felsch46fe6e72020-03-12 11:31:48 +010024#include <media/v4l2-rect.h>
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080025
26#include "tvp5150_reg.h"
27
Philipp Zabel785a3de2014-02-27 13:44:47 -030028#define TVP5150_H_MAX 720U
29#define TVP5150_V_MAX_525_60 480U
30#define TVP5150_V_MAX_OTHERS 576U
Javier Martin963ddc62012-01-31 05:23:46 -030031#define TVP5150_MAX_CROP_LEFT 511
32#define TVP5150_MAX_CROP_TOP 127
33#define TVP5150_CROP_SHIFT 2
Marco Felsch5cb82942018-06-28 12:20:39 -040034#define TVP5150_MBUS_FMT MEDIA_BUS_FMT_UYVY8_2X8
35#define TVP5150_FIELD V4L2_FIELD_ALTERNATE
36#define TVP5150_COLORSPACE V4L2_COLORSPACE_SMPTE170M
Marco Felschbaf17822020-03-12 11:31:55 +010037#define TVP5150_STD_MASK (V4L2_STD_NTSC | \
38 V4L2_STD_NTSC_443 | \
39 V4L2_STD_PAL | \
40 V4L2_STD_PAL_M | \
41 V4L2_STD_PAL_N | \
42 V4L2_STD_PAL_Nc | \
43 V4L2_STD_SECAM)
Javier Martin963ddc62012-01-31 05:23:46 -030044
Marco Felsch0556f1d2020-03-12 11:31:45 +010045#define TVP5150_MAX_CONNECTORS 3 /* Check dt-bindings for more information */
46
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -020047MODULE_DESCRIPTION("Texas Instruments TVP5150A/TVP5150AM1/TVP5151 video decoder driver");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080048MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab459ee172017-12-01 08:47:12 -050049MODULE_LICENSE("GPL v2");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080050
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080051
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030052static int debug;
Philipp Zabel2a0489d2014-02-27 13:44:48 -030053module_param(debug, int, 0644);
Hans Verkuil6b8fe022008-12-18 11:17:25 -030054MODULE_PARM_DESC(debug, "Debug level (0-2)");
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080055
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -020056#define dprintk0(__dev, __arg...) dev_dbg_lvl(__dev, 0, 0, __arg)
57
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040058enum tvp5150_pads {
Marco Felsch0556f1d2020-03-12 11:31:45 +010059 TVP5150_PAD_AIP1A,
60 TVP5150_PAD_AIP1B,
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040061 TVP5150_PAD_VID_OUT,
62 TVP5150_NUM_PADS
63};
64
Marco Felsch0556f1d2020-03-12 11:31:45 +010065struct tvp5150_connector {
66 struct v4l2_fwnode_connector base;
67 struct media_entity ent;
68 struct media_pad pad;
69};
70
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080071struct tvp5150 {
Hans Verkuil6b8fe022008-12-18 11:17:25 -030072 struct v4l2_subdev sd;
Marco Felsch0556f1d2020-03-12 11:31:45 +010073
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -040074 struct media_pad pads[TVP5150_NUM_PADS];
Marco Felsch0556f1d2020-03-12 11:31:45 +010075 struct tvp5150_connector connectors[TVP5150_MAX_CONNECTORS];
Marco Felschbaf17822020-03-12 11:31:55 +010076 struct tvp5150_connector *cur_connector;
Marco Felsch0556f1d2020-03-12 11:31:45 +010077 unsigned int connectors_num;
78
Hans Verkuil6c45ec72010-12-12 08:45:43 -030079 struct v4l2_ctrl_handler hdl;
Javier Martin963ddc62012-01-31 05:23:46 -030080 struct v4l2_rect rect;
Philipp Zabel28b9e222018-06-28 12:20:35 -040081 struct regmap *regmap;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -040082 int irq;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080083
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -020084 v4l2_std_id norm; /* Current set standard */
Philipp Zabelb440b732018-06-28 12:20:40 -040085 v4l2_std_id detected_norm;
Hans Verkuil5325b422009-04-02 11:26:22 -030086 u32 input;
87 u32 output;
Philipp Zabel62a764e2018-06-28 12:20:45 -040088 u32 oe;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -080089 int enable;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -040090 bool lock;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020091
Javier Martinez Canillas82275132016-02-05 17:09:54 -020092 u16 dev_id;
93 u16 rom_ver;
94
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -020095 enum v4l2_mbus_type mbus_type;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080096};
97
Hans Verkuil6b8fe022008-12-18 11:17:25 -030098static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -080099{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300100 return container_of(sd, struct tvp5150, sd);
101}
102
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300103static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
104{
105 return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
106}
107
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300108static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
109{
Philipp Zabel28b9e222018-06-28 12:20:35 -0400110 struct tvp5150 *decoder = to_tvp5150(sd);
111 int ret, val;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800112
Philipp Zabel28b9e222018-06-28 12:20:35 -0400113 ret = regmap_read(decoder->regmap, addr, &val);
114 if (ret < 0)
115 return ret;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200116
Philipp Zabel28b9e222018-06-28 12:20:35 -0400117 return val;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800118}
119
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300120static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
121 const u8 end, int max_line)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200122{
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200123 u8 buf[16];
124 int i = 0, j, len;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200125
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200126 if (max_line > 16) {
127 dprintk0(sd->dev, "too much data to dump\n");
128 return;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200129 }
Mauro Carvalho Chehabe5134112016-10-20 11:36:42 -0200130
131 for (i = init; i < end; i += max_line) {
132 len = (end - i > max_line) ? max_line : end - i;
133
134 for (j = 0; j < len; j++)
135 buf[j] = tvp5150_read(sd, i + j);
136
137 dprintk0(sd->dev, "%s reg %02x = %*ph\n", s, i, len, buf);
138 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200139}
140
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300141static int tvp5150_log_status(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800142{
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200143 dprintk0(sd->dev, "tvp5150: Video input source selection #1 = 0x%02x\n",
144 tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
145 dprintk0(sd->dev, "tvp5150: Analog channel controls = 0x%02x\n",
146 tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
147 dprintk0(sd->dev, "tvp5150: Operation mode controls = 0x%02x\n",
148 tvp5150_read(sd, TVP5150_OP_MODE_CTL));
149 dprintk0(sd->dev, "tvp5150: Miscellaneous controls = 0x%02x\n",
150 tvp5150_read(sd, TVP5150_MISC_CTL));
151 dprintk0(sd->dev, "tvp5150: Autoswitch mask= 0x%02x\n",
152 tvp5150_read(sd, TVP5150_AUTOSW_MSK));
153 dprintk0(sd->dev, "tvp5150: Color killer threshold control = 0x%02x\n",
154 tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
155 dprintk0(sd->dev, "tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
156 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
157 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
158 tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
159 dprintk0(sd->dev, "tvp5150: Brightness control = 0x%02x\n",
160 tvp5150_read(sd, TVP5150_BRIGHT_CTL));
161 dprintk0(sd->dev, "tvp5150: Color saturation control = 0x%02x\n",
162 tvp5150_read(sd, TVP5150_SATURATION_CTL));
163 dprintk0(sd->dev, "tvp5150: Hue control = 0x%02x\n",
164 tvp5150_read(sd, TVP5150_HUE_CTL));
165 dprintk0(sd->dev, "tvp5150: Contrast control = 0x%02x\n",
166 tvp5150_read(sd, TVP5150_CONTRAST_CTL));
167 dprintk0(sd->dev, "tvp5150: Outputs and data rates select = 0x%02x\n",
168 tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
169 dprintk0(sd->dev, "tvp5150: Configuration shared pins = 0x%02x\n",
170 tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
171 dprintk0(sd->dev, "tvp5150: Active video cropping start = 0x%02x%02x\n",
172 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
173 tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
174 dprintk0(sd->dev, "tvp5150: Active video cropping stop = 0x%02x%02x\n",
175 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
176 tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
177 dprintk0(sd->dev, "tvp5150: Genlock/RTC = 0x%02x\n",
178 tvp5150_read(sd, TVP5150_GENLOCK));
179 dprintk0(sd->dev, "tvp5150: Horizontal sync start = 0x%02x\n",
180 tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
181 dprintk0(sd->dev, "tvp5150: Vertical blanking start = 0x%02x\n",
182 tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
183 dprintk0(sd->dev, "tvp5150: Vertical blanking stop = 0x%02x\n",
184 tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
185 dprintk0(sd->dev, "tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
186 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
187 tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
188 dprintk0(sd->dev, "tvp5150: Interrupt reset register B = 0x%02x\n",
189 tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
190 dprintk0(sd->dev, "tvp5150: Interrupt enable register B = 0x%02x\n",
191 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
192 dprintk0(sd->dev, "tvp5150: Interrupt configuration register B = 0x%02x\n",
193 tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
194 dprintk0(sd->dev, "tvp5150: Video standard = 0x%02x\n",
195 tvp5150_read(sd, TVP5150_VIDEO_STD));
196 dprintk0(sd->dev, "tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
197 tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
198 tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
199 dprintk0(sd->dev, "tvp5150: Macrovision on counter = 0x%02x\n",
200 tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
201 dprintk0(sd->dev, "tvp5150: Macrovision off counter = 0x%02x\n",
202 tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
203 dprintk0(sd->dev, "tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
204 (tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
205 dprintk0(sd->dev, "tvp5150: Device ID = %02x%02x\n",
206 tvp5150_read(sd, TVP5150_MSB_DEV_ID),
207 tvp5150_read(sd, TVP5150_LSB_DEV_ID));
208 dprintk0(sd->dev, "tvp5150: ROM version = (hex) %02x.%02x\n",
209 tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
210 tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
211 dprintk0(sd->dev, "tvp5150: Vertical line count = 0x%02x%02x\n",
212 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
213 tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
214 dprintk0(sd->dev, "tvp5150: Interrupt status register B = 0x%02x\n",
215 tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
216 dprintk0(sd->dev, "tvp5150: Interrupt active register B = 0x%02x\n",
217 tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
218 dprintk0(sd->dev, "tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
219 tvp5150_read(sd, TVP5150_STATUS_REG_1),
220 tvp5150_read(sd, TVP5150_STATUS_REG_2),
221 tvp5150_read(sd, TVP5150_STATUS_REG_3),
222 tvp5150_read(sd, TVP5150_STATUS_REG_4),
223 tvp5150_read(sd, TVP5150_STATUS_REG_5));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200224
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300225 dump_reg_range(sd, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
226 TVP5150_TELETEXT_FIL1_END, 8);
227 dump_reg_range(sd, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
228 TVP5150_TELETEXT_FIL2_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200229
Mauro Carvalho Chehabad0e3742016-11-13 05:34:12 -0200230 dprintk0(sd->dev, "tvp5150: Teletext filter enable = 0x%02x\n",
231 tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
232 dprintk0(sd->dev, "tvp5150: Interrupt status register A = 0x%02x\n",
233 tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
234 dprintk0(sd->dev, "tvp5150: Interrupt enable register A = 0x%02x\n",
235 tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
236 dprintk0(sd->dev, "tvp5150: Interrupt configuration = 0x%02x\n",
237 tvp5150_read(sd, TVP5150_INT_CONF));
238 dprintk0(sd->dev, "tvp5150: VDP status register = 0x%02x\n",
239 tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
240 dprintk0(sd->dev, "tvp5150: FIFO word count = 0x%02x\n",
241 tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
242 dprintk0(sd->dev, "tvp5150: FIFO interrupt threshold = 0x%02x\n",
243 tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
244 dprintk0(sd->dev, "tvp5150: FIFO reset = 0x%02x\n",
245 tvp5150_read(sd, TVP5150_FIFO_RESET));
246 dprintk0(sd->dev, "tvp5150: Line number interrupt = 0x%02x\n",
247 tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
248 dprintk0(sd->dev, "tvp5150: Pixel alignment register = 0x%02x%02x\n",
249 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
250 tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
251 dprintk0(sd->dev, "tvp5150: FIFO output control = 0x%02x\n",
252 tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
253 dprintk0(sd->dev, "tvp5150: Full field enable = 0x%02x\n",
254 tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
255 dprintk0(sd->dev, "tvp5150: Full field mode register = 0x%02x\n",
256 tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200257
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300258 dump_reg_range(sd, "CC data", TVP5150_CC_DATA_INI,
259 TVP5150_CC_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200260
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300261 dump_reg_range(sd, "WSS data", TVP5150_WSS_DATA_INI,
262 TVP5150_WSS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200263
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300264 dump_reg_range(sd, "VPS data", TVP5150_VPS_DATA_INI,
265 TVP5150_VPS_DATA_END, 8);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200266
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300267 dump_reg_range(sd, "VITC data", TVP5150_VITC_DATA_INI,
268 TVP5150_VITC_DATA_END, 10);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200269
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300270 dump_reg_range(sd, "Line mode", TVP5150_LINE_MODE_INI,
271 TVP5150_LINE_MODE_END, 8);
272 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800273}
274
275/****************************************************************************
276 Basic functions
277 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800278
Laurent Pinchart6e98bee2016-12-08 20:22:42 -0200279static void tvp5150_selmux(struct v4l2_subdev *sd)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800280{
Paul Walmsley2962fc02010-10-09 01:31:40 -0300281 int opmode = 0;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300282 struct tvp5150 *decoder = to_tvp5150(sd);
Marco Felsch8a7441b2018-06-28 12:20:36 -0400283 unsigned int mask, val;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300284 int input = 0;
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800285
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200286 /* Only tvp5150am1 and tvp5151 have signal generator support */
287 if ((decoder->dev_id == 0x5150 && decoder->rom_ver == 0x0400) ||
288 (decoder->dev_id == 0x5151 && decoder->rom_ver == 0x0100)) {
289 if (!decoder->enable)
290 input = 8;
291 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800292
Hans Verkuil5325b422009-04-02 11:26:22 -0300293 switch (decoder->input) {
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300294 case TVP5150_COMPOSITE1:
295 input |= 2;
296 /* fall through */
297 case TVP5150_COMPOSITE0:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200298 break;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300299 case TVP5150_SVIDEO:
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200300 default:
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300301 input |= 1;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200302 break;
303 }
304
Marco Felsch430f35b2020-03-12 11:31:56 +0100305 dev_dbg_lvl(sd->dev, 1, debug,
306 "Selecting video route: route input=%s, output=%s => tvp5150 input=0x%02x, opmode=0x%02x\n",
307 decoder->input == 0 ? "aip1a" :
308 decoder->input == 2 ? "aip1b" : "svideo",
309 decoder->output == 0 ? "normal" : "black-frame-gen",
310 input, opmode);
Mauro Carvalho Chehab12500f02006-08-18 07:31:10 -0300311
Philipp Zabel28b9e222018-06-28 12:20:35 -0400312 regmap_write(decoder->regmap, TVP5150_OP_MODE_CTL, opmode);
313 regmap_write(decoder->regmap, TVP5150_VD_IN_SRC_SEL_1, input);
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300314
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200315 /*
316 * Setup the FID/GLCO/VLK/HVLK and INTREQ/GPCL/VBLK output signals. For
317 * S-Video we output the vertical lock (VLK) signal on FID/GLCO/VLK/HVLK
318 * and set INTREQ/GPCL/VBLK to logic 0. For composite we output the
319 * field indicator (FID) signal on FID/GLCO/VLK/HVLK and set
320 * INTREQ/GPCL/VBLK to logic 1.
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300321 */
Marco Felsch8a7441b2018-06-28 12:20:36 -0400322 mask = TVP5150_MISC_CTL_GPCL | TVP5150_MISC_CTL_HVLK;
Hans Verkuil5325b422009-04-02 11:26:22 -0300323 if (decoder->input == TVP5150_SVIDEO)
Marco Felsch8a7441b2018-06-28 12:20:36 -0400324 val = TVP5150_MISC_CTL_HVLK;
Mauro Carvalho Chehabf4b8b3a2007-11-03 22:40:24 -0300325 else
Marco Felsch8a7441b2018-06-28 12:20:36 -0400326 val = TVP5150_MISC_CTL_GPCL;
327 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800328};
329
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200330struct i2c_reg_value {
331 unsigned char reg;
332 unsigned char value;
333};
334
335/* Default values as sugested at TVP5150AM1 datasheet */
336static const struct i2c_reg_value tvp5150_init_default[] = {
337 { /* 0x00 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400338 TVP5150_VD_IN_SRC_SEL_1, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200339 },
340 { /* 0x01 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400341 TVP5150_ANAL_CHL_CTL, 0x15
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200342 },
343 { /* 0x02 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400344 TVP5150_OP_MODE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200345 },
346 { /* 0x03 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400347 TVP5150_MISC_CTL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200348 },
349 { /* 0x06 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400350 TVP5150_COLOR_KIL_THSH_CTL, 0x10
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200351 },
352 { /* 0x07 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400353 TVP5150_LUMA_PROC_CTL_1, 0x60
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200354 },
355 { /* 0x08 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400356 TVP5150_LUMA_PROC_CTL_2, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200357 },
358 { /* 0x09 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400359 TVP5150_BRIGHT_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200360 },
361 { /* 0x0a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400362 TVP5150_SATURATION_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200363 },
364 { /* 0x0b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400365 TVP5150_HUE_CTL, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200366 },
367 { /* 0x0c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400368 TVP5150_CONTRAST_CTL, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200369 },
370 { /* 0x0d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400371 TVP5150_DATA_RATE_SEL, 0x47
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200372 },
373 { /* 0x0e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400374 TVP5150_LUMA_PROC_CTL_3, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200375 },
376 { /* 0x0f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400377 TVP5150_CONF_SHARED_PIN, 0x08
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200378 },
379 { /* 0x11 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400380 TVP5150_ACT_VD_CROP_ST_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200381 },
382 { /* 0x12 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400383 TVP5150_ACT_VD_CROP_ST_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200384 },
385 { /* 0x13 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400386 TVP5150_ACT_VD_CROP_STP_MSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200387 },
388 { /* 0x14 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400389 TVP5150_ACT_VD_CROP_STP_LSB, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200390 },
391 { /* 0x15 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400392 TVP5150_GENLOCK, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200393 },
394 { /* 0x16 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400395 TVP5150_HORIZ_SYNC_START, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200396 },
397 { /* 0x18 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400398 TVP5150_VERT_BLANKING_START, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200399 },
400 { /* 0x19 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400401 TVP5150_VERT_BLANKING_STOP, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200402 },
403 { /* 0x1a */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400404 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200405 },
406 { /* 0x1b */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400407 TVP5150_CHROMA_PROC_CTL_2, 0x14
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200408 },
409 { /* 0x1c */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400410 TVP5150_INT_RESET_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200411 },
412 { /* 0x1d */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400413 TVP5150_INT_ENABLE_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200414 },
415 { /* 0x1e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400416 TVP5150_INTT_CONFIG_REG_B, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200417 },
418 { /* 0x28 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400419 TVP5150_VIDEO_STD, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200420 },
421 { /* 0x2e */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400422 TVP5150_MACROVISION_ON_CTR, 0x0f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200423 },
424 { /* 0x2f */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400425 TVP5150_MACROVISION_OFF_CTR, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200426 },
427 { /* 0xbb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400428 TVP5150_TELETEXT_FIL_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200429 },
430 { /* 0xc0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400431 TVP5150_INT_STATUS_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200432 },
433 { /* 0xc1 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400434 TVP5150_INT_ENABLE_REG_A, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200435 },
436 { /* 0xc2 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400437 TVP5150_INT_CONF, 0x04
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200438 },
439 { /* 0xc8 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400440 TVP5150_FIFO_INT_THRESHOLD, 0x80
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200441 },
442 { /* 0xc9 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400443 TVP5150_FIFO_RESET, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200444 },
445 { /* 0xca */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400446 TVP5150_LINE_NUMBER_INT, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200447 },
448 { /* 0xcb */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400449 TVP5150_PIX_ALIGN_REG_LOW, 0x4e
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200450 },
451 { /* 0xcc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400452 TVP5150_PIX_ALIGN_REG_HIGH, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200453 },
454 { /* 0xcd */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400455 TVP5150_FIFO_OUT_CTRL, 0x01
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200456 },
457 { /* 0xcf */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400458 TVP5150_FULL_FIELD_ENA, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200459 },
460 { /* 0xd0 */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400461 TVP5150_LINE_MODE_INI, 0x00
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200462 },
463 { /* 0xfc */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400464 TVP5150_FULL_FIELD_MODE_REG, 0x7f
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200465 },
466 { /* end of data */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400467 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200468 }
469};
470
471/* Default values as sugested at TVP5150AM1 datasheet */
472static const struct i2c_reg_value tvp5150_init_enable[] = {
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400473 { /* Automatic offset and AGC enabled */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200474 TVP5150_ANAL_CHL_CTL, 0x15
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400475 }, { /* Activate YCrCb output 0x9 or 0xd ? */
Laurent Pinchartb4b2de32016-12-09 09:47:18 -0200476 TVP5150_MISC_CTL, TVP5150_MISC_CTL_GPCL |
477 TVP5150_MISC_CTL_INTREQ_OE |
478 TVP5150_MISC_CTL_YCBCR_OE |
479 TVP5150_MISC_CTL_SYNC_OE |
480 TVP5150_MISC_CTL_VBLANK |
481 TVP5150_MISC_CTL_CLOCK_OE,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400482 }, { /* Activates video std autodetection for all standards */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200483 TVP5150_AUTOSW_MSK, 0x0
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400484 }, { /* Default format: 0x47. For 4:2:2: 0x40 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200485 TVP5150_DATA_RATE_SEL, 0x47
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400486 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200487 TVP5150_CHROMA_PROC_CTL_1, 0x0c
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400488 }, {
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200489 TVP5150_CHROMA_PROC_CTL_2, 0x54
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400490 }, { /* Non documented, but initialized on WinTV USB2 */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200491 0x27, 0x20
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400492 }, {
493 0xff, 0xff
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200494 }
495};
496
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200497struct tvp5150_vbi_type {
498 unsigned int vbi_type;
499 unsigned int ini_line;
500 unsigned int end_line;
501 unsigned int by_field :1;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200502};
503
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200504struct i2c_vbi_ram_value {
505 u16 reg;
506 struct tvp5150_vbi_type type;
507 unsigned char values[16];
508};
509
510/* This struct have the values for each supported VBI Standard
511 * by
512 tvp5150_vbi_types should follow the same order as vbi_ram_default
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200513 * value 0 means rom position 0x10, value 1 means rom position 0x30
514 * and so on. There are 16 possible locations from 0 to 15.
515 */
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200516
Nasser Afshin65dc7602018-04-02 18:23:19 -0400517static struct i2c_vbi_ram_value vbi_ram_default[] = {
518
Nasser Afshin9bfd8f82018-04-02 18:23:18 -0400519 /*
520 * FIXME: Current api doesn't handle all VBI types, those not
521 * yet supported are placed under #if 0
522 */
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300523#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500524 [0] = {0x010, /* Teletext, SECAM, WST System A */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400525 {V4L2_SLICED_TELETEXT_SECAM, 6, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200526 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
527 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200528 },
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300529#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500530 [1] = {0x030, /* Teletext, PAL, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400531 {V4L2_SLICED_TELETEXT_B, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200532 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
533 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200534 },
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300535#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500536 [2] = {0x050, /* Teletext, PAL, WST System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400537 {V4L2_SLICED_TELETEXT_PAL_C, 6, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200538 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
539 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200540 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500541 [3] = {0x070, /* Teletext, NTSC, WST System B */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400542 {V4L2_SLICED_TELETEXT_NTSC_B, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200543 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
544 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200545 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500546 [4] = {0x090, /* Tetetext, NTSC NABTS System C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400547 {V4L2_SLICED_TELETEXT_NTSC_C, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200548 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
549 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200550 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500551 [5] = {0x0b0, /* Teletext, NTSC-J, NABTS System D */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400552 {V4L2_SLICED_TELETEXT_NTSC_D, 10, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200553 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
554 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200555 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500556 [6] = {0x0d0, /* Closed Caption, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400557 {V4L2_SLICED_CAPTION_625, 22, 22, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200558 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
559 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200560 },
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300561#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500562 [7] = {0x0f0, /* Closed Caption, NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400563 {V4L2_SLICED_CAPTION_525, 21, 21, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200564 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
565 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200566 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500567 [8] = {0x110, /* Wide Screen Signal, PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400568 {V4L2_SLICED_WSS_625, 23, 23, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200569 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
570 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200571 },
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300572#if 0
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500573 [9] = {0x130, /* Wide Screen Signal, NTSC C */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400574 {V4L2_SLICED_WSS_525, 20, 20, 1},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200575 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
576 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200577 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500578 [10] = {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400579 {V4l2_SLICED_VITC_625, 6, 22, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200580 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
581 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200582 },
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500583 [11] = {0x170, /* Vertical Interval Timecode (VITC), NTSC */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400584 {V4l2_SLICED_VITC_525, 10, 20, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200585 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
586 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200587 },
Hans Verkuil9bc7400a2006-03-29 18:02:51 -0300588#endif
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500589 [12] = {0x190, /* Video Program System (VPS), PAL */
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400590 {V4L2_SLICED_VPS, 16, 16, 0},
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200591 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
592 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200593 },
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200594 /* 0x1d0 User programmable */
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200595};
596
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300597static int tvp5150_write_inittab(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200598 const struct i2c_reg_value *regs)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200599{
Philipp Zabel28b9e222018-06-28 12:20:35 -0400600 struct tvp5150 *decoder = to_tvp5150(sd);
601
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200602 while (regs->reg != 0xff) {
Philipp Zabel28b9e222018-06-28 12:20:35 -0400603 regmap_write(decoder->regmap, regs->reg, regs->value);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200604 regs++;
605 }
606 return 0;
607}
608
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500609static int tvp5150_vdp_init(struct v4l2_subdev *sd)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200610{
Philipp Zabel28b9e222018-06-28 12:20:35 -0400611 struct tvp5150 *decoder = to_tvp5150(sd);
612 struct regmap *map = decoder->regmap;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200613 unsigned int i;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500614 int j;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200615
616 /* Disable Full Field */
Philipp Zabel28b9e222018-06-28 12:20:35 -0400617 regmap_write(map, TVP5150_FULL_FIELD_ENA, 0);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200618
619 /* Before programming, Line mode should be at 0xff */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300620 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400621 regmap_write(map, i, 0xff);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200622
623 /* Load Ram Table */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500624 for (j = 0; j < ARRAY_SIZE(vbi_ram_default); j++) {
625 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[j];
626
627 if (!regs->type.vbi_type)
628 continue;
629
Philipp Zabel28b9e222018-06-28 12:20:35 -0400630 regmap_write(map, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
631 regmap_write(map, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200632
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300633 for (i = 0; i < 16; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400634 regmap_write(map, TVP5150_VDP_CONF_RAM_DATA,
635 regs->values[i]);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200636 }
637 return 0;
638}
639
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200640/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300641static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200642 struct v4l2_sliced_vbi_cap *cap)
643{
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500644 int line, i;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200645
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200646 dev_dbg_lvl(sd->dev, 1, debug, "g_sliced_vbi_cap\n");
Nasser Afshinbb7a3682018-04-02 18:23:20 -0400647 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200648
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500649 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
650 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
651
652 if (!regs->type.vbi_type)
653 continue;
654
655 for (line = regs->type.ini_line;
656 line <= regs->type.end_line;
657 line++) {
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200658 cap->service_lines[0][line] |= regs->type.vbi_type;
659 }
660 cap->service_set |= regs->type.vbi_type;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200661 }
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300662 return 0;
Mauro Carvalho Chehab6ac48b42006-01-23 17:11:05 -0200663}
664
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200665/* Set vbi processing
666 * type - one of tvp5150_vbi_types
667 * line - line to gather data
668 * fields: bit 0 field1, bit 1, field2
669 * flags (default=0xf0) is a bitmask, were set means:
670 * bit 7: enable filtering null bytes on CC
671 * bit 6: send data also to FIFO
672 * bit 5: don't allow data with errors on FIFO
673 * bit 4: enable ECC when possible
674 * pix_align = pix alignment:
675 * LSB = field1
676 * MSB = field2
677 */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300678static int tvp5150_set_vbi(struct v4l2_subdev *sd,
Nasser Afshin4efcd5e2018-04-02 18:23:17 -0400679 unsigned int type, u8 flags, int line,
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200680 const int fields)
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200681{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300682 struct tvp5150 *decoder = to_tvp5150(sd);
683 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200684 u8 reg;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500685 int i, pos = 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200686
687 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200688 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200689 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300690 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200691 /* Don't follow NTSC Line number convension */
692 line += 3;
693 }
694
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300695 if (line < 6 || line > 27)
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200696 return 0;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200697
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500698 for (i = 0; i < ARRAY_SIZE(vbi_ram_default); i++) {
699 const struct i2c_vbi_ram_value *regs = &vbi_ram_default[i];
700
701 if (!regs->type.vbi_type)
702 continue;
703
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200704 if ((type & regs->type.vbi_type) &&
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300705 (line >= regs->type.ini_line) &&
706 (line <= regs->type.end_line))
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200707 break;
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200708 pos++;
709 }
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300710
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300711 type = pos | (flags & 0xf0);
712 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200713
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300714 if (fields & 1)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400715 regmap_write(decoder->regmap, reg, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200716
Gustavo A. R. Silvab3d930aa2017-06-23 19:37:00 -0300717 if (fields & 2)
Philipp Zabel28b9e222018-06-28 12:20:35 -0400718 regmap_write(decoder->regmap, reg + 1, type);
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200719
Mauro Carvalho Chehab2701dac2006-01-23 17:11:06 -0200720 return type;
Mauro Carvalho Chehab3ad96832006-01-23 17:11:05 -0200721}
722
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500723static int tvp5150_get_vbi(struct v4l2_subdev *sd, int line)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200724{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300725 struct tvp5150 *decoder = to_tvp5150(sd);
726 v4l2_std_id std = decoder->norm;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200727 u8 reg;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300728 int pos, type = 0;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300729 int i, ret = 0;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200730
731 if (std == V4L2_STD_ALL) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200732 dev_err(sd->dev, "VBI can't be configured without knowing number of lines\n");
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200733 return 0;
Roel Kluin7d5b7b92008-03-09 21:19:13 -0300734 } else if (std & V4L2_STD_625_50) {
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200735 /* Don't follow NTSC Line number convension */
736 line += 3;
737 }
738
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300739 if (line < 6 || line > 27)
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200740 return 0;
741
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300742 reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200743
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300744 for (i = 0; i <= 1; i++) {
745 ret = tvp5150_read(sd, reg + i);
746 if (ret < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200747 dev_err(sd->dev, "%s: failed with error = %d\n",
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300748 __func__, ret);
749 return 0;
750 }
751 pos = ret & 0x0f;
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500752 if (pos < ARRAY_SIZE(vbi_ram_default))
753 type |= vbi_ram_default[pos].type.vbi_type;
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -0300754 }
Mauro Carvalho Chehab12db5602006-01-23 17:11:08 -0200755
756 return type;
757}
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -0800758
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300759static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
760{
761 struct tvp5150 *decoder = to_tvp5150(sd);
762 int fmt = 0;
763
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200764 /* First tests should be against specific std */
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800765
Hans Verkuil26811ae2013-05-29 10:19:06 -0300766 if (std == V4L2_STD_NTSC_443) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300767 fmt = VIDEO_STD_NTSC_4_43_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300768 } else if (std == V4L2_STD_PAL_M) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300769 fmt = VIDEO_STD_PAL_M_BIT;
Hans Verkuil26811ae2013-05-29 10:19:06 -0300770 } else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300771 fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200772 } else {
773 /* Then, test against generic ones */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300774 if (std & V4L2_STD_NTSC)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300775 fmt = VIDEO_STD_NTSC_MJ_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300776 else if (std & V4L2_STD_PAL)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300777 fmt = VIDEO_STD_PAL_BDGHIN_BIT;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300778 else if (std & V4L2_STD_SECAM)
Javier Martinez Canillas2da12fc2011-11-04 13:23:21 -0300779 fmt = VIDEO_STD_SECAM_BIT;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200780 }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800781
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -0200782 dev_dbg_lvl(sd->dev, 1, debug, "Set video std register to %d.\n", fmt);
Philipp Zabel28b9e222018-06-28 12:20:35 -0400783 regmap_write(decoder->regmap, TVP5150_VIDEO_STD, fmt);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200784 return 0;
785}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800786
Marco Felsch0db87cc2018-06-28 12:20:49 -0400787static int tvp5150_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
788{
789 struct tvp5150 *decoder = to_tvp5150(sd);
790
791 *std = decoder->norm;
792
793 return 0;
794}
795
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300796static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200797{
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300798 struct tvp5150 *decoder = to_tvp5150(sd);
Marco Felschbaf17822020-03-12 11:31:55 +0100799 struct tvp5150_connector *cur_con = decoder->cur_connector;
800 v4l2_std_id supported_stds;
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300801
802 if (decoder->norm == std)
803 return 0;
804
Marco Felschbaf17822020-03-12 11:31:55 +0100805 /* In case of no of-connectors are available no limitations are made */
806 if (!decoder->connectors_num)
807 supported_stds = V4L2_STD_ALL;
808 else
809 supported_stds = cur_con->base.connector.analog.sdtv_stds;
810
811 /*
812 * Check if requested std or group of std's is/are supported by the
813 * connector.
814 */
815 if ((supported_stds & std) == 0)
816 return -EINVAL;
817
Javier Martin963ddc62012-01-31 05:23:46 -0300818 /* Change cropping height limits */
819 if (std & V4L2_STD_525_60)
820 decoder->rect.height = TVP5150_V_MAX_525_60;
821 else
822 decoder->rect.height = TVP5150_V_MAX_OTHERS;
823
Marco Felschbaf17822020-03-12 11:31:55 +0100824 /* Set only the specific supported std in case of group of std's. */
825 decoder->norm = supported_stds & std;
Javier Martin963ddc62012-01-31 05:23:46 -0300826
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300827 return tvp5150_set_std(sd, std);
828}
829
Philipp Zabel15695862018-06-28 12:20:41 -0400830static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
831{
832 int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
833
834 switch (val & 0x0F) {
835 case 0x01:
836 return V4L2_STD_NTSC;
837 case 0x03:
838 return V4L2_STD_PAL;
839 case 0x05:
840 return V4L2_STD_PAL_M;
841 case 0x07:
842 return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
843 case 0x09:
844 return V4L2_STD_NTSC_443;
845 case 0xb:
846 return V4L2_STD_SECAM;
847 default:
848 return V4L2_STD_UNKNOWN;
849 }
850}
851
Mauro Carvalho Chehab5bd1d912018-08-01 10:09:24 -0400852static int query_lock(struct v4l2_subdev *sd)
853{
854 struct tvp5150 *decoder = to_tvp5150(sd);
855 int status;
856
857 if (decoder->irq)
858 return decoder->lock;
859
860 regmap_read(decoder->regmap, TVP5150_STATUS_REG_1, &status);
861
862 /* For standard detection, we need the 3 locks */
863 return (status & 0x0e) == 0x0e;
864}
865
Philipp Zabelee9a6ff2018-06-28 12:20:48 -0400866static int tvp5150_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
867{
Mauro Carvalho Chehab5bd1d912018-08-01 10:09:24 -0400868 *std_id = query_lock(sd) ? tvp5150_read_std(sd) : V4L2_STD_UNKNOWN;
Philipp Zabelee9a6ff2018-06-28 12:20:48 -0400869
870 return 0;
871}
872
Philipp Zabel2f0a5c62018-06-28 12:20:46 -0400873static const struct v4l2_event tvp5150_ev_fmt = {
874 .type = V4L2_EVENT_SOURCE_CHANGE,
875 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
876};
877
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400878static irqreturn_t tvp5150_isr(int irq, void *dev_id)
879{
880 struct tvp5150 *decoder = dev_id;
881 struct regmap *map = decoder->regmap;
Philipp Zabel62a764e2018-06-28 12:20:45 -0400882 unsigned int mask, active = 0, status = 0;
883
884 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
885 TVP5150_MISC_CTL_CLOCK_OE;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400886
887 regmap_read(map, TVP5150_INT_STATUS_REG_A, &status);
888 if (status) {
889 regmap_write(map, TVP5150_INT_STATUS_REG_A, status);
890
Philipp Zabel62a764e2018-06-28 12:20:45 -0400891 if (status & TVP5150_INT_A_LOCK) {
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400892 decoder->lock = !!(status & TVP5150_INT_A_LOCK_STATUS);
Philipp Zabel7bb3c332018-06-28 12:20:47 -0400893 dev_dbg_lvl(decoder->sd.dev, 1, debug,
894 "sync lo%s signal\n",
895 decoder->lock ? "ck" : "ss");
Philipp Zabel2f0a5c62018-06-28 12:20:46 -0400896 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
Philipp Zabel62a764e2018-06-28 12:20:45 -0400897 regmap_update_bits(map, TVP5150_MISC_CTL, mask,
898 decoder->lock ? decoder->oe : 0);
899 }
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400900
901 return IRQ_HANDLED;
902 }
903
904 regmap_read(map, TVP5150_INT_ACTIVE_REG_B, &active);
905 if (active) {
906 status = 0;
907 regmap_read(map, TVP5150_INT_STATUS_REG_B, &status);
908 if (status)
909 regmap_write(map, TVP5150_INT_RESET_REG_B, status);
910 }
911
912 return IRQ_HANDLED;
913}
914
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300915static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
916{
917 struct tvp5150 *decoder = to_tvp5150(sd);
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400918 struct regmap *map = decoder->regmap;
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200919
920 /* Initializes TVP5150 to its default values */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300921 tvp5150_write_inittab(sd, tvp5150_init_default);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200922
Philipp Zabel8e4c97e2018-06-28 12:20:44 -0400923 if (decoder->irq) {
924 /* Configure pins: FID, VSYNC, INTREQ, SCLK */
925 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x0);
926 /* Set interrupt polarity to active high */
927 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE | 0x1);
928 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x1);
929 } else {
930 /* Configure pins: FID, VSYNC, GPCL/VBLK, SCLK */
931 regmap_write(map, TVP5150_CONF_SHARED_PIN, 0x2);
932 /* Keep interrupt polarity active low */
933 regmap_write(map, TVP5150_INT_CONF, TVP5150_VDPOE);
934 regmap_write(map, TVP5150_INTT_CONFIG_REG_B, 0x0);
935 }
Philipp Zabel8105e1b2018-06-28 12:20:43 -0400936
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200937 /* Initializes VDP registers */
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -0500938 tvp5150_vdp_init(sd);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200939
940 /* Selects decoder input */
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300941 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -0800942
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200943 /* Initialize image preferences */
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300944 v4l2_ctrl_handler_setup(&decoder->hdl);
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -0200945
Philipp Zabel1bb086b2018-06-28 12:20:42 -0400946 return 0;
947}
948
949static int tvp5150_enable(struct v4l2_subdev *sd)
950{
951 struct tvp5150 *decoder = to_tvp5150(sd);
952 v4l2_std_id std;
953
954 /* Initializes TVP5150 to stream enabled values */
955 tvp5150_write_inittab(sd, tvp5150_init_enable);
956
Philipp Zabel15695862018-06-28 12:20:41 -0400957 if (decoder->norm == V4L2_STD_ALL)
958 std = tvp5150_read_std(sd);
959 else
960 std = decoder->norm;
961
962 /* Disable autoswitch mode */
963 tvp5150_set_std(sd, std);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200964
Philipp Zabel62a764e2018-06-28 12:20:45 -0400965 /*
966 * Enable the YCbCr and clock outputs. In discrete sync mode
967 * (non-BT.656) additionally enable the the sync outputs.
968 */
969 switch (decoder->mbus_type) {
970 case V4L2_MBUS_PARALLEL:
Marco Felsch8a7441b2018-06-28 12:20:36 -0400971 /* 8-bit 4:2:2 YUV with discrete sync output */
972 regmap_update_bits(decoder->regmap, TVP5150_DATA_RATE_SEL,
973 0x7, 0x0);
Philipp Zabel62a764e2018-06-28 12:20:45 -0400974 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
975 TVP5150_MISC_CTL_CLOCK_OE |
976 TVP5150_MISC_CTL_SYNC_OE;
977 break;
978 case V4L2_MBUS_BT656:
979 decoder->oe = TVP5150_MISC_CTL_YCBCR_OE |
980 TVP5150_MISC_CTL_CLOCK_OE;
981 break;
982 default:
983 return -EINVAL;
984 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -0200985
Hans Verkuil6b8fe022008-12-18 11:17:25 -0300986 return 0;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -0800987};
988
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300989static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800990{
Hans Verkuil6c45ec72010-12-12 08:45:43 -0300991 struct v4l2_subdev *sd = to_sd(ctrl);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -0200992 struct tvp5150 *decoder = to_tvp5150(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800993
994 switch (ctrl->id) {
995 case V4L2_CID_BRIGHTNESS:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400996 regmap_write(decoder->regmap, TVP5150_BRIGHT_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800997 return 0;
998 case V4L2_CID_CONTRAST:
Philipp Zabel28b9e222018-06-28 12:20:35 -0400999 regmap_write(decoder->regmap, TVP5150_CONTRAST_CTL, ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001000 return 0;
1001 case V4L2_CID_SATURATION:
Philipp Zabel28b9e222018-06-28 12:20:35 -04001002 regmap_write(decoder->regmap, TVP5150_SATURATION_CTL,
1003 ctrl->val);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001004 return 0;
1005 case V4L2_CID_HUE:
Philipp Zabel28b9e222018-06-28 12:20:35 -04001006 regmap_write(decoder->regmap, TVP5150_HUE_CTL, ctrl->val);
Marco Felsch2d29bcc2018-06-28 12:20:34 -04001007 return 0;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001008 case V4L2_CID_TEST_PATTERN:
1009 decoder->enable = ctrl->val ? false : true;
1010 tvp5150_selmux(sd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001011 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001012 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001013 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001014}
1015
Marco Felsch5cb82942018-06-28 12:20:39 -04001016static void tvp5150_set_default(v4l2_std_id std, struct v4l2_rect *crop)
1017{
1018 /* Default is no cropping */
1019 crop->top = 0;
1020 crop->left = 0;
1021 crop->width = TVP5150_H_MAX;
1022 if (std & V4L2_STD_525_60)
1023 crop->height = TVP5150_V_MAX_525_60;
1024 else
1025 crop->height = TVP5150_V_MAX_OTHERS;
1026}
1027
Marco Felsch46fe6e72020-03-12 11:31:48 +01001028static struct v4l2_rect *
1029tvp5150_get_pad_crop(struct tvp5150 *decoder,
1030 struct v4l2_subdev_pad_config *cfg, unsigned int pad,
1031 enum v4l2_subdev_format_whence which)
1032{
1033 switch (which) {
1034 case V4L2_SUBDEV_FORMAT_ACTIVE:
1035 return &decoder->rect;
1036 case V4L2_SUBDEV_FORMAT_TRY:
1037#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1038 return v4l2_subdev_get_try_crop(&decoder->sd, cfg, pad);
1039#else
1040 return ERR_PTR(-EINVAL);
1041#endif
1042 default:
1043 return ERR_PTR(-EINVAL);
1044 }
1045}
1046
Hans Verkuilda298c62015-04-09 04:02:34 -03001047static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
Marco Felsch5cb82942018-06-28 12:20:39 -04001048 struct v4l2_subdev_pad_config *cfg,
1049 struct v4l2_subdev_format *format)
Javier Martinec2c4f32012-01-05 10:57:39 -03001050{
Hans Verkuilda298c62015-04-09 04:02:34 -03001051 struct v4l2_mbus_framefmt *f;
Javier Martinec2c4f32012-01-05 10:57:39 -03001052 struct tvp5150 *decoder = to_tvp5150(sd);
Javier Martinec2c4f32012-01-05 10:57:39 -03001053
Mauro Carvalho Chehabbc322c02018-08-01 06:10:05 -04001054 if (!format || (format->pad != TVP5150_PAD_VID_OUT))
Javier Martinec2c4f32012-01-05 10:57:39 -03001055 return -EINVAL;
1056
Hans Verkuilda298c62015-04-09 04:02:34 -03001057 f = &format->format;
1058
Javier Martin963ddc62012-01-31 05:23:46 -03001059 f->width = decoder->rect.width;
Javier Martinez Canillas1831af02018-06-10 16:43:02 -04001060 f->height = decoder->rect.height / 2;
Javier Martinec2c4f32012-01-05 10:57:39 -03001061
Marco Felsch5cb82942018-06-28 12:20:39 -04001062 f->code = TVP5150_MBUS_FMT;
1063 f->field = TVP5150_FIELD;
1064 f->colorspace = TVP5150_COLORSPACE;
Javier Martinec2c4f32012-01-05 10:57:39 -03001065
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001066 dev_dbg_lvl(sd->dev, 1, debug, "width = %d, height = %d\n", f->width,
Marco Felsch5cb82942018-06-28 12:20:39 -04001067 f->height);
Javier Martinec2c4f32012-01-05 10:57:39 -03001068 return 0;
1069}
1070
Marco Felsch46fe6e72020-03-12 11:31:48 +01001071static unsigned int tvp5150_get_hmax(struct v4l2_subdev *sd)
Javier Martin963ddc62012-01-31 05:23:46 -03001072{
Javier Martin963ddc62012-01-31 05:23:46 -03001073 struct tvp5150 *decoder = to_tvp5150(sd);
1074 v4l2_std_id std;
Javier Martin963ddc62012-01-31 05:23:46 -03001075
1076 /* Calculate height based on current standard */
1077 if (decoder->norm == V4L2_STD_ALL)
1078 std = tvp5150_read_std(sd);
1079 else
1080 std = decoder->norm;
1081
Marco Felsch46fe6e72020-03-12 11:31:48 +01001082 return (std & V4L2_STD_525_60) ?
1083 TVP5150_V_MAX_525_60 : TVP5150_V_MAX_OTHERS;
1084}
Javier Martin963ddc62012-01-31 05:23:46 -03001085
Marco Felsch46fe6e72020-03-12 11:31:48 +01001086static void tvp5150_set_hw_selection(struct v4l2_subdev *sd,
1087 struct v4l2_rect *rect)
1088{
1089 struct tvp5150 *decoder = to_tvp5150(sd);
1090 unsigned int hmax = tvp5150_get_hmax(sd);
Javier Martin963ddc62012-01-31 05:23:46 -03001091
Marco Felschb4125e52020-03-12 11:31:47 +01001092 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START, rect->top);
Philipp Zabel28b9e222018-06-28 12:20:35 -04001093 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP,
Marco Felschb4125e52020-03-12 11:31:47 +01001094 rect->top + rect->height - hmax);
Philipp Zabel28b9e222018-06-28 12:20:35 -04001095 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_MSB,
Marco Felschb4125e52020-03-12 11:31:47 +01001096 rect->left >> TVP5150_CROP_SHIFT);
Philipp Zabel28b9e222018-06-28 12:20:35 -04001097 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_ST_LSB,
Marco Felschb4125e52020-03-12 11:31:47 +01001098 rect->left | (1 << TVP5150_CROP_SHIFT));
Philipp Zabel28b9e222018-06-28 12:20:35 -04001099 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_MSB,
Marco Felschb4125e52020-03-12 11:31:47 +01001100 (rect->left + rect->width - TVP5150_MAX_CROP_LEFT) >>
Philipp Zabel28b9e222018-06-28 12:20:35 -04001101 TVP5150_CROP_SHIFT);
1102 regmap_write(decoder->regmap, TVP5150_ACT_VD_CROP_STP_LSB,
Marco Felschb4125e52020-03-12 11:31:47 +01001103 rect->left + rect->width - TVP5150_MAX_CROP_LEFT);
Marco Felsch46fe6e72020-03-12 11:31:48 +01001104}
Javier Martin963ddc62012-01-31 05:23:46 -03001105
Marco Felsch46fe6e72020-03-12 11:31:48 +01001106static int tvp5150_set_selection(struct v4l2_subdev *sd,
1107 struct v4l2_subdev_pad_config *cfg,
1108 struct v4l2_subdev_selection *sel)
1109{
1110 struct tvp5150 *decoder = to_tvp5150(sd);
1111 struct v4l2_rect *rect = &sel->r;
1112 struct v4l2_rect *crop;
1113 unsigned int hmax;
1114
1115 if (sel->target != V4L2_SEL_TGT_CROP)
1116 return -EINVAL;
1117
1118 dev_dbg_lvl(sd->dev, 1, debug, "%s left=%d, top=%d, width=%d, height=%d\n",
1119 __func__, rect->left, rect->top, rect->width, rect->height);
1120
1121 /* tvp5150 has some special limits */
1122 rect->left = clamp(rect->left, 0, TVP5150_MAX_CROP_LEFT);
1123 rect->top = clamp(rect->top, 0, TVP5150_MAX_CROP_TOP);
1124 hmax = tvp5150_get_hmax(sd);
1125
1126 /*
1127 * alignments:
1128 * - width = 2 due to UYVY colorspace
1129 * - height, image = no special alignment
1130 */
1131 v4l_bound_align_image(&rect->width,
1132 TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect->left,
1133 TVP5150_H_MAX - rect->left, 1, &rect->height,
1134 hmax - TVP5150_MAX_CROP_TOP - rect->top,
1135 hmax - rect->top, 0, 0);
1136
1137 if (!IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API) &&
1138 sel->which == V4L2_SUBDEV_FORMAT_TRY)
1139 return 0;
1140
1141 crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad, sel->which);
1142 if (IS_ERR(crop))
1143 return PTR_ERR(crop);
1144
1145 /*
1146 * Update output image size if the selection (crop) rectangle size or
1147 * position has been modified.
1148 */
1149 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
1150 !v4l2_rect_equal(rect, crop))
1151 tvp5150_set_hw_selection(sd, rect);
1152
1153 *crop = *rect;
Javier Martin963ddc62012-01-31 05:23:46 -03001154
1155 return 0;
1156}
1157
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001158static int tvp5150_get_selection(struct v4l2_subdev *sd,
1159 struct v4l2_subdev_pad_config *cfg,
1160 struct v4l2_subdev_selection *sel)
Javier Martin963ddc62012-01-31 05:23:46 -03001161{
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001162 struct tvp5150 *decoder = container_of(sd, struct tvp5150, sd);
Marco Felsch46fe6e72020-03-12 11:31:48 +01001163 struct v4l2_rect *crop;
Javier Martin963ddc62012-01-31 05:23:46 -03001164 v4l2_std_id std;
1165
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001166 switch (sel->target) {
1167 case V4L2_SEL_TGT_CROP_BOUNDS:
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001168 sel->r.left = 0;
1169 sel->r.top = 0;
1170 sel->r.width = TVP5150_H_MAX;
Javier Martin963ddc62012-01-31 05:23:46 -03001171
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001172 /* Calculate height based on current standard */
1173 if (decoder->norm == V4L2_STD_ALL)
1174 std = tvp5150_read_std(sd);
1175 else
1176 std = decoder->norm;
1177 if (std & V4L2_STD_525_60)
1178 sel->r.height = TVP5150_V_MAX_525_60;
1179 else
1180 sel->r.height = TVP5150_V_MAX_OTHERS;
1181 return 0;
1182 case V4L2_SEL_TGT_CROP:
Marco Felsch46fe6e72020-03-12 11:31:48 +01001183 crop = tvp5150_get_pad_crop(decoder, cfg, sel->pad,
1184 sel->which);
1185 if (IS_ERR(crop))
1186 return PTR_ERR(crop);
1187 sel->r = *crop;
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001188 return 0;
1189 default:
1190 return -EINVAL;
1191 }
Javier Martin963ddc62012-01-31 05:23:46 -03001192}
1193
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001194static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
1195 struct v4l2_mbus_config *cfg)
1196{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001197 struct tvp5150 *decoder = to_tvp5150(sd);
1198
1199 cfg->type = decoder->mbus_type;
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001200 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
1201 | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
1202
1203 return 0;
1204}
1205
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001206/****************************************************************************
Laurent Pincharte545ac82016-01-26 10:46:24 -02001207 V4L2 subdev pad ops
1208 ****************************************************************************/
Philipp Zabelb440b732018-06-28 12:20:40 -04001209static int tvp5150_init_cfg(struct v4l2_subdev *sd,
1210 struct v4l2_subdev_pad_config *cfg)
1211{
1212 struct tvp5150 *decoder = to_tvp5150(sd);
1213 v4l2_std_id std;
1214
1215 /*
1216 * Reset selection to maximum on subdev_open() if autodetection is on
1217 * and a standard change is detected.
1218 */
1219 if (decoder->norm == V4L2_STD_ALL) {
1220 std = tvp5150_read_std(sd);
1221 if (std != decoder->detected_norm) {
1222 decoder->detected_norm = std;
1223 tvp5150_set_default(std, &decoder->rect);
1224 }
1225 }
1226
1227 return 0;
1228}
1229
Laurent Pincharte545ac82016-01-26 10:46:24 -02001230static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
1231 struct v4l2_subdev_pad_config *cfg,
1232 struct v4l2_subdev_mbus_code_enum *code)
1233{
1234 if (code->pad || code->index)
1235 return -EINVAL;
1236
Marco Felsch5cb82942018-06-28 12:20:39 -04001237 code->code = TVP5150_MBUS_FMT;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001238 return 0;
1239}
1240
1241static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
1242 struct v4l2_subdev_pad_config *cfg,
1243 struct v4l2_subdev_frame_size_enum *fse)
1244{
1245 struct tvp5150 *decoder = to_tvp5150(sd);
1246
Marco Felsch5cb82942018-06-28 12:20:39 -04001247 if (fse->index >= 8 || fse->code != TVP5150_MBUS_FMT)
Laurent Pincharte545ac82016-01-26 10:46:24 -02001248 return -EINVAL;
1249
Marco Felsch5cb82942018-06-28 12:20:39 -04001250 fse->code = TVP5150_MBUS_FMT;
Laurent Pincharte545ac82016-01-26 10:46:24 -02001251 fse->min_width = decoder->rect.width;
1252 fse->max_width = decoder->rect.width;
1253 fse->min_height = decoder->rect.height / 2;
1254 fse->max_height = decoder->rect.height / 2;
1255
1256 return 0;
1257}
1258
1259/****************************************************************************
Marco Felsch0556f1d2020-03-12 11:31:45 +01001260 * Media entity ops
1261 ****************************************************************************/
1262#if defined(CONFIG_MEDIA_CONTROLLER)
1263static int tvp5150_set_link(struct media_pad *connector_pad,
1264 struct media_pad *tvp5150_pad, u32 flags)
1265{
1266 struct media_link *link;
1267
1268 link = media_entity_find_link(connector_pad, tvp5150_pad);
1269 if (!link)
1270 return -EINVAL;
1271
1272 link->flags = flags;
1273 link->reverse->flags = link->flags;
1274
1275 return 0;
1276}
1277
1278static int tvp5150_disable_all_input_links(struct tvp5150 *decoder)
1279{
1280 struct media_pad *connector_pad;
1281 unsigned int i;
1282 int err;
1283
1284 for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
1285 connector_pad = media_entity_remote_pad(&decoder->pads[i]);
1286 if (!connector_pad)
1287 continue;
1288
1289 err = tvp5150_set_link(connector_pad, &decoder->pads[i], 0);
1290 if (err)
1291 return err;
1292 }
1293
1294 return 0;
1295}
1296
1297static int tvp5150_s_routing(struct v4l2_subdev *sd, u32 input, u32 output,
1298 u32 config);
1299
1300static int tvp5150_link_setup(struct media_entity *entity,
1301 const struct media_pad *tvp5150_pad,
1302 const struct media_pad *remote, u32 flags)
1303{
1304 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1305 struct tvp5150 *decoder = to_tvp5150(sd);
1306 struct media_pad *other_tvp5150_pad =
1307 &decoder->pads[tvp5150_pad->index ^ 1];
1308 struct v4l2_fwnode_connector *v4l2c;
1309 bool is_svideo = false;
1310 unsigned int i;
1311 int err;
1312
1313 /*
1314 * The TVP5150 state is determined by the enabled sink pad link(s).
1315 * Enabling or disabling the source pad link has no effect.
1316 */
1317 if (tvp5150_pad->flags & MEDIA_PAD_FL_SOURCE)
1318 return 0;
1319
1320 /* Check if the svideo connector should be enabled */
1321 for (i = 0; i < decoder->connectors_num; i++) {
1322 if (remote->entity == &decoder->connectors[i].ent) {
1323 v4l2c = &decoder->connectors[i].base;
1324 is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
1325 break;
1326 }
1327 }
1328
1329 dev_dbg_lvl(sd->dev, 1, debug, "link setup '%s':%d->'%s':%d[%d]",
1330 remote->entity->name, remote->index,
1331 tvp5150_pad->entity->name, tvp5150_pad->index,
1332 flags & MEDIA_LNK_FL_ENABLED);
1333 if (is_svideo)
1334 dev_dbg_lvl(sd->dev, 1, debug,
1335 "link setup '%s':%d->'%s':%d[%d]",
1336 remote->entity->name, remote->index,
1337 other_tvp5150_pad->entity->name,
1338 other_tvp5150_pad->index,
1339 flags & MEDIA_LNK_FL_ENABLED);
1340
1341 /*
1342 * The TVP5150 has an internal mux which allows the following setup:
1343 *
1344 * comp-connector1 --\
1345 * |---> AIP1A
1346 * /
1347 * svideo-connector -|
1348 * \
1349 * |---> AIP1B
1350 * comp-connector2 --/
1351 *
1352 * We can't rely on user space that the current connector gets disabled
1353 * first before enabling the new connector. Disable all active
1354 * connector links to be on the safe side.
1355 */
1356 err = tvp5150_disable_all_input_links(decoder);
1357 if (err)
1358 return err;
1359
1360 tvp5150_s_routing(sd, is_svideo ? TVP5150_SVIDEO : tvp5150_pad->index,
1361 flags & MEDIA_LNK_FL_ENABLED ? TVP5150_NORMAL :
1362 TVP5150_BLACK_SCREEN, 0);
1363
1364 if (flags & MEDIA_LNK_FL_ENABLED) {
Marco Felschbaf17822020-03-12 11:31:55 +01001365 struct v4l2_fwnode_connector_analog *v4l2ca;
1366 u32 new_norm;
1367
Marco Felsch0556f1d2020-03-12 11:31:45 +01001368 /*
1369 * S-Video connector is conneted to both ports AIP1A and AIP1B.
1370 * Both links must be enabled in one-shot regardless which link
1371 * the user requests.
1372 */
1373 if (is_svideo) {
1374 err = tvp5150_set_link((struct media_pad *)remote,
1375 other_tvp5150_pad, flags);
1376 if (err)
1377 return err;
1378 }
Marco Felschbaf17822020-03-12 11:31:55 +01001379
1380 if (!decoder->connectors_num)
1381 return 0;
1382
1383 /* Update the current connector */
1384 decoder->cur_connector =
1385 container_of(remote, struct tvp5150_connector, pad);
1386
1387 /*
1388 * Do nothing if the new connector supports the same tv-norms as
1389 * the old one.
1390 */
1391 v4l2ca = &decoder->cur_connector->base.connector.analog;
1392 new_norm = decoder->norm & v4l2ca->sdtv_stds;
1393 if (decoder->norm == new_norm)
1394 return 0;
1395
1396 /*
1397 * Fallback to the new connector tv-norms if we can't find any
1398 * common between the current tv-norm and the new one.
1399 */
1400 tvp5150_s_std(sd, new_norm ? new_norm : v4l2ca->sdtv_stds);
Marco Felsch0556f1d2020-03-12 11:31:45 +01001401 }
1402
1403 return 0;
1404}
1405
1406static const struct media_entity_operations tvp5150_sd_media_ops = {
1407 .link_setup = tvp5150_link_setup,
1408};
1409#endif
1410/****************************************************************************
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001411 I2C Command
1412 ****************************************************************************/
Marco Felsch73549a62020-03-12 11:31:50 +01001413static int __maybe_unused tvp5150_runtime_suspend(struct device *dev)
1414{
1415 struct i2c_client *client = to_i2c_client(dev);
1416 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1417 struct tvp5150 *decoder = to_tvp5150(sd);
1418
1419 if (decoder->irq)
1420 /* Disable lock interrupt */
1421 return regmap_update_bits(decoder->regmap,
1422 TVP5150_INT_ENABLE_REG_A,
1423 TVP5150_INT_A_LOCK, 0);
1424 return 0;
1425}
1426
1427static int __maybe_unused tvp5150_runtime_resume(struct device *dev)
1428{
1429 struct i2c_client *client = to_i2c_client(dev);
1430 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1431 struct tvp5150 *decoder = to_tvp5150(sd);
1432
1433 if (decoder->irq)
1434 /* Enable lock interrupt */
1435 return regmap_update_bits(decoder->regmap,
1436 TVP5150_INT_ENABLE_REG_A,
1437 TVP5150_INT_A_LOCK,
1438 TVP5150_INT_A_LOCK);
1439 return 0;
1440}
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001441
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001442static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1443{
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001444 struct tvp5150 *decoder = to_tvp5150(sd);
Marco Felsch73549a62020-03-12 11:31:50 +01001445 unsigned int mask, val = 0;
1446 int ret;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001447
Marco Felsch8a7441b2018-06-28 12:20:36 -04001448 mask = TVP5150_MISC_CTL_YCBCR_OE | TVP5150_MISC_CTL_SYNC_OE |
1449 TVP5150_MISC_CTL_CLOCK_OE;
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001450
Laurent Pinchart79d62052016-12-09 09:47:19 -02001451 if (enable) {
Marco Felsch73549a62020-03-12 11:31:50 +01001452 ret = pm_runtime_get_sync(sd->dev);
1453 if (ret < 0) {
1454 pm_runtime_put_noidle(sd->dev);
1455 return ret;
1456 }
1457
Philipp Zabel1bb086b2018-06-28 12:20:42 -04001458 tvp5150_enable(sd);
1459
Philipp Zabel62a764e2018-06-28 12:20:45 -04001460 /* Enable outputs if decoder is locked */
Mauro Carvalho Chehab5bd1d912018-08-01 10:09:24 -04001461 if (decoder->irq)
1462 val = decoder->lock ? decoder->oe : 0;
1463 else
1464 val = decoder->oe;
Marco Felsch73549a62020-03-12 11:31:50 +01001465
Philipp Zabel2f0a5c62018-06-28 12:20:46 -04001466 v4l2_subdev_notify_event(&decoder->sd, &tvp5150_ev_fmt);
Marco Felsch73549a62020-03-12 11:31:50 +01001467 } else {
1468 pm_runtime_put(sd->dev);
Laurent Pinchart79d62052016-12-09 09:47:19 -02001469 }
1470
Marco Felsch8a7441b2018-06-28 12:20:36 -04001471 regmap_update_bits(decoder->regmap, TVP5150_MISC_CTL, mask, val);
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001472
1473 return 0;
1474}
1475
Hans Verkuil5325b422009-04-02 11:26:22 -03001476static int tvp5150_s_routing(struct v4l2_subdev *sd,
1477 u32 input, u32 output, u32 config)
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001478{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001479 struct tvp5150 *decoder = to_tvp5150(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001480
Hans Verkuil5325b422009-04-02 11:26:22 -03001481 decoder->input = input;
1482 decoder->output = output;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02001483
1484 if (output == TVP5150_BLACK_SCREEN)
1485 decoder->enable = false;
1486 else
1487 decoder->enable = true;
1488
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001489 tvp5150_selmux(sd);
Mauro Carvalho Chehab84486d52005-11-08 21:36:41 -08001490 return 0;
1491}
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001492
Hans Verkuild37dad42010-03-14 10:59:16 -03001493static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001494{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001495 struct tvp5150 *decoder = to_tvp5150(sd);
1496
Nasser Afshin9bfd8f82018-04-02 18:23:18 -04001497 /*
1498 * this is for capturing 36 raw vbi lines
1499 * if there's a way to cut off the beginning 2 vbi lines
1500 * with the tvp5150 then the vbi line count could be lowered
1501 * to 17 lines/field again, although I couldn't find a register
1502 * which could do that cropping
1503 */
1504
Hans Verkuild37dad42010-03-14 10:59:16 -03001505 if (fmt->sample_format == V4L2_PIX_FMT_GREY)
Philipp Zabel28b9e222018-06-28 12:20:35 -04001506 regmap_write(decoder->regmap, TVP5150_LUMA_PROC_CTL_1, 0x70);
Hans Verkuild37dad42010-03-14 10:59:16 -03001507 if (fmt->count[0] == 18 && fmt->count[1] == 18) {
Philipp Zabel28b9e222018-06-28 12:20:35 -04001508 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_START,
1509 0x00);
1510 regmap_write(decoder->regmap, TVP5150_VERT_BLANKING_STOP, 0x01);
Hans Verkuild37dad42010-03-14 10:59:16 -03001511 }
1512 return 0;
1513}
1514
1515static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1516{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001517 struct tvp5150 *decoder = to_tvp5150(sd);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001518 int i;
1519
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001520 if (svbi->service_set != 0) {
1521 for (i = 0; i <= 23; i++) {
1522 svbi->service_lines[1][i] = 0;
1523 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001524 tvp5150_set_vbi(sd, svbi->service_lines[0][i],
1525 0xf0, i, 3);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001526 }
1527 /* Enables FIFO */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001528 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 1);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001529 } else {
1530 /* Disables FIFO*/
Philipp Zabel28b9e222018-06-28 12:20:35 -04001531 regmap_write(decoder->regmap, TVP5150_FIFO_OUT_CTRL, 0);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001532
1533 /* Disable Full Field */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001534 regmap_write(decoder->regmap, TVP5150_FULL_FIELD_ENA, 0);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001535
1536 /* Disable Line modes */
1537 for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
Philipp Zabel28b9e222018-06-28 12:20:35 -04001538 regmap_write(decoder->regmap, i, 0xff);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001539 }
1540 return 0;
1541}
1542
Hans Verkuild37dad42010-03-14 10:59:16 -03001543static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1544{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001545 int i, mask = 0;
1546
Hans Verkuil30634e82012-09-05 10:38:10 -03001547 memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001548
1549 for (i = 0; i <= 23; i++) {
1550 svbi->service_lines[0][i] =
Mauro Carvalho Chehab3dd6b562018-02-19 13:23:39 -05001551 tvp5150_get_vbi(sd, i);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001552 mask |= svbi->service_lines[0][i];
1553 }
1554 svbi->service_set = mask;
1555 return 0;
1556}
1557
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001558#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001559static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001560{
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001561 int res;
1562
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001563 res = tvp5150_read(sd, reg->reg & 0xff);
1564 if (res < 0) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001565 dev_err(sd->dev, "%s: failed with error = %d\n", __func__, res);
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03001566 return res;
1567 }
1568
1569 reg->val = res;
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001570 reg->size = 1;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001571 return 0;
1572}
1573
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001574static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001575{
Philipp Zabel28b9e222018-06-28 12:20:35 -04001576 struct tvp5150 *decoder = to_tvp5150(sd);
1577
1578 return regmap_write(decoder->regmap, reg->reg & 0xff, reg->val & 0xff);
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001579}
1580#endif
1581
Marco Felsche953c102020-03-12 11:31:51 +01001582static int tvp5150_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1583 struct v4l2_event_subscription *sub)
1584{
1585 switch (sub->type) {
1586 case V4L2_EVENT_SOURCE_CHANGE:
1587 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
1588 case V4L2_EVENT_CTRL:
1589 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
1590 default:
1591 return -EINVAL;
1592 }
1593}
1594
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001595static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1596{
1597 int status = tvp5150_read(sd, 0x88);
1598
1599 vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1600 return 0;
1601}
1602
Marco Felsch0556f1d2020-03-12 11:31:45 +01001603static int tvp5150_registered(struct v4l2_subdev *sd)
1604{
1605#if defined(CONFIG_MEDIA_CONTROLLER)
1606 struct tvp5150 *decoder = to_tvp5150(sd);
1607 unsigned int i;
1608 int ret;
1609
1610 /*
1611 * Setup connector pads and links. Enable the link to the first
1612 * available connector per default.
1613 */
1614 for (i = 0; i < decoder->connectors_num; i++) {
1615 struct media_entity *con = &decoder->connectors[i].ent;
1616 struct media_pad *pad = &decoder->connectors[i].pad;
1617 struct v4l2_fwnode_connector *v4l2c =
1618 &decoder->connectors[i].base;
1619 struct v4l2_connector_link *link =
1620 v4l2_connector_first_link(v4l2c);
1621 unsigned int port = link->fwnode_link.remote_port;
1622 unsigned int flags = i ? 0 : MEDIA_LNK_FL_ENABLED;
1623 bool is_svideo = v4l2c->type == V4L2_CONN_SVIDEO;
1624
1625 pad->flags = MEDIA_PAD_FL_SOURCE;
1626 ret = media_entity_pads_init(con, 1, pad);
1627 if (ret < 0)
1628 goto err;
1629
1630 ret = media_device_register_entity(sd->v4l2_dev->mdev, con);
1631 if (ret < 0)
1632 goto err;
1633
1634 ret = media_create_pad_link(con, 0, &sd->entity, port, flags);
1635 if (ret < 0)
1636 goto err;
1637
1638 if (is_svideo) {
1639 /*
1640 * Check tvp5150_link_setup() comments for more
1641 * information.
1642 */
1643 link = v4l2_connector_last_link(v4l2c);
1644 port = link->fwnode_link.remote_port;
1645 ret = media_create_pad_link(con, 0, &sd->entity, port,
1646 flags);
1647 if (ret < 0)
1648 goto err;
1649 }
1650
1651 /* Enable default input. */
1652 if (flags == MEDIA_LNK_FL_ENABLED) {
1653 decoder->input =
1654 is_svideo ? TVP5150_SVIDEO :
1655 port == 0 ? TVP5150_COMPOSITE0 :
1656 TVP5150_COMPOSITE1;
1657
1658 tvp5150_selmux(sd);
Marco Felschbaf17822020-03-12 11:31:55 +01001659 decoder->cur_connector = &decoder->connectors[i];
1660 tvp5150_s_std(sd, v4l2c->connector.analog.sdtv_stds);
Marco Felsch0556f1d2020-03-12 11:31:45 +01001661 }
1662 }
1663
1664 return 0;
1665
1666err:
Chuhong Yuand000e9b2020-06-06 16:39:18 +02001667 for (i = 0; i < decoder->connectors_num; i++) {
Marco Felsch0556f1d2020-03-12 11:31:45 +01001668 media_device_unregister_entity(&decoder->connectors[i].ent);
Chuhong Yuand000e9b2020-06-06 16:39:18 +02001669 media_entity_cleanup(&decoder->connectors[i].ent);
1670 }
Marco Felsch0556f1d2020-03-12 11:31:45 +01001671 return ret;
1672#endif
1673
1674 return 0;
1675}
1676
Marco Felsch9c8e5092020-03-12 11:31:52 +01001677static int tvp5150_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1678{
1679 int ret;
1680
1681 ret = pm_runtime_get_sync(sd->dev);
1682 if (ret < 0) {
1683 pm_runtime_put_noidle(sd->dev);
1684 return ret;
1685 }
1686
1687 return 0;
1688}
1689
1690static int tvp5150_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1691{
1692 pm_runtime_put(sd->dev);
1693
1694 return 0;
1695}
1696
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001697/* ----------------------------------------------------------------------- */
1698
Hans Verkuil6c45ec72010-12-12 08:45:43 -03001699static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1700 .s_ctrl = tvp5150_s_ctrl,
1701};
1702
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001703static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1704 .log_status = tvp5150_log_status,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001705 .reset = tvp5150_reset,
1706#ifdef CONFIG_VIDEO_ADV_DEBUG
1707 .g_register = tvp5150_g_register,
1708 .s_register = tvp5150_s_register,
1709#endif
Marco Felsche953c102020-03-12 11:31:51 +01001710 .subscribe_event = tvp5150_subscribe_event,
1711 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001712};
1713
1714static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001715 .g_tuner = tvp5150_g_tuner,
1716};
1717
1718static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001719 .s_std = tvp5150_s_std,
Marco Felsch0db87cc2018-06-28 12:20:49 -04001720 .g_std = tvp5150_g_std,
Philipp Zabelee9a6ff2018-06-28 12:20:48 -04001721 .querystd = tvp5150_querystd,
Laurent Pinchart460b6c02016-01-07 10:46:45 -02001722 .s_stream = tvp5150_s_stream,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001723 .s_routing = tvp5150_s_routing,
Laurent Pinchartdd3a46b2016-01-07 10:46:46 -02001724 .g_mbus_config = tvp5150_g_mbus_config,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001725};
1726
1727static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001728 .g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
Hans Verkuild37dad42010-03-14 10:59:16 -03001729 .g_sliced_fmt = tvp5150_g_sliced_fmt,
1730 .s_sliced_fmt = tvp5150_s_sliced_fmt,
1731 .s_raw_fmt = tvp5150_s_raw_fmt,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001732};
1733
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001734static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
Philipp Zabelb440b732018-06-28 12:20:40 -04001735 .init_cfg = tvp5150_init_cfg,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001736 .enum_mbus_code = tvp5150_enum_mbus_code,
Laurent Pincharte545ac82016-01-26 10:46:24 -02001737 .enum_frame_size = tvp5150_enum_frame_size,
Hans Verkuilda298c62015-04-09 04:02:34 -03001738 .set_fmt = tvp5150_fill_fmt,
1739 .get_fmt = tvp5150_fill_fmt,
Hans Verkuil10d5509c2015-12-14 08:25:32 -02001740 .get_selection = tvp5150_get_selection,
1741 .set_selection = tvp5150_set_selection,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001742};
1743
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001744static const struct v4l2_subdev_ops tvp5150_ops = {
1745 .core = &tvp5150_core_ops,
1746 .tuner = &tvp5150_tuner_ops,
1747 .video = &tvp5150_video_ops,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001748 .vbi = &tvp5150_vbi_ops,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001749 .pad = &tvp5150_pad_ops,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03001750};
1751
Marco Felsch0556f1d2020-03-12 11:31:45 +01001752static const struct v4l2_subdev_internal_ops tvp5150_internal_ops = {
1753 .registered = tvp5150_registered,
Marco Felsch9c8e5092020-03-12 11:31:52 +01001754 .open = tvp5150_open,
1755 .close = tvp5150_close,
Marco Felsch0556f1d2020-03-12 11:31:45 +01001756};
1757
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001758/****************************************************************************
1759 I2C Client & Driver
1760 ****************************************************************************/
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08001761
Philipp Zabel28b9e222018-06-28 12:20:35 -04001762static const struct regmap_range tvp5150_readable_ranges[] = {
1763 {
1764 .range_min = TVP5150_VD_IN_SRC_SEL_1,
1765 .range_max = TVP5150_AUTOSW_MSK,
1766 }, {
1767 .range_min = TVP5150_COLOR_KIL_THSH_CTL,
1768 .range_max = TVP5150_CONF_SHARED_PIN,
1769 }, {
1770 .range_min = TVP5150_ACT_VD_CROP_ST_MSB,
1771 .range_max = TVP5150_HORIZ_SYNC_START,
1772 }, {
1773 .range_min = TVP5150_VERT_BLANKING_START,
1774 .range_max = TVP5150_INTT_CONFIG_REG_B,
1775 }, {
1776 .range_min = TVP5150_VIDEO_STD,
1777 .range_max = TVP5150_VIDEO_STD,
1778 }, {
1779 .range_min = TVP5150_CB_GAIN_FACT,
1780 .range_max = TVP5150_REV_SELECT,
1781 }, {
1782 .range_min = TVP5150_MSB_DEV_ID,
1783 .range_max = TVP5150_STATUS_REG_5,
1784 }, {
1785 .range_min = TVP5150_CC_DATA_INI,
1786 .range_max = TVP5150_TELETEXT_FIL_ENA,
1787 }, {
1788 .range_min = TVP5150_INT_STATUS_REG_A,
1789 .range_max = TVP5150_FIFO_OUT_CTRL,
1790 }, {
1791 .range_min = TVP5150_FULL_FIELD_ENA,
1792 .range_max = TVP5150_FULL_FIELD_MODE_REG,
1793 },
1794};
1795
Mauro Carvalho Chehabc135e992018-08-01 10:16:48 -04001796static bool tvp5150_volatile_reg(struct device *dev, unsigned int reg)
Philipp Zabel28b9e222018-06-28 12:20:35 -04001797{
1798 switch (reg) {
1799 case TVP5150_VERT_LN_COUNT_MSB:
1800 case TVP5150_VERT_LN_COUNT_LSB:
1801 case TVP5150_INT_STATUS_REG_A:
1802 case TVP5150_INT_STATUS_REG_B:
1803 case TVP5150_INT_ACTIVE_REG_B:
1804 case TVP5150_STATUS_REG_1:
1805 case TVP5150_STATUS_REG_2:
1806 case TVP5150_STATUS_REG_3:
1807 case TVP5150_STATUS_REG_4:
1808 case TVP5150_STATUS_REG_5:
1809 /* CC, WSS, VPS, VITC data? */
1810 case TVP5150_VBI_FIFO_READ_DATA:
1811 case TVP5150_VDP_STATUS_REG:
1812 case TVP5150_FIFO_WORD_COUNT:
1813 return true;
1814 default:
1815 return false;
1816 }
1817}
1818
1819static const struct regmap_access_table tvp5150_readable_table = {
1820 .yes_ranges = tvp5150_readable_ranges,
1821 .n_yes_ranges = ARRAY_SIZE(tvp5150_readable_ranges),
1822};
1823
1824static struct regmap_config tvp5150_config = {
1825 .reg_bits = 8,
1826 .val_bits = 8,
1827 .max_register = 0xff,
1828
1829 .cache_type = REGCACHE_RBTREE,
1830
1831 .rd_table = &tvp5150_readable_table,
1832 .volatile_reg = tvp5150_volatile_reg,
1833};
1834
Laurent Pinchart78715972016-01-07 10:46:41 -02001835static int tvp5150_detect_version(struct tvp5150 *core)
1836{
1837 struct v4l2_subdev *sd = &core->sd;
1838 struct i2c_client *c = v4l2_get_subdevdata(sd);
Laurent Pinchart78715972016-01-07 10:46:41 -02001839 u8 regs[4];
1840 int res;
1841
1842 /*
1843 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1844 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1845 */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001846 res = regmap_bulk_read(core->regmap, TVP5150_MSB_DEV_ID, regs, 4);
1847 if (res < 0) {
1848 dev_err(&c->dev, "reading ID registers failed: %d\n", res);
1849 return res;
Laurent Pinchart78715972016-01-07 10:46:41 -02001850 }
1851
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001852 core->dev_id = (regs[0] << 8) | regs[1];
1853 core->rom_ver = (regs[2] << 8) | regs[3];
Laurent Pinchart78715972016-01-07 10:46:41 -02001854
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001855 dev_info(sd->dev, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001856 core->dev_id, regs[2], regs[3], c->addr << 1,
1857 c->adapter->name);
Laurent Pinchart78715972016-01-07 10:46:41 -02001858
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001859 if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001860 dev_info(sd->dev, "tvp5150a detected.\n");
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001861 } else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001862 dev_info(sd->dev, "tvp5150am1 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001863
1864 /* ITU-T BT.656.4 timing */
Philipp Zabel28b9e222018-06-28 12:20:35 -04001865 regmap_write(core->regmap, TVP5150_REV_SELECT, 0);
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001866 } else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001867 dev_info(sd->dev, "tvp5151 detected.\n");
Laurent Pinchart78715972016-01-07 10:46:41 -02001868 } else {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02001869 dev_info(sd->dev, "*** unknown tvp%04x chip detected.\n",
Javier Martinez Canillas82275132016-02-05 17:09:54 -02001870 core->dev_id);
Laurent Pinchart78715972016-01-07 10:46:41 -02001871 }
1872
1873 return 0;
1874}
1875
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02001876static int tvp5150_init(struct i2c_client *c)
1877{
1878 struct gpio_desc *pdn_gpio;
1879 struct gpio_desc *reset_gpio;
1880
1881 pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1882 if (IS_ERR(pdn_gpio))
1883 return PTR_ERR(pdn_gpio);
1884
1885 if (pdn_gpio) {
1886 gpiod_set_value_cansleep(pdn_gpio, 0);
1887 /* Delay time between power supplies active and reset */
1888 msleep(20);
1889 }
1890
1891 reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1892 if (IS_ERR(reset_gpio))
1893 return PTR_ERR(reset_gpio);
1894
1895 if (reset_gpio) {
1896 /* RESETB pulse duration */
1897 ndelay(500);
1898 gpiod_set_value_cansleep(reset_gpio, 0);
1899 /* Delay time between end of reset to I2C active */
1900 usleep_range(200, 250);
1901 }
1902
1903 return 0;
1904}
1905
Marco Felsch0556f1d2020-03-12 11:31:45 +01001906#if defined(CONFIG_MEDIA_CONTROLLER)
1907static int tvp5150_mc_init(struct tvp5150 *decoder)
1908{
1909 struct v4l2_subdev *sd = &decoder->sd;
1910 unsigned int i;
1911
1912 sd->entity.ops = &tvp5150_sd_media_ops;
1913 sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1914
1915 for (i = 0; i < TVP5150_NUM_PADS - 1; i++) {
1916 decoder->pads[i].flags = MEDIA_PAD_FL_SINK;
1917 decoder->pads[i].sig_type = PAD_SIGNAL_ANALOG;
1918 }
1919
1920 decoder->pads[i].flags = MEDIA_PAD_FL_SOURCE;
1921 decoder->pads[i].sig_type = PAD_SIGNAL_DV;
1922
1923 return media_entity_pads_init(&sd->entity, TVP5150_NUM_PADS,
1924 decoder->pads);
1925}
1926
1927#else /* !defined(CONFIG_MEDIA_CONTROLLER) */
1928
1929static inline int tvp5150_mc_init(struct tvp5150 *decoder)
1930{
1931 return 0;
1932}
1933#endif /* defined(CONFIG_MEDIA_CONTROLLER) */
1934
1935static int tvp5150_validate_connectors(struct tvp5150 *decoder)
1936{
1937 struct device *dev = decoder->sd.dev;
1938 struct tvp5150_connector *tvpc;
1939 struct v4l2_fwnode_connector *v4l2c;
1940 unsigned int i;
1941
1942 if (!decoder->connectors_num) {
1943 dev_err(dev, "No valid connector found\n");
1944 return -ENODEV;
1945 }
1946
1947 for (i = 0; i < decoder->connectors_num; i++) {
1948 struct v4l2_connector_link *link0 = NULL;
1949 struct v4l2_connector_link *link1;
1950
1951 tvpc = &decoder->connectors[i];
1952 v4l2c = &tvpc->base;
1953
1954 if (v4l2c->type == V4L2_CONN_COMPOSITE) {
1955 if (v4l2c->nr_of_links != 1) {
1956 dev_err(dev, "Composite: connector needs 1 link\n");
1957 return -EINVAL;
1958 }
1959 link0 = v4l2_connector_first_link(v4l2c);
1960 if (!link0) {
1961 dev_err(dev, "Composite: invalid first link\n");
1962 return -EINVAL;
1963 }
1964 if (link0->fwnode_link.remote_id == 1) {
1965 dev_err(dev, "Composite: invalid endpoint id\n");
1966 return -EINVAL;
1967 }
1968 }
1969
1970 if (v4l2c->type == V4L2_CONN_SVIDEO) {
1971 if (v4l2c->nr_of_links != 2) {
1972 dev_err(dev, "SVideo: connector needs 2 links\n");
1973 return -EINVAL;
1974 }
1975 link0 = v4l2_connector_first_link(v4l2c);
1976 if (!link0) {
1977 dev_err(dev, "SVideo: invalid first link\n");
1978 return -EINVAL;
1979 }
1980 link1 = v4l2_connector_last_link(v4l2c);
1981 if (link0->fwnode_link.remote_port ==
1982 link1->fwnode_link.remote_port) {
1983 dev_err(dev, "SVideo: invalid link setup\n");
1984 return -EINVAL;
1985 }
1986 }
Marco Felschbaf17822020-03-12 11:31:55 +01001987
1988 if (!(v4l2c->connector.analog.sdtv_stds & TVP5150_STD_MASK)) {
1989 dev_err(dev, "Unsupported tv-norm on connector %s\n",
1990 v4l2c->name);
1991 return -EINVAL;
1992 }
Marco Felsch0556f1d2020-03-12 11:31:45 +01001993 }
1994
1995 return 0;
1996}
1997
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02001998static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1999{
Marco Felsch0556f1d2020-03-12 11:31:45 +01002000 struct device *dev = decoder->sd.dev;
2001 struct v4l2_fwnode_endpoint bus_cfg = {
2002 .bus_type = V4L2_MBUS_UNKNOWN
2003 };
2004 struct device_node *ep_np;
2005 struct tvp5150_connector *tvpc;
2006 struct v4l2_fwnode_connector *v4l2c;
2007 unsigned int flags, ep_num;
2008 unsigned int i;
2009 int ret;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002010
Marco Felsch0556f1d2020-03-12 11:31:45 +01002011 /* At least 1 output and 1 input */
2012 ep_num = of_graph_get_endpoint_count(np);
2013 if (ep_num < 2 || ep_num > 5) {
2014 dev_err(dev, "At least 1 input and 1 output must be connected to the device.\n");
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002015 return -EINVAL;
Marco Felsch0556f1d2020-03-12 11:31:45 +01002016 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002017
Marco Felsch0556f1d2020-03-12 11:31:45 +01002018 /* Layout if all connectors are used:
2019 *
2020 * tvp-5150 port@0 (AIP1A)
2021 * endpoint@0 -----------> Comp0-Con port
2022 * endpoint@1 --------+--> Svideo-Con port
2023 * tvp-5150 port@1 (AIP1B) |
2024 * endpoint@1 --------+
2025 * endpoint@0 -----------> Comp1-Con port
2026 * tvp-5150 port@2
2027 * endpoint (video bitstream output at YOUT[0-7] parallel bus)
2028 */
2029 for_each_endpoint_of_node(np, ep_np) {
2030 struct fwnode_handle *ep_fwnode = of_fwnode_handle(ep_np);
2031 unsigned int next_connector = decoder->connectors_num;
2032 struct of_endpoint ep;
2033
2034 of_graph_parse_endpoint(ep_np, &ep);
2035 if (ep.port > 1 || ep.id > 1) {
2036 dev_dbg(dev, "Ignore connector on port@%u/ep@%u\n",
2037 ep.port, ep.id);
2038 continue;
2039 }
2040
2041 tvpc = &decoder->connectors[next_connector];
2042 v4l2c = &tvpc->base;
2043
2044 if (ep.port == 0 || (ep.port == 1 && ep.id == 0)) {
2045 ret = v4l2_fwnode_connector_parse(ep_fwnode, v4l2c);
2046 if (ret)
2047 goto err_put;
2048 ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
2049 if (ret)
2050 goto err_put;
2051 decoder->connectors_num++;
2052 } else {
2053 /* Adding the 2nd svideo link */
2054 for (i = 0; i < TVP5150_MAX_CONNECTORS; i++) {
2055 tvpc = &decoder->connectors[i];
2056 v4l2c = &tvpc->base;
2057 if (v4l2c->type == V4L2_CONN_SVIDEO)
2058 break;
2059 }
2060
2061 ret = v4l2_fwnode_connector_add_link(ep_fwnode, v4l2c);
2062 if (ret)
2063 goto err_put;
2064 }
2065 }
2066
2067 ret = tvp5150_validate_connectors(decoder);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002068 if (ret)
Marco Felsch0556f1d2020-03-12 11:31:45 +01002069 goto err_free;
2070
2071 for (i = 0; i < decoder->connectors_num; i++) {
2072 tvpc = &decoder->connectors[i];
2073 v4l2c = &tvpc->base;
2074 tvpc->ent.flags = MEDIA_ENT_FL_CONNECTOR;
2075 tvpc->ent.function = v4l2c->type == V4L2_CONN_SVIDEO ?
2076 MEDIA_ENT_F_CONN_SVIDEO : MEDIA_ENT_F_CONN_COMPOSITE;
2077 tvpc->ent.name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
2078 v4l2c->name, v4l2c->label ?
2079 v4l2c->label : "");
2080 }
2081
2082 ep_np = of_graph_get_endpoint_by_regs(np, TVP5150_PAD_VID_OUT, 0);
2083 if (!ep_np) {
2084 dev_err(dev, "Error no output endpoint available\n");
2085 goto err_free;
2086 }
2087 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_np), &bus_cfg);
2088 of_node_put(ep_np);
2089 if (ret)
2090 goto err_free;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002091
2092 flags = bus_cfg.bus.parallel.flags;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002093 if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
2094 !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
2095 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
Javier Martinez Canillas2bd5e432016-02-05 17:09:53 -02002096 flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
2097 ret = -EINVAL;
Marco Felsch0556f1d2020-03-12 11:31:45 +01002098 goto err_free;
Javier Martinez Canillas2bd5e432016-02-05 17:09:53 -02002099 }
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002100
2101 decoder->mbus_type = bus_cfg.bus_type;
2102
Marco Felsch0556f1d2020-03-12 11:31:45 +01002103 return 0;
2104
2105err_put:
2106 of_node_put(ep_np);
2107err_free:
2108 for (i = 0; i < TVP5150_MAX_CONNECTORS; i++)
2109 v4l2_fwnode_connector_free(&decoder->connectors[i].base);
2110
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002111 return ret;
2112}
2113
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02002114static const char * const tvp5150_test_patterns[2] = {
2115 "Disabled",
2116 "Black screen"
2117};
2118
Kieran Binghame6714992019-07-10 18:51:49 -03002119static int tvp5150_probe(struct i2c_client *c)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002120{
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002121 struct tvp5150 *core;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002122 struct v4l2_subdev *sd;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002123 struct device_node *np = c->dev.of_node;
Philipp Zabel28b9e222018-06-28 12:20:35 -04002124 struct regmap *map;
Marco Felschbaf17822020-03-12 11:31:55 +01002125 unsigned int i;
Laurent Pinchart78715972016-01-07 10:46:41 -02002126 int res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002127
2128 /* Check if the adapter supports the needed features */
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002129 if (!i2c_check_functionality(c->adapter,
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002130 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002131 return -EIO;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002132
Javier Martinez Canillas09aa2602016-01-07 10:46:49 -02002133 res = tvp5150_init(c);
2134 if (res)
2135 return res;
2136
Laurent Pinchartc02b2112013-05-02 08:29:43 -03002137 core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
2138 if (!core)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002139 return -ENOMEM;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002140
Philipp Zabel28b9e222018-06-28 12:20:35 -04002141 map = devm_regmap_init_i2c(c, &tvp5150_config);
2142 if (IS_ERR(map))
2143 return PTR_ERR(map);
2144
2145 core->regmap = map;
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002146 sd = &core->sd;
Michael Tretter96ca7c42020-03-12 11:31:49 +01002147 v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
2148 sd->internal_ops = &tvp5150_internal_ops;
Marco Felsche953c102020-03-12 11:31:51 +01002149 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002150
2151 if (IS_ENABLED(CONFIG_OF) && np) {
2152 res = tvp5150_parse_dt(core, np);
2153 if (res) {
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02002154 dev_err(sd->dev, "DT parsing error: %d\n", res);
Javier Martinez Canillasa2e5f1b2016-01-07 10:46:50 -02002155 return res;
2156 }
2157 } else {
2158 /* Default to BT.656 embedded sync */
2159 core->mbus_type = V4L2_MBUS_BT656;
2160 }
2161
Marco Felsch0556f1d2020-03-12 11:31:45 +01002162 res = tvp5150_mc_init(core);
2163 if (res)
Laurent Pincharte545ac82016-01-26 10:46:24 -02002164 return res;
Javier Martinez Canillasf7b4b542016-02-05 17:09:58 -02002165
Laurent Pinchart78715972016-01-07 10:46:41 -02002166 res = tvp5150_detect_version(core);
2167 if (res < 0)
2168 return res;
Mauro Carvalho Chehab0e09a3c2011-02-22 00:10:22 -03002169
Marco Felschbaf17822020-03-12 11:31:55 +01002170 /*
2171 * Iterate over all available connectors in case they are supported and
2172 * successfully parsed. Fallback to default autodetect in case they
2173 * aren't supported.
2174 */
2175 for (i = 0; i < core->connectors_num; i++) {
2176 struct v4l2_fwnode_connector *v4l2c;
2177
2178 v4l2c = &core->connectors[i].base;
2179 core->norm |= v4l2c->connector.analog.sdtv_stds;
2180 }
2181
2182 if (!core->connectors_num)
2183 core->norm = V4L2_STD_ALL;
2184
Philipp Zabelb440b732018-06-28 12:20:40 -04002185 core->detected_norm = V4L2_STD_UNKNOWN;
Hans Verkuil5325b422009-04-02 11:26:22 -03002186 core->input = TVP5150_COMPOSITE1;
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02002187 core->enable = true;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002188
Laurent Pinchartb1950b82016-01-07 10:46:44 -02002189 v4l2_ctrl_handler_init(&core->hdl, 5);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002190 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2191 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2192 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2193 V4L2_CID_CONTRAST, 0, 255, 1, 128);
2194 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2195 V4L2_CID_SATURATION, 0, 255, 1, 128);
2196 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2197 V4L2_CID_HUE, -128, 127, 1, 0);
Laurent Pinchartb1950b82016-01-07 10:46:44 -02002198 v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
2199 V4L2_CID_PIXEL_RATE, 27000000,
2200 27000000, 1, 27000000);
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02002201 v4l2_ctrl_new_std_menu_items(&core->hdl, &tvp5150_ctrl_ops,
2202 V4L2_CID_TEST_PATTERN,
Mauro Carvalho Chehab5c4c4505b2018-09-13 16:49:51 -04002203 ARRAY_SIZE(tvp5150_test_patterns) - 1,
Mauro Carvalho Chehabc43875f2016-02-12 15:42:02 -02002204 0, 0, tvp5150_test_patterns);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002205 sd->ctrl_handler = &core->hdl;
2206 if (core->hdl.error) {
Dmitry Lifshitz8cd0d4c2012-06-13 07:49:30 -03002207 res = core->hdl.error;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03002208 goto err;
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002209 }
Mauro Carvalho Chehab4c86f972005-11-08 21:36:43 -08002210
Marco Felsch5cb82942018-06-28 12:20:39 -04002211 tvp5150_set_default(tvp5150_read_std(sd), &core->rect);
Javier Martin963ddc62012-01-31 05:23:46 -03002212
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04002213 core->irq = c->irq;
Laurent Pinchartaff808e2016-12-09 09:47:17 -02002214 tvp5150_reset(sd, 0); /* Calls v4l2_ctrl_handler_setup() */
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04002215 if (c->irq) {
2216 res = devm_request_threaded_irq(&c->dev, c->irq, NULL,
2217 tvp5150_isr, IRQF_TRIGGER_HIGH |
2218 IRQF_ONESHOT, "tvp5150", core);
2219 if (res)
Marco Felsch81fd5fd2018-09-18 09:14:46 -04002220 goto err;
Philipp Zabel8e4c97e2018-06-28 12:20:44 -04002221 }
Laurent Pinchartaff808e2016-12-09 09:47:17 -02002222
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03002223 res = v4l2_async_register_subdev(sd);
2224 if (res < 0)
2225 goto err;
2226
Markus Rechbergerf1e5ee42006-02-07 04:01:19 +01002227 if (debug > 1)
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002228 tvp5150_log_status(sd);
Marco Felsch73549a62020-03-12 11:31:50 +01002229
2230 pm_runtime_set_active(&c->dev);
2231 pm_runtime_enable(&c->dev);
2232 pm_runtime_idle(&c->dev);
2233
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002234 return 0;
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03002235
2236err:
2237 v4l2_ctrl_handler_free(&core->hdl);
2238 return res;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002239}
2240
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002241static int tvp5150_remove(struct i2c_client *c)
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002242{
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002243 struct v4l2_subdev *sd = i2c_get_clientdata(c);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002244 struct tvp5150 *decoder = to_tvp5150(sd);
Marco Felsch0556f1d2020-03-12 11:31:45 +01002245 unsigned int i;
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002246
Mauro Carvalho Chehab257e29f2016-11-16 08:58:05 -02002247 dev_dbg_lvl(sd->dev, 1, debug,
Mauro Carvalho Chehabe1bc80a2006-01-09 15:25:36 -02002248 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
2249 c->addr << 1);
2250
Marco Felsch0556f1d2020-03-12 11:31:45 +01002251 for (i = 0; i < decoder->connectors_num; i++)
2252 v4l2_fwnode_connector_free(&decoder->connectors[i].base);
Chuhong Yuand000e9b2020-06-06 16:39:18 +02002253 for (i = 0; i < decoder->connectors_num; i++) {
Marco Felsch0556f1d2020-03-12 11:31:45 +01002254 media_device_unregister_entity(&decoder->connectors[i].ent);
Chuhong Yuand000e9b2020-06-06 16:39:18 +02002255 media_entity_cleanup(&decoder->connectors[i].ent);
2256 }
Javier Martinez Canillasc7d97492015-09-21 08:23:09 -03002257 v4l2_async_unregister_subdev(sd);
Hans Verkuil6c45ec72010-12-12 08:45:43 -03002258 v4l2_ctrl_handler_free(&decoder->hdl);
Marco Felsch73549a62020-03-12 11:31:50 +01002259 pm_runtime_disable(&c->dev);
2260 pm_runtime_set_suspended(&c->dev);
2261
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002262 return 0;
2263}
2264
2265/* ----------------------------------------------------------------------- */
2266
Marco Felsch73549a62020-03-12 11:31:50 +01002267static const struct dev_pm_ops tvp5150_pm_ops = {
2268 SET_RUNTIME_PM_OPS(tvp5150_runtime_suspend,
2269 tvp5150_runtime_resume,
2270 NULL)
2271};
2272
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002273static const struct i2c_device_id tvp5150_id[] = {
2274 { "tvp5150", 0 },
2275 { }
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002276};
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002277MODULE_DEVICE_TABLE(i2c, tvp5150_id);
Mauro Carvalho Chehabcd4665c2005-11-08 21:36:40 -08002278
Eduard Gavin7ef930a2016-01-07 10:46:48 -02002279#if IS_ENABLED(CONFIG_OF)
2280static const struct of_device_id tvp5150_of_match[] = {
2281 { .compatible = "ti,tvp5150", },
2282 { /* sentinel */ },
2283};
2284MODULE_DEVICE_TABLE(of, tvp5150_of_match);
2285#endif
2286
Hans Verkuilc7711452010-09-15 15:45:20 -03002287static struct i2c_driver tvp5150_driver = {
2288 .driver = {
Eduard Gavin7ef930a2016-01-07 10:46:48 -02002289 .of_match_table = of_match_ptr(tvp5150_of_match),
Hans Verkuilc7711452010-09-15 15:45:20 -03002290 .name = "tvp5150",
Marco Felsch73549a62020-03-12 11:31:50 +01002291 .pm = &tvp5150_pm_ops,
Hans Verkuilc7711452010-09-15 15:45:20 -03002292 },
Kieran Binghame6714992019-07-10 18:51:49 -03002293 .probe_new = tvp5150_probe,
Hans Verkuilc7711452010-09-15 15:45:20 -03002294 .remove = tvp5150_remove,
2295 .id_table = tvp5150_id,
Hans Verkuil6b8fe022008-12-18 11:17:25 -03002296};
Hans Verkuilc7711452010-09-15 15:45:20 -03002297
Axel Linc6e8d862012-02-12 06:56:32 -03002298module_i2c_driver(tvp5150_driver);