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>
34 static u8 initial_registers[] =
101 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
104 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
106 return i2c_smbus_write_byte_data(client, reg, value);
109 static int write_regs(struct i2c_client *client, u8 *regs)
113 for (i = 0; regs[i] != 0x00; i += 2)
114 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
119 static int wis_saa7113_command(struct i2c_client *client,
120 unsigned int cmd, void *arg)
122 struct wis_saa7113 *dec = i2c_get_clientdata(client);
129 i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
130 i2c_smbus_write_byte_data(client, 0x09,
131 *input < 6 ? 0x40 : 0x80);
136 v4l2_std_id *input = arg;
138 if (dec->norm & V4L2_STD_NTSC) {
139 write_reg(client, 0x0e, 0x01);
140 write_reg(client, 0x10, 0x40);
141 } else if (dec->norm & V4L2_STD_PAL) {
142 write_reg(client, 0x0e, 0x01);
143 write_reg(client, 0x10, 0x48);
144 } else if (dec->norm * V4L2_STD_SECAM) {
145 write_reg(client, 0x0e, 0x50);
146 write_reg(client, 0x10, 0x48);
150 case VIDIOC_QUERYCTRL:
152 struct v4l2_queryctrl *ctrl = arg;
155 case V4L2_CID_BRIGHTNESS:
156 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
157 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
161 ctrl->default_value = 128;
164 case V4L2_CID_CONTRAST:
165 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
166 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
170 ctrl->default_value = 71;
173 case V4L2_CID_SATURATION:
174 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
175 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
179 ctrl->default_value = 64;
183 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
184 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
185 ctrl->minimum = -128;
188 ctrl->default_value = 0;
196 struct v4l2_control *ctrl = arg;
199 case V4L2_CID_BRIGHTNESS:
200 if (ctrl->value > 255)
201 dec->brightness = 255;
202 else if (ctrl->value < 0)
205 dec->brightness = ctrl->value;
206 write_reg(client, 0x0a, dec->brightness);
208 case V4L2_CID_CONTRAST:
209 if (ctrl->value > 127)
211 else if (ctrl->value < 0)
214 dec->contrast = ctrl->value;
215 write_reg(client, 0x0b, dec->contrast);
217 case V4L2_CID_SATURATION:
218 if (ctrl->value > 127)
219 dec->saturation = 127;
220 else if (ctrl->value < 0)
223 dec->saturation = ctrl->value;
224 write_reg(client, 0x0c, dec->saturation);
227 if (ctrl->value > 127)
229 else if (ctrl->value < -128)
232 dec->hue = ctrl->value;
233 write_reg(client, 0x0d, dec->hue);
240 struct v4l2_control *ctrl = arg;
243 case V4L2_CID_BRIGHTNESS:
244 ctrl->value = dec->brightness;
246 case V4L2_CID_CONTRAST:
247 ctrl->value = dec->contrast;
249 case V4L2_CID_SATURATION:
250 ctrl->value = dec->saturation;
253 ctrl->value = dec->hue;
264 static struct i2c_driver wis_saa7113_driver;
266 static struct i2c_client wis_saa7113_client_templ = {
267 .name = "SAA7113 (WIS)",
268 .driver = &wis_saa7113_driver,
271 static int wis_saa7113_detect(struct i2c_adapter *adapter, int addr, int kind)
273 struct i2c_client *client;
274 struct wis_saa7113 *dec;
276 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
279 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
282 memcpy(client, &wis_saa7113_client_templ,
283 sizeof(wis_saa7113_client_templ));
284 client->adapter = adapter;
287 dec = kmalloc(sizeof(struct wis_saa7113), GFP_KERNEL);
292 dec->norm = V4L2_STD_NTSC;
293 dec->brightness = 128;
295 dec->saturation = 64;
297 i2c_set_clientdata(client, dec);
300 "wis-saa7113: initializing SAA7113 at address %d on %s\n",
301 addr, adapter->name);
303 if (write_regs(client, initial_registers) < 0) {
305 "wis-saa7113: error initializing SAA7113\n");
311 i2c_attach_client(client);
315 static int wis_saa7113_detach(struct i2c_client *client)
317 struct wis_saa7113 *dec = i2c_get_clientdata(client);
320 r = i2c_detach_client(client);
329 static struct i2c_driver wis_saa7113_driver = {
331 .name = "WIS SAA7113 I2C driver",
333 .id = I2C_DRIVERID_WIS_SAA7113,
334 .detach_client = wis_saa7113_detach,
335 .command = wis_saa7113_command,
338 static int __init wis_saa7113_init(void)
342 r = i2c_add_driver(&wis_saa7113_driver);
345 return wis_i2c_add_driver(wis_saa7113_driver.id, wis_saa7113_detect);
348 static void __exit wis_saa7113_cleanup(void)
350 wis_i2c_del_driver(wis_saa7113_detect);
351 i2c_del_driver(&wis_saa7113_driver);
354 module_init(wis_saa7113_init);
355 module_exit(wis_saa7113_cleanup);
357 MODULE_LICENSE("GPL v2");