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/videodev.h>
23 #include <linux/video_decoder.h>
24 #include <linux/ioctl.h>
37 static u8 global_registers[] =
46 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
49 static u8 channel_registers[] =
106 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
109 static int write_reg(struct i2c_client *client, u8 reg, u8 value, int channel)
111 return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
114 static int write_regs(struct i2c_client *client, u8 *regs, int channel)
118 for (i = 0; regs[i] != 0xff; i += 2)
119 if (i2c_smbus_write_byte_data(client,
120 regs[i] | (channel << 6), regs[i + 1]) < 0)
125 static int wis_tw2804_command(struct i2c_client *client,
126 unsigned int cmd, void *arg)
128 struct wis_tw2804 *dec = i2c_get_clientdata(client);
130 if (cmd == DECODER_SET_CHANNEL) {
133 if (*input < 0 || *input > 3) {
134 printk(KERN_ERR "wis-tw2804: channel %d is not "
135 "between 0 and 3!\n", *input);
138 dec->channel = *input;
139 printk(KERN_DEBUG "wis-tw2804: initializing TW2804 "
140 "channel %d\n", dec->channel);
141 if (dec->channel == 0 &&
142 write_regs(client, global_registers, 0) < 0) {
143 printk(KERN_ERR "wis-tw2804: error initializing "
144 "TW2804 global registers\n");
147 if (write_regs(client, channel_registers, dec->channel) < 0) {
148 printk(KERN_ERR "wis-tw2804: error initializing "
149 "TW2804 channel %d\n", dec->channel);
155 if (dec->channel < 0) {
156 printk(KERN_DEBUG "wis-tw2804: ignoring command %08x until "
157 "channel number is set\n", cmd);
162 case DECODER_SET_NORM:
166 0x01, *input == VIDEO_MODE_NTSC ? 0xc4 : 0x84,
167 0x09, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
168 0x0a, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
169 0x0b, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
170 0x0c, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
171 0x0d, *input == VIDEO_MODE_NTSC ? 0x40 : 0x4a,
172 0x16, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
173 0x17, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
174 0x20, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
175 0x21, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
178 write_regs(client, regs, dec->channel);
182 case VIDIOC_QUERYCTRL:
184 struct v4l2_queryctrl *ctrl = arg;
187 case V4L2_CID_BRIGHTNESS:
188 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
189 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
193 ctrl->default_value = 128;
196 case V4L2_CID_CONTRAST:
197 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
198 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
202 ctrl->default_value = 128;
205 case V4L2_CID_SATURATION:
206 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
207 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
211 ctrl->default_value = 128;
215 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
216 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
220 ctrl->default_value = 128;
228 struct v4l2_control *ctrl = arg;
231 case V4L2_CID_BRIGHTNESS:
232 if (ctrl->value > 255)
233 dec->brightness = 255;
234 else if (ctrl->value < 0)
237 dec->brightness = ctrl->value;
238 write_reg(client, 0x12, dec->brightness, dec->channel);
240 case V4L2_CID_CONTRAST:
241 if (ctrl->value > 255)
243 else if (ctrl->value < 0)
246 dec->contrast = ctrl->value;
247 write_reg(client, 0x11, dec->contrast, dec->channel);
249 case V4L2_CID_SATURATION:
250 if (ctrl->value > 255)
251 dec->saturation = 255;
252 else if (ctrl->value < 0)
255 dec->saturation = ctrl->value;
256 write_reg(client, 0x10, dec->saturation, dec->channel);
259 if (ctrl->value > 255)
261 else if (ctrl->value < 0)
264 dec->hue = ctrl->value;
265 write_reg(client, 0x0f, dec->hue, dec->channel);
272 struct v4l2_control *ctrl = arg;
275 case V4L2_CID_BRIGHTNESS:
276 ctrl->value = dec->brightness;
278 case V4L2_CID_CONTRAST:
279 ctrl->value = dec->contrast;
281 case V4L2_CID_SATURATION:
282 ctrl->value = dec->saturation;
285 ctrl->value = dec->hue;
296 static struct i2c_driver wis_tw2804_driver;
298 static struct i2c_client wis_tw2804_client_templ = {
299 .name = "TW2804 (WIS)",
300 .driver = &wis_tw2804_driver,
303 static int wis_tw2804_detect(struct i2c_adapter *adapter, int addr, int kind)
305 struct i2c_client *client;
306 struct wis_tw2804 *dec;
308 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
311 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
314 memcpy(client, &wis_tw2804_client_templ,
315 sizeof(wis_tw2804_client_templ));
316 client->adapter = adapter;
319 dec = kmalloc(sizeof(struct wis_tw2804), GFP_KERNEL);
325 dec->norm = VIDEO_MODE_NTSC;
326 dec->brightness = 128;
328 dec->saturation = 128;
330 i2c_set_clientdata(client, dec);
332 printk(KERN_DEBUG "wis-tw2804: creating TW2804 at address %d on %s\n",
333 addr, adapter->name);
335 i2c_attach_client(client);
339 static int wis_tw2804_detach(struct i2c_client *client)
341 struct wis_tw2804 *dec = i2c_get_clientdata(client);
344 r = i2c_detach_client(client);
353 static struct i2c_driver wis_tw2804_driver = {
355 .name = "WIS TW2804 I2C driver",
357 .id = I2C_DRIVERID_WIS_TW2804,
358 .detach_client = wis_tw2804_detach,
359 .command = wis_tw2804_command,
362 static int __init wis_tw2804_init(void)
366 r = i2c_add_driver(&wis_tw2804_driver);
369 return wis_i2c_add_driver(wis_tw2804_driver.id, wis_tw2804_detect);
372 static void __exit wis_tw2804_cleanup(void)
374 wis_i2c_del_driver(wis_tw2804_detect);
375 i2c_del_driver(&wis_tw2804_driver);
378 module_init(wis_tw2804_init);
379 module_exit(wis_tw2804_cleanup);
381 MODULE_LICENSE("GPL v2");