⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mt9m111.c

📁 trident tm5600的linux驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)		return -EINVAL;	if (id->match_chip != mt9m111->client->addr)		return -ENODEV;	id->ident	= mt9m111->model;	id->revision	= 0;	return 0;}#ifdef CONFIG_VIDEO_ADV_DEBUGstatic int mt9m111_get_register(struct soc_camera_device *icd,				struct v4l2_register *reg){	int val;	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)		return -EINVAL;	if (reg->match_chip != mt9m111->client->addr)		return -ENODEV;	val = mt9m111_reg_read(icd, reg->reg);	reg->val = (u64)val;	if (reg->val > 0xffff)		return -EIO;	return 0;}static int mt9m111_set_register(struct soc_camera_device *icd,				struct v4l2_register *reg){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x2ff)		return -EINVAL;	if (reg->match_chip != mt9m111->client->addr)		return -ENODEV;	if (mt9m111_reg_write(icd, reg->reg, reg->val) < 0)		return -EIO;	return 0;}#endifstatic const struct v4l2_queryctrl mt9m111_controls[] = {	{		.id		= V4L2_CID_VFLIP,		.type		= V4L2_CTRL_TYPE_BOOLEAN,		.name		= "Flip Verticaly",		.minimum	= 0,		.maximum	= 1,		.step		= 1,		.default_value	= 0,	}, {		.id		= V4L2_CID_HFLIP,		.type		= V4L2_CTRL_TYPE_BOOLEAN,		.name		= "Flip Horizontaly",		.minimum	= 0,		.maximum	= 1,		.step		= 1,		.default_value	= 0,	}, {	/* gain = 1/32*val (=>gain=1 if val==32) */		.id		= V4L2_CID_GAIN,		.type		= V4L2_CTRL_TYPE_INTEGER,		.name		= "Gain",		.minimum	= 0,		.maximum	= 63 * 2 * 2,		.step		= 1,		.default_value	= 32,		.flags		= V4L2_CTRL_FLAG_SLIDER,	}, {		.id		= V4L2_CID_EXPOSURE_AUTO,		.type		= V4L2_CTRL_TYPE_BOOLEAN,		.name		= "Auto Exposure",		.minimum	= 0,		.maximum	= 1,		.step		= 1,		.default_value	= 1,	}};static int mt9m111_video_probe(struct soc_camera_device *);static void mt9m111_video_remove(struct soc_camera_device *);static int mt9m111_get_control(struct soc_camera_device *,			       struct v4l2_control *);static int mt9m111_set_control(struct soc_camera_device *,			       struct v4l2_control *);static int mt9m111_resume(struct soc_camera_device *icd);static int mt9m111_init(struct soc_camera_device *icd);static int mt9m111_release(struct soc_camera_device *icd);static struct soc_camera_ops mt9m111_ops = {	.owner			= THIS_MODULE,	.probe			= mt9m111_video_probe,	.remove			= mt9m111_video_remove,	.init			= mt9m111_init,	.resume			= mt9m111_resume,	.release		= mt9m111_release,	.start_capture		= mt9m111_start_capture,	.stop_capture		= mt9m111_stop_capture,	.set_fmt_cap		= mt9m111_set_fmt_cap,	.try_fmt_cap		= mt9m111_try_fmt_cap,	.query_bus_param	= mt9m111_query_bus_param,	.set_bus_param		= mt9m111_set_bus_param,	.controls		= mt9m111_controls,	.num_controls		= ARRAY_SIZE(mt9m111_controls),	.get_control		= mt9m111_get_control,	.set_control		= mt9m111_set_control,	.get_chip_id		= mt9m111_get_chip_id,#ifdef CONFIG_VIDEO_ADV_DEBUG	.get_register		= mt9m111_get_register,	.set_register		= mt9m111_set_register,#endif};static int mt9m111_set_flip(struct soc_camera_device *icd, int flip, int mask){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	int ret;	if (mt9m111->context == HIGHPOWER) {		if (flip)			ret = reg_set(READ_MODE_B, mask);		else			ret = reg_clear(READ_MODE_B, mask);	} else {		if (flip)			ret = reg_set(READ_MODE_A, mask);		else			ret = reg_clear(READ_MODE_A, mask);	}	return ret;}static int mt9m111_get_global_gain(struct soc_camera_device *icd){	unsigned int data, gain;	data = reg_read(GLOBAL_GAIN);	if (data >= 0)		gain = ((data & (1 << 10)) * 2)			| ((data & (1 << 9)) * 2)			| (data & 0x2f);	else		gain = data;	return gain;}static int mt9m111_set_global_gain(struct soc_camera_device *icd, int gain){	u16 val;	if (gain > 63 * 2 * 2)		return -EINVAL;	icd->gain = gain;	if ((gain >= 64 * 2) && (gain < 63 * 2 * 2))		val = (1 << 10) | (1 << 9) | (gain / 4);	else if ((gain >= 64) && (gain < 64 * 2))		val = (1 << 9) | (gain / 2);	else		val = gain;	return reg_write(GLOBAL_GAIN, val);}static int mt9m111_set_autoexposure(struct soc_camera_device *icd, int on){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	int ret;	if (on)		ret = reg_set(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);	else		ret = reg_clear(OPER_MODE_CTRL, MT9M111_OPMODE_AUTOEXPO_EN);	if (!ret)		mt9m111->autoexposure = on;	return ret;}static int mt9m111_get_control(struct soc_camera_device *icd,			       struct v4l2_control *ctrl){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	int data;	switch (ctrl->id) {	case V4L2_CID_VFLIP:		if (mt9m111->context == HIGHPOWER)			data = reg_read(READ_MODE_B);		else			data = reg_read(READ_MODE_A);		if (data < 0)			return -EIO;		ctrl->value = !!(data & MT9M111_RMB_MIRROR_ROWS);		break;	case V4L2_CID_HFLIP:		if (mt9m111->context == HIGHPOWER)			data = reg_read(READ_MODE_B);		else			data = reg_read(READ_MODE_A);		if (data < 0)			return -EIO;		ctrl->value = !!(data & MT9M111_RMB_MIRROR_COLS);		break;	case V4L2_CID_GAIN:		data = mt9m111_get_global_gain(icd);		if (data < 0)			return data;		ctrl->value = data;		break;	case V4L2_CID_EXPOSURE_AUTO:		ctrl->value = mt9m111->autoexposure;		break;	}	return 0;}static int mt9m111_set_control(struct soc_camera_device *icd,			       struct v4l2_control *ctrl){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	const struct v4l2_queryctrl *qctrl;	int ret;	qctrl = soc_camera_find_qctrl(&mt9m111_ops, ctrl->id);	if (!qctrl)		return -EINVAL;	switch (ctrl->id) {	case V4L2_CID_VFLIP:		mt9m111->vflip = ctrl->value;		ret = mt9m111_set_flip(icd, ctrl->value,					MT9M111_RMB_MIRROR_ROWS);		break;	case V4L2_CID_HFLIP:		mt9m111->hflip = ctrl->value;		ret = mt9m111_set_flip(icd, ctrl->value,					MT9M111_RMB_MIRROR_COLS);		break;	case V4L2_CID_GAIN:		ret = mt9m111_set_global_gain(icd, ctrl->value);		break;	case V4L2_CID_EXPOSURE_AUTO:		ret =  mt9m111_set_autoexposure(icd, ctrl->value);		break;	default:		ret = -EINVAL;	}	return ret;}static int mt9m111_restore_state(struct soc_camera_device *icd){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	mt9m111_set_context(icd, mt9m111->context);	mt9m111_set_pixfmt(icd, mt9m111->pixfmt);	mt9m111_setup_rect(icd);	mt9m111_set_flip(icd, mt9m111->hflip, MT9M111_RMB_MIRROR_COLS);	mt9m111_set_flip(icd, mt9m111->vflip, MT9M111_RMB_MIRROR_ROWS);	mt9m111_set_global_gain(icd, icd->gain);	mt9m111_set_autoexposure(icd, mt9m111->autoexposure);	return 0;}static int mt9m111_resume(struct soc_camera_device *icd){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	int ret = 0;	if (mt9m111->powered) {		ret = mt9m111_enable(icd);		if (!ret)			ret = mt9m111_reset(icd);		if (!ret)			ret = mt9m111_restore_state(icd);	}	return ret;}static int mt9m111_init(struct soc_camera_device *icd){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	int ret;	mt9m111->context = HIGHPOWER;	ret = mt9m111_enable(icd);	if (!ret)		ret = mt9m111_reset(icd);	if (!ret)		ret = mt9m111_set_context(icd, mt9m111->context);	if (!ret)		ret = mt9m111_set_autoexposure(icd, mt9m111->autoexposure);	if (ret)		dev_err(&icd->dev, "mt9m111 init failed: %d\n", ret);	return ret;}static int mt9m111_release(struct soc_camera_device *icd){	int ret;	ret = mt9m111_disable(icd);	if (ret < 0)		dev_err(&icd->dev, "mt9m111 release failed: %d\n", ret);	return ret;}/* * Interface active, can use i2c. If it fails, it can indeed mean, that * this wasn't our capture interface, so, we wait for the right one */static int mt9m111_video_probe(struct soc_camera_device *icd){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	s32 data;	int ret;	/*	 * We must have a parent by now. And it cannot be a wrong one.	 * So this entire test is completely redundant.	 */	if (!icd->dev.parent ||	    to_soc_camera_host(icd->dev.parent)->nr != icd->iface)		return -ENODEV;	ret = mt9m111_enable(icd);	if (ret)		goto ei2c;	ret = mt9m111_reset(icd);	if (ret)		goto ei2c;	data = reg_read(CHIP_VERSION);	switch (data) {	case 0x143a:		mt9m111->model = V4L2_IDENT_MT9M111;		icd->formats = mt9m111_colour_formats;		icd->num_formats = ARRAY_SIZE(mt9m111_colour_formats);		break;	default:		ret = -ENODEV;		dev_err(&icd->dev,			"No MT9M111 chip detected, register read %x\n", data);		goto ei2c;	}	dev_info(&icd->dev, "Detected a MT9M111 chip ID 0x143a\n");	ret = soc_camera_video_start(icd);	if (ret)		goto eisis;	mt9m111->autoexposure = 1;	mt9m111->swap_rgb_even_odd = 1;	mt9m111->swap_rgb_red_blue = 1;	return 0;eisis:ei2c:	return ret;}static void mt9m111_video_remove(struct soc_camera_device *icd){	struct mt9m111 *mt9m111 = container_of(icd, struct mt9m111, icd);	dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m111->client->addr,		mt9m111->icd.dev.parent, mt9m111->icd.vdev);	soc_camera_video_stop(&mt9m111->icd);}#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)static int mt9m111_probe(struct i2c_client *client)#elsestatic int mt9m111_probe(struct i2c_client *client,			 const struct i2c_device_id *did)#endif{	struct mt9m111 *mt9m111;	struct soc_camera_device *icd;	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);	struct soc_camera_link *icl = client->dev.platform_data;	int ret;	if (!icl) {		dev_err(&client->dev, "MT9M111 driver needs platform data\n");		return -EINVAL;	}	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {		dev_warn(&adapter->dev,			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");		return -EIO;	}	mt9m111 = kzalloc(sizeof(struct mt9m111), GFP_KERNEL);	if (!mt9m111)		return -ENOMEM;	mt9m111->client = client;	i2c_set_clientdata(client, mt9m111);	/* Second stage probe - when a capture adapter is there */	icd 		= &mt9m111->icd;	icd->ops	= &mt9m111_ops;	icd->control	= &client->dev;	icd->x_min	= MT9M111_MIN_DARK_COLS;	icd->y_min	= MT9M111_MIN_DARK_ROWS;	icd->x_current	= icd->x_min;	icd->y_current	= icd->y_min;	icd->width_min	= MT9M111_MIN_DARK_ROWS;	icd->width_max	= MT9M111_MAX_WIDTH;	icd->height_min	= MT9M111_MIN_DARK_COLS;	icd->height_max	= MT9M111_MAX_HEIGHT;	icd->y_skip_top	= 0;	icd->iface	= icl->bus_id;	ret = soc_camera_device_register(icd);	if (ret)		goto eisdr;	return 0;eisdr:	kfree(mt9m111);	return ret;}static int mt9m111_remove(struct i2c_client *client){	struct mt9m111 *mt9m111 = i2c_get_clientdata(client);	soc_camera_device_unregister(&mt9m111->icd);	kfree(mt9m111);	return 0;}#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)static const struct i2c_device_id mt9m111_id[] = {	{ "mt9m111", 0 },	{ }};MODULE_DEVICE_TABLE(i2c, mt9m111_id);#endifstatic struct i2c_driver mt9m111_i2c_driver = {	.driver = {		.name = "mt9m111",	},	.probe		= mt9m111_probe,	.remove		= mt9m111_remove,#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)	.id_table	= mt9m111_id,#endif};static int __init mt9m111_mod_init(void){	return i2c_add_driver(&mt9m111_i2c_driver);}static void __exit mt9m111_mod_exit(void){	i2c_del_driver(&mt9m111_i2c_driver);}module_init(mt9m111_mod_init);module_exit(mt9m111_mod_exit);MODULE_DESCRIPTION("Micron MT9M111 Camera driver");MODULE_AUTHOR("Robert Jarzmik");MODULE_LICENSE("GPL");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -