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>
27 #include <media/v4l2-i2c-drv-legacy.h>
31 #define INDYCAM_MODULE_VERSION "0.0.5"
33 MODULE_DESCRIPTION("SGI IndyCam driver");
34 MODULE_VERSION(INDYCAM_MODULE_VERSION);
35 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
36 MODULE_LICENSE("GPL");
38 static unsigned short normal_i2c[] = { 0x56 >> 1, I2C_CLIENT_END };
42 // #define INDYCAM_DEBUG
45 #define dprintk(x...) printk("IndyCam: " x);
46 #define indycam_regdump(client) indycam_regdump_debug(client)
49 #define indycam_regdump(client)
53 struct v4l2_subdev sd;
57 static inline struct indycam *to_indycam(struct v4l2_subdev *sd)
59 return container_of(sd, struct indycam, sd);
62 static const u8 initseq[] = {
63 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
64 INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
65 INDYCAM_GAIN_DEFAULT, /* INDYCAM_GAIN */
66 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
67 INDYCAM_RED_BALANCE_DEFAULT, /* INDYCAM_RED_BALANCE */
68 INDYCAM_BLUE_BALANCE_DEFAULT, /* INDYCAM_BLUE_BALANCE */
69 INDYCAM_RED_SATURATION_DEFAULT, /* INDYCAM_RED_SATURATION */
70 INDYCAM_BLUE_SATURATION_DEFAULT,/* INDYCAM_BLUE_SATURATION */
73 /* IndyCam register handling */
75 static int indycam_read_reg(struct v4l2_subdev *sd, u8 reg, u8 *value)
77 struct i2c_client *client = v4l2_get_subdevdata(sd);
80 if (reg == INDYCAM_REG_RESET) {
81 dprintk("indycam_read_reg(): "
82 "skipping write-only register %d\n", reg);
87 ret = i2c_smbus_read_byte_data(client, reg);
90 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
91 "register = 0x%02x\n", reg);
100 static int indycam_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
102 struct i2c_client *client = v4l2_get_subdevdata(sd);
105 if (reg == INDYCAM_REG_BRIGHTNESS || reg == INDYCAM_REG_VERSION) {
106 dprintk("indycam_write_reg(): "
107 "skipping read-only register %d\n", reg);
111 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
112 err = i2c_smbus_write_byte_data(client, reg, value);
115 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
116 "register = 0x%02x, value = 0x%02x\n", reg, value);
121 static int indycam_write_block(struct v4l2_subdev *sd, u8 reg,
126 for (i = 0; i < length; i++) {
127 err = indycam_write_reg(sd, reg + i, data[i]);
135 /* Helper functions */
138 static void indycam_regdump_debug(struct v4l2_subdev *sd)
143 for (i = 0; i < 9; i++) {
144 indycam_read_reg(sd, i, &val);
145 dprintk("Reg %d = 0x%02x\n", i, val);
150 static int indycam_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
152 struct indycam *camera = to_indycam(sd);
157 case V4L2_CID_AUTOGAIN:
158 case V4L2_CID_AUTO_WHITE_BALANCE:
159 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, ®);
162 if (ctrl->id == V4L2_CID_AUTOGAIN)
163 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
166 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
169 case V4L2_CID_EXPOSURE:
170 ret = indycam_read_reg(sd, INDYCAM_REG_SHUTTER, ®);
173 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
176 ret = indycam_read_reg(sd, INDYCAM_REG_GAIN, ®);
179 ctrl->value = (s32)reg;
181 case V4L2_CID_RED_BALANCE:
182 ret = indycam_read_reg(sd, INDYCAM_REG_RED_BALANCE, ®);
185 ctrl->value = (s32)reg;
187 case V4L2_CID_BLUE_BALANCE:
188 ret = indycam_read_reg(sd, INDYCAM_REG_BLUE_BALANCE, ®);
191 ctrl->value = (s32)reg;
193 case INDYCAM_CONTROL_RED_SATURATION:
194 ret = indycam_read_reg(sd,
195 INDYCAM_REG_RED_SATURATION, ®);
198 ctrl->value = (s32)reg;
200 case INDYCAM_CONTROL_BLUE_SATURATION:
201 ret = indycam_read_reg(sd,
202 INDYCAM_REG_BLUE_SATURATION, ®);
205 ctrl->value = (s32)reg;
208 if (camera->version == CAMERA_VERSION_MOOSE) {
209 ret = indycam_read_reg(sd,
210 INDYCAM_REG_GAMMA, ®);
213 ctrl->value = (s32)reg;
215 ctrl->value = INDYCAM_GAMMA_DEFAULT;
225 static int indycam_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
227 struct indycam *camera = to_indycam(sd);
232 case V4L2_CID_AUTOGAIN:
233 case V4L2_CID_AUTO_WHITE_BALANCE:
234 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, ®);
238 if (ctrl->id == V4L2_CID_AUTOGAIN) {
240 reg |= INDYCAM_CONTROL_AGCENA;
242 reg &= ~INDYCAM_CONTROL_AGCENA;
245 reg |= INDYCAM_CONTROL_AWBCTL;
247 reg &= ~INDYCAM_CONTROL_AWBCTL;
250 ret = indycam_write_reg(sd, INDYCAM_REG_CONTROL, reg);
252 case V4L2_CID_EXPOSURE:
253 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
254 ret = indycam_write_reg(sd, INDYCAM_REG_SHUTTER, reg);
257 ret = indycam_write_reg(sd, INDYCAM_REG_GAIN, ctrl->value);
259 case V4L2_CID_RED_BALANCE:
260 ret = indycam_write_reg(sd, INDYCAM_REG_RED_BALANCE,
263 case V4L2_CID_BLUE_BALANCE:
264 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_BALANCE,
267 case INDYCAM_CONTROL_RED_SATURATION:
268 ret = indycam_write_reg(sd, INDYCAM_REG_RED_SATURATION,
271 case INDYCAM_CONTROL_BLUE_SATURATION:
272 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_SATURATION,
276 if (camera->version == CAMERA_VERSION_MOOSE) {
277 ret = indycam_write_reg(sd, INDYCAM_REG_GAMMA,
290 static int indycam_g_chip_ident(struct v4l2_subdev *sd,
291 struct v4l2_dbg_chip_ident *chip)
293 struct i2c_client *client = v4l2_get_subdevdata(sd);
294 struct indycam *camera = to_indycam(sd);
296 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_INDYCAM,
300 static int indycam_command(struct i2c_client *client, unsigned cmd, void *arg)
302 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
305 /* ----------------------------------------------------------------------- */
307 static const struct v4l2_subdev_core_ops indycam_core_ops = {
308 .g_chip_ident = indycam_g_chip_ident,
309 .g_ctrl = indycam_g_ctrl,
310 .s_ctrl = indycam_s_ctrl,
313 static const struct v4l2_subdev_ops indycam_ops = {
314 .core = &indycam_core_ops,
317 static int indycam_probe(struct i2c_client *client,
318 const struct i2c_device_id *id)
321 struct indycam *camera;
322 struct v4l2_subdev *sd;
324 v4l_info(client, "chip found @ 0x%x (%s)\n",
325 client->addr << 1, client->adapter->name);
327 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
332 v4l2_i2c_subdev_init(sd, client, &indycam_ops);
334 camera->version = i2c_smbus_read_byte_data(client,
335 INDYCAM_REG_VERSION);
336 if (camera->version != CAMERA_VERSION_INDY &&
337 camera->version != CAMERA_VERSION_MOOSE) {
342 printk(KERN_INFO "IndyCam v%d.%d detected\n",
343 INDYCAM_VERSION_MAJOR(camera->version),
344 INDYCAM_VERSION_MINOR(camera->version));
349 err = indycam_write_block(sd, 0, sizeof(initseq), (u8 *)&initseq);
351 printk(KERN_ERR "IndyCam initialization failed\n");
359 err = indycam_write_reg(sd, INDYCAM_REG_CONTROL,
360 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
362 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
369 printk(KERN_INFO "IndyCam initialized\n");
374 static int indycam_remove(struct i2c_client *client)
376 struct v4l2_subdev *sd = i2c_get_clientdata(client);
378 v4l2_device_unregister_subdev(sd);
379 kfree(to_indycam(sd));
383 static int indycam_legacy_probe(struct i2c_adapter *adapter)
385 return adapter->id == I2C_HW_SGI_VINO;
388 static const struct i2c_device_id indycam_id[] = {
392 MODULE_DEVICE_TABLE(i2c, indycam_id);
394 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
396 .driverid = I2C_DRIVERID_INDYCAM,
397 .command = indycam_command,
398 .probe = indycam_probe,
399 .remove = indycam_remove,
400 .legacy_probe = indycam_legacy_probe,
401 .id_table = indycam_id,