2 * indycam.c - Silicon Graphics IndyCam digital camera driver
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include <linux/videodev.h>
24 /* IndyCam decodes stream of photons into digital image representation ;-) */
25 #include <linux/video_decoder.h>
26 #include <linux/i2c.h>
30 //#define INDYCAM_DEBUG
32 #define INDYCAM_MODULE_VERSION "0.0.3"
34 MODULE_DESCRIPTION("SGI IndyCam driver");
35 MODULE_VERSION(INDYCAM_MODULE_VERSION
);
36 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
37 MODULE_LICENSE("GPL");
40 #define dprintk(x...) printk("IndyCam: " x);
41 #define indycam_regdump(client) indycam_regdump_debug(client)
44 #define indycam_regdump(client)
48 struct i2c_client
*client
;
52 static struct i2c_driver i2c_driver_indycam
;
54 static const unsigned char initseq
[] = {
55 INDYCAM_CONTROL_AGCENA
, /* INDYCAM_CONTROL */
56 INDYCAM_SHUTTER_DEFAULT
, /* INDYCAM_SHUTTER */
57 INDYCAM_GAIN_DEFAULT
, /* INDYCAM_GAIN */
58 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
59 INDYCAM_RED_BALANCE_DEFAULT
, /* INDYCAM_RED_BALANCE */
60 INDYCAM_BLUE_BALANCE_DEFAULT
, /* INDYCAM_BLUE_BALANCE */
61 INDYCAM_RED_SATURATION_DEFAULT
, /* INDYCAM_RED_SATURATION */
62 INDYCAM_BLUE_SATURATION_DEFAULT
,/* INDYCAM_BLUE_SATURATION */
65 /* IndyCam register handling */
67 static int indycam_read_reg(struct i2c_client
*client
, unsigned char reg
,
72 if (reg
== INDYCAM_RESET
) {
73 dprintk("indycam_read_reg(): "
74 "skipping write-only register %d\n", reg
);
79 ret
= i2c_smbus_read_byte_data(client
, reg
);
81 printk(KERN_ERR
"IndyCam: indycam_read_reg(): read failed, "
82 "register = 0x%02x\n", reg
);
86 *value
= (unsigned char)ret
;
91 static int indycam_write_reg(struct i2c_client
*client
, unsigned char reg
,
96 if ((reg
== INDYCAM_BRIGHTNESS
)
97 || (reg
== INDYCAM_VERSION
)) {
98 dprintk("indycam_write_reg(): "
99 "skipping read-only register %d\n", reg
);
103 dprintk("Writing Reg %d = 0x%02x\n", reg
, value
);
104 err
= i2c_smbus_write_byte_data(client
, reg
, value
);
106 printk(KERN_ERR
"IndyCam: indycam_write_reg(): write failed, "
107 "register = 0x%02x, value = 0x%02x\n", reg
, value
);
112 static int indycam_write_block(struct i2c_client
*client
, unsigned char reg
,
113 unsigned char length
, unsigned char *data
)
118 for (i
= reg
; i
< length
; i
++) {
119 err
= indycam_write_reg(client
, reg
+ i
, data
[i
]);
127 /* Helper functions */
130 static void indycam_regdump_debug(struct i2c_client
*client
)
135 for (i
= 0; i
< 9; i
++) {
136 indycam_read_reg(client
, i
, &val
);
137 dprintk("Reg %d = 0x%02x\n", i
, val
);
142 static int indycam_get_controls(struct i2c_client
*client
,
143 struct indycam_control
*ctrl
)
145 unsigned char ctrl_reg
;
147 indycam_read_reg(client
, INDYCAM_CONTROL
, &ctrl_reg
);
148 ctrl
->agc
= (ctrl_reg
& INDYCAM_CONTROL_AGCENA
)
149 ? INDYCAM_VALUE_ENABLED
150 : INDYCAM_VALUE_DISABLED
;
151 ctrl
->awb
= (ctrl_reg
& INDYCAM_CONTROL_AWBCTL
)
152 ? INDYCAM_VALUE_ENABLED
153 : INDYCAM_VALUE_DISABLED
;
154 indycam_read_reg(client
, INDYCAM_SHUTTER
,
155 (unsigned char *)&ctrl
->shutter
);
156 indycam_read_reg(client
, INDYCAM_GAIN
,
157 (unsigned char *)&ctrl
->gain
);
158 indycam_read_reg(client
, INDYCAM_RED_BALANCE
,
159 (unsigned char *)&ctrl
->red_balance
);
160 indycam_read_reg(client
, INDYCAM_BLUE_BALANCE
,
161 (unsigned char *)&ctrl
->blue_balance
);
162 indycam_read_reg(client
, INDYCAM_RED_SATURATION
,
163 (unsigned char *)&ctrl
->red_saturation
);
164 indycam_read_reg(client
, INDYCAM_BLUE_SATURATION
,
165 (unsigned char *)&ctrl
->blue_saturation
);
166 indycam_read_reg(client
, INDYCAM_GAMMA
,
167 (unsigned char *)&ctrl
->gamma
);
172 static int indycam_set_controls(struct i2c_client
*client
,
173 struct indycam_control
*ctrl
)
175 unsigned char ctrl_reg
;
177 indycam_read_reg(client
, INDYCAM_CONTROL
, &ctrl_reg
);
178 if (ctrl
->agc
!= INDYCAM_VALUE_UNCHANGED
) {
180 ctrl_reg
|= INDYCAM_CONTROL_AGCENA
;
182 ctrl_reg
&= ~INDYCAM_CONTROL_AGCENA
;
184 if (ctrl
->awb
!= INDYCAM_VALUE_UNCHANGED
) {
186 ctrl_reg
|= INDYCAM_CONTROL_AWBCTL
;
188 ctrl_reg
&= ~INDYCAM_CONTROL_AWBCTL
;
190 indycam_write_reg(client
, INDYCAM_CONTROL
, ctrl_reg
);
192 if (ctrl
->shutter
>= 0)
193 indycam_write_reg(client
, INDYCAM_SHUTTER
, ctrl
->shutter
);
195 indycam_write_reg(client
, INDYCAM_GAIN
, ctrl
->gain
);
196 if (ctrl
->red_balance
>= 0)
197 indycam_write_reg(client
, INDYCAM_RED_BALANCE
,
199 if (ctrl
->blue_balance
>= 0)
200 indycam_write_reg(client
, INDYCAM_BLUE_BALANCE
,
202 if (ctrl
->red_saturation
>= 0)
203 indycam_write_reg(client
, INDYCAM_RED_SATURATION
,
204 ctrl
->red_saturation
);
205 if (ctrl
->blue_saturation
>= 0)
206 indycam_write_reg(client
, INDYCAM_BLUE_SATURATION
,
207 ctrl
->blue_saturation
);
208 if (ctrl
->gamma
>= 0)
209 indycam_write_reg(client
, INDYCAM_GAMMA
, ctrl
->gamma
);
216 static int indycam_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
219 struct indycam
*camera
;
220 struct i2c_client
*client
;
222 printk(KERN_INFO
"SGI IndyCam driver version %s\n",
223 INDYCAM_MODULE_VERSION
);
225 client
= kmalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
228 camera
= kmalloc(sizeof(struct indycam
), GFP_KERNEL
);
231 goto out_free_client
;
234 memset(client
, 0, sizeof(struct i2c_client
));
235 memset(camera
, 0, sizeof(struct indycam
));
238 client
->adapter
= adap
;
239 client
->driver
= &i2c_driver_indycam
;
241 strcpy(client
->name
, "IndyCam client");
242 i2c_set_clientdata(client
, camera
);
244 camera
->client
= client
;
246 err
= i2c_attach_client(client
);
248 goto out_free_camera
;
250 camera
->version
= i2c_smbus_read_byte_data(client
, INDYCAM_VERSION
);
251 if (camera
->version
!= CAMERA_VERSION_INDY
&&
252 camera
->version
!= CAMERA_VERSION_MOOSE
) {
254 goto out_detach_client
;
256 printk(KERN_INFO
"IndyCam v%d.%d detected\n",
257 INDYCAM_VERSION_MAJOR(camera
->version
),
258 INDYCAM_VERSION_MINOR(camera
->version
));
260 indycam_regdump(client
);
263 err
= indycam_write_block(client
, 0, sizeof(initseq
),
264 (unsigned char *)&initseq
);
266 printk(KERN_ERR
"IndyCam initalization failed\n");
268 goto out_detach_client
;
271 indycam_regdump(client
);
274 err
= indycam_write_reg(client
, INDYCAM_CONTROL
,
275 INDYCAM_CONTROL_AGCENA
| INDYCAM_CONTROL_AWBCTL
);
277 printk(KERN_ERR
"IndyCam white balance "
278 "initialization failed\n");
280 goto out_detach_client
;
283 indycam_regdump(client
);
285 printk(KERN_INFO
"IndyCam initialized\n");
290 i2c_detach_client(client
);
298 static int indycam_probe(struct i2c_adapter
*adap
)
300 /* Indy specific crap */
301 if (adap
->id
== I2C_HW_SGI_VINO
)
302 return indycam_attach(adap
, INDYCAM_ADDR
, 0);
303 /* Feel free to add probe here :-) */
307 static int indycam_detach(struct i2c_client
*client
)
309 struct indycam
*camera
= i2c_get_clientdata(client
);
311 i2c_detach_client(client
);
317 static int indycam_command(struct i2c_client
*client
, unsigned int cmd
,
320 // struct indycam *camera = i2c_get_clientdata(client);
322 /* The old video_decoder interface just isn't enough,
323 * so we'll use some custom commands. */
325 case DECODER_GET_CAPABILITIES
: {
326 struct video_decoder_capability
*cap
= arg
;
328 cap
->flags
= VIDEO_DECODER_NTSC
;
333 case DECODER_GET_STATUS
: {
336 *iarg
= DECODER_STATUS_GOOD
| DECODER_STATUS_NTSC
|
337 DECODER_STATUS_COLOR
;
340 case DECODER_SET_NORM
: {
344 case VIDEO_MODE_NTSC
:
351 case DECODER_SET_INPUT
: {
358 case DECODER_SET_OUTPUT
: {
365 case DECODER_ENABLE_OUTPUT
: {
369 case DECODER_SET_PICTURE
: {
370 // struct video_picture *pic = arg;
371 /* TODO: convert values for indycam_set_controls() */
374 case DECODER_INDYCAM_GET_CONTROLS
: {
375 struct indycam_control
*ctrl
= arg
;
376 indycam_get_controls(client
, ctrl
);
378 case DECODER_INDYCAM_SET_CONTROLS
: {
379 struct indycam_control
*ctrl
= arg
;
380 indycam_set_controls(client
, ctrl
);
389 static struct i2c_driver i2c_driver_indycam
= {
390 .owner
= THIS_MODULE
,
392 .id
= I2C_DRIVERID_INDYCAM
,
393 .flags
= I2C_DF_NOTIFY
,
394 .attach_adapter
= indycam_probe
,
395 .detach_client
= indycam_detach
,
396 .command
= indycam_command
,
399 static int __init
indycam_init(void)
401 return i2c_add_driver(&i2c_driver_indycam
);
404 static void __exit
indycam_exit(void)
406 i2c_del_driver(&i2c_driver_indycam
);
409 module_init(indycam_init
);
410 module_exit(indycam_exit
);