aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/drivers/staging/media/atomisp/i2c/imx/drv201.c
blob: 6d9d4c968722c92ef20d5d53472ae066da960e47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/kmod.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <media/v4l2-device.h>
#include <asm/intel-mid.h>

#include "drv201.h"

static struct drv201_device drv201_dev;

static int drv201_i2c_rd8(struct i2c_client *client, u8 reg, u8 *val)
{
	struct i2c_msg msg[2];
	u8 buf[2];
	buf[0] = reg;
	buf[1] = 0;

	msg[0].addr = DRV201_VCM_ADDR;
	msg[0].flags = 0;
	msg[0].len = 1;
	msg[0].buf = &buf[0];

	msg[1].addr = DRV201_VCM_ADDR;
	msg[1].flags = I2C_M_RD;
	msg[1].len = 1;
	msg[1].buf = &buf[1];
	*val = 0;
	if (i2c_transfer(client->adapter, msg, 2) != 2)
		return -EIO;
	*val = buf[1];
	return 0;
}

static int drv201_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
{
	struct i2c_msg msg;
	u8 buf[2];
	buf[0] = reg;
	buf[1] = val;
	msg.addr = DRV201_VCM_ADDR;
	msg.flags = 0;
	msg.len = 2;
	msg.buf = &buf[0];
	if (i2c_transfer(client->adapter, &msg, 1) != 1)
		return -EIO;
	return 0;
}

static int drv201_i2c_wr16(struct i2c_client *client, u8 reg, u16 val)
{
	struct i2c_msg msg;
	u8 buf[3];
	buf[0] = reg;
	buf[1] = (u8)(val >> 8);
	buf[2] = (u8)(val & 0xff);
	msg.addr = DRV201_VCM_ADDR;
	msg.flags = 0;
	msg.len = 3;
	msg.buf = &buf[0];
	if (i2c_transfer(client->adapter, &msg, 1) != 1)
		return -EIO;
	return 0;
}

int drv201_vcm_power_up(struct v4l2_subdev *sd)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret;
	u8 value;

	/* Enable power */
	ret = drv201_dev.platform_data->power_ctrl(sd, 1);
	if (ret)
		return ret;
	/* Wait for VBAT to stabilize */
	udelay(1);
	/*
	 * Jiggle SCL pin to wake up device.
	 * Drv201 expect SCL from low to high to wake device up.
	 * So the 1st access to i2c would fail.
	 * Using following function to wake device up.
	 */
	drv201_i2c_wr8(client, DRV201_CONTROL, DRV201_RESET);

	/* Need 100us to transit from SHUTDOWN to STANDBY*/
	usleep_range(WAKEUP_DELAY_US, WAKEUP_DELAY_US * 10);

	/* Reset device */
	ret = drv201_i2c_wr8(client, DRV201_CONTROL, DRV201_RESET);
	if (ret < 0)
		goto fail_powerdown;

	/* Detect device */
	ret = drv201_i2c_rd8(client, DRV201_CONTROL, &value);
	if (ret < 0)
		goto fail_powerdown;
	if (value != DEFAULT_CONTROL_VAL) {
		ret = -ENXIO;
		goto fail_powerdown;
	}

	drv201_dev.focus = DRV201_MAX_FOCUS_POS;
	drv201_dev.initialized = true;

	return 0;
fail_powerdown:
	drv201_dev.platform_data->power_ctrl(sd, 0);
	return ret;
}

int drv201_vcm_power_down(struct v4l2_subdev *sd)
{
	return drv201_dev.platform_data->power_ctrl(sd, 0);
}


static int drv201_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	u16 data = val & VCM_CODE_MASK;

	if (!drv201_dev.initialized)
		return -ENODEV;
	return drv201_i2c_wr16(client, DRV201_VCM_CURRENT, data);
}

int drv201_t_focus_abs(struct v4l2_subdev *sd, s32 value)
{
	int ret;

	value = clamp(value, 0, DRV201_MAX_FOCUS_POS);
	ret = drv201_t_focus_vcm(sd, value);
	if (ret == 0) {
		drv201_dev.number_of_steps = value - drv201_dev.focus;
		drv201_dev.focus = value;
		getnstimeofday(&(drv201_dev.timestamp_t_focus_abs));
	}

	return ret;
}

int drv201_t_focus_rel(struct v4l2_subdev *sd, s32 value)
{
	return drv201_t_focus_abs(sd, drv201_dev.focus + value);
}

int drv201_q_focus_status(struct v4l2_subdev *sd, s32 *value)
{
	u32 status = 0;
	struct timespec temptime;
	const struct timespec timedelay = {
		0,
		min_t(u32, abs(drv201_dev.number_of_steps)*DELAY_PER_STEP_NS,
			DELAY_MAX_PER_STEP_NS),
	};

	ktime_get_ts(&temptime);

	temptime = timespec_sub(temptime, (drv201_dev.timestamp_t_focus_abs));

	if (timespec_compare(&temptime, &timedelay) <= 0) {
		status |= ATOMISP_FOCUS_STATUS_MOVING;
		status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
	} else {
		status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
		status |= ATOMISP_FOCUS_HP_COMPLETE;
	}
	*value = status;

	return 0;
}

int drv201_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
{
	s32 val;

	drv201_q_focus_status(sd, &val);

	if (val & ATOMISP_FOCUS_STATUS_MOVING)
		*value  = drv201_dev.focus - drv201_dev.number_of_steps;
	else
		*value  = drv201_dev.focus;

	return 0;
}

int drv201_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
{
	return 0;
}

int drv201_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
{
	return 0;
}