1 /* Shared Code for OmniVision Camera Chip Drivers
3 * Copyright (c) 2004 Mark McClelland <mark@alpha.dyndns.org>
4 * http://alpha.dyndns.org/ov511/
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.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-i2c-drv.h>
21 #include "ovcamchip_priv.h"
23 #define DRIVER_VERSION "v2.27 for Linux 2.6"
24 #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org>"
25 #define DRIVER_DESC "OV camera chip I2C driver"
27 #define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
28 #define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
31 int ovcamchip_debug
= 0;
33 module_param(debug
, int, 0);
34 MODULE_PARM_DESC(debug
,
35 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=all");
38 /* By default, let bridge driver tell us if chip is monochrome. mono=0
39 * will ignore that and always treat chips as color. mono=1 will force
40 * monochrome mode for all chips. */
42 module_param(mono
, int, 0);
43 MODULE_PARM_DESC(mono
,
44 "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
46 MODULE_AUTHOR(DRIVER_AUTHOR
);
47 MODULE_DESCRIPTION(DRIVER_DESC
);
48 MODULE_LICENSE("GPL");
51 /* Registers common to all chips, that are needed for detection */
52 #define GENERIC_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
53 #define GENERIC_REG_ID_LOW 0x1D /* manufacturer ID LSB */
54 #define GENERIC_REG_COM_I 0x29 /* misc ID bits */
56 static char *chip_names
[NUM_CC_TYPES
] = {
57 [CC_UNKNOWN
] = "Unknown chip",
58 [CC_OV76BE
] = "OV76BE",
59 [CC_OV7610
] = "OV7610",
60 [CC_OV7620
] = "OV7620",
61 [CC_OV7620AE
] = "OV7620AE",
62 [CC_OV6620
] = "OV6620",
63 [CC_OV6630
] = "OV6630",
64 [CC_OV6630AE
] = "OV6630AE",
65 [CC_OV6630AF
] = "OV6630AF",
68 /* ----------------------------------------------------------------------- */
70 int ov_write_regvals(struct i2c_client
*c
, struct ovcamchip_regvals
*rvals
)
74 while (rvals
->reg
!= 0xff) {
75 rc
= ov_write(c
, rvals
->reg
, rvals
->val
);
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".
89 int ov_write_mask(struct i2c_client
*c
,
95 unsigned char oldval
, newval
;
100 rc
= ov_read(c
, reg
, &oldval
);
104 oldval
&= (~mask
); /* Clear the masked bits */
105 value
&= mask
; /* Enforce mask on value */
106 newval
= oldval
| value
; /* Set the desired bits */
109 return ov_write(c
, reg
, newval
);
112 /* ----------------------------------------------------------------------- */
114 /* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
116 static int init_camchip(struct i2c_client
*c
)
119 unsigned char high
, low
;
122 ov_write(c
, 0x12, 0x80);
124 /* Wait for it to initialize */
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) {
138 ov_write(c
, 0x12, 0x80);
140 /* Wait for it to initialize */
143 /* Dummy read to sync I2C */
144 ov_read(c
, 0x00, &low
);
150 PDEBUG(1, "I2C synced in %d attempt(s)", i
);
155 /* This detects the OV7610, OV7620, or OV76BE chip. */
156 static int ov7xx0_detect(struct i2c_client
*c
)
158 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
164 /* Detect chip (sub)type */
165 rc
= ov_read(c
, GENERIC_REG_COM_I
, &val
);
167 PERROR("Error detecting ov7xx0 type");
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
);
177 PERROR("Error detecting ov7xx0 type");
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
186 ov
->subtype
= CC_OV7620
;
188 PINFO("Camera chip is an OV76BE");
189 ov
->subtype
= CC_OV76BE
;
191 } else if ((val
& 3) == 0) {
192 PINFO("Camera chip is an OV7620");
193 ov
->subtype
= CC_OV7620
;
195 PERROR("Unknown camera chip version: %d", val
& 3);
199 if (ov
->subtype
== CC_OV76BE
)
200 ov
->sops
= &ov76be_ops
;
201 else if (ov
->subtype
== CC_OV7620
)
202 ov
->sops
= &ov7x20_ops
;
204 ov
->sops
= &ov7x10_ops
;
209 /* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
210 static int ov6xx0_detect(struct i2c_client
*c
)
212 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
218 /* Detect chip (sub)type */
219 rc
= ov_read(c
, GENERIC_REG_COM_I
, &val
);
221 PERROR("Error detecting ov6xx0 type");
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");
239 if (ov
->subtype
== CC_OV6620
)
240 ov
->sops
= &ov6x20_ops
;
242 ov
->sops
= &ov6x30_ops
;
247 static int ovcamchip_detect(struct i2c_client
*c
)
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. */
255 PDEBUG(3, "Testing for 0V7xx0");
256 if (init_camchip(c
) < 0)
258 /* 7-bit addresses with bit 0 set are for the OV7xx0 */
260 if (ov7xx0_detect(c
) < 0) {
261 PERROR("Failed to init OV7xx0");
267 PDEBUG(3, "Testing for 0V6xx0");
268 if (ov6xx0_detect(c
) < 0) {
269 PERROR("Failed to init OV6xx0");
275 /* ----------------------------------------------------------------------- */
277 static long ovcamchip_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
279 struct ovcamchip
*ov
= to_ovcamchip(sd
);
280 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
282 if (!ov
->initialized
&&
283 cmd
!= OVCAMCHIP_CMD_Q_SUBTYPE
&&
284 cmd
!= OVCAMCHIP_CMD_INITIALIZE
) {
285 v4l2_err(sd
, "Camera chip not initialized yet!\n");
290 case OVCAMCHIP_CMD_Q_SUBTYPE
:
292 *(int *)arg
= ov
->subtype
;
295 case OVCAMCHIP_CMD_INITIALIZE
:
300 ov
->mono
= *(int *)arg
;
305 if (ov
->subtype
!= CC_OV7620
)
306 v4l2_warn(sd
, "Monochrome not "
307 "implemented for this chip\n");
309 v4l2_info(sd
, "Initializing chip as "
313 rc
= ov
->sops
->init(c
);
321 return ov
->sops
->command(c
, cmd
, arg
);
325 /* ----------------------------------------------------------------------- */
327 static const struct v4l2_subdev_core_ops ovcamchip_core_ops
= {
328 .ioctl
= ovcamchip_ioctl
,
331 static const struct v4l2_subdev_ops ovcamchip_ops
= {
332 .core
= &ovcamchip_core_ops
,
335 static int ovcamchip_probe(struct i2c_client
*client
,
336 const struct i2c_device_id
*id
)
338 struct ovcamchip
*ov
;
339 struct v4l2_subdev
*sd
;
342 ov
= kzalloc(sizeof *ov
, GFP_KERNEL
);
348 v4l2_i2c_subdev_init(sd
, client
, &ovcamchip_ops
);
350 rc
= ovcamchip_detect(client
);
354 v4l_info(client
, "%s found @ 0x%02x (%s)\n",
355 chip_names
[ov
->subtype
], client
->addr
<< 1, client
->adapter
->name
);
357 PDEBUG(1, "Camera chip detection complete");
363 PDEBUG(1, "returning %d", rc
);
367 static int ovcamchip_remove(struct i2c_client
*client
)
369 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
370 struct ovcamchip
*ov
= to_ovcamchip(sd
);
373 v4l2_device_unregister_subdev(sd
);
374 rc
= ov
->sops
->free(client
);
382 /* ----------------------------------------------------------------------- */
384 static const struct i2c_device_id ovcamchip_id
[] = {
388 MODULE_DEVICE_TABLE(i2c
, ovcamchip_id
);
390 static struct v4l2_i2c_driver_data v4l2_i2c_data
= {
392 .probe
= ovcamchip_probe
,
393 .remove
= ovcamchip_remove
,
394 .id_table
= ovcamchip_id
,