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>
23 #include <linux/slab.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 int wis_saa7113_probe(struct i2c_client
*client
,
266 const struct i2c_device_id
*id
)
268 struct i2c_adapter
*adapter
= client
->adapter
;
269 struct wis_saa7113
*dec
;
271 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
274 dec
= kmalloc(sizeof(struct wis_saa7113
), GFP_KERNEL
);
278 dec
->norm
= V4L2_STD_NTSC
;
279 dec
->brightness
= 128;
281 dec
->saturation
= 64;
283 i2c_set_clientdata(client
, dec
);
286 "wis-saa7113: initializing SAA7113 at address %d on %s\n",
287 client
->addr
, adapter
->name
);
289 if (write_regs(client
, initial_registers
) < 0) {
291 "wis-saa7113: error initializing SAA7113\n");
299 static int wis_saa7113_remove(struct i2c_client
*client
)
301 struct wis_saa7113
*dec
= i2c_get_clientdata(client
);
307 static const struct i2c_device_id wis_saa7113_id
[] = {
308 { "wis_saa7113", 0 },
311 MODULE_DEVICE_TABLE(i2c
, wis_saa7113_id
);
313 static struct i2c_driver wis_saa7113_driver
= {
315 .name
= "WIS SAA7113 I2C driver",
317 .probe
= wis_saa7113_probe
,
318 .remove
= wis_saa7113_remove
,
319 .command
= wis_saa7113_command
,
320 .id_table
= wis_saa7113_id
,
323 static int __init
wis_saa7113_init(void)
325 return i2c_add_driver(&wis_saa7113_driver
);
328 static void __exit
wis_saa7113_cleanup(void)
330 i2c_del_driver(&wis_saa7113_driver
);
333 module_init(wis_saa7113_init
);
334 module_exit(wis_saa7113_cleanup
);
336 MODULE_LICENSE("GPL v2");