(三)v4l2: 根据应用层顺序,查看驱动

采用简单的驱动进行分析,方便:

ov9650.c    D:\source_code\linux-4.19.11\drivers\media\i2c
 

第一步

static struct i2c_driver ov965x_i2c_driver = {
	.driver = {
		.name	= DRIVER_NAME,
		.of_match_table = of_match_ptr(ov965x_of_match),
	},
	.probe		= ov965x_probe,
	.remove		= ov965x_remove,
	.id_table	= ov965x_id,
};

module_i2c_driver(ov965x_i2c_driver);

第二布  设置设备树 或者 id_table

static const struct i2c_device_id ov965x_id[] = {
	{ "OV9650", 0 },
	{ "OV9652", 0 },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, ov965x_id);

#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id ov965x_of_match[] = {
	{ .compatible = "ovti,ov9650", },
	{ .compatible = "ovti,ov9652", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, ov965x_of_match);
#endif

m5602_ov9650.h    D:\source_code\linux-4.19.11\drivers\media\usb\gspca\m5602
 

/* Kernel module parameters */
extern int force_sensor;
extern bool dump_sensor;

int ov9650_probe(struct sd *sd);
int ov9650_init(struct sd *sd);
int ov9650_init_controls(struct sd *sd);
int ov9650_start(struct sd *sd);
int ov9650_stop(struct sd *sd);
void ov9650_disconnect(struct sd *sd);

static const struct m5602_sensor ov9650 = {
	.name = "OV9650",
	.i2c_slave_id = 0x60,
	.i2c_regW = 1,
	.probe = ov9650_probe,
	.init = ov9650_init,
	.init_controls = ov9650_init_controls,
	.start = ov9650_start,
	.stop = ov9650_stop,
	.disconnect = ov9650_disconnect,
};

m5602_ov9650.c    D:\source_code\linux-4.19.11\drivers\media\usb\gspca\m5602
 


第三步:  执行probe函数

struct ov965x {
	struct v4l2_subdev sd;
	struct media_pad pad;
	enum v4l2_mbus_type bus_type;
	struct gpio_desc *gpios[NUM_GPIOS];
	/* External master clock frequency */
	unsigned long mclk_frequency;
	struct clk *clk;

	/* Protects the struct fields below */
	struct mutex lock;

	struct i2c_client *client;

	/* Exposure row interval in us */
	unsigned int exp_row_interval;

	unsigned short id;
	const struct ov965x_framesize *frame_size;
	/* YUYV sequence (pixel format) control register */
	u8 tslb_reg;
	struct v4l2_mbus_framefmt format;

	struct ov965x_ctrls ctrls;
	/* Pointer to frame rate control data structure */
	const struct ov965x_interval *fiv;

	int streaming;
	int power;

	u8 apply_frame_fmt;
};


struct ov9650_platform_data {
	unsigned long mclk_frequency;
	int gpio_pwdn;
	int gpio_reset;
};


