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/slab.h>
22 /* IndyCam decodes stream of photons into digital image representation ;-) */
23 #include <linux/videodev2.h>
24 #include <linux/i2c.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-chip-ident.h>
30 #define INDYCAM_MODULE_VERSION "0.0.5"
32 MODULE_DESCRIPTION("SGI IndyCam driver");
33 MODULE_VERSION(INDYCAM_MODULE_VERSION
);
34 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
35 MODULE_LICENSE("GPL");
38 // #define INDYCAM_DEBUG
41 #define dprintk(x...) printk("IndyCam: " x);
42 #define indycam_regdump(client) indycam_regdump_debug(client)
45 #define indycam_regdump(client)
49 struct v4l2_subdev sd
;
53 static inline struct indycam
*to_indycam(struct v4l2_subdev
*sd
)
55 return container_of(sd
, struct indycam
, sd
);
58 static const u8 initseq
[] = {
59 INDYCAM_CONTROL_AGCENA
, /* INDYCAM_CONTROL */
60 INDYCAM_SHUTTER_60
, /* INDYCAM_SHUTTER */
61 INDYCAM_GAIN_DEFAULT
, /* INDYCAM_GAIN */
62 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
63 INDYCAM_RED_BALANCE_DEFAULT
, /* INDYCAM_RED_BALANCE */
64 INDYCAM_BLUE_BALANCE_DEFAULT
, /* INDYCAM_BLUE_BALANCE */
65 INDYCAM_RED_SATURATION_DEFAULT
, /* INDYCAM_RED_SATURATION */
66 INDYCAM_BLUE_SATURATION_DEFAULT
,/* INDYCAM_BLUE_SATURATION */
69 /* IndyCam register handling */
71 static int indycam_read_reg(struct v4l2_subdev
*sd
, u8 reg
, u8
*value
)
73 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
76 if (reg
== INDYCAM_REG_RESET
) {
77 dprintk("indycam_read_reg(): "
78 "skipping write-only register %d\n", reg
);
83 ret
= i2c_smbus_read_byte_data(client
, reg
);
86 printk(KERN_ERR
"IndyCam: indycam_read_reg(): read failed, "
87 "register = 0x%02x\n", reg
);
96 static int indycam_write_reg(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
98 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
101 if (reg
== INDYCAM_REG_BRIGHTNESS
|| reg
== INDYCAM_REG_VERSION
) {
102 dprintk("indycam_write_reg(): "
103 "skipping read-only register %d\n", reg
);
107 dprintk("Writing Reg %d = 0x%02x\n", reg
, value
);
108 err
= i2c_smbus_write_byte_data(client
, reg
, value
);
111 printk(KERN_ERR
"IndyCam: indycam_write_reg(): write failed, "
112 "register = 0x%02x, value = 0x%02x\n", reg
, value
);
117 static int indycam_write_block(struct v4l2_subdev
*sd
, u8 reg
,
122 for (i
= 0; i
< length
; i
++) {
123 err
= indycam_write_reg(sd
, reg
+ i
, data
[i
]);
131 /* Helper functions */
134 static void indycam_regdump_debug(struct v4l2_subdev
*sd
)
139 for (i
= 0; i
< 9; i
++) {
140 indycam_read_reg(sd
, i
, &val
);
141 dprintk("Reg %d = 0x%02x\n", i
, val
);
146 static int indycam_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
148 struct indycam
*camera
= to_indycam(sd
);
153 case V4L2_CID_AUTOGAIN
:
154 case V4L2_CID_AUTO_WHITE_BALANCE
:
155 ret
= indycam_read_reg(sd
, INDYCAM_REG_CONTROL
, ®
);
158 if (ctrl
->id
== V4L2_CID_AUTOGAIN
)
159 ctrl
->value
= (reg
& INDYCAM_CONTROL_AGCENA
)
162 ctrl
->value
= (reg
& INDYCAM_CONTROL_AWBCTL
)
165 case V4L2_CID_EXPOSURE
:
166 ret
= indycam_read_reg(sd
, INDYCAM_REG_SHUTTER
, ®
);
169 ctrl
->value
= ((s32
)reg
== 0x00) ? 0xff : ((s32
)reg
- 1);
172 ret
= indycam_read_reg(sd
, INDYCAM_REG_GAIN
, ®
);
175 ctrl
->value
= (s32
)reg
;
177 case V4L2_CID_RED_BALANCE
:
178 ret
= indycam_read_reg(sd
, INDYCAM_REG_RED_BALANCE
, ®
);
181 ctrl
->value
= (s32
)reg
;
183 case V4L2_CID_BLUE_BALANCE
:
184 ret
= indycam_read_reg(sd
, INDYCAM_REG_BLUE_BALANCE
, ®
);
187 ctrl
->value
= (s32
)reg
;
189 case INDYCAM_CONTROL_RED_SATURATION
:
190 ret
= indycam_read_reg(sd
,
191 INDYCAM_REG_RED_SATURATION
, ®
);
194 ctrl
->value
= (s32
)reg
;
196 case INDYCAM_CONTROL_BLUE_SATURATION
:
197 ret
= indycam_read_reg(sd
,
198 INDYCAM_REG_BLUE_SATURATION
, ®
);
201 ctrl
->value
= (s32
)reg
;
204 if (camera
->version
== CAMERA_VERSION_MOOSE
) {
205 ret
= indycam_read_reg(sd
,
206 INDYCAM_REG_GAMMA
, ®
);
209 ctrl
->value
= (s32
)reg
;
211 ctrl
->value
= INDYCAM_GAMMA_DEFAULT
;
221 static int indycam_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
223 struct indycam
*camera
= to_indycam(sd
);
228 case V4L2_CID_AUTOGAIN
:
229 case V4L2_CID_AUTO_WHITE_BALANCE
:
230 ret
= indycam_read_reg(sd
, INDYCAM_REG_CONTROL
, ®
);
234 if (ctrl
->id
== V4L2_CID_AUTOGAIN
) {
236 reg
|= INDYCAM_CONTROL_AGCENA
;
238 reg
&= ~INDYCAM_CONTROL_AGCENA
;
241 reg
|= INDYCAM_CONTROL_AWBCTL
;
243 reg
&= ~INDYCAM_CONTROL_AWBCTL
;
246 ret
= indycam_write_reg(sd
, INDYCAM_REG_CONTROL
, reg
);
248 case V4L2_CID_EXPOSURE
:
249 reg
= (ctrl
->value
== 0xff) ? 0x00 : (ctrl
->value
+ 1);
250 ret
= indycam_write_reg(sd
, INDYCAM_REG_SHUTTER
, reg
);
253 ret
= indycam_write_reg(sd
, INDYCAM_REG_GAIN
, ctrl
->value
);
255 case V4L2_CID_RED_BALANCE
:
256 ret
= indycam_write_reg(sd
, INDYCAM_REG_RED_BALANCE
,
259 case V4L2_CID_BLUE_BALANCE
:
260 ret
= indycam_write_reg(sd
, INDYCAM_REG_BLUE_BALANCE
,
263 case INDYCAM_CONTROL_RED_SATURATION
:
264 ret
= indycam_write_reg(sd
, INDYCAM_REG_RED_SATURATION
,
267 case INDYCAM_CONTROL_BLUE_SATURATION
:
268 ret
= indycam_write_reg(sd
, INDYCAM_REG_BLUE_SATURATION
,
272 if (camera
->version
== CAMERA_VERSION_MOOSE
) {
273 ret
= indycam_write_reg(sd
, INDYCAM_REG_GAMMA
,
286 static int indycam_g_chip_ident(struct v4l2_subdev
*sd
,
287 struct v4l2_dbg_chip_ident
*chip
)
289 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
290 struct indycam
*camera
= to_indycam(sd
);
292 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_INDYCAM
,
296 /* ----------------------------------------------------------------------- */
298 static const struct v4l2_subdev_core_ops indycam_core_ops
= {
299 .g_chip_ident
= indycam_g_chip_ident
,
300 .g_ctrl
= indycam_g_ctrl
,
301 .s_ctrl
= indycam_s_ctrl
,
304 static const struct v4l2_subdev_ops indycam_ops
= {
305 .core
= &indycam_core_ops
,
308 static int indycam_probe(struct i2c_client
*client
,
309 const struct i2c_device_id
*id
)
312 struct indycam
*camera
;
313 struct v4l2_subdev
*sd
;
315 v4l_info(client
, "chip found @ 0x%x (%s)\n",
316 client
->addr
<< 1, client
->adapter
->name
);
318 camera
= kzalloc(sizeof(struct indycam
), GFP_KERNEL
);
323 v4l2_i2c_subdev_init(sd
, client
, &indycam_ops
);
325 camera
->version
= i2c_smbus_read_byte_data(client
,
326 INDYCAM_REG_VERSION
);
327 if (camera
->version
!= CAMERA_VERSION_INDY
&&
328 camera
->version
!= CAMERA_VERSION_MOOSE
) {
333 printk(KERN_INFO
"IndyCam v%d.%d detected\n",
334 INDYCAM_VERSION_MAJOR(camera
->version
),
335 INDYCAM_VERSION_MINOR(camera
->version
));
340 err
= indycam_write_block(sd
, 0, sizeof(initseq
), (u8
*)&initseq
);
342 printk(KERN_ERR
"IndyCam initialization failed\n");
350 err
= indycam_write_reg(sd
, INDYCAM_REG_CONTROL
,
351 INDYCAM_CONTROL_AGCENA
| INDYCAM_CONTROL_AWBCTL
);
353 printk(KERN_ERR
"IndyCam: White balancing camera failed\n");
360 printk(KERN_INFO
"IndyCam initialized\n");
365 static int indycam_remove(struct i2c_client
*client
)
367 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
369 v4l2_device_unregister_subdev(sd
);
370 kfree(to_indycam(sd
));
374 static const struct i2c_device_id indycam_id
[] = {
378 MODULE_DEVICE_TABLE(i2c
, indycam_id
);
380 static struct i2c_driver indycam_driver
= {
382 .owner
= THIS_MODULE
,
385 .probe
= indycam_probe
,
386 .remove
= indycam_remove
,
387 .id_table
= indycam_id
,
390 module_i2c_driver(indycam_driver
);