PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / platform / indycam.c
blobf1d192bbcb4c42296c6dbb2e3549646403f9f368
1 /*
2 * indycam.c - Silicon Graphics IndyCam digital camera driver
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
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.
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>
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>
27 #include "indycam.h"
29 #define INDYCAM_MODULE_VERSION "0.0.5"
31 MODULE_DESCRIPTION("SGI IndyCam driver");
32 MODULE_VERSION(INDYCAM_MODULE_VERSION);
33 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
34 MODULE_LICENSE("GPL");
37 // #define INDYCAM_DEBUG
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
47 struct indycam {
48 struct v4l2_subdev sd;
49 u8 version;
52 static inline struct indycam *to_indycam(struct v4l2_subdev *sd)
54 return container_of(sd, struct indycam, sd);
57 static const u8 initseq[] = {
58 INDYCAM_CONTROL_AGCENA, /* INDYCAM_CONTROL */
59 INDYCAM_SHUTTER_60, /* INDYCAM_SHUTTER */
60 INDYCAM_GAIN_DEFAULT, /* INDYCAM_GAIN */
61 0x00, /* INDYCAM_BRIGHTNESS (read-only) */
62 INDYCAM_RED_BALANCE_DEFAULT, /* INDYCAM_RED_BALANCE */
63 INDYCAM_BLUE_BALANCE_DEFAULT, /* INDYCAM_BLUE_BALANCE */
64 INDYCAM_RED_SATURATION_DEFAULT, /* INDYCAM_RED_SATURATION */
65 INDYCAM_BLUE_SATURATION_DEFAULT,/* INDYCAM_BLUE_SATURATION */
68 /* IndyCam register handling */
70 static int indycam_read_reg(struct v4l2_subdev *sd, u8 reg, u8 *value)
72 struct i2c_client *client = v4l2_get_subdevdata(sd);
73 int ret;
75 if (reg == INDYCAM_REG_RESET) {
76 dprintk("indycam_read_reg(): "
77 "skipping write-only register %d\n", reg);
78 *value = 0;
79 return 0;
82 ret = i2c_smbus_read_byte_data(client, reg);
84 if (ret < 0) {
85 printk(KERN_ERR "IndyCam: indycam_read_reg(): read failed, "
86 "register = 0x%02x\n", reg);
87 return ret;
90 *value = (u8)ret;
92 return 0;
95 static int indycam_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
97 struct i2c_client *client = v4l2_get_subdevdata(sd);
98 int err;
100 if (reg == INDYCAM_REG_BRIGHTNESS || reg == INDYCAM_REG_VERSION) {
101 dprintk("indycam_write_reg(): "
102 "skipping read-only register %d\n", reg);
103 return 0;
106 dprintk("Writing Reg %d = 0x%02x\n", reg, value);
107 err = i2c_smbus_write_byte_data(client, reg, value);
109 if (err) {
110 printk(KERN_ERR "IndyCam: indycam_write_reg(): write failed, "
111 "register = 0x%02x, value = 0x%02x\n", reg, value);
113 return err;
116 static int indycam_write_block(struct v4l2_subdev *sd, u8 reg,
117 u8 length, u8 *data)
119 int i, err;
121 for (i = 0; i < length; i++) {
122 err = indycam_write_reg(sd, reg + i, data[i]);
123 if (err)
124 return err;
127 return 0;
130 /* Helper functions */
132 #ifdef INDYCAM_DEBUG
133 static void indycam_regdump_debug(struct v4l2_subdev *sd)
135 int i;
136 u8 val;
138 for (i = 0; i < 9; i++) {
139 indycam_read_reg(sd, i, &val);
140 dprintk("Reg %d = 0x%02x\n", i, val);
143 #endif
145 static int indycam_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
147 struct indycam *camera = to_indycam(sd);
148 u8 reg;
149 int ret = 0;
151 switch (ctrl->id) {
152 case V4L2_CID_AUTOGAIN:
153 case V4L2_CID_AUTO_WHITE_BALANCE:
154 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, &reg);
155 if (ret)
156 return -EIO;
157 if (ctrl->id == V4L2_CID_AUTOGAIN)
158 ctrl->value = (reg & INDYCAM_CONTROL_AGCENA)
159 ? 1 : 0;
160 else
161 ctrl->value = (reg & INDYCAM_CONTROL_AWBCTL)
162 ? 1 : 0;
163 break;
164 case V4L2_CID_EXPOSURE:
165 ret = indycam_read_reg(sd, INDYCAM_REG_SHUTTER, &reg);
166 if (ret)
167 return -EIO;
168 ctrl->value = ((s32)reg == 0x00) ? 0xff : ((s32)reg - 1);
169 break;
170 case V4L2_CID_GAIN:
171 ret = indycam_read_reg(sd, INDYCAM_REG_GAIN, &reg);
172 if (ret)
173 return -EIO;
174 ctrl->value = (s32)reg;
175 break;
176 case V4L2_CID_RED_BALANCE:
177 ret = indycam_read_reg(sd, INDYCAM_REG_RED_BALANCE, &reg);
178 if (ret)
179 return -EIO;
180 ctrl->value = (s32)reg;
181 break;
182 case V4L2_CID_BLUE_BALANCE:
183 ret = indycam_read_reg(sd, INDYCAM_REG_BLUE_BALANCE, &reg);
184 if (ret)
185 return -EIO;
186 ctrl->value = (s32)reg;
187 break;
188 case INDYCAM_CONTROL_RED_SATURATION:
189 ret = indycam_read_reg(sd,
190 INDYCAM_REG_RED_SATURATION, &reg);
191 if (ret)
192 return -EIO;
193 ctrl->value = (s32)reg;
194 break;
195 case INDYCAM_CONTROL_BLUE_SATURATION:
196 ret = indycam_read_reg(sd,
197 INDYCAM_REG_BLUE_SATURATION, &reg);
198 if (ret)
199 return -EIO;
200 ctrl->value = (s32)reg;
201 break;
202 case V4L2_CID_GAMMA:
203 if (camera->version == CAMERA_VERSION_MOOSE) {
204 ret = indycam_read_reg(sd,
205 INDYCAM_REG_GAMMA, &reg);
206 if (ret)
207 return -EIO;
208 ctrl->value = (s32)reg;
209 } else {
210 ctrl->value = INDYCAM_GAMMA_DEFAULT;
212 break;
213 default:
214 ret = -EINVAL;
217 return ret;
220 static int indycam_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
222 struct indycam *camera = to_indycam(sd);
223 u8 reg;
224 int ret = 0;
226 switch (ctrl->id) {
227 case V4L2_CID_AUTOGAIN:
228 case V4L2_CID_AUTO_WHITE_BALANCE:
229 ret = indycam_read_reg(sd, INDYCAM_REG_CONTROL, &reg);
230 if (ret)
231 break;
233 if (ctrl->id == V4L2_CID_AUTOGAIN) {
234 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;
245 ret = indycam_write_reg(sd, INDYCAM_REG_CONTROL, reg);
246 break;
247 case V4L2_CID_EXPOSURE:
248 reg = (ctrl->value == 0xff) ? 0x00 : (ctrl->value + 1);
249 ret = indycam_write_reg(sd, INDYCAM_REG_SHUTTER, reg);
250 break;
251 case V4L2_CID_GAIN:
252 ret = indycam_write_reg(sd, INDYCAM_REG_GAIN, ctrl->value);
253 break;
254 case V4L2_CID_RED_BALANCE:
255 ret = indycam_write_reg(sd, INDYCAM_REG_RED_BALANCE,
256 ctrl->value);
257 break;
258 case V4L2_CID_BLUE_BALANCE:
259 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_BALANCE,
260 ctrl->value);
261 break;
262 case INDYCAM_CONTROL_RED_SATURATION:
263 ret = indycam_write_reg(sd, INDYCAM_REG_RED_SATURATION,
264 ctrl->value);
265 break;
266 case INDYCAM_CONTROL_BLUE_SATURATION:
267 ret = indycam_write_reg(sd, INDYCAM_REG_BLUE_SATURATION,
268 ctrl->value);
269 break;
270 case V4L2_CID_GAMMA:
271 if (camera->version == CAMERA_VERSION_MOOSE) {
272 ret = indycam_write_reg(sd, INDYCAM_REG_GAMMA,
273 ctrl->value);
275 break;
276 default:
277 ret = -EINVAL;
280 return ret;
283 /* I2C-interface */
285 /* ----------------------------------------------------------------------- */
287 static const struct v4l2_subdev_core_ops indycam_core_ops = {
288 .g_ctrl = indycam_g_ctrl,
289 .s_ctrl = indycam_s_ctrl,
292 static const struct v4l2_subdev_ops indycam_ops = {
293 .core = &indycam_core_ops,
296 static int indycam_probe(struct i2c_client *client,
297 const struct i2c_device_id *id)
299 int err = 0;
300 struct indycam *camera;
301 struct v4l2_subdev *sd;
303 v4l_info(client, "chip found @ 0x%x (%s)\n",
304 client->addr << 1, client->adapter->name);
306 camera = kzalloc(sizeof(struct indycam), GFP_KERNEL);
307 if (!camera)
308 return -ENOMEM;
310 sd = &camera->sd;
311 v4l2_i2c_subdev_init(sd, client, &indycam_ops);
313 camera->version = i2c_smbus_read_byte_data(client,
314 INDYCAM_REG_VERSION);
315 if (camera->version != CAMERA_VERSION_INDY &&
316 camera->version != CAMERA_VERSION_MOOSE) {
317 kfree(camera);
318 return -ENODEV;
321 printk(KERN_INFO "IndyCam v%d.%d detected\n",
322 INDYCAM_VERSION_MAJOR(camera->version),
323 INDYCAM_VERSION_MINOR(camera->version));
325 indycam_regdump(sd);
327 // initialize
328 err = indycam_write_block(sd, 0, sizeof(initseq), (u8 *)&initseq);
329 if (err) {
330 printk(KERN_ERR "IndyCam initialization failed\n");
331 kfree(camera);
332 return -EIO;
335 indycam_regdump(sd);
337 // white balance
338 err = indycam_write_reg(sd, INDYCAM_REG_CONTROL,
339 INDYCAM_CONTROL_AGCENA | INDYCAM_CONTROL_AWBCTL);
340 if (err) {
341 printk(KERN_ERR "IndyCam: White balancing camera failed\n");
342 kfree(camera);
343 return -EIO;
346 indycam_regdump(sd);
348 printk(KERN_INFO "IndyCam initialized\n");
350 return 0;
353 static int indycam_remove(struct i2c_client *client)
355 struct v4l2_subdev *sd = i2c_get_clientdata(client);
357 v4l2_device_unregister_subdev(sd);
358 kfree(to_indycam(sd));
359 return 0;
362 static const struct i2c_device_id indycam_id[] = {
363 { "indycam", 0 },
366 MODULE_DEVICE_TABLE(i2c, indycam_id);
368 static struct i2c_driver indycam_driver = {
369 .driver = {
370 .owner = THIS_MODULE,
371 .name = "indycam",
373 .probe = indycam_probe,
374 .remove = indycam_remove,
375 .id_table = indycam_id,
378 module_i2c_driver(indycam_driver);