Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6-block.git] / drivers / media / video / ovcamchip / ovcamchip_core.c
CommitLineData
1da177e4
LT
1/* Shared Code for OmniVision Camera Chip Drivers
2 *
3 * Copyright (c) 2004 Mark McClelland <mark@alpha.dyndns.org>
4 * http://alpha.dyndns.org/ov511/
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
10 */
11
12#define DEBUG
13
14#include <linux/init.h>
15#include <linux/module.h>
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/delay.h>
18#include "ovcamchip_priv.h"
19
20#define DRIVER_VERSION "v2.27 for Linux 2.6"
21#define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org>"
22#define DRIVER_DESC "OV camera chip I2C driver"
23
24#define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
25#define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
26
27#ifdef DEBUG
28int ovcamchip_debug = 0;
29static int debug;
30module_param(debug, int, 0);
31MODULE_PARM_DESC(debug,
32 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=all");
33#endif
34
35/* By default, let bridge driver tell us if chip is monochrome. mono=0
36 * will ignore that and always treat chips as color. mono=1 will force
37 * monochrome mode for all chips. */
38static int mono = -1;
39module_param(mono, int, 0);
40MODULE_PARM_DESC(mono,
41 "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
42
43MODULE_AUTHOR(DRIVER_AUTHOR);
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
47/* Registers common to all chips, that are needed for detection */
48#define GENERIC_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
49#define GENERIC_REG_ID_LOW 0x1D /* manufacturer ID LSB */
50#define GENERIC_REG_COM_I 0x29 /* misc ID bits */
51
1da177e4
LT
52static char *chip_names[NUM_CC_TYPES] = {
53 [CC_UNKNOWN] = "Unknown chip",
54 [CC_OV76BE] = "OV76BE",
55 [CC_OV7610] = "OV7610",
56 [CC_OV7620] = "OV7620",
57 [CC_OV7620AE] = "OV7620AE",
58 [CC_OV6620] = "OV6620",
59 [CC_OV6630] = "OV6630",
60 [CC_OV6630AE] = "OV6630AE",
61 [CC_OV6630AF] = "OV6630AF",
62};
63
64/* Forward declarations */
65static struct i2c_driver driver;
66static struct i2c_client client_template;
67
68/* ----------------------------------------------------------------------- */
69
70int ov_write_regvals(struct i2c_client *c, struct ovcamchip_regvals *rvals)
71{
72 int rc;
73
74 while (rvals->reg != 0xff) {
75 rc = ov_write(c, rvals->reg, rvals->val);
76 if (rc < 0)
77 return rc;
78 rvals++;
79 }
80
81 return 0;
82}
83
84/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
85 * the same position as 1's in "mask" are cleared and set to "value". Bits
86 * that are in the same position as 0's in "mask" are preserved, regardless
87 * of their respective state in "value".
88 */
89int ov_write_mask(struct i2c_client *c,
90 unsigned char reg,
91 unsigned char value,
92 unsigned char mask)
93{
94 int rc;
95 unsigned char oldval, newval;
96
97 if (mask == 0xff) {
98 newval = value;
99 } else {
100 rc = ov_read(c, reg, &oldval);
101 if (rc < 0)
102 return rc;
103
104 oldval &= (~mask); /* Clear the masked bits */
105 value &= mask; /* Enforce mask on value */
106 newval = oldval | value; /* Set the desired bits */
107 }
108
109 return ov_write(c, reg, newval);
110}
111
112/* ----------------------------------------------------------------------- */
113
114/* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
115 */
116static int init_camchip(struct i2c_client *c)
117{
118 int i, success;
119 unsigned char high, low;
120
121 /* Reset the chip */
122 ov_write(c, 0x12, 0x80);
123
124 /* Wait for it to initialize */
125 msleep(150);
126
127 for (i = 0, success = 0; i < I2C_DETECT_RETRIES && !success; i++) {
128 if (ov_read(c, GENERIC_REG_ID_HIGH, &high) >= 0) {
129 if (ov_read(c, GENERIC_REG_ID_LOW, &low) >= 0) {
130 if (high == 0x7F && low == 0xA2) {
131 success = 1;
132 continue;
133 }
134 }
135 }
136
137 /* Reset the chip */
138 ov_write(c, 0x12, 0x80);
139
140 /* Wait for it to initialize */
141 msleep(150);
142
143 /* Dummy read to sync I2C */
144 ov_read(c, 0x00, &low);
145 }
146
147 if (!success)
148 return -EIO;
149
150 PDEBUG(1, "I2C synced in %d attempt(s)", i);
151
152 return 0;
153}
154
155/* This detects the OV7610, OV7620, or OV76BE chip. */
156static int ov7xx0_detect(struct i2c_client *c)
157{
158 struct ovcamchip *ov = i2c_get_clientdata(c);
159 int rc;
160 unsigned char val;
161
162 PDEBUG(4, "");
163
164 /* Detect chip (sub)type */
165 rc = ov_read(c, GENERIC_REG_COM_I, &val);
166 if (rc < 0) {
167 PERROR("Error detecting ov7xx0 type");
168 return rc;
169 }
170
171 if ((val & 3) == 3) {
172 PINFO("Camera chip is an OV7610");
173 ov->subtype = CC_OV7610;
174 } else if ((val & 3) == 1) {
175 rc = ov_read(c, 0x15, &val);
176 if (rc < 0) {
177 PERROR("Error detecting ov7xx0 type");
178 return rc;
179 }
180
181 if (val & 1) {
182 PINFO("Camera chip is an OV7620AE");
183 /* OV7620 is a close enough match for now. There are
184 * some definite differences though, so this should be
185 * fixed */
186 ov->subtype = CC_OV7620;
187 } else {
188 PINFO("Camera chip is an OV76BE");
189 ov->subtype = CC_OV76BE;
190 }
191 } else if ((val & 3) == 0) {
192 PINFO("Camera chip is an OV7620");
193 ov->subtype = CC_OV7620;
194 } else {
195 PERROR("Unknown camera chip version: %d", val & 3);
196 return -ENOSYS;
197 }
198
199 if (ov->subtype == CC_OV76BE)
200 ov->sops = &ov76be_ops;
201 else if (ov->subtype == CC_OV7620)
202 ov->sops = &ov7x20_ops;
203 else
204 ov->sops = &ov7x10_ops;
205
206 return 0;
207}
208
209/* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
210static int ov6xx0_detect(struct i2c_client *c)
211{
212 struct ovcamchip *ov = i2c_get_clientdata(c);
213 int rc;
214 unsigned char val;
215
216 PDEBUG(4, "");
217
218 /* Detect chip (sub)type */
219 rc = ov_read(c, GENERIC_REG_COM_I, &val);
220 if (rc < 0) {
221 PERROR("Error detecting ov6xx0 type");
222 return -1;
223 }
224
225 if ((val & 3) == 0) {
226 ov->subtype = CC_OV6630;
227 PINFO("Camera chip is an OV6630");
228 } else if ((val & 3) == 1) {
229 ov->subtype = CC_OV6620;
230 PINFO("Camera chip is an OV6620");
231 } else if ((val & 3) == 2) {
232 ov->subtype = CC_OV6630;
233 PINFO("Camera chip is an OV6630AE");
234 } else if ((val & 3) == 3) {
235 ov->subtype = CC_OV6630;
236 PINFO("Camera chip is an OV6630AF");
237 }
238
239 if (ov->subtype == CC_OV6620)
240 ov->sops = &ov6x20_ops;
241 else
242 ov->sops = &ov6x30_ops;
243
244 return 0;
245}
246
247static int ovcamchip_detect(struct i2c_client *c)
248{
249 /* Ideally we would just try a single register write and see if it NAKs.
250 * That isn't possible since the OV518 can't report I2C transaction
251 * failures. So, we have to try to initialize the chip (i.e. reset it
252 * and check the ID registers) to detect its presence. */
253
254 /* Test for 7xx0 */
255 PDEBUG(3, "Testing for 0V7xx0");
256 c->addr = OV7xx0_SID;
257 if (init_camchip(c) < 0) {
258 /* Test for 6xx0 */
259 PDEBUG(3, "Testing for 0V6xx0");
260 c->addr = OV6xx0_SID;
261 if (init_camchip(c) < 0) {
d56410e0 262 return -ENODEV;
1da177e4
LT
263 } else {
264 if (ov6xx0_detect(c) < 0) {
265 PERROR("Failed to init OV6xx0");
d56410e0 266 return -EIO;
1da177e4
LT
267 }
268 }
269 } else {
270 if (ov7xx0_detect(c) < 0) {
271 PERROR("Failed to init OV7xx0");
d56410e0 272 return -EIO;
1da177e4
LT
273 }
274 }
275
276 return 0;
277}
278
279/* ----------------------------------------------------------------------- */
280
281static int ovcamchip_attach(struct i2c_adapter *adap)
282{
283 int rc = 0;
284 struct ovcamchip *ov;
285 struct i2c_client *c;
286
287 /* I2C is not a PnP bus, so we can never be certain that we're talking
288 * to the right chip. To prevent damage to EEPROMS and such, only
289 * attach to adapters that are known to contain OV camera chips. */
290
291 switch (adap->id) {
c7a46533
JD
292 case I2C_HW_SMBUS_OV511:
293 case I2C_HW_SMBUS_OV518:
c7a46533 294 case I2C_HW_SMBUS_W9968CF:
1da177e4
LT
295 PDEBUG(1, "Adapter ID 0x%06x accepted", adap->id);
296 break;
297 default:
298 PDEBUG(1, "Adapter ID 0x%06x rejected", adap->id);
299 return -ENODEV;
300 }
301
302 c = kmalloc(sizeof *c, GFP_KERNEL);
303 if (!c) {
304 rc = -ENOMEM;
305 goto no_client;
306 }
307 memcpy(c, &client_template, sizeof *c);
308 c->adapter = adap;
fae91e72 309 strcpy(c->name, "OV????");
1da177e4 310
7408187d 311 ov = kzalloc(sizeof *ov, GFP_KERNEL);
1da177e4
LT
312 if (!ov) {
313 rc = -ENOMEM;
314 goto no_ov;
315 }
1da177e4
LT
316 i2c_set_clientdata(c, ov);
317
318 rc = ovcamchip_detect(c);
319 if (rc < 0)
320 goto error;
321
fae91e72 322 strcpy(c->name, chip_names[ov->subtype]);
1da177e4
LT
323
324 PDEBUG(1, "Camera chip detection complete");
325
326 i2c_attach_client(c);
327
328 return rc;
329error:
330 kfree(ov);
331no_ov:
332 kfree(c);
333no_client:
334 PDEBUG(1, "returning %d", rc);
335 return rc;
336}
337
338static int ovcamchip_detach(struct i2c_client *c)
339{
340 struct ovcamchip *ov = i2c_get_clientdata(c);
341 int rc;
342
343 rc = ov->sops->free(c);
344 if (rc < 0)
345 return rc;
346
347 i2c_detach_client(c);
348
349 kfree(ov);
350 kfree(c);
351 return 0;
352}
353
354static int ovcamchip_command(struct i2c_client *c, unsigned int cmd, void *arg)
355{
356 struct ovcamchip *ov = i2c_get_clientdata(c);
357
358 if (!ov->initialized &&
359 cmd != OVCAMCHIP_CMD_Q_SUBTYPE &&
360 cmd != OVCAMCHIP_CMD_INITIALIZE) {
361 dev_err(&c->dev, "ERROR: Camera chip not initialized yet!\n");
362 return -EPERM;
363 }
364
365 switch (cmd) {
366 case OVCAMCHIP_CMD_Q_SUBTYPE:
367 {
368 *(int *)arg = ov->subtype;
369 return 0;
370 }
371 case OVCAMCHIP_CMD_INITIALIZE:
372 {
373 int rc;
374
375 if (mono == -1)
376 ov->mono = *(int *)arg;
377 else
378 ov->mono = mono;
379
380 if (ov->mono) {
381 if (ov->subtype != CC_OV7620)
382 dev_warn(&c->dev, "Warning: Monochrome not "
383 "implemented for this chip\n");
384 else
385 dev_info(&c->dev, "Initializing chip as "
386 "monochrome\n");
387 }
388
389 rc = ov->sops->init(c);
390 if (rc < 0)
391 return rc;
392
393 ov->initialized = 1;
394 return 0;
395 }
396 default:
397 return ov->sops->command(c, cmd, arg);
398 }
399}
400
401/* ----------------------------------------------------------------------- */
402
403static struct i2c_driver driver = {
604f28e2 404 .driver = {
604f28e2
LR
405 .name = "ovcamchip",
406 },
1da177e4
LT
407 .id = I2C_DRIVERID_OVCAMCHIP,
408 .class = I2C_CLASS_CAM_DIGITAL,
1da177e4
LT
409 .attach_adapter = ovcamchip_attach,
410 .detach_client = ovcamchip_detach,
411 .command = ovcamchip_command,
412};
413
414static struct i2c_client client_template = {
fae91e72 415 .name = "(unset)",
1da177e4
LT
416 .driver = &driver,
417};
418
419static int __init ovcamchip_init(void)
420{
421#ifdef DEBUG
422 ovcamchip_debug = debug;
423#endif
424
425 PINFO(DRIVER_VERSION " : " DRIVER_DESC);
426 return i2c_add_driver(&driver);
427}
428
429static void __exit ovcamchip_exit(void)
430{
431 i2c_del_driver(&driver);
432}
433
434module_init(ovcamchip_init);
435module_exit(ovcamchip_exit);