2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/videodev2.h>
22 #include <linux/ioctl.h>
23 #include <linux/slab.h>
34 static u8 initial_registers
[] =
36 0x02, 0x44, /* input 1, composite */
37 0x03, 0x92, /* correct digital format */
39 0x05, 0x80, /* or 0x00 for PAL */
40 0x06, 0x40, /* second internal current reference */
41 0x07, 0x02, /* window */
42 0x08, 0x14, /* window */
43 0x09, 0xf0, /* window */
44 0x0a, 0x81, /* window */
45 0x0b, 0xd0, /* window */
47 0x0d, 0x00, /* scaling */
48 0x0e, 0x11, /* scaling */
49 0x0f, 0x00, /* scaling */
50 0x10, 0x00, /* brightness */
51 0x11, 0x60, /* contrast */
52 0x12, 0x01, /* sharpness */
53 0x13, 0x7f, /* U gain */
54 0x14, 0x5a, /* V gain */
56 0x16, 0xc3, /* sharpness */
60 0x1c, 0x0f, /* video norm */
61 0x1d, 0x7f, /* video norm */
62 0x20, 0xa0, /* clamping gain (working 0x50) */
76 0x2e, 0xa5, /* burst PLL control (working: a9) */
77 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
83 0x06, 0xc0, /* reset device */
84 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
87 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
)
89 return i2c_smbus_write_byte_data(client
, reg
, value
);
92 static int write_regs(struct i2c_client
*client
, u8
*regs
)
96 for (i
= 0; regs
[i
] != 0x00; i
+= 2)
97 if (i2c_smbus_write_byte_data(client
, regs
[i
], regs
[i
+ 1]) < 0)
102 static int wis_tw9903_command(struct i2c_client
*client
,
103 unsigned int cmd
, void *arg
)
105 struct wis_tw9903
*dec
= i2c_get_clientdata(client
);
112 i2c_smbus_write_byte_data(client
, 0x02, 0x40 | (*input
<< 1));
116 /* The scaler on this thing seems to be horribly broken */
117 case DECODER_SET_RESOLUTION
:
119 struct video_decoder_resolution
*res
= arg
;
120 /*int hscale = 256 * 720 / res->width;*/
121 int hscale
= 256 * 720 / (res
->width
- (res
->width
> 704 ? 0 : 8));
122 int vscale
= 256 * (dec
->norm
& V4L2_STD_NTSC
? 240 : 288)
127 0x0e, ((vscale
& 0xf00) >> 4) | ((hscale
& 0xf00) >> 8),
128 0x06, 0xc0, /* reset device */
131 printk(KERN_DEBUG
"vscale is %04x, hscale is %04x\n",
133 /*write_regs(client, regs);*/
139 v4l2_std_id
*input
= arg
;
141 0x05, *input
& V4L2_STD_NTSC
? 0x80 : 0x00,
142 0x07, *input
& V4L2_STD_NTSC
? 0x02 : 0x12,
143 0x08, *input
& V4L2_STD_NTSC
? 0x14 : 0x18,
144 0x09, *input
& V4L2_STD_NTSC
? 0xf0 : 0x20,
147 write_regs(client
, regs
);
151 case VIDIOC_QUERYCTRL
:
153 struct v4l2_queryctrl
*ctrl
= arg
;
156 case V4L2_CID_BRIGHTNESS
:
157 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
158 strncpy(ctrl
->name
, "Brightness", sizeof(ctrl
->name
));
159 ctrl
->minimum
= -128;
162 ctrl
->default_value
= 0x00;
165 case V4L2_CID_CONTRAST
:
166 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
167 strncpy(ctrl
->name
, "Contrast", sizeof(ctrl
->name
));
171 ctrl
->default_value
= 0x60;
175 /* I don't understand how the Chroma Gain registers work... */
176 case V4L2_CID_SATURATION
:
177 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
178 strncpy(ctrl
->name
, "Saturation", sizeof(ctrl
->name
));
182 ctrl
->default_value
= 64;
187 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
188 strncpy(ctrl
->name
, "Hue", sizeof(ctrl
->name
));
189 ctrl
->minimum
= -128;
192 ctrl
->default_value
= 0;
200 struct v4l2_control
*ctrl
= arg
;
203 case V4L2_CID_BRIGHTNESS
:
204 if (ctrl
->value
> 127)
205 dec
->brightness
= 127;
206 else if (ctrl
->value
< -128)
207 dec
->brightness
= -128;
209 dec
->brightness
= ctrl
->value
;
210 write_reg(client
, 0x10, dec
->brightness
);
212 case V4L2_CID_CONTRAST
:
213 if (ctrl
->value
> 255)
215 else if (ctrl
->value
< 0)
218 dec
->contrast
= ctrl
->value
;
219 write_reg(client
, 0x11, dec
->contrast
);
222 case V4L2_CID_SATURATION
:
223 if (ctrl
->value
> 127)
224 dec
->saturation
= 127;
225 else if (ctrl
->value
< 0)
228 dec
->saturation
= ctrl
->value
;
229 /*write_reg(client, 0x0c, dec->saturation);*/
233 if (ctrl
->value
> 127)
235 else if (ctrl
->value
< -128)
238 dec
->hue
= ctrl
->value
;
239 write_reg(client
, 0x15, dec
->hue
);
246 struct v4l2_control
*ctrl
= arg
;
249 case V4L2_CID_BRIGHTNESS
:
250 ctrl
->value
= dec
->brightness
;
252 case V4L2_CID_CONTRAST
:
253 ctrl
->value
= dec
->contrast
;
256 case V4L2_CID_SATURATION
:
257 ctrl
->value
= dec
->saturation
;
261 ctrl
->value
= dec
->hue
;
272 static int wis_tw9903_probe(struct i2c_client
*client
,
273 const struct i2c_device_id
*id
)
275 struct i2c_adapter
*adapter
= client
->adapter
;
276 struct wis_tw9903
*dec
;
278 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
281 dec
= kmalloc(sizeof(struct wis_tw9903
), GFP_KERNEL
);
285 dec
->norm
= V4L2_STD_NTSC
;
287 dec
->contrast
= 0x60;
289 i2c_set_clientdata(client
, dec
);
292 "wis-tw9903: initializing TW9903 at address %d on %s\n",
293 client
->addr
, adapter
->name
);
295 if (write_regs(client
, initial_registers
) < 0) {
296 printk(KERN_ERR
"wis-tw9903: error initializing TW9903\n");
304 static int wis_tw9903_remove(struct i2c_client
*client
)
306 struct wis_tw9903
*dec
= i2c_get_clientdata(client
);
312 static const struct i2c_device_id wis_tw9903_id
[] = {
316 MODULE_DEVICE_TABLE(i2c
, wis_tw9903_id
);
318 static struct i2c_driver wis_tw9903_driver
= {
320 .name
= "WIS TW9903 I2C driver",
322 .probe
= wis_tw9903_probe
,
323 .remove
= wis_tw9903_remove
,
324 .command
= wis_tw9903_command
,
325 .id_table
= wis_tw9903_id
,
328 static int __init
wis_tw9903_init(void)
330 return i2c_add_driver(&wis_tw9903_driver
);
333 static void __exit
wis_tw9903_cleanup(void)
335 i2c_del_driver(&wis_tw9903_driver
);
338 module_init(wis_tw9903_init
);
339 module_exit(wis_tw9903_cleanup
);
341 MODULE_LICENSE("GPL v2");