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>
35 static u8 initial_registers[] =
102 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
105 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
107 return i2c_smbus_write_byte_data(client, reg, value);
110 static int write_regs(struct i2c_client *client, u8 *regs)
114 for (i = 0; regs[i] != 0x00; i += 2)
115 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
120 static int wis_saa7113_command(struct i2c_client *client,
121 unsigned int cmd, void *arg)
123 struct wis_saa7113 *dec = i2c_get_clientdata(client);
130 i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
131 i2c_smbus_write_byte_data(client, 0x09,
132 *input < 6 ? 0x40 : 0x80);
137 v4l2_std_id *input = arg;
139 if (dec->norm & V4L2_STD_NTSC) {
140 write_reg(client, 0x0e, 0x01);
141 write_reg(client, 0x10, 0x40);
142 } else if (dec->norm & V4L2_STD_PAL) {
143 write_reg(client, 0x0e, 0x01);
144 write_reg(client, 0x10, 0x48);
145 } else if (dec->norm * V4L2_STD_SECAM) {
146 write_reg(client, 0x0e, 0x50);
147 write_reg(client, 0x10, 0x48);
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));
162 ctrl->default_value = 128;
165 case V4L2_CID_CONTRAST:
166 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
167 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
171 ctrl->default_value = 71;
174 case V4L2_CID_SATURATION:
175 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
176 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
180 ctrl->default_value = 64;
184 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
185 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
186 ctrl->minimum = -128;
189 ctrl->default_value = 0;
197 struct v4l2_control *ctrl = arg;
200 case V4L2_CID_BRIGHTNESS:
201 if (ctrl->value > 255)
202 dec->brightness = 255;
203 else if (ctrl->value < 0)
206 dec->brightness = ctrl->value;
207 write_reg(client, 0x0a, dec->brightness);
209 case V4L2_CID_CONTRAST:
210 if (ctrl->value > 127)
212 else if (ctrl->value < 0)
215 dec->contrast = ctrl->value;
216 write_reg(client, 0x0b, dec->contrast);
218 case V4L2_CID_SATURATION:
219 if (ctrl->value > 127)
220 dec->saturation = 127;
221 else if (ctrl->value < 0)
224 dec->saturation = ctrl->value;
225 write_reg(client, 0x0c, dec->saturation);
228 if (ctrl->value > 127)
230 else if (ctrl->value < -128)
233 dec->hue = ctrl->value;
234 write_reg(client, 0x0d, dec->hue);
241 struct v4l2_control *ctrl = arg;
244 case V4L2_CID_BRIGHTNESS:
245 ctrl->value = dec->brightness;
247 case V4L2_CID_CONTRAST:
248 ctrl->value = dec->contrast;
250 case V4L2_CID_SATURATION:
251 ctrl->value = dec->saturation;
254 ctrl->value = dec->hue;
265 static struct i2c_driver wis_saa7113_driver;
267 static struct i2c_client wis_saa7113_client_templ = {
268 .name = "SAA7113 (WIS)",
269 .driver = &wis_saa7113_driver,
272 static int wis_saa7113_detect(struct i2c_adapter *adapter, int addr, int kind)
274 struct i2c_client *client;
275 struct wis_saa7113 *dec;
277 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
280 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
283 memcpy(client, &wis_saa7113_client_templ,
284 sizeof(wis_saa7113_client_templ));
285 client->adapter = adapter;
288 dec = kmalloc(sizeof(struct wis_saa7113), GFP_KERNEL);
293 dec->norm = V4L2_STD_NTSC;
294 dec->brightness = 128;
296 dec->saturation = 64;
298 i2c_set_clientdata(client, dec);
301 "wis-saa7113: initializing SAA7113 at address %d on %s\n",
302 addr, adapter->name);
304 if (write_regs(client, initial_registers) < 0) {
306 "wis-saa7113: error initializing SAA7113\n");
312 i2c_attach_client(client);
316 static int wis_saa7113_detach(struct i2c_client *client)
318 struct wis_saa7113 *dec = i2c_get_clientdata(client);
321 r = i2c_detach_client(client);
330 static struct i2c_driver wis_saa7113_driver = {
332 .name = "WIS SAA7113 I2C driver",
334 .id = I2C_DRIVERID_WIS_SAA7113,
335 .detach_client = wis_saa7113_detach,
336 .command = wis_saa7113_command,
339 static int __init wis_saa7113_init(void)
343 r = i2c_add_driver(&wis_saa7113_driver);
346 return wis_i2c_add_driver(wis_saa7113_driver.id, wis_saa7113_detect);
349 static void __exit wis_saa7113_cleanup(void)
351 wis_i2c_del_driver(wis_saa7113_detect);
352 i2c_del_driver(&wis_saa7113_driver);
355 module_init(wis_saa7113_init);
356 module_exit(wis_saa7113_cleanup);
358 MODULE_LICENSE("GPL v2");