2 * Support for dw9718 vcm driver.
4 * Copyright (c) 2014 Intel Corporation. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <linux/delay.h>
25 static struct dw9718_device dw9718_dev;
27 static int dw9718_i2c_rd8(struct i2c_client *client, u8 reg, u8 *val)
29 struct i2c_msg msg[2];
32 msg[0].addr = DW9718_VCM_ADDR;
37 msg[1].addr = DW9718_VCM_ADDR;
38 msg[1].flags = I2C_M_RD;
43 if (i2c_transfer(client->adapter, msg, 2) != 2)
50 static int dw9718_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
53 u8 buf[2] = { reg, val};
55 msg.addr = DW9718_VCM_ADDR;
57 msg.len = sizeof(buf);
60 if (i2c_transfer(client->adapter, &msg, 1) != 1)
66 static int dw9718_i2c_wr16(struct i2c_client *client, u8 reg, u16 val)
69 u8 buf[3] = { reg, (u8)(val >> 8), (u8)(val & 0xff)};
71 msg.addr = DW9718_VCM_ADDR;
73 msg.len = sizeof(buf);
76 if (i2c_transfer(client->adapter, &msg, 1) != 1)
82 int dw9718_t_focus_abs(struct v4l2_subdev *sd, s32 value)
84 struct i2c_client *client = v4l2_get_subdevdata(sd);
87 value = clamp(value, 0, DW9718_MAX_FOCUS_POS);
88 ret = dw9718_i2c_wr16(client, DW9718_DATA_M, value);
89 /*pr_info("%s: value = %d\n", __func__, value);*/
93 getnstimeofday(&dw9718_dev.focus_time);
94 dw9718_dev.focus = value;
99 int dw9718_vcm_power_up(struct v4l2_subdev *sd)
101 struct i2c_client *client = v4l2_get_subdevdata(sd);
105 if (dw9718_dev.power_on)
109 ret = dw9718_dev.platform_data->power_ctrl(sd, 1);
111 dev_err(&client->dev, "DW9718_PD power_ctrl failed %d\n", ret);
114 /* Wait for VBAT to stabilize */
118 ret = dw9718_i2c_rd8(client, DW9718_SACT, &value);
120 dev_err(&client->dev, "read DW9718_SACT failed %d\n", ret);
124 * WORKAROUND: for module P8V12F-203 which are used on
125 * Cherrytrail Refresh Davis Reef AoB, register SACT is not
126 * returning default value as spec. But VCM works as expected and
127 * root cause is still under discussion with vendor.
128 * workaround here to avoid aborting the power up sequence and just
129 * give a warning about this error.
131 if (value != DW9718_SACT_DEFAULT_VAL)
132 dev_warn(&client->dev, "%s error, incorrect ID\n", __func__);
134 /* Initialize according to recommended settings */
135 ret = dw9718_i2c_wr8(client, DW9718_CONTROL,
136 DW9718_CONTROL_SW_LINEAR |
137 DW9718_CONTROL_S_SAC4 |
138 DW9718_CONTROL_OCP_DISABLE |
139 DW9718_CONTROL_UVLO_DISABLE);
141 dev_err(&client->dev, "write DW9718_CONTROL failed %d\n", ret);
144 ret = dw9718_i2c_wr8(client, DW9718_SACT,
145 DW9718_SACT_MULT_TWO |
146 DW9718_SACT_PERIOD_8_8MS);
148 dev_err(&client->dev, "write DW9718_SACT failed %d\n", ret);
152 ret = dw9718_t_focus_abs(sd, dw9718_dev.focus);
155 dw9718_dev.initialized = true;
156 dw9718_dev.power_on = 1;
161 dev_err(&client->dev, "%s error, powerup failed\n", __func__);
162 dw9718_dev.platform_data->power_ctrl(sd, 0);
166 int dw9718_vcm_power_down(struct v4l2_subdev *sd)
168 struct i2c_client *client = v4l2_get_subdevdata(sd);
171 if (!dw9718_dev.power_on)
174 ret = dw9718_dev.platform_data->power_ctrl(sd, 0);
176 dev_err(&client->dev, "%s power_ctrl failed\n",
180 dw9718_dev.power_on = 0;
185 int dw9718_q_focus_status(struct v4l2_subdev *sd, s32 *value)
187 static const struct timespec move_time = {
191 struct timespec current_time, finish_time, delta_time;
193 getnstimeofday(¤t_time);
194 finish_time = timespec_add(dw9718_dev.focus_time, move_time);
195 delta_time = timespec_sub(current_time, finish_time);
196 if (delta_time.tv_sec >= 0 && delta_time.tv_nsec >= 0) {
197 *value = ATOMISP_FOCUS_HP_COMPLETE |
198 ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
200 *value = ATOMISP_FOCUS_STATUS_MOVING |
201 ATOMISP_FOCUS_HP_IN_PROGRESS;
207 int dw9718_t_focus_rel(struct v4l2_subdev *sd, s32 value)
209 return dw9718_t_focus_abs(sd, dw9718_dev.focus + value);
212 int dw9718_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
214 *value = dw9718_dev.focus;
217 int dw9718_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
222 int dw9718_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
227 int dw9718_vcm_init(struct v4l2_subdev *sd)
229 dw9718_dev.platform_data = camera_get_af_platform_data();
230 dw9718_dev.focus = DW9718_DEFAULT_FOCUS_POSITION;
231 dw9718_dev.power_on = 0;
232 return (NULL == dw9718_dev.platform_data) ? -ENODEV : 0;