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_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");
37 // #define INDYCAM_DEBUG
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 u8 initseq[] = {
55 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
56 INDYCAM_SHUTTER_60, /* 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, u8 reg, u8 *value)
71 if (reg == INDYCAM_REG_RESET) {
72 dprintk("indycam_read_reg(): "
73 "skipping write-only register %d\n", reg);
78 ret = i2c_smbus_read_byte_data(client, reg);
81 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
82 "register = 0x%02x\n", reg);
91 static int indycam_write_reg(struct i2c_client *client, u8 reg, u8 value)
95 if ((reg == INDYCAM_REG_BRIGHTNESS)
96 || (reg == INDYCAM_REG_VERSION)) {
97 dprintk("indycam_write_reg(): "
98 "skipping read-only register %d\n", reg);
102 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
103 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, u8 reg,
117 for (i = 0; i < length; i++) {
118 err = indycam_write_reg(client, reg + i, data[i]);
126 /* Helper functions */
129 static void indycam_regdump_debug(struct i2c_client *client)
134 for (i = 0; i < 9; i++) {
135 indycam_read_reg(client, i, &val);
136 dprintk("Reg %d = 0x%02x\n", i, val);
141 static int indycam_get_control(struct i2c_client *client,
142 struct indycam_control *ctrl)
144 struct indycam *camera = i2c_get_clientdata(client);
148 switch (ctrl->type) {
149 case INDYCAM_CONTROL_AGC:
150 case INDYCAM_CONTROL_AWB:
151 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, ®);
154 if (ctrl->type == INDYCAM_CONTROL_AGC)
155 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
158 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
161 case INDYCAM_CONTROL_SHUTTER:
162 ret = indycam_read_reg(client, INDYCAM_REG_SHUTTER, ®);
165 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
167 case INDYCAM_CONTROL_GAIN:
168 ret = indycam_read_reg(client, INDYCAM_REG_GAIN, ®);
171 ctrl->value = (s32)reg;
173 case INDYCAM_CONTROL_RED_BALANCE:
174 ret = indycam_read_reg(client, INDYCAM_REG_RED_BALANCE, ®);
177 ctrl->value = (s32)reg;
179 case INDYCAM_CONTROL_BLUE_BALANCE:
180 ret = indycam_read_reg(client, INDYCAM_REG_BLUE_BALANCE, ®);
183 ctrl->value = (s32)reg;
185 case INDYCAM_CONTROL_RED_SATURATION:
186 ret = indycam_read_reg(client,
187 INDYCAM_REG_RED_SATURATION, ®);
190 ctrl->value = (s32)reg;
192 case INDYCAM_CONTROL_BLUE_SATURATION:
193 ret = indycam_read_reg(client,
194 INDYCAM_REG_BLUE_SATURATION, ®);
197 ctrl->value = (s32)reg;
199 case INDYCAM_CONTROL_GAMMA:
200 if (camera->version == CAMERA_VERSION_MOOSE) {
201 ret = indycam_read_reg(client,
202 INDYCAM_REG_GAMMA, ®);
205 ctrl->value = (s32)reg;
207 ctrl->value = INDYCAM_GAMMA_DEFAULT;
217 static int indycam_set_control(struct i2c_client *client,
218 struct indycam_control *ctrl)
220 struct indycam *camera = i2c_get_clientdata(client);
224 switch (ctrl->type) {
225 case INDYCAM_CONTROL_AGC:
226 case INDYCAM_CONTROL_AWB:
227 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, ®);
231 if (ctrl->type == INDYCAM_CONTROL_AGC) {
233 reg |= INDYCAM_CONTROL_AGCENA;
235 reg &= ~INDYCAM_CONTROL_AGCENA;
238 reg |= INDYCAM_CONTROL_AWBCTL;
240 reg &= ~INDYCAM_CONTROL_AWBCTL;
243 ret = indycam_write_reg(client, INDYCAM_REG_CONTROL, reg);
245 case INDYCAM_CONTROL_SHUTTER:
246 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
247 ret = indycam_write_reg(client, INDYCAM_REG_SHUTTER, reg);
249 case INDYCAM_CONTROL_GAIN:
250 ret = indycam_write_reg(client, INDYCAM_REG_GAIN, ctrl->value);
252 case INDYCAM_CONTROL_RED_BALANCE:
253 ret = indycam_write_reg(client, INDYCAM_REG_RED_BALANCE,
256 case INDYCAM_CONTROL_BLUE_BALANCE:
257 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_BALANCE,
260 case INDYCAM_CONTROL_RED_SATURATION:
261 ret = indycam_write_reg(client, INDYCAM_REG_RED_SATURATION,
264 case INDYCAM_CONTROL_BLUE_SATURATION:
265 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_SATURATION,
268 case INDYCAM_CONTROL_GAMMA:
269 if (camera->version == CAMERA_VERSION_MOOSE) {
270 ret = indycam_write_reg(client, INDYCAM_REG_GAMMA,
283 static int indycam_attach(struct i2c_adapter *adap, int addr, int kind)
286 struct indycam *camera;
287 struct i2c_client *client;
289 printk(KERN_INFO "SGI IndyCam driver version %s\n",
290 INDYCAM_MODULE_VERSION);
292 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
295 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
298 goto out_free_client;
302 client->adapter = adap;
303 client->driver = &i2c_driver_indycam;
305 strcpy(client->name, "IndyCam client");
306 i2c_set_clientdata(client, camera);
308 camera->client = client;
310 err = i2c_attach_client(client);
312 goto out_free_camera;
314 camera->version = i2c_smbus_read_byte_data(client,
315 INDYCAM_REG_VERSION);
316 if (camera->version != CAMERA_VERSION_INDY &&
317 camera->version != CAMERA_VERSION_MOOSE) {
319 goto out_detach_client;
321 printk(KERN_INFO "IndyCam v%d.%d detected\n",
322 INDYCAM_VERSION_MAJOR(camera->version),
323 INDYCAM_VERSION_MINOR(camera->version));
325 indycam_regdump(client);
328 err = indycam_write_block(client, 0, sizeof(initseq), (u8 *)&initseq);
330 printk(KERN_ERR "IndyCam initalization failed\n");
332 goto out_detach_client;
335 indycam_regdump(client);
338 err = indycam_write_reg(client, INDYCAM_REG_CONTROL,
339 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
341 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
343 goto out_detach_client;
346 indycam_regdump(client);
348 printk(KERN_INFO "IndyCam initialized\n");
353 i2c_detach_client(client);
361 static int indycam_probe(struct i2c_adapter *adap)
363 /* Indy specific crap */
364 if (adap->id == I2C_HW_SGI_VINO)
365 return indycam_attach(adap, INDYCAM_ADDR, 0);
366 /* Feel free to add probe here :-) */
370 static int indycam_detach(struct i2c_client *client)
372 struct indycam *camera = i2c_get_clientdata(client);
374 i2c_detach_client(client);
380 static int indycam_command(struct i2c_client *client, unsigned int cmd,
383 // struct indycam *camera = i2c_get_clientdata(client);
385 /* The old video_decoder interface just isn't enough,
386 * so we'll use some custom commands. */
388 case DECODER_GET_CAPABILITIES: {
389 struct video_decoder_capability *cap = arg;
391 cap->flags = VIDEO_DECODER_NTSC;
396 case DECODER_GET_STATUS: {
399 *iarg = DECODER_STATUS_GOOD | DECODER_STATUS_NTSC |
400 DECODER_STATUS_COLOR;
403 case DECODER_SET_NORM: {
407 case VIDEO_MODE_NTSC:
414 case DECODER_SET_INPUT: {
421 case DECODER_SET_OUTPUT: {
428 case DECODER_ENABLE_OUTPUT: {
432 case DECODER_SET_PICTURE: {
433 // struct video_picture *pic = arg;
434 /* TODO: convert values for indycam_set_controls() */
437 case DECODER_INDYCAM_GET_CONTROL: {
438 return indycam_get_control(client, arg);
440 case DECODER_INDYCAM_SET_CONTROL: {
441 return indycam_set_control(client, arg);
450 static struct i2c_driver i2c_driver_indycam = {
454 .id = I2C_DRIVERID_INDYCAM,
455 .attach_adapter = indycam_probe,
456 .detach_client = indycam_detach,
457 .command = indycam_command,
460 static int __init indycam_init(void)
462 return i2c_add_driver(&i2c_driver_indycam);
465 static void __exit indycam_exit(void)
467 i2c_del_driver(&i2c_driver_indycam);
470 module_init(indycam_init);
471 module_exit(indycam_exit);