blob: 72c6ac1d767cdfe592cff5721b3b541043273474 [file] [log] [blame]
Andrew Chew9bd060e2011-02-17 19:14:33 -03001/*
2 * OmniVision OV9740 Camera Driver
3 *
4 * Copyright (C) 2011 NVIDIA Corporation
5 *
6 * Based on ov9640 camera driver.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/slab.h>
17#include <media/v4l2-chip-ident.h>
18#include <media/soc_camera.h>
19
20#define to_ov9740(sd) container_of(sd, struct ov9740_priv, subdev)
21
22/* General Status Registers */
23#define OV9740_MODEL_ID_HI 0x0000
24#define OV9740_MODEL_ID_LO 0x0001
25#define OV9740_REVISION_NUMBER 0x0002
26#define OV9740_MANUFACTURER_ID 0x0003
27#define OV9740_SMIA_VERSION 0x0004
28
29/* General Setup Registers */
30#define OV9740_MODE_SELECT 0x0100
31#define OV9740_IMAGE_ORT 0x0101
32#define OV9740_SOFTWARE_RESET 0x0103
33#define OV9740_GRP_PARAM_HOLD 0x0104
34#define OV9740_MSK_CORRUP_FM 0x0105
35
36/* Timing Setting */
37#define OV9740_FRM_LENGTH_LN_HI 0x0340 /* VTS */
38#define OV9740_FRM_LENGTH_LN_LO 0x0341 /* VTS */
39#define OV9740_LN_LENGTH_PCK_HI 0x0342 /* HTS */
40#define OV9740_LN_LENGTH_PCK_LO 0x0343 /* HTS */
41#define OV9740_X_ADDR_START_HI 0x0344
42#define OV9740_X_ADDR_START_LO 0x0345
43#define OV9740_Y_ADDR_START_HI 0x0346
44#define OV9740_Y_ADDR_START_LO 0x0347
45#define OV9740_X_ADDR_END_HI 0x0348
46#define OV9740_X_ADDR_END_LO 0x0349
Andrew Chewc6aac9f2011-06-23 20:19:39 -030047#define OV9740_Y_ADDR_END_HI 0x034a
48#define OV9740_Y_ADDR_END_LO 0x034b
49#define OV9740_X_OUTPUT_SIZE_HI 0x034c
50#define OV9740_X_OUTPUT_SIZE_LO 0x034d
51#define OV9740_Y_OUTPUT_SIZE_HI 0x034e
52#define OV9740_Y_OUTPUT_SIZE_LO 0x034f
Andrew Chew9bd060e2011-02-17 19:14:33 -030053
54/* IO Control Registers */
55#define OV9740_IO_CREL00 0x3002
56#define OV9740_IO_CREL01 0x3004
57#define OV9740_IO_CREL02 0x3005
58#define OV9740_IO_OUTPUT_SEL01 0x3026
59#define OV9740_IO_OUTPUT_SEL02 0x3027
60
61/* AWB Registers */
62#define OV9740_AWB_MANUAL_CTRL 0x3406
63
64/* Analog Control Registers */
65#define OV9740_ANALOG_CTRL01 0x3601
66#define OV9740_ANALOG_CTRL02 0x3602
67#define OV9740_ANALOG_CTRL03 0x3603
68#define OV9740_ANALOG_CTRL04 0x3604
69#define OV9740_ANALOG_CTRL10 0x3610
70#define OV9740_ANALOG_CTRL12 0x3612
Andrew Chewc4fdce52011-06-23 20:19:41 -030071#define OV9740_ANALOG_CTRL15 0x3615
Andrew Chew9bd060e2011-02-17 19:14:33 -030072#define OV9740_ANALOG_CTRL20 0x3620
73#define OV9740_ANALOG_CTRL21 0x3621
74#define OV9740_ANALOG_CTRL22 0x3622
75#define OV9740_ANALOG_CTRL30 0x3630
76#define OV9740_ANALOG_CTRL31 0x3631
77#define OV9740_ANALOG_CTRL32 0x3632
78#define OV9740_ANALOG_CTRL33 0x3633
79
80/* Sensor Control */
81#define OV9740_SENSOR_CTRL03 0x3703
82#define OV9740_SENSOR_CTRL04 0x3704
83#define OV9740_SENSOR_CTRL05 0x3705
84#define OV9740_SENSOR_CTRL07 0x3707
85
86/* Timing Control */
87#define OV9740_TIMING_CTRL17 0x3817
88#define OV9740_TIMING_CTRL19 0x3819
89#define OV9740_TIMING_CTRL33 0x3833
90#define OV9740_TIMING_CTRL35 0x3835
91
92/* Banding Filter */
Andrew Chewc6aac9f2011-06-23 20:19:39 -030093#define OV9740_AEC_MAXEXPO_60_H 0x3a02
94#define OV9740_AEC_MAXEXPO_60_L 0x3a03
95#define OV9740_AEC_B50_STEP_HI 0x3a08
96#define OV9740_AEC_B50_STEP_LO 0x3a09
97#define OV9740_AEC_B60_STEP_HI 0x3a0a
98#define OV9740_AEC_B60_STEP_LO 0x3a0b
99#define OV9740_AEC_CTRL0D 0x3a0d
100#define OV9740_AEC_CTRL0E 0x3a0e
101#define OV9740_AEC_MAXEXPO_50_H 0x3a14
102#define OV9740_AEC_MAXEXPO_50_L 0x3a15
Andrew Chew9bd060e2011-02-17 19:14:33 -0300103
104/* AEC/AGC Control */
105#define OV9740_AEC_ENABLE 0x3503
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300106#define OV9740_GAIN_CEILING_01 0x3a18
107#define OV9740_GAIN_CEILING_02 0x3a19
108#define OV9740_AEC_HI_THRESHOLD 0x3a11
109#define OV9740_AEC_3A1A 0x3a1a
110#define OV9740_AEC_CTRL1B_WPT2 0x3a1b
111#define OV9740_AEC_CTRL0F_WPT 0x3a0f
112#define OV9740_AEC_CTRL10_BPT 0x3a10
113#define OV9740_AEC_CTRL1E_BPT2 0x3a1e
114#define OV9740_AEC_LO_THRESHOLD 0x3a1f
Andrew Chew9bd060e2011-02-17 19:14:33 -0300115
116/* BLC Control */
117#define OV9740_BLC_AUTO_ENABLE 0x4002
118#define OV9740_BLC_MODE 0x4005
119
120/* VFIFO */
121#define OV9740_VFIFO_READ_START_HI 0x4608
122#define OV9740_VFIFO_READ_START_LO 0x4609
123
124/* DVP Control */
125#define OV9740_DVP_VSYNC_CTRL02 0x4702
126#define OV9740_DVP_VSYNC_MODE 0x4704
127#define OV9740_DVP_VSYNC_CTRL06 0x4706
128
129/* PLL Setting */
130#define OV9740_PLL_MODE_CTRL01 0x3104
131#define OV9740_PRE_PLL_CLK_DIV 0x0305
132#define OV9740_PLL_MULTIPLIER 0x0307
133#define OV9740_VT_SYS_CLK_DIV 0x0303
134#define OV9740_VT_PIX_CLK_DIV 0x0301
135#define OV9740_PLL_CTRL3010 0x3010
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300136#define OV9740_VFIFO_CTRL00 0x460e
Andrew Chew9bd060e2011-02-17 19:14:33 -0300137
138/* ISP Control */
139#define OV9740_ISP_CTRL00 0x5000
140#define OV9740_ISP_CTRL01 0x5001
141#define OV9740_ISP_CTRL03 0x5003
142#define OV9740_ISP_CTRL05 0x5005
143#define OV9740_ISP_CTRL12 0x5012
144#define OV9740_ISP_CTRL19 0x5019
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300145#define OV9740_ISP_CTRL1A 0x501a
146#define OV9740_ISP_CTRL1E 0x501e
147#define OV9740_ISP_CTRL1F 0x501f
Andrew Chew9bd060e2011-02-17 19:14:33 -0300148#define OV9740_ISP_CTRL20 0x5020
149#define OV9740_ISP_CTRL21 0x5021
150
151/* AWB */
152#define OV9740_AWB_CTRL00 0x5180
153#define OV9740_AWB_CTRL01 0x5181
154#define OV9740_AWB_CTRL02 0x5182
155#define OV9740_AWB_CTRL03 0x5183
156#define OV9740_AWB_ADV_CTRL01 0x5184
157#define OV9740_AWB_ADV_CTRL02 0x5185
158#define OV9740_AWB_ADV_CTRL03 0x5186
159#define OV9740_AWB_ADV_CTRL04 0x5187
160#define OV9740_AWB_ADV_CTRL05 0x5188
161#define OV9740_AWB_ADV_CTRL06 0x5189
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300162#define OV9740_AWB_ADV_CTRL07 0x518a
163#define OV9740_AWB_ADV_CTRL08 0x518b
164#define OV9740_AWB_ADV_CTRL09 0x518c
165#define OV9740_AWB_ADV_CTRL10 0x518d
166#define OV9740_AWB_ADV_CTRL11 0x518e
167#define OV9740_AWB_CTRL0F 0x518f
Andrew Chew9bd060e2011-02-17 19:14:33 -0300168#define OV9740_AWB_CTRL10 0x5190
169#define OV9740_AWB_CTRL11 0x5191
170#define OV9740_AWB_CTRL12 0x5192
171#define OV9740_AWB_CTRL13 0x5193
172#define OV9740_AWB_CTRL14 0x5194
173
174/* MIPI Control */
175#define OV9740_MIPI_CTRL00 0x4800
176#define OV9740_MIPI_3837 0x3837
177#define OV9740_MIPI_CTRL01 0x4801
178#define OV9740_MIPI_CTRL03 0x4803
179#define OV9740_MIPI_CTRL05 0x4805
180#define OV9740_VFIFO_RD_CTRL 0x4601
181#define OV9740_MIPI_CTRL_3012 0x3012
182#define OV9740_SC_CMMM_MIPI_CTR 0x3014
183
184/* supported resolutions */
185enum {
186 OV9740_VGA,
187 OV9740_720P,
188};
189
190struct ov9740_resolution {
191 unsigned int width;
192 unsigned int height;
193};
194
195static struct ov9740_resolution ov9740_resolutions[] = {
196 [OV9740_VGA] = {
197 .width = 640,
198 .height = 480,
199 },
200 [OV9740_720P] = {
201 .width = 1280,
202 .height = 720,
203 },
204};
205
206/* Misc. structures */
207struct ov9740_reg {
208 u16 reg;
209 u8 val;
210};
211
212struct ov9740_priv {
213 struct v4l2_subdev subdev;
214
215 int ident;
216 u16 model;
217 u8 revision;
218 u8 manid;
219 u8 smiaver;
220
221 bool flag_vflip;
222 bool flag_hflip;
223};
224
225static const struct ov9740_reg ov9740_defaults[] = {
Andrew Chewc4fdce52011-06-23 20:19:41 -0300226 /* Software Reset */
227 { OV9740_SOFTWARE_RESET, 0x01 },
228
Andrew Chew9bd060e2011-02-17 19:14:33 -0300229 /* Banding Filter */
230 { OV9740_AEC_B50_STEP_HI, 0x00 },
231 { OV9740_AEC_B50_STEP_LO, 0xe8 },
232 { OV9740_AEC_CTRL0E, 0x03 },
233 { OV9740_AEC_MAXEXPO_50_H, 0x15 },
234 { OV9740_AEC_MAXEXPO_50_L, 0xc6 },
235 { OV9740_AEC_B60_STEP_HI, 0x00 },
236 { OV9740_AEC_B60_STEP_LO, 0xc0 },
237 { OV9740_AEC_CTRL0D, 0x04 },
238 { OV9740_AEC_MAXEXPO_60_H, 0x18 },
239 { OV9740_AEC_MAXEXPO_60_L, 0x20 },
240
241 /* LC */
242 { 0x5842, 0x02 }, { 0x5843, 0x5e }, { 0x5844, 0x04 }, { 0x5845, 0x32 },
243 { 0x5846, 0x03 }, { 0x5847, 0x29 }, { 0x5848, 0x02 }, { 0x5849, 0xcc },
244
245 /* Un-documented OV9740 registers */
246 { 0x5800, 0x29 }, { 0x5801, 0x25 }, { 0x5802, 0x20 }, { 0x5803, 0x21 },
247 { 0x5804, 0x26 }, { 0x5805, 0x2e }, { 0x5806, 0x11 }, { 0x5807, 0x0c },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300248 { 0x5808, 0x09 }, { 0x5809, 0x0a }, { 0x580a, 0x0e }, { 0x580b, 0x16 },
249 { 0x580c, 0x06 }, { 0x580d, 0x02 }, { 0x580e, 0x00 }, { 0x580f, 0x00 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300250 { 0x5810, 0x04 }, { 0x5811, 0x0a }, { 0x5812, 0x05 }, { 0x5813, 0x02 },
251 { 0x5814, 0x00 }, { 0x5815, 0x00 }, { 0x5816, 0x03 }, { 0x5817, 0x09 },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300252 { 0x5818, 0x0f }, { 0x5819, 0x0a }, { 0x581a, 0x07 }, { 0x581b, 0x08 },
253 { 0x581c, 0x0b }, { 0x581d, 0x14 }, { 0x581e, 0x28 }, { 0x581f, 0x23 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300254 { 0x5820, 0x1d }, { 0x5821, 0x1e }, { 0x5822, 0x24 }, { 0x5823, 0x2a },
255 { 0x5824, 0x4f }, { 0x5825, 0x6f }, { 0x5826, 0x5f }, { 0x5827, 0x7f },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300256 { 0x5828, 0x9f }, { 0x5829, 0x5f }, { 0x582a, 0x8f }, { 0x582b, 0x9e },
257 { 0x582c, 0x8f }, { 0x582d, 0x9f }, { 0x582e, 0x4f }, { 0x582f, 0x87 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300258 { 0x5830, 0x86 }, { 0x5831, 0x97 }, { 0x5832, 0xae }, { 0x5833, 0x3f },
259 { 0x5834, 0x8e }, { 0x5835, 0x7c }, { 0x5836, 0x7e }, { 0x5837, 0xaf },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300260 { 0x5838, 0x8f }, { 0x5839, 0x8f }, { 0x583a, 0x9f }, { 0x583b, 0x7f },
261 { 0x583c, 0x5f },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300262
263 /* Y Gamma */
264 { 0x5480, 0x07 }, { 0x5481, 0x18 }, { 0x5482, 0x2c }, { 0x5483, 0x4e },
265 { 0x5484, 0x5e }, { 0x5485, 0x6b }, { 0x5486, 0x77 }, { 0x5487, 0x82 },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300266 { 0x5488, 0x8c }, { 0x5489, 0x95 }, { 0x548a, 0xa4 }, { 0x548b, 0xb1 },
267 { 0x548c, 0xc6 }, { 0x548d, 0xd8 }, { 0x548e, 0xe9 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300268
269 /* UV Gamma */
270 { 0x5490, 0x0f }, { 0x5491, 0xff }, { 0x5492, 0x0d }, { 0x5493, 0x05 },
271 { 0x5494, 0x07 }, { 0x5495, 0x1a }, { 0x5496, 0x04 }, { 0x5497, 0x01 },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300272 { 0x5498, 0x03 }, { 0x5499, 0x53 }, { 0x549a, 0x02 }, { 0x549b, 0xeb },
273 { 0x549c, 0x02 }, { 0x549d, 0xa0 }, { 0x549e, 0x02 }, { 0x549f, 0x67 },
274 { 0x54a0, 0x02 }, { 0x54a1, 0x3b }, { 0x54a2, 0x02 }, { 0x54a3, 0x18 },
275 { 0x54a4, 0x01 }, { 0x54a5, 0xe7 }, { 0x54a6, 0x01 }, { 0x54a7, 0xc3 },
276 { 0x54a8, 0x01 }, { 0x54a9, 0x94 }, { 0x54aa, 0x01 }, { 0x54ab, 0x72 },
277 { 0x54ac, 0x01 }, { 0x54ad, 0x57 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300278
279 /* AWB */
280 { OV9740_AWB_CTRL00, 0xf0 },
281 { OV9740_AWB_CTRL01, 0x00 },
282 { OV9740_AWB_CTRL02, 0x41 },
283 { OV9740_AWB_CTRL03, 0x42 },
284 { OV9740_AWB_ADV_CTRL01, 0x8a },
285 { OV9740_AWB_ADV_CTRL02, 0x61 },
286 { OV9740_AWB_ADV_CTRL03, 0xce },
287 { OV9740_AWB_ADV_CTRL04, 0xa8 },
288 { OV9740_AWB_ADV_CTRL05, 0x17 },
289 { OV9740_AWB_ADV_CTRL06, 0x1f },
290 { OV9740_AWB_ADV_CTRL07, 0x27 },
291 { OV9740_AWB_ADV_CTRL08, 0x41 },
292 { OV9740_AWB_ADV_CTRL09, 0x34 },
293 { OV9740_AWB_ADV_CTRL10, 0xf0 },
294 { OV9740_AWB_ADV_CTRL11, 0x10 },
295 { OV9740_AWB_CTRL0F, 0xff },
296 { OV9740_AWB_CTRL10, 0x00 },
297 { OV9740_AWB_CTRL11, 0xff },
298 { OV9740_AWB_CTRL12, 0x00 },
299 { OV9740_AWB_CTRL13, 0xff },
300 { OV9740_AWB_CTRL14, 0x00 },
301
302 /* CIP */
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300303 { 0x530d, 0x12 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300304
305 /* CMX */
306 { 0x5380, 0x01 }, { 0x5381, 0x00 }, { 0x5382, 0x00 }, { 0x5383, 0x17 },
307 { 0x5384, 0x00 }, { 0x5385, 0x01 }, { 0x5386, 0x00 }, { 0x5387, 0x00 },
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300308 { 0x5388, 0x00 }, { 0x5389, 0xe0 }, { 0x538a, 0x00 }, { 0x538b, 0x20 },
309 { 0x538c, 0x00 }, { 0x538d, 0x00 }, { 0x538e, 0x00 }, { 0x538f, 0x16 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300310 { 0x5390, 0x00 }, { 0x5391, 0x9c }, { 0x5392, 0x00 }, { 0x5393, 0xa0 },
311 { 0x5394, 0x18 },
312
313 /* 50/60 Detection */
Andrew Chewc6aac9f2011-06-23 20:19:39 -0300314 { 0x3c0a, 0x9c }, { 0x3c0b, 0x3f },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300315
316 /* Output Select */
317 { OV9740_IO_OUTPUT_SEL01, 0x00 },
318 { OV9740_IO_OUTPUT_SEL02, 0x00 },
319 { OV9740_IO_CREL00, 0x00 },
320 { OV9740_IO_CREL01, 0x00 },
321 { OV9740_IO_CREL02, 0x00 },
322
323 /* AWB Control */
324 { OV9740_AWB_MANUAL_CTRL, 0x00 },
325
326 /* Analog Control */
327 { OV9740_ANALOG_CTRL03, 0xaa },
328 { OV9740_ANALOG_CTRL32, 0x2f },
329 { OV9740_ANALOG_CTRL20, 0x66 },
330 { OV9740_ANALOG_CTRL21, 0xc0 },
331 { OV9740_ANALOG_CTRL31, 0x52 },
332 { OV9740_ANALOG_CTRL33, 0x50 },
333 { OV9740_ANALOG_CTRL30, 0xca },
334 { OV9740_ANALOG_CTRL04, 0x0c },
335 { OV9740_ANALOG_CTRL01, 0x40 },
336 { OV9740_ANALOG_CTRL02, 0x16 },
337 { OV9740_ANALOG_CTRL10, 0xa1 },
338 { OV9740_ANALOG_CTRL12, 0x24 },
339 { OV9740_ANALOG_CTRL22, 0x9f },
Andrew Chewc4fdce52011-06-23 20:19:41 -0300340 { OV9740_ANALOG_CTRL15, 0xf0 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300341
342 /* Sensor Control */
343 { OV9740_SENSOR_CTRL03, 0x42 },
344 { OV9740_SENSOR_CTRL04, 0x10 },
345 { OV9740_SENSOR_CTRL05, 0x45 },
346 { OV9740_SENSOR_CTRL07, 0x14 },
347
348 /* Timing Control */
349 { OV9740_TIMING_CTRL33, 0x04 },
350 { OV9740_TIMING_CTRL35, 0x02 },
351 { OV9740_TIMING_CTRL19, 0x6e },
352 { OV9740_TIMING_CTRL17, 0x94 },
353
354 /* AEC/AGC Control */
355 { OV9740_AEC_ENABLE, 0x10 },
356 { OV9740_GAIN_CEILING_01, 0x00 },
357 { OV9740_GAIN_CEILING_02, 0x7f },
358 { OV9740_AEC_HI_THRESHOLD, 0xa0 },
359 { OV9740_AEC_3A1A, 0x05 },
360 { OV9740_AEC_CTRL1B_WPT2, 0x50 },
361 { OV9740_AEC_CTRL0F_WPT, 0x50 },
362 { OV9740_AEC_CTRL10_BPT, 0x4c },
363 { OV9740_AEC_CTRL1E_BPT2, 0x4c },
364 { OV9740_AEC_LO_THRESHOLD, 0x26 },
365
366 /* BLC Control */
367 { OV9740_BLC_AUTO_ENABLE, 0x45 },
368 { OV9740_BLC_MODE, 0x18 },
369
370 /* DVP Control */
371 { OV9740_DVP_VSYNC_CTRL02, 0x04 },
372 { OV9740_DVP_VSYNC_MODE, 0x00 },
373 { OV9740_DVP_VSYNC_CTRL06, 0x08 },
374
375 /* PLL Setting */
376 { OV9740_PLL_MODE_CTRL01, 0x20 },
377 { OV9740_PRE_PLL_CLK_DIV, 0x03 },
378 { OV9740_PLL_MULTIPLIER, 0x4c },
379 { OV9740_VT_SYS_CLK_DIV, 0x01 },
380 { OV9740_VT_PIX_CLK_DIV, 0x08 },
381 { OV9740_PLL_CTRL3010, 0x01 },
382 { OV9740_VFIFO_CTRL00, 0x82 },
383
384 /* Timing Setting */
385 /* VTS */
386 { OV9740_FRM_LENGTH_LN_HI, 0x03 },
387 { OV9740_FRM_LENGTH_LN_LO, 0x07 },
388 /* HTS */
389 { OV9740_LN_LENGTH_PCK_HI, 0x06 },
390 { OV9740_LN_LENGTH_PCK_LO, 0x62 },
391
392 /* MIPI Control */
Andrew Chewc4fdce52011-06-23 20:19:41 -0300393 { OV9740_MIPI_CTRL00, 0x44 }, /* 0x64 for discontinuous clk */
Andrew Chew9bd060e2011-02-17 19:14:33 -0300394 { OV9740_MIPI_3837, 0x01 },
395 { OV9740_MIPI_CTRL01, 0x0f },
396 { OV9740_MIPI_CTRL03, 0x05 },
397 { OV9740_MIPI_CTRL05, 0x10 },
398 { OV9740_VFIFO_RD_CTRL, 0x16 },
399 { OV9740_MIPI_CTRL_3012, 0x70 },
400 { OV9740_SC_CMMM_MIPI_CTR, 0x01 },
Andrew Chewc4fdce52011-06-23 20:19:41 -0300401
402 /* YUYV order */
403 { OV9740_ISP_CTRL19, 0x02 },
Andrew Chew9bd060e2011-02-17 19:14:33 -0300404};
405
406static const struct ov9740_reg ov9740_regs_vga[] = {
407 { OV9740_X_ADDR_START_HI, 0x00 },
408 { OV9740_X_ADDR_START_LO, 0xa0 },
409 { OV9740_Y_ADDR_START_HI, 0x00 },
410 { OV9740_Y_ADDR_START_LO, 0x00 },
411 { OV9740_X_ADDR_END_HI, 0x04 },
412 { OV9740_X_ADDR_END_LO, 0x63 },
413 { OV9740_Y_ADDR_END_HI, 0x02 },
414 { OV9740_Y_ADDR_END_LO, 0xd3 },
415 { OV9740_X_OUTPUT_SIZE_HI, 0x02 },
416 { OV9740_X_OUTPUT_SIZE_LO, 0x80 },
417 { OV9740_Y_OUTPUT_SIZE_HI, 0x01 },
418 { OV9740_Y_OUTPUT_SIZE_LO, 0xe0 },
419 { OV9740_ISP_CTRL1E, 0x03 },
420 { OV9740_ISP_CTRL1F, 0xc0 },
421 { OV9740_ISP_CTRL20, 0x02 },
422 { OV9740_ISP_CTRL21, 0xd0 },
423 { OV9740_VFIFO_READ_START_HI, 0x01 },
424 { OV9740_VFIFO_READ_START_LO, 0x40 },
425 { OV9740_ISP_CTRL00, 0xff },
426 { OV9740_ISP_CTRL01, 0xff },
427 { OV9740_ISP_CTRL03, 0xff },
428};
429
430static const struct ov9740_reg ov9740_regs_720p[] = {
431 { OV9740_X_ADDR_START_HI, 0x00 },
432 { OV9740_X_ADDR_START_LO, 0x00 },
433 { OV9740_Y_ADDR_START_HI, 0x00 },
434 { OV9740_Y_ADDR_START_LO, 0x00 },
435 { OV9740_X_ADDR_END_HI, 0x05 },
436 { OV9740_X_ADDR_END_LO, 0x03 },
437 { OV9740_Y_ADDR_END_HI, 0x02 },
438 { OV9740_Y_ADDR_END_LO, 0xd3 },
439 { OV9740_X_OUTPUT_SIZE_HI, 0x05 },
440 { OV9740_X_OUTPUT_SIZE_LO, 0x00 },
441 { OV9740_Y_OUTPUT_SIZE_HI, 0x02 },
442 { OV9740_Y_OUTPUT_SIZE_LO, 0xd0 },
443 { OV9740_ISP_CTRL1E, 0x05 },
444 { OV9740_ISP_CTRL1F, 0x00 },
445 { OV9740_ISP_CTRL20, 0x02 },
446 { OV9740_ISP_CTRL21, 0xd0 },
447 { OV9740_VFIFO_READ_START_HI, 0x02 },
448 { OV9740_VFIFO_READ_START_LO, 0x30 },
449 { OV9740_ISP_CTRL00, 0xff },
450 { OV9740_ISP_CTRL01, 0xef },
451 { OV9740_ISP_CTRL03, 0xff },
452};
453
454static enum v4l2_mbus_pixelcode ov9740_codes[] = {
455 V4L2_MBUS_FMT_YUYV8_2X8,
456};
457
458static const struct v4l2_queryctrl ov9740_controls[] = {
459 {
460 .id = V4L2_CID_VFLIP,
461 .type = V4L2_CTRL_TYPE_BOOLEAN,
462 .name = "Flip Vertically",
463 .minimum = 0,
464 .maximum = 1,
465 .step = 1,
466 .default_value = 0,
467 },
468 {
469 .id = V4L2_CID_HFLIP,
470 .type = V4L2_CTRL_TYPE_BOOLEAN,
471 .name = "Flip Horizontally",
472 .minimum = 0,
473 .maximum = 1,
474 .step = 1,
475 .default_value = 0,
476 },
477};
478
479/* read a register */
480static int ov9740_reg_read(struct i2c_client *client, u16 reg, u8 *val)
481{
482 int ret;
483 struct i2c_msg msg[] = {
484 {
485 .addr = client->addr,
486 .flags = 0,
487 .len = 2,
488 .buf = (u8 *)&reg,
489 },
490 {
491 .addr = client->addr,
492 .flags = I2C_M_RD,
493 .len = 1,
494 .buf = val,
495 },
496 };
497
498 reg = swab16(reg);
499
500 ret = i2c_transfer(client->adapter, msg, 2);
501 if (ret < 0) {
502 dev_err(&client->dev, "Failed reading register 0x%04x!\n", reg);
503 return ret;
504 }
505
506 return 0;
507}
508
509/* write a register */
510static int ov9740_reg_write(struct i2c_client *client, u16 reg, u8 val)
511{
512 struct i2c_msg msg;
513 struct {
514 u16 reg;
515 u8 val;
516 } __packed buf;
517 int ret;
518
519 reg = swab16(reg);
520
521 buf.reg = reg;
522 buf.val = val;
523
524 msg.addr = client->addr;
525 msg.flags = 0;
526 msg.len = 3;
527 msg.buf = (u8 *)&buf;
528
529 ret = i2c_transfer(client->adapter, &msg, 1);
530 if (ret < 0) {
531 dev_err(&client->dev, "Failed writing register 0x%04x!\n", reg);
532 return ret;
533 }
534
535 return 0;
536}
537
538
539/* Read a register, alter its bits, write it back */
540static int ov9740_reg_rmw(struct i2c_client *client, u16 reg, u8 set, u8 unset)
541{
542 u8 val;
543 int ret;
544
545 ret = ov9740_reg_read(client, reg, &val);
546 if (ret < 0) {
547 dev_err(&client->dev,
Andrew Chew5fec8b92011-06-23 20:19:40 -0300548 "[Read]-Modify-Write of register 0x%04x failed!\n",
549 reg);
Andrew Chew9bd060e2011-02-17 19:14:33 -0300550 return ret;
551 }
552
553 val |= set;
554 val &= ~unset;
555
556 ret = ov9740_reg_write(client, reg, val);
557 if (ret < 0) {
558 dev_err(&client->dev,
Andrew Chew5fec8b92011-06-23 20:19:40 -0300559 "Read-Modify-[Write] of register 0x%04x failed!\n",
560 reg);
Andrew Chew9bd060e2011-02-17 19:14:33 -0300561 return ret;
562 }
563
564 return 0;
565}
566
567static int ov9740_reg_write_array(struct i2c_client *client,
568 const struct ov9740_reg *regarray,
569 int regarraylen)
570{
571 int i;
572 int ret;
573
574 for (i = 0; i < regarraylen; i++) {
575 ret = ov9740_reg_write(client,
576 regarray[i].reg, regarray[i].val);
577 if (ret < 0)
578 return ret;
579 }
580
581 return 0;
582}
583
584/* Start/Stop streaming from the device */
585static int ov9740_s_stream(struct v4l2_subdev *sd, int enable)
586{
587 struct i2c_client *client = v4l2_get_subdevdata(sd);
588 struct ov9740_priv *priv = to_ov9740(sd);
589 int ret;
590
591 /* Program orientation register. */
592 if (priv->flag_vflip)
593 ret = ov9740_reg_rmw(client, OV9740_IMAGE_ORT, 0x2, 0);
594 else
595 ret = ov9740_reg_rmw(client, OV9740_IMAGE_ORT, 0, 0x2);
596 if (ret < 0)
597 return ret;
598
599 if (priv->flag_hflip)
600 ret = ov9740_reg_rmw(client, OV9740_IMAGE_ORT, 0x1, 0);
601 else
602 ret = ov9740_reg_rmw(client, OV9740_IMAGE_ORT, 0, 0x1);
603 if (ret < 0)
604 return ret;
605
606 if (enable) {
607 dev_dbg(&client->dev, "Enabling Streaming\n");
608 /* Start Streaming */
609 ret = ov9740_reg_write(client, OV9740_MODE_SELECT, 0x01);
610
611 } else {
612 dev_dbg(&client->dev, "Disabling Streaming\n");
613 /* Software Reset */
614 ret = ov9740_reg_write(client, OV9740_SOFTWARE_RESET, 0x01);
615 if (!ret)
616 /* Setting Streaming to Standby */
617 ret = ov9740_reg_write(client, OV9740_MODE_SELECT,
618 0x00);
619 }
620
621 return ret;
622}
623
624/* Alter bus settings on camera side */
625static int ov9740_set_bus_param(struct soc_camera_device *icd,
626 unsigned long flags)
627{
628 return 0;
629}
630
631/* Request bus settings on camera side */
632static unsigned long ov9740_query_bus_param(struct soc_camera_device *icd)
633{
634 struct soc_camera_link *icl = to_soc_camera_link(icd);
635
636 unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
637 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
638 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
639
640 return soc_camera_apply_sensor_flags(icl, flags);
641}
642
643/* Get status of additional camera capabilities */
644static int ov9740_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
645{
646 struct ov9740_priv *priv = to_ov9740(sd);
647
648 switch (ctrl->id) {
649 case V4L2_CID_VFLIP:
650 ctrl->value = priv->flag_vflip;
651 break;
652 case V4L2_CID_HFLIP:
653 ctrl->value = priv->flag_hflip;
654 break;
655 default:
656 return -EINVAL;
657 }
658
659 return 0;
660}
661
662/* Set status of additional camera capabilities */
663static int ov9740_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
664{
665 struct ov9740_priv *priv = to_ov9740(sd);
666
667 switch (ctrl->id) {
668 case V4L2_CID_VFLIP:
669 priv->flag_vflip = ctrl->value;
670 break;
671 case V4L2_CID_HFLIP:
672 priv->flag_hflip = ctrl->value;
673 break;
674 default:
675 return -EINVAL;
676 }
677
678 return 0;
679}
680
681/* Get chip identification */
682static int ov9740_g_chip_ident(struct v4l2_subdev *sd,
683 struct v4l2_dbg_chip_ident *id)
684{
685 struct ov9740_priv *priv = to_ov9740(sd);
686
687 id->ident = priv->ident;
688 id->revision = priv->revision;
689
690 return 0;
691}
692
693#ifdef CONFIG_VIDEO_ADV_DEBUG
694static int ov9740_get_register(struct v4l2_subdev *sd,
695 struct v4l2_dbg_register *reg)
696{
697 struct i2c_client *client = v4l2_get_subdevdata(sd);
698 int ret;
699 u8 val;
700
701 if (reg->reg & ~0xffff)
702 return -EINVAL;
703
704 reg->size = 2;
705
706 ret = ov9740_reg_read(client, reg->reg, &val);
707 if (ret)
708 return ret;
709
710 reg->val = (__u64)val;
711
712 return ret;
713}
714
715static int ov9740_set_register(struct v4l2_subdev *sd,
716 struct v4l2_dbg_register *reg)
717{
718 struct i2c_client *client = v4l2_get_subdevdata(sd);
719
720 if (reg->reg & ~0xffff || reg->val & ~0xff)
721 return -EINVAL;
722
723 return ov9740_reg_write(client, reg->reg, reg->val);
724}
725#endif
726
727/* select nearest higher resolution for capture */
728static void ov9740_res_roundup(u32 *width, u32 *height)
729{
730 int i;
731
732 for (i = 0; i < ARRAY_SIZE(ov9740_resolutions); i++)
733 if ((ov9740_resolutions[i].width >= *width) &&
734 (ov9740_resolutions[i].height >= *height)) {
735 *width = ov9740_resolutions[i].width;
736 *height = ov9740_resolutions[i].height;
737 return;
738 }
739
740 *width = ov9740_resolutions[OV9740_720P].width;
741 *height = ov9740_resolutions[OV9740_720P].height;
742}
743
744/* Setup registers according to resolution and color encoding */
745static int ov9740_set_res(struct i2c_client *client, u32 width)
746{
747 int ret;
748
749 /* select register configuration for given resolution */
750 if (width == ov9740_resolutions[OV9740_VGA].width) {
751 dev_dbg(&client->dev, "Setting image size to 640x480\n");
752 ret = ov9740_reg_write_array(client, ov9740_regs_vga,
753 ARRAY_SIZE(ov9740_regs_vga));
754 } else if (width == ov9740_resolutions[OV9740_720P].width) {
755 dev_dbg(&client->dev, "Setting image size to 1280x720\n");
756 ret = ov9740_reg_write_array(client, ov9740_regs_720p,
757 ARRAY_SIZE(ov9740_regs_720p));
758 } else {
759 dev_err(&client->dev, "Failed to select resolution!\n");
760 return -EINVAL;
761 }
762
763 return ret;
764}
765
766/* set the format we will capture in */
767static int ov9740_s_fmt(struct v4l2_subdev *sd,
768 struct v4l2_mbus_framefmt *mf)
769{
770 struct i2c_client *client = v4l2_get_subdevdata(sd);
771 enum v4l2_colorspace cspace;
772 enum v4l2_mbus_pixelcode code = mf->code;
773 int ret;
774
775 ov9740_res_roundup(&mf->width, &mf->height);
776
777 switch (code) {
778 case V4L2_MBUS_FMT_YUYV8_2X8:
779 cspace = V4L2_COLORSPACE_SRGB;
780 break;
781 default:
782 return -EINVAL;
783 }
784
785 ret = ov9740_reg_write_array(client, ov9740_defaults,
786 ARRAY_SIZE(ov9740_defaults));
787 if (ret < 0)
788 return ret;
789
790 ret = ov9740_set_res(client, mf->width);
791 if (ret < 0)
792 return ret;
793
794 mf->code = code;
795 mf->colorspace = cspace;
796
797 return ret;
798}
799
800static int ov9740_try_fmt(struct v4l2_subdev *sd,
801 struct v4l2_mbus_framefmt *mf)
802{
803 ov9740_res_roundup(&mf->width, &mf->height);
804
805 mf->field = V4L2_FIELD_NONE;
806 mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
807 mf->colorspace = V4L2_COLORSPACE_SRGB;
808
809 return 0;
810}
811
812static int ov9740_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
813 enum v4l2_mbus_pixelcode *code)
814{
815 if (index >= ARRAY_SIZE(ov9740_codes))
816 return -EINVAL;
817
818 *code = ov9740_codes[index];
819
820 return 0;
821}
822
823static int ov9740_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
824{
825 a->bounds.left = 0;
826 a->bounds.top = 0;
827 a->bounds.width = ov9740_resolutions[OV9740_720P].width;
828 a->bounds.height = ov9740_resolutions[OV9740_720P].height;
829 a->defrect = a->bounds;
830 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
831 a->pixelaspect.numerator = 1;
832 a->pixelaspect.denominator = 1;
833
834 return 0;
835}
836
837static int ov9740_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
838{
839 a->c.left = 0;
840 a->c.top = 0;
841 a->c.width = ov9740_resolutions[OV9740_720P].width;
842 a->c.height = ov9740_resolutions[OV9740_720P].height;
843 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844
845 return 0;
846}
847
848static int ov9740_video_probe(struct soc_camera_device *icd,
849 struct i2c_client *client)
850{
851 struct v4l2_subdev *sd = i2c_get_clientdata(client);
852 struct ov9740_priv *priv = to_ov9740(sd);
853 u8 modelhi, modello;
854 int ret;
855
856 /*
857 * We must have a parent by now. And it cannot be a wrong one.
858 * So this entire test is completely redundant.
859 */
860 if (!icd->dev.parent ||
861 to_soc_camera_host(icd->dev.parent)->nr != icd->iface) {
862 dev_err(&client->dev, "Parent missing or invalid!\n");
863 ret = -ENODEV;
864 goto err;
865 }
866
867 /*
868 * check and show product ID and manufacturer ID
869 */
870 ret = ov9740_reg_read(client, OV9740_MODEL_ID_HI, &modelhi);
871 if (ret < 0)
872 goto err;
873
874 ret = ov9740_reg_read(client, OV9740_MODEL_ID_LO, &modello);
875 if (ret < 0)
876 goto err;
877
878 priv->model = (modelhi << 8) | modello;
879
880 ret = ov9740_reg_read(client, OV9740_REVISION_NUMBER, &priv->revision);
881 if (ret < 0)
882 goto err;
883
884 ret = ov9740_reg_read(client, OV9740_MANUFACTURER_ID, &priv->manid);
885 if (ret < 0)
886 goto err;
887
888 ret = ov9740_reg_read(client, OV9740_SMIA_VERSION, &priv->smiaver);
889 if (ret < 0)
890 goto err;
891
892 if (priv->model != 0x9740) {
893 ret = -ENODEV;
894 goto err;
895 }
896
897 priv->ident = V4L2_IDENT_OV9740;
898
899 dev_info(&client->dev, "ov9740 Model ID 0x%04x, Revision 0x%02x, "
900 "Manufacturer 0x%02x, SMIA Version 0x%02x\n",
901 priv->model, priv->revision, priv->manid, priv->smiaver);
902
903err:
904 return ret;
905}
906
907static struct soc_camera_ops ov9740_ops = {
908 .set_bus_param = ov9740_set_bus_param,
909 .query_bus_param = ov9740_query_bus_param,
910 .controls = ov9740_controls,
911 .num_controls = ARRAY_SIZE(ov9740_controls),
912};
913
914static struct v4l2_subdev_core_ops ov9740_core_ops = {
915 .g_ctrl = ov9740_g_ctrl,
916 .s_ctrl = ov9740_s_ctrl,
917 .g_chip_ident = ov9740_g_chip_ident,
918#ifdef CONFIG_VIDEO_ADV_DEBUG
919 .g_register = ov9740_get_register,
920 .s_register = ov9740_set_register,
921#endif
Andrew Chew9bd060e2011-02-17 19:14:33 -0300922};
923
924static struct v4l2_subdev_video_ops ov9740_video_ops = {
925 .s_stream = ov9740_s_stream,
926 .s_mbus_fmt = ov9740_s_fmt,
927 .try_mbus_fmt = ov9740_try_fmt,
928 .enum_mbus_fmt = ov9740_enum_fmt,
929 .cropcap = ov9740_cropcap,
930 .g_crop = ov9740_g_crop,
931};
932
933static struct v4l2_subdev_ops ov9740_subdev_ops = {
934 .core = &ov9740_core_ops,
935 .video = &ov9740_video_ops,
936};
937
938/*
939 * i2c_driver function
940 */
941static int ov9740_probe(struct i2c_client *client,
942 const struct i2c_device_id *did)
943{
944 struct ov9740_priv *priv;
945 struct soc_camera_device *icd = client->dev.platform_data;
946 struct soc_camera_link *icl;
947 int ret;
948
949 if (!icd) {
950 dev_err(&client->dev, "Missing soc-camera data!\n");
951 return -EINVAL;
952 }
953
954 icl = to_soc_camera_link(icd);
955 if (!icl) {
956 dev_err(&client->dev, "Missing platform_data for driver\n");
957 return -EINVAL;
958 }
959
960 priv = kzalloc(sizeof(struct ov9740_priv), GFP_KERNEL);
961 if (!priv) {
962 dev_err(&client->dev, "Failed to allocate private data!\n");
963 return -ENOMEM;
964 }
965
966 v4l2_i2c_subdev_init(&priv->subdev, client, &ov9740_subdev_ops);
967
968 icd->ops = &ov9740_ops;
969
970 ret = ov9740_video_probe(icd, client);
971 if (ret < 0) {
972 icd->ops = NULL;
973 kfree(priv);
974 }
975
976 return ret;
977}
978
979static int ov9740_remove(struct i2c_client *client)
980{
981 struct ov9740_priv *priv = i2c_get_clientdata(client);
982
983 kfree(priv);
984
985 return 0;
986}
987
988static const struct i2c_device_id ov9740_id[] = {
989 { "ov9740", 0 },
990 { }
991};
992MODULE_DEVICE_TABLE(i2c, ov9740_id);
993
994static struct i2c_driver ov9740_i2c_driver = {
995 .driver = {
996 .name = "ov9740",
997 },
998 .probe = ov9740_probe,
999 .remove = ov9740_remove,
1000 .id_table = ov9740_id,
1001};
1002
1003static int __init ov9740_module_init(void)
1004{
1005 return i2c_add_driver(&ov9740_i2c_driver);
1006}
1007
1008static void __exit ov9740_module_exit(void)
1009{
1010 i2c_del_driver(&ov9740_i2c_driver);
1011}
1012
1013module_init(ov9740_module_init);
1014module_exit(ov9740_module_exit);
1015
1016MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV9740");
1017MODULE_AUTHOR("Andrew Chew <achew@nvidia.com>");
1018MODULE_LICENSE("GPL v2");