2 * indycam.c - Silicon Graphics IndyCam digital camera driver
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>
29 #define INDYCAM_MODULE_VERSION "0.0.5"
31 MODULE_DESCRIPTION("SGI IndyCam driver");
32 MODULE_VERSION(INDYCAM_MODULE_VERSION);
34 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 v4l2_subdev sd;
52 static inline struct indycam *to_indycam(struct v4l2_subdev *sd)
54 return container_of(sd, struct indycam, sd);
57 static const u8 initseq[] = {
58 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
59 INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
60 INDYCAM_GAIN_DEFAULT, /* INDYCAM_GAIN */
61 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
62 INDYCAM_RED_BALANCE_DEFAULT, /* INDYCAM_RED_BALANCE */
63 INDYCAM_BLUE_BALANCE_DEFAULT, /* INDYCAM_BLUE_BALANCE */
64 INDYCAM_RED_SATURATION_DEFAULT, /* INDYCAM_RED_SATURATION */
65 INDYCAM_BLUE_SATURATION_DEFAULT,/* INDYCAM_BLUE_SATURATION */
68 /* IndyCam register handling */
70 static int indycam_read_reg(struct v4l2_subdev *sd, u8 reg, u8 *value)
72 struct i2c_client *client = v4l2_get_subdevdata(sd);
75 if (reg == INDYCAM_REG_RESET) {
76 dprintk("indycam_read_reg(): "
77 "skipping write-only register %d\n", reg);
82 ret = i2c_smbus_read_byte_data(client, reg);
85 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
86 "register = 0x%02x\n", reg);
95 static int indycam_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
97 struct i2c_client *client = v4l2_get_subdevdata(sd);
100 if (reg == INDYCAM_REG_BRIGHTNESS || reg == INDYCAM_REG_VERSION) {
101 dprintk("indycam_write_reg(): "
102 "skipping read-only register %d\n", reg);
106 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
107 err = i2c_smbus_write_byte_data(client, reg, value);
110 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
111 "register = 0x%02x, value = 0x%02x\n", reg, value);
116 static int indycam_write_block(struct v4l2_subdev *sd, u8 reg,
121 for (i = 0; i < length; i++) {
122 err = indycam_write_reg(sd, reg + i, data[i]);
130 /* Helper functions */
133 static void indycam_regdump_debug(struct v4l2_subdev *sd)
138 for (i = 0; i < 9; i++) {
139 indycam_read_reg(sd, i, &val);
140 dprintk("Reg %d = 0x%02x\n", i, val);
145 static int indycam_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
147 struct indycam *camera = to_indycam(sd);
152 case V4L2_CID_AUTOGAIN:
153 case V4L2_CID_AUTO_WHITE_BALANCE:
154 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, ®);
157 if (ctrl->id == V4L2_CID_AUTOGAIN)
158 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
161 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
164 case V4L2_CID_EXPOSURE:
165 ret = indycam_read_reg(sd, INDYCAM_REG_SHUTTER, ®);
168 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
171 ret = indycam_read_reg(sd, INDYCAM_REG_GAIN, ®);
174 ctrl->value = (s32)reg;
176 case V4L2_CID_RED_BALANCE:
177 ret = indycam_read_reg(sd, INDYCAM_REG_RED_BALANCE, ®);
180 ctrl->value = (s32)reg;
182 case V4L2_CID_BLUE_BALANCE:
183 ret = indycam_read_reg(sd, INDYCAM_REG_BLUE_BALANCE, ®);
186 ctrl->value = (s32)reg;
188 case INDYCAM_CONTROL_RED_SATURATION:
189 ret = indycam_read_reg(sd,
190 INDYCAM_REG_RED_SATURATION, ®);
193 ctrl->value = (s32)reg;
195 case INDYCAM_CONTROL_BLUE_SATURATION:
196 ret = indycam_read_reg(sd,
197 INDYCAM_REG_BLUE_SATURATION, ®);
200 ctrl->value = (s32)reg;
203 if (camera->version == CAMERA_VERSION_MOOSE) {
204 ret = indycam_read_reg(sd,
205 INDYCAM_REG_GAMMA, ®);
208 ctrl->value = (s32)reg;
210 ctrl->value = INDYCAM_GAMMA_DEFAULT;
220 static int indycam_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
222 struct indycam *camera = to_indycam(sd);
227 case V4L2_CID_AUTOGAIN:
228 case V4L2_CID_AUTO_WHITE_BALANCE:
229 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, ®);
233 if (ctrl->id == V4L2_CID_AUTOGAIN) {
235 reg |= INDYCAM_CONTROL_AGCENA;
237 reg &= ~INDYCAM_CONTROL_AGCENA;
240 reg |= INDYCAM_CONTROL_AWBCTL;
242 reg &= ~INDYCAM_CONTROL_AWBCTL;
245 ret = indycam_write_reg(sd, INDYCAM_REG_CONTROL, reg);
247 case V4L2_CID_EXPOSURE:
248 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
249 ret = indycam_write_reg(sd, INDYCAM_REG_SHUTTER, reg);
252 ret = indycam_write_reg(sd, INDYCAM_REG_GAIN, ctrl->value);
254 case V4L2_CID_RED_BALANCE:
255 ret = indycam_write_reg(sd, INDYCAM_REG_RED_BALANCE,
258 case V4L2_CID_BLUE_BALANCE:
259 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_BALANCE,
262 case INDYCAM_CONTROL_RED_SATURATION:
263 ret = indycam_write_reg(sd, INDYCAM_REG_RED_SATURATION,
266 case INDYCAM_CONTROL_BLUE_SATURATION:
267 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_SATURATION,
271 if (camera->version == CAMERA_VERSION_MOOSE) {
272 ret = indycam_write_reg(sd, INDYCAM_REG_GAMMA,
285 /* ----------------------------------------------------------------------- */
287 static const struct v4l2_subdev_core_ops indycam_core_ops = {
288 .g_ctrl = indycam_g_ctrl,
289 .s_ctrl = indycam_s_ctrl,
292 static const struct v4l2_subdev_ops indycam_ops = {
293 .core = &indycam_core_ops,
296 static int indycam_probe(struct i2c_client *client,
297 const struct i2c_device_id *id)
300 struct indycam *camera;
301 struct v4l2_subdev *sd;
303 v4l_info(client, "chip found @ 0x%x (%s)\n",
304 client->addr << 1, client->adapter->name);
306 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
311 v4l2_i2c_subdev_init(sd, client, &indycam_ops);
313 camera->version = i2c_smbus_read_byte_data(client,
314 INDYCAM_REG_VERSION);
315 if (camera->version != CAMERA_VERSION_INDY &&
316 camera->version != CAMERA_VERSION_MOOSE) {
321 printk(KERN_INFO "IndyCam v%d.%d detected\n",
322 INDYCAM_VERSION_MAJOR(camera->version),
323 INDYCAM_VERSION_MINOR(camera->version));
328 err = indycam_write_block(sd, 0, sizeof(initseq), (u8 *)&initseq);
330 printk(KERN_ERR "IndyCam initialization failed\n");
338 err = indycam_write_reg(sd, INDYCAM_REG_CONTROL,
339 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
341 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
348 printk(KERN_INFO "IndyCam initialized\n");
353 static int indycam_remove(struct i2c_client *client)
355 struct v4l2_subdev *sd = i2c_get_clientdata(client);
357 v4l2_device_unregister_subdev(sd);
358 kfree(to_indycam(sd));
362 static const struct i2c_device_id indycam_id[] = {
366 MODULE_DEVICE_TABLE(i2c, indycam_id);
368 static struct i2c_driver indycam_driver = {
370 .owner = THIS_MODULE,
373 .probe = indycam_probe,
374 .remove = indycam_remove,
375 .id_table = indycam_id,
378 module_i2c_driver(indycam_driver);