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>
36 static u8 initial_registers[] =
103 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
106 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
108 return i2c_smbus_write_byte_data(client, reg, value);
111 static int write_regs(struct i2c_client *client, u8 *regs)
115 for (i = 0; regs[i] != 0x00; i += 2)
116 if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
121 static int wis_saa7113_command(struct i2c_client *client,
122 unsigned int cmd, void *arg)
124 struct wis_saa7113 *dec = i2c_get_clientdata(client);
127 case DECODER_SET_INPUT:
131 i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
132 i2c_smbus_write_byte_data(client, 0x09,
133 *input < 6 ? 0x40 : 0x80);
136 case DECODER_SET_NORM:
142 write_reg(client, 0x0e, 0x01);
143 write_reg(client, 0x10, 0x48);
145 case VIDEO_MODE_NTSC:
146 write_reg(client, 0x0e, 0x01);
147 write_reg(client, 0x10, 0x40);
149 case VIDEO_MODE_SECAM:
150 write_reg(client, 0x0e, 0x50);
151 write_reg(client, 0x10, 0x48);
156 case VIDIOC_QUERYCTRL:
158 struct v4l2_queryctrl *ctrl = arg;
161 case V4L2_CID_BRIGHTNESS:
162 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
163 strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
167 ctrl->default_value = 128;
170 case V4L2_CID_CONTRAST:
171 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
172 strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
176 ctrl->default_value = 71;
179 case V4L2_CID_SATURATION:
180 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
181 strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
185 ctrl->default_value = 64;
189 ctrl->type = V4L2_CTRL_TYPE_INTEGER;
190 strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
191 ctrl->minimum = -128;
194 ctrl->default_value = 0;
202 struct v4l2_control *ctrl = arg;
205 case V4L2_CID_BRIGHTNESS:
206 if (ctrl->value > 255)
207 dec->brightness = 255;
208 else if (ctrl->value < 0)
211 dec->brightness = ctrl->value;
212 write_reg(client, 0x0a, dec->brightness);
214 case V4L2_CID_CONTRAST:
215 if (ctrl->value > 127)
217 else if (ctrl->value < 0)
220 dec->contrast = ctrl->value;
221 write_reg(client, 0x0b, dec->contrast);
223 case V4L2_CID_SATURATION:
224 if (ctrl->value > 127)
225 dec->saturation = 127;
226 else if (ctrl->value < 0)
229 dec->saturation = ctrl->value;
230 write_reg(client, 0x0c, dec->saturation);
233 if (ctrl->value > 127)
235 else if (ctrl->value < -128)
238 dec->hue = ctrl->value;
239 write_reg(client, 0x0d, dec->hue);
246 struct v4l2_control *ctrl = arg;
249 case V4L2_CID_BRIGHTNESS:
250 ctrl->value = dec->brightness;
252 case V4L2_CID_CONTRAST:
253 ctrl->value = dec->contrast;
255 case V4L2_CID_SATURATION:
256 ctrl->value = dec->saturation;
259 ctrl->value = dec->hue;
270 static struct i2c_driver wis_saa7113_driver;
272 static struct i2c_client wis_saa7113_client_templ = {
273 .name = "SAA7113 (WIS)",
274 .driver = &wis_saa7113_driver,
277 static int wis_saa7113_detect(struct i2c_adapter *adapter, int addr, int kind)
279 struct i2c_client *client;
280 struct wis_saa7113 *dec;
282 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
285 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
288 memcpy(client, &wis_saa7113_client_templ,
289 sizeof(wis_saa7113_client_templ));
290 client->adapter = adapter;
293 dec = kmalloc(sizeof(struct wis_saa7113), GFP_KERNEL);
298 dec->norm = VIDEO_MODE_NTSC;
299 dec->brightness = 128;
301 dec->saturation = 64;
303 i2c_set_clientdata(client, dec);
306 "wis-saa7113: initializing SAA7113 at address %d on %s\n",
307 addr, adapter->name);
309 if (write_regs(client, initial_registers) < 0) {
311 "wis-saa7113: error initializing SAA7113\n");
317 i2c_attach_client(client);
321 static int wis_saa7113_detach(struct i2c_client *client)
323 struct wis_saa7113 *dec = i2c_get_clientdata(client);
326 r = i2c_detach_client(client);
335 static struct i2c_driver wis_saa7113_driver = {
337 .name = "WIS SAA7113 I2C driver",
339 .id = I2C_DRIVERID_WIS_SAA7113,
340 .detach_client = wis_saa7113_detach,
341 .command = wis_saa7113_command,
344 static int __init wis_saa7113_init(void)
348 r = i2c_add_driver(&wis_saa7113_driver);
351 return wis_i2c_add_driver(wis_saa7113_driver.id, wis_saa7113_detect);
354 static void __exit wis_saa7113_cleanup(void)
356 wis_i2c_del_driver(wis_saa7113_detect);
357 i2c_del_driver(&wis_saa7113_driver);
360 module_init(wis_saa7113_init);
361 module_exit(wis_saa7113_cleanup);
363 MODULE_LICENSE("GPL v2");