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