static int ov965x_probe(struct i2c_client *client,
			const struct i2c_device_id *id) :
    const struct ov9650_platform_data *pdata = client->dev.platform_data;
	struct v4l2_subdev *sd;
	struct ov965x *ov965x;

    ov965x = devm_kzalloc(&client->dev, sizeof(*ov965x), GFP_KERNEL);

    ov965x->client = client;

    if (pdata) {
		if (pdata->mclk_frequency == 0) {
			dev_err(&client->dev, "MCLK frequency not specified\n");
			return -EINVAL;
		}
		ov965x->mclk_frequency = pdata->mclk_frequency;  //初始化时钟

		ret = ov965x_configure_gpios_pdata(ov965x, pdata); //设置gpio 看后面
		if (ret < 0)
			return ret;
	} else if (dev_fwnode(&client->dev)) {
		ov965x->clk = devm_clk_get(&ov965x->client->dev, NULL);
		if (IS_ERR(ov965x->clk))
			return PTR_ERR(ov965x->clk);
		ov965x->mclk_frequency = clk_get_rate(ov965x->clk);

		ret = ov965x_configure_gpios(ov965x);
		if (ret < 0)
			return ret;
	} else {
		dev_err(&client->dev,
			"Neither platform data nor device property specified\n");

		return -EINVAL;
	}


    // 以上只是用来初始化 时钟等。
    sd = &ov965x->sd;
	v4l2_i2c_subdev_init(sd, client, &ov965x_subdev_ops);
            static const struct v4l2_subdev_ops ov965x_subdev_ops = {
	                .core = &ov965x_core_ops,
	                .pad = &ov965x_pad_ops,
	                .video = &ov965x_video_ops,
                };

	strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name));

	sd->internal_ops = &ov965x_sd_internal_ops;
                static const struct v4l2_subdev_internal_ops ov965x_sd_internal_ops = {
	                 .open = ov965x_open,
                };
	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
		     V4L2_SUBDEV_FL_HAS_EVENTS;

    ret = ov965x_initialize_controls(ov965x);   // 很重要 看下面

    ov965x_get_default_format(&ov965x->format);   // 初始化 format

    ov965x->frame_size = &ov965x_framesizes[0];
	ov965x->fiv = &ov965x_intervals[0]; 

    ret = ov965x_detect_sensor(sd);

    /* Update exposure time min/max to match frame format */
	ov965x_update_exposure_ctrl(ov965x);

	ret = v4l2_async_register_subdev(sd);
        list_for_each_entry(notifier, ¬ifier_list, list) {
                asd = v4l2_async_find_match(notifier, sd);
                        case V4L2_ASYNC_MATCH_I2C:
			                        match = match_i2c;
                                        static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
                                                {
                                                #if IS_ENABLED(CONFIG_I2C)
	                                                struct i2c_client *client = i2c_verify_client(sd->dev);
	                                                return client &&
		                                                asd->match.i2c.adapter_id == client->adapter->nr &&
		                                                asd->match.i2c.address == client->addr;
                                                #else
	                                            return false;
                                                #endif
                                                }
                ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
                        ret = v4l2_device_register_subdev(v4l2_dev, sd);
                        if (sd->internal_ops && sd->internal_ops->registered) {
		                        err = sd->internal_ops->registered(sd);   // 注册,可惜没有
		                    if (err)
			                        goto error_unregister;
	                        }

                ret = v4l2_async_notifier_try_complete(notifier);
 
        /* None matched, wait for hot-plugging */
	    list_add(&sd->async_list, &subdev_list);
    return 0;
   
/

static int ov965x_configure_gpios(struct ov965x *ov965x)
{
	struct device *dev = &ov965x->client->dev;

	ov965x->gpios[GPIO_PWDN] = devm_gpiod_get_optional(dev, "powerdown",
							GPIOD_OUT_HIGH);
	if (IS_ERR(ov965x->gpios[GPIO_PWDN])) {
		dev_info(dev, "can't get %s GPIO\n", "powerdown");
		return PTR_ERR(ov965x->gpios[GPIO_PWDN]);
	}

	ov965x->gpios[GPIO_RST] = devm_gpiod_get_optional(dev, "reset",
							GPIOD_OUT_HIGH);
	if (IS_ERR(ov965x->gpios[GPIO_RST])) {
		dev_info(dev, "can't get %s GPIO\n", "reset");
		return PTR_ERR(ov965x->gpios[GPIO_RST]);
	}

	return 0;
}



static int ov965x_initialize_controls(struct ov965x *ov965x) :
    const struct v4l2_ctrl_ops *ops = &ov965x_ctrl_ops;
    struct ov965x_ctrls *ctrls = &ov965x->ctrls;
    struct v4l2_ctrl_handler *hdl = &ctrls->handler;

    ret = v4l2_ctrl_handler_init(hdl, 16);
    /* Auto/manual white balance */
	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
					   V4L2_CID_AUTO_WHITE_BALANCE,
					   0, 1, 1, 1);
	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
						0, 0xff, 1, 0x80);
	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
					       0, 0xff, 1, 0x80);
	/* Auto/manual exposure */
	ctrls->auto_exp =
		v4l2_ctrl_new_std_menu(hdl, ops,
				       V4L2_CID_EXPOSURE_AUTO,
				       V4L2_EXPOSURE_MANUAL, 0,
				       V4L2_EXPOSURE_AUTO);    

      。。。。。

    ov965x->sd.ctrl_handler = hdl;

1.v4l2_i2c_subdev_init  // D:\source_code\linux-4.19.11\drivers\media\v4l2-core\v4l2-common.c

v4l2_i2c_subdev_init(sd, client, &ov965x_subdev_ops);

