23 #include <linux/module.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
28 #include <linux/videodev2.h>
37 #define to_ov9640_sensor(sd) container_of(sd, struct ov9640_priv, subdev)
40 static const struct ov9640_reg ov9640_regs_dflt[] = {
50 { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 },
51 { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 },
52 { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 },
53 { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 },
56 { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 },
57 { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 },
58 { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e },
59 { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 },
71 static const struct ov9640_reg ov9640_regs_qqcif[] = {
81 static const struct ov9640_reg ov9640_regs_qqvga[] = {
91 static const struct ov9640_reg ov9640_regs_qcif[] = {
100 static const struct ov9640_reg ov9640_regs_qvga[] = {
109 static const struct ov9640_reg ov9640_regs_cif[] = {
118 static const struct ov9640_reg ov9640_regs_vga[] = {
127 static const struct ov9640_reg ov9640_regs_sxga[] = {
136 static const struct ov9640_reg ov9640_regs_yuv[] = {
149 static const struct ov9640_reg ov9640_regs_rgb[] = {
174 .addr = client->
addr,
193 dev_err(&client->
dev,
"Failed reading register 0x%02x!\n", reg);
198 static int ov9640_reg_write(
struct i2c_client *client,
u8 reg,
u8 val)
202 unsigned char data[2] = {
reg, val };
212 dev_err(&client->
dev,
"Failed writing register 0x%02x!\n", reg);
217 ret = ov9640_reg_read(client, reg, &_val);
220 "Failed reading back register 0x%02x!\n", reg);
227 static int ov9640_reg_rmw(
struct i2c_client *client,
u8 reg,
u8 set,
u8 unset)
232 ret = ov9640_reg_read(client, reg, &val);
235 "[Read]-Modify-Write of register %02x failed!\n", reg);
242 ret = ov9640_reg_write(client, reg, val);
245 "Read-Modify-[Write] of register %02x failed!\n", reg);
251 static int ov9640_reset(
struct i2c_client *client)
258 "An error occurred while entering soft reset!\n");
296 id->ident = priv->
model;
302 #ifdef CONFIG_VIDEO_ADV_DEBUG
306 struct i2c_client *client = v4l2_get_subdevdata(sd);
310 if (reg->
reg & ~0xff)
315 ret = ov9640_reg_read(client, reg->
reg, &val);
324 static int ov9640_set_register(
struct v4l2_subdev *sd,
327 struct i2c_client *client = v4l2_get_subdevdata(sd);
329 if (reg->
reg & ~0xff || reg->
val & ~0xff)
332 return ov9640_reg_write(client, reg->
reg, reg->
val);
336 static int ov9640_s_power(
struct v4l2_subdev *sd,
int on)
338 struct i2c_client *client = v4l2_get_subdevdata(sd);
341 return soc_camera_set_power(&client->
dev, icl, on);
349 int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
350 int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
353 if (res_x[i] >= *width && res_y[i] >= *height) {
360 *width = res_x[SXGA];
361 *height = res_y[SXGA];
389 static int ov9640_write_regs(
struct i2c_client *client,
u32 width,
392 const struct ov9640_reg *ov9640_regs, *matrix_regs;
393 int ov9640_regs_len, matrix_regs_len;
400 ov9640_regs = ov9640_regs_qqcif;
401 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_qqcif);
404 ov9640_regs = ov9640_regs_qqvga;
405 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_qqvga);
408 ov9640_regs = ov9640_regs_qcif;
409 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_qcif);
412 ov9640_regs = ov9640_regs_qvga;
413 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_qvga);
416 ov9640_regs = ov9640_regs_cif;
417 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_cif);
420 ov9640_regs = ov9640_regs_vga;
421 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_vga);
424 ov9640_regs = ov9640_regs_sxga;
425 ov9640_regs_len =
ARRAY_SIZE(ov9640_regs_sxga);
428 dev_err(&client->
dev,
"Failed to select resolution!\n");
434 matrix_regs = ov9640_regs_yuv;
435 matrix_regs_len =
ARRAY_SIZE(ov9640_regs_yuv);
437 matrix_regs = ov9640_regs_rgb;
438 matrix_regs_len =
ARRAY_SIZE(ov9640_regs_rgb);
442 for (i = 0; i < ov9640_regs_len; i++) {
443 val = ov9640_regs[
i].
val;
445 switch (ov9640_regs[i].reg) {
460 ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
466 for (i = 0; i < matrix_regs_len; i++) {
467 ret = ov9640_reg_write(client, matrix_regs[i].reg,
477 static int ov9640_prog_dflt(
struct i2c_client *client)
481 for (i = 0; i <
ARRAY_SIZE(ov9640_regs_dflt); i++) {
482 ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
483 ov9640_regs_dflt[i].val);
496 struct v4l2_mbus_framefmt *mf)
498 struct i2c_client *client = v4l2_get_subdevdata(sd);
504 ov9640_res_roundup(&mf->width, &mf->height);
505 ov9640_alter_regs(mf->code, &alts);
507 ov9640_reset(client);
509 ret = ov9640_prog_dflt(client);
524 ret = ov9640_write_regs(client, mf->width, code, &alts);
527 mf->colorspace = cspace;
534 struct v4l2_mbus_framefmt *mf)
536 ov9640_res_roundup(&mf->width, &mf->height);
560 *code = ov9640_codes[
index];
589 static int ov9640_video_probe(
struct i2c_client *client)
591 struct v4l2_subdev *sd = i2c_get_clientdata(client);
597 ret = ov9640_s_power(&priv->
subdev, 1);
605 ret = ov9640_reg_read(client,
OV9640_PID, &pid);
607 ret = ov9640_reg_read(client,
OV9640_VER, &ver);
627 dev_err(&client->
dev,
"Product ID error %x:%x\n", pid, ver);
632 dev_info(&client->
dev,
"%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
633 devname, pid, ver, midh, midl);
638 ov9640_s_power(&priv->
subdev, 0);
643 .s_ctrl = ov9640_s_ctrl,
647 .g_chip_ident = ov9640_g_chip_ident,
648 #ifdef CONFIG_VIDEO_ADV_DEBUG
649 .g_register = ov9640_get_register,
650 .s_register = ov9640_set_register,
652 .s_power = ov9640_s_power,
656 static int ov9640_g_mbus_config(
struct v4l2_subdev *sd,
659 struct i2c_client *client = v4l2_get_subdevdata(sd);
672 .s_stream = ov9640_s_stream,
673 .s_mbus_fmt = ov9640_s_fmt,
674 .try_mbus_fmt = ov9640_try_fmt,
675 .enum_mbus_fmt = ov9640_enum_fmt,
676 .cropcap = ov9640_cropcap,
677 .g_crop = ov9640_g_crop,
678 .g_mbus_config = ov9640_g_mbus_config,
682 .core = &ov9640_core_ops,
683 .video = &ov9640_video_ops,
689 static int ov9640_probe(
struct i2c_client *client,
697 dev_err(&client->
dev,
"Missing platform_data for driver\n");
704 "Failed to allocate memory for private data!\n");
716 if (priv->
hdl.error) {
717 int err = priv->
hdl.error;
723 ret = ov9640_video_probe(client);
733 static int ov9640_remove(
struct i2c_client *client)
735 struct v4l2_subdev *sd = i2c_get_clientdata(client);
750 static struct i2c_driver ov9640_i2c_driver = {
754 .probe = ov9640_probe,
755 .remove = ov9640_remove,
756 .id_table = ov9640_id,