summaryrefslogtreecommitdiff
path: root/drivers/media/video/wmt_v4l2/sensors/gc0309/gc0309.c
blob: 488a677f316ec716f5345ef33ecc3483b0b2c4c8 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

#include "../cmos-subdev.h"
#include "../../wmt-vid.h"
#include "gc0309.h"

#define sensor_write_array(sd, arr, sz) \
	cmos_init_8bit_addr(arr, sz, (sd)->i2c_addr)

#define sensor_read(sd, reg) \
	wmt_vid_i2c_read(sd->i2c_addr, reg)

#define sensor_write(sd, reg, val) \
	wmt_vid_i2c_write(sd->i2c_addr, reg, val)

struct cmos_win_size {
	char		*name;
	int		width;
	int		height;
	uint8_t 	*regs;
	size_t		size;
};

#define CMOS_WIN_SIZE(n, w, h, r) \
	{.name = n, .width = w , .height = h, .regs = r, .size = ARRAY_SIZE(r) }

#define FILP_REG_INIT_VALUE	  0x10
#define DELAY_INTERVAL		50
#define RETRY_TIMES              10

static const struct cmos_win_size cmos_supported_win_sizes[] = {
//	CMOS_WIN_SIZE("QVGA", 320, 240, gc0309_320x240),
	CMOS_WIN_SIZE("VGA",  640, 480, gc0309_640x480),
};

static const struct cmos_win_size *cmos_select_win(u32 *width, u32 *height)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(cmos_supported_win_sizes); i++) {
		if (cmos_supported_win_sizes[i].width == *width &&
		    cmos_supported_win_sizes[i].height == *height) {
			*width = cmos_supported_win_sizes[i].width;
			*height = cmos_supported_win_sizes[i].height;
			return &cmos_supported_win_sizes[i];
		}
	}
	return NULL;
}

static int sensor_s_wb(struct cmos_subdev *sd, enum v4l2_wb_mode value)
{
	uint8_t *regs;
	size_t size;

	switch (value) {
	case WHITE_BALANCE_AUTO:
		regs = gc0309_wb_auto;
		size = ARRAY_SIZE(gc0309_wb_auto);
		break;
	case WHITE_BALANCE_INCANDESCENCE:
		regs = gc0309_wb_incandescent;
		size = ARRAY_SIZE(gc0309_wb_incandescent);
		break;
	case WHITE_BALANCE_DAYLIGHT:
		regs = gc0309_wb_daylight;
		size = ARRAY_SIZE(gc0309_wb_daylight);
		break;
	case WHITE_BALANCE_CLOUDY:
		regs = gc0309_wb_cloudy;
		size = ARRAY_SIZE(gc0309_wb_cloudy);
		break;
	case WHITE_BALANCE_FLUORESCENT:
		regs = gc0309_wb_fluorescent;
		size = ARRAY_SIZE(gc0309_wb_fluorescent);
		break;
	default:
		return -EINVAL;
	}

	sensor_write_array(sd, regs, size);
	return 0;
}

static int sensor_s_scenemode(struct cmos_subdev *sd, enum v4l2_scene_mode value)
{
	uint8_t *regs;
	size_t size;

	switch (value) {
	case SCENE_MODE_AUTO:
		regs = gc0309_scene_mode_auto;
		size = ARRAY_SIZE(gc0309_scene_mode_auto);
		break;
	case SCENE_MODE_NIGHTSHOT:
		regs = gc0309_scene_mode_night;
		size = ARRAY_SIZE(gc0309_scene_mode_night);
		break;
	default:
		return -EINVAL;
	}

	sensor_write_array(sd, regs, size);
	return 0;
}

static int sensor_s_exposure(struct cmos_subdev *sd, int value)
{
	uint8_t *regs;
	size_t size;

	switch (value) {
	case -2:
		regs = gc0309_exposure_neg6;
		size = ARRAY_SIZE(gc0309_exposure_neg6);
		break;
	case -1:
		regs = gc0309_exposure_neg3;
		size = ARRAY_SIZE(gc0309_exposure_neg3);
		break;
	case 0:
		regs = gc0309_exposure_zero;
		size = ARRAY_SIZE(gc0309_exposure_zero);
		break;
	case 1:
		regs = gc0309_exposure_pos3;
		size = ARRAY_SIZE(gc0309_exposure_pos3);
		break;
	case 2:
		regs = gc0309_exposure_pos6;
		size = ARRAY_SIZE(gc0309_exposure_pos6);
		break;
	default:
		return -EINVAL;
	}

	sensor_write_array(sd, regs, size);
	return 0;
}

static int sensor_s_hflip(struct cmos_subdev *sd, int value)
{
	uint8_t data,tmp=0;
	uint8_t retry_times=0;
	
	sensor_write(sd, 0xfe, 0x00); // set page0

	data = sensor_read(sd, 0x14);

	switch (value) {
	case 0:
		data &= ~0x01;
		break;
	case 1:
		data |= 0x01;
		break;
	default:
		return -EINVAL;
	}
	
	tmp=data | FILP_REG_INIT_VALUE;
	for(retry_times=0;retry_times<RETRY_TIMES;retry_times++){
		sensor_write(sd, 0x14, tmp);
		msleep(50+retry_times*DELAY_INTERVAL);
		if(sensor_read(sd, 0x14)==tmp)
			break;
	}

	return 0;
}

