blob: 80b214efb24ea5fcaeee25fc2a3e139186ba0054 [file] [log] [blame]
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001/*
2 * tw9910 Video Driver
3 *
4 * Copyright (C) 2008 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
6 *
7 * Based on ov772x driver,
8 *
9 * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
11 * Copyright (C) 2008 Magnus Damm
12 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/i2c.h>
22#include <linux/slab.h>
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/videodev2.h>
26#include <media/v4l2-chip-ident.h>
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -030027#include <media/v4l2-subdev.h>
Kuninori Morimotoed922a82008-12-29 06:04:06 -030028#include <media/soc_camera.h>
29#include <media/tw9910.h>
30
31#define GET_ID(val) ((val & 0xF8) >> 3)
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -030032#define GET_REV(val) (val & 0x07)
Kuninori Morimotoed922a82008-12-29 06:04:06 -030033
34/*
35 * register offset
36 */
37#define ID 0x00 /* Product ID Code Register */
38#define STATUS1 0x01 /* Chip Status Register I */
39#define INFORM 0x02 /* Input Format */
40#define OPFORM 0x03 /* Output Format Control Register */
41#define DLYCTR 0x04 /* Hysteresis and HSYNC Delay Control */
42#define OUTCTR1 0x05 /* Output Control I */
43#define ACNTL1 0x06 /* Analog Control Register 1 */
44#define CROP_HI 0x07 /* Cropping Register, High */
45#define VDELAY_LO 0x08 /* Vertical Delay Register, Low */
46#define VACTIVE_LO 0x09 /* Vertical Active Register, Low */
47#define HDELAY_LO 0x0A /* Horizontal Delay Register, Low */
48#define HACTIVE_LO 0x0B /* Horizontal Active Register, Low */
49#define CNTRL1 0x0C /* Control Register I */
50#define VSCALE_LO 0x0D /* Vertical Scaling Register, Low */
51#define SCALE_HI 0x0E /* Scaling Register, High */
52#define HSCALE_LO 0x0F /* Horizontal Scaling Register, Low */
53#define BRIGHT 0x10 /* BRIGHTNESS Control Register */
54#define CONTRAST 0x11 /* CONTRAST Control Register */
55#define SHARPNESS 0x12 /* SHARPNESS Control Register I */
56#define SAT_U 0x13 /* Chroma (U) Gain Register */
57#define SAT_V 0x14 /* Chroma (V) Gain Register */
58#define HUE 0x15 /* Hue Control Register */
59#define CORING1 0x17
60#define CORING2 0x18 /* Coring and IF compensation */
61#define VBICNTL 0x19 /* VBI Control Register */
62#define ACNTL2 0x1A /* Analog Control 2 */
63#define OUTCTR2 0x1B /* Output Control 2 */
64#define SDT 0x1C /* Standard Selection */
65#define SDTR 0x1D /* Standard Recognition */
66#define TEST 0x1F /* Test Control Register */
67#define CLMPG 0x20 /* Clamping Gain */
68#define IAGC 0x21 /* Individual AGC Gain */
69#define AGCGAIN 0x22 /* AGC Gain */
70#define PEAKWT 0x23 /* White Peak Threshold */
71#define CLMPL 0x24 /* Clamp level */
72#define SYNCT 0x25 /* Sync Amplitude */
73#define MISSCNT 0x26 /* Sync Miss Count Register */
74#define PCLAMP 0x27 /* Clamp Position Register */
75#define VCNTL1 0x28 /* Vertical Control I */
76#define VCNTL2 0x29 /* Vertical Control II */
77#define CKILL 0x2A /* Color Killer Level Control */
78#define COMB 0x2B /* Comb Filter Control */
79#define LDLY 0x2C /* Luma Delay and H Filter Control */
80#define MISC1 0x2D /* Miscellaneous Control I */
81#define LOOP 0x2E /* LOOP Control Register */
82#define MISC2 0x2F /* Miscellaneous Control II */
83#define MVSN 0x30 /* Macrovision Detection */
84#define STATUS2 0x31 /* Chip STATUS II */
85#define HFREF 0x32 /* H monitor */
86#define CLMD 0x33 /* CLAMP MODE */
87#define IDCNTL 0x34 /* ID Detection Control */
88#define CLCNTL1 0x35 /* Clamp Control I */
89#define ANAPLLCTL 0x4C
90#define VBIMIN 0x4D
91#define HSLOWCTL 0x4E
92#define WSS3 0x4F
93#define FILLDATA 0x50
94#define SDID 0x51
95#define DID 0x52
96#define WSS1 0x53
97#define WSS2 0x54
98#define VVBI 0x55
99#define LCTL6 0x56
100#define LCTL7 0x57
101#define LCTL8 0x58
102#define LCTL9 0x59
103#define LCTL10 0x5A
104#define LCTL11 0x5B
105#define LCTL12 0x5C
106#define LCTL13 0x5D
107#define LCTL14 0x5E
108#define LCTL15 0x5F
109#define LCTL16 0x60
110#define LCTL17 0x61
111#define LCTL18 0x62
112#define LCTL19 0x63
113#define LCTL20 0x64
114#define LCTL21 0x65
115#define LCTL22 0x66
116#define LCTL23 0x67
117#define LCTL24 0x68
118#define LCTL25 0x69
119#define LCTL26 0x6A
120#define HSGEGIN 0x6B
121#define HSEND 0x6C
122#define OVSDLY 0x6D
123#define OVSEND 0x6E
124#define VBIDELAY 0x6F
125
126/*
127 * register detail
128 */
129
130/* INFORM */
131#define FC27_ON 0x40 /* 1 : Input crystal clock frequency is 27MHz */
132#define FC27_FF 0x00 /* 0 : Square pixel mode. */
133 /* Must use 24.54MHz for 60Hz field rate */
134 /* source or 29.5MHz for 50Hz field rate */
135#define IFSEL_S 0x10 /* 01 : S-video decoding */
136#define IFSEL_C 0x00 /* 00 : Composite video decoding */
137 /* Y input video selection */
138#define YSEL_M0 0x00 /* 00 : Mux0 selected */
139#define YSEL_M1 0x04 /* 01 : Mux1 selected */
140#define YSEL_M2 0x08 /* 10 : Mux2 selected */
141#define YSEL_M3 0x10 /* 11 : Mux3 selected */
142
143/* OPFORM */
144#define MODE 0x80 /* 0 : CCIR601 compatible YCrCb 4:2:2 format */
145 /* 1 : ITU-R-656 compatible data sequence format */
146#define LEN 0x40 /* 0 : 8-bit YCrCb 4:2:2 output format */
147 /* 1 : 16-bit YCrCb 4:2:2 output format.*/
148#define LLCMODE 0x20 /* 1 : LLC output mode. */
149 /* 0 : free-run output mode */
150#define AINC 0x10 /* Serial interface auto-indexing control */
151 /* 0 : auto-increment */
152 /* 1 : non-auto */
153#define VSCTL 0x08 /* 1 : Vertical out ctrl by DVALID */
154 /* 0 : Vertical out ctrl by HACTIVE and DVALID */
Kuninori Morimoto06f292e2009-12-11 11:34:50 -0300155#define OEN_TRI_SEL_MASK 0x07
156#define OEN_TRI_SEL_ALL_ON 0x00 /* Enable output for Rev0/Rev1 */
157#define OEN_TRI_SEL_ALL_OFF_r0 0x06 /* All tri-stated for Rev0 */
158#define OEN_TRI_SEL_ALL_OFF_r1 0x07 /* All tri-stated for Rev1 */
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300159
160/* OUTCTR1 */
161#define VSP_LO 0x00 /* 0 : VS pin output polarity is active low */
162#define VSP_HI 0x80 /* 1 : VS pin output polarity is active high. */
163 /* VS pin output control */
164#define VSSL_VSYNC 0x00 /* 0 : VSYNC */
165#define VSSL_VACT 0x10 /* 1 : VACT */
166#define VSSL_FIELD 0x20 /* 2 : FIELD */
167#define VSSL_VVALID 0x30 /* 3 : VVALID */
168#define VSSL_ZERO 0x70 /* 7 : 0 */
169#define HSP_LOW 0x00 /* 0 : HS pin output polarity is active low */
170#define HSP_HI 0x08 /* 1 : HS pin output polarity is active high.*/
171 /* HS pin output control */
172#define HSSL_HACT 0x00 /* 0 : HACT */
173#define HSSL_HSYNC 0x01 /* 1 : HSYNC */
174#define HSSL_DVALID 0x02 /* 2 : DVALID */
175#define HSSL_HLOCK 0x03 /* 3 : HLOCK */
176#define HSSL_ASYNCW 0x04 /* 4 : ASYNCW */
177#define HSSL_ZERO 0x07 /* 7 : 0 */
178
179/* ACNTL1 */
180#define SRESET 0x80 /* resets the device to its default state
181 * but all register content remain unchanged.
182 * This bit is self-resetting.
183 */
184
185/* VBICNTL */
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300186
187/* RTSEL : control the real time signal output from the MPOUT pin */
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300188#define RTSEL_MASK 0x07
189#define RTSEL_VLOSS 0x00 /* 0000 = Video loss */
190#define RTSEL_HLOCK 0x01 /* 0001 = H-lock */
191#define RTSEL_SLOCK 0x02 /* 0010 = S-lock */
192#define RTSEL_VLOCK 0x03 /* 0011 = V-lock */
193#define RTSEL_MONO 0x04 /* 0100 = MONO */
194#define RTSEL_DET50 0x05 /* 0101 = DET50 */
195#define RTSEL_FIELD 0x06 /* 0110 = FIELD */
196#define RTSEL_RTCO 0x07 /* 0111 = RTCO ( Real Time Control ) */
197
198/*
199 * structure
200 */
201
202struct regval_list {
203 unsigned char reg_num;
204 unsigned char value;
205};
206
207struct tw9910_scale_ctrl {
208 char *name;
209 unsigned short width;
210 unsigned short height;
211 u16 hscale;
212 u16 vscale;
213};
214
215struct tw9910_cropping_ctrl {
216 u16 vdelay;
217 u16 vactive;
218 u16 hdelay;
219 u16 hactive;
220};
221
222struct tw9910_hsync_ctrl {
223 u16 start;
224 u16 end;
225};
226
227struct tw9910_priv {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300228 struct v4l2_subdev subdev;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300229 struct tw9910_video_info *info;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300230 const struct tw9910_scale_ctrl *scale;
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -0300231 u32 revision;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300232};
233
234/*
235 * register settings
236 */
237
238#define ENDMARKER { 0xff, 0xff }
239
240static const struct regval_list tw9910_default_regs[] =
241{
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300242 { OUTCTR1, VSP_LO | VSSL_VVALID | HSP_HI | HSSL_HSYNC },
243 ENDMARKER,
244};
245
246static const struct soc_camera_data_format tw9910_color_fmt[] = {
247 {
248 .name = "VYUY",
249 .fourcc = V4L2_PIX_FMT_VYUY,
250 .depth = 16,
251 .colorspace = V4L2_COLORSPACE_SMPTE170M,
252 }
253};
254
255static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
256 {
257 .name = "NTSC SQ",
258 .width = 640,
259 .height = 480,
260 .hscale = 0x0100,
261 .vscale = 0x0100,
262 },
263 {
264 .name = "NTSC CCIR601",
265 .width = 720,
266 .height = 480,
267 .hscale = 0x0100,
268 .vscale = 0x0100,
269 },
270 {
271 .name = "NTSC SQ (CIF)",
272 .width = 320,
273 .height = 240,
274 .hscale = 0x0200,
275 .vscale = 0x0200,
276 },
277 {
278 .name = "NTSC CCIR601 (CIF)",
279 .width = 360,
280 .height = 240,
281 .hscale = 0x0200,
282 .vscale = 0x0200,
283 },
284 {
285 .name = "NTSC SQ (QCIF)",
286 .width = 160,
287 .height = 120,
288 .hscale = 0x0400,
289 .vscale = 0x0400,
290 },
291 {
292 .name = "NTSC CCIR601 (QCIF)",
293 .width = 180,
294 .height = 120,
295 .hscale = 0x0400,
296 .vscale = 0x0400,
297 },
298};
299
300static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
301 {
302 .name = "PAL SQ",
303 .width = 768,
304 .height = 576,
305 .hscale = 0x0100,
306 .vscale = 0x0100,
307 },
308 {
309 .name = "PAL CCIR601",
310 .width = 720,
311 .height = 576,
312 .hscale = 0x0100,
313 .vscale = 0x0100,
314 },
315 {
316 .name = "PAL SQ (CIF)",
317 .width = 384,
318 .height = 288,
319 .hscale = 0x0200,
320 .vscale = 0x0200,
321 },
322 {
323 .name = "PAL CCIR601 (CIF)",
324 .width = 360,
325 .height = 288,
326 .hscale = 0x0200,
327 .vscale = 0x0200,
328 },
329 {
330 .name = "PAL SQ (QCIF)",
331 .width = 192,
332 .height = 144,
333 .hscale = 0x0400,
334 .vscale = 0x0400,
335 },
336 {
337 .name = "PAL CCIR601 (QCIF)",
338 .width = 180,
339 .height = 144,
340 .hscale = 0x0400,
341 .vscale = 0x0400,
342 },
343};
344
345static const struct tw9910_cropping_ctrl tw9910_cropping_ctrl = {
346 .vdelay = 0x0012,
347 .vactive = 0x00F0,
348 .hdelay = 0x0010,
349 .hactive = 0x02D0,
350};
351
352static const struct tw9910_hsync_ctrl tw9910_hsync_ctrl = {
353 .start = 0x0260,
354 .end = 0x0300,
355};
356
357/*
358 * general function
359 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300360static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
361{
Guennadi Liakhovetski96c75392009-08-25 11:53:23 -0300362 return container_of(i2c_get_clientdata(client), struct tw9910_priv,
363 subdev);
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300364}
365
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300366static int tw9910_set_scale(struct i2c_client *client,
367 const struct tw9910_scale_ctrl *scale)
368{
369 int ret;
370
371 ret = i2c_smbus_write_byte_data(client, SCALE_HI,
372 (scale->vscale & 0x0F00) >> 4 |
373 (scale->hscale & 0x0F00) >> 8);
374 if (ret < 0)
375 return ret;
376
377 ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
378 scale->hscale & 0x00FF);
379 if (ret < 0)
380 return ret;
381
382 ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
383 scale->vscale & 0x00FF);
384
385 return ret;
386}
387
388static int tw9910_set_cropping(struct i2c_client *client,
389 const struct tw9910_cropping_ctrl *cropping)
390{
391 int ret;
392
393 ret = i2c_smbus_write_byte_data(client, CROP_HI,
394 (cropping->vdelay & 0x0300) >> 2 |
395 (cropping->vactive & 0x0300) >> 4 |
396 (cropping->hdelay & 0x0300) >> 6 |
397 (cropping->hactive & 0x0300) >> 8);
398 if (ret < 0)
399 return ret;
400
401 ret = i2c_smbus_write_byte_data(client, VDELAY_LO,
402 cropping->vdelay & 0x00FF);
403 if (ret < 0)
404 return ret;
405
406 ret = i2c_smbus_write_byte_data(client, VACTIVE_LO,
407 cropping->vactive & 0x00FF);
408 if (ret < 0)
409 return ret;
410
411 ret = i2c_smbus_write_byte_data(client, HDELAY_LO,
412 cropping->hdelay & 0x00FF);
413 if (ret < 0)
414 return ret;
415
416 ret = i2c_smbus_write_byte_data(client, HACTIVE_LO,
417 cropping->hactive & 0x00FF);
418
419 return ret;
420}
421
422static int tw9910_set_hsync(struct i2c_client *client,
423 const struct tw9910_hsync_ctrl *hsync)
424{
425 int ret;
426
427 /* bit 10 - 3 */
428 ret = i2c_smbus_write_byte_data(client, HSGEGIN,
429 (hsync->start & 0x07F8) >> 3);
430 if (ret < 0)
431 return ret;
432
433 /* bit 10 - 3 */
434 ret = i2c_smbus_write_byte_data(client, HSEND,
435 (hsync->end & 0x07F8) >> 3);
436 if (ret < 0)
437 return ret;
438
439 /* bit 2 - 0 */
440 ret = i2c_smbus_read_byte_data(client, HSLOWCTL);
441 if (ret < 0)
442 return ret;
443
444 ret = i2c_smbus_write_byte_data(client, HSLOWCTL,
445 (ret & 0x88) |
446 (hsync->start & 0x0007) << 4 |
447 (hsync->end & 0x0007));
448
449 return ret;
450}
451
452static int tw9910_write_array(struct i2c_client *client,
453 const struct regval_list *vals)
454{
455 while (vals->reg_num != 0xff) {
456 int ret = i2c_smbus_write_byte_data(client,
457 vals->reg_num,
458 vals->value);
459 if (ret < 0)
460 return ret;
461 vals++;
462 }
463 return 0;
464}
465
466static int tw9910_mask_set(struct i2c_client *client, u8 command,
467 u8 mask, u8 set)
468{
469 s32 val = i2c_smbus_read_byte_data(client, command);
Kuninori Morimoto1af1b7a2009-02-23 12:12:58 -0300470 if (val < 0)
471 return val;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300472
473 val &= ~mask;
Kuninori Morimoto1af1b7a2009-02-23 12:12:58 -0300474 val |= set & mask;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300475
476 return i2c_smbus_write_byte_data(client, command, val);
477}
478
479static void tw9910_reset(struct i2c_client *client)
480{
481 i2c_smbus_write_byte_data(client, ACNTL1, SRESET);
482 msleep(1);
483}
484
485static const struct tw9910_scale_ctrl*
486tw9910_select_norm(struct soc_camera_device *icd, u32 width, u32 height)
487{
488 const struct tw9910_scale_ctrl *scale;
489 const struct tw9910_scale_ctrl *ret = NULL;
490 v4l2_std_id norm = icd->vdev->current_norm;
491 __u32 diff = 0xffffffff, tmp;
492 int size, i;
493
494 if (norm & V4L2_STD_NTSC) {
495 scale = tw9910_ntsc_scales;
496 size = ARRAY_SIZE(tw9910_ntsc_scales);
497 } else if (norm & V4L2_STD_PAL) {
498 scale = tw9910_pal_scales;
499 size = ARRAY_SIZE(tw9910_pal_scales);
500 } else {
501 return NULL;
502 }
503
504 for (i = 0; i < size; i++) {
505 tmp = abs(width - scale[i].width) +
506 abs(height - scale[i].height);
507 if (tmp < diff) {
508 diff = tmp;
509 ret = scale + i;
510 }
511 }
512
513 return ret;
514}
515
516/*
517 * soc_camera_ops function
518 */
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300519static int tw9910_s_stream(struct v4l2_subdev *sd, int enable)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300520{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300521 struct i2c_client *client = sd->priv;
522 struct tw9910_priv *priv = to_tw9910(client);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300523
Kuninori Morimoto06f292e2009-12-11 11:34:50 -0300524 u8 val;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300525
Kuninori Morimoto06f292e2009-12-11 11:34:50 -0300526 if (!enable) {
527 switch (priv->revision) {
528 case 0:
529 val = OEN_TRI_SEL_ALL_OFF_r0;
530 break;
531 case 1:
532 val = OEN_TRI_SEL_ALL_OFF_r1;
533 break;
534 default:
535 dev_err(&client->dev, "un-supported revision\n");
536 return -EINVAL;
537 }
538 } else {
539 val = OEN_TRI_SEL_ALL_ON;
540
541 if (!priv->scale) {
542 dev_err(&client->dev, "norm select error\n");
543 return -EPERM;
544 }
545
546 dev_dbg(&client->dev, "%s %dx%d\n",
547 priv->scale->name,
548 priv->scale->width,
549 priv->scale->height);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300550 }
551
Kuninori Morimoto06f292e2009-12-11 11:34:50 -0300552 return tw9910_mask_set(client, OPFORM, OEN_TRI_SEL_MASK, val);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300553}
554
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300555static int tw9910_set_bus_param(struct soc_camera_device *icd,
556 unsigned long flags)
557{
558 return 0;
559}
560
561static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
562{
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300563 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300564 struct tw9910_priv *priv = to_tw9910(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300565 struct soc_camera_link *icl = to_soc_camera_link(icd);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300566 unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
567 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
568 SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
569
570 return soc_camera_apply_sensor_flags(icl, flags);
571}
572
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300573static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300574{
575 int ret = -EINVAL;
576
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300577 if (norm & (V4L2_STD_NTSC | V4L2_STD_PAL))
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300578 ret = 0;
579
580 return ret;
581}
582
583static int tw9910_enum_input(struct soc_camera_device *icd,
584 struct v4l2_input *inp)
585{
586 inp->type = V4L2_INPUT_TYPE_TUNER;
587 inp->std = V4L2_STD_UNKNOWN;
588 strcpy(inp->name, "Video");
589
590 return 0;
591}
592
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300593static int tw9910_g_chip_ident(struct v4l2_subdev *sd,
594 struct v4l2_dbg_chip_ident *id)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300595{
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -0300596 struct i2c_client *client = sd->priv;
597 struct tw9910_priv *priv = to_tw9910(client);
598
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300599 id->ident = V4L2_IDENT_TW9910;
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -0300600 id->revision = priv->revision;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300601
602 return 0;
603}
604
605#ifdef CONFIG_VIDEO_ADV_DEBUG
606static int tw9910_g_register(struct v4l2_subdev *sd,
607 struct v4l2_dbg_register *reg)
608{
609 struct i2c_client *client = sd->priv;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300610 int ret;
611
612 if (reg->reg > 0xff)
613 return -EINVAL;
614
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300615 ret = i2c_smbus_read_byte_data(client, reg->reg);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300616 if (ret < 0)
617 return ret;
618
Guennadi Liakhovetski5d28d522009-12-11 11:15:05 -0300619 /*
620 * ret = int
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300621 * reg->val = __u64
622 */
623 reg->val = (__u64)ret;
624
625 return 0;
626}
627
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300628static int tw9910_s_register(struct v4l2_subdev *sd,
629 struct v4l2_dbg_register *reg)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300630{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300631 struct i2c_client *client = sd->priv;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300632
633 if (reg->reg > 0xff ||
634 reg->val > 0xff)
635 return -EINVAL;
636
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300637 return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300638}
639#endif
640
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300641static int tw9910_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300642{
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300643 struct v4l2_rect *rect = &a->c;
644 struct i2c_client *client = sd->priv;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300645 struct tw9910_priv *priv = to_tw9910(client);
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300646 struct soc_camera_device *icd = client->dev.platform_data;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300647 int ret = -EINVAL;
648 u8 val;
649
650 /*
651 * select suitable norm
652 */
653 priv->scale = tw9910_select_norm(icd, rect->width, rect->height);
654 if (!priv->scale)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300655 goto tw9910_set_fmt_error;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300656
657 /*
658 * reset hardware
659 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300660 tw9910_reset(client);
661 ret = tw9910_write_array(client, tw9910_default_regs);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300662 if (ret < 0)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300663 goto tw9910_set_fmt_error;
664
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300665 /*
666 * set bus width
667 */
668 val = 0x00;
669 if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
670 val = LEN;
671
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300672 ret = tw9910_mask_set(client, OPFORM, LEN, val);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300673 if (ret < 0)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300674 goto tw9910_set_fmt_error;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300675
676 /*
677 * select MPOUT behavior
678 */
679 switch (priv->info->mpout) {
680 case TW9910_MPO_VLOSS:
681 val = RTSEL_VLOSS; break;
682 case TW9910_MPO_HLOCK:
683 val = RTSEL_HLOCK; break;
684 case TW9910_MPO_SLOCK:
685 val = RTSEL_SLOCK; break;
686 case TW9910_MPO_VLOCK:
687 val = RTSEL_VLOCK; break;
688 case TW9910_MPO_MONO:
689 val = RTSEL_MONO; break;
690 case TW9910_MPO_DET50:
691 val = RTSEL_DET50; break;
692 case TW9910_MPO_FIELD:
693 val = RTSEL_FIELD; break;
694 case TW9910_MPO_RTCO:
695 val = RTSEL_RTCO; break;
696 default:
697 val = 0;
698 }
699
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300700 ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300701 if (ret < 0)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300702 goto tw9910_set_fmt_error;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300703
704 /*
705 * set scale
706 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300707 ret = tw9910_set_scale(client, priv->scale);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300708 if (ret < 0)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300709 goto tw9910_set_fmt_error;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300710
711 /*
712 * set cropping
713 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300714 ret = tw9910_set_cropping(client, &tw9910_cropping_ctrl);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300715 if (ret < 0)
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300716 goto tw9910_set_fmt_error;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300717
718 /*
719 * set hsync
720 */
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300721 ret = tw9910_set_hsync(client, &tw9910_hsync_ctrl);
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300722 if (ret < 0)
723 goto tw9910_set_fmt_error;
724
Guennadi Liakhovetski123ab622009-08-25 11:46:52 -0300725 rect->width = priv->scale->width;
726 rect->height = priv->scale->height;
727 rect->left = 0;
728 rect->top = 0;
729
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300730 return ret;
731
732tw9910_set_fmt_error:
733
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300734 tw9910_reset(client);
Kuninori Morimoto6d756112008-12-29 06:04:16 -0300735 priv->scale = NULL;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300736
737 return ret;
738}
739
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300740static int tw9910_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
741{
742 struct i2c_client *client = sd->priv;
743 struct tw9910_priv *priv = to_tw9910(client);
744
745 if (!priv->scale) {
746 int ret;
747 struct v4l2_crop crop = {
748 .c = {
749 .left = 0,
750 .top = 0,
751 .width = 640,
752 .height = 480,
753 },
754 };
755 ret = tw9910_s_crop(sd, &crop);
756 if (ret < 0)
757 return ret;
758 }
759
760 a->c.left = 0;
761 a->c.top = 0;
762 a->c.width = priv->scale->width;
763 a->c.height = priv->scale->height;
764 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
765
766 return 0;
767}
768
769static int tw9910_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
770{
771 a->bounds.left = 0;
772 a->bounds.top = 0;
773 a->bounds.width = 768;
774 a->bounds.height = 576;
775 a->defrect.left = 0;
776 a->defrect.top = 0;
777 a->defrect.width = 640;
778 a->defrect.height = 480;
779 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
780 a->pixelaspect.numerator = 1;
781 a->pixelaspect.denominator = 1;
782
783 return 0;
784}
785
786static int tw9910_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
787{
788 struct i2c_client *client = sd->priv;
789 struct tw9910_priv *priv = to_tw9910(client);
790 struct v4l2_pix_format *pix = &f->fmt.pix;
791
792 if (!priv->scale) {
793 int ret;
794 struct v4l2_crop crop = {
795 .c = {
796 .left = 0,
797 .top = 0,
798 .width = 640,
799 .height = 480,
800 },
801 };
802 ret = tw9910_s_crop(sd, &crop);
803 if (ret < 0)
804 return ret;
805 }
806
807 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
808
809 pix->width = priv->scale->width;
810 pix->height = priv->scale->height;
811 pix->pixelformat = V4L2_PIX_FMT_VYUY;
812 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
813 pix->field = V4L2_FIELD_INTERLACED;
814
815 return 0;
816}
817
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300818static int tw9910_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300819{
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300820 struct i2c_client *client = sd->priv;
821 struct tw9910_priv *priv = to_tw9910(client);
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300822 struct v4l2_pix_format *pix = &f->fmt.pix;
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300823 /* See tw9910_s_crop() - no proper cropping support */
824 struct v4l2_crop a = {
825 .c = {
826 .left = 0,
827 .top = 0,
828 .width = pix->width,
829 .height = pix->height,
830 },
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300831 };
Guennadi Liakhovetski123ab622009-08-25 11:46:52 -0300832 int i, ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300833
834 /*
835 * check color format
836 */
837 for (i = 0; i < ARRAY_SIZE(tw9910_color_fmt); i++)
838 if (pix->pixelformat == tw9910_color_fmt[i].fourcc)
839 break;
840
841 if (i == ARRAY_SIZE(tw9910_color_fmt))
842 return -EINVAL;
843
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300844 ret = tw9910_s_crop(sd, &a);
Guennadi Liakhovetski123ab622009-08-25 11:46:52 -0300845 if (!ret) {
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300846 pix->width = priv->scale->width;
847 pix->height = priv->scale->height;
Guennadi Liakhovetski123ab622009-08-25 11:46:52 -0300848 }
849 return ret;
Guennadi Liakhovetski09e231b2009-03-13 06:08:20 -0300850}
851
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300852static int tw9910_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300853{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300854 struct i2c_client *client = sd->priv;
855 struct soc_camera_device *icd = client->dev.platform_data;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300856 struct v4l2_pix_format *pix = &f->fmt.pix;
857 const struct tw9910_scale_ctrl *scale;
858
859 if (V4L2_FIELD_ANY == pix->field) {
860 pix->field = V4L2_FIELD_INTERLACED;
861 } else if (V4L2_FIELD_INTERLACED != pix->field) {
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300862 dev_err(&client->dev, "Field type invalid.\n");
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300863 return -EINVAL;
864 }
865
866 /*
867 * select suitable norm
868 */
869 scale = tw9910_select_norm(icd, pix->width, pix->height);
870 if (!scale)
871 return -EINVAL;
872
873 pix->width = scale->width;
874 pix->height = scale->height;
875
876 return 0;
877}
878
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300879static int tw9910_video_probe(struct soc_camera_device *icd,
880 struct i2c_client *client)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300881{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300882 struct tw9910_priv *priv = to_tw9910(client);
Kuninori Morimoto88a67c52009-12-11 11:34:50 -0300883 s32 id;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300884
885 /*
886 * We must have a parent by now. And it cannot be a wrong one.
887 * So this entire test is completely redundant.
888 */
889 if (!icd->dev.parent ||
890 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
891 return -ENODEV;
892
893 /*
894 * tw9910 only use 8 or 16 bit bus width
895 */
896 if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
897 SOCAM_DATAWIDTH_8 != priv->info->buswidth) {
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300898 dev_err(&client->dev, "bus width error\n");
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300899 return -ENODEV;
900 }
901
902 icd->formats = tw9910_color_fmt;
903 icd->num_formats = ARRAY_SIZE(tw9910_color_fmt);
904
905 /*
906 * check and show Product ID
Kuninori Morimoto4a4bc5e2009-12-11 11:06:00 -0300907 * So far only revisions 0 and 1 have been seen
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300908 */
Kuninori Morimoto88a67c52009-12-11 11:34:50 -0300909 id = i2c_smbus_read_byte_data(client, ID);
910 priv->revision = GET_REV(id);
911 id = GET_ID(id);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300912
Kuninori Morimoto88a67c52009-12-11 11:34:50 -0300913 if (0x0B != id ||
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -0300914 0x01 < priv->revision) {
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300915 dev_err(&client->dev,
Kuninori Morimoto6d74e5f2009-12-11 11:34:50 -0300916 "Product ID error %x:%x\n",
Kuninori Morimoto88a67c52009-12-11 11:34:50 -0300917 id, priv->revision);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300918 return -ENODEV;
919 }
920
Guennadi Liakhovetski85f8be62009-08-25 11:47:00 -0300921 dev_info(&client->dev,
Kuninori Morimoto88a67c52009-12-11 11:34:50 -0300922 "tw9910 Product ID %0x:%0x\n", id, priv->revision);
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300923
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300924 icd->vdev->tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL;
925 icd->vdev->current_norm = V4L2_STD_NTSC;
926
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300927 return 0;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300928}
929
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300930static struct soc_camera_ops tw9910_ops = {
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300931 .set_bus_param = tw9910_set_bus_param,
932 .query_bus_param = tw9910_query_bus_param,
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300933 .enum_input = tw9910_enum_input,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300934};
935
936static struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
937 .g_chip_ident = tw9910_g_chip_ident,
938 .s_std = tw9910_s_std,
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300939#ifdef CONFIG_VIDEO_ADV_DEBUG
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300940 .g_register = tw9910_g_register,
941 .s_register = tw9910_s_register,
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300942#endif
943};
944
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300945static struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
946 .s_stream = tw9910_s_stream,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300947 .g_fmt = tw9910_g_fmt,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300948 .s_fmt = tw9910_s_fmt,
949 .try_fmt = tw9910_try_fmt,
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300950 .cropcap = tw9910_cropcap,
951 .g_crop = tw9910_g_crop,
Guennadi Liakhovetski08590b92009-08-25 11:46:54 -0300952 .s_crop = tw9910_s_crop,
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300953};
954
955static struct v4l2_subdev_ops tw9910_subdev_ops = {
956 .core = &tw9910_subdev_core_ops,
957 .video = &tw9910_subdev_video_ops,
958};
959
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300960/*
961 * i2c_driver function
962 */
963
964static int tw9910_probe(struct i2c_client *client,
965 const struct i2c_device_id *did)
966
967{
968 struct tw9910_priv *priv;
969 struct tw9910_video_info *info;
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300970 struct soc_camera_device *icd = client->dev.platform_data;
971 struct i2c_adapter *adapter =
972 to_i2c_adapter(client->dev.parent);
973 struct soc_camera_link *icl;
Guennadi Liakhovetski6a6c8782009-08-25 11:50:46 -0300974 int ret;
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300975
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300976 if (!icd) {
977 dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
978 return -EINVAL;
979 }
980
981 icl = to_soc_camera_link(icd);
Guennadi Liakhovetski0f448292009-12-11 11:31:35 -0300982 if (!icl || !icl->priv)
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300983 return -EINVAL;
984
Guennadi Liakhovetski0f448292009-12-11 11:31:35 -0300985 info = icl->priv;
Guennadi Liakhovetski0a861e92009-05-12 15:13:32 +0000986
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -0300987 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Kuninori Morimotoed922a82008-12-29 06:04:06 -0300988 dev_err(&client->dev,
989 "I2C-Adapter doesn't support "
990 "I2C_FUNC_SMBUS_BYTE_DATA\n");
991 return -EIO;
992 }
993
994 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
995 if (!priv)
996 return -ENOMEM;
997
998 priv->info = info;
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -0300999
1000 v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001001
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001002 icd->ops = &tw9910_ops;
Guennadi Liakhovetski0f448292009-12-11 11:31:35 -03001003 icd->iface = icl->bus_id;
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001004
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001005 ret = tw9910_video_probe(icd, client);
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001006 if (ret) {
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001007 icd->ops = NULL;
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001008 i2c_set_clientdata(client, NULL);
1009 kfree(priv);
1010 }
1011
1012 return ret;
1013}
1014
1015static int tw9910_remove(struct i2c_client *client)
1016{
Guennadi Liakhovetski979ea1d2009-08-25 11:43:33 -03001017 struct tw9910_priv *priv = to_tw9910(client);
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001018 struct soc_camera_device *icd = client->dev.platform_data;
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001019
Guennadi Liakhovetski40e2e092009-08-25 11:28:22 -03001020 icd->ops = NULL;
Kuninori Morimotoed922a82008-12-29 06:04:06 -03001021 i2c_set_clientdata(client, NULL);
1022 kfree(priv);
1023 return 0;
1024}
1025
1026static const struct i2c_device_id tw9910_id[] = {
1027 { "tw9910", 0 },
1028 { }
1029};
1030MODULE_DEVICE_TABLE(i2c, tw9910_id);
1031
1032static struct i2c_driver tw9910_i2c_driver = {
1033 .driver = {
1034 .name = "tw9910",
1035 },
1036 .probe = tw9910_probe,
1037 .remove = tw9910_remove,
1038 .id_table = tw9910_id,
1039};
1040
1041/*
1042 * module function
1043 */
1044static int __init tw9910_module_init(void)
1045{
1046 return i2c_add_driver(&tw9910_i2c_driver);
1047}
1048
1049static void __exit tw9910_module_exit(void)
1050{
1051 i2c_del_driver(&tw9910_i2c_driver);
1052}
1053
1054module_init(tw9910_module_init);
1055module_exit(tw9910_module_exit);
1056
1057MODULE_DESCRIPTION("SoC Camera driver for tw9910");
1058MODULE_AUTHOR("Kuninori Morimoto");
1059MODULE_LICENSE("GPL v2");