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/v4l2-mediabus.h>
16 #include <linux/slab.h>
17 #include <linux/videodev2.h>
18 #include <linux/module.h>
20 #include <media/soc_camera.h>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-clk.h>
23 #include <media/v4l2-subdev.h>
25 /* IMX074 registers */
27 #define MODE_SELECT 0x0100
28 #define IMAGE_ORIENTATION 0x0101
29 #define GROUPED_PARAMETER_HOLD 0x0104
31 /* Integration Time */
32 #define COARSE_INTEGRATION_TIME_HI 0x0202
33 #define COARSE_INTEGRATION_TIME_LO 0x0203
35 #define ANALOGUE_GAIN_CODE_GLOBAL_HI 0x0204
36 #define ANALOGUE_GAIN_CODE_GLOBAL_LO 0x0205
39 #define PRE_PLL_CLK_DIV 0x0305
40 #define PLL_MULTIPLIER 0x0307
41 #define PLSTATIM 0x302b
42 #define VNDMY_ABLMGSHLMT 0x300a
43 #define Y_OPBADDR_START_DI 0x3014
45 #define FRAME_LENGTH_LINES_HI 0x0340
46 #define FRAME_LENGTH_LINES_LO 0x0341
47 #define LINE_LENGTH_PCK_HI 0x0342
48 #define LINE_LENGTH_PCK_LO 0x0343
49 #define YADDR_START 0x0347
50 #define YADDR_END 0x034b
51 #define X_OUTPUT_SIZE_MSB 0x034c
52 #define X_OUTPUT_SIZE_LSB 0x034d
53 #define Y_OUTPUT_SIZE_MSB 0x034e
54 #define Y_OUTPUT_SIZE_LSB 0x034f
55 #define X_EVEN_INC 0x0381
56 #define X_ODD_INC 0x0383
57 #define Y_EVEN_INC 0x0385
58 #define Y_ODD_INC 0x0387
60 #define HMODEADD 0x3001
61 #define VMODEADD 0x3016
62 #define VAPPLINE_START 0x3069
63 #define VAPPLINE_END 0x306b
64 #define SHUTTER 0x3086
65 #define HADDAVE 0x30e8
66 #define LANESEL 0x3301
68 /* IMX074 supported geometry */
69 #define IMX074_WIDTH 1052
70 #define IMX074_HEIGHT 780
72 /* IMX074 has only one fixed colorspace per pixelcode */
73 struct imx074_datafmt
{
75 enum v4l2_colorspace colorspace
;
79 struct v4l2_subdev subdev
;
80 const struct imx074_datafmt
*fmt
;
84 static const struct imx074_datafmt imx074_colour_fmts
[] = {
85 {MEDIA_BUS_FMT_SBGGR8_1X8
, V4L2_COLORSPACE_SRGB
},
88 static struct imx074
*to_imx074(const struct i2c_client
*client
)
90 return container_of(i2c_get_clientdata(client
), struct imx074
, subdev
);
93 /* Find a data format by a pixel code in an array */
94 static const struct imx074_datafmt
*imx074_find_datafmt(u32 code
)
98 for (i
= 0; i
< ARRAY_SIZE(imx074_colour_fmts
); i
++)
99 if (imx074_colour_fmts
[i
].code
== code
)
100 return imx074_colour_fmts
+ i
;
105 static int reg_write(struct i2c_client
*client
, const u16 addr
, const u8 data
)
107 struct i2c_adapter
*adap
= client
->adapter
;
112 msg
.addr
= client
->addr
;
121 ret
= i2c_transfer(adap
, &msg
, 1);
125 return ret
== 1 ? 0 : -EIO
;
128 static int reg_read(struct i2c_client
*client
, const u16 addr
)
130 u8 buf
[2] = {addr
>> 8, addr
& 0xff};
132 struct i2c_msg msgs
[] = {
134 .addr
= client
->addr
,
139 .addr
= client
->addr
,
146 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
148 dev_warn(&client
->dev
, "Reading register %x from %x failed\n",
153 return buf
[0] & 0xff; /* no sign-extension */
156 static int imx074_try_fmt(struct v4l2_subdev
*sd
,
157 struct v4l2_mbus_framefmt
*mf
)
159 const struct imx074_datafmt
*fmt
= imx074_find_datafmt(mf
->code
);
161 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
164 mf
->code
= imx074_colour_fmts
[0].code
;
165 mf
->colorspace
= imx074_colour_fmts
[0].colorspace
;
168 mf
->width
= IMX074_WIDTH
;
169 mf
->height
= IMX074_HEIGHT
;
170 mf
->field
= V4L2_FIELD_NONE
;
175 static int imx074_s_fmt(struct v4l2_subdev
*sd
,
176 struct v4l2_mbus_framefmt
*mf
)
178 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
179 struct imx074
*priv
= to_imx074(client
);
181 dev_dbg(sd
->v4l2_dev
->dev
, "%s(%u)\n", __func__
, mf
->code
);
183 /* MIPI CSI could have changed the format, double-check */
184 if (!imx074_find_datafmt(mf
->code
))
187 imx074_try_fmt(sd
, mf
);
189 priv
->fmt
= imx074_find_datafmt(mf
->code
);
194 static int imx074_get_fmt(struct v4l2_subdev
*sd
,
195 struct v4l2_subdev_pad_config
*cfg
,
196 struct v4l2_subdev_format
*format
)
198 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
199 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
200 struct imx074
*priv
= to_imx074(client
);
202 const struct imx074_datafmt
*fmt
= priv
->fmt
;
207 mf
->code
= fmt
->code
;
208 mf
->colorspace
= fmt
->colorspace
;
209 mf
->width
= IMX074_WIDTH
;
210 mf
->height
= IMX074_HEIGHT
;
211 mf
->field
= V4L2_FIELD_NONE
;
216 static int imx074_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
218 struct v4l2_rect
*rect
= &a
->c
;
220 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
223 rect
->width
= IMX074_WIDTH
;
224 rect
->height
= IMX074_HEIGHT
;
229 static int imx074_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
233 a
->bounds
.width
= IMX074_WIDTH
;
234 a
->bounds
.height
= IMX074_HEIGHT
;
235 a
->defrect
= a
->bounds
;
236 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
237 a
->pixelaspect
.numerator
= 1;
238 a
->pixelaspect
.denominator
= 1;
243 static int imx074_enum_mbus_code(struct v4l2_subdev
*sd
,
244 struct v4l2_subdev_pad_config
*cfg
,
245 struct v4l2_subdev_mbus_code_enum
*code
)
248 (unsigned int)code
->index
>= ARRAY_SIZE(imx074_colour_fmts
))
251 code
->code
= imx074_colour_fmts
[code
->index
].code
;
255 static int imx074_s_stream(struct v4l2_subdev
*sd
, int enable
)
257 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
259 /* MODE_SELECT: stream or standby */
260 return reg_write(client
, MODE_SELECT
, !!enable
);
263 static int imx074_s_power(struct v4l2_subdev
*sd
, int on
)
265 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
266 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
267 struct imx074
*priv
= to_imx074(client
);
269 return soc_camera_set_power(&client
->dev
, ssdd
, priv
->clk
, on
);
272 static int imx074_g_mbus_config(struct v4l2_subdev
*sd
,
273 struct v4l2_mbus_config
*cfg
)
275 cfg
->type
= V4L2_MBUS_CSI2
;
276 cfg
->flags
= V4L2_MBUS_CSI2_2_LANE
|
277 V4L2_MBUS_CSI2_CHANNEL_0
|
278 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK
;
283 static struct v4l2_subdev_video_ops imx074_subdev_video_ops
= {
284 .s_stream
= imx074_s_stream
,
285 .s_mbus_fmt
= imx074_s_fmt
,
286 .try_mbus_fmt
= imx074_try_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 .s_power
= imx074_s_power
,
296 static const struct v4l2_subdev_pad_ops imx074_subdev_pad_ops
= {
297 .enum_mbus_code
= imx074_enum_mbus_code
,
298 .get_fmt
= imx074_get_fmt
,
301 static struct v4l2_subdev_ops imx074_subdev_ops
= {
302 .core
= &imx074_subdev_core_ops
,
303 .video
= &imx074_subdev_video_ops
,
304 .pad
= &imx074_subdev_pad_ops
,
307 static int imx074_video_probe(struct i2c_client
*client
)
309 struct v4l2_subdev
*subdev
= i2c_get_clientdata(client
);
313 ret
= imx074_s_power(subdev
, 1);
317 /* Read sensor Model ID */
318 ret
= reg_read(client
, 0);
324 ret
= reg_read(client
, 1);
330 dev_info(&client
->dev
, "Chip ID 0x%04x detected\n", id
);
337 /* PLL Setting EXTCLK=24MHz, 22.5times */
338 reg_write(client
, PLL_MULTIPLIER
, 0x2D);
339 reg_write(client
, PRE_PLL_CLK_DIV
, 0x02);
340 reg_write(client
, PLSTATIM
, 0x4B);
343 reg_write(client
, 0x3024, 0x00);
345 reg_write(client
, IMAGE_ORIENTATION
, 0x00);
348 * 0x08+0x08 = top 8 bits
349 * 0x0a+0x08 = compressed 8-bits
350 * 0x0a+0x0a = 10 bits
352 reg_write(client
, 0x0112, 0x08);
353 reg_write(client
, 0x0113, 0x08);
355 /* Base setting for High frame mode */
356 reg_write(client
, VNDMY_ABLMGSHLMT
, 0x80);
357 reg_write(client
, Y_OPBADDR_START_DI
, 0x08);
358 reg_write(client
, 0x3015, 0x37);
359 reg_write(client
, 0x301C, 0x01);
360 reg_write(client
, 0x302C, 0x05);
361 reg_write(client
, 0x3031, 0x26);
362 reg_write(client
, 0x3041, 0x60);
363 reg_write(client
, 0x3051, 0x24);
364 reg_write(client
, 0x3053, 0x34);
365 reg_write(client
, 0x3057, 0xC0);
366 reg_write(client
, 0x305C, 0x09);
367 reg_write(client
, 0x305D, 0x07);
368 reg_write(client
, 0x3060, 0x30);
369 reg_write(client
, 0x3065, 0x00);
370 reg_write(client
, 0x30AA, 0x08);
371 reg_write(client
, 0x30AB, 0x1C);
372 reg_write(client
, 0x30B0, 0x32);
373 reg_write(client
, 0x30B2, 0x83);
374 reg_write(client
, 0x30D3, 0x04);
375 reg_write(client
, 0x3106, 0x78);
376 reg_write(client
, 0x310C, 0x82);
377 reg_write(client
, 0x3304, 0x05);
378 reg_write(client
, 0x3305, 0x04);
379 reg_write(client
, 0x3306, 0x11);
380 reg_write(client
, 0x3307, 0x02);
381 reg_write(client
, 0x3308, 0x0C);
382 reg_write(client
, 0x3309, 0x06);
383 reg_write(client
, 0x330A, 0x08);
384 reg_write(client
, 0x330B, 0x04);
385 reg_write(client
, 0x330C, 0x08);
386 reg_write(client
, 0x330D, 0x06);
387 reg_write(client
, 0x330E, 0x01);
388 reg_write(client
, 0x3381, 0x00);
390 /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */
391 /* 1608 = 1560 + 48 (black lines) */
392 reg_write(client
, FRAME_LENGTH_LINES_HI
, 0x06);
393 reg_write(client
, FRAME_LENGTH_LINES_LO
, 0x48);
394 reg_write(client
, YADDR_START
, 0x00);
395 reg_write(client
, YADDR_END
, 0x2F);
397 reg_write(client
, X_OUTPUT_SIZE_MSB
, 0x08);
398 reg_write(client
, X_OUTPUT_SIZE_LSB
, 0x38);
400 reg_write(client
, Y_OUTPUT_SIZE_MSB
, 0x06);
401 reg_write(client
, Y_OUTPUT_SIZE_LSB
, 0x18);
402 reg_write(client
, X_EVEN_INC
, 0x01);
403 reg_write(client
, X_ODD_INC
, 0x03);
404 reg_write(client
, Y_EVEN_INC
, 0x01);
405 reg_write(client
, Y_ODD_INC
, 0x03);
406 reg_write(client
, HMODEADD
, 0x00);
407 reg_write(client
, VMODEADD
, 0x16);
408 reg_write(client
, VAPPLINE_START
, 0x24);
409 reg_write(client
, VAPPLINE_END
, 0x53);
410 reg_write(client
, SHUTTER
, 0x00);
411 reg_write(client
, HADDAVE
, 0x80);
413 reg_write(client
, LANESEL
, 0x00);
415 reg_write(client
, GROUPED_PARAMETER_HOLD
, 0x00); /* off */
420 imx074_s_power(subdev
, 0);
424 static int imx074_probe(struct i2c_client
*client
,
425 const struct i2c_device_id
*did
)
428 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
429 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
433 dev_err(&client
->dev
, "IMX074: missing platform data!\n");
437 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
438 dev_warn(&adapter
->dev
,
439 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
443 priv
= devm_kzalloc(&client
->dev
, sizeof(struct imx074
), GFP_KERNEL
);
447 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &imx074_subdev_ops
);
449 priv
->fmt
= &imx074_colour_fmts
[0];
451 priv
->clk
= v4l2_clk_get(&client
->dev
, "mclk");
452 if (IS_ERR(priv
->clk
)) {
453 dev_info(&client
->dev
, "Error %ld getting clock\n", PTR_ERR(priv
->clk
));
454 return -EPROBE_DEFER
;
457 ret
= soc_camera_power_init(&client
->dev
, ssdd
);
461 ret
= imx074_video_probe(client
);
465 ret
= v4l2_async_register_subdev(&priv
->subdev
);
471 v4l2_clk_put(priv
->clk
);
475 static int imx074_remove(struct i2c_client
*client
)
477 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
478 struct imx074
*priv
= to_imx074(client
);
480 v4l2_async_unregister_subdev(&priv
->subdev
);
481 v4l2_clk_put(priv
->clk
);
484 ssdd
->free_bus(ssdd
);
489 static const struct i2c_device_id imx074_id
[] = {
493 MODULE_DEVICE_TABLE(i2c
, imx074_id
);
495 static struct i2c_driver imx074_i2c_driver
= {
499 .probe
= imx074_probe
,
500 .remove
= imx074_remove
,
501 .id_table
= imx074_id
,
504 module_i2c_driver(imx074_i2c_driver
);
506 MODULE_DESCRIPTION("Sony IMX074 Camera driver");
507 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
508 MODULE_LICENSE("GPL v2");