aboutsummaryrefslogtreecommitdiff
path: root/drivers/media/i2c/soc_camera/s5k8aa.c
blob: c66027f88eb25247086ed94caf2ce7929c4f153b (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * s5k8aa Camera Driver
 *
 * Copyright (c) 2013 Marvell Ltd.
 * Libin Yang <lbyang@marvell.com>
 *
 * 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.
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/videodev2.h>
#include <linux/platform_data/camera-mmp.h>
#include <linux/v4l2-mediabus.h>
#include "s5k8aa.h"

MODULE_DESCRIPTION("Samsumg S5K8AA Camera Driver");
MODULE_LICENSE("GPL");

static int debug;
module_param(debug, int, 0644);

static const struct s5k8aa_datafmt s5k8aa_colour_fmts[] = {
	{V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
	{V4L2_MBUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG},
	{V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
	{V4L2_MBUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
};

static struct s5k8aa_regval *regs_resolution;
static struct s5k8aa_regval *regs_display_setting;

static struct s5k8aa_regval *regs_res_all[] = {
	[S5K8AA_FMT_QCIF] = regs_res_qcif,
	[S5K8AA_FMT_240X160] = regs_res_240x160,
	[S5K8AA_FMT_QVGA] = regs_res_qvga,
	[S5K8AA_FMT_CIF] = regs_res_cif,
	[S5K8AA_FMT_VGA] = regs_res_vga,
	[S5K8AA_FMT_720X480] = regs_res_720x480,
	[S5K8AA_FMT_WVGA] = regs_res_wvga,
	[S5K8AA_FMT_720P] = regs_res_720p,
};

static struct s5k8aa *to_s5k8aa(const struct i2c_client
						 *client)
{
	return container_of(i2c_get_clientdata(client),
				struct s5k8aa, subdev);
}

static int s5k8aa_i2c_read(struct i2c_client *client, u16 addr, u16 *val)
{
	u8 wbuf[2] = {addr >> 8, addr & 0xFF};
	struct i2c_msg msg[2];
	u8 rbuf[2];
	int ret;

	msg[0].addr = client->addr;
	msg[0].flags = 0;
	msg[0].len = 2;
	msg[0].buf = wbuf;

	msg[1].addr = client->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].len = 2;
	msg[1].buf = rbuf;

	ret = i2c_transfer(client->adapter, msg, 2);
	*val = be16_to_cpu(*((u16 *)rbuf));

	v4l2_dbg(3, debug, client, "i2c_read: 0x%04X : 0x%04x\n", addr, *val);

	return ret == 2 ? 0 : ret;
}

static int s5k8aa_i2c_write(struct i2c_client *client, u16 addr, u16 val)
{
	u8 buf[4] = {addr >> 8, addr & 0xFF, val >> 8, val & 0xFF};

	int ret = i2c_master_send(client, buf, 4);
	v4l2_dbg(3, debug, client, "i2c_write: 0x%04X : 0x%04x\n", addr, val);

	return ret == 4 ? 0 : ret;
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
/* The command register write, assumes Command_Wr_addH = 0x7000. */
static int s5k8aa_write(struct i2c_client *c, u16 addr, u16 val)
{
	int ret = s5k8aa_i2c_write(c, REG_CMDWR_ADDRL, addr);
	if (ret)
		return ret;
	return s5k8aa_i2c_write(c, REG_CMDBUF0_ADDR, val);
}
#endif

/* The command register read, assumes Command_Rd_addH = 0x7000. */
static int s5k8aa_read(struct i2c_client *client, u16 addr, u16 *val)
{
	int ret = s5k8aa_i2c_write(client, REG_CMDRD_ADDRL, addr);
	if (ret)
		return ret;

	return s5k8aa_i2c_read(client, REG_CMDBUF0_ADDR, val);
}

static int s5k8aa_write_raw_array(struct v4l2_subdev *sd,
				  const struct s5k8aa_regval *msg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret = 0;

	while (msg->addr != S5K8AA_TERM) {
		ret = s5k8aa_i2c_write(client, msg->addr, msg->val);
		if (ret)
			break;
		/* Assume that msg->addr is always less than 0xfffc */
		msg++;
	}

	return ret;
}

static int s5k8aa_power_on(struct s5k8aa *s5k8aa)
{
	int ret;

	ret = regulator_bulk_enable(ARRAY_SIZE(s5k8aa->supplies),
				    s5k8aa->supplies);
	if (ret)
		return ret;

	usleep_range(5000, 10000);

	if (gpio_is_valid(s5k8aa->pwdn_gpio)) {
		gpio_direction_output(s5k8aa->pwdn_gpio, 1);
		usleep_range(5000, 10000);
	}

	if (gpio_is_valid(s5k8aa->reset_gpio)) {
		gpio_direction_output(s5k8aa->reset_gpio, 1);
		usleep_range(5000, 10000);
	}

	clk_set_rate(s5k8aa->clk, 26000000);
	clk_prepare_enable(s5k8aa->clk);

	return 0;
}

static void s5k8aa_power_off(struct s5k8aa *s5k8aa)
{
	clk_disable_unprepare(s5k8aa->clk);

	if (gpio_is_valid(s5k8aa->reset_gpio))
		gpio_direction_output(s5k8aa->reset_gpio, 0);

	if (gpio_is_valid(s5k8aa->pwdn_gpio))
		gpio_direction_output(s5k8aa->pwdn_gpio, 0);

	regulator_bulk_disable(ARRAY_SIZE(s5k8aa->supplies),
			       s5k8aa->supplies);
}

static int __s5k8aa_set_power(struct s5k8aa *s5k8aa, bool on)
{
	if (!on) {
		s5k8aa_power_off(s5k8aa);
		return 0;
	}

	return s5k8aa_power_on(s5k8aa);
}

static int s5k8aa_s_power(struct v4l2_subdev *sd, int on)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct s5k8aa *s5k8aa = to_s5k8aa(client);
	int ret = 0;

	mutex_lock(&s5k8aa->power_lock);

	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
	 * update the power state.
	 */
	if (s5k8aa->power_count == !on) {
		ret = __s5k8aa_set_power(s5k8aa, !!on);
		if (ret < 0)
			goto out;
	}

	/* Update the power count. */
	s5k8aa->power_count += on ? 1 : -1;
	WARN_ON(s5k8aa->power_count < 0);

out:
	mutex_unlock(&s5k8aa->power_lock);
	return ret;
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
static int s5k8aa_g_register(struct v4l2_subdev *sd,
				 struct v4l2_dbg_register *reg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	return s5k8aa_read(client, (u16) reg->reg,
				   (u16 *)&(reg->val));
}

static int s5k8aa_s_register(struct v4l2_subdev *sd,
				 const struct v4l2_dbg_register *reg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	return s5k8aa_write(client, (u16) reg->reg,
				(u16)reg->val);
}
#endif

static int s5k8aa_s_stream(struct v4l2_subdev *sd, int enable)
{

	int ret = 0;


	if (enable) {
		ret |= s5k8aa_write_raw_array(sd, regs_start_stream);
		ret |= s5k8aa_write_raw_array(sd, regs_select_display);

	} else {
		ret = s5k8aa_write_raw_array(sd, regs_stop_stream);
	}
	return ret;
}

static int s5k8aa_enum_fmt(struct v4l2_subdev *sd,
		unsigned int index,
		enum v4l2_mbus_pixelcode *code)
{
	if (index >= ARRAY_SIZE(s5k8aa_colour_fmts))
		return -EINVAL;
	*code = s5k8aa_colour_fmts[index].code;
	return 0;
}

static inline enum s5k8aa_res_support find_res_code(int width, int height)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(s5k8aa_resolutions); i++) {
		if (s5k8aa_resolutions[i].width == width &&
			s5k8aa_resolutions[i].height == height)
			return s5k8aa_resolutions[i].res_code;
	}
	return -EINVAL;
}

static struct s5k8aa_regval *s5k8aa_get_res_regs(int width, int height)
{
	enum s5k8aa_res_support res_code;

	res_code = find_res_code(width, height);
	if (res_code == -EINVAL)
		return NULL;
	return regs_res_all[res_code];
}

static int s5k8aa_try_fmt(struct v4l2_subdev *sd,
			   struct v4l2_mbus_framefmt *mf)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct s5k8aa *s5k8aa = to_s5k8aa(client);

	switch (mf->code) {
	case V4L2_MBUS_FMT_UYVY8_2X8:
	case V4L2_MBUS_FMT_VYUY8_2X8:
	case V4L2_MBUS_FMT_YVYU8_2X8:
	case V4L2_MBUS_FMT_YUYV8_2X8:
		regs_resolution = s5k8aa_get_res_regs(mf->width, mf->height);
		if (!regs_resolution) {
			/* set default resolution */
			dev_warn(&client->dev,
				"s5k8aa: res not support, set to VGA\n");
			mf->width = S5K8AA_OUT_WIDTH_DEF;
			mf->height = S5K8AA_OUT_HEIGHT_DEF;
			regs_resolution =
				s5k8aa_get_res_regs(mf->width, mf->height);
		}
		s5k8aa->rect.width = mf->width;
		s5k8aa->rect.height = mf->height;
		/* colorspace should be set in host driver */
		mf->colorspace = V4L2_COLORSPACE_JPEG;
		if (mf->code == V4L2_MBUS_FMT_UYVY8_2X8)
			regs_display_setting = regs_display_uyvy_setting;
		else if (mf->code == V4L2_MBUS_FMT_VYUY8_2X8)
			regs_display_setting = regs_display_vyuy_setting;
		else if (mf->code == V4L2_MBUS_FMT_YUYV8_2X8)
			regs_display_setting = regs_display_yuyv_setting;
		else
			regs_display_setting = regs_display_yvyu_setting;
		break;
	default:
		/* This should never happen */
		mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
		return -EINVAL;
	}
	return 0;
}

/*
 * each time before start streaming,
 * this function must be called
 */
static int s5k8aa_s_fmt(struct v4l2_subdev *sd,
			 struct v4l2_mbus_framefmt *mf)
{
	int ret = 0;

	msleep(150);
	ret = s5k8aa_write_raw_array(sd, regs_start_setting);
	msleep(20);
	ret |= s5k8aa_write_raw_array(sd, regs_trap_patch);
	ret |= s5k8aa_write_raw_array(sd, regs_stop_stream);
	ret |= s5k8aa_write_raw_array(sd, regs_analog_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_otp_control);
	ret |= s5k8aa_write_raw_array(sd, regs_gas_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_analog_setting2);
	ret |= s5k8aa_write_raw_array(sd, regs_awb_basic_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_clk_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_flicker_detection);
	ret |= s5k8aa_write_raw_array(sd, regs_ae_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_ae_weight);
	ret |= s5k8aa_write_raw_array(sd, regs_ccm_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_gamma_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_afit_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_display_setting);
	ret |= s5k8aa_write_raw_array(sd, regs_resolution);

	return ret;
}

static int s5k8aa_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct s5k8aa *s5k8aa = to_s5k8aa(client);

	mf->width		= s5k8aa->rect.width;
	mf->height		= s5k8aa->rect.height;
	/* all belows are fixed */
	mf->code		= V4L2_MBUS_FMT_UYVY8_2X8;
	mf->field		= V4L2_FIELD_NONE;
	mf->colorspace		= V4L2_COLORSPACE_JPEG;
	return 0;
}

static int s5k8aa_enum_fsizes(struct v4l2_subdev *sd,
				struct v4l2_frmsizeenum *fsize)
{
	if (!fsize)
		return -EINVAL;

	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;

	if (fsize->index >= ARRAY_SIZE(s5k8aa_resolutions))
		return -EINVAL;

	fsize->discrete.height = s5k8aa_resolutions[fsize->index].height;
	fsize->discrete.width = s5k8aa_resolutions[fsize->index].width;
	return 0;
}

static struct v4l2_subdev_core_ops s5k8aa_subdev_core_ops = {
	.s_power        = s5k8aa_s_power,
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.g_register	= s5k8aa_g_register,
	.s_register	= s5k8aa_s_register,
#endif
};

static struct v4l2_subdev_video_ops s5k8aa_subdev_video_ops = {
	.s_stream = s5k8aa_s_stream,
	.g_mbus_fmt = s5k8aa_g_fmt,
	.s_mbus_fmt = s5k8aa_s_fmt,
	.try_mbus_fmt = s5k8aa_try_fmt,
	.enum_framesizes = s5k8aa_enum_fsizes,
	.enum_mbus_fmt = s5k8aa_enum_fmt,
};

static struct v4l2_subdev_ops s5k8aa_subdev_ops = {
	.core = &s5k8aa_subdev_core_ops,
	.video = &s5k8aa_subdev_video_ops,
};

