V4L/DVB (11357): au0828: remove explicitly set v4l2_device.name and unused au0828_ins...
[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/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/slab.h>
21
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>
26 #include <media/v4l2-chip-ident.h>
27 #include <media/v4l2-i2c-drv.h>
28
29 #include "indycam.h"
30
31 #define INDYCAM_MODULE_VERSION "0.0.5"
32
33 MODULE_DESCRIPTION("SGI IndyCam driver");
34 MODULE_VERSION(INDYCAM_MODULE_VERSION);
35 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
36 MODULE_LICENSE("GPL");
37
38
39 // #define INDYCAM_DEBUG
40
41 #ifdef INDYCAM_DEBUG
42 #define dprintk(x...) printk("IndyCam: " x);
43 #define indycam_regdump(client) indycam_regdump_debug(client)
44 #else
45 #define dprintk(x...)
46 #define indycam_regdump(client)
47 #endif
48
49 struct indycam {
50         struct v4l2_subdev sd;
51         u8 version;
52 };
53
54 static inline struct indycam *to_indycam(struct v4l2_subdev *sd)
55 {
56         return container_of(sd, struct indycam, sd);
57 }
58
59 static const u8 initseq[] = {
60         INDYCAM_CONTROL_AGCENA,         /* INDYCAM_CONTROL */
61         INDYCAM_SHUTTER_60,             /* INDYCAM_SHUTTER */
62         INDYCAM_GAIN_DEFAULT,           /* INDYCAM_GAIN */
63         0x00,                           /* INDYCAM_BRIGHTNESS (read-only) */
64         INDYCAM_RED_BALANCE_DEFAULT,    /* INDYCAM_RED_BALANCE */
65         INDYCAM_BLUE_BALANCE_DEFAULT,   /* INDYCAM_BLUE_BALANCE */
66         INDYCAM_RED_SATURATION_DEFAULT, /* INDYCAM_RED_SATURATION */
67         INDYCAM_BLUE_SATURATION_DEFAULT,/* INDYCAM_BLUE_SATURATION */
68 };
69
70 /* IndyCam register handling */
71
72 static int indycam_read_reg(struct v4l2_subdev *sd, u8 reg, u8 *value)
73 {
74         struct i2c_client *client = v4l2_get_subdevdata(sd);
75         int ret;
76
77         if (reg == INDYCAM_REG_RESET) {
78                 dprintk("indycam_read_reg(): "
79                         "skipping write-only register %d\n", reg);
80                 *value = 0;
81                 return 0;
82         }
83
84         ret = i2c_smbus_read_byte_data(client, reg);
85
86         if (ret < 0) {
87                 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
88                        "register = 0x%02x\n", reg);
89                 return ret;
90         }
91
92         *value = (u8)ret;
93
94         return 0;
95 }
96
97 static int indycam_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
98 {
99         struct i2c_client *client = v4l2_get_subdevdata(sd);
100         int err;
101
102         if (reg == INDYCAM_REG_BRIGHTNESS || reg == INDYCAM_REG_VERSION) {
103                 dprintk("indycam_write_reg(): "
104                         "skipping read-only register %d\n", reg);
105                 return 0;
106         }
107
108         dprintk("Writing Reg %d = 0x%02x\n", reg, value);
109         err = i2c_smbus_write_byte_data(client, reg, value);
110
111         if (err) {
112                 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
113                        "register = 0x%02x, value = 0x%02x\n", reg, value);
114         }
115         return err;
116 }
117
118 static int indycam_write_block(struct v4l2_subdev *sd, u8 reg,
119                                u8 length, u8 *data)
120 {
121         int i, err;
122
123         for (i = 0; i < length; i++) {
124                 err = indycam_write_reg(sd, reg + i, data[i]);
125                 if (err)
126                         return err;
127         }
128
129         return 0;
130 }
131
132 /* Helper functions */
133
134 #ifdef INDYCAM_DEBUG
135 static void indycam_regdump_debug(struct v4l2_subdev *sd)
136 {
137         int i;
138         u8 val;
139
140         for (i = 0; i < 9; i++) {
141                 indycam_read_reg(sd, i, &val);
142                 dprintk("Reg %d = 0x%02x\n", i, val);
143         }
144 }
145 #endif
146
147 static int indycam_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
148 {
149         struct indycam *camera = to_indycam(sd);
150         u8 reg;
151         int ret = 0;
152
153         switch (ctrl->id) {
154         case V4L2_CID_AUTOGAIN:
155         case V4L2_CID_AUTO_WHITE_BALANCE:
156                 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, &reg);
157                 if (ret)
158                         return -EIO;
159                 if (ctrl->id == V4L2_CID_AUTOGAIN)
160                         ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
161                                 ? 1 : 0;
162                 else
163                         ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
164                                 ? 1 : 0;
165                 break;
166         case V4L2_CID_EXPOSURE:
167                 ret = indycam_read_reg(sd, INDYCAM_REG_SHUTTER, &reg);
168                 if (ret)
169                         return -EIO;
170                 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
171                 break;
172         case V4L2_CID_GAIN:
173                 ret = indycam_read_reg(sd, INDYCAM_REG_GAIN, &reg);
174                 if (ret)
175                         return -EIO;
176                 ctrl->value = (s32)reg;
177                 break;
178         case V4L2_CID_RED_BALANCE:
179                 ret = indycam_read_reg(sd, INDYCAM_REG_RED_BALANCE, &reg);
180                 if (ret)
181                         return -EIO;
182                 ctrl->value = (s32)reg;
183                 break;
184         case V4L2_CID_BLUE_BALANCE:
185                 ret = indycam_read_reg(sd, INDYCAM_REG_BLUE_BALANCE, &reg);
186                 if (ret)
187                         return -EIO;
188                 ctrl->value = (s32)reg;
189                 break;
190         case INDYCAM_CONTROL_RED_SATURATION:
191                 ret = indycam_read_reg(sd,
192                                        INDYCAM_REG_RED_SATURATION, &reg);
193                 if (ret)
194                         return -EIO;
195                 ctrl->value = (s32)reg;
196                 break;
197         case INDYCAM_CONTROL_BLUE_SATURATION:
198                 ret = indycam_read_reg(sd,
199                                        INDYCAM_REG_BLUE_SATURATION, &reg);
200                 if (ret)
201                         return -EIO;
202                 ctrl->value = (s32)reg;
203                 break;
204         case V4L2_CID_GAMMA:
205                 if (camera->version == CAMERA_VERSION_MOOSE) {
206                         ret = indycam_read_reg(sd,
207                                                INDYCAM_REG_GAMMA, &reg);
208                         if (ret)
209                                 return -EIO;
210                         ctrl->value = (s32)reg;
211                 } else {
212                         ctrl->value = INDYCAM_GAMMA_DEFAULT;
213                 }
214                 break;
215         default:
216                 ret = -EINVAL;
217         }
218
219         return ret;
220 }
221
222 static int indycam_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
223 {
224         struct indycam *camera = to_indycam(sd);
225         u8 reg;
226         int ret = 0;
227
228         switch (ctrl->id) {
229         case V4L2_CID_AUTOGAIN:
230         case V4L2_CID_AUTO_WHITE_BALANCE:
231                 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, &reg);
232                 if (ret)
233                         break;
234
235                 if (ctrl->id == V4L2_CID_AUTOGAIN) {
236                         if (ctrl->value)
237                                 reg |= INDYCAM_CONTROL_AGCENA;
238                         else
239                                 reg &= ~INDYCAM_CONTROL_AGCENA;
240                 } else {
241                         if (ctrl->value)
242                                 reg |= INDYCAM_CONTROL_AWBCTL;
243                         else
244                                 reg &= ~INDYCAM_CONTROL_AWBCTL;
245                 }
246
247                 ret = indycam_write_reg(sd, INDYCAM_REG_CONTROL, reg);
248                 break;
249         case V4L2_CID_EXPOSURE:
250                 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
251                 ret = indycam_write_reg(sd, INDYCAM_REG_SHUTTER, reg);
252                 break;
253         case V4L2_CID_GAIN:
254                 ret = indycam_write_reg(sd, INDYCAM_REG_GAIN, ctrl->value);
255                 break;
256         case V4L2_CID_RED_BALANCE:
257                 ret = indycam_write_reg(sd, INDYCAM_REG_RED_BALANCE,
258                                         ctrl->value);
259                 break;
260         case V4L2_CID_BLUE_BALANCE:
261                 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_BALANCE,
262                                         ctrl->value);
263                 break;
264         case INDYCAM_CONTROL_RED_SATURATION:
265                 ret = indycam_write_reg(sd, INDYCAM_REG_RED_SATURATION,
266                                         ctrl->value);
267                 break;
268         case INDYCAM_CONTROL_BLUE_SATURATION:
269                 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_SATURATION,
270                                         ctrl->value);
271                 break;
272         case V4L2_CID_GAMMA:
273                 if (camera->version == CAMERA_VERSION_MOOSE) {
274                         ret = indycam_write_reg(sd, INDYCAM_REG_GAMMA,
275                                                 ctrl->value);
276                 }
277                 break;
278         default:
279                 ret = -EINVAL;
280         }
281
282         return ret;
283 }
284
285 /* I2C-interface */
286
287 static int indycam_g_chip_ident(struct v4l2_subdev *sd,
288                 struct v4l2_dbg_chip_ident *chip)
289 {
290         struct i2c_client *client = v4l2_get_subdevdata(sd);
291         struct indycam *camera = to_indycam(sd);
292
293         return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_INDYCAM,
294                        camera->version);
295 }
296
297 /* ----------------------------------------------------------------------- */
298
299 static const struct v4l2_subdev_core_ops indycam_core_ops = {
300         .g_chip_ident = indycam_g_chip_ident,
301         .g_ctrl = indycam_g_ctrl,
302         .s_ctrl = indycam_s_ctrl,
303 };
304
305 static const struct v4l2_subdev_ops indycam_ops = {
306         .core = &indycam_core_ops,
307 };
308
309 static int indycam_probe(struct i2c_client *client,
310                           const struct i2c_device_id *id)
311 {
312         int err = 0;
313         struct indycam *camera;
314         struct v4l2_subdev *sd;
315
316         v4l_info(client, "chip found @ 0x%x (%s)\n",
317                         client->addr << 1, client->adapter->name);
318
319         camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
320         if (!camera)
321                 return -ENOMEM;
322
323         sd = &camera->sd;
324         v4l2_i2c_subdev_init(sd, client, &indycam_ops);
325
326         camera->version = i2c_smbus_read_byte_data(client,
327                                                    INDYCAM_REG_VERSION);
328         if (camera->version != CAMERA_VERSION_INDY &&
329             camera->version != CAMERA_VERSION_MOOSE) {
330                 kfree(camera);
331                 return -ENODEV;
332         }
333
334         printk(KERN_INFO "IndyCam v%d.%d detected\n",
335                INDYCAM_VERSION_MAJOR(camera->version),
336                INDYCAM_VERSION_MINOR(camera->version));
337
338         indycam_regdump(sd);
339
340         // initialize
341         err = indycam_write_block(sd, 0, sizeof(initseq), (u8 *)&initseq);
342         if (err) {
343                 printk(KERN_ERR "IndyCam initialization failed\n");
344                 kfree(camera);
345                 return -EIO;
346         }
347
348         indycam_regdump(sd);
349
350         // white balance
351         err = indycam_write_reg(sd, INDYCAM_REG_CONTROL,
352                           INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
353         if (err) {
354                 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
355                 kfree(camera);
356                 return -EIO;
357         }
358
359         indycam_regdump(sd);
360
361         printk(KERN_INFO "IndyCam initialized\n");
362
363         return 0;
364 }
365
366 static int indycam_remove(struct i2c_client *client)
367 {
368         struct v4l2_subdev *sd = i2c_get_clientdata(client);
369
370         v4l2_device_unregister_subdev(sd);
371         kfree(to_indycam(sd));
372         return 0;
373 }
374
375 static const struct i2c_device_id indycam_id[] = {
376         { "indycam", 0 },
377         { }
378 };
379 MODULE_DEVICE_TABLE(i2c, indycam_id);
380
381 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
382         .name = "indycam",
383         .probe = indycam_probe,
384         .remove = indycam_remove,
385         .id_table = indycam_id,
386 };