static int sensor_s_vflip(struct cmos_subdev *sd, int value)
{
	uint8_t data,tmp=0;
	uint8_t retry_times=0;

	sensor_write(sd, 0xfe, 0x00); // set page0

	data = sensor_read(sd, 0x14);

	switch (value) {
	case 0:
		data &= ~0x02;
		break;
	case 1:
		data |= 0x02;
		break;
	default:
		return -EINVAL;
	}

	tmp=data | FILP_REG_INIT_VALUE;
	for(retry_times=0;retry_times<RETRY_TIMES;retry_times++){
		sensor_write(sd, 0x14, tmp);
		msleep(50+retry_times*DELAY_INTERVAL);
		if(sensor_read(sd, 0x14)==tmp)
			break;
	}

	return 0;
}

static int sensor_queryctrl(struct cmos_subdev *sd, struct v4l2_queryctrl *qc)
{
	switch (qc->id) {
	case V4L2_CID_VFLIP:
	case V4L2_CID_HFLIP:
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
	case V4L2_CID_CAMERA_SCENE_MODE:
		return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
	case V4L2_CID_DO_WHITE_BALANCE:
		return v4l2_ctrl_query_fill(qc, 0, 3, 1, 0);
	case V4L2_CID_EXPOSURE:
		return v4l2_ctrl_query_fill(qc, -2, 2, 1, 0);
	}
	return -EINVAL;
}

static int sensor_s_ctrl(struct cmos_subdev *sd, struct v4l2_control *ctrl)
{
	switch (ctrl->id) {
	case V4L2_CID_CAMERA_SCENE_MODE:
		return sensor_s_scenemode(sd, ctrl->value);
	case V4L2_CID_DO_WHITE_BALANCE:	
		return sensor_s_wb(sd, ctrl->value);	
	case V4L2_CID_EXPOSURE:
		return sensor_s_exposure(sd, ctrl->value);	
	case V4L2_CID_HFLIP:
		return sensor_s_hflip(sd, ctrl->value);
	case V4L2_CID_VFLIP:
		return sensor_s_vflip(sd, ctrl->value);
	default:
	case WMT_V4L2_CID_CAMERA_ANTIBANDING:
		return -EINVAL;
	}
	return -EINVAL;
}

static int sensor_g_mbus_fmt(struct cmos_subdev *sd,
			     struct v4l2_mbus_framefmt *mf)
{
	return -EINVAL;
}

static int sensor_s_mbus_fmt(struct cmos_subdev *sd,
			     struct v4l2_mbus_framefmt *mf)
{
	const struct cmos_win_size *win;

	win = cmos_select_win(&mf->width, &mf->height);
	if (!win) {
		pr_err("%s, s_mbus_fmt failed, width %d, height %d\n", 
		       sd->name, mf->width, mf->height);
		return -EINVAL;
	}

	sensor_write_array(sd, win->regs, win->size);
	return 0;
}

static int sensor_try_mbus_fmt(struct cmos_subdev *sd,
			       struct v4l2_mbus_framefmt *mf)
{
	return 0;
}

static int sensor_enum_framesizes(struct cmos_subdev *sd,
		struct v4l2_frmsizeenum *fsize)
{
	int i;
	int num_valid = -1;
	__u32 index = fsize->index;

	for (i = 0; i < ARRAY_SIZE(cmos_supported_win_sizes); i++) {
		const struct cmos_win_size *win = &cmos_supported_win_sizes[index];
		if (index == ++num_valid) {
			fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
			fsize->discrete.width = win->width;
			fsize->discrete.height = win->height;
			return 0;
		}
	}

	return -EINVAL;
}

static int sensor_identify(struct cmos_subdev *sd)
{
	return (sensor_read(sd, 0) == sd->id) ? 0 : -EINVAL;
}

static int sensor_init(struct cmos_subdev *sd)
{
	if (sensor_identify(sd)) {
		return -1;
	}

	sensor_write_array(sd, gc0309_default_regs_init,
			   ARRAY_SIZE(gc0309_default_regs_init));
	mdelay(200);
	return 0;
}

static int sensor_exit(struct cmos_subdev *sd)
{
	sensor_write_array(sd, gc0309_default_regs_exit,
			   ARRAY_SIZE(gc0309_default_regs_exit));
	return 0;
}

static struct cmos_subdev_ops gc0309_ops = {
	.identify	= sensor_identify,
	.init		= sensor_init,
	.exit		= sensor_exit,
	.queryctrl	= sensor_queryctrl,
	.s_ctrl		= sensor_s_ctrl,
	.s_mbus_fmt     = sensor_s_mbus_fmt,
	.g_mbus_fmt     = sensor_g_mbus_fmt,
	.try_mbus_fmt	= sensor_try_mbus_fmt,
	.enum_framesizes = sensor_enum_framesizes,
};

struct cmos_subdev gc0309 = {
	.name		= "gc0309",
	.i2c_addr	= 0x21,
	.id		= 0xa0,
	.max_width	= 640,
	.max_height	= 480,
	.ops		= &gc0309_ops,
};

#if 0
static int __init gc0309_init(void)
{
	return cmos_register_sudbdev(&gc0309);
}

static void __exit gc0309_exit(void)
{
	return cmos_unregister_subdev(&gc0309);
}

module_init(gc0309_init);
module_exit(gc0309_exit);

MODULE_LICENSE("GPL");
#endif