void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client,
		const struct v4l2_subdev_ops *ops)
{
	v4l2_subdev_init(sd, ops);
	sd->flags |= V4L2_SUBDEV_FL_IS_I2C;
	/* the owner is the same as the i2c_client's driver owner */
	sd->owner = client->dev.driver->owner;
	sd->dev = &client->dev;
	/* i2c_client and v4l2_subdev point to one another */
	v4l2_set_subdevdata(sd, client);
	i2c_set_clientdata(client, sd);
	/* initialize name */
	snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
		client->dev.driver->name, i2c_adapter_id(client->adapter),
		client->addr);
}
EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init);
static const struct v4l2_subdev_ops ov965x_subdev_ops = {
	.core = &ov965x_core_ops,
	.pad = &ov965x_pad_ops,
	.video = &ov965x_video_ops,
};

static const struct v4l2_subdev_core_ops ov965x_core_ops = {
	.s_power = ov965x_s_power,    //启动
	.log_status = v4l2_ctrl_subdev_log_status,
	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
};

static const struct v4l2_subdev_pad_ops ov965x_pad_ops = {
	.enum_mbus_code = ov965x_enum_mbus_code,
	.enum_frame_size = ov965x_enum_frame_sizes,
	.get_fmt = ov965x_get_fmt,
	.set_fmt = ov965x_set_fmt,
};

static const struct v4l2_subdev_video_ops ov965x_video_ops = {
	.s_stream = ov965x_s_stream,  
	.g_frame_interval = ov965x_g_frame_interval,
	.s_frame_interval = ov965x_s_frame_interval,

};

.s_power = ov965x_s_power, 

/
static int ov965x_s_power(struct v4l2_subdev *sd, int on)
{
	struct ov965x *ov965x = to_ov965x(sd);
	struct i2c_client *client = ov965x->client;
	int ret = 0;

	v4l2_dbg(1, debug, client, "%s: on: %d\n", __func__, on);

	mutex_lock(&ov965x->lock);
	if (ov965x->power == !on) {
		ret = __ov965x_set_power(ov965x, on);  //操作寄存器,启动 如下
		if (!ret && on) {
			ret = ov965x_write_array(client,
						 ov965x_init_regs);   //如果起来来就初始化init寄存器
			ov965x->apply_frame_fmt = 1;
			ov965x->ctrls.update = 1;
		}
	}
	if (!ret)
		ov965x->power += on ? 1 : -1;

	WARN_ON(ov965x->power < 0);
	mutex_unlock(&ov965x->lock);
	return ret;
}

static int __ov965x_set_power(struct ov965x *ov965x, int on)
{
	if (on) {
		int ret = clk_prepare_enable(ov965x->clk);

		if (ret)
			return ret;

		gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 0);
		gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 0);
		msleep(25);
	} else {
		gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 1);
		gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 1);

		clk_disable_unprepare(ov965x->clk);
	}

	ov965x->streaming = 0;

	return 0;
}

///
static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
{
	return &container_of(ctrl->handler, struct ov965x, ctrls.handler)->sd;
}

static inline struct ov965x *to_ov965x(struct v4l2_subdev *sd)
{
	return container_of(sd, struct ov965x, sd);
}


static int ov965x_write_array(struct i2c_client *client,
			      const struct i2c_rv *regs)
{
	int i, ret = 0;

	for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
		ret = ov965x_write(client, regs[i].addr, regs[i].value);

	return ret;
}

static int ov965x_write(struct i2c_client *client, u8 addr, u8 val)
{
	u8 buf[2] = { addr, val };

	int ret = i2c_master_send(client, buf, 2);

	v4l2_dbg(2, debug, client, "%s: 0x%02x @ 0x%02X (%d)\n",
		 __func__, val, addr, ret);

	return ret == 2 ? 0 : ret;
}

static int ov965x_read(struct i2c_client *client, u8 addr, u8 *val)
{
	u8 buf = addr;
	struct i2c_msg msg = {
		.addr = client->addr,
		.flags = 0,
		.len = 1,
		.buf = &buf
	};
	int ret;

	ret = i2c_transfer(client->adapter, &msg, 1);
	if (ret == 1) {
		msg.flags = I2C_M_RD;
		ret = i2c_transfer(client->adapter, &msg, 1);

		if (ret == 1)
			*val = buf;
	}

	v4l2_dbg(2, debug, client, "%s: 0x%02x @ 0x%02x. (%d)\n",
		 __func__, *val, addr, ret);

	return ret == 1 ? 0 : ret;
}



