Merge branch 'master'
[deliverable/linux.git] / drivers / media / video / indycam.c
1 /*
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
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/module.h>
19 #include <linux/mm.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22
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
30 //#define INDYCAM_DEBUG
31
32 #define INDYCAM_MODULE_VERSION "0.0.3"
33
34 MODULE_DESCRIPTION("SGI IndyCam driver");
35 MODULE_VERSION(INDYCAM_MODULE_VERSION);
36 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
37 MODULE_LICENSE("GPL");
38
39 #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
47 struct indycam {
48 struct i2c_client *client;
49 int version;
50 };
51
52 static struct i2c_driver i2c_driver_indycam;
53
54 static const unsigned char initseq[] = {
55 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
56 INDYCAM_SHUTTER_DEFAULT, /* 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 */
63 };
64
65 /* IndyCam register handling */
66
67 static int indycam_read_reg(struct i2c_client *client, unsigned char reg,
68 unsigned char *value)
69 {
70 int ret;
71
72 if (reg == INDYCAM_RESET) {
73 dprintk("indycam_read_reg(): "
74 "skipping write-only register %d\n", reg);
75 *value = 0;
76 return 0;
77 }
78
79 ret = i2c_smbus_read_byte_data(client, reg);
80 if (ret < 0) {
81 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
82 "register = 0x%02x\n", reg);
83 return ret;
84 }
85
86 *value = (unsigned char)ret;
87
88 return 0;
89 }
90
91 static int indycam_write_reg(struct i2c_client *client, unsigned char reg,
92 unsigned char value)
93 {
94 int err;
95
96 if ((reg == INDYCAM_BRIGHTNESS)
97 || (reg == INDYCAM_VERSION)) {
98 dprintk("indycam_write_reg(): "
99 "skipping read-only register %d\n", reg);
100 return 0;
101 }
102
103 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
104 err = i2c_smbus_write_byte_data(client, reg, value);
105 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
112 static int indycam_write_block(struct i2c_client *client, unsigned char reg,
113 unsigned char length, unsigned char *data)
114 {
115 unsigned char i;
116 int err;
117
118 for (i = reg; i < length; i++) {
119 err = indycam_write_reg(client, reg + i, data[i]);
120 if (err)
121 return err;
122 }
123
124 return 0;
125 }
126
127 /* Helper functions */
128
129 #ifdef INDYCAM_DEBUG
130 static void indycam_regdump_debug(struct i2c_client *client)
131 {
132 int i;
133 unsigned char val;
134
135 for (i = 0; i < 9; i++) {
136 indycam_read_reg(client, i, &val);
137 dprintk("Reg %d = 0x%02x\n", i, val);
138 }
139 }
140 #endif
141
142 static int indycam_get_controls(struct i2c_client *client,
143 struct indycam_control *ctrl)
144 {
145 unsigned char ctrl_reg;
146
147 indycam_read_reg(client, INDYCAM_CONTROL, &ctrl_reg);
148 ctrl->agc = (ctrl_reg & INDYCAM_CONTROL_AGCENA)
149 ? INDYCAM_VALUE_ENABLED
150 : INDYCAM_VALUE_DISABLED;
151 ctrl->awb = (ctrl_reg & INDYCAM_CONTROL_AWBCTL)
152 ? INDYCAM_VALUE_ENABLED
153 : INDYCAM_VALUE_DISABLED;
154 indycam_read_reg(client, INDYCAM_SHUTTER,
155 (unsigned char *)&ctrl->shutter);
156 indycam_read_reg(client, INDYCAM_GAIN,
157 (unsigned char *)&ctrl->gain);
158 indycam_read_reg(client, INDYCAM_RED_BALANCE,
159 (unsigned char *)&ctrl->red_balance);
160 indycam_read_reg(client, INDYCAM_BLUE_BALANCE,
161 (unsigned char *)&ctrl->blue_balance);
162 indycam_read_reg(client, INDYCAM_RED_SATURATION,
163 (unsigned char *)&ctrl->red_saturation);
164 indycam_read_reg(client, INDYCAM_BLUE_SATURATION,
165 (unsigned char *)&ctrl->blue_saturation);
166 indycam_read_reg(client, INDYCAM_GAMMA,
167 (unsigned char *)&ctrl->gamma);
168
169 return 0;
170 }
171
172 static int indycam_set_controls(struct i2c_client *client,
173 struct indycam_control *ctrl)
174 {
175 unsigned char ctrl_reg;
176
177 indycam_read_reg(client, INDYCAM_CONTROL, &ctrl_reg);
178 if (ctrl->agc != INDYCAM_VALUE_UNCHANGED) {
179 if (ctrl->agc)
180 ctrl_reg |= INDYCAM_CONTROL_AGCENA;
181 else
182 ctrl_reg &= ~INDYCAM_CONTROL_AGCENA;
183 }
184 if (ctrl->awb != INDYCAM_VALUE_UNCHANGED) {
185 if (ctrl->awb)
186 ctrl_reg |= INDYCAM_CONTROL_AWBCTL;
187 else
188 ctrl_reg &= ~INDYCAM_CONTROL_AWBCTL;
189 }
190 indycam_write_reg(client, INDYCAM_CONTROL, ctrl_reg);
191
192 if (ctrl->shutter >= 0)
193 indycam_write_reg(client, INDYCAM_SHUTTER, ctrl->shutter);
194 if (ctrl->gain >= 0)
195 indycam_write_reg(client, INDYCAM_GAIN, ctrl->gain);
196 if (ctrl->red_balance >= 0)
197 indycam_write_reg(client, INDYCAM_RED_BALANCE,
198 ctrl->red_balance);
199 if (ctrl->blue_balance >= 0)
200 indycam_write_reg(client, INDYCAM_BLUE_BALANCE,
201 ctrl->blue_balance);
202 if (ctrl->red_saturation >= 0)
203 indycam_write_reg(client, INDYCAM_RED_SATURATION,
204 ctrl->red_saturation);
205 if (ctrl->blue_saturation >= 0)
206 indycam_write_reg(client, INDYCAM_BLUE_SATURATION,
207 ctrl->blue_saturation);
208 if (ctrl->gamma >= 0)
209 indycam_write_reg(client, INDYCAM_GAMMA, ctrl->gamma);
210
211 return 0;
212 }
213
214 /* I2C-interface */
215
216 static int indycam_attach(struct i2c_adapter *adap, int addr, int kind)
217 {
218 int err = 0;
219 struct indycam *camera;
220 struct i2c_client *client;
221
222 printk(KERN_INFO "SGI IndyCam driver version %s\n",
223 INDYCAM_MODULE_VERSION);
224
225 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
226 if (!client)
227 return -ENOMEM;
228 camera = kmalloc(sizeof(struct indycam), GFP_KERNEL);
229 if (!camera) {
230 err = -ENOMEM;
231 goto out_free_client;
232 }
233
234 memset(client, 0, sizeof(struct i2c_client));
235 memset(camera, 0, sizeof(struct indycam));
236
237 client->addr = addr;
238 client->adapter = adap;
239 client->driver = &i2c_driver_indycam;
240 client->flags = 0;
241 strcpy(client->name, "IndyCam client");
242 i2c_set_clientdata(client, camera);
243
244 camera->client = client;
245
246 err = i2c_attach_client(client);
247 if (err)
248 goto out_free_camera;
249
250 camera->version = i2c_smbus_read_byte_data(client, INDYCAM_VERSION);
251 if (camera->version != CAMERA_VERSION_INDY &&
252 camera->version != CAMERA_VERSION_MOOSE) {
253 err = -ENODEV;
254 goto out_detach_client;
255 }
256 printk(KERN_INFO "IndyCam v%d.%d detected\n",
257 INDYCAM_VERSION_MAJOR(camera->version),
258 INDYCAM_VERSION_MINOR(camera->version));
259
260 indycam_regdump(client);
261
262 // initialize
263 err = indycam_write_block(client, 0, sizeof(initseq),
264 (unsigned char *)&initseq);
265 if (err) {
266 printk(KERN_ERR "IndyCam initalization failed\n");
267 err = -EIO;
268 goto out_detach_client;
269 }
270
271 indycam_regdump(client);
272
273 // white balance
274 err = indycam_write_reg(client, INDYCAM_CONTROL,
275 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
276 if (err) {
277 printk(KERN_ERR "IndyCam white balance "
278 "initialization failed\n");
279 err = -EIO;
280 goto out_detach_client;
281 }
282
283 indycam_regdump(client);
284
285 printk(KERN_INFO "IndyCam initialized\n");
286
287 return 0;
288
289 out_detach_client:
290 i2c_detach_client(client);
291 out_free_camera:
292 kfree(camera);
293 out_free_client:
294 kfree(client);
295 return err;
296 }
297
298 static int indycam_probe(struct i2c_adapter *adap)
299 {
300 /* Indy specific crap */
301 if (adap->id == I2C_HW_SGI_VINO)
302 return indycam_attach(adap, INDYCAM_ADDR, 0);
303 /* Feel free to add probe here :-) */
304 return -ENODEV;
305 }
306
307 static int indycam_detach(struct i2c_client *client)
308 {
309 struct indycam *camera = i2c_get_clientdata(client);
310
311 i2c_detach_client(client);
312 kfree(camera);
313 kfree(client);
314 return 0;
315 }
316
317 static int indycam_command(struct i2c_client *client, unsigned int cmd,
318 void *arg)
319 {
320 // struct indycam *camera = i2c_get_clientdata(client);
321
322 /* The old video_decoder interface just isn't enough,
323 * so we'll use some custom commands. */
324 switch (cmd) {
325 case DECODER_GET_CAPABILITIES: {
326 struct video_decoder_capability *cap = arg;
327
328 cap->flags = VIDEO_DECODER_NTSC;
329 cap->inputs = 1;
330 cap->outputs = 1;
331 break;
332 }
333 case DECODER_GET_STATUS: {
334 int *iarg = arg;
335
336 *iarg = DECODER_STATUS_GOOD | DECODER_STATUS_NTSC |
337 DECODER_STATUS_COLOR;
338 break;
339 }
340 case DECODER_SET_NORM: {
341 int *iarg = arg;
342
343 switch (*iarg) {
344 case VIDEO_MODE_NTSC:
345 break;
346 default:
347 return -EINVAL;
348 }
349 break;
350 }
351 case DECODER_SET_INPUT: {
352 int *iarg = arg;
353
354 if (*iarg != 0)
355 return -EINVAL;
356 break;
357 }
358 case DECODER_SET_OUTPUT: {
359 int *iarg = arg;
360
361 if (*iarg != 0)
362 return -EINVAL;
363 break;
364 }
365 case DECODER_ENABLE_OUTPUT: {
366 /* Always enabled */
367 break;
368 }
369 case DECODER_SET_PICTURE: {
370 // struct video_picture *pic = arg;
371 /* TODO: convert values for indycam_set_controls() */
372 break;
373 }
374 case DECODER_INDYCAM_GET_CONTROLS: {
375 struct indycam_control *ctrl = arg;
376 indycam_get_controls(client, ctrl);
377 }
378 case DECODER_INDYCAM_SET_CONTROLS: {
379 struct indycam_control *ctrl = arg;
380 indycam_set_controls(client, ctrl);
381 }
382 default:
383 return -EINVAL;
384 }
385
386 return 0;
387 }
388
389 static struct i2c_driver i2c_driver_indycam = {
390 .owner = THIS_MODULE,
391 .name = "indycam",
392 .id = I2C_DRIVERID_INDYCAM,
393 .flags = I2C_DF_NOTIFY,
394 .attach_adapter = indycam_probe,
395 .detach_client = indycam_detach,
396 .command = indycam_command,
397 };
398
399 static int __init indycam_init(void)
400 {
401 return i2c_add_driver(&i2c_driver_indycam);
402 }
403
404 static void __exit indycam_exit(void)
405 {
406 i2c_del_driver(&i2c_driver_indycam);
407 }
408
409 module_init(indycam_init);
410 module_exit(indycam_exit);
This page took 0.039436 seconds and 6 git commands to generate.