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>
36 static u8 global_registers
[] = {
44 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
47 static u8 channel_registers
[] = {
103 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
106 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
, int channel
)
108 return i2c_smbus_write_byte_data(client
, reg
| (channel
<< 6), value
);
111 static int write_regs(struct i2c_client
*client
, u8
*regs
, int channel
)
115 for (i
= 0; regs
[i
] != 0xff; i
+= 2)
116 if (i2c_smbus_write_byte_data(client
,
117 regs
[i
] | (channel
<< 6), regs
[i
+ 1]) < 0)
122 static int wis_tw2804_command(struct i2c_client
*client
,
123 unsigned int cmd
, void *arg
)
125 struct wis_tw2804
*dec
= i2c_get_clientdata(client
);
127 if (cmd
== DECODER_SET_CHANNEL
) {
130 if (*input
< 0 || *input
> 3) {
131 printk(KERN_ERR
"wis-tw2804: channel %d is not "
132 "between 0 and 3!\n", *input
);
135 dec
->channel
= *input
;
136 printk(KERN_DEBUG
"wis-tw2804: initializing TW2804 "
137 "channel %d\n", dec
->channel
);
138 if (dec
->channel
== 0 &&
139 write_regs(client
, global_registers
, 0) < 0) {
140 printk(KERN_ERR
"wis-tw2804: error initializing "
141 "TW2804 global registers\n");
144 if (write_regs(client
, channel_registers
, dec
->channel
) < 0) {
145 printk(KERN_ERR
"wis-tw2804: error initializing "
146 "TW2804 channel %d\n", dec
->channel
);
152 if (dec
->channel
< 0) {
153 printk(KERN_DEBUG
"wis-tw2804: ignoring command %08x until "
154 "channel number is set\n", cmd
);
161 v4l2_std_id
*input
= arg
;
163 0x01, *input
& V4L2_STD_NTSC
? 0xc4 : 0x84,
164 0x09, *input
& V4L2_STD_NTSC
? 0x07 : 0x04,
165 0x0a, *input
& V4L2_STD_NTSC
? 0xf0 : 0x20,
166 0x0b, *input
& V4L2_STD_NTSC
? 0x07 : 0x04,
167 0x0c, *input
& V4L2_STD_NTSC
? 0xf0 : 0x20,
168 0x0d, *input
& V4L2_STD_NTSC
? 0x40 : 0x4a,
169 0x16, *input
& V4L2_STD_NTSC
? 0x00 : 0x40,
170 0x17, *input
& V4L2_STD_NTSC
? 0x00 : 0x40,
171 0x20, *input
& V4L2_STD_NTSC
? 0x07 : 0x0f,
172 0x21, *input
& V4L2_STD_NTSC
? 0x07 : 0x0f,
175 write_regs(client
, regs
, dec
->channel
);
179 case VIDIOC_QUERYCTRL
:
181 struct v4l2_queryctrl
*ctrl
= arg
;
184 case V4L2_CID_BRIGHTNESS
:
185 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
186 strncpy(ctrl
->name
, "Brightness", sizeof(ctrl
->name
));
190 ctrl
->default_value
= 128;
193 case V4L2_CID_CONTRAST
:
194 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
195 strncpy(ctrl
->name
, "Contrast", sizeof(ctrl
->name
));
199 ctrl
->default_value
= 128;
202 case V4L2_CID_SATURATION
:
203 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
204 strncpy(ctrl
->name
, "Saturation", sizeof(ctrl
->name
));
208 ctrl
->default_value
= 128;
212 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
213 strncpy(ctrl
->name
, "Hue", sizeof(ctrl
->name
));
217 ctrl
->default_value
= 128;
225 struct v4l2_control
*ctrl
= arg
;
228 case V4L2_CID_BRIGHTNESS
:
229 if (ctrl
->value
> 255)
230 dec
->brightness
= 255;
231 else if (ctrl
->value
< 0)
234 dec
->brightness
= ctrl
->value
;
235 write_reg(client
, 0x12, dec
->brightness
, dec
->channel
);
237 case V4L2_CID_CONTRAST
:
238 if (ctrl
->value
> 255)
240 else if (ctrl
->value
< 0)
243 dec
->contrast
= ctrl
->value
;
244 write_reg(client
, 0x11, dec
->contrast
, dec
->channel
);
246 case V4L2_CID_SATURATION
:
247 if (ctrl
->value
> 255)
248 dec
->saturation
= 255;
249 else if (ctrl
->value
< 0)
252 dec
->saturation
= ctrl
->value
;
253 write_reg(client
, 0x10, dec
->saturation
, dec
->channel
);
256 if (ctrl
->value
> 255)
258 else if (ctrl
->value
< 0)
261 dec
->hue
= ctrl
->value
;
262 write_reg(client
, 0x0f, dec
->hue
, dec
->channel
);
269 struct v4l2_control
*ctrl
= arg
;
272 case V4L2_CID_BRIGHTNESS
:
273 ctrl
->value
= dec
->brightness
;
275 case V4L2_CID_CONTRAST
:
276 ctrl
->value
= dec
->contrast
;
278 case V4L2_CID_SATURATION
:
279 ctrl
->value
= dec
->saturation
;
282 ctrl
->value
= dec
->hue
;
293 static int wis_tw2804_probe(struct i2c_client
*client
,
294 const struct i2c_device_id
*id
)
296 struct i2c_adapter
*adapter
= client
->adapter
;
297 struct wis_tw2804
*dec
;
299 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
302 dec
= kmalloc(sizeof(struct wis_tw2804
), GFP_KERNEL
);
307 dec
->norm
= V4L2_STD_NTSC
;
308 dec
->brightness
= 128;
310 dec
->saturation
= 128;
312 i2c_set_clientdata(client
, dec
);
314 printk(KERN_DEBUG
"wis-tw2804: creating TW2804 at address %d on %s\n",
315 client
->addr
, adapter
->name
);
320 static int wis_tw2804_remove(struct i2c_client
*client
)
322 struct wis_tw2804
*dec
= i2c_get_clientdata(client
);
328 static const struct i2c_device_id wis_tw2804_id
[] = {
332 MODULE_DEVICE_TABLE(i2c
, wis_tw2804_id
);
334 static struct i2c_driver wis_tw2804_driver
= {
336 .name
= "WIS TW2804 I2C driver",
338 .probe
= wis_tw2804_probe
,
339 .remove
= wis_tw2804_remove
,
340 .command
= wis_tw2804_command
,
341 .id_table
= wis_tw2804_id
,
344 static int __init
wis_tw2804_init(void)
346 return i2c_add_driver(&wis_tw2804_driver
);
349 static void __exit
wis_tw2804_cleanup(void)
351 i2c_del_driver(&wis_tw2804_driver
);
354 module_init(wis_tw2804_init
);
355 module_exit(wis_tw2804_cleanup
);
357 MODULE_LICENSE("GPL v2");