static const struct i2c_rv ov965x_init_regs[] = {
	{ REG_COM2, 0x10 },	/* Set soft sleep mode */
	{ REG_COM5, 0x00 },	/* System clock options */
	{ REG_COM2, 0x01 },	/* Output drive, soft sleep mode */
	{ REG_COM10, 0x00 },	/* Slave mode, HREF vs HSYNC, signals negate */
	{ REG_EDGE, 0xa6 },	/* Edge enhancement treshhold and factor */
	{ REG_COM16, 0x02 },	/* Color matrix coeff double option */
	{ REG_COM17, 0x08 },	/* Single frame out, banding filter */
	{ 0x16, 0x06 },
	{ REG_CHLF, 0xc0 },	/* Reserved  */
	{ 0x34, 0xbf },
	{ 0xa8, 0x80 },
	{ 0x96, 0x04 },
	{ 0x8e, 0x00 },
	{ REG_COM12, 0x77 },	/* HREF option, UV average  */
	{ 0x8b, 0x06 },
	{ 0x35, 0x91 },
	{ 0x94, 0x88 },
	{ 0x95, 0x88 },
	{ REG_COM15, 0xc1 },	/* Output range, RGB 555/565 */
	{ REG_GRCOM, 0x2f },	/* Analog BLC & regulator */
	{ REG_COM6, 0x43 },	/* HREF & ADBLC options */
	{ REG_COM8, 0xe5 },	/* AGC/AEC options */
	{ REG_COM13, 0x90 },	/* Gamma selection, colour matrix, UV delay */
	{ REG_HV, 0x80 },	/* Manual banding filter MSB  */
	{ 0x5c, 0x96 },		/* Reserved up to 0xa5 */
	{ 0x5d, 0x96 },
	{ 0x5e, 0x10 },
	{ 0x59, 0xeb },
	{ 0x5a, 0x9c },
	{ 0x5b, 0x55 },
	{ 0x43, 0xf0 },
	{ 0x44, 0x10 },
	{ 0x45, 0x55 },
	{ 0x46, 0x86 },
	{ 0x47, 0x64 },
	{ 0x48, 0x86 },
	{ 0x5f, 0xe0 },
	{ 0x60, 0x8c },
	{ 0x61, 0x20 },
	{ 0xa5, 0xd9 },
	{ 0xa4, 0x74 },		/* reserved */
	{ REG_COM23, 0x02 },	/* Color gain analog/_digital_ */
	{ REG_COM8, 0xe7 },	/* Enable AEC, AWB, AEC */
	{ REG_COM22, 0x23 },	/* Edge enhancement, denoising */
	{ 0xa9, 0xb8 },
	{ 0xaa, 0x92 },
	{ 0xab, 0x0a },
	{ REG_DBLC1, 0xdf },	/* Digital BLC */
	{ REG_DBLC_B, 0x00 },	/* Digital BLC B chan offset */
	{ REG_DBLC_R, 0x00 },	/* Digital BLC R chan offset */
	{ REG_DBLC_GB, 0x00 },	/* Digital BLC GB chan offset */
	{ REG_DBLC_GR, 0x00 },
	{ REG_COM9, 0x3a },	/* Gain ceiling 16x */
	{ REG_NULL, 0 }
};



.enum_frame_size = ov965x_enum_frame_sizes,

static int ov965x_enum_frame_sizes(struct v4l2_subdev *sd,
				   struct v4l2_subdev_pad_config *cfg,
				   struct v4l2_subdev_frame_size_enum *fse)
{
	int i = ARRAY_SIZE(ov965x_formats);

	if (fse->index >= ARRAY_SIZE(ov965x_framesizes))
		return -EINVAL;

	while (--i)
		if (fse->code == ov965x_formats[i].code)
			break;

	fse->code = ov965x_formats[i].code;

	fse->min_width  = ov965x_framesizes[fse->index].width;
	fse->max_width  = fse->min_width;
	fse->max_height = ov965x_framesizes[fse->index].height;
	fse->min_height = fse->max_height;

	return 0;
}

.get_fmt = ov965x_get_fmt,

static int ov965x_get_fmt(struct v4l2_subdev *sd,
			  struct v4l2_subdev_pad_config *cfg,
			  struct v4l2_subdev_format *fmt)
{
	struct ov965x *ov965x = to_ov965x(sd);
	struct v4l2_mbus_framefmt *mf;

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
		fmt->format = *mf;
		return 0;
	}

	mutex_lock(&ov965x->lock);
	fmt->format = ov965x->format;
	mutex_unlock(&ov965x->lock);

	return 0;
}