static int s5k8aa_detect(struct s5k8aa *s5k8aa)
{
	int ret;
	u16 val;

	ret = s5k8aa_power_on(s5k8aa);
	if (ret)
		return ret;

	ret = s5k8aa_i2c_write(s5k8aa->client, 0xfcfc, 0xd000);
	if (ret)
		goto done;

	ret = s5k8aa_i2c_write(s5k8aa->client, 0x002c, 0x0000);
	if (ret)
		goto done;

	/* revision */
	/* s5k8aa_read(s5k8aa->client, 0x0042, &val);	 */
	ret = s5k8aa_read(s5k8aa->client, 0x0040, &val);
	if (ret)
		goto done;

	if (val != 0x8aa)
		ret = -ENODEV;

done:
	s5k8aa_power_off(s5k8aa);
	return ret;
}

static int s5k8aa_probe(struct i2c_client *client,
				const struct i2c_device_id *did)
{
	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
	struct device_node *np = client->dev.of_node;
	struct s5k8aa *s5k8aa;
	int ret = 0;

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
		dev_warn(&adapter->dev,
			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
		return -EIO;
	}

	s5k8aa = devm_kzalloc(&client->dev, sizeof(*s5k8aa), GFP_KERNEL);
	if (!s5k8aa)
		return -ENOMEM;

	mutex_init(&s5k8aa->power_lock);
	s5k8aa->client = client;

	/* Get power supplies, clocks and GPIOs. */
	s5k8aa->supplies[0].supply = "avdd";
	s5k8aa->supplies[1].supply = "dvdd";
	s5k8aa->supplies[2].supply = "dovdd";

	ret = devm_regulator_bulk_get(&client->dev,
				      ARRAY_SIZE(s5k8aa->supplies),
				      s5k8aa->supplies);
	if (ret) {
		dev_err(&client->dev, "failed to get power supplies\n");
		return ret;
	}

	regulator_set_voltage(s5k8aa->supplies[0].consumer, 2800000, 2800000);
	regulator_set_voltage(s5k8aa->supplies[1].consumer, 1200000, 1200000);
	regulator_set_voltage(s5k8aa->supplies[2].consumer, 1800000, 1800000);

	s5k8aa->clk = devm_clk_get(&client->dev, NULL);
	if (IS_ERR(s5k8aa->clk))
		return PTR_ERR(s5k8aa->clk);

	s5k8aa->pwdn_gpio = of_get_named_gpio(np, "pwdn-gpios", 0);
	s5k8aa->reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);

	if (gpio_is_valid(s5k8aa->reset_gpio)) {
		ret = devm_gpio_request_one(&client->dev, s5k8aa->reset_gpio,
					    GPIOF_OUT_INIT_LOW, "reset");
		if (ret) {
			dev_err(&client->dev, "failed to request reset GPIO\n");
			return ret;
		}

		usleep_range(5000, 20000);
	}

	if (gpio_is_valid(s5k8aa->pwdn_gpio)) {
		ret = devm_gpio_request_one(&client->dev, s5k8aa->pwdn_gpio,
					    GPIOF_OUT_INIT_LOW, "pwdn");
		if (ret) {
			dev_err(&client->dev, "failed to request pwdn GPIO\n");
			return ret;
		}
	}

	ret = s5k8aa_detect(s5k8aa);
	if (ret)
		return ret;

	v4l2_i2c_subdev_init(&s5k8aa->subdev, client, &s5k8aa_subdev_ops);
	s5k8aa->rect.left = 0;
	s5k8aa->rect.top = 0;
	s5k8aa->rect.width = S5K8AA_OUT_WIDTH_DEF;
	s5k8aa->rect.height = S5K8AA_OUT_HEIGHT_DEF;
	s5k8aa->pixfmt = V4L2_PIX_FMT_UYVY;
	return ret;
}

static int s5k8aa_remove(struct i2c_client *client)
{
	return 0;
}

static const struct i2c_device_id s5k8aa_idtable[] = {
	{"s5k8aay", 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, s5k8aa_idtable);

static struct i2c_driver s5k8aa_driver = {
	.driver = {
		   .name = "s5k8aay",
		   },
	.probe = s5k8aa_probe,
	.remove = s5k8aa_remove,
	.id_table = s5k8aa_idtable,
};

static int __init s5k8aa_mod_init(void)
{
	int ret = 0;
	ret = i2c_add_driver(&s5k8aa_driver);
	return ret;
}

static void __exit s5k8aa_mod_exit(void)
{
	i2c_del_driver(&s5k8aa_driver);
}

module_init(s5k8aa_mod_init);
module_exit(s5k8aa_mod_exit);