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 int wis_saa7113_probe(struct i2c_client
*client
,
265 const struct i2c_device_id
*id
)
267 struct i2c_adapter
*adapter
= client
->adapter
;
268 struct wis_saa7113
*dec
;
270 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
273 dec
= kmalloc(sizeof(struct wis_saa7113
), GFP_KERNEL
);
277 dec
->norm
= V4L2_STD_NTSC
;
278 dec
->brightness
= 128;
280 dec
->saturation
= 64;
282 i2c_set_clientdata(client
, dec
);
285 "wis-saa7113: initializing SAA7113 at address %d on %s\n",
286 client
->addr
, adapter
->name
);
288 if (write_regs(client
, initial_registers
) < 0) {
290 "wis-saa7113: error initializing SAA7113\n");
298 static int wis_saa7113_remove(struct i2c_client
*client
)
300 struct wis_saa7113
*dec
= i2c_get_clientdata(client
);
302 i2c_set_clientdata(client
, NULL
);
307 static struct i2c_device_id wis_saa7113_id
[] = {
308 { "wis_saa7113", 0 },
312 static struct i2c_driver wis_saa7113_driver
= {
314 .name
= "WIS SAA7113 I2C driver",
316 .probe
= wis_saa7113_probe
,
317 .remove
= wis_saa7113_remove
,
318 .command
= wis_saa7113_command
,
319 .id_table
= wis_saa7113_id
,
322 static int __init
wis_saa7113_init(void)
324 return i2c_add_driver(&wis_saa7113_driver
);
327 static void __exit
wis_saa7113_cleanup(void)
329 i2c_del_driver(&wis_saa7113_driver
);
332 module_init(wis_saa7113_init
);
333 module_exit(wis_saa7113_cleanup
);
335 MODULE_LICENSE("GPL v2");