1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bitops.h>
3 #include <linux/device.h>
4 #include <linux/delay.h>
5 #include <linux/errno.h>
7 #include <linux/gpio.h>
8 #include <linux/init.h>
11 #include <linux/kernel.h>
13 #include <linux/kmod.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <media/v4l2-device.h>
20 #include <asm/intel-mid.h>
24 static struct drv201_device drv201_dev;
26 static int drv201_i2c_rd8(struct i2c_client *client, u8 reg, u8 *val)
28 struct i2c_msg msg[2];
33 msg[0].addr = DRV201_VCM_ADDR;
38 msg[1].addr = DRV201_VCM_ADDR;
39 msg[1].flags = I2C_M_RD;
43 if (i2c_transfer(client->adapter, msg, 2) != 2)
49 static int drv201_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
55 msg.addr = DRV201_VCM_ADDR;
59 if (i2c_transfer(client->adapter, &msg, 1) != 1)
64 static int drv201_i2c_wr16(struct i2c_client *client, u8 reg, u16 val)
69 buf[1] = (u8)(val >> 8);
70 buf[2] = (u8)(val & 0xff);
71 msg.addr = DRV201_VCM_ADDR;
75 if (i2c_transfer(client->adapter, &msg, 1) != 1)
80 int drv201_vcm_power_up(struct v4l2_subdev *sd)
82 struct i2c_client *client = v4l2_get_subdevdata(sd);
87 ret = drv201_dev.platform_data->power_ctrl(sd, 1);
90 /* Wait for VBAT to stabilize */
93 * Jiggle SCL pin to wake up device.
94 * Drv201 expect SCL from low to high to wake device up.
95 * So the 1st access to i2c would fail.
96 * Using following function to wake device up.
98 drv201_i2c_wr8(client, DRV201_CONTROL, DRV201_RESET);
100 /* Need 100us to transit from SHUTDOWN to STANDBY*/
101 usleep_range(WAKEUP_DELAY_US, WAKEUP_DELAY_US * 10);
104 ret = drv201_i2c_wr8(client, DRV201_CONTROL, DRV201_RESET);
109 ret = drv201_i2c_rd8(client, DRV201_CONTROL, &value);
112 if (value != DEFAULT_CONTROL_VAL) {
117 drv201_dev.focus = DRV201_MAX_FOCUS_POS;
118 drv201_dev.initialized = true;
122 drv201_dev.platform_data->power_ctrl(sd, 0);
126 int drv201_vcm_power_down(struct v4l2_subdev *sd)
128 return drv201_dev.platform_data->power_ctrl(sd, 0);
132 static int drv201_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
134 struct i2c_client *client = v4l2_get_subdevdata(sd);
135 u16 data = val & VCM_CODE_MASK;
137 if (!drv201_dev.initialized)
139 return drv201_i2c_wr16(client, DRV201_VCM_CURRENT, data);
142 int drv201_t_focus_abs(struct v4l2_subdev *sd, s32 value)
146 value = clamp(value, 0, DRV201_MAX_FOCUS_POS);
147 ret = drv201_t_focus_vcm(sd, value);
149 drv201_dev.number_of_steps = value - drv201_dev.focus;
150 drv201_dev.focus = value;
151 getnstimeofday(&(drv201_dev.timestamp_t_focus_abs));
157 int drv201_t_focus_rel(struct v4l2_subdev *sd, s32 value)
159 return drv201_t_focus_abs(sd, drv201_dev.focus + value);
162 int drv201_q_focus_status(struct v4l2_subdev *sd, s32 *value)
165 struct timespec temptime;
166 const struct timespec timedelay = {
168 min_t(u32, abs(drv201_dev.number_of_steps)*DELAY_PER_STEP_NS,
169 DELAY_MAX_PER_STEP_NS),
172 ktime_get_ts(&temptime);
174 temptime = timespec_sub(temptime, (drv201_dev.timestamp_t_focus_abs));
176 if (timespec_compare(&temptime, &timedelay) <= 0) {
177 status |= ATOMISP_FOCUS_STATUS_MOVING;
178 status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
180 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
181 status |= ATOMISP_FOCUS_HP_COMPLETE;
188 int drv201_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
192 drv201_q_focus_status(sd, &val);
194 if (val & ATOMISP_FOCUS_STATUS_MOVING)
195 *value = drv201_dev.focus - drv201_dev.number_of_steps;
197 *value = drv201_dev.focus;
202 int drv201_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
207 int drv201_t_vcm_timing(struct v4l2_subdev *sd, s32 value)