.set_fmt = ov965x_set_fmt,


sd->internal_ops = &ov965x_sd_internal_ops;

static const struct v4l2_subdev_internal_ops ov965x_sd_internal_ops = {
	.open = ov965x_open,
};
/*
 * V4L2 subdev internal operations
 */
static int ov965x_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
	struct v4l2_mbus_framefmt *mf =
		v4l2_subdev_get_try_format(sd, fh->pad, 0);

	ov965x_get_default_format(mf);
	return 0;
}

/*
 * V4L2 subdev video and pad level operations
 */
static void ov965x_get_default_format(struct v4l2_mbus_framefmt *mf)
{
	mf->width = ov965x_framesizes[0].width;
	mf->height = ov965x_framesizes[0].height;
	mf->colorspace = ov965x_formats[0].colorspace;
	mf->code = ov965x_formats[0].code;
	mf->field = V4L2_FIELD_NONE;
}

static const struct ov965x_framesize ov965x_framesizes[] = {
	{
		.width		= SXGA_WIDTH,
		.height		= SXGA_HEIGHT,
		.regs		= ov965x_sxga_regs,
		.max_exp_lines	= 1048,
	}, {
		.width		= VGA_WIDTH,
		.height		= VGA_HEIGHT,
		.regs		= ov965x_vga_regs,
		.max_exp_lines	= 498,
	}, {
		.width		= QVGA_WIDTH,
		.height		= QVGA_HEIGHT,
		.regs		= ov965x_qvga_regs,
		.max_exp_lines	= 248,
	},
};


static const struct ov965x_pixfmt ov965x_formats[] = {
	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 0x00},
	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG, 0x04},
	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG, 0x0c},
	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 0x08},
};

ret = ov965x_initialize_controls(ov965x);   //重点

static int ov965x_initialize_controls(struct ov965x *ov965x) :
    const struct v4l2_ctrl_ops *ops = &ov965x_ctrl_ops;
    struct ov965x_ctrls *ctrls = &ov965x->ctrls;
 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
	int ret;

    ret = v4l2_ctrl_handler_init(hdl, 16);

    /* Auto/manual white balance */
	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
					   V4L2_CID_AUTO_WHITE_BALANCE,
					   0, 1, 1, 1);
	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
						0, 0xff, 1, 0x80);
	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
					       0, 0xff, 1, 0x80);
	/* Auto/manual exposure */
	ctrls->auto_exp =
		v4l2_ctrl_new_std_menu(hdl, ops,
				       V4L2_CID_EXPOSURE_AUTO,
				       V4L2_EXPOSURE_MANUAL, 0,
				       V4L2_EXPOSURE_AUTO);
	/* Exposure time, in 100 us units. min/max is updated dynamically. */
	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops,
					    V4L2_CID_EXPOSURE_ABSOLUTE,
					    2, 1500, 1, 500);
	/* Auto/manual gain */
	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
					     0, 1, 1, 1);
	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
					16, 64 * (16 + 15), 1, 64 * 16);

	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
					      -2, 2, 1, 0);
	ctrls->brightness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS,
					      -3, 3, 1, 0);
	ctrls->sharpness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS,
					     0, 32, 1, 6);

	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);

	ctrls->light_freq =
		v4l2_ctrl_new_std_menu(hdl, ops,
				       V4L2_CID_POWER_LINE_FREQUENCY,
				       V4L2_CID_POWER_LINE_FREQUENCY_60HZ, ~0x7,
				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);

	v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
				     ARRAY_SIZE(test_pattern_menu) - 1, 0, 0,
				     test_pattern_menu);

static const struct v4l2_ctrl_ops ov965x_ctrl_ops = {
    .g_volatile_ctrl = ov965x_g_volatile_ctrl,
    .s_ctrl    = ov965x_s_ctrl,
};

