From patchwork Tue May 18 09:23:34 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Xiaolin" X-Patchwork-Id: 3422 Return-path: Envelope-to: mchehab@infradead.org Delivery-date: Tue, 18 May 2010 09:23:51 +0000 Received: from bombadil.infradead.org [18.85.46.34] by pedra with IMAP (fetchmail-6.3.6) for (single-drop); Tue, 18 May 2010 09:54:14 -0300 (BRT) Received: from vger.kernel.org ([209.132.180.67]) by bombadil.infradead.org with esmtp (Exim 4.69 #1 (Red Hat Linux)) id 1OEJ1j-0007GL-Bq; Tue, 18 May 2010 09:23:51 +0000 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756701Ab0ERJXu (ORCPT + 1 other); Tue, 18 May 2010 05:23:50 -0400 Received: from mga14.intel.com ([143.182.124.37]:25114 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754842Ab0ERJXt convert rfc822-to-8bit (ORCPT ); Tue, 18 May 2010 05:23:49 -0400 Received: from azsmga001.ch.intel.com ([10.2.17.19]) by azsmga102.ch.intel.com with ESMTP; 18 May 2010 02:23:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.53,254,1272870000"; d="scan'208,223";a="278374340" Received: from pgsmsx602.gar.corp.intel.com ([10.221.43.81]) by azsmga001.ch.intel.com with ESMTP; 18 May 2010 02:23:47 -0700 Received: from shsmsx602.ccr.corp.intel.com (10.239.4.104) by pgsmsx602.gar.corp.intel.com (10.221.43.81) with Microsoft SMTP Server (TLS) id 8.2.176.0; Tue, 18 May 2010 17:23:44 +0800 Received: from shsmsx501.ccr.corp.intel.com ([10.239.4.141]) by SHSMSX602.ccr.corp.intel.com ([10.239.4.104]) with mapi; Tue, 18 May 2010 17:23:43 +0800 From: "Zhang, Xiaolin" To: "linux-media@vger.kernel.org" Date: Tue, 18 May 2010 17:23:34 +0800 Subject: [PATCH v3 6/8] V4L2 subdev patchset for Intel Moorestown Camera Imaging Subsystem Thread-Topic: [PATCH v3 6/8] V4L2 subdev patchset for Intel Moorestown Camera Imaging Subsystem Thread-Index: Acr2a8nziNd0uk1XQeKxNbquF89f/g== Message-ID: <33AB447FBD802F4E932063B962385B351E895733@shsmsx501.ccr.corp.intel.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-cr-puzzleid: {A0A8EE2B-6314-4D5E-ADC4-C07FEFF37018} x-cr-hashedpuzzle: eKg= BW8Y Be3q B9yb DeBm DvFE E85I FazF GM80 ITiB IZIY K1BZ LDFs LGCb LHyx LNRL; 1; bABpAG4AdQB4AC0AbQBlAGQAaQBhAEAAdgBnAGUAcgAuAGsAZQByAG4AZQBsAC4AbwByAGcA; Sosha1_v1; 7; {A0A8EE2B-6314-4D5E-ADC4-C07FEFF37018}; eABpAGEAbwBsAGkAbgAuAHoAaABhAG4AZwBAAGkAbgB0AGUAbAAuAGMAbwBtAA==; Tue, 18 May 2010 09:23:34 GMT; WwBQAEEAVABDAEgAIAB2ADMAIAA2AC8AOABdACAAVgA0AEwAMgAgAHMAdQBiAGQAZQB2ACAAcABhAHQAYwBoAHMAZQB0ACAAZgBvAHIAIABJAG4AdABlAGwAIABNAG8AbwByAGUAcwB0AG8AdwBuACAAQwBhAG0AZQByAGEAIABJAG0AYQBnAGkAbgBnACAAUwB1AGIAcwB5AHMAdABlAG0A acceptlanguage: en-US MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From 46bf8433c86a7450604a981981c8ce487130dce0 Mon Sep 17 00:00:00 2001 From: Xiaolin Zhang Date: Tue, 18 May 2010 15:25:50 +0800 Subject: [PATCH 6/8] This patch is to add AD5820 VCM (for ov5630)driver support which is based on the video4linux2 sub-dev driver framework. Signed-off-by: Xiaolin Zhang --- drivers/media/video/ov5630_motor.c | 365 ++++++++++++++++++++++++++++++++++++ drivers/media/video/ov5630_motor.h | 77 ++++++++ 2 files changed, 442 insertions(+), 0 deletions(-) create mode 100644 drivers/media/video/ov5630_motor.c create mode 100644 drivers/media/video/ov5630_motor.h diff --git a/drivers/media/video/ov5630_motor.c b/drivers/media/video/ov5630_motor.c new file mode 100644 index 0000000..dfac612 --- /dev/null +++ b/drivers/media/video/ov5630_motor.c @@ -0,0 +1,365 @@ +/* + * Support for Moorestown Langwell Camera Imaging ISP subsystem. + * + * Copyright (c) 2009 Intel Corporation. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * + * Xiaolin Zhang + */ + +#include +#include +#include +#include + +#include "ov5630_motor.h" + +static int mrstov5630_motor_debug; +module_param(mrstov5630_motor_debug, int, 0644); +MODULE_PARM_DESC(mrstov5630_motor_debug, "Debug level (0-1)"); + +#define dprintk(level, fmt, arg...) do { \ + if (mrstov5630_motor_debug >= level) \ + printk(KERN_DEBUG "mrstisp@%s: " fmt "\n", \ + __func__, ## arg); } \ + while (0) + +#define eprintk(fmt, arg...) \ + printk(KERN_ERR "mrstisp@%s: line %d: " fmt "\n", \ + __func__, __LINE__, ## arg); + +static inline struct ov5630_motor *to_motor_config(struct v4l2_subdev *sd) +{ + return container_of(sd, struct ov5630_motor, sd); +} + +static int motor_read(struct i2c_client *c, u16 *reg) +{ + int ret; + struct i2c_msg msg; + u8 msgbuf[2]; + + msgbuf[0] = 0; + msgbuf[1] = 0; + + memset(&msg, 0, sizeof(msg)); + msg.addr = c->addr; + msg.buf = msgbuf; + msg.len = 2; + msg.flags = I2C_M_RD; + + ret = i2c_transfer(c->adapter, &msg, 1); + *reg = (msgbuf[0] << 8 | msgbuf[1]); + + ret = (ret == 1) ? 0 : -1; + return ret; +} + +static int motor_write(struct i2c_client *c, u16 reg) +{ + int ret; + struct i2c_msg msg; + u8 msgbuf[2]; + + memset(&msg, 0, sizeof(msg)); + msgbuf[0] = reg >> 8; + msgbuf[1] = reg; + + msg.addr = c->addr; + msg.flags = 0; + msg.buf = msgbuf; + msg.len = 2; + + ret = i2c_transfer(c->adapter, &msg, 1); + ret = (ret == 1) ? 0 : -1; + return ret; +} + +static int ov5630_motor_goto_position(struct i2c_client *c, + unsigned short code, + struct ov5630_motor *config) +{ + int max_code, min_code; + u8 cmdh, cmdl; + u16 cmd, val = 0; + + max_code = config->macro_code; + min_code = config->infin_code; + + if (code > max_code) + code = max_code; + if (code < min_code) + code = min_code; + + cmdh = (MOTOR_DAC_CODE_H(code)); + cmdl = (MOTOR_DAC_CODE_L(code) | MOTOR_DAC_CTRL_MODE_2(SUB_MODE_4)); + cmd = cmdh << 8 | cmdl; + + motor_write(c, cmd); + msleep(8); + motor_read(c, &val); + + return (cmd == val ? 0 : -1); +} + +int ov5630_motor_init(struct i2c_client *client, struct ov5630_motor *config) +{ + int ret; + int infin_cur, macro_cur; + + infin_cur = max(MOTOR_INFIN_CUR, MOTOR_DAC_MIN_CUR); + macro_cur = min(MOTOR_MACRO_CUR, MOTOR_DAC_MAX_CUR); + + config->infin_cur = infin_cur; + config->macro_cur = macro_cur; + config->infin_code = (int)((infin_cur * MOTOR_DAC_MAX_CODE) + / MOTOR_DAC_MAX_CUR); + config->macro_code = (int)((macro_cur * MOTOR_DAC_MAX_CODE) + / MOTOR_DAC_MAX_CUR); + + config->max_step = ((config->macro_code - config->infin_code) + >> MOTOR_STEP_SHIFT) + 1; + ret = ov5630_motor_goto_position(client, config->infin_code, config); + if (!ret) + config->cur_code = config->infin_code; + else + printk(KERN_ERR "Error while initializing motor\n"); + + return ret; +} + +int ov5630_motor_set_focus(struct i2c_client *c, int step, + struct ov5630_motor *config) +{ + int s_code, ret; + int max_step = config->max_step; + unsigned int val = step; + + dprintk(1, "setting setp %d", step); + if (val > max_step) + val = max_step; + + s_code = (val << MOTOR_STEP_SHIFT); + s_code += config->infin_code; + + ret = ov5630_motor_goto_position(c, s_code, config); + if (!ret) + config->cur_code = s_code; + + return ret; +} + +static int ov5630_motor_s_ctrl(struct v4l2_subdev *sd, + struct v4l2_control *ctrl) +{ + int ret = -EINVAL; + struct i2c_client *c = v4l2_get_subdevdata(sd); + struct ov5630_motor *config = to_motor_config(sd); + if (!sd || !ctrl) + return ret; + + switch (ctrl->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + ret = ov5630_motor_set_focus(c, ctrl->value, config); + if (ret) { + eprintk("error call ov5630_motor_set_focue"); + return ret; + } + break; + default: + dprintk(1, "not supported ctrl id"); + break; + } + return ret; +} + +int ov5630_motor_get_focus(struct i2c_client *c, unsigned int *step, + struct ov5630_motor *config) +{ + int ret_step; + + ret_step = ((config->cur_code - config->infin_code) + >> MOTOR_STEP_SHIFT); + + if (ret_step <= config->max_step) + *step = ret_step; + else + *step = config->max_step; + + return 0; +} + +static int ov5630_motor_g_ctrl(struct v4l2_subdev *sd, + struct v4l2_control *ctrl) +{ + int ret = -EINVAL; + struct i2c_client *c = v4l2_get_subdevdata(sd); + struct ov5630_motor *config = to_motor_config(sd); + if (!sd || !ctrl) + return ret; + + switch (ctrl->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + ret = ov5630_motor_get_focus(c, &ctrl->value, config); + if (ret) { + eprintk("error call ov5630_motor_get_focue"); + return ret; + } + break; + default: + dprintk(1, "not supported ctrl id"); + break; + } + return ret; +} + +static int ov5630_motor_queryctrl(struct v4l2_subdev *sd, + struct v4l2_queryctrl *qc) +{ + int ret = -EINVAL; + struct ov5630_motor *config; + if (!sd) + return -ENODEV; + if (!qc) + return ret; + config = to_motor_config(sd); + + switch (qc->id) { + case V4L2_CID_FOCUS_ABSOLUTE: + ret = v4l2_ctrl_query_fill(qc, 0, config->max_step, + config->max_step, config->cur_code); + break; + default: + dprintk(1, "not supported queryctrl id"); + break; + } + return ret; +} + +static const struct v4l2_subdev_core_ops ov5630_motor_core_ops = { + .g_ctrl = ov5630_motor_g_ctrl, + .s_ctrl = ov5630_motor_s_ctrl, + .queryctrl = ov5630_motor_queryctrl, +}; + +static const struct v4l2_subdev_ops ov5630_motor_ops = { + .core = &ov5630_motor_core_ops, +}; + +static int ov5630_motor_detect(struct i2c_client *client) +{ + struct i2c_adapter *adapter = client->adapter; + int adap_id = i2c_adapter_id(adapter); + + if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { + eprintk("error i2c check func"); + return -ENODEV; + } + + if (adap_id != 1) { + eprintk("adap_id != 1"); + return -ENODEV; + } + + ssleep(1); + + return 0; +} + +static int ov5630_motor_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct ov5630_motor *info; + struct v4l2_subdev *sd; + int ret = -1; + + v4l_info(client, "chip found @ 0x%x (%s)\n", + client->addr << 1, client->adapter->name); + + info = kzalloc(sizeof(struct ov5630_motor), GFP_KERNEL); + if (!info) { + eprintk("fail to malloc for ci_motor"); + ret = -ENOMEM; + goto out; + } + + ret = ov5630_motor_detect(client); + if (ret) { + eprintk("error ov5630_motor_detect"); + goto out_free; + } + + sd = &info->sd; + v4l2_i2c_subdev_init(sd, client, &ov5630_motor_ops); + + ret = ov5630_motor_init(client, info); + if (ret) { + eprintk("error calling ov5630_motor_init"); + goto out_free; + } + + ret = 0; + goto out; + +out_free: + kfree(info); +out: + return ret; +} + +static int ov5630_motor_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + + v4l2_device_unregister_subdev(sd); + kfree(to_motor_config(sd)); + + return 0; +} + +static const struct i2c_device_id ov5630_motor_id[] = { + {"ov5630_motor", 0}, + {} +}; + +MODULE_DEVICE_TABLE(i2c, ov5630_motor_id); + +static struct i2c_driver ov5630_motor_i2c_driver = { + .driver = { + .name = "ov5630_motor", + }, + .probe = ov5630_motor_probe, + .remove = ov5630_motor_remove, + .id_table = ov5630_motor_id, +}; + +static int __init ov5630_motor_drv_init(void) +{ + return i2c_add_driver(&ov5630_motor_i2c_driver); +} + +static void __exit ov5630_motor_drv_cleanup(void) +{ + i2c_del_driver(&ov5630_motor_i2c_driver); +} + +module_init(ov5630_motor_drv_init); +module_exit(ov5630_motor_drv_cleanup); + +MODULE_AUTHOR("Xiaolin Zhang "); +MODULE_DESCRIPTION("A low-level driver for OmniVision 5630 sensors"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/video/ov5630_motor.h b/drivers/media/video/ov5630_motor.h new file mode 100644 index 0000000..44dd799 --- /dev/null +++ b/drivers/media/video/ov5630_motor.h @@ -0,0 +1,77 @@ +/* + * Support for Moorestown Langwell Camera Imaging ISP subsystem. + * + * Copyright (c) 2009 Intel Corporation. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * + * Xiaolin Zhang + */ + +#include +#include + +/* VCM start current (mA) */ +#define MOTOR_INFIN_CUR 15 + +/* VCM max current for Macro (mA) */ +#define MOTOR_MACRO_CUR 90 + +/* DAC output max current (mA) */ +#define MOTOR_DAC_MAX_CUR 100 + +/* DAC output min current (mA) */ +#define MOTOR_DAC_MIN_CUR 3 + +#define MOTOR_DAC_BIT_RES 10 +#define MOTOR_DAC_MAX_CODE ((1 << MOTOR_DAC_BIT_RES) - 1) + +#define MOTOR_STEP_SHIFT 4 + + +/* DAC register related define */ +#define MOTOR_POWER_DOWN (1 << 7) +#define PD_ENABLE (1 << 7) +#define PD_DISABLE (0) + +#define MOTOR_DAC_CODE_H(x) ((x >> 4) & 0x3f) +#define MOTOR_DAC_CODE_L(x) ((x << 4) & 0xf0) + +#define MOTOR_DAC_CTRL_MODE_0 0x00 +#define MOTOR_DAC_CTRL_MODE_1(x) (x & 0x07) +#define MOTOR_DAC_CTRL_MODE_2(x) ((x & 0x07) | 0x08) + +#define SUB_MODE_1 0x01 +#define SUB_MODE_2 0x02 +#define SUB_MODE_3 0x03 +#define SUB_MODE_4 0x04 +#define SUB_MODE_5 0x05 +#define SUB_MODE_6 0x06 +#define SUB_MODE_7 0x07 + +#define OV5630_MOTOR_ADDR (0x18 >> 1) +#define POWER_EN_PIN 7 +#define GPIO_AF_PD 95 + +struct ov5630_motor{ + unsigned int infin_cur; + unsigned int infin_code; + unsigned int macro_cur; + unsigned int macro_code; + unsigned int max_step; + unsigned int cur_code; + struct v4l2_subdev sd; +};