summaryrefslogtreecommitdiffstats
path: root/meta-rcar-gen3-adas/recipes-kernel/linux/linux-renesas/0031-media-i2c-Add-ov5647-sensor.patch
blob: 1cd366c2a2a4df755fcd2900f99dfa53f9034ead (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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
From 8b97232e3ecbb4ee38bda454915e62914ef52fcc Mon Sep 17 00:00:00 2001
From: Vladimir Barinov <vladimir.barinov@cogentembedded.com>
Date: Thu, 1 Jun 2017 19:59:56 +0300
Subject: [PATCH] media: i2c: Add ov5647 sensor

Add ov5647 camera sensor driver

Signed-off-by: Vladimir Barinov <vladimir.barinov@cogentembedded.com>
---
 drivers/media/i2c/soc_camera/Kconfig  |   6 +
 drivers/media/i2c/soc_camera/Makefile |   1 +
 drivers/media/i2c/soc_camera/ov5647.c | 649 ++++++++++++++++++++++++++++++++++
 drivers/media/i2c/soc_camera/ov5647.h | 242 +++++++++++++
 4 files changed, 898 insertions(+)
 create mode 100644 drivers/media/i2c/soc_camera/ov5647.c
 create mode 100644 drivers/media/i2c/soc_camera/ov5647.h

diff --git a/drivers/media/i2c/soc_camera/Kconfig b/drivers/media/i2c/soc_camera/Kconfig
index e1c65ca..c272aeb 100644
--- a/drivers/media/i2c/soc_camera/Kconfig
+++ b/drivers/media/i2c/soc_camera/Kconfig
@@ -97,6 +97,12 @@ config SOC_CAMERA_OV5642
 	help
 	  This is a V4L2 camera driver for the OmniVision OV5642 sensor
 
+config SOC_CAMERA_OV5647
+	tristate "ov5647 camera support"
+	depends on SOC_CAMERA && I2C
+	help
+	  This is a V4L2 camera driver for the OmniVision OV5647 sensor
+
 config SOC_CAMERA_OV6650
 	tristate "ov6650 sensor support"
 	depends on SOC_CAMERA && I2C
diff --git a/drivers/media/i2c/soc_camera/Makefile b/drivers/media/i2c/soc_camera/Makefile
index 8e24d5d..a67fff8 100644
--- a/drivers/media/i2c/soc_camera/Makefile
+++ b/drivers/media/i2c/soc_camera/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_SOC_CAMERA_OV495_OV2775)	+= ov495_ov2775.o
 obj-$(CONFIG_SOC_CAMERA_OV106XX)	+= ov106xx.o
 obj-$(CONFIG_SOC_CAMERA_OV2640)		+= ov2640.o
 obj-$(CONFIG_SOC_CAMERA_OV5642)		+= ov5642.o
+obj-$(CONFIG_SOC_CAMERA_OV5647)		+= ov5647.o
 obj-$(CONFIG_SOC_CAMERA_OV6650)		+= ov6650.o
 obj-$(CONFIG_SOC_CAMERA_OV772X)		+= ov772x.o
 obj-$(CONFIG_SOC_CAMERA_OV9640)		+= ov9640.o
