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/module.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
17 #include <linux/kernel.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
21 #include <linux/sched.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)
47 #define VINO_ADAPTER (I2C_ALGO_SGI | I2C_HW_SGI_VINO)
50 struct i2c_client
*client
;
54 static struct i2c_driver i2c_driver_indycam
;
56 static const unsigned char initseq
[] = {
57 INDYCAM_CONTROL_AGCENA
, /* INDYCAM_CONTROL */
58 INDYCAM_SHUTTER_DEFAULT
, /* INDYCAM_SHUTTER */
59 INDYCAM_GAIN_DEFAULT
, /* INDYCAM_GAIN */
60 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
61 INDYCAM_RED_BALANCE_DEFAULT
, /* INDYCAM_RED_BALANCE */
62 INDYCAM_BLUE_BALANCE_DEFAULT
, /* INDYCAM_BLUE_BALANCE */
63 INDYCAM_RED_SATURATION_DEFAULT
, /* INDYCAM_RED_SATURATION */
64 INDYCAM_BLUE_SATURATION_DEFAULT
,/* INDYCAM_BLUE_SATURATION */
67 /* IndyCam register handling */
69 static int indycam_read_reg(struct i2c_client
*client
, unsigned char reg
,
74 if (reg
== INDYCAM_RESET
) {
75 dprintk("indycam_read_reg(): "
76 "skipping write-only register %d\n", reg
);
81 ret
= i2c_smbus_read_byte_data(client
, reg
);
83 printk(KERN_ERR
"IndyCam: indycam_read_reg(): read failed, "
84 "register = 0x%02x\n", reg
);
88 *value
= (unsigned char)ret
;
93 static int indycam_write_reg(struct i2c_client
*client
, unsigned char reg
,
98 if ((reg
== INDYCAM_BRIGHTNESS
)
99 || (reg
== INDYCAM_VERSION
)) {
100 dprintk("indycam_write_reg(): "
101 "skipping read-only register %d\n", reg
);
105 dprintk("Writing Reg %d = 0x%02x\n", reg
, value
);
106 err
= i2c_smbus_write_byte_data(client
, reg
, value
);
108 printk(KERN_ERR
"IndyCam: indycam_write_reg(): write failed, "
109 "register = 0x%02x, value = 0x%02x\n", reg
, value
);
114 static int indycam_write_block(struct i2c_client
*client
, unsigned char reg
,
115 unsigned char length
, unsigned char *data
)
120 for (i
= reg
; i
< length
; i
++) {
121 err
= indycam_write_reg(client
, reg
+ i
, data
[i
]);
129 /* Helper functions */
132 static void indycam_regdump_debug(struct i2c_client
*client
)
137 for (i
= 0; i
< 9; i
++) {
138 indycam_read_reg(client
, i
, &val
);
139 dprintk("Reg %d = 0x%02x\n", i
, val
);
144 static int indycam_get_controls(struct i2c_client
*client
,
145 struct indycam_control
*ctrl
)
147 unsigned char ctrl_reg
;
149 indycam_read_reg(client
, INDYCAM_CONTROL
, &ctrl_reg
);
150 ctrl
->agc
= (ctrl_reg
& INDYCAM_CONTROL_AGCENA
)
151 ? INDYCAM_VALUE_ENABLED
152 : INDYCAM_VALUE_DISABLED
;
153 ctrl
->awb
= (ctrl_reg
& INDYCAM_CONTROL_AWBCTL
)
154 ? INDYCAM_VALUE_ENABLED
155 : INDYCAM_VALUE_DISABLED
;
156 indycam_read_reg(client
, INDYCAM_SHUTTER
,
157 (unsigned char *)&ctrl
->shutter
);
158 indycam_read_reg(client
, INDYCAM_GAIN
,
159 (unsigned char *)&ctrl
->gain
);
160 indycam_read_reg(client
, INDYCAM_RED_BALANCE
,
161 (unsigned char *)&ctrl
->red_balance
);
162 indycam_read_reg(client
, INDYCAM_BLUE_BALANCE
,
163 (unsigned char *)&ctrl
->blue_balance
);
164 indycam_read_reg(client
, INDYCAM_RED_SATURATION
,
165 (unsigned char *)&ctrl
->red_saturation
);
166 indycam_read_reg(client
, INDYCAM_BLUE_SATURATION
,
167 (unsigned char *)&ctrl
->blue_saturation
);
168 indycam_read_reg(client
, INDYCAM_GAMMA
,
169 (unsigned char *)&ctrl
->gamma
);
174 static int indycam_set_controls(struct i2c_client
*client
,
175 struct indycam_control
*ctrl
)
177 unsigned char ctrl_reg
;
179 indycam_read_reg(client
, INDYCAM_CONTROL
, &ctrl_reg
);
180 if (ctrl
->agc
!= INDYCAM_VALUE_UNCHANGED
) {
182 ctrl_reg
|= INDYCAM_CONTROL_AGCENA
;
184 ctrl_reg
&= ~INDYCAM_CONTROL_AGCENA
;
186 if (ctrl
->awb
!= INDYCAM_VALUE_UNCHANGED
) {
188 ctrl_reg
|= INDYCAM_CONTROL_AWBCTL
;
190 ctrl_reg
&= ~INDYCAM_CONTROL_AWBCTL
;
192 indycam_write_reg(client
, INDYCAM_CONTROL
, ctrl_reg
);
194 if (ctrl
->shutter
>= 0)
195 indycam_write_reg(client
, INDYCAM_SHUTTER
, ctrl
->shutter
);
197 indycam_write_reg(client
, INDYCAM_GAIN
, ctrl
->gain
);
198 if (ctrl
->red_balance
>= 0)
199 indycam_write_reg(client
, INDYCAM_RED_BALANCE
,
201 if (ctrl
->blue_balance
>= 0)
202 indycam_write_reg(client
, INDYCAM_BLUE_BALANCE
,
204 if (ctrl
->red_saturation
>= 0)
205 indycam_write_reg(client
, INDYCAM_RED_SATURATION
,
206 ctrl
->red_saturation
);
207 if (ctrl
->blue_saturation
>= 0)
208 indycam_write_reg(client
, INDYCAM_BLUE_SATURATION
,
209 ctrl
->blue_saturation
);
210 if (ctrl
->gamma
>= 0)
211 indycam_write_reg(client
, INDYCAM_GAMMA
, ctrl
->gamma
);
218 static int indycam_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
221 struct indycam
*camera
;
222 struct i2c_client
*client
;
224 printk(KERN_INFO
"SGI IndyCam driver version %s\n",
225 INDYCAM_MODULE_VERSION
);
227 client
= kmalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
230 camera
= kmalloc(sizeof(struct indycam
), GFP_KERNEL
);
233 goto out_free_client
;
236 memset(client
, 0, sizeof(struct i2c_client
));
237 memset(camera
, 0, sizeof(struct indycam
));
240 client
->adapter
= adap
;
241 client
->driver
= &i2c_driver_indycam
;
243 strcpy(client
->name
, "IndyCam client");
244 i2c_set_clientdata(client
, camera
);
246 camera
->client
= client
;
248 err
= i2c_attach_client(client
);
250 goto out_free_camera
;
252 camera
->version
= i2c_smbus_read_byte_data(client
, INDYCAM_VERSION
);
253 if (camera
->version
!= CAMERA_VERSION_INDY
&&
254 camera
->version
!= CAMERA_VERSION_MOOSE
) {
256 goto out_detach_client
;
258 printk(KERN_INFO
"IndyCam v%d.%d detected\n",
259 INDYCAM_VERSION_MAJOR(camera
->version
),
260 INDYCAM_VERSION_MINOR(camera
->version
));
262 indycam_regdump(client
);
265 err
= indycam_write_block(client
, 0, sizeof(initseq
),
266 (unsigned char *)&initseq
);
268 printk(KERN_ERR
"IndyCam initalization failed\n");
270 goto out_detach_client
;
273 indycam_regdump(client
);
276 err
= indycam_write_reg(client
, INDYCAM_CONTROL
,
277 INDYCAM_CONTROL_AGCENA
| INDYCAM_CONTROL_AWBCTL
);
279 printk(KERN_ERR
"IndyCam white balance "
280 "initialization failed\n");
282 goto out_detach_client
;
285 indycam_regdump(client
);
287 printk(KERN_INFO
"IndyCam initialized\n");
292 i2c_detach_client(client
);
300 static int indycam_probe(struct i2c_adapter
*adap
)
302 /* Indy specific crap */
303 if (adap
->id
== VINO_ADAPTER
)
304 return indycam_attach(adap
, INDYCAM_ADDR
, 0);
305 /* Feel free to add probe here :-) */
309 static int indycam_detach(struct i2c_client
*client
)
311 struct indycam
*camera
= i2c_get_clientdata(client
);
313 i2c_detach_client(client
);
319 static int indycam_command(struct i2c_client
*client
, unsigned int cmd
,
322 // struct indycam *camera = i2c_get_clientdata(client);
324 /* The old video_decoder interface just isn't enough,
325 * so we'll use some custom commands. */
327 case DECODER_GET_CAPABILITIES
: {
328 struct video_decoder_capability
*cap
= arg
;
330 cap
->flags
= VIDEO_DECODER_NTSC
;
335 case DECODER_GET_STATUS
: {
338 *iarg
= DECODER_STATUS_GOOD
| DECODER_STATUS_NTSC
|
339 DECODER_STATUS_COLOR
;
342 case DECODER_SET_NORM
: {
346 case VIDEO_MODE_NTSC
:
353 case DECODER_SET_INPUT
: {
360 case DECODER_SET_OUTPUT
: {
367 case DECODER_ENABLE_OUTPUT
: {
371 case DECODER_SET_PICTURE
: {
372 // struct video_picture *pic = arg;
373 /* TODO: convert values for indycam_set_controls() */
376 case DECODER_INDYCAM_GET_CONTROLS
: {
377 struct indycam_control
*ctrl
= arg
;
378 indycam_get_controls(client
, ctrl
);
380 case DECODER_INDYCAM_SET_CONTROLS
: {
381 struct indycam_control
*ctrl
= arg
;
382 indycam_set_controls(client
, ctrl
);
391 static struct i2c_driver i2c_driver_indycam
= {
392 .owner
= THIS_MODULE
,
394 .id
= I2C_DRIVERID_INDYCAM
,
395 .flags
= I2C_DF_NOTIFY
,
396 .attach_adapter
= indycam_probe
,
397 .detach_client
= indycam_detach
,
398 .command
= indycam_command
,
401 static int __init
indycam_init(void)
403 return i2c_add_driver(&i2c_driver_indycam
);
406 static void __exit
indycam_exit(void)
408 i2c_del_driver(&i2c_driver_indycam
);
411 module_init(indycam_init
);
412 module_exit(indycam_exit
);