2 * Driver for IMX074 CMOS Image Sensor from Sony
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * Partially inspired by the IMX074 driver from the Android / MSM tree
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.
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/videodev2.h>
18 #include <media/soc_camera.h>
19 #include <media/soc_mediabus.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-chip-ident.h>
23 /* IMX074 registers */
25 #define MODE_SELECT 0x0100
26 #define IMAGE_ORIENTATION 0x0101
27 #define GROUPED_PARAMETER_HOLD 0x0104
29 /* Integration Time */
30 #define COARSE_INTEGRATION_TIME_HI 0x0202
31 #define COARSE_INTEGRATION_TIME_LO 0x0203
33 #define ANALOGUE_GAIN_CODE_GLOBAL_HI 0x0204
34 #define ANALOGUE_GAIN_CODE_GLOBAL_LO 0x0205
37 #define PRE_PLL_CLK_DIV 0x0305
38 #define PLL_MULTIPLIER 0x0307
39 #define PLSTATIM 0x302b
40 #define VNDMY_ABLMGSHLMT 0x300a
41 #define Y_OPBADDR_START_DI 0x3014
43 #define FRAME_LENGTH_LINES_HI 0x0340
44 #define FRAME_LENGTH_LINES_LO 0x0341
45 #define LINE_LENGTH_PCK_HI 0x0342
46 #define LINE_LENGTH_PCK_LO 0x0343
47 #define YADDR_START 0x0347
48 #define YADDR_END 0x034b
49 #define X_OUTPUT_SIZE_MSB 0x034c
50 #define X_OUTPUT_SIZE_LSB 0x034d
51 #define Y_OUTPUT_SIZE_MSB 0x034e
52 #define Y_OUTPUT_SIZE_LSB 0x034f
53 #define X_EVEN_INC 0x0381
54 #define X_ODD_INC 0x0383
55 #define Y_EVEN_INC 0x0385
56 #define Y_ODD_INC 0x0387
58 #define HMODEADD 0x3001
59 #define VMODEADD 0x3016
60 #define VAPPLINE_START 0x3069
61 #define VAPPLINE_END 0x306b
62 #define SHUTTER 0x3086
63 #define HADDAVE 0x30e8
64 #define LANESEL 0x3301
66 /* IMX074 supported geometry */
67 #define IMX074_WIDTH 1052
68 #define IMX074_HEIGHT 780
70 /* IMX074 has only one fixed colorspace per pixelcode */
71 struct imx074_datafmt
{
72 enum v4l2_mbus_pixelcode code
;
73 enum v4l2_colorspace colorspace
;
77 struct v4l2_subdev subdev
;
78 const struct imx074_datafmt
*fmt
;
81 static const struct imx074_datafmt imx074_colour_fmts
[] = {
82 {V4L2_MBUS_FMT_SBGGR8_1X8
, V4L2_COLORSPACE_SRGB
},
85 static struct imx074
*to_imx074(const struct i2c_client
*client
)
87 return container_of(i2c_get_clientdata(client
), struct imx074
, subdev
);
90 /* Find a data format by a pixel code in an array */
91 static const struct imx074_datafmt
*imx074_find_datafmt(enum v4l2_mbus_pixelcode code
)
95 for (i
= 0; i
< ARRAY_SIZE(imx074_colour_fmts
); i
++)
96 if (imx074_colour_fmts
[i
].code
== code
)
97 return imx074_colour_fmts
+ i
;
102 static int reg_write(struct i2c_client
*client
, const u16 addr
, const u8 data
)
104 struct i2c_adapter
*adap
= client
->adapter
;
109 msg
.addr
= client
->addr
;
118 ret
= i2c_transfer(adap
, &msg
, 1);
122 return ret
== 1 ? 0 : -EIO
;
125 static int reg_read(struct i2c_client
*client
, const u16 addr
)
127 u8 buf
[2] = {addr
>> 8, addr
& 0xff};
129 struct i2c_msg msgs
[] = {
131 .addr
= client
->addr
,
136 .addr
= client
->addr
,
143 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
145 dev_warn(&client
->dev
, "Reading register %x from %x failed\n",
150 return buf
[0] & 0xff; /* no sign-extension */
153 static int imx074_try_fmt(struct v4l2_subdev
*sd
,
154 struct v4l2_mbus_framefmt
*mf
)
156 const struct imx074_datafmt
*fmt
= imx074_find_datafmt(mf
->code
);
158 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
161 mf
->code
= imx074_colour_fmts
[0].code
;
162 mf
->colorspace
= imx074_colour_fmts
[0].colorspace
;
165 mf
->width
= IMX074_WIDTH
;
166 mf
->height
= IMX074_HEIGHT
;
167 mf
->field
= V4L2_FIELD_NONE
;
172 static int imx074_s_fmt(struct v4l2_subdev
*sd
,
173 struct v4l2_mbus_framefmt
*mf
)
175 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
176 struct imx074
*priv
= to_imx074(client
);
178 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
180 /* MIPI CSI could have changed the format, double-check */
181 if (!imx074_find_datafmt(mf
->code
))
184 imx074_try_fmt(sd
, mf
);
186 priv
->fmt
= imx074_find_datafmt(mf
->code
);
191 static int imx074_g_fmt(struct v4l2_subdev
*sd
,
192 struct v4l2_mbus_framefmt
*mf
)
194 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
195 struct imx074
*priv
= to_imx074(client
);
197 const struct imx074_datafmt
*fmt
= priv
->fmt
;
199 mf
->code
= fmt
->code
;
200 mf
->colorspace
= fmt
->colorspace
;
201 mf
->width
= IMX074_WIDTH
;
202 mf
->height
= IMX074_HEIGHT
;
203 mf
->field
= V4L2_FIELD_NONE
;
208 static int imx074_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
210 struct v4l2_rect
*rect
= &a
->c
;
212 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
215 rect
->width
= IMX074_WIDTH
;
216 rect
->height
= IMX074_HEIGHT
;
221 static int imx074_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
225 a
->bounds
.width
= IMX074_WIDTH
;
226 a
->bounds
.height
= IMX074_HEIGHT
;
227 a
->defrect
= a
->bounds
;
228 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
229 a
->pixelaspect
.numerator
= 1;
230 a
->pixelaspect
.denominator
= 1;
235 static int imx074_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
236 enum v4l2_mbus_pixelcode
*code
)
238 if ((unsigned int)index
>= ARRAY_SIZE(imx074_colour_fmts
))
241 *code
= imx074_colour_fmts
[index
].code
;
245 static int imx074_s_stream(struct v4l2_subdev
*sd
, int enable
)
247 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
249 /* MODE_SELECT: stream or standby */
250 return reg_write(client
, MODE_SELECT
, !!enable
);
253 static int imx074_g_chip_ident(struct v4l2_subdev
*sd
,
254 struct v4l2_dbg_chip_ident
*id
)
256 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
258 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
261 if (id
->match
.addr
!= client
->addr
)
264 id
->ident
= V4L2_IDENT_IMX074
;
270 static int imx074_g_mbus_config(struct v4l2_subdev
*sd
,
271 struct v4l2_mbus_config
*cfg
)
273 cfg
->type
= V4L2_MBUS_CSI2
;
274 cfg
->flags
= V4L2_MBUS_CSI2_2_LANE
|
275 V4L2_MBUS_CSI2_CHANNEL_0
|
276 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK
;
281 static struct v4l2_subdev_video_ops imx074_subdev_video_ops
= {
282 .s_stream
= imx074_s_stream
,
283 .s_mbus_fmt
= imx074_s_fmt
,
284 .g_mbus_fmt
= imx074_g_fmt
,
285 .try_mbus_fmt
= imx074_try_fmt
,
286 .enum_mbus_fmt
= imx074_enum_fmt
,
287 .g_crop
= imx074_g_crop
,
288 .cropcap
= imx074_cropcap
,
289 .g_mbus_config
= imx074_g_mbus_config
,
292 static struct v4l2_subdev_core_ops imx074_subdev_core_ops
= {
293 .g_chip_ident
= imx074_g_chip_ident
,
296 static struct v4l2_subdev_ops imx074_subdev_ops
= {
297 .core
= &imx074_subdev_core_ops
,
298 .video
= &imx074_subdev_video_ops
,
301 static int imx074_video_probe(struct i2c_client
*client
)
306 /* Read sensor Model ID */
307 ret
= reg_read(client
, 0);
313 ret
= reg_read(client
, 1);
319 dev_info(&client
->dev
, "Chip ID 0x%04x detected\n", id
);
324 /* PLL Setting EXTCLK=24MHz, 22.5times */
325 reg_write(client
, PLL_MULTIPLIER
, 0x2D);
326 reg_write(client
, PRE_PLL_CLK_DIV
, 0x02);
327 reg_write(client
, PLSTATIM
, 0x4B);
330 reg_write(client
, 0x3024, 0x00);
332 reg_write(client
, IMAGE_ORIENTATION
, 0x00);
335 * 0x08+0x08 = top 8 bits
336 * 0x0a+0x08 = compressed 8-bits
337 * 0x0a+0x0a = 10 bits
339 reg_write(client
, 0x0112, 0x08);
340 reg_write(client
, 0x0113, 0x08);
342 /* Base setting for High frame mode */
343 reg_write(client
, VNDMY_ABLMGSHLMT
, 0x80);
344 reg_write(client
, Y_OPBADDR_START_DI
, 0x08);
345 reg_write(client
, 0x3015, 0x37);
346 reg_write(client
, 0x301C, 0x01);
347 reg_write(client
, 0x302C, 0x05);
348 reg_write(client
, 0x3031, 0x26);
349 reg_write(client
, 0x3041, 0x60);
350 reg_write(client
, 0x3051, 0x24);
351 reg_write(client
, 0x3053, 0x34);
352 reg_write(client
, 0x3057, 0xC0);
353 reg_write(client
, 0x305C, 0x09);
354 reg_write(client
, 0x305D, 0x07);
355 reg_write(client
, 0x3060, 0x30);
356 reg_write(client
, 0x3065, 0x00);
357 reg_write(client
, 0x30AA, 0x08);
358 reg_write(client
, 0x30AB, 0x1C);
359 reg_write(client
, 0x30B0, 0x32);
360 reg_write(client
, 0x30B2, 0x83);
361 reg_write(client
, 0x30D3, 0x04);
362 reg_write(client
, 0x3106, 0x78);
363 reg_write(client
, 0x310C, 0x82);
364 reg_write(client
, 0x3304, 0x05);
365 reg_write(client
, 0x3305, 0x04);
366 reg_write(client
, 0x3306, 0x11);
367 reg_write(client
, 0x3307, 0x02);
368 reg_write(client
, 0x3308, 0x0C);
369 reg_write(client
, 0x3309, 0x06);
370 reg_write(client
, 0x330A, 0x08);
371 reg_write(client
, 0x330B, 0x04);
372 reg_write(client
, 0x330C, 0x08);
373 reg_write(client
, 0x330D, 0x06);
374 reg_write(client
, 0x330E, 0x01);
375 reg_write(client
, 0x3381, 0x00);
377 /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */
378 /* 1608 = 1560 + 48 (black lines) */
379 reg_write(client
, FRAME_LENGTH_LINES_HI
, 0x06);
380 reg_write(client
, FRAME_LENGTH_LINES_LO
, 0x48);
381 reg_write(client
, YADDR_START
, 0x00);
382 reg_write(client
, YADDR_END
, 0x2F);
384 reg_write(client
, X_OUTPUT_SIZE_MSB
, 0x08);
385 reg_write(client
, X_OUTPUT_SIZE_LSB
, 0x38);
387 reg_write(client
, Y_OUTPUT_SIZE_MSB
, 0x06);
388 reg_write(client
, Y_OUTPUT_SIZE_LSB
, 0x18);
389 reg_write(client
, X_EVEN_INC
, 0x01);
390 reg_write(client
, X_ODD_INC
, 0x03);
391 reg_write(client
, Y_EVEN_INC
, 0x01);
392 reg_write(client
, Y_ODD_INC
, 0x03);
393 reg_write(client
, HMODEADD
, 0x00);
394 reg_write(client
, VMODEADD
, 0x16);
395 reg_write(client
, VAPPLINE_START
, 0x24);
396 reg_write(client
, VAPPLINE_END
, 0x53);
397 reg_write(client
, SHUTTER
, 0x00);
398 reg_write(client
, HADDAVE
, 0x80);
400 reg_write(client
, LANESEL
, 0x00);
402 reg_write(client
, GROUPED_PARAMETER_HOLD
, 0x00); /* off */
407 static int imx074_probe(struct i2c_client
*client
,
408 const struct i2c_device_id
*did
)
411 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
412 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
416 dev_err(&client
->dev
, "IMX074: missing platform data!\n");
420 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
421 dev_warn(&adapter
->dev
,
422 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
426 priv
= kzalloc(sizeof(struct imx074
), GFP_KERNEL
);
430 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &imx074_subdev_ops
);
432 priv
->fmt
= &imx074_colour_fmts
[0];
434 ret
= imx074_video_probe(client
);
443 static int imx074_remove(struct i2c_client
*client
)
445 struct imx074
*priv
= to_imx074(client
);
446 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
455 static const struct i2c_device_id imx074_id
[] = {
459 MODULE_DEVICE_TABLE(i2c
, imx074_id
);
461 static struct i2c_driver imx074_i2c_driver
= {
465 .probe
= imx074_probe
,
466 .remove
= imx074_remove
,
467 .id_table
= imx074_id
,
470 static int __init
imx074_mod_init(void)
472 return i2c_add_driver(&imx074_i2c_driver
);
475 static void __exit
imx074_mod_exit(void)
477 i2c_del_driver(&imx074_i2c_driver
);
480 module_init(imx074_mod_init
);
481 module_exit(imx074_mod_exit
);
483 MODULE_DESCRIPTION("Sony IMX074 Camera driver");
484 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
485 MODULE_LICENSE("GPL v2");