static int ov965x_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
	struct ov965x *ov965x = to_ov965x(sd);
	int ret = -EINVAL;

	v4l2_dbg(1, debug, sd, "s_ctrl: %s, value: %d. power: %d\n",
		 ctrl->name, ctrl->val, ov965x->power);

	mutex_lock(&ov965x->lock);
	/*
	 * If the device is not powered up now postpone applying control's
	 * value to the hardware, until it is ready to accept commands.
	 */
	if (ov965x->power == 0) {
		mutex_unlock(&ov965x->lock);
		return 0;
	}

	switch (ctrl->id) {
	case V4L2_CID_AUTO_WHITE_BALANCE:
		ret = ov965x_set_white_balance(ov965x, ctrl->val);
		break;

	case V4L2_CID_BRIGHTNESS:
		ret = ov965x_set_brightness(ov965x, ctrl->val);
		break;

	case V4L2_CID_EXPOSURE_AUTO:
		ret = ov965x_set_exposure(ov965x, ctrl->val);
		break;

	case V4L2_CID_AUTOGAIN:
		ret = ov965x_set_gain(ov965x, ctrl->val);
		break;

	case V4L2_CID_HFLIP:
		ret = ov965x_set_flip(ov965x);
		break;

	case V4L2_CID_POWER_LINE_FREQUENCY:
		ret = ov965x_set_banding_filter(ov965x, ctrl->val);
		break;

	case V4L2_CID_SATURATION:
		ret = ov965x_set_saturation(ov965x, ctrl->val);
		break;

	case V4L2_CID_SHARPNESS:
		ret = ov965x_set_sharpness(ov965x, ctrl->val);
		break;

	case V4L2_CID_TEST_PATTERN:
		ret = ov965x_set_test_pattern(ov965x, ctrl->val);
		break;
	}

	mutex_unlock(&ov965x->lock);
	return ret;
}

以上都是把相关的操作方法都设置完毕。


    ov965x_get_default_format(&ov965x->format);
    ov965x->frame_size = &ov965x_framesizes[0];
    ov965x->fiv = &ov965x_intervals[0];

static void ov965x_get_default_format(struct v4l2_mbus_framefmt *mf)
{
	mf->width = ov965x_framesizes[0].width;
	mf->height = ov965x_framesizes[0].height;
	mf->colorspace = ov965x_formats[0].colorspace;
	mf->code = ov965x_formats[0].code;
	mf->field = V4L2_FIELD_NONE;
}


static const struct ov965x_framesize ov965x_framesizes[] = {
	{
		.width		= SXGA_WIDTH,
		.height		= SXGA_HEIGHT,
		.regs		= ov965x_sxga_regs,
		.max_exp_lines	= 1048,
	}, {
		.width		= VGA_WIDTH,
		.height		= VGA_HEIGHT,
		.regs		= ov965x_vga_regs,
		.max_exp_lines	= 498,
	}, {
		.width		= QVGA_WIDTH,
		.height		= QVGA_HEIGHT,
		.regs		= ov965x_qvga_regs,
		.max_exp_lines	= 248,
	},
};


static const struct ov965x_pixfmt ov965x_formats[] = {
	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 0x00},
	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG, 0x04},
	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG, 0x0c},
	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 0x08},
};

ret = ov965x_detect_sensor(sd);


ret = v4l2_async_register_subdev(sd);

int v4l2_async_register_subdev(struct v4l2_subdev *sd) :
    list_for_each_entry(notifier, ¬ifier_list, list) {
            asd = v4l2_async_find_match(notifier, sd);
                        	list_for_each_entry(asd, ¬ifier->waiting, list) {
                    		/* bus_type has been verified valid before */
		                    switch (asd->match_type) {
		                            case V4L2_ASYNC_MATCH_CUSTOM:
			                            match = match_custom;
			                            break;
		                            case V4L2_ASYNC_MATCH_DEVNAME:
			                               match = match_devname;
			                            break;
		                            case V4L2_ASYNC_MATCH_I2C:
			                                match = match_i2c;
			                                break;
		                            case V4L2_ASYNC_MATCH_FWNODE:
			                                match = match_fwnode;
			                                break;
		                            default:
			                        /* Cannot happen, unless someone breaks us */
			                            WARN_ON(true);
			                            return NULL;
		                        }

		                    /* match cannot be NULL here */
		                    if (match(sd, asd))
			                    return asd;
     ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
            ret = v4l2_device_register_subdev(v4l2_dev, sd);
            ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
            return v4l2_async_notifier_try_all_subdevs(subdev_notifier);       
     ret = v4l2_async_notifier_try_complete(notifier);  


这个驱动不完善????  还是看vivi吧 ,简单一点

你可能感兴趣的:(camera)