diff --git a/drivers/media/i2c/soc_camera/ov5647.c b/drivers/media/i2c/soc_camera/ov5647.c
new file mode 100644
index 0000000..caccf39
--- /dev/null
+++ b/drivers/media/i2c/soc_camera/ov5647.c
@@ -0,0 +1,649 @@
+/*
+ * V4L2 driver for OmniVision OV5647 cameras.
+ *
+ * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
+ * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
+ *
+ * Based on Omnivision OV7670 Camera Driver
+ * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
+ *
+ * Copyright (C) 2016, Synopsys, Inc.
+ *
+ * Copyright (C) 2017 Cogent Embedded, Inc
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/i2c.h>
+#include <linux/delay.h>
+#include <linux/videodev2.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-mediabus.h>
+#include <media/v4l2-image-sizes.h>
+#include <media/v4l2-of.h>
+#include <linux/io.h>
+
+#include "ov5647.h"
+
+static bool debug = true;
+module_param(debug, bool, 0644);
+MODULE_PARM_DESC(debug, "Debug level (0-1)");
+
+#define OV5647_I2C_ADDR 0x6c
+#define SENSOR_NAME "ov5647"
+
+#define OV5647_REG_CHIPID_H	0x300A
+#define OV5647_REG_CHIPID_L	0x300B
+
+#define REG_TERM 0xfffe
+#define VAL_TERM 0xfe
+#define REG_DLY  0xffff
+
+/*define the voltage level of control signal*/
+#define CSI_STBY_ON		1
+#define CSI_STBY_OFF		0
+#define CSI_RST_ON		0
+#define CSI_RST_OFF		1
+#define CSI_PWR_ON		1
+#define CSI_PWR_OFF		0
+#define CSI_AF_PWR_ON		1
+#define CSI_AF_PWR_OFF		0
+
+#define OV5647_ROW_START		0x01
+#define OV5647_ROW_START_MIN		0
+#define OV5647_ROW_START_MAX		2004
+#define OV5647_ROW_START_DEF		54
+
+#define OV5647_COLUMN_START		0x02
+#define OV5647_COLUMN_START_MIN		0
+#define OV5647_COLUMN_START_MAX		2750
+#define OV5647_COLUMN_START_DEF		16
+
+#define OV5647_WINDOW_HEIGHT		0x03
+#define OV5647_WINDOW_HEIGHT_MIN	2
+#define OV5647_WINDOW_HEIGHT_MAX	2006
+#define OV5647_WINDOW_HEIGHT_DEF	1944
+
+#define OV5647_WINDOW_WIDTH		0x04
+#define OV5647_WINDOW_WIDTH_MIN		2
+#define OV5647_WINDOW_WIDTH_MAX		2752
+#define OV5647_WINDOW_WIDTH_DEF		2592
+
+enum power_seq_cmd {
+	CSI_SUBDEV_PWR_OFF = 0x00,
+	CSI_SUBDEV_PWR_ON = 0x01,
+};
+
+struct sensor_format_struct {
+	__u8 *desc;
+	u32 mbus_code;
+	enum v4l2_colorspace colorspace;
+	struct regval_list *regs;
+	int regs_size;
+	int bpp;
+};
+
+struct cfg_array {
+	struct regval_list *regs;
+	int size;
+};
+
+struct sensor_win_size {
+	int width;
+	int height;
+	unsigned int hoffset;
+	unsigned int voffset;
+	unsigned int hts;
+	unsigned int vts;
+	unsigned int pclk;
+	unsigned int mipi_bps;
+	unsigned int fps_fixed;
+	unsigned int bin_factor;
+	unsigned int intg_min;
+	unsigned int intg_max;
+	void *regs;
+	int regs_size;
+	int (*set_size)(struct v4l2_subdev *subdev);
+};
+
+struct ov5647 {
+	struct device			*dev;
+	struct v4l2_subdev		subdev;
+	struct media_pad		pad;
+	struct mutex			lock;
+	struct v4l2_mbus_framefmt	format;
+	struct sensor_format_struct	*fmt;
+	unsigned int			width;
+	unsigned int			height;
+	unsigned int			capture_mode;
+	int				hue;
+	struct v4l2_fract		tpf;
+	struct sensor_win_size		*current_wins;
+};
+
+static inline struct ov5647 *to_state(struct v4l2_subdev *subdev)
+{
+	return container_of(subdev, struct ov5647, subdev);
+}
+
+static struct sensor_format_struct sensor_formats[] = {
+	{
+		.mbus_code	= OV5647_CODE,
+		.colorspace	= V4L2_COLORSPACE_JPEG,
+	},
+};
+#define N_FMTS ARRAY_SIZE(sensor_formats)
+
+static int ov5647_write(struct v4l2_subdev *sd, uint16_t reg, uint8_t val)
+{
+	int ret;
+	unsigned char data[3] = { reg >> 8, reg & 0xff, val};
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+	ret = i2c_master_send(client, data, 3);
+	if (ret < 3) {
+		printk( "%s: i2c write error, reg: %x, %d\n",
+				__func__, reg, ret);
+		return ret < 0 ? ret : -EIO;
+	}
+
+	return 0;
+}
+
+static int ov5647_read(struct v4l2_subdev *sd, uint16_t reg, uint8_t *val)
+{
+	int ret;
+	unsigned char data_w[2] = { reg >> 8, reg & 0xff };
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+
+
+	ret = i2c_master_send(client, data_w, 2);
+
+	if (ret < 2) {
+		printk("%s: i2c read error, reg: %x\n",
+			__func__, reg);
+		return ret < 0 ? ret : -EIO;
+	}
+
+	ret = i2c_master_recv(client, val, 1);
+
+	if (ret < 1) {
+		printk("%s: i2c read error, reg: %x\n",
+				__func__, reg);
+		return ret < 0 ? ret : -EIO;
+	}
+
+	return 0;
+}
+
+static int ov5647_write_array(struct v4l2_subdev *subdev,
+				struct regval_list *regs, int array_size)
+{
+	int i = 0;
+	int ret = 0;
+
+	if (!regs)
+		return -EINVAL;
+
+	while (i < array_size) {
+		if (regs->addr == REG_DLY)
+			mdelay(regs->data);
+		else
+			ret = ov5647_write(subdev, regs->addr, regs->data);
+
+		if (ret == -EIO)
+			return ret;
+
+		i++;
+		regs++;
+	}
+	return 0;
+}
+
+static void ov5647_set_virtual_channel(struct v4l2_subdev *subdev, int channel)
+{
+#if 0
+	u8 channel_id;
+
+	ov5647_read(subdev, 0x4814, &channel_id);
+//	channel_id = 0x1e; //override
+
+	channel_id &= ~(3 << 6);
+	channel_id |= (channel << 6);
+	printk("0x4814 = 0x%02x\n", channel_id);
+	ov5647_write(subdev, 0x4814, channel_id);
+	ov5647_write(subdev, 0x4801, 0x8f);
+#endif
+}
+
+void ov5647_stream_on(struct v4l2_subdev *subdev)
+{
+	ov5647_write(subdev, 0x4202, 0x00);
+	ov5647_write(subdev, 0x300D, 0x00);
+}
+
+void ov5647_stream_off(struct v4l2_subdev *subdev)
+{
+	ov5647_write(subdev, 0x4202, 0x0f);
+	ov5647_write(subdev, 0x300D, 0x01);
+}
+
+static int sensor_s_sw_stby(struct v4l2_subdev *subdev, int on_off)
+{
+	int ret;
+	unsigned char rdval;
+
+	ret = ov5647_read(subdev, 0x0100, &rdval);
+	if (ret != 0)
+		return ret;
+
+	if (on_off == CSI_STBY_ON)
+		ret = ov5647_write(subdev, 0x0100, rdval&0xfe);
+	else
+		ret = ov5647_write(subdev, 0x0100, rdval|0x01);
+
+	return ret;
+}
+
+static int __sensor_init(struct v4l2_subdev *subdev)
+{
+	int ret;
+	unsigned char rdval;
+
+	ret = ov5647_read(subdev, 0x0100, &rdval);
+	if (ret != 0)
+		return ret;
+
+	ov5647_write(subdev, 0x4800, 0x25);
+	ov5647_stream_off(subdev);
+
+	ov5647_write(subdev, 0x100, 0);
+	/* reset */
+	ov5647_write(subdev, 0x103, 1);
+	ov5647_write(subdev, 0x103, 1);
+	ov5647_write(subdev, 0x103, 1);
+	mdelay(10);
+
+	ret = ov5647_write_array(subdev, ov5647_recommend_settings,
+					ARRAY_SIZE(ov5647_recommend_settings));
+#if 1
+	ret = ov5647_write_array(subdev, ov5647_snap_settings,
+					ARRAY_SIZE(ov5647_snap_settings));
+#else
+	ret = ov5647_write_array(subdev, ov5647_prev_settings,
+					ARRAY_SIZE(ov5647_prev_settings));
+#endif
+	ov5647_set_virtual_channel(subdev, 0);
+
+	ov5647_write(subdev, 0x0100, 0x01);
+
+	ov5647_write(subdev, 0x04800, 0x04);
+	ov5647_stream_on(subdev);
+	msleep(30);
+
+	return 0;
+}
+
+static int sensor_power(struct v4l2_subdev *subdev, int on)
+{
+	int ret = 0;
+	struct ov5647 *ov5647 = to_state(subdev);
+
+	mutex_lock(&ov5647->lock);
+
+	switch (on) {
+	case CSI_SUBDEV_PWR_OFF:
+		ret = sensor_s_sw_stby(subdev, CSI_STBY_ON);
+		if (ret < 0)
+			printk("soft stby failed!\n");
+		break;
+	case CSI_SUBDEV_PWR_ON:
+		ret = __sensor_init(subdev);
+		if (ret < 0) {
+			v4l2_err(subdev, "Camera not available, check power\n");
+			break;
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	mutex_unlock(&ov5647->lock);
+
+	return 0;
+}
+
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+static int sensor_get_register(struct v4l2_subdev *subdev,
+			       struct v4l2_dbg_register *reg)
+{
+	u8 val = 0;
+	int ret;
+
+	ret = ov5647_read(subdev, (u16)reg->reg, &val);
+	if (ret < 0)
+		return ret;
+
+	reg->val = val;
+	reg->size = sizeof(u8);
+
+	return ret;
+}
+
+static int sensor_set_register(struct v4l2_subdev *subdev,
+			       const struct v4l2_dbg_register *reg)
+{
+	ov5647_write(subdev, (u16)reg->reg, (u8)reg->val);
+
+	return 0;
+}
+#endif
+
+static const struct v4l2_subdev_core_ops sensor_core_ops = {
+	.s_power		= sensor_power,
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+	.g_register		= sensor_get_register,
+	.s_register		= sensor_set_register,
+#endif
+};
+
+static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
+{
+	if (enable)
+		ov5647_stream_on(sd);
+	else
+		ov5647_stream_off(sd);
+
+	return 0;
+}
+
+static int sensor_enum_fmt(struct v4l2_subdev *sd,
+			   struct v4l2_subdev_pad_config *cfg,
+			   struct v4l2_subdev_mbus_code_enum *code)
+{
+	if (code->pad || code->index >= N_FMTS)
+		return -EINVAL;
+
+	code->code = OV5647_CODE;
+
+	return 0;
+}
+
+static int sensor_try_fmt_internal(struct v4l2_subdev *subdev,
+				   struct v4l2_mbus_framefmt *fmt, 
+				   struct sensor_format_struct **ret_fmt,
+				   struct sensor_win_size **ret_wsize)
+{
+	int index;
+
+	for (index = 0; index < N_FMTS; index++)
+		if (sensor_formats[index].mbus_code == fmt->code)
+			break;
+
+	if (index >= N_FMTS)
+		return -EINVAL;
+
+	if (ret_fmt != NULL)
+		*ret_fmt = sensor_formats + index;
+
+	fmt->field = V4L2_FIELD_NONE;
+
+	return 0;
+}
+
+static int sensor_s_fmt(struct v4l2_subdev *subdev,
+			struct v4l2_subdev_pad_config *cfg,
+			struct v4l2_subdev_format *fmt)
+{
+	int ret;
+	struct sensor_format_struct *sensor_fmt;
+	struct sensor_win_size *wsize = NULL;
+	struct ov5647 *info = to_state(subdev);
+
+	ret = sensor_try_fmt_internal(subdev, &fmt->format,
+					&sensor_fmt, &wsize);
+	if (ret)
+		return ret;
+
+	info->fmt = sensor_fmt;
+	info->width = OV5647_WIDTH;
+	info->height = OV5647_HEIGHT;
+
+	return 0;
+}
+
+static int sensor_g_fmt(struct v4l2_subdev *sd,
+			struct v4l2_subdev_pad_config *cfg,
+			struct v4l2_subdev_format *format)
+{
+	struct ov5647 *info = to_state(sd);
+	struct v4l2_mbus_framefmt *mf = &format->format;
+
+	if (format->pad != 0)
+		return -EINVAL;
+
+	mf->width = OV5647_WIDTH;
+	mf->height = OV5647_HEIGHT;
+	mf->code = OV5647_CODE;
+	mf->colorspace = info->fmt->colorspace;
+	mf->field = V4L2_FIELD_NONE;
+
+	return 0;
+}
+
+static int sensor_s_parm(struct v4l2_subdev *subdev,
+			 struct v4l2_streamparm *parms)
+{
+	struct v4l2_captureparm *cp = &parms->parm.capture;
+	struct ov5647 *info = to_state(subdev);
+
+	if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+
+	if (info->tpf.numerator == 0)
+		return -EINVAL;
+
+	info->capture_mode = cp->capturemode;
+
+	return 0;
+}
+
+static int sensor_g_parm(struct v4l2_subdev *subdev,
+			 struct v4l2_streamparm *parms)
+{
+	struct v4l2_captureparm *cp = &parms->parm.capture;
+	struct ov5647 *info = to_state(subdev);
+
+	if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+
+	memset(cp, 0, sizeof(struct v4l2_captureparm));
+	cp->capability = V4L2_CAP_TIMEPERFRAME;
+	cp->capturemode = info->capture_mode;
+
+	return 0;
+}
+
+static int sensor_g_mbus_config(struct v4l2_subdev *sd,
+				struct v4l2_mbus_config *cfg)
+{
+	cfg->flags = V4L2_MBUS_CSI2_1_LANE | V4L2_MBUS_CSI2_CHANNEL_0 |
+		     V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
+	cfg->type = V4L2_MBUS_CSI2;
+
+	return 0;
+}
+
+static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
+	.enum_mbus_code	= sensor_enum_fmt,
+	.set_fmt	= sensor_s_fmt,
+	.get_fmt	= sensor_g_fmt,
+};
+
+static const struct v4l2_subdev_video_ops sensor_video_ops = {
+	.s_stream	= sensor_s_stream,
+	.s_parm		= sensor_s_parm,
+	.g_parm		= sensor_g_parm,
+	.g_mbus_config	= sensor_g_mbus_config,
+};
+
+static const struct v4l2_subdev_ops subdev_ops = {
+	.core		= &sensor_core_ops,
+	.video		= &sensor_video_ops,
+	.pad		= &sensor_pad_ops,
+};
+
+static int ov5647_detect(struct v4l2_subdev *sd)
+{
+	struct i2c_client *client = v4l2_get_subdevdata(sd);
+	unsigned char id_h, id_l;
+	int ret;
+
+	ret = sensor_power(sd, 1);
+	if (ret < 0)
+		return ret;
+
+	ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &id_h);
+	if (ret < 0)
+		return ret;
+	ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &id_l);
+	if (ret < 0)
+		return ret;
+
+	if ((id_h != 0x56) || (id_l != 0x47)) {
+		v4l2_info(sd, "Invalid device ID: %02x%02x\n", id_h, id_l);
+		return -ENODEV;
+	}
+
+	v4l2_info(sd, "OV5647 detected at address 0x%02x\n", client->addr);
+
+	ret = sensor_power(sd, 0);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+
+static int ov5647_registered(struct v4l2_subdev *subdev)
+{
+	return 0;
+}
+
+static int ov5647_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
+{
+	struct v4l2_mbus_framefmt *format =
+				v4l2_subdev_get_try_format(subdev, fh->pad, 0);
+	struct v4l2_rect *crop =
+				v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
+
+	crop->left = 0;
+	crop->top = 0;
+	crop->width = OV5647_WIDTH;
+	crop->height = OV5647_HEIGHT;
+
+	format->code = OV5647_CODE;
+
+	format->width = OV5647_WIDTH;
+	format->height = OV5647_HEIGHT;
+	format->field = V4L2_FIELD_NONE;
+	format->colorspace = sensor_formats[0].colorspace;
+
+	return sensor_power(subdev, 1);
+}
+
+static int ov5647_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
+{
+	return sensor_power(subdev, 0);
+}
+
+static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
+	.registered = ov5647_registered,
+	.open = ov5647_open,
+	.close = ov5647_close,
+};
+
+static int ov5647_probe(struct i2c_client *client,
+			const struct i2c_device_id *id)
+{
+	struct device *dev = &client->dev;
+	struct ov5647 *sensor;
+	int ret = 0;
+	struct v4l2_subdev *sd;
+
+	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
+	if (sensor == NULL)
+		return -ENOMEM;
+
+	mutex_init(&sensor->lock);
+	sensor->dev = dev;
+	sensor->fmt = &sensor_formats[0];
+	sensor->width = OV5647_WIDTH;
+	sensor->height = OV5647_HEIGHT;
+
+	sd = &sensor->subdev;
+	v4l2_i2c_subdev_init(sd, client, &subdev_ops);
+	sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
+
+	ret = ov5647_detect(sd);
+	if (ret < 0) {
+		v4l2_err(sd, "OV5647 not found!\n");
+		goto out;
+	}
+
+	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
+	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
+	ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
+	if (ret < 0)
+		return ret;
+
+	ret = v4l2_async_register_subdev(sd);
+	if (ret < 0)
+		media_entity_cleanup(&sd->entity);
+
+out:
+	return ret;
+}
+
+static int ov5647_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
+	struct ov5647 *ov5647 = to_state(subdev);
+
+	v4l2_async_unregister_subdev(&ov5647->subdev);
+	media_entity_cleanup(&ov5647->subdev.entity);
+	v4l2_device_unregister_subdev(subdev);
+
+	return 0;
+}
+
+static const struct i2c_device_id ov5647_id[] = {
+	{ "ov5647", 0 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, ov5647_id);
+
+#if IS_ENABLED(CONFIG_OF)
+static const struct of_device_id ov5647_of_match[] = {
+	{ .compatible = "ovti,ov5647" },
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, ov5647_of_match);
+#endif
+
+static struct i2c_driver ov5647_driver = {
+	.driver = {
+		.of_match_table = of_match_ptr(ov5647_of_match),
+		.owner	= THIS_MODULE,
+		.name	= "ov5647",
+	},
+	.probe		= ov5647_probe,
+	.remove		= ov5647_remove,
+	.id_table	= ov5647_id,
+};
+module_i2c_driver(ov5647_driver);
+
+MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
+MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
+MODULE_LICENSE("GPL");
diff --git a/drivers/media/i2c/soc_camera/ov5647.h b/drivers/media/i2c/soc_camera/ov5647.h
new file mode 100644
index 0000000..f854da8
--- /dev/null
+++ b/drivers/media/i2c/soc_camera/ov5647.h
@@ -0,0 +1,242 @@
+/*
+ * Copyright (c) 2012, The Linux Foundation. All rights reserved.
+ * Copyright (C) 2017 Cogent Embedded, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+//#define TEST_PATTERN
+
+#define OV5647_WIDTH 2592
+#define OV5647_HEIGHT 1944
+
+#define OV5647_CODE MEDIA_BUS_FMT_SBGGR8_1X8
+//#define OV5647_CODE MEDIA_BUS_FMT_YUYV8_2X8
+
+struct regval_list {
+	uint16_t addr;
+	uint8_t data;
+};
+
+enum ov5647_test_mode_t {
+	TEST_OFF,
+	TEST_1,
+	TEST_2,
+	TEST_3
+};
+
+struct regval_list ov5647_prev_settings[] = {
+	/*1280*960 Reference Setting 24M MCLK 2lane 280Mbps/lane 30fps
+	for back to preview*/
+	{0x3035, 0x21},
+	{0x3036, 0x37},
+	{0x3821, 0x07},
+	{0x3820, 0x41},
+	{0x3612, 0x09},
+	{0x3618, 0x00},
+	{0x380c, 0x07},
+	{0x380d, 0x68},
+	{0x380e, 0x03},
+	{0x380f, 0xd8},
+	{0x3814, 0x31},
+	{0x3815, 0x31},
+	{0x3709, 0x52},
+	{0x3808, 0x05}, // 1280
+	{0x3809, 0x00},
+	{0x380a, 0x03}, // 960
+	{0x380b, 0xc0},
+	{0x3800, 0x00},
+	{0x3801, 0x18},
+	{0x3802, 0x00},
+	{0x3803, 0x0e},
+	{0x3804, 0x0a},
+	{0x3805, 0x27},
+	{0x3806, 0x07},
+	{0x3807, 0x95},
+	{0x4004, 0x02},
+};
+struct regval_list ov5647_snap_settings[] = {
+	/*2608*1952 Reference Setting 24M MCLK 2lane 280Mbps/lane 30fps*/
+	{0x3035, 0x21},
+	{0x3036, 0x4f},
+	{0x3821, 0x06},
+	{0x3820, 0x00},
+	{0x3612, 0x0b},
+	{0x3618, 0x04},
+	{0x380c, 0x0a},
+	{0x380d, 0x8c},
+	{0x380e, 0x07},
+	{0x380f, 0xb0},
+	{0x3814, 0x11},
+	{0x3815, 0x11},
+	{0x3709, 0x12},
+#if 0
+	{0x3808, 0x0a},
+	{0x3809, 0x30},
+	{0x380a, 0x07},
+	{0x380b, 0xa0},
+#else
+	{0x3808, OV5647_WIDTH >> 8},
+	{0x3809, OV5647_WIDTH & 0xff},
+	{0x380a, OV5647_HEIGHT >> 8},
+	{0x380b, OV5647_HEIGHT & 0xff},
+#endif
+	{0x3800, 0x00},
+	{0x3801, 0x04},
+	{0x3802, 0x00},
+	{0x3803, 0x00},
+	{0x3804, 0x0a},
+	{0x3805, 0x3b},
+	{0x3806, 0x07},
+	{0x3807, 0xa3},
+	{0x4004, 0x04},
+};
+struct regval_list ov5647_recommend_settings[] = {
+#ifdef TEST_PATTERN
+	{0x503d, 0x80},
+#endif
+#if 0
+	{0x4814, 0x1e},
+	{0x4801, 0x8f},
+#endif
+	{0x3035, 0x11},
+	{0x303c, 0x11},
+	{0x370c, 0x03},
+	{0x5000, 0x06},
+	{0x5003, 0x08},
+	{0x5a00, 0x08},
+	{0x3000, 0xff},
+	{0x3001, 0xff},
+	{0x3002, 0xff},
+	{0x301d, 0xf0},
+	{0x3a18, 0x00},
+	{0x3a19, 0xf8},
+	{0x3c01, 0x80},
+	{0x3b07, 0x0c},
+	{0x3708, 0x64},
+	{0x3630, 0x2e},
+	{0x3632, 0xe2},
+	{0x3633, 0x23},
+	{0x3634, 0x44},
+	{0x3620, 0x64},
+	{0x3621, 0xe0},
+	{0x3600, 0x37},
+	{0x3704, 0xa0},
+	{0x3703, 0x5a},
+	{0x3715, 0x78},
+	{0x3717, 0x01},
+	{0x3731, 0x02},
+	{0x370b, 0x60},
+	{0x3705, 0x1a},
+	{0x3f05, 0x02},
+	{0x3f06, 0x10},
+	{0x3f01, 0x0a},
+	{0x3a08, 0x01},
+	{0x3a0f, 0x58},
+	{0x3a10, 0x50},
+	{0x3a1b, 0x58},
+	{0x3a1e, 0x50},
+	{0x3a11, 0x60},
+	{0x3a1f, 0x28},
+	{0x4001, 0x02},
+	{0x4000, 0x09},
+	{0x3000, 0x00},
+	{0x3001, 0x00},
+	{0x3002, 0x00},
+	{0x3017, 0xe0},
+	{0x301c, 0xfc},
+	{0x3636, 0x06},
+	{0x3016, 0x08},
+	{0x3827, 0xec},
+	{0x3018, 0x44},
+	{0x3035, 0x21},
+	{0x3106, 0xf5},
+	{0x3034, 0x18},
+	{0x301c, 0xf8},
+	/*lens setting*/
+	{0x5000, 0x86},
+	{0x5800, 0x11},
+	{0x5801, 0x0c},
+	{0x5802, 0x0a},
+	{0x5803, 0x0b},
+	{0x5804, 0x0d},
+	{0x5805, 0x13},
+	{0x5806, 0x09},
+	{0x5807, 0x05},
+	{0x5808, 0x03},
+	{0x5809, 0x03},
+	{0x580a, 0x06},
+	{0x580b, 0x08},
+	{0x580c, 0x05},
+	{0x580d, 0x01},
+	{0x580e, 0x00},
+	{0x580f, 0x00},
+	{0x5810, 0x02},
+	{0x5811, 0x06},
+	{0x5812, 0x05},
+	{0x5813, 0x01},
+	{0x5814, 0x00},
+	{0x5815, 0x00},
+	{0x5816, 0x02},
+	{0x5817, 0x06},
+	{0x5818, 0x09},
+	{0x5819, 0x05},
+	{0x581a, 0x04},
+	{0x581b, 0x04},
+	{0x581c, 0x06},
+	{0x581d, 0x09},
+	{0x581e, 0x11},
+	{0x581f, 0x0c},
+	{0x5820, 0x0b},
+	{0x5821, 0x0b},
+	{0x5822, 0x0d},
+	{0x5823, 0x13},
+	{0x5824, 0x22},
+	{0x5825, 0x26},
+	{0x5826, 0x26},
+	{0x5827, 0x24},
+	{0x5828, 0x24},
+	{0x5829, 0x24},
+	{0x582a, 0x22},
+	{0x582b, 0x20},
+	{0x582c, 0x22},
+	{0x582d, 0x26},
+	{0x582e, 0x22},
+	{0x582f, 0x22},
+	{0x5830, 0x42},
+	{0x5831, 0x22},
+	{0x5832, 0x02},
+	{0x5833, 0x24},
+	{0x5834, 0x22},
+	{0x5835, 0x22},
+	{0x5836, 0x22},
+	{0x5837, 0x26},
+	{0x5838, 0x42},
+	{0x5839, 0x26},
+	{0x583a, 0x06},
+	{0x583b, 0x26},
+	{0x583c, 0x24},
+	{0x583d, 0xce},
+	/* manual AWB,manual AE,close Lenc,open WBC*/
+	{0x3503, 0x03}, /*manual AE*/
+	{0x3501, 0x10},
+	{0x3502, 0x80},
+	{0x350a, 0x00},
+	{0x350b, 0x7f},
+	{0x5001, 0x01}, /*manual AWB*/
+	{0x5180, 0x08},
+	{0x5186, 0x04},
+	{0x5187, 0x00},
+	{0x5188, 0x04},
+	{0x5189, 0x00},
+	{0x518a, 0x04},
+	{0x518b, 0x00},
+	{0x5000, 0x06}, /*No lenc,WBC on*/
+};
-- 
1.9.1