blob: 4156c9f6375bb980734ec591ad42eec3d1df9663 [file] [log] [blame]
Marek Vasut8d648272009-09-23 04:59:24 -03001/*
2 * OmniVision OV96xx Camera Driver
3 *
4 * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * Based on ov772x camera driver:
7 *
8 * Copyright (C) 2008 Renesas Solutions Corp.
9 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 *
11 * Based on ov7670 and soc_camera_platform driver,
12 *
13 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
14 * Copyright (C) 2008 Magnus Damm
15 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/i2c.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
27#include <linux/videodev2.h>
Guennadi Liakhovetskide2e3882011-07-26 12:28:10 -030028
29#include <media/soc_camera.h>
30#include <media/soc_mediabus.h>
Marek Vasut8d648272009-09-23 04:59:24 -030031#include <media/v4l2-chip-ident.h>
32#include <media/v4l2-common.h>
Marek Vasut8d648272009-09-23 04:59:24 -030033
34#include "ov9640.h"
35
David Cohenf7b74f72010-12-08 09:13:58 -030036#define to_ov9640_sensor(sd) container_of(sd, struct ov9640_priv, subdev)
37
Marek Vasut8d648272009-09-23 04:59:24 -030038/* default register setup */
39static const struct ov9640_reg ov9640_regs_dflt[] = {
40 { OV9640_COM5, OV9640_COM5_SYSCLK | OV9640_COM5_LONGEXP },
41 { OV9640_COM6, OV9640_COM6_OPT_BLC | OV9640_COM6_ADBLC_BIAS |
42 OV9640_COM6_FMT_RST | OV9640_COM6_ADBLC_OPTEN },
43 { OV9640_PSHFT, OV9640_PSHFT_VAL(0x01) },
44 { OV9640_ACOM, OV9640_ACOM_2X_ANALOG | OV9640_ACOM_RSVD },
45 { OV9640_TSLB, OV9640_TSLB_YUYV_UYVY },
46 { OV9640_COM16, OV9640_COM16_RB_AVG },
47
48 /* Gamma curve P */
49 { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 },
50 { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 },
51 { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 },
52 { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 },
53
54 /* Gamma curve T */
55 { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 },
56 { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 },
57 { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e },
58 { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 },
59};
60
61/* Configurations
62 * NOTE: for YUV, alter the following registers:
63 * COM12 |= OV9640_COM12_YUV_AVG
64 *
65 * for RGB, alter the following registers:
Mauro Carvalho Chehab000f64e2010-03-11 01:58:12 -030066 * COM7 |= OV9640_COM7_RGB
67 * COM13 |= OV9640_COM13_RGB_AVG
68 * COM15 |= proper RGB color encoding mode
Marek Vasut8d648272009-09-23 04:59:24 -030069 */
70static const struct ov9640_reg ov9640_regs_qqcif[] = {
71 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x0f) },
72 { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
73 { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
74 { OV9640_COM7, OV9640_COM7_QCIF },
75 { OV9640_COM12, OV9640_COM12_RSVD },
76 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
77 { OV9640_COM15, OV9640_COM15_OR_10F0 },
78};
79
80static const struct ov9640_reg ov9640_regs_qqvga[] = {
81 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
82 { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
83 { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
84 { OV9640_COM7, OV9640_COM7_QVGA },
85 { OV9640_COM12, OV9640_COM12_RSVD },
86 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
87 { OV9640_COM15, OV9640_COM15_OR_10F0 },
88};
89
90static const struct ov9640_reg ov9640_regs_qcif[] = {
91 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
92 { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
93 { OV9640_COM7, OV9640_COM7_QCIF },
94 { OV9640_COM12, OV9640_COM12_RSVD },
95 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
96 { OV9640_COM15, OV9640_COM15_OR_10F0 },
97};
98
99static const struct ov9640_reg ov9640_regs_qvga[] = {
100 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
101 { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
102 { OV9640_COM7, OV9640_COM7_QVGA },
103 { OV9640_COM12, OV9640_COM12_RSVD },
104 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
105 { OV9640_COM15, OV9640_COM15_OR_10F0 },
106};
107
108static const struct ov9640_reg ov9640_regs_cif[] = {
109 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
110 { OV9640_COM3, OV9640_COM3_VP },
111 { OV9640_COM7, OV9640_COM7_CIF },
112 { OV9640_COM12, OV9640_COM12_RSVD },
113 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
114 { OV9640_COM15, OV9640_COM15_OR_10F0 },
115};
116
117static const struct ov9640_reg ov9640_regs_vga[] = {
118 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
119 { OV9640_COM3, OV9640_COM3_VP },
120 { OV9640_COM7, OV9640_COM7_VGA },
121 { OV9640_COM12, OV9640_COM12_RSVD },
122 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
123 { OV9640_COM15, OV9640_COM15_OR_10F0 },
124};
125
126static const struct ov9640_reg ov9640_regs_sxga[] = {
127 { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
128 { OV9640_COM3, OV9640_COM3_VP },
129 { OV9640_COM7, 0 },
130 { OV9640_COM12, OV9640_COM12_RSVD },
131 { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
132 { OV9640_COM15, OV9640_COM15_OR_10F0 },
133};
134
135static const struct ov9640_reg ov9640_regs_yuv[] = {
136 { OV9640_MTX1, 0x58 },
137 { OV9640_MTX2, 0x48 },
138 { OV9640_MTX3, 0x10 },
139 { OV9640_MTX4, 0x28 },
140 { OV9640_MTX5, 0x48 },
141 { OV9640_MTX6, 0x70 },
142 { OV9640_MTX7, 0x40 },
143 { OV9640_MTX8, 0x40 },
144 { OV9640_MTX9, 0x40 },
145 { OV9640_MTXS, 0x0f },
146};
147
148static const struct ov9640_reg ov9640_regs_rgb[] = {
149 { OV9640_MTX1, 0x71 },
150 { OV9640_MTX2, 0x3e },
151 { OV9640_MTX3, 0x0c },
152 { OV9640_MTX4, 0x33 },
153 { OV9640_MTX5, 0x72 },
154 { OV9640_MTX6, 0x00 },
155 { OV9640_MTX7, 0x2b },
156 { OV9640_MTX8, 0x66 },
157 { OV9640_MTX9, 0xd2 },
158 { OV9640_MTXS, 0x65 },
159};
160
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300161static enum v4l2_mbus_pixelcode ov9640_codes[] = {
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300162 V4L2_MBUS_FMT_UYVY8_2X8,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300163 V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
164 V4L2_MBUS_FMT_RGB565_2X8_LE,
Marek Vasut8d648272009-09-23 04:59:24 -0300165};
166
167static const struct v4l2_queryctrl ov9640_controls[] = {
168 {
169 .id = V4L2_CID_VFLIP,
170 .type = V4L2_CTRL_TYPE_BOOLEAN,
171 .name = "Flip Vertically",
172 .minimum = 0,
173 .maximum = 1,
174 .step = 1,
175 .default_value = 0,
176 },
177 {
178 .id = V4L2_CID_HFLIP,
179 .type = V4L2_CTRL_TYPE_BOOLEAN,
180 .name = "Flip Horizontally",
181 .minimum = 0,
182 .maximum = 1,
183 .step = 1,
184 .default_value = 0,
185 },
186};
187
188/* read a register */
189static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val)
190{
191 int ret;
192 u8 data = reg;
193 struct i2c_msg msg = {
194 .addr = client->addr,
195 .flags = 0,
196 .len = 1,
197 .buf = &data,
198 };
199
200 ret = i2c_transfer(client->adapter, &msg, 1);
201 if (ret < 0)
202 goto err;
203
204 msg.flags = I2C_M_RD;
205 ret = i2c_transfer(client->adapter, &msg, 1);
206 if (ret < 0)
207 goto err;
208
209 *val = data;
210 return 0;
211
212err:
213 dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
214 return ret;
215}
216
217/* write a register */
218static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val)
219{
220 int ret;
221 u8 _val;
222 unsigned char data[2] = { reg, val };
223 struct i2c_msg msg = {
224 .addr = client->addr,
225 .flags = 0,
226 .len = 2,
227 .buf = data,
228 };
229
230 ret = i2c_transfer(client->adapter, &msg, 1);
231 if (ret < 0) {
232 dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
233 return ret;
234 }
235
236 /* we have to read the register back ... no idea why, maybe HW bug */
237 ret = ov9640_reg_read(client, reg, &_val);
238 if (ret)
239 dev_err(&client->dev,
240 "Failed reading back register 0x%02x!\n", reg);
241
242 return 0;
243}
244
245
246/* Read a register, alter its bits, write it back */
247static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset)
248{
249 u8 val;
250 int ret;
251
252 ret = ov9640_reg_read(client, reg, &val);
253 if (ret) {
254 dev_err(&client->dev,
255 "[Read]-Modify-Write of register %02x failed!\n", reg);
256 return val;
257 }
258
259 val |= set;
260 val &= ~unset;
261
262 ret = ov9640_reg_write(client, reg, val);
263 if (ret)
264 dev_err(&client->dev,
265 "Read-Modify-[Write] of register %02x failed!\n", reg);
266
267 return ret;
268}
269
270/* Soft reset the camera. This has nothing to do with the RESET pin! */
271static int ov9640_reset(struct i2c_client *client)
272{
273 int ret;
274
275 ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET);
276 if (ret)
277 dev_err(&client->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300278 "An error occurred while entering soft reset!\n");
Marek Vasut8d648272009-09-23 04:59:24 -0300279
280 return ret;
281}
282
283/* Start/Stop streaming from the device */
284static int ov9640_s_stream(struct v4l2_subdev *sd, int enable)
285{
286 return 0;
287}
288
289/* Alter bus settings on camera side */
290static int ov9640_set_bus_param(struct soc_camera_device *icd,
291 unsigned long flags)
292{
293 return 0;
294}
295
296/* Request bus settings on camera side */
297static unsigned long ov9640_query_bus_param(struct soc_camera_device *icd)
298{
299 struct soc_camera_link *icl = to_soc_camera_link(icd);
300
301 /*
302 * REVISIT: the camera probably can do 10 bit transfers, but I don't
303 * have those pins connected on my hardware.
304 */
305 unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
306 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
307 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
308
309 return soc_camera_apply_sensor_flags(icl, flags);
310}
311
312/* Get status of additional camera capabilities */
313static int ov9640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
314{
David Cohenf7b74f72010-12-08 09:13:58 -0300315 struct ov9640_priv *priv = to_ov9640_sensor(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300316
317 switch (ctrl->id) {
318 case V4L2_CID_VFLIP:
319 ctrl->value = priv->flag_vflip;
320 break;
321 case V4L2_CID_HFLIP:
322 ctrl->value = priv->flag_hflip;
323 break;
324 }
325 return 0;
326}
327
328/* Set status of additional camera capabilities */
329static int ov9640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
330{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300331 struct i2c_client *client = v4l2_get_subdevdata(sd);
David Cohenf7b74f72010-12-08 09:13:58 -0300332 struct ov9640_priv *priv = to_ov9640_sensor(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300333
334 int ret = 0;
335
336 switch (ctrl->id) {
337 case V4L2_CID_VFLIP:
338 priv->flag_vflip = ctrl->value;
339 if (ctrl->value)
340 ret = ov9640_reg_rmw(client, OV9640_MVFP,
341 OV9640_MVFP_V, 0);
342 else
343 ret = ov9640_reg_rmw(client, OV9640_MVFP,
344 0, OV9640_MVFP_V);
345 break;
346 case V4L2_CID_HFLIP:
347 priv->flag_hflip = ctrl->value;
348 if (ctrl->value)
349 ret = ov9640_reg_rmw(client, OV9640_MVFP,
350 OV9640_MVFP_H, 0);
351 else
352 ret = ov9640_reg_rmw(client, OV9640_MVFP,
353 0, OV9640_MVFP_H);
354 break;
355 }
356
357 return ret;
358}
359
360/* Get chip identification */
361static int ov9640_g_chip_ident(struct v4l2_subdev *sd,
362 struct v4l2_dbg_chip_ident *id)
363{
David Cohenf7b74f72010-12-08 09:13:58 -0300364 struct ov9640_priv *priv = to_ov9640_sensor(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300365
366 id->ident = priv->model;
367 id->revision = priv->revision;
368
369 return 0;
370}
371
372#ifdef CONFIG_VIDEO_ADV_DEBUG
373static int ov9640_get_register(struct v4l2_subdev *sd,
374 struct v4l2_dbg_register *reg)
375{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300376 struct i2c_client *client = v4l2_get_subdevdata(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300377 int ret;
378 u8 val;
379
380 if (reg->reg & ~0xff)
381 return -EINVAL;
382
383 reg->size = 1;
384
385 ret = ov9640_reg_read(client, reg->reg, &val);
386 if (ret)
387 return ret;
388
389 reg->val = (__u64)val;
390
391 return 0;
392}
393
394static int ov9640_set_register(struct v4l2_subdev *sd,
395 struct v4l2_dbg_register *reg)
396{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300397 struct i2c_client *client = v4l2_get_subdevdata(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300398
399 if (reg->reg & ~0xff || reg->val & ~0xff)
400 return -EINVAL;
401
402 return ov9640_reg_write(client, reg->reg, reg->val);
403}
404#endif
405
406/* select nearest higher resolution for capture */
407static void ov9640_res_roundup(u32 *width, u32 *height)
408{
409 int i;
410 enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
411 int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
412 int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
413
414 for (i = 0; i < ARRAY_SIZE(res_x); i++) {
415 if (res_x[i] >= *width && res_y[i] >= *height) {
416 *width = res_x[i];
417 *height = res_y[i];
418 return;
419 }
420 }
421
422 *width = res_x[SXGA];
423 *height = res_y[SXGA];
424}
425
426/* Prepare necessary register changes depending on color encoding */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300427static void ov9640_alter_regs(enum v4l2_mbus_pixelcode code,
428 struct ov9640_reg_alt *alt)
Marek Vasut8d648272009-09-23 04:59:24 -0300429{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300430 switch (code) {
431 default:
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300432 case V4L2_MBUS_FMT_UYVY8_2X8:
Marek Vasut8d648272009-09-23 04:59:24 -0300433 alt->com12 = OV9640_COM12_YUV_AVG;
434 alt->com13 = OV9640_COM13_Y_DELAY_EN |
435 OV9640_COM13_YUV_DLY(0x01);
436 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300437 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
Marek Vasut8d648272009-09-23 04:59:24 -0300438 alt->com7 = OV9640_COM7_RGB;
439 alt->com13 = OV9640_COM13_RGB_AVG;
440 alt->com15 = OV9640_COM15_RGB_555;
441 break;
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300442 case V4L2_MBUS_FMT_RGB565_2X8_LE:
Marek Vasut8d648272009-09-23 04:59:24 -0300443 alt->com7 = OV9640_COM7_RGB;
444 alt->com13 = OV9640_COM13_RGB_AVG;
445 alt->com15 = OV9640_COM15_RGB_565;
446 break;
447 };
448}
449
450/* Setup registers according to resolution and color encoding */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300451static int ov9640_write_regs(struct i2c_client *client, u32 width,
452 enum v4l2_mbus_pixelcode code, struct ov9640_reg_alt *alts)
Marek Vasut8d648272009-09-23 04:59:24 -0300453{
454 const struct ov9640_reg *ov9640_regs, *matrix_regs;
455 int ov9640_regs_len, matrix_regs_len;
456 int i, ret;
457 u8 val;
458
459 /* select register configuration for given resolution */
460 switch (width) {
461 case W_QQCIF:
462 ov9640_regs = ov9640_regs_qqcif;
463 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
464 break;
465 case W_QQVGA:
466 ov9640_regs = ov9640_regs_qqvga;
467 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
468 break;
469 case W_QCIF:
470 ov9640_regs = ov9640_regs_qcif;
471 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
472 break;
473 case W_QVGA:
474 ov9640_regs = ov9640_regs_qvga;
475 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
476 break;
477 case W_CIF:
478 ov9640_regs = ov9640_regs_cif;
479 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
480 break;
481 case W_VGA:
482 ov9640_regs = ov9640_regs_vga;
483 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
484 break;
485 case W_SXGA:
486 ov9640_regs = ov9640_regs_sxga;
487 ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
488 break;
489 default:
490 dev_err(&client->dev, "Failed to select resolution!\n");
491 return -EINVAL;
492 }
493
494 /* select color matrix configuration for given color encoding */
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300495 if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
Marek Vasut8d648272009-09-23 04:59:24 -0300496 matrix_regs = ov9640_regs_yuv;
497 matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
498 } else {
499 matrix_regs = ov9640_regs_rgb;
500 matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
501 }
502
503 /* write register settings into the module */
504 for (i = 0; i < ov9640_regs_len; i++) {
505 val = ov9640_regs[i].val;
506
507 switch (ov9640_regs[i].reg) {
508 case OV9640_COM7:
509 val |= alts->com7;
510 break;
511 case OV9640_COM12:
512 val |= alts->com12;
513 break;
514 case OV9640_COM13:
515 val |= alts->com13;
516 break;
517 case OV9640_COM15:
518 val |= alts->com15;
519 break;
520 }
521
522 ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
523 if (ret)
524 return ret;
525 }
526
527 /* write color matrix configuration into the module */
528 for (i = 0; i < matrix_regs_len; i++) {
529 ret = ov9640_reg_write(client, matrix_regs[i].reg,
530 matrix_regs[i].val);
531 if (ret)
532 return ret;
533 }
534
535 return 0;
536}
537
538/* program default register values */
539static int ov9640_prog_dflt(struct i2c_client *client)
540{
541 int i, ret;
542
543 for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
544 ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
545 ov9640_regs_dflt[i].val);
546 if (ret)
547 return ret;
548 }
549
550 /* wait for the changes to actually happen, 140ms are not enough yet */
551 mdelay(150);
552
553 return 0;
554}
555
556/* set the format we will capture in */
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300557static int ov9640_s_fmt(struct v4l2_subdev *sd,
558 struct v4l2_mbus_framefmt *mf)
Marek Vasut8d648272009-09-23 04:59:24 -0300559{
Laurent Pinchartc4ce6d12010-07-30 17:24:54 -0300560 struct i2c_client *client = v4l2_get_subdevdata(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300561 struct ov9640_reg_alt alts = {0};
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300562 enum v4l2_colorspace cspace;
563 enum v4l2_mbus_pixelcode code = mf->code;
Marek Vasut8d648272009-09-23 04:59:24 -0300564 int ret;
565
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300566 ov9640_res_roundup(&mf->width, &mf->height);
567 ov9640_alter_regs(mf->code, &alts);
Marek Vasut8d648272009-09-23 04:59:24 -0300568
569 ov9640_reset(client);
570
571 ret = ov9640_prog_dflt(client);
572 if (ret)
573 return ret;
574
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300575 switch (code) {
576 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
577 case V4L2_MBUS_FMT_RGB565_2X8_LE:
578 cspace = V4L2_COLORSPACE_SRGB;
579 break;
580 default:
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300581 code = V4L2_MBUS_FMT_UYVY8_2X8;
582 case V4L2_MBUS_FMT_UYVY8_2X8:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300583 cspace = V4L2_COLORSPACE_JPEG;
584 }
585
586 ret = ov9640_write_regs(client, mf->width, code, &alts);
587 if (!ret) {
588 mf->code = code;
589 mf->colorspace = cspace;
590 }
591
592 return ret;
Marek Vasut8d648272009-09-23 04:59:24 -0300593}
594
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300595static int ov9640_try_fmt(struct v4l2_subdev *sd,
596 struct v4l2_mbus_framefmt *mf)
Marek Vasut8d648272009-09-23 04:59:24 -0300597{
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300598 ov9640_res_roundup(&mf->width, &mf->height);
Marek Vasut8d648272009-09-23 04:59:24 -0300599
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300600 mf->field = V4L2_FIELD_NONE;
Marek Vasut8d648272009-09-23 04:59:24 -0300601
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300602 switch (mf->code) {
603 case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
604 case V4L2_MBUS_FMT_RGB565_2X8_LE:
605 mf->colorspace = V4L2_COLORSPACE_SRGB;
606 break;
607 default:
Guennadi Liakhovetskiace6e972010-07-22 16:52:51 -0300608 mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
609 case V4L2_MBUS_FMT_UYVY8_2X8:
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300610 mf->colorspace = V4L2_COLORSPACE_JPEG;
611 }
612
613 return 0;
614}
615
Hans Verkuil3805f202010-05-08 17:55:00 -0300616static int ov9640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300617 enum v4l2_mbus_pixelcode *code)
618{
Hans Verkuil3805f202010-05-08 17:55:00 -0300619 if (index >= ARRAY_SIZE(ov9640_codes))
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300620 return -EINVAL;
621
622 *code = ov9640_codes[index];
Marek Vasut8d648272009-09-23 04:59:24 -0300623 return 0;
624}
625
626static int ov9640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
627{
628 a->c.left = 0;
629 a->c.top = 0;
630 a->c.width = W_SXGA;
631 a->c.height = H_SXGA;
632 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
633
634 return 0;
635}
636
637static int ov9640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
638{
639 a->bounds.left = 0;
640 a->bounds.top = 0;
641 a->bounds.width = W_SXGA;
642 a->bounds.height = H_SXGA;
643 a->defrect = a->bounds;
644 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
645 a->pixelaspect.numerator = 1;
646 a->pixelaspect.denominator = 1;
647
648 return 0;
649}
650
651
652
653static int ov9640_video_probe(struct soc_camera_device *icd,
654 struct i2c_client *client)
655{
David Cohen63c97182010-12-08 09:13:59 -0300656 struct v4l2_subdev *sd = i2c_get_clientdata(client);
657 struct ov9640_priv *priv = to_ov9640_sensor(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300658 u8 pid, ver, midh, midl;
659 const char *devname;
660 int ret = 0;
661
Guennadi Liakhovetski7dfff952011-07-15 20:03:38 -0300662 /* We must have a parent by now. And it cannot be a wrong one. */
663 BUG_ON(!icd->parent ||
664 to_soc_camera_host(icd->parent)->nr != icd->iface);
Marek Vasut8d648272009-09-23 04:59:24 -0300665
Marek Vasut8d648272009-09-23 04:59:24 -0300666 /*
667 * check and show product ID and manufacturer ID
668 */
669
670 ret = ov9640_reg_read(client, OV9640_PID, &pid);
671 if (ret)
672 goto err;
673
674 ret = ov9640_reg_read(client, OV9640_VER, &ver);
675 if (ret)
676 goto err;
677
678 ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
679 if (ret)
680 goto err;
681
682 ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
683 if (ret)
684 goto err;
685
686 switch (VERSION(pid, ver)) {
687 case OV9640_V2:
688 devname = "ov9640";
689 priv->model = V4L2_IDENT_OV9640;
690 priv->revision = 2;
691 case OV9640_V3:
692 devname = "ov9640";
693 priv->model = V4L2_IDENT_OV9640;
694 priv->revision = 3;
695 break;
696 default:
697 dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
698 ret = -ENODEV;
699 goto err;
700 }
701
702 dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
703 devname, pid, ver, midh, midl);
704
705err:
706 return ret;
707}
708
709static struct soc_camera_ops ov9640_ops = {
710 .set_bus_param = ov9640_set_bus_param,
711 .query_bus_param = ov9640_query_bus_param,
712 .controls = ov9640_controls,
713 .num_controls = ARRAY_SIZE(ov9640_controls),
714};
715
716static struct v4l2_subdev_core_ops ov9640_core_ops = {
717 .g_ctrl = ov9640_g_ctrl,
718 .s_ctrl = ov9640_s_ctrl,
719 .g_chip_ident = ov9640_g_chip_ident,
720#ifdef CONFIG_VIDEO_ADV_DEBUG
721 .g_register = ov9640_get_register,
722 .s_register = ov9640_set_register,
723#endif
724
725};
726
Guennadi Liakhovetskide2e3882011-07-26 12:28:10 -0300727static int ov9640_g_mbus_config(struct v4l2_subdev *sd,
728 struct v4l2_mbus_config *cfg)
729{
730 struct i2c_client *client = v4l2_get_subdevdata(sd);
731 struct soc_camera_device *icd = client->dev.platform_data;
732 struct soc_camera_link *icl = to_soc_camera_link(icd);
733
734 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
735 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
736 V4L2_MBUS_DATA_ACTIVE_HIGH;
737 cfg->type = V4L2_MBUS_PARALLEL;
738 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
739
740 return 0;
741}
742
Marek Vasut8d648272009-09-23 04:59:24 -0300743static struct v4l2_subdev_video_ops ov9640_video_ops = {
Guennadi Liakhovetski760697b2009-12-11 11:46:49 -0300744 .s_stream = ov9640_s_stream,
745 .s_mbus_fmt = ov9640_s_fmt,
746 .try_mbus_fmt = ov9640_try_fmt,
747 .enum_mbus_fmt = ov9640_enum_fmt,
748 .cropcap = ov9640_cropcap,
749 .g_crop = ov9640_g_crop,
Guennadi Liakhovetskide2e3882011-07-26 12:28:10 -0300750 .g_mbus_config = ov9640_g_mbus_config,
Marek Vasut8d648272009-09-23 04:59:24 -0300751};
752
753static struct v4l2_subdev_ops ov9640_subdev_ops = {
754 .core = &ov9640_core_ops,
755 .video = &ov9640_video_ops,
756};
757
758/*
759 * i2c_driver function
760 */
761static int ov9640_probe(struct i2c_client *client,
762 const struct i2c_device_id *did)
763{
764 struct ov9640_priv *priv;
765 struct soc_camera_device *icd = client->dev.platform_data;
766 struct soc_camera_link *icl;
767 int ret;
768
769 if (!icd) {
770 dev_err(&client->dev, "Missing soc-camera data!\n");
771 return -EINVAL;
772 }
773
774 icl = to_soc_camera_link(icd);
775 if (!icl) {
776 dev_err(&client->dev, "Missing platform_data for driver\n");
777 return -EINVAL;
778 }
779
780 priv = kzalloc(sizeof(struct ov9640_priv), GFP_KERNEL);
781 if (!priv) {
782 dev_err(&client->dev,
783 "Failed to allocate memory for private data!\n");
784 return -ENOMEM;
785 }
786
787 v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
788
789 icd->ops = &ov9640_ops;
790
791 ret = ov9640_video_probe(icd, client);
792
793 if (ret) {
794 icd->ops = NULL;
Marek Vasut8d648272009-09-23 04:59:24 -0300795 kfree(priv);
796 }
797
798 return ret;
799}
800
801static int ov9640_remove(struct i2c_client *client)
802{
David Cohen63c97182010-12-08 09:13:59 -0300803 struct v4l2_subdev *sd = i2c_get_clientdata(client);
804 struct ov9640_priv *priv = to_ov9640_sensor(sd);
Marek Vasut8d648272009-09-23 04:59:24 -0300805
Marek Vasut8d648272009-09-23 04:59:24 -0300806 kfree(priv);
807 return 0;
808}
809
810static const struct i2c_device_id ov9640_id[] = {
811 { "ov9640", 0 },
812 { }
813};
814MODULE_DEVICE_TABLE(i2c, ov9640_id);
815
816static struct i2c_driver ov9640_i2c_driver = {
817 .driver = {
818 .name = "ov9640",
819 },
820 .probe = ov9640_probe,
821 .remove = ov9640_remove,
822 .id_table = ov9640_id,
823};
824
825static int __init ov9640_module_init(void)
826{
827 return i2c_add_driver(&ov9640_i2c_driver);
828}
829
830static void __exit ov9640_module_exit(void)
831{
832 i2c_del_driver(&ov9640_i2c_driver);
833}
834
835module_init(ov9640_module_init);
836module_exit(ov9640_module_exit);
837
838MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx");
839MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
840MODULE_LICENSE("GPL v2");