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/version.h>
21 #include <linux/i2c.h>
22 #include <linux/videodev2.h>
23 #include <linux/ioctl.h>
36 static u8 global_registers[] =
45 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
48 static u8 channel_registers[] =
105 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
108 static int write_reg(struct i2c_client *client, u8 reg, u8 value, int channel)
110 return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
113 static int write_regs(struct i2c_client *client, u8 *regs, int channel)
117 for (i = 0; regs[i] != 0xff; i += 2)
118 if (i2c_smbus_write_byte_data(client,
119 regs[i] | (channel << 6), regs[i + 1]) < 0)
124 static int wis_tw2804_command(struct i2c_client *client,
125 unsigned int cmd, void *arg)
127 struct wis_tw2804 *dec = i2c_get_clientdata(client);
129 if (cmd == DECODER_SET_CHANNEL) {
132 if (*input < 0 || *input > 3) {
133 printk(KERN_ERR "wis-tw2804: channel %d is not "
134 "between 0 and 3!\n", *input);
137 dec->channel = *input;
138 printk(KERN_DEBUG "wis-tw2804: initializing TW2804 "
139 "channel %d\n", dec->channel);
140 if (dec->channel == 0 &&
141 write_regs(client, global_registers, 0) < 0) {
142 printk(KERN_ERR "wis-tw2804: error initializing "
143 "TW2804 global registers\n");
146 if (write_regs(client, channel_registers, dec->channel) < 0) {
147 printk(KERN_ERR "wis-tw2804: error initializing "
148 "TW2804 channel %d\n", dec->channel);
154 if (dec->channel < 0) {
155 printk(KERN_DEBUG "wis-tw2804: ignoring command %08x until "
156 "channel number is set\n", cmd);
163 v4l2_std_id *input = arg;
165 0x01, *input & V4L2_STD_NTSC ? 0xc4 : 0x84,
166 0x09, *input & V4L2_STD_NTSC ? 0x07 : 0x04,
167 0x0a, *input & V4L2_STD_NTSC ? 0xf0 : 0x20,
168 0x0b, *input & V4L2_STD_NTSC ? 0x07 : 0x04,
169 0x0c, *input & V4L2_STD_NTSC ? 0xf0 : 0x20,
170 0x0d, *input & V4L2_STD_NTSC ? 0x40 : 0x4a,
171 0x16, *input & V4L2_STD_NTSC ? 0x00 : 0x40,
172 0x17, *input & V4L2_STD_NTSC ? 0x00 : 0x40,
173 0x20, *input & V4L2_STD_NTSC ? 0x07 : 0x0f,
174 0x21, *input & V4L2_STD_NTSC ? 0x07 : 0x0f,
177 write_regs(client, regs, dec->channel);
181 case VIDIOC_QUERYCTRL:
183 struct v4l2_queryctrl *ctrl = arg;
186 case V4L2_CID_BRIGHTNESS:
187 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
188 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
192 ctrl->default_value = 128;
195 case V4L2_CID_CONTRAST:
196 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
197 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
201 ctrl->default_value = 128;
204 case V4L2_CID_SATURATION:
205 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
206 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
210 ctrl->default_value = 128;
214 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
215 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
219 ctrl->default_value = 128;
227 struct v4l2_control *ctrl = arg;
230 case V4L2_CID_BRIGHTNESS:
231 if (ctrl->value > 255)
232 dec->brightness = 255;
233 else if (ctrl->value < 0)
236 dec->brightness = ctrl->value;
237 write_reg(client, 0x12, dec->brightness, dec->channel);
239 case V4L2_CID_CONTRAST:
240 if (ctrl->value > 255)
242 else if (ctrl->value < 0)
245 dec->contrast = ctrl->value;
246 write_reg(client, 0x11, dec->contrast, dec->channel);
248 case V4L2_CID_SATURATION:
249 if (ctrl->value > 255)
250 dec->saturation = 255;
251 else if (ctrl->value < 0)
254 dec->saturation = ctrl->value;
255 write_reg(client, 0x10, dec->saturation, dec->channel);
258 if (ctrl->value > 255)
260 else if (ctrl->value < 0)
263 dec->hue = ctrl->value;
264 write_reg(client, 0x0f, dec->hue, dec->channel);
271 struct v4l2_control *ctrl = arg;
274 case V4L2_CID_BRIGHTNESS:
275 ctrl->value = dec->brightness;
277 case V4L2_CID_CONTRAST:
278 ctrl->value = dec->contrast;
280 case V4L2_CID_SATURATION:
281 ctrl->value = dec->saturation;
284 ctrl->value = dec->hue;
295 static struct i2c_driver wis_tw2804_driver;
297 static struct i2c_client wis_tw2804_client_templ = {
298 .name = "TW2804 (WIS)",
299 .driver = &wis_tw2804_driver,
302 static int wis_tw2804_detect(struct i2c_adapter *adapter, int addr, int kind)
304 struct i2c_client *client;
305 struct wis_tw2804 *dec;
307 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
310 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
313 memcpy(client, &wis_tw2804_client_templ,
314 sizeof(wis_tw2804_client_templ));
315 client->adapter = adapter;
318 dec = kmalloc(sizeof(struct wis_tw2804), GFP_KERNEL);
324 dec->norm = V4L2_STD_NTSC;
325 dec->brightness = 128;
327 dec->saturation = 128;
329 i2c_set_clientdata(client, dec);
331 printk(KERN_DEBUG "wis-tw2804: creating TW2804 at address %d on %s\n",
332 addr, adapter->name);
334 i2c_attach_client(client);
338 static int wis_tw2804_detach(struct i2c_client *client)
340 struct wis_tw2804 *dec = i2c_get_clientdata(client);
343 r = i2c_detach_client(client);
352 static struct i2c_driver wis_tw2804_driver = {
354 .name = "WIS TW2804 I2C driver",
356 .id = I2C_DRIVERID_WIS_TW2804,
357 .detach_client = wis_tw2804_detach,
358 .command = wis_tw2804_command,
361 static int __init wis_tw2804_init(void)
365 r = i2c_add_driver(&wis_tw2804_driver);
368 return wis_i2c_add_driver(wis_tw2804_driver.id, wis_tw2804_detect);
371 static void __exit wis_tw2804_cleanup(void)
373 wis_i2c_del_driver(wis_tw2804_detect);
374 i2c_del_driver(&wis_tw2804_driver);
377 module_init(wis_tw2804_init);
378 module_exit(wis_tw2804_cleanup);
380 MODULE_LICENSE("GPL v2");