blob: 54099e303c8d402b8e0bbdc6dd4ccbdffe2ac0ee [file] [log] [blame]
Ralf Baechled203a7e2005-09-06 15:19:37 -07001/*
2 * indycam.c - Silicon Graphics IndyCam digital camera driver
3 *
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
6 *
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.
10 */
11
Ralf Baechled203a7e2005-09-06 15:19:37 -070012#include <linux/delay.h>
13#include <linux/errno.h>
14#include <linux/fs.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000015#include <linux/init.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070016#include <linux/kernel.h>
17#include <linux/major.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000018#include <linux/module.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070019#include <linux/mm.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000020#include <linux/slab.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070021
Ralf Baechled203a7e2005-09-06 15:19:37 -070022/* IndyCam decodes stream of photons into digital image representation ;-) */
Hans Verkuilcf4e9482009-02-27 09:05:10 -030023#include <linux/videodev2.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070024#include <linux/i2c.h>
Hans Verkuilcf4e9482009-02-27 09:05:10 -030025#include <media/v4l2-common.h>
26#include <media/v4l2-i2c-drv-legacy.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070027
28#include "indycam.h"
29
Ladislav Michla637a112005-09-01 15:07:34 +000030#define INDYCAM_MODULE_VERSION "0.0.5"
Ralf Baechled203a7e2005-09-06 15:19:37 -070031
32MODULE_DESCRIPTION("SGI IndyCam driver");
33MODULE_VERSION(INDYCAM_MODULE_VERSION);
34MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
35MODULE_LICENSE("GPL");
36
Hans Verkuilcf4e9482009-02-27 09:05:10 -030037static unsigned short normal_i2c[] = { 0x56 >> 1, I2C_CLIENT_END };
38
39I2C_CLIENT_INSMOD;
40
Ladislav Michla637a112005-09-01 15:07:34 +000041// #define INDYCAM_DEBUG
42
Ralf Baechled203a7e2005-09-06 15:19:37 -070043#ifdef INDYCAM_DEBUG
44#define dprintk(x...) printk("IndyCam: " x);
45#define indycam_regdump(client) indycam_regdump_debug(client)
46#else
47#define dprintk(x...)
48#define indycam_regdump(client)
49#endif
50
Ralf Baechled203a7e2005-09-06 15:19:37 -070051struct indycam {
52 struct i2c_client *client;
Ladislav Michla637a112005-09-01 15:07:34 +000053 u8 version;
Ralf Baechled203a7e2005-09-06 15:19:37 -070054};
55
Ladislav Michla637a112005-09-01 15:07:34 +000056static const u8 initseq[] = {
Ralf Baechled203a7e2005-09-06 15:19:37 -070057 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
Ladislav Michla637a112005-09-01 15:07:34 +000058 INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
Ralf Baechled203a7e2005-09-06 15:19:37 -070059 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 */
65};
66
67/* IndyCam register handling */
68
Ladislav Michla637a112005-09-01 15:07:34 +000069static int indycam_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Ralf Baechled203a7e2005-09-06 15:19:37 -070070{
71 int ret;
72
Ladislav Michla637a112005-09-01 15:07:34 +000073 if (reg == INDYCAM_REG_RESET) {
Ralf Baechled203a7e2005-09-06 15:19:37 -070074 dprintk("indycam_read_reg(): "
75 "skipping write-only register %d\n", reg);
76 *value = 0;
77 return 0;
78 }
79
80 ret = i2c_smbus_read_byte_data(client, reg);
Ladislav Michla637a112005-09-01 15:07:34 +000081
Ralf Baechled203a7e2005-09-06 15:19:37 -070082 if (ret < 0) {
83 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
84 "register = 0x%02x\n", reg);
85 return ret;
86 }
87
Ladislav Michla637a112005-09-01 15:07:34 +000088 *value = (u8)ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -070089
90 return 0;
91}
92
Ladislav Michla637a112005-09-01 15:07:34 +000093static int indycam_write_reg(struct i2c_client *client, u8 reg, u8 value)
Ralf Baechled203a7e2005-09-06 15:19:37 -070094{
95 int err;
96
Ladislav Michla637a112005-09-01 15:07:34 +000097 if ((reg == INDYCAM_REG_BRIGHTNESS)
98 || (reg == INDYCAM_REG_VERSION)) {
Ralf Baechled203a7e2005-09-06 15:19:37 -070099 dprintk("indycam_write_reg(): "
100 "skipping read-only register %d\n", reg);
101 return 0;
102 }
103
104 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
105 err = i2c_smbus_write_byte_data(client, reg, value);
Ladislav Michla637a112005-09-01 15:07:34 +0000106
Ralf Baechled203a7e2005-09-06 15:19:37 -0700107 if (err) {
108 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
109 "register = 0x%02x, value = 0x%02x\n", reg, value);
110 }
111 return err;
112}
113
Ladislav Michla637a112005-09-01 15:07:34 +0000114static int indycam_write_block(struct i2c_client *client, u8 reg,
115 u8 length, u8 *data)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700116{
Ladislav Michla637a112005-09-01 15:07:34 +0000117 int i, err;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700118
Ladislav Michla637a112005-09-01 15:07:34 +0000119 for (i = 0; i < length; i++) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700120 err = indycam_write_reg(client, reg + i, data[i]);
121 if (err)
122 return err;
123 }
124
125 return 0;
126}
127
128/* Helper functions */
129
130#ifdef INDYCAM_DEBUG
131static void indycam_regdump_debug(struct i2c_client *client)
132{
133 int i;
Ladislav Michla637a112005-09-01 15:07:34 +0000134 u8 val;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700135
136 for (i = 0; i < 9; i++) {
137 indycam_read_reg(client, i, &val);
138 dprintk("Reg %d = 0x%02x\n", i, val);
139 }
140}
141#endif
142
Ladislav Michla637a112005-09-01 15:07:34 +0000143static int indycam_get_control(struct i2c_client *client,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300144 struct v4l2_control *ctrl)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700145{
Ladislav Michla637a112005-09-01 15:07:34 +0000146 struct indycam *camera = i2c_get_clientdata(client);
147 u8 reg;
148 int ret = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700149
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300150 switch (ctrl->id) {
151 case V4L2_CID_AUTOGAIN:
152 case V4L2_CID_AUTO_WHITE_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000153 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
154 if (ret)
155 return -EIO;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300156 if (ctrl->id == V4L2_CID_AUTOGAIN)
Ladislav Michla637a112005-09-01 15:07:34 +0000157 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
158 ? 1 : 0;
159 else
160 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
161 ? 1 : 0;
162 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300163 case V4L2_CID_EXPOSURE:
Ladislav Michla637a112005-09-01 15:07:34 +0000164 ret = indycam_read_reg(client, INDYCAM_REG_SHUTTER, &reg);
165 if (ret)
166 return -EIO;
167 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
168 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300169 case V4L2_CID_GAIN:
Ladislav Michla637a112005-09-01 15:07:34 +0000170 ret = indycam_read_reg(client, INDYCAM_REG_GAIN, &reg);
171 if (ret)
172 return -EIO;
173 ctrl->value = (s32)reg;
174 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300175 case V4L2_CID_RED_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000176 ret = indycam_read_reg(client, INDYCAM_REG_RED_BALANCE, &reg);
177 if (ret)
178 return -EIO;
179 ctrl->value = (s32)reg;
180 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300181 case V4L2_CID_BLUE_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000182 ret = indycam_read_reg(client, INDYCAM_REG_BLUE_BALANCE, &reg);
183 if (ret)
184 return -EIO;
185 ctrl->value = (s32)reg;
186 break;
187 case INDYCAM_CONTROL_RED_SATURATION:
188 ret = indycam_read_reg(client,
189 INDYCAM_REG_RED_SATURATION, &reg);
190 if (ret)
191 return -EIO;
192 ctrl->value = (s32)reg;
193 break;
194 case INDYCAM_CONTROL_BLUE_SATURATION:
195 ret = indycam_read_reg(client,
196 INDYCAM_REG_BLUE_SATURATION, &reg);
197 if (ret)
198 return -EIO;
199 ctrl->value = (s32)reg;
200 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300201 case V4L2_CID_GAMMA:
Ladislav Michla637a112005-09-01 15:07:34 +0000202 if (camera->version == CAMERA_VERSION_MOOSE) {
203 ret = indycam_read_reg(client,
204 INDYCAM_REG_GAMMA, &reg);
205 if (ret)
206 return -EIO;
207 ctrl->value = (s32)reg;
208 } else {
209 ctrl->value = INDYCAM_GAMMA_DEFAULT;
210 }
211 break;
212 default:
213 ret = -EINVAL;
214 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700215
Ladislav Michla637a112005-09-01 15:07:34 +0000216 return ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700217}
218
Ladislav Michla637a112005-09-01 15:07:34 +0000219static int indycam_set_control(struct i2c_client *client,
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300220 struct v4l2_control *ctrl)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700221{
Ladislav Michla637a112005-09-01 15:07:34 +0000222 struct indycam *camera = i2c_get_clientdata(client);
223 u8 reg;
224 int ret = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700225
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300226 switch (ctrl->id) {
227 case V4L2_CID_AUTOGAIN:
228 case V4L2_CID_AUTO_WHITE_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000229 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
230 if (ret)
231 break;
232
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300233 if (ctrl->id == V4L2_CID_AUTOGAIN) {
Ladislav Michla637a112005-09-01 15:07:34 +0000234 if (ctrl->value)
235 reg |= INDYCAM_CONTROL_AGCENA;
236 else
237 reg &= ~INDYCAM_CONTROL_AGCENA;
238 } else {
239 if (ctrl->value)
240 reg |= INDYCAM_CONTROL_AWBCTL;
241 else
242 reg &= ~INDYCAM_CONTROL_AWBCTL;
243 }
244
245 ret = indycam_write_reg(client, INDYCAM_REG_CONTROL, reg);
246 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300247 case V4L2_CID_EXPOSURE:
Ladislav Michla637a112005-09-01 15:07:34 +0000248 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
249 ret = indycam_write_reg(client, INDYCAM_REG_SHUTTER, reg);
250 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300251 case V4L2_CID_GAIN:
Ladislav Michla637a112005-09-01 15:07:34 +0000252 ret = indycam_write_reg(client, INDYCAM_REG_GAIN, ctrl->value);
253 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300254 case V4L2_CID_RED_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000255 ret = indycam_write_reg(client, INDYCAM_REG_RED_BALANCE,
256 ctrl->value);
257 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300258 case V4L2_CID_BLUE_BALANCE:
Ladislav Michla637a112005-09-01 15:07:34 +0000259 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_BALANCE,
260 ctrl->value);
261 break;
262 case INDYCAM_CONTROL_RED_SATURATION:
263 ret = indycam_write_reg(client, INDYCAM_REG_RED_SATURATION,
264 ctrl->value);
265 break;
266 case INDYCAM_CONTROL_BLUE_SATURATION:
267 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_SATURATION,
268 ctrl->value);
269 break;
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300270 case V4L2_CID_GAMMA:
Ladislav Michla637a112005-09-01 15:07:34 +0000271 if (camera->version == CAMERA_VERSION_MOOSE) {
272 ret = indycam_write_reg(client, INDYCAM_REG_GAMMA,
273 ctrl->value);
274 }
275 break;
276 default:
277 ret = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700278 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700279
Ladislav Michla637a112005-09-01 15:07:34 +0000280 return ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700281}
282
283/* I2C-interface */
284
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300285static int indycam_command(struct i2c_client *client, unsigned int cmd,
286 void *arg)
287{
288 /* The old video_decoder interface just isn't enough,
289 * so we'll use some custom commands. */
290 switch (cmd) {
291 case VIDIOC_G_CTRL:
292 return indycam_get_control(client, arg);
293
294 case VIDIOC_S_CTRL:
295 return indycam_set_control(client, arg);
296
297 default:
298 return -EINVAL;
299 }
300
301 return 0;
302}
303
304static int indycam_probe(struct i2c_client *client,
305 const struct i2c_device_id *id)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700306{
307 int err = 0;
308 struct indycam *camera;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700309
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300310 v4l_info(client, "chip found @ 0x%x (%s)\n",
311 client->addr << 1, client->adapter->name);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700312
Panagiotis Issaris74081872006-01-11 19:40:56 -0200313 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300314 if (!camera)
315 return -ENOMEM;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700316
Ralf Baechled203a7e2005-09-06 15:19:37 -0700317 i2c_set_clientdata(client, camera);
318
319 camera->client = client;
320
Ladislav Michla637a112005-09-01 15:07:34 +0000321 camera->version = i2c_smbus_read_byte_data(client,
322 INDYCAM_REG_VERSION);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700323 if (camera->version != CAMERA_VERSION_INDY &&
324 camera->version != CAMERA_VERSION_MOOSE) {
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300325 kfree(camera);
326 return -ENODEV;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700327 }
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300328
Ralf Baechled203a7e2005-09-06 15:19:37 -0700329 printk(KERN_INFO "IndyCam v%d.%d detected\n",
330 INDYCAM_VERSION_MAJOR(camera->version),
331 INDYCAM_VERSION_MINOR(camera->version));
332
333 indycam_regdump(client);
334
335 // initialize
Ladislav Michla637a112005-09-01 15:07:34 +0000336 err = indycam_write_block(client, 0, sizeof(initseq), (u8 *)&initseq);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700337 if (err) {
Joe Perchesc84e6032008-02-03 17:18:59 +0200338 printk(KERN_ERR "IndyCam initialization failed\n");
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300339 kfree(camera);
340 return -EIO;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700341 }
342
343 indycam_regdump(client);
344
345 // white balance
Ladislav Michla637a112005-09-01 15:07:34 +0000346 err = indycam_write_reg(client, INDYCAM_REG_CONTROL,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700347 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
348 if (err) {
Ladislav Michla637a112005-09-01 15:07:34 +0000349 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300350 kfree(camera);
351 return -EIO;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700352 }
353
354 indycam_regdump(client);
355
356 printk(KERN_INFO "IndyCam initialized\n");
357
358 return 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700359}
360
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300361static int indycam_remove(struct i2c_client *client)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700362{
363 struct indycam *camera = i2c_get_clientdata(client);
364
Ralf Baechled203a7e2005-09-06 15:19:37 -0700365 kfree(camera);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700366 return 0;
367}
368
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300369static int indycam_legacy_probe(struct i2c_adapter *adapter)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700370{
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300371 return adapter->id == I2C_HW_SGI_VINO;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700372}
373
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300374static const struct i2c_device_id indycam_id[] = {
375 { "indycam", 0 },
376 { }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700377};
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300378MODULE_DEVICE_TABLE(i2c, indycam_id);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700379
Hans Verkuilcf4e9482009-02-27 09:05:10 -0300380static struct v4l2_i2c_driver_data v4l2_i2c_data = {
381 .name = "indycam",
382 .driverid = I2C_DRIVERID_INDYCAM,
383 .command = indycam_command,
384 .probe = indycam_probe,
385 .remove = indycam_remove,
386 .legacy_probe = indycam_legacy_probe,
387 .id_table = indycam_id,
388};