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>
35 static u8 global_registers
[] =
44 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
47 static u8 channel_registers
[] =
104 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
107 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
, int channel
)
109 return i2c_smbus_write_byte_data(client
, reg
| (channel
<< 6), value
);
112 static int write_regs(struct i2c_client
*client
, u8
*regs
, int channel
)
116 for (i
= 0; regs
[i
] != 0xff; i
+= 2)
117 if (i2c_smbus_write_byte_data(client
,
118 regs
[i
] | (channel
<< 6), regs
[i
+ 1]) < 0)
123 static int wis_tw2804_command(struct i2c_client
*client
,
124 unsigned int cmd
, void *arg
)
126 struct wis_tw2804
*dec
= i2c_get_clientdata(client
);
128 if (cmd
== DECODER_SET_CHANNEL
) {
131 if (*input
< 0 || *input
> 3) {
132 printk(KERN_ERR
"wis-tw2804: channel %d is not "
133 "between 0 and 3!\n", *input
);
136 dec
->channel
= *input
;
137 printk(KERN_DEBUG
"wis-tw2804: initializing TW2804 "
138 "channel %d\n", dec
->channel
);
139 if (dec
->channel
== 0 &&
140 write_regs(client
, global_registers
, 0) < 0) {
141 printk(KERN_ERR
"wis-tw2804: error initializing "
142 "TW2804 global registers\n");
145 if (write_regs(client
, channel_registers
, dec
->channel
) < 0) {
146 printk(KERN_ERR
"wis-tw2804: error initializing "
147 "TW2804 channel %d\n", dec
->channel
);
153 if (dec
->channel
< 0) {
154 printk(KERN_DEBUG
"wis-tw2804: ignoring command %08x until "
155 "channel number is set\n", cmd
);
162 v4l2_std_id
*input
= arg
;
164 0x01, *input
& V4L2_STD_NTSC
? 0xc4 : 0x84,
165 0x09, *input
& V4L2_STD_NTSC
? 0x07 : 0x04,
166 0x0a, *input
& V4L2_STD_NTSC
? 0xf0 : 0x20,
167 0x0b, *input
& V4L2_STD_NTSC
? 0x07 : 0x04,
168 0x0c, *input
& V4L2_STD_NTSC
? 0xf0 : 0x20,
169 0x0d, *input
& V4L2_STD_NTSC
? 0x40 : 0x4a,
170 0x16, *input
& V4L2_STD_NTSC
? 0x00 : 0x40,
171 0x17, *input
& V4L2_STD_NTSC
? 0x00 : 0x40,
172 0x20, *input
& V4L2_STD_NTSC
? 0x07 : 0x0f,
173 0x21, *input
& V4L2_STD_NTSC
? 0x07 : 0x0f,
176 write_regs(client
, regs
, dec
->channel
);
180 case VIDIOC_QUERYCTRL
:
182 struct v4l2_queryctrl
*ctrl
= arg
;
185 case V4L2_CID_BRIGHTNESS
:
186 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
187 strncpy(ctrl
->name
, "Brightness", sizeof(ctrl
->name
));
191 ctrl
->default_value
= 128;
194 case V4L2_CID_CONTRAST
:
195 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
196 strncpy(ctrl
->name
, "Contrast", sizeof(ctrl
->name
));
200 ctrl
->default_value
= 128;
203 case V4L2_CID_SATURATION
:
204 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
205 strncpy(ctrl
->name
, "Saturation", sizeof(ctrl
->name
));
209 ctrl
->default_value
= 128;
213 ctrl
->type
= V4L2_CTRL_TYPE_INTEGER
;
214 strncpy(ctrl
->name
, "Hue", sizeof(ctrl
->name
));
218 ctrl
->default_value
= 128;
226 struct v4l2_control
*ctrl
= arg
;
229 case V4L2_CID_BRIGHTNESS
:
230 if (ctrl
->value
> 255)
231 dec
->brightness
= 255;
232 else if (ctrl
->value
< 0)
235 dec
->brightness
= ctrl
->value
;
236 write_reg(client
, 0x12, dec
->brightness
, dec
->channel
);
238 case V4L2_CID_CONTRAST
:
239 if (ctrl
->value
> 255)
241 else if (ctrl
->value
< 0)
244 dec
->contrast
= ctrl
->value
;
245 write_reg(client
, 0x11, dec
->contrast
, dec
->channel
);
247 case V4L2_CID_SATURATION
:
248 if (ctrl
->value
> 255)
249 dec
->saturation
= 255;
250 else if (ctrl
->value
< 0)
253 dec
->saturation
= ctrl
->value
;
254 write_reg(client
, 0x10, dec
->saturation
, dec
->channel
);
257 if (ctrl
->value
> 255)
259 else if (ctrl
->value
< 0)
262 dec
->hue
= ctrl
->value
;
263 write_reg(client
, 0x0f, dec
->hue
, dec
->channel
);
270 struct v4l2_control
*ctrl
= arg
;
273 case V4L2_CID_BRIGHTNESS
:
274 ctrl
->value
= dec
->brightness
;
276 case V4L2_CID_CONTRAST
:
277 ctrl
->value
= dec
->contrast
;
279 case V4L2_CID_SATURATION
:
280 ctrl
->value
= dec
->saturation
;
283 ctrl
->value
= dec
->hue
;
294 static int wis_tw2804_probe(struct i2c_client
*client
,
295 const struct i2c_device_id
*id
)
297 struct i2c_adapter
*adapter
= client
->adapter
;
298 struct wis_tw2804
*dec
;
300 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
303 dec
= kmalloc(sizeof(struct wis_tw2804
), GFP_KERNEL
);
308 dec
->norm
= V4L2_STD_NTSC
;
309 dec
->brightness
= 128;
311 dec
->saturation
= 128;
313 i2c_set_clientdata(client
, dec
);
315 printk(KERN_DEBUG
"wis-tw2804: creating TW2804 at address %d on %s\n",
316 client
->addr
, adapter
->name
);
321 static int wis_tw2804_remove(struct i2c_client
*client
)
323 struct wis_tw2804
*dec
= i2c_get_clientdata(client
);
325 i2c_set_clientdata(client
, NULL
);
330 static struct i2c_device_id wis_tw2804_id
[] = {
335 static struct i2c_driver wis_tw2804_driver
= {
337 .name
= "WIS TW2804 I2C driver",
339 .probe
= wis_tw2804_probe
,
340 .remove
= wis_tw2804_remove
,
341 .command
= wis_tw2804_command
,
342 .id_table
= wis_tw2804_id
,
345 static int __init
wis_tw2804_init(void)
347 return i2c_add_driver(&wis_tw2804_driver
);
350 static void __exit
wis_tw2804_cleanup(void)
352 i2c_del_driver(&wis_tw2804_driver
);
355 module_init(wis_tw2804_init
);
356 module_exit(wis_tw2804_cleanup
);
358 MODULE_LICENSE("GPL v2");