Commit | Line | Data |
---|---|---|
07b1747c | 1 | /* |
cb7a01ac | 2 | * drivers/media/i2c/tvp514x.c |
07b1747c VH |
3 | * |
4 | * TI TVP5146/47 decoder driver | |
5 | * | |
6 | * Copyright (C) 2008 Texas Instruments Inc | |
7 | * Author: Vaibhav Hiremath <hvaibhav@ti.com> | |
8 | * | |
9 | * Contributors: | |
10 | * Sivaraj R <sivaraj@ti.com> | |
11 | * Brijesh R Jadav <brijesh.j@ti.com> | |
12 | * Hardik Shah <hardik.shah@ti.com> | |
13 | * Manjunath Hadli <mrh@ti.com> | |
14 | * Karicheri Muralidharan <m-karicheri2@ti.com> | |
5b38b0f8 | 15 | * Prabhakar Lad <prabhakar.lad@ti.com> |
07b1747c VH |
16 | * |
17 | * This package is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License version 2 as | |
19 | * published by the Free Software Foundation. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
29 | * | |
30 | */ | |
31 | ||
32 | #include <linux/i2c.h> | |
5a0e3ad6 | 33 | #include <linux/slab.h> |
07b1747c VH |
34 | #include <linux/delay.h> |
35 | #include <linux/videodev2.h> | |
7a707b89 | 36 | #include <linux/module.h> |
5b38b0f8 | 37 | #include <linux/v4l2-mediabus.h> |
62ef80a1 MK |
38 | |
39 | #include <media/v4l2-device.h> | |
40 | #include <media/v4l2-common.h> | |
83811913 | 41 | #include <media/v4l2-mediabus.h> |
cf6832af | 42 | #include <media/v4l2-ctrls.h> |
07b1747c | 43 | #include <media/tvp514x.h> |
5b38b0f8 | 44 | #include <media/media-entity.h> |
07b1747c VH |
45 | |
46 | #include "tvp514x_regs.h" | |
47 | ||
07b1747c VH |
48 | /* Private macros for TVP */ |
49 | #define I2C_RETRY_COUNT (5) | |
50 | #define LOCK_RETRY_COUNT (5) | |
51 | #define LOCK_RETRY_DELAY (200) | |
52 | ||
53 | /* Debug functions */ | |
90ab5ee9 | 54 | static bool debug; |
07b1747c VH |
55 | module_param(debug, bool, 0644); |
56 | MODULE_PARM_DESC(debug, "Debug level (0-1)"); | |
57 | ||
62ef80a1 MK |
58 | MODULE_AUTHOR("Texas Instruments"); |
59 | MODULE_DESCRIPTION("TVP514X linux decoder driver"); | |
60 | MODULE_LICENSE("GPL"); | |
07b1747c | 61 | |
c1c9d09c | 62 | /* enum tvp514x_std - enum for supported standards */ |
07b1747c VH |
63 | enum tvp514x_std { |
64 | STD_NTSC_MJ = 0, | |
65 | STD_PAL_BDGHIN, | |
66 | STD_INVALID | |
67 | }; | |
68 | ||
c1c9d09c | 69 | /** |
07b1747c VH |
70 | * struct tvp514x_std_info - Structure to store standard informations |
71 | * @width: Line width in pixels | |
72 | * @height:Number of active lines | |
73 | * @video_std: Value to write in REG_VIDEO_STD register | |
74 | * @standard: v4l2 standard structure information | |
75 | */ | |
76 | struct tvp514x_std_info { | |
77 | unsigned long width; | |
78 | unsigned long height; | |
79 | u8 video_std; | |
80 | struct v4l2_standard standard; | |
81 | }; | |
82 | ||
6722e0ef | 83 | static struct tvp514x_reg tvp514x_reg_list_default[0x40]; |
63b59cec VH |
84 | |
85 | static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable); | |
c1c9d09c | 86 | /** |
6722e0ef | 87 | * struct tvp514x_decoder - TVP5146/47 decoder object |
62ef80a1 | 88 | * @sd: Subdevice Slave handle |
6722e0ef | 89 | * @tvp514x_regs: copy of hw's regs with preset values. |
07b1747c | 90 | * @pdata: Board specific |
07b1747c | 91 | * @ver: Chip version |
62ef80a1 | 92 | * @streaming: TVP5146/47 decoder streaming - enabled or disabled. |
5b38b0f8 MH |
93 | * @pix: Current pixel format |
94 | * @num_fmts: Number of formats | |
95 | * @fmt_list: Format list | |
07b1747c VH |
96 | * @current_std: Current standard |
97 | * @num_stds: Number of standards | |
98 | * @std_list: Standards list | |
62ef80a1 MK |
99 | * @input: Input routing at chip level |
100 | * @output: Output routing at chip level | |
07b1747c VH |
101 | */ |
102 | struct tvp514x_decoder { | |
62ef80a1 | 103 | struct v4l2_subdev sd; |
cf6832af | 104 | struct v4l2_ctrl_handler hdl; |
6722e0ef | 105 | struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)]; |
07b1747c | 106 | const struct tvp514x_platform_data *pdata; |
07b1747c VH |
107 | |
108 | int ver; | |
62ef80a1 | 109 | int streaming; |
07b1747c | 110 | |
5b38b0f8 MH |
111 | struct v4l2_pix_format pix; |
112 | int num_fmts; | |
113 | const struct v4l2_fmtdesc *fmt_list; | |
114 | ||
07b1747c VH |
115 | enum tvp514x_std current_std; |
116 | int num_stds; | |
a75ffc12 | 117 | const struct tvp514x_std_info *std_list; |
c1c9d09c | 118 | /* Input and Output Routing parameters */ |
62ef80a1 MK |
119 | u32 input; |
120 | u32 output; | |
5b38b0f8 MH |
121 | |
122 | /* mc related members */ | |
123 | struct media_pad pad; | |
124 | struct v4l2_mbus_framefmt format; | |
07b1747c VH |
125 | }; |
126 | ||
127 | /* TVP514x default register values */ | |
6722e0ef | 128 | static struct tvp514x_reg tvp514x_reg_list_default[] = { |
c1c9d09c MK |
129 | /* Composite selected */ |
130 | {TOK_WRITE, REG_INPUT_SEL, 0x05}, | |
07b1747c | 131 | {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F}, |
c1c9d09c MK |
132 | /* Auto mode */ |
133 | {TOK_WRITE, REG_VIDEO_STD, 0x00}, | |
07b1747c VH |
134 | {TOK_WRITE, REG_OPERATION_MODE, 0x00}, |
135 | {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F}, | |
136 | {TOK_WRITE, REG_COLOR_KILLER, 0x10}, | |
137 | {TOK_WRITE, REG_LUMA_CONTROL1, 0x00}, | |
138 | {TOK_WRITE, REG_LUMA_CONTROL2, 0x00}, | |
139 | {TOK_WRITE, REG_LUMA_CONTROL3, 0x02}, | |
140 | {TOK_WRITE, REG_BRIGHTNESS, 0x80}, | |
141 | {TOK_WRITE, REG_CONTRAST, 0x80}, | |
142 | {TOK_WRITE, REG_SATURATION, 0x80}, | |
143 | {TOK_WRITE, REG_HUE, 0x00}, | |
144 | {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00}, | |
145 | {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E}, | |
c1c9d09c MK |
146 | /* Reserved */ |
147 | {TOK_SKIP, 0x0F, 0x00}, | |
07b1747c VH |
148 | {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80}, |
149 | {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80}, | |
150 | {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80}, | |
c1c9d09c MK |
151 | /* Reserved */ |
152 | {TOK_SKIP, 0x13, 0x00}, | |
07b1747c | 153 | {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80}, |
c1c9d09c MK |
154 | /* Reserved */ |
155 | {TOK_SKIP, 0x15, 0x00}, | |
156 | /* NTSC timing */ | |
157 | {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55}, | |
07b1747c VH |
158 | {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00}, |
159 | {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25}, | |
160 | {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03}, | |
c1c9d09c MK |
161 | /* NTSC timing */ |
162 | {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00}, | |
07b1747c VH |
163 | {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00}, |
164 | {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40}, | |
165 | {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00}, | |
c1c9d09c MK |
166 | /* NTSC timing */ |
167 | {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04}, | |
07b1747c VH |
168 | {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00}, |
169 | {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07}, | |
170 | {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00}, | |
c1c9d09c MK |
171 | /* NTSC timing */ |
172 | {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01}, | |
07b1747c VH |
173 | {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00}, |
174 | {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15}, | |
175 | {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00}, | |
c1c9d09c MK |
176 | /* Reserved */ |
177 | {TOK_SKIP, 0x26, 0x00}, | |
178 | /* Reserved */ | |
179 | {TOK_SKIP, 0x27, 0x00}, | |
07b1747c | 180 | {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC}, |
c1c9d09c MK |
181 | /* Reserved */ |
182 | {TOK_SKIP, 0x29, 0x00}, | |
07b1747c | 183 | {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00}, |
c1c9d09c MK |
184 | /* Reserved */ |
185 | {TOK_SKIP, 0x2B, 0x00}, | |
07b1747c VH |
186 | {TOK_SKIP, REG_SCART_DELAY, 0x00}, |
187 | {TOK_SKIP, REG_CTI_DELAY, 0x00}, | |
188 | {TOK_SKIP, REG_CTI_CONTROL, 0x00}, | |
c1c9d09c MK |
189 | /* Reserved */ |
190 | {TOK_SKIP, 0x2F, 0x00}, | |
191 | /* Reserved */ | |
192 | {TOK_SKIP, 0x30, 0x00}, | |
193 | /* Reserved */ | |
194 | {TOK_SKIP, 0x31, 0x00}, | |
195 | /* HS, VS active high */ | |
196 | {TOK_WRITE, REG_SYNC_CONTROL, 0x00}, | |
197 | /* 10-bit BT.656 */ | |
198 | {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00}, | |
199 | /* Enable clk & data */ | |
200 | {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11}, | |
201 | /* Enable AVID & FLD */ | |
202 | {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE}, | |
203 | /* Enable VS & HS */ | |
204 | {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF}, | |
07b1747c VH |
205 | {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF}, |
206 | {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF}, | |
c1c9d09c MK |
207 | /* Clear status */ |
208 | {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01}, | |
07b1747c VH |
209 | {TOK_TERM, 0, 0}, |
210 | }; | |
211 | ||
5b38b0f8 MH |
212 | /** |
213 | * List of image formats supported by TVP5146/47 decoder | |
214 | * Currently we are using 8 bit mode only, but can be | |
215 | * extended to 10/20 bit mode. | |
216 | */ | |
217 | static const struct v4l2_fmtdesc tvp514x_fmt_list[] = { | |
218 | { | |
219 | .index = 0, | |
220 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
221 | .flags = 0, | |
222 | .description = "8-bit UYVY 4:2:2 Format", | |
223 | .pixelformat = V4L2_PIX_FMT_UYVY, | |
224 | }, | |
225 | }; | |
226 | ||
c1c9d09c | 227 | /** |
07b1747c VH |
228 | * Supported standards - |
229 | * | |
230 | * Currently supports two standards only, need to add support for rest of the | |
231 | * modes, like SECAM, etc... | |
232 | */ | |
a75ffc12 | 233 | static const struct tvp514x_std_info tvp514x_std_list[] = { |
07b1747c VH |
234 | /* Standard: STD_NTSC_MJ */ |
235 | [STD_NTSC_MJ] = { | |
236 | .width = NTSC_NUM_ACTIVE_PIXELS, | |
237 | .height = NTSC_NUM_ACTIVE_LINES, | |
238 | .video_std = VIDEO_STD_NTSC_MJ_BIT, | |
239 | .standard = { | |
240 | .index = 0, | |
241 | .id = V4L2_STD_NTSC, | |
242 | .name = "NTSC", | |
243 | .frameperiod = {1001, 30000}, | |
244 | .framelines = 525 | |
245 | }, | |
246 | /* Standard: STD_PAL_BDGHIN */ | |
247 | }, | |
248 | [STD_PAL_BDGHIN] = { | |
249 | .width = PAL_NUM_ACTIVE_PIXELS, | |
250 | .height = PAL_NUM_ACTIVE_LINES, | |
251 | .video_std = VIDEO_STD_PAL_BDGHIN_BIT, | |
252 | .standard = { | |
253 | .index = 1, | |
254 | .id = V4L2_STD_PAL, | |
255 | .name = "PAL", | |
256 | .frameperiod = {1, 25}, | |
257 | .framelines = 625 | |
258 | }, | |
259 | }, | |
260 | /* Standard: need to add for additional standard */ | |
261 | }; | |
62ef80a1 MK |
262 | |
263 | ||
264 | static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd) | |
265 | { | |
266 | return container_of(sd, struct tvp514x_decoder, sd); | |
267 | } | |
268 | ||
cf6832af HV |
269 | static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) |
270 | { | |
271 | return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd; | |
272 | } | |
273 | ||
07b1747c | 274 | |
c1c9d09c MK |
275 | /** |
276 | * tvp514x_read_reg() - Read a value from a register in an TVP5146/47. | |
277 | * @sd: ptr to v4l2_subdev struct | |
278 | * @reg: TVP5146/47 register address | |
279 | * | |
07b1747c VH |
280 | * Returns value read if successful, or non-zero (-1) otherwise. |
281 | */ | |
62ef80a1 | 282 | static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg) |
07b1747c | 283 | { |
62ef80a1 MK |
284 | int err, retry = 0; |
285 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
286 | ||
07b1747c VH |
287 | read_again: |
288 | ||
289 | err = i2c_smbus_read_byte_data(client, reg); | |
6f901a99 | 290 | if (err < 0) { |
07b1747c | 291 | if (retry <= I2C_RETRY_COUNT) { |
62ef80a1 | 292 | v4l2_warn(sd, "Read: retry ... %d\n", retry); |
07b1747c VH |
293 | retry++; |
294 | msleep_interruptible(10); | |
295 | goto read_again; | |
296 | } | |
297 | } | |
298 | ||
299 | return err; | |
300 | } | |
301 | ||
c1c9d09c MK |
302 | /** |
303 | * dump_reg() - dump the register content of TVP5146/47. | |
304 | * @sd: ptr to v4l2_subdev struct | |
305 | * @reg: TVP5146/47 register address | |
306 | */ | |
62ef80a1 MK |
307 | static void dump_reg(struct v4l2_subdev *sd, u8 reg) |
308 | { | |
309 | u32 val; | |
310 | ||
311 | val = tvp514x_read_reg(sd, reg); | |
312 | v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val); | |
313 | } | |
314 | ||
c1c9d09c MK |
315 | /** |
316 | * tvp514x_write_reg() - Write a value to a register in TVP5146/47 | |
317 | * @sd: ptr to v4l2_subdev struct | |
318 | * @reg: TVP5146/47 register address | |
319 | * @val: value to be written to the register | |
320 | * | |
07b1747c VH |
321 | * Write a value to a register in an TVP5146/47 decoder device. |
322 | * Returns zero if successful, or non-zero otherwise. | |
323 | */ | |
62ef80a1 | 324 | static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val) |
07b1747c | 325 | { |
62ef80a1 MK |
326 | int err, retry = 0; |
327 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
328 | ||
07b1747c VH |
329 | write_again: |
330 | ||
331 | err = i2c_smbus_write_byte_data(client, reg, val); | |
332 | if (err) { | |
333 | if (retry <= I2C_RETRY_COUNT) { | |
62ef80a1 | 334 | v4l2_warn(sd, "Write: retry ... %d\n", retry); |
07b1747c VH |
335 | retry++; |
336 | msleep_interruptible(10); | |
337 | goto write_again; | |
338 | } | |
339 | } | |
340 | ||
341 | return err; | |
342 | } | |
343 | ||
c1c9d09c MK |
344 | /** |
345 | * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers | |
346 | * @sd: ptr to v4l2_subdev struct | |
347 | * @reglist: list of TVP5146/47 registers and values | |
348 | * | |
349 | * Initializes a list of TVP5146/47 registers:- | |
07b1747c VH |
350 | * if token is TOK_TERM, then entire write operation terminates |
351 | * if token is TOK_DELAY, then a delay of 'val' msec is introduced | |
352 | * if token is TOK_SKIP, then the register write is skipped | |
353 | * if token is TOK_WRITE, then the register write is performed | |
07b1747c VH |
354 | * Returns zero if successful, or non-zero otherwise. |
355 | */ | |
62ef80a1 | 356 | static int tvp514x_write_regs(struct v4l2_subdev *sd, |
07b1747c VH |
357 | const struct tvp514x_reg reglist[]) |
358 | { | |
359 | int err; | |
360 | const struct tvp514x_reg *next = reglist; | |
361 | ||
362 | for (; next->token != TOK_TERM; next++) { | |
363 | if (next->token == TOK_DELAY) { | |
364 | msleep(next->val); | |
365 | continue; | |
366 | } | |
367 | ||
368 | if (next->token == TOK_SKIP) | |
369 | continue; | |
370 | ||
62ef80a1 | 371 | err = tvp514x_write_reg(sd, next->reg, (u8) next->val); |
07b1747c | 372 | if (err) { |
62ef80a1 | 373 | v4l2_err(sd, "Write failed. Err[%d]\n", err); |
07b1747c VH |
374 | return err; |
375 | } | |
376 | } | |
377 | return 0; | |
378 | } | |
379 | ||
c1c9d09c | 380 | /** |
2db4e78f | 381 | * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47 |
c1c9d09c MK |
382 | * @sd: ptr to v4l2_subdev struct |
383 | * | |
2db4e78f | 384 | * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no |
c1c9d09c | 385 | * standard detected. |
07b1747c | 386 | */ |
2db4e78f | 387 | static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd) |
07b1747c VH |
388 | { |
389 | u8 std, std_status; | |
390 | ||
62ef80a1 MK |
391 | std = tvp514x_read_reg(sd, REG_VIDEO_STD); |
392 | if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT) | |
07b1747c | 393 | /* use the standard status register */ |
62ef80a1 MK |
394 | std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS); |
395 | else | |
c1c9d09c MK |
396 | /* use the standard register itself */ |
397 | std_status = std; | |
07b1747c VH |
398 | |
399 | switch (std_status & VIDEO_STD_MASK) { | |
400 | case VIDEO_STD_NTSC_MJ_BIT: | |
401 | return STD_NTSC_MJ; | |
402 | ||
403 | case VIDEO_STD_PAL_BDGHIN_BIT: | |
404 | return STD_PAL_BDGHIN; | |
405 | ||
406 | default: | |
407 | return STD_INVALID; | |
408 | } | |
409 | ||
410 | return STD_INVALID; | |
411 | } | |
412 | ||
c1c9d09c | 413 | /* TVP5146/47 register dump function */ |
62ef80a1 | 414 | static void tvp514x_reg_dump(struct v4l2_subdev *sd) |
07b1747c | 415 | { |
62ef80a1 MK |
416 | dump_reg(sd, REG_INPUT_SEL); |
417 | dump_reg(sd, REG_AFE_GAIN_CTRL); | |
418 | dump_reg(sd, REG_VIDEO_STD); | |
419 | dump_reg(sd, REG_OPERATION_MODE); | |
420 | dump_reg(sd, REG_COLOR_KILLER); | |
421 | dump_reg(sd, REG_LUMA_CONTROL1); | |
422 | dump_reg(sd, REG_LUMA_CONTROL2); | |
423 | dump_reg(sd, REG_LUMA_CONTROL3); | |
424 | dump_reg(sd, REG_BRIGHTNESS); | |
425 | dump_reg(sd, REG_CONTRAST); | |
426 | dump_reg(sd, REG_SATURATION); | |
427 | dump_reg(sd, REG_HUE); | |
428 | dump_reg(sd, REG_CHROMA_CONTROL1); | |
429 | dump_reg(sd, REG_CHROMA_CONTROL2); | |
430 | dump_reg(sd, REG_COMP_PR_SATURATION); | |
431 | dump_reg(sd, REG_COMP_Y_CONTRAST); | |
432 | dump_reg(sd, REG_COMP_PB_SATURATION); | |
433 | dump_reg(sd, REG_COMP_Y_BRIGHTNESS); | |
434 | dump_reg(sd, REG_AVID_START_PIXEL_LSB); | |
435 | dump_reg(sd, REG_AVID_START_PIXEL_MSB); | |
436 | dump_reg(sd, REG_AVID_STOP_PIXEL_LSB); | |
437 | dump_reg(sd, REG_AVID_STOP_PIXEL_MSB); | |
438 | dump_reg(sd, REG_HSYNC_START_PIXEL_LSB); | |
439 | dump_reg(sd, REG_HSYNC_START_PIXEL_MSB); | |
440 | dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB); | |
441 | dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB); | |
442 | dump_reg(sd, REG_VSYNC_START_LINE_LSB); | |
443 | dump_reg(sd, REG_VSYNC_START_LINE_MSB); | |
444 | dump_reg(sd, REG_VSYNC_STOP_LINE_LSB); | |
445 | dump_reg(sd, REG_VSYNC_STOP_LINE_MSB); | |
446 | dump_reg(sd, REG_VBLK_START_LINE_LSB); | |
447 | dump_reg(sd, REG_VBLK_START_LINE_MSB); | |
448 | dump_reg(sd, REG_VBLK_STOP_LINE_LSB); | |
449 | dump_reg(sd, REG_VBLK_STOP_LINE_MSB); | |
450 | dump_reg(sd, REG_SYNC_CONTROL); | |
451 | dump_reg(sd, REG_OUTPUT_FORMATTER1); | |
452 | dump_reg(sd, REG_OUTPUT_FORMATTER2); | |
453 | dump_reg(sd, REG_OUTPUT_FORMATTER3); | |
454 | dump_reg(sd, REG_OUTPUT_FORMATTER4); | |
455 | dump_reg(sd, REG_OUTPUT_FORMATTER5); | |
456 | dump_reg(sd, REG_OUTPUT_FORMATTER6); | |
457 | dump_reg(sd, REG_CLEAR_LOST_LOCK); | |
07b1747c VH |
458 | } |
459 | ||
c1c9d09c MK |
460 | /** |
461 | * tvp514x_configure() - Configure the TVP5146/47 registers | |
462 | * @sd: ptr to v4l2_subdev struct | |
463 | * @decoder: ptr to tvp514x_decoder structure | |
464 | * | |
07b1747c VH |
465 | * Returns zero if successful, or non-zero otherwise. |
466 | */ | |
62ef80a1 MK |
467 | static int tvp514x_configure(struct v4l2_subdev *sd, |
468 | struct tvp514x_decoder *decoder) | |
07b1747c VH |
469 | { |
470 | int err; | |
471 | ||
472 | /* common register initialization */ | |
473 | err = | |
62ef80a1 | 474 | tvp514x_write_regs(sd, decoder->tvp514x_regs); |
07b1747c VH |
475 | if (err) |
476 | return err; | |
477 | ||
478 | if (debug) | |
62ef80a1 | 479 | tvp514x_reg_dump(sd); |
07b1747c VH |
480 | |
481 | return 0; | |
482 | } | |
483 | ||
c1c9d09c MK |
484 | /** |
485 | * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision. | |
486 | * @sd: pointer to standard V4L2 sub-device structure | |
487 | * @decoder: pointer to tvp514x_decoder structure | |
488 | * | |
07b1747c VH |
489 | * A device is considered to be detected if the chip ID (LSB and MSB) |
490 | * registers match the expected values. | |
491 | * Any value of the rom version register is accepted. | |
492 | * Returns ENODEV error number if no device is detected, or zero | |
493 | * if a device is detected. | |
494 | */ | |
62ef80a1 MK |
495 | static int tvp514x_detect(struct v4l2_subdev *sd, |
496 | struct tvp514x_decoder *decoder) | |
07b1747c VH |
497 | { |
498 | u8 chip_id_msb, chip_id_lsb, rom_ver; | |
62ef80a1 | 499 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
07b1747c | 500 | |
62ef80a1 MK |
501 | chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB); |
502 | chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB); | |
503 | rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION); | |
07b1747c | 504 | |
62ef80a1 | 505 | v4l2_dbg(1, debug, sd, |
07b1747c VH |
506 | "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n", |
507 | chip_id_msb, chip_id_lsb, rom_ver); | |
508 | if ((chip_id_msb != TVP514X_CHIP_ID_MSB) | |
509 | || ((chip_id_lsb != TVP5146_CHIP_ID_LSB) | |
510 | && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) { | |
511 | /* We didn't read the values we expected, so this must not be | |
512 | * an TVP5146/47. | |
513 | */ | |
62ef80a1 MK |
514 | v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n", |
515 | chip_id_msb, chip_id_lsb); | |
07b1747c VH |
516 | return -ENODEV; |
517 | } | |
518 | ||
519 | decoder->ver = rom_ver; | |
07b1747c | 520 | |
62ef80a1 MK |
521 | v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n", |
522 | client->name, decoder->ver, | |
523 | client->addr << 1, client->adapter->name); | |
07b1747c VH |
524 | return 0; |
525 | } | |
526 | ||
c1c9d09c MK |
527 | /** |
528 | * tvp514x_querystd() - V4L2 decoder interface handler for querystd | |
62ef80a1 | 529 | * @sd: pointer to standard V4L2 sub-device structure |
07b1747c VH |
530 | * @std_id: standard V4L2 std_id ioctl enum |
531 | * | |
532 | * Returns the current standard detected by TVP5146/47. If no active input is | |
2db4e78f | 533 | * detected then *std_id is set to 0 and the function returns 0. |
07b1747c | 534 | */ |
62ef80a1 | 535 | static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id) |
07b1747c | 536 | { |
62ef80a1 | 537 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
538 | enum tvp514x_std current_std; |
539 | enum tvp514x_input input_sel; | |
540 | u8 sync_lock_status, lock_mask; | |
541 | ||
542 | if (std_id == NULL) | |
543 | return -EINVAL; | |
544 | ||
2db4e78f HV |
545 | *std_id = V4L2_STD_UNKNOWN; |
546 | ||
c389648a HV |
547 | /* To query the standard the TVP514x must power on the ADCs. */ |
548 | if (!decoder->streaming) { | |
549 | tvp514x_s_stream(sd, 1); | |
550 | msleep(LOCK_RETRY_DELAY); | |
551 | } | |
552 | ||
2db4e78f HV |
553 | /* query the current standard */ |
554 | current_std = tvp514x_query_current_std(sd); | |
07b1747c | 555 | if (current_std == STD_INVALID) |
2db4e78f | 556 | return 0; |
07b1747c | 557 | |
62ef80a1 | 558 | input_sel = decoder->input; |
07b1747c VH |
559 | |
560 | switch (input_sel) { | |
561 | case INPUT_CVBS_VI1A: | |
562 | case INPUT_CVBS_VI1B: | |
563 | case INPUT_CVBS_VI1C: | |
564 | case INPUT_CVBS_VI2A: | |
565 | case INPUT_CVBS_VI2B: | |
566 | case INPUT_CVBS_VI2C: | |
567 | case INPUT_CVBS_VI3A: | |
568 | case INPUT_CVBS_VI3B: | |
569 | case INPUT_CVBS_VI3C: | |
570 | case INPUT_CVBS_VI4A: | |
571 | lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT | | |
572 | STATUS_HORZ_SYNC_LOCK_BIT | | |
573 | STATUS_VIRT_SYNC_LOCK_BIT; | |
574 | break; | |
575 | ||
576 | case INPUT_SVIDEO_VI2A_VI1A: | |
577 | case INPUT_SVIDEO_VI2B_VI1B: | |
578 | case INPUT_SVIDEO_VI2C_VI1C: | |
579 | case INPUT_SVIDEO_VI2A_VI3A: | |
580 | case INPUT_SVIDEO_VI2B_VI3B: | |
581 | case INPUT_SVIDEO_VI2C_VI3C: | |
582 | case INPUT_SVIDEO_VI4A_VI1A: | |
583 | case INPUT_SVIDEO_VI4A_VI1B: | |
584 | case INPUT_SVIDEO_VI4A_VI1C: | |
585 | case INPUT_SVIDEO_VI4A_VI3A: | |
586 | case INPUT_SVIDEO_VI4A_VI3B: | |
587 | case INPUT_SVIDEO_VI4A_VI3C: | |
588 | lock_mask = STATUS_HORZ_SYNC_LOCK_BIT | | |
589 | STATUS_VIRT_SYNC_LOCK_BIT; | |
590 | break; | |
591 | /*Need to add other interfaces*/ | |
592 | default: | |
593 | return -EINVAL; | |
594 | } | |
595 | /* check whether signal is locked */ | |
62ef80a1 | 596 | sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1); |
07b1747c | 597 | if (lock_mask != (sync_lock_status & lock_mask)) |
2db4e78f | 598 | return 0; /* No input detected */ |
07b1747c | 599 | |
07b1747c VH |
600 | *std_id = decoder->std_list[current_std].standard.id; |
601 | ||
2db4e78f | 602 | v4l2_dbg(1, debug, sd, "Current STD: %s\n", |
07b1747c VH |
603 | decoder->std_list[current_std].standard.name); |
604 | return 0; | |
605 | } | |
606 | ||
c1c9d09c MK |
607 | /** |
608 | * tvp514x_s_std() - V4L2 decoder interface handler for s_std | |
62ef80a1 | 609 | * @sd: pointer to standard V4L2 sub-device structure |
07b1747c VH |
610 | * @std_id: standard V4L2 v4l2_std_id ioctl enum |
611 | * | |
612 | * If std_id is supported, sets the requested standard. Otherwise, returns | |
613 | * -EINVAL | |
614 | */ | |
62ef80a1 | 615 | static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id) |
07b1747c | 616 | { |
62ef80a1 | 617 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
618 | int err, i; |
619 | ||
07b1747c | 620 | for (i = 0; i < decoder->num_stds; i++) |
62ef80a1 | 621 | if (std_id & decoder->std_list[i].standard.id) |
07b1747c VH |
622 | break; |
623 | ||
624 | if ((i == decoder->num_stds) || (i == STD_INVALID)) | |
625 | return -EINVAL; | |
626 | ||
62ef80a1 | 627 | err = tvp514x_write_reg(sd, REG_VIDEO_STD, |
07b1747c VH |
628 | decoder->std_list[i].video_std); |
629 | if (err) | |
630 | return err; | |
631 | ||
632 | decoder->current_std = i; | |
6722e0ef SAS |
633 | decoder->tvp514x_regs[REG_VIDEO_STD].val = |
634 | decoder->std_list[i].video_std; | |
07b1747c | 635 | |
3907b072 | 636 | v4l2_dbg(1, debug, sd, "Standard set to: %s\n", |
07b1747c VH |
637 | decoder->std_list[i].standard.name); |
638 | return 0; | |
639 | } | |
640 | ||
c1c9d09c MK |
641 | /** |
642 | * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing | |
62ef80a1 | 643 | * @sd: pointer to standard V4L2 sub-device structure |
c1c9d09c MK |
644 | * @input: input selector for routing the signal |
645 | * @output: output selector for routing the signal | |
646 | * @config: config value. Not used | |
07b1747c VH |
647 | * |
648 | * If index is valid, selects the requested input. Otherwise, returns -EINVAL if | |
649 | * the input is not supported or there is no active signal present in the | |
650 | * selected input. | |
651 | */ | |
62ef80a1 MK |
652 | static int tvp514x_s_routing(struct v4l2_subdev *sd, |
653 | u32 input, u32 output, u32 config) | |
07b1747c | 654 | { |
62ef80a1 | 655 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
656 | int err; |
657 | enum tvp514x_input input_sel; | |
658 | enum tvp514x_output output_sel; | |
07b1747c | 659 | |
62ef80a1 MK |
660 | if ((input >= INPUT_INVALID) || |
661 | (output >= OUTPUT_INVALID)) | |
c1c9d09c MK |
662 | /* Index out of bound */ |
663 | return -EINVAL; | |
07b1747c | 664 | |
62ef80a1 MK |
665 | input_sel = input; |
666 | output_sel = output; | |
07b1747c | 667 | |
62ef80a1 | 668 | err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel); |
07b1747c VH |
669 | if (err) |
670 | return err; | |
671 | ||
62ef80a1 | 672 | output_sel |= tvp514x_read_reg(sd, |
07b1747c | 673 | REG_OUTPUT_FORMATTER1) & 0x7; |
62ef80a1 | 674 | err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1, |
07b1747c VH |
675 | output_sel); |
676 | if (err) | |
677 | return err; | |
678 | ||
6722e0ef SAS |
679 | decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel; |
680 | decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel; | |
62ef80a1 MK |
681 | decoder->input = input; |
682 | decoder->output = output; | |
07b1747c | 683 | |
2db4e78f | 684 | v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel); |
07b1747c VH |
685 | |
686 | return 0; | |
687 | } | |
688 | ||
c1c9d09c MK |
689 | /** |
690 | * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl | |
cf6832af | 691 | * @ctrl: pointer to v4l2_ctrl structure |
07b1747c VH |
692 | * |
693 | * If the requested control is supported, sets the control's current | |
694 | * value in HW. Otherwise, returns -EINVAL if the control is not supported. | |
695 | */ | |
cf6832af | 696 | static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl) |
07b1747c | 697 | { |
cf6832af | 698 | struct v4l2_subdev *sd = to_sd(ctrl); |
62ef80a1 | 699 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
700 | int err = -EINVAL, value; |
701 | ||
cf6832af | 702 | value = ctrl->val; |
07b1747c VH |
703 | |
704 | switch (ctrl->id) { | |
705 | case V4L2_CID_BRIGHTNESS: | |
cf6832af HV |
706 | err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value); |
707 | if (!err) | |
708 | decoder->tvp514x_regs[REG_BRIGHTNESS].val = value; | |
07b1747c VH |
709 | break; |
710 | case V4L2_CID_CONTRAST: | |
62ef80a1 | 711 | err = tvp514x_write_reg(sd, REG_CONTRAST, value); |
cf6832af HV |
712 | if (!err) |
713 | decoder->tvp514x_regs[REG_CONTRAST].val = value; | |
07b1747c VH |
714 | break; |
715 | case V4L2_CID_SATURATION: | |
62ef80a1 | 716 | err = tvp514x_write_reg(sd, REG_SATURATION, value); |
cf6832af HV |
717 | if (!err) |
718 | decoder->tvp514x_regs[REG_SATURATION].val = value; | |
07b1747c VH |
719 | break; |
720 | case V4L2_CID_HUE: | |
721 | if (value == 180) | |
722 | value = 0x7F; | |
723 | else if (value == -180) | |
724 | value = 0x80; | |
62ef80a1 | 725 | err = tvp514x_write_reg(sd, REG_HUE, value); |
cf6832af HV |
726 | if (!err) |
727 | decoder->tvp514x_regs[REG_HUE].val = value; | |
07b1747c VH |
728 | break; |
729 | case V4L2_CID_AUTOGAIN: | |
cf6832af HV |
730 | err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c); |
731 | if (!err) | |
732 | decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value; | |
07b1747c | 733 | break; |
07b1747c VH |
734 | } |
735 | ||
3907b072 | 736 | v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n", |
cf6832af | 737 | ctrl->id, ctrl->val); |
07b1747c VH |
738 | return err; |
739 | } | |
740 | ||
83811913 HV |
741 | /** |
742 | * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt | |
743 | * @sd: pointer to standard V4L2 sub-device structure | |
744 | * @index: index of pixelcode to retrieve | |
745 | * @code: receives the pixelcode | |
746 | * | |
747 | * Enumerates supported mediabus formats | |
748 | */ | |
749 | static int | |
750 | tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index, | |
751 | enum v4l2_mbus_pixelcode *code) | |
752 | { | |
753 | if (index) | |
754 | return -EINVAL; | |
755 | ||
756 | *code = V4L2_MBUS_FMT_YUYV10_2X10; | |
757 | return 0; | |
758 | } | |
759 | ||
83811913 | 760 | /** |
5b38b0f8 | 761 | * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt |
83811913 HV |
762 | * @sd: pointer to standard V4L2 sub-device structure |
763 | * @f: pointer to the mediabus format structure | |
764 | * | |
765 | * Negotiates the image capture size and mediabus format. | |
766 | */ | |
767 | static int | |
768 | tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f) | |
769 | { | |
770 | struct tvp514x_decoder *decoder = to_decoder(sd); | |
771 | enum tvp514x_std current_std; | |
772 | ||
773 | if (f == NULL) | |
774 | return -EINVAL; | |
775 | ||
776 | /* Calculate height and width based on current standard */ | |
777 | current_std = decoder->current_std; | |
778 | ||
5b38b0f8 | 779 | f->code = V4L2_MBUS_FMT_YUYV8_2X8; |
83811913 HV |
780 | f->width = decoder->std_list[current_std].width; |
781 | f->height = decoder->std_list[current_std].height; | |
782 | f->field = V4L2_FIELD_INTERLACED; | |
783 | f->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
83811913 HV |
784 | v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n", |
785 | f->width, f->height); | |
786 | return 0; | |
787 | } | |
788 | ||
c1c9d09c MK |
789 | /** |
790 | * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm | |
62ef80a1 | 791 | * @sd: pointer to standard V4L2 sub-device structure |
07b1747c VH |
792 | * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure |
793 | * | |
794 | * Returns the decoder's video CAPTURE parameters. | |
795 | */ | |
796 | static int | |
62ef80a1 | 797 | tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a) |
07b1747c | 798 | { |
62ef80a1 | 799 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
800 | struct v4l2_captureparm *cparm; |
801 | enum tvp514x_std current_std; | |
802 | ||
803 | if (a == NULL) | |
804 | return -EINVAL; | |
805 | ||
806 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
c1c9d09c MK |
807 | /* only capture is supported */ |
808 | return -EINVAL; | |
07b1747c | 809 | |
07b1747c | 810 | /* get the current standard */ |
2db4e78f | 811 | current_std = decoder->current_std; |
07b1747c VH |
812 | |
813 | cparm = &a->parm.capture; | |
814 | cparm->capability = V4L2_CAP_TIMEPERFRAME; | |
815 | cparm->timeperframe = | |
816 | decoder->std_list[current_std].standard.frameperiod; | |
817 | ||
818 | return 0; | |
819 | } | |
820 | ||
c1c9d09c MK |
821 | /** |
822 | * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm | |
62ef80a1 | 823 | * @sd: pointer to standard V4L2 sub-device structure |
07b1747c VH |
824 | * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure |
825 | * | |
826 | * Configures the decoder to use the input parameters, if possible. If | |
827 | * not possible, returns the appropriate error code. | |
828 | */ | |
829 | static int | |
62ef80a1 | 830 | tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a) |
07b1747c | 831 | { |
62ef80a1 | 832 | struct tvp514x_decoder *decoder = to_decoder(sd); |
07b1747c VH |
833 | struct v4l2_fract *timeperframe; |
834 | enum tvp514x_std current_std; | |
835 | ||
836 | if (a == NULL) | |
837 | return -EINVAL; | |
838 | ||
839 | if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
c1c9d09c MK |
840 | /* only capture is supported */ |
841 | return -EINVAL; | |
07b1747c VH |
842 | |
843 | timeperframe = &a->parm.capture.timeperframe; | |
844 | ||
845 | /* get the current standard */ | |
2db4e78f | 846 | current_std = decoder->current_std; |
07b1747c VH |
847 | |
848 | *timeperframe = | |
849 | decoder->std_list[current_std].standard.frameperiod; | |
850 | ||
851 | return 0; | |
852 | } | |
853 | ||
c1c9d09c MK |
854 | /** |
855 | * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream | |
62ef80a1 MK |
856 | * @sd: pointer to standard V4L2 sub-device structure |
857 | * @enable: streaming enable or disable | |
07b1747c | 858 | * |
62ef80a1 | 859 | * Sets streaming to enable or disable, if possible. |
07b1747c | 860 | */ |
62ef80a1 | 861 | static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable) |
07b1747c | 862 | { |
07b1747c | 863 | int err = 0; |
62ef80a1 MK |
864 | struct i2c_client *client = v4l2_get_subdevdata(sd); |
865 | struct tvp514x_decoder *decoder = to_decoder(sd); | |
07b1747c | 866 | |
62ef80a1 MK |
867 | if (decoder->streaming == enable) |
868 | return 0; | |
07b1747c | 869 | |
62ef80a1 MK |
870 | switch (enable) { |
871 | case 0: | |
872 | { | |
873 | /* Power Down Sequence */ | |
874 | err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01); | |
875 | if (err) { | |
876 | v4l2_err(sd, "Unable to turn off decoder\n"); | |
877 | return err; | |
878 | } | |
879 | decoder->streaming = enable; | |
07b1747c | 880 | break; |
62ef80a1 MK |
881 | } |
882 | case 1: | |
883 | { | |
884 | struct tvp514x_reg *int_seq = (struct tvp514x_reg *) | |
885 | client->driver->id_table->driver_data; | |
07b1747c | 886 | |
62ef80a1 MK |
887 | /* Power Up Sequence */ |
888 | err = tvp514x_write_regs(sd, int_seq); | |
889 | if (err) { | |
890 | v4l2_err(sd, "Unable to turn on decoder\n"); | |
891 | return err; | |
892 | } | |
893 | /* Detect if not already detected */ | |
894 | err = tvp514x_detect(sd, decoder); | |
895 | if (err) { | |
896 | v4l2_err(sd, "Unable to detect decoder\n"); | |
897 | return err; | |
07b1747c | 898 | } |
62ef80a1 MK |
899 | err = tvp514x_configure(sd, decoder); |
900 | if (err) { | |
901 | v4l2_err(sd, "Unable to configure decoder\n"); | |
902 | return err; | |
903 | } | |
904 | decoder->streaming = enable; | |
07b1747c | 905 | break; |
62ef80a1 | 906 | } |
07b1747c VH |
907 | default: |
908 | err = -ENODEV; | |
909 | break; | |
910 | } | |
911 | ||
912 | return err; | |
913 | } | |
914 | ||
cf6832af | 915 | static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = { |
62ef80a1 | 916 | .s_ctrl = tvp514x_s_ctrl, |
cf6832af HV |
917 | }; |
918 | ||
5b38b0f8 MH |
919 | /** |
920 | * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code | |
921 | * @sd: pointer to standard V4L2 sub-device structure | |
922 | * @fh: file handle | |
923 | * @code: pointer to v4l2_subdev_mbus_code_enum structure | |
924 | * | |
925 | * Enumertaes mbus codes supported | |
926 | */ | |
927 | static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd, | |
928 | struct v4l2_subdev_fh *fh, | |
929 | struct v4l2_subdev_mbus_code_enum *code) | |
930 | { | |
931 | u32 pad = code->pad; | |
932 | u32 index = code->index; | |
933 | ||
934 | memset(code, 0, sizeof(*code)); | |
935 | code->index = index; | |
936 | code->pad = pad; | |
937 | ||
938 | if (index != 0) | |
939 | return -EINVAL; | |
940 | ||
941 | code->code = V4L2_MBUS_FMT_YUYV8_2X8; | |
942 | ||
943 | return 0; | |
944 | } | |
945 | ||
946 | /** | |
947 | * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format | |
948 | * @sd: pointer to standard V4L2 sub-device structure | |
949 | * @fh: file handle | |
950 | * @format: pointer to v4l2_subdev_format structure | |
951 | * | |
952 | * Retrieves pad format which is active or tried based on requirement | |
953 | */ | |
954 | static int tvp514x_get_pad_format(struct v4l2_subdev *sd, | |
955 | struct v4l2_subdev_fh *fh, | |
956 | struct v4l2_subdev_format *format) | |
957 | { | |
958 | struct tvp514x_decoder *decoder = to_decoder(sd); | |
959 | __u32 which = format->which; | |
960 | ||
961 | if (which == V4L2_SUBDEV_FORMAT_ACTIVE) { | |
962 | format->format = decoder->format; | |
963 | return 0; | |
964 | } | |
965 | ||
966 | format->format.code = V4L2_MBUS_FMT_YUYV8_2X8; | |
967 | format->format.width = tvp514x_std_list[decoder->current_std].width; | |
968 | format->format.height = tvp514x_std_list[decoder->current_std].height; | |
969 | format->format.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
970 | format->format.field = V4L2_FIELD_INTERLACED; | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
975 | /** | |
976 | * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format | |
977 | * @sd: pointer to standard V4L2 sub-device structure | |
978 | * @fh: file handle | |
979 | * @format: pointer to v4l2_subdev_format structure | |
980 | * | |
981 | * Set pad format for the output pad | |
982 | */ | |
983 | static int tvp514x_set_pad_format(struct v4l2_subdev *sd, | |
984 | struct v4l2_subdev_fh *fh, | |
985 | struct v4l2_subdev_format *fmt) | |
986 | { | |
987 | struct tvp514x_decoder *decoder = to_decoder(sd); | |
988 | ||
989 | if (fmt->format.field != V4L2_FIELD_INTERLACED || | |
990 | fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 || | |
991 | fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M || | |
992 | fmt->format.width != tvp514x_std_list[decoder->current_std].width || | |
993 | fmt->format.height != tvp514x_std_list[decoder->current_std].height) | |
994 | return -EINVAL; | |
995 | ||
996 | decoder->format = fmt->format; | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
cf6832af HV |
1001 | static const struct v4l2_subdev_core_ops tvp514x_core_ops = { |
1002 | .g_ext_ctrls = v4l2_subdev_g_ext_ctrls, | |
1003 | .try_ext_ctrls = v4l2_subdev_try_ext_ctrls, | |
1004 | .s_ext_ctrls = v4l2_subdev_s_ext_ctrls, | |
1005 | .g_ctrl = v4l2_subdev_g_ctrl, | |
1006 | .s_ctrl = v4l2_subdev_s_ctrl, | |
1007 | .queryctrl = v4l2_subdev_queryctrl, | |
1008 | .querymenu = v4l2_subdev_querymenu, | |
62ef80a1 MK |
1009 | .s_std = tvp514x_s_std, |
1010 | }; | |
07b1747c | 1011 | |
62ef80a1 MK |
1012 | static const struct v4l2_subdev_video_ops tvp514x_video_ops = { |
1013 | .s_routing = tvp514x_s_routing, | |
1014 | .querystd = tvp514x_querystd, | |
83811913 HV |
1015 | .enum_mbus_fmt = tvp514x_enum_mbus_fmt, |
1016 | .g_mbus_fmt = tvp514x_mbus_fmt, | |
1017 | .try_mbus_fmt = tvp514x_mbus_fmt, | |
1018 | .s_mbus_fmt = tvp514x_mbus_fmt, | |
62ef80a1 MK |
1019 | .g_parm = tvp514x_g_parm, |
1020 | .s_parm = tvp514x_s_parm, | |
1021 | .s_stream = tvp514x_s_stream, | |
1022 | }; | |
07b1747c | 1023 | |
5b38b0f8 MH |
1024 | static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = { |
1025 | .enum_mbus_code = tvp514x_enum_mbus_code, | |
1026 | .get_fmt = tvp514x_get_pad_format, | |
1027 | .set_fmt = tvp514x_set_pad_format, | |
1028 | }; | |
1029 | ||
62ef80a1 MK |
1030 | static const struct v4l2_subdev_ops tvp514x_ops = { |
1031 | .core = &tvp514x_core_ops, | |
1032 | .video = &tvp514x_video_ops, | |
5b38b0f8 | 1033 | .pad = &tvp514x_pad_ops, |
07b1747c VH |
1034 | }; |
1035 | ||
07b1747c | 1036 | static struct tvp514x_decoder tvp514x_dev = { |
62ef80a1 | 1037 | .streaming = 0, |
5b38b0f8 MH |
1038 | .fmt_list = tvp514x_fmt_list, |
1039 | .num_fmts = ARRAY_SIZE(tvp514x_fmt_list), | |
1040 | .pix = { | |
1041 | /* Default to NTSC 8-bit YUV 422 */ | |
1042 | .width = NTSC_NUM_ACTIVE_PIXELS, | |
1043 | .height = NTSC_NUM_ACTIVE_LINES, | |
1044 | .pixelformat = V4L2_PIX_FMT_UYVY, | |
1045 | .field = V4L2_FIELD_INTERLACED, | |
1046 | .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2, | |
1047 | .sizeimage = NTSC_NUM_ACTIVE_PIXELS * 2 * | |
1048 | NTSC_NUM_ACTIVE_LINES, | |
1049 | .colorspace = V4L2_COLORSPACE_SMPTE170M, | |
1050 | }, | |
07b1747c VH |
1051 | .current_std = STD_NTSC_MJ, |
1052 | .std_list = tvp514x_std_list, | |
1053 | .num_stds = ARRAY_SIZE(tvp514x_std_list), | |
62ef80a1 | 1054 | |
07b1747c VH |
1055 | }; |
1056 | ||
c1c9d09c MK |
1057 | /** |
1058 | * tvp514x_probe() - decoder driver i2c probe handler | |
07b1747c | 1059 | * @client: i2c driver client device structure |
62ef80a1 | 1060 | * @id: i2c driver id table |
07b1747c VH |
1061 | * |
1062 | * Register decoder as an i2c client device and V4L2 | |
1063 | * device. | |
1064 | */ | |
1065 | static int | |
1066 | tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id) | |
1067 | { | |
6722e0ef | 1068 | struct tvp514x_decoder *decoder; |
62ef80a1 | 1069 | struct v4l2_subdev *sd; |
5b38b0f8 | 1070 | int ret; |
07b1747c VH |
1071 | |
1072 | /* Check if the adapter supports the needed features */ | |
1073 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
1074 | return -EIO; | |
1075 | ||
62ef80a1 MK |
1076 | if (!client->dev.platform_data) { |
1077 | v4l2_err(client, "No platform data!!\n"); | |
1078 | return -ENODEV; | |
1079 | } | |
1080 | ||
08754d31 | 1081 | decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL); |
6722e0ef SAS |
1082 | if (!decoder) |
1083 | return -ENOMEM; | |
1084 | ||
c1c9d09c | 1085 | /* Initialize the tvp514x_decoder with default configuration */ |
6722e0ef | 1086 | *decoder = tvp514x_dev; |
62ef80a1 | 1087 | /* Copy default register configuration */ |
6722e0ef SAS |
1088 | memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default, |
1089 | sizeof(tvp514x_reg_list_default)); | |
62ef80a1 | 1090 | |
c1c9d09c | 1091 | /* Copy board specific information here */ |
62ef80a1 MK |
1092 | decoder->pdata = client->dev.platform_data; |
1093 | ||
c1c9d09c | 1094 | /** |
07b1747c VH |
1095 | * Fetch platform specific data, and configure the |
1096 | * tvp514x_reg_list[] accordingly. Since this is one | |
1097 | * time configuration, no need to preserve. | |
1098 | */ | |
6722e0ef | 1099 | decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |= |
62ef80a1 | 1100 | (decoder->pdata->clk_polarity << 1); |
6722e0ef | 1101 | decoder->tvp514x_regs[REG_SYNC_CONTROL].val |= |
62ef80a1 MK |
1102 | ((decoder->pdata->hs_polarity << 2) | |
1103 | (decoder->pdata->vs_polarity << 3)); | |
1104 | /* Set default standard to auto */ | |
1105 | decoder->tvp514x_regs[REG_VIDEO_STD].val = | |
1106 | VIDEO_STD_AUTO_SWITCH_BIT; | |
07b1747c VH |
1107 | |
1108 | /* Register with V4L2 layer as slave device */ | |
62ef80a1 MK |
1109 | sd = &decoder->sd; |
1110 | v4l2_i2c_subdev_init(sd, client, &tvp514x_ops); | |
5b38b0f8 MH |
1111 | strlcpy(sd->name, TVP514X_MODULE_NAME, sizeof(sd->name)); |
1112 | ||
1113 | #if defined(CONFIG_MEDIA_CONTROLLER) | |
1114 | decoder->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1115 | decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; | |
1116 | decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER; | |
1117 | ||
1118 | ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0); | |
1119 | if (ret < 0) { | |
1120 | v4l2_err(sd, "%s decoder driver failed to register !!\n", | |
1121 | sd->name); | |
5b38b0f8 MH |
1122 | return ret; |
1123 | } | |
1124 | #endif | |
cf6832af HV |
1125 | v4l2_ctrl_handler_init(&decoder->hdl, 5); |
1126 | v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops, | |
1127 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 128); | |
1128 | v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops, | |
1129 | V4L2_CID_CONTRAST, 0, 255, 1, 128); | |
1130 | v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops, | |
1131 | V4L2_CID_SATURATION, 0, 255, 1, 128); | |
1132 | v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops, | |
1133 | V4L2_CID_HUE, -180, 180, 180, 0); | |
1134 | v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops, | |
1135 | V4L2_CID_AUTOGAIN, 0, 1, 1, 1); | |
1136 | sd->ctrl_handler = &decoder->hdl; | |
1137 | if (decoder->hdl.error) { | |
5b38b0f8 | 1138 | ret = decoder->hdl.error; |
cf6832af HV |
1139 | |
1140 | v4l2_ctrl_handler_free(&decoder->hdl); | |
5b38b0f8 | 1141 | return ret; |
cf6832af HV |
1142 | } |
1143 | v4l2_ctrl_handler_setup(&decoder->hdl); | |
1144 | ||
62ef80a1 MK |
1145 | v4l2_info(sd, "%s decoder driver registered !!\n", sd->name); |
1146 | ||
07b1747c | 1147 | return 0; |
6722e0ef | 1148 | |
07b1747c VH |
1149 | } |
1150 | ||
c1c9d09c MK |
1151 | /** |
1152 | * tvp514x_remove() - decoder driver i2c remove handler | |
07b1747c VH |
1153 | * @client: i2c driver client device structure |
1154 | * | |
1155 | * Unregister decoder as an i2c client device and V4L2 | |
1156 | * device. Complement of tvp514x_probe(). | |
1157 | */ | |
62ef80a1 | 1158 | static int tvp514x_remove(struct i2c_client *client) |
07b1747c | 1159 | { |
62ef80a1 MK |
1160 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
1161 | struct tvp514x_decoder *decoder = to_decoder(sd); | |
07b1747c | 1162 | |
62ef80a1 | 1163 | v4l2_device_unregister_subdev(sd); |
5b38b0f8 MH |
1164 | #if defined(CONFIG_MEDIA_CONTROLLER) |
1165 | media_entity_cleanup(&decoder->sd.entity); | |
1166 | #endif | |
cf6832af | 1167 | v4l2_ctrl_handler_free(&decoder->hdl); |
07b1747c VH |
1168 | return 0; |
1169 | } | |
c1c9d09c | 1170 | /* TVP5146 Init/Power on Sequence */ |
07b1747c VH |
1171 | static const struct tvp514x_reg tvp5146_init_reg_seq[] = { |
1172 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02}, | |
1173 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1174 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80}, | |
1175 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01}, | |
1176 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60}, | |
1177 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1178 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0}, | |
1179 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01}, | |
1180 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00}, | |
1181 | {TOK_WRITE, REG_OPERATION_MODE, 0x01}, | |
1182 | {TOK_WRITE, REG_OPERATION_MODE, 0x00}, | |
62ef80a1 | 1183 | {TOK_TERM, 0, 0}, |
07b1747c | 1184 | }; |
62ef80a1 | 1185 | |
c1c9d09c | 1186 | /* TVP5147 Init/Power on Sequence */ |
07b1747c VH |
1187 | static const struct tvp514x_reg tvp5147_init_reg_seq[] = { |
1188 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02}, | |
1189 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1190 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80}, | |
1191 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01}, | |
1192 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60}, | |
1193 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1194 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0}, | |
1195 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01}, | |
1196 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16}, | |
1197 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1198 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0}, | |
1199 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16}, | |
1200 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60}, | |
1201 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00}, | |
1202 | {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0}, | |
1203 | {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00}, | |
1204 | {TOK_WRITE, REG_OPERATION_MODE, 0x01}, | |
1205 | {TOK_WRITE, REG_OPERATION_MODE, 0x00}, | |
62ef80a1 | 1206 | {TOK_TERM, 0, 0}, |
07b1747c | 1207 | }; |
62ef80a1 | 1208 | |
c1c9d09c | 1209 | /* TVP5146M2/TVP5147M1 Init/Power on Sequence */ |
07b1747c VH |
1210 | static const struct tvp514x_reg tvp514xm_init_reg_seq[] = { |
1211 | {TOK_WRITE, REG_OPERATION_MODE, 0x01}, | |
1212 | {TOK_WRITE, REG_OPERATION_MODE, 0x00}, | |
62ef80a1 | 1213 | {TOK_TERM, 0, 0}, |
07b1747c | 1214 | }; |
62ef80a1 | 1215 | |
c1c9d09c | 1216 | /** |
07b1747c VH |
1217 | * I2C Device Table - |
1218 | * | |
1219 | * name - Name of the actual device/chip. | |
1220 | * driver_data - Driver data | |
1221 | */ | |
1222 | static const struct i2c_device_id tvp514x_id[] = { | |
62ef80a1 MK |
1223 | {"tvp5146", (unsigned long)tvp5146_init_reg_seq}, |
1224 | {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq}, | |
1225 | {"tvp5147", (unsigned long)tvp5147_init_reg_seq}, | |
1226 | {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq}, | |
07b1747c VH |
1227 | {}, |
1228 | }; | |
1229 | ||
1230 | MODULE_DEVICE_TABLE(i2c, tvp514x_id); | |
1231 | ||
62ef80a1 | 1232 | static struct i2c_driver tvp514x_driver = { |
07b1747c | 1233 | .driver = { |
62ef80a1 MK |
1234 | .owner = THIS_MODULE, |
1235 | .name = TVP514X_MODULE_NAME, | |
1236 | }, | |
07b1747c | 1237 | .probe = tvp514x_probe, |
62ef80a1 | 1238 | .remove = tvp514x_remove, |
07b1747c VH |
1239 | .id_table = tvp514x_id, |
1240 | }; | |
1241 | ||
c6e8d86f | 1242 | module_i2c_driver(tvp514x_driver); |