blob: 7420b79e987abe775d81a5135d9d8e2657a227f2 [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>
20#include <linux/sched.h>
Ladislav Michl495515b2005-09-23 10:52:27 +000021#include <linux/slab.h>
Ralf Baechled203a7e2005-09-06 15:19:37 -070022
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>
27
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
Ladislav Michla637a112005-09-01 15:07:34 +000037// #define INDYCAM_DEBUG
38
Ralf Baechled203a7e2005-09-06 15:19:37 -070039#ifdef INDYCAM_DEBUG
40#define dprintk(x...) printk("IndyCam: " x);
41#define indycam_regdump(client) indycam_regdump_debug(client)
42#else
43#define dprintk(x...)
44#define indycam_regdump(client)
45#endif
46
Ralf Baechled203a7e2005-09-06 15:19:37 -070047struct indycam {
48 struct i2c_client *client;
Ladislav Michla637a112005-09-01 15:07:34 +000049 u8 version;
Ralf Baechled203a7e2005-09-06 15:19:37 -070050};
51
52static struct i2c_driver i2c_driver_indycam;
53
Ladislav Michla637a112005-09-01 15:07:34 +000054static const u8 initseq[] = {
Ralf Baechled203a7e2005-09-06 15:19:37 -070055 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
Ladislav Michla637a112005-09-01 15:07:34 +000056 INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
Ralf Baechled203a7e2005-09-06 15:19:37 -070057 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 */
63};
64
65/* IndyCam register handling */
66
Ladislav Michla637a112005-09-01 15:07:34 +000067static int indycam_read_reg(struct i2c_client *client, u8 reg, u8 *value)
Ralf Baechled203a7e2005-09-06 15:19:37 -070068{
69 int ret;
70
Ladislav Michla637a112005-09-01 15:07:34 +000071 if (reg == INDYCAM_REG_RESET) {
Ralf Baechled203a7e2005-09-06 15:19:37 -070072 dprintk("indycam_read_reg(): "
73 "skipping write-only register %d\n", reg);
74 *value = 0;
75 return 0;
76 }
77
78 ret = i2c_smbus_read_byte_data(client, reg);
Ladislav Michla637a112005-09-01 15:07:34 +000079
Ralf Baechled203a7e2005-09-06 15:19:37 -070080 if (ret < 0) {
81 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
82 "register = 0x%02x\n", reg);
83 return ret;
84 }
85
Ladislav Michla637a112005-09-01 15:07:34 +000086 *value = (u8)ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -070087
88 return 0;
89}
90
Ladislav Michla637a112005-09-01 15:07:34 +000091static int indycam_write_reg(struct i2c_client *client, u8 reg, u8 value)
Ralf Baechled203a7e2005-09-06 15:19:37 -070092{
93 int err;
94
Ladislav Michla637a112005-09-01 15:07:34 +000095 if ((reg == INDYCAM_REG_BRIGHTNESS)
96 || (reg == INDYCAM_REG_VERSION)) {
Ralf Baechled203a7e2005-09-06 15:19:37 -070097 dprintk("indycam_write_reg(): "
98 "skipping read-only register %d\n", reg);
99 return 0;
100 }
101
102 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
103 err = i2c_smbus_write_byte_data(client, reg, value);
Ladislav Michla637a112005-09-01 15:07:34 +0000104
Ralf Baechled203a7e2005-09-06 15:19:37 -0700105 if (err) {
106 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
107 "register = 0x%02x, value = 0x%02x\n", reg, value);
108 }
109 return err;
110}
111
Ladislav Michla637a112005-09-01 15:07:34 +0000112static int indycam_write_block(struct i2c_client *client, u8 reg,
113 u8 length, u8 *data)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700114{
Ladislav Michla637a112005-09-01 15:07:34 +0000115 int i, err;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700116
Ladislav Michla637a112005-09-01 15:07:34 +0000117 for (i = 0; i < length; i++) {
Ralf Baechled203a7e2005-09-06 15:19:37 -0700118 err = indycam_write_reg(client, reg + i, data[i]);
119 if (err)
120 return err;
121 }
122
123 return 0;
124}
125
126/* Helper functions */
127
128#ifdef INDYCAM_DEBUG
129static void indycam_regdump_debug(struct i2c_client *client)
130{
131 int i;
Ladislav Michla637a112005-09-01 15:07:34 +0000132 u8 val;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700133
134 for (i = 0; i < 9; i++) {
135 indycam_read_reg(client, i, &val);
136 dprintk("Reg %d = 0x%02x\n", i, val);
137 }
138}
139#endif
140
Ladislav Michla637a112005-09-01 15:07:34 +0000141static int indycam_get_control(struct i2c_client *client,
142 struct indycam_control *ctrl)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700143{
Ladislav Michla637a112005-09-01 15:07:34 +0000144 struct indycam *camera = i2c_get_clientdata(client);
145 u8 reg;
146 int ret = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700147
Ladislav Michla637a112005-09-01 15:07:34 +0000148 switch (ctrl->type) {
149 case INDYCAM_CONTROL_AGC:
150 case INDYCAM_CONTROL_AWB:
151 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
152 if (ret)
153 return -EIO;
154 if (ctrl->type == INDYCAM_CONTROL_AGC)
155 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
156 ? 1 : 0;
157 else
158 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
159 ? 1 : 0;
160 break;
161 case INDYCAM_CONTROL_SHUTTER:
162 ret = indycam_read_reg(client, INDYCAM_REG_SHUTTER, &reg);
163 if (ret)
164 return -EIO;
165 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
166 break;
167 case INDYCAM_CONTROL_GAIN:
168 ret = indycam_read_reg(client, INDYCAM_REG_GAIN, &reg);
169 if (ret)
170 return -EIO;
171 ctrl->value = (s32)reg;
172 break;
173 case INDYCAM_CONTROL_RED_BALANCE:
174 ret = indycam_read_reg(client, INDYCAM_REG_RED_BALANCE, &reg);
175 if (ret)
176 return -EIO;
177 ctrl->value = (s32)reg;
178 break;
179 case INDYCAM_CONTROL_BLUE_BALANCE:
180 ret = indycam_read_reg(client, INDYCAM_REG_BLUE_BALANCE, &reg);
181 if (ret)
182 return -EIO;
183 ctrl->value = (s32)reg;
184 break;
185 case INDYCAM_CONTROL_RED_SATURATION:
186 ret = indycam_read_reg(client,
187 INDYCAM_REG_RED_SATURATION, &reg);
188 if (ret)
189 return -EIO;
190 ctrl->value = (s32)reg;
191 break;
192 case INDYCAM_CONTROL_BLUE_SATURATION:
193 ret = indycam_read_reg(client,
194 INDYCAM_REG_BLUE_SATURATION, &reg);
195 if (ret)
196 return -EIO;
197 ctrl->value = (s32)reg;
198 break;
199 case INDYCAM_CONTROL_GAMMA:
200 if (camera->version == CAMERA_VERSION_MOOSE) {
201 ret = indycam_read_reg(client,
202 INDYCAM_REG_GAMMA, &reg);
203 if (ret)
204 return -EIO;
205 ctrl->value = (s32)reg;
206 } else {
207 ctrl->value = INDYCAM_GAMMA_DEFAULT;
208 }
209 break;
210 default:
211 ret = -EINVAL;
212 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700213
Ladislav Michla637a112005-09-01 15:07:34 +0000214 return ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700215}
216
Ladislav Michla637a112005-09-01 15:07:34 +0000217static int indycam_set_control(struct i2c_client *client,
218 struct indycam_control *ctrl)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700219{
Ladislav Michla637a112005-09-01 15:07:34 +0000220 struct indycam *camera = i2c_get_clientdata(client);
221 u8 reg;
222 int ret = 0;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700223
Ladislav Michla637a112005-09-01 15:07:34 +0000224 switch (ctrl->type) {
225 case INDYCAM_CONTROL_AGC:
226 case INDYCAM_CONTROL_AWB:
227 ret = indycam_read_reg(client, INDYCAM_REG_CONTROL, &reg);
228 if (ret)
229 break;
230
231 if (ctrl->type == INDYCAM_CONTROL_AGC) {
232 if (ctrl->value)
233 reg |= INDYCAM_CONTROL_AGCENA;
234 else
235 reg &= ~INDYCAM_CONTROL_AGCENA;
236 } else {
237 if (ctrl->value)
238 reg |= INDYCAM_CONTROL_AWBCTL;
239 else
240 reg &= ~INDYCAM_CONTROL_AWBCTL;
241 }
242
243 ret = indycam_write_reg(client, INDYCAM_REG_CONTROL, reg);
244 break;
245 case INDYCAM_CONTROL_SHUTTER:
246 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
247 ret = indycam_write_reg(client, INDYCAM_REG_SHUTTER, reg);
248 break;
249 case INDYCAM_CONTROL_GAIN:
250 ret = indycam_write_reg(client, INDYCAM_REG_GAIN, ctrl->value);
251 break;
252 case INDYCAM_CONTROL_RED_BALANCE:
253 ret = indycam_write_reg(client, INDYCAM_REG_RED_BALANCE,
254 ctrl->value);
255 break;
256 case INDYCAM_CONTROL_BLUE_BALANCE:
257 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_BALANCE,
258 ctrl->value);
259 break;
260 case INDYCAM_CONTROL_RED_SATURATION:
261 ret = indycam_write_reg(client, INDYCAM_REG_RED_SATURATION,
262 ctrl->value);
263 break;
264 case INDYCAM_CONTROL_BLUE_SATURATION:
265 ret = indycam_write_reg(client, INDYCAM_REG_BLUE_SATURATION,
266 ctrl->value);
267 break;
268 case INDYCAM_CONTROL_GAMMA:
269 if (camera->version == CAMERA_VERSION_MOOSE) {
270 ret = indycam_write_reg(client, INDYCAM_REG_GAMMA,
271 ctrl->value);
272 }
273 break;
274 default:
275 ret = -EINVAL;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700276 }
Ralf Baechled203a7e2005-09-06 15:19:37 -0700277
Ladislav Michla637a112005-09-01 15:07:34 +0000278 return ret;
Ralf Baechled203a7e2005-09-06 15:19:37 -0700279}
280
281/* I2C-interface */
282
283static int indycam_attach(struct i2c_adapter *adap, int addr, int kind)
284{
285 int err = 0;
286 struct indycam *camera;
287 struct i2c_client *client;
288
289 printk(KERN_INFO "SGI IndyCam driver version %s\n",
290 INDYCAM_MODULE_VERSION);
291
Panagiotis Issaris74081872006-01-11 19:40:56 -0200292 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700293 if (!client)
294 return -ENOMEM;
Panagiotis Issaris74081872006-01-11 19:40:56 -0200295 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700296 if (!camera) {
297 err = -ENOMEM;
298 goto out_free_client;
299 }
300
Ralf Baechled203a7e2005-09-06 15:19:37 -0700301 client->addr = addr;
302 client->adapter = adap;
303 client->driver = &i2c_driver_indycam;
304 client->flags = 0;
305 strcpy(client->name, "IndyCam client");
306 i2c_set_clientdata(client, camera);
307
308 camera->client = client;
309
310 err = i2c_attach_client(client);
311 if (err)
312 goto out_free_camera;
313
Ladislav Michla637a112005-09-01 15:07:34 +0000314 camera->version = i2c_smbus_read_byte_data(client,
315 INDYCAM_REG_VERSION);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700316 if (camera->version != CAMERA_VERSION_INDY &&
317 camera->version != CAMERA_VERSION_MOOSE) {
318 err = -ENODEV;
319 goto out_detach_client;
320 }
321 printk(KERN_INFO "IndyCam v%d.%d detected\n",
322 INDYCAM_VERSION_MAJOR(camera->version),
323 INDYCAM_VERSION_MINOR(camera->version));
324
325 indycam_regdump(client);
326
327 // initialize
Ladislav Michla637a112005-09-01 15:07:34 +0000328 err = indycam_write_block(client, 0, sizeof(initseq), (u8 *)&initseq);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700329 if (err) {
330 printk(KERN_ERR "IndyCam initalization failed\n");
331 err = -EIO;
332 goto out_detach_client;
333 }
334
335 indycam_regdump(client);
336
337 // white balance
Ladislav Michla637a112005-09-01 15:07:34 +0000338 err = indycam_write_reg(client, INDYCAM_REG_CONTROL,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700339 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
340 if (err) {
Ladislav Michla637a112005-09-01 15:07:34 +0000341 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
Ralf Baechled203a7e2005-09-06 15:19:37 -0700342 err = -EIO;
343 goto out_detach_client;
344 }
345
346 indycam_regdump(client);
347
348 printk(KERN_INFO "IndyCam initialized\n");
349
350 return 0;
351
352out_detach_client:
353 i2c_detach_client(client);
354out_free_camera:
355 kfree(camera);
356out_free_client:
357 kfree(client);
358 return err;
359}
360
361static int indycam_probe(struct i2c_adapter *adap)
362{
363 /* Indy specific crap */
Ladislav Michl495515b2005-09-23 10:52:27 +0000364 if (adap->id == I2C_HW_SGI_VINO)
Ralf Baechled203a7e2005-09-06 15:19:37 -0700365 return indycam_attach(adap, INDYCAM_ADDR, 0);
366 /* Feel free to add probe here :-) */
367 return -ENODEV;
368}
369
370static int indycam_detach(struct i2c_client *client)
371{
372 struct indycam *camera = i2c_get_clientdata(client);
373
374 i2c_detach_client(client);
375 kfree(camera);
376 kfree(client);
377 return 0;
378}
379
380static int indycam_command(struct i2c_client *client, unsigned int cmd,
381 void *arg)
382{
383 // struct indycam *camera = i2c_get_clientdata(client);
384
385 /* The old video_decoder interface just isn't enough,
386 * so we'll use some custom commands. */
387 switch (cmd) {
388 case DECODER_GET_CAPABILITIES: {
389 struct video_decoder_capability *cap = arg;
390
391 cap->flags = VIDEO_DECODER_NTSC;
392 cap->inputs = 1;
393 cap->outputs = 1;
394 break;
395 }
396 case DECODER_GET_STATUS: {
397 int *iarg = arg;
398
399 *iarg = DECODER_STATUS_GOOD | DECODER_STATUS_NTSC |
400 DECODER_STATUS_COLOR;
401 break;
402 }
403 case DECODER_SET_NORM: {
404 int *iarg = arg;
405
406 switch (*iarg) {
407 case VIDEO_MODE_NTSC:
408 break;
409 default:
410 return -EINVAL;
411 }
412 break;
413 }
414 case DECODER_SET_INPUT: {
415 int *iarg = arg;
416
417 if (*iarg != 0)
418 return -EINVAL;
419 break;
420 }
421 case DECODER_SET_OUTPUT: {
422 int *iarg = arg;
423
424 if (*iarg != 0)
425 return -EINVAL;
426 break;
427 }
428 case DECODER_ENABLE_OUTPUT: {
429 /* Always enabled */
430 break;
431 }
432 case DECODER_SET_PICTURE: {
433 // struct video_picture *pic = arg;
434 /* TODO: convert values for indycam_set_controls() */
435 break;
436 }
Ladislav Michla637a112005-09-01 15:07:34 +0000437 case DECODER_INDYCAM_GET_CONTROL: {
438 return indycam_get_control(client, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700439 }
Ladislav Michla637a112005-09-01 15:07:34 +0000440 case DECODER_INDYCAM_SET_CONTROL: {
441 return indycam_set_control(client, arg);
Ralf Baechled203a7e2005-09-06 15:19:37 -0700442 }
443 default:
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
450static struct i2c_driver i2c_driver_indycam = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100451 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100452 .name = "indycam",
453 },
Ladislav Michla637a112005-09-01 15:07:34 +0000454 .id = I2C_DRIVERID_INDYCAM,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700455 .attach_adapter = indycam_probe,
Ladislav Michla637a112005-09-01 15:07:34 +0000456 .detach_client = indycam_detach,
457 .command = indycam_command,
Ralf Baechled203a7e2005-09-06 15:19:37 -0700458};
459
460static int __init indycam_init(void)
461{
462 return i2c_add_driver(&i2c_driver_indycam);
463}
464
465static void __exit indycam_exit(void)
466{
467 i2c_del_driver(&i2c_driver_indycam);
468}
469
470module_init(indycam_init);
471module_exit(